[llvm] [DebugInfo] Add -disable-dwarf-locations (PR #221007)

Gheorghe-Teodor Bercea via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 16 08:52:45 PDT 2026


https://github.com/doru1004 updated https://github.com/llvm/llvm-project/pull/221007

>From 1f9db05a9eed12488370901d6b22316e244b3f21 Mon Sep 17 00:00:00 2001
From: Gheorghe-Teodor Bercea <dobercea at amd.com>
Date: Thu, 3 Sep 2026 13:35:16 -0400
Subject: [PATCH] Add -disable-dwarf-locations

---
 .../CodeGen/AsmPrinter/DwarfCompileUnit.cpp   |  9 ++++
 llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp    |  6 +++
 llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp     |  8 ++++
 llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h       |  2 +
 .../Generic/disable-dwarf-locations.ll        | 46 +++++++++++++++++++
 5 files changed, 71 insertions(+)
 create mode 100644 llvm/test/DebugInfo/Generic/disable-dwarf-locations.ll

diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
index bb7dba9f37585..d0cdd8b5561bf 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
@@ -1743,6 +1743,9 @@ void DwarfCompileUnit::addVariableAddress(const DbgVariable &DV, DIE &Die,
 void DwarfCompileUnit::addLocationWithExpr(DIE &Die, dwarf::Attribute Attribute,
                                            const MachineLocation &Location,
                                            ArrayRef<uint64_t> Expr) {
+  if (DisableDwarfLocations)
+    return;
+
   DIELoc *Loc = new (DIEValueAllocator) DIELoc;
   DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
   if (Location.isIndirect())
@@ -1786,6 +1789,9 @@ void DwarfCompileUnit::addMemoryLocation(DIE &Die, dwarf::Attribute Attribute,
 void DwarfCompileUnit::addComplexAddress(const DIExpression *DIExpr, DIE &Die,
                                          dwarf::Attribute Attribute,
                                          const MachineLocation &Location) {
+  if (DisableDwarfLocations)
+    return;
+
   DIELoc *Loc = new (DIEValueAllocator) DIELoc;
   DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
   DwarfExpr.addFragmentOffset(DIExpr);
@@ -1812,6 +1818,9 @@ void DwarfCompileUnit::addComplexAddress(const DIExpression *DIExpr, DIE &Die,
 /// Add a Dwarf loclistptr attribute data and value.
 void DwarfCompileUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute,
                                        unsigned Index) {
+  if (DisableDwarfLocations)
+    return;
+
   dwarf::Form Form = (DD->getDwarfVersion() >= 5)
                          ? dwarf::DW_FORM_loclistx
                          : DD->getDwarfSectionOffsetForm();
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
index b5828314590a5..b483dd30f9e32 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -3620,6 +3620,9 @@ void DwarfDebug::emitDebugLocImpl(MCSection *Sec) {
 
 // Emit locations into the .debug_loc/.debug_loclists section.
 void DwarfDebug::emitDebugLoc() {
+  if (DisableDwarfLocations)
+    return;
+
   emitDebugLocImpl(
       getDwarfVersion() >= 5
           ? Asm->getObjFileLowering().getDwarfLoclistsSection()
@@ -3628,6 +3631,9 @@ void DwarfDebug::emitDebugLoc() {
 
 // Emit locations into the .debug_loc.dwo/.debug_loclists.dwo section.
 void DwarfDebug::emitDebugLocDWO() {
+  if (DisableDwarfLocations)
+    return;
+
   if (getDwarfVersion() >= 5) {
     emitDebugLocImpl(
         Asm->getObjFileLowering().getDwarfLoclistsDWOSection());
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
index 634f0d90fa712..3f0689321ed0e 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
@@ -37,6 +37,12 @@ using namespace llvm;
 
 #define DEBUG_TYPE "dwarfdebug"
 
+bool llvm::DisableDwarfLocations;
+static cl::opt<bool, true> DisableDwarfLocationsOpt(
+    "disable-dwarf-locations",
+    cl::desc("Disable emitting DWARF location DIE attributes"),
+    cl::ReallyHidden, cl::location(DisableDwarfLocations), cl::init(false));
+
 DIEDwarfExpression::DIEDwarfExpression(const AsmPrinter &AP,
                                        DwarfCompileUnit &CU, DIELoc &DIE)
     : DwarfExpression(AP.getDwarfVersion(), CU), AP(AP), OutDIE(DIE) {}
@@ -437,6 +443,8 @@ DIE &DwarfUnit::createAndAddDIE(dwarf::Tag Tag, DIE &Parent, const DINode *N) {
 void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc) {
   Loc->computeSize(Asm->getDwarfFormParams());
   DIELocs.push_back(Loc); // Memoize so we can call the destructor later on.
+  if (DisableDwarfLocations)
+    return;
   addAttribute(Die, Attribute, Loc->BestForm(DD->getDwarfVersion()), Loc);
 }
 
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
index 7913a4bd8a237..f7e99aacb7a83 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.h
@@ -30,6 +30,8 @@ class DwarfCompileUnit;
 class MCDwarfDwoLineTable;
 class MCSymbol;
 
+extern bool DisableDwarfLocations;
+
 //===----------------------------------------------------------------------===//
 /// This dwarf writer support class manages information associated with a
 /// source file.
diff --git a/llvm/test/DebugInfo/Generic/disable-dwarf-locations.ll b/llvm/test/DebugInfo/Generic/disable-dwarf-locations.ll
new file mode 100644
index 0000000000000..7ec1c05a94e6b
--- /dev/null
+++ b/llvm/test/DebugInfo/Generic/disable-dwarf-locations.ll
@@ -0,0 +1,46 @@
+; Check that -disable-dwarf-locations suppresses variable location information
+; while leaving the rest of the debug info intact.
+
+; RUN: llc -o - %s -filetype=obj \
+; RUN:   | llvm-dwarfdump -debug-info - | FileCheck %s --check-prefix=DEFAULT
+; RUN: llc -o - %s -filetype=obj -disable-dwarf-locations \
+; RUN:   | llvm-dwarfdump -debug-info - | FileCheck %s --check-prefix=DISABLED
+
+; DEFAULT:      DW_TAG_formal_parameter
+; DEFAULT-NEXT:   DW_AT_location
+; DEFAULT-NEXT:   DW_AT_name ("foo")
+
+; The parameter, its name and its type are still described; only the location
+; is gone.
+; DISABLED:      DW_TAG_formal_parameter
+; DISABLED-NEXT:   DW_AT_name ("foo")
+; DISABLED-NOT:  DW_AT_location
+
+define void @f(ptr %bar) !dbg !6 {
+entry:
+  %foo.addr = alloca ptr
+  store ptr %bar, ptr %foo.addr
+  call void @llvm.dbg.declare(metadata ptr %foo.addr, metadata !12, metadata !13), !dbg !14
+  ret void, !dbg !15
+}
+
+declare void @llvm.dbg.declare(metadata, metadata, metadata)
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3, !4}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
+!1 = !DIFile(filename: "t.c", directory: "/")
+!2 = !{}
+!3 = !{i32 2, !"Dwarf Version", i32 4}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!6 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !7, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: false, unit: !0, retainedNodes: !2)
+!7 = !DISubroutineType(types: !8)
+!8 = !{null, !9}
+!9 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64)
+!10 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !11)
+!11 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
+!12 = !DILocalVariable(name: "foo", arg: 1, scope: !6, file: !1, line: 1, type: !10)
+!13 = !DIExpression(DW_OP_deref)
+!14 = !DILocation(line: 1, scope: !6)
+!15 = !DILocation(line: 1, scope: !6)



More information about the llvm-commits mailing list