[llvm-branch-commits] [llvm] release/22.x: [BranchFolding][WinEH] Do not remove EH pads (#176735) (PR #176888)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Jan 20 02:13:58 PST 2026
https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/176888
Backport 528bb2bedaa9e51c8078d41b977d266fa7d65ec7
Requested by: @nikic
>From a13f1190336515edd1f4e32dd320432116f6f20a Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Tue, 20 Jan 2026 11:05:15 +0100
Subject: [PATCH] [BranchFolding][WinEH] Do not remove EH pads (#176735)
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 https://github.com/llvm/llvm-project/issues/176421.
(cherry picked from commit 528bb2bedaa9e51c8078d41b977d266fa7d65ec7)
---
llvm/lib/CodeGen/BranchFolding.cpp | 3 +-
.../wineh-dangling-eh-pad-reference.ll | 34 +++++++++++++++++++
2 files changed, 36 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/CodeGen/AArch64/wineh-dangling-eh-pad-reference.ll
diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp
index 0b212fb0beb20..2bfb34739af7c 100644
--- a/llvm/lib/CodeGen/BranchFolding.cpp
+++ b/llvm/lib/CodeGen/BranchFolding.cpp
@@ -1250,7 +1250,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 at 4HA"
+; CHECK-LABEL: $stateUnwindMap$test:
+; CHECK: .word -1 // ToState
+; CHECK: .word "?dtor$1@?0?test at 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(...)
More information about the llvm-branch-commits
mailing list