Skip to content

[IRGen] NFC: Simplify IRGenModule::defineTypeMetadata #33626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 36 additions & 51 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3943,32 +3943,14 @@ llvm::GlobalValue *IRGenModule::defineTypeMetadata(CanType concreteType,
return cast<llvm::GlobalValue>(addr);
}

/// For concrete metadata, we want to use the initializer on the
/// "full metadata", and define the "direct" address point as an alias.
TypeMetadataAddress addrKind;
unsigned adjustmentIndex;

auto nominal = concreteType->getAnyNominal();

// Native Swift class metadata has a destructor before the address point.
// Foreign class metadata candidates do not, and neither does value type
// metadata.
if (nominal && isa<ClassDecl>(nominal) &&
!requiresForeignTypeMetadata(nominal)) {
addrKind = TypeMetadataAddress::FullMetadata;
adjustmentIndex = MetadataAdjustmentIndex::Class;
} else {
addrKind = TypeMetadataAddress::FullMetadata;
adjustmentIndex = MetadataAdjustmentIndex::ValueType;
}

auto entity =
(isPrespecialized &&
!irgen::isCanonicalInitializableTypeMetadataStaticallyAddressable(
*this, concreteType))
? LinkEntity::forNoncanonicalSpecializedGenericTypeMetadata(
concreteType)
: LinkEntity::forTypeMetadata(concreteType, addrKind);
: LinkEntity::forTypeMetadata(concreteType,
TypeMetadataAddress::FullMetadata);

auto DbgTy = DebugTypeInfo::getMetadata(MetatypeType::get(concreteType),
entity.getDefaultDeclarationType(*this)->getPointerTo(),
Expand All @@ -3986,30 +3968,35 @@ llvm::GlobalValue *IRGenModule::defineTypeMetadata(CanType concreteType,
if (link.isUsed())
addUsedGlobal(var);

// Keep type metadata around for all types.
if (nominal)
/// For concrete metadata, we want to use the initializer on the
/// "full metadata", and define the "direct" address point as an alias.
unsigned adjustmentIndex = MetadataAdjustmentIndex::ValueType;

if (auto nominal = concreteType->getAnyNominal()) {
// Keep type metadata around for all types.
addRuntimeResolvableType(nominal);

// Don't define the alias for foreign type metadata or prespecialized generic
// metadata, since neither is ABI.
if ((nominal && requiresForeignTypeMetadata(nominal)) || isPrespecialized)
return var;
// Don't define the alias for foreign type metadata or prespecialized
// generic metadata, since neither is ABI.
if (requiresForeignTypeMetadata(nominal) || isPrespecialized)
return var;

// For concrete metadata, declare the alias to its address point.
auto directEntity = LinkEntity::forTypeMetadata(concreteType,
TypeMetadataAddress::AddressPoint);
// Native Swift class metadata has a destructor before the address point.
if (isa<ClassDecl>(nominal)) {
adjustmentIndex = MetadataAdjustmentIndex::Class;
}
}

llvm::Constant *addr = var;
// Do an adjustment if necessary.
if (adjustmentIndex) {
llvm::Constant *indices[] = {
llvm::Constant *indices[] = {
llvm::ConstantInt::get(Int32Ty, 0),
llvm::ConstantInt::get(Int32Ty, adjustmentIndex)
};
addr = llvm::ConstantExpr::getInBoundsGetElementPtr(/*Ty=*/nullptr,
addr, indices);
}
llvm::ConstantInt::get(Int32Ty, adjustmentIndex)};
auto addr = llvm::ConstantExpr::getInBoundsGetElementPtr(/*Ty=*/nullptr, var,
indices);
addr = llvm::ConstantExpr::getBitCast(addr, TypeMetadataPtrTy);

// For concrete metadata, declare the alias to its address point.
auto directEntity = LinkEntity::forTypeMetadata(
concreteType, TypeMetadataAddress::AddressPoint);
return defineAlias(directEntity, addr);
}

Expand All @@ -4030,25 +4017,27 @@ IRGenModule::getAddrOfTypeMetadata(CanType concreteType,

auto nominal = concreteType->getAnyNominal();

llvm::Type *defaultVarTy;
unsigned adjustmentIndex;

bool foreign = nominal && requiresForeignTypeMetadata(nominal);

// Foreign classes and prespecialized generic types do not use an alias into
// the full metadata and therefore require a GEP.
bool fullMetadata =
foreign || (concreteType->getAnyGeneric() &&
concreteType->getAnyGeneric()->isGenericContext());

// Foreign classes reference the full metadata with a GEP.
llvm::Type *defaultVarTy;
unsigned adjustmentIndex;

if (fullMetadata) {
defaultVarTy = FullTypeMetadataStructTy;
if (concreteType->getClassOrBoundGenericClass() && !foreign) {
adjustmentIndex = MetadataAdjustmentIndex::Class;
} else {
adjustmentIndex = MetadataAdjustmentIndex::ValueType;
}
// The symbol for other nominal type metadata is generated at the address
// point.
} else if (nominal) {
// The symbol for native non-generic nominal type metadata is generated at
// the aliased address point (see defineTypeMetadata() above).
assert(!nominal->hasClangNode());

defaultVarTy = TypeMetadataStructTy;
Expand Down Expand Up @@ -4082,13 +4071,9 @@ IRGenModule::getAddrOfTypeMetadata(CanType concreteType,

switch (canonicality) {
case TypeMetadataCanonicality::Canonical:
if (fullMetadata) {
entity = LinkEntity::forTypeMetadata(concreteType,
TypeMetadataAddress::FullMetadata);
} else {
entity = LinkEntity::forTypeMetadata(concreteType,
TypeMetadataAddress::AddressPoint);
}
entity = LinkEntity::forTypeMetadata(
concreteType, fullMetadata ? TypeMetadataAddress::FullMetadata
: TypeMetadataAddress::AddressPoint);
break;
case TypeMetadataCanonicality::Noncanonical:
entity =
Expand Down