blob: 773a2e50abc4d70053db917658120d236bf5edbc [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
Fangrui Song32401af2018-10-22 17:10:47 +000031namespace llvm {
Clement Courbetac74acd2018-04-04 11:37:06 +000032namespace exegesis {
33
Clement Courbeta66bfaa42018-05-15 13:07:05 +000034struct InstructionBenchmarkKey {
35 // The LLVM opcode name.
Clement Courbet53d35d22018-06-05 10:56:19 +000036 std::vector<llvm::MCInst> Instructions;
Guillaume Chatelet345fae52018-09-25 15:15:54 +000037 // The initial values of the registers.
38 std::vector<RegisterValue> RegisterInitialValues;
Clement Courbeta66bfaa42018-05-15 13:07:05 +000039 // An opaque configuration, that can be used to separate several benchmarks of
40 // the same instruction under different configurations.
41 std::string Config;
Clement Courbetac74acd2018-04-04 11:37:06 +000042};
43
44struct BenchmarkMeasure {
Clement Courbet596c56f2018-09-26 11:22:56 +000045 // A helper to create an unscaled BenchmarkMeasure.
46 static BenchmarkMeasure Create(std::string Key, double Value) {
Clement Courbet28d4f852018-09-26 13:35:10 +000047 return {Key, Value, Value};
Clement Courbet596c56f2018-09-26 11:22:56 +000048 }
Clement Courbetac74acd2018-04-04 11:37:06 +000049 std::string Key;
Clement Courbet684a5f62018-09-26 08:37:21 +000050 // This is the per-instruction value, i.e. measured quantity scaled per
51 // instruction.
52 double PerInstructionValue;
53 // This is the per-snippet value, i.e. measured quantity for one repetition of
54 // the whole snippet.
55 double PerSnippetValue;
Clement Courbetac74acd2018-04-04 11:37:06 +000056};
57
58// The result of an instruction benchmark.
59struct InstructionBenchmark {
Clement Courbeta66bfaa42018-05-15 13:07:05 +000060 InstructionBenchmarkKey Key;
Clement Courbet0d79aaf2018-11-08 12:09:45 +000061 enum ModeE { Unknown, Latency, Uops };
Clement Courbet62b34fa2018-06-06 09:42:36 +000062 ModeE Mode;
Clement Courbetac74acd2018-04-04 11:37:06 +000063 std::string CpuName;
64 std::string LLVMTriple;
Clement Courbetf64007f2018-06-15 09:27:12 +000065 // The number of instructions inside the repeated snippet. For example, if a
66 // snippet of 3 instructions is repeated 4 times, this is 12.
Clement Courbet8fb6e402018-04-04 12:01:46 +000067 int NumRepetitions = 0;
Clement Courbetf64007f2018-06-15 09:27:12 +000068 // Note that measurements are per instruction.
Clement Courbetac74acd2018-04-04 11:37:06 +000069 std::vector<BenchmarkMeasure> Measurements;
70 std::string Error;
Clement Courbeta66bfaa42018-05-15 13:07:05 +000071 std::string Info;
Clement Courbet4273e1e2018-06-15 07:30:45 +000072 std::vector<uint8_t> AssembledSnippet;
Clement Courbetac74acd2018-04-04 11:37:06 +000073
Clement Courbet0e69e2d2018-05-17 10:52:18 +000074 // Read functions.
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000075 static llvm::Expected<InstructionBenchmark>
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000076 readYaml(const LLVMState &State, llvm::StringRef Filename);
Clement Courbet53d35d22018-06-05 10:56:19 +000077
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000078 static llvm::Expected<std::vector<InstructionBenchmark>>
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000079 readYamls(const LLVMState &State, llvm::StringRef Filename);
Clement Courbet53d35d22018-06-05 10:56:19 +000080
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000081 void readYamlFrom(const LLVMState &State, llvm::StringRef InputContent);
Clement Courbet0e69e2d2018-05-17 10:52:18 +000082
83 // Write functions, non-const because of YAML traits.
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000084 void writeYamlTo(const LLVMState &State, llvm::raw_ostream &S);
Clement Courbet53d35d22018-06-05 10:56:19 +000085
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000086 llvm::Error writeYaml(const LLVMState &State, const llvm::StringRef Filename);
Clement Courbetac74acd2018-04-04 11:37:06 +000087};
88
Clement Courbetae8ae5dc2018-05-24 12:41:02 +000089//------------------------------------------------------------------------------
90// Utilities to work with Benchmark measures.
91
92// A class that measures stats over benchmark measures.
Clement Courbet684a5f62018-09-26 08:37:21 +000093class PerInstructionStats {
Clement Courbetae8ae5dc2018-05-24 12:41:02 +000094public:
95 void push(const BenchmarkMeasure &BM);
96
97 double avg() const {
98 assert(NumValues);
99 return SumValues / NumValues;
100 }
101 double min() const { return MinValue; }
102 double max() const { return MaxValue; }
103
Clement Courbet72287212018-06-04 11:11:55 +0000104 const std::string &key() const { return Key; }
105
Clement Courbetae8ae5dc2018-05-24 12:41:02 +0000106private:
107 std::string Key;
108 double SumValues = 0.0;
109 int NumValues = 0;
110 double MaxValue = std::numeric_limits<double>::min();
111 double MinValue = std::numeric_limits<double>::max();
112};
113
Clement Courbetac74acd2018-04-04 11:37:06 +0000114} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000115} // namespace llvm
Clement Courbetac74acd2018-04-04 11:37:06 +0000116
117#endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H