Skip to content

Commit 3089c86

Browse files
Merge pull request #24436 from aschwaighofer/opaque_specializer_fix_cast_nested_types
Opaque archetypes specializer: Fix cast of nested types
2 parents 6069814 + d435460 commit 3089c86

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

lib/SILOptimizer/Transforms/SpecializeOpaqueArchetypes.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,19 @@ class OpaqueSpecializerCloner
332332
loc, opd, type, /*withoutActuallyEscaping*/ false);
333333
} else if (opd->getType().isTrivial(CurFn)) {
334334
return getBuilder().createUncheckedTrivialBitCast(loc, opd, type);
335-
} else {
335+
} else if (opd->getType().isObject()) {
336336
return getBuilder().createUncheckedRefCast(loc, opd, type);
337+
} else {
338+
// This could be improved upon by recursively recomposing the type.
339+
auto *stackLoc = getBuilder().createAllocStack(loc, type);
340+
auto *addr =
341+
getBuilder().createUncheckedAddrCast(loc, stackLoc, opd->getType());
342+
getBuilder().createTrivialStoreOr(loc, addr, opd,
343+
StoreOwnershipQualifier::Init);
344+
SILValue res = getBuilder().createTrivialLoadOr(
345+
loc, addr, LoadOwnershipQualifier::Take);
346+
getBuilder().createDeallocStack(loc, stackLoc);
347+
return res;
337348
}
338349
}
339350

@@ -400,24 +411,7 @@ void OpaqueSpecializerCloner::insertOpaqueToConcreteAddressCasts(
400411
auto argIdx = apply.getCalleeArgIndex(opd);
401412
auto argType = substConv.getSILArgumentType(argIdx);
402413
if (argType.getASTType() != opd.get()->getType().getASTType()) {
403-
if (argConv.isIndirectConvention()) {
404-
auto cast = getBuilder().createUncheckedAddrCast(apply.getLoc(),
405-
opd.get(), argType);
406-
opd.set(cast);
407-
} else if (argType.is<SILFunctionType>()) {
408-
auto cast = getBuilder().createConvertFunction(
409-
apply.getLoc(), opd.get(), argType,
410-
/*withoutActuallyEscaping*/ false);
411-
opd.set(cast);
412-
} else if (argType.isTrivial(getBuilder().getFunction())) {
413-
auto cast = getBuilder().createUncheckedTrivialBitCast(
414-
apply.getLoc(), opd.get(), argType);
415-
opd.set(cast);
416-
} else {
417-
auto cast = getBuilder().createUncheckedRefCast(apply.getLoc(),
418-
opd.get(), argType);
419-
opd.set(cast);
420-
}
414+
opd.set(createCast(apply.getLoc(), opd.get(), argType));
421415
}
422416
++idx;
423417
}

test/SILOptimizer/specialize_opaque_type_archetypes.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// RUN: %target-swift-frontend %S/Inputs/specialize_opaque_type_archetypes_3.swift -enable-library-evolution -module-name External2 -emit-module -emit-module-path %t/External2.swiftmodule
44
// RUN: %target-swift-frontend %S/Inputs/specialize_opaque_type_archetypes_4.swift -I %t -enable-library-evolution -module-name External3 -emit-module -emit-module-path %t/External3.swiftmodule
55
// RUN: %target-swift-frontend %S/Inputs/specialize_opaque_type_archetypes_3.swift -I %t -enable-library-evolution -module-name External2 -Osize -emit-module -o - | %target-sil-opt -module-name External2 | %FileCheck --check-prefix=RESILIENT %s
6+
// RUN: %target-swift-frontend -I %t -module-name A -enforce-exclusivity=checked -Osize -emit-sil -sil-verify-all %s | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-%ptrsize
67
// RUN: %target-swift-frontend -I %t -module-name A -enforce-exclusivity=checked -Osize -emit-sil -sil-verify-all %s | %FileCheck %s
78
// RUN: %target-swift-frontend -I %t -module-name A -enforce-exclusivity=checked -enable-library-evolution -Osize -emit-sil -sil-verify-all %s | %FileCheck %s
9+
810
import External
911
import External2
1012
import External3
@@ -374,16 +376,30 @@ public func testResilientInlinablePropertyCallsResilientInlinable() {
374376
// RESILIENT: apply [[FUN]]([[RES]], %0)
375377

376378

377-
protocol P4 {
379+
public protocol P4 {
378380
associatedtype AT
379381
func foo(_ x: Int64) -> AT
382+
func test()
380383
}
384+
381385
struct PA : P4 {
382386
func foo(_ x: Int64) -> some P {
383387
return Int64(x)
384388
}
385389
}
386390

391+
// CHECK-LABEL: sil private [transparent] [thunk] @$s1A2PAVAA2P4A2aDP4testyyFTW
392+
// CHECK: [[V:%.*]] = load %0 : $*PA
393+
// CHECK: [[F:%.*]] = function_ref @$s1A2PAV4testyyF
394+
// CHECK: apply [[F]]([[V]])
395+
396+
// CHECK-64-LABEL: sil hidden @$s1A2PAV4testyyF : $@convention(method) (PA) -> ()
397+
// CHECK-64: [[V:%.*]] = integer_literal $Builtin.Int64, 5
398+
// CHECK-64: [[I:%.*]] = struct $Int64 ([[V]] : $Builtin.Int64)
399+
// CHECK-64: [[F:%.*]] = function_ref @$s1A4usePyyxAA1PRzlFs5Int64V_Tg5
400+
// CHECK-64: apply [[F]]([[I]]) : $@convention(thin) (Int64) -> ()
401+
// CHECK-64: apply [[F]]([[I]]) : $@convention(thin) (Int64) -> ()
402+
387403
@inline(never)
388404
func testIt<T>(cl: (Int64) throws -> T) {
389405
do {
@@ -443,3 +459,11 @@ public func testTuple() {
443459
useP(t.0)
444460
useP(t.1)
445461
}
462+
463+
extension PA {
464+
func test() {
465+
var p = (foo, foo)
466+
useP(p.0(5))
467+
useP(p.1(5))
468+
}
469+
}

0 commit comments

Comments
 (0)