Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 1 | //===-- 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 Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 19 | #include "Assembler.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 20 | #include "BenchmarkResult.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 21 | #include "LlvmState.h" |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 22 | #include "MCInstrDescView.h" |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 23 | #include "RegisterAliasing.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCInst.h" |
| 25 | #include "llvm/Support/Error.h" |
| 26 | #include <vector> |
| 27 | |
| 28 | namespace exegesis { |
| 29 | |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 30 | // A class representing failures that happened during Benchmark, they are used |
| 31 | // to report informations to the user. |
| 32 | class BenchmarkFailure : public llvm::StringError { |
| 33 | public: |
| 34 | BenchmarkFailure(const llvm::Twine &S); |
| 35 | }; |
| 36 | |
Guillaume Chatelet | 7b852cd | 2018-06-07 08:11:54 +0000 | [diff] [blame] | 37 | // A collection of instructions that are to be assembled, executed and measured. |
| 38 | struct 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 Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 42 | struct Setup { |
| 43 | std::vector<unsigned> RegsToDef; |
| 44 | }; |
| 45 | Setup SnippetSetup; |
Guillaume Chatelet | 7b852cd | 2018-06-07 08:11:54 +0000 | [diff] [blame] | 46 | |
| 47 | // The sequence of instructions that are to be repeated. |
| 48 | std::vector<llvm::MCInst> Snippet; |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 49 | |
| 50 | // Informations about how this configuration was built. |
| 51 | std::string Info; |
Guillaume Chatelet | 7b852cd | 2018-06-07 08:11:54 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 54 | // Common code for all benchmark modes. |
| 55 | class BenchmarkRunner { |
| 56 | public: |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 57 | explicit BenchmarkRunner(const LLVMState &State); |
| 58 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 59 | // Subtargets can disable running benchmarks for some instructions by |
| 60 | // returning an error here. |
| 61 | class InstructionFilter { |
| 62 | public: |
| 63 | virtual ~InstructionFilter(); |
| 64 | |
| 65 | virtual llvm::Error shouldRun(const LLVMState &State, |
| 66 | unsigned Opcode) const { |
| 67 | return llvm::ErrorSuccess(); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | virtual ~BenchmarkRunner(); |
| 72 | |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 73 | llvm::Expected<std::vector<InstructionBenchmark>> |
| 74 | run(unsigned Opcode, const InstructionFilter &Filter, |
| 75 | unsigned NumRepetitions); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 76 | |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 77 | // Given a snippet, computes which registers the setup code needs to define. |
| 78 | std::vector<unsigned> |
| 79 | computeRegsToDef(const std::vector<InstructionInstance> &Snippet) const; |
| 80 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 81 | protected: |
| 82 | const LLVMState &State; |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 83 | const RegisterAliasingTrackerCache RATC; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 84 | |
| 85 | private: |
Guillaume Chatelet | b4f1582 | 2018-06-07 14:00:29 +0000 | [diff] [blame] | 86 | InstructionBenchmark runOne(const BenchmarkConfiguration &Configuration, |
| 87 | unsigned Opcode, unsigned NumRepetitions) const; |
| 88 | |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 89 | // Calls generatePrototype and expands the SnippetPrototype into one or more |
| 90 | // BenchmarkConfiguration. |
| 91 | llvm::Expected<std::vector<BenchmarkConfiguration>> |
| 92 | generateConfigurations(unsigned Opcode) const; |
| 93 | |
Clement Courbet | 62b34fa | 2018-06-06 09:42:36 +0000 | [diff] [blame] | 94 | virtual InstructionBenchmark::ModeE getMode() const = 0; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 95 | |
Guillaume Chatelet | ef6cef5 | 2018-06-20 08:52:30 +0000 | [diff] [blame] | 96 | virtual llvm::Expected<SnippetPrototype> |
| 97 | generatePrototype(unsigned Opcode) const = 0; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 98 | |
| 99 | virtual std::vector<BenchmarkMeasure> |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 100 | runMeasurements(const ExecutableFunction &EF, |
| 101 | const unsigned NumRepetitions) const = 0; |
| 102 | |
| 103 | llvm::Expected<std::string> |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 104 | writeObjectFile(const BenchmarkConfiguration::Setup &Setup, |
| 105 | llvm::ArrayRef<llvm::MCInst> Code) const; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | } // namespace exegesis |
| 109 | |
| 110 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRUNNER_H |