blob: 13227a7bfb3007150f9ad40f1ccef554cab8cbd9 [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 Courbet9431b722019-09-27 12:56:24 +000023#include "SnippetRepetitor.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
Fangrui Song32401af2018-10-22 17:10:47 +000030namespace llvm {
Clement Courbetac74acd2018-04-04 11:37:06 +000031namespace exegesis {
32
Guillaume Chateletc9f727b2018-06-13 13:24:41 +000033// A class representing failures that happened during Benchmark, they are used
34// to report informations to the user.
35class BenchmarkFailure : public llvm::StringError {
36public:
37 BenchmarkFailure(const llvm::Twine &S);
38};
39
Clement Courbetac74acd2018-04-04 11:37:06 +000040// Common code for all benchmark modes.
41class BenchmarkRunner {
42public:
Clement Courbet2c278cd2018-07-05 12:26:12 +000043 explicit BenchmarkRunner(const LLVMState &State,
44 InstructionBenchmark::ModeE Mode);
Clement Courbetac74acd2018-04-04 11:37:06 +000045
46 virtual ~BenchmarkRunner();
47
Clement Courbetd939f6d2018-09-13 07:40:53 +000048 InstructionBenchmark runConfiguration(const BenchmarkCode &Configuration,
Guillaume Chatelet848df5b2019-04-05 15:18:59 +000049 unsigned NumRepetitions,
Clement Courbet9431b722019-09-27 12:56:24 +000050 const SnippetRepetitor &Repetitor,
Guillaume Chatelet848df5b2019-04-05 15:18:59 +000051 bool DumpObjectToDisk) const;
Clement Courbeta51efc22018-06-25 13:12:02 +000052
Guillaume Chateletfb943542018-08-01 14:41:45 +000053 // Scratch space to run instructions that touch memory.
54 struct ScratchSpace {
55 static constexpr const size_t kAlignment = 1024;
56 static constexpr const size_t kSize = 1 << 20; // 1MB.
57 ScratchSpace()
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000058 : UnalignedPtr(std::make_unique<char[]>(kSize + kAlignment)),
Guillaume Chateletfb943542018-08-01 14:41:45 +000059 AlignedPtr(
60 UnalignedPtr.get() + kAlignment -
61 (reinterpret_cast<intptr_t>(UnalignedPtr.get()) % kAlignment)) {}
62 char *ptr() const { return AlignedPtr; }
63 void clear() { std::memset(ptr(), 0, kSize); }
64
65 private:
66 const std::unique_ptr<char[]> UnalignedPtr;
67 char *const AlignedPtr;
68 };
69
Clement Courbetf973c2d2018-10-17 15:04:15 +000070 // A helper to measure counters while executing a function in a sandboxed
71 // context.
72 class FunctionExecutor {
73 public:
Krasimir Georgiev11bc3a12018-10-18 02:06:16 +000074 virtual ~FunctionExecutor();
Clement Courbetf973c2d2018-10-17 15:04:15 +000075 virtual llvm::Expected<int64_t>
76 runAndMeasure(const char *Counters) const = 0;
77 };
78
Clement Courbet0e69e2d2018-05-17 10:52:18 +000079protected:
80 const LLVMState &State;
Clement Courbet362653f2019-01-30 16:02:20 +000081 const InstructionBenchmark::ModeE Mode;
Clement Courbet717c9762018-06-28 07:41:16 +000082
Clement Courbetac74acd2018-04-04 11:37:06 +000083private:
Clement Courbetf973c2d2018-10-17 15:04:15 +000084 virtual llvm::Expected<std::vector<BenchmarkMeasure>>
85 runMeasurements(const FunctionExecutor &Executor) const = 0;
Clement Courbet4860b982018-06-26 08:49:30 +000086
Clement Courbet0e69e2d2018-05-17 10:52:18 +000087 llvm::Expected<std::string>
Guillaume Chatelete60866a2018-08-03 09:29:38 +000088 writeObjectFile(const BenchmarkCode &Configuration,
Clement Courbet9431b722019-09-27 12:56:24 +000089 const FillFunction &Fill) const;
Clement Courbet4860b982018-06-26 08:49:30 +000090
Guillaume Chateletfb943542018-08-01 14:41:45 +000091 const std::unique_ptr<ScratchSpace> Scratch;
Clement Courbetac74acd2018-04-04 11:37:06 +000092};
93
94} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +000095} // namespace llvm
Clement Courbetac74acd2018-04-04 11:37:06 +000096
97#endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H