Skip to content

Commit 07cd329

Browse files
authored
Merge pull request #61219 from eeckstein/fix-fast-class-casting
IRGen: fix how metadata is loaded for fast class casting
2 parents d015505 + 2527d37 commit 07cd329

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

lib/IRGen/GenCast.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ llvm::Value *irgen::emitFastClassCastIfPossible(IRGenFunction &IGF,
11081108

11091109
// Load the isa pointer.
11101110
llvm::Value *objMetadata = emitHeapMetadataRefForHeapObject(IGF, instance,
1111-
targetFormalType, GenericSignature(), /*suppress cast*/ true);
1111+
sourceFormalType, GenericSignature(), /*suppress cast*/ true);
11121112
llvm::Value *rhs = IGF.Builder.CreateBitCast(objMetadata, IGF.IGM.Int8PtrTy);
11131113

11141114
// return isa_ptr == metadata_ptr ? instance : nullptr

lib/IRGen/GenHeap.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1917,7 +1917,8 @@ llvm::Value *irgen::emitHeapMetadataRefForHeapObject(IRGenFunction &IGF,
19171917
GenericSignature sig,
19181918
bool suppressCast) {
19191919
ClassDecl *theClass = objectType.getClassOrBoundGenericClass();
1920-
if (theClass && isKnownNotTaggedPointer(IGF.IGM, theClass)) {
1920+
if ((theClass && isKnownNotTaggedPointer(IGF.IGM, theClass)) ||
1921+
!IGF.IGM.ObjCInterop) {
19211922
auto isaEncoding = getIsaEncodingForType(IGF.IGM, objectType, sig);
19221923
return emitLoadOfHeapMetadataRef(IGF, object,
19231924
isaEncoding,

test/Casting/fast_class_casts.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
// 2. check if the generated IR looks like expected:
1313

14-
// RUN: %target-swift-frontend -module-name=Main -I%t %s -emit-ir -g -o - | %FileCheck %s
14+
// RUN: %target-swift-frontend -module-name=Main -I%t %s -emit-ir -g -o - | %FileCheck %s -check-prefix=CHECK -check-prefix=CHECK-%target-objc-interop
1515

1616
// REQUIRES: executable_test
1717

@@ -48,6 +48,7 @@ func castToFinal(_ b: Classes.Base) -> Classes.Final? {
4848
}
4949

5050
// CHECK-LABEL: define {{.*}} @"$s4Main24unconditionalCastToFinaly7Classes0E0CAC4BaseCF"
51+
// CHECK-NOT: call {{.*}}@object_getClass
5152
// CHECK-NOT: @swift_dynamicCastClass
5253
// CHECK: }
5354
@inline(never)
@@ -64,6 +65,8 @@ func castToResilientFinal(_ b: ResilientClasses.Base) -> ResilientClasses.Final?
6465
}
6566

6667
// CHECK-LABEL: define {{.*}} @"$s4Main19castProtocolToFinaly7Classes0E0CSgAC1P_pF"
68+
// CHECK-objc: call {{.*}}@object_getClass
69+
// CHECK-nonobjc: load
6770
// CHECK-NOT: @swift_dynamicCastClass
6871
// CHECK: }
6972
@inline(never)

0 commit comments

Comments
 (0)