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 | |
Guillaume Chatelet | 083a0c16 | 2018-06-07 07:40:40 +0000 | [diff] [blame] | 18 | static constexpr const char kIntegerFormat[] = "i_0x%" PRId64 "x"; |
| 19 | static constexpr const char kDoubleFormat[] = "f_%la"; |
| 20 | |
| 21 | static void serialize(const exegesis::BenchmarkResultContext &Context, |
| 22 | const llvm::MCOperand &MCOperand, llvm::raw_ostream &OS) { |
| 23 | if (MCOperand.isReg()) { |
| 24 | OS << Context.getRegName(MCOperand.getReg()); |
| 25 | } else if (MCOperand.isImm()) { |
| 26 | OS << llvm::format(kIntegerFormat, MCOperand.getImm()); |
| 27 | } else if (MCOperand.isFPImm()) { |
| 28 | OS << llvm::format(kDoubleFormat, MCOperand.getFPImm()); |
| 29 | } else { |
| 30 | OS << "INVALID"; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | static void serialize(const exegesis::BenchmarkResultContext &Context, |
| 35 | const llvm::MCInst &MCInst, llvm::raw_ostream &OS) { |
| 36 | OS << Context.getInstrName(MCInst.getOpcode()); |
| 37 | for (const auto &Op : MCInst) { |
| 38 | OS << ' '; |
| 39 | serialize(Context, Op, OS); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | static llvm::MCOperand |
| 44 | deserialize(const exegesis::BenchmarkResultContext &Context, |
| 45 | llvm::StringRef String) { |
| 46 | assert(!String.empty()); |
| 47 | int64_t IntValue = 0; |
| 48 | double DoubleValue = 0; |
| 49 | if (sscanf(String.data(), kIntegerFormat, &IntValue) == 1) |
| 50 | return llvm::MCOperand::createImm(IntValue); |
| 51 | if (sscanf(String.data(), kDoubleFormat, &DoubleValue) == 1) |
| 52 | return llvm::MCOperand::createFPImm(DoubleValue); |
| 53 | if (unsigned RegNo = Context.getRegNo(String)) // Returns 0 if invalid. |
| 54 | return llvm::MCOperand::createReg(RegNo); |
| 55 | return {}; |
| 56 | } |
| 57 | |
| 58 | static llvm::StringRef |
| 59 | deserialize(const exegesis::BenchmarkResultContext &Context, |
| 60 | llvm::StringRef String, llvm::MCInst &Value) { |
| 61 | llvm::SmallVector<llvm::StringRef, 8> Pieces; |
| 62 | String.split(Pieces, " "); |
| 63 | if (Pieces.empty()) |
| 64 | return "Invalid Instruction"; |
| 65 | bool ProcessOpcode = true; |
| 66 | for (llvm::StringRef Piece : Pieces) { |
| 67 | if (ProcessOpcode) { |
| 68 | ProcessOpcode = false; |
| 69 | Value.setOpcode(Context.getInstrOpcode(Piece)); |
| 70 | if (Value.getOpcode() == 0) |
| 71 | return "Unknown Opcode Name"; |
| 72 | } else { |
| 73 | Value.addOperand(deserialize(Context, Piece)); |
| 74 | } |
| 75 | } |
| 76 | return {}; |
| 77 | } |
| 78 | |
| 79 | // YAML IO requires a mutable pointer to Context but we guarantee to not |
| 80 | // modify it. |
| 81 | static void *getUntypedContext(const exegesis::BenchmarkResultContext &Ctx) { |
| 82 | return const_cast<exegesis::BenchmarkResultContext *>(&Ctx); |
| 83 | } |
| 84 | |
| 85 | static const exegesis::BenchmarkResultContext &getTypedContext(void *Ctx) { |
| 86 | assert(Ctx); |
| 87 | return *static_cast<const exegesis::BenchmarkResultContext *>(Ctx); |
| 88 | } |
| 89 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 90 | // Defining YAML traits for IO. |
| 91 | namespace llvm { |
| 92 | namespace yaml { |
| 93 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 94 | // std::vector<llvm::MCInst> will be rendered as a list. |
| 95 | template <> struct SequenceElementTraits<llvm::MCInst> { |
| 96 | static const bool flow = false; |
| 97 | }; |
| 98 | |
| 99 | template <> struct ScalarTraits<llvm::MCInst> { |
| 100 | |
| 101 | static void output(const llvm::MCInst &Value, void *Ctx, |
| 102 | llvm::raw_ostream &Out) { |
Guillaume Chatelet | 083a0c16 | 2018-06-07 07:40:40 +0000 | [diff] [blame] | 103 | serialize(getTypedContext(Ctx), Value, Out); |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static StringRef input(StringRef Scalar, void *Ctx, llvm::MCInst &Value) { |
Guillaume Chatelet | 083a0c16 | 2018-06-07 07:40:40 +0000 | [diff] [blame] | 107 | return deserialize(getTypedContext(Ctx), Scalar, Value); |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static QuotingType mustQuote(StringRef) { return QuotingType::Single; } |
| 111 | |
| 112 | static const bool flow = true; |
| 113 | }; |
| 114 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 115 | // std::vector<exegesis::Measure> will be rendered as a list. |
| 116 | template <> struct SequenceElementTraits<exegesis::BenchmarkMeasure> { |
| 117 | static const bool flow = false; |
| 118 | }; |
| 119 | |
| 120 | // exegesis::Measure is rendererd as a flow instead of a list. |
| 121 | // e.g. { "key": "the key", "value": 0123 } |
| 122 | template <> struct MappingTraits<exegesis::BenchmarkMeasure> { |
| 123 | static void mapping(IO &Io, exegesis::BenchmarkMeasure &Obj) { |
| 124 | Io.mapRequired("key", Obj.Key); |
| 125 | Io.mapRequired("value", Obj.Value); |
| 126 | Io.mapOptional("debug_string", Obj.DebugString); |
| 127 | } |
| 128 | static const bool flow = true; |
| 129 | }; |
| 130 | |
Clement Courbet | 2cb97b9 | 2018-06-04 11:43:40 +0000 | [diff] [blame] | 131 | template <> |
Clement Courbet | 62b34fa | 2018-06-06 09:42:36 +0000 | [diff] [blame] | 132 | struct ScalarEnumerationTraits<exegesis::InstructionBenchmark::ModeE> { |
Clement Courbet | 2cb97b9 | 2018-06-04 11:43:40 +0000 | [diff] [blame] | 133 | static void enumeration(IO &Io, |
Clement Courbet | 62b34fa | 2018-06-06 09:42:36 +0000 | [diff] [blame] | 134 | exegesis::InstructionBenchmark::ModeE &Value) { |
| 135 | Io.enumCase(Value, "", exegesis::InstructionBenchmark::Unknown); |
| 136 | Io.enumCase(Value, "latency", exegesis::InstructionBenchmark::Latency); |
| 137 | Io.enumCase(Value, "uops", exegesis::InstructionBenchmark::Uops); |
Clement Courbet | 2cb97b9 | 2018-06-04 11:43:40 +0000 | [diff] [blame] | 138 | } |
| 139 | }; |
| 140 | |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 141 | template <> struct MappingTraits<exegesis::InstructionBenchmarkKey> { |
| 142 | static void mapping(IO &Io, exegesis::InstructionBenchmarkKey &Obj) { |
| 143 | Io.mapRequired("opcode_name", Obj.OpcodeName); |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 144 | Io.mapOptional("instructions", Obj.Instructions); |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 145 | Io.mapOptional("config", Obj.Config); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 146 | } |
| 147 | }; |
| 148 | |
| 149 | template <> struct MappingTraits<exegesis::InstructionBenchmark> { |
| 150 | static void mapping(IO &Io, exegesis::InstructionBenchmark &Obj) { |
Clement Courbet | 62b34fa | 2018-06-06 09:42:36 +0000 | [diff] [blame] | 151 | Io.mapRequired("mode", Obj.Mode); |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 152 | Io.mapRequired("key", Obj.Key); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 153 | Io.mapRequired("cpu_name", Obj.CpuName); |
| 154 | Io.mapRequired("llvm_triple", Obj.LLVMTriple); |
| 155 | Io.mapRequired("num_repetitions", Obj.NumRepetitions); |
| 156 | Io.mapRequired("measurements", Obj.Measurements); |
| 157 | Io.mapRequired("error", Obj.Error); |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 158 | Io.mapOptional("info", Obj.Info); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 159 | } |
| 160 | }; |
| 161 | |
| 162 | } // namespace yaml |
| 163 | } // namespace llvm |
| 164 | |
Clement Courbet | 7b7c27a | 2018-05-14 09:01:22 +0000 | [diff] [blame] | 165 | LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(exegesis::InstructionBenchmark) |
| 166 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 167 | namespace exegesis { |
| 168 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 169 | void BenchmarkResultContext::addRegEntry(unsigned RegNo, llvm::StringRef Name) { |
| 170 | assert(RegNoToName.find(RegNo) == RegNoToName.end()); |
| 171 | assert(RegNameToNo.find(Name) == RegNameToNo.end()); |
| 172 | RegNoToName[RegNo] = Name; |
| 173 | RegNameToNo[Name] = RegNo; |
| 174 | } |
| 175 | |
| 176 | llvm::StringRef BenchmarkResultContext::getRegName(unsigned RegNo) const { |
| 177 | const auto Itr = RegNoToName.find(RegNo); |
| 178 | if (Itr != RegNoToName.end()) |
| 179 | return Itr->second; |
| 180 | return {}; |
| 181 | } |
| 182 | |
| 183 | unsigned BenchmarkResultContext::getRegNo(llvm::StringRef Name) const { |
| 184 | const auto Itr = RegNameToNo.find(Name); |
| 185 | if (Itr != RegNameToNo.end()) |
| 186 | return Itr->second; |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | void BenchmarkResultContext::addInstrEntry(unsigned Opcode, |
| 191 | llvm::StringRef Name) { |
| 192 | assert(InstrOpcodeToName.find(Opcode) == InstrOpcodeToName.end()); |
| 193 | assert(InstrNameToOpcode.find(Name) == InstrNameToOpcode.end()); |
| 194 | InstrOpcodeToName[Opcode] = Name; |
| 195 | InstrNameToOpcode[Name] = Opcode; |
| 196 | } |
| 197 | |
| 198 | llvm::StringRef BenchmarkResultContext::getInstrName(unsigned Opcode) const { |
| 199 | const auto Itr = InstrOpcodeToName.find(Opcode); |
| 200 | if (Itr != InstrOpcodeToName.end()) |
| 201 | return Itr->second; |
| 202 | return {}; |
| 203 | } |
| 204 | |
| 205 | unsigned BenchmarkResultContext::getInstrOpcode(llvm::StringRef Name) const { |
| 206 | const auto Itr = InstrNameToOpcode.find(Name); |
| 207 | if (Itr != InstrNameToOpcode.end()) |
| 208 | return Itr->second; |
| 209 | return 0; |
| 210 | } |
| 211 | |
Clement Courbet | 7b7c27a | 2018-05-14 09:01:22 +0000 | [diff] [blame] | 212 | template <typename ObjectOrList> |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 213 | static ObjectOrList readYamlOrDieCommon(const BenchmarkResultContext &Context, |
| 214 | llvm::StringRef Filename) { |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 215 | std::unique_ptr<llvm::MemoryBuffer> MemBuffer = llvm::cantFail( |
| 216 | llvm::errorOrToExpected(llvm::MemoryBuffer::getFile(Filename))); |
Guillaume Chatelet | 083a0c16 | 2018-06-07 07:40:40 +0000 | [diff] [blame] | 217 | llvm::yaml::Input Yin(*MemBuffer, getUntypedContext(Context)); |
Clement Courbet | 7b7c27a | 2018-05-14 09:01:22 +0000 | [diff] [blame] | 218 | ObjectOrList Benchmark; |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 219 | Yin >> Benchmark; |
| 220 | return Benchmark; |
| 221 | } |
| 222 | |
Clement Courbet | 7b7c27a | 2018-05-14 09:01:22 +0000 | [diff] [blame] | 223 | InstructionBenchmark |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 224 | InstructionBenchmark::readYamlOrDie(const BenchmarkResultContext &Context, |
| 225 | llvm::StringRef Filename) { |
| 226 | return readYamlOrDieCommon<InstructionBenchmark>(Context, Filename); |
Clement Courbet | 7b7c27a | 2018-05-14 09:01:22 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | std::vector<InstructionBenchmark> |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 230 | InstructionBenchmark::readYamlsOrDie(const BenchmarkResultContext &Context, |
| 231 | llvm::StringRef Filename) { |
| 232 | return readYamlOrDieCommon<std::vector<InstructionBenchmark>>(Context, |
| 233 | Filename); |
Clement Courbet | 7b7c27a | 2018-05-14 09:01:22 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 236 | void InstructionBenchmark::writeYamlTo(const BenchmarkResultContext &Context, |
Guillaume Chatelet | 083a0c16 | 2018-06-07 07:40:40 +0000 | [diff] [blame] | 237 | llvm::raw_ostream &OS) { |
| 238 | llvm::yaml::Output Yout(OS, getUntypedContext(Context)); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 239 | Yout << *this; |
| 240 | } |
| 241 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 242 | void InstructionBenchmark::readYamlFrom(const BenchmarkResultContext &Context, |
| 243 | llvm::StringRef InputContent) { |
Guillaume Chatelet | 083a0c16 | 2018-06-07 07:40:40 +0000 | [diff] [blame] | 244 | llvm::yaml::Input Yin(InputContent, getUntypedContext(Context)); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 245 | Yin >> *this; |
| 246 | } |
| 247 | |
| 248 | // FIXME: Change the API to let the caller handle errors. |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 249 | void InstructionBenchmark::writeYamlOrDie(const BenchmarkResultContext &Context, |
| 250 | const llvm::StringRef Filename) { |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 251 | if (Filename == "-") { |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 252 | writeYamlTo(Context, llvm::outs()); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 253 | } else { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 254 | int ResultFD = 0; |
| 255 | llvm::cantFail(llvm::errorCodeToError( |
| 256 | openFileForWrite(Filename, ResultFD, llvm::sys::fs::F_Text))); |
| 257 | llvm::raw_fd_ostream Ostr(ResultFD, true /*shouldClose*/); |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 258 | writeYamlTo(Context, Ostr); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Clement Courbet | ae8ae5dc | 2018-05-24 12:41:02 +0000 | [diff] [blame] | 262 | void BenchmarkMeasureStats::push(const BenchmarkMeasure &BM) { |
| 263 | if (Key.empty()) |
| 264 | Key = BM.Key; |
| 265 | assert(Key == BM.Key); |
| 266 | ++NumValues; |
| 267 | SumValues += BM.Value; |
| 268 | MaxValue = std::max(MaxValue, BM.Value); |
| 269 | MinValue = std::min(MinValue, BM.Value); |
| 270 | } |
| 271 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 272 | } // namespace exegesis |