• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- BenchmarkRunner.h ---------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// Defines the abstract BenchmarkRunner class for measuring a certain execution
11 /// property of instructions (e.g. latency).
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H
16 #define LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H
17 
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/ADT/SmallVector.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/Support/Error.h"
27 #include <cstdlib>
28 #include <memory>
29 #include <vector>
30 
31 namespace llvm {
32 namespace exegesis {
33 
34 // Common code for all benchmark modes.
35 class BenchmarkRunner {
36 public:
37   explicit BenchmarkRunner(const LLVMState &State,
38                            InstructionBenchmark::ModeE Mode);
39 
40   virtual ~BenchmarkRunner();
41 
42   Expected<InstructionBenchmark>
43   runConfiguration(const BenchmarkCode &Configuration, unsigned NumRepetitions,
44                    ArrayRef<std::unique_ptr<const SnippetRepetitor>> Repetitors,
45                    bool DumpObjectToDisk) const;
46 
47   // Scratch space to run instructions that touch memory.
48   struct ScratchSpace {
49     static constexpr const size_t kAlignment = 1024;
50     static constexpr const size_t kSize = 1 << 20; // 1MB.
ScratchSpaceScratchSpace51     ScratchSpace()
52         : UnalignedPtr(std::make_unique<char[]>(kSize + kAlignment)),
53           AlignedPtr(
54               UnalignedPtr.get() + kAlignment -
55               (reinterpret_cast<intptr_t>(UnalignedPtr.get()) % kAlignment)) {}
ptrScratchSpace56     char *ptr() const { return AlignedPtr; }
clearScratchSpace57     void clear() { std::memset(ptr(), 0, kSize); }
58 
59   private:
60     const std::unique_ptr<char[]> UnalignedPtr;
61     char *const AlignedPtr;
62   };
63 
64   // A helper to measure counters while executing a function in a sandboxed
65   // context.
66   class FunctionExecutor {
67   public:
68     virtual ~FunctionExecutor();
69     // FIXME deprecate this.
70     virtual Expected<int64_t> runAndMeasure(const char *Counters) const = 0;
71 
72     virtual Expected<llvm::SmallVector<int64_t, 4>>
73     runAndSample(const char *Counters) const = 0;
74   };
75 
76 protected:
77   const LLVMState &State;
78   const InstructionBenchmark::ModeE Mode;
79 
80 private:
81   virtual Expected<std::vector<BenchmarkMeasure>>
82   runMeasurements(const FunctionExecutor &Executor) const = 0;
83 
84   Expected<std::string> writeObjectFile(const BenchmarkCode &Configuration,
85                                         const FillFunction &Fill) const;
86 
87   const std::unique_ptr<ScratchSpace> Scratch;
88 };
89 
90 } // namespace exegesis
91 } // namespace llvm
92 
93 #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H
94