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. |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame^] | 61 | static llvm::Expected<InstructionBenchmark> |
| 62 | readYaml(const BenchmarkResultContext &Context, llvm::StringRef Filename); |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 63 | |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame^] | 64 | static llvm::Expected<std::vector<InstructionBenchmark>> |
| 65 | readYamls(const BenchmarkResultContext &Context, llvm::StringRef Filename); |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 66 | |
| 67 | void readYamlFrom(const BenchmarkResultContext &Context, |
| 68 | llvm::StringRef InputContent); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 69 | |
| 70 | // Write functions, non-const because of YAML traits. |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 71 | void writeYamlTo(const BenchmarkResultContext &Context, llvm::raw_ostream &S); |
| 72 | |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame^] | 73 | llvm::Error writeYaml(const BenchmarkResultContext &Context, |
| 74 | const llvm::StringRef Filename); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Clement Courbet | ae8ae5dc | 2018-05-24 12:41:02 +0000 | [diff] [blame] | 77 | //------------------------------------------------------------------------------ |
| 78 | // Utilities to work with Benchmark measures. |
| 79 | |
| 80 | // A class that measures stats over benchmark measures. |
| 81 | class BenchmarkMeasureStats { |
| 82 | public: |
| 83 | void push(const BenchmarkMeasure &BM); |
| 84 | |
| 85 | double avg() const { |
| 86 | assert(NumValues); |
| 87 | return SumValues / NumValues; |
| 88 | } |
| 89 | double min() const { return MinValue; } |
| 90 | double max() const { return MaxValue; } |
| 91 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 92 | const std::string &key() const { return Key; } |
| 93 | |
Clement Courbet | ae8ae5dc | 2018-05-24 12:41:02 +0000 | [diff] [blame] | 94 | private: |
| 95 | std::string Key; |
| 96 | double SumValues = 0.0; |
| 97 | int NumValues = 0; |
| 98 | double MaxValue = std::numeric_limits<double>::min(); |
| 99 | double MinValue = std::numeric_limits<double>::max(); |
| 100 | }; |
| 101 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 102 | // This context is used when de/serializing InstructionBenchmark to guarantee |
| 103 | // that Registers and Instructions are human readable and preserved accross |
| 104 | // different versions of LLVM. |
| 105 | struct BenchmarkResultContext { |
| 106 | BenchmarkResultContext() = default; |
| 107 | BenchmarkResultContext(BenchmarkResultContext &&) = default; |
| 108 | BenchmarkResultContext &operator=(BenchmarkResultContext &&) = default; |
| 109 | BenchmarkResultContext(const BenchmarkResultContext &) = delete; |
| 110 | BenchmarkResultContext &operator=(const BenchmarkResultContext &) = delete; |
| 111 | |
| 112 | // Populate Registers and Instruction mapping. |
| 113 | void addRegEntry(unsigned RegNo, llvm::StringRef Name); |
| 114 | void addInstrEntry(unsigned Opcode, llvm::StringRef Name); |
| 115 | |
| 116 | // Register accessors. |
| 117 | llvm::StringRef getRegName(unsigned RegNo) const; |
| 118 | unsigned getRegNo(llvm::StringRef Name) const; // 0 is not found. |
| 119 | |
| 120 | // Instruction accessors. |
| 121 | llvm::StringRef getInstrName(unsigned Opcode) const; |
| 122 | unsigned getInstrOpcode(llvm::StringRef Name) const; // 0 is not found. |
| 123 | |
| 124 | private: |
| 125 | // Ideally we would like to use MCRegisterInfo and MCInstrInfo but doing so |
| 126 | // would make testing harder, instead we create a mapping that we can easily |
| 127 | // populate. |
| 128 | std::unordered_map<unsigned, llvm::StringRef> InstrOpcodeToName; |
| 129 | std::unordered_map<unsigned, llvm::StringRef> RegNoToName; |
| 130 | llvm::StringMap<unsigned> InstrNameToOpcode; |
| 131 | llvm::StringMap<unsigned> RegNameToNo; |
| 132 | }; |
| 133 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 134 | } // namespace exegesis |
| 135 | |
| 136 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_BENCHMARKRESULT_H |