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