[llvm-exegesis] Implements a cache of Instruction objects.
[llvm-core.git] / tools / llvm-exegesis / lib / BenchmarkRunner.h
blob4f77f492ab4b09e6b64df77f276b836d63e3ccee
1 //===-- BenchmarkRunner.h ---------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// Defines the abstract BenchmarkRunner class for measuring a certain execution
12 /// property of instructions (e.g. latency).
13 ///
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"
26 #include <cstdlib>
27 #include <memory>
28 #include <vector>
30 namespace llvm {
31 namespace exegesis {
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 {
36 public:
37 BenchmarkFailure(const llvm::Twine &S);
40 // Common code for all benchmark modes.
41 class BenchmarkRunner {
42 public:
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.
52 struct ScratchSpace {
53 static constexpr const size_t kAlignment = 1024;
54 static constexpr const size_t kSize = 1 << 20; // 1MB.
55 ScratchSpace()
56 : UnalignedPtr(llvm::make_unique<char[]>(kSize + kAlignment)),
57 AlignedPtr(
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); }
63 private:
64 const std::unique_ptr<char[]> UnalignedPtr;
65 char *const AlignedPtr;
68 // A helper to measure counters while executing a function in a sandboxed
69 // context.
70 class FunctionExecutor {
71 public:
72 virtual ~FunctionExecutor();
73 virtual llvm::Expected<int64_t>
74 runAndMeasure(const char *Counters) const = 0;
77 protected:
78 const LLVMState &State;
80 private:
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
94 } // namespace llvm
96 #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H