[CodeGen] Fix ShrinkWrap crash when FindIDom receives empty predecessor/successor list#198995
Merged
Michael-Chen-NJU merged 2 commits intoJun 3, 2026
Conversation
|
@llvm/pr-subscribers-backend-risc-v Author: 陈子昂 (Michael-Chen-NJU) ChangesWhen using Fix by returning nullptr from FindIDom when the block list is empty, which signals the caller to stop searching and give up the optimization. Fixes #166759 2 Files Affected:
diff --git a/llvm/lib/CodeGen/ShrinkWrap.cpp b/llvm/lib/CodeGen/ShrinkWrap.cpp
index ff71bb07dab05..faa93f5abac3d 100644
--- a/llvm/lib/CodeGen/ShrinkWrap.cpp
+++ b/llvm/lib/CodeGen/ShrinkWrap.cpp
@@ -384,6 +384,8 @@ bool ShrinkWrapImpl::useOrDefCSROrFI(const MachineInstr &MI, RegScavenger *RS,
template <typename ListOfBBs, typename DominanceAnalysis>
static MachineBasicBlock *FindIDom(MachineBasicBlock &Block, ListOfBBs BBs,
DominanceAnalysis &Dom, bool Strict = true) {
+ if (BBs.begin() == BBs.end())
+ return nullptr;
MachineBasicBlock *IDom = Dom.findNearestCommonDominator(iterator_range(BBs));
if (Strict && IDom == &Block)
return nullptr;
diff --git a/llvm/test/CodeGen/RISCV/shrinkwrap-save-restore-fixed-reg.ll b/llvm/test/CodeGen/RISCV/shrinkwrap-save-restore-fixed-reg.ll
new file mode 100644
index 0000000000000..2f51014c36742
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/shrinkwrap-save-restore-fixed-reg.ll
@@ -0,0 +1,38 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=riscv64 -mattr=+save-restore,+reserve-x5 -verify-machineinstrs < %s \
+; RUN: | FileCheck %s
+
+; This test verifies that the shrink-wrap pass does not crash when the save
+; point reaches the entry block (which has no predecessors) while trying to find
+; a cheaper save point via FindIDom.
+
+@a = global i32 0, align 4
+
+define signext i32 @shrinkwrap_entry_no_preds() {
+; CHECK-LABEL: shrinkwrap_entry_no_preds:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: call t0, __riscv_save_0
+; CHECK-NEXT: .cfi_def_cfa_offset 16
+; CHECK-NEXT: .cfi_offset ra, -8
+; CHECK-NEXT: lui a0, %hi(a)
+; CHECK-NEXT: lw a0, %lo(a)(a0)
+; CHECK-NEXT: beqz a0, .LBB0_2
+; CHECK-NEXT: # %bb.1: # %if.then
+; CHECK-NEXT: call b
+; CHECK-NEXT: .LBB0_2: # %if.end
+; CHECK-NEXT: li a0, 0
+; CHECK-NEXT: tail __riscv_restore_0
+entry:
+ %0 = load i32, ptr @a, align 4
+ %tobool.not = icmp eq i32 %0, 0
+ br i1 %tobool.not, label %if.end, label %if.then
+
+if.then:
+ tail call void @b()
+ br label %if.end
+
+if.end:
+ ret i32 0
+}
+
+declare void @b()
|
Contributor
Author
|
ping. |
enoskova-sc
reviewed
Jun 1, 2026
…or/successor list When using -msave-restore with -ffixed-x5 on RISC-V, canUseAsPrologue returns false for all blocks because the save-restore prologue requires t0 (x5) which is reserved. This causes the shrink-wrap loop to keep searching for a valid save point, eventually reaching the entry block. On the next iteration, it calls FindIDom with the entry block's empty predecessor list, triggering an assertion in findNearestCommonDominator. Fix by returning nullptr from FindIDom when the block list is empty, which signals the caller to stop searching and give up the optimization. Fixes llvm#166759
fd37907 to
1f475b8
Compare
🪟 Windows x64 Test Results
✅ The build succeeded and all tests passed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When using
-msave-restorewith-ffixed-x5on RISC-V,canUseAsProloguereturns false for all blocks because the save-restore prologue requires t0 (x5) which is reserved. This causes the shrink-wrap loop to keep searching for a valid save point, eventually reaching the entry block. On the next iteration, it calls FindIDom with the entry block's empty predecessor list, triggering an assertion in findNearestCommonDominator.Fix by returning nullptr from FindIDom when the block list is empty, which signals the caller to stop searching and give up the optimization.
Fixes #166759