blob: 76ca39138c4a91fb97485770b762c5d9168f7888 [file] [log] [blame]
Jinsong Ji56c74cf2018-11-20 14:41:59 +00001//===-- AnalysisTest.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
Jinsong Ji56c74cf2018-11-20 14:41:59 +00006//
7//===----------------------------------------------------------------------===//
8
Clement Courbetdf79e792018-06-01 14:18:02 +00009#include "Analysis.h"
10
11#include <cassert>
12#include <memory>
13
14#include "llvm/Support/TargetRegistry.h"
15#include "llvm/Support/TargetSelect.h"
16#include "gmock/gmock.h"
17#include "gtest/gtest.h"
18
Fangrui Song32401af2018-10-22 17:10:47 +000019namespace llvm {
Clement Courbetdf79e792018-06-01 14:18:02 +000020namespace exegesis {
21namespace {
22
23using testing::Pair;
24using testing::UnorderedElementsAre;
25
26class AnalysisTest : public ::testing::Test {
27protected:
28 AnalysisTest() {
29 const std::string TT = "x86_64-unknown-linux";
30 std::string error;
31 const llvm::Target *const TheTarget =
32 llvm::TargetRegistry::lookupTarget(TT, error);
33 if (!TheTarget) {
34 llvm::errs() << error << "\n";
35 return;
36 }
37 STI.reset(TheTarget->createMCSubtargetInfo(TT, "haswell", ""));
38
Jinsong Ji56c74cf2018-11-20 14:41:59 +000039 // Compute the ProxResIdx of ports uses in tests.
Clement Courbetdf79e792018-06-01 14:18:02 +000040 const auto &SM = STI->getSchedModel();
41 for (unsigned I = 0, E = SM.getNumProcResourceKinds(); I < E; ++I) {
42 const std::string Name = SM.getProcResource(I)->Name;
43 if (Name == "HWPort0") {
44 P0Idx = I;
45 } else if (Name == "HWPort1") {
46 P1Idx = I;
47 } else if (Name == "HWPort5") {
48 P5Idx = I;
49 } else if (Name == "HWPort6") {
50 P6Idx = I;
51 } else if (Name == "HWPort05") {
52 P05Idx = I;
53 } else if (Name == "HWPort0156") {
54 P0156Idx = I;
55 }
56 }
57 EXPECT_NE(P0Idx, 0);
58 EXPECT_NE(P1Idx, 0);
59 EXPECT_NE(P5Idx, 0);
60 EXPECT_NE(P6Idx, 0);
61 EXPECT_NE(P05Idx, 0);
62 EXPECT_NE(P0156Idx, 0);
63 }
64
65 static void SetUpTestCase() {
66 LLVMInitializeX86TargetInfo();
67 LLVMInitializeX86Target();
68 LLVMInitializeX86TargetMC();
69 }
70
71protected:
72 std::unique_ptr<const llvm::MCSubtargetInfo> STI;
73 uint16_t P0Idx = 0;
74 uint16_t P1Idx = 0;
75 uint16_t P5Idx = 0;
76 uint16_t P6Idx = 0;
77 uint16_t P05Idx = 0;
78 uint16_t P0156Idx = 0;
79};
80
81TEST_F(AnalysisTest, ComputeIdealizedProcResPressure_2P0) {
82 const auto Pressure =
83 computeIdealizedProcResPressure(STI->getSchedModel(), {{P0Idx, 2}});
84 EXPECT_THAT(Pressure, UnorderedElementsAre(Pair(P0Idx, 2.0)));
85}
86
87TEST_F(AnalysisTest, ComputeIdealizedProcResPressure_2P05) {
88 const auto Pressure =
89 computeIdealizedProcResPressure(STI->getSchedModel(), {{P05Idx, 2}});
90 EXPECT_THAT(Pressure,
91 UnorderedElementsAre(Pair(P0Idx, 1.0), Pair(P5Idx, 1.0)));
92}
93
94TEST_F(AnalysisTest, ComputeIdealizedProcResPressure_2P05_2P0156) {
95 const auto Pressure = computeIdealizedProcResPressure(
96 STI->getSchedModel(), {{P05Idx, 2}, {P0156Idx, 2}});
97 EXPECT_THAT(Pressure,
98 UnorderedElementsAre(Pair(P0Idx, 1.0), Pair(P1Idx, 1.0),
99 Pair(P5Idx, 1.0), Pair(P6Idx, 1.0)));
100}
101
102TEST_F(AnalysisTest, ComputeIdealizedProcResPressure_1P1_1P05_2P0156) {
103 const auto Pressure = computeIdealizedProcResPressure(
104 STI->getSchedModel(), {{P1Idx, 1}, {P05Idx, 1}, {P0156Idx, 2}});
105 EXPECT_THAT(Pressure,
106 UnorderedElementsAre(Pair(P0Idx, 1.0), Pair(P1Idx, 1.0),
107 Pair(P5Idx, 1.0), Pair(P6Idx, 1.0)));
108}
109
110} // namespace
111} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000112} // namespace llvm