blob: 421e210274f57187dc7d3604c457a6326ccb912b [file] [log] [blame]
Clement Courbetac74acd2018-04-04 11:37:06 +00001//===-- BenchmarkResult.h ---------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// Defines classes to represent measurements and serialize/deserialize them to
12// Yaml.
13///
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H
17#define LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H
18
Clement Courbet53d35d22018-06-05 10:56:19 +000019#include "llvm/ADT/StringMap.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000020#include "llvm/ADT/StringRef.h"
Clement Courbet53d35d22018-06-05 10:56:19 +000021#include "llvm/MC/MCInst.h"
22#include "llvm/MC/MCInstBuilder.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000023#include "llvm/Support/YAMLTraits.h"
Clement Courbetae8ae5dc2018-05-24 12:41:02 +000024#include <limits>
Clement Courbetac74acd2018-04-04 11:37:06 +000025#include <string>
Clement Courbet53d35d22018-06-05 10:56:19 +000026#include <unordered_map>
Clement Courbetac74acd2018-04-04 11:37:06 +000027#include <vector>
28
29namespace exegesis {
30
Clement Courbet53d35d22018-06-05 10:56:19 +000031struct BenchmarkResultContext; // Forward declaration.
32
Clement Courbeta66bfaa42018-05-15 13:07:05 +000033struct InstructionBenchmarkKey {
34 // The LLVM opcode name.
Clement Courbet53d35d22018-06-05 10:56:19 +000035 std::string OpcodeName; // FIXME: Deprecated, use Instructions below.
36 std::vector<llvm::MCInst> Instructions;
Clement Courbeta66bfaa42018-05-15 13:07:05 +000037 // An opaque configuration, that can be used to separate several benchmarks of
38 // the same instruction under different configurations.
39 std::string Config;
Clement Courbetac74acd2018-04-04 11:37:06 +000040};
41
42struct BenchmarkMeasure {
43 std::string Key;
44 double Value;
45 std::string DebugString;
46};
47
48// The result of an instruction benchmark.
49struct InstructionBenchmark {
Clement Courbeta66bfaa42018-05-15 13:07:05 +000050 InstructionBenchmarkKey Key;
Clement Courbet62b34fa2018-06-06 09:42:36 +000051 enum ModeE { Unknown, Latency, Uops };
52 ModeE Mode;
Clement Courbetac74acd2018-04-04 11:37:06 +000053 std::string CpuName;
54 std::string LLVMTriple;
Clement Courbet8fb6e402018-04-04 12:01:46 +000055 int NumRepetitions = 0;
Clement Courbetac74acd2018-04-04 11:37:06 +000056 std::vector<BenchmarkMeasure> Measurements;
57 std::string Error;
Clement Courbeta66bfaa42018-05-15 13:07:05 +000058 std::string Info;
Clement Courbetac74acd2018-04-04 11:37:06 +000059
Clement Courbet0e69e2d2018-05-17 10:52:18 +000060 // Read functions.
Clement Courbet53d35d22018-06-05 10:56:19 +000061 static InstructionBenchmark
62 readYamlOrDie(const BenchmarkResultContext &Context,
63 llvm::StringRef Filename);
64
65 static std::vector<InstructionBenchmark>
66 readYamlsOrDie(const BenchmarkResultContext &Context,
67 llvm::StringRef Filename);
68
69 void readYamlFrom(const BenchmarkResultContext &Context,
70 llvm::StringRef InputContent);
Clement Courbet0e69e2d2018-05-17 10:52:18 +000071
72 // Write functions, non-const because of YAML traits.
Clement Courbet53d35d22018-06-05 10:56:19 +000073 void writeYamlTo(const BenchmarkResultContext &Context, llvm::raw_ostream &S);
74
75 void writeYamlOrDie(const BenchmarkResultContext &Context,
76 const llvm::StringRef Filename);
Clement Courbetac74acd2018-04-04 11:37:06 +000077};
78
Clement Courbetae8ae5dc2018-05-24 12:41:02 +000079//------------------------------------------------------------------------------
80// Utilities to work with Benchmark measures.
81
82// A class that measures stats over benchmark measures.
83class BenchmarkMeasureStats {
84public:
85 void push(const BenchmarkMeasure &BM);
86
87 double avg() const {
88 assert(NumValues);
89 return SumValues / NumValues;
90 }
91 double min() const { return MinValue; }
92 double max() const { return MaxValue; }
93
Clement Courbet72287212018-06-04 11:11:55 +000094 const std::string &key() const { return Key; }
95
Clement Courbetae8ae5dc2018-05-24 12:41:02 +000096private:
97 std::string Key;
98 double SumValues = 0.0;
99 int NumValues = 0;
100 double MaxValue = std::numeric_limits<double>::min();
101 double MinValue = std::numeric_limits<double>::max();
102};
103
Clement Courbet53d35d22018-06-05 10:56:19 +0000104// This context is used when de/serializing InstructionBenchmark to guarantee
105// that Registers and Instructions are human readable and preserved accross
106// different versions of LLVM.
107struct BenchmarkResultContext {
108 BenchmarkResultContext() = default;
109 BenchmarkResultContext(BenchmarkResultContext &&) = default;
110 BenchmarkResultContext &operator=(BenchmarkResultContext &&) = default;
111 BenchmarkResultContext(const BenchmarkResultContext &) = delete;
112 BenchmarkResultContext &operator=(const BenchmarkResultContext &) = delete;
113
114 // Populate Registers and Instruction mapping.
115 void addRegEntry(unsigned RegNo, llvm::StringRef Name);
116 void addInstrEntry(unsigned Opcode, llvm::StringRef Name);
117
118 // Register accessors.
119 llvm::StringRef getRegName(unsigned RegNo) const;
120 unsigned getRegNo(llvm::StringRef Name) const; // 0 is not found.
121
122 // Instruction accessors.
123 llvm::StringRef getInstrName(unsigned Opcode) const;
124 unsigned getInstrOpcode(llvm::StringRef Name) const; // 0 is not found.
125
126private:
127 // Ideally we would like to use MCRegisterInfo and MCInstrInfo but doing so
128 // would make testing harder, instead we create a mapping that we can easily
129 // populate.
130 std::unordered_map<unsigned, llvm::StringRef> InstrOpcodeToName;
131 std::unordered_map<unsigned, llvm::StringRef> RegNoToName;
132 llvm::StringMap<unsigned> InstrNameToOpcode;
133 llvm::StringMap<unsigned> RegNameToNo;
134};
135
Clement Courbetac74acd2018-04-04 11:37:06 +0000136} // namespace exegesis
137
138#endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H