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/MC/MCInst.h"
25 #include "llvm/Support/Error.h"
33 // Common code for all benchmark modes.
34 class BenchmarkRunner
{
36 explicit BenchmarkRunner(const LLVMState
&State
,
37 InstructionBenchmark::ModeE Mode
);
39 virtual ~BenchmarkRunner();
41 InstructionBenchmark
runConfiguration(const BenchmarkCode
&Configuration
,
42 unsigned NumRepetitions
,
43 const SnippetRepetitor
&Repetitor
,
44 bool DumpObjectToDisk
) const;
46 // Scratch space to run instructions that touch memory.
48 static constexpr const size_t kAlignment
= 1024;
49 static constexpr const size_t kSize
= 1 << 20; // 1MB.
51 : UnalignedPtr(std::make_unique
<char[]>(kSize
+ kAlignment
)),
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
); }
59 const std::unique_ptr
<char[]> UnalignedPtr
;
60 char *const AlignedPtr
;
63 // A helper to measure counters while executing a function in a sandboxed
65 class FunctionExecutor
{
67 virtual ~FunctionExecutor();
68 virtual llvm::Expected
<int64_t>
69 runAndMeasure(const char *Counters
) const = 0;
73 const LLVMState
&State
;
74 const InstructionBenchmark::ModeE Mode
;
77 virtual llvm::Expected
<std::vector
<BenchmarkMeasure
>>
78 runMeasurements(const FunctionExecutor
&Executor
) const = 0;
80 llvm::Expected
<std::string
>
81 writeObjectFile(const BenchmarkCode
&Configuration
,
82 const FillFunction
&Fill
) const;
84 const std::unique_ptr
<ScratchSpace
> Scratch
;
87 } // namespace exegesis
90 #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H