blob: 07ab23c336b2a9817dbcaae002e04d53942f9d0f [file] [log] [blame]
Clement Courbetac74acd2018-04-04 11:37:06 +00001//===-- 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
Clement Courbet0e69e2d2018-05-17 10:52:18 +000019#include "Assembler.h"
Clement Courbetd939f6d2018-09-13 07:40:53 +000020#include "BenchmarkCode.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000021#include "BenchmarkResult.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000022#include "LlvmState.h"
Guillaume Chateletef6cef52018-06-20 08:52:30 +000023#include "MCInstrDescView.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000024#include "llvm/MC/MCInst.h"
25#include "llvm/Support/Error.h"
Guillaume Chateletfb943542018-08-01 14:41:45 +000026#include <cstdlib>
27#include <memory>
Clement Courbetac74acd2018-04-04 11:37:06 +000028#include <vector>
29
30namespace 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 Courbet0e69e2d2018-05-17 10:52:18 +000067protected:
68 const LLVMState &State;
Clement Courbet717c9762018-06-28 07:41:16 +000069
Clement Courbetac74acd2018-04-04 11:37:06 +000070private:
Clement Courbet4860b982018-06-26 08:49:30 +000071 virtual std::vector<BenchmarkMeasure>
Guillaume Chateletfb943542018-08-01 14:41:45 +000072 runMeasurements(const ExecutableFunction &EF, ScratchSpace &Scratch,
Clement Courbet2c278cd2018-07-05 12:26:12 +000073 const unsigned NumRepetitions) const = 0;
Clement Courbet4860b982018-06-26 08:49:30 +000074
Clement Courbet0e69e2d2018-05-17 10:52:18 +000075 llvm::Expected<std::string>
Guillaume Chatelete60866a2018-08-03 09:29:38 +000076 writeObjectFile(const BenchmarkCode &Configuration,
Clement Courbeta51efc22018-06-25 13:12:02 +000077 llvm::ArrayRef<llvm::MCInst> Code) const;
Clement Courbet4860b982018-06-26 08:49:30 +000078
79 const InstructionBenchmark::ModeE Mode;
Guillaume Chateletfb943542018-08-01 14:41:45 +000080
81 const std::unique_ptr<ScratchSpace> Scratch;
Clement Courbetac74acd2018-04-04 11:37:06 +000082};
83
84} // namespace exegesis
85
86#endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H