xref: /openbsd-src/gnu/llvm/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.h (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
1 //===-- BenchmarkRunner.h ---------------------------------------*- C++ -*-===//
2 //
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
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 
18 #include "Assembler.h"
19 #include "BenchmarkCode.h"
20 #include "BenchmarkResult.h"
21 #include "LlvmState.h"
22 #include "MCInstrDescView.h"
23 #include "SnippetRepetitor.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/Support/Error.h"
27 #include <cstdlib>
28 #include <memory>
29 #include <vector>
30 
31 namespace llvm {
32 namespace exegesis {
33 
34 // Common code for all benchmark modes.
35 class BenchmarkRunner {
36 public:
37   explicit BenchmarkRunner(const LLVMState &State,
38                            InstructionBenchmark::ModeE Mode,
39                            BenchmarkPhaseSelectorE BenchmarkPhaseSelector);
40 
41   virtual ~BenchmarkRunner();
42 
43   class RunnableConfiguration {
44     friend class BenchmarkRunner;
45 
46   public:
47     ~RunnableConfiguration() = default;
48     RunnableConfiguration(RunnableConfiguration &&) = default;
49 
50     RunnableConfiguration(const RunnableConfiguration &) = delete;
51     RunnableConfiguration &operator=(RunnableConfiguration &&) = delete;
52     RunnableConfiguration &operator=(const RunnableConfiguration &) = delete;
53 
54   private:
55     RunnableConfiguration() = default;
56 
57     InstructionBenchmark InstrBenchmark;
58     object::OwningBinary<object::ObjectFile> ObjectFile;
59   };
60 
61   Expected<RunnableConfiguration>
62   getRunnableConfiguration(const BenchmarkCode &Configuration,
63                            unsigned NumRepetitions, unsigned LoopUnrollFactor,
64                            const SnippetRepetitor &Repetitor) const;
65 
66   Expected<InstructionBenchmark> runConfiguration(RunnableConfiguration &&RC,
67                                                   bool DumpObjectToDisk) const;
68 
69   // Scratch space to run instructions that touch memory.
70   struct ScratchSpace {
71     static constexpr const size_t kAlignment = 1024;
72     static constexpr const size_t kSize = 1 << 20; // 1MB.
ScratchSpaceScratchSpace73     ScratchSpace()
74         : UnalignedPtr(std::make_unique<char[]>(kSize + kAlignment)),
75           AlignedPtr(
76               UnalignedPtr.get() + kAlignment -
77               (reinterpret_cast<intptr_t>(UnalignedPtr.get()) % kAlignment)) {}
ptrScratchSpace78     char *ptr() const { return AlignedPtr; }
clearScratchSpace79     void clear() { std::memset(ptr(), 0, kSize); }
80 
81   private:
82     const std::unique_ptr<char[]> UnalignedPtr;
83     char *const AlignedPtr;
84   };
85 
86   // A helper to measure counters while executing a function in a sandboxed
87   // context.
88   class FunctionExecutor {
89   public:
90     virtual ~FunctionExecutor();
91     // FIXME deprecate this.
92     virtual Expected<int64_t> runAndMeasure(const char *Counters) const = 0;
93 
94     virtual Expected<llvm::SmallVector<int64_t, 4>>
95     runAndSample(const char *Counters) const = 0;
96   };
97 
98 protected:
99   const LLVMState &State;
100   const InstructionBenchmark::ModeE Mode;
101   const BenchmarkPhaseSelectorE BenchmarkPhaseSelector;
102 
103 private:
104   virtual Expected<std::vector<BenchmarkMeasure>>
105   runMeasurements(const FunctionExecutor &Executor) const = 0;
106 
107   Expected<SmallString<0>> assembleSnippet(const BenchmarkCode &BC,
108                                            const SnippetRepetitor &Repetitor,
109                                            unsigned MinInstructions,
110                                            unsigned LoopBodySize) const;
111 
112   Expected<std::string> writeObjectFile(StringRef Buffer) const;
113 
114   const std::unique_ptr<ScratchSpace> Scratch;
115 };
116 
117 } // namespace exegesis
118 } // namespace llvm
119 
120 #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H
121