Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 2cee5cc

Browse files
committed
[DebugInfo] Emit address space with DW_AT_address_class attribute for pointer and reference types
Differential Revision: https://reviews.llvm.org/D29670 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@297320 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 94a8180 commit 2cee5cc

29 files changed

+411
-83
lines changed

bindings/go/llvm/DIBuilderBindings.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ LLVMMetadataRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,
119119
const char *Name) {
120120
DIBuilder *D = unwrap(Dref);
121121
return wrap(D->createPointerType(unwrap<DIType>(PointeeType), SizeInBits,
122-
AlignInBits, Name));
122+
AlignInBits, /* DWARFAddressSpace */ None,
123+
Name));
123124
}
124125

125126
LLVMMetadataRef

docs/AMDGPUUsage.rst

+14-10
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,24 @@ Address Spaces
1919

2020
The AMDGPU back-end uses the following address space mapping:
2121

22-
============= ============================================
23-
Address Space Memory Space
24-
============= ============================================
25-
0 Private
26-
1 Global
27-
2 Constant
28-
3 Local
29-
4 Generic (Flat)
30-
5 Region
31-
============= ============================================
22+
================== =================== ==============
23+
LLVM Address Space DWARF Address Space Memory Space
24+
================== =================== ==============
25+
0 1 Private
26+
1 N/A Global
27+
2 N/A Constant
28+
3 2 Local
29+
4 N/A Generic (Flat)
30+
5 N/A Region
31+
================== =================== ==============
3232

3333
The terminology in the table, aside from the region memory space, is from the
3434
OpenCL standard.
3535

36+
LLVM Address Space is used throughout LLVM (for example, in LLVM IR). DWARF
37+
Address Space is emitted in DWARF, and is used by tools, such as debugger,
38+
profiler and others.
39+
3640
Trap Handler ABI
3741
----------------
3842
The OS element of the target triple controls the trap handler behavior.

include/llvm/IR/DIBuilder.h

+10-5
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,15 @@ namespace llvm {
168168
DIDerivedType *createQualifiedType(unsigned Tag, DIType *FromTy);
169169

170170
/// Create debugging information entry for a pointer.
171-
/// \param PointeeTy Type pointed by this pointer.
172-
/// \param SizeInBits Size.
173-
/// \param AlignInBits Alignment. (optional)
174-
/// \param Name Pointer type name. (optional)
171+
/// \param PointeeTy Type pointed by this pointer.
172+
/// \param SizeInBits Size.
173+
/// \param AlignInBits Alignment. (optional)
174+
/// \param DWARFAddressSpace DWARF address space. (optional)
175+
/// \param Name Pointer type name. (optional)
175176
DIDerivedType *createPointerType(DIType *PointeeTy, uint64_t SizeInBits,
176177
uint32_t AlignInBits = 0,
178+
Optional<unsigned> DWARFAddressSpace =
179+
None,
177180
StringRef Name = "");
178181

179182
/// Create debugging information entry for a pointer to member.
@@ -190,7 +193,9 @@ namespace llvm {
190193
/// style reference or rvalue reference type.
191194
DIDerivedType *createReferenceType(unsigned Tag, DIType *RTy,
192195
uint64_t SizeInBits = 0,
193-
uint32_t AlignInBits = 0);
196+
uint32_t AlignInBits = 0,
197+
Optional<unsigned> DWARFAddressSpace =
198+
None);
194199

195200
/// Create debugging information entry for a typedef.
196201
/// \param Ty Original type.

include/llvm/IR/DebugInfoMetadata.h

+32-16
Original file line numberDiff line numberDiff line change
@@ -710,62 +710,78 @@ class DIDerivedType : public DIType {
710710
friend class LLVMContextImpl;
711711
friend class MDNode;
712712

713+
/// \brief The DWARF address space of the memory pointed to or referenced by a
714+
/// pointer or reference type respectively.
715+
Optional<unsigned> DWARFAddressSpace;
716+
713717
DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
714718
unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
715-
uint64_t OffsetInBits, DIFlags Flags, ArrayRef<Metadata *> Ops)
719+
uint64_t OffsetInBits, Optional<unsigned> DWARFAddressSpace,
720+
DIFlags Flags, ArrayRef<Metadata *> Ops)
716721
: DIType(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
717-
AlignInBits, OffsetInBits, Flags, Ops) {}
722+
AlignInBits, OffsetInBits, Flags, Ops),
723+
DWARFAddressSpace(DWARFAddressSpace) {}
718724
~DIDerivedType() = default;
719725

720726
static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
721727
StringRef Name, DIFile *File, unsigned Line,
722728
DIScopeRef Scope, DITypeRef BaseType,
723729
uint64_t SizeInBits, uint32_t AlignInBits,
724-
uint64_t OffsetInBits, DIFlags Flags,
725-
Metadata *ExtraData, StorageType Storage,
726-
bool ShouldCreate = true) {
730+
uint64_t OffsetInBits,
731+
Optional<unsigned> DWARFAddressSpace,
732+
DIFlags Flags, Metadata *ExtraData,
733+
StorageType Storage, bool ShouldCreate = true) {
727734
return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
728735
Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
729-
Flags, ExtraData, Storage, ShouldCreate);
736+
DWARFAddressSpace, Flags, ExtraData, Storage, ShouldCreate);
730737
}
731738
static DIDerivedType *getImpl(LLVMContext &Context, unsigned Tag,
732739
MDString *Name, Metadata *File, unsigned Line,
733740
Metadata *Scope, Metadata *BaseType,
734741
uint64_t SizeInBits, uint32_t AlignInBits,
735-
uint64_t OffsetInBits, DIFlags Flags,
736-
Metadata *ExtraData, StorageType Storage,
737-
bool ShouldCreate = true);
742+
uint64_t OffsetInBits,
743+
Optional<unsigned> DWARFAddressSpace,
744+
DIFlags Flags, Metadata *ExtraData,
745+
StorageType Storage, bool ShouldCreate = true);
738746

739747
TempDIDerivedType cloneImpl() const {
740748
return getTemporary(getContext(), getTag(), getName(), getFile(), getLine(),
741749
getScope(), getBaseType(), getSizeInBits(),
742-
getAlignInBits(), getOffsetInBits(), getFlags(),
743-
getExtraData());
750+
getAlignInBits(), getOffsetInBits(),
751+
getDWARFAddressSpace(), getFlags(), getExtraData());
744752
}
745753

746754
public:
747755
DEFINE_MDNODE_GET(DIDerivedType,
748756
(unsigned Tag, MDString *Name, Metadata *File,
749757
unsigned Line, Metadata *Scope, Metadata *BaseType,
750758
uint64_t SizeInBits, uint32_t AlignInBits,
751-
uint64_t OffsetInBits, DIFlags Flags,
759+
uint64_t OffsetInBits,
760+
Optional<unsigned> DWARFAddressSpace, DIFlags Flags,
752761
Metadata *ExtraData = nullptr),
753762
(Tag, Name, File, Line, Scope, BaseType, SizeInBits,
754-
AlignInBits, OffsetInBits, Flags, ExtraData))
763+
AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
764+
ExtraData))
755765
DEFINE_MDNODE_GET(DIDerivedType,
756766
(unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
757767
DIScopeRef Scope, DITypeRef BaseType, uint64_t SizeInBits,
758768
uint32_t AlignInBits, uint64_t OffsetInBits,
759-
DIFlags Flags, Metadata *ExtraData = nullptr),
769+
Optional<unsigned> DWARFAddressSpace, DIFlags Flags,
770+
Metadata *ExtraData = nullptr),
760771
(Tag, Name, File, Line, Scope, BaseType, SizeInBits,
761-
AlignInBits, OffsetInBits, Flags, ExtraData))
772+
AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
773+
ExtraData))
762774

763775
TempDIDerivedType clone() const { return cloneImpl(); }
764776

765-
//// Get the base type this is derived from.
777+
/// Get the base type this is derived from.
766778
DITypeRef getBaseType() const { return DITypeRef(getRawBaseType()); }
767779
Metadata *getRawBaseType() const { return getOperand(3); }
768780

781+
/// \returns The DWARF address space of the memory pointed to or referenced by
782+
/// a pointer or reference type respectively.
783+
Optional<unsigned> getDWARFAddressSpace() const { return DWARFAddressSpace; }
784+
769785
/// Get extra data associated with this derived type.
770786
///
771787
/// Class type for pointer-to-members, objective-c property node for ivars,

lib/AsmParser/LLParser.cpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -3908,7 +3908,8 @@ bool LLParser::ParseDIBasicType(MDNode *&Result, bool IsDistinct) {
39083908
/// ParseDIDerivedType:
39093909
/// ::= !DIDerivedType(tag: DW_TAG_pointer_type, name: "int", file: !0,
39103910
/// line: 7, scope: !1, baseType: !2, size: 32,
3911-
/// align: 32, offset: 0, flags: 0, extraData: !3)
3911+
/// align: 32, offset: 0, flags: 0, extraData: !3,
3912+
/// dwarfAddressSpace: 3)
39123913
bool LLParser::ParseDIDerivedType(MDNode *&Result, bool IsDistinct) {
39133914
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
39143915
REQUIRED(tag, DwarfTagField, ); \
@@ -3921,14 +3922,20 @@ bool LLParser::ParseDIDerivedType(MDNode *&Result, bool IsDistinct) {
39213922
OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
39223923
OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX)); \
39233924
OPTIONAL(flags, DIFlagField, ); \
3924-
OPTIONAL(extraData, MDField, );
3925+
OPTIONAL(extraData, MDField, ); \
3926+
OPTIONAL(dwarfAddressSpace, MDUnsignedField, (UINT32_MAX, UINT32_MAX));
39253927
PARSE_MD_FIELDS();
39263928
#undef VISIT_MD_FIELDS
39273929

3930+
Optional<unsigned> DWARFAddressSpace;
3931+
if (dwarfAddressSpace.Val != UINT32_MAX)
3932+
DWARFAddressSpace = dwarfAddressSpace.Val;
3933+
39283934
Result = GET_OR_DISTINCT(DIDerivedType,
39293935
(Context, tag.Val, name.Val, file.Val, line.Val,
39303936
scope.Val, baseType.Val, size.Val, align.Val,
3931-
offset.Val, flags.Val, extraData.Val));
3937+
offset.Val, DWARFAddressSpace, flags.Val,
3938+
extraData.Val));
39323939
return false;
39333940
}
39343941

lib/Bitcode/Reader/MetadataLoader.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -1110,9 +1110,15 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
11101110
break;
11111111
}
11121112
case bitc::METADATA_DERIVED_TYPE: {
1113-
if (Record.size() != 12)
1113+
if (Record.size() < 12 || Record.size() > 13)
11141114
return error("Invalid record");
11151115

1116+
// DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means
1117+
// that there is no DWARF address space associated with DIDerivedType.
1118+
Optional<unsigned> DWARFAddressSpace;
1119+
if (Record.size() > 12 && Record[12])
1120+
DWARFAddressSpace = Record[12] - 1;
1121+
11161122
IsDistinct = Record[0];
11171123
DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]);
11181124
MetadataList.assignValue(
@@ -1121,7 +1127,8 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
11211127
getMDOrNull(Record[3]), Record[4],
11221128
getDITypeRefOrNull(Record[5]),
11231129
getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1124-
Record[9], Flags, getDITypeRefOrNull(Record[11]))),
1130+
Record[9], DWARFAddressSpace, Flags,
1131+
getDITypeRefOrNull(Record[11]))),
11251132
NextMetadataNo);
11261133
NextMetadataNo++;
11271134
break;

lib/Bitcode/Writer/BitcodeWriter.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,13 @@ void ModuleBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
14731473
Record.push_back(N->getFlags());
14741474
Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
14751475

1476+
// DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means
1477+
// that there is no DWARF address space associated with DIDerivedType.
1478+
if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
1479+
Record.push_back(*DWARFAddressSpace + 1);
1480+
else
1481+
Record.push_back(0);
1482+
14761483
Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
14771484
Record.clear();
14781485
}

lib/CodeGen/AsmPrinter/DwarfUnit.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,13 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) {
853853
// Add source line info if available and TyDesc is not a forward declaration.
854854
if (!DTy->isForwardDecl())
855855
addSourceLine(Buffer, DTy);
856+
857+
// If DWARF address space value is other than None, add it for pointer and
858+
// reference types as DW_AT_address_class.
859+
if (DTy->getDWARFAddressSpace() && (Tag == dwarf::DW_TAG_pointer_type ||
860+
Tag == dwarf::DW_TAG_reference_type))
861+
addUInt(Buffer, dwarf::DW_AT_address_class, dwarf::DW_FORM_data4,
862+
DTy->getDWARFAddressSpace().getValue());
856863
}
857864

858865
void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args) {

lib/IR/AsmWriter.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,9 @@ static void writeDIDerivedType(raw_ostream &Out, const DIDerivedType *N,
16141614
Printer.printInt("offset", N->getOffsetInBits());
16151615
Printer.printDIFlags("flags", N->getFlags());
16161616
Printer.printMetadata("extraData", N->getRawExtraData());
1617+
if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
1618+
Printer.printInt("dwarfAddressSpace", *DWARFAddressSpace,
1619+
/* ShouldSkipZero */ false);
16171620
Out << ")";
16181621
}
16191622

lib/IR/DIBuilder.cpp

+25-18
Original file line numberDiff line numberDiff line change
@@ -241,17 +241,20 @@ DIBasicType *DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
241241

242242
DIDerivedType *DIBuilder::createQualifiedType(unsigned Tag, DIType *FromTy) {
243243
return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr, FromTy, 0,
244-
0, 0, DINode::FlagZero);
244+
0, 0, None, DINode::FlagZero);
245245
}
246246

247-
DIDerivedType *DIBuilder::createPointerType(DIType *PointeeTy,
248-
uint64_t SizeInBits,
249-
uint32_t AlignInBits,
250-
StringRef Name) {
247+
DIDerivedType *DIBuilder::createPointerType(
248+
DIType *PointeeTy,
249+
uint64_t SizeInBits,
250+
uint32_t AlignInBits,
251+
Optional<unsigned> DWARFAddressSpace,
252+
StringRef Name) {
251253
// FIXME: Why is there a name here?
252254
return DIDerivedType::get(VMContext, dwarf::DW_TAG_pointer_type, Name,
253255
nullptr, 0, nullptr, PointeeTy, SizeInBits,
254-
AlignInBits, 0, DINode::FlagZero);
256+
AlignInBits, 0, DWARFAddressSpace,
257+
DINode::FlagZero);
255258
}
256259

257260
DIDerivedType *DIBuilder::createMemberPointerType(DIType *PointeeTy,
@@ -261,38 +264,41 @@ DIDerivedType *DIBuilder::createMemberPointerType(DIType *PointeeTy,
261264
DINode::DIFlags Flags) {
262265
return DIDerivedType::get(VMContext, dwarf::DW_TAG_ptr_to_member_type, "",
263266
nullptr, 0, nullptr, PointeeTy, SizeInBits,
264-
AlignInBits, 0, Flags, Base);
267+
AlignInBits, 0, None, Flags, Base);
265268
}
266269

267-
DIDerivedType *DIBuilder::createReferenceType(unsigned Tag, DIType *RTy,
268-
uint64_t SizeInBits,
269-
uint32_t AlignInBits) {
270+
DIDerivedType *DIBuilder::createReferenceType(
271+
unsigned Tag, DIType *RTy,
272+
uint64_t SizeInBits,
273+
uint32_t AlignInBits,
274+
Optional<unsigned> DWARFAddressSpace) {
270275
assert(RTy && "Unable to create reference type");
271276
return DIDerivedType::get(VMContext, Tag, "", nullptr, 0, nullptr, RTy,
272-
SizeInBits, AlignInBits, 0, DINode::FlagZero);
277+
SizeInBits, AlignInBits, 0, DWARFAddressSpace,
278+
DINode::FlagZero);
273279
}
274280

275281
DIDerivedType *DIBuilder::createTypedef(DIType *Ty, StringRef Name,
276282
DIFile *File, unsigned LineNo,
277283
DIScope *Context) {
278284
return DIDerivedType::get(VMContext, dwarf::DW_TAG_typedef, Name, File,
279285
LineNo, getNonCompileUnitScope(Context), Ty, 0, 0,
280-
0, DINode::FlagZero);
286+
0, None, DINode::FlagZero);
281287
}
282288

283289
DIDerivedType *DIBuilder::createFriend(DIType *Ty, DIType *FriendTy) {
284290
assert(Ty && "Invalid type!");
285291
assert(FriendTy && "Invalid friend type!");
286292
return DIDerivedType::get(VMContext, dwarf::DW_TAG_friend, "", nullptr, 0, Ty,
287-
FriendTy, 0, 0, 0, DINode::FlagZero);
293+
FriendTy, 0, 0, 0, None, DINode::FlagZero);
288294
}
289295

290296
DIDerivedType *DIBuilder::createInheritance(DIType *Ty, DIType *BaseTy,
291297
uint64_t BaseOffset,
292298
DINode::DIFlags Flags) {
293299
assert(Ty && "Unable to create inheritance");
294300
return DIDerivedType::get(VMContext, dwarf::DW_TAG_inheritance, "", nullptr,
295-
0, Ty, BaseTy, 0, 0, BaseOffset, Flags);
301+
0, Ty, BaseTy, 0, 0, BaseOffset, None, Flags);
296302
}
297303

298304
DIDerivedType *DIBuilder::createMemberType(DIScope *Scope, StringRef Name,
@@ -303,7 +309,7 @@ DIDerivedType *DIBuilder::createMemberType(DIScope *Scope, StringRef Name,
303309
DINode::DIFlags Flags, DIType *Ty) {
304310
return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
305311
LineNumber, getNonCompileUnitScope(Scope), Ty,
306-
SizeInBits, AlignInBits, OffsetInBits, Flags);
312+
SizeInBits, AlignInBits, OffsetInBits, None, Flags);
307313
}
308314

309315
static ConstantAsMetadata *getConstantOrNull(Constant *C) {
@@ -320,7 +326,7 @@ DIDerivedType *DIBuilder::createBitFieldMemberType(
320326
return DIDerivedType::get(
321327
VMContext, dwarf::DW_TAG_member, Name, File, LineNumber,
322328
getNonCompileUnitScope(Scope), Ty, SizeInBits, /* AlignInBits */ 0,
323-
OffsetInBits, Flags,
329+
OffsetInBits, None, Flags,
324330
ConstantAsMetadata::get(ConstantInt::get(IntegerType::get(VMContext, 64),
325331
StorageOffsetInBits)));
326332
}
@@ -333,7 +339,8 @@ DIBuilder::createStaticMemberType(DIScope *Scope, StringRef Name, DIFile *File,
333339
Flags |= DINode::FlagStaticMember;
334340
return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
335341
LineNumber, getNonCompileUnitScope(Scope), Ty, 0,
336-
AlignInBits, 0, Flags, getConstantOrNull(Val));
342+
AlignInBits, 0, None, Flags,
343+
getConstantOrNull(Val));
337344
}
338345

339346
DIDerivedType *
@@ -343,7 +350,7 @@ DIBuilder::createObjCIVar(StringRef Name, DIFile *File, unsigned LineNumber,
343350
DIType *Ty, MDNode *PropertyNode) {
344351
return DIDerivedType::get(VMContext, dwarf::DW_TAG_member, Name, File,
345352
LineNumber, getNonCompileUnitScope(File), Ty,
346-
SizeInBits, AlignInBits, OffsetInBits, Flags,
353+
SizeInBits, AlignInBits, OffsetInBits, None, Flags,
347354
PropertyNode);
348355
}
349356

0 commit comments

Comments
 (0)