[llvm] [CodeGen] Avoid sinking EH pad blocks (PR #224812)
Trung Nguyen via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 21 02:04:10 PDT 2026
https://github.com/trungnt2910 updated https://github.com/llvm/llvm-project/pull/224812
>From b8bc223d340acf92b57f17bdc6fe125d9ff0bc40 Mon Sep 17 00:00:00 2001
From: Trung Nguyen <git at trungnt2910.com>
Date: Sat, 19 Sep 2026 16:20:47 +1000
Subject: [PATCH] [CodeGen] Avoid sinking EH pad blocks
EH pad blocks should not be sinkable since the control flow comes from
the unwinder instead of the predecessor.
This exclusion also matches the behavior of other sinkers.
Fixes #224802.
---
llvm/lib/CodeGen/MachineSink.cpp | 2 +-
.../X86/postra-machine-sink-eh-pad.mir | 40 +++++++++++++++++++
2 files changed, 41 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/CodeGen/X86/postra-machine-sink-eh-pad.mir
diff --git a/llvm/lib/CodeGen/MachineSink.cpp b/llvm/lib/CodeGen/MachineSink.cpp
index cbd2598a47210..3bfa604ccbd7b 100644
--- a/llvm/lib/CodeGen/MachineSink.cpp
+++ b/llvm/lib/CodeGen/MachineSink.cpp
@@ -2249,7 +2249,7 @@ bool PostRAMachineSinkingImpl::tryToSinkCopy(MachineBasicBlock &CurBB,
// so that we can directly sink COPY instructions to the successor without
// adding any new block or branch instruction.
for (MachineBasicBlock *SI : CurBB.successors())
- if (!SI->livein_empty() && SI->pred_size() == 1)
+ if (!SI->livein_empty() && SI->pred_size() == 1 && !SI->isEHPad())
SinkableBBs.insert(SI);
if (SinkableBBs.empty())
diff --git a/llvm/test/CodeGen/X86/postra-machine-sink-eh-pad.mir b/llvm/test/CodeGen/X86/postra-machine-sink-eh-pad.mir
new file mode 100644
index 0000000000000..884f46b882fec
--- /dev/null
+++ b/llvm/test/CodeGen/X86/postra-machine-sink-eh-pad.mir
@@ -0,0 +1,40 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple=x86_64-unknown-linux-gnu -run-pass=postra-machine-sink -verify-machineinstrs -o - %s | FileCheck %s
+# RUN: llc -mtriple=x86_64-unknown-linux-gnu -passes=postra-machine-sink -o - %s | FileCheck %s
+
+# Regression test for llvm/llvm-project#224802.
+
+---
+name: no_sink_into_landing_pad
+tracksRegLiveness: true
+body: |
+ ; CHECK-LABEL: name: no_sink_into_landing_pad
+ ; CHECK: bb.0:
+ ; CHECK-NEXT: successors: %bb.1(0x40000000), %bb.2(0x40000000)
+ ; CHECK-NEXT: liveins: $rbx
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: renamable $rax = COPY killed renamable $rbx
+ ; CHECK-NEXT: JMP_1 %bb.1
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: bb.1:
+ ; CHECK-NEXT: RET 0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: bb.2 (landing-pad):
+ ; CHECK-NEXT: liveins: $rax
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: RET 0, $rax
+ bb.0:
+ successors: %bb.1, %bb.2
+ liveins: $rbx
+
+ renamable $rax = COPY killed renamable $rbx
+ JMP_1 %bb.1
+
+ bb.1:
+ RET 0
+
+ bb.2 (landing-pad):
+ liveins: $rax
+
+ RET 0, $rax
+...
More information about the llvm-commits
mailing list