Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 1 | //===-- BenchmarkResult.cpp -------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [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 |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "BenchmarkResult.h" |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 10 | #include "BenchmarkRunner.h" |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
Roman Lebedev | 5b94fe9 | 2019-02-04 09:12:21 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/StringMap.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/StringRef.h" |
Roman Lebedev | 5b94fe9 | 2019-02-04 09:12:21 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/bit.h" |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 15 | #include "llvm/ObjectYAML/YAML.h" |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 16 | #include "llvm/Support/FileOutputBuffer.h" |
| 17 | #include "llvm/Support/FileSystem.h" |
| 18 | #include "llvm/Support/Format.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | |
Guillaume Chatelet | 9157bc9 | 2018-10-04 12:33:46 +0000 | [diff] [blame] | 21 | static constexpr const char kIntegerPrefix[] = "i_0x"; |
| 22 | static constexpr const char kDoublePrefix[] = "f_"; |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 23 | static constexpr const char kInvalidOperand[] = "INVALID"; |
Guillaume Chatelet | 083a0c16 | 2018-06-07 07:40:40 +0000 | [diff] [blame] | 24 | |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 25 | namespace llvm { |
| 26 | |
| 27 | namespace { |
| 28 | |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 29 | // A mutable struct holding an LLVMState that can be passed through the |
| 30 | // serialization process to encode/decode registers and instructions. |
| 31 | struct YamlContext { |
| 32 | YamlContext(const exegesis::LLVMState &State) |
Roman Lebedev | 5b94fe9 | 2019-02-04 09:12:21 +0000 | [diff] [blame] | 33 | : State(&State), ErrorStream(LastError), |
| 34 | OpcodeNameToOpcodeIdx( |
Roman Lebedev | bd84b13 | 2019-02-04 09:12:25 +0000 | [diff] [blame] | 35 | generateOpcodeNameToOpcodeIdxMapping(State.getInstrInfo())), |
| 36 | RegNameToRegNo(generateRegNameToRegNoMapping(State.getRegInfo())) {} |
Roman Lebedev | 5b94fe9 | 2019-02-04 09:12:21 +0000 | [diff] [blame] | 37 | |
| 38 | static llvm::StringMap<unsigned> |
| 39 | generateOpcodeNameToOpcodeIdxMapping(const llvm::MCInstrInfo &InstrInfo) { |
| 40 | llvm::StringMap<unsigned> Map(InstrInfo.getNumOpcodes()); |
| 41 | for (unsigned I = 0, E = InstrInfo.getNumOpcodes(); I < E; ++I) |
| 42 | Map[InstrInfo.getName(I)] = I; |
| 43 | assert(Map.size() == InstrInfo.getNumOpcodes() && "Size prediction failed"); |
| 44 | return Map; |
| 45 | }; |
Guillaume Chatelet | 083a0c16 | 2018-06-07 07:40:40 +0000 | [diff] [blame] | 46 | |
Roman Lebedev | bd84b13 | 2019-02-04 09:12:25 +0000 | [diff] [blame] | 47 | llvm::StringMap<unsigned> |
| 48 | generateRegNameToRegNoMapping(const llvm::MCRegisterInfo &RegInfo) { |
| 49 | llvm::StringMap<unsigned> Map(RegInfo.getNumRegs()); |
| 50 | for (unsigned I = 0, E = RegInfo.getNumRegs(); I < E; ++I) |
| 51 | Map[RegInfo.getName(I)] = I; |
| 52 | assert(Map.size() == RegInfo.getNumRegs() && "Size prediction failed"); |
| 53 | return Map; |
| 54 | }; |
| 55 | |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 56 | void serializeMCInst(const llvm::MCInst &MCInst, llvm::raw_ostream &OS) { |
| 57 | OS << getInstrName(MCInst.getOpcode()); |
| 58 | for (const auto &Op : MCInst) { |
| 59 | OS << ' '; |
| 60 | serializeMCOperand(Op, OS); |
Guillaume Chatelet | 083a0c16 | 2018-06-07 07:40:40 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
Guillaume Chatelet | 083a0c16 | 2018-06-07 07:40:40 +0000 | [diff] [blame] | 63 | |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 64 | void deserializeMCInst(llvm::StringRef String, llvm::MCInst &Value) { |
Roman Lebedev | dc78bc2 | 2019-02-04 09:12:13 +0000 | [diff] [blame] | 65 | llvm::SmallVector<llvm::StringRef, 16> Pieces; |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 66 | String.split(Pieces, " ", /* MaxSplit */ -1, /* KeepEmpty */ false); |
| 67 | if (Pieces.empty()) { |
| 68 | ErrorStream << "Unknown Instruction: '" << String << "'"; |
| 69 | return; |
| 70 | } |
| 71 | bool ProcessOpcode = true; |
| 72 | for (llvm::StringRef Piece : Pieces) { |
| 73 | if (ProcessOpcode) |
| 74 | Value.setOpcode(getInstrOpcode(Piece)); |
| 75 | else |
| 76 | Value.addOperand(deserializeMCOperand(Piece)); |
| 77 | ProcessOpcode = false; |
| 78 | } |
| 79 | } |
Guillaume Chatelet | 083a0c16 | 2018-06-07 07:40:40 +0000 | [diff] [blame] | 80 | |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 81 | std::string &getLastError() { return ErrorStream.str(); } |
| 82 | |
Guillaume Chatelet | 345fae5 | 2018-09-25 15:15:54 +0000 | [diff] [blame] | 83 | llvm::raw_string_ostream &getErrorStream() { return ErrorStream; } |
| 84 | |
| 85 | llvm::StringRef getRegName(unsigned RegNo) { |
| 86 | const llvm::StringRef RegName = State->getRegInfo().getName(RegNo); |
| 87 | if (RegName.empty()) |
| 88 | ErrorStream << "No register with enum value" << RegNo; |
| 89 | return RegName; |
| 90 | } |
| 91 | |
| 92 | unsigned getRegNo(llvm::StringRef RegName) { |
Roman Lebedev | bd84b13 | 2019-02-04 09:12:25 +0000 | [diff] [blame] | 93 | auto Iter = RegNameToRegNo.find(RegName); |
| 94 | if (Iter != RegNameToRegNo.end()) |
| 95 | return Iter->second; |
Guillaume Chatelet | 345fae5 | 2018-09-25 15:15:54 +0000 | [diff] [blame] | 96 | ErrorStream << "No register with name " << RegName; |
| 97 | return 0; |
| 98 | } |
| 99 | |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 100 | private: |
Guillaume Chatelet | 9157bc9 | 2018-10-04 12:33:46 +0000 | [diff] [blame] | 101 | void serializeIntegerOperand(llvm::raw_ostream &OS, int64_t Value) { |
| 102 | OS << kIntegerPrefix; |
| 103 | OS.write_hex(llvm::bit_cast<uint64_t>(Value)); |
| 104 | } |
| 105 | |
| 106 | bool tryDeserializeIntegerOperand(llvm::StringRef String, int64_t &Value) { |
| 107 | if (!String.consume_front(kIntegerPrefix)) |
| 108 | return false; |
| 109 | return !String.consumeInteger(16, Value); |
| 110 | } |
| 111 | |
| 112 | void serializeFPOperand(llvm::raw_ostream &OS, double Value) { |
| 113 | OS << kDoublePrefix << llvm::format("%la", Value); |
| 114 | } |
| 115 | |
| 116 | bool tryDeserializeFPOperand(llvm::StringRef String, double &Value) { |
| 117 | if (!String.consume_front(kDoublePrefix)) |
| 118 | return false; |
| 119 | char *EndPointer = nullptr; |
| 120 | Value = strtod(String.begin(), &EndPointer); |
| 121 | return EndPointer == String.end(); |
| 122 | } |
| 123 | |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 124 | void serializeMCOperand(const llvm::MCOperand &MCOperand, |
| 125 | llvm::raw_ostream &OS) { |
| 126 | if (MCOperand.isReg()) { |
| 127 | OS << getRegName(MCOperand.getReg()); |
| 128 | } else if (MCOperand.isImm()) { |
Guillaume Chatelet | 9157bc9 | 2018-10-04 12:33:46 +0000 | [diff] [blame] | 129 | serializeIntegerOperand(OS, MCOperand.getImm()); |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 130 | } else if (MCOperand.isFPImm()) { |
Guillaume Chatelet | 9157bc9 | 2018-10-04 12:33:46 +0000 | [diff] [blame] | 131 | serializeFPOperand(OS, MCOperand.getFPImm()); |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 132 | } else { |
| 133 | OS << kInvalidOperand; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | llvm::MCOperand deserializeMCOperand(llvm::StringRef String) { |
| 138 | assert(!String.empty()); |
| 139 | int64_t IntValue = 0; |
| 140 | double DoubleValue = 0; |
Guillaume Chatelet | 9157bc9 | 2018-10-04 12:33:46 +0000 | [diff] [blame] | 141 | if (tryDeserializeIntegerOperand(String, IntValue)) |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 142 | return llvm::MCOperand::createImm(IntValue); |
Guillaume Chatelet | 9157bc9 | 2018-10-04 12:33:46 +0000 | [diff] [blame] | 143 | if (tryDeserializeFPOperand(String, DoubleValue)) |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 144 | return llvm::MCOperand::createFPImm(DoubleValue); |
| 145 | if (unsigned RegNo = getRegNo(String)) |
| 146 | return llvm::MCOperand::createReg(RegNo); |
| 147 | if (String != kInvalidOperand) |
| 148 | ErrorStream << "Unknown Operand: '" << String << "'"; |
| 149 | return {}; |
| 150 | } |
| 151 | |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 152 | llvm::StringRef getInstrName(unsigned InstrNo) { |
| 153 | const llvm::StringRef InstrName = State->getInstrInfo().getName(InstrNo); |
| 154 | if (InstrName.empty()) |
| 155 | ErrorStream << "No opcode with enum value" << InstrNo; |
| 156 | return InstrName; |
| 157 | } |
| 158 | |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 159 | unsigned getInstrOpcode(llvm::StringRef InstrName) { |
Roman Lebedev | 5b94fe9 | 2019-02-04 09:12:21 +0000 | [diff] [blame] | 160 | auto Iter = OpcodeNameToOpcodeIdx.find(InstrName); |
| 161 | if (Iter != OpcodeNameToOpcodeIdx.end()) |
| 162 | return Iter->second; |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 163 | ErrorStream << "No opcode with name " << InstrName; |
| 164 | return 0; |
| 165 | } |
| 166 | |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 167 | const llvm::exegesis::LLVMState *State; |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 168 | std::string LastError; |
| 169 | llvm::raw_string_ostream ErrorStream; |
Roman Lebedev | 5b94fe9 | 2019-02-04 09:12:21 +0000 | [diff] [blame] | 170 | const llvm::StringMap<unsigned> OpcodeNameToOpcodeIdx; |
Roman Lebedev | bd84b13 | 2019-02-04 09:12:25 +0000 | [diff] [blame] | 171 | const llvm::StringMap<unsigned> RegNameToRegNo; |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 172 | }; |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 173 | } // namespace |
Guillaume Chatelet | 083a0c16 | 2018-06-07 07:40:40 +0000 | [diff] [blame] | 174 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 175 | // Defining YAML traits for IO. |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 176 | namespace yaml { |
| 177 | |
Guillaume Chatelet | 345fae5 | 2018-09-25 15:15:54 +0000 | [diff] [blame] | 178 | static YamlContext &getTypedContext(void *Ctx) { |
| 179 | return *reinterpret_cast<YamlContext *>(Ctx); |
| 180 | } |
| 181 | |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 182 | // std::vector<llvm::MCInst> will be rendered as a list. |
| 183 | template <> struct SequenceElementTraits<llvm::MCInst> { |
| 184 | static const bool flow = false; |
| 185 | }; |
| 186 | |
| 187 | template <> struct ScalarTraits<llvm::MCInst> { |
| 188 | |
| 189 | static void output(const llvm::MCInst &Value, void *Ctx, |
| 190 | llvm::raw_ostream &Out) { |
Guillaume Chatelet | 345fae5 | 2018-09-25 15:15:54 +0000 | [diff] [blame] | 191 | getTypedContext(Ctx).serializeMCInst(Value, Out); |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static StringRef input(StringRef Scalar, void *Ctx, llvm::MCInst &Value) { |
Guillaume Chatelet | 345fae5 | 2018-09-25 15:15:54 +0000 | [diff] [blame] | 195 | YamlContext &Context = getTypedContext(Ctx); |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 196 | Context.deserializeMCInst(Scalar, Value); |
| 197 | return Context.getLastError(); |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Guillaume Chatelet | 345fae5 | 2018-09-25 15:15:54 +0000 | [diff] [blame] | 200 | // By default strings are quoted only when necessary. |
| 201 | // We force the use of single quotes for uniformity. |
Clement Courbet | 53d35d2 | 2018-06-05 10:56:19 +0000 | [diff] [blame] | 202 | static QuotingType mustQuote(StringRef) { return QuotingType::Single; } |
| 203 | |
| 204 | static const bool flow = true; |
| 205 | }; |
| 206 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 207 | // std::vector<exegesis::Measure> will be rendered as a list. |
| 208 | template <> struct SequenceElementTraits<exegesis::BenchmarkMeasure> { |
| 209 | static const bool flow = false; |
| 210 | }; |
| 211 | |
| 212 | // exegesis::Measure is rendererd as a flow instead of a list. |
| 213 | // e.g. { "key": "the key", "value": 0123 } |
| 214 | template <> struct MappingTraits<exegesis::BenchmarkMeasure> { |
| 215 | static void mapping(IO &Io, exegesis::BenchmarkMeasure &Obj) { |
Clement Courbet | 28d4f85 | 2018-09-26 13:35:10 +0000 | [diff] [blame] | 216 | Io.mapRequired("key", Obj.Key); |
| 217 | if (!Io.outputting()) { |
| 218 | // For backward compatibility, interpret debug_string as a key. |
| 219 | Io.mapOptional("debug_string", Obj.Key); |
| 220 | } |
Clement Courbet | 684a5f6 | 2018-09-26 08:37:21 +0000 | [diff] [blame] | 221 | Io.mapRequired("value", Obj.PerInstructionValue); |
| 222 | Io.mapOptional("per_snippet_value", Obj.PerSnippetValue); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 223 | } |
| 224 | static const bool flow = true; |
| 225 | }; |
| 226 | |
Clement Courbet | 2cb97b9 | 2018-06-04 11:43:40 +0000 | [diff] [blame] | 227 | template <> |
Clement Courbet | 62b34fa | 2018-06-06 09:42:36 +0000 | [diff] [blame] | 228 | struct ScalarEnumerationTraits<exegesis::InstructionBenchmark::ModeE> { |
Clement Courbet | 2cb97b9 | 2018-06-04 11:43:40 +0000 | [diff] [blame] | 229 | static void enumeration(IO &Io, |
Clement Courbet | 62b34fa | 2018-06-06 09:42:36 +0000 | [diff] [blame] | 230 | exegesis::InstructionBenchmark::ModeE &Value) { |
| 231 | Io.enumCase(Value, "", exegesis::InstructionBenchmark::Unknown); |
| 232 | Io.enumCase(Value, "latency", exegesis::InstructionBenchmark::Latency); |
| 233 | Io.enumCase(Value, "uops", exegesis::InstructionBenchmark::Uops); |
Clement Courbet | 362653f | 2019-01-30 16:02:20 +0000 | [diff] [blame] | 234 | Io.enumCase(Value, "inverse_throughput", |
| 235 | exegesis::InstructionBenchmark::InverseThroughput); |
Clement Courbet | 2cb97b9 | 2018-06-04 11:43:40 +0000 | [diff] [blame] | 236 | } |
| 237 | }; |
| 238 | |
Guillaume Chatelet | 345fae5 | 2018-09-25 15:15:54 +0000 | [diff] [blame] | 239 | // std::vector<exegesis::RegisterValue> will be rendered as a list. |
| 240 | template <> struct SequenceElementTraits<exegesis::RegisterValue> { |
| 241 | static const bool flow = false; |
| 242 | }; |
| 243 | |
| 244 | template <> struct ScalarTraits<exegesis::RegisterValue> { |
| 245 | static constexpr const unsigned kRadix = 16; |
| 246 | static constexpr const bool kSigned = false; |
| 247 | |
| 248 | static void output(const exegesis::RegisterValue &RV, void *Ctx, |
| 249 | llvm::raw_ostream &Out) { |
| 250 | YamlContext &Context = getTypedContext(Ctx); |
| 251 | Out << Context.getRegName(RV.Register) << "=0x" |
| 252 | << RV.Value.toString(kRadix, kSigned); |
| 253 | } |
| 254 | |
| 255 | static StringRef input(StringRef String, void *Ctx, |
| 256 | exegesis::RegisterValue &RV) { |
| 257 | llvm::SmallVector<llvm::StringRef, 2> Pieces; |
| 258 | String.split(Pieces, "=0x", /* MaxSplit */ -1, |
| 259 | /* KeepEmpty */ false); |
| 260 | YamlContext &Context = getTypedContext(Ctx); |
| 261 | if (Pieces.size() == 2) { |
| 262 | RV.Register = Context.getRegNo(Pieces[0]); |
| 263 | const unsigned BitsNeeded = llvm::APInt::getBitsNeeded(Pieces[1], kRadix); |
| 264 | RV.Value = llvm::APInt(BitsNeeded, Pieces[1], kRadix); |
| 265 | } else { |
| 266 | Context.getErrorStream() |
| 267 | << "Unknown initial register value: '" << String << "'"; |
| 268 | } |
| 269 | return Context.getLastError(); |
| 270 | } |
| 271 | |
| 272 | static QuotingType mustQuote(StringRef) { return QuotingType::Single; } |
| 273 | |
| 274 | static const bool flow = true; |
| 275 | }; |
| 276 | |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 277 | template <> |
| 278 | struct MappingContextTraits<exegesis::InstructionBenchmarkKey, YamlContext> { |
| 279 | static void mapping(IO &Io, exegesis::InstructionBenchmarkKey &Obj, |
| 280 | YamlContext &Context) { |
| 281 | Io.setContext(&Context); |
Clement Courbet | 49fad1c | 2018-06-14 06:57:52 +0000 | [diff] [blame] | 282 | Io.mapRequired("instructions", Obj.Instructions); |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 283 | Io.mapOptional("config", Obj.Config); |
Guillaume Chatelet | 345fae5 | 2018-09-25 15:15:54 +0000 | [diff] [blame] | 284 | Io.mapRequired("register_initial_values", Obj.RegisterInitialValues); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 285 | } |
| 286 | }; |
| 287 | |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 288 | template <> |
| 289 | struct MappingContextTraits<exegesis::InstructionBenchmark, YamlContext> { |
Guillaume Chatelet | 345fae5 | 2018-09-25 15:15:54 +0000 | [diff] [blame] | 290 | struct NormalizedBinary { |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 291 | NormalizedBinary(IO &io) {} |
| 292 | NormalizedBinary(IO &, std::vector<uint8_t> &Data) : Binary(Data) {} |
| 293 | std::vector<uint8_t> denormalize(IO &) { |
| 294 | std::vector<uint8_t> Data; |
| 295 | std::string Str; |
| 296 | raw_string_ostream OSS(Str); |
| 297 | Binary.writeAsBinary(OSS); |
| 298 | OSS.flush(); |
| 299 | Data.assign(Str.begin(), Str.end()); |
| 300 | return Data; |
| 301 | } |
| 302 | |
| 303 | BinaryRef Binary; |
| 304 | }; |
| 305 | |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 306 | static void mapping(IO &Io, exegesis::InstructionBenchmark &Obj, |
| 307 | YamlContext &Context) { |
Clement Courbet | 62b34fa | 2018-06-06 09:42:36 +0000 | [diff] [blame] | 308 | Io.mapRequired("mode", Obj.Mode); |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 309 | Io.mapRequired("key", Obj.Key, Context); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 310 | Io.mapRequired("cpu_name", Obj.CpuName); |
| 311 | Io.mapRequired("llvm_triple", Obj.LLVMTriple); |
| 312 | Io.mapRequired("num_repetitions", Obj.NumRepetitions); |
| 313 | Io.mapRequired("measurements", Obj.Measurements); |
| 314 | Io.mapRequired("error", Obj.Error); |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 315 | Io.mapOptional("info", Obj.Info); |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 316 | // AssembledSnippet |
| 317 | MappingNormalization<NormalizedBinary, std::vector<uint8_t>> BinaryString( |
| 318 | Io, Obj.AssembledSnippet); |
| 319 | Io.mapOptional("assembled_snippet", BinaryString->Binary); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 320 | } |
| 321 | }; |
| 322 | |
| 323 | } // namespace yaml |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 324 | |
| 325 | namespace exegesis { |
| 326 | |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 327 | llvm::Expected<InstructionBenchmark> |
| 328 | InstructionBenchmark::readYaml(const LLVMState &State, |
| 329 | llvm::StringRef Filename) { |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 330 | if (auto ExpectedMemoryBuffer = |
| 331 | llvm::errorOrToExpected(llvm::MemoryBuffer::getFile(Filename))) { |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 332 | llvm::yaml::Input Yin(*ExpectedMemoryBuffer.get()); |
| 333 | YamlContext Context(State); |
| 334 | InstructionBenchmark Benchmark; |
| 335 | if (Yin.setCurrentDocument()) |
| 336 | llvm::yaml::yamlize(Yin, Benchmark, /*unused*/ true, Context); |
| 337 | if (!Context.getLastError().empty()) |
| 338 | return llvm::make_error<BenchmarkFailure>(Context.getLastError()); |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 339 | return Benchmark; |
| 340 | } else { |
| 341 | return ExpectedMemoryBuffer.takeError(); |
| 342 | } |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 345 | llvm::Expected<std::vector<InstructionBenchmark>> |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 346 | InstructionBenchmark::readYamls(const LLVMState &State, |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 347 | llvm::StringRef Filename) { |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 348 | if (auto ExpectedMemoryBuffer = |
| 349 | llvm::errorOrToExpected(llvm::MemoryBuffer::getFile(Filename))) { |
| 350 | llvm::yaml::Input Yin(*ExpectedMemoryBuffer.get()); |
| 351 | YamlContext Context(State); |
| 352 | std::vector<InstructionBenchmark> Benchmarks; |
| 353 | while (Yin.setCurrentDocument()) { |
| 354 | Benchmarks.emplace_back(); |
| 355 | yamlize(Yin, Benchmarks.back(), /*unused*/ true, Context); |
| 356 | if (Yin.error()) |
| 357 | return llvm::errorCodeToError(Yin.error()); |
| 358 | if (!Context.getLastError().empty()) |
| 359 | return llvm::make_error<BenchmarkFailure>(Context.getLastError()); |
| 360 | Yin.nextDocument(); |
| 361 | } |
| 362 | return Benchmarks; |
| 363 | } else { |
| 364 | return ExpectedMemoryBuffer.takeError(); |
| 365 | } |
Clement Courbet | 7b7c27a | 2018-05-14 09:01:22 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Roman Lebedev | 628f1ae | 2019-04-10 12:19:57 +0000 | [diff] [blame^] | 368 | llvm::Error InstructionBenchmark::writeYamlTo(const LLVMState &State, |
| 369 | llvm::raw_ostream &OS) { |
Clement Courbet | 7066769 | 2018-11-07 15:46:45 +0000 | [diff] [blame] | 370 | llvm::yaml::Output Yout(OS, nullptr /*Ctx*/, 200 /*WrapColumn*/); |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 371 | YamlContext Context(State); |
Guillaume Chatelet | 6078f82 | 2018-09-25 14:48:24 +0000 | [diff] [blame] | 372 | Yout.beginDocuments(); |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 373 | llvm::yaml::yamlize(Yout, *this, /*unused*/ true, Context); |
Roman Lebedev | 628f1ae | 2019-04-10 12:19:57 +0000 | [diff] [blame^] | 374 | if (!Context.getLastError().empty()) |
| 375 | return llvm::make_error<BenchmarkFailure>(Context.getLastError()); |
Guillaume Chatelet | 6078f82 | 2018-09-25 14:48:24 +0000 | [diff] [blame] | 376 | Yout.endDocuments(); |
Roman Lebedev | 628f1ae | 2019-04-10 12:19:57 +0000 | [diff] [blame^] | 377 | return Error::success(); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Roman Lebedev | 628f1ae | 2019-04-10 12:19:57 +0000 | [diff] [blame^] | 380 | llvm::Error InstructionBenchmark::readYamlFrom(const LLVMState &State, |
| 381 | llvm::StringRef InputContent) { |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 382 | llvm::yaml::Input Yin(InputContent); |
| 383 | YamlContext Context(State); |
| 384 | if (Yin.setCurrentDocument()) |
| 385 | llvm::yaml::yamlize(Yin, *this, /*unused*/ true, Context); |
Roman Lebedev | 628f1ae | 2019-04-10 12:19:57 +0000 | [diff] [blame^] | 386 | if (!Context.getLastError().empty()) |
| 387 | return llvm::make_error<BenchmarkFailure>(Context.getLastError()); |
| 388 | return Error::success(); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Guillaume Chatelet | 55ad087 | 2018-09-25 12:18:08 +0000 | [diff] [blame] | 391 | llvm::Error InstructionBenchmark::writeYaml(const LLVMState &State, |
| 392 | const llvm::StringRef Filename) { |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 393 | if (Filename == "-") { |
Roman Lebedev | 628f1ae | 2019-04-10 12:19:57 +0000 | [diff] [blame^] | 394 | if (auto Err = writeYamlTo(State, llvm::outs())) |
| 395 | return std::move(Err); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 396 | } else { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 397 | int ResultFD = 0; |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 398 | if (auto E = llvm::errorCodeToError( |
Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 399 | openFileForWrite(Filename, ResultFD, llvm::sys::fs::CD_CreateAlways, |
| 400 | llvm::sys::fs::F_Text))) { |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 401 | return E; |
| 402 | } |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 403 | llvm::raw_fd_ostream Ostr(ResultFD, true /*shouldClose*/); |
Roman Lebedev | 628f1ae | 2019-04-10 12:19:57 +0000 | [diff] [blame^] | 404 | if (auto Err = writeYamlTo(State, Ostr)) |
| 405 | return std::move(Err); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 406 | } |
Guillaume Chatelet | 8c91d4c | 2018-06-07 07:51:16 +0000 | [diff] [blame] | 407 | return llvm::Error::success(); |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Clement Courbet | 684a5f6 | 2018-09-26 08:37:21 +0000 | [diff] [blame] | 410 | void PerInstructionStats::push(const BenchmarkMeasure &BM) { |
Clement Courbet | ae8ae5dc | 2018-05-24 12:41:02 +0000 | [diff] [blame] | 411 | if (Key.empty()) |
| 412 | Key = BM.Key; |
| 413 | assert(Key == BM.Key); |
| 414 | ++NumValues; |
Clement Courbet | 684a5f6 | 2018-09-26 08:37:21 +0000 | [diff] [blame] | 415 | SumValues += BM.PerInstructionValue; |
| 416 | MaxValue = std::max(MaxValue, BM.PerInstructionValue); |
| 417 | MinValue = std::min(MinValue, BM.PerInstructionValue); |
Clement Courbet | ae8ae5dc | 2018-05-24 12:41:02 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Clement Courbet | ac74acd | 2018-04-04 11:37:06 +0000 | [diff] [blame] | 420 | } // namespace exegesis |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 421 | } // namespace llvm |