xref: /llvm-project/llvm/unittests/tools/llvm-exegesis/X86/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 "X86InstrInfo.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/MC/TargetRegistry.h"
13 #include "llvm/Support/Error.h"
14 #include "llvm/Support/FileSystem.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 "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 
22 using ::testing::AllOf;
23 using ::testing::ElementsAre;
24 using ::testing::Eq;
25 using ::testing::Field;
26 using ::testing::get;
27 using ::testing::Pointwise;
28 using ::testing::Property;
29 
30 namespace llvm {
31 namespace exegesis {
32 
33 void InitializeX86ExegesisTarget();
34 
35 static std::string Dump(const MCInst &McInst) {
36   std::string Buffer;
37   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 
54 TEST(BenchmarkResultTest, WriteToAndReadFromDisk) {
55   LLVMInitializeX86TargetInfo();
56   LLVMInitializeX86Target();
57   LLVMInitializeX86TargetMC();
58   InitializeX86ExegesisTarget();
59 
60   // Read benchmarks.
61   const LLVMState State =
62       cantFail(LLVMState::Create("x86_64-unknown-linux", "haswell"));
63 
64   ExitOnError ExitOnErr;
65 
66   InstructionBenchmark ToDisk;
67 
68   ToDisk.Key.Instructions.push_back(MCInstBuilder(X86::XOR32rr)
69                                         .addReg(X86::AL)
70                                         .addReg(X86::AH)
71                                         .addImm(123)
72                                         .addDFPImm(bit_cast<uint64_t>(0.5)));
73   ToDisk.Key.Config = "config";
74   ToDisk.Key.RegisterInitialValues = {
75       RegisterValue{X86::AL, APInt(8, "-1", 10)},
76       RegisterValue{X86::AH, APInt(8, "123", 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   const std::unique_ptr<MemoryBuffer> Buffer =
95       std::move(*MemoryBuffer::getFile(Filename));
96 
97   {
98     // Read Triples/Cpu only.
99     const auto TriplesAndCpus =
100         ExitOnErr(InstructionBenchmark::readTriplesAndCpusFromYamls(*Buffer));
101 
102     ASSERT_THAT(TriplesAndCpus,
103                 testing::ElementsAre(
104                     AllOf(Field(&InstructionBenchmark::TripleAndCpu::LLVMTriple,
105                                 Eq("llvm_triple")),
106                           Field(&InstructionBenchmark::TripleAndCpu::CpuName,
107                                 Eq("cpu_name")))));
108   }
109   {
110     // One-element version.
111     const auto FromDisk =
112         ExitOnErr(InstructionBenchmark::readYaml(State, *Buffer));
113 
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     // Vector version.
127     const auto FromDiskVector =
128         ExitOnErr(InstructionBenchmark::readYamls(State, *Buffer));
129     ASSERT_EQ(FromDiskVector.size(), size_t{1});
130     const auto FromDisk = FromDiskVector[0];
131     EXPECT_THAT(FromDisk.Key.Instructions,
132                 Pointwise(EqMCInst(), ToDisk.Key.Instructions));
133     EXPECT_EQ(FromDisk.Key.Config, ToDisk.Key.Config);
134     EXPECT_EQ(FromDisk.Mode, ToDisk.Mode);
135     EXPECT_EQ(FromDisk.CpuName, ToDisk.CpuName);
136     EXPECT_EQ(FromDisk.LLVMTriple, ToDisk.LLVMTriple);
137     EXPECT_EQ(FromDisk.NumRepetitions, ToDisk.NumRepetitions);
138     EXPECT_THAT(FromDisk.Measurements, ToDisk.Measurements);
139     EXPECT_THAT(FromDisk.Error, ToDisk.Error);
140     EXPECT_EQ(FromDisk.Info, ToDisk.Info);
141   }
142 }
143 
144 TEST(BenchmarkResultTest, PerInstructionStats) {
145   PerInstructionStats Stats;
146   Stats.push(BenchmarkMeasure{"a", 0.5, 0.0});
147   Stats.push(BenchmarkMeasure{"a", 1.5, 0.0});
148   Stats.push(BenchmarkMeasure{"a", -1.0, 0.0});
149   Stats.push(BenchmarkMeasure{"a", 0.0, 0.0});
150   EXPECT_EQ(Stats.min(), -1.0);
151   EXPECT_EQ(Stats.max(), 1.5);
152   EXPECT_EQ(Stats.avg(), 0.25); // (0.5+1.5-1.0+0.0) / 4
153 }
154 } // namespace
155 } // namespace exegesis
156 } // namespace llvm
157