[llvm] [LLVM][Transforms] Add unit test for resolved cloning bug (PR #139223)

Christian Ulmann via llvm-commits llvm-commits at lists.llvm.org
Fri May 9 01:51:06 PDT 2025


https://github.com/Dinistro updated https://github.com/llvm/llvm-project/pull/139223

>From 6b02b3eb0235627353d91f96ec0aaf8b837a0bbc Mon Sep 17 00:00:00 2001
From: Christian Ulmann <christian.ulmann at nextsilicon.com>
Date: Fri, 9 May 2025 08:50:14 +0000
Subject: [PATCH] [LLVM][Transforms] Add unit test for resolved cloning bug

This commit introduces a new unit test that covers a block address
cloning bug. Specifically, this test covers the bug tracked in
http://github.com/llvm/llvm-project/issues/47769 which has been
resolved in the meantime.
---
 .../Transforms/Utils/CloningTest.cpp          | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

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<BlockAddress>(
+      OldM->getGlobalVariable("blockaddr")->getInitializer());
+  auto *ClonedBa = cast<BlockAddress>(
+      NewM->getGlobalVariable("blockaddr")->getInitializer());
+  ASSERT_NE(OriginalBa->getBasicBlock(), ClonedBa->getBasicBlock());
+}
+
 } // namespace



More information about the llvm-commits mailing list