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+
7899DwarfCompileUnit::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 )
0 commit comments