[llvm] [AsmPrinter] Fix trailing CFI emission past FDE end with basic-block-sections (PR #204562)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 25 04:55:00 PDT 2026


https://github.com/ayrai-gb updated https://github.com/llvm/llvm-project/pull/204562

>From ba1dbf11676a36904ee695382ac69acfb01cfe4d Mon Sep 17 00:00:00 2001
From: Ayush Rai <your-email at amd.com>
Date: Thu, 18 Jun 2026 17:03:59 +0530
Subject: [PATCH 1/2] [AsmPrinter] Skip trailing CFI at the end of any section,
 not just the function

---
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp    |  8 ++-
 .../CodeGen/X86/cfi-trailing-section-end.ll   | 63 +++++++++++++++++++
 2 files changed, 68 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/cfi-trailing-section-end.ll

diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index cdb9d760606f6..9a62a84fafd83 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -1424,13 +1424,15 @@ void AsmPrinter::emitCFIInstruction(const MachineInstr &MI) {
     return;
 
   // If there is no "real" instruction following this CFI instruction, skip
-  // emitting it; it would be beyond the end of the function's FDE range.
+  // emitting it; it would be beyond the end of this block's FDE range. With
+  // basic-block-sections / function splitting a single function is emitted as
+  // several FDEs (one per section), so the relevant boundary is the end of the
+  // current section, not just the end of the whole function.
   auto *MBB = MI.getParent();
   auto I = std::next(MI.getIterator());
   while (I != MBB->end() && I->isTransient())
     ++I;
-  if (I == MBB->instr_end() &&
-      MBB->getReverseIterator() == MBB->getParent()->rbegin())
+  if (I == MBB->instr_end() && MBB->isEndSection())
     return;
 
   const std::vector<MCCFIInstruction> &Instrs = MF->getFrameInstructions();
diff --git a/llvm/test/CodeGen/X86/cfi-trailing-section-end.ll b/llvm/test/CodeGen/X86/cfi-trailing-section-end.ll
new file mode 100644
index 0000000000000..b3e4ee3b37c0c
--- /dev/null
+++ b/llvm/test/CodeGen/X86/cfi-trailing-section-end.ll
@@ -0,0 +1,63 @@
+; RUN: llc -O0 %s --basic-block-sections=all -mtriple=x86_64-- --frame-pointer=all -filetype=asm -o - | FileCheck %s
+; RUN: llc -O0 %s --basic-block-sections=all -mtriple=x86_64-- --frame-pointer=all -filetype=obj -o %t.o
+; RUN: llvm-dwarfdump --verify --eh-frame %t.o | FileCheck %s --check-prefix=VERIFY
+
+;; AsmPrinter::emitCFIInstruction skips a trailing CFI (one with no following
+;; "real" instruction) because such a directive would lie beyond the end of its
+;; FDE. The block boundary that matters is the end of the current *section*, not
+;; just the end of the whole function: with -basic-block-sections / function
+;; splitting a single function is emitted as several FDEs (one per section).
+;;
+;; Here the two empty `unreachable` blocks %d0 and %d1 each become their own
+;; section/FDE under -basic-block-sections=all, and neither is the function's
+;; last block (%ret0 is). The trailing CFI in those non-final sections must
+;; still be skipped (guarded by MBB->isEndSection()), so each empty section
+;; comes out bare: just .cfi_startproc / .cfi_endproc, with no .cfi_def_cfa or
+;; .cfi_offset in between.
+
+; The function entry still emits its prologue CFI.
+; CHECK-LABEL: trailing_cfi_section_end:
+; CHECK:       .cfi_startproc
+; CHECK:       .cfi_def_cfa_offset 16
+
+; %d0 - non-final empty section: trailing CFI must be skipped.
+; CHECK-LABEL: __part.{{[0-9]+}}: {{.*}}%d0
+; CHECK-NEXT:  .cfi_startproc
+; CHECK-NOT:   .cfi_def_cfa
+; CHECK-NOT:   .cfi_offset
+; CHECK:       .cfi_endproc
+
+; %d1 - non-final empty section: trailing CFI must be skipped.
+; CHECK-LABEL: __part.{{[0-9]+}}: {{.*}}%d1
+; CHECK-NEXT:  .cfi_startproc
+; CHECK-NOT:   .cfi_def_cfa
+; CHECK-NOT:   .cfi_offset
+; CHECK:       .cfi_endproc
+
+; %ret0 - a real section keeps its CFI (we must not over-suppress).
+; CHECK-LABEL: __part.{{[0-9]+}}: {{.*}}%ret0
+; CHECK-NEXT:  .cfi_startproc
+; CHECK:       .cfi_def_cfa
+; CHECK:       .cfi_endproc
+
+; The emitted unwind info must remain well-formed.
+; VERIFY: No errors.
+
+target triple = "x86_64-unknown-linux-gnu"
+
+declare void @ext()
+
+define void @trailing_cfi_section_end(i32 %x) #0 {
+entry:
+  switch i32 %x, label %ret0 [ i32 0, label %d0
+                               i32 1, label %d1 ]
+d0:
+  unreachable
+d1:
+  unreachable
+ret0:
+  call void @ext()
+  ret void
+}
+
+attributes #0 = { uwtable "frame-pointer"="all" }

>From 19133ac4bf4ae1d8ec01ad221cc11c66f96c5e7f Mon Sep 17 00:00:00 2001
From: Ayush Rai <your-email at amd.com>
Date: Thu, 25 Jun 2026 17:14:53 +0530
Subject: [PATCH 2/2] [AsmPrinter] Also skip trailing CFI at the function's
 last block, not just section ends

---
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index fc360e327b554..a360ca31a7642 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -1427,12 +1427,16 @@ void AsmPrinter::emitCFIInstruction(const MachineInstr &MI) {
   // emitting it; it would be beyond the end of this block's FDE range. With
   // basic-block-sections / function splitting a single function is emitted as
   // several FDEs (one per section), so the relevant boundary is the end of the
-  // current section, not just the end of the whole function.
+  // current section. isEndSection() is only populated when sections are
+  // assigned, so also fall back to the function's last block for the ordinary
+  // non-sectioned path.
   auto *MBB = MI.getParent();
   auto I = std::next(MI.getIterator());
   while (I != MBB->end() && I->isTransient())
     ++I;
-  if (I == MBB->instr_end() && MBB->isEndSection())
+  if (I == MBB->instr_end() &&
+      (MBB->isEndSection() ||
+       MBB->getReverseIterator() == MBB->getParent()->rbegin()))
     return;
 
   const std::vector<MCCFIInstruction> &Instrs = MF->getFrameInstructions();



More information about the llvm-commits mailing list