Skip to content

IRGen: Separate the concept of "metadata should be cached" from "statically referenced" #30036

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
Feb 25, 2020
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion lib/IRGen/LocalTypeData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ addAbstractForFulfillments(IRGenFunction &IGF, FulfillmentMap &&fulfillments,
// the type metadata for Int by chasing through N layers of metadata
// just because that path happens to be in the cache.
if (!type->hasArchetype() &&
isTypeMetadataAccessTrivial(IGF.IGM, type)) {
!shouldCacheTypeMetadataAccess(IGF.IGM, type)) {
continue;
}

Expand Down
36 changes: 22 additions & 14 deletions lib/IRGen/MetadataRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ irgen::tryEmitConstantTypeMetadataRef(IRGenModule &IGM, CanType type,
SymbolReferenceKind refKind) {
if (IGM.isStandardLibrary())
return ConstantReference();
if (!isTypeMetadataAccessTrivial(IGM, type))
if (isCompleteTypeMetadataStaticallyAddressable(IGM, type))
return ConstantReference();
return IGM.getAddrOfTypeMetadata(type, refKind);
}
Expand Down Expand Up @@ -722,7 +722,7 @@ bool irgen::isNominalGenericContextTypeMetadataAccessTrivial(
};
auto isExistential = [&]() { return argument->isExistentialType(); };
auto metadataAccessIsTrivial = [&]() {
return irgen::isTypeMetadataAccessTrivial(IGM,
return irgen::isCompleteTypeMetadataStaticallyAddressable(IGM,
argument->getCanonicalType());
};
return !isGenericWithoutPrespecializedConformance() && !isExistential() &&
Expand All @@ -732,9 +732,9 @@ bool irgen::isNominalGenericContextTypeMetadataAccessTrivial(
&& IGM.getTypeInfoForUnlowered(type).isFixedSize(ResilienceExpansion::Maximal);
}

/// Is it basically trivial to access the given metadata? If so, we don't
/// need a cache variable in its accessor.
bool irgen::isTypeMetadataAccessTrivial(IRGenModule &IGM, CanType type) {
/// Is complete metadata for the given type available at a fixed address?
bool irgen::isCompleteTypeMetadataStaticallyAddressable(IRGenModule &IGM,
CanType type) {
assert(!type->hasArchetype());

// Value type metadata only requires dynamic initialization on first
Expand Down Expand Up @@ -775,10 +775,6 @@ bool irgen::isTypeMetadataAccessTrivial(IRGenModule &IGM, CanType type) {
if (isa<SILBoxType>(type))
return true;

// DynamicSelfType is actually local.
if (type->hasDynamicSelfType())
return true;

if (isa<BoundGenericStructType>(type) || isa<BoundGenericEnumType>(type)) {
auto nominalType = cast<BoundGenericType>(type);
auto *nominalDecl = nominalType->getDecl();
Expand All @@ -794,6 +790,19 @@ bool irgen::isTypeMetadataAccessTrivial(IRGenModule &IGM, CanType type) {
return false;
}

/// Should requests for the given type's metadata be cached?
bool irgen::shouldCacheTypeMetadataAccess(IRGenModule &IGM, CanType type) {
// DynamicSelfType is actually local.
if (type->hasDynamicSelfType())
return false;

// Statically addressable metadata does not need a cache.
if (isCompleteTypeMetadataStaticallyAddressable(IGM, type))
return false;

return true;
}

/// Return the standard access strategy for getting a non-dependent
/// type metadata object.
MetadataAccessStrategy irgen::getTypeMetadataAccessStrategy(CanType type) {
Expand Down Expand Up @@ -2016,7 +2025,7 @@ emitDirectTypeMetadataAccessFunctionBody(IRGenFunction &IGF,
}

// We should not be doing more serious work along this path.
assert(isTypeMetadataAccessTrivial(IGF.IGM, type));
assert(isCompleteTypeMetadataStaticallyAddressable(IGF.IGM, type));

// Okay, everything else is built from a Swift metadata object.
llvm::Constant *metadata = IGF.IGM.getAddrOfTypeMetadata(type);
Expand Down Expand Up @@ -2064,7 +2073,7 @@ irgen::createTypeMetadataAccessFunction(IRGenModule &IGM, CanType type,

// If our preferred access method is to go via an accessor, it means
// there is some non-trivial computation that needs to be cached.
if (isTypeMetadataAccessTrivial(IGM, type)) {
if (!shouldCacheTypeMetadataAccess(IGM, type)) {
cacheStrategy = CacheStrategy::None;
} else {
switch (cacheStrategy) {
Expand Down Expand Up @@ -2194,7 +2203,7 @@ static bool shouldAccessByMangledName(IRGenModule &IGM, CanType type) {
// others may require accessors to trigger instantiation.
//
// TODO: Also need to count the parent type's generic arguments.
if (isTypeMetadataAccessTrivial(IGM, nom)) {
if (!shouldCacheTypeMetadataAccess(IGM, nom)) {
NumAddresses += 1;
} else {
NumCalls += 1;
Expand Down Expand Up @@ -2545,8 +2554,7 @@ IRGenFunction::emitTypeMetadataRef(CanType type,
}

if (type->hasArchetype() ||
isTypeMetadataAccessTrivial(IGM, type)) {
// FIXME: propagate metadata request!
!shouldCacheTypeMetadataAccess(IGM, type)) {
return emitDirectTypeMetadataRef(*this, type, request);
}

Expand Down
7 changes: 4 additions & 3 deletions lib/IRGen/MetadataRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,10 @@ static inline bool isAccessorLazilyGenerated(MetadataAccessStrategy strategy) {
llvm_unreachable("bad kind");
}

/// Is it basically trivial to access the given metadata? If so, we don't
/// need a cache variable in its accessor.
bool isTypeMetadataAccessTrivial(IRGenModule &IGM, CanType type);
/// Is complete metadata for the given type available at a fixed address?
bool isCompleteTypeMetadataStaticallyAddressable(IRGenModule &IGM, CanType type);
/// Should requests for the given type's metadata be cached?
bool shouldCacheTypeMetadataAccess(IRGenModule &IGM, CanType type);

bool isNominalGenericContextTypeMetadataAccessTrivial(IRGenModule &IGM,
NominalTypeDecl &nominal,
Expand Down
18 changes: 1 addition & 17 deletions test/IRGen/dynamic_self_metadata_future.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,7 @@ class C {
// CHECK-SAME: {{.*}} @"$s28dynamic_self_metadata_future1GVyxGAA1PAAMc"
// CHECK-SAME: to %swift.protocol_conformance_descriptor*
// CHECK-SAME: ),
// CHECK-SAME: %swift.type* getelementptr inbounds (
// CHECK-SAME: %swift.full_type,
// CHECK-SAME: %swift.full_type* bitcast (
// CHECK-SAME: <{
// CHECK-SAME: i8**,
// CHECK-SAME: [[INT]],
// CHECK-SAME: %swift.type_descriptor*,
// CHECK-SAME: %swift.type*,
// CHECK-SAME: i32,
// CHECK-SAME: {{(\[4 x i8\])?}},
// CHECK-SAME: i64
// CHECK-SAME: }>* @"$s28dynamic_self_metadata_future1GVyAA1CCXDGMf"
// CHECK-SAME: to %swift.full_type*
// CHECK-SAME: ),
// CHECK-SAME: i32 0,
// CHECK-SAME: i32 1
// CHECK-SAME: ),
// CHECK-SAME: %swift.type* %{{[0-9]+}},
// CHECK-SAME: i8*** undef
// CHECK-SAME: )
}