Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 1 | //===-- BenchmarkResult.h ---------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 6 | // |
| 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 Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 18 | #include "LlvmState.h" |
Clement Courbet | 4919534 | 2019-10-08 09:06:48 +0000 | [diff] [blame] | 19 | #include "RegisterValue.h" |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringMap.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringRef.h" |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCInst.h" |
| 23 | #include "llvm/MC/MCInstBuilder.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 24 | #include "llvm/Support/YAMLTraits.h" |
Clement Courbet | ae8ae5dc | 2018-05-24 12:41:02 +0000 | [diff] [blame] | 25 | #include <limits> |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 26 | #include <string> |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 27 | #include <unordered_map> |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 30 | namespace llvm { |
Reid Kleckner | af450ea | 2020-02-29 10:23:54 -0800 | [diff] [blame] | 31 | class Error; |
| 32 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 33 | namespace exegesis { |
| 34 | |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 35 | struct InstructionBenchmarkKey { |
| 36 | // The LLVM opcode name. |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 37 | std::vector<MCInst> Instructions; |
Guillaume Chatelet | 345fae5 | 2018-09-25 15:15:54 +0000 | [diff] [blame] | 38 | // The initial values of the registers. |
| 39 | std::vector<RegisterValue> RegisterInitialValues; |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 40 | // An opaque configuration, that can be used to separate several benchmarks of |
| 41 | // the same instruction under different configurations. |
| 42 | std::string Config; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | struct BenchmarkMeasure { |
Clement Courbet | 596c56f | 2018-09-26 11:22:56 +0000 | [diff] [blame] | 46 | // A helper to create an unscaled BenchmarkMeasure. |
| 47 | static BenchmarkMeasure Create(std::string Key, double Value) { |
Clement Courbet | 28d4f85 | 2018-09-26 13:35:10 +0000 | [diff] [blame] | 48 | return {Key, Value, Value}; |
Clement Courbet | 596c56f | 2018-09-26 11:22:56 +0000 | [diff] [blame] | 49 | } |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 50 | std::string Key; |
Clement Courbet | 684a5f6 | 2018-09-26 08:37:21 +0000 | [diff] [blame] | 51 | // 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 Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | // The result of an instruction benchmark. |
| 60 | struct InstructionBenchmark { |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 61 | InstructionBenchmarkKey Key; |
Clement Courbet | 362653f | 2019-01-30 16:02:20 +0000 | [diff] [blame] | 62 | enum ModeE { Unknown, Latency, Uops, InverseThroughput }; |
Clement Courbet | 62b34fa | 2018-06-06 09:42:36 +0000 | [diff] [blame] | 63 | ModeE Mode; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 64 | std::string CpuName; |
| 65 | std::string LLVMTriple; |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 66 | // Which instruction is being benchmarked here? |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 67 | const MCInst &keyInstruction() const { return Key.Instructions[0]; } |
Clement Courbet | f64007f | 2018-06-15 09:27:12 +0000 | [diff] [blame] | 68 | // 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 Courbet | 8fb6e40 | 2018-04-04 12:01:46 +0000 | [diff] [blame] | 70 | int NumRepetitions = 0; |
Roman Lebedev | de22d71 | 2020-04-02 09:28:35 +0300 | [diff] [blame] | 71 | enum RepetitionModeE { Duplicate, Loop, AggregateMin }; |
Clement Courbet | f64007f | 2018-06-15 09:27:12 +0000 | [diff] [blame] | 72 | // Note that measurements are per instruction. |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 73 | std::vector<BenchmarkMeasure> Measurements; |
| 74 | std::string Error; |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 75 | std::string Info; |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 76 | std::vector<uint8_t> AssembledSnippet; |
Vy Nguyen | e086a39 | 2020-06-25 11:15:16 -0400 | [diff] [blame] | 77 | // How to aggregate measurements. |
| 78 | enum ResultAggregationModeE { Min, Max, Mean, MinVariance }; |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 79 | // Read functions. |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 80 | static Expected<InstructionBenchmark> readYaml(const LLVMState &State, |
| 81 | StringRef Filename); |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 82 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 83 | static Expected<std::vector<InstructionBenchmark>> |
| 84 | readYamls(const LLVMState &State, StringRef Filename); |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 85 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 86 | class Error readYamlFrom(const LLVMState &State, StringRef InputContent); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 87 | |
| 88 | // Write functions, non-const because of YAML traits. |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 89 | class Error writeYamlTo(const LLVMState &State, raw_ostream &S); |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 90 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 91 | class Error writeYaml(const LLVMState &State, const StringRef Filename); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
Clement Courbet | ae8ae5dc | 2018-05-24 12:41:02 +0000 | [diff] [blame] | 94 | //------------------------------------------------------------------------------ |
| 95 | // Utilities to work with Benchmark measures. |
| 96 | |
| 97 | // A class that measures stats over benchmark measures. |
Clement Courbet | 684a5f6 | 2018-09-26 08:37:21 +0000 | [diff] [blame] | 98 | class PerInstructionStats { |
Clement Courbet | ae8ae5dc | 2018-05-24 12:41:02 +0000 | [diff] [blame] | 99 | public: |
| 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 Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 109 | const std::string &key() const { return Key; } |
| 110 | |
Clement Courbet | ae8ae5dc | 2018-05-24 12:41:02 +0000 | [diff] [blame] | 111 | private: |
| 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 Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 119 | } // namespace exegesis |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 120 | } // namespace llvm |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 121 | |
| 122 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H |