blob: 8907cccc4ddd822bf717b23c495167d3d5073882 [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;
38};
39
Clement Courbetac74acd2018-04-04 11:37:06 +000040// Common code for all benchmark modes.
41class BenchmarkRunner {
42public:
Clement Courbet0e69e2d2018-05-17 10:52:18 +000043 explicit BenchmarkRunner(const LLVMState &State);
44
Clement Courbetac74acd2018-04-04 11:37:06 +000045 // Subtargets can disable running benchmarks for some instructions by
46 // returning an error here.
47 class InstructionFilter {
48 public:
49 virtual ~InstructionFilter();
50
51 virtual llvm::Error shouldRun(const LLVMState &State,
52 unsigned Opcode) const {
53 return llvm::ErrorSuccess();
54 }
55 };
56
57 virtual ~BenchmarkRunner();
58
Clement Courbet0e69e2d2018-05-17 10:52:18 +000059 InstructionBenchmark run(unsigned Opcode, const InstructionFilter &Filter,
60 unsigned NumRepetitions);
61
62protected:
63 const LLVMState &State;
64 const llvm::MCInstrInfo &MCInstrInfo;
65 const llvm::MCRegisterInfo &MCRegisterInfo;
Clement Courbetac74acd2018-04-04 11:37:06 +000066
67private:
Clement Courbet62b34fa2018-06-06 09:42:36 +000068 virtual InstructionBenchmark::ModeE getMode() const = 0;
Clement Courbetac74acd2018-04-04 11:37:06 +000069
Guillaume Chatelet7b852cd2018-06-07 08:11:54 +000070 virtual llvm::Expected<BenchmarkConfiguration>
71 createConfiguration(RegisterAliasingTrackerCache &RATC, unsigned Opcode,
72 llvm::raw_ostream &Debug) const = 0;
Clement Courbetac74acd2018-04-04 11:37:06 +000073
74 virtual std::vector<BenchmarkMeasure>
Clement Courbet0e69e2d2018-05-17 10:52:18 +000075 runMeasurements(const ExecutableFunction &EF,
76 const unsigned NumRepetitions) const = 0;
77
78 llvm::Expected<std::string>
79 writeObjectFile(llvm::ArrayRef<llvm::MCInst> Code) const;
80
81 RegisterAliasingTrackerCache RATC;
Clement Courbetac74acd2018-04-04 11:37:06 +000082};
83
84} // namespace exegesis
85
86#endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H