1 //===-- BenchmarkResult.h ---------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// Defines classes to represent measurements and serialize/deserialize them to
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H
16 #define LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H
18 #include "BenchmarkCode.h"
19 #include "LlvmState.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"
27 #include <unordered_map>
33 struct InstructionBenchmarkKey
{
34 // The LLVM opcode name.
35 std::vector
<llvm::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.
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
};
49 // This is the per-instruction value, i.e. measured quantity scaled per
51 double PerInstructionValue
;
52 // This is the per-snippet value, i.e. measured quantity for one repetition of
54 double PerSnippetValue
;
57 // The result of an instruction benchmark.
58 struct InstructionBenchmark
{
59 InstructionBenchmarkKey Key
;
60 enum ModeE
{ Unknown
, Latency
, Uops
, InverseThroughput
};
63 std::string LLVMTriple
;
64 // Which instruction is being benchmarked here?
65 const llvm::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 // Note that measurements are per instruction.
70 std::vector
<BenchmarkMeasure
> Measurements
;
73 std::vector
<uint8_t> AssembledSnippet
;
76 static llvm::Expected
<InstructionBenchmark
>
77 readYaml(const LLVMState
&State
, llvm::StringRef Filename
);
79 static llvm::Expected
<std::vector
<InstructionBenchmark
>>
80 readYamls(const LLVMState
&State
, llvm::StringRef Filename
);
82 llvm::Error
readYamlFrom(const LLVMState
&State
,
83 llvm::StringRef InputContent
);
85 // Write functions, non-const because of YAML traits.
86 llvm::Error
writeYamlTo(const LLVMState
&State
, llvm::raw_ostream
&S
);
88 llvm::Error
writeYaml(const LLVMState
&State
, const llvm::StringRef Filename
);
91 //------------------------------------------------------------------------------
92 // Utilities to work with Benchmark measures.
94 // A class that measures stats over benchmark measures.
95 class PerInstructionStats
{
97 void push(const BenchmarkMeasure
&BM
);
101 return SumValues
/ NumValues
;
103 double min() const { return MinValue
; }
104 double max() const { return MaxValue
; }
106 const std::string
&key() const { return Key
; }
110 double SumValues
= 0.0;
112 double MaxValue
= std::numeric_limits
<double>::min();
113 double MinValue
= std::numeric_limits
<double>::max();
116 } // namespace exegesis
119 #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H