xref: /llvm-project/llvm/unittests/tools/llvm-exegesis/Mips/BenchmarkResultTest.cpp (revision 84728e65e95b78880ad65bd035fdee36e143265a)
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 "MipsInstrInfo.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 
30 void InitializeMipsExegesisTarget();
31 
32 bool operator==(const BenchmarkMeasure &A, const BenchmarkMeasure &B) {
33   return std::tie(A.Key, A.PerInstructionValue, A.PerSnippetValue) ==
34          std::tie(B.Key, B.PerInstructionValue, B.PerSnippetValue);
35 }
36 
37 static std::string Dump(const MCInst &McInst) {
38   std::string Buffer;
39   raw_string_ostream OS(Buffer);
40   McInst.print(OS);
41   return Buffer;
42 }
43 
44 MATCHER(EqMCInst, "") {
45   const std::string Lhs = Dump(get<0>(arg));
46   const std::string Rhs = Dump(get<1>(arg));
47   if (Lhs != Rhs) {
48     *result_listener << Lhs << " <=> " << Rhs;
49     return false;
50   }
51   return true;
52 }
53 
54 namespace {
55 
56 TEST(BenchmarkResultTest, WriteToAndReadFromDisk) {
57   LLVMInitializeMipsTargetInfo();
58   LLVMInitializeMipsTarget();
59   LLVMInitializeMipsTargetMC();
60   InitializeMipsExegesisTarget();
61 
62   // Read benchmarks.
63   const LLVMState State("mips-unknown-linux", "mips32");
64 
65   ExitOnError ExitOnErr;
66 
67   InstructionBenchmark ToDisk;
68 
69   ToDisk.Key.Instructions.push_back(MCInstBuilder(Mips::XOR)
70                                         .addReg(Mips::T0)
71                                         .addReg(Mips::T1)
72                                         .addReg(Mips::T2));
73   ToDisk.Key.Config = "config";
74   ToDisk.Key.RegisterInitialValues = {
75       RegisterValue{Mips::T1, APInt(8, "123", 10)},
76       RegisterValue{Mips::T2, APInt(8, "456", 10)}};
77   ToDisk.Mode = InstructionBenchmark::Latency;
78   ToDisk.CpuName = "cpu_name";
79   ToDisk.LLVMTriple = "llvm_triple";
80   ToDisk.NumRepetitions = 1;
81   ToDisk.Measurements.push_back(BenchmarkMeasure{"a", 1, 1});
82   ToDisk.Measurements.push_back(BenchmarkMeasure{"b", 2, 2});
83   ToDisk.Error = "error";
84   ToDisk.Info = "info";
85 
86   SmallString<64> Filename;
87   std::error_code EC;
88   EC = sys::fs::createUniqueDirectory("BenchmarkResultTestDir", Filename);
89   ASSERT_FALSE(EC);
90   sys::path::append(Filename, "data.yaml");
91   errs() << Filename << "-------\n";
92   ExitOnErr(ToDisk.writeYaml(State, Filename));
93 
94   {
95     // One-element version.
96     const auto FromDisk =
97         ExitOnErr(InstructionBenchmark::readYaml(State, Filename));
98 
99     EXPECT_THAT(FromDisk.Key.Instructions,
100                 Pointwise(EqMCInst(), ToDisk.Key.Instructions));
101     EXPECT_EQ(FromDisk.Key.Config, ToDisk.Key.Config);
102     EXPECT_EQ(FromDisk.Mode, ToDisk.Mode);
103     EXPECT_EQ(FromDisk.CpuName, ToDisk.CpuName);
104     EXPECT_EQ(FromDisk.LLVMTriple, ToDisk.LLVMTriple);
105     EXPECT_EQ(FromDisk.NumRepetitions, ToDisk.NumRepetitions);
106     EXPECT_THAT(FromDisk.Measurements, ToDisk.Measurements);
107     EXPECT_THAT(FromDisk.Error, ToDisk.Error);
108     EXPECT_EQ(FromDisk.Info, ToDisk.Info);
109   }
110   {
111     // Vector version.
112     const auto FromDiskVector =
113         ExitOnErr(InstructionBenchmark::readYamls(State, Filename));
114     ASSERT_EQ(FromDiskVector.size(), size_t{1});
115     const auto FromDisk = FromDiskVector[0];
116     EXPECT_THAT(FromDisk.Key.Instructions,
117                 Pointwise(EqMCInst(), ToDisk.Key.Instructions));
118     EXPECT_EQ(FromDisk.Key.Config, ToDisk.Key.Config);
119     EXPECT_EQ(FromDisk.Mode, ToDisk.Mode);
120     EXPECT_EQ(FromDisk.CpuName, ToDisk.CpuName);
121     EXPECT_EQ(FromDisk.LLVMTriple, ToDisk.LLVMTriple);
122     EXPECT_EQ(FromDisk.NumRepetitions, ToDisk.NumRepetitions);
123     EXPECT_THAT(FromDisk.Measurements, ToDisk.Measurements);
124     EXPECT_THAT(FromDisk.Error, ToDisk.Error);
125     EXPECT_EQ(FromDisk.Info, ToDisk.Info);
126   }
127 }
128 
129 TEST(BenchmarkResultTest, PerInstructionStats) {
130   PerInstructionStats Stats;
131   Stats.push(BenchmarkMeasure{"a", 0.5, 0.0});
132   Stats.push(BenchmarkMeasure{"a", 1.5, 0.0});
133   Stats.push(BenchmarkMeasure{"a", -1.0, 0.0});
134   Stats.push(BenchmarkMeasure{"a", 0.0, 0.0});
135   EXPECT_EQ(Stats.min(), -1.0);
136   EXPECT_EQ(Stats.max(), 1.5);
137   EXPECT_EQ(Stats.avg(), 0.25); // (0.5+1.5-1.0+0.0) / 4
138 }
139 } // namespace
140 } // namespace exegesis
141 } // namespace llvm
142