[llvm] [CodeGenPrepare] Fix crash in tryUnmergingGEPsAcrossIndirectBr with asm goto (PR #201443)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 4 02:26:45 PDT 2026
https://github.com/hesam-oxe updated https://github.com/llvm/llvm-project/pull/201443
>From 21880b8a8f66b7b6815fa0ed66ee2c252932bf94 Mon Sep 17 00:00:00 2001
From: hesam-oxe <chngyzkhanwhsht at gmail.com>
Date: Wed, 3 Jun 2026 23:31:55 +0330
Subject: [PATCH 1/2] fix: guard getTerminator() in
tryUnmergingGEPsAcrossIndirectBr for asm goto
asm goto can create blocks without terminators during CodeGenPrepare. Check hasTerminator() before accessing getTerminator() to avoid assertion failure. Fixes #201252
---
llvm/lib/CodeGen/CodeGenPrepare.cpp | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index aa14d2586a534..2ca24702d4e4e 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -8732,6 +8732,11 @@ static bool GEPSequentialConstIndexed(GetElementPtrInst *GEP) {
static bool tryUnmergingGEPsAcrossIndirectBr(GetElementPtrInst *GEPI,
const TargetTransformInfo *TTI) {
BasicBlock *SrcBlock = GEPI->getParent();
+ // SrcBlock may not be well-formed yet (e.g., asm goto can create blocks
+ // without terminators during CodeGenPrepare). Bail out early to avoid
+ // crashing in getTerminator().
+ if (SrcBlock->empty() || !SrcBlock->hasTerminator())
+ return false;
// Check that SrcBlock ends with an IndirectBr. If not, give up. The common
// (non-IndirectBr) cases exit early here.
if (!isa<IndirectBrInst>(SrcBlock->getTerminator()))
>From 037ce9a8c83e4af49981beb20a3bd7d57a4a2b67 Mon Sep 17 00:00:00 2001
From: hesam-oxe <chngyzkhanwhsht at gmail.com>
Date: Thu, 4 Jun 2026 12:56:26 +0330
Subject: [PATCH 2/2] test: add regression test for asm goto crash in
CodeGenPrepare
---
llvm/test/CodeGen/RISCV/asm-goto-cgp-crash.ll | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
create mode 100644 llvm/test/CodeGen/RISCV/asm-goto-cgp-crash.ll
diff --git a/llvm/test/CodeGen/RISCV/asm-goto-cgp-crash.ll b/llvm/test/CodeGen/RISCV/asm-goto-cgp-crash.ll
new file mode 100644
index 0000000000000..b649bd583d8cf
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/asm-goto-cgp-crash.ll
@@ -0,0 +1,18 @@
+; RUN: llc -O1 -mtriple=riscv64 -filetype=null < %s
+; REQUIRES: riscv-registered-target
+; Test that CodeGenPrepare doesn't crash with asm goto
+
+define void @test_asm_goto_crash(i32 %x) {
+entry:
+ switch i32 %x, label %default [
+ i32 0, label %indirect
+ ]
+indirect:
+ %target = phi ptr [ label %default, label %entry ]
+ callbr void asm sideeffect "j ${0:l}", "X"(ptr blockaddress(@test_asm_goto_crash, %default))
+ to label %normal [label %default]
+normal:
+ ret void
+default:
+ ret void
+}
\ No newline at end of file
More information about the llvm-commits
mailing list