[llvm] a82b97c - [CodeGen] Fix lpad padding at section start after empty block (#112595)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 3 18:29:28 PST 2025
Author: Fabian Parzefall
Date: 2025-12-04T10:29:24+08:00
New Revision: a82b97c524e851b0807b95c34a2154a2cbce9bdf
URL: https://github.com/llvm/llvm-project/commit/a82b97c524e851b0807b95c34a2154a2cbce9bdf
DIFF: https://github.com/llvm/llvm-project/commit/a82b97c524e851b0807b95c34a2154a2cbce9bdf.diff
LOG: [CodeGen] Fix lpad padding at section start after empty block (#112595)
If a landing pad is at the very start of a split section, it has to be
padded by a nop instruction. Otherwise its offset is marked as zero in
the LSDA, which means no landing pad (leading it to be skipped).
LLVM already handles this. If a landing pad is the first machine block
in a section, a nop is inserted to ensure a non-zero offset. However, if
the landing pad is preceeded by an empty block, the nop would be
omitted.
To fix this, this patch adds a field to machine blocks indicating
whether this block contains the first instruction in its section. This
variable is then used to determine whether to emit the padding.
Co-authored-by: Jinjie Huang <huangjinjie at bytedance.com>
Added:
Modified:
llvm/lib/CodeGen/BasicBlockSections.cpp
llvm/test/CodeGen/Generic/machine-function-splitter.ll
Removed:
################################################################################
diff --git a/llvm/lib/CodeGen/BasicBlockSections.cpp b/llvm/lib/CodeGen/BasicBlockSections.cpp
index 52e2909bec072..b2108744d9c47 100644
--- a/llvm/lib/CodeGen/BasicBlockSections.cpp
+++ b/llvm/lib/CodeGen/BasicBlockSections.cpp
@@ -263,8 +263,16 @@ void llvm::sortBasicBlocksAndUpdateBranches(
// zero implies "no landing pad." This function inserts a NOP just before the EH
// pad label to ensure a nonzero offset.
void llvm::avoidZeroOffsetLandingPad(MachineFunction &MF) {
+ std::optional<MBBSectionID> CurrentSection;
+ auto IsFirstNonEmptyBBInSection = [&](const MachineBasicBlock &MBB) {
+ if (MBB.empty() || MBB.getSectionID() == CurrentSection)
+ return false;
+ CurrentSection = MBB.getSectionID();
+ return true;
+ };
+
for (auto &MBB : MF) {
- if (MBB.isBeginSection() && MBB.isEHPad()) {
+ if (IsFirstNonEmptyBBInSection(MBB) && MBB.isEHPad()) {
MachineBasicBlock::iterator MI = MBB.begin();
while (!MI->isEHLabel())
++MI;
diff --git a/llvm/test/CodeGen/Generic/machine-function-splitter.ll b/llvm/test/CodeGen/Generic/machine-function-splitter.ll
index 1a8c9ede8f8b7..d798b2875645b 100644
--- a/llvm/test/CodeGen/Generic/machine-function-splitter.ll
+++ b/llvm/test/CodeGen/Generic/machine-function-splitter.ll
@@ -674,6 +674,36 @@ define void @foo22(i1 zeroext %0) nounwind !prof !14 !section_prefix !15 {
ret void
}
+define i32 @foo23(i1 zeroext %0) personality ptr @__gxx_personality_v0 !prof !14 {
+;; Check that nop is inserted just before the EH pad if it is the first
+;; instruction in a section (but is preceeded by another empty block).
+; MFS-DEFAULTS-LABEL: foo23
+; MFS-DEFAULTS-X86-LABEL: callq baz
+; MFS-DEFAULTS-X86: .section .text.split.foo23,"ax", at progbits
+; MFS-DEFAULTS-X86-NEXT: foo23.cold:
+; MFS-DEFAULTS-X86: nop
+; MFS-DEFAULTS-X86: callq _Unwind_Resume at PLT
+entry:
+ br i1 %0, label %try, label %unreachable, !prof !17
+
+try:
+ invoke void @_Z1fv()
+ to label %try.cont unwind label %lpad, !prof !17
+
+try.cont:
+ %1 = call i32 @baz()
+ ret i32 %1
+
+unreachable:
+ unreachable
+
+lpad:
+ %2 = landingpad { ptr, i32 }
+ cleanup
+ catch ptr @_ZTIi
+ resume { ptr, i32 } %2
+}
+
declare i32 @bar()
declare i32 @baz()
declare i32 @bam()
More information about the llvm-commits
mailing list