[UpdateCCTestChecks] Detect function mangled name on separate line
[llvm-core.git] / tools / llvm-exegesis / lib / BenchmarkRunner.h
blob2f6ea0f5b5a18b4f98fe8fa994a29fa3203587f7
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 "SnippetRepetitor.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 // Common code for all benchmark modes.
34 class BenchmarkRunner {
35 public:
36 explicit BenchmarkRunner(const LLVMState &State,
37 InstructionBenchmark::ModeE Mode);
39 virtual ~BenchmarkRunner();
41 InstructionBenchmark runConfiguration(const BenchmarkCode &Configuration,
42 unsigned NumRepetitions,
43 const SnippetRepetitor &Repetitor,
44 bool DumpObjectToDisk) const;
46 // Scratch space to run instructions that touch memory.
47 struct ScratchSpace {
48 static constexpr const size_t kAlignment = 1024;
49 static constexpr const size_t kSize = 1 << 20; // 1MB.
50 ScratchSpace()
51 : UnalignedPtr(std::make_unique<char[]>(kSize + kAlignment)),
52 AlignedPtr(
53 UnalignedPtr.get() + kAlignment -
54 (reinterpret_cast<intptr_t>(UnalignedPtr.get()) % kAlignment)) {}
55 char *ptr() const { return AlignedPtr; }
56 void clear() { std::memset(ptr(), 0, kSize); }
58 private:
59 const std::unique_ptr<char[]> UnalignedPtr;
60 char *const AlignedPtr;
63 // A helper to measure counters while executing a function in a sandboxed
64 // context.
65 class FunctionExecutor {
66 public:
67 virtual ~FunctionExecutor();
68 virtual llvm::Expected<int64_t>
69 runAndMeasure(const char *Counters) const = 0;
72 protected:
73 const LLVMState &State;
74 const InstructionBenchmark::ModeE Mode;
76 private:
77 virtual llvm::Expected<std::vector<BenchmarkMeasure>>
78 runMeasurements(const FunctionExecutor &Executor) const = 0;
80 llvm::Expected<std::string>
81 writeObjectFile(const BenchmarkCode &Configuration,
82 const FillFunction &Fill) const;
84 const std::unique_ptr<ScratchSpace> Scratch;
87 } // namespace exegesis
88 } // namespace llvm
90 #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H