Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 1 | //===-- 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 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringMap.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringRef.h" |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCInst.h" |
| 22 | #include "llvm/MC/MCInstBuilder.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 23 | #include "llvm/Support/YAMLTraits.h" |
Clement Courbet | ae8ae5dc | 2018-05-24 12:41:02 +0000 | [diff] [blame] | 24 | #include <limits> |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 25 | #include <string> |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 26 | #include <unordered_map> |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
| 29 | namespace exegesis { |
| 30 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 31 | struct BenchmarkResultContext; // Forward declaration. |
| 32 | |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 33 | struct InstructionBenchmarkKey { |
| 34 | // The LLVM opcode name. |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 35 | std::string OpcodeName; // FIXME: Deprecated, use Instructions below. |
| 36 | std::vector<llvm::MCInst> Instructions; |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 37 | // An opaque configuration, that can be used to separate several benchmarks of |
| 38 | // the same instruction under different configurations. |
| 39 | std::string Config; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | struct BenchmarkMeasure { |
| 43 | std::string Key; |
| 44 | double Value; |
| 45 | std::string DebugString; |
| 46 | }; |
| 47 | |
| 48 | // The result of an instruction benchmark. |
| 49 | struct InstructionBenchmark { |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 50 | InstructionBenchmarkKey Key; |
Clement Courbet | 62b34fa | 2018-06-06 09:42:36 +0000 | [diff] [blame^] | 51 | enum ModeE { Unknown, Latency, Uops }; |
| 52 | ModeE Mode; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 53 | std::string CpuName; |
| 54 | std::string LLVMTriple; |
Clement Courbet | 8fb6e40 | 2018-04-04 12:01:46 +0000 | [diff] [blame] | 55 | int NumRepetitions = 0; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 56 | std::vector<BenchmarkMeasure> Measurements; |
| 57 | std::string Error; |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 58 | std::string Info; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 59 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 60 | // Read functions. |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 61 | static InstructionBenchmark |
| 62 | readYamlOrDie(const BenchmarkResultContext &Context, |
| 63 | llvm::StringRef Filename); |
| 64 | |
| 65 | static std::vector<InstructionBenchmark> |
| 66 | readYamlsOrDie(const BenchmarkResultContext &Context, |
| 67 | llvm::StringRef Filename); |
| 68 | |
| 69 | void readYamlFrom(const BenchmarkResultContext &Context, |
| 70 | llvm::StringRef InputContent); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 71 | |
| 72 | // Write functions, non-const because of YAML traits. |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 73 | void writeYamlTo(const BenchmarkResultContext &Context, llvm::raw_ostream &S); |
| 74 | |
| 75 | void writeYamlOrDie(const BenchmarkResultContext &Context, |
| 76 | const llvm::StringRef Filename); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Clement Courbet | ae8ae5dc | 2018-05-24 12:41:02 +0000 | [diff] [blame] | 79 | //------------------------------------------------------------------------------ |
| 80 | // Utilities to work with Benchmark measures. |
| 81 | |
| 82 | // A class that measures stats over benchmark measures. |
| 83 | class BenchmarkMeasureStats { |
| 84 | public: |
| 85 | void push(const BenchmarkMeasure &BM); |
| 86 | |
| 87 | double avg() const { |
| 88 | assert(NumValues); |
| 89 | return SumValues / NumValues; |
| 90 | } |
| 91 | double min() const { return MinValue; } |
| 92 | double max() const { return MaxValue; } |
| 93 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 94 | const std::string &key() const { return Key; } |
| 95 | |
Clement Courbet | ae8ae5dc | 2018-05-24 12:41:02 +0000 | [diff] [blame] | 96 | private: |
| 97 | std::string Key; |
| 98 | double SumValues = 0.0; |
| 99 | int NumValues = 0; |
| 100 | double MaxValue = std::numeric_limits<double>::min(); |
| 101 | double MinValue = std::numeric_limits<double>::max(); |
| 102 | }; |
| 103 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 104 | // This context is used when de/serializing InstructionBenchmark to guarantee |
| 105 | // that Registers and Instructions are human readable and preserved accross |
| 106 | // different versions of LLVM. |
| 107 | struct BenchmarkResultContext { |
| 108 | BenchmarkResultContext() = default; |
| 109 | BenchmarkResultContext(BenchmarkResultContext &&) = default; |
| 110 | BenchmarkResultContext &operator=(BenchmarkResultContext &&) = default; |
| 111 | BenchmarkResultContext(const BenchmarkResultContext &) = delete; |
| 112 | BenchmarkResultContext &operator=(const BenchmarkResultContext &) = delete; |
| 113 | |
| 114 | // Populate Registers and Instruction mapping. |
| 115 | void addRegEntry(unsigned RegNo, llvm::StringRef Name); |
| 116 | void addInstrEntry(unsigned Opcode, llvm::StringRef Name); |
| 117 | |
| 118 | // Register accessors. |
| 119 | llvm::StringRef getRegName(unsigned RegNo) const; |
| 120 | unsigned getRegNo(llvm::StringRef Name) const; // 0 is not found. |
| 121 | |
| 122 | // Instruction accessors. |
| 123 | llvm::StringRef getInstrName(unsigned Opcode) const; |
| 124 | unsigned getInstrOpcode(llvm::StringRef Name) const; // 0 is not found. |
| 125 | |
| 126 | private: |
| 127 | // Ideally we would like to use MCRegisterInfo and MCInstrInfo but doing so |
| 128 | // would make testing harder, instead we create a mapping that we can easily |
| 129 | // populate. |
| 130 | std::unordered_map<unsigned, llvm::StringRef> InstrOpcodeToName; |
| 131 | std::unordered_map<unsigned, llvm::StringRef> RegNoToName; |
| 132 | llvm::StringMap<unsigned> InstrNameToOpcode; |
| 133 | llvm::StringMap<unsigned> RegNameToNo; |
| 134 | }; |
| 135 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 136 | } // namespace exegesis |
| 137 | |
| 138 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H |