1 //===- MachineStableHashTest.cpp ------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/CodeGen/MachineStableHash.h"
10 #include "llvm/CodeGen/MIRParser/MIRParser.h"
11 #include "llvm/CodeGen/MachineFunction.h"
12 #include "llvm/CodeGen/MachineModuleInfo.h"
13 #include "llvm/FileCheck/FileCheck.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/MC/TargetRegistry.h"
16 #include "llvm/Support/SourceMgr.h"
17 #include "llvm/Support/TargetSelect.h"
18 #include "llvm/Target/TargetMachine.h"
19 #include "gtest/gtest.h"
23 class MachineStableHashTest
: public testing::Test
{
25 MachineStableHashTest() {}
29 std::unique_ptr
<Module
> M
;
30 std::unique_ptr
<MIRParser
> MIR
;
32 static void SetUpTestCase() {
33 InitializeAllTargetInfos();
34 InitializeAllTargets();
35 InitializeAllTargetMCs();
38 void SetUp() override
{ M
= std::make_unique
<Module
>("Dummy", Context
); }
40 std::unique_ptr
<TargetMachine
>
41 createTargetMachine(std::string TT
, StringRef CPU
, StringRef FS
) {
43 const Target
*T
= TargetRegistry::lookupTarget(TT
, Error
);
46 TargetOptions Options
;
47 return std::unique_ptr
<TargetMachine
>(T
->createTargetMachine(
48 TT
, CPU
, FS
, Options
, std::nullopt
, std::nullopt
));
51 std::unique_ptr
<Module
> parseMIR(const TargetMachine
&TM
, StringRef MIRCode
,
52 MachineModuleInfo
&MMI
) {
53 SMDiagnostic Diagnostic
;
54 std::unique_ptr
<MemoryBuffer
> MBuffer
= MemoryBuffer::getMemBuffer(MIRCode
);
55 MIR
= createMIRParser(std::move(MBuffer
), Context
);
59 std::unique_ptr
<Module
> Mod
= MIR
->parseIRModule();
63 Mod
->setDataLayout(TM
.createDataLayout());
65 if (MIR
->parseMachineFunctions(*Mod
, MMI
)) {
74 TEST_F(MachineStableHashTest
, StableGlobalName
) {
75 auto TM
= createTargetMachine(("aarch64--"), "", "");
78 StringRef MIRString
= R
"MIR(
80 define void @f1() { ret void }
81 define void @f2() { ret void }
82 define void @f3() { ret void }
83 define void @f4() { ret void }
85 declare void @goo.llvm.123()
86 declare void @goo.__uniq.456()
87 declare void @goo.invalid.789()
92 tracksRegLiveness: true
95 machineFunctionInfo: {}
128 MachineModuleInfo
MMI(TM
.get());
129 M
= parseMIR(*TM
, MIRString
, MMI
);
131 auto *MF1
= MMI
.getMachineFunction(*M
->getFunction("f1"));
132 auto *MF2
= MMI
.getMachineFunction(*M
->getFunction("f2"));
133 auto *MF3
= MMI
.getMachineFunction(*M
->getFunction("f3"));
134 auto *MF4
= MMI
.getMachineFunction(*M
->getFunction("f4"));
136 EXPECT_EQ(stableHashValue(*MF1
), stableHashValue(*MF2
))
137 << "Expect the suffix, `.llvm.{number}` to be ignored.";
138 EXPECT_EQ(stableHashValue(*MF1
), stableHashValue(*MF3
))
139 << "Expect the suffix, `.__uniq.{number}` to be ignored.";
140 // Do not ignore `.invalid.{number}`.
141 EXPECT_NE(stableHashValue(*MF1
), stableHashValue(*MF4
));
144 TEST_F(MachineStableHashTest
, ContentName
) {
145 auto TM
= createTargetMachine(("aarch64--"), "", "");
148 StringRef MIRString
= R
"MIR(
150 define void @f1() { ret void }
151 define void @f2() { ret void }
152 define void @f3() { ret void }
153 define void @f4() { ret void }
155 declare void @goo.content.123()
156 declare void @zoo.content.123()
157 declare void @goo.content.456()
162 tracksRegLiveness: true
165 machineFunctionInfo: {}
197 MachineModuleInfo
MMI(TM
.get());
198 M
= parseMIR(*TM
, MIRString
, MMI
);
200 auto *MF1
= MMI
.getMachineFunction(*M
->getFunction("f1"));
201 auto *MF2
= MMI
.getMachineFunction(*M
->getFunction("f2"));
202 auto *MF3
= MMI
.getMachineFunction(*M
->getFunction("f3"));
203 auto *MF4
= MMI
.getMachineFunction(*M
->getFunction("f4"));
205 // Do not ignore `.content.{number}`.
206 EXPECT_NE(stableHashValue(*MF1
), stableHashValue(*MF2
));
207 EXPECT_EQ(stableHashValue(*MF2
), stableHashValue(*MF3
))
208 << "Expect the same hash for the same suffix, `.content.{number}`";
209 // Different suffixes should result in different hashes.
210 EXPECT_NE(stableHashValue(*MF2
), stableHashValue(*MF4
));
211 EXPECT_NE(stableHashValue(*MF3
), stableHashValue(*MF4
));