blob: c883a3409ae31a3157dd52e878ef56e5065275fa [file] [log] [blame]
Clement Courbetac74acd2018-04-04 11:37:06 +00001//===-- BenchmarkResult.h ---------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Clement Courbetac74acd2018-04-04 11:37:06 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Defines classes to represent measurements and serialize/deserialize them to
11// Yaml.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H
16#define LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H
17
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000018#include "LlvmState.h"
Clement Courbet49195342019-10-08 09:06:48 +000019#include "RegisterValue.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
Fangrui Song32401af2018-10-22 17:10:47 +000030namespace llvm {
Reid Kleckneraf450ea2020-02-29 10:23:54 -080031class Error;
32
Clement Courbetac74acd2018-04-04 11:37:06 +000033namespace exegesis {
34
Clement Courbeta66bfaa42018-05-15 13:07:05 +000035struct InstructionBenchmarkKey {
36 // The LLVM opcode name.
Clement Courbet50cdd562019-10-09 11:58:42 +000037 std::vector<MCInst> Instructions;
Guillaume Chatelet345fae52018-09-25 15:15:54 +000038 // The initial values of the registers.
39 std::vector<RegisterValue> RegisterInitialValues;
Clement Courbeta66bfaa42018-05-15 13:07:05 +000040 // An opaque configuration, that can be used to separate several benchmarks of
41 // the same instruction under different configurations.
42 std::string Config;
Clement Courbetac74acd2018-04-04 11:37:06 +000043};
44
45struct BenchmarkMeasure {
Clement Courbet596c56f2018-09-26 11:22:56 +000046 // A helper to create an unscaled BenchmarkMeasure.
47 static BenchmarkMeasure Create(std::string Key, double Value) {
Clement Courbet28d4f852018-09-26 13:35:10 +000048 return {Key, Value, Value};
Clement Courbet596c56f2018-09-26 11:22:56 +000049 }
Clement Courbetac74acd2018-04-04 11:37:06 +000050 std::string Key;
Clement Courbet684a5f62018-09-26 08:37:21 +000051 // This is the per-instruction value, i.e. measured quantity scaled per
52 // instruction.
53 double PerInstructionValue;
54 // This is the per-snippet value, i.e. measured quantity for one repetition of
55 // the whole snippet.
56 double PerSnippetValue;
Clement Courbetac74acd2018-04-04 11:37:06 +000057};
58
59// The result of an instruction benchmark.
60struct InstructionBenchmark {
Clement Courbeta66bfaa42018-05-15 13:07:05 +000061 InstructionBenchmarkKey Key;
Clement Courbet362653f2019-01-30 16:02:20 +000062 enum ModeE { Unknown, Latency, Uops, InverseThroughput };
Clement Courbet62b34fa2018-06-06 09:42:36 +000063 ModeE Mode;
Clement Courbetac74acd2018-04-04 11:37:06 +000064 std::string CpuName;
65 std::string LLVMTriple;
Roman Lebedev69716392019-02-20 09:14:04 +000066 // Which instruction is being benchmarked here?
Clement Courbet50cdd562019-10-09 11:58:42 +000067 const MCInst &keyInstruction() const { return Key.Instructions[0]; }
Clement Courbetf64007f2018-06-15 09:27:12 +000068 // The number of instructions inside the repeated snippet. For example, if a
69 // snippet of 3 instructions is repeated 4 times, this is 12.
Clement Courbet8fb6e402018-04-04 12:01:46 +000070 int NumRepetitions = 0;
Roman Lebedevde22d712020-04-02 09:28:35 +030071 enum RepetitionModeE { Duplicate, Loop, AggregateMin };
Clement Courbetf64007f2018-06-15 09:27:12 +000072 // Note that measurements are per instruction.
Clement Courbetac74acd2018-04-04 11:37:06 +000073 std::vector<BenchmarkMeasure> Measurements;
74 std::string Error;
Clement Courbeta66bfaa42018-05-15 13:07:05 +000075 std::string Info;
Clement Courbet4273e1e2018-06-15 07:30:45 +000076 std::vector<uint8_t> AssembledSnippet;
Vy Nguyene086a392020-06-25 11:15:16 -040077 // How to aggregate measurements.
78 enum ResultAggregationModeE { Min, Max, Mean, MinVariance };
Clement Courbet0e69e2d2018-05-17 10:52:18 +000079 // Read functions.
Clement Courbet50cdd562019-10-09 11:58:42 +000080 static Expected<InstructionBenchmark> readYaml(const LLVMState &State,
81 StringRef Filename);
Clement Courbet53d35d22018-06-05 10:56:19 +000082
Clement Courbet50cdd562019-10-09 11:58:42 +000083 static Expected<std::vector<InstructionBenchmark>>
84 readYamls(const LLVMState &State, StringRef Filename);
Clement Courbet53d35d22018-06-05 10:56:19 +000085
Clement Courbet50cdd562019-10-09 11:58:42 +000086 class Error readYamlFrom(const LLVMState &State, StringRef InputContent);
Clement Courbet0e69e2d2018-05-17 10:52:18 +000087
88 // Write functions, non-const because of YAML traits.
Clement Courbet50cdd562019-10-09 11:58:42 +000089 class Error writeYamlTo(const LLVMState &State, raw_ostream &S);
Clement Courbet53d35d22018-06-05 10:56:19 +000090
Clement Courbet50cdd562019-10-09 11:58:42 +000091 class Error writeYaml(const LLVMState &State, const StringRef Filename);
Clement Courbetac74acd2018-04-04 11:37:06 +000092};
93
Clement Courbetae8ae5dc2018-05-24 12:41:02 +000094//------------------------------------------------------------------------------
95// Utilities to work with Benchmark measures.
96
97// A class that measures stats over benchmark measures.
Clement Courbet684a5f62018-09-26 08:37:21 +000098class PerInstructionStats {
Clement Courbetae8ae5dc2018-05-24 12:41:02 +000099public:
100 void push(const BenchmarkMeasure &BM);
101
102 double avg() const {
103 assert(NumValues);
104 return SumValues / NumValues;
105 }
106 double min() const { return MinValue; }
107 double max() const { return MaxValue; }
108
Clement Courbet72287212018-06-04 11:11:55 +0000109 const std::string &key() const { return Key; }
110
Clement Courbetae8ae5dc2018-05-24 12:41:02 +0000111private:
112 std::string Key;
113 double SumValues = 0.0;
114 int NumValues = 0;
115 double MaxValue = std::numeric_limits<double>::min();
116 double MinValue = std::numeric_limits<double>::max();
117};
118
Clement Courbetac74acd2018-04-04 11:37:06 +0000119} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000120} // namespace llvm
Clement Courbetac74acd2018-04-04 11:37:06 +0000121
122#endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H