xref: /llvm-project/llvm/unittests/tools/llvm-exegesis/Mips/BenchmarkResultTest.cpp (revision e52f8406e8fce18a22cf71ad89d038aaaddfef0f)
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 "TestBase.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/MC/TargetRegistry.h"
14 #include "llvm/Support/Error.h"
15 #include "llvm/Support/Path.h"
16 #include "llvm/Support/TargetSelect.h"
17 #include "llvm/Support/YAMLTraits.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/Testing/Support/SupportHelpers.h"
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 
23 using ::testing::AllOf;
24 using ::testing::Eq;
25 using ::testing::get;
26 using ::testing::Pointwise;
27 using ::testing::Property;
28 
29 using llvm::unittest::TempDir;
30 
31 namespace llvm {
32 namespace exegesis {
33 
34 static std::string Dump(const MCInst &McInst) {
35   std::string Buffer;
36   raw_string_ostream OS(Buffer);
37   McInst.print(OS);
38   return Buffer;
39 }
40 
41 MATCHER(EqMCInst, "") {
42   const std::string Lhs = Dump(get<0>(arg));
43   const std::string Rhs = Dump(get<1>(arg));
44   if (Lhs != Rhs) {
45     *result_listener << Lhs << " <=> " << Rhs;
46     return false;
47   }
48   return true;
49 }
50 
51 namespace {
52 
53 class MipsBenchmarkResultTest : public MipsTestBase {};
54 
55 TEST_F(MipsBenchmarkResultTest, WriteToAndReadFromDisk) {
56   ExitOnError ExitOnErr;
57 
58   InstructionBenchmark ToDisk;
59 
60   ToDisk.Key.Instructions.push_back(MCInstBuilder(Mips::XOR)
61                                         .addReg(Mips::T0)
62                                         .addReg(Mips::T1)
63                                         .addReg(Mips::T2));
64   ToDisk.Key.Config = "config";
65   ToDisk.Key.RegisterInitialValues = {
66       RegisterValue{Mips::T1, APInt(8, "123", 10)},
67       RegisterValue{Mips::T2, APInt(8, "456", 10)}};
68   ToDisk.Mode = InstructionBenchmark::Latency;
69   ToDisk.CpuName = "cpu_name";
70   ToDisk.LLVMTriple = "llvm_triple";
71   ToDisk.NumRepetitions = 1;
72   ToDisk.Measurements.push_back(BenchmarkMeasure{"a", 1, 1});
73   ToDisk.Measurements.push_back(BenchmarkMeasure{"b", 2, 2});
74   ToDisk.Error = "error";
75   ToDisk.Info = "info";
76 
77   TempDir TestDirectory("BenchmarkResultTestDir", /*Unique*/ true);
78   SmallString<64> Filename(TestDirectory.path());
79   sys::path::append(Filename, "data.yaml");
80   errs() << Filename << "-------\n";
81   ExitOnErr(ToDisk.writeYaml(State, Filename));
82 
83   const std::unique_ptr<MemoryBuffer> Buffer =
84       std::move(*MemoryBuffer::getFile(Filename));
85 
86   {
87     // One-element version.
88     const auto FromDisk =
89         ExitOnErr(InstructionBenchmark::readYaml(State, *Buffer));
90 
91     EXPECT_THAT(FromDisk.Key.Instructions,
92                 Pointwise(EqMCInst(), ToDisk.Key.Instructions));
93     EXPECT_EQ(FromDisk.Key.Config, ToDisk.Key.Config);
94     EXPECT_EQ(FromDisk.Mode, ToDisk.Mode);
95     EXPECT_EQ(FromDisk.CpuName, ToDisk.CpuName);
96     EXPECT_EQ(FromDisk.LLVMTriple, ToDisk.LLVMTriple);
97     EXPECT_EQ(FromDisk.NumRepetitions, ToDisk.NumRepetitions);
98     EXPECT_THAT(FromDisk.Measurements, ToDisk.Measurements);
99     EXPECT_THAT(FromDisk.Error, ToDisk.Error);
100     EXPECT_EQ(FromDisk.Info, ToDisk.Info);
101   }
102   {
103     // Vector version.
104     const auto FromDiskVector =
105         ExitOnErr(InstructionBenchmark::readYamls(State, *Buffer));
106     ASSERT_EQ(FromDiskVector.size(), size_t{1});
107     const auto FromDisk = FromDiskVector[0];
108     EXPECT_THAT(FromDisk.Key.Instructions,
109                 Pointwise(EqMCInst(), ToDisk.Key.Instructions));
110     EXPECT_EQ(FromDisk.Key.Config, ToDisk.Key.Config);
111     EXPECT_EQ(FromDisk.Mode, ToDisk.Mode);
112     EXPECT_EQ(FromDisk.CpuName, ToDisk.CpuName);
113     EXPECT_EQ(FromDisk.LLVMTriple, ToDisk.LLVMTriple);
114     EXPECT_EQ(FromDisk.NumRepetitions, ToDisk.NumRepetitions);
115     EXPECT_THAT(FromDisk.Measurements, ToDisk.Measurements);
116     EXPECT_THAT(FromDisk.Error, ToDisk.Error);
117     EXPECT_EQ(FromDisk.Info, ToDisk.Info);
118   }
119 }
120 
121 TEST_F(MipsBenchmarkResultTest, PerInstructionStats) {
122   PerInstructionStats Stats;
123   Stats.push(BenchmarkMeasure{"a", 0.5, 0.0});
124   Stats.push(BenchmarkMeasure{"a", 1.5, 0.0});
125   Stats.push(BenchmarkMeasure{"a", -1.0, 0.0});
126   Stats.push(BenchmarkMeasure{"a", 0.0, 0.0});
127   EXPECT_EQ(Stats.min(), -1.0);
128   EXPECT_EQ(Stats.max(), 1.5);
129   EXPECT_EQ(Stats.avg(), 0.25); // (0.5+1.5-1.0+0.0) / 4
130 }
131 } // namespace
132 } // namespace exegesis
133 } // namespace llvm
134