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 | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame^] | 20 | #include "BenchmarkCode.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 21 | #include "BenchmarkResult.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 22 | #include "LlvmState.h" |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 23 | #include "MCInstrDescView.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 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 39 | // Common code for all benchmark modes. |
| 40 | class BenchmarkRunner { |
| 41 | public: |
Clement Courbet | 2c278cd | 2018-07-05 12:26:12 +0000 | [diff] [blame] | 42 | explicit BenchmarkRunner(const LLVMState &State, |
| 43 | InstructionBenchmark::ModeE Mode); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 44 | |
| 45 | virtual ~BenchmarkRunner(); |
| 46 | |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame^] | 47 | InstructionBenchmark runConfiguration(const BenchmarkCode &Configuration, |
| 48 | unsigned NumRepetitions) const; |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 49 | |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 50 | // Scratch space to run instructions that touch memory. |
| 51 | struct ScratchSpace { |
| 52 | static constexpr const size_t kAlignment = 1024; |
| 53 | static constexpr const size_t kSize = 1 << 20; // 1MB. |
| 54 | ScratchSpace() |
| 55 | : UnalignedPtr(llvm::make_unique<char[]>(kSize + kAlignment)), |
| 56 | AlignedPtr( |
| 57 | UnalignedPtr.get() + kAlignment - |
| 58 | (reinterpret_cast<intptr_t>(UnalignedPtr.get()) % kAlignment)) {} |
| 59 | char *ptr() const { return AlignedPtr; } |
| 60 | void clear() { std::memset(ptr(), 0, kSize); } |
| 61 | |
| 62 | private: |
| 63 | const std::unique_ptr<char[]> UnalignedPtr; |
| 64 | char *const AlignedPtr; |
| 65 | }; |
| 66 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 67 | protected: |
| 68 | const LLVMState &State; |
Clement Courbet | 717c976 | 2018-06-28 07:41:16 +0000 | [diff] [blame] | 69 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 70 | private: |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 71 | virtual std::vector<BenchmarkMeasure> |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 72 | runMeasurements(const ExecutableFunction &EF, ScratchSpace &Scratch, |
Clement Courbet | 2c278cd | 2018-07-05 12:26:12 +0000 | [diff] [blame] | 73 | const unsigned NumRepetitions) const = 0; |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 74 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 75 | llvm::Expected<std::string> |
Guillaume Chatelet | e60866a | 2018-08-03 09:29:38 +0000 | [diff] [blame] | 76 | writeObjectFile(const BenchmarkCode &Configuration, |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 77 | llvm::ArrayRef<llvm::MCInst> Code) const; |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 78 | |
| 79 | const InstructionBenchmark::ModeE Mode; |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 80 | |
| 81 | const std::unique_ptr<ScratchSpace> Scratch; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | } // namespace exegesis |
| 85 | |
| 86 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H |