1 //===-- BenchmarkResultTest.cpp ---------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "BenchmarkResult.h"
10 #include "X86InstrInfo.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Support/Error.h"
13 #include "llvm/Support/FileSystem.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Support/TargetRegistry.h"
16 #include "llvm/Support/TargetSelect.h"
17 #include "llvm/Support/YAMLTraits.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21
22 using ::testing::AllOf;
23 using ::testing::Eq;
24 using ::testing::get;
25 using ::testing::Pointwise;
26 using ::testing::Property;
27
28 namespace llvm {
29 namespace exegesis {
30
Dump(const MCInst & McInst)31 static std::string Dump(const MCInst &McInst) {
32 std::string Buffer;
33 raw_string_ostream OS(Buffer);
34 McInst.print(OS);
35 return Buffer;
36 }
37
38 MATCHER(EqMCInst, "") {
39 const std::string Lhs = Dump(get<0>(arg));
40 const std::string Rhs = Dump(get<1>(arg));
41 if (Lhs != Rhs) {
42 *result_listener << Lhs << " <=> " << Rhs;
43 return false;
44 }
45 return true;
46 }
47
48 namespace {
49
TEST(BenchmarkResultTest,WriteToAndReadFromDisk)50 TEST(BenchmarkResultTest, WriteToAndReadFromDisk) {
51 LLVMInitializeX86TargetInfo();
52 LLVMInitializeX86Target();
53 LLVMInitializeX86TargetMC();
54
55 // Read benchmarks.
56 const LLVMState State("x86_64-unknown-linux", "haswell");
57
58 ExitOnError ExitOnErr;
59
60 InstructionBenchmark ToDisk;
61
62 ToDisk.Key.Instructions.push_back(MCInstBuilder(X86::XOR32rr)
63 .addReg(X86::AL)
64 .addReg(X86::AH)
65 .addImm(123)
66 .addDFPImm(bit_cast<uint64_t>(0.5)));
67 ToDisk.Key.Config = "config";
68 ToDisk.Key.RegisterInitialValues = {
69 RegisterValue{X86::AL, APInt(8, "-1", 10)},
70 RegisterValue{X86::AH, APInt(8, "123", 10)}};
71 ToDisk.Mode = InstructionBenchmark::Latency;
72 ToDisk.CpuName = "cpu_name";
73 ToDisk.LLVMTriple = "llvm_triple";
74 ToDisk.NumRepetitions = 1;
75 ToDisk.Measurements.push_back(BenchmarkMeasure{"a", 1, 1});
76 ToDisk.Measurements.push_back(BenchmarkMeasure{"b", 2, 2});
77 ToDisk.Error = "error";
78 ToDisk.Info = "info";
79
80 SmallString<64> Filename;
81 std::error_code EC;
82 EC = sys::fs::createUniqueDirectory("BenchmarkResultTestDir", Filename);
83 ASSERT_FALSE(EC);
84 sys::path::append(Filename, "data.yaml");
85 errs() << Filename << "-------\n";
86 ExitOnErr(ToDisk.writeYaml(State, Filename));
87
88 {
89 // One-element version.
90 const auto FromDisk =
91 ExitOnErr(InstructionBenchmark::readYaml(State, Filename));
92
93 EXPECT_THAT(FromDisk.Key.Instructions,
94 Pointwise(EqMCInst(), ToDisk.Key.Instructions));
95 EXPECT_EQ(FromDisk.Key.Config, ToDisk.Key.Config);
96 EXPECT_EQ(FromDisk.Mode, ToDisk.Mode);
97 EXPECT_EQ(FromDisk.CpuName, ToDisk.CpuName);
98 EXPECT_EQ(FromDisk.LLVMTriple, ToDisk.LLVMTriple);
99 EXPECT_EQ(FromDisk.NumRepetitions, ToDisk.NumRepetitions);
100 EXPECT_THAT(FromDisk.Measurements, ToDisk.Measurements);
101 EXPECT_THAT(FromDisk.Error, ToDisk.Error);
102 EXPECT_EQ(FromDisk.Info, ToDisk.Info);
103 }
104 {
105 // Vector version.
106 const auto FromDiskVector =
107 ExitOnErr(InstructionBenchmark::readYamls(State, Filename));
108 ASSERT_EQ(FromDiskVector.size(), size_t{1});
109 const auto FromDisk = FromDiskVector[0];
110 EXPECT_THAT(FromDisk.Key.Instructions,
111 Pointwise(EqMCInst(), ToDisk.Key.Instructions));
112 EXPECT_EQ(FromDisk.Key.Config, ToDisk.Key.Config);
113 EXPECT_EQ(FromDisk.Mode, ToDisk.Mode);
114 EXPECT_EQ(FromDisk.CpuName, ToDisk.CpuName);
115 EXPECT_EQ(FromDisk.LLVMTriple, ToDisk.LLVMTriple);
116 EXPECT_EQ(FromDisk.NumRepetitions, ToDisk.NumRepetitions);
117 EXPECT_THAT(FromDisk.Measurements, ToDisk.Measurements);
118 EXPECT_THAT(FromDisk.Error, ToDisk.Error);
119 EXPECT_EQ(FromDisk.Info, ToDisk.Info);
120 }
121 }
122
TEST(BenchmarkResultTest,PerInstructionStats)123 TEST(BenchmarkResultTest, PerInstructionStats) {
124 PerInstructionStats Stats;
125 Stats.push(BenchmarkMeasure{"a", 0.5, 0.0});
126 Stats.push(BenchmarkMeasure{"a", 1.5, 0.0});
127 Stats.push(BenchmarkMeasure{"a", -1.0, 0.0});
128 Stats.push(BenchmarkMeasure{"a", 0.0, 0.0});
129 EXPECT_EQ(Stats.min(), -1.0);
130 EXPECT_EQ(Stats.max(), 1.5);
131 EXPECT_EQ(Stats.avg(), 0.25); // (0.5+1.5-1.0+0.0) / 4
132 }
133 } // namespace
134 } // namespace exegesis
135 } // namespace llvm
136