[docs] Fix build-docs.sh
[llvm-project.git] / llvm / unittests / tools / llvm-exegesis / X86 / BenchmarkResultTest.cpp
blobf8e7d627e1d9a1ee194fd72ff3d02af975992a4c
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 //===----------------------------------------------------------------------===//
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"
22 using ::testing::AllOf;
23 using ::testing::Eq;
24 using ::testing::get;
25 using ::testing::Pointwise;
26 using ::testing::Property;
28 namespace llvm {
29 namespace exegesis {
31 void InitializeX86ExegesisTarget();
33 static std::string Dump(const MCInst &McInst) {
34 std::string Buffer;
35 raw_string_ostream OS(Buffer);
36 McInst.print(OS);
37 return Buffer;
40 MATCHER(EqMCInst, "") {
41 const std::string Lhs = Dump(get<0>(arg));
42 const std::string Rhs = Dump(get<1>(arg));
43 if (Lhs != Rhs) {
44 *result_listener << Lhs << " <=> " << Rhs;
45 return false;
47 return true;
50 namespace {
52 TEST(BenchmarkResultTest, WriteToAndReadFromDisk) {
53 LLVMInitializeX86TargetInfo();
54 LLVMInitializeX86Target();
55 LLVMInitializeX86TargetMC();
56 InitializeX86ExegesisTarget();
58 // Read benchmarks.
59 const LLVMState State =
60 cantFail(LLVMState::Create("x86_64-unknown-linux", "haswell"));
62 ExitOnError ExitOnErr;
64 InstructionBenchmark ToDisk;
66 ToDisk.Key.Instructions.push_back(MCInstBuilder(X86::XOR32rr)
67 .addReg(X86::AL)
68 .addReg(X86::AH)
69 .addImm(123)
70 .addDFPImm(bit_cast<uint64_t>(0.5)));
71 ToDisk.Key.Config = "config";
72 ToDisk.Key.RegisterInitialValues = {
73 RegisterValue{X86::AL, APInt(8, "-1", 10)},
74 RegisterValue{X86::AH, 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";
84 SmallString<64> Filename;
85 std::error_code EC;
86 EC = sys::fs::createUniqueDirectory("BenchmarkResultTestDir", Filename);
87 ASSERT_FALSE(EC);
88 sys::path::append(Filename, "data.yaml");
89 errs() << Filename << "-------\n";
90 ExitOnErr(ToDisk.writeYaml(State, Filename));
93 // One-element version.
94 const auto FromDisk =
95 ExitOnErr(InstructionBenchmark::readYaml(State, Filename));
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);
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);
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
137 } // namespace
138 } // namespace exegesis
139 } // namespace llvm