blob: e9b8d29aaec4d43737b17c336f4a8dc6c5725d54 [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"
11#include "llvm/ADT/StringRef.h"
12#include "llvm/Support/FileOutputBuffer.h"
13#include "llvm/Support/FileSystem.h"
14#include "llvm/Support/Format.h"
15#include "llvm/Support/raw_ostream.h"
16
17// Defining YAML traits for IO.
18namespace llvm {
19namespace yaml {
20
21// std::vector<exegesis::Measure> will be rendered as a list.
22template <> struct SequenceElementTraits<exegesis::BenchmarkMeasure> {
23 static const bool flow = false;
24};
25
26// exegesis::Measure is rendererd as a flow instead of a list.
27// e.g. { "key": "the key", "value": 0123 }
28template <> struct MappingTraits<exegesis::BenchmarkMeasure> {
29 static void mapping(IO &Io, exegesis::BenchmarkMeasure &Obj) {
30 Io.mapRequired("key", Obj.Key);
31 Io.mapRequired("value", Obj.Value);
32 Io.mapOptional("debug_string", Obj.DebugString);
33 }
34 static const bool flow = true;
35};
36
37template <> struct MappingTraits<exegesis::AsmTemplate> {
38 static void mapping(IO &Io, exegesis::AsmTemplate &Obj) {
39 Io.mapRequired("name", Obj.Name);
40 }
41};
42
43template <> struct MappingTraits<exegesis::InstructionBenchmark> {
44 static void mapping(IO &Io, exegesis::InstructionBenchmark &Obj) {
45 Io.mapRequired("asm_template", Obj.AsmTmpl);
46 Io.mapRequired("cpu_name", Obj.CpuName);
47 Io.mapRequired("llvm_triple", Obj.LLVMTriple);
48 Io.mapRequired("num_repetitions", Obj.NumRepetitions);
49 Io.mapRequired("measurements", Obj.Measurements);
50 Io.mapRequired("error", Obj.Error);
51 }
52};
53
54} // namespace yaml
55} // namespace llvm
56
57namespace exegesis {
58
59InstructionBenchmark
60InstructionBenchmark::readYamlOrDie(llvm::StringRef Filename) {
61 std::unique_ptr<llvm::MemoryBuffer> MemBuffer = llvm::cantFail(
62 llvm::errorOrToExpected(llvm::MemoryBuffer::getFile(Filename)));
63 llvm::yaml::Input Yin(*MemBuffer);
64 InstructionBenchmark Benchmark;
65 Yin >> Benchmark;
66 return Benchmark;
67}
68
69void InstructionBenchmark::writeYamlOrDie(const llvm::StringRef Filename) {
70 if (Filename == "-") {
71 llvm::yaml::Output Yout(llvm::outs());
72 Yout << *this;
73 } else {
74 llvm::SmallString<1024> Buffer;
75 llvm::raw_svector_ostream Ostr(Buffer);
76 llvm::yaml::Output Yout(Ostr);
77 Yout << *this;
78 std::unique_ptr<llvm::FileOutputBuffer> File =
79 llvm::cantFail(llvm::FileOutputBuffer::create(Filename, Buffer.size()));
80 memcpy(File->getBufferStart(), Buffer.data(), Buffer.size());
81 llvm::cantFail(File->commit());
82 }
83}
84
85} // namespace exegesis