Skip to content

Commit 1330d12

Browse files
committed
Opaque archetypes specializer: Fix cast of nested types
1 parent 8d2b1bb commit 1330d12

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

lib/SILOptimizer/Transforms/SpecializeOpaqueArchetypes.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,19 @@ class OpaqueSpecializerCloner
326326
loc, opd, type, /*withoutActuallyEscaping*/ false);
327327
} else if (opd->getType().isTrivial(CurFn)) {
328328
return getBuilder().createUncheckedTrivialBitCast(loc, opd, type);
329-
} else {
329+
} else if (opd->getType().isObject()) {
330330
return getBuilder().createUncheckedRefCast(loc, opd, type);
331+
} else {
332+
// This could be improved upon by recursively recomposing the type.
333+
auto *stackLoc = getBuilder().createAllocStack(loc, type);
334+
auto *addr =
335+
getBuilder().createUncheckedAddrCast(loc, stackLoc, opd->getType());
336+
getBuilder().createTrivialStoreOr(loc, addr, opd,
337+
StoreOwnershipQualifier::Init);
338+
SILValue res = getBuilder().createTrivialLoadOr(
339+
loc, addr, LoadOwnershipQualifier::Take);
340+
getBuilder().createDeallocStack(loc, stackLoc);
341+
return res;
331342
}
332343
}
333344

@@ -394,24 +405,7 @@ void OpaqueSpecializerCloner::insertOpaqueToConcreteAddressCasts(
394405
auto argIdx = apply.getCalleeArgIndex(opd);
395406
auto argType = substConv.getSILArgumentType(argIdx);
396407
if (argType.getASTType() != opd.get()->getType().getASTType()) {
397-
if (argConv.isIndirectConvention()) {
398-
auto cast = getBuilder().createUncheckedAddrCast(apply.getLoc(),
399-
opd.get(), argType);
400-
opd.set(cast);
401-
} else if (argType.is<SILFunctionType>()) {
402-
auto cast = getBuilder().createConvertFunction(
403-
apply.getLoc(), opd.get(), argType,
404-
/*withoutActuallyEscaping*/ false);
405-
opd.set(cast);
406-
} else if (argType.isTrivial(getBuilder().getFunction())) {
407-
auto cast = getBuilder().createUncheckedTrivialBitCast(
408-
apply.getLoc(), opd.get(), argType);
409-
opd.set(cast);
410-
} else {
411-
auto cast = getBuilder().createUncheckedRefCast(apply.getLoc(),
412-
opd.get(), argType);
413-
opd.set(cast);
414-
}
408+
opd.set(createCast(apply.getLoc(), opd.get(), argType));
415409
}
416410
++idx;
417411
}

test/SILOptimizer/specialize_opaque_type_archetypes.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,16 +374,30 @@ public func testResilientInlinablePropertyCallsResilientInlinable() {
374374
// RESILIENT: apply [[FUN]]([[RES]], %0)
375375

376376

377-
protocol P4 {
377+
public protocol P4 {
378378
associatedtype AT
379379
func foo(_ x: Int64) -> AT
380+
func test()
380381
}
382+
381383
struct PA : P4 {
382384
func foo(_ x: Int64) -> some P {
383385
return Int64(x)
384386
}
385387
}
386388

389+
// CHECK-LABEL: sil private [transparent] [thunk] @$s1A2PAVAA2P4A2aDP4testyyFTW
390+
// CHECK: [[V:%.*]] = load %0 : $*PA
391+
// CHECK: [[F:%.*]] = function_ref @$s1A2PAV4testyyF
392+
// CHECK: apply [[F]]([[V]])
393+
394+
// CHECK-LABEL: sil hidden @$s1A2PAV4testyyF : $@convention(method) (PA) -> ()
395+
// CHECK: [[V:%.*]] = integer_literal $Builtin.Int64, 5
396+
// CHECK: [[I:%.*]] = struct $Int64 ([[V]] : $Builtin.Int64)
397+
// CHECK: [[F:%.*]] = function_ref @$s1A4usePyyxAA1PRzlFs5Int64V_Tg5
398+
// CHECK: apply [[F]]([[I]]) : $@convention(thin) (Int64) -> ()
399+
// CHECK: apply [[F]]([[I]]) : $@convention(thin) (Int64) -> ()
400+
387401
@inline(never)
388402
func testIt<T>(cl: (Int64) throws -> T) {
389403
do {
@@ -443,3 +457,11 @@ public func testTuple() {
443457
useP(t.0)
444458
useP(t.1)
445459
}
460+
461+
extension PA {
462+
func test() {
463+
var p = (foo, foo)
464+
useP(p.0(5))
465+
useP(p.1(5))
466+
}
467+
}

0 commit comments

Comments
 (0)