Skip to content

Commit 98c7825

Browse files
committed
[DebugInfo] Emit alternative module name in debug info
To support Swift types defined with @_originallyDefinedIn, we need to encode the ABI module name in debug info somehow. This patch adds a new alternative_module_name to LLVM IR and DWARF to encode that information.
1 parent b1f6197 commit 98c7825

File tree

14 files changed

+234
-117
lines changed

14 files changed

+234
-117
lines changed

llvm/include/llvm/BinaryFormat/Dwarf.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ HANDLE_DW_AT(0x3e08, LLVM_ptrauth_isa_pointer, 0, LLVM)
618618
HANDLE_DW_AT(0x3e09, LLVM_ptrauth_authenticates_null_values, 0, LLVM)
619619
HANDLE_DW_AT(0x3e0a, LLVM_ptrauth_authentication_mode, 0, LLVM)
620620
HANDLE_DW_AT(0x3e0b, LLVM_num_extra_inhabitants, 0, LLVM)
621+
HANDLE_DW_AT(0x3ff1, LLVM_alternative_module_name, 0, APPLE)
621622

622623
// Apple extensions.
623624
HANDLE_DW_AT(0x3fe1, APPLE_optimized, 0, APPLE)

llvm/include/llvm/IR/DIBuilder.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,12 +494,16 @@ namespace llvm {
494494
/// \param NumExtraInhabitants The number of extra inhabitants of the type.
495495
/// An extra inhabitant is a bit pattern that does not represent a valid
496496
/// value for instances of a given type.
497+
/// \param AlternativeModuleName An alternative module name associated with
498+
/// this type. This is used by Swift to encode the module's ABI name for
499+
/// types defined with @_originallyDefinedIn.
497500
DICompositeType *createStructType(
498501
DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
499502
uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
500503
DIType *DerivedFrom, DINodeArray Elements, unsigned RunTimeLang = 0,
501504
DIType *VTableHolder = nullptr, StringRef UniqueIdentifier = "",
502-
DIType *SpecificationOf = nullptr, uint32_t NumExtraInhabitants = 0);
505+
DIType *SpecificationOf = nullptr, uint32_t NumExtraInhabitants = 0,
506+
StringRef AlternativeModuleName = "");
503507

504508
/// Create debugging information entry for an union.
505509
/// \param Scope Scope in which this union is defined.

llvm/include/llvm/IR/DebugInfoMetadata.h

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,20 +1206,22 @@ class DICompositeType : public DIType {
12061206
getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
12071207
unsigned Line, DIScope *Scope, DIType *BaseType, uint64_t SizeInBits,
12081208
uint32_t AlignInBits, uint64_t OffsetInBits, DIType *SpecificationOf,
1209-
uint32_t NumExtraInhabitants, DIFlags Flags, DINodeArray Elements,
1210-
unsigned RuntimeLang, DIType *VTableHolder,
1211-
DITemplateParameterArray TemplateParams, StringRef Identifier,
1212-
DIDerivedType *Discriminator, Metadata *DataLocation,
1213-
Metadata *Associated, Metadata *Allocated, Metadata *Rank,
1214-
DINodeArray Annotations, StorageType Storage,
1209+
uint32_t NumExtraInhabitants, StringRef AlternativeModuleName,
1210+
DIFlags Flags, DINodeArray Elements, unsigned RuntimeLang,
1211+
DIType *VTableHolder, DITemplateParameterArray TemplateParams,
1212+
StringRef Identifier, DIDerivedType *Discriminator,
1213+
Metadata *DataLocation, Metadata *Associated, Metadata *Allocated,
1214+
Metadata *Rank, DINodeArray Annotations, StorageType Storage,
12151215
bool ShouldCreate = true) {
12161216
return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
12171217
Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
12181218
Flags, Elements.get(), RuntimeLang, VTableHolder,
12191219
TemplateParams.get(),
12201220
getCanonicalMDString(Context, Identifier), Discriminator,
12211221
DataLocation, Associated, Allocated, Rank, Annotations.get(),
1222-
SpecificationOf, NumExtraInhabitants, Storage, ShouldCreate);
1222+
SpecificationOf, NumExtraInhabitants,
1223+
getCanonicalMDString(Context, AlternativeModuleName),
1224+
Storage, ShouldCreate);
12231225
}
12241226
static DICompositeType *
12251227
getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
@@ -1230,8 +1232,8 @@ class DICompositeType : public DIType {
12301232
MDString *Identifier, Metadata *Discriminator, Metadata *DataLocation,
12311233
Metadata *Associated, Metadata *Allocated, Metadata *Rank,
12321234
Metadata *Annotations, Metadata *SpecificationOf,
1233-
uint32_t NumExtraInhabitants, StorageType Storage,
1234-
bool ShouldCreate = true);
1235+
uint32_t NumExtraInhabitants, MDString *AlternativeModuleName,
1236+
StorageType Storage, bool ShouldCreate = true);
12351237

12361238
TempDICompositeType cloneImpl() const {
12371239
return getTemporary(
@@ -1241,7 +1243,7 @@ class DICompositeType : public DIType {
12411243
getTemplateParams(), getIdentifier(), getDiscriminator(),
12421244
getRawDataLocation(), getRawAssociated(), getRawAllocated(),
12431245
getRawRank(), getAnnotations(), getSpecificationOf(),
1244-
getNumExtraInhabitants());
1246+
getNumExtraInhabitants(), getAlternativeModuleName());
12451247
}
12461248

12471249
public:
@@ -1256,11 +1258,12 @@ class DICompositeType : public DIType {
12561258
Metadata *DataLocation = nullptr, Metadata *Associated = nullptr,
12571259
Metadata *Allocated = nullptr, Metadata *Rank = nullptr,
12581260
DINodeArray Annotations = nullptr, DIType *SpecificationOf = nullptr,
1259-
uint32_t NumExtraInhabitants = 0),
1261+
uint32_t NumExtraInhabitants = 0, StringRef AlternativeModuleName = ""),
12601262
(Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1261-
OffsetInBits, SpecificationOf, NumExtraInhabitants, Flags, Elements,
1262-
RuntimeLang, VTableHolder, TemplateParams, Identifier, Discriminator,
1263-
DataLocation, Associated, Allocated, Rank, Annotations))
1263+
OffsetInBits, SpecificationOf, NumExtraInhabitants,
1264+
AlternativeModuleName, Flags, Elements, RuntimeLang, VTableHolder,
1265+
TemplateParams, Identifier, Discriminator, DataLocation, Associated,
1266+
Allocated, Rank, Annotations))
12641267
DEFINE_MDNODE_GET(
12651268
DICompositeType,
12661269
(unsigned Tag, MDString *Name, Metadata *File, unsigned Line,
@@ -1271,11 +1274,13 @@ class DICompositeType : public DIType {
12711274
Metadata *Discriminator = nullptr, Metadata *DataLocation = nullptr,
12721275
Metadata *Associated = nullptr, Metadata *Allocated = nullptr,
12731276
Metadata *Rank = nullptr, Metadata *Annotations = nullptr,
1274-
Metadata *SpecificationOf = nullptr, uint32_t NumExtraInhabitants = 0),
1277+
Metadata *SpecificationOf = nullptr, uint32_t NumExtraInhabitants = 0,
1278+
MDString *AlternativeModuleName = nullptr),
12751279
(Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
12761280
OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder, TemplateParams,
12771281
Identifier, Discriminator, DataLocation, Associated, Allocated, Rank,
1278-
Annotations, SpecificationOf, NumExtraInhabitants))
1282+
Annotations, SpecificationOf, NumExtraInhabitants,
1283+
AlternativeModuleName))
12791284

12801285
TempDICompositeType clone() const { return cloneImpl(); }
12811286

@@ -1286,16 +1291,15 @@ class DICompositeType : public DIType {
12861291
/// a new node.
12871292
///
12881293
/// Else, returns \c nullptr.
1289-
static DICompositeType *
1290-
getODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag,
1291-
MDString *Name, Metadata *File, unsigned Line, Metadata *Scope,
1292-
Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits,
1293-
uint64_t OffsetInBits, Metadata *SpecificationOf,
1294-
uint32_t NumExtraInhabitants, DIFlags Flags,
1295-
Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
1296-
Metadata *TemplateParams, Metadata *Discriminator,
1297-
Metadata *DataLocation, Metadata *Associated, Metadata *Allocated,
1298-
Metadata *Rank, Metadata *Annotations);
1294+
static DICompositeType *getODRType(
1295+
LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name,
1296+
Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType,
1297+
uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
1298+
Metadata *SpecificationOf, uint32_t NumExtraInhabitants,
1299+
MDString *AlternativeModuleName, DIFlags Flags, Metadata *Elements,
1300+
unsigned RuntimeLang, Metadata *VTableHolder, Metadata *TemplateParams,
1301+
Metadata *Discriminator, Metadata *DataLocation, Metadata *Associated,
1302+
Metadata *Allocated, Metadata *Rank, Metadata *Annotations);
12991303
static DICompositeType *getODRTypeIfExists(LLVMContext &Context,
13001304
MDString &Identifier);
13011305

@@ -1308,16 +1312,15 @@ class DICompositeType : public DIType {
13081312
///
13091313
/// If not \a LLVMContext::isODRUniquingDebugTypes(), this function returns
13101314
/// nullptr.
1311-
static DICompositeType *
1312-
buildODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag,
1313-
MDString *Name, Metadata *File, unsigned Line, Metadata *Scope,
1314-
Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits,
1315-
uint64_t OffsetInBits, Metadata *SpecificationOf,
1316-
uint32_t NumExtraInhabitants, DIFlags Flags, Metadata *Elements,
1317-
unsigned RuntimeLang, Metadata *VTableHolder,
1318-
Metadata *TemplateParams, Metadata *Discriminator,
1319-
Metadata *DataLocation, Metadata *Associated,
1320-
Metadata *Allocated, Metadata *Rank, Metadata *Annotations);
1315+
static DICompositeType *buildODRType(
1316+
LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name,
1317+
Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType,
1318+
uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
1319+
Metadata *SpecificationOf, uint32_t NumExtraInhabitants,
1320+
MDString *AlternativeModuleName, DIFlags Flags, Metadata *Elements,
1321+
unsigned RuntimeLang, Metadata *VTableHolder, Metadata *TemplateParams,
1322+
Metadata *Discriminator, Metadata *DataLocation, Metadata *Associated,
1323+
Metadata *Allocated, Metadata *Rank, Metadata *Annotations);
13211324

13221325
DIType *getBaseType() const { return cast_or_null<DIType>(getRawBaseType()); }
13231326
DINodeArray getElements() const {
@@ -1382,6 +1385,10 @@ class DICompositeType : public DIType {
13821385
return cast_or_null<DIType>(getRawSpecificationOf());
13831386
}
13841387

1388+
MDString *getRawAlternativeModuleName() const {
1389+
return getOperandAs<MDString>(15);
1390+
}
1391+
StringRef getAlternativeModuleName() const { return getStringOperand(15); }
13851392
/// Replace operands.
13861393
///
13871394
/// If this \a isUniqued() and not \a isResolved(), on a uniquing collision

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5433,7 +5433,8 @@ bool LLParser::parseDICompositeType(MDNode *&Result, bool IsDistinct) {
54335433
OPTIONAL(rank, MDSignedOrMDField, ); \
54345434
OPTIONAL(annotations, MDField, ); \
54355435
OPTIONAL(num_extra_inhabitants, MDUnsignedField, (0, UINT32_MAX)); \
5436-
OPTIONAL(specification_of, MDField, );
5436+
OPTIONAL(specification_of, MDField, ); \
5437+
OPTIONAL(alternative_module_name, MDStringField, );
54375438
PARSE_MD_FIELDS();
54385439
#undef VISIT_MD_FIELDS
54395440

@@ -5449,8 +5450,9 @@ bool LLParser::parseDICompositeType(MDNode *&Result, bool IsDistinct) {
54495450
if (auto *CT = DICompositeType::buildODRType(
54505451
Context, *identifier.Val, tag.Val, name.Val, file.Val, line.Val,
54515452
scope.Val, baseType.Val, size.Val, align.Val, offset.Val,
5452-
specification_of.Val, num_extra_inhabitants.Val, flags.Val,
5453-
elements.Val, runtimeLang.Val, vtableHolder.Val, templateParams.Val,
5453+
specification_of.Val, num_extra_inhabitants.Val,
5454+
alternative_module_name.Val, flags.Val, elements.Val,
5455+
runtimeLang.Val, vtableHolder.Val, templateParams.Val,
54545456
discriminator.Val, dataLocation.Val, associated.Val, allocated.Val,
54555457
Rank, annotations.Val)) {
54565458
Result = CT;
@@ -5465,7 +5467,8 @@ bool LLParser::parseDICompositeType(MDNode *&Result, bool IsDistinct) {
54655467
size.Val, align.Val, offset.Val, flags.Val, elements.Val,
54665468
runtimeLang.Val, vtableHolder.Val, templateParams.Val, identifier.Val,
54675469
discriminator.Val, dataLocation.Val, associated.Val, allocated.Val, Rank,
5468-
annotations.Val, specification_of.Val, num_extra_inhabitants.Val));
5470+
annotations.Val, specification_of.Val, num_extra_inhabitants.Val,
5471+
alternative_module_name.Val));
54695472
return false;
54705473
}
54715474

llvm/lib/Bitcode/Reader/MetadataLoader.cpp

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,7 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
16341634
Metadata *Rank = nullptr;
16351635
Metadata *Annotations = nullptr;
16361636
Metadata *SpecificationOf = nullptr;
1637+
MDString *AlternativeModuleName = nullptr;
16371638
auto *Identifier = getMDString(Record[15]);
16381639
// If this module is being parsed so that it can be ThinLTO imported
16391640
// into another module, composite types only need to be imported as
@@ -1685,15 +1686,53 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
16851686
if (Record.size() > 23) {
16861687
SpecificationOf = getMDOrNull(Record[23]);
16871688
}
1689+
if (Record.size() > 24) {
1690+
if (uint64_t ID = Record[24]) {
1691+
// Field 24 used to be occupied by the spare bits mask, which was
1692+
// encoded as an uint64_t. To not crash when loading a bitcode file
1693+
// with the old spare bits mask format, we take advantage of the fact
1694+
// that strings cannot be forward declared, and only load the field if
1695+
// it's already loaded. This lambda is a copy of getMDString, but
1696+
// which fails if the field is not forward declared.
1697+
// TODO: When we don't need to support bitcode with spare bits mask,
1698+
// replace this with a call to getMDString.
1699+
auto GetAlternativeName = [&](uint64_t ID) -> Metadata * {
1700+
if (ID < MDStringRef.size())
1701+
return lazyLoadOneMDString(ID);
1702+
if (!IsDistinct) {
1703+
if (auto *MD = MetadataList.lookup(ID))
1704+
return MD;
1705+
// If lazy-loading is enabled, we try recursively to load the
1706+
// operand instead of creating a temporary.
1707+
if (ID <
1708+
(MDStringRef.size() + GlobalMetadataBitPosIndex.size())) {
1709+
// Create a temporary for the node that is referencing the
1710+
// operand we will lazy-load. It is needed before recursing in
1711+
// case there are uniquing cycles.
1712+
MetadataList.getMetadataFwdRef(NextMetadataNo);
1713+
lazyLoadOneMetadata(ID, Placeholders);
1714+
return MetadataList.lookup(ID);
1715+
}
1716+
}
1717+
if (auto *MD = MetadataList.getMetadataIfResolved(ID))
1718+
return MD;
1719+
return nullptr;
1720+
};
1721+
ID = ID - 1;
1722+
Metadata *MaybeAlternativeModuleName = GetAlternativeName(ID);
1723+
AlternativeModuleName =
1724+
dyn_cast_or_null<MDString>(MaybeAlternativeModuleName);
1725+
}
1726+
}
16881727
}
16891728
DICompositeType *CT = nullptr;
16901729
if (Identifier)
16911730
CT = DICompositeType::buildODRType(
16921731
Context, *Identifier, Tag, Name, File, Line, Scope, BaseType,
16931732
SizeInBits, AlignInBits, OffsetInBits, SpecificationOf,
1694-
NumExtraInhabitants, Flags, Elements, RuntimeLang, VTableHolder,
1695-
TemplateParams, Discriminator, DataLocation, Associated, Allocated,
1696-
Rank, Annotations);
1733+
NumExtraInhabitants, AlternativeModuleName, Flags, Elements,
1734+
RuntimeLang, VTableHolder, TemplateParams, Discriminator,
1735+
DataLocation, Associated, Allocated, Rank, Annotations);
16971736

16981737
// Create a node if we didn't get a lazy ODR type.
16991738
if (!CT)
@@ -1703,7 +1742,7 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
17031742
Elements, RuntimeLang, VTableHolder, TemplateParams,
17041743
Identifier, Discriminator, DataLocation, Associated,
17051744
Allocated, Rank, Annotations, SpecificationOf,
1706-
NumExtraInhabitants));
1745+
NumExtraInhabitants, AlternativeModuleName));
17071746
if (!IsNotUsedInTypeRef && Identifier)
17081747
MetadataList.addTypeRef(*Identifier, *cast<DICompositeType>(CT));
17091748

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,7 @@ void ModuleBitcodeWriter::writeDICompositeType(
19291929
Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
19301930
Record.push_back(N->getNumExtraInhabitants());
19311931
Record.push_back(VE.getMetadataOrNullID(N->getRawSpecificationOf()));
1932+
Record.push_back(VE.getMetadataOrNullID(N->getRawAlternativeModuleName()));
19321933

19331934
Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
19341935
Record.clear();

llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,11 @@ void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
10491049
addDIEEntry(Buffer, dwarf::DW_AT_specification,
10501050
*getOrCreateContextDIE(SpecifiedFrom));
10511051

1052+
auto AlternativeModuleName = CTy->getAlternativeModuleName();
1053+
if (!AlternativeModuleName.empty())
1054+
addString(Buffer, dwarf::DW_AT_LLVM_alternative_module_name,
1055+
AlternativeModuleName);
1056+
10521057
break;
10531058
}
10541059
default:

llvm/lib/IR/AsmWriter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,6 +2221,7 @@ static void writeDICompositeType(raw_ostream &Out, const DICompositeType *N,
22212221
Printer.printMetadata("annotations", N->getRawAnnotations());
22222222
if (auto *SpecificationOf = N->getRawSpecificationOf())
22232223
Printer.printMetadata("specification_of", SpecificationOf);
2224+
Printer.printString("alternative_module_name", N->getAlternativeModuleName());
22242225
Out << ")";
22252226
}
22262227

llvm/lib/IR/DIBuilder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,13 +523,13 @@ DICompositeType *DIBuilder::createStructType(
523523
uint64_t SizeInBits, uint32_t AlignInBits, DINode::DIFlags Flags,
524524
DIType *DerivedFrom, DINodeArray Elements, unsigned RunTimeLang,
525525
DIType *VTableHolder, StringRef UniqueIdentifier, DIType *SpecificationOf,
526-
uint32_t NumExtraInhabitants) {
526+
uint32_t NumExtraInhabitants, StringRef AlternativeModuleName) {
527527
auto *R = DICompositeType::get(
528528
VMContext, dwarf::DW_TAG_structure_type, Name, File, LineNumber,
529529
getNonCompileUnitScope(Context), DerivedFrom, SizeInBits, AlignInBits, 0,
530530
Flags, Elements, RunTimeLang, VTableHolder, nullptr, UniqueIdentifier,
531531
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, SpecificationOf,
532-
NumExtraInhabitants);
532+
NumExtraInhabitants, AlternativeModuleName);
533533
trackIfUnresolved(R);
534534
return R;
535535
}

0 commit comments

Comments
 (0)