Skip to content

Commit a5b72ab

Browse files
committed
[DebugInfo] Enhance DIImportedEntity to accept children entities
New field `elements` is added to '!DIImportedEntity', representing list of aliased entities. This is needed to dump optimized debugging information where all names in a module are imported, but a few names are imported with overriding aliases. Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D109343
1 parent a01f772 commit a5b72ab

File tree

20 files changed

+478
-127
lines changed

20 files changed

+478
-127
lines changed

llvm/docs/LangRef.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5790,12 +5790,16 @@ DIImportedEntity
57905790
""""""""""""""""
57915791

57925792
``DIImportedEntity`` nodes represent entities (such as modules) imported into a
5793-
compile unit.
5793+
compile unit. The ``elements`` field is a list of renamed entities (such as
5794+
variables and subprograms) in the imported entity (such as module).
57945795

57955796
.. code-block:: text
57965797

57975798
!2 = !DIImportedEntity(tag: DW_TAG_imported_module, name: "foo", scope: !0,
5798-
entity: !1, line: 7)
5799+
entity: !1, line: 7, elements: !3)
5800+
!3 = !{!4}
5801+
!4 = !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "bar", scope: !0,
5802+
entity: !5, line: 7)
57995803

58005804
DIMacro
58015805
"""""""

llvm/include/llvm-c/DebugInfo.h

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -389,48 +389,48 @@ LLVMDIBuilderCreateImportedModuleFromNamespace(LLVMDIBuilderRef Builder,
389389
* \param ImportedEntity Previous imported entity to alias.
390390
* \param File File where the declaration is located.
391391
* \param Line Line number of the declaration.
392+
* \param Elements Renamed elements.
393+
* \param NumElements Number of renamed elements.
392394
*/
393-
LLVMMetadataRef
394-
LLVMDIBuilderCreateImportedModuleFromAlias(LLVMDIBuilderRef Builder,
395-
LLVMMetadataRef Scope,
396-
LLVMMetadataRef ImportedEntity,
397-
LLVMMetadataRef File,
398-
unsigned Line);
395+
LLVMMetadataRef LLVMDIBuilderCreateImportedModuleFromAlias(
396+
LLVMDIBuilderRef Builder, LLVMMetadataRef Scope,
397+
LLVMMetadataRef ImportedEntity, LLVMMetadataRef File, unsigned Line,
398+
LLVMMetadataRef *Elements, unsigned NumElements);
399399

400400
/**
401401
* Create a descriptor for an imported module.
402-
* \param Builder The \c DIBuilder.
403-
* \param Scope The scope this module is imported into
404-
* \param M The module being imported here
405-
* \param File File where the declaration is located.
406-
* \param Line Line number of the declaration.
402+
* \param Builder The \c DIBuilder.
403+
* \param Scope The scope this module is imported into
404+
* \param M The module being imported here
405+
* \param File File where the declaration is located.
406+
* \param Line Line number of the declaration.
407+
* \param Elements Renamed elements.
408+
* \param NumElements Number of renamed elements.
407409
*/
408-
LLVMMetadataRef
409-
LLVMDIBuilderCreateImportedModuleFromModule(LLVMDIBuilderRef Builder,
410-
LLVMMetadataRef Scope,
411-
LLVMMetadataRef M,
412-
LLVMMetadataRef File,
413-
unsigned Line);
410+
LLVMMetadataRef LLVMDIBuilderCreateImportedModuleFromModule(
411+
LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, LLVMMetadataRef M,
412+
LLVMMetadataRef File, unsigned Line, LLVMMetadataRef *Elements,
413+
unsigned NumElements);
414414

415415
/**
416416
* Create a descriptor for an imported function, type, or variable. Suitable
417417
* for e.g. FORTRAN-style USE declarations.
418-
* \param Builder The DIBuilder.
419-
* \param Scope The scope this module is imported into.
420-
* \param Decl The declaration (or definition) of a function, type,
421-
or variable.
422-
* \param File File where the declaration is located.
423-
* \param Line Line number of the declaration.
424-
* \param Name A name that uniquely identifies this imported declaration.
425-
* \param NameLen The length of the C string passed to \c Name.
426-
*/
427-
LLVMMetadataRef
428-
LLVMDIBuilderCreateImportedDeclaration(LLVMDIBuilderRef Builder,
429-
LLVMMetadataRef Scope,
430-
LLVMMetadataRef Decl,
431-
LLVMMetadataRef File,
432-
unsigned Line,
433-
const char *Name, size_t NameLen);
418+
* \param Builder The DIBuilder.
419+
* \param Scope The scope this module is imported into.
420+
* \param Decl The declaration (or definition) of a function, type,
421+
or variable.
422+
* \param File File where the declaration is located.
423+
* \param Line Line number of the declaration.
424+
* \param Name A name that uniquely identifies this imported
425+
declaration.
426+
* \param NameLen The length of the C string passed to \c Name.
427+
* \param Elements Renamed elements.
428+
* \param NumElements Number of renamed elements.
429+
*/
430+
LLVMMetadataRef LLVMDIBuilderCreateImportedDeclaration(
431+
LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, LLVMMetadataRef Decl,
432+
LLVMMetadataRef File, unsigned Line, const char *Name, size_t NameLen,
433+
LLVMMetadataRef *Elements, unsigned NumElements);
434434

435435
/**
436436
* Creates a new DebugLocation that describes a source location.

llvm/include/llvm/IR/DIBuilder.h

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -827,39 +827,47 @@ namespace llvm {
827827
unsigned Line, unsigned Col);
828828

829829
/// Create a descriptor for an imported module.
830-
/// \param Context The scope this module is imported into
831-
/// \param NS The namespace being imported here.
832-
/// \param File File where the declaration is located.
833-
/// \param Line Line number of the declaration.
830+
/// \param Context The scope this module is imported into
831+
/// \param NS The namespace being imported here.
832+
/// \param File File where the declaration is located.
833+
/// \param Line Line number of the declaration.
834+
/// \param Elements Renamed elements.
834835
DIImportedEntity *createImportedModule(DIScope *Context, DINamespace *NS,
835-
DIFile *File, unsigned Line);
836+
DIFile *File, unsigned Line,
837+
DINodeArray Elements = nullptr);
836838

837839
/// Create a descriptor for an imported module.
838840
/// \param Context The scope this module is imported into.
839841
/// \param NS An aliased namespace.
840842
/// \param File File where the declaration is located.
841843
/// \param Line Line number of the declaration.
844+
/// \param Elements Renamed elements.
842845
DIImportedEntity *createImportedModule(DIScope *Context,
843846
DIImportedEntity *NS, DIFile *File,
844-
unsigned Line);
847+
unsigned Line,
848+
DINodeArray Elements = nullptr);
845849

846850
/// Create a descriptor for an imported module.
847-
/// \param Context The scope this module is imported into.
848-
/// \param M The module being imported here
849-
/// \param File File where the declaration is located.
850-
/// \param Line Line number of the declaration.
851+
/// \param Context The scope this module is imported into.
852+
/// \param M The module being imported here
853+
/// \param File File where the declaration is located.
854+
/// \param Line Line number of the declaration.
855+
/// \param Elements Renamed elements.
851856
DIImportedEntity *createImportedModule(DIScope *Context, DIModule *M,
852-
DIFile *File, unsigned Line);
857+
DIFile *File, unsigned Line,
858+
DINodeArray Elements = nullptr);
853859

854860
/// Create a descriptor for an imported function.
855861
/// \param Context The scope this module is imported into.
856862
/// \param Decl The declaration (or definition) of a function, type, or
857863
/// variable.
858864
/// \param File File where the declaration is located.
859865
/// \param Line Line number of the declaration.
866+
/// \param Elements Renamed elements.
860867
DIImportedEntity *createImportedDeclaration(DIScope *Context, DINode *Decl,
861868
DIFile *File, unsigned Line,
862-
StringRef Name = "");
869+
StringRef Name = "",
870+
DINodeArray Elements = nullptr);
863871

864872
/// Insert a new llvm.dbg.declare intrinsic call.
865873
/// \param Storage llvm::Value of the variable

llvm/include/llvm/IR/DebugInfoMetadata.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3347,31 +3347,33 @@ class DIImportedEntity : public DINode {
33473347
static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
33483348
DIScope *Scope, DINode *Entity, DIFile *File,
33493349
unsigned Line, StringRef Name,
3350-
StorageType Storage,
3350+
DINodeArray Elements, StorageType Storage,
33513351
bool ShouldCreate = true) {
33523352
return getImpl(Context, Tag, Scope, Entity, File, Line,
3353-
getCanonicalMDString(Context, Name), Storage, ShouldCreate);
3353+
getCanonicalMDString(Context, Name), Elements.get(), Storage,
3354+
ShouldCreate);
33543355
}
3355-
static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
3356-
Metadata *Scope, Metadata *Entity,
3357-
Metadata *File, unsigned Line,
3358-
MDString *Name, StorageType Storage,
3359-
bool ShouldCreate = true);
3356+
static DIImportedEntity *
3357+
getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, Metadata *Entity,
3358+
Metadata *File, unsigned Line, MDString *Name, Metadata *Elements,
3359+
StorageType Storage, bool ShouldCreate = true);
33603360

33613361
TempDIImportedEntity cloneImpl() const {
33623362
return getTemporary(getContext(), getTag(), getScope(), getEntity(),
3363-
getFile(), getLine(), getName());
3363+
getFile(), getLine(), getName(), getElements());
33643364
}
33653365

33663366
public:
33673367
DEFINE_MDNODE_GET(DIImportedEntity,
33683368
(unsigned Tag, DIScope *Scope, DINode *Entity, DIFile *File,
3369-
unsigned Line, StringRef Name = ""),
3370-
(Tag, Scope, Entity, File, Line, Name))
3369+
unsigned Line, StringRef Name = "",
3370+
DINodeArray Elements = nullptr),
3371+
(Tag, Scope, Entity, File, Line, Name, Elements))
33713372
DEFINE_MDNODE_GET(DIImportedEntity,
33723373
(unsigned Tag, Metadata *Scope, Metadata *Entity,
3373-
Metadata *File, unsigned Line, MDString *Name),
3374-
(Tag, Scope, Entity, File, Line, Name))
3374+
Metadata *File, unsigned Line, MDString *Name,
3375+
Metadata *Elements = nullptr),
3376+
(Tag, Scope, Entity, File, Line, Name, Elements))
33753377

33763378
TempDIImportedEntity clone() const { return cloneImpl(); }
33773379

@@ -3380,11 +3382,15 @@ class DIImportedEntity : public DINode {
33803382
DINode *getEntity() const { return cast_or_null<DINode>(getRawEntity()); }
33813383
StringRef getName() const { return getStringOperand(2); }
33823384
DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
3385+
DINodeArray getElements() const {
3386+
return cast_or_null<MDTuple>(getRawElements());
3387+
}
33833388

33843389
Metadata *getRawScope() const { return getOperand(0); }
33853390
Metadata *getRawEntity() const { return getOperand(1); }
33863391
MDString *getRawName() const { return getOperandAs<MDString>(2); }
33873392
Metadata *getRawFile() const { return getOperand(3); }
3393+
Metadata *getRawElements() const { return getOperand(4); }
33883394

33893395
static bool classof(const Metadata *MD) {
33903396
return MD->getMetadataID() == DIImportedEntityKind;

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5114,21 +5114,22 @@ bool LLParser::parseDIObjCProperty(MDNode *&Result, bool IsDistinct) {
51145114

51155115
/// parseDIImportedEntity:
51165116
/// ::= !DIImportedEntity(tag: DW_TAG_imported_module, scope: !0, entity: !1,
5117-
/// line: 7, name: "foo")
5117+
/// line: 7, name: "foo", elements: !2)
51185118
bool LLParser::parseDIImportedEntity(MDNode *&Result, bool IsDistinct) {
51195119
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
51205120
REQUIRED(tag, DwarfTagField, ); \
51215121
REQUIRED(scope, MDField, ); \
51225122
OPTIONAL(entity, MDField, ); \
51235123
OPTIONAL(file, MDField, ); \
51245124
OPTIONAL(line, LineField, ); \
5125-
OPTIONAL(name, MDStringField, );
5125+
OPTIONAL(name, MDStringField, ); \
5126+
OPTIONAL(elements, MDField, );
51265127
PARSE_MD_FIELDS();
51275128
#undef VISIT_MD_FIELDS
51285129

5129-
Result = GET_OR_DISTINCT(
5130-
DIImportedEntity,
5131-
(Context, tag.Val, scope.Val, entity.Val, file.Val, line.Val, name.Val));
5130+
Result = GET_OR_DISTINCT(DIImportedEntity,
5131+
(Context, tag.Val, scope.Val, entity.Val, file.Val,
5132+
line.Val, name.Val, elements.Val));
51325133
return false;
51335134
}
51345135

llvm/lib/Bitcode/Reader/MetadataLoader.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,17 +2044,19 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
20442044
break;
20452045
}
20462046
case bitc::METADATA_IMPORTED_ENTITY: {
2047-
if (Record.size() != 6 && Record.size() != 7)
2047+
if (Record.size() < 6 && Record.size() > 8)
20482048
return error("Invalid record");
20492049

20502050
IsDistinct = Record[0];
2051-
bool HasFile = (Record.size() == 7);
2051+
bool HasFile = (Record.size() >= 7);
2052+
bool HasElements = (Record.size() >= 8);
20522053
MetadataList.assignValue(
20532054
GET_OR_DISTINCT(DIImportedEntity,
20542055
(Context, Record[1], getMDOrNull(Record[2]),
20552056
getDITypeRefOrNull(Record[3]),
20562057
HasFile ? getMDOrNull(Record[6]) : nullptr,
2057-
HasFile ? Record[4] : 0, getMDString(Record[5]))),
2058+
HasFile ? Record[4] : 0, getMDString(Record[5]),
2059+
HasElements ? getMDOrNull(Record[7]) : nullptr)),
20582060
NextMetadataNo);
20592061
NextMetadataNo++;
20602062
break;

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,6 +2062,7 @@ void ModuleBitcodeWriter::writeDIImportedEntity(
20622062
Record.push_back(N->getLine());
20632063
Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
20642064
Record.push_back(VE.getMetadataOrNullID(N->getRawFile()));
2065+
Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
20652066

20662067
Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
20672068
Record.clear();

llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,16 @@ DIE *DwarfCompileUnit::constructImportedEntityDIE(
12691269
if (!Name.empty())
12701270
addString(*IMDie, dwarf::DW_AT_name, Name);
12711271

1272+
// This is for imported module with renamed entities (such as variables and
1273+
// subprograms).
1274+
DINodeArray Elements = Module->getElements();
1275+
for (const auto *Element : Elements) {
1276+
if (!Element)
1277+
continue;
1278+
IMDie->addChild(
1279+
constructImportedEntityDIE(cast<DIImportedEntity>(Element)));
1280+
}
1281+
12721282
return IMDie;
12731283
}
12741284

llvm/lib/IR/AsmWriter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,6 +2411,7 @@ static void writeDIImportedEntity(raw_ostream &Out, const DIImportedEntity *N,
24112411
Printer.printMetadata("entity", N->getRawEntity());
24122412
Printer.printMetadata("file", N->getRawFile());
24132413
Printer.printInt("line", N->getLine());
2414+
Printer.printMetadata("elements", N->getRawElements());
24142415
Out << ")";
24152416
}
24162417

llvm/lib/IR/DIBuilder.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,13 @@ DICompileUnit *DIBuilder::createCompileUnit(
165165
static DIImportedEntity *
166166
createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope *Context,
167167
Metadata *NS, DIFile *File, unsigned Line, StringRef Name,
168+
DINodeArray Elements,
168169
SmallVectorImpl<TrackingMDNodeRef> &AllImportedModules) {
169170
if (Line)
170171
assert(File && "Source location has line number but no file");
171172
unsigned EntitiesCount = C.pImpl->DIImportedEntitys.size();
172173
auto *M = DIImportedEntity::get(C, Tag, Context, cast_or_null<DINode>(NS),
173-
File, Line, Name);
174+
File, Line, Name, Elements);
174175
if (EntitiesCount < C.pImpl->DIImportedEntitys.size())
175176
// A new Imported Entity was just added to the context.
176177
// Add it to the Imported Modules list.
@@ -180,36 +181,38 @@ createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope *Context,
180181

181182
DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
182183
DINamespace *NS, DIFile *File,
183-
unsigned Line) {
184+
unsigned Line,
185+
DINodeArray Elements) {
184186
return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
185-
Context, NS, File, Line, StringRef(),
187+
Context, NS, File, Line, StringRef(), Elements,
186188
AllImportedModules);
187189
}
188190

189191
DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
190192
DIImportedEntity *NS,
191-
DIFile *File, unsigned Line) {
193+
DIFile *File, unsigned Line,
194+
DINodeArray Elements) {
192195
return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
193-
Context, NS, File, Line, StringRef(),
196+
Context, NS, File, Line, StringRef(), Elements,
194197
AllImportedModules);
195198
}
196199

197200
DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context, DIModule *M,
198-
DIFile *File, unsigned Line) {
201+
DIFile *File, unsigned Line,
202+
DINodeArray Elements) {
199203
return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
200-
Context, M, File, Line, StringRef(),
204+
Context, M, File, Line, StringRef(), Elements,
201205
AllImportedModules);
202206
}
203207

204-
DIImportedEntity *DIBuilder::createImportedDeclaration(DIScope *Context,
205-
DINode *Decl,
206-
DIFile *File,
207-
unsigned Line,
208-
StringRef Name) {
208+
DIImportedEntity *
209+
DIBuilder::createImportedDeclaration(DIScope *Context, DINode *Decl,
210+
DIFile *File, unsigned Line,
211+
StringRef Name, DINodeArray Elements) {
209212
// Make sure to use the unique identifier based metadata reference for
210213
// types that have one.
211214
return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
212-
Context, Decl, File, Line, Name,
215+
Context, Decl, File, Line, Name, Elements,
213216
AllImportedModules);
214217
}
215218

0 commit comments

Comments
 (0)