|
| 1 | +//===- MachineStableHashTest.cpp ------------------------------------------===// |
| 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 "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" |
| 20 | + |
| 21 | +using namespace llvm; |
| 22 | + |
| 23 | +class MachineStableHashTest : public testing::Test { |
| 24 | +public: |
| 25 | + MachineStableHashTest() {} |
| 26 | + |
| 27 | +protected: |
| 28 | + LLVMContext Context; |
| 29 | + std::unique_ptr<Module> M; |
| 30 | + std::unique_ptr<MIRParser> MIR; |
| 31 | + |
| 32 | + static void SetUpTestCase() { |
| 33 | + InitializeAllTargetInfos(); |
| 34 | + InitializeAllTargets(); |
| 35 | + InitializeAllTargetMCs(); |
| 36 | + } |
| 37 | + |
| 38 | + void SetUp() override { M = std::make_unique<Module>("Dummy", Context); } |
| 39 | + |
| 40 | + std::unique_ptr<LLVMTargetMachine> |
| 41 | + createTargetMachine(std::string TT, StringRef CPU, StringRef FS) { |
| 42 | + std::string Error; |
| 43 | + const Target *T = TargetRegistry::lookupTarget(TT, Error); |
| 44 | + if (!T) |
| 45 | + return nullptr; |
| 46 | + TargetOptions Options; |
| 47 | + return std::unique_ptr<LLVMTargetMachine>( |
| 48 | + static_cast<LLVMTargetMachine *>(T->createTargetMachine( |
| 49 | + TT, CPU, FS, Options, std::nullopt, std::nullopt))); |
| 50 | + } |
| 51 | + |
| 52 | + std::unique_ptr<Module> parseMIR(const TargetMachine &TM, StringRef MIRCode, |
| 53 | + MachineModuleInfo &MMI) { |
| 54 | + SMDiagnostic Diagnostic; |
| 55 | + std::unique_ptr<MemoryBuffer> MBuffer = MemoryBuffer::getMemBuffer(MIRCode); |
| 56 | + MIR = createMIRParser(std::move(MBuffer), Context); |
| 57 | + if (!MIR) |
| 58 | + return nullptr; |
| 59 | + |
| 60 | + std::unique_ptr<Module> Mod = MIR->parseIRModule(); |
| 61 | + if (!Mod) |
| 62 | + return nullptr; |
| 63 | + |
| 64 | + Mod->setDataLayout(TM.createDataLayout()); |
| 65 | + |
| 66 | + if (MIR->parseMachineFunctions(*Mod, MMI)) { |
| 67 | + M.reset(); |
| 68 | + return nullptr; |
| 69 | + } |
| 70 | + |
| 71 | + return Mod; |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +TEST_F(MachineStableHashTest, StableGlobalName) { |
| 76 | + auto TM = createTargetMachine(("aarch64--"), "", ""); |
| 77 | + if (!TM) |
| 78 | + GTEST_SKIP(); |
| 79 | + StringRef MIRString = R"MIR( |
| 80 | +--- | |
| 81 | + define void @f1() { ret void } |
| 82 | + define void @f2() { ret void } |
| 83 | + define void @f3() { ret void } |
| 84 | + define void @f4() { ret void } |
| 85 | + declare void @goo() |
| 86 | + declare void @goo.llvm.123() |
| 87 | + declare void @goo.__uniq.456() |
| 88 | + declare void @goo.invalid.789() |
| 89 | +... |
| 90 | +--- |
| 91 | +name: f1 |
| 92 | +alignment: 16 |
| 93 | +tracksRegLiveness: true |
| 94 | +frameInfo: |
| 95 | + maxAlignment: 16 |
| 96 | +machineFunctionInfo: {} |
| 97 | +body: | |
| 98 | + bb.0: |
| 99 | + liveins: $lr |
| 100 | + BL @goo |
| 101 | + RET undef $lr |
| 102 | +
|
| 103 | +... |
| 104 | +--- |
| 105 | +name: f2 |
| 106 | +body: | |
| 107 | + bb.0: |
| 108 | + liveins: $lr |
| 109 | + BL @goo.llvm.123 |
| 110 | + RET undef $lr |
| 111 | +... |
| 112 | +--- |
| 113 | +name: f3 |
| 114 | +body: | |
| 115 | + bb.0: |
| 116 | + liveins: $lr |
| 117 | + BL @goo.__uniq.456 |
| 118 | + RET undef $lr |
| 119 | +... |
| 120 | +--- |
| 121 | +name: f4 |
| 122 | +body: | |
| 123 | + bb.0: |
| 124 | + liveins: $lr |
| 125 | + BL @goo.invalid.789 |
| 126 | + RET undef $lr |
| 127 | +... |
| 128 | +)MIR"; |
| 129 | + MachineModuleInfo MMI(TM.get()); |
| 130 | + M = parseMIR(*TM, MIRString, MMI); |
| 131 | + ASSERT_TRUE(M); |
| 132 | + auto *MF1 = MMI.getMachineFunction(*M->getFunction("f1")); |
| 133 | + auto *MF2 = MMI.getMachineFunction(*M->getFunction("f2")); |
| 134 | + auto *MF3 = MMI.getMachineFunction(*M->getFunction("f3")); |
| 135 | + auto *MF4 = MMI.getMachineFunction(*M->getFunction("f4")); |
| 136 | + |
| 137 | + EXPECT_EQ(stableHashValue(*MF1), stableHashValue(*MF2)) |
| 138 | + << "Expect the suffix, `.llvm.{number}` to be ignored."; |
| 139 | + EXPECT_EQ(stableHashValue(*MF1), stableHashValue(*MF3)) |
| 140 | + << "Expect the suffix, `.__uniq.{number}` to be ignored."; |
| 141 | + // Do not ignore `.invalid.{number}`. |
| 142 | + EXPECT_NE(stableHashValue(*MF1), stableHashValue(*MF4)); |
| 143 | +} |
0 commit comments