| 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 | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 29 | BenchmarkRunner::InstructionFilter::~InstructionFilter() = default; |
| Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 30 | |
| Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 31 | BenchmarkRunner::BenchmarkRunner(const LLVMState &State) |
| 32 | : State(State), MCInstrInfo(State.getInstrInfo()), |
| 33 | MCRegisterInfo(State.getRegInfo()), |
| 34 | RATC(MCRegisterInfo, |
| 35 | getFunctionReservedRegs(*State.createTargetMachine())) {} |
| Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 36 | |
| Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 37 | BenchmarkRunner::~BenchmarkRunner() = default; |
| 38 | |
| Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 39 | llvm::Expected<std::vector<InstructionBenchmark>> |
| 40 | BenchmarkRunner::run(unsigned Opcode, const InstructionFilter &Filter, |
| 41 | unsigned NumRepetitions) { |
| 42 | // Ignore instructions that we cannot run. |
| 43 | if (State.getInstrInfo().get(Opcode).isPseudo()) |
| Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 44 | return llvm::make_error<BenchmarkFailure>("Unsupported opcode: isPseudo"); |
| Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 45 | |
| Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 46 | if (llvm::Error E = Filter.shouldRun(State, Opcode)) |
| 47 | return std::move(E); |
| 48 | |
| 49 | llvm::Expected<std::vector<BenchmarkConfiguration>> ConfigurationOrError = |
| Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 50 | generateConfigurations(Opcode); |
| Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 51 | |
| 52 | if (llvm::Error E = ConfigurationOrError.takeError()) |
| 53 | return std::move(E); |
| 54 | |
| 55 | std::vector<InstructionBenchmark> InstrBenchmarks; |
| 56 | for (const BenchmarkConfiguration &Conf : ConfigurationOrError.get()) |
| 57 | InstrBenchmarks.push_back(runOne(Conf, Opcode, NumRepetitions)); |
| 58 | return InstrBenchmarks; |
| 59 | } |
| 60 | |
| 61 | InstructionBenchmark |
| 62 | BenchmarkRunner::runOne(const BenchmarkConfiguration &Configuration, |
| 63 | unsigned Opcode, unsigned NumRepetitions) const { |
| 64 | InstructionBenchmark InstrBenchmark; |
| Clement Courbet | 62b34fa | 2018-06-06 09:42:36 +0000 | [diff] [blame] | 65 | InstrBenchmark.Mode = getMode(); |
| Clement Courbet | 760d1d5 | 2018-06-21 14:11:09 +0000 | [diff] [blame] | 66 | InstrBenchmark.CpuName = State.getTargetMachine().getTargetCPU(); |
| 67 | InstrBenchmark.LLVMTriple = |
| 68 | State.getTargetMachine().getTargetTriple().normalize(); |
| Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 69 | InstrBenchmark.NumRepetitions = NumRepetitions; |
| Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 70 | InstrBenchmark.Info = Configuration.Info; |
| Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 71 | |
| Guillaume Chatelet | 7b852cd | 2018-06-07 08:11:54 +0000 | [diff] [blame] | 72 | const std::vector<llvm::MCInst> &Snippet = Configuration.Snippet; |
| Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 73 | if (Snippet.empty()) { |
| 74 | InstrBenchmark.Error = "Empty snippet"; |
| 75 | return InstrBenchmark; |
| 76 | } |
| Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 77 | |
| Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 78 | InstrBenchmark.Key.Instructions = Snippet; |
| Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 79 | |
| Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 80 | // Repeat the snippet until there are at least NumInstructions in the |
| 81 | // resulting code. The snippet is always repeated at least once. |
| 82 | const auto GenerateInstructions = [&Snippet](const int MinInstructions) { |
| 83 | std::vector<llvm::MCInst> Code = Snippet; |
| 84 | for (int I = 0; I < MinInstructions; ++I) |
| 85 | Code.push_back(Snippet[I % Snippet.size()]); |
| 86 | return Code; |
| 87 | }; |
| Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 88 | |
| Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 89 | // Assemble at least kMinInstructionsForSnippet instructions by repeating the |
| 90 | // snippet for debug/analysis. This is so that the user clearly understands |
| 91 | // that the inside instructions are repeated. |
| 92 | constexpr const int kMinInstructionsForSnippet = 16; |
| 93 | { |
| Clement Courbet | 1ef6aa8 | 2018-06-21 14:49:04 +0000 | [diff] [blame^] | 94 | auto ObjectFilePath = writeObjectFile( |
| Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 95 | GenerateInstructions(kMinInstructionsForSnippet)); |
| Clement Courbet | 1ef6aa8 | 2018-06-21 14:49:04 +0000 | [diff] [blame^] | 96 | if (llvm::Error E = ObjectFilePath.takeError()) { |
| Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 97 | InstrBenchmark.Error = llvm::toString(std::move(E)); |
| 98 | return InstrBenchmark; |
| 99 | } |
| Clement Courbet | 1ef6aa8 | 2018-06-21 14:49:04 +0000 | [diff] [blame^] | 100 | const ExecutableFunction EF(State.createTargetMachine(), |
| 101 | getObjectFromFile(*ObjectFilePath)); |
| 102 | const auto FnBytes = EF.getFunctionBytes(); |
| Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 103 | InstrBenchmark.AssembledSnippet.assign(FnBytes.begin(), FnBytes.end()); |
| 104 | } |
| 105 | |
| 106 | // Assemble NumRepetitions instructions repetitions of the snippet for |
| 107 | // measurements. |
| Clement Courbet | 1ef6aa8 | 2018-06-21 14:49:04 +0000 | [diff] [blame^] | 108 | auto ObjectFilePath = writeObjectFile( |
| Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 109 | GenerateInstructions(InstrBenchmark.NumRepetitions)); |
| Clement Courbet | 1ef6aa8 | 2018-06-21 14:49:04 +0000 | [diff] [blame^] | 110 | if (llvm::Error E = ObjectFilePath.takeError()) { |
| Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 111 | InstrBenchmark.Error = llvm::toString(std::move(E)); |
| 112 | return InstrBenchmark; |
| 113 | } |
| Clement Courbet | 1ef6aa8 | 2018-06-21 14:49:04 +0000 | [diff] [blame^] | 114 | llvm::outs() << "Check generated assembly with: /usr/bin/objdump -d " |
| 115 | << *ObjectFilePath << "\n"; |
| 116 | const ExecutableFunction EF(State.createTargetMachine(), |
| 117 | getObjectFromFile(*ObjectFilePath)); |
| 118 | InstrBenchmark.Measurements = runMeasurements(EF, NumRepetitions); |
| Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 119 | |
| Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 120 | return InstrBenchmark; |
| 121 | } |
| 122 | |
| Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 123 | llvm::Expected<std::vector<BenchmarkConfiguration>> |
| 124 | BenchmarkRunner::generateConfigurations(unsigned Opcode) const { |
| 125 | if (auto E = generatePrototype(Opcode)) { |
| 126 | SnippetPrototype &Prototype = E.get(); |
| 127 | // TODO: Generate as many configurations as needed here. |
| 128 | BenchmarkConfiguration Configuration; |
| 129 | Configuration.Info = Prototype.Explanation; |
| 130 | for (InstructionInstance &II : Prototype.Snippet) |
| 131 | Configuration.Snippet.push_back(II.randomizeUnsetVariablesAndBuild()); |
| 132 | return std::vector<BenchmarkConfiguration>{Configuration}; |
| 133 | } else |
| 134 | return E.takeError(); |
| 135 | } |
| 136 | |
| Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 137 | llvm::Expected<std::string> |
| 138 | BenchmarkRunner::writeObjectFile(llvm::ArrayRef<llvm::MCInst> Code) const { |
| 139 | int ResultFD = 0; |
| 140 | llvm::SmallString<256> ResultPath; |
| 141 | if (llvm::Error E = llvm::errorCodeToError(llvm::sys::fs::createTemporaryFile( |
| 142 | "snippet", "o", ResultFD, ResultPath))) |
| 143 | return std::move(E); |
| 144 | llvm::raw_fd_ostream OFS(ResultFD, true /*ShouldClose*/); |
| 145 | assembleToStream(State.createTargetMachine(), Code, OFS); |
| Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 146 | return ResultPath.str(); |
| 147 | } |
| 148 | |
| Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 149 | } // namespace exegesis |