1//===-- BenchmarkResult.cpp -------------------------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "BenchmarkResult.h"
10#include "Error.h"
11#include "ValidationEvent.h"
12#include "llvm/ADT/ScopeExit.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/ADT/bit.h"
15#include "llvm/ObjectYAML/YAML.h"
16#include "llvm/Support/Error.h"
17#include "llvm/Support/Format.h"
18#include "llvm/Support/MathExtras.h"
19#include "llvm/Support/WithColor.h"
20#include "llvm/Support/raw_ostream.h"
21
22static constexpr char kIntegerPrefix[] = "i_0x";
23static constexpr char kDoublePrefix[] = "f_";
24static constexpr char kInvalidOperand[] = "INVALID";
25
26namespace llvm {
27
28namespace {
29
30// A mutable struct holding an LLVMState that can be passed through the
31// serialization process to encode/decode registers and instructions.
32struct YamlContext {
33 YamlContext(const exegesis::LLVMState &State)
34 : State(&State), ErrorStream(LastError),
35 OpcodeNameToOpcodeIdx(State.getOpcodeNameToOpcodeIdxMapping()) {}
36
37 void serializeMCInst(const MCInst &MCInst, raw_ostream &OS) {
38 OS << getInstrName(InstrNo: MCInst.getOpcode());
39 for (const auto &Op : MCInst) {
40 OS << ' ';
41 serializeMCOperand(MCOperand: Op, OS);
42 }
43 }
44
45 void deserializeMCInst(StringRef String, MCInst &Value) {
46 SmallVector<StringRef, 16> Pieces;
47 String.split(A&: Pieces, Separator: " ", /* MaxSplit */ -1, /* KeepEmpty */ false);
48 if (Pieces.empty()) {
49 ErrorStream << "Unknown Instruction: '" << String << "'\n";
50 return;
51 }
52 bool ProcessOpcode = true;
53 for (StringRef Piece : Pieces) {
54 if (ProcessOpcode)
55 Value.setOpcode(getInstrOpcode(InstrName: Piece));
56 else
57 Value.addOperand(Op: deserializeMCOperand(String: Piece));
58 ProcessOpcode = false;
59 }
60 }
61
62 std::string &getLastError() { return ErrorStream.str(); }
63
64 // Returns the recorded error to report to the YAML parser from a
65 // ScalarTraits::input. When ContinueOnError is set, recoverable per-entry
66 // errors (e.g. an unknown opcode in a bitrotted sample) are left recorded for
67 // the caller to inspect but are not reported to the parser, which would
68 // otherwise abort parsing of every remaining document.
69 StringRef getInputError() {
70 return ContinueOnError ? StringRef() : StringRef(getLastError());
71 }
72
73 raw_string_ostream &getErrorStream() { return ErrorStream; }
74
75 // When set, ScalarTraits::input swallows recoverable deserialization errors
76 // (see getInputError) so that readYamls can skip the offending entry instead
77 // of aborting the whole file.
78 bool ContinueOnError = false;
79
80 StringRef getRegName(MCRegister Reg) {
81 // Special case: Reg may be invalid. We have to deal with it explicitly.
82 if (!Reg.isValid())
83 return kNoRegister;
84 const StringRef RegName = State->getRegInfo().getName(RegNo: Reg);
85 if (RegName.empty())
86 ErrorStream << "No register with enum value '" << Reg.id() << "'\n";
87 return RegName;
88 }
89
90 std::optional<MCRegister> getRegNo(StringRef RegName) {
91 std::optional<MCRegister> RegisterNumber =
92 State->getRegisterNumberFromName(RegisterName: RegName);
93 if (!RegisterNumber.has_value())
94 ErrorStream << "No register with name '" << RegName << "'\n";
95 return RegisterNumber;
96 }
97
98private:
99 void serializeIntegerOperand(raw_ostream &OS, int64_t Value) {
100 OS << kIntegerPrefix;
101 OS.write_hex(N: bit_cast<uint64_t>(from: Value));
102 }
103
104 bool tryDeserializeIntegerOperand(StringRef String, int64_t &Value) {
105 if (!String.consume_front(Prefix: kIntegerPrefix))
106 return false;
107 return !String.consumeInteger(Radix: 16, Result&: Value);
108 }
109
110 void serializeFPOperand(raw_ostream &OS, double Value) {
111 OS << kDoublePrefix << format(Fmt: "%la", Vals: Value);
112 }
113
114 bool tryDeserializeFPOperand(StringRef String, double &Value) {
115 if (!String.consume_front(Prefix: kDoublePrefix))
116 return false;
117 char *EndPointer = nullptr;
118 Value = strtod(nptr: String.begin(), endptr: &EndPointer);
119 return EndPointer == String.end();
120 }
121
122 void serializeMCOperand(const MCOperand &MCOperand, raw_ostream &OS) {
123 if (MCOperand.isReg()) {
124 OS << getRegName(Reg: MCOperand.getReg());
125 } else if (MCOperand.isImm()) {
126 serializeIntegerOperand(OS, Value: MCOperand.getImm());
127 } else if (MCOperand.isDFPImm()) {
128 serializeFPOperand(OS, Value: bit_cast<double>(from: MCOperand.getDFPImm()));
129 } else {
130 OS << kInvalidOperand;
131 }
132 }
133
134 MCOperand deserializeMCOperand(StringRef String) {
135 assert(!String.empty());
136 int64_t IntValue = 0;
137 double DoubleValue = 0;
138 if (tryDeserializeIntegerOperand(String, Value&: IntValue))
139 return MCOperand::createImm(Val: IntValue);
140 if (tryDeserializeFPOperand(String, Value&: DoubleValue))
141 return MCOperand::createDFPImm(Val: bit_cast<uint64_t>(from: DoubleValue));
142 if (auto RegNo = getRegNo(RegName: String))
143 return MCOperand::createReg(Reg: *RegNo);
144 if (String != kInvalidOperand)
145 ErrorStream << "Unknown Operand: '" << String << "'\n";
146 return {};
147 }
148
149 StringRef getInstrName(unsigned InstrNo) {
150 const StringRef InstrName = State->getInstrInfo().getName(Opcode: InstrNo);
151 if (InstrName.empty())
152 ErrorStream << "No opcode with enum value '" << InstrNo << "'\n";
153 return InstrName;
154 }
155
156 StringRef findNearestOpcodeName(StringRef InstrName) const {
157 unsigned BestDistance = 2;
158 StringRef Nearest;
159
160 for (const auto &Entry : OpcodeNameToOpcodeIdx) {
161 StringRef Candidate = Entry.getFirst();
162
163 size_t CandidateSize = Candidate.size();
164 size_t InstrSize = InstrName.size();
165 size_t AbsDiff = llvm::AbsoluteDifference(X: CandidateSize, Y: InstrSize);
166
167 if (AbsDiff >= BestDistance)
168 continue;
169
170 unsigned Distance =
171 InstrName.edit_distance(Other: Candidate, /*AllowReplacements=*/true,
172 /*MaxEditDistance=*/BestDistance - 1);
173
174 if (Distance < BestDistance) {
175 BestDistance = Distance;
176 Nearest = Candidate;
177 if (BestDistance == 1)
178 break;
179 }
180 }
181
182 return Nearest;
183 }
184
185 unsigned getInstrOpcode(StringRef InstrName) {
186 auto Iter = OpcodeNameToOpcodeIdx.find(Val: InstrName);
187 if (Iter != OpcodeNameToOpcodeIdx.end())
188 return Iter->second;
189
190 ErrorStream << "No opcode with name '" << InstrName << "'";
191 if (StringRef Nearest = findNearestOpcodeName(InstrName); !Nearest.empty())
192 ErrorStream << " - did you mean '" << Nearest << "' ?";
193 ErrorStream << "\n";
194 return 0;
195 }
196
197 const exegesis::LLVMState *State;
198 std::string LastError;
199 raw_string_ostream ErrorStream;
200 const DenseMap<StringRef, unsigned> &OpcodeNameToOpcodeIdx;
201};
202} // namespace
203
204// Defining YAML traits for IO.
205namespace yaml {
206
207static YamlContext &getTypedContext(void *Ctx) {
208 return *reinterpret_cast<YamlContext *>(Ctx);
209}
210
211// std::vector<MCInst> will be rendered as a list.
212template <> struct SequenceElementTraits<MCInst> {
213 static const bool flow = false;
214};
215
216template <> struct ScalarTraits<MCInst> {
217
218 static void output(const MCInst &Value, void *Ctx, raw_ostream &Out) {
219 getTypedContext(Ctx).serializeMCInst(MCInst: Value, OS&: Out);
220 }
221
222 static StringRef input(StringRef Scalar, void *Ctx, MCInst &Value) {
223 YamlContext &Context = getTypedContext(Ctx);
224 Context.deserializeMCInst(String: Scalar, Value);
225 return Context.getInputError();
226 }
227
228 // By default strings are quoted only when necessary.
229 // We force the use of single quotes for uniformity.
230 static QuotingType mustQuote(StringRef) { return QuotingType::Single; }
231
232 static const bool flow = true;
233};
234
235// std::vector<exegesis::Measure> will be rendered as a list.
236template <> struct SequenceElementTraits<exegesis::BenchmarkMeasure> {
237 static const bool flow = false;
238};
239
240template <>
241struct CustomMappingTraits<std::map<exegesis::ValidationEvent, int64_t>> {
242 static void inputOne(IO &Io, StringRef KeyStr,
243 std::map<exegesis::ValidationEvent, int64_t> &VI) {
244 Expected<exegesis::ValidationEvent> Key =
245 exegesis::getValidationEventByName(Name: KeyStr);
246 if (!Key) {
247 Io.setError("Key is not a valid validation event");
248 return;
249 }
250 Io.mapRequired(Key: KeyStr, Val&: VI[*Key]);
251 }
252
253 static void output(IO &Io, std::map<exegesis::ValidationEvent, int64_t> &VI) {
254 for (auto &IndividualVI : VI) {
255 Io.mapRequired(Key: exegesis::getValidationEventName(VE: IndividualVI.first),
256 Val&: IndividualVI.second);
257 }
258 }
259};
260
261// exegesis::Measure is rendererd as a flow instead of a list.
262// e.g. { "key": "the key", "value": 0123 }
263template <> struct MappingTraits<exegesis::BenchmarkMeasure> {
264 static void mapping(IO &Io, exegesis::BenchmarkMeasure &Obj) {
265 Io.mapRequired(Key: "key", Val&: Obj.Key);
266 if (!Io.outputting()) {
267 // For backward compatibility, interpret debug_string as a key.
268 Io.mapOptional(Key: "debug_string", Val&: Obj.Key);
269 }
270 Io.mapRequired(Key: "value", Val&: Obj.PerInstructionValue);
271 Io.mapOptional(Key: "per_snippet_value", Val&: Obj.PerSnippetValue);
272 Io.mapOptional(Key: "validation_counters", Val&: Obj.ValidationCounters);
273 }
274 static const bool flow = true;
275};
276
277template <> struct ScalarEnumerationTraits<exegesis::Benchmark::ModeE> {
278 static void enumeration(IO &Io, exegesis::Benchmark::ModeE &Value) {
279 Io.enumCase(Val&: Value, Str: "", ConstVal: exegesis::Benchmark::Unknown);
280 Io.enumCase(Val&: Value, Str: "latency", ConstVal: exegesis::Benchmark::Latency);
281 Io.enumCase(Val&: Value, Str: "uops", ConstVal: exegesis::Benchmark::Uops);
282 Io.enumCase(Val&: Value, Str: "inverse_throughput",
283 ConstVal: exegesis::Benchmark::InverseThroughput);
284 }
285};
286
287// std::vector<exegesis::RegisterValue> will be rendered as a list.
288template <> struct SequenceElementTraits<exegesis::RegisterValue> {
289 static const bool flow = false;
290};
291
292template <> struct ScalarTraits<exegesis::RegisterValue> {
293 static constexpr unsigned kRadix = 16;
294 static constexpr bool kSigned = false;
295
296 static void output(const exegesis::RegisterValue &RV, void *Ctx,
297 raw_ostream &Out) {
298 YamlContext &Context = getTypedContext(Ctx);
299 Out << Context.getRegName(Reg: RV.Register) << "=0x"
300 << toString(I: RV.Value, Radix: kRadix, Signed: kSigned);
301 }
302
303 static StringRef input(StringRef String, void *Ctx,
304 exegesis::RegisterValue &RV) {
305 SmallVector<StringRef, 2> Pieces;
306 String.split(A&: Pieces, Separator: "=0x", /* MaxSplit */ -1,
307 /* KeepEmpty */ false);
308 YamlContext &Context = getTypedContext(Ctx);
309 std::optional<MCRegister> RegNo;
310 if (Pieces.size() == 2 && (RegNo = Context.getRegNo(RegName: Pieces[0]))) {
311 RV.Register = *RegNo;
312 const unsigned BitsNeeded = APInt::getBitsNeeded(str: Pieces[1], radix: kRadix);
313 RV.Value = APInt(BitsNeeded, Pieces[1], kRadix);
314 } else {
315 Context.getErrorStream()
316 << "Unknown initial register value: '" << String << "'";
317 }
318 return Context.getInputError();
319 }
320
321 static QuotingType mustQuote(StringRef) { return QuotingType::Single; }
322
323 static const bool flow = true;
324};
325
326template <> struct MappingContextTraits<exegesis::BenchmarkKey, YamlContext> {
327 static void mapping(IO &Io, exegesis::BenchmarkKey &Obj,
328 YamlContext &Context) {
329 Io.setContext(&Context);
330 Io.mapRequired(Key: "instructions", Val&: Obj.Instructions);
331 Io.mapOptional(Key: "config", Val&: Obj.Config);
332 Io.mapRequired(Key: "register_initial_values", Val&: Obj.RegisterInitialValues);
333 }
334};
335
336template <> struct MappingContextTraits<exegesis::Benchmark, YamlContext> {
337 struct NormalizedBinary {
338 NormalizedBinary(IO &io) {}
339 NormalizedBinary(IO &, std::vector<uint8_t> &Data) : Binary(Data) {}
340 std::vector<uint8_t> denormalize(IO &) {
341 std::vector<uint8_t> Data;
342 std::string Str;
343 raw_string_ostream OSS(Str);
344 Binary.writeAsBinary(OS&: OSS);
345 Data.assign(first: Str.begin(), last: Str.end());
346 return Data;
347 }
348
349 BinaryRef Binary;
350 };
351
352 static void mapping(IO &Io, exegesis::Benchmark &Obj, YamlContext &Context) {
353 Io.mapRequired(Key: "mode", Val&: Obj.Mode);
354 Io.mapRequired(Key: "key", Val&: Obj.Key, Ctx&: Context);
355 Io.mapRequired(Key: "cpu_name", Val&: Obj.CpuName);
356 Io.mapRequired(Key: "llvm_triple", Val&: Obj.LLVMTriple);
357 // Optionally map num_repetitions and min_instructions to the same
358 // value to preserve backwards compatibility.
359 // TODO(boomanaiden154): Move min_instructions to mapRequired and
360 // remove num_repetitions once num_repetitions is ready to be removed
361 // completely.
362 if (Io.outputting())
363 Io.mapRequired(Key: "min_instructions", Val&: Obj.MinInstructions);
364 else {
365 Io.mapOptional(Key: "num_repetitions", Val&: Obj.MinInstructions);
366 Io.mapOptional(Key: "min_instructions", Val&: Obj.MinInstructions);
367 }
368 Io.mapRequired(Key: "measurements", Val&: Obj.Measurements);
369 Io.mapRequired(Key: "error", Val&: Obj.Error);
370 Io.mapOptional(Key: "info", Val&: Obj.Info);
371 // AssembledSnippet
372 MappingNormalization<NormalizedBinary, std::vector<uint8_t>> BinaryString(
373 Io, Obj.AssembledSnippet);
374 Io.mapOptional(Key: "assembled_snippet", Val&: BinaryString->Binary);
375 }
376};
377
378template <> struct MappingTraits<exegesis::Benchmark::TripleAndCpu> {
379 static void mapping(IO &Io, exegesis::Benchmark::TripleAndCpu &Obj) {
380 assert(!Io.outputting() && "can only read TripleAndCpu");
381 // Read triple.
382 Io.mapRequired(Key: "llvm_triple", Val&: Obj.LLVMTriple);
383 Io.mapRequired(Key: "cpu_name", Val&: Obj.CpuName);
384 // Drop everything else.
385 }
386};
387
388} // namespace yaml
389
390namespace exegesis {
391
392Expected<std::set<Benchmark::TripleAndCpu>>
393Benchmark::readTriplesAndCpusFromYamls(MemoryBufferRef Buffer) {
394 // We're only mapping a field, drop other fields and silence the corresponding
395 // warnings.
396 yaml::Input Yin(Buffer, nullptr, +[](const SMDiagnostic &, void *Context) {});
397 Yin.setAllowUnknownKeys(true);
398 std::set<TripleAndCpu> Result;
399 yaml::EmptyContext Context;
400 while (Yin.setCurrentDocument()) {
401 TripleAndCpu TC;
402 yamlize(io&: Yin, Val&: TC, /*unused*/ true, Ctx&: Context);
403 if (Yin.error())
404 return errorCodeToError(EC: Yin.error());
405 Result.insert(x: TC);
406 Yin.nextDocument();
407 }
408 return Result;
409}
410
411Expected<Benchmark> Benchmark::readYaml(const LLVMState &State,
412 MemoryBufferRef Buffer) {
413 yaml::Input Yin(Buffer);
414 YamlContext Context(State);
415 Benchmark Benchmark;
416 if (Yin.setCurrentDocument())
417 yaml::yamlize(io&: Yin, Val&: Benchmark, /*unused*/ true, Ctx&: Context);
418 if (!Context.getLastError().empty())
419 return make_error<Failure>(Args&: Context.getLastError());
420 return std::move(Benchmark);
421}
422
423Expected<std::vector<Benchmark>> Benchmark::readYamls(const LLVMState &State,
424 MemoryBufferRef Buffer) {
425 yaml::Input Yin(Buffer);
426 YamlContext Context(State);
427 // Recoverable per-entry errors (e.g. unknown opcodes) are recorded rather
428 // than reported to the parser, so a single bad entry can be dropped without
429 // aborting the read of the whole file.
430 Context.ContinueOnError = true;
431 std::vector<Benchmark> Benchmarks;
432 unsigned NumSkippedEntries = 0;
433 while (Yin.setCurrentDocument()) {
434 Benchmarks.emplace_back();
435 yamlize(io&: Yin, Val&: Benchmarks.back(), /*unused*/ true, Ctx&: Context);
436 if (Yin.error())
437 return errorCodeToError(EC: Yin.error());
438 if (!Context.getLastError().empty()) {
439 // Warn about the unparsable entry (e.g. an unknown opcode from a
440 // bitrotted sample), discard it, and continue so that a single bad entry
441 // doesn't abort the read of the whole file.
442 WithColor::warning() << "skipping benchmark entry: "
443 << Context.getLastError();
444 Context.getLastError().clear();
445 Benchmarks.pop_back();
446 ++NumSkippedEntries;
447 }
448 Yin.nextDocument();
449 }
450 if (NumSkippedEntries)
451 WithColor::warning() << "skipped " << NumSkippedEntries
452 << " benchmark entries that could not be parsed\n";
453 return std::move(Benchmarks);
454}
455
456Error Benchmark::writeYamlTo(const LLVMState &State, raw_ostream &OS) {
457 llvm::scope_exit Cleanup([&] { OS.flush(); });
458 yaml::Output Yout(OS, nullptr /*Ctx*/, 200 /*WrapColumn*/);
459 YamlContext Context(State);
460 Yout.beginDocuments();
461 yaml::yamlize(io&: Yout, Val&: *this, /*unused*/ true, Ctx&: Context);
462 if (!Context.getLastError().empty())
463 return make_error<Failure>(Args&: Context.getLastError());
464 Yout.endDocuments();
465 return Error::success();
466}
467
468Error Benchmark::readYamlFrom(const LLVMState &State, StringRef InputContent) {
469 yaml::Input Yin(InputContent);
470 YamlContext Context(State);
471 if (Yin.setCurrentDocument())
472 yaml::yamlize(io&: Yin, Val&: *this, /*unused*/ true, Ctx&: Context);
473 if (!Context.getLastError().empty())
474 return make_error<Failure>(Args&: Context.getLastError());
475 return Error::success();
476}
477
478void PerInstructionStats::push(const BenchmarkMeasure &BM) {
479 if (Key.empty())
480 Key = BM.Key;
481 assert(Key == BM.Key);
482 ++NumValues;
483 SumValues += BM.PerInstructionValue;
484 MaxValue = std::max(a: MaxValue, b: BM.PerInstructionValue);
485 MinValue = std::min(a: MinValue, b: BM.PerInstructionValue);
486}
487
488bool operator==(const BenchmarkMeasure &A, const BenchmarkMeasure &B) {
489 return std::tie(args: A.Key, args: A.PerInstructionValue, args: A.PerSnippetValue) ==
490 std::tie(args: B.Key, args: B.PerInstructionValue, args: B.PerSnippetValue);
491}
492
493} // namespace exegesis
494} // namespace llvm
495