blob: c9e77ca492239cc590797f3f43ec3721d097e9d0 [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 Courbetf64007f2018-06-15 09:27:12 +000054 // The number of instructions inside the repeated snippet. For example, if a
55 // snippet of 3 instructions is repeated 4 times, this is 12.
Clement Courbet8fb6e402018-04-04 12:01:46 +000056 int NumRepetitions = 0;
Clement Courbetf64007f2018-06-15 09:27:12 +000057 // Note that measurements are per instruction.
Clement Courbetac74acd2018-04-04 11:37:06 +000058 std::vector<BenchmarkMeasure> Measurements;
59 std::string Error;
Clement Courbeta66bfaa42018-05-15 13:07:05 +000060 std::string Info;
Clement Courbet4273e1e2018-06-15 07:30:45 +000061 std::vector<uint8_t> AssembledSnippet;
Clement Courbetac74acd2018-04-04 11:37:06 +000062
Clement Courbet0e69e2d2018-05-17 10:52:18 +000063 // Read functions.
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000064 static llvm::Expected<InstructionBenchmark>
65 readYaml(const BenchmarkResultContext &Context, llvm::StringRef Filename);
Clement Courbet53d35d22018-06-05 10:56:19 +000066
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000067 static llvm::Expected<std::vector<InstructionBenchmark>>
68 readYamls(const BenchmarkResultContext &Context, llvm::StringRef Filename);
Clement Courbet53d35d22018-06-05 10:56:19 +000069
70 void readYamlFrom(const BenchmarkResultContext &Context,
71 llvm::StringRef InputContent);
Clement Courbet0e69e2d2018-05-17 10:52:18 +000072
73 // Write functions, non-const because of YAML traits.
Clement Courbet53d35d22018-06-05 10:56:19 +000074 void writeYamlTo(const BenchmarkResultContext &Context, llvm::raw_ostream &S);
75
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000076 llvm::Error writeYaml(const BenchmarkResultContext &Context,
77 const llvm::StringRef Filename);
Clement Courbetac74acd2018-04-04 11:37:06 +000078};
79
Clement Courbetae8ae5dc2018-05-24 12:41:02 +000080//------------------------------------------------------------------------------
81// Utilities to work with Benchmark measures.
82
83// A class that measures stats over benchmark measures.
84class BenchmarkMeasureStats {
85public:
86 void push(const BenchmarkMeasure &BM);
87
88 double avg() const {
89 assert(NumValues);
90 return SumValues / NumValues;
91 }
92 double min() const { return MinValue; }
93 double max() const { return MaxValue; }
94
Clement Courbet72287212018-06-04 11:11:55 +000095 const std::string &key() const { return Key; }
96
Clement Courbetae8ae5dc2018-05-24 12:41:02 +000097private:
98 std::string Key;
99 double SumValues = 0.0;
100 int NumValues = 0;
101 double MaxValue = std::numeric_limits<double>::min();
102 double MinValue = std::numeric_limits<double>::max();
103};
104
Clement Courbet53d35d22018-06-05 10:56:19 +0000105// This context is used when de/serializing InstructionBenchmark to guarantee
106// that Registers and Instructions are human readable and preserved accross
107// different versions of LLVM.
108struct BenchmarkResultContext {
109 BenchmarkResultContext() = default;
110 BenchmarkResultContext(BenchmarkResultContext &&) = default;
111 BenchmarkResultContext &operator=(BenchmarkResultContext &&) = default;
112 BenchmarkResultContext(const BenchmarkResultContext &) = delete;
113 BenchmarkResultContext &operator=(const BenchmarkResultContext &) = delete;
114
115 // Populate Registers and Instruction mapping.
116 void addRegEntry(unsigned RegNo, llvm::StringRef Name);
117 void addInstrEntry(unsigned Opcode, llvm::StringRef Name);
118
119 // Register accessors.
120 llvm::StringRef getRegName(unsigned RegNo) const;
121 unsigned getRegNo(llvm::StringRef Name) const; // 0 is not found.
122
123 // Instruction accessors.
124 llvm::StringRef getInstrName(unsigned Opcode) const;
125 unsigned getInstrOpcode(llvm::StringRef Name) const; // 0 is not found.
126
127private:
128 // Ideally we would like to use MCRegisterInfo and MCInstrInfo but doing so
129 // would make testing harder, instead we create a mapping that we can easily
130 // populate.
131 std::unordered_map<unsigned, llvm::StringRef> InstrOpcodeToName;
132 std::unordered_map<unsigned, llvm::StringRef> RegNoToName;
133 llvm::StringMap<unsigned> InstrNameToOpcode;
134 llvm::StringMap<unsigned> RegNameToNo;
135};
136
Clement Courbetac74acd2018-04-04 11:37:06 +0000137} // namespace exegesis
138
139#endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H