Skip to content

Commit 358a48b

Browse files
authored
[NVPTX] Fix DWARF address space for globals (#122715)
Fix an issue with defining actual DWARF address space for module scope globals. Previously it was always `ADDR_global_space`. Also, this patch introduces CUDA-specific DWARF codes for address space specification in correspondence with: https://docs.nvidia.com/cuda/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf-definitions Previously hardcoded constant values are replaced with enum values.
1 parent 2b3ddec commit 358a48b

File tree

3 files changed

+89
-31
lines changed

3 files changed

+89
-31
lines changed

llvm/include/llvm/Support/NVPTXAddrSpace.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace llvm {
1919
namespace NVPTXAS {
20+
2021
enum AddressSpace : unsigned {
2122
ADDRESS_SPACE_GENERIC = 0,
2223
ADDRESS_SPACE_GLOBAL = 1,
@@ -27,8 +28,30 @@ enum AddressSpace : unsigned {
2728

2829
ADDRESS_SPACE_PARAM = 101,
2930
};
30-
} // end namespace NVPTXAS
3131

32+
// According to official PTX Writer's Guide, DWARF debug information should
33+
// contain DW_AT_address_class attribute for all variables and parameters.
34+
// It's required for cuda-gdb to be able to properly reflect the memory space
35+
// of variable address. Acceptable address class codes are listed in this enum.
36+
//
37+
// More detailed information:
38+
// https://docs.nvidia.com/cuda/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf-definitions
39+
enum DWARF_AddressSpace : unsigned {
40+
DWARF_ADDR_code_space = 1,
41+
DWARF_ADDR_reg_space = 2,
42+
DWARF_ADDR_sreg_space = 3,
43+
DWARF_ADDR_const_space = 4,
44+
DWARF_ADDR_global_space = 5,
45+
DWARF_ADDR_local_space = 6,
46+
DWARF_ADDR_param_space = 7,
47+
DWARF_ADDR_shared_space = 8,
48+
DWARF_ADDR_surf_space = 9,
49+
DWARF_ADDR_tex_space = 10,
50+
DWARF_ADDR_tex_sampler_space = 11,
51+
DWARF_ADDR_generic_space = 12
52+
};
53+
54+
} // end namespace NVPTXAS
3255
} // end namespace llvm
3356

3457
#endif // LLVM_SUPPORT_NVPTXADDRSPACE_H

llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "llvm/MC/MCSymbolWasm.h"
3434
#include "llvm/MC/MachineLocation.h"
3535
#include "llvm/Support/CommandLine.h"
36+
#include "llvm/Support/NVPTXAddrSpace.h"
3637
#include "llvm/Target/TargetLoweringObjectFile.h"
3738
#include "llvm/Target/TargetMachine.h"
3839
#include "llvm/Target/TargetOptions.h"
@@ -75,6 +76,26 @@ static dwarf::Tag GetCompileUnitType(UnitKind Kind, DwarfDebug *DW) {
7576
return dwarf::DW_TAG_compile_unit;
7677
}
7778

79+
/// Translate NVVM IR address space code to DWARF correspondent value
80+
static unsigned translateToNVVMDWARFAddrSpace(unsigned AddrSpace) {
81+
switch (AddrSpace) {
82+
case NVPTXAS::ADDRESS_SPACE_GENERIC:
83+
return NVPTXAS::DWARF_ADDR_generic_space;
84+
case NVPTXAS::ADDRESS_SPACE_GLOBAL:
85+
return NVPTXAS::DWARF_ADDR_global_space;
86+
case NVPTXAS::ADDRESS_SPACE_SHARED:
87+
return NVPTXAS::DWARF_ADDR_shared_space;
88+
case NVPTXAS::ADDRESS_SPACE_CONST:
89+
return NVPTXAS::DWARF_ADDR_const_space;
90+
case NVPTXAS::ADDRESS_SPACE_LOCAL:
91+
return NVPTXAS::DWARF_ADDR_local_space;
92+
default:
93+
llvm_unreachable(
94+
"Cannot translate unknown address space to DWARF address space");
95+
return AddrSpace;
96+
}
97+
}
98+
7899
DwarfCompileUnit::DwarfCompileUnit(unsigned UID, const DICompileUnit *Node,
79100
AsmPrinter *A, DwarfDebug *DW,
80101
DwarfFile *DWU, UnitKind Kind)
@@ -264,14 +285,11 @@ void DwarfCompileUnit::addLocationAttribute(
264285
}
265286

266287
if (Expr) {
267-
// According to
268-
// https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
269-
// cuda-gdb requires DW_AT_address_class for all variables to be able to
270-
// correctly interpret address space of the variable address.
288+
// cuda-gdb special requirement. See NVPTXAS::DWARF_AddressSpace
271289
// Decode DW_OP_constu <DWARF Address Space> DW_OP_swap DW_OP_xderef
272-
// sequence for the NVPTX + gdb target.
273-
unsigned LocalNVPTXAddressSpace;
290+
// sequence to specify corresponding address space.
274291
if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) {
292+
unsigned LocalNVPTXAddressSpace;
275293
const DIExpression *NewExpr =
276294
DIExpression::extractAddressClass(Expr, LocalNVPTXAddressSpace);
277295
if (NewExpr != Expr) {
@@ -363,6 +381,10 @@ void DwarfCompileUnit::addLocationAttribute(
363381
DD->addArangeLabel(SymbolCU(this, Sym));
364382
addOpAddress(*Loc, Sym);
365383
}
384+
if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB() &&
385+
!NVPTXAddressSpace)
386+
NVPTXAddressSpace =
387+
translateToNVVMDWARFAddrSpace(Global->getType()->getAddressSpace());
366388
}
367389
// Global variables attached to symbols are memory locations.
368390
// It would be better if this were unconditional, but malformed input that
@@ -373,13 +395,9 @@ void DwarfCompileUnit::addLocationAttribute(
373395
DwarfExpr->addExpression(Expr);
374396
}
375397
if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) {
376-
// According to
377-
// https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
378-
// cuda-gdb requires DW_AT_address_class for all variables to be able to
379-
// correctly interpret address space of the variable address.
380-
const unsigned NVPTX_ADDR_global_space = 5;
398+
// cuda-gdb special requirement. See NVPTXAS::DWARF_AddressSpace
381399
addUInt(*VariableDIE, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1,
382-
NVPTXAddressSpace.value_or(NVPTX_ADDR_global_space));
400+
NVPTXAddressSpace.value_or(NVPTXAS::DWARF_ADDR_global_space));
383401
}
384402
if (Loc)
385403
addBlock(*VariableDIE, dwarf::DW_AT_location, DwarfExpr->finalize());
@@ -793,10 +811,10 @@ void DwarfCompileUnit::applyConcreteDbgVariableAttributes(
793811
const DbgValueLoc *DVal = &Single.getValueLoc();
794812
if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB() &&
795813
!Single.getExpr()) {
796-
// Lack of expression means it is a register. Registers for PTX need to
797-
// be marked with DW_AT_address_class = 2. See
798-
// https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
799-
addUInt(VariableDie, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1, 2);
814+
// cuda-gdb special requirement. See NVPTXAS::DWARF_AddressSpace
815+
// Lack of expression means it is a register.
816+
addUInt(VariableDie, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1,
817+
NVPTXAS::DWARF_ADDR_reg_space);
800818
}
801819
if (!DVal->isVariadic()) {
802820
const DbgValueLocEntry *Entry = DVal->getLocEntries().begin();
@@ -922,14 +940,11 @@ void DwarfCompileUnit::applyConcreteDbgVariableAttributes(const Loc::MMI &MMI,
922940
SmallVector<uint64_t, 8> Ops;
923941
TRI->getOffsetOpcodes(Offset, Ops);
924942

925-
// According to
926-
// https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
927-
// cuda-gdb requires DW_AT_address_class for all variables to be
928-
// able to correctly interpret address space of the variable
929-
// address. Decode DW_OP_constu <DWARF Address Space> DW_OP_swap
930-
// DW_OP_xderef sequence for the NVPTX + gdb target.
931-
unsigned LocalNVPTXAddressSpace;
943+
// cuda-gdb special requirement. See NVPTXAS::DWARF_AddressSpace.
944+
// Decode DW_OP_constu <DWARF Address Space> DW_OP_swap
945+
// DW_OP_xderef sequence to specify address space.
932946
if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) {
947+
unsigned LocalNVPTXAddressSpace;
933948
const DIExpression *NewExpr =
934949
DIExpression::extractAddressClass(Expr, LocalNVPTXAddressSpace);
935950
if (NewExpr != Expr) {
@@ -949,14 +964,9 @@ void DwarfCompileUnit::applyConcreteDbgVariableAttributes(const Loc::MMI &MMI,
949964
DwarfExpr.addExpression(std::move(Cursor));
950965
}
951966
if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) {
952-
// According to
953-
// https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf
954-
// cuda-gdb requires DW_AT_address_class for all variables to be
955-
// able to correctly interpret address space of the variable
956-
// address.
957-
const unsigned NVPTX_ADDR_local_space = 6;
967+
// cuda-gdb special requirement. See NVPTXAS::DWARF_AddressSpace.
958968
addUInt(VariableDie, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1,
959-
NVPTXAddressSpace.value_or(NVPTX_ADDR_local_space));
969+
NVPTXAddressSpace.value_or(NVPTXAS::DWARF_ADDR_local_space));
960970
}
961971
addBlock(VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize());
962972
if (DwarfExpr.TagOffset)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; RUN: llc < %s -march=nvptx64 | FileCheck %s
2+
3+
; Test that translateToNVVMDWARFAddrSpace() function translates NVVM IR address space
4+
; value `Shared` (3) to the corresponding DWARF DW_AT_address_class attribute for PTX.
5+
6+
; CHECK: .section .debug_info
7+
; CHECK: .b8 103 // DW_AT_name
8+
; CHECK-NEXT: .b8 0
9+
; CHECK-NEXT: .b32 55 // DW_AT_type
10+
; CHECK-NEXT: .b8 1 // DW_AT_decl_file
11+
; CHECK-NEXT: .b8 1 // DW_AT_decl_line
12+
; CHECK-NEXT: .b8 8 // DW_AT_address_class
13+
14+
@g = internal addrspace(3) global i32 0, align 4, !dbg !0
15+
16+
!llvm.dbg.cu = !{!2}
17+
!llvm.module.flags = !{!6}
18+
19+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
20+
!1 = distinct !DIGlobalVariable(name: "g", linkageName: "g", scope: !2, file: !3, line: 1, type: !5, isLocal: true, isDefinition: true)
21+
!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: !4)
22+
!3 = !DIFile(filename: "test.cu", directory: "test")
23+
!4 = !{!0}
24+
!5 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
25+
!6 = !{i32 1, !"Debug Info Version", i32 3}

0 commit comments

Comments
 (0)