[BranchFolding][WinEH] Do not remove EH pads#176735
Merged
Conversation
If branch folding remoes an EH pad, we're left with a dangling reference to it from the CxxUnwindMap. We could try to fix this up, but given that this should be a rare situation, just leave the dead EH pad blocks around.
Member
|
@llvm/pr-subscribers-backend-aarch64 Author: Nikita Popov (nikic) ChangesIf branch folding remoes an EH pad, we're left with a dangling reference to it from the CxxUnwindMap. We could try to fix this up, but given that this should be a rare situation, just leave the dead EH pad blocks around. Fixes #176421. 2 Files Affected:
diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp
index 9a12039291989..5a43f4e1ec993 100644
--- a/llvm/lib/CodeGen/BranchFolding.cpp
+++ b/llvm/lib/CodeGen/BranchFolding.cpp
@@ -1259,7 +1259,8 @@ bool BranchFolder::OptimizeBranches(MachineFunction &MF) {
MadeChange |= OptimizeBlock(&MBB);
// If it is dead, remove it.
- if (MBB.pred_empty() && !MBB.isMachineBlockAddressTaken()) {
+ if (MBB.pred_empty() && !MBB.isMachineBlockAddressTaken() &&
+ !MBB.isEHPad()) {
RemoveDeadBlock(&MBB);
MadeChange = true;
++NumDeadBlocks;
diff --git a/llvm/test/CodeGen/AArch64/wineh-dangling-eh-pad-reference.ll b/llvm/test/CodeGen/AArch64/wineh-dangling-eh-pad-reference.ll
new file mode 100644
index 0000000000000..e20d4ce3633d7
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/wineh-dangling-eh-pad-reference.ll
@@ -0,0 +1,34 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=aarch64-pc-windows-msvc < %s | FileCheck %s
+
+declare void @func()
+
+; Make sure that we do not end up with a dangling EH pad reference.
+
+define void @test(ptr %p) personality ptr @__CxxFrameHandler3 {
+; CHECK-LABEL: test:
+; CHECK: .seh_proc "?dtor$1@?0?test@4HA"
+; CHECK-LABEL: $stateUnwindMap$test:
+; CHECK: .word -1 // ToState
+; CHECK: .word "?dtor$1@?0?test@4HA"@IMGREL // Action
+
+ %v0 = load i32, ptr %p
+ %v1 = load i32, ptr %p
+ %xor = xor i32 %v0, %v1
+ %cmp = icmp eq i32 %xor, 0
+ br i1 %cmp, label %exit, label %bb
+
+bb:
+ invoke void @func()
+ to label %exit unwind label %unwind
+
+unwind:
+ %cp = cleanuppad within none []
+ store volatile i32 0, ptr %p
+ cleanupret from %cp unwind to caller
+
+exit:
+ ret void
+}
+
+declare i32 @__CxxFrameHandler3(...)
|
rnk
approved these changes
Jan 20, 2026
c-rhodes
pushed a commit
to llvmbot/llvm-project
that referenced
this pull request
Jan 26, 2026
If branch folding remoes an EH pad, we're left with a dangling reference to it from the CxxUnwindMap. We could try to fix this up, but given that this should be a rare situation, just leave the dead EH pad blocks around. Fixes llvm#176421. (cherry picked from commit 528bb2b)
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.
If branch folding remoes an EH pad, we're left with a dangling reference to it from the CxxUnwindMap. We could try to fix this up, but given that this should be a rare situation, just leave the dead EH pad blocks around.
Fixes #176421.