blob: c226aa0020ae67b1e61b8ea0a226e0fec3417baa [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::vector<llvm::MCInst> Instructions;
Clement Courbeta66bfaa42018-05-15 13:07:05 +000036 // An opaque configuration, that can be used to separate several benchmarks of
37 // the same instruction under different configurations.
38 std::string Config;
Clement Courbetac74acd2018-04-04 11:37:06 +000039};
40
41struct BenchmarkMeasure {
42 std::string Key;
43 double Value;
44 std::string DebugString;
45};
46
47// The result of an instruction benchmark.
48struct InstructionBenchmark {
Clement Courbeta66bfaa42018-05-15 13:07:05 +000049 InstructionBenchmarkKey Key;
Clement Courbet62b34fa2018-06-06 09:42:36 +000050 enum ModeE { Unknown, Latency, Uops };
51 ModeE Mode;
Clement Courbetac74acd2018-04-04 11:37:06 +000052 std::string CpuName;
53 std::string LLVMTriple;
Clement Courbet8fb6e402018-04-04 12:01:46 +000054 int NumRepetitions = 0;
Clement Courbetac74acd2018-04-04 11:37:06 +000055 std::vector<BenchmarkMeasure> Measurements;
56 std::string Error;
Clement Courbeta66bfaa42018-05-15 13:07:05 +000057 std::string Info;
Clement Courbetac74acd2018-04-04 11:37:06 +000058
Clement Courbet0e69e2d2018-05-17 10:52:18 +000059 // Read functions.
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000060 static llvm::Expected<InstructionBenchmark>
61 readYaml(const BenchmarkResultContext &Context, llvm::StringRef Filename);
Clement Courbet53d35d22018-06-05 10:56:19 +000062
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000063 static llvm::Expected<std::vector<InstructionBenchmark>>
64 readYamls(const BenchmarkResultContext &Context, llvm::StringRef Filename);
Clement Courbet53d35d22018-06-05 10:56:19 +000065
66 void readYamlFrom(const BenchmarkResultContext &Context,
67 llvm::StringRef InputContent);
Clement Courbet0e69e2d2018-05-17 10:52:18 +000068
69 // Write functions, non-const because of YAML traits.
Clement Courbet53d35d22018-06-05 10:56:19 +000070 void writeYamlTo(const BenchmarkResultContext &Context, llvm::raw_ostream &S);
71
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000072 llvm::Error writeYaml(const BenchmarkResultContext &Context,
73 const llvm::StringRef Filename);
Clement Courbetac74acd2018-04-04 11:37:06 +000074};
75
Clement Courbetae8ae5dc2018-05-24 12:41:02 +000076//------------------------------------------------------------------------------
77// Utilities to work with Benchmark measures.
78
79// A class that measures stats over benchmark measures.
80class BenchmarkMeasureStats {
81public:
82 void push(const BenchmarkMeasure &BM);
83
84 double avg() const {
85 assert(NumValues);
86 return SumValues / NumValues;
87 }
88 double min() const { return MinValue; }
89 double max() const { return MaxValue; }
90
Clement Courbet72287212018-06-04 11:11:55 +000091 const std::string &key() const { return Key; }
92
Clement Courbetae8ae5dc2018-05-24 12:41:02 +000093private:
94 std::string Key;
95 double SumValues = 0.0;
96 int NumValues = 0;
97 double MaxValue = std::numeric_limits<double>::min();
98 double MinValue = std::numeric_limits<double>::max();
99};
100
Clement Courbet53d35d22018-06-05 10:56:19 +0000101// This context is used when de/serializing InstructionBenchmark to guarantee
102// that Registers and Instructions are human readable and preserved accross
103// different versions of LLVM.
104struct BenchmarkResultContext {
105 BenchmarkResultContext() = default;
106 BenchmarkResultContext(BenchmarkResultContext &&) = default;
107 BenchmarkResultContext &operator=(BenchmarkResultContext &&) = default;
108 BenchmarkResultContext(const BenchmarkResultContext &) = delete;
109 BenchmarkResultContext &operator=(const BenchmarkResultContext &) = delete;
110
111 // Populate Registers and Instruction mapping.
112 void addRegEntry(unsigned RegNo, llvm::StringRef Name);
113 void addInstrEntry(unsigned Opcode, llvm::StringRef Name);
114
115 // Register accessors.
116 llvm::StringRef getRegName(unsigned RegNo) const;
117 unsigned getRegNo(llvm::StringRef Name) const; // 0 is not found.
118
119 // Instruction accessors.
120 llvm::StringRef getInstrName(unsigned Opcode) const;
121 unsigned getInstrOpcode(llvm::StringRef Name) const; // 0 is not found.
122
123private:
124 // Ideally we would like to use MCRegisterInfo and MCInstrInfo but doing so
125 // would make testing harder, instead we create a mapping that we can easily
126 // populate.
127 std::unordered_map<unsigned, llvm::StringRef> InstrOpcodeToName;
128 std::unordered_map<unsigned, llvm::StringRef> RegNoToName;
129 llvm::StringMap<unsigned> InstrNameToOpcode;
130 llvm::StringMap<unsigned> RegNameToNo;
131};
132
Clement Courbetac74acd2018-04-04 11:37:06 +0000133} // namespace exegesis
134
135#endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H