blob: 517d3ac5acb0b352fb41e51d236986d35b4c823d [file] [log] [blame]
Clement Courbetac74acd2018-04-04 11:37:06 +00001//===-- BenchmarkResultTest.cpp ---------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Clement Courbetac74acd2018-04-04 11:37:06 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "BenchmarkResult.h"
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000010#include "X86InstrInfo.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000011#include "llvm/ADT/SmallString.h"
12#include "llvm/Support/Error.h"
Clement Courbet308f8b72018-04-13 13:37:07 +000013#include "llvm/Support/Path.h"
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000014#include "llvm/Support/TargetRegistry.h"
15#include "llvm/Support/TargetSelect.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000016#include "llvm/Support/YAMLTraits.h"
17#include "llvm/Support/raw_ostream.h"
18#include "gmock/gmock.h"
19#include "gtest/gtest.h"
20
Clement Courbet53d35d22018-06-05 10:56:19 +000021using ::testing::AllOf;
22using ::testing::Eq;
23using ::testing::get;
24using ::testing::Pointwise;
25using ::testing::Property;
26
Fangrui Song32401af2018-10-22 17:10:47 +000027namespace llvm {
Clement Courbetac74acd2018-04-04 11:37:06 +000028namespace exegesis {
29
30bool operator==(const BenchmarkMeasure &A, const BenchmarkMeasure &B) {
Clement Courbet684a5f62018-09-26 08:37:21 +000031 return std::tie(A.Key, A.PerInstructionValue, A.PerSnippetValue) ==
32 std::tie(B.Key, B.PerInstructionValue, B.PerSnippetValue);
Clement Courbetac74acd2018-04-04 11:37:06 +000033}
34
Clement Courbetd422d3a2019-10-09 11:29:21 +000035static std::string Dump(const MCInst &McInst) {
Guillaume Chatelet083a0c162018-06-07 07:40:40 +000036 std::string Buffer;
Clement Courbetd422d3a2019-10-09 11:29:21 +000037 raw_string_ostream OS(Buffer);
Guillaume Chatelet083a0c162018-06-07 07:40:40 +000038 McInst.print(OS);
39 return Buffer;
40}
41
Clement Courbet53d35d22018-06-05 10:56:19 +000042MATCHER(EqMCInst, "") {
Guillaume Chatelet083a0c162018-06-07 07:40:40 +000043 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;
Clement Courbet53d35d22018-06-05 10:56:19 +000050}
51
Clement Courbetac74acd2018-04-04 11:37:06 +000052namespace {
53
54TEST(BenchmarkResultTest, WriteToAndReadFromDisk) {
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000055 LLVMInitializeX86TargetInfo();
56 LLVMInitializeX86Target();
57 LLVMInitializeX86TargetMC();
58
59 // Read benchmarks.
Guillaume Chateleteece4052018-09-25 13:18:10 +000060 const LLVMState State("x86_64-unknown-linux", "haswell");
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000061
Clement Courbetd422d3a2019-10-09 11:29:21 +000062 ExitOnError ExitOnErr;
Clement Courbet53d35d22018-06-05 10:56:19 +000063
Clement Courbetac74acd2018-04-04 11:37:06 +000064 InstructionBenchmark ToDisk;
65
Clement Courbetd422d3a2019-10-09 11:29:21 +000066 ToDisk.Key.Instructions.push_back(MCInstBuilder(X86::XOR32rr)
67 .addReg(X86::AL)
68 .addReg(X86::AH)
Guillaume Chatelet083a0c162018-06-07 07:40:40 +000069 .addImm(123)
70 .addFPImm(0.5));
Clement Courbeta66bfaa42018-05-15 13:07:05 +000071 ToDisk.Key.Config = "config";
Guillaume Chatelet345fae52018-09-25 15:15:54 +000072 ToDisk.Key.RegisterInitialValues = {
Clement Courbetd422d3a2019-10-09 11:29:21 +000073 RegisterValue{X86::AL, APInt(8, "-1", 10)},
74 RegisterValue{X86::AH, APInt(8, "123", 10)}};
Clement Courbet62b34fa2018-06-06 09:42:36 +000075 ToDisk.Mode = InstructionBenchmark::Latency;
Clement Courbetac74acd2018-04-04 11:37:06 +000076 ToDisk.CpuName = "cpu_name";
77 ToDisk.LLVMTriple = "llvm_triple";
78 ToDisk.NumRepetitions = 1;
Clement Courbet28d4f852018-09-26 13:35:10 +000079 ToDisk.Measurements.push_back(BenchmarkMeasure{"a", 1, 1});
80 ToDisk.Measurements.push_back(BenchmarkMeasure{"b", 2, 2});
Clement Courbetac74acd2018-04-04 11:37:06 +000081 ToDisk.Error = "error";
Clement Courbeta66bfaa42018-05-15 13:07:05 +000082 ToDisk.Info = "info";
Clement Courbetac74acd2018-04-04 11:37:06 +000083
Clement Courbetd422d3a2019-10-09 11:29:21 +000084 SmallString<64> Filename;
Clement Courbet308f8b72018-04-13 13:37:07 +000085 std::error_code EC;
Clement Courbetd422d3a2019-10-09 11:29:21 +000086 EC = sys::fs::createUniqueDirectory("BenchmarkResultTestDir", Filename);
Clement Courbet308f8b72018-04-13 13:37:07 +000087 ASSERT_FALSE(EC);
Clement Courbetd422d3a2019-10-09 11:29:21 +000088 sys::path::append(Filename, "data.yaml");
89 errs() << Filename << "-------\n";
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000090 ExitOnErr(ToDisk.writeYaml(State, Filename));
Clement Courbetac74acd2018-04-04 11:37:06 +000091
92 {
Clement Courbet7b7c27a2018-05-14 09:01:22 +000093 // One-element version.
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000094 const auto FromDisk =
Guillaume Chatelet55ad0872018-09-25 12:18:08 +000095 ExitOnErr(InstructionBenchmark::readYaml(State, Filename));
Clement Courbetac74acd2018-04-04 11:37:06 +000096
Clement Courbet53d35d22018-06-05 10:56:19 +000097 EXPECT_THAT(FromDisk.Key.Instructions,
98 Pointwise(EqMCInst(), ToDisk.Key.Instructions));
Clement Courbeta66bfaa42018-05-15 13:07:05 +000099 EXPECT_EQ(FromDisk.Key.Config, ToDisk.Key.Config);
Clement Courbet62b34fa2018-06-06 09:42:36 +0000100 EXPECT_EQ(FromDisk.Mode, ToDisk.Mode);
Clement Courbetac74acd2018-04-04 11:37:06 +0000101 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);
Clement Courbeta66bfaa42018-05-15 13:07:05 +0000106 EXPECT_EQ(FromDisk.Info, ToDisk.Info);
Clement Courbetac74acd2018-04-04 11:37:06 +0000107 }
Clement Courbet7b7c27a2018-05-14 09:01:22 +0000108 {
109 // Vector version.
Clement Courbet53d35d22018-06-05 10:56:19 +0000110 const auto FromDiskVector =
Guillaume Chatelet55ad0872018-09-25 12:18:08 +0000111 ExitOnErr(InstructionBenchmark::readYamls(State, Filename));
Clement Courbet4c0fb1d2018-05-14 12:00:35 +0000112 ASSERT_EQ(FromDiskVector.size(), size_t{1});
Clement Courbet7b7c27a2018-05-14 09:01:22 +0000113 const auto FromDisk = FromDiskVector[0];
Clement Courbet53d35d22018-06-05 10:56:19 +0000114 EXPECT_THAT(FromDisk.Key.Instructions,
115 Pointwise(EqMCInst(), ToDisk.Key.Instructions));
Clement Courbeta66bfaa42018-05-15 13:07:05 +0000116 EXPECT_EQ(FromDisk.Key.Config, ToDisk.Key.Config);
Clement Courbet62b34fa2018-06-06 09:42:36 +0000117 EXPECT_EQ(FromDisk.Mode, ToDisk.Mode);
Clement Courbet7b7c27a2018-05-14 09:01:22 +0000118 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);
Clement Courbeta66bfaa42018-05-15 13:07:05 +0000123 EXPECT_EQ(FromDisk.Info, ToDisk.Info);
Clement Courbet7b7c27a2018-05-14 09:01:22 +0000124 }
Clement Courbetac74acd2018-04-04 11:37:06 +0000125}
126
Clement Courbet684a5f62018-09-26 08:37:21 +0000127TEST(BenchmarkResultTest, PerInstructionStats) {
128 PerInstructionStats Stats;
Clement Courbet28d4f852018-09-26 13:35:10 +0000129 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});
Clement Courbetae8ae5dc2018-05-24 12:41:02 +0000133 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
136}
Clement Courbetac74acd2018-04-04 11:37:06 +0000137} // namespace
138} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000139} // namespace llvm