Skip to content

Commit 310bd6b

Browse files
committed
[Runtime] Mangle the reference kind in the lower two bits of type references.
The separate section of type references uses the same type reference format as in protocol conformance records. As with protocol conformance records, mangle the type reference kind into the lower two bits. Then, eliminate the separate "flags" field from the type metadata record. Finally, rename the section because the Swift 5 stable format for this section is different from prior formats, and the two runtimes need to be able to coexist.
1 parent bfd2230 commit 310bd6b

File tree

9 files changed

+33
-30
lines changed

9 files changed

+33
-30
lines changed

include/swift/Runtime/Metadata.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2460,23 +2460,22 @@ struct TargetTypeMetadataRecord {
24602460
// Some description of the type that is resolvable at runtime.
24612461
union {
24622462
/// A direct reference to the metadata.
2463-
RelativeDirectPointer<const TargetMetadata<Runtime>> DirectType;
2463+
RelativeDirectPointerIntPair<const TargetMetadata<Runtime>,
2464+
TypeMetadataRecordKind> DirectType;
24642465

24652466
/// The nominal type descriptor for a resilient or generic type.
2466-
RelativeDirectPointer<TargetNominalTypeDescriptor<Runtime>>
2467+
RelativeDirectPointerIntPair<TargetNominalTypeDescriptor<Runtime>,
2468+
TypeMetadataRecordKind>
24672469
TypeDescriptor;
24682470
};
24692471

2470-
/// Flags describing the type metadata record.
2471-
TypeMetadataRecordFlags Flags;
2472-
24732472
public:
24742473
TypeMetadataRecordKind getTypeKind() const {
2475-
return Flags.getTypeKind();
2474+
return DirectType.getInt();
24762475
}
24772476

24782477
const TargetMetadata<Runtime> *getDirectType() const {
2479-
switch (Flags.getTypeKind()) {
2478+
switch (getTypeKind()) {
24802479
case TypeMetadataRecordKind::NonuniqueDirectType:
24812480
break;
24822481

@@ -2486,12 +2485,12 @@ struct TargetTypeMetadataRecord {
24862485
assert(false && "not direct type metadata");
24872486
}
24882487

2489-
return this->DirectType;
2488+
return this->DirectType.getPointer();
24902489
}
24912490

24922491
const TargetNominalTypeDescriptor<Runtime> *
24932492
getNominalTypeDescriptor() const {
2494-
switch (Flags.getTypeKind()) {
2493+
switch (getTypeKind()) {
24952494
case TypeMetadataRecordKind::DirectNominalTypeDescriptor:
24962495
break;
24972496

@@ -2501,7 +2500,7 @@ struct TargetTypeMetadataRecord {
25012500
assert(false && "not generic metadata pattern");
25022501
}
25032502

2504-
return this->TypeDescriptor;
2503+
return this->TypeDescriptor.getPointer();
25052504
}
25062505

25072506
/// Get the canonical metadata for the type referenced by this record, or

lib/IRGen/GenDecl.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,13 +2401,13 @@ llvm::Constant *IRGenModule::emitTypeMetadataRecords() {
24012401
std::string sectionName;
24022402
switch (TargetInfo.OutputObjectFormat) {
24032403
case llvm::Triple::MachO:
2404-
sectionName = "__TEXT, __swift2_types, regular, no_dead_strip";
2404+
sectionName = "__TEXT, __swift5_types, regular, no_dead_strip";
24052405
break;
24062406
case llvm::Triple::ELF:
2407-
sectionName = "swift2_type_metadata";
2407+
sectionName = "swift5_type_metadata";
24082408
break;
24092409
case llvm::Triple::COFF:
2410-
sectionName = ".sw2tymd$B";
2410+
sectionName = ".sw5tymd$B";
24112411
break;
24122412
default:
24132413
llvm_unreachable("Don't know how to emit type metadata table for "
@@ -2437,14 +2437,19 @@ llvm::Constant *IRGenModule::emitTypeMetadataRecords() {
24372437
auto typeEntity = getTypeEntityInfo(*this, type);
24382438
auto typeRef = getAddrOfLLVMVariableOrGOTEquivalent(
24392439
typeEntity.entity, getPointerAlignment(), typeEntity.defaultTy);
2440+
typeEntity.adjustForKnownRef(typeRef);
24402441

2442+
// Form the relative address, with the type refernce kind in the low bits.
24412443
unsigned arrayIdx = elts.size();
2442-
llvm::Constant *recordFields[] = {
2443-
emitRelativeReference(typeRef, var, { arrayIdx, 0 }),
2444-
llvm::ConstantInt::get(Int32Ty,
2445-
static_cast<unsigned>(typeEntity.typeKind)),
2446-
};
2444+
llvm::Constant *relativeAddr =
2445+
emitDirectRelativeReference(typeRef.getValue(), var, { arrayIdx, 0 });
2446+
unsigned lowBits = static_cast<unsigned>(typeEntity.typeKind);
2447+
if (lowBits != 0) {
2448+
relativeAddr = llvm::ConstantExpr::getAdd(relativeAddr,
2449+
llvm::ConstantInt::get(RelativeAddressTy, lowBits));
2450+
}
24472451

2452+
llvm::Constant *recordFields[] = { relativeAddr };
24482453
auto record = llvm::ConstantStruct::get(TypeMetadataRecordTy,
24492454
recordFields);
24502455
elts.push_back(record);
@@ -2454,7 +2459,7 @@ llvm::Constant *IRGenModule::emitTypeMetadataRecords() {
24542459

24552460
var->setInitializer(initializer);
24562461
var->setSection(sectionName);
2457-
var->setAlignment(getPointerAlignment().getValue());
2462+
var->setAlignment(4);
24582463
addUsedGlobal(var);
24592464
return var;
24602465
}

lib/IRGen/IRGenModule.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,7 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
304304

305305
TypeMetadataRecordTy
306306
= createStructType(*this, "swift.type_metadata_record", {
307-
RelativeAddressTy,
308-
Int32Ty
307+
RelativeAddressTy
309308
});
310309
TypeMetadataRecordPtrTy
311310
= TypeMetadataRecordTy->getPointerTo(DefaultAS);

stdlib/public/runtime/ImageInspectionCOFF.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void swift::initializeTypeMetadataRecordLookup() {
5656
const swift::MetadataSections *sections = registered;
5757
while (true) {
5858
const swift::MetadataSections::Range &type_metadata =
59-
sections->swift2_type_metadata;
59+
sections->swift5_type_metadata;
6060
if (type_metadata.length)
6161
addImageTypeMetadataRecordBlockCallback(reinterpret_cast<void *>(type_metadata.start),
6262
type_metadata.length);
@@ -81,7 +81,7 @@ void swift_addNewDSOImage(const void *addr) {
8181
addImageProtocolConformanceBlockCallback(conformances,
8282
protocol_conformances.length);
8383

84-
const auto &type_metadata = sections->swift2_type_metadata;
84+
const auto &type_metadata = sections->swift5_type_metadata;
8585
const void *metadata = reinterpret_cast<void *>(type_metadata.start);
8686
if (type_metadata.length)
8787
addImageTypeMetadataRecordBlockCallback(metadata, type_metadata.length);

stdlib/public/runtime/ImageInspectionCOFF.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct MetadataSections {
4646
};
4747

4848
Range swift5_protocol_conformances;
49-
Range swift2_type_metadata;
49+
Range swift5_type_metadata;
5050
Range swift3_typeref;
5151
Range swift3_reflstr;
5252
Range swift3_fieldmd;

stdlib/public/runtime/ImageInspectionELF.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void swift::initializeTypeMetadataRecordLookup() {
6161
const swift::MetadataSections *sections = registered;
6262
while (true) {
6363
const swift::MetadataSections::Range &type_metadata =
64-
sections->swift2_type_metadata;
64+
sections->swift5_type_metadata;
6565
if (type_metadata.length)
6666
addImageTypeMetadataRecordBlockCallback(reinterpret_cast<void *>(type_metadata.start),
6767
type_metadata.length);
@@ -90,7 +90,7 @@ void swift_addNewDSOImage(const void *addr) {
9090
addImageProtocolConformanceBlockCallback(conformances,
9191
protocol_conformances.length);
9292

93-
const auto &type_metadata = sections->swift2_type_metadata;
93+
const auto &type_metadata = sections->swift5_type_metadata;
9494
const void *metadata = reinterpret_cast<void *>(type_metadata.start);
9595
if (type_metadata.length)
9696
addImageTypeMetadataRecordBlockCallback(metadata, type_metadata.length);

stdlib/public/runtime/ImageInspectionELF.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct MetadataSections {
4646
};
4747

4848
Range swift5_protocol_conformances;
49-
Range swift2_type_metadata;
49+
Range swift5_type_metadata;
5050
Range swift3_typeref;
5151
Range swift3_reflstr;
5252
Range swift3_fieldmd;

stdlib/public/runtime/ImageInspectionMachO.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace {
3434
constexpr const char ProtocolConformancesSection[] = "__swift5_proto";
3535
/// The Mach-O section name for the section containing type references.
3636
/// This lives within SEG_TEXT.
37-
constexpr const char TypeMetadataRecordSection[] = "__swift2_types";
37+
constexpr const char TypeMetadataRecordSection[] = "__swift5_types";
3838

3939
template<const char *SECTION_NAME,
4040
void CONSUME_BLOCK(const void *start, uintptr_t size)>

stdlib/public/runtime/SwiftRT-ELF.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
extern "C" {
2727
DECLARE_SWIFT_SECTION(swift5_protocol_conformances)
28-
DECLARE_SWIFT_SECTION(swift2_type_metadata)
28+
DECLARE_SWIFT_SECTION(swift5_type_metadata)
2929

3030
DECLARE_SWIFT_SECTION(swift3_typeref)
3131
DECLARE_SWIFT_SECTION(swift3_reflstr)
@@ -53,7 +53,7 @@ static void swift_image_constructor() {
5353
nullptr,
5454

5555
SWIFT_SECTION_RANGE(swift5_protocol_conformances),
56-
SWIFT_SECTION_RANGE(swift2_type_metadata),
56+
SWIFT_SECTION_RANGE(swift5_type_metadata),
5757

5858
SWIFT_SECTION_RANGE(swift3_typeref),
5959
SWIFT_SECTION_RANGE(swift3_reflstr),

0 commit comments

Comments
 (0)