blob: cb3f9569ed0beb0197b3384261cfef659f0f5c7d [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 Courbetac74acd2018-04-04 11:37:06 +000020#include "BenchmarkResult.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000021#include "LlvmState.h"
Clement Courbet0e69e2d2018-05-17 10:52:18 +000022#include "RegisterAliasing.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000023#include "llvm/MC/MCInst.h"
24#include "llvm/Support/Error.h"
25#include <vector>
26
27namespace exegesis {
28
Guillaume Chatelet7b852cd2018-06-07 08:11:54 +000029// A collection of instructions that are to be assembled, executed and measured.
30struct BenchmarkConfiguration {
31 // This code is run before the Snippet is iterated. Since it is part of the
32 // measurement it should be as short as possible. It is usually used to setup
33 // the content of the Registers.
34 std::vector<llvm::MCInst> SnippetSetup;
35
36 // The sequence of instructions that are to be repeated.
37 std::vector<llvm::MCInst> Snippet;
Guillaume Chateletb4f15822018-06-07 14:00:29 +000038
39 // Informations about how this configuration was built.
40 std::string Info;
Guillaume Chatelet7b852cd2018-06-07 08:11:54 +000041};
42
Clement Courbetac74acd2018-04-04 11:37:06 +000043// Common code for all benchmark modes.
44class BenchmarkRunner {
45public:
Clement Courbet0e69e2d2018-05-17 10:52:18 +000046 explicit BenchmarkRunner(const LLVMState &State);
47
Clement Courbetac74acd2018-04-04 11:37:06 +000048 // Subtargets can disable running benchmarks for some instructions by
49 // returning an error here.
50 class InstructionFilter {
51 public:
52 virtual ~InstructionFilter();
53
54 virtual llvm::Error shouldRun(const LLVMState &State,
55 unsigned Opcode) const {
56 return llvm::ErrorSuccess();
57 }
58 };
59
60 virtual ~BenchmarkRunner();
61
Guillaume Chateletb4f15822018-06-07 14:00:29 +000062 llvm::Expected<std::vector<InstructionBenchmark>>
63 run(unsigned Opcode, const InstructionFilter &Filter,
64 unsigned NumRepetitions);
Clement Courbet0e69e2d2018-05-17 10:52:18 +000065
66protected:
67 const LLVMState &State;
68 const llvm::MCInstrInfo &MCInstrInfo;
69 const llvm::MCRegisterInfo &MCRegisterInfo;
Clement Courbetac74acd2018-04-04 11:37:06 +000070
71private:
Guillaume Chateletb4f15822018-06-07 14:00:29 +000072 InstructionBenchmark runOne(const BenchmarkConfiguration &Configuration,
73 unsigned Opcode, unsigned NumRepetitions) const;
74
Clement Courbet62b34fa2018-06-06 09:42:36 +000075 virtual InstructionBenchmark::ModeE getMode() const = 0;
Clement Courbetac74acd2018-04-04 11:37:06 +000076
Guillaume Chateletb4f15822018-06-07 14:00:29 +000077 virtual llvm::Expected<std::vector<BenchmarkConfiguration>>
78 createConfigurations(RegisterAliasingTrackerCache &RATC,
79 unsigned Opcode) const = 0;
Clement Courbetac74acd2018-04-04 11:37:06 +000080
81 virtual std::vector<BenchmarkMeasure>
Clement Courbet0e69e2d2018-05-17 10:52:18 +000082 runMeasurements(const ExecutableFunction &EF,
83 const unsigned NumRepetitions) const = 0;
84
85 llvm::Expected<std::string>
86 writeObjectFile(llvm::ArrayRef<llvm::MCInst> Code) const;
87
88 RegisterAliasingTrackerCache RATC;
Clement Courbetac74acd2018-04-04 11:37:06 +000089};
90
91} // namespace exegesis
92
93#endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H