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