Skip to content

[metadata prespecialization] Support classes with non-generic ancestors. #32174

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
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
23 changes: 16 additions & 7 deletions lib/IRGen/GenClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ namespace {
/// to compute FieldAccesses for them.
void addFieldsForClass(ClassDecl *theClass, SILType classType,
bool superclass) {
addFieldsForClassImpl(theClass, classType, theClass, classType,
superclass);
}

void addFieldsForClassImpl(ClassDecl *rootClass, SILType rootClassType,
ClassDecl *theClass, SILType classType,
bool superclass) {
if (theClass->hasClangNode()) {
Options |= ClassMetadataFlags::ClassHasObjCAncestry;
return;
Expand Down Expand Up @@ -240,7 +247,8 @@ namespace {
} else {
// Otherwise, we are allowed to have total knowledge of the superclass
// fields, so walk them to compute the layout.
addFieldsForClass(superclassDecl, superclassType, /*superclass=*/true);
addFieldsForClassImpl(rootClass, rootClassType, superclassDecl,
superclassType, /*superclass=*/true);
}
}

Expand All @@ -259,11 +267,12 @@ namespace {
}

// Collect fields from this class and add them to the layout as a chunk.
addDirectFieldsFromClass(theClass, classType, superclass);
addDirectFieldsFromClass(rootClass, rootClassType, theClass, classType,
superclass);
}

void addDirectFieldsFromClass(ClassDecl *theClass,
SILType classType,
void addDirectFieldsFromClass(ClassDecl *rootClass, SILType rootClassType,
ClassDecl *theClass, SILType classType,
bool superclass) {
for (VarDecl *var : theClass->getStoredProperties()) {
SILType type = classType.getFieldType(var, IGM.getSILModule(),
Expand All @@ -287,9 +296,9 @@ namespace {
bool isKnownEmpty = !addField(element, LayoutStrategy::Universal);

bool isSpecializedGeneric =
(theClass->isGenericContext() && !classType.getASTType()
->getRecursiveProperties()
.hasUnboundGeneric());
(rootClass->isGenericContext() && !rootClassType.getASTType()
->getRecursiveProperties()
.hasUnboundGeneric());

// The 'Elements' list only contains superclass fields when we're
// building a layout for tail allocation.
Expand Down
38 changes: 24 additions & 14 deletions lib/IRGen/MetadataRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,10 @@ bool irgen::isCanonicalSpecializedNominalTypeMetadataStaticallyAddressable(
auto superclassType =
type->getSuperclass(/*useArchetypes=*/false)->getCanonicalType();
if (!isInitializableTypeMetadataStaticallyAddressable(IGM,
superclassType)) {
superclassType) &&
!tryEmitConstantHeapMetadataRef(
IGM, superclassType,
/*allowDynamicUninitialized=*/false)) {
return false;
}
}
Expand Down Expand Up @@ -2171,12 +2174,21 @@ emitIdempotentCanonicalSpecializedClassMetadataInitializationComponent(
return;
}
initializedTypes.insert(theType);
llvm::Function *accessor =
IGF.IGM.getAddrOfCanonicalSpecializedGenericTypeMetadataAccessFunction(
theType, NotForDefinition);
auto *classDecl = theType->getClassOrBoundGenericClass();
assert(classDecl);
if (classDecl->isGenericContext()) {
llvm::Function *accessor =
IGF.IGM.getAddrOfCanonicalSpecializedGenericTypeMetadataAccessFunction(
theType, NotForDefinition);

auto request = DynamicMetadataRequest(MetadataState::Complete);
IGF.emitGenericTypeMetadataAccessFunctionCall(accessor, {}, request);
auto request = DynamicMetadataRequest(MetadataState::Complete);
IGF.emitGenericTypeMetadataAccessFunctionCall(accessor, {}, request);
} else {
llvm::Function *accessor =
IGF.IGM.getAddrOfTypeMetadataAccessFunction(theType, NotForDefinition);
auto request = DynamicMetadataRequest(MetadataState::Complete);
IGF.emitGenericTypeMetadataAccessFunctionCall(accessor, {}, request);
}
}

MetadataResponse
Expand All @@ -2196,10 +2208,6 @@ irgen::emitCanonicalSpecializedGenericTypeMetadataAccessFunction(
assert(nominal->isGenericContext());
assert(!theType->hasUnboundGenericType());

auto *uninitializedMetadata = IGF.IGM.getAddrOfTypeMetadata(theType);
initializedTypes.insert(theType);
auto *initializedMetadata =
emitIdempotentClassMetadataInitialization(IGF, uninitializedMetadata);
auto requirements = GenericTypeRequirements(IGF.IGM, nominal);
auto substitutions =
theType->getContextSubstitutionMap(IGF.IGM.getSwiftModule(), nominal);
Expand All @@ -2219,12 +2227,14 @@ irgen::emitCanonicalSpecializedGenericTypeMetadataAccessFunction(
if (superclassType) {
auto superclass = superclassType->getCanonicalType();
auto *superclassNominal = superclass->getAnyNominal();
if (superclassNominal->isGenericContext()) {
emitIdempotentCanonicalSpecializedClassMetadataInitializationComponent(
IGF, superclassType->getCanonicalType(), initializedTypes);
}
emitIdempotentCanonicalSpecializedClassMetadataInitializationComponent(
IGF, superclassType->getCanonicalType(), initializedTypes);
}

auto *uninitializedMetadata = IGF.IGM.getAddrOfTypeMetadata(theType);
initializedTypes.insert(theType);
auto *initializedMetadata =
emitIdempotentClassMetadataInitialization(IGF, uninitializedMetadata);
return MetadataResponse::forComplete(initializedMetadata);
}

Expand Down
33 changes: 33 additions & 0 deletions stdlib/private/StdlibUnittest/StdlibUnittest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1879,6 +1879,7 @@ public enum TestRunPredicate : CustomStringConvertible {

case iOSAny(/*reason:*/ String)
case iOSMajor(Int, reason: String)
case iOSMajorRange(ClosedRange<Int>, reason: String)
case iOSMinor(Int, Int, reason: String)
case iOSMinorRange(Int, ClosedRange<Int>, reason: String)
case iOSBugFix(Int, Int, Int, reason: String)
Expand All @@ -1888,6 +1889,7 @@ public enum TestRunPredicate : CustomStringConvertible {

case tvOSAny(/*reason:*/ String)
case tvOSMajor(Int, reason: String)
case tvOSMajorRange(ClosedRange<Int>, reason: String)
case tvOSMinor(Int, Int, reason: String)
case tvOSMinorRange(Int, ClosedRange<Int>, reason: String)
case tvOSBugFix(Int, Int, Int, reason: String)
Expand All @@ -1897,6 +1899,7 @@ public enum TestRunPredicate : CustomStringConvertible {

case watchOSAny(/*reason:*/ String)
case watchOSMajor(Int, reason: String)
case watchOSMajorRange(ClosedRange<Int>, reason: String)
case watchOSMinor(Int, Int, reason: String)
case watchOSMinorRange(Int, ClosedRange<Int>, reason: String)
case watchOSBugFix(Int, Int, Int, reason: String)
Expand Down Expand Up @@ -1948,6 +1951,8 @@ public enum TestRunPredicate : CustomStringConvertible {
return "iOS(*, reason: \(reason))"
case .iOSMajor(let major, let reason):
return "iOS(\(major).*, reason: \(reason))"
case .iOSMajorRange(let range, let reason):
return "iOS([\(range)], reason: \(reason))"
case .iOSMinor(let major, let minor, let reason):
return "iOS(\(major).\(minor), reason: \(reason))"
case .iOSMinorRange(let major, let minorRange, let reason):
Expand All @@ -1964,6 +1969,8 @@ public enum TestRunPredicate : CustomStringConvertible {
return "tvOS(*, reason: \(reason))"
case .tvOSMajor(let major, let reason):
return "tvOS(\(major).*, reason: \(reason))"
case .tvOSMajorRange(let range, let reason):
return "tvOS([\(range)], reason: \(reason))"
case .tvOSMinor(let major, let minor, let reason):
return "tvOS(\(major).\(minor), reason: \(reason))"
case .tvOSMinorRange(let major, let minorRange, let reason):
Expand All @@ -1980,6 +1987,8 @@ public enum TestRunPredicate : CustomStringConvertible {
return "watchOS(*, reason: \(reason))"
case .watchOSMajor(let major, let reason):
return "watchOS(\(major).*, reason: \(reason))"
case .watchOSMajorRange(let range, let reason):
return "watchOS([\(range)], reason: \(reason))"
case .watchOSMinor(let major, let minor, let reason):
return "watchOS(\(major).\(minor), reason: \(reason))"
case .watchOSMinorRange(let major, let minorRange, let reason):
Expand Down Expand Up @@ -2094,6 +2103,14 @@ public enum TestRunPredicate : CustomStringConvertible {
return false
}

case .iOSMajorRange(let range, _):
switch _getRunningOSVersion() {
case .iOS(let major, _, _):
return range.contains(major)
default:
return false
}

case .iOSMinor(let major, let minor, _):
switch _getRunningOSVersion() {
case .iOS(major, minor, _):
Expand Down Expand Up @@ -2150,6 +2167,14 @@ public enum TestRunPredicate : CustomStringConvertible {
return false
}

case .tvOSMajorRange(let range, _):
switch _getRunningOSVersion() {
case .tvOS(let major, _, _):
return range.contains(major)
default:
return false
}

case .tvOSMinor(let major, let minor, _):
switch _getRunningOSVersion() {
case .tvOS(major, minor, _):
Expand Down Expand Up @@ -2206,6 +2231,14 @@ public enum TestRunPredicate : CustomStringConvertible {
return false
}

case .watchOSMajorRange(let range, _):
switch _getRunningOSVersion() {
case .watchOS(let major, _, _):
return range.contains(major)
default:
return false
}

case .watchOSMinor(let major, let minor, _):
switch _getRunningOSVersion() {
case .watchOS(major, minor, _):
Expand Down
19 changes: 19 additions & 0 deletions stdlib/public/core/Prespecialize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ internal func _prespecialize() {
consume(Array<Int>.self)
consume(Array<Optional<Any>>.self)
consume(Array<Optional<String>>.self)
#if _runtime(_ObjC)
consume(_ArrayBuffer<()>.self)
consume(_ArrayBuffer<(Optional<String>, Any)>.self)
consume(_ArrayBuffer<Any>.self)
consume(_ArrayBuffer<AnyHashable>.self)
consume(_ArrayBuffer<Dictionary<String, Any>>.self)
consume(_ArrayBuffer<Dictionary<String, AnyObject>>.self)
consume(_ArrayBuffer<Int64>.self)
consume(_ArrayBuffer<Int>.self)
consume(_ArrayBuffer<Optional<Any>>.self)
consume(_ArrayBuffer<Optional<String>>.self)
#endif
consume(ClosedRange<Int>.self)
consume(ContiguousArray<(AnyHashable, Any)>.self)
consume(ContiguousArray<(Optional<String>, Any)>.self)
Expand All @@ -22,6 +34,13 @@ internal func _prespecialize() {
consume(ContiguousArray<Dictionary<String, AnyObject>>.self)
consume(ContiguousArray<Int64>.self)
consume(ContiguousArray<String>.self)
consume(_ContiguousArrayStorage<(AnyHashable, Any)>.self)
consume(_ContiguousArrayStorage<(Optional<String>, Any)>.self)
consume(_ContiguousArrayStorage<Any>.self)
consume(_ContiguousArrayStorage<AnyHashable>.self)
consume(_ContiguousArrayStorage<Dictionary<String, AnyObject>>.self)
consume(_ContiguousArrayStorage<Int64>.self)
consume(_ContiguousArrayStorage<String>.self)
consume(Dictionary<String, String>.Index.self)
consume(Dictionary<String, String>.Iterator.self)
consume(Dictionary<AnyHashable, Any>.self)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
open class Ancestor1 {
public let value: Int

public init(_ value: Int) {
self.value = value
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ doit()

// CHECK: define linkonce_odr hidden swiftcc %swift.metadata_response @"$s4main5Value[[UNIQUE_ID_4]]LLCyAA9Argument1ACLLCySiGAA9Argument2ACLLCySSGGMb"([[INT]] {{%[0-9]+}}) {{#[0-9]}} {
// CHECK: entry:
// CHECK-unknown: call swiftcc %swift.metadata_response @"$s4main9Argument1[[UNIQUE_ID_1]]LLCySiGMb"([[INT]] 0)
// CHECK-unknown: call swiftcc %swift.metadata_response @"$s4main9Argument2[[UNIQUE_ID_1]]LLCySSGMb"([[INT]] 0)
// CHECK: call swiftcc %swift.metadata_response @"$s4main9Argument1[[UNIQUE_ID_1]]LLCySiGMb"([[INT]] 0)
// CHECK: call swiftcc %swift.metadata_response @"$s4main9Argument2[[UNIQUE_ID_1]]LLCySSGMb"([[INT]] 0)
// CHECK-unknown: ret
// CHECK-apple: [[INITIALIZED_CLASS:%[0-9]+]] = call %objc_class* @objc_opt_self(
// : %objc_class* bitcast (
Expand Down Expand Up @@ -385,8 +385,6 @@ doit()
// : )
// : )
// CHECK-apple: [[INITIALIZED_METADATA:%[0-9]+]] = bitcast %objc_class* [[INITIALIZED_CLASS]] to %swift.type*
// CHECK-apple: call swiftcc %swift.metadata_response @"$s4main9Argument1[[UNIQUE_ID_1]]LLCySiGMb"([[INT]] 0)
// CHECK-apple: call swiftcc %swift.metadata_response @"$s4main9Argument2[[UNIQUE_ID_1]]LLCySSGMb"([[INT]] 0)
// CHECK-apple: [[PARTIAL_METADATA_RESPONSE:%[0-9]+]] = insertvalue %swift.metadata_response undef, %swift.type* [[INITIALIZED_METADATA]], 0
// CHECK-apple: [[METADATA_RESPONSE:%[0-9]+]] = insertvalue %swift.metadata_response [[PARTIAL_METADATA_RESPONSE]], [[INT]] 0, 1
// CHECK-apple: ret %swift.metadata_response [[METADATA_RESPONSE]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ doit()

// CHECK: define linkonce_odr hidden swiftcc %swift.metadata_response @"$s4main5Value[[UNIQUE_ID_4]]LLCyAA9Argument1ACLLCySiGAFySSGGMb"([[INT]] {{%[0-9]+}}) {{#[0-9]}} {
// CHECK: entry:
// CHECK-unknown: call swiftcc %swift.metadata_response @"$s4main9Argument1[[UNIQUE_ID_1]]LLCySiGMb"([[INT]] 0)
// CHECK-unknown: call swiftcc %swift.metadata_response @"$s4main9Argument1[[UNIQUE_ID_1]]LLCySSGMb"([[INT]] 0)
// CHECK: call swiftcc %swift.metadata_response @"$s4main9Argument1[[UNIQUE_ID_1]]LLCySiGMb"([[INT]] 0)
// CHECK: call swiftcc %swift.metadata_response @"$s4main9Argument1[[UNIQUE_ID_1]]LLCySSGMb"([[INT]] 0)
// CHECK-unknown: ret
// CHECK-apple: [[INITIALIZED_CLASS:%[0-9]+]] = call %objc_class* @objc_opt_self(
// : %objc_class* bitcast (
Expand Down Expand Up @@ -375,8 +375,6 @@ doit()
// : )
// : )
// CHECK-apple: [[INITIALIZED_METADATA:%[0-9]+]] = bitcast %objc_class* [[INITIALIZED_CLASS]] to %swift.type*
// CHECK-apple: call swiftcc %swift.metadata_response @"$s4main9Argument1[[UNIQUE_ID_1]]LLCySiGMb"([[INT]] 0)
// CHECK-apple: call swiftcc %swift.metadata_response @"$s4main9Argument1[[UNIQUE_ID_1]]LLCySSGMb"([[INT]] 0)
// CHECK-apple: [[PARTIAL_METADATA_RESPONSE:%[0-9]+]] = insertvalue %swift.metadata_response undef, %swift.type* [[INITIALIZED_METADATA]], 0
// CHECK-apple: [[METADATA_RESPONSE:%[0-9]+]] = insertvalue %swift.metadata_response [[PARTIAL_METADATA_RESPONSE]], [[INT]] 0, 1
// CHECK-apple: ret %swift.metadata_response [[METADATA_RESPONSE]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ doit()

// CHECK: define linkonce_odr hidden swiftcc %swift.metadata_response @"$s4main5Value[[UNIQUE_ID_4]]LLCyAA9Argument1ACLLCySiGAGGMb"([[INT]] {{%[0-9]+}}) {{#[0-9]}} {
// CHECK: entry:
// CHECK-unknown: call swiftcc %swift.metadata_response @"$s4main9Argument1[[UNIQUE_ID_1]]LLCySiGMb"([[INT]] 0)
// CHECK-unknown-NOT: call swiftcc %swift.metadata_response @"$s4main9Argument1[[UNIQUE_ID_1]]LLCySiGMb"([[INT]] 0)
// CHECK: call swiftcc %swift.metadata_response @"$s4main9Argument1[[UNIQUE_ID_1]]LLCySiGMb"([[INT]] 0)
// CHECK-NOT: call swiftcc %swift.metadata_response @"$s4main9Argument1[[UNIQUE_ID_1]]LLCySiGMb"([[INT]] 0)
// CHECK-unknown: ret
// CHECK-apple: [[INITIALIZED_CLASS:%[0-9]+]] = call %objc_class* @objc_opt_self(
// : %objc_class* bitcast (
Expand Down Expand Up @@ -375,8 +375,6 @@ doit()
// : )
// : )
// CHECK-apple: [[INITIALIZED_METADATA:%[0-9]+]] = bitcast %objc_class* [[INITIALIZED_CLASS]] to %swift.type*
// CHECK-apple: call swiftcc %swift.metadata_response @"$s4main9Argument1[[UNIQUE_ID_1]]LLCySiGMb"([[INT]] 0)
// CHECK-apple-NOT: call swiftcc %swift.metadata_response @"$s4main9Argument1[[UNIQUE_ID_1]]LLCySiGMb"([[INT]] 0)
// CHECK-apple: [[PARTIAL_METADATA_RESPONSE:%[0-9]+]] = insertvalue %swift.metadata_response undef, %swift.type* [[INITIALIZED_METADATA]], 0
// CHECK-apple: [[METADATA_RESPONSE:%[0-9]+]] = insertvalue %swift.metadata_response [[PARTIAL_METADATA_RESPONSE]], [[INT]] 0, 1
// CHECK-apple: ret %swift.metadata_response [[METADATA_RESPONSE]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,12 @@ doit()
// CHECK: ; Function Attrs: noinline nounwind readnone
// CHECK: define linkonce_odr hidden swiftcc %swift.metadata_response @"$s4main5Value[[UNIQUE_ID_1]]CySSGMb"([[INT]] {{%[0-9]+}}) #{{[0-9]+}} {
// CHECK: entry:
// CHECK-unknown: call swiftcc %swift.metadata_response @"$s4main9Ancestor1[[UNIQUE_ID_1]]CySiGMb"([[INT]] 0)
// CHECK: call swiftcc %swift.metadata_response @"$s4main9Ancestor1[[UNIQUE_ID_1]]CySiGMb"([[INT]] 0)
// CHECK-NOT: call swiftcc %swift.metadata_response @"$s4main9Ancestor2[[UNIQUE_ID_1]]CySiGMb"([[INT]] 0)
// CHECK-unknown: ret
// CHECK-apple: [[INITIALIZED_CLASS:%[0-9]+]] = call %objc_class* @objc_opt_self(
// CHECK-SAME: @"$s4main5Value[[UNIQUE_ID_1]]CySSGMf"
// CHECK-apple: [[INITIALIZED_METADATA:%[0-9]+]] = bitcast %objc_class* [[INITIALIZED_CLASS]] to %swift.type*
// CHECK-apple: call swiftcc %swift.metadata_response @"$s4main9Ancestor1[[UNIQUE_ID_1]]CySiGMb"([[INT]] 0)
// CHECK-NOT: call swiftcc %swift.metadata_response @"$s4main9Ancestor2[[UNIQUE_ID_1]]CySiGMb"([[INT]] 0)
// CHECK-apple: [[PARTIAL_METADATA_RESPONSE:%[0-9]+]] = insertvalue %swift.metadata_response undef, %swift.type* [[INITIALIZED_METADATA]], 0
// CHECK-apple: [[METADATA_RESPONSE:%[0-9]+]] = insertvalue %swift.metadata_response [[PARTIAL_METADATA_RESPONSE]], [[INT]] 0, 1
// CHECK-apple: ret %swift.metadata_response [[METADATA_RESPONSE]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,12 @@ doit()
// CHECK: ; Function Attrs: noinline nounwind readnone
// CHECK: define linkonce_odr hidden swiftcc %swift.metadata_response @"$s4main5Value[[UNIQUE_ID_1]]CySSGMb"([[INT]] {{%[0-9]+}}) #{{[0-9]+}} {
// CHECK: entry:
// CHECK-unknown: call swiftcc %swift.metadata_response @"$s4main9Ancestor1[[UNIQUE_ID_1]]CySiGMb"([[INT]] 0)
// CHECK: call swiftcc %swift.metadata_response @"$s4main9Ancestor1[[UNIQUE_ID_1]]CySiGMb"([[INT]] 0)
// CHECK-NOT: call swiftcc %swift.metadata_response @"$s4main9Ancestor2[[UNIQUE_ID_1]]CySiGMb"([[INT]] 0)
// CHECK-unknown: ret
// CHECK-apple: [[INITIALIZED_CLASS:%[0-9]+]] = call %objc_class* @objc_opt_self(
// CHECK-SAME: @"$s4main5Value[[UNIQUE_ID_1]]CySSGMf"
// CHECK-apple: [[INITIALIZED_METADATA:%[0-9]+]] = bitcast %objc_class* [[INITIALIZED_CLASS]] to %swift.type*
// CHECK-apple: call swiftcc %swift.metadata_response @"$s4main9Ancestor1[[UNIQUE_ID_1]]CySiGMb"([[INT]] 0)
// CHECK-NOT: call swiftcc %swift.metadata_response @"$s4main9Ancestor2[[UNIQUE_ID_1]]CySiGMb"([[INT]] 0)
// CHECK-apple: [[PARTIAL_METADATA_RESPONSE:%[0-9]+]] = insertvalue %swift.metadata_response undef, %swift.type* [[INITIALIZED_METADATA]], 0
// CHECK-apple: [[METADATA_RESPONSE:%[0-9]+]] = insertvalue %swift.metadata_response [[PARTIAL_METADATA_RESPONSE]], [[INT]] 0, 1
// CHECK-apple: ret %swift.metadata_response [[METADATA_RESPONSE]]
Expand Down
Loading