blob: 4387bc8456e858ec03388e6a685beb8a577deb30 [file] [log] [blame]
Clement Courbetac74acd2018-04-04 11:37:06 +00001//===-- BenchmarkRunner.h ---------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Clement Courbetac74acd2018-04-04 11:37:06 +00006//
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
Clement Courbet0e69e2d2018-05-17 10:52:18 +000018#include "Assembler.h"
Clement Courbetd939f6d2018-09-13 07:40:53 +000019#include "BenchmarkCode.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000020#include "BenchmarkResult.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000021#include "LlvmState.h"
Guillaume Chateletef6cef52018-06-20 08:52:30 +000022#include "MCInstrDescView.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000023#include "llvm/MC/MCInst.h"
24#include "llvm/Support/Error.h"
Guillaume Chateletfb943542018-08-01 14:41:45 +000025#include <cstdlib>
26#include <memory>
Clement Courbetac74acd2018-04-04 11:37:06 +000027#include <vector>
28
Fangrui Song32401af2018-10-22 17:10:47 +000029namespace llvm {
Clement Courbetac74acd2018-04-04 11:37:06 +000030namespace exegesis {
31
Guillaume Chateletc9f727b2018-06-13 13:24:41 +000032// A class representing failures that happened during Benchmark, they are used
33// to report informations to the user.
34class BenchmarkFailure : public llvm::StringError {
35public:
36 BenchmarkFailure(const llvm::Twine &S);
37};
38
Clement Courbetac74acd2018-04-04 11:37:06 +000039// Common code for all benchmark modes.
40class BenchmarkRunner {
41public:
Clement Courbet2c278cd2018-07-05 12:26:12 +000042 explicit BenchmarkRunner(const LLVMState &State,
43 InstructionBenchmark::ModeE Mode);
Clement Courbetac74acd2018-04-04 11:37:06 +000044
45 virtual ~BenchmarkRunner();
46
Clement Courbetd939f6d2018-09-13 07:40:53 +000047 InstructionBenchmark runConfiguration(const BenchmarkCode &Configuration,
48 unsigned NumRepetitions) const;
Clement Courbeta51efc22018-06-25 13:12:02 +000049
Guillaume Chateletfb943542018-08-01 14:41:45 +000050 // Scratch space to run instructions that touch memory.
51 struct ScratchSpace {
52 static constexpr const size_t kAlignment = 1024;
53 static constexpr const size_t kSize = 1 << 20; // 1MB.
54 ScratchSpace()
55 : UnalignedPtr(llvm::make_unique<char[]>(kSize + kAlignment)),
56 AlignedPtr(
57 UnalignedPtr.get() + kAlignment -
58 (reinterpret_cast<intptr_t>(UnalignedPtr.get()) % kAlignment)) {}
59 char *ptr() const { return AlignedPtr; }
60 void clear() { std::memset(ptr(), 0, kSize); }
61
62 private:
63 const std::unique_ptr<char[]> UnalignedPtr;
64 char *const AlignedPtr;
65 };
66
Clement Courbetf973c2d2018-10-17 15:04:15 +000067 // A helper to measure counters while executing a function in a sandboxed
68 // context.
69 class FunctionExecutor {
70 public:
Krasimir Georgiev11bc3a12018-10-18 02:06:16 +000071 virtual ~FunctionExecutor();
Clement Courbetf973c2d2018-10-17 15:04:15 +000072 virtual llvm::Expected<int64_t>
73 runAndMeasure(const char *Counters) const = 0;
74 };
75
Clement Courbet0e69e2d2018-05-17 10:52:18 +000076protected:
77 const LLVMState &State;
Clement Courbet362653f2019-01-30 16:02:20 +000078 const InstructionBenchmark::ModeE Mode;
Clement Courbet717c9762018-06-28 07:41:16 +000079
Clement Courbetac74acd2018-04-04 11:37:06 +000080private:
Clement Courbetf973c2d2018-10-17 15:04:15 +000081 virtual llvm::Expected<std::vector<BenchmarkMeasure>>
82 runMeasurements(const FunctionExecutor &Executor) const = 0;
Clement Courbet4860b982018-06-26 08:49:30 +000083
Clement Courbet0e69e2d2018-05-17 10:52:18 +000084 llvm::Expected<std::string>
Guillaume Chatelete60866a2018-08-03 09:29:38 +000085 writeObjectFile(const BenchmarkCode &Configuration,
Clement Courbeta51efc22018-06-25 13:12:02 +000086 llvm::ArrayRef<llvm::MCInst> Code) const;
Clement Courbet4860b982018-06-26 08:49:30 +000087
Guillaume Chateletfb943542018-08-01 14:41:45 +000088
89 const std::unique_ptr<ScratchSpace> Scratch;
Clement Courbetac74acd2018-04-04 11:37:06 +000090};
91
92} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +000093} // namespace llvm
Clement Courbetac74acd2018-04-04 11:37:06 +000094
95#endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H