Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 1 | //===-- BenchmarkResult.cpp -------------------------------------*- 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 | #include "BenchmarkResult.h" |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/StringRef.h" |
| 13 | #include "llvm/Support/FileOutputBuffer.h" |
| 14 | #include "llvm/Support/FileSystem.h" |
| 15 | #include "llvm/Support/Format.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | |
| 18 | // Defining YAML traits for IO. |
| 19 | namespace llvm { |
| 20 | namespace yaml { |
| 21 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 22 | // std::vector<llvm::MCInst> will be rendered as a list. |
| 23 | template <> struct SequenceElementTraits<llvm::MCInst> { |
| 24 | static const bool flow = false; |
| 25 | }; |
| 26 | |
| 27 | template <> struct ScalarTraits<llvm::MCInst> { |
| 28 | |
| 29 | static void output(const llvm::MCInst &Value, void *Ctx, |
| 30 | llvm::raw_ostream &Out) { |
| 31 | assert(Ctx); |
| 32 | auto *Context = static_cast<const exegesis::BenchmarkResultContext *>(Ctx); |
| 33 | const StringRef Name = Context->getInstrName(Value.getOpcode()); |
| 34 | assert(!Name.empty()); |
| 35 | Out << Name; |
| 36 | } |
| 37 | |
| 38 | static StringRef input(StringRef Scalar, void *Ctx, llvm::MCInst &Value) { |
| 39 | assert(Ctx); |
| 40 | auto *Context = static_cast<const exegesis::BenchmarkResultContext *>(Ctx); |
| 41 | const unsigned Opcode = Context->getInstrOpcode(Scalar); |
| 42 | if (Opcode == 0) { |
| 43 | return "Unable to parse instruction"; |
| 44 | } |
| 45 | Value.setOpcode(Opcode); |
| 46 | return StringRef(); |
| 47 | } |
| 48 | |
| 49 | static QuotingType mustQuote(StringRef) { return QuotingType::Single; } |
| 50 | |
| 51 | static const bool flow = true; |
| 52 | }; |
| 53 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 54 | // std::vector<exegesis::Measure> will be rendered as a list. |
| 55 | template <> struct SequenceElementTraits<exegesis::BenchmarkMeasure> { |
| 56 | static const bool flow = false; |
| 57 | }; |
| 58 | |
| 59 | // exegesis::Measure is rendererd as a flow instead of a list. |
| 60 | // e.g. { "key": "the key", "value": 0123 } |
| 61 | template <> struct MappingTraits<exegesis::BenchmarkMeasure> { |
| 62 | static void mapping(IO &Io, exegesis::BenchmarkMeasure &Obj) { |
| 63 | Io.mapRequired("key", Obj.Key); |
| 64 | Io.mapRequired("value", Obj.Value); |
| 65 | Io.mapOptional("debug_string", Obj.DebugString); |
| 66 | } |
| 67 | static const bool flow = true; |
| 68 | }; |
| 69 | |
Clement Courbet | 2cb97b9 | 2018-06-04 11:43:40 +0000 | [diff] [blame] | 70 | template <> |
| 71 | struct ScalarEnumerationTraits<exegesis::InstructionBenchmarkKey::ModeE> { |
| 72 | static void enumeration(IO &Io, |
| 73 | exegesis::InstructionBenchmarkKey::ModeE &Value) { |
| 74 | Io.enumCase(Value, "", exegesis::InstructionBenchmarkKey::Unknown); |
| 75 | Io.enumCase(Value, "latency", exegesis::InstructionBenchmarkKey::Latency); |
| 76 | Io.enumCase(Value, "uops", exegesis::InstructionBenchmarkKey::Uops); |
| 77 | } |
| 78 | }; |
| 79 | |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 80 | template <> struct MappingTraits<exegesis::InstructionBenchmarkKey> { |
| 81 | static void mapping(IO &Io, exegesis::InstructionBenchmarkKey &Obj) { |
| 82 | Io.mapRequired("opcode_name", Obj.OpcodeName); |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 83 | Io.mapOptional("instructions", Obj.Instructions); |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 84 | Io.mapRequired("mode", Obj.Mode); |
| 85 | Io.mapOptional("config", Obj.Config); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 86 | } |
| 87 | }; |
| 88 | |
| 89 | template <> struct MappingTraits<exegesis::InstructionBenchmark> { |
| 90 | static void mapping(IO &Io, exegesis::InstructionBenchmark &Obj) { |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 91 | Io.mapRequired("key", Obj.Key); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 92 | Io.mapRequired("cpu_name", Obj.CpuName); |
| 93 | Io.mapRequired("llvm_triple", Obj.LLVMTriple); |
| 94 | Io.mapRequired("num_repetitions", Obj.NumRepetitions); |
| 95 | Io.mapRequired("measurements", Obj.Measurements); |
| 96 | Io.mapRequired("error", Obj.Error); |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 97 | Io.mapOptional("info", Obj.Info); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 98 | } |
| 99 | }; |
| 100 | |
| 101 | } // namespace yaml |
| 102 | } // namespace llvm |
| 103 | |
Clement Courbet | 7b7c27a | 2018-05-14 09:01:22 +0000 | [diff] [blame] | 104 | LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(exegesis::InstructionBenchmark) |
| 105 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 106 | namespace exegesis { |
| 107 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 108 | void BenchmarkResultContext::addRegEntry(unsigned RegNo, llvm::StringRef Name) { |
| 109 | assert(RegNoToName.find(RegNo) == RegNoToName.end()); |
| 110 | assert(RegNameToNo.find(Name) == RegNameToNo.end()); |
| 111 | RegNoToName[RegNo] = Name; |
| 112 | RegNameToNo[Name] = RegNo; |
| 113 | } |
| 114 | |
| 115 | llvm::StringRef BenchmarkResultContext::getRegName(unsigned RegNo) const { |
| 116 | const auto Itr = RegNoToName.find(RegNo); |
| 117 | if (Itr != RegNoToName.end()) |
| 118 | return Itr->second; |
| 119 | return {}; |
| 120 | } |
| 121 | |
| 122 | unsigned BenchmarkResultContext::getRegNo(llvm::StringRef Name) const { |
| 123 | const auto Itr = RegNameToNo.find(Name); |
| 124 | if (Itr != RegNameToNo.end()) |
| 125 | return Itr->second; |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | void BenchmarkResultContext::addInstrEntry(unsigned Opcode, |
| 130 | llvm::StringRef Name) { |
| 131 | assert(InstrOpcodeToName.find(Opcode) == InstrOpcodeToName.end()); |
| 132 | assert(InstrNameToOpcode.find(Name) == InstrNameToOpcode.end()); |
| 133 | InstrOpcodeToName[Opcode] = Name; |
| 134 | InstrNameToOpcode[Name] = Opcode; |
| 135 | } |
| 136 | |
| 137 | llvm::StringRef BenchmarkResultContext::getInstrName(unsigned Opcode) const { |
| 138 | const auto Itr = InstrOpcodeToName.find(Opcode); |
| 139 | if (Itr != InstrOpcodeToName.end()) |
| 140 | return Itr->second; |
| 141 | return {}; |
| 142 | } |
| 143 | |
| 144 | unsigned BenchmarkResultContext::getInstrOpcode(llvm::StringRef Name) const { |
| 145 | const auto Itr = InstrNameToOpcode.find(Name); |
| 146 | if (Itr != InstrNameToOpcode.end()) |
| 147 | return Itr->second; |
| 148 | return 0; |
| 149 | } |
| 150 | |
Clement Courbet | 7b7c27a | 2018-05-14 09:01:22 +0000 | [diff] [blame] | 151 | template <typename ObjectOrList> |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 152 | static ObjectOrList readYamlOrDieCommon(const BenchmarkResultContext &Context, |
| 153 | llvm::StringRef Filename) { |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 154 | std::unique_ptr<llvm::MemoryBuffer> MemBuffer = llvm::cantFail( |
| 155 | llvm::errorOrToExpected(llvm::MemoryBuffer::getFile(Filename))); |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 156 | // YAML IO requires a mutable pointer to Context but we guarantee to not |
| 157 | // modify it. |
| 158 | llvm::yaml::Input Yin(*MemBuffer, |
| 159 | const_cast<BenchmarkResultContext *>(&Context)); |
Clement Courbet | 7b7c27a | 2018-05-14 09:01:22 +0000 | [diff] [blame] | 160 | ObjectOrList Benchmark; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 161 | Yin >> Benchmark; |
| 162 | return Benchmark; |
| 163 | } |
| 164 | |
Clement Courbet | 7b7c27a | 2018-05-14 09:01:22 +0000 | [diff] [blame] | 165 | InstructionBenchmark |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 166 | InstructionBenchmark::readYamlOrDie(const BenchmarkResultContext &Context, |
| 167 | llvm::StringRef Filename) { |
| 168 | return readYamlOrDieCommon<InstructionBenchmark>(Context, Filename); |
Clement Courbet | 7b7c27a | 2018-05-14 09:01:22 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | std::vector<InstructionBenchmark> |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 172 | InstructionBenchmark::readYamlsOrDie(const BenchmarkResultContext &Context, |
| 173 | llvm::StringRef Filename) { |
| 174 | return readYamlOrDieCommon<std::vector<InstructionBenchmark>>(Context, |
| 175 | Filename); |
Clement Courbet | 7b7c27a | 2018-05-14 09:01:22 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 178 | void InstructionBenchmark::writeYamlTo(const BenchmarkResultContext &Context, |
| 179 | llvm::raw_ostream &S) { |
| 180 | // YAML IO requires a mutable pointer to Context but we guarantee to not |
| 181 | // modify it. |
| 182 | llvm::yaml::Output Yout(S, const_cast<BenchmarkResultContext *>(&Context)); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 183 | Yout << *this; |
| 184 | } |
| 185 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 186 | void InstructionBenchmark::readYamlFrom(const BenchmarkResultContext &Context, |
| 187 | llvm::StringRef InputContent) { |
| 188 | // YAML IO requires a mutable pointer to Context but we guarantee to not |
| 189 | // modify it. |
| 190 | llvm::yaml::Input Yin(InputContent, |
| 191 | const_cast<BenchmarkResultContext *>(&Context)); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 192 | Yin >> *this; |
| 193 | } |
| 194 | |
| 195 | // FIXME: Change the API to let the caller handle errors. |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 196 | void InstructionBenchmark::writeYamlOrDie(const BenchmarkResultContext &Context, |
| 197 | const llvm::StringRef Filename) { |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 198 | if (Filename == "-") { |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 199 | writeYamlTo(Context, llvm::outs()); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 200 | } else { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 201 | int ResultFD = 0; |
| 202 | llvm::cantFail(llvm::errorCodeToError( |
| 203 | openFileForWrite(Filename, ResultFD, llvm::sys::fs::F_Text))); |
| 204 | llvm::raw_fd_ostream Ostr(ResultFD, true /*shouldClose*/); |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 205 | writeYamlTo(Context, Ostr); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Clement Courbet | ae8ae5dc | 2018-05-24 12:41:02 +0000 | [diff] [blame] | 209 | void BenchmarkMeasureStats::push(const BenchmarkMeasure &BM) { |
| 210 | if (Key.empty()) |
| 211 | Key = BM.Key; |
| 212 | assert(Key == BM.Key); |
| 213 | ++NumValues; |
| 214 | SumValues += BM.Value; |
| 215 | MaxValue = std::max(MaxValue, BM.Value); |
| 216 | MinValue = std::min(MinValue, BM.Value); |
| 217 | } |
| 218 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 219 | } // namespace exegesis |