[llvm] Stop including unordered_map (NFC)
[llvm-project.git] / llvm / tools / llvm-exegesis / lib / BenchmarkResult.h
blob38111519a2c8987c152eb14b56e9174aa0d2b610
1 //===-- BenchmarkResult.h ---------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// Defines classes to represent measurements and serialize/deserialize them to
11 // Yaml.
12 ///
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H
16 #define LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H
18 #include "LlvmState.h"
19 #include "RegisterValue.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/MC/MCInstBuilder.h"
23 #include "llvm/Support/YAMLTraits.h"
24 #include <limits>
25 #include <set>
26 #include <string>
27 #include <unordered_map>
28 #include <vector>
30 namespace llvm {
31 class Error;
33 namespace exegesis {
35 enum class BenchmarkPhaseSelectorE {
36 PrepareSnippet,
37 PrepareAndAssembleSnippet,
38 AssembleMeasuredCode,
39 Measure,
42 enum class BenchmarkFilter { All, RegOnly, WithMem };
44 struct MemoryValue {
45 // The arbitrary bit width constant that defines the value.
46 APInt Value;
47 // The size of the value in bytes.
48 size_t SizeBytes;
49 // The index of the memory value.
50 size_t Index;
53 struct MemoryMapping {
54 // The address to place the mapping at.
55 intptr_t Address;
56 // The name of the value that should be mapped.
57 std::string MemoryValueName;
60 struct BenchmarkKey {
61 // The LLVM opcode name.
62 std::vector<MCInst> Instructions;
63 // The initial values of the registers.
64 std::vector<RegisterValue> RegisterInitialValues;
65 // The memory values that can be mapped into the execution context of the
66 // snippet.
67 std::unordered_map<std::string, MemoryValue> MemoryValues;
68 // The memory mappings that the snippet can access.
69 std::vector<MemoryMapping> MemoryMappings;
70 // An opaque configuration, that can be used to separate several benchmarks of
71 // the same instruction under different configurations.
72 std::string Config;
75 struct BenchmarkMeasure {
76 // A helper to create an unscaled BenchmarkMeasure.
77 static BenchmarkMeasure Create(std::string Key, double Value) {
78 return {Key, Value, Value};
80 std::string Key;
81 // This is the per-instruction value, i.e. measured quantity scaled per
82 // instruction.
83 double PerInstructionValue;
84 // This is the per-snippet value, i.e. measured quantity for one repetition of
85 // the whole snippet.
86 double PerSnippetValue;
89 // The result of an instruction benchmark.
90 struct Benchmark {
91 BenchmarkKey Key;
92 enum ModeE { Unknown, Latency, Uops, InverseThroughput };
93 ModeE Mode;
94 std::string CpuName;
95 std::string LLVMTriple;
96 // Which instruction is being benchmarked here?
97 const MCInst &keyInstruction() const { return Key.Instructions[0]; }
98 // The number of instructions inside the repeated snippet. For example, if a
99 // snippet of 3 instructions is repeated 4 times, this is 12.
100 unsigned NumRepetitions = 0;
101 enum RepetitionModeE { Duplicate, Loop, AggregateMin };
102 // Note that measurements are per instruction.
103 std::vector<BenchmarkMeasure> Measurements;
104 std::string Error;
105 std::string Info;
106 std::vector<uint8_t> AssembledSnippet;
107 // How to aggregate measurements.
108 enum ResultAggregationModeE { Min, Max, Mean, MinVariance };
110 Benchmark() = default;
111 Benchmark(Benchmark &&) = default;
113 Benchmark(const Benchmark &) = delete;
114 Benchmark &operator=(const Benchmark &) = delete;
115 Benchmark &operator=(Benchmark &&) = delete;
117 // Read functions.
118 static Expected<Benchmark> readYaml(const LLVMState &State,
119 MemoryBufferRef Buffer);
121 static Expected<std::vector<Benchmark>>
122 readYamls(const LLVMState &State, MemoryBufferRef Buffer);
124 // Given a set of serialized instruction benchmarks, returns the set of
125 // triples and CPUs that appear in the list of benchmarks.
126 struct TripleAndCpu {
127 std::string LLVMTriple;
128 std::string CpuName;
129 bool operator<(const TripleAndCpu &O) const {
130 return std::tie(LLVMTriple, CpuName) < std::tie(O.LLVMTriple, O.CpuName);
133 static Expected<std::set<TripleAndCpu>>
134 readTriplesAndCpusFromYamls(MemoryBufferRef Buffer);
136 class Error readYamlFrom(const LLVMState &State, StringRef InputContent);
138 // Write functions, non-const because of YAML traits.
139 // NOTE: we intentionally do *NOT* have a variant of this function taking
140 // filename, because it's behaviour is bugprone with regards to
141 // accidentally using it more than once and overriding previous YAML.
142 class Error writeYamlTo(const LLVMState &State, raw_ostream &S);
145 bool operator==(const BenchmarkMeasure &A, const BenchmarkMeasure &B);
147 //------------------------------------------------------------------------------
148 // Utilities to work with Benchmark measures.
150 // A class that measures stats over benchmark measures.
151 class PerInstructionStats {
152 public:
153 void push(const BenchmarkMeasure &BM);
155 double avg() const {
156 assert(NumValues);
157 return SumValues / NumValues;
159 double min() const { return MinValue; }
160 double max() const { return MaxValue; }
162 const std::string &key() const { return Key; }
164 private:
165 std::string Key;
166 double SumValues = 0.0;
167 int NumValues = 0;
168 double MaxValue = std::numeric_limits<double>::min();
169 double MinValue = std::numeric_limits<double>::max();
172 } // namespace exegesis
173 } // namespace llvm
175 #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H