blob: d7346de85a08e8a36eed3fcf410d9f75a1bdb270 [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"
Guillaume Chateletfb943542018-08-01 14:41:45 +000016#include "Target.h"
Clement Courbet0e69e2d2018-05-17 10:52:18 +000017
18// FIXME: Load constants into registers (e.g. with fld1) to not break
19// instructions like x87.
20
21// Ideally we would like the only limitation on executing uops to be the issue
22// ports. Maximizing port pressure increases the likelihood that the load is
23// distributed evenly across possible ports.
24
25// To achieve that, one approach is to generate instructions that do not have
26// data dependencies between them.
27//
28// For some instructions, this is trivial:
29// mov rax, qword ptr [rsi]
30// mov rax, qword ptr [rsi]
31// mov rax, qword ptr [rsi]
32// mov rax, qword ptr [rsi]
33// For the above snippet, haswell just renames rax four times and executes the
34// four instructions two at a time on P23 and P0126.
35//
36// For some instructions, we just need to make sure that the source is
37// different from the destination. For example, IDIV8r reads from GPR and
38// writes to AX. We just need to ensure that the Var is assigned a
39// register which is different from AX:
40// idiv bx
41// idiv bx
42// idiv bx
43// idiv bx
44// The above snippet will be able to fully saturate the ports, while the same
45// with ax would issue one uop every `latency(IDIV8r)` cycles.
46//
47// Some instructions make this harder because they both read and write from
48// the same register:
49// inc rax
50// inc rax
51// inc rax
52// inc rax
53// This has a data dependency from each instruction to the next, limit the
54// number of instructions that can be issued in parallel.
55// It turns out that this is not a big issue on recent Intel CPUs because they
56// have heuristics to balance port pressure. In the snippet above, subsequent
57// instructions will end up evenly distributed on {P0,P1,P5,P6}, but some CPUs
58// might end up executing them all on P0 (just because they can), or try
59// avoiding P5 because it's usually under high pressure from vector
60// instructions.
61// This issue is even more important for high-latency instructions because
62// they increase the idle time of the CPU, e.g. :
63// imul rax, rbx
64// imul rax, rbx
65// imul rax, rbx
66// imul rax, rbx
67//
68// To avoid that, we do the renaming statically by generating as many
69// independent exclusive assignments as possible (until all possible registers
70// are exhausted) e.g.:
71// imul rax, rbx
72// imul rcx, rbx
73// imul rdx, rbx
74// imul r8, rbx
75//
76// Some instruction even make the above static renaming impossible because
77// they implicitly read and write from the same operand, e.g. ADC16rr reads
78// and writes from EFLAGS.
79// In that case we just use a greedy register assignment and hope for the
80// best.
Clement Courbetac74acd2018-04-04 11:37:06 +000081
82namespace exegesis {
83
Clement Courbet0e69e2d2018-05-17 10:52:18 +000084static bool hasUnknownOperand(const llvm::MCOperandInfo &OpInfo) {
85 return OpInfo.OperandType == llvm::MCOI::OPERAND_UNKNOWN;
86}
87
Guillaume Chateletc9f727b2018-06-13 13:24:41 +000088llvm::Error
89UopsBenchmarkRunner::isInfeasible(const llvm::MCInstrDesc &MCInstrDesc) const {
Guillaume Chateletc9f727b2018-06-13 13:24:41 +000090 if (llvm::any_of(MCInstrDesc.operands(), hasUnknownOperand))
91 return llvm::make_error<BenchmarkFailure>(
92 "Infeasible : has unknown operands");
Guillaume Chateletc9f727b2018-06-13 13:24:41 +000093 return llvm::Error::success();
Clement Courbet0e69e2d2018-05-17 10:52:18 +000094}
95
96// Returns whether this Variable ties Use and Def operands together.
Guillaume Chateletef6cef52018-06-20 08:52:30 +000097static bool hasTiedOperands(const Instruction &Instr, const Variable &Var) {
Clement Courbet0e69e2d2018-05-17 10:52:18 +000098 bool HasUse = false;
99 bool HasDef = false;
Guillaume Chateletef6cef52018-06-20 08:52:30 +0000100 for (const unsigned OpIndex : Var.TiedOperands) {
101 const Operand &Op = Instr.Operands[OpIndex];
102 if (Op.IsDef)
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000103 HasDef = true;
104 else
105 HasUse = true;
106 }
107 return HasUse && HasDef;
108}
109
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000110static llvm::SmallVector<const Variable *, 8>
111getTiedVariables(const Instruction &Instr) {
112 llvm::SmallVector<const Variable *, 8> Result;
113 for (const auto &Var : Instr.Variables)
Guillaume Chateletef6cef52018-06-20 08:52:30 +0000114 if (hasTiedOperands(Instr, Var))
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000115 Result.push_back(&Var);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000116 return Result;
117}
118
119static void remove(llvm::BitVector &a, const llvm::BitVector &b) {
120 assert(a.size() == b.size());
121 for (auto I : b.set_bits())
122 a.reset(I);
Clement Courbetac74acd2018-04-04 11:37:06 +0000123}
124
Clement Courbetac74acd2018-04-04 11:37:06 +0000125UopsBenchmarkRunner::~UopsBenchmarkRunner() = default;
126
Guillaume Chateletfb943542018-08-01 14:41:45 +0000127void UopsBenchmarkRunner::instantiateMemoryOperands(
Guillaume Chatelete60866a2018-08-03 09:29:38 +0000128 const unsigned ScratchSpacePointerInReg,
129 std::vector<InstructionBuilder> &Instructions) const {
130 if (ScratchSpacePointerInReg == 0)
Guillaume Chatelet171f3f42018-08-02 11:12:02 +0000131 return; // no memory operands.
Guillaume Chateletfb943542018-08-01 14:41:45 +0000132 const auto &ET = State.getExegesisTarget();
133 const unsigned MemStep = ET.getMaxMemoryAccessSize();
Guillaume Chatelete60866a2018-08-03 09:29:38 +0000134 const size_t OriginalInstructionsSize = Instructions.size();
Guillaume Chateletfb943542018-08-01 14:41:45 +0000135 size_t I = 0;
Guillaume Chatelete60866a2018-08-03 09:29:38 +0000136 for (InstructionBuilder &IB : Instructions) {
137 ET.fillMemoryOperands(IB, ScratchSpacePointerInReg, I * MemStep);
Guillaume Chateletfb943542018-08-01 14:41:45 +0000138 ++I;
139 }
140
Guillaume Chatelete60866a2018-08-03 09:29:38 +0000141 while (Instructions.size() < kMinNumDifferentAddresses) {
142 InstructionBuilder IB = Instructions[I % OriginalInstructionsSize];
143 ET.fillMemoryOperands(IB, ScratchSpacePointerInReg, I * MemStep);
Guillaume Chateletfb943542018-08-01 14:41:45 +0000144 ++I;
Guillaume Chatelete60866a2018-08-03 09:29:38 +0000145 Instructions.push_back(std::move(IB));
Guillaume Chateletfb943542018-08-01 14:41:45 +0000146 }
147 assert(I * MemStep < ScratchSpace::kSize && "not enough scratch space");
148}
149
Guillaume Chatelete60866a2018-08-03 09:29:38 +0000150llvm::Expected<CodeTemplate>
151UopsBenchmarkRunner::generateCodeTemplate(unsigned Opcode) const {
Clement Courbet0e8bf4e2018-06-25 13:44:27 +0000152 const auto &InstrDesc = State.getInstrInfo().get(Opcode);
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000153 if (auto E = isInfeasible(InstrDesc))
154 return std::move(E);
155 const Instruction Instr(InstrDesc, RATC);
Guillaume Chateletfb943542018-08-01 14:41:45 +0000156 const auto &ET = State.getExegesisTarget();
Guillaume Chatelete60866a2018-08-03 09:29:38 +0000157 CodeTemplate CT;
Guillaume Chateletfb943542018-08-01 14:41:45 +0000158
159 const llvm::BitVector *ScratchSpaceAliasedRegs = nullptr;
160 if (Instr.hasMemoryOperands()) {
Guillaume Chatelete60866a2018-08-03 09:29:38 +0000161 CT.ScratchSpacePointerInReg =
Guillaume Chateletfb943542018-08-01 14:41:45 +0000162 ET.getScratchMemoryRegister(State.getTargetMachine().getTargetTriple());
Guillaume Chatelete60866a2018-08-03 09:29:38 +0000163 if (CT.ScratchSpacePointerInReg == 0)
Guillaume Chateletfb943542018-08-01 14:41:45 +0000164 return llvm::make_error<BenchmarkFailure>(
165 "Infeasible : target does not support memory instructions");
166 ScratchSpaceAliasedRegs =
Guillaume Chatelete60866a2018-08-03 09:29:38 +0000167 &RATC.getRegister(CT.ScratchSpacePointerInReg).aliasedBits();
168 // If the instruction implicitly writes to ScratchSpacePointerInReg , abort.
Guillaume Chateletfb943542018-08-01 14:41:45 +0000169 // FIXME: We could make a copy of the scratch register.
170 for (const auto &Op : Instr.Operands) {
171 if (Op.IsDef && Op.ImplicitReg &&
172 ScratchSpaceAliasedRegs->test(*Op.ImplicitReg))
173 return llvm::make_error<BenchmarkFailure>(
174 "Infeasible : memory instruction uses scratch memory register");
175 }
176 }
177
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000178 const AliasingConfigurations SelfAliasing(Instr, Instr);
Guillaume Chatelet171f3f42018-08-02 11:12:02 +0000179 InstructionBuilder IB(Instr);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000180 if (SelfAliasing.empty()) {
Guillaume Chatelete60866a2018-08-03 09:29:38 +0000181 CT.Info = "instruction is parallel, repeating a random one.";
182 CT.Instructions.push_back(std::move(IB));
183 instantiateMemoryOperands(CT.ScratchSpacePointerInReg, CT.Instructions);
184 return std::move(CT);
Clement Courbetac74acd2018-04-04 11:37:06 +0000185 }
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000186 if (SelfAliasing.hasImplicitAliasing()) {
Guillaume Chatelete60866a2018-08-03 09:29:38 +0000187 CT.Info = "instruction is serial, repeating a random one.";
188 CT.Instructions.push_back(std::move(IB));
189 instantiateMemoryOperands(CT.ScratchSpacePointerInReg, CT.Instructions);
190 return std::move(CT);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000191 }
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000192 const auto TiedVariables = getTiedVariables(Instr);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000193 if (!TiedVariables.empty()) {
Guillaume Chateletb4f15822018-06-07 14:00:29 +0000194 if (TiedVariables.size() > 1)
195 return llvm::make_error<llvm::StringError>(
196 "Infeasible : don't know how to handle several tied variables",
197 llvm::inconvertibleErrorCode());
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000198 const Variable *Var = TiedVariables.front();
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000199 assert(Var);
200 assert(!Var->TiedOperands.empty());
Guillaume Chateletef6cef52018-06-20 08:52:30 +0000201 const Operand &Op = Instr.Operands[Var->TiedOperands.front()];
202 assert(Op.Tracker);
Guillaume Chatelete60866a2018-08-03 09:29:38 +0000203 CT.Info = "instruction has tied variables using static renaming.";
Guillaume Chateletef6cef52018-06-20 08:52:30 +0000204 for (const llvm::MCPhysReg Reg : Op.Tracker->sourceBits().set_bits()) {
Guillaume Chateletfb943542018-08-01 14:41:45 +0000205 if (ScratchSpaceAliasedRegs && ScratchSpaceAliasedRegs->test(Reg))
206 continue; // Do not use the scratch memory address register.
Guillaume Chatelet171f3f42018-08-02 11:12:02 +0000207 InstructionBuilder TmpIB = IB;
208 TmpIB.getValueFor(*Var) = llvm::MCOperand::createReg(Reg);
Guillaume Chatelete60866a2018-08-03 09:29:38 +0000209 CT.Instructions.push_back(std::move(TmpIB));
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000210 }
Guillaume Chatelete60866a2018-08-03 09:29:38 +0000211 instantiateMemoryOperands(CT.ScratchSpacePointerInReg, CT.Instructions);
212 return std::move(CT);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000213 }
214 // No tied variables, we pick random values for defs.
Clement Courbet0e8bf4e2018-06-25 13:44:27 +0000215 llvm::BitVector Defs(State.getRegInfo().getNumRegs());
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000216 for (const auto &Op : Instr.Operands) {
Guillaume Chateletfb943542018-08-01 14:41:45 +0000217 if (Op.Tracker && Op.IsExplicit && Op.IsDef && !Op.IsMem) {
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000218 auto PossibleRegisters = Op.Tracker->sourceBits();
219 remove(PossibleRegisters, RATC.reservedRegisters());
Guillaume Chateletfb943542018-08-01 14:41:45 +0000220 // Do not use the scratch memory address register.
221 if (ScratchSpaceAliasedRegs)
222 remove(PossibleRegisters, *ScratchSpaceAliasedRegs);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000223 assert(PossibleRegisters.any() && "No register left to choose from");
224 const auto RandomReg = randomBit(PossibleRegisters);
225 Defs.set(RandomReg);
Guillaume Chatelet171f3f42018-08-02 11:12:02 +0000226 IB.getValueFor(Op) = llvm::MCOperand::createReg(RandomReg);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000227 }
228 }
229 // And pick random use values that are not reserved and don't alias with defs.
Clement Courbet0e8bf4e2018-06-25 13:44:27 +0000230 const auto DefAliases = getAliasedBits(State.getRegInfo(), Defs);
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000231 for (const auto &Op : Instr.Operands) {
Guillaume Chateletfb943542018-08-01 14:41:45 +0000232 if (Op.Tracker && Op.IsExplicit && !Op.IsDef && !Op.IsMem) {
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000233 auto PossibleRegisters = Op.Tracker->sourceBits();
234 remove(PossibleRegisters, RATC.reservedRegisters());
Guillaume Chateletfb943542018-08-01 14:41:45 +0000235 // Do not use the scratch memory address register.
236 if (ScratchSpaceAliasedRegs)
237 remove(PossibleRegisters, *ScratchSpaceAliasedRegs);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000238 remove(PossibleRegisters, DefAliases);
239 assert(PossibleRegisters.any() && "No register left to choose from");
240 const auto RandomReg = randomBit(PossibleRegisters);
Guillaume Chatelet171f3f42018-08-02 11:12:02 +0000241 IB.getValueFor(Op) = llvm::MCOperand::createReg(RandomReg);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000242 }
243 }
Guillaume Chatelete60866a2018-08-03 09:29:38 +0000244 CT.Info =
Guillaume Chateletb4f15822018-06-07 14:00:29 +0000245 "instruction has no tied variables picking Uses different from defs";
Guillaume Chatelete60866a2018-08-03 09:29:38 +0000246 CT.Instructions.push_back(std::move(IB));
247 instantiateMemoryOperands(CT.ScratchSpacePointerInReg, CT.Instructions);
248 return std::move(CT);
Clement Courbetac74acd2018-04-04 11:37:06 +0000249}
250
251std::vector<BenchmarkMeasure>
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000252UopsBenchmarkRunner::runMeasurements(const ExecutableFunction &Function,
Guillaume Chateletfb943542018-08-01 14:41:45 +0000253 ScratchSpace &Scratch,
Clement Courbetac74acd2018-04-04 11:37:06 +0000254 const unsigned NumRepetitions) const {
255 const auto &SchedModel = State.getSubtargetInfo().getSchedModel();
256
257 std::vector<BenchmarkMeasure> Result;
258 for (unsigned ProcResIdx = 1;
259 ProcResIdx < SchedModel.getNumProcResourceKinds(); ++ProcResIdx) {
Clement Courbetb4493792018-04-10 08:16:37 +0000260 const char *const PfmCounters = SchedModel.getExtraProcessorInfo()
261 .PfmCounters.IssueCounters[ProcResIdx];
262 if (!PfmCounters)
Clement Courbetac74acd2018-04-04 11:37:06 +0000263 continue;
Clement Courbet38275372018-06-12 13:28:37 +0000264 // We sum counts when there are several counters for a single ProcRes
Clement Courbetb4493792018-04-10 08:16:37 +0000265 // (e.g. P23 on SandyBridge).
Clement Courbet38275372018-06-12 13:28:37 +0000266 int64_t CounterValue = 0;
267 llvm::SmallVector<llvm::StringRef, 2> CounterNames;
268 llvm::StringRef(PfmCounters).split(CounterNames, ',');
Guillaume Chateletc9f727b2018-06-13 13:24:41 +0000269 for (const auto &CounterName : CounterNames) {
Clement Courbet38275372018-06-12 13:28:37 +0000270 pfm::PerfEvent UopPerfEvent(CounterName);
271 if (!UopPerfEvent.valid())
272 llvm::report_fatal_error(
273 llvm::Twine("invalid perf event ").concat(PfmCounters));
274 pfm::Counter Counter(UopPerfEvent);
Guillaume Chateletfb943542018-08-01 14:41:45 +0000275 Scratch.clear();
Clement Courbet38275372018-06-12 13:28:37 +0000276 Counter.start();
Guillaume Chateletfb943542018-08-01 14:41:45 +0000277 Function(Scratch.ptr());
Clement Courbet38275372018-06-12 13:28:37 +0000278 Counter.stop();
279 CounterValue += Counter.read();
280 }
Clement Courbetac74acd2018-04-04 11:37:06 +0000281 Result.push_back({llvm::itostr(ProcResIdx),
Clement Courbet38275372018-06-12 13:28:37 +0000282 static_cast<double>(CounterValue) / NumRepetitions,
Clement Courbetb4493792018-04-10 08:16:37 +0000283 SchedModel.getProcResource(ProcResIdx)->Name});
Clement Courbetac74acd2018-04-04 11:37:06 +0000284 }
285 return Result;
286}
287
Guillaume Chateletfb943542018-08-01 14:41:45 +0000288constexpr const size_t UopsBenchmarkRunner::kMinNumDifferentAddresses;
289
Clement Courbetac74acd2018-04-04 11:37:06 +0000290} // namespace exegesis