Skip to content

Commit b657076

Browse files
committed
[CodeGen] Fix lpad padding at section start after empty block
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 checks not whether a block is the first in section, but instead whether it is the first *non-empty* block in the section.
1 parent bee33b5 commit b657076

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

llvm/lib/CodeGen/BasicBlockSections.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,16 @@ void llvm::sortBasicBlocksAndUpdateBranches(
265265
// zero implies "no landing pad." This function inserts a NOP just before the EH
266266
// pad label to ensure a nonzero offset.
267267
void llvm::avoidZeroOffsetLandingPad(MachineFunction &MF) {
268+
std::optional<MBBSectionID> CurrentSection;
269+
auto IsFirstNonEmptyBBInSection = [&](const MachineBasicBlock &MBB) {
270+
if (MBB.empty() || MBB.getSectionID() == CurrentSection)
271+
return false;
272+
CurrentSection = MBB.getSectionID();
273+
return true;
274+
};
275+
268276
for (auto &MBB : MF) {
269-
if (MBB.isBeginSection() && MBB.isEHPad()) {
277+
if (IsFirstNonEmptyBBInSection(MBB) && MBB.isEHPad()) {
270278
MachineBasicBlock::iterator MI = MBB.begin();
271279
while (!MI->isEHLabel())
272280
++MI;

llvm/test/CodeGen/Generic/machine-function-splitter.ll

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,36 @@ define void @foo22(i1 zeroext %0) nounwind !prof !14 !section_prefix !15 {
674674
ret void
675675
}
676676

677+
define i32 @foo23(i1 zeroext %0) personality ptr @__gxx_personality_v0 !prof !14 {
678+
;; Check that nop is inserted just before the EH pad if it is the first
679+
;; instruction in a section (but is preceeded by another empty block).
680+
; MFS-DEFAULTS-LABEL: foo23
681+
; MFS-DEFAULTS-X86-LABEL: callq baz
682+
; MFS-DEFAULTS-X86: .section .text.split.foo23,"ax",@progbits
683+
; MFS-DEFAULTS-X86-NEXT: foo23.cold:
684+
; MFS-DEFAULTS-X86: nop
685+
; MFS-DEFAULTS-X86: callq _Unwind_Resume@PLT
686+
entry:
687+
br i1 %0, label %try, label %unreachable, !prof !17
688+
689+
try:
690+
invoke void @_Z1fv()
691+
to label %try.cont unwind label %lpad, !prof !17
692+
693+
try.cont:
694+
%1 = call i32 @baz()
695+
ret i32 %1
696+
697+
unreachable:
698+
unreachable
699+
700+
lpad:
701+
%2 = landingpad { ptr, i32 }
702+
cleanup
703+
catch ptr @_ZTIi
704+
resume { ptr, i32 } %2
705+
}
706+
677707
declare i32 @bar()
678708
declare i32 @baz()
679709
declare i32 @bam()

0 commit comments

Comments
 (0)