Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 1 | //===-- BenchmarkRunner.h ---------------------------------------*- 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 | /// \file |
| 11 | /// Defines the abstract BenchmarkRunner class for measuring a certain execution |
| 12 | /// property of instructions (e.g. latency). |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H |
| 17 | #define LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H |
| 18 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 19 | #include "Assembler.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 20 | #include "BenchmarkResult.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 21 | #include "LlvmState.h" |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 22 | #include "MCInstrDescView.h" |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 23 | #include "RegisterAliasing.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCInst.h" |
| 25 | #include "llvm/Support/Error.h" |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 26 | #include <cstdlib> |
| 27 | #include <memory> |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
| 30 | namespace exegesis { |
| 31 | |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 32 | // A class representing failures that happened during Benchmark, they are used |
| 33 | // to report informations to the user. |
| 34 | class BenchmarkFailure : public llvm::StringError { |
| 35 | public: |
| 36 | BenchmarkFailure(const llvm::Twine &S); |
| 37 | }; |
| 38 | |
Guillaume Chatelet | 7b852cd | 2018-06-07 08:11:54 +0000 | [diff] [blame] | 39 | // A collection of instructions that are to be assembled, executed and measured. |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 40 | struct BenchmarkCode { |
Guillaume Chatelet | 7b852cd | 2018-06-07 08:11:54 +0000 | [diff] [blame] | 41 | // The sequence of instructions that are to be repeated. |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 42 | std::vector<llvm::MCInst> Instructions; |
| 43 | |
| 44 | // Before the code is executed some instructions are added to setup the |
| 45 | // registers initial values. |
| 46 | std::vector<unsigned> RegsToDef; |
| 47 | |
| 48 | // We also need to provide the registers that are live on entry for the |
| 49 | // assembler to generate proper prologue/epilogue. |
| 50 | std::vector<unsigned> LiveIns; |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 51 | |
| 52 | // Informations about how this configuration was built. |
| 53 | std::string Info; |
Guillaume Chatelet | 7b852cd | 2018-06-07 08:11:54 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 56 | // Common code for all benchmark modes. |
| 57 | class BenchmarkRunner { |
| 58 | public: |
Clement Courbet | 2c278cd | 2018-07-05 12:26:12 +0000 | [diff] [blame] | 59 | explicit BenchmarkRunner(const LLVMState &State, |
| 60 | InstructionBenchmark::ModeE Mode); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 61 | |
| 62 | virtual ~BenchmarkRunner(); |
| 63 | |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 64 | llvm::Expected<std::vector<InstructionBenchmark>> |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 65 | run(unsigned Opcode, unsigned NumRepetitions); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 66 | |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 67 | // Given a snippet, computes which registers the setup code needs to define. |
| 68 | std::vector<unsigned> |
Guillaume Chatelet | 171f3f4 | 2018-08-02 11:12:02 +0000 | [diff] [blame] | 69 | computeRegsToDef(const std::vector<InstructionBuilder> &Snippet) const; |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 70 | |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 71 | // Scratch space to run instructions that touch memory. |
| 72 | struct ScratchSpace { |
| 73 | static constexpr const size_t kAlignment = 1024; |
| 74 | static constexpr const size_t kSize = 1 << 20; // 1MB. |
| 75 | ScratchSpace() |
| 76 | : UnalignedPtr(llvm::make_unique<char[]>(kSize + kAlignment)), |
| 77 | AlignedPtr( |
| 78 | UnalignedPtr.get() + kAlignment - |
| 79 | (reinterpret_cast<intptr_t>(UnalignedPtr.get()) % kAlignment)) {} |
| 80 | char *ptr() const { return AlignedPtr; } |
| 81 | void clear() { std::memset(ptr(), 0, kSize); } |
| 82 | |
| 83 | private: |
| 84 | const std::unique_ptr<char[]> UnalignedPtr; |
| 85 | char *const AlignedPtr; |
| 86 | }; |
| 87 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 88 | protected: |
| 89 | const LLVMState &State; |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 90 | const RegisterAliasingTrackerCache RATC; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 91 | |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 92 | // Generates a single code template that has a self-dependency. |
| 93 | llvm::Expected<CodeTemplate> |
| 94 | generateSelfAliasingCodeTemplate(const Instruction &Instr) const; |
| 95 | // Generates a single code template without assignment constraints. |
| 96 | llvm::Expected<CodeTemplate> |
| 97 | generateUnconstrainedCodeTemplate(const Instruction &Instr, |
| 98 | llvm::StringRef Msg) const; |
Clement Courbet | 717c976 | 2018-06-28 07:41:16 +0000 | [diff] [blame] | 99 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 100 | private: |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 101 | // API to be implemented by subclasses. |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 102 | virtual llvm::Expected<CodeTemplate> |
| 103 | generateCodeTemplate(unsigned Opcode) const = 0; |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 104 | |
| 105 | virtual std::vector<BenchmarkMeasure> |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 106 | runMeasurements(const ExecutableFunction &EF, ScratchSpace &Scratch, |
Clement Courbet | 2c278cd | 2018-07-05 12:26:12 +0000 | [diff] [blame] | 107 | const unsigned NumRepetitions) const = 0; |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 108 | |
| 109 | // Internal helpers. |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 110 | InstructionBenchmark runConfiguration(const BenchmarkCode &Configuration, |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 111 | unsigned NumRepetitions) const; |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 112 | |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 113 | // Calls generateCodeTemplate and expands it into one or more BenchmarkCode. |
| 114 | llvm::Expected<std::vector<BenchmarkCode>> |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 115 | generateConfigurations(unsigned Opcode) const; |
| 116 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 117 | llvm::Expected<std::string> |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 118 | writeObjectFile(const BenchmarkCode &Configuration, |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 119 | llvm::ArrayRef<llvm::MCInst> Code) const; |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 120 | |
| 121 | const InstructionBenchmark::ModeE Mode; |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 122 | |
| 123 | const std::unique_ptr<ScratchSpace> Scratch; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | } // namespace exegesis |
| 127 | |
| 128 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H |