blob: 4705e713a922d8c51298fa1d7f8bb6afa247b464 [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 Chateletc9f727b2018-06-13 13:24:41 +000029// A class representing failures that happened during Benchmark, they are used
30// to report informations to the user.
31class BenchmarkFailure : public llvm::StringError {
32public:
33 BenchmarkFailure(const llvm::Twine &S);
34};
35
Guillaume Chatelet7b852cd2018-06-07 08:11:54 +000036// A collection of instructions that are to be assembled, executed and measured.
37struct BenchmarkConfiguration {
38 // This code is run before the Snippet is iterated. Since it is part of the
39 // measurement it should be as short as possible. It is usually used to setup
40 // the content of the Registers.
41 std::vector<llvm::MCInst> SnippetSetup;
42
43 // The sequence of instructions that are to be repeated.
44 std::vector<llvm::MCInst> Snippet;
Guillaume Chateletb4f15822018-06-07 14:00:29 +000045
46 // Informations about how this configuration was built.
47 std::string Info;
Guillaume Chatelet7b852cd2018-06-07 08:11:54 +000048};
49
Clement Courbetac74acd2018-04-04 11:37:06 +000050// Common code for all benchmark modes.
51class BenchmarkRunner {
52public:
Clement Courbet0e69e2d2018-05-17 10:52:18 +000053 explicit BenchmarkRunner(const LLVMState &State);
54
Clement Courbetac74acd2018-04-04 11:37:06 +000055 // Subtargets can disable running benchmarks for some instructions by
56 // returning an error here.
57 class InstructionFilter {
58 public:
59 virtual ~InstructionFilter();
60
61 virtual llvm::Error shouldRun(const LLVMState &State,
62 unsigned Opcode) const {
63 return llvm::ErrorSuccess();
64 }
65 };
66
67 virtual ~BenchmarkRunner();
68
Guillaume Chateletb4f15822018-06-07 14:00:29 +000069 llvm::Expected<std::vector<InstructionBenchmark>>
70 run(unsigned Opcode, const InstructionFilter &Filter,
71 unsigned NumRepetitions);
Clement Courbet0e69e2d2018-05-17 10:52:18 +000072
73protected:
74 const LLVMState &State;
75 const llvm::MCInstrInfo &MCInstrInfo;
76 const llvm::MCRegisterInfo &MCRegisterInfo;
Guillaume Chateletc9f727b2018-06-13 13:24:41 +000077 const RegisterAliasingTrackerCache RATC;
Clement Courbetac74acd2018-04-04 11:37:06 +000078
79private:
Guillaume Chateletb4f15822018-06-07 14:00:29 +000080 InstructionBenchmark runOne(const BenchmarkConfiguration &Configuration,
81 unsigned Opcode, unsigned NumRepetitions) const;
82
Clement Courbet62b34fa2018-06-06 09:42:36 +000083 virtual InstructionBenchmark::ModeE getMode() const = 0;
Clement Courbetac74acd2018-04-04 11:37:06 +000084
Guillaume Chateletb4f15822018-06-07 14:00:29 +000085 virtual llvm::Expected<std::vector<BenchmarkConfiguration>>
Guillaume Chateletc9f727b2018-06-13 13:24:41 +000086 createConfigurations(unsigned Opcode) const = 0;
Clement Courbetac74acd2018-04-04 11:37:06 +000087
88 virtual std::vector<BenchmarkMeasure>
Clement Courbet0e69e2d2018-05-17 10:52:18 +000089 runMeasurements(const ExecutableFunction &EF,
90 const unsigned NumRepetitions) const = 0;
91
92 llvm::Expected<std::string>
93 writeObjectFile(llvm::ArrayRef<llvm::MCInst> Code) const;
Clement Courbet4273e1e2018-06-15 07:30:45 +000094 llvm::Expected<ExecutableFunction>
95 createExecutableFunction(llvm::ArrayRef<llvm::MCInst> Code) const;
Clement Courbetac74acd2018-04-04 11:37:06 +000096};
97
98} // namespace exegesis
99
100#endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H