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