blob: 91d2c8e2d8c0b6b255bb42e231469e745d90f7ee [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"
Guillaume Chateletef6cef52018-06-20 08:52:30 +000022#include "MCInstrDescView.h"
Clement Courbet0e69e2d2018-05-17 10:52:18 +000023#include "RegisterAliasing.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000024#include "llvm/MC/MCInst.h"
25#include "llvm/Support/Error.h"
26#include <vector>
27
28namespace exegesis {
29
Guillaume Chateletc9f727b2018-06-13 13:24:41 +000030// A class representing failures that happened during Benchmark, they are used
31// to report informations to the user.
32class BenchmarkFailure : public llvm::StringError {
33public:
34 BenchmarkFailure(const llvm::Twine &S);
35};
36
Guillaume Chatelet7b852cd2018-06-07 08:11:54 +000037// A collection of instructions that are to be assembled, executed and measured.
38struct BenchmarkConfiguration {
39 // This code is run before the Snippet is iterated. Since it is part of the
40 // measurement it should be as short as possible. It is usually used to setup
41 // the content of the Registers.
Clement Courbeta51efc22018-06-25 13:12:02 +000042 struct Setup {
43 std::vector<unsigned> RegsToDef;
44 };
45 Setup SnippetSetup;
Guillaume Chatelet7b852cd2018-06-07 08:11:54 +000046
47 // The sequence of instructions that are to be repeated.
48 std::vector<llvm::MCInst> Snippet;
Guillaume Chateletb4f15822018-06-07 14:00:29 +000049
50 // Informations about how this configuration was built.
51 std::string Info;
Guillaume Chatelet7b852cd2018-06-07 08:11:54 +000052};
53
Clement Courbetac74acd2018-04-04 11:37:06 +000054// Common code for all benchmark modes.
55class BenchmarkRunner {
56public:
Clement Courbet2c278cd2018-07-05 12:26:12 +000057 explicit BenchmarkRunner(const LLVMState &State,
58 InstructionBenchmark::ModeE Mode);
Clement Courbetac74acd2018-04-04 11:37:06 +000059
60 virtual ~BenchmarkRunner();
61
Guillaume Chateletb4f15822018-06-07 14:00:29 +000062 llvm::Expected<std::vector<InstructionBenchmark>>
Clement Courbet4860b982018-06-26 08:49:30 +000063 run(unsigned Opcode, unsigned NumRepetitions);
Clement Courbet0e69e2d2018-05-17 10:52:18 +000064
Clement Courbeta51efc22018-06-25 13:12:02 +000065 // Given a snippet, computes which registers the setup code needs to define.
66 std::vector<unsigned>
67 computeRegsToDef(const std::vector<InstructionInstance> &Snippet) const;
68
Clement Courbet0e69e2d2018-05-17 10:52:18 +000069protected:
70 const LLVMState &State;
Guillaume Chateletc9f727b2018-06-13 13:24:41 +000071 const RegisterAliasingTrackerCache RATC;
Clement Courbetac74acd2018-04-04 11:37:06 +000072
Clement Courbetf9a0bb32018-07-05 13:54:51 +000073 // Generates a single instruction prototype that has a self-dependency.
Clement Courbet2c278cd2018-07-05 12:26:12 +000074 llvm::Expected<SnippetPrototype>
75 generateSelfAliasingPrototype(const Instruction &Instr) const;
Clement Courbetf9a0bb32018-07-05 13:54:51 +000076 // Generates a single instruction prototype without assignment constraints.
77 llvm::Expected<SnippetPrototype>
78 generateUnconstrainedPrototype(const Instruction &Instr,
79 llvm::StringRef Msg) const;
Clement Courbet717c9762018-06-28 07:41:16 +000080
Clement Courbetac74acd2018-04-04 11:37:06 +000081private:
Clement Courbet4860b982018-06-26 08:49:30 +000082 // API to be implemented by subclasses.
83 virtual llvm::Expected<SnippetPrototype>
Clement Courbet2c278cd2018-07-05 12:26:12 +000084 generatePrototype(unsigned Opcode) const = 0;
Clement Courbet4860b982018-06-26 08:49:30 +000085
86 virtual std::vector<BenchmarkMeasure>
Clement Courbet2c278cd2018-07-05 12:26:12 +000087 runMeasurements(const ExecutableFunction &EF,
88 const unsigned NumRepetitions) const = 0;
Clement Courbet4860b982018-06-26 08:49:30 +000089
90 // Internal helpers.
Guillaume Chateletb4f15822018-06-07 14:00:29 +000091 InstructionBenchmark runOne(const BenchmarkConfiguration &Configuration,
92 unsigned Opcode, unsigned NumRepetitions) const;
93
Guillaume Chateletef6cef52018-06-20 08:52:30 +000094 // Calls generatePrototype and expands the SnippetPrototype into one or more
95 // BenchmarkConfiguration.
96 llvm::Expected<std::vector<BenchmarkConfiguration>>
97 generateConfigurations(unsigned Opcode) const;
98
Clement Courbet0e69e2d2018-05-17 10:52:18 +000099 llvm::Expected<std::string>
Clement Courbeta51efc22018-06-25 13:12:02 +0000100 writeObjectFile(const BenchmarkConfiguration::Setup &Setup,
101 llvm::ArrayRef<llvm::MCInst> Code) const;
Clement Courbet4860b982018-06-26 08:49:30 +0000102
103 const InstructionBenchmark::ModeE Mode;
Clement Courbetac74acd2018-04-04 11:37:06 +0000104};
105
106} // namespace exegesis
107
108#endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H