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