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