blob: b0fdb34450ee3e89ee16c62a267cc5064ed92efb [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
33// Common code for all benchmark modes.
34class BenchmarkRunner {
35public:
Clement Courbet2c278cd2018-07-05 12:26:12 +000036 explicit BenchmarkRunner(const LLVMState &State,
37 InstructionBenchmark::ModeE Mode);
Clement Courbetac74acd2018-04-04 11:37:06 +000038
39 virtual ~BenchmarkRunner();
40
Miloš Stojanović20529272020-02-07 13:45:10 +010041 Expected<InstructionBenchmark>
42 runConfiguration(const BenchmarkCode &Configuration, unsigned NumRepetitions,
Roman Lebedevde22d712020-04-02 09:28:35 +030043 ArrayRef<std::unique_ptr<const SnippetRepetitor>> Repetitors,
Miloš Stojanović20529272020-02-07 13:45:10 +010044 bool DumpObjectToDisk) const;
Clement Courbeta51efc22018-06-25 13:12:02 +000045
Guillaume Chateletfb943542018-08-01 14:41:45 +000046 // 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()
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000051 : UnalignedPtr(std::make_unique<char[]>(kSize + kAlignment)),
Guillaume Chateletfb943542018-08-01 14:41:45 +000052 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); }
57
58 private:
59 const std::unique_ptr<char[]> UnalignedPtr;
60 char *const AlignedPtr;
61 };
62
Clement Courbetf973c2d2018-10-17 15:04:15 +000063 // A helper to measure counters while executing a function in a sandboxed
64 // context.
65 class FunctionExecutor {
66 public:
Krasimir Georgiev11bc3a12018-10-18 02:06:16 +000067 virtual ~FunctionExecutor();
Clement Courbet50cdd562019-10-09 11:58:42 +000068 virtual Expected<int64_t> runAndMeasure(const char *Counters) const = 0;
Clement Courbetf973c2d2018-10-17 15:04:15 +000069 };
70
Clement Courbet0e69e2d2018-05-17 10:52:18 +000071protected:
72 const LLVMState &State;
Clement Courbet362653f2019-01-30 16:02:20 +000073 const InstructionBenchmark::ModeE Mode;
Clement Courbet717c9762018-06-28 07:41:16 +000074
Clement Courbetac74acd2018-04-04 11:37:06 +000075private:
Clement Courbet50cdd562019-10-09 11:58:42 +000076 virtual Expected<std::vector<BenchmarkMeasure>>
Clement Courbetf973c2d2018-10-17 15:04:15 +000077 runMeasurements(const FunctionExecutor &Executor) const = 0;
Clement Courbet4860b982018-06-26 08:49:30 +000078
Clement Courbet50cdd562019-10-09 11:58:42 +000079 Expected<std::string> writeObjectFile(const BenchmarkCode &Configuration,
80 const FillFunction &Fill) const;
Clement Courbet4860b982018-06-26 08:49:30 +000081
Guillaume Chateletfb943542018-08-01 14:41:45 +000082 const std::unique_ptr<ScratchSpace> Scratch;
Clement Courbetac74acd2018-04-04 11:37:06 +000083};
84
85} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +000086} // namespace llvm
Clement Courbetac74acd2018-04-04 11:37:06 +000087
88#endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H