[llvm] 66813ba - X86: Remove redundant SJLJ landing pad alignment in X86LFIRewritePass (#227295)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 30 14:56:17 PDT 2026
Author: Matt Arsenault
Date: 2026-09-30T23:56:06+02:00
New Revision: 66813bafefc24420a0a7f8fed0aef775f56c71a8
URL: https://github.com/llvm/llvm-project/commit/66813bafefc24420a0a7f8fed0aef775f56c71a8
DIFF: https://github.com/llvm/llvm-project/commit/66813bafefc24420a0a7f8fed0aef775f56c71a8.diff
LOG: X86: Remove redundant SJLJ landing pad alignment in X86LFIRewritePass (#227295)
X86LFIRewritePass separately scanned for blocks holding a call site's landing pad label.
With SJLJ exception handling the dispatch block reaches those blocks through an indirect
jump, and they are no longer marked as EH pads by the time this pass runs, so they would
otherwise be missed.
EmitSjLjDispatchBlock puts those blocks in a jump table, so they are already aligned as jump
table targets. Dropping it removes the use of the TargetOptions exception model field from
this in preparation for its removal.
The new test checks the landing pad alignment, which was previously untested.
Co-authored-by: Claude Opus 5 <noreply at anthropic.com>
Added:
llvm/test/CodeGen/X86/lfi-align-sjlj.ll
Modified:
llvm/lib/Target/X86/X86LFIRewritePass.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Target/X86/X86LFIRewritePass.cpp b/llvm/lib/Target/X86/X86LFIRewritePass.cpp
index 261efa209b253..f9245bdc1fb9c 100644
--- a/llvm/lib/Target/X86/X86LFIRewritePass.cpp
+++ b/llvm/lib/Target/X86/X86LFIRewritePass.cpp
@@ -17,7 +17,6 @@
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineJumpTableInfo.h"
#include "llvm/Support/Alignment.h"
-#include "llvm/Target/TargetMachine.h"
using namespace llvm;
@@ -43,21 +42,8 @@ static void alignToBundle(MachineBasicBlock &MBB) {
// Returns true if MBB may be reached by an indirect branch (does not include
// jump table targets).
-static bool isIndirectlyReachable(MachineFunction &MF,
- const MachineBasicBlock &MBB) {
- if (MBB.hasAddressTaken() || MBB.isEHPad())
- return true;
-
- // With SJLJ exception handling, the dispatch block jumps indirectly to the
- // block holding the call site's landing pad label, which is no longer marked
- // as an EH pad by that point.
- if (MF.getTarget().Options.ExceptionModel == ExceptionHandling::SjLj)
- for (const MachineInstr &MI : MBB)
- if (MI.isEHLabel() &&
- MF.hasCallSiteLandingPad(MI.getOperand(0).getMCSymbol()))
- return true;
-
- return false;
+static bool isIndirectlyReachable(const MachineBasicBlock &MBB) {
+ return MBB.hasAddressTaken() || MBB.isEHPad();
}
static void alignIndirectBranchTargets(MachineFunction &MF) {
@@ -66,14 +52,17 @@ static void alignIndirectBranchTargets(MachineFunction &MF) {
// Blocks that are the target of a jump table are not considered
// address-taken by LLVM, but they are still reached by an indirect branch.
+ // This also covers the SJLJ landing pads, which EmitSjLjDispatchBlock puts
+ // into a jump table.
if (const MachineJumpTableInfo *JTI = MF.getJumpTableInfo())
for (const MachineJumpTableEntry &JTE : JTI->getJumpTables())
for (MachineBasicBlock *MBB : JTE.MBBs)
alignToBundle(*MBB);
- for (MachineBasicBlock &MBB : MF)
- if (isIndirectlyReachable(MF, MBB))
+ for (MachineBasicBlock &MBB : MF) {
+ if (isIndirectlyReachable(MBB))
alignToBundle(MBB);
+ }
}
bool X86LFIRewriteLegacy::runOnMachineFunction(MachineFunction &MF) {
diff --git a/llvm/test/CodeGen/X86/lfi-align-sjlj.ll b/llvm/test/CodeGen/X86/lfi-align-sjlj.ll
new file mode 100644
index 0000000000000..96daf3dd14430
--- /dev/null
+++ b/llvm/test/CodeGen/X86/lfi-align-sjlj.ll
@@ -0,0 +1,66 @@
+; RUN: split-file %s %t
+; RUN: llc -mtriple=x86_64_lfi -exception-model=sjlj < %t/sjlj.ll | FileCheck -check-prefix=SJLJ %s
+; RUN: llc -mtriple=x86_64_lfi < %t/dwarf.ll | FileCheck -check-prefix=DWARF %s
+
+; The SJLJ dispatch block jumps indirectly to the landing pad through a jump
+; table, so the landing pad is bundle aligned as a jump table target. Other
+; exception models have no dispatch block, and the landing pad is only
+; aligned because it is an EH pad.
+
+;--- sjlj.ll
+; FIXME: SJLJ lowering still requires -exception-model=sjlj
+
+declare void @may_throw()
+declare i32 @__gxx_personality_sj0(...)
+
+define void @invoke_aligns_landing_pad() personality ptr @__gxx_personality_sj0 {
+; SJLJ-LABEL: invoke_aligns_landing_pad:
+; SJLJ: callq may_throw at PLT
+; SJLJ: .p2align 5
+; SJLJ-NEXT: .LBB0_3:
+; SJLJ: jmpq *(%rcx,%rax,8)
+; SJLJ-NEXT: .p2align 5
+; SJLJ-NEXT: .LBB0_2:
+; SJLJ-NEXT: .Ltmp2:
+; SJLJ: .LJTI0_0:
+; SJLJ-NEXT: .quad .LBB0_2
+entry:
+ invoke void @may_throw() to label %cont unwind label %lpad
+
+cont:
+ ret void
+
+lpad:
+ %l = landingpad { ptr, i32 } catch ptr null
+ ret void
+}
+
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"exception-model", !"sjlj"}
+
+;--- dwarf.ll
+
+declare void @may_throw()
+declare i32 @__gxx_personality_v0(...)
+
+define void @invoke_aligns_landing_pad() personality ptr @__gxx_personality_v0 {
+; DWARF-LABEL: invoke_aligns_landing_pad:
+; DWARF: callq may_throw at PLT
+; DWARF: .p2align 5
+; DWARF-NEXT: .LBB0_2:
+; DWARF: .Ltmp2:
+; DWARF-NOT: jmpq *
+; DWARF-NOT: .LJTI
+entry:
+ invoke void @may_throw() to label %cont unwind label %lpad
+
+cont:
+ ret void
+
+lpad:
+ %l = landingpad { ptr, i32 } catch ptr null
+ ret void
+}
+
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"exception-model", !"dwarf"}
More information about the llvm-commits
mailing list