blob: e54e8c2a8dac9b3f21510cc9b767909359a67634 [file] [log] [blame]
Inna Palantff3f07a2019-07-11 16:15:26 -07001//===-- BenchmarkResult.h ---------------------------------------*- C++ -*-===//
2//
Chih-Hung Hsieh43f06942019-12-19 15:01:08 -08003// 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
Inna Palantff3f07a2019-07-11 16:15:26 -07006//
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 "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"
25#include <limits>
26#include <string>
27#include <unordered_map>
28#include <vector>
29
30namespace llvm {
31namespace exegesis {
32
33struct 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.
40 std::string Config;
41};
42
43struct BenchmarkMeasure {
44 // A helper to create an unscaled BenchmarkMeasure.
45 static BenchmarkMeasure Create(std::string Key, double Value) {
46 return {Key, Value, Value};
47 }
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;
55};
56
57// The result of an instruction benchmark.
58struct InstructionBenchmark {
59 InstructionBenchmarkKey Key;
Chih-Hung Hsieh43f06942019-12-19 15:01:08 -080060 enum ModeE { Unknown, Latency, Uops, InverseThroughput };
Inna Palantff3f07a2019-07-11 16:15:26 -070061 ModeE Mode;
62 std::string CpuName;
63 std::string LLVMTriple;
Chih-Hung Hsieh43f06942019-12-19 15:01:08 -080064 // Which instruction is being benchmarked here?
65 const llvm::MCInst &keyInstruction() const { return Key.Instructions[0]; }
Inna Palantff3f07a2019-07-11 16:15:26 -070066 // 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;
71 std::string Error;
72 std::string Info;
73 std::vector<uint8_t> AssembledSnippet;
74
75 // Read functions.
76 static llvm::Expected<InstructionBenchmark>
77 readYaml(const LLVMState &State, llvm::StringRef Filename);
78
79 static llvm::Expected<std::vector<InstructionBenchmark>>
80 readYamls(const LLVMState &State, llvm::StringRef Filename);
81
Chih-Hung Hsieh43f06942019-12-19 15:01:08 -080082 llvm::Error readYamlFrom(const LLVMState &State,
83 llvm::StringRef InputContent);
Inna Palantff3f07a2019-07-11 16:15:26 -070084
85 // Write functions, non-const because of YAML traits.
Chih-Hung Hsieh43f06942019-12-19 15:01:08 -080086 llvm::Error writeYamlTo(const LLVMState &State, llvm::raw_ostream &S);
Inna Palantff3f07a2019-07-11 16:15:26 -070087
88 llvm::Error writeYaml(const LLVMState &State, const llvm::StringRef Filename);
89};
90
91//------------------------------------------------------------------------------
92// Utilities to work with Benchmark measures.
93
94// A class that measures stats over benchmark measures.
95class PerInstructionStats {
96public:
97 void push(const BenchmarkMeasure &BM);
98
99 double avg() const {
100 assert(NumValues);
101 return SumValues / NumValues;
102 }
103 double min() const { return MinValue; }
104 double max() const { return MaxValue; }
105
106 const std::string &key() const { return Key; }
107
108private:
109 std::string Key;
110 double SumValues = 0.0;
111 int NumValues = 0;
112 double MaxValue = std::numeric_limits<double>::min();
113 double MinValue = std::numeric_limits<double>::max();
114};
115
116} // namespace exegesis
117} // namespace llvm
118
119#endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H