1 //===-- BenchmarkRunner.h ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// Defines the abstract BenchmarkRunner class for measuring a certain execution
12 /// property of instructions (e.g. latency).
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H
17 #define LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H
19 #include "Assembler.h"
20 #include "BenchmarkCode.h"
21 #include "BenchmarkResult.h"
22 #include "LlvmState.h"
23 #include "MCInstrDescView.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
) const;
51 // Scratch space to run instructions that touch memory.
53 static constexpr const size_t kAlignment
= 1024;
54 static constexpr const size_t kSize
= 1 << 20; // 1MB.
56 : UnalignedPtr(llvm::make_unique
<char[]>(kSize
+ kAlignment
)),
58 UnalignedPtr
.get() + kAlignment
-
59 (reinterpret_cast<intptr_t>(UnalignedPtr
.get()) % kAlignment
)) {}
60 char *ptr() const { return AlignedPtr
; }
61 void clear() { std::memset(ptr(), 0, kSize
); }
64 const std::unique_ptr
<char[]> UnalignedPtr
;
65 char *const AlignedPtr
;
68 // A helper to measure counters while executing a function in a sandboxed
70 class FunctionExecutor
{
72 virtual ~FunctionExecutor();
73 virtual llvm::Expected
<int64_t>
74 runAndMeasure(const char *Counters
) const = 0;
78 const LLVMState
&State
;
81 virtual llvm::Expected
<std::vector
<BenchmarkMeasure
>>
82 runMeasurements(const FunctionExecutor
&Executor
) const = 0;
84 llvm::Expected
<std::string
>
85 writeObjectFile(const BenchmarkCode
&Configuration
,
86 llvm::ArrayRef
<llvm::MCInst
> Code
) const;
88 const InstructionBenchmark::ModeE Mode
;
90 const std::unique_ptr
<ScratchSpace
> Scratch
;
93 } // namespace exegesis
96 #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H