blob: 83451ca039620b5aa9ccd3942391e2ca39cc151f [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 Chatelet55ad0872018-09-25 12:18:08 +000019#include "LlvmState.h"
Clement Courbet53d35d22018-06-05 10:56:19 +000020#include "llvm/ADT/StringMap.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000021#include "llvm/ADT/StringRef.h"
Clement Courbet53d35d22018-06-05 10:56:19 +000022#include "llvm/MC/MCInst.h"
23#include "llvm/MC/MCInstBuilder.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000024#include "llvm/Support/YAMLTraits.h"
Clement Courbetae8ae5dc2018-05-24 12:41:02 +000025#include <limits>
Clement Courbetac74acd2018-04-04 11:37:06 +000026#include <string>
Clement Courbet53d35d22018-06-05 10:56:19 +000027#include <unordered_map>
Clement Courbetac74acd2018-04-04 11:37:06 +000028#include <vector>
29
30namespace exegesis {
31
Clement Courbeta66bfaa42018-05-15 13:07:05 +000032struct InstructionBenchmarkKey {
33 // The LLVM opcode name.
Clement Courbet53d35d22018-06-05 10:56:19 +000034 std::vector<llvm::MCInst> Instructions;
Clement Courbeta66bfaa42018-05-15 13:07:05 +000035 // An opaque configuration, that can be used to separate several benchmarks of
36 // the same instruction under different configurations.
37 std::string Config;
Clement Courbetac74acd2018-04-04 11:37:06 +000038};
39
40struct BenchmarkMeasure {
41 std::string Key;
42 double Value;
43 std::string DebugString;
44};
45
46// The result of an instruction benchmark.
47struct InstructionBenchmark {
Clement Courbeta66bfaa42018-05-15 13:07:05 +000048 InstructionBenchmarkKey Key;
Clement Courbet62b34fa2018-06-06 09:42:36 +000049 enum ModeE { Unknown, Latency, Uops };
50 ModeE Mode;
Clement Courbetac74acd2018-04-04 11:37:06 +000051 std::string CpuName;
52 std::string LLVMTriple;
Clement Courbetf64007f2018-06-15 09:27:12 +000053 // The number of instructions inside the repeated snippet. For example, if a
54 // snippet of 3 instructions is repeated 4 times, this is 12.
Clement Courbet8fb6e402018-04-04 12:01:46 +000055 int NumRepetitions = 0;
Clement Courbetf64007f2018-06-15 09:27:12 +000056 // Note that measurements are per instruction.
Clement Courbetac74acd2018-04-04 11:37:06 +000057 std::vector<BenchmarkMeasure> Measurements;
58 std::string Error;
Clement Courbeta66bfaa42018-05-15 13:07:05 +000059 std::string Info;
Clement Courbet4273e1e2018-06-15 07:30:45 +000060 std::vector<uint8_t> AssembledSnippet;
Clement Courbetac74acd2018-04-04 11:37:06 +000061
Clement Courbet0e69e2d2018-05-17 10:52:18 +000062 // Read functions.
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000063 static llvm::Expected<InstructionBenchmark>
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000064 readYaml(const LLVMState &State, llvm::StringRef Filename);
Clement Courbet53d35d22018-06-05 10:56:19 +000065
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000066 static llvm::Expected<std::vector<InstructionBenchmark>>
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000067 readYamls(const LLVMState &State, llvm::StringRef Filename);
Clement Courbet53d35d22018-06-05 10:56:19 +000068
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000069 void readYamlFrom(const LLVMState &State, llvm::StringRef InputContent);
Clement Courbet0e69e2d2018-05-17 10:52:18 +000070
71 // Write functions, non-const because of YAML traits.
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000072 void writeYamlTo(const LLVMState &State, llvm::raw_ostream &S);
Clement Courbet53d35d22018-06-05 10:56:19 +000073
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000074 llvm::Error writeYaml(const LLVMState &State, const llvm::StringRef Filename);
Clement Courbetac74acd2018-04-04 11:37:06 +000075};
76
Clement Courbetae8ae5dc2018-05-24 12:41:02 +000077//------------------------------------------------------------------------------
78// Utilities to work with Benchmark measures.
79
80// A class that measures stats over benchmark measures.
81class BenchmarkMeasureStats {
82public:
83 void push(const BenchmarkMeasure &BM);
84
85 double avg() const {
86 assert(NumValues);
87 return SumValues / NumValues;
88 }
89 double min() const { return MinValue; }
90 double max() const { return MaxValue; }
91
Clement Courbet72287212018-06-04 11:11:55 +000092 const std::string &key() const { return Key; }
93
Clement Courbetae8ae5dc2018-05-24 12:41:02 +000094private:
95 std::string Key;
96 double SumValues = 0.0;
97 int NumValues = 0;
98 double MaxValue = std::numeric_limits<double>::min();
99 double MinValue = std::numeric_limits<double>::max();
100};
101
Clement Courbetac74acd2018-04-04 11:37:06 +0000102} // namespace exegesis
103
104#endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H