1 //===-- BenchmarkRunner.h ---------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// Defines the abstract BenchmarkRunner class for measuring a certain execution
11 /// property of instructions (e.g. latency).
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H
16 #define LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H
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"
34 // Common code for all benchmark modes.
35 class BenchmarkRunner
{
37 enum ExecutionModeE
{ InProcess
, SubProcess
};
39 explicit BenchmarkRunner(const LLVMState
&State
, Benchmark::ModeE Mode
,
40 BenchmarkPhaseSelectorE BenchmarkPhaseSelector
,
41 ExecutionModeE ExecutionMode
);
43 virtual ~BenchmarkRunner();
45 class RunnableConfiguration
{
46 friend class BenchmarkRunner
;
49 ~RunnableConfiguration() = default;
50 RunnableConfiguration(RunnableConfiguration
&&) = default;
52 RunnableConfiguration(const RunnableConfiguration
&) = delete;
53 RunnableConfiguration
&operator=(RunnableConfiguration
&&) = delete;
54 RunnableConfiguration
&operator=(const RunnableConfiguration
&) = delete;
57 RunnableConfiguration() = default;
59 Benchmark InstrBenchmark
;
60 object::OwningBinary
<object::ObjectFile
> ObjectFile
;
63 Expected
<RunnableConfiguration
>
64 getRunnableConfiguration(const BenchmarkCode
&Configuration
,
65 unsigned NumRepetitions
, unsigned LoopUnrollFactor
,
66 const SnippetRepetitor
&Repetitor
) const;
68 std::pair
<Error
, Benchmark
>
69 runConfiguration(RunnableConfiguration
&&RC
,
70 const std::optional
<StringRef
> &DumpFile
) const;
72 // Scratch space to run instructions that touch memory.
74 static constexpr const size_t kAlignment
= 1024;
75 static constexpr const size_t kSize
= 1 << 20; // 1MB.
77 : UnalignedPtr(std::make_unique
<char[]>(kSize
+ kAlignment
)),
79 UnalignedPtr
.get() + kAlignment
-
80 (reinterpret_cast<intptr_t>(UnalignedPtr
.get()) % kAlignment
)) {}
81 char *ptr() const { return AlignedPtr
; }
82 void clear() { std::memset(ptr(), 0, kSize
); }
85 const std::unique_ptr
<char[]> UnalignedPtr
;
86 char *const AlignedPtr
;
89 // A helper to measure counters while executing a function in a sandboxed
91 class FunctionExecutor
{
93 virtual ~FunctionExecutor();
95 Expected
<llvm::SmallVector
<int64_t, 4>>
96 runAndSample(const char *Counters
) const;
100 accumulateCounterValues(const llvm::SmallVectorImpl
<int64_t> &NewValues
,
101 llvm::SmallVectorImpl
<int64_t> *Result
);
102 virtual Expected
<llvm::SmallVector
<int64_t, 4>>
103 runWithCounter(StringRef CounterName
) const = 0;
107 const LLVMState
&State
;
108 const Benchmark::ModeE Mode
;
109 const BenchmarkPhaseSelectorE BenchmarkPhaseSelector
;
110 const ExecutionModeE ExecutionMode
;
113 virtual Expected
<std::vector
<BenchmarkMeasure
>>
114 runMeasurements(const FunctionExecutor
&Executor
) const = 0;
116 Expected
<SmallString
<0>>
117 assembleSnippet(const BenchmarkCode
&BC
, const SnippetRepetitor
&Repetitor
,
118 unsigned MinInstructions
, unsigned LoopBodySize
,
119 bool GenerateMemoryInstructions
) const;
121 Expected
<std::string
> writeObjectFile(StringRef Buffer
,
122 StringRef FileName
) const;
124 const std::unique_ptr
<ScratchSpace
> Scratch
;
126 Expected
<std::unique_ptr
<FunctionExecutor
>>
127 createFunctionExecutor(object::OwningBinary
<object::ObjectFile
> Obj
,
128 const BenchmarkKey
&Key
) const;
131 } // namespace exegesis
134 #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H