Skip to content

Commit 0254365

Browse files
authored
[DebugInfo] Enhance DIImportedEntity to accept children entities (#93)
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 40810c2 commit 0254365

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
@@ -5758,12 +5758,16 @@ DIImportedEntity
57585758
""""""""""""""""
57595759

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

57635764
.. code-block:: text
57645765

57655766
!2 = !DIImportedEntity(tag: DW_TAG_imported_module, name: "foo", scope: !0,
5766-
entity: !1, line: 7)
5767+
entity: !1, line: 7, elements: !3)
5768+
!3 = !{!4}
5769+
!4 = !DIImportedEntity(tag: DW_TAG_imported_declaration, name: "bar", scope: !0,
5770+
entity: !5, line: 7)
57675771

57685772
DIMacro
57695773
"""""""

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
@@ -831,39 +831,47 @@ namespace llvm {
831831
unsigned Line, unsigned Col);
832832

833833
/// Create a descriptor for an imported module.
834-
/// \param Context The scope this module is imported into
835-
/// \param NS The namespace being imported here.
836-
/// \param File File where the declaration is located.
837-
/// \param Line Line number of the declaration.
834+
/// \param Context The scope this module is imported into
835+
/// \param NS The namespace being imported here.
836+
/// \param File File where the declaration is located.
837+
/// \param Line Line number of the declaration.
838+
/// \param Elements Renamed elements.
838839
DIImportedEntity *createImportedModule(DIScope *Context, DINamespace *NS,
839-
DIFile *File, unsigned Line);
840+
DIFile *File, unsigned Line,
841+
DINodeArray Elements = nullptr);
840842

841843
/// Create a descriptor for an imported module.
842844
/// \param Context The scope this module is imported into.
843845
/// \param NS An aliased namespace.
844846
/// \param File File where the declaration is located.
845847
/// \param Line Line number of the declaration.
848+
/// \param Elements Renamed elements.
846849
DIImportedEntity *createImportedModule(DIScope *Context,
847850
DIImportedEntity *NS, DIFile *File,
848-
unsigned Line);
851+
unsigned Line,
852+
DINodeArray Elements = nullptr);
849853

850854
/// Create a descriptor for an imported module.
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 Context The scope this module is imported into.
856+
/// \param M The module being imported here
857+
/// \param File File where the declaration is located.
858+
/// \param Line Line number of the declaration.
859+
/// \param Elements Renamed elements.
855860
DIImportedEntity *createImportedModule(DIScope *Context, DIModule *M,
856-
DIFile *File, unsigned Line);
861+
DIFile *File, unsigned Line,
862+
DINodeArray Elements = nullptr);
857863

858864
/// Create a descriptor for an imported function.
859865
/// \param Context The scope this module is imported into.
860866
/// \param Decl The declaration (or definition) of a function, type, or
861867
/// variable.
862868
/// \param File File where the declaration is located.
863869
/// \param Line Line number of the declaration.
870+
/// \param Elements Renamed elements.
864871
DIImportedEntity *createImportedDeclaration(DIScope *Context, DINode *Decl,
865872
DIFile *File, unsigned Line,
866-
StringRef Name = "");
873+
StringRef Name = "",
874+
DINodeArray Elements = nullptr);
867875

868876
/// Insert a new llvm.dbg.declare intrinsic call.
869877
/// \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
@@ -3303,31 +3303,33 @@ class DIImportedEntity : public DINode {
33033303
static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
33043304
DIScope *Scope, DINode *Entity, DIFile *File,
33053305
unsigned Line, StringRef Name,
3306-
StorageType Storage,
3306+
DINodeArray Elements, StorageType Storage,
33073307
bool ShouldCreate = true) {
33083308
return getImpl(Context, Tag, Scope, Entity, File, Line,
3309-
getCanonicalMDString(Context, Name), Storage, ShouldCreate);
3309+
getCanonicalMDString(Context, Name), Elements.get(), Storage,
3310+
ShouldCreate);
33103311
}
3311-
static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
3312-
Metadata *Scope, Metadata *Entity,
3313-
Metadata *File, unsigned Line,
3314-
MDString *Name, StorageType Storage,
3315-
bool ShouldCreate = true);
3312+
static DIImportedEntity *
3313+
getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, Metadata *Entity,
3314+
Metadata *File, unsigned Line, MDString *Name, Metadata *Elements,
3315+
StorageType Storage, bool ShouldCreate = true);
33163316

33173317
TempDIImportedEntity cloneImpl() const {
33183318
return getTemporary(getContext(), getTag(), getScope(), getEntity(),
3319-
getFile(), getLine(), getName());
3319+
getFile(), getLine(), getName(), getElements());
33203320
}
33213321

33223322
public:
33233323
DEFINE_MDNODE_GET(DIImportedEntity,
33243324
(unsigned Tag, DIScope *Scope, DINode *Entity, DIFile *File,
3325-
unsigned Line, StringRef Name = ""),
3326-
(Tag, Scope, Entity, File, Line, Name))
3325+
unsigned Line, StringRef Name = "",
3326+
DINodeArray Elements = nullptr),
3327+
(Tag, Scope, Entity, File, Line, Name, Elements))
33273328
DEFINE_MDNODE_GET(DIImportedEntity,
33283329
(unsigned Tag, Metadata *Scope, Metadata *Entity,
3329-
Metadata *File, unsigned Line, MDString *Name),
3330-
(Tag, Scope, Entity, File, Line, Name))
3330+
Metadata *File, unsigned Line, MDString *Name,
3331+
Metadata *Elements = nullptr),
3332+
(Tag, Scope, Entity, File, Line, Name, Elements))
33313333

33323334
TempDIImportedEntity clone() const { return cloneImpl(); }
33333335

@@ -3336,11 +3338,15 @@ class DIImportedEntity : public DINode {
33363338
DINode *getEntity() const { return cast_or_null<DINode>(getRawEntity()); }
33373339
StringRef getName() const { return getStringOperand(2); }
33383340
DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
3341+
DINodeArray getElements() const {
3342+
return cast_or_null<MDTuple>(getRawElements());
3343+
}
33393344

33403345
Metadata *getRawScope() const { return getOperand(0); }
33413346
Metadata *getRawEntity() const { return getOperand(1); }
33423347
MDString *getRawName() const { return getOperandAs<MDString>(2); }
33433348
Metadata *getRawFile() const { return getOperand(3); }
3349+
Metadata *getRawElements() const { return getOperand(4); }
33443350

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

llvm/lib/AsmParser/LLParser.cpp

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

51555155
/// parseDIImportedEntity:
51565156
/// ::= !DIImportedEntity(tag: DW_TAG_imported_module, scope: !0, entity: !1,
5157-
/// line: 7, name: "foo")
5157+
/// line: 7, name: "foo", elements: !2)
51585158
bool LLParser::parseDIImportedEntity(MDNode *&Result, bool IsDistinct) {
51595159
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
51605160
REQUIRED(tag, DwarfTagField, ); \
51615161
REQUIRED(scope, MDField, ); \
51625162
OPTIONAL(entity, MDField, ); \
51635163
OPTIONAL(file, MDField, ); \
51645164
OPTIONAL(line, LineField, ); \
5165-
OPTIONAL(name, MDStringField, );
5165+
OPTIONAL(name, MDStringField, ); \
5166+
OPTIONAL(elements, MDField, );
51665167
PARSE_MD_FIELDS();
51675168
#undef VISIT_MD_FIELDS
51685169

5169-
Result = GET_OR_DISTINCT(
5170-
DIImportedEntity,
5171-
(Context, tag.Val, scope.Val, entity.Val, file.Val, line.Val, name.Val));
5170+
Result = GET_OR_DISTINCT(DIImportedEntity,
5171+
(Context, tag.Val, scope.Val, entity.Val, file.Val,
5172+
line.Val, name.Val, elements.Val));
51725173
return false;
51735174
}
51745175

llvm/lib/Bitcode/Reader/MetadataLoader.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,17 +2035,19 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
20352035
break;
20362036
}
20372037
case bitc::METADATA_IMPORTED_ENTITY: {
2038-
if (Record.size() != 6 && Record.size() != 7)
2038+
if (Record.size() < 6 && Record.size() > 8)
20392039
return error("Invalid record");
20402040

20412041
IsDistinct = Record[0];
2042-
bool HasFile = (Record.size() == 7);
2042+
bool HasFile = (Record.size() >= 7);
2043+
bool HasElements = (Record.size() >= 8);
20432044
MetadataList.assignValue(
20442045
GET_OR_DISTINCT(DIImportedEntity,
20452046
(Context, Record[1], getMDOrNull(Record[2]),
20462047
getDITypeRefOrNull(Record[3]),
20472048
HasFile ? getMDOrNull(Record[6]) : nullptr,
2048-
HasFile ? Record[4] : 0, getMDString(Record[5]))),
2049+
HasFile ? Record[4] : 0, getMDString(Record[5]),
2050+
HasElements ? getMDOrNull(Record[7]) : nullptr)),
20492051
NextMetadataNo);
20502052
NextMetadataNo++;
20512053
break;

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,6 +2057,7 @@ void ModuleBitcodeWriter::writeDIImportedEntity(
20572057
Record.push_back(N->getLine());
20582058
Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
20592059
Record.push_back(VE.getMetadataOrNullID(N->getRawFile()));
2060+
Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
20602061

20612062
Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
20622063
Record.clear();

llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp

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

1278+
// This is for imported module with renamed entities (such as variables and
1279+
// subprograms).
1280+
DINodeArray Elements = Module->getElements();
1281+
for (const auto *Element : Elements) {
1282+
if (!Element)
1283+
continue;
1284+
IMDie->addChild(
1285+
constructImportedEntityDIE(cast<DIImportedEntity>(Element)));
1286+
}
1287+
12781288
return IMDie;
12791289
}
12801290

llvm/lib/IR/AsmWriter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,6 +2408,7 @@ static void writeDIImportedEntity(raw_ostream &Out, const DIImportedEntity *N,
24082408
Printer.printMetadata("entity", N->getRawEntity());
24092409
Printer.printMetadata("file", N->getRawFile());
24102410
Printer.printInt("line", N->getLine());
2411+
Printer.printMetadata("elements", N->getRawElements());
24112412
Out << ")";
24122413
}
24132414

llvm/lib/IR/DIBuilder.cpp

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

180181
DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
181182
DINamespace *NS, DIFile *File,
182-
unsigned Line) {
183+
unsigned Line,
184+
DINodeArray Elements) {
183185
return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
184-
Context, NS, File, Line, StringRef(),
186+
Context, NS, File, Line, StringRef(), Elements,
185187
AllImportedModules);
186188
}
187189

188190
DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context,
189191
DIImportedEntity *NS,
190-
DIFile *File, unsigned Line) {
192+
DIFile *File, unsigned Line,
193+
DINodeArray Elements) {
191194
return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
192-
Context, NS, File, Line, StringRef(),
195+
Context, NS, File, Line, StringRef(), Elements,
193196
AllImportedModules);
194197
}
195198

196199
DIImportedEntity *DIBuilder::createImportedModule(DIScope *Context, DIModule *M,
197-
DIFile *File, unsigned Line) {
200+
DIFile *File, unsigned Line,
201+
DINodeArray Elements) {
198202
return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
199-
Context, M, File, Line, StringRef(),
203+
Context, M, File, Line, StringRef(), Elements,
200204
AllImportedModules);
201205
}
202206

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

0 commit comments

Comments
 (0)