blob: 132dc36622a887bb1ec7d8205a05fa643c64ae9a [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 {
Clement Courbetac74acd2018-04-04 11:37:06 +000031namespace 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 {
Clement Courbet596c56f2018-09-26 11:22:56 +000044 // A helper to create an unscaled BenchmarkMeasure.
45 static BenchmarkMeasure Create(std::string Key, double Value) {
Clement Courbet28d4f852018-09-26 13:35:10 +000046 return {Key, Value, Value};
Clement Courbet596c56f2018-09-26 11:22:56 +000047 }
Clement Courbetac74acd2018-04-04 11:37:06 +000048 std::string Key;
Clement Courbet684a5f62018-09-26 08:37:21 +000049 // This is the per-instruction value, i.e. measured quantity scaled per
50 // instruction.
51 double PerInstructionValue;
52 // This is the per-snippet value, i.e. measured quantity for one repetition of
53 // the whole snippet.
54 double PerSnippetValue;
Clement Courbetac74acd2018-04-04 11:37:06 +000055};
56
57// The result of an instruction benchmark.
58struct InstructionBenchmark {
Clement Courbeta66bfaa42018-05-15 13:07:05 +000059 InstructionBenchmarkKey Key;
Clement Courbet362653f2019-01-30 16:02:20 +000060 enum ModeE { Unknown, Latency, Uops, InverseThroughput };
Clement Courbet62b34fa2018-06-06 09:42:36 +000061 ModeE Mode;
Clement Courbetac74acd2018-04-04 11:37:06 +000062 std::string CpuName;
63 std::string LLVMTriple;
Roman Lebedev69716392019-02-20 09:14:04 +000064 // Which instruction is being benchmarked here?
65 const llvm::MCInst &keyInstruction() const { return Key.Instructions[0]; }
Clement Courbetf64007f2018-06-15 09:27:12 +000066 // The number of instructions inside the repeated snippet. For example, if a
67 // snippet of 3 instructions is repeated 4 times, this is 12.
Clement Courbet8fb6e402018-04-04 12:01:46 +000068 int NumRepetitions = 0;
Clement Courbet9431b722019-09-27 12:56:24 +000069 enum RepetitionModeE { Duplicate, Loop };
70 RepetitionModeE RepetitionMode;
Clement Courbetf64007f2018-06-15 09:27:12 +000071 // Note that measurements are per instruction.
Clement Courbetac74acd2018-04-04 11:37:06 +000072 std::vector<BenchmarkMeasure> Measurements;
73 std::string Error;
Clement Courbeta66bfaa42018-05-15 13:07:05 +000074 std::string Info;
Clement Courbet4273e1e2018-06-15 07:30:45 +000075 std::vector<uint8_t> AssembledSnippet;
Clement Courbetac74acd2018-04-04 11:37:06 +000076
Clement Courbet0e69e2d2018-05-17 10:52:18 +000077 // Read functions.
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000078 static llvm::Expected<InstructionBenchmark>
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000079 readYaml(const LLVMState &State, llvm::StringRef Filename);
Clement Courbet53d35d22018-06-05 10:56:19 +000080
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000081 static llvm::Expected<std::vector<InstructionBenchmark>>
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000082 readYamls(const LLVMState &State, llvm::StringRef Filename);
Clement Courbet53d35d22018-06-05 10:56:19 +000083
Roman Lebedev628f1ae2019-04-10 12:19:57 +000084 llvm::Error readYamlFrom(const LLVMState &State,
85 llvm::StringRef InputContent);
Clement Courbet0e69e2d2018-05-17 10:52:18 +000086
87 // Write functions, non-const because of YAML traits.
Roman Lebedev628f1ae2019-04-10 12:19:57 +000088 llvm::Error writeYamlTo(const LLVMState &State, llvm::raw_ostream &S);
Clement Courbet53d35d22018-06-05 10:56:19 +000089
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000090 llvm::Error writeYaml(const LLVMState &State, const llvm::StringRef Filename);
Clement Courbetac74acd2018-04-04 11:37:06 +000091};
92
Clement Courbetae8ae5dc2018-05-24 12:41:02 +000093//------------------------------------------------------------------------------
94// Utilities to work with Benchmark measures.
95
96// A class that measures stats over benchmark measures.
Clement Courbet684a5f62018-09-26 08:37:21 +000097class PerInstructionStats {
Clement Courbetae8ae5dc2018-05-24 12:41:02 +000098public:
99 void push(const BenchmarkMeasure &BM);
100
101 double avg() const {
102 assert(NumValues);
103 return SumValues / NumValues;
104 }
105 double min() const { return MinValue; }
106 double max() const { return MaxValue; }
107
Clement Courbet72287212018-06-04 11:11:55 +0000108 const std::string &key() const { return Key; }
109
Clement Courbetae8ae5dc2018-05-24 12:41:02 +0000110private:
111 std::string Key;
112 double SumValues = 0.0;
113 int NumValues = 0;
114 double MaxValue = std::numeric_limits<double>::min();
115 double MinValue = std::numeric_limits<double>::max();
116};
117
Clement Courbetac74acd2018-04-04 11:37:06 +0000118} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000119} // namespace llvm
Clement Courbetac74acd2018-04-04 11:37:06 +0000120
121#endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H