Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 1 | //===-- BenchmarkRunner.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 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 10 | #include <array> |
| 11 | #include <string> |
| 12 | |
| 13 | #include "Assembler.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 14 | #include "BenchmarkRunner.h" |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 15 | #include "MCInstrDescView.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringExtras.h" |
| 17 | #include "llvm/ADT/StringRef.h" |
| 18 | #include "llvm/ADT/Twine.h" |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 19 | #include "llvm/Support/FileSystem.h" |
| 20 | #include "llvm/Support/FormatVariadic.h" |
| 21 | #include "llvm/Support/MemoryBuffer.h" |
| 22 | #include "llvm/Support/Program.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 23 | |
| 24 | namespace exegesis { |
| 25 | |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 26 | BenchmarkFailure::BenchmarkFailure(const llvm::Twine &S) |
| 27 | : llvm::StringError(S, llvm::inconvertibleErrorCode()) {} |
| 28 | |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 29 | BenchmarkRunner::BenchmarkRunner(const LLVMState &State, |
| 30 | InstructionBenchmark::ModeE Mode) |
| 31 | : State(State), RATC(State.getRegInfo(), |
| 32 | getFunctionReservedRegs(State.getTargetMachine())), |
| 33 | Mode(Mode) {} |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 34 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 35 | BenchmarkRunner::~BenchmarkRunner() = default; |
| 36 | |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 37 | llvm::Expected<std::vector<InstructionBenchmark>> |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 38 | BenchmarkRunner::run(unsigned Opcode, unsigned NumRepetitions) { |
| 39 | const llvm::MCInstrDesc &InstrDesc = State.getInstrInfo().get(Opcode); |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 40 | // Ignore instructions that we cannot run. |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 41 | if (InstrDesc.isPseudo()) |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 42 | return llvm::make_error<BenchmarkFailure>("Unsupported opcode: isPseudo"); |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 43 | if (InstrDesc.isBranch() || InstrDesc.isIndirectBranch()) |
| 44 | return llvm::make_error<BenchmarkFailure>( |
| 45 | "Unsupported opcode: isBranch/isIndirectBranch"); |
| 46 | if (InstrDesc.isCall() || InstrDesc.isReturn()) |
| 47 | return llvm::make_error<BenchmarkFailure>( |
| 48 | "Unsupported opcode: isCall/isReturn"); |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 49 | |
| 50 | llvm::Expected<std::vector<BenchmarkConfiguration>> ConfigurationOrError = |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 51 | generateConfigurations(Opcode); |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 52 | |
| 53 | if (llvm::Error E = ConfigurationOrError.takeError()) |
| 54 | return std::move(E); |
| 55 | |
| 56 | std::vector<InstructionBenchmark> InstrBenchmarks; |
| 57 | for (const BenchmarkConfiguration &Conf : ConfigurationOrError.get()) |
| 58 | InstrBenchmarks.push_back(runOne(Conf, Opcode, NumRepetitions)); |
| 59 | return InstrBenchmarks; |
| 60 | } |
| 61 | |
| 62 | InstructionBenchmark |
| 63 | BenchmarkRunner::runOne(const BenchmarkConfiguration &Configuration, |
| 64 | unsigned Opcode, unsigned NumRepetitions) const { |
| 65 | InstructionBenchmark InstrBenchmark; |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 66 | InstrBenchmark.Mode = Mode; |
Clement Courbet | 760d1d5 | 2018-06-21 14:11:09 +0000 | [diff] [blame] | 67 | InstrBenchmark.CpuName = State.getTargetMachine().getTargetCPU(); |
| 68 | InstrBenchmark.LLVMTriple = |
| 69 | State.getTargetMachine().getTargetTriple().normalize(); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 70 | InstrBenchmark.NumRepetitions = NumRepetitions; |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 71 | InstrBenchmark.Info = Configuration.Info; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 72 | |
Guillaume Chatelet | 7b852cd | 2018-06-07 08:11:54 +0000 | [diff] [blame] | 73 | const std::vector<llvm::MCInst> &Snippet = Configuration.Snippet; |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 74 | if (Snippet.empty()) { |
| 75 | InstrBenchmark.Error = "Empty snippet"; |
| 76 | return InstrBenchmark; |
| 77 | } |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 78 | |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 79 | InstrBenchmark.Key.Instructions = Snippet; |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 80 | |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 81 | // Repeat the snippet until there are at least NumInstructions in the |
| 82 | // resulting code. The snippet is always repeated at least once. |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 83 | const auto GenerateInstructions = [&Configuration]( |
| 84 | const int MinInstructions) { |
| 85 | std::vector<llvm::MCInst> Code = Configuration.Snippet; |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 86 | for (int I = 0; I < MinInstructions; ++I) |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 87 | Code.push_back(Configuration.Snippet[I % Configuration.Snippet.size()]); |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 88 | return Code; |
| 89 | }; |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 90 | |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 91 | // Assemble at least kMinInstructionsForSnippet instructions by repeating the |
| 92 | // snippet for debug/analysis. This is so that the user clearly understands |
| 93 | // that the inside instructions are repeated. |
| 94 | constexpr const int kMinInstructionsForSnippet = 16; |
| 95 | { |
Clement Courbet | cff2caa | 2018-06-25 11:22:23 +0000 | [diff] [blame] | 96 | auto ObjectFilePath = |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 97 | writeObjectFile(Configuration.SnippetSetup, |
| 98 | GenerateInstructions(kMinInstructionsForSnippet)); |
Clement Courbet | 1ef6aa8 | 2018-06-21 14:49:04 +0000 | [diff] [blame] | 99 | if (llvm::Error E = ObjectFilePath.takeError()) { |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 100 | InstrBenchmark.Error = llvm::toString(std::move(E)); |
| 101 | return InstrBenchmark; |
| 102 | } |
Clement Courbet | 1ef6aa8 | 2018-06-21 14:49:04 +0000 | [diff] [blame] | 103 | const ExecutableFunction EF(State.createTargetMachine(), |
Clement Courbet | cff2caa | 2018-06-25 11:22:23 +0000 | [diff] [blame] | 104 | getObjectFromFile(*ObjectFilePath)); |
Clement Courbet | 1ef6aa8 | 2018-06-21 14:49:04 +0000 | [diff] [blame] | 105 | const auto FnBytes = EF.getFunctionBytes(); |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 106 | InstrBenchmark.AssembledSnippet.assign(FnBytes.begin(), FnBytes.end()); |
| 107 | } |
| 108 | |
| 109 | // Assemble NumRepetitions instructions repetitions of the snippet for |
| 110 | // measurements. |
Clement Courbet | cff2caa | 2018-06-25 11:22:23 +0000 | [diff] [blame] | 111 | auto ObjectFilePath = |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 112 | writeObjectFile(Configuration.SnippetSetup, |
| 113 | GenerateInstructions(InstrBenchmark.NumRepetitions)); |
Clement Courbet | 1ef6aa8 | 2018-06-21 14:49:04 +0000 | [diff] [blame] | 114 | if (llvm::Error E = ObjectFilePath.takeError()) { |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 115 | InstrBenchmark.Error = llvm::toString(std::move(E)); |
| 116 | return InstrBenchmark; |
| 117 | } |
Clement Courbet | 1ef6aa8 | 2018-06-21 14:49:04 +0000 | [diff] [blame] | 118 | llvm::outs() << "Check generated assembly with: /usr/bin/objdump -d " |
| 119 | << *ObjectFilePath << "\n"; |
| 120 | const ExecutableFunction EF(State.createTargetMachine(), |
Clement Courbet | cff2caa | 2018-06-25 11:22:23 +0000 | [diff] [blame] | 121 | getObjectFromFile(*ObjectFilePath)); |
Clement Courbet | 1ef6aa8 | 2018-06-21 14:49:04 +0000 | [diff] [blame] | 122 | InstrBenchmark.Measurements = runMeasurements(EF, NumRepetitions); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 123 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 124 | return InstrBenchmark; |
| 125 | } |
| 126 | |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 127 | llvm::Expected<std::vector<BenchmarkConfiguration>> |
| 128 | BenchmarkRunner::generateConfigurations(unsigned Opcode) const { |
| 129 | if (auto E = generatePrototype(Opcode)) { |
| 130 | SnippetPrototype &Prototype = E.get(); |
| 131 | // TODO: Generate as many configurations as needed here. |
| 132 | BenchmarkConfiguration Configuration; |
| 133 | Configuration.Info = Prototype.Explanation; |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 134 | for (InstructionInstance &II : Prototype.Snippet) { |
| 135 | II.randomizeUnsetVariables(); |
| 136 | Configuration.Snippet.push_back(II.build()); |
| 137 | } |
| 138 | Configuration.SnippetSetup.RegsToDef = computeRegsToDef(Prototype.Snippet); |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 139 | return std::vector<BenchmarkConfiguration>{Configuration}; |
| 140 | } else |
| 141 | return E.takeError(); |
| 142 | } |
| 143 | |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 144 | std::vector<unsigned> BenchmarkRunner::computeRegsToDef( |
| 145 | const std::vector<InstructionInstance> &Snippet) const { |
| 146 | // Collect all register uses and create an assignment for each of them. |
| 147 | // Loop invariant: DefinedRegs[i] is true iif it has been set at least once |
| 148 | // before the current instruction. |
| 149 | llvm::BitVector DefinedRegs = RATC.emptyRegisters(); |
| 150 | std::vector<unsigned> RegsToDef; |
| 151 | for (const InstructionInstance &II : Snippet) { |
| 152 | // Returns the register that this Operand sets or uses, or 0 if this is not |
| 153 | // a register. |
| 154 | const auto GetOpReg = [&II](const Operand &Op) -> unsigned { |
| 155 | if (Op.ImplicitReg) { |
| 156 | return *Op.ImplicitReg; |
| 157 | } else if (Op.IsExplicit && II.getValueFor(Op).isReg()) { |
| 158 | return II.getValueFor(Op).getReg(); |
| 159 | } |
| 160 | return 0; |
| 161 | }; |
| 162 | // Collect used registers that have never been def'ed. |
| 163 | for (const Operand &Op : II.Instr.Operands) { |
| 164 | if (!Op.IsDef) { |
| 165 | const unsigned Reg = GetOpReg(Op); |
| 166 | if (Reg > 0 && !DefinedRegs.test(Reg)) { |
| 167 | RegsToDef.push_back(Reg); |
| 168 | DefinedRegs.set(Reg); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | // Mark defs as having been def'ed. |
| 173 | for (const Operand &Op : II.Instr.Operands) { |
| 174 | if (Op.IsDef) { |
| 175 | const unsigned Reg = GetOpReg(Op); |
| 176 | if (Reg > 0) { |
| 177 | DefinedRegs.set(Reg); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | return RegsToDef; |
| 183 | } |
| 184 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 185 | llvm::Expected<std::string> |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 186 | BenchmarkRunner::writeObjectFile(const BenchmarkConfiguration::Setup &Setup, |
| 187 | llvm::ArrayRef<llvm::MCInst> Code) const { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 188 | int ResultFD = 0; |
| 189 | llvm::SmallString<256> ResultPath; |
| 190 | if (llvm::Error E = llvm::errorCodeToError(llvm::sys::fs::createTemporaryFile( |
| 191 | "snippet", "o", ResultFD, ResultPath))) |
| 192 | return std::move(E); |
| 193 | llvm::raw_fd_ostream OFS(ResultFD, true /*ShouldClose*/); |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 194 | assembleToStream(State.getExegesisTarget(), State.createTargetMachine(), |
| 195 | Setup.RegsToDef, Code, OFS); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 196 | return ResultPath.str(); |
| 197 | } |
| 198 | |
Clement Courbet | 717c976 | 2018-06-28 07:41:16 +0000 | [diff] [blame] | 199 | llvm::Expected<SnippetPrototype> BenchmarkRunner::generateSelfAliasingPrototype( |
| 200 | const Instruction &Instr) const { |
| 201 | const AliasingConfigurations SelfAliasing(Instr, Instr); |
| 202 | if (SelfAliasing.empty()) { |
| 203 | return llvm::make_error<BenchmarkFailure>("empty self aliasing"); |
| 204 | } |
| 205 | SnippetPrototype Prototype; |
| 206 | InstructionInstance II(Instr); |
| 207 | if (SelfAliasing.hasImplicitAliasing()) { |
| 208 | Prototype.Explanation = "implicit Self cycles, picking random values."; |
| 209 | } else { |
| 210 | Prototype.Explanation = |
| 211 | "explicit self cycles, selecting one aliasing Conf."; |
| 212 | // This is a self aliasing instruction so defs and uses are from the same |
| 213 | // instance, hence twice II in the following call. |
| 214 | setRandomAliasing(SelfAliasing, II, II); |
| 215 | } |
| 216 | Prototype.Snippet.push_back(std::move(II)); |
| 217 | return std::move(Prototype); |
| 218 | } |
| 219 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 220 | } // namespace exegesis |