[InstCombine] Signed saturation patterns
[llvm-core.git] / tools / llvm-exegesis / lib / BenchmarkResult.h
blob29bf9963c9953e3a382df68befe75a25a506cea8
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/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>
30 namespace llvm {
31 namespace exegesis {
33 struct InstructionBenchmarkKey {
34 // The LLVM opcode name.
35 std::vector<MCInst> Instructions;
36 // The initial values of the registers.
37 std::vector<RegisterValue> RegisterInitialValues;
38 // An opaque configuration, that can be used to separate several benchmarks of
39 // the same instruction under different configurations.
40 std::string Config;
43 struct BenchmarkMeasure {
44 // A helper to create an unscaled BenchmarkMeasure.
45 static BenchmarkMeasure Create(std::string Key, double Value) {
46 return {Key, Value, Value};
48 std::string Key;
49 // This is the per-instruction value, i.e. measured quantity scaled per
50 // instruction.
51 double PerInstructionValue;
52 // This is the per-snippet value, i.e. measured quantity for one repetition of
53 // the whole snippet.
54 double PerSnippetValue;
57 // The result of an instruction benchmark.
58 struct InstructionBenchmark {
59 InstructionBenchmarkKey Key;
60 enum ModeE { Unknown, Latency, Uops, InverseThroughput };
61 ModeE Mode;
62 std::string CpuName;
63 std::string LLVMTriple;
64 // Which instruction is being benchmarked here?
65 const MCInst &keyInstruction() const { return Key.Instructions[0]; }
66 // The number of instructions inside the repeated snippet. For example, if a
67 // snippet of 3 instructions is repeated 4 times, this is 12.
68 int NumRepetitions = 0;
69 enum RepetitionModeE { Duplicate, Loop };
70 RepetitionModeE RepetitionMode;
71 // Note that measurements are per instruction.
72 std::vector<BenchmarkMeasure> Measurements;
73 std::string Error;
74 std::string Info;
75 std::vector<uint8_t> AssembledSnippet;
77 // Read functions.
78 static Expected<InstructionBenchmark> readYaml(const LLVMState &State,
79 StringRef Filename);
81 static Expected<std::vector<InstructionBenchmark>>
82 readYamls(const LLVMState &State, StringRef Filename);
84 class Error readYamlFrom(const LLVMState &State, StringRef InputContent);
86 // Write functions, non-const because of YAML traits.
87 class Error writeYamlTo(const LLVMState &State, raw_ostream &S);
89 class Error writeYaml(const LLVMState &State, const StringRef Filename);
92 //------------------------------------------------------------------------------
93 // Utilities to work with Benchmark measures.
95 // A class that measures stats over benchmark measures.
96 class PerInstructionStats {
97 public:
98 void push(const BenchmarkMeasure &BM);
100 double avg() const {
101 assert(NumValues);
102 return SumValues / NumValues;
104 double min() const { return MinValue; }
105 double max() const { return MaxValue; }
107 const std::string &key() const { return Key; }
109 private:
110 std::string Key;
111 double SumValues = 0.0;
112 int NumValues = 0;
113 double MaxValue = std::numeric_limits<double>::min();
114 double MinValue = std::numeric_limits<double>::max();
117 } // namespace exegesis
118 } // namespace llvm
120 #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H