summaryrefslogtreecommitdiffstats
path: root/llvm/tools/llvm-exegesis/lib/BenchmarkResult.cpp
blob: dcc798d71c0dfe756a5fe47ff7bfc2014afdc176 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//===-- BenchmarkResult.cpp -------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "BenchmarkResult.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/FileOutputBuffer.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/raw_ostream.h"

// Defining YAML traits for IO.
namespace llvm {
namespace yaml {

// std::vector<exegesis::Measure> will be rendered as a list.
template <> struct SequenceElementTraits<exegesis::BenchmarkMeasure> {
  static const bool flow = false;
};

// exegesis::Measure is rendererd as a flow instead of a list.
// e.g. { "key": "the key", "value": 0123 }
template <> struct MappingTraits<exegesis::BenchmarkMeasure> {
  static void mapping(IO &Io, exegesis::BenchmarkMeasure &Obj) {
    Io.mapRequired("key", Obj.Key);
    Io.mapRequired("value", Obj.Value);
    Io.mapOptional("debug_string", Obj.DebugString);
  }
  static const bool flow = true;
};

template <> struct MappingTraits<exegesis::AsmTemplate> {
  static void mapping(IO &Io, exegesis::AsmTemplate &Obj) {
    Io.mapRequired("name", Obj.Name);
  }
};

template <> struct MappingTraits<exegesis::InstructionBenchmark> {
  static void mapping(IO &Io, exegesis::InstructionBenchmark &Obj) {
    Io.mapRequired("asm_template", Obj.AsmTmpl);
    Io.mapRequired("cpu_name", Obj.CpuName);
    Io.mapRequired("llvm_triple", Obj.LLVMTriple);
    Io.mapRequired("num_repetitions", Obj.NumRepetitions);
    Io.mapRequired("measurements", Obj.Measurements);
    Io.mapRequired("error", Obj.Error);
  }
};

} // namespace yaml
} // namespace llvm

LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(exegesis::InstructionBenchmark)

namespace exegesis {

namespace {

template <typename ObjectOrList>
ObjectOrList readYamlOrDieCommon(llvm::StringRef Filename) {
  std::unique_ptr<llvm::MemoryBuffer> MemBuffer = llvm::cantFail(
      llvm::errorOrToExpected(llvm::MemoryBuffer::getFile(Filename)));
  llvm::yaml::Input Yin(*MemBuffer);
  ObjectOrList Benchmark;
  Yin >> Benchmark;
  return Benchmark;
}

} // namespace

InstructionBenchmark
InstructionBenchmark::readYamlOrDie(llvm::StringRef Filename) {
  return readYamlOrDieCommon<InstructionBenchmark>(Filename);
}

std::vector<InstructionBenchmark>
InstructionBenchmark::readYamlsOrDie(llvm::StringRef Filename) {
  return readYamlOrDieCommon<std::vector<InstructionBenchmark>>(Filename);
}

void InstructionBenchmark::writeYamlOrDie(const llvm::StringRef Filename) {
  if (Filename == "-") {
    llvm::yaml::Output Yout(llvm::outs());
    Yout << *this;
  } else {
    llvm::SmallString<1024> Buffer;
    llvm::raw_svector_ostream Ostr(Buffer);
    llvm::yaml::Output Yout(Ostr);
    Yout << *this;
    std::unique_ptr<llvm::FileOutputBuffer> File =
        llvm::cantFail(llvm::FileOutputBuffer::create(Filename, Buffer.size()));
    memcpy(File->getBufferStart(), Buffer.data(), Buffer.size());
    llvm::cantFail(File->commit());
  }
}

} // namespace exegesis
OpenPOWER on IntegriCloud