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 // A class representing failures that happened during Benchmark, they are used
34 // to report informations to the user.
35 class BenchmarkFailure
: public llvm::StringError
{
37 BenchmarkFailure(const llvm::Twine
&S
);
40 // Common code for all benchmark modes.
41 class BenchmarkRunner
{
43 explicit BenchmarkRunner(const LLVMState
&State
,
44 InstructionBenchmark::ModeE Mode
);
46 virtual ~BenchmarkRunner();
48 InstructionBenchmark
runConfiguration(const BenchmarkCode
&Configuration
,
49 unsigned NumRepetitions
,
50 const SnippetRepetitor
&Repetitor
,
51 bool DumpObjectToDisk
) const;
53 // Scratch space to run instructions that touch memory.
55 static constexpr const size_t kAlignment
= 1024;
56 static constexpr const size_t kSize
= 1 << 20; // 1MB.
58 : UnalignedPtr(std::make_unique
<char[]>(kSize
+ kAlignment
)),
60 UnalignedPtr
.get() + kAlignment
-
61 (reinterpret_cast<intptr_t>(UnalignedPtr
.get()) % kAlignment
)) {}
62 char *ptr() const { return AlignedPtr
; }
63 void clear() { std::memset(ptr(), 0, kSize
); }
66 const std::unique_ptr
<char[]> UnalignedPtr
;
67 char *const AlignedPtr
;
70 // A helper to measure counters while executing a function in a sandboxed
72 class FunctionExecutor
{
74 virtual ~FunctionExecutor();
75 virtual llvm::Expected
<int64_t>
76 runAndMeasure(const char *Counters
) const = 0;
80 const LLVMState
&State
;
81 const InstructionBenchmark::ModeE Mode
;
84 virtual llvm::Expected
<std::vector
<BenchmarkMeasure
>>
85 runMeasurements(const FunctionExecutor
&Executor
) const = 0;
87 llvm::Expected
<std::string
>
88 writeObjectFile(const BenchmarkCode
&Configuration
,
89 const FillFunction
&Fill
) const;
91 const std::unique_ptr
<ScratchSpace
> Scratch
;
94 } // namespace exegesis
97 #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H