blob: a3dd56ca3a216b49568f23c86a141fbb3dff2879 [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) {
Clement Courbet28d4f852018-09-26 13:35:10 +0000167 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 Courbet684a5f62018-09-26 08:37:21 +0000172 Io.mapRequired("value", Obj.PerInstructionValue);
173 Io.mapOptional("per_snippet_value", Obj.PerSnippetValue);
Clement Courbetac74acd2018-04-04 11:37:06 +0000174 }
175 static const bool flow = true;
176};
177
Clement Courbet2cb97b92018-06-04 11:43:40 +0000178template <>
Clement Courbet62b34fa2018-06-06 09:42:36 +0000179struct ScalarEnumerationTraits<exegesis::InstructionBenchmark::ModeE> {
Clement Courbet2cb97b92018-06-04 11:43:40 +0000180 static void enumeration(IO &Io,
Clement Courbet62b34fa2018-06-06 09:42:36 +0000181 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 Courbet2cb97b92018-06-04 11:43:40 +0000185 }
186};
187
Guillaume Chatelet345fae52018-09-25 15:15:54 +0000188// std::vector<exegesis::RegisterValue> will be rendered as a list.
189template <> struct SequenceElementTraits<exegesis::RegisterValue> {
190 static const bool flow = false;
191};
192
193template <> 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 Chatelet55ad0872018-09-25 12:18:08 +0000226template <>
227struct MappingContextTraits<exegesis::InstructionBenchmarkKey, YamlContext> {
228 static void mapping(IO &Io, exegesis::InstructionBenchmarkKey &Obj,
229 YamlContext &Context) {
230 Io.setContext(&Context);
Clement Courbet49fad1c2018-06-14 06:57:52 +0000231 Io.mapRequired("instructions", Obj.Instructions);
Clement Courbeta66bfaa42018-05-15 13:07:05 +0000232 Io.mapOptional("config", Obj.Config);
Guillaume Chatelet345fae52018-09-25 15:15:54 +0000233 Io.mapRequired("register_initial_values", Obj.RegisterInitialValues);
Clement Courbetac74acd2018-04-04 11:37:06 +0000234 }
235};
236
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000237template <>
238struct MappingContextTraits<exegesis::InstructionBenchmark, YamlContext> {
Guillaume Chatelet345fae52018-09-25 15:15:54 +0000239 struct NormalizedBinary {
Clement Courbet4273e1e2018-06-15 07:30:45 +0000240 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 Chatelet55ad0872018-09-25 12:18:08 +0000255 static void mapping(IO &Io, exegesis::InstructionBenchmark &Obj,
256 YamlContext &Context) {
Clement Courbet62b34fa2018-06-06 09:42:36 +0000257 Io.mapRequired("mode", Obj.Mode);
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000258 Io.mapRequired("key", Obj.Key, Context);
Clement Courbetac74acd2018-04-04 11:37:06 +0000259 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 Courbeta66bfaa42018-05-15 13:07:05 +0000264 Io.mapOptional("info", Obj.Info);
Clement Courbet4273e1e2018-06-15 07:30:45 +0000265 // AssembledSnippet
266 MappingNormalization<NormalizedBinary, std::vector<uint8_t>> BinaryString(
267 Io, Obj.AssembledSnippet);
268 Io.mapOptional("assembled_snippet", BinaryString->Binary);
Clement Courbetac74acd2018-04-04 11:37:06 +0000269 }
270};
271
272} // namespace yaml
273} // namespace llvm
274
275namespace exegesis {
276
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000277llvm::Expected<InstructionBenchmark>
278InstructionBenchmark::readYaml(const LLVMState &State,
279 llvm::StringRef Filename) {
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000280 if (auto ExpectedMemoryBuffer =
281 llvm::errorOrToExpected(llvm::MemoryBuffer::getFile(Filename))) {
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000282 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 Chatelet8c91d4c2018-06-07 07:51:16 +0000289 return Benchmark;
290 } else {
291 return ExpectedMemoryBuffer.takeError();
292 }
Clement Courbetac74acd2018-04-04 11:37:06 +0000293}
294
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000295llvm::Expected<std::vector<InstructionBenchmark>>
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000296InstructionBenchmark::readYamls(const LLVMState &State,
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000297 llvm::StringRef Filename) {
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000298 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 Courbet7b7c27a2018-05-14 09:01:22 +0000316}
317
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000318void InstructionBenchmark::writeYamlTo(const LLVMState &State,
Guillaume Chatelet083a0c162018-06-07 07:40:40 +0000319 llvm::raw_ostream &OS) {
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000320 llvm::yaml::Output Yout(OS);
321 YamlContext Context(State);
Guillaume Chatelet6078f822018-09-25 14:48:24 +0000322 Yout.beginDocuments();
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000323 llvm::yaml::yamlize(Yout, *this, /*unused*/ true, Context);
Guillaume Chatelet6078f822018-09-25 14:48:24 +0000324 Yout.endDocuments();
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000325}
326
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000327void InstructionBenchmark::readYamlFrom(const LLVMState &State,
Clement Courbet53d35d22018-06-05 10:56:19 +0000328 llvm::StringRef InputContent) {
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000329 llvm::yaml::Input Yin(InputContent);
330 YamlContext Context(State);
331 if (Yin.setCurrentDocument())
332 llvm::yaml::yamlize(Yin, *this, /*unused*/ true, Context);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000333}
334
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000335llvm::Error InstructionBenchmark::writeYaml(const LLVMState &State,
336 const llvm::StringRef Filename) {
Clement Courbetac74acd2018-04-04 11:37:06 +0000337 if (Filename == "-") {
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000338 writeYamlTo(State, llvm::outs());
Clement Courbetac74acd2018-04-04 11:37:06 +0000339 } else {
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000340 int ResultFD = 0;
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000341 if (auto E = llvm::errorCodeToError(
Zachary Turner1f67a3c2018-06-07 19:58:58 +0000342 openFileForWrite(Filename, ResultFD, llvm::sys::fs::CD_CreateAlways,
343 llvm::sys::fs::F_Text))) {
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000344 return E;
345 }
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000346 llvm::raw_fd_ostream Ostr(ResultFD, true /*shouldClose*/);
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000347 writeYamlTo(State, Ostr);
Clement Courbetac74acd2018-04-04 11:37:06 +0000348 }
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000349 return llvm::Error::success();
Clement Courbetac74acd2018-04-04 11:37:06 +0000350}
351
Clement Courbet684a5f62018-09-26 08:37:21 +0000352void PerInstructionStats::push(const BenchmarkMeasure &BM) {
Clement Courbetae8ae5dc2018-05-24 12:41:02 +0000353 if (Key.empty())
354 Key = BM.Key;
355 assert(Key == BM.Key);
356 ++NumValues;
Clement Courbet684a5f62018-09-26 08:37:21 +0000357 SumValues += BM.PerInstructionValue;
358 MaxValue = std::max(MaxValue, BM.PerInstructionValue);
359 MinValue = std::min(MinValue, BM.PerInstructionValue);
Clement Courbetae8ae5dc2018-05-24 12:41:02 +0000360}
361
Clement Courbetac74acd2018-04-04 11:37:06 +0000362} // namespace exegesis