Add an option do not dump the generated object on disk
[llvm-complete.git] / tools / llvm-exegesis / lib / BenchmarkRunner.h
blob395c0f8e32dbd394505325e009e0e73a77ec1be1
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 //===----------------------------------------------------------------------===//
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 "llvm/MC/MCInst.h"
24 #include "llvm/Support/Error.h"
25 #include <cstdlib>
26 #include <memory>
27 #include <vector>
29 namespace llvm {
30 namespace exegesis {
32 // A class representing failures that happened during Benchmark, they are used
33 // to report informations to the user.
34 class BenchmarkFailure : public llvm::StringError {
35 public:
36 BenchmarkFailure(const llvm::Twine &S);
39 // Common code for all benchmark modes.
40 class BenchmarkRunner {
41 public:
42 explicit BenchmarkRunner(const LLVMState &State,
43 InstructionBenchmark::ModeE Mode);
45 virtual ~BenchmarkRunner();
47 InstructionBenchmark runConfiguration(const BenchmarkCode &Configuration,
48 unsigned NumRepetitions,
49 bool DumpObjectToDisk) 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;
79 const InstructionBenchmark::ModeE Mode;
81 private:
82 virtual llvm::Expected<std::vector<BenchmarkMeasure>>
83 runMeasurements(const FunctionExecutor &Executor) const = 0;
85 llvm::Expected<std::string>
86 writeObjectFile(const BenchmarkCode &Configuration,
87 llvm::ArrayRef<llvm::MCInst> Code) const;
89 const std::unique_ptr<ScratchSpace> Scratch;
92 } // namespace exegesis
93 } // namespace llvm
95 #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H