blob: 62653cd76047a87093b2c2ac21457b75f959776d [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
Guillaume Chatelet345fae52018-09-25 15:15:54 +000019#include "BenchmarkCode.h"
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000020#include "LlvmState.h"
Clement Courbet53d35d22018-06-05 10:56:19 +000021#include "llvm/ADT/StringMap.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000022#include "llvm/ADT/StringRef.h"
Clement Courbet53d35d22018-06-05 10:56:19 +000023#include "llvm/MC/MCInst.h"
24#include "llvm/MC/MCInstBuilder.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000025#include "llvm/Support/YAMLTraits.h"
Clement Courbetae8ae5dc2018-05-24 12:41:02 +000026#include <limits>
Clement Courbetac74acd2018-04-04 11:37:06 +000027#include <string>
Clement Courbet53d35d22018-06-05 10:56:19 +000028#include <unordered_map>
Clement Courbetac74acd2018-04-04 11:37:06 +000029#include <vector>
30
31namespace exegesis {
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;
Guillaume Chatelet345fae52018-09-25 15:15:54 +000036 // The initial values of the registers.
37 std::vector<RegisterValue> RegisterInitialValues;
Clement Courbeta66bfaa42018-05-15 13:07:05 +000038 // An opaque configuration, that can be used to separate several benchmarks of
39 // the same instruction under different configurations.
40 std::string Config;
Clement Courbetac74acd2018-04-04 11:37:06 +000041};
42
43struct BenchmarkMeasure {
44 std::string Key;
45 double Value;
46 std::string DebugString;
47};
48
49// The result of an instruction benchmark.
50struct InstructionBenchmark {
Clement Courbeta66bfaa42018-05-15 13:07:05 +000051 InstructionBenchmarkKey Key;
Clement Courbet62b34fa2018-06-06 09:42:36 +000052 enum ModeE { Unknown, Latency, Uops };
53 ModeE Mode;
Clement Courbetac74acd2018-04-04 11:37:06 +000054 std::string CpuName;
55 std::string LLVMTriple;
Clement Courbetf64007f2018-06-15 09:27:12 +000056 // The number of instructions inside the repeated snippet. For example, if a
57 // snippet of 3 instructions is repeated 4 times, this is 12.
Clement Courbet8fb6e402018-04-04 12:01:46 +000058 int NumRepetitions = 0;
Clement Courbetf64007f2018-06-15 09:27:12 +000059 // Note that measurements are per instruction.
Clement Courbetac74acd2018-04-04 11:37:06 +000060 std::vector<BenchmarkMeasure> Measurements;
61 std::string Error;
Clement Courbeta66bfaa42018-05-15 13:07:05 +000062 std::string Info;
Clement Courbet4273e1e2018-06-15 07:30:45 +000063 std::vector<uint8_t> AssembledSnippet;
Clement Courbetac74acd2018-04-04 11:37:06 +000064
Clement Courbet0e69e2d2018-05-17 10:52:18 +000065 // Read functions.
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000066 static llvm::Expected<InstructionBenchmark>
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000067 readYaml(const LLVMState &State, llvm::StringRef Filename);
Clement Courbet53d35d22018-06-05 10:56:19 +000068
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000069 static llvm::Expected<std::vector<InstructionBenchmark>>
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000070 readYamls(const LLVMState &State, llvm::StringRef Filename);
Clement Courbet53d35d22018-06-05 10:56:19 +000071
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000072 void readYamlFrom(const LLVMState &State, llvm::StringRef InputContent);
Clement Courbet0e69e2d2018-05-17 10:52:18 +000073
74 // Write functions, non-const because of YAML traits.
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000075 void writeYamlTo(const LLVMState &State, llvm::raw_ostream &S);
Clement Courbet53d35d22018-06-05 10:56:19 +000076
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000077 llvm::Error writeYaml(const LLVMState &State, 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 Courbetac74acd2018-04-04 11:37:06 +0000105} // namespace exegesis
106
107#endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H