Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 1 | //===-- BenchmarkRunner.h ---------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
| 10 | /// Defines the abstract BenchmarkRunner class for measuring a certain execution |
| 11 | /// property of instructions (e.g. latency). |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H |
| 16 | #define LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H |
| 17 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 18 | #include "Assembler.h" |
Clement Courbet | d939f6d | 2018-09-13 07:40:53 +0000 | [diff] [blame] | 19 | #include "BenchmarkCode.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 | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 23 | #include "SnippetRepetitor.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 | |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 30 | namespace llvm { |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 31 | namespace exegesis { |
| 32 | |
| 33 | // Common code for all benchmark modes. |
| 34 | class BenchmarkRunner { |
| 35 | public: |
Clement Courbet | 2c278cd | 2018-07-05 12:26:12 +0000 | [diff] [blame] | 36 | explicit BenchmarkRunner(const LLVMState &State, |
| 37 | InstructionBenchmark::ModeE Mode); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 38 | |
| 39 | virtual ~BenchmarkRunner(); |
| 40 | |
Miloš Stojanović | 2052927 | 2020-02-07 13:45:10 +0100 | [diff] [blame] | 41 | Expected<InstructionBenchmark> |
| 42 | runConfiguration(const BenchmarkCode &Configuration, unsigned NumRepetitions, |
Roman Lebedev | de22d71 | 2020-04-02 09:28:35 +0300 | [diff] [blame] | 43 | ArrayRef<std::unique_ptr<const SnippetRepetitor>> Repetitors, |
Miloš Stojanović | 2052927 | 2020-02-07 13:45:10 +0100 | [diff] [blame] | 44 | bool DumpObjectToDisk) const; |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 45 | |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 46 | // Scratch space to run instructions that touch memory. |
| 47 | struct ScratchSpace { |
| 48 | static constexpr const size_t kAlignment = 1024; |
| 49 | static constexpr const size_t kSize = 1 << 20; // 1MB. |
| 50 | ScratchSpace() |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 51 | : UnalignedPtr(std::make_unique<char[]>(kSize + kAlignment)), |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 52 | AlignedPtr( |
| 53 | UnalignedPtr.get() + kAlignment - |
| 54 | (reinterpret_cast<intptr_t>(UnalignedPtr.get()) % kAlignment)) {} |
| 55 | char *ptr() const { return AlignedPtr; } |
| 56 | void clear() { std::memset(ptr(), 0, kSize); } |
| 57 | |
| 58 | private: |
| 59 | const std::unique_ptr<char[]> UnalignedPtr; |
| 60 | char *const AlignedPtr; |
| 61 | }; |
| 62 | |
Clement Courbet | f973c2d | 2018-10-17 15:04:15 +0000 | [diff] [blame] | 63 | // A helper to measure counters while executing a function in a sandboxed |
| 64 | // context. |
| 65 | class FunctionExecutor { |
| 66 | public: |
Krasimir Georgiev | 11bc3a1 | 2018-10-18 02:06:16 +0000 | [diff] [blame] | 67 | virtual ~FunctionExecutor(); |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 68 | virtual Expected<int64_t> runAndMeasure(const char *Counters) const = 0; |
Clement Courbet | f973c2d | 2018-10-17 15:04:15 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 71 | protected: |
| 72 | const LLVMState &State; |
Clement Courbet | 362653f | 2019-01-30 16:02:20 +0000 | [diff] [blame] | 73 | const InstructionBenchmark::ModeE Mode; |
Clement Courbet | 717c976 | 2018-06-28 07:41:16 +0000 | [diff] [blame] | 74 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 75 | private: |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 76 | virtual Expected<std::vector<BenchmarkMeasure>> |
Clement Courbet | f973c2d | 2018-10-17 15:04:15 +0000 | [diff] [blame] | 77 | runMeasurements(const FunctionExecutor &Executor) const = 0; |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 78 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 79 | Expected<std::string> writeObjectFile(const BenchmarkCode &Configuration, |
| 80 | const FillFunction &Fill) const; |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 81 | |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 82 | const std::unique_ptr<ScratchSpace> Scratch; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | } // namespace exegesis |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 86 | } // namespace llvm |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 87 | |
| 88 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H |