Skip to content

[3.0] SILGen/IRGen: Lower protocol witness thunks for ObjC lightweight generics as pseudogeneric. #5497

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
1 change: 1 addition & 0 deletions include/swift/SIL/SILType.h
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ NON_SIL_TYPE(LValue)
CanSILFunctionType getNativeSILFunctionType(SILModule &M,
Lowering::AbstractionPattern orig,
CanAnyFunctionType substInterface,
Optional<SILDeclRef> constant = None,
SILDeclRef::Kind kind = SILDeclRef::Kind::Func);

inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, SILType T) {
Expand Down
9 changes: 7 additions & 2 deletions lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,18 @@ PolymorphicConvention::PolymorphicConvention(IRGenModule &IGM,
: IGM(IGM), M(*IGM.getSwiftModule()), FnType(fnType) {
initGenerics();

auto rep = fnType->getRepresentation();

if (fnType->isPseudogeneric()) {
// Protocol witnesses still get Self metadata no matter what. The type
// parameters of Self are pseudogeneric, though.
if (rep == SILFunctionTypeRepresentation::WitnessMethod)
considerWitnessSelf(fnType);

addPseudogenericFulfillments();
return;
}

auto rep = fnType->getRepresentation();

if (rep == SILFunctionTypeRepresentation::WitnessMethod) {
// Protocol witnesses always derive all polymorphic parameter
// information from the Self argument. We also *cannot* consider other
Expand Down
4 changes: 3 additions & 1 deletion lib/SIL/SILFunctionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,7 @@ static CanSILFunctionType getNativeSILFunctionType(SILModule &M,
CanSILFunctionType swift::getNativeSILFunctionType(SILModule &M,
AbstractionPattern origType,
CanAnyFunctionType substInterfaceType,
Optional<SILDeclRef> constant,
SILDeclRef::Kind kind) {
AnyFunctionType::ExtInfo extInfo;

Expand All @@ -1033,7 +1034,7 @@ CanSILFunctionType swift::getNativeSILFunctionType(SILModule &M,
extInfo = substInterfaceType->getExtInfo();
}
return ::getNativeSILFunctionType(M, origType, substInterfaceType,
extInfo, None, kind);
extInfo, constant, kind);
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -2070,6 +2071,7 @@ SILConstantInfo TypeConverter::getConstantOverrideInfo(SILDeclRef derived,
// Build the SILFunctionType for the vtable thunk.
CanSILFunctionType fnTy = getNativeSILFunctionType(M, basePattern,
overrideLoweredInterfaceTy,
derived,
derived.kind);

{
Expand Down
3 changes: 2 additions & 1 deletion lib/SILGen/SILGenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1737,7 +1737,8 @@ SILGenModule::emitProtocolWitness(ProtocolConformance *conformance,
// Lower the witness type with the requirement's abstraction level.
auto witnessSILFnType = getNativeSILFunctionType(M,
AbstractionPattern(reqtOrigTy),
reqtSubstTy);
reqtSubstTy,
witness);

// Mangle the name of the witness thunk.
std::string nameBuffer;
Expand Down
7 changes: 7 additions & 0 deletions test/IRGen/Inputs/objc_generic_protocol_conformance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@import Foundation;

@interface Foo <T> : NSObject

- (void)foo;

@end
13 changes: 13 additions & 0 deletions test/IRGen/objc_generic_protocol_conformance.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-silgen %s -import-objc-header %S/Inputs/objc_generic_protocol_conformance.h | %FileCheck --check-prefix=SIL %s
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-ir %s -import-objc-header %S/Inputs/objc_generic_protocol_conformance.h | %FileCheck --check-prefix=IR %s

// REQUIRES: objc_interop

protocol P {
func foo()
}

extension Foo: P {}

// SIL-LABEL: sil hidden [transparent] [thunk] @_TTWuRxs9AnyObjectrGCSo3Foo{{.*}} @pseudogeneric
// IR-LABEL: define hidden void @_TTWuRxs9AnyObjectrGCSo3Foo{{.*}}(%CSo3Foo** noalias nocapture dereferenceable({{4|8}}), %swift.type*{{( %Self)?}}, i8**{{( %SelfWitnessTable)?}})