Skip to content

Opaque archetypes specializer: Fix cast of nested types #24436

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
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
32 changes: 13 additions & 19 deletions lib/SILOptimizer/Transforms/SpecializeOpaqueArchetypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,19 @@ class OpaqueSpecializerCloner
loc, opd, type, /*withoutActuallyEscaping*/ false);
} else if (opd->getType().isTrivial(CurFn)) {
return getBuilder().createUncheckedTrivialBitCast(loc, opd, type);
} else {
} else if (opd->getType().isObject()) {
return getBuilder().createUncheckedRefCast(loc, opd, type);
} else {
// This could be improved upon by recursively recomposing the type.
auto *stackLoc = getBuilder().createAllocStack(loc, type);
auto *addr =
getBuilder().createUncheckedAddrCast(loc, stackLoc, opd->getType());
getBuilder().createTrivialStoreOr(loc, addr, opd,
StoreOwnershipQualifier::Init);
SILValue res = getBuilder().createTrivialLoadOr(
loc, addr, LoadOwnershipQualifier::Take);
getBuilder().createDeallocStack(loc, stackLoc);
return res;
}
}

Expand Down Expand Up @@ -394,24 +405,7 @@ void OpaqueSpecializerCloner::insertOpaqueToConcreteAddressCasts(
auto argIdx = apply.getCalleeArgIndex(opd);
auto argType = substConv.getSILArgumentType(argIdx);
if (argType.getASTType() != opd.get()->getType().getASTType()) {
if (argConv.isIndirectConvention()) {
auto cast = getBuilder().createUncheckedAddrCast(apply.getLoc(),
opd.get(), argType);
opd.set(cast);
} else if (argType.is<SILFunctionType>()) {
auto cast = getBuilder().createConvertFunction(
apply.getLoc(), opd.get(), argType,
/*withoutActuallyEscaping*/ false);
opd.set(cast);
} else if (argType.isTrivial(getBuilder().getFunction())) {
auto cast = getBuilder().createUncheckedTrivialBitCast(
apply.getLoc(), opd.get(), argType);
opd.set(cast);
} else {
auto cast = getBuilder().createUncheckedRefCast(apply.getLoc(),
opd.get(), argType);
opd.set(cast);
}
opd.set(createCast(apply.getLoc(), opd.get(), argType));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is createCast() equivalent to castValueToABICompatibleType() in include/swift/SILOptimizer/Utils/Local.h?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah looks like it might be. I will fix in a follow-up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Copy link
Contributor Author

@aschwaighofer aschwaighofer May 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow up: I can not use castValueToABICompatibleType because it does only handle tuple aggregates (no structs).

}
++idx;
}
Expand Down
26 changes: 25 additions & 1 deletion test/SILOptimizer/specialize_opaque_type_archetypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
// 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
// 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
// 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
// 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
// RUN: %target-swift-frontend -I %t -module-name A -enforce-exclusivity=checked -Osize -emit-sil -sil-verify-all %s | %FileCheck %s
// RUN: %target-swift-frontend -I %t -module-name A -enforce-exclusivity=checked -enable-library-evolution -Osize -emit-sil -sil-verify-all %s | %FileCheck %s

import External
import External2
import External3
Expand Down Expand Up @@ -374,16 +376,30 @@ public func testResilientInlinablePropertyCallsResilientInlinable() {
// RESILIENT: apply [[FUN]]([[RES]], %0)


protocol P4 {
public protocol P4 {
associatedtype AT
func foo(_ x: Int64) -> AT
func test()
}

struct PA : P4 {
func foo(_ x: Int64) -> some P {
return Int64(x)
}
}

// CHECK-LABEL: sil private [transparent] [thunk] @$s1A2PAVAA2P4A2aDP4testyyFTW
// CHECK: [[V:%.*]] = load %0 : $*PA
// CHECK: [[F:%.*]] = function_ref @$s1A2PAV4testyyF
// CHECK: apply [[F]]([[V]])

// CHECK-64-LABEL: sil hidden @$s1A2PAV4testyyF : $@convention(method) (PA) -> ()
// CHECK-64: [[V:%.*]] = integer_literal $Builtin.Int64, 5
// CHECK-64: [[I:%.*]] = struct $Int64 ([[V]] : $Builtin.Int64)
// CHECK-64: [[F:%.*]] = function_ref @$s1A4usePyyxAA1PRzlFs5Int64V_Tg5
// CHECK-64: apply [[F]]([[I]]) : $@convention(thin) (Int64) -> ()
// CHECK-64: apply [[F]]([[I]]) : $@convention(thin) (Int64) -> ()

@inline(never)
func testIt<T>(cl: (Int64) throws -> T) {
do {
Expand Down Expand Up @@ -443,3 +459,11 @@ public func testTuple() {
useP(t.0)
useP(t.1)
}

extension PA {
func test() {
var p = (foo, foo)
useP(p.0(5))
useP(p.1(5))
}
}