blob: 29e98619301aec8ea299d5053b4990b0a558aae1 [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,
Guillaume Chatelet848df5b2019-04-05 15:18:59 +000048 unsigned NumRepetitions,
49 bool DumpObjectToDisk) const;
Clement Courbeta51efc22018-06-25 13:12:02 +000050
Guillaume Chateletfb943542018-08-01 14:41:45 +000051 // 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()
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000056 : UnalignedPtr(std::make_unique<char[]>(kSize + kAlignment)),
Guillaume Chateletfb943542018-08-01 14:41:45 +000057 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); }
62
63 private:
64 const std::unique_ptr<char[]> UnalignedPtr;
65 char *const AlignedPtr;
66 };
67
Clement Courbetf973c2d2018-10-17 15:04:15 +000068 // A helper to measure counters while executing a function in a sandboxed
69 // context.
70 class FunctionExecutor {
71 public:
Krasimir Georgiev11bc3a12018-10-18 02:06:16 +000072 virtual ~FunctionExecutor();
Clement Courbetf973c2d2018-10-17 15:04:15 +000073 virtual llvm::Expected<int64_t>
74 runAndMeasure(const char *Counters) const = 0;
75 };
76
Clement Courbet0e69e2d2018-05-17 10:52:18 +000077protected:
78 const LLVMState &State;
Clement Courbet362653f2019-01-30 16:02:20 +000079 const InstructionBenchmark::ModeE Mode;
Clement Courbet717c9762018-06-28 07:41:16 +000080
Clement Courbetac74acd2018-04-04 11:37:06 +000081private:
Clement Courbetf973c2d2018-10-17 15:04:15 +000082 virtual llvm::Expected<std::vector<BenchmarkMeasure>>
83 runMeasurements(const FunctionExecutor &Executor) const = 0;
Clement Courbet4860b982018-06-26 08:49:30 +000084
Clement Courbet0e69e2d2018-05-17 10:52:18 +000085 llvm::Expected<std::string>
Guillaume Chatelete60866a2018-08-03 09:29:38 +000086 writeObjectFile(const BenchmarkCode &Configuration,
Clement Courbeta51efc22018-06-25 13:12:02 +000087 llvm::ArrayRef<llvm::MCInst> Code) const;
Clement Courbet4860b982018-06-26 08:49:30 +000088
Guillaume Chateletfb943542018-08-01 14:41:45 +000089 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