blob: ed449dbd94e2d50bbbcc1c1cb10f4463aea47746 [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
Clement Courbeta66bfaa42018-05-15 13:07:05 +000037template <> struct MappingTraits<exegesis::InstructionBenchmarkKey> {
38 static void mapping(IO &Io, exegesis::InstructionBenchmarkKey &Obj) {
39 Io.mapRequired("opcode_name", Obj.OpcodeName);
40 Io.mapRequired("mode", Obj.Mode);
41 Io.mapOptional("config", Obj.Config);
Clement Courbetac74acd2018-04-04 11:37:06 +000042 }
43};
44
45template <> struct MappingTraits<exegesis::InstructionBenchmark> {
46 static void mapping(IO &Io, exegesis::InstructionBenchmark &Obj) {
Clement Courbeta66bfaa42018-05-15 13:07:05 +000047 Io.mapRequired("key", Obj.Key);
Clement Courbetac74acd2018-04-04 11:37:06 +000048 Io.mapRequired("cpu_name", Obj.CpuName);
49 Io.mapRequired("llvm_triple", Obj.LLVMTriple);
50 Io.mapRequired("num_repetitions", Obj.NumRepetitions);
51 Io.mapRequired("measurements", Obj.Measurements);
52 Io.mapRequired("error", Obj.Error);
Clement Courbeta66bfaa42018-05-15 13:07:05 +000053 Io.mapOptional("info", Obj.Info);
Clement Courbetac74acd2018-04-04 11:37:06 +000054 }
55};
56
57} // namespace yaml
58} // namespace llvm
59
Clement Courbet7b7c27a2018-05-14 09:01:22 +000060LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(exegesis::InstructionBenchmark)
61
Clement Courbetac74acd2018-04-04 11:37:06 +000062namespace exegesis {
63
Clement Courbet7b7c27a2018-05-14 09:01:22 +000064template <typename ObjectOrList>
Clement Courbet0e69e2d2018-05-17 10:52:18 +000065static ObjectOrList readYamlOrDieCommon(llvm::StringRef Filename) {
Clement Courbetac74acd2018-04-04 11:37:06 +000066 std::unique_ptr<llvm::MemoryBuffer> MemBuffer = llvm::cantFail(
67 llvm::errorOrToExpected(llvm::MemoryBuffer::getFile(Filename)));
68 llvm::yaml::Input Yin(*MemBuffer);
Clement Courbet7b7c27a2018-05-14 09:01:22 +000069 ObjectOrList Benchmark;
Clement Courbetac74acd2018-04-04 11:37:06 +000070 Yin >> Benchmark;
71 return Benchmark;
72}
73
Clement Courbet7b7c27a2018-05-14 09:01:22 +000074InstructionBenchmark
75InstructionBenchmark::readYamlOrDie(llvm::StringRef Filename) {
76 return readYamlOrDieCommon<InstructionBenchmark>(Filename);
77}
78
79std::vector<InstructionBenchmark>
80InstructionBenchmark::readYamlsOrDie(llvm::StringRef Filename) {
81 return readYamlOrDieCommon<std::vector<InstructionBenchmark>>(Filename);
82}
83
Clement Courbet0e69e2d2018-05-17 10:52:18 +000084void InstructionBenchmark::writeYamlTo(llvm::raw_ostream &S) {
85 llvm::yaml::Output Yout(S);
86 Yout << *this;
87}
88
89void InstructionBenchmark::readYamlFrom(llvm::StringRef InputContent) {
90 llvm::yaml::Input Yin(InputContent);
91 Yin >> *this;
92}
93
94// FIXME: Change the API to let the caller handle errors.
Clement Courbetac74acd2018-04-04 11:37:06 +000095void InstructionBenchmark::writeYamlOrDie(const llvm::StringRef Filename) {
96 if (Filename == "-") {
Clement Courbet0e69e2d2018-05-17 10:52:18 +000097 writeYamlTo(llvm::outs());
Clement Courbetac74acd2018-04-04 11:37:06 +000098 } else {
Clement Courbet0e69e2d2018-05-17 10:52:18 +000099 int ResultFD = 0;
100 llvm::cantFail(llvm::errorCodeToError(
101 openFileForWrite(Filename, ResultFD, llvm::sys::fs::F_Text)));
102 llvm::raw_fd_ostream Ostr(ResultFD, true /*shouldClose*/);
103 writeYamlTo(Ostr);
Clement Courbetac74acd2018-04-04 11:37:06 +0000104 }
105}
106
Clement Courbetae8ae5dc2018-05-24 12:41:02 +0000107void BenchmarkMeasureStats::push(const BenchmarkMeasure &BM) {
108 if (Key.empty())
109 Key = BM.Key;
110 assert(Key == BM.Key);
111 ++NumValues;
112 SumValues += BM.Value;
113 MaxValue = std::max(MaxValue, BM.Value);
114 MinValue = std::min(MinValue, BM.Value);
115}
116
Clement Courbetac74acd2018-04-04 11:37:06 +0000117} // namespace exegesis