Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 1 | //===-- 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 Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 11 | |
| 12 | #include "Assembler.h" |
| 13 | #include "BenchmarkRunner.h" |
| 14 | #include "MCInstrDescView.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 15 | #include "PerfHelper.h" |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 16 | |
| 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 Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 80 | |
| 81 | namespace exegesis { |
| 82 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 83 | static bool hasUnknownOperand(const llvm::MCOperandInfo &OpInfo) { |
| 84 | return OpInfo.OperandType == llvm::MCOI::OPERAND_UNKNOWN; |
| 85 | } |
| 86 | |
| 87 | // FIXME: Handle memory, see PR36905. |
| 88 | static bool hasMemoryOperand(const llvm::MCOperandInfo &OpInfo) { |
| 89 | return OpInfo.OperandType == llvm::MCOI::OPERAND_MEMORY; |
| 90 | } |
| 91 | |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 92 | llvm::Error |
| 93 | UopsBenchmarkRunner::isInfeasible(const llvm::MCInstrDesc &MCInstrDesc) const { |
| 94 | if (MCInstrDesc.isPseudo()) |
| 95 | return llvm::make_error<BenchmarkFailure>("Infeasible : is pseudo"); |
| 96 | if (llvm::any_of(MCInstrDesc.operands(), hasUnknownOperand)) |
| 97 | return llvm::make_error<BenchmarkFailure>( |
| 98 | "Infeasible : has unknown operands"); |
| 99 | if (llvm::any_of(MCInstrDesc.operands(), hasMemoryOperand)) |
Clement Courbet | 44b4c54 | 2018-06-19 11:28:59 +0000 | [diff] [blame] | 100 | return llvm::make_error<BenchmarkFailure>("Infeasible : has memory operands"); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 101 | return llvm::Error::success(); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Returns whether this Variable ties Use and Def operands together. |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 105 | static bool hasTiedOperands(const Instruction &Instr, const Variable &Var) { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 106 | bool HasUse = false; |
| 107 | bool HasDef = false; |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 108 | for (const unsigned OpIndex : Var.TiedOperands) { |
| 109 | const Operand &Op = Instr.Operands[OpIndex]; |
| 110 | if (Op.IsDef) |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 111 | HasDef = true; |
| 112 | else |
| 113 | HasUse = true; |
| 114 | } |
| 115 | return HasUse && HasDef; |
| 116 | } |
| 117 | |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 118 | static llvm::SmallVector<const Variable *, 8> |
| 119 | getTiedVariables(const Instruction &Instr) { |
| 120 | llvm::SmallVector<const Variable *, 8> Result; |
| 121 | for (const auto &Var : Instr.Variables) |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 122 | if (hasTiedOperands(Instr, Var)) |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 123 | Result.push_back(&Var); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 124 | return Result; |
| 125 | } |
| 126 | |
| 127 | static void remove(llvm::BitVector &a, const llvm::BitVector &b) { |
| 128 | assert(a.size() == b.size()); |
| 129 | for (auto I : b.set_bits()) |
| 130 | a.reset(I); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 133 | UopsBenchmarkRunner::~UopsBenchmarkRunner() = default; |
| 134 | |
Clement Courbet | 62b34fa | 2018-06-06 09:42:36 +0000 | [diff] [blame] | 135 | InstructionBenchmark::ModeE UopsBenchmarkRunner::getMode() const { |
| 136 | return InstructionBenchmark::Uops; |
Clement Courbet | 2cb97b9 | 2018-06-04 11:43:40 +0000 | [diff] [blame] | 137 | } |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 138 | |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 139 | llvm::Expected<SnippetPrototype> |
| 140 | UopsBenchmarkRunner::generatePrototype(unsigned Opcode) const { |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 141 | const auto &InstrDesc = MCInstrInfo.get(Opcode); |
| 142 | if (auto E = isInfeasible(InstrDesc)) |
| 143 | return std::move(E); |
| 144 | const Instruction Instr(InstrDesc, RATC); |
| 145 | const AliasingConfigurations SelfAliasing(Instr, Instr); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 146 | if (SelfAliasing.empty()) { |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 147 | SnippetPrototype Prototype; |
| 148 | Prototype.Explanation = "instruction is parallel, repeating a random one."; |
| 149 | Prototype.Snippet.emplace_back(Instr); |
Clement Courbet | 2c40970 | 2018-06-20 09:18:32 +0000 | [diff] [blame^] | 150 | return std::move(Prototype); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 151 | } |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 152 | if (SelfAliasing.hasImplicitAliasing()) { |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 153 | SnippetPrototype Prototype; |
| 154 | Prototype.Explanation = "instruction is serial, repeating a random one."; |
| 155 | Prototype.Snippet.emplace_back(Instr); |
Clement Courbet | 2c40970 | 2018-06-20 09:18:32 +0000 | [diff] [blame^] | 156 | return std::move(Prototype); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 157 | } |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 158 | const auto TiedVariables = getTiedVariables(Instr); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 159 | if (!TiedVariables.empty()) { |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 160 | if (TiedVariables.size() > 1) |
| 161 | return llvm::make_error<llvm::StringError>( |
| 162 | "Infeasible : don't know how to handle several tied variables", |
| 163 | llvm::inconvertibleErrorCode()); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 164 | const Variable *Var = TiedVariables.front(); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 165 | assert(Var); |
| 166 | assert(!Var->TiedOperands.empty()); |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 167 | const Operand &Op = Instr.Operands[Var->TiedOperands.front()]; |
| 168 | assert(Op.Tracker); |
| 169 | SnippetPrototype Prototype; |
| 170 | Prototype.Explanation = |
| 171 | "instruction has tied variables using static renaming."; |
| 172 | for (const llvm::MCPhysReg Reg : Op.Tracker->sourceBits().set_bits()) { |
| 173 | Prototype.Snippet.emplace_back(Instr); |
| 174 | Prototype.Snippet.back().getValueFor(*Var) = |
| 175 | llvm::MCOperand::createReg(Reg); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 176 | } |
Clement Courbet | 2c40970 | 2018-06-20 09:18:32 +0000 | [diff] [blame^] | 177 | return std::move(Prototype); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 178 | } |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 179 | InstructionInstance II(Instr); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 180 | // No tied variables, we pick random values for defs. |
| 181 | llvm::BitVector Defs(MCRegisterInfo.getNumRegs()); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 182 | for (const auto &Op : Instr.Operands) { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 183 | if (Op.Tracker && Op.IsExplicit && Op.IsDef) { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 184 | auto PossibleRegisters = Op.Tracker->sourceBits(); |
| 185 | remove(PossibleRegisters, RATC.reservedRegisters()); |
| 186 | assert(PossibleRegisters.any() && "No register left to choose from"); |
| 187 | const auto RandomReg = randomBit(PossibleRegisters); |
| 188 | Defs.set(RandomReg); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 189 | II.getValueFor(Op) = llvm::MCOperand::createReg(RandomReg); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | // And pick random use values that are not reserved and don't alias with defs. |
| 193 | const auto DefAliases = getAliasedBits(MCRegisterInfo, Defs); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 194 | for (const auto &Op : Instr.Operands) { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 195 | if (Op.Tracker && Op.IsExplicit && !Op.IsDef) { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 196 | auto PossibleRegisters = Op.Tracker->sourceBits(); |
| 197 | remove(PossibleRegisters, RATC.reservedRegisters()); |
| 198 | remove(PossibleRegisters, DefAliases); |
| 199 | assert(PossibleRegisters.any() && "No register left to choose from"); |
| 200 | const auto RandomReg = randomBit(PossibleRegisters); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 201 | II.getValueFor(Op) = llvm::MCOperand::createReg(RandomReg); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 204 | SnippetPrototype Prototype; |
| 205 | Prototype.Explanation = |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 206 | "instruction has no tied variables picking Uses different from defs"; |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 207 | Prototype.Snippet.push_back(std::move(II)); |
Clement Courbet | 2c40970 | 2018-06-20 09:18:32 +0000 | [diff] [blame^] | 208 | return std::move(Prototype); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | std::vector<BenchmarkMeasure> |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 212 | UopsBenchmarkRunner::runMeasurements(const ExecutableFunction &Function, |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 213 | const unsigned NumRepetitions) const { |
| 214 | const auto &SchedModel = State.getSubtargetInfo().getSchedModel(); |
| 215 | |
| 216 | std::vector<BenchmarkMeasure> Result; |
| 217 | for (unsigned ProcResIdx = 1; |
| 218 | ProcResIdx < SchedModel.getNumProcResourceKinds(); ++ProcResIdx) { |
Clement Courbet | b449379 | 2018-04-10 08:16:37 +0000 | [diff] [blame] | 219 | const char *const PfmCounters = SchedModel.getExtraProcessorInfo() |
| 220 | .PfmCounters.IssueCounters[ProcResIdx]; |
| 221 | if (!PfmCounters) |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 222 | continue; |
Clement Courbet | 3827537 | 2018-06-12 13:28:37 +0000 | [diff] [blame] | 223 | // We sum counts when there are several counters for a single ProcRes |
Clement Courbet | b449379 | 2018-04-10 08:16:37 +0000 | [diff] [blame] | 224 | // (e.g. P23 on SandyBridge). |
Clement Courbet | 3827537 | 2018-06-12 13:28:37 +0000 | [diff] [blame] | 225 | int64_t CounterValue = 0; |
| 226 | llvm::SmallVector<llvm::StringRef, 2> CounterNames; |
| 227 | llvm::StringRef(PfmCounters).split(CounterNames, ','); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 228 | for (const auto &CounterName : CounterNames) { |
Clement Courbet | 3827537 | 2018-06-12 13:28:37 +0000 | [diff] [blame] | 229 | pfm::PerfEvent UopPerfEvent(CounterName); |
| 230 | if (!UopPerfEvent.valid()) |
| 231 | llvm::report_fatal_error( |
| 232 | llvm::Twine("invalid perf event ").concat(PfmCounters)); |
| 233 | pfm::Counter Counter(UopPerfEvent); |
| 234 | Counter.start(); |
| 235 | Function(); |
| 236 | Counter.stop(); |
| 237 | CounterValue += Counter.read(); |
| 238 | } |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 239 | Result.push_back({llvm::itostr(ProcResIdx), |
Clement Courbet | 3827537 | 2018-06-12 13:28:37 +0000 | [diff] [blame] | 240 | static_cast<double>(CounterValue) / NumRepetitions, |
Clement Courbet | b449379 | 2018-04-10 08:16:37 +0000 | [diff] [blame] | 241 | SchedModel.getProcResource(ProcResIdx)->Name}); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 242 | } |
| 243 | return Result; |
| 244 | } |
| 245 | |
| 246 | } // namespace exegesis |