[llvm-exegesis] Implements a cache of Instruction objects.
[llvm-core.git] / tools / llvm-exegesis / lib / BenchmarkResult.h
blob773a2e50abc4d70053db917658120d236bf5edbc
1 //===-- BenchmarkResult.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 classes to represent measurements and serialize/deserialize them to
12 // Yaml.
13 ///
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H
17 #define LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H
19 #include "BenchmarkCode.h"
20 #include "LlvmState.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCInstBuilder.h"
25 #include "llvm/Support/YAMLTraits.h"
26 #include <limits>
27 #include <string>
28 #include <unordered_map>
29 #include <vector>
31 namespace llvm {
32 namespace exegesis {
34 struct InstructionBenchmarkKey {
35 // The LLVM opcode name.
36 std::vector<llvm::MCInst> Instructions;
37 // The initial values of the registers.
38 std::vector<RegisterValue> RegisterInitialValues;
39 // An opaque configuration, that can be used to separate several benchmarks of
40 // the same instruction under different configurations.
41 std::string Config;
44 struct BenchmarkMeasure {
45 // A helper to create an unscaled BenchmarkMeasure.
46 static BenchmarkMeasure Create(std::string Key, double Value) {
47 return {Key, Value, Value};
49 std::string Key;
50 // This is the per-instruction value, i.e. measured quantity scaled per
51 // instruction.
52 double PerInstructionValue;
53 // This is the per-snippet value, i.e. measured quantity for one repetition of
54 // the whole snippet.
55 double PerSnippetValue;
58 // The result of an instruction benchmark.
59 struct InstructionBenchmark {
60 InstructionBenchmarkKey Key;
61 enum ModeE { Unknown, Latency, Uops };
62 ModeE Mode;
63 std::string CpuName;
64 std::string LLVMTriple;
65 // The number of instructions inside the repeated snippet. For example, if a
66 // snippet of 3 instructions is repeated 4 times, this is 12.
67 int NumRepetitions = 0;
68 // Note that measurements are per instruction.
69 std::vector<BenchmarkMeasure> Measurements;
70 std::string Error;
71 std::string Info;
72 std::vector<uint8_t> AssembledSnippet;
74 // Read functions.
75 static llvm::Expected<InstructionBenchmark>
76 readYaml(const LLVMState &State, llvm::StringRef Filename);
78 static llvm::Expected<std::vector<InstructionBenchmark>>
79 readYamls(const LLVMState &State, llvm::StringRef Filename);
81 void readYamlFrom(const LLVMState &State, llvm::StringRef InputContent);
83 // Write functions, non-const because of YAML traits.
84 void writeYamlTo(const LLVMState &State, llvm::raw_ostream &S);
86 llvm::Error writeYaml(const LLVMState &State, const llvm::StringRef Filename);
89 //------------------------------------------------------------------------------
90 // Utilities to work with Benchmark measures.
92 // A class that measures stats over benchmark measures.
93 class PerInstructionStats {
94 public:
95 void push(const BenchmarkMeasure &BM);
97 double avg() const {
98 assert(NumValues);
99 return SumValues / NumValues;
101 double min() const { return MinValue; }
102 double max() const { return MaxValue; }
104 const std::string &key() const { return Key; }
106 private:
107 std::string Key;
108 double SumValues = 0.0;
109 int NumValues = 0;
110 double MaxValue = std::numeric_limits<double>::min();
111 double MinValue = std::numeric_limits<double>::max();
114 } // namespace exegesis
115 } // namespace llvm
117 #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H