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 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H
17 #define LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H
18 
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>
29 
30 namespace llvm {
31 namespace exegesis {
32 
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);
38 };
39 
40 // Common code for all benchmark modes.
41 class BenchmarkRunner {
42 public:
43   explicit BenchmarkRunner(const LLVMState &State,
44                            InstructionBenchmark::ModeE Mode);
45 
46   virtual ~BenchmarkRunner();
47 
48   InstructionBenchmark runConfiguration(const BenchmarkCode &Configuration,
49                                         unsigned NumRepetitions) const;
50 
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.
ScratchSpaceScratchSpace55     ScratchSpace()
56         : UnalignedPtr(llvm::make_unique<char[]>(kSize + kAlignment)),
57           AlignedPtr(
58               UnalignedPtr.get() + kAlignment -
59               (reinterpret_cast<intptr_t>(UnalignedPtr.get()) % kAlignment)) {}
ptrScratchSpace60     char *ptr() const { return AlignedPtr; }
clearScratchSpace61     void clear() { std::memset(ptr(), 0, kSize); }
62 
63   private:
64     const std::unique_ptr<char[]> UnalignedPtr;
65     char *const AlignedPtr;
66   };
67 
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;
75   };
76 
77 protected:
78   const LLVMState &State;
79 
80 private:
81   virtual llvm::Expected<std::vector<BenchmarkMeasure>>
82   runMeasurements(const FunctionExecutor &Executor) const = 0;
83 
84   llvm::Expected<std::string>
85   writeObjectFile(const BenchmarkCode &Configuration,
86                   llvm::ArrayRef<llvm::MCInst> Code) const;
87 
88   const InstructionBenchmark::ModeE Mode;
89 
90   const std::unique_ptr<ScratchSpace> Scratch;
91 };
92 
93 } // namespace exegesis
94 } // namespace llvm
95 
96 #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H
97