Skip to content

Fix problems with pseudogeneric thunks (3.0) #4823

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
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
18 changes: 17 additions & 1 deletion lib/IRGen/GenReflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,10 @@ class CaptureDescriptorBuilder : public ReflectionMetadataBuilder {
MetadataSourceMap getMetadataSourceMap() {
MetadataSourceMap SourceMap;

if (!OrigCalleeType->isPolymorphic())
// Generic parameters of pseudogeneric functions do not have
// runtime metadata.
if (!OrigCalleeType->isPolymorphic() ||
OrigCalleeType->isPseudogeneric())
return SourceMap;

// Any generic parameters that are not fulfilled are passed in via the
Expand Down Expand Up @@ -702,6 +705,19 @@ class CaptureDescriptorBuilder : public ReflectionMetadataBuilder {

for (auto ElementType : getElementTypes()) {
auto SwiftType = ElementType.getSwiftRValueType();

// Erase pseudogeneric captures down to AnyObject.
if (OrigCalleeType->isPseudogeneric()) {
SwiftType = SwiftType.transform([&](Type t) -> Type {
if (auto *archetype = t->getAs<ArchetypeType>()) {
assert(archetype->requiresClass() && "don't know what to do");
return IGM.Context.getProtocol(KnownProtocolKind::AnyObject)
->getDeclaredType();
}
return t;
})->getCanonicalType();
}

auto InterfaceType = Caller.mapTypeOutOfContext(SwiftType);
CaptureTypes.push_back(InterfaceType->getCanonicalType());
}
Expand Down
25 changes: 15 additions & 10 deletions lib/SILGen/SILGenBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,11 @@ ManagedValue SILGenFunction::emitFuncToBlock(SILLocation loc,
blockInterfaceTy->getParameters().end(),
std::back_inserter(params));

auto genericSig = F.getLoweredFunctionType()->getGenericSignature();
auto extInfo =
SILFunctionType::ExtInfo()
.withRepresentation(SILFunctionType::Representation::CFunctionPointer);

// The block invoke function must be pseudogeneric. This should be OK for now
// since a bridgeable function's parameters and returns should all be
// trivially representable in ObjC so not need to exercise the type metadata.
Expand All @@ -349,17 +354,17 @@ ManagedValue SILGenFunction::emitFuncToBlock(SILLocation loc,
// that will require a redesign of the interface to support dependent-layout
// context. Currently we don't capture anything directly into a block but a
// Swift closure, but that's totally dumb.
if (genericSig)
extInfo = extInfo.withIsPseudogeneric();

auto invokeTy =
SILFunctionType::get(F.getLoweredFunctionType()->getGenericSignature(),
SILFunctionType::ExtInfo()
.withRepresentation(SILFunctionType::Representation::
CFunctionPointer)
.withIsPseudogeneric(),
ParameterConvention::Direct_Unowned,
params,
blockInterfaceTy->getAllResults(),
blockInterfaceTy->getOptionalErrorResult(),
getASTContext());
SILFunctionType::get(genericSig,
extInfo,
ParameterConvention::Direct_Unowned,
params,
blockInterfaceTy->getAllResults(),
blockInterfaceTy->getOptionalErrorResult(),
getASTContext());

// Create the invoke function. Borrow the mangling scheme from reabstraction
// thunks, which is what we are in spirit.
Expand Down
2 changes: 1 addition & 1 deletion lib/SILGen/SILGenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1973,7 +1973,7 @@ getOrCreateReabstractionThunk(GenericParamList *thunkContextParams,
// makes the actual thunk.
mangler.append("_TTR");
if (auto generics = thunkType->getGenericSignature()) {
mangler.append('G');
mangler.append(thunkType->isPseudogeneric() ? 'g' : 'G');
mangler.setModuleContext(M.getSwiftModule());
mangler.mangleGenericSignature(generics);
}
Expand Down
7 changes: 6 additions & 1 deletion lib/SILGen/SILGenPoly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2377,7 +2377,12 @@ CanSILFunctionType SILGenFunction::buildThunkType(

auto extInfo = expectedType->getExtInfo()
.withRepresentation(SILFunctionType::Representation::Thin);


// If our parent function was pseudogeneric, this thunk must also be
// pseudogeneric, since we have no way to pass generic parameters.
if (F.getLoweredFunctionType()->isPseudogeneric())
extInfo = extInfo.withIsPseudogeneric();

// Map the parameter and expected types out of context to get the interface
// type of the thunk.
SmallVector<SILParameterInfo, 4> interfaceParams;
Expand Down
22 changes: 22 additions & 0 deletions test/Reflection/capture_descriptors.sil
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,28 @@ sil_vtable GenericClass {}
// CHECK-NEXT: (reference_capture index=0))


// Pseudogeneric caller and pseudogeneric callee -- type parameters are
// erased at runtime.

sil @pseudogeneric_callee : $@convention(thin) @pseudogeneric <T : AnyObject, U : AnyObject> (@owned T, @owned U) -> () {
bb0(%t: $T, %u: $U):
%12 = tuple ()
return %12 : $()
}

sil @pseudogeneric_caller : $@convention(thin) @pseudogeneric <A : AnyObject, B : AnyObject, C : AnyObject> (@owned A, @owned B) -> @owned @pseudogeneric @callee_owned () -> () {
bb0(%a: $A, %b: $B):
%f = function_ref @pseudogeneric_callee : $@convention(thin) @pseudogeneric <T : AnyObject, U : AnyObject> (@owned T, @owned U) -> ()
%c = partial_apply %f<A, B>(%a, %b) : $@convention(thin) @pseudogeneric <A : AnyObject, B : AnyObject> (@owned A, @owned B) -> ()
return %c : $@pseudogeneric @callee_owned () -> ()
}

// CHECK: - Capture types:
// CHECK-NEXT: (protocol Swift.AnyObject)
// CHECK-NEXT: (protocol Swift.AnyObject)
// CHECK-NEXT: - Metadata sources:


// Capturing lowered function types

sil @function_callee : $@convention(thin) (@convention(thin) () -> (), @convention(c) () -> (), @convention(block) () -> (), @convention(thick) () -> (), @convention(method) () -> (), @convention(witness_method) () -> ()) -> () {
Expand Down
18 changes: 18 additions & 0 deletions test/SILGen/objc_blocks_bridging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,21 @@ func bridgeNoescapeBlock() {
// CHECK: function_ref @_TTRXFo___XFdCb___
noescapeNonnullBlockAlias { }
}

class ObjCClass : NSObject {}

extension ObjCClass {
func someDynamicMethod(closure: (() -> ()) -> ()) {}
}

struct GenericStruct<T> {
let closure: (() -> ()) -> ()

func doStuff(o: ObjCClass) {
o.someDynamicMethod(closure: closure)
}
}

// CHECK-LABEL: sil shared [transparent] [reabstraction_thunk] @_TTRgrXFo_oXFo_____XFdCb_dXFdCb_____ : $@convention(c) @pseudogeneric <T> (@inout_aliasable @block_storage @callee_owned (@owned @callee_owned () -> ()) -> (), @convention(block) () -> ()) -> ()
// CHECK-LABEL: sil shared [transparent] [reabstraction_thunk] @_TTRgrXFdCb___XFo___ : $@convention(thin) @pseudogeneric <T> (@owned @convention(block) () -> ()) -> ()

4 changes: 2 additions & 2 deletions test/SILGen/objc_bridging_any.swift
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ class SwiftIdLover : NSObject, Anyable {
// CHECK-NEXT: strong_release [[RESULT]]
// CHECK-NEXT: return [[BLOCK]]

// CHECK-LABEL: sil shared [transparent] [reabstraction_thunk] @_TTRXFo_iP___XFdCb_dPs9AnyObject___ : $@convention(c) @pseudogeneric (@inout_aliasable @block_storage @callee_owned (@in Any) -> (), AnyObject) -> ()
// CHECK-LABEL: sil shared [transparent] [reabstraction_thunk] @_TTRXFo_iP___XFdCb_dPs9AnyObject___ : $@convention(c) (@inout_aliasable @block_storage @callee_owned (@in Any) -> (), AnyObject) -> ()
// CHECK: bb0(%0 : $*@block_storage @callee_owned (@in Any) -> (), %1 : $AnyObject):
// CHECK-NEXT: [[BLOCK_STORAGE_ADDR:%.*]] = project_block_storage %0
// CHECK-NEXT: [[FUNCTION:%.*]] = load [[BLOCK_STORAGE_ADDR]]
Expand Down Expand Up @@ -493,7 +493,7 @@ class SwiftIdLover : NSObject, Anyable {
// CHECK-NEXT: strong_release [[FUNCTION]]
// CHECK-NEXT: return [[BLOCK]]

// CHECK-LABEL: sil shared [transparent] [reabstraction_thunk] @_TTRXFo__iP__XFdCb__aPs9AnyObject__ : $@convention(c) @pseudogeneric (@inout_aliasable @block_storage @callee_owned () -> @out Any) -> @autoreleased AnyObject
// CHECK-LABEL: sil shared [transparent] [reabstraction_thunk] @_TTRXFo__iP__XFdCb__aPs9AnyObject__ : $@convention(c) (@inout_aliasable @block_storage @callee_owned () -> @out Any) -> @autoreleased AnyObject
// CHECK: bb0(%0 : $*@block_storage @callee_owned () -> @out Any):
// CHECK-NEXT: [[BLOCK_STORAGE_ADDR:%.*]] = project_block_storage %0
// CHECK-NEXT: [[FUNCTION:%.*]] = load [[BLOCK_STORAGE_ADDR]]
Expand Down
2 changes: 1 addition & 1 deletion test/SILGen/objc_imported_generic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public func genericBlockBridging<T: Ansible>(x: GenericClass<T>) {
// CHECK-LABEL: sil @_TF21objc_imported_generic20genericBlockBridging
// CHECK: [[BLOCK_TO_FUNC:%.*]] = function_ref @_TTRGRxs9AnyObjectx21objc_imported_generic7AnsiblerXFdCb_dx_ax_XFo_ox_ox_
// CHECK: partial_apply [[BLOCK_TO_FUNC]]<T, {{.*}}>
// CHECK: [[FUNC_TO_BLOCK:%.*]] = function_ref @_TTRGRxs9AnyObjectx21objc_imported_generic7AnsiblerXFo_ox_ox_XFdCb_dx_ax_
// CHECK: [[FUNC_TO_BLOCK:%.*]] = function_ref @_TTRgRxs9AnyObjectx21objc_imported_generic7AnsiblerXFo_ox_ox_XFdCb_dx_ax_
// CHECK: init_block_storage_header {{.*}} invoke [[FUNC_TO_BLOCK]]<T,{{.*}}>

// CHECK-LABEL: sil @_TF21objc_imported_generic20arraysOfGenericParam
Expand Down
2 changes: 1 addition & 1 deletion test/SILGen/objc_thunks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func registerAnsible() {

// FIXME: would be nice if we didn't need to re-abstract as much here.

// CHECK-LABEL: sil shared [transparent] [reabstraction_thunk] @_TTRXFo_oGSqFT_T____XFdCb_dGSqbT_T____ : $@convention(c) @pseudogeneric (@inout_aliasable @block_storage @callee_owned (@owned Optional<() -> ()>) -> (), Optional<@convention(block) () -> ()>) -> ()
// CHECK-LABEL: sil shared [transparent] [reabstraction_thunk] @_TTRXFo_oGSqFT_T____XFdCb_dGSqbT_T____ : $@convention(c) (@inout_aliasable @block_storage @callee_owned (@owned Optional<() -> ()>) -> (), Optional<@convention(block) () -> ()>) -> ()
// CHECK: [[HEAP_BLOCK_IUO:%.*]] = copy_block %1
// CHECK: select_enum [[HEAP_BLOCK_IUO]]
// CHECK: bb1:
Expand Down