Skip to content

IRGen: partial_applies of ObjC methods must emit loads from loadable … #5135

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: 31 additions & 1 deletion lib/IRGen/GenObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,36 @@ static llvm::Function *emitObjCPartialApplicationForwarder(IRGenModule &IGM,
}
}

// Translate direct parameters passed indirectly.
Explosion translatedParams;

// We already handled self.
assert(origMethodType->hasSelfParam());
auto origParamInfos = origMethodType->getParameters();
origParamInfos = origParamInfos.drop_back();

for (auto info : origParamInfos) {
// Addresses consist of a single pointer argument.
if (isIndirectParameter(info.getConvention())) {
translatedParams.add(params.claimNext());
continue;
}
// Otherwise, we have a loadable type that can either be passed directly or
// indirectly.
assert(info.getSILType().isObject());
auto &ti = cast<LoadableTypeInfo>(IGM.getTypeInfo(info.getSILType()));
auto schema = ti.getSchema();

// Load the indirectly passed parameter.
if (schema.requiresIndirectParameter(IGM)) {
Address paramAddr = ti.getAddressForPointer(params.claimNext());
ti.loadAsTake(subIGF, paramAddr, translatedParams);
continue;
}
// Pass along the value.
ti.reexplode(subIGF, params, translatedParams);
}

// Prepare the call to the underlying method.

CallEmission emission
Expand All @@ -899,7 +929,7 @@ static llvm::Function *emitObjCPartialApplicationForwarder(IRGenModule &IGM,

addObjCMethodCallImplicitArguments(subIGF, args, method.getMethod(), self,
method.getSearchType());
args.add(params.claimAll());
args.add(translatedParams.claimAll());
emission.setArgs(args);

// Cleanup that always has to occur after the function call.
Expand Down
27 changes: 27 additions & 0 deletions test/IRGen/partial_apply.sil
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import gizmo

@objc class ObjCClass : BaseClassForMethodFamilies {
func method(x: Int) {}
func method2(r: NSRect) {}

override func fakeInitFamily() -> ObjCClass { return self }
}
Expand All @@ -36,6 +37,11 @@ bb0(%0 : $Int, %1 : $ObjCClass):
%v = tuple()
return %v : $()
}
sil @_TToFC13partial_apply9ObjCClass7method2fT1rVSC6NSRect_T_ : $@convention(objc_method) (NSRect, ObjCClass) -> () {
bb0(%0 : $NSRect, %1 : $ObjCClass):
%v = tuple()
return %v : $()
}

sil @_TToFC13partial_apply9ObjCClass14fakeInitFamilyfT_S0_ : $@convention(objc_method) (@owned ObjCClass) -> @owned ObjCClass {
bb0(%0 : $ObjCClass):
Expand Down Expand Up @@ -107,6 +113,27 @@ entry(%c : $ObjCClass):
return %p : $@callee_owned Int -> ()
}

// CHECK-LABEL: define{{.*}} { i8*, %swift.refcounted* } @objc_partial_apply_indirect_sil_argument(%C13partial_apply9ObjCClass*) {{.*}}
// CHECK: [[CTX:%.*]] = call noalias %swift.refcounted* @rt_swift_allocObject
// CHECK: [[CTX_ADDR:%.*]] = bitcast %swift.refcounted* [[CTX]] to [[CTXTYPE:<{ %swift.refcounted, %C13partial_apply9ObjCClass\* }>]]*
// CHECK: [[SELF_ADDR:%.*]] = getelementptr inbounds [[CTXTYPE]], [[CTXTYPE]]* [[CTX_ADDR]], i32 0, i32 1
// CHECK: store %C13partial_apply9ObjCClass* %0, %C13partial_apply9ObjCClass** [[SELF_ADDR]]
// CHECK: [[CLOSURE:%.*]] = insertvalue { i8*, %swift.refcounted* } { i8* bitcast (void (%VSC6NSRect*, %swift.refcounted*)* [[OBJC_PARTIAL_APPLY_STUB2:@_TPA[A-Za-z0-9_.]*]] to i8*), %swift.refcounted* undef }, %swift.refcounted* [[CTX]], 1
// CHECK: ret { i8*, %swift.refcounted* } [[CLOSURE]]
// CHECK:}
// CHECK: define internal void [[OBJC_PARTIAL_APPLY_STUB2]](%VSC6NSRect* noalias nocapture dereferenceable(32), %swift.refcounted*
// CHECK: [[TMP:%.*]] = alloca %VSC6NSRect
// CHECK: [[ORIGIN:%.*]] = getelementptr inbounds %VSC6NSRect, %VSC6NSRect* %0
// CHECK: [[ORIGINX:%.*]] = getelementptr inbounds %VSC7NSPoint, %VSC7NSPoint* [[ORIGIN]]
// CHECK: [[ORIGINXVAL:%*]] = getelementptr inbounds %Sd, %Sd* [[ORIGINX]]
// CHECK: [[VAL:%.*]] = load double, double* [[ORIGINXVAL]]
sil @objc_partial_apply_indirect_sil_argument : $@convention(thin) ObjCClass -> @callee_owned NSRect -> () {
entry(%c : $ObjCClass):
%m = class_method [volatile] %c : $ObjCClass, #ObjCClass.method2!1.foreign : (ObjCClass) -> (NSRect) -> () , $@convention(objc_method) (NSRect, ObjCClass) -> ()
%p = partial_apply %m(%c) : $@convention(objc_method) (NSRect, ObjCClass) -> ()
return %p : $@callee_owned NSRect -> ()
}

// CHECK-LABEL: define{{( protected)?}} { i8*, %swift.refcounted* } @objc_partial_apply_consumes_self(%C13partial_apply9ObjCClass*) {{.*}} {
// CHECK: bitcast %swift.refcounted* {{%.*}} to [[DATA_TYPE:<{ %swift.refcounted, %C13partial_apply9ObjCClass\* }>]]*
// CHECK: insertvalue {{.*}} [[OBJC_CONSUMES_SELF_PARTIAL_APPLY_STUB:@_TPA[A-Za-z0-9_.]*]]
Expand Down