blob: 9a41114a853fb443d7e5f67772ffcb89441b9ee7 [file] [log] [blame]
Clement Courbetac74acd2018-04-04 11:37:06 +00001//===-- 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 Chatelet55ad0872018-09-25 12:18:08 +000011#include "BenchmarkRunner.h"
Clement Courbet53d35d22018-06-05 10:56:19 +000012#include "llvm/ADT/STLExtras.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000013#include "llvm/ADT/StringRef.h"
Clement Courbet4273e1e2018-06-15 07:30:45 +000014#include "llvm/ObjectYAML/YAML.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000015#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 Chatelet083a0c162018-06-07 07:40:40 +000020static constexpr const char kIntegerFormat[] = "i_0x%" PRId64 "x";
21static constexpr const char kDoubleFormat[] = "f_%la";
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000022static constexpr const char kInvalidOperand[] = "INVALID";
Guillaume Chatelet083a0c162018-06-07 07:40:40 +000023
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000024// A mutable struct holding an LLVMState that can be passed through the
25// serialization process to encode/decode registers and instructions.
26struct YamlContext {
27 YamlContext(const exegesis::LLVMState &State)
28 : State(&State), ErrorStream(LastError) {}
Guillaume Chatelet083a0c162018-06-07 07:40:40 +000029
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000030 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 Chatelet083a0c162018-06-07 07:40:40 +000035 }
36 }
Guillaume Chatelet083a0c162018-06-07 07:40:40 +000037
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000038 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 Chatelet083a0c162018-06-07 07:40:40 +000054
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000055 std::string &getLastError() { return ErrorStream.str(); }
56
Guillaume Chatelet345fae52018-09-25 15:15:54 +000057 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 Chatelet55ad0872018-09-25 12:18:08 +000075private:
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 Chatelet55ad0872018-09-25 12:18:08 +0000104 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 Chatelet55ad0872018-09-25 12:18:08 +0000111 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 Chatelet083a0c162018-06-07 07:40:40 +0000124
Clement Courbetac74acd2018-04-04 11:37:06 +0000125// Defining YAML traits for IO.
126namespace llvm {
127namespace yaml {
128
Guillaume Chatelet345fae52018-09-25 15:15:54 +0000129static YamlContext &getTypedContext(void *Ctx) {
130 return *reinterpret_cast<YamlContext *>(Ctx);
131}
132
Clement Courbet53d35d22018-06-05 10:56:19 +0000133// std::vector<llvm::MCInst> will be rendered as a list.
134template <> struct SequenceElementTraits<llvm::MCInst> {
135 static const bool flow = false;
136};
137
138template <> struct ScalarTraits<llvm::MCInst> {
139
140 static void output(const llvm::MCInst &Value, void *Ctx,
141 llvm::raw_ostream &Out) {
Guillaume Chatelet345fae52018-09-25 15:15:54 +0000142 getTypedContext(Ctx).serializeMCInst(Value, Out);
Clement Courbet53d35d22018-06-05 10:56:19 +0000143 }
144
145 static StringRef input(StringRef Scalar, void *Ctx, llvm::MCInst &Value) {
Guillaume Chatelet345fae52018-09-25 15:15:54 +0000146 YamlContext &Context = getTypedContext(Ctx);
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000147 Context.deserializeMCInst(Scalar, Value);
148 return Context.getLastError();
Clement Courbet53d35d22018-06-05 10:56:19 +0000149 }
150
Guillaume Chatelet345fae52018-09-25 15:15:54 +0000151 // By default strings are quoted only when necessary.
152 // We force the use of single quotes for uniformity.
Clement Courbet53d35d22018-06-05 10:56:19 +0000153 static QuotingType mustQuote(StringRef) { return QuotingType::Single; }
154
155 static const bool flow = true;
156};
157
Clement Courbetac74acd2018-04-04 11:37:06 +0000158// std::vector<exegesis::Measure> will be rendered as a list.
159template <> 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 }
165template <> struct MappingTraits<exegesis::BenchmarkMeasure> {
166 static void mapping(IO &Io, exegesis::BenchmarkMeasure &Obj) {
167 Io.mapRequired("key", Obj.Key);
168 Io.mapRequired("value", Obj.Value);
169 Io.mapOptional("debug_string", Obj.DebugString);
170 }
171 static const bool flow = true;
172};
173
Clement Courbet2cb97b92018-06-04 11:43:40 +0000174template <>
Clement Courbet62b34fa2018-06-06 09:42:36 +0000175struct ScalarEnumerationTraits<exegesis::InstructionBenchmark::ModeE> {
Clement Courbet2cb97b92018-06-04 11:43:40 +0000176 static void enumeration(IO &Io,
Clement Courbet62b34fa2018-06-06 09:42:36 +0000177 exegesis::InstructionBenchmark::ModeE &Value) {
178 Io.enumCase(Value, "", exegesis::InstructionBenchmark::Unknown);
179 Io.enumCase(Value, "latency", exegesis::InstructionBenchmark::Latency);
180 Io.enumCase(Value, "uops", exegesis::InstructionBenchmark::Uops);
Clement Courbet2cb97b92018-06-04 11:43:40 +0000181 }
182};
183
Guillaume Chatelet345fae52018-09-25 15:15:54 +0000184// std::vector<exegesis::RegisterValue> will be rendered as a list.
185template <> struct SequenceElementTraits<exegesis::RegisterValue> {
186 static const bool flow = false;
187};
188
189template <> struct ScalarTraits<exegesis::RegisterValue> {
190 static constexpr const unsigned kRadix = 16;
191 static constexpr const bool kSigned = false;
192
193 static void output(const exegesis::RegisterValue &RV, void *Ctx,
194 llvm::raw_ostream &Out) {
195 YamlContext &Context = getTypedContext(Ctx);
196 Out << Context.getRegName(RV.Register) << "=0x"
197 << RV.Value.toString(kRadix, kSigned);
198 }
199
200 static StringRef input(StringRef String, void *Ctx,
201 exegesis::RegisterValue &RV) {
202 llvm::SmallVector<llvm::StringRef, 2> Pieces;
203 String.split(Pieces, "=0x", /* MaxSplit */ -1,
204 /* KeepEmpty */ false);
205 YamlContext &Context = getTypedContext(Ctx);
206 if (Pieces.size() == 2) {
207 RV.Register = Context.getRegNo(Pieces[0]);
208 const unsigned BitsNeeded = llvm::APInt::getBitsNeeded(Pieces[1], kRadix);
209 RV.Value = llvm::APInt(BitsNeeded, Pieces[1], kRadix);
210 } else {
211 Context.getErrorStream()
212 << "Unknown initial register value: '" << String << "'";
213 }
214 return Context.getLastError();
215 }
216
217 static QuotingType mustQuote(StringRef) { return QuotingType::Single; }
218
219 static const bool flow = true;
220};
221
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000222template <>
223struct MappingContextTraits<exegesis::InstructionBenchmarkKey, YamlContext> {
224 static void mapping(IO &Io, exegesis::InstructionBenchmarkKey &Obj,
225 YamlContext &Context) {
226 Io.setContext(&Context);
Clement Courbet49fad1c2018-06-14 06:57:52 +0000227 Io.mapRequired("instructions", Obj.Instructions);
Clement Courbeta66bfaa42018-05-15 13:07:05 +0000228 Io.mapOptional("config", Obj.Config);
Guillaume Chatelet345fae52018-09-25 15:15:54 +0000229 Io.mapRequired("register_initial_values", Obj.RegisterInitialValues);
Clement Courbetac74acd2018-04-04 11:37:06 +0000230 }
231};
232
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000233template <>
234struct MappingContextTraits<exegesis::InstructionBenchmark, YamlContext> {
Guillaume Chatelet345fae52018-09-25 15:15:54 +0000235 struct NormalizedBinary {
Clement Courbet4273e1e2018-06-15 07:30:45 +0000236 NormalizedBinary(IO &io) {}
237 NormalizedBinary(IO &, std::vector<uint8_t> &Data) : Binary(Data) {}
238 std::vector<uint8_t> denormalize(IO &) {
239 std::vector<uint8_t> Data;
240 std::string Str;
241 raw_string_ostream OSS(Str);
242 Binary.writeAsBinary(OSS);
243 OSS.flush();
244 Data.assign(Str.begin(), Str.end());
245 return Data;
246 }
247
248 BinaryRef Binary;
249 };
250
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000251 static void mapping(IO &Io, exegesis::InstructionBenchmark &Obj,
252 YamlContext &Context) {
Clement Courbet62b34fa2018-06-06 09:42:36 +0000253 Io.mapRequired("mode", Obj.Mode);
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000254 Io.mapRequired("key", Obj.Key, Context);
Clement Courbetac74acd2018-04-04 11:37:06 +0000255 Io.mapRequired("cpu_name", Obj.CpuName);
256 Io.mapRequired("llvm_triple", Obj.LLVMTriple);
257 Io.mapRequired("num_repetitions", Obj.NumRepetitions);
258 Io.mapRequired("measurements", Obj.Measurements);
259 Io.mapRequired("error", Obj.Error);
Clement Courbeta66bfaa42018-05-15 13:07:05 +0000260 Io.mapOptional("info", Obj.Info);
Clement Courbet4273e1e2018-06-15 07:30:45 +0000261 // AssembledSnippet
262 MappingNormalization<NormalizedBinary, std::vector<uint8_t>> BinaryString(
263 Io, Obj.AssembledSnippet);
264 Io.mapOptional("assembled_snippet", BinaryString->Binary);
Clement Courbetac74acd2018-04-04 11:37:06 +0000265 }
266};
267
268} // namespace yaml
269} // namespace llvm
270
271namespace exegesis {
272
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000273llvm::Expected<InstructionBenchmark>
274InstructionBenchmark::readYaml(const LLVMState &State,
275 llvm::StringRef Filename) {
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000276 if (auto ExpectedMemoryBuffer =
277 llvm::errorOrToExpected(llvm::MemoryBuffer::getFile(Filename))) {
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000278 llvm::yaml::Input Yin(*ExpectedMemoryBuffer.get());
279 YamlContext Context(State);
280 InstructionBenchmark Benchmark;
281 if (Yin.setCurrentDocument())
282 llvm::yaml::yamlize(Yin, Benchmark, /*unused*/ true, Context);
283 if (!Context.getLastError().empty())
284 return llvm::make_error<BenchmarkFailure>(Context.getLastError());
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000285 return Benchmark;
286 } else {
287 return ExpectedMemoryBuffer.takeError();
288 }
Clement Courbetac74acd2018-04-04 11:37:06 +0000289}
290
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000291llvm::Expected<std::vector<InstructionBenchmark>>
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000292InstructionBenchmark::readYamls(const LLVMState &State,
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000293 llvm::StringRef Filename) {
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000294 if (auto ExpectedMemoryBuffer =
295 llvm::errorOrToExpected(llvm::MemoryBuffer::getFile(Filename))) {
296 llvm::yaml::Input Yin(*ExpectedMemoryBuffer.get());
297 YamlContext Context(State);
298 std::vector<InstructionBenchmark> Benchmarks;
299 while (Yin.setCurrentDocument()) {
300 Benchmarks.emplace_back();
301 yamlize(Yin, Benchmarks.back(), /*unused*/ true, Context);
302 if (Yin.error())
303 return llvm::errorCodeToError(Yin.error());
304 if (!Context.getLastError().empty())
305 return llvm::make_error<BenchmarkFailure>(Context.getLastError());
306 Yin.nextDocument();
307 }
308 return Benchmarks;
309 } else {
310 return ExpectedMemoryBuffer.takeError();
311 }
Clement Courbet7b7c27a2018-05-14 09:01:22 +0000312}
313
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000314void InstructionBenchmark::writeYamlTo(const LLVMState &State,
Guillaume Chatelet083a0c162018-06-07 07:40:40 +0000315 llvm::raw_ostream &OS) {
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000316 llvm::yaml::Output Yout(OS);
317 YamlContext Context(State);
Guillaume Chatelet6078f822018-09-25 14:48:24 +0000318 Yout.beginDocuments();
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000319 llvm::yaml::yamlize(Yout, *this, /*unused*/ true, Context);
Guillaume Chatelet6078f822018-09-25 14:48:24 +0000320 Yout.endDocuments();
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000321}
322
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000323void InstructionBenchmark::readYamlFrom(const LLVMState &State,
Clement Courbet53d35d22018-06-05 10:56:19 +0000324 llvm::StringRef InputContent) {
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000325 llvm::yaml::Input Yin(InputContent);
326 YamlContext Context(State);
327 if (Yin.setCurrentDocument())
328 llvm::yaml::yamlize(Yin, *this, /*unused*/ true, Context);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000329}
330
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000331llvm::Error InstructionBenchmark::writeYaml(const LLVMState &State,
332 const llvm::StringRef Filename) {
Clement Courbetac74acd2018-04-04 11:37:06 +0000333 if (Filename == "-") {
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000334 writeYamlTo(State, llvm::outs());
Clement Courbetac74acd2018-04-04 11:37:06 +0000335 } else {
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000336 int ResultFD = 0;
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000337 if (auto E = llvm::errorCodeToError(
Zachary Turner1f67a3c2018-06-07 19:58:58 +0000338 openFileForWrite(Filename, ResultFD, llvm::sys::fs::CD_CreateAlways,
339 llvm::sys::fs::F_Text))) {
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000340 return E;
341 }
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000342 llvm::raw_fd_ostream Ostr(ResultFD, true /*shouldClose*/);
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000343 writeYamlTo(State, Ostr);
Clement Courbetac74acd2018-04-04 11:37:06 +0000344 }
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000345 return llvm::Error::success();
Clement Courbetac74acd2018-04-04 11:37:06 +0000346}
347
Clement Courbetae8ae5dc2018-05-24 12:41:02 +0000348void BenchmarkMeasureStats::push(const BenchmarkMeasure &BM) {
349 if (Key.empty())
350 Key = BM.Key;
351 assert(Key == BM.Key);
352 ++NumValues;
353 SumValues += BM.Value;
354 MaxValue = std::max(MaxValue, BM.Value);
355 MinValue = std::min(MinValue, BM.Value);
356}
357
Clement Courbetac74acd2018-04-04 11:37:06 +0000358} // namespace exegesis