diff --git a/llvm/unittests/Transforms/Utils/CloningTest.cpp b/llvm/unittests/Transforms/Utils/CloningTest.cpp index 8f6ec12ec77aa..09b32bf09df0e 100644 --- a/llvm/unittests/Transforms/Utils/CloningTest.cpp +++ b/llvm/unittests/Transforms/Utils/CloningTest.cpp @@ -1004,6 +1004,16 @@ class CloneModule : public ::testing::Test { Function::Create(FuncType, GlobalValue::ExternalLinkage, "g", OldM); G->addMetadata(LLVMContext::MD_type, *MDNode::get(C, {})); + auto *NonEntryBlock = BasicBlock::Create(C, "", F); + IBuilder.SetInsertPoint(NonEntryBlock); + IBuilder.CreateRetVoid(); + + // Create a global that contains the block address in its initializer. + auto *BlockAddress = BlockAddress::get(NonEntryBlock); + new GlobalVariable(*OldM, BlockAddress->getType(), /*isConstant=*/true, + GlobalVariable::ExternalLinkage, BlockAddress, + "blockaddr"); + // Finalize the debug info DBuilder.finalize(); } @@ -1266,4 +1276,13 @@ TEST_F(CloneInstruction, cloneKeyInstructions) { #undef EXPECT_ATOM } +// Checks that block addresses in global initializers are properly cloned. +TEST_F(CloneModule, GlobalWithBlockAddressesInitializer) { + auto *OriginalBa = cast( + OldM->getGlobalVariable("blockaddr")->getInitializer()); + auto *ClonedBa = cast( + NewM->getGlobalVariable("blockaddr")->getInitializer()); + ASSERT_NE(OriginalBa->getBasicBlock(), ClonedBa->getBasicBlock()); +} + } // namespace