blob: 897f10808889b73198c426f4d3ef708e5d9c54a0 [file] [log] [blame]
Clement Courbetac74acd2018-04-04 11:37:06 +00001//===-- Uops.cpp ------------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Uops.h"
Clement Courbet0e69e2d2018-05-17 10:52:18 +000011
12#include "Assembler.h"
13#include "BenchmarkRunner.h"
14#include "MCInstrDescView.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000015#include "PerfHelper.h"
Clement Courbet0e69e2d2018-05-17 10:52:18 +000016
17// FIXME: Load constants into registers (e.g. with fld1) to not break
18// instructions like x87.
19
20// Ideally we would like the only limitation on executing uops to be the issue
21// ports. Maximizing port pressure increases the likelihood that the load is
22// distributed evenly across possible ports.
23
24// To achieve that, one approach is to generate instructions that do not have
25// data dependencies between them.
26//
27// For some instructions, this is trivial:
28// mov rax, qword ptr [rsi]
29// mov rax, qword ptr [rsi]
30// mov rax, qword ptr [rsi]
31// mov rax, qword ptr [rsi]
32// For the above snippet, haswell just renames rax four times and executes the
33// four instructions two at a time on P23 and P0126.
34//
35// For some instructions, we just need to make sure that the source is
36// different from the destination. For example, IDIV8r reads from GPR and
37// writes to AX. We just need to ensure that the Var is assigned a
38// register which is different from AX:
39// idiv bx
40// idiv bx
41// idiv bx
42// idiv bx
43// The above snippet will be able to fully saturate the ports, while the same
44// with ax would issue one uop every `latency(IDIV8r)` cycles.
45//
46// Some instructions make this harder because they both read and write from
47// the same register:
48// inc rax
49// inc rax
50// inc rax
51// inc rax
52// This has a data dependency from each instruction to the next, limit the
53// number of instructions that can be issued in parallel.
54// It turns out that this is not a big issue on recent Intel CPUs because they
55// have heuristics to balance port pressure. In the snippet above, subsequent
56// instructions will end up evenly distributed on {P0,P1,P5,P6}, but some CPUs
57// might end up executing them all on P0 (just because they can), or try
58// avoiding P5 because it's usually under high pressure from vector
59// instructions.
60// This issue is even more important for high-latency instructions because
61// they increase the idle time of the CPU, e.g. :
62// imul rax, rbx
63// imul rax, rbx
64// imul rax, rbx
65// imul rax, rbx
66//
67// To avoid that, we do the renaming statically by generating as many
68// independent exclusive assignments as possible (until all possible registers
69// are exhausted) e.g.:
70// imul rax, rbx
71// imul rcx, rbx
72// imul rdx, rbx
73// imul r8, rbx
74//
75// Some instruction even make the above static renaming impossible because
76// they implicitly read and write from the same operand, e.g. ADC16rr reads
77// and writes from EFLAGS.
78// In that case we just use a greedy register assignment and hope for the
79// best.
Clement Courbetac74acd2018-04-04 11:37:06 +000080
81namespace exegesis {
82
Clement Courbet0e69e2d2018-05-17 10:52:18 +000083static bool hasUnknownOperand(const llvm::MCOperandInfo &OpInfo) {
84 return OpInfo.OperandType == llvm::MCOI::OPERAND_UNKNOWN;
85}
86
87// FIXME: Handle memory, see PR36905.
88static bool hasMemoryOperand(const llvm::MCOperandInfo &OpInfo) {
89 return OpInfo.OperandType == llvm::MCOI::OPERAND_MEMORY;
90}
91
Guillaume Chateletc9f727b2018-06-13 13:24:41 +000092llvm::Error
93UopsBenchmarkRunner::isInfeasible(const llvm::MCInstrDesc &MCInstrDesc) const {
Guillaume Chateletc9f727b2018-06-13 13:24:41 +000094 if (llvm::any_of(MCInstrDesc.operands(), hasUnknownOperand))
95 return llvm::make_error<BenchmarkFailure>(
96 "Infeasible : has unknown operands");
97 if (llvm::any_of(MCInstrDesc.operands(), hasMemoryOperand))
Clement Courbetcff2caa2018-06-25 11:22:23 +000098 return llvm::make_error<BenchmarkFailure>(
99 "Infeasible : has memory operands");
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000100 return llvm::Error::success();
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000101}
102
103// Returns whether this Variable ties Use and Def operands together.
Guillaume Chateletef6cef52018-06-20 08:52:30 +0000104static bool hasTiedOperands(const Instruction &Instr, const Variable &Var) {
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000105 bool HasUse = false;
106 bool HasDef = false;
Guillaume Chateletef6cef52018-06-20 08:52:30 +0000107 for (const unsigned OpIndex : Var.TiedOperands) {
108 const Operand &Op = Instr.Operands[OpIndex];
109 if (Op.IsDef)
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000110 HasDef = true;
111 else
112 HasUse = true;
113 }
114 return HasUse && HasDef;
115}
116
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000117static llvm::SmallVector<const Variable *, 8>
118getTiedVariables(const Instruction &Instr) {
119 llvm::SmallVector<const Variable *, 8> Result;
120 for (const auto &Var : Instr.Variables)
Guillaume Chateletef6cef52018-06-20 08:52:30 +0000121 if (hasTiedOperands(Instr, Var))
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000122 Result.push_back(&Var);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000123 return Result;
124}
125
126static void remove(llvm::BitVector &a, const llvm::BitVector &b) {
127 assert(a.size() == b.size());
128 for (auto I : b.set_bits())
129 a.reset(I);
Clement Courbetac74acd2018-04-04 11:37:06 +0000130}
131
Clement Courbetac74acd2018-04-04 11:37:06 +0000132UopsBenchmarkRunner::~UopsBenchmarkRunner() = default;
133
Guillaume Chateletef6cef52018-06-20 08:52:30 +0000134llvm::Expected<SnippetPrototype>
135UopsBenchmarkRunner::generatePrototype(unsigned Opcode) const {
Clement Courbet0e8bf4e2018-06-25 13:44:27 +0000136 const auto &InstrDesc = State.getInstrInfo().get(Opcode);
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000137 if (auto E = isInfeasible(InstrDesc))
138 return std::move(E);
139 const Instruction Instr(InstrDesc, RATC);
140 const AliasingConfigurations SelfAliasing(Instr, Instr);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000141 if (SelfAliasing.empty()) {
Clement Courbetf9a0bb32018-07-05 13:54:51 +0000142 return generateUnconstrainedPrototype(Instr, "instruction is parallel");
Clement Courbetac74acd2018-04-04 11:37:06 +0000143 }
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000144 if (SelfAliasing.hasImplicitAliasing()) {
Clement Courbetf9a0bb32018-07-05 13:54:51 +0000145 return generateUnconstrainedPrototype(Instr, "instruction is serial");
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000146 }
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000147 const auto TiedVariables = getTiedVariables(Instr);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000148 if (!TiedVariables.empty()) {
Guillaume Chateletb4f15822018-06-07 14:00:29 +0000149 if (TiedVariables.size() > 1)
150 return llvm::make_error<llvm::StringError>(
151 "Infeasible : don't know how to handle several tied variables",
152 llvm::inconvertibleErrorCode());
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000153 const Variable *Var = TiedVariables.front();
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000154 assert(Var);
155 assert(!Var->TiedOperands.empty());
Guillaume Chateletef6cef52018-06-20 08:52:30 +0000156 const Operand &Op = Instr.Operands[Var->TiedOperands.front()];
157 assert(Op.Tracker);
158 SnippetPrototype Prototype;
159 Prototype.Explanation =
160 "instruction has tied variables using static renaming.";
161 for (const llvm::MCPhysReg Reg : Op.Tracker->sourceBits().set_bits()) {
162 Prototype.Snippet.emplace_back(Instr);
163 Prototype.Snippet.back().getValueFor(*Var) =
164 llvm::MCOperand::createReg(Reg);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000165 }
Clement Courbet2c409702018-06-20 09:18:32 +0000166 return std::move(Prototype);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000167 }
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000168 InstructionInstance II(Instr);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000169 // No tied variables, we pick random values for defs.
Clement Courbet0e8bf4e2018-06-25 13:44:27 +0000170 llvm::BitVector Defs(State.getRegInfo().getNumRegs());
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000171 for (const auto &Op : Instr.Operands) {
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000172 if (Op.Tracker && Op.IsExplicit && Op.IsDef) {
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000173 auto PossibleRegisters = Op.Tracker->sourceBits();
174 remove(PossibleRegisters, RATC.reservedRegisters());
175 assert(PossibleRegisters.any() && "No register left to choose from");
176 const auto RandomReg = randomBit(PossibleRegisters);
177 Defs.set(RandomReg);
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000178 II.getValueFor(Op) = llvm::MCOperand::createReg(RandomReg);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000179 }
180 }
181 // And pick random use values that are not reserved and don't alias with defs.
Clement Courbet0e8bf4e2018-06-25 13:44:27 +0000182 const auto DefAliases = getAliasedBits(State.getRegInfo(), Defs);
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000183 for (const auto &Op : Instr.Operands) {
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000184 if (Op.Tracker && Op.IsExplicit && !Op.IsDef) {
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000185 auto PossibleRegisters = Op.Tracker->sourceBits();
186 remove(PossibleRegisters, RATC.reservedRegisters());
187 remove(PossibleRegisters, DefAliases);
188 assert(PossibleRegisters.any() && "No register left to choose from");
189 const auto RandomReg = randomBit(PossibleRegisters);
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000190 II.getValueFor(Op) = llvm::MCOperand::createReg(RandomReg);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000191 }
192 }
Guillaume Chateletef6cef52018-06-20 08:52:30 +0000193 SnippetPrototype Prototype;
194 Prototype.Explanation =
Guillaume Chateletb4f15822018-06-07 14:00:29 +0000195 "instruction has no tied variables picking Uses different from defs";
Guillaume Chateletef6cef52018-06-20 08:52:30 +0000196 Prototype.Snippet.push_back(std::move(II));
Clement Courbet2c409702018-06-20 09:18:32 +0000197 return std::move(Prototype);
Clement Courbetac74acd2018-04-04 11:37:06 +0000198}
199
200std::vector<BenchmarkMeasure>
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000201UopsBenchmarkRunner::runMeasurements(const ExecutableFunction &Function,
Clement Courbetac74acd2018-04-04 11:37:06 +0000202 const unsigned NumRepetitions) const {
203 const auto &SchedModel = State.getSubtargetInfo().getSchedModel();
204
205 std::vector<BenchmarkMeasure> Result;
206 for (unsigned ProcResIdx = 1;
207 ProcResIdx < SchedModel.getNumProcResourceKinds(); ++ProcResIdx) {
Clement Courbetb4493792018-04-10 08:16:37 +0000208 const char *const PfmCounters = SchedModel.getExtraProcessorInfo()
209 .PfmCounters.IssueCounters[ProcResIdx];
210 if (!PfmCounters)
Clement Courbetac74acd2018-04-04 11:37:06 +0000211 continue;
Clement Courbet38275372018-06-12 13:28:37 +0000212 // We sum counts when there are several counters for a single ProcRes
Clement Courbetb4493792018-04-10 08:16:37 +0000213 // (e.g. P23 on SandyBridge).
Clement Courbet38275372018-06-12 13:28:37 +0000214 int64_t CounterValue = 0;
215 llvm::SmallVector<llvm::StringRef, 2> CounterNames;
216 llvm::StringRef(PfmCounters).split(CounterNames, ',');
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000217 for (const auto &CounterName : CounterNames) {
Clement Courbet38275372018-06-12 13:28:37 +0000218 pfm::PerfEvent UopPerfEvent(CounterName);
219 if (!UopPerfEvent.valid())
220 llvm::report_fatal_error(
221 llvm::Twine("invalid perf event ").concat(PfmCounters));
222 pfm::Counter Counter(UopPerfEvent);
223 Counter.start();
224 Function();
225 Counter.stop();
226 CounterValue += Counter.read();
227 }
Clement Courbetac74acd2018-04-04 11:37:06 +0000228 Result.push_back({llvm::itostr(ProcResIdx),
Clement Courbet38275372018-06-12 13:28:37 +0000229 static_cast<double>(CounterValue) / NumRepetitions,
Clement Courbetb4493792018-04-10 08:16:37 +0000230 SchedModel.getProcResource(ProcResIdx)->Name});
Clement Courbetac74acd2018-04-04 11:37:06 +0000231 }
232 return Result;
233}
234
235} // namespace exegesis