Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 1 | //===-- BenchmarkResult.h ---------------------------------------*- C++ -*-===// |
| 2 | // |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [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 |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [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 | |
| 18 | #include "BenchmarkCode.h" |
| 19 | #include "LlvmState.h" |
| 20 | #include "llvm/ADT/StringMap.h" |
| 21 | #include "llvm/ADT/StringRef.h" |
| 22 | #include "llvm/MC/MCInst.h" |
| 23 | #include "llvm/MC/MCInstBuilder.h" |
| 24 | #include "llvm/Support/YAMLTraits.h" |
| 25 | #include <limits> |
| 26 | #include <string> |
| 27 | #include <unordered_map> |
| 28 | #include <vector> |
| 29 | |
| 30 | namespace llvm { |
| 31 | namespace exegesis { |
| 32 | |
| 33 | struct InstructionBenchmarkKey { |
| 34 | // The LLVM opcode name. |
| 35 | std::vector<llvm::MCInst> Instructions; |
| 36 | // The initial values of the registers. |
| 37 | std::vector<RegisterValue> RegisterInitialValues; |
| 38 | // An opaque configuration, that can be used to separate several benchmarks of |
| 39 | // the same instruction under different configurations. |
| 40 | std::string Config; |
| 41 | }; |
| 42 | |
| 43 | struct BenchmarkMeasure { |
| 44 | // A helper to create an unscaled BenchmarkMeasure. |
| 45 | static BenchmarkMeasure Create(std::string Key, double Value) { |
| 46 | return {Key, Value, Value}; |
| 47 | } |
| 48 | std::string Key; |
| 49 | // 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; |
| 55 | }; |
| 56 | |
| 57 | // The result of an instruction benchmark. |
| 58 | struct InstructionBenchmark { |
| 59 | InstructionBenchmarkKey Key; |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame^] | 60 | enum ModeE { Unknown, Latency, Uops, InverseThroughput }; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 61 | ModeE Mode; |
| 62 | std::string CpuName; |
| 63 | std::string LLVMTriple; |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame^] | 64 | // Which instruction is being benchmarked here? |
| 65 | const llvm::MCInst &keyInstruction() const { return Key.Instructions[0]; } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 66 | // The number of instructions inside the repeated snippet. For example, if a |
| 67 | // snippet of 3 instructions is repeated 4 times, this is 12. |
| 68 | int NumRepetitions = 0; |
| 69 | // Note that measurements are per instruction. |
| 70 | std::vector<BenchmarkMeasure> Measurements; |
| 71 | std::string Error; |
| 72 | std::string Info; |
| 73 | std::vector<uint8_t> AssembledSnippet; |
| 74 | |
| 75 | // Read functions. |
| 76 | static llvm::Expected<InstructionBenchmark> |
| 77 | readYaml(const LLVMState &State, llvm::StringRef Filename); |
| 78 | |
| 79 | static llvm::Expected<std::vector<InstructionBenchmark>> |
| 80 | readYamls(const LLVMState &State, llvm::StringRef Filename); |
| 81 | |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame^] | 82 | llvm::Error readYamlFrom(const LLVMState &State, |
| 83 | llvm::StringRef InputContent); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 84 | |
| 85 | // Write functions, non-const because of YAML traits. |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame^] | 86 | llvm::Error writeYamlTo(const LLVMState &State, llvm::raw_ostream &S); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 87 | |
| 88 | llvm::Error writeYaml(const LLVMState &State, const llvm::StringRef Filename); |
| 89 | }; |
| 90 | |
| 91 | //------------------------------------------------------------------------------ |
| 92 | // Utilities to work with Benchmark measures. |
| 93 | |
| 94 | // A class that measures stats over benchmark measures. |
| 95 | class PerInstructionStats { |
| 96 | public: |
| 97 | void push(const BenchmarkMeasure &BM); |
| 98 | |
| 99 | double avg() const { |
| 100 | assert(NumValues); |
| 101 | return SumValues / NumValues; |
| 102 | } |
| 103 | double min() const { return MinValue; } |
| 104 | double max() const { return MaxValue; } |
| 105 | |
| 106 | const std::string &key() const { return Key; } |
| 107 | |
| 108 | private: |
| 109 | std::string Key; |
| 110 | double SumValues = 0.0; |
| 111 | int NumValues = 0; |
| 112 | double MaxValue = std::numeric_limits<double>::min(); |
| 113 | double MinValue = std::numeric_limits<double>::max(); |
| 114 | }; |
| 115 | |
| 116 | } // namespace exegesis |
| 117 | } // namespace llvm |
| 118 | |
| 119 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H |