[llvm] [NFC][DWARF] Prefer addSectionLabel (PR #214239)

Aiden Grossman via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 5 07:20:42 PDT 2026


https://github.com/boomanaiden154 created https://github.com/llvm/llvm-project/pull/214239

This is a follow-up to a comment on #213128.

addSectionDelta was only called in sites within if statements that would branch on whether or not split DWARF is enabled and then branching to either addSectionDelta or addSectionLabel. Instead we can just make addSectionLabel call addSectionDelta internally if we're in a DwoUnit to make things simpler.

This is obviously NFC for most cases. For macro info, we were previously branching on useSplitDWARF which can differ, but only in the case we are emitting into the skeleton compile unit, which we should not be doing for macro info.

>From 5fcce3a9f66bf50328738bdd880a4a3a90305294 Mon Sep 17 00:00:00 2001
From: Aiden Grossman <aidengrossman at google.com>
Date: Wed, 5 Aug 2026 14:12:20 +0000
Subject: [PATCH] [NFC][DWARF] Prefer addSectionLabel

This is a follow-up to a comment on #213128.

addSectionDelta was only called in sites within if statements that would
branch on whether or not split DWARF is enabled and then branching to
either addSectionDelta or addSectionLabel. Instead we can just make
addSectionLabel call addSectionDelta internally if we're in a DwoUnit to
make things simpler.

This is obviously NFC for most cases. For macro info, we were previously
branching on useSplitDWARF which can differ, but only in the case we are
emitting into the skeleton compile unit, which we should not be doing
for macro info.
---
 .../CodeGen/AsmPrinter/DwarfCompileUnit.cpp   | 16 ++--------
 llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp    | 29 ++++++-------------
 llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp     |  2 +-
 llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h       |  8 ++---
 4 files changed, 17 insertions(+), 38 deletions(-)

diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
index 7072fc9c8d2fb..752dc69b3971e 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
@@ -526,13 +526,8 @@ DIE &DwarfCompileUnit::updateSubprogramScopeDIE(const DISubprogram *SP,
   if (emitFuncLineTableOffsets() && LineTableSym) {
     MCSymbol *Symbol =
         Asm->getObjFileLowering().getDwarfLineSection()->getBeginSymbol();
-    if (isDwoUnit()) {
-      addSectionDelta(*SPDie, dwarf::DW_AT_LLVM_stmt_sequence, LineTableSym,
-                      Symbol);
-    } else {
-      addSectionLabel(*SPDie, dwarf::DW_AT_LLVM_stmt_sequence, LineTableSym,
-                      Symbol);
-    }
+    addSectionLabel(*SPDie, dwarf::DW_AT_LLVM_stmt_sequence, LineTableSym,
+                    Symbol);
   }
 
   // Only include DW_AT_frame_base in full debug info
@@ -648,12 +643,7 @@ void DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE,
     const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
     const MCSymbol *RangeSectionSym =
         TLOF.getDwarfRangesSection()->getBeginSymbol();
-    if (isDwoUnit())
-      addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, List.Label,
-                      RangeSectionSym);
-    else
-      addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, List.Label,
-                      RangeSectionSym);
+    addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, List.Label, RangeSectionSym);
   }
 }
 
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
index 0b21819cb7bdf..7260b0b7ced38 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -1428,28 +1428,17 @@ void DwarfDebug::finalizeModuleInfo() {
     // If compile Unit has macros, emit "DW_AT_macro_info/DW_AT_macros"
     // attribute.
     if (CUNode->getMacros()) {
+      DIE &InfoEntry = useSplitDwarf() ? TheCU.getUnitDie() : U.getUnitDie();
       if (UseDebugMacroSection) {
-        if (useSplitDwarf())
-          TheCU.addSectionDelta(
-              TheCU.getUnitDie(), dwarf::DW_AT_macros, U.getMacroLabelBegin(),
-              TLOF.getDwarfMacroDWOSection()->getBeginSymbol());
-        else {
-          dwarf::Attribute MacrosAttr = getDwarfVersion() >= 5
-                                            ? dwarf::DW_AT_macros
-                                            : dwarf::DW_AT_GNU_macros;
-          U.addSectionLabel(U.getUnitDie(), MacrosAttr, U.getMacroLabelBegin(),
-                            TLOF.getDwarfMacroSection()->getBeginSymbol());
-        }
+        dwarf::Attribute MacrosAttr = getDwarfVersion() >= 5 || useSplitDwarf()
+                                          ? dwarf::DW_AT_macros
+                                          : dwarf::DW_AT_GNU_macros;
+        U.addSectionLabel(InfoEntry, MacrosAttr, U.getMacroLabelBegin(),
+                          TLOF.getDwarfMacroSection()->getBeginSymbol());
       } else {
-        if (useSplitDwarf())
-          TheCU.addSectionDelta(
-              TheCU.getUnitDie(), dwarf::DW_AT_macro_info,
-              U.getMacroLabelBegin(),
-              TLOF.getDwarfMacinfoDWOSection()->getBeginSymbol());
-        else
-          U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_macro_info,
-                            U.getMacroLabelBegin(),
-                            TLOF.getDwarfMacinfoSection()->getBeginSymbol());
+        U.addSectionLabel(InfoEntry, dwarf::DW_AT_macro_info,
+                          U.getMacroLabelBegin(),
+                          TLOF.getDwarfMacinfoSection()->getBeginSymbol());
       }
     }
   }
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
index 78c0769e49161..f69bf09d81694 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
@@ -2129,7 +2129,7 @@ void DwarfUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
 
 void DwarfUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
                                 const MCSymbol *Label, const MCSymbol *Sec) {
-  if (Asm->doesDwarfUseRelocationsAcrossSections())
+  if (Asm->doesDwarfUseRelocationsAcrossSections() && !isDwoUnit())
     addLabel(Die, Attribute, DD->getDwarfSectionOffsetForm(), Label);
   else
     addSectionDelta(Die, Attribute, Label, Sec);
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
index 139fae5621940..8397445b710a5 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
@@ -317,10 +317,6 @@ class DwarfUnit : public DIEUnit {
 
   void constructTypeDIE(DIE &Buffer, const DICompositeType *CTy);
 
-  /// addSectionDelta - Add a label delta attribute data and value.
-  void addSectionDelta(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
-                       const MCSymbol *Lo);
-
   /// Add a Dwarf section label attribute data and value.
   void addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
                        const MCSymbol *Label, const MCSymbol *Sec);
@@ -413,6 +409,10 @@ class DwarfUnit : public DIEUnit {
   /// Returns 'true' if the current DwarfVersion is compatible
   /// with the specified \p Version.
   bool isCompatibleWithVersion(uint16_t Version) const;
+
+  /// addSectionDelta - Add a label delta attribute data and value.
+  void addSectionDelta(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
+                       const MCSymbol *Lo);
 };
 
 class DwarfTypeUnit final : public DwarfUnit {



More information about the llvm-commits mailing list