Skip to content

Compute the ISA encoding correctly for Class-bounded archetypes #15523

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
Mar 26, 2018
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: 2 additions & 0 deletions lib/IRGen/GenClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2314,6 +2314,8 @@ bool irgen::doesClassMetadataRequireDynamicInitialization(IRGenModule &IGM,
}

bool irgen::hasKnownSwiftMetadata(IRGenModule &IGM, CanType type) {
// This needs to be kept up-to-date with getIsaEncodingForType.

if (ClassDecl *theClass = type.getClassOrBoundGenericClass()) {
return hasKnownSwiftMetadata(IGM, theClass);
}
Expand Down
19 changes: 19 additions & 0 deletions lib/IRGen/GenHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1969,13 +1969,32 @@ static ClassDecl *getRootClass(ClassDecl *theClass) {
/// What isa encoding mechanism does a type have?
IsaEncoding irgen::getIsaEncodingForType(IRGenModule &IGM,
CanType type) {
if (!IGM.ObjCInterop) return IsaEncoding::Pointer;

// This needs to be kept up-to-date with hasKnownSwiftMetadata.

if (auto theClass = type->getClassOrBoundGenericClass()) {
// We can access the isas of pure Swift classes directly.
if (getRootClass(theClass)->hasKnownSwiftImplementation())
return IsaEncoding::Pointer;
// For ObjC or mixed classes, we need to use object_getClass.
return IsaEncoding::ObjC;
}

if (auto archetype = dyn_cast<ArchetypeType>(type)) {
// If we have a concrete superclass constraint, just recurse.
if (auto superclass = archetype->getSuperclass()) {
return getIsaEncodingForType(IGM, superclass->getCanonicalType());
}

// Otherwise, we must just have a class constraint. Use the
// conservative answer.
return IsaEncoding::ObjC;
}

// We should never be working with an unopened existential type here.
assert(!type->isAnyExistentialType());

// Non-class heap objects should be pure Swift, so we can access their isas
// directly.
return IsaEncoding::Pointer;
Expand Down
29 changes: 29 additions & 0 deletions test/IRGen/objc_casts.sil
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import Swift
import Foundation

class SwiftClass {}
sil_vtable SwiftClass {}

protocol ClassProto : class {}

// CHECK-LABEL: define hidden swiftcc %TSo8NSObjectC* @checkedClassBoundCast(%swift.type*, %TSo8NSObjectC*, %swift.type* %T) #0 {
// CHECK: [[OPAQUE_OBJ:%.+]] = bitcast %TSo8NSObjectC* %1 to i8*
// CHECK: [[OPAQUE_CLASS:%.+]] = bitcast %swift.type* %T to i8*
Expand Down Expand Up @@ -54,3 +59,27 @@ bb0(%metatype : $Optional<@thick T.Type>):
%tuple = tuple ()
return %tuple : $()
}

// CHECK-LABEL: define hidden swiftcc { %objc_object*, i8** } @swift_class_bounded_to_cp
// CHECK-SAME: ([[SWIFTCLASS:%T10objc_casts10SwiftClassC]]*, %swift.type* %T)
// CHECK: [[T0:%.*]] = bitcast [[SWIFTCLASS]]* %0 to %swift.type**
// CHECK-NEXT: [[TYPE:%.*]] = load %swift.type*, %swift.type** [[T0]],
// CHECK-NEXT: [[T0:%.*]] = bitcast [[SWIFTCLASS]]* %0 to i8*
// CHECK-NEXT: call { i8*, i8** } @dynamic_cast_existential_1_unconditional(i8* [[T0]], %swift.type* [[TYPE]], %swift.protocol* @"$S10objc_casts10ClassProtoMp")
sil hidden @swift_class_bounded_to_cp : $@convention(thin) <T: SwiftClass> (@owned T) -> @owned ClassProto {
entry(%a : $T):
%0 = unconditional_checked_cast %a : $T to $ClassProto
return %0 : $ClassProto
}

// CHECK-LABEL: define hidden swiftcc { %objc_object*, i8** } @objc_class_bounded_to_cp
// CHECK-SAME: ([[OBJCCLASS:%TSo8NSObjectC]]*, %swift.type* %T)
// CHECK: [[T0:%.*]] = bitcast [[OBJCCLASS]]* %0 to %objc_object*
// CHECK-NEXT: [[TYPE:%.*]] = call %swift.type* @swift_getObjectType(%objc_object* [[T0]])
// CHECK-NEXT: [[T0:%.*]] = bitcast [[OBJCCLASS]]* %0 to i8*
// CHECK-NEXT: call { i8*, i8** } @dynamic_cast_existential_1_unconditional(i8* [[T0]], %swift.type* [[TYPE]], %swift.protocol* @"$S10objc_casts10ClassProtoMp")
sil hidden @objc_class_bounded_to_cp : $@convention(thin) <T: NSObject> (@owned T) -> @owned ClassProto {
entry(%a : $T):
%0 = unconditional_checked_cast %a : $T to $ClassProto
return %0 : $ClassProto
}