[llvm] [LTO] Fix assertion failure in BlockAddress::get during inlining (PR #176044)
Aniket Singh via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 14 14:14:19 PST 2026
https://github.com/Aniketsingh54 created https://github.com/llvm/llvm-project/pull/176044
When a function is inlined, its BasicBlocks are cloned. For a short period during cloning, the new block is created but not yet inserted into the new parent function (it is 'floating').
If a BlockAddress is requested for this block during this window -- for example, when remapping 'asm goto' operands -- BlockAddress::get previously asserted that 'BB->getParent() != nullptr'.
This patch relaxes the assertion in BlockAddress::get. If the block has no parent (is floating), we trust that the 'Function *F' passed to the method is the correct intended parent.
Added a regression test in llvm/test/Linker/blockaddress-inlining.ll.
Fixes #175516
>From 174de7d1c7b73cfa9168b1006abd66c34133841a Mon Sep 17 00:00:00 2001
From: Aniket Singh <amiket.singh.3200.00 at gmail.com>
Date: Thu, 15 Jan 2026 03:39:15 +0530
Subject: [PATCH] [LTO] Fix assertion failure in BlockAddress::get during
inlining
When a function is inlined, its BasicBlocks are cloned. For a short period during cloning, the new block is created but not yet inserted into the new parent function (it is 'floating').
If a BlockAddress is requested for this block during this window -- for example, when remapping 'asm goto' operands -- BlockAddress::get previously asserted that 'BB->getParent() != nullptr'.
This patch relaxes the assertion in BlockAddress::get. If the block has no parent (is floating), we trust that the 'Function *F' passed to the method is the correct intended parent.
Added a regression test in llvm/test/Linker/blockaddress-inlining.ll.
Fixes #175516
---
llvm/lib/IR/Constants.cpp | 5 ++--
llvm/test/Linker/blockaddress-inlining.ll | 29 +++++++++++++++++++++++
2 files changed, 32 insertions(+), 2 deletions(-)
create mode 100644 llvm/test/Linker/blockaddress-inlining.ll
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index 76152fe91a532..f2055ce57159a 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -1929,8 +1929,9 @@ BlockAddress *BlockAddress::get(BasicBlock *BB) {
}
BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
- assert(BB->getParent() == F && "Block not part of specified function");
- return get(BB->getParent()->getType(), BB);
+ if (BB->getParent())
+ assert(BB->getParent() == F && "Block not part of specified function");
+ return get(F->getType(), BB);
}
BlockAddress::BlockAddress(Type *Ty, BasicBlock *BB)
diff --git a/llvm/test/Linker/blockaddress-inlining.ll b/llvm/test/Linker/blockaddress-inlining.ll
new file mode 100644
index 0000000000000..613a283b587b5
--- /dev/null
+++ b/llvm/test/Linker/blockaddress-inlining.ll
@@ -0,0 +1,29 @@
+; RUN: opt -S -o - < %s -passes='always-inline' | FileCheck %s
+
+; -------------------------------------------------------------------------
+; Regression Test for Issue #175516
+; Verify that inlining an asm goto with a BlockAddress operand does not
+; crash. The BlockAddress should be updated to refer to the new function.
+; -------------------------------------------------------------------------
+
+define void @asm_goto_repro(i32 %b) alwaysinline {
+entry:
+ ; The crash happened here during cloning.
+ ; "r,!i" constraints: input register "r" and label "!i".
+ callbr void asm sideeffect "", "r,!i"(ptr blockaddress(@asm_goto_repro, %label_c))
+ to label %fallthrough [label %label_c]
+
+fallthrough:
+ br label %label_c
+
+label_c:
+ ret void
+}
+
+define void @test_pr175516() {
+; CHECK-LABEL: @test_pr175516
+; CHECK: callbr void asm sideeffect "", "r,!i"(ptr blockaddress(@test_pr175516, %asm_goto_repro.exit))
+entry:
+ call void @asm_goto_repro(i32 1)
+ ret void
+}
\ No newline at end of file
More information about the llvm-commits
mailing list