Skip to content

[DynamicCast] Rely on runtime when casts can't be optimized #33761

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 2 commits into from
Oct 28, 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
52 changes: 40 additions & 12 deletions lib/IRGen/GenCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,8 @@ llvm::Value *irgen::emitMetatypeToAnyObjectDowncast(IRGenFunction &IGF,
CheckedCastMode mode) {
// If ObjC interop is enabled, casting a metatype to AnyObject succeeds
// if the metatype is for a class.

auto triviallyFail = [&]() -> llvm::Value* {
return llvm::ConstantPointerNull::get(IGF.IGM.ObjCPtrTy);
};

if (!IGF.IGM.ObjCInterop)
return triviallyFail();
return nullptr;

switch (type->getRepresentation()) {
case MetatypeRepresentation::ObjC:
Expand All @@ -496,7 +491,7 @@ llvm::Value *irgen::emitMetatypeToAnyObjectDowncast(IRGenFunction &IGF,
// TODO: Final class metatypes could in principle be thin.
assert(!type.getInstanceType()->mayHaveSuperclass()
&& "classes should not have thin metatypes (yet)");
return triviallyFail();
return nullptr;

case MetatypeRepresentation::Thick: {
auto instanceTy = type.getInstanceType();
Expand All @@ -508,10 +503,10 @@ llvm::Value *irgen::emitMetatypeToAnyObjectDowncast(IRGenFunction &IGF,
return IGF.Builder.CreateBitCast(heapMetadata, IGF.IGM.ObjCPtrTy);
}

// Is the type obviously not a class?
if (!isa<ArchetypeType>(instanceTy)
&& !isa<ExistentialMetatypeType>(type))
return triviallyFail();
// If it's not a class, we can't handle it here
if (!isa<ArchetypeType>(instanceTy) && !isa<ExistentialMetatypeType>(type)) {
return nullptr;
}

// Ask the runtime whether this is class metadata.
llvm::Constant *castFn;
Expand Down Expand Up @@ -966,10 +961,43 @@ void irgen::emitScalarCheckedCast(IRGenFunction &IGF,
// Otherwise, this is a metatype-to-object cast.
assert(targetLoweredType.isAnyClassReferenceType());

// Convert the metatype value to AnyObject.
// Can we convert the metatype value to AnyObject using Obj-C machinery?
llvm::Value *object =
emitMetatypeToAnyObjectDowncast(IGF, metatypeVal, sourceMetatype, mode);

if (object == nullptr) {
// Obj-C cast routine failed, use swift_dynamicCast instead

if (sourceMetatype->getRepresentation() == MetatypeRepresentation::Thin
|| metatypeVal == nullptr) {
// Earlier stages *should* never generate a checked cast with a thin metatype argument.
// TODO: Move this assertion up to apply to all checked cast operations.
// In assert builds, enforce this by failing here:
assert(false && "Invalid SIL: General checked_cast_br cannot have thin argument");
// In non-assert builds, stay compatible with previous behavior by emitting a null load.
object = llvm::ConstantPointerNull::get(IGF.IGM.ObjCPtrTy);
} else {
Address src = IGF.createAlloca(metatypeVal->getType(),
IGF.IGM.getPointerAlignment(),
"castSrc");
IGF.Builder.CreateStore(metatypeVal, src);
llvm::PointerType *destPtrType = IGF.IGM.getStoragePointerType(targetLoweredType);
Address dest = IGF.createAlloca(destPtrType,
IGF.IGM.getPointerAlignment(),
"castDest");
IGF.Builder.CreateStore(llvm::ConstantPointerNull::get(destPtrType), dest);
llvm::Value *success = emitCheckedCast(IGF,
src, sourceFormalType,
dest, targetFormalType,
CastConsumptionKind::TakeAlways,
mode);
llvm::Value *successResult = IGF.Builder.CreateLoad(dest);
llvm::Value *failureResult = llvm::ConstantPointerNull::get(destPtrType);
llvm::Value *result = IGF.Builder.CreateSelect(success, successResult, failureResult);
object = std::move(result);
}
}

sourceFormalType = IGF.IGM.Context.getAnyObjectType();
sourceLoweredType = SILType::getPrimitiveObjectType(sourceFormalType);

Expand Down
7 changes: 1 addition & 6 deletions test/Casting/Casts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -522,12 +522,7 @@ class ClassInt: Equatable, Hashable {
static func == (lhs: ClassInt, rhs: ClassInt) -> Bool {return true}
func hash(into hasher: inout Hasher) {}
}
CastsTests.test("AnyHashable(Class) -> Obj-C -> Class")
.skip(.custom({
!_isDebugAssertConfiguration()
},
reason: "Cast optimizer breaks this test"))
.code {
CastsTests.test("AnyHashable(Class) -> Obj-C -> Class") {
let a = ClassInt()
let b = runtimeCast(a, to: AnyHashable.self)!
let c = _bridgeAnythingToObjectiveC(b)
Expand Down
6 changes: 3 additions & 3 deletions test/IRGen/casts.sil
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,9 @@ bb3(%9 : $Optional<CP>):
// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swiftcc void @checked_metatype_to_object_casts
sil @checked_metatype_to_object_casts : $@convention(thin) <T> (@thick Any.Type) -> () {
entry(%e : $@thick Any.Type):
%a = metatype $@thin NotClass.Type
// CHECK: br i1 false
checked_cast_br %a : $@thin NotClass.Type to AnyObject, a_yea, a_nay
%a = metatype $@thick NotClass.Type
// CHECK: call i1 @swift_dynamicCast({{.*}})
checked_cast_br %a : $@thick NotClass.Type to AnyObject, a_yea, a_nay
a_yea(%1 : $AnyObject):
%b = metatype $@thick A.Type
// CHECK: bitcast %swift.type* {{%.*}} to %objc_object*
Expand Down
23 changes: 17 additions & 6 deletions validation-test/Casting/BoxingCasts.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,27 @@
// RUN: %empty-directory(%t)
//
// RUN: %gyb %s -o %t/BoxingCasts.swift
// RUN: %line-directive %t/BoxingCasts.swift -- %target-build-swift -g -module-name a -swift-version 5 -Onone %t/BoxingCasts.swift -o %t/a.swift5.Onone.out
//
// RUN: %line-directive %t/BoxingCasts.swift -- %target-build-swift -g -module-name a -Onone -swift-version 5 %t/BoxingCasts.swift -o %t/a.swift5.Onone.out
// RUN: %target-codesign %t/a.swift5.Onone.out
// RUN: %line-directive %t/BoxingCasts.swift -- %target-run %t/a.swift5.Onone.out
//
// Note: The RUN directives above override the default test optimizations.
// This test is deliberately run non-optimized in order to verify the
// behavior of runtime methods that may not be called for optimized casts.
// RUN: %line-directive %t/BoxingCasts.swift -- %target-build-swift -g -O -module-name a -O -swift-version 5 %t/BoxingCasts.swift -o %t/a.swift5.O.out
// RUN: %target-codesign %t/a.swift5.O.out
// RUN: %line-directive %t/BoxingCasts.swift -- %target-run %t/a.swift5.O.out
//
// RUN: %line-directive %t/BoxingCasts.swift -- %target-build-swift -g -module-name a -Onone -swift-version 4 %t/BoxingCasts.swift -o %t/a.swift4.Onone.out
// RUN: %target-codesign %t/a.swift4.Onone.out
// RUN: %line-directive %t/BoxingCasts.swift -- %target-run %t/a.swift4.Onone.out
//
// XXX FIXME XXX TODO XXX _Also_ build this with optimizations in order to
// verify compiler behaviors.
// RUN: %line-directive %t/BoxingCasts.swift -- %target-build-swift -g -O -module-name a -O -swift-version 4 %t/BoxingCasts.swift -o %t/a.swift4.O.out
// RUN: %target-codesign %t/a.swift4.O.out
// RUN: %line-directive %t/BoxingCasts.swift -- %target-run %t/a.swift4.O.out
//
// Note: The RUN directives above override the default test optimizations.
// This test is deliberately run both ways:
// * optimized to verify compiler cast optimizations, and
// * non-optimized to verify the runtime methods used for non-optimized casts.
//
// REQUIRES: executable_test

Expand Down