Skip to content

Commit 16dc394

Browse files
committed
[NVPTX] Fix DWARF address space for globals
There was issue with defining actual addr space for module scope globals. Previously it was always ADDR_global_space. Also, introduce enum with CUDA-specific DWARF address space codes.
1 parent 162397f commit 16dc394

File tree

3 files changed

+88
-31
lines changed

3 files changed

+88
-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,
@@ -26,8 +27,30 @@ enum AddressSpace : unsigned {
2627

2728
ADDRESS_SPACE_PARAM = 101,
2829
};
29-
} // end namespace NVPTXAS
3030

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

3356
#endif // LLVM_SUPPORT_NVPTXADDRSPACE_H

llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp

Lines changed: 39 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,25 @@ static dwarf::Tag GetCompileUnitType(UnitKind Kind, DwarfDebug *DW) {
7576
return dwarf::DW_TAG_compile_unit;
7677
}
7778

79+
static unsigned translateToNVVMDWARFAddrSpace(unsigned AddrSpace) {
80+
switch (AddrSpace) {
81+
case NVPTXAS::ADDRESS_SPACE_GENERIC:
82+
return NVPTXAS::DWARF_ADDR_generic_space;
83+
case NVPTXAS::ADDRESS_SPACE_GLOBAL:
84+
return NVPTXAS::DWARF_ADDR_global_space;
85+
case NVPTXAS::ADDRESS_SPACE_SHARED:
86+
return NVPTXAS::DWARF_ADDR_shared_space;
87+
case NVPTXAS::ADDRESS_SPACE_CONST:
88+
return NVPTXAS::DWARF_ADDR_const_space;
89+
case NVPTXAS::ADDRESS_SPACE_LOCAL:
90+
return NVPTXAS::DWARF_ADDR_local_space;
91+
default:
92+
llvm_unreachable(
93+
"Cannot translate unknown address space to DWARF address space");
94+
return AddrSpace;
95+
}
96+
}
97+
7898
DwarfCompileUnit::DwarfCompileUnit(unsigned UID, const DICompileUnit *Node,
7999
AsmPrinter *A, DwarfDebug *DW,
80100
DwarfFile *DWU, UnitKind Kind)
@@ -264,14 +284,11 @@ void DwarfCompileUnit::addLocationAttribute(
264284
}
265285

266286
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.
287+
// cuda-gdb special requirement. See NVPTXAS::DWARF_AddressSpace
271288
// Decode DW_OP_constu <DWARF Address Space> DW_OP_swap DW_OP_xderef
272-
// sequence for the NVPTX + gdb target.
273-
unsigned LocalNVPTXAddressSpace;
289+
// sequence to specify corresponding address space.
274290
if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) {
291+
unsigned LocalNVPTXAddressSpace;
275292
const DIExpression *NewExpr =
276293
DIExpression::extractAddressClass(Expr, LocalNVPTXAddressSpace);
277294
if (NewExpr != Expr) {
@@ -363,6 +380,10 @@ void DwarfCompileUnit::addLocationAttribute(
363380
DD->addArangeLabel(SymbolCU(this, Sym));
364381
addOpAddress(*Loc, Sym);
365382
}
383+
if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB() &&
384+
!NVPTXAddressSpace)
385+
NVPTXAddressSpace = translateToNVVMDWARFAddrSpace(
386+
Global->getType()->getAddressSpace());
366387
}
367388
// Global variables attached to symbols are memory locations.
368389
// It would be better if this were unconditional, but malformed input that
@@ -373,13 +394,9 @@ void DwarfCompileUnit::addLocationAttribute(
373394
DwarfExpr->addExpression(Expr);
374395
}
375396
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;
397+
// cuda-gdb special requirement. See NVPTXAS::DWARF_AddressSpace
381398
addUInt(*VariableDIE, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1,
382-
NVPTXAddressSpace.value_or(NVPTX_ADDR_global_space));
399+
NVPTXAddressSpace.value_or(NVPTXAS::DWARF_ADDR_global_space));
383400
}
384401
if (Loc)
385402
addBlock(*VariableDIE, dwarf::DW_AT_location, DwarfExpr->finalize());
@@ -793,10 +810,10 @@ void DwarfCompileUnit::applyConcreteDbgVariableAttributes(
793810
const DbgValueLoc *DVal = &Single.getValueLoc();
794811
if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB() &&
795812
!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);
813+
// cuda-gdb special requirement. See NVPTXAS::DWARF_AddressSpace
814+
// Lack of expression means it is a register.
815+
addUInt(VariableDie, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1,
816+
NVPTXAS::DWARF_ADDR_reg_space);
800817
}
801818
if (!DVal->isVariadic()) {
802819
const DbgValueLocEntry *Entry = DVal->getLocEntries().begin();
@@ -922,14 +939,11 @@ void DwarfCompileUnit::applyConcreteDbgVariableAttributes(const Loc::MMI &MMI,
922939
SmallVector<uint64_t, 8> Ops;
923940
TRI->getOffsetOpcodes(Offset, Ops);
924941

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;
942+
// cuda-gdb special requirement. See NVPTXAS::DWARF_AddressSpace.
943+
// Decode DW_OP_constu <DWARF Address Space> DW_OP_swap
944+
// DW_OP_xderef sequence to specify address space.
932945
if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) {
946+
unsigned LocalNVPTXAddressSpace;
933947
const DIExpression *NewExpr =
934948
DIExpression::extractAddressClass(Expr, LocalNVPTXAddressSpace);
935949
if (NewExpr != Expr) {
@@ -949,14 +963,9 @@ void DwarfCompileUnit::applyConcreteDbgVariableAttributes(const Loc::MMI &MMI,
949963
DwarfExpr.addExpression(std::move(Cursor));
950964
}
951965
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;
966+
// cuda-gdb special requirement. See NVPTXAS::DWARF_AddressSpace.
958967
addUInt(VariableDie, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1,
959-
NVPTXAddressSpace.value_or(NVPTX_ADDR_local_space));
968+
NVPTXAddressSpace.value_or(NVPTXAS::DWARF_ADDR_local_space));
960969
}
961970
addBlock(VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize());
962971
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)