[llvm-branch-commits] [llvm] [DebugInfo] Emit DW_AT_LLVM_address_space for AMDGPU (PR #220789)

Gheorghe-Teodor Bercea via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Sep 14 14:09:27 PDT 2026


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

>From f0c90e5300bd3015f1a73cc183b0a61c4007aab7 Mon Sep 17 00:00:00 2001
From: Gheorghe-Teodor Bercea <dobercea at amd.com>
Date: Wed, 2 Sep 2026 22:41:51 -0400
Subject: [PATCH 1/5] Use DW_AT_LLVM_address_space instead of
 DW_AT_address_class

---
 llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp     |  5 +-
 .../AMDGPU/dwarfdump-address-spaces.ll        | 91 +++++++++++++++++++
 .../DebugInfo/Generic/address_space_rvalue.ll |  3 +-
 3 files changed, 95 insertions(+), 4 deletions(-)
 create mode 100644 llvm/test/DebugInfo/AMDGPU/dwarfdump-address-spaces.ll

diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
index 634f0d90fa712..11cd2482d3199 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
@@ -913,9 +913,8 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) {
   // If DWARF address space value is other than None, add it.  The IR
   // verifier checks that DWARF address space only exists for pointer
   // or reference types.
-  if (DTy->getDWARFAddressSpace())
-    addUInt(Buffer, dwarf::DW_AT_address_class, dwarf::DW_FORM_data4,
-            *DTy->getDWARFAddressSpace());
+  if (auto AS = DTy->getDWARFAddressSpace())
+    addUInt(Buffer, dwarf::DW_AT_LLVM_address_space, dwarf::DW_FORM_data4, *AS);
 
   // Add template alias template parameters.
   if (Tag == dwarf::DW_TAG_template_alias)
diff --git a/llvm/test/DebugInfo/AMDGPU/dwarfdump-address-spaces.ll b/llvm/test/DebugInfo/AMDGPU/dwarfdump-address-spaces.ll
new file mode 100644
index 0000000000000..b0342edc2be69
--- /dev/null
+++ b/llvm/test/DebugInfo/AMDGPU/dwarfdump-address-spaces.ll
@@ -0,0 +1,91 @@
+; RUN: llc -mtriple=x86_64--gnu -filetype=obj --verify-machineinstrs < %s | llvm-dwarfdump - 2>&1 | FileCheck %s --check-prefixes=COMMON,X86
+; RUN: llc -mtriple=amdgcn--amdhsa -mcpu=gfx900 -filetype=obj --verify-machineinstrs < %s | llvm-dwarfdump - 2>&1 | FileCheck %s --check-prefixes=COMMON,AMDGPU
+
+; Check that the address spaces are correctly printed for AMDGPU.
+; The interpretation of the address space is dependent on the target.
+
+;COMMON: DW_TAG_compile_unit
+;COMMON:   DW_TAG_subprogram
+;COMMON:     DW_TAG_variable
+;COMMON:       DW_AT_name ("A_none")
+;COMMON:       DW_AT_type ([[PTR_NONE:0x[0-9a-f]+]]
+;COMMON:     DW_TAG_variable
+;COMMON:       DW_AT_name ("A_generic")
+;COMMON:       DW_AT_type ([[PTR_FLAT:0x[0-9a-f]+]]
+;COMMON:     DW_TAG_variable
+;COMMON:       DW_AT_name ("A_region")
+;COMMON:       DW_AT_type ([[PTR_REGION:0x[0-9a-f]+]]
+;COMMON:     DW_TAG_variable
+;COMMON:       DW_AT_name ("A_local")
+;COMMON:       DW_AT_type ([[PTR_LOCAL:0x[0-9a-f]+]]
+;COMMON:     DW_TAG_variable
+;COMMON:       DW_AT_name ("A_private_lane")
+;COMMON:       DW_AT_type ([[PTR_PRIVATE_LANE:0x[0-9a-f]+]]
+;COMMON:     DW_TAG_variable
+;COMMON:       DW_AT_name ("A_private_wave")
+;COMMON:       DW_AT_type ([[PTR_PRIVATE_WAVE:0x[0-9a-f]+]]
+
+;COMMON: [[PTR_NONE]]: DW_TAG_pointer_type
+;COMMON:   DW_AT_type ([[INT:0x[0-9a-f]+]] "int")
+;AMDGPU:   DW_AT_LLVM_address_space (0x00000000 "DW_ASPACE_LLVM_none")
+;X86:      DW_AT_LLVM_address_space (0x00000000 "DW_ASPACE_LLVM_none")
+
+;COMMON: [[INT]]: DW_TAG_base_type
+;COMMON:   DW_AT_name ("int")
+
+;COMMON: [[PTR_FLAT]]: DW_TAG_pointer_type
+;COMMON:   DW_AT_type ([[INT]] "int")
+;AMDGPU:   DW_AT_LLVM_address_space (0x00000001 "DW_ASPACE_LLVM_AMDGPU_generic")
+;X86:      DW_AT_LLVM_address_space (0x00000001)
+
+;COMMON: [[PTR_REGION]]: DW_TAG_pointer_type
+;COMMON:   DW_AT_type ([[INT]] "int")
+;AMDGPU:   DW_AT_LLVM_address_space (0x00000002 "DW_ASPACE_LLVM_AMDGPU_region")
+;X86:      DW_AT_LLVM_address_space (0x00000002)
+
+;COMMON: [[PTR_LOCAL]]: DW_TAG_pointer_type
+;COMMON:   DW_AT_type ([[INT]] "int")
+;AMDGPU:   DW_AT_LLVM_address_space (0x00000003 "DW_ASPACE_LLVM_AMDGPU_local")
+;X86:      DW_AT_LLVM_address_space (0x00000003)
+
+;COMMON: [[PTR_PRIVATE_LANE]]: DW_TAG_pointer_type
+;COMMON:   DW_AT_type ([[INT]] "int")
+;AMDGPU:   DW_AT_LLVM_address_space (0x00000005 "DW_ASPACE_LLVM_AMDGPU_private_lane")
+;X86:      DW_AT_LLVM_address_space (0x00000005)
+
+;COMMON: [[PTR_PRIVATE_WAVE]]: DW_TAG_pointer_type
+;COMMON:   DW_AT_type ([[INT]] "int")
+;AMDGPU:   DW_AT_LLVM_address_space (0x00000006 "DW_ASPACE_LLVM_AMDGPU_private_wave")
+;X86:      DW_AT_LLVM_address_space (0x00000006)
+
+define void @kernel() !dbg !7 {
+entry:
+  ret void, !dbg !6
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!4, !5}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
+!1 = !DIFile(filename: "dummy.cl", directory: "/some/random/directory")
+!2 = !{}
+!3 = !{!20, !21, !22, !23, !24, !25}
+!4 = !{i32 2, !"Dwarf Version", i32 2}
+!5 = !{i32 2, !"Debug Info Version", i32 3}
+!6 = !DILocation(line: 3, column: 1, scope: !7)
+!7 = distinct !DISubprogram(name: "kernel", scope: !1, file: !1, line: 1, type: !8, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: false, unit: !0, retainedNodes: !3)
+!8 = !DISubroutineType(types: !9)
+!9 = !{null}
+!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!20 = !DILocalVariable(name: "A_none", scope: !7, file: !1, line: 1, type: !30)
+!21 = !DILocalVariable(name: "A_generic", scope: !7, file: !1, line: 1, type: !31)
+!22 = !DILocalVariable(name: "A_region", scope: !7, file: !1, line: 1, type: !32)
+!23 = !DILocalVariable(name: "A_local", scope: !7, file: !1, line: 1, type: !33)
+!24 = !DILocalVariable(name: "A_private_lane", scope: !7, file: !1, line: 1, type: !34)
+!25 = !DILocalVariable(name: "A_private_wave", scope: !7, file: !1, line: 1, type: !35)
+!30 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64, dwarfAddressSpace: 0)
+!31 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64, dwarfAddressSpace: 1)
+!32 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64, dwarfAddressSpace: 2)
+!33 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64, dwarfAddressSpace: 3)
+!34 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64, dwarfAddressSpace: 5)
+!35 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64, dwarfAddressSpace: 6)
diff --git a/llvm/test/DebugInfo/Generic/address_space_rvalue.ll b/llvm/test/DebugInfo/Generic/address_space_rvalue.ll
index ff39188b6419c..331677f23004d 100644
--- a/llvm/test/DebugInfo/Generic/address_space_rvalue.ll
+++ b/llvm/test/DebugInfo/Generic/address_space_rvalue.ll
@@ -6,7 +6,8 @@
 
 ; CHECK: DW_TAG_rvalue_reference_type
 ; CHECK-NOT: DW_TAG
-; CHECK: DW_AT_address_class	(0x00000001)
+; CHECK-NOT: DW_AT_address_class
+; CHECK: DW_AT_LLVM_address_space (0x00000001)
 
 @y = global ptr null, align 8, !dbg !0
 

>From 77051825523ea307aa3a04c53d1ba25616f719e6 Mon Sep 17 00:00:00 2001
From: Gheorghe-Teodor Bercea <dobercea at amd.com>
Date: Tue, 8 Sep 2026 21:28:44 -0400
Subject: [PATCH 2/5] Address review comment: simplify the AMDGPU RUN line

---
 llvm/test/DebugInfo/AMDGPU/dwarfdump-address-spaces.ll | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/test/DebugInfo/AMDGPU/dwarfdump-address-spaces.ll b/llvm/test/DebugInfo/AMDGPU/dwarfdump-address-spaces.ll
index b0342edc2be69..4753bb0188f99 100644
--- a/llvm/test/DebugInfo/AMDGPU/dwarfdump-address-spaces.ll
+++ b/llvm/test/DebugInfo/AMDGPU/dwarfdump-address-spaces.ll
@@ -1,5 +1,5 @@
 ; RUN: llc -mtriple=x86_64--gnu -filetype=obj --verify-machineinstrs < %s | llvm-dwarfdump - 2>&1 | FileCheck %s --check-prefixes=COMMON,X86
-; RUN: llc -mtriple=amdgcn--amdhsa -mcpu=gfx900 -filetype=obj --verify-machineinstrs < %s | llvm-dwarfdump - 2>&1 | FileCheck %s --check-prefixes=COMMON,AMDGPU
+; RUN: llc -mtriple=amdgpu9.00--amdhsa -filetype=obj < %s | llvm-dwarfdump - 2>&1 | FileCheck %s --check-prefixes=COMMON,AMDGPU
 
 ; Check that the address spaces are correctly printed for AMDGPU.
 ; The interpretation of the address space is dependent on the target.

>From 5e5a78be1c2b0899a70617afc8a3964041c08168 Mon Sep 17 00:00:00 2001
From: Gheorghe-Teodor Bercea <dobercea at amd.com>
Date: Tue, 8 Sep 2026 22:23:23 -0400
Subject: [PATCH 3/5] Emit DW_AT_LLVM_address_space only for AMDGPU

---
 llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp            | 11 +++++++++--
 .../DebugInfo/AMDGPU/dwarfdump-address-spaces.ll     | 12 ++++++------
 llvm/test/DebugInfo/AMDGPU/pointer-address-space.ll  |  6 +++---
 llvm/test/DebugInfo/Generic/address_space_rvalue.ll  |  3 +--
 4 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
index 11cd2482d3199..4a1088ea8a1ba 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
@@ -913,8 +913,15 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) {
   // If DWARF address space value is other than None, add it.  The IR
   // verifier checks that DWARF address space only exists for pointer
   // or reference types.
-  if (auto AS = DTy->getDWARFAddressSpace())
-    addUInt(Buffer, dwarf::DW_AT_LLVM_address_space, dwarf::DW_FORM_data4, *AS);
+  if (auto AS = DTy->getDWARFAddressSpace()) {
+    // The AMDGPU DWARF extensions define DW_AT_LLVM_address_space for these
+    // values. Other targets keep DW_AT_address_class; NVPTX in particular
+    // emits values there that cuda-gdb consumes.
+    dwarf::Attribute Attr = Asm->TM.getTargetTriple().isAMDGPU()
+                                ? dwarf::DW_AT_LLVM_address_space
+                                : dwarf::DW_AT_address_class;
+    addUInt(Buffer, Attr, dwarf::DW_FORM_data4, *AS);
+  }
 
   // Add template alias template parameters.
   if (Tag == dwarf::DW_TAG_template_alias)
diff --git a/llvm/test/DebugInfo/AMDGPU/dwarfdump-address-spaces.ll b/llvm/test/DebugInfo/AMDGPU/dwarfdump-address-spaces.ll
index 4753bb0188f99..84f8af9e3051c 100644
--- a/llvm/test/DebugInfo/AMDGPU/dwarfdump-address-spaces.ll
+++ b/llvm/test/DebugInfo/AMDGPU/dwarfdump-address-spaces.ll
@@ -28,7 +28,7 @@
 ;COMMON: [[PTR_NONE]]: DW_TAG_pointer_type
 ;COMMON:   DW_AT_type ([[INT:0x[0-9a-f]+]] "int")
 ;AMDGPU:   DW_AT_LLVM_address_space (0x00000000 "DW_ASPACE_LLVM_none")
-;X86:      DW_AT_LLVM_address_space (0x00000000 "DW_ASPACE_LLVM_none")
+;X86:      DW_AT_address_class (0x00000000)
 
 ;COMMON: [[INT]]: DW_TAG_base_type
 ;COMMON:   DW_AT_name ("int")
@@ -36,27 +36,27 @@
 ;COMMON: [[PTR_FLAT]]: DW_TAG_pointer_type
 ;COMMON:   DW_AT_type ([[INT]] "int")
 ;AMDGPU:   DW_AT_LLVM_address_space (0x00000001 "DW_ASPACE_LLVM_AMDGPU_generic")
-;X86:      DW_AT_LLVM_address_space (0x00000001)
+;X86:      DW_AT_address_class (0x00000001)
 
 ;COMMON: [[PTR_REGION]]: DW_TAG_pointer_type
 ;COMMON:   DW_AT_type ([[INT]] "int")
 ;AMDGPU:   DW_AT_LLVM_address_space (0x00000002 "DW_ASPACE_LLVM_AMDGPU_region")
-;X86:      DW_AT_LLVM_address_space (0x00000002)
+;X86:      DW_AT_address_class (0x00000002)
 
 ;COMMON: [[PTR_LOCAL]]: DW_TAG_pointer_type
 ;COMMON:   DW_AT_type ([[INT]] "int")
 ;AMDGPU:   DW_AT_LLVM_address_space (0x00000003 "DW_ASPACE_LLVM_AMDGPU_local")
-;X86:      DW_AT_LLVM_address_space (0x00000003)
+;X86:      DW_AT_address_class (0x00000003)
 
 ;COMMON: [[PTR_PRIVATE_LANE]]: DW_TAG_pointer_type
 ;COMMON:   DW_AT_type ([[INT]] "int")
 ;AMDGPU:   DW_AT_LLVM_address_space (0x00000005 "DW_ASPACE_LLVM_AMDGPU_private_lane")
-;X86:      DW_AT_LLVM_address_space (0x00000005)
+;X86:      DW_AT_address_class (0x00000005)
 
 ;COMMON: [[PTR_PRIVATE_WAVE]]: DW_TAG_pointer_type
 ;COMMON:   DW_AT_type ([[INT]] "int")
 ;AMDGPU:   DW_AT_LLVM_address_space (0x00000006 "DW_ASPACE_LLVM_AMDGPU_private_wave")
-;X86:      DW_AT_LLVM_address_space (0x00000006)
+;X86:      DW_AT_address_class (0x00000006)
 
 define void @kernel() !dbg !7 {
 entry:
diff --git a/llvm/test/DebugInfo/AMDGPU/pointer-address-space.ll b/llvm/test/DebugInfo/AMDGPU/pointer-address-space.ll
index a289d83951c7a..9bc910fafaf3e 100644
--- a/llvm/test/DebugInfo/AMDGPU/pointer-address-space.ll
+++ b/llvm/test/DebugInfo/AMDGPU/pointer-address-space.ll
@@ -39,15 +39,15 @@
 
 ; CHECK:      0x[[NONE]]: DW_TAG_pointer_type
 ; CHECK-NEXT:               DW_AT_type
-; CHECK-NOT:                DW_AT_address_class
+; CHECK-NOT:                DW_AT_LLVM_address_space
 
 ; CHECK:      0x[[LOCAL]]: DW_TAG_pointer_type
 ; CHECK-NEXT:                DW_AT_type
-; CHECK-NEXT:                DW_AT_address_class [DW_FORM_data4] (0x00000002)
+; CHECK-NEXT:                DW_AT_LLVM_address_space [DW_FORM_data4] (0x00000002 "DW_ASPACE_LLVM_AMDGPU_region")
 
 ; CHECK:      0x[[PRIVATE]]: DW_TAG_pointer_type
 ; CHECK-NEXT:                  DW_AT_type
-; CHECK-NEXT:                  DW_AT_address_class [DW_FORM_data4] (0x00000001)
+; CHECK-NEXT:                  DW_AT_LLVM_address_space [DW_FORM_data4] (0x00000001 "DW_ASPACE_LLVM_AMDGPU_generic")
 
 declare void @llvm.dbg.declare(metadata, metadata, metadata)
 
diff --git a/llvm/test/DebugInfo/Generic/address_space_rvalue.ll b/llvm/test/DebugInfo/Generic/address_space_rvalue.ll
index 331677f23004d..ff39188b6419c 100644
--- a/llvm/test/DebugInfo/Generic/address_space_rvalue.ll
+++ b/llvm/test/DebugInfo/Generic/address_space_rvalue.ll
@@ -6,8 +6,7 @@
 
 ; CHECK: DW_TAG_rvalue_reference_type
 ; CHECK-NOT: DW_TAG
-; CHECK-NOT: DW_AT_address_class
-; CHECK: DW_AT_LLVM_address_space (0x00000001)
+; CHECK: DW_AT_address_class	(0x00000001)
 
 @y = global ptr null, align 8, !dbg !0
 

>From 9c6a379f4d32c73b97f88be6dc1bad4a252c95e7 Mon Sep 17 00:00:00 2001
From: Gheorghe-Teodor Bercea <dobercea at amd.com>
Date: Thu, 10 Sep 2026 19:21:37 -0400
Subject: [PATCH 4/5] Address review comments: add a DwarfDebug target hook,
 gate the test on x86

---
 llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h      |  7 ++++
 llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp     | 12 ++-----
 llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp   |  6 ++++
 llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.h     |  3 ++
 llvm/lib/Target/AMDGPU/AMDGPUDwarfDebug.h     | 35 +++++++++++++++++++
 .../AMDGPU/dwarfdump-address-spaces.ll        |  1 +
 6 files changed, 55 insertions(+), 9 deletions(-)
 create mode 100644 llvm/lib/Target/AMDGPU/AMDGPUDwarfDebug.h

diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h
index 73285711d0aa9..d26d0806ffecb 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h
@@ -772,6 +772,13 @@ class DwarfDebug : public DebugHandlerBase {
                               VariableLocationKind VarLocKind,
                               const GlobalVariable *GV = nullptr) const {}
 
+  /// The attribute describing the DWARF address space of a pointer or
+  /// reference type. Targets whose DWARF extensions define their own
+  /// attribute for this can override it.
+  virtual dwarf::Attribute getTypeAddressSpaceAttribute() const {
+    return dwarf::DW_AT_address_class;
+  }
+
   //===--------------------------------------------------------------------===//
   // Main entry points.
   //
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
index 4a1088ea8a1ba..5507a4e20cff8 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
@@ -913,15 +913,9 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) {
   // If DWARF address space value is other than None, add it.  The IR
   // verifier checks that DWARF address space only exists for pointer
   // or reference types.
-  if (auto AS = DTy->getDWARFAddressSpace()) {
-    // The AMDGPU DWARF extensions define DW_AT_LLVM_address_space for these
-    // values. Other targets keep DW_AT_address_class; NVPTX in particular
-    // emits values there that cuda-gdb consumes.
-    dwarf::Attribute Attr = Asm->TM.getTargetTriple().isAMDGPU()
-                                ? dwarf::DW_AT_LLVM_address_space
-                                : dwarf::DW_AT_address_class;
-    addUInt(Buffer, Attr, dwarf::DW_FORM_data4, *AS);
-  }
+  if (auto AS = DTy->getDWARFAddressSpace())
+    addUInt(Buffer, DD->getTypeAddressSpaceAttribute(), dwarf::DW_FORM_data4,
+            *AS);
 
   // Add template alias template parameters.
   if (Tag == dwarf::DW_TAG_template_alias)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
index 11c482c83f1e1..3d097dc800ff4 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
@@ -17,6 +17,7 @@
 
 #include "AMDGPUAsmPrinter.h"
 #include "AMDGPU.h"
+#include "AMDGPUDwarfDebug.h"
 #include "AMDGPUHSAMetadataStreamer.h"
 #include "AMDGPUMCResourceInfo.h"
 #include "AMDGPUResourceUsageAnalysis.h"
@@ -131,6 +132,11 @@ StringRef AMDGPUAsmPrinter::getPassName() const {
   return "AMDGPU Assembly Printer";
 }
 
+/// Create AMDGPU-specific DwarfDebug handler.
+DwarfDebug *AMDGPUAsmPrinter::createDwarfDebug() {
+  return new AMDGPUDwarfDebug(this);
+}
+
 const MCSubtargetInfo *AMDGPUAsmPrinter::getGlobalSTI() const {
   return &TM.getMCSubtargetInfo();
 }
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.h b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.h
index 4394cde308665..60e8b0529fe9f 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.h
+++ b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.h
@@ -120,6 +120,9 @@ class AMDGPUAsmPrinter final : public AsmPrinter {
   bool doFinalization(Module &M) override;
   bool runOnMachineFunction(MachineFunction &MF) override;
 
+  /// Create AMDGPU-specific DwarfDebug handler.
+  DwarfDebug *createDwarfDebug() override;
+
   /// Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated
   /// pseudo lowering.
   bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const;
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUDwarfDebug.h b/llvm/lib/Target/AMDGPU/AMDGPUDwarfDebug.h
new file mode 100644
index 0000000000000..54c4dc7ff13a8
--- /dev/null
+++ b/llvm/lib/Target/AMDGPU/AMDGPUDwarfDebug.h
@@ -0,0 +1,35 @@
+//===-- AMDGPUDwarfDebug.h - AMDGPU DwarfDebug Implementation -*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file declares the AMDGPUDwarfDebug class, the AMDGPU-specific subclass
+// of DwarfDebug. It customizes DWARF emission for the AMDGPU DWARF extensions.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUDWARFDEBUG_H
+#define LLVM_LIB_TARGET_AMDGPU_AMDGPUDWARFDEBUG_H
+
+#include "../../CodeGen/AsmPrinter/DwarfDebug.h"
+
+namespace llvm {
+
+/// AMDGPU-specific DwarfDebug implementation.
+class AMDGPUDwarfDebug : public DwarfDebug {
+public:
+  AMDGPUDwarfDebug(AsmPrinter *A) : DwarfDebug(A) {}
+
+  /// The AMDGPU DWARF extensions describe a pointer type's address space with
+  /// DW_AT_LLVM_address_space rather than DW_AT_address_class.
+  dwarf::Attribute getTypeAddressSpaceAttribute() const override {
+    return dwarf::DW_AT_LLVM_address_space;
+  }
+};
+
+} // end namespace llvm
+
+#endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUDWARFDEBUG_H
diff --git a/llvm/test/DebugInfo/AMDGPU/dwarfdump-address-spaces.ll b/llvm/test/DebugInfo/AMDGPU/dwarfdump-address-spaces.ll
index 84f8af9e3051c..6e10ec8aeca26 100644
--- a/llvm/test/DebugInfo/AMDGPU/dwarfdump-address-spaces.ll
+++ b/llvm/test/DebugInfo/AMDGPU/dwarfdump-address-spaces.ll
@@ -1,3 +1,4 @@
+; REQUIRES: x86-registered-target
 ; RUN: llc -mtriple=x86_64--gnu -filetype=obj --verify-machineinstrs < %s | llvm-dwarfdump - 2>&1 | FileCheck %s --check-prefixes=COMMON,X86
 ; RUN: llc -mtriple=amdgpu9.00--amdhsa -filetype=obj < %s | llvm-dwarfdump - 2>&1 | FileCheck %s --check-prefixes=COMMON,AMDGPU
 

>From 6bfafddf682efce7d377cf327efdfb20312d50fd Mon Sep 17 00:00:00 2001
From: Gheorghe-Teodor Bercea <dobercea at amd.com>
Date: Mon, 14 Sep 2026 11:30:23 -0400
Subject: [PATCH 5/5] Address review comments: move the hook to AsmPrinter

---
 llvm/include/llvm/CodeGen/AsmPrinter.h      |  7 +++++
 llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h    |  7 -----
 llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp   |  2 +-
 llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp |  6 ----
 llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.h   |  7 +++--
 llvm/lib/Target/AMDGPU/AMDGPUDwarfDebug.h   | 35 ---------------------
 6 files changed, 13 insertions(+), 51 deletions(-)
 delete mode 100644 llvm/lib/Target/AMDGPU/AMDGPUDwarfDebug.h

diff --git a/llvm/include/llvm/CodeGen/AsmPrinter.h b/llvm/include/llvm/CodeGen/AsmPrinter.h
index f6c19280030a9..d3000ed3dd00f 100644
--- a/llvm/include/llvm/CodeGen/AsmPrinter.h
+++ b/llvm/include/llvm/CodeGen/AsmPrinter.h
@@ -285,6 +285,13 @@ class LLVM_ABI AsmPrinter : public MachineFunctionPass {
   virtual DwarfDebug *createDwarfDebug();
 
 public:
+  /// The attribute describing the DWARF address space of a pointer or
+  /// reference type. Targets whose DWARF extensions define their own attribute
+  /// for this can override it.
+  virtual dwarf::Attribute getTypeAddressSpaceAttribute() const {
+    return dwarf::DW_AT_address_class;
+  }
+
   ~AsmPrinter() override;
 
   DwarfDebug *getDwarfDebug() { return DD; }
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h
index d26d0806ffecb..73285711d0aa9 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h
@@ -772,13 +772,6 @@ class DwarfDebug : public DebugHandlerBase {
                               VariableLocationKind VarLocKind,
                               const GlobalVariable *GV = nullptr) const {}
 
-  /// The attribute describing the DWARF address space of a pointer or
-  /// reference type. Targets whose DWARF extensions define their own
-  /// attribute for this can override it.
-  virtual dwarf::Attribute getTypeAddressSpaceAttribute() const {
-    return dwarf::DW_AT_address_class;
-  }
-
   //===--------------------------------------------------------------------===//
   // Main entry points.
   //
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
index 5507a4e20cff8..4f847327ec5c2 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
@@ -914,7 +914,7 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) {
   // verifier checks that DWARF address space only exists for pointer
   // or reference types.
   if (auto AS = DTy->getDWARFAddressSpace())
-    addUInt(Buffer, DD->getTypeAddressSpaceAttribute(), dwarf::DW_FORM_data4,
+    addUInt(Buffer, Asm->getTypeAddressSpaceAttribute(), dwarf::DW_FORM_data4,
             *AS);
 
   // Add template alias template parameters.
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
index 3d097dc800ff4..11c482c83f1e1 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
@@ -17,7 +17,6 @@
 
 #include "AMDGPUAsmPrinter.h"
 #include "AMDGPU.h"
-#include "AMDGPUDwarfDebug.h"
 #include "AMDGPUHSAMetadataStreamer.h"
 #include "AMDGPUMCResourceInfo.h"
 #include "AMDGPUResourceUsageAnalysis.h"
@@ -132,11 +131,6 @@ StringRef AMDGPUAsmPrinter::getPassName() const {
   return "AMDGPU Assembly Printer";
 }
 
-/// Create AMDGPU-specific DwarfDebug handler.
-DwarfDebug *AMDGPUAsmPrinter::createDwarfDebug() {
-  return new AMDGPUDwarfDebug(this);
-}
-
 const MCSubtargetInfo *AMDGPUAsmPrinter::getGlobalSTI() const {
   return &TM.getMCSubtargetInfo();
 }
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.h b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.h
index 60e8b0529fe9f..49cd121232504 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.h
+++ b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.h
@@ -120,8 +120,11 @@ class AMDGPUAsmPrinter final : public AsmPrinter {
   bool doFinalization(Module &M) override;
   bool runOnMachineFunction(MachineFunction &MF) override;
 
-  /// Create AMDGPU-specific DwarfDebug handler.
-  DwarfDebug *createDwarfDebug() override;
+  /// The AMDGPU DWARF extensions describe a pointer type's address space with
+  /// DW_AT_LLVM_address_space rather than DW_AT_address_class.
+  dwarf::Attribute getTypeAddressSpaceAttribute() const override {
+    return dwarf::DW_AT_LLVM_address_space;
+  }
 
   /// Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated
   /// pseudo lowering.
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUDwarfDebug.h b/llvm/lib/Target/AMDGPU/AMDGPUDwarfDebug.h
deleted file mode 100644
index 54c4dc7ff13a8..0000000000000
--- a/llvm/lib/Target/AMDGPU/AMDGPUDwarfDebug.h
+++ /dev/null
@@ -1,35 +0,0 @@
-//===-- AMDGPUDwarfDebug.h - AMDGPU DwarfDebug Implementation -*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-//
-// This file declares the AMDGPUDwarfDebug class, the AMDGPU-specific subclass
-// of DwarfDebug. It customizes DWARF emission for the AMDGPU DWARF extensions.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUDWARFDEBUG_H
-#define LLVM_LIB_TARGET_AMDGPU_AMDGPUDWARFDEBUG_H
-
-#include "../../CodeGen/AsmPrinter/DwarfDebug.h"
-
-namespace llvm {
-
-/// AMDGPU-specific DwarfDebug implementation.
-class AMDGPUDwarfDebug : public DwarfDebug {
-public:
-  AMDGPUDwarfDebug(AsmPrinter *A) : DwarfDebug(A) {}
-
-  /// The AMDGPU DWARF extensions describe a pointer type's address space with
-  /// DW_AT_LLVM_address_space rather than DW_AT_address_class.
-  dwarf::Attribute getTypeAddressSpaceAttribute() const override {
-    return dwarf::DW_AT_LLVM_address_space;
-  }
-};
-
-} // end namespace llvm
-
-#endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUDWARFDEBUG_H



More information about the llvm-branch-commits mailing list