Skip to content

partial_apply fixup #1767

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 2 commits into from
Mar 22, 2016
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
20 changes: 18 additions & 2 deletions lib/SIL/SILBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,29 @@ SILType SILBuilder::getPartialApplyResultType(SILType origTy, unsigned argCount,
auto extInfo = SILFunctionType::ExtInfo(
SILFunctionType::Representation::Thick,
/*noreturn*/ FTI->isNoReturn());


// If the original method has an @unowned_inner_pointer return, the partial
// application thunk will lifetime-extend 'self' for us, converting the
// return value to @unowned.
//
// If the original method has an @autoreleased return, the partial application
// thunk will retain it for us, converting the return value to @owned.
SmallVector<SILResultInfo, 4> results;
results.append(FTI->getAllResults().begin(), FTI->getAllResults().end());
for (auto &result : results) {
if (result.getConvention() == ResultConvention::UnownedInnerPointer)
result = SILResultInfo(result.getType(), ResultConvention::Unowned);
else if (result.getConvention() == ResultConvention::Autoreleased)
result = SILResultInfo(result.getType(), ResultConvention::Owned);
}

auto appliedFnType = SILFunctionType::get(nullptr, extInfo,
ParameterConvention::Direct_Owned,
newParams,
FTI->getAllResults(),
results,
FTI->getOptionalErrorResult(),
M.getASTContext());

return SILType::getPrimitiveObjectType(appliedFnType);
}

Expand Down
36 changes: 6 additions & 30 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4577,34 +4577,10 @@ static SILValue emitDynamicPartialApply(SILGenFunction &gen,
SILValue method,
SILValue self,
CanFunctionType methodTy) {
// Pop the self type off of the function type.
// Just to be weird, partially applying an objc method produces a native
// function (?!)
auto fnTy = method->getType().castTo<SILFunctionType>();

// If the original method has an @unowned_inner_pointer return, the partial
// application thunk will lifetime-extend 'self' for us, converting the
// return value to @unowned.
//
// If the original method has an @autoreleased return, the partial application
// thunk will retain it for us, converting the return value to @owned.
SmallVector<SILResultInfo, 4> results;
results.append(fnTy->getAllResults().begin(), fnTy->getAllResults().end());
for (auto &result : results) {
if (result.getConvention() == ResultConvention::UnownedInnerPointer)
result = SILResultInfo(result.getType(), ResultConvention::Unowned);
else if (result.getConvention() == ResultConvention::Autoreleased)
result = SILResultInfo(result.getType(), ResultConvention::Owned);
}

auto partialApplyTy = SILFunctionType::get(fnTy->getGenericSignature(),
fnTy->getExtInfo()
.withRepresentation(SILFunctionType::Representation::Thick),
ParameterConvention::Direct_Owned,
fnTy->getParameters()
.slice(0, fnTy->getParameters().size() - 1),
results, fnTy->getOptionalErrorResult(),
gen.getASTContext());
auto partialApplyTy = SILBuilder::getPartialApplyResultType(method->getType(),
/*argCount*/1,
gen.SGM.M,
/*subs*/{});

// Retain 'self' because the partial apply will take ownership.
// We can't simply forward 'self' because the partial apply is conditional.
Expand All @@ -4618,11 +4594,11 @@ static SILValue emitDynamicPartialApply(SILGenFunction &gen,
#endif

SILValue result = gen.B.createPartialApply(loc, method, method->getType(), {},
self, SILType::getPrimitiveObjectType(partialApplyTy));
self, partialApplyTy);
// If necessary, thunk to the native ownership conventions and bridged types.
auto nativeTy = gen.getLoweredLoadableType(methodTy).castTo<SILFunctionType>();

if (nativeTy != partialApplyTy) {
if (nativeTy != partialApplyTy.getSwiftRValueType()) {
result = gen.emitBlockToFunc(loc, ManagedValue::forUnmanaged(result),
nativeTy).forward(gen);
}
Expand Down
2 changes: 0 additions & 2 deletions test/ClangModules/Inputs/SerializationHelper.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Foundation

@_exported import ProtoWithInitializer
@_exported import TypeAndValue

Expand Down
7 changes: 7 additions & 0 deletions test/ClangModules/Inputs/serialization-sil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@protocol Test
@optional
- (nonnull id)normalObject;
- (nonnull void *)innerPointer __attribute__((objc_returns_inner_pointer));
@property (nonnull) id normalObjectProp;
@property (nonnull) void *innerPointerProp __attribute__((objc_returns_inner_pointer));
@end
6 changes: 3 additions & 3 deletions test/ClangModules/serialization-search-paths.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// RUN: rm -rf %t && mkdir -p %t
// RUN: %target-swift-frontend -emit-module-path %t/SerializationHelper.swiftmodule -I %S/Inputs/custom-modules -F %S/Inputs/frameworks %S/Inputs/SerializationHelper.swift
// RUN: %target-swift-frontend -parse -I %t %s -verify
// RUN: %target-swift-frontend -emit-module-path %t/SerializationHelper.swiftmodule -I %S/Inputs/custom-modules -F %S/Inputs/frameworks -sdk "" -disable-objc-attr-requires-foundation-module %S/Inputs/SerializationHelper.swift
// RUN: %target-swift-frontend -parse -I %t %s -sdk "" -verify

// XFAIL: linux
// REQUIRES: objc_interop

import SerializationHelper
import Module
Expand Down
45 changes: 45 additions & 0 deletions test/ClangModules/serialization-sil.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// RUN: rm -rf %t && mkdir -p %t
// RUN: %target-swift-frontend -emit-module-path %t/Test.swiftmodule -emit-sil -o /dev/null -module-name Test %s -sdk "" -import-objc-header %S/Inputs/serialization-sil.h
// RUN: %target-sil-extract %t/Test.swiftmodule -func=_TF4Test16testPartialApplyFPSo4Test_T_ -o - | FileCheck %s

// REQUIRES: objc_interop

// @_transparent to force serialization.
@_transparent
public func testPartialApply(obj: Test) {
// CHECK-LABEL: @_TF4Test16testPartialApplyFPSo4Test_T_ : $@convention(thin) (@owned Test) -> () {
if let curried1 = obj.normalObject {
// CHECK: dynamic_method_br [[CURRIED1_OBJ:%.+]] : $@opened([[CURRIED1_EXISTENTIAL:.+]]) Test, #Test.normalObject!1.foreign, [[CURRIED1_TRUE:[^,]+]], [[CURRIED1_FALSE:[^,]+]]
// CHECK: [[CURRIED1_TRUE]]([[CURRIED1_METHOD:%.+]] : $@convention(objc_method) (@opened([[CURRIED1_EXISTENTIAL]]) Test) -> @autoreleased AnyObject):
// CHECK: [[CURRIED1_PARTIAL:%.+]] = partial_apply [[CURRIED1_METHOD]]([[CURRIED1_OBJ]]) : $@convention(objc_method) (@opened([[CURRIED1_EXISTENTIAL]]) Test) -> @autoreleased AnyObject
// CHECK: [[CURRIED1_THUNK:%.+]] = function_ref @_TTRXFo__oPs9AnyObject__XFo_iT__iPS___ : $@convention(thin) (@in (), @owned @callee_owned () -> @owned AnyObject) -> @out AnyObject
// CHECK: = partial_apply [[CURRIED1_THUNK]]([[CURRIED1_PARTIAL]]) : $@convention(thin) (@in (), @owned @callee_owned () -> @owned AnyObject) -> @out AnyObject
// CHECK: [[CURRIED1_FALSE]]:
curried1()
}
if let curried2 = obj.innerPointer {
// CHECK: dynamic_method_br [[CURRIED2_OBJ:%.+]] : $@opened([[CURRIED2_EXISTENTIAL:.+]]) Test, #Test.innerPointer!1.foreign, [[CURRIED2_TRUE:[^,]+]], [[CURRIED2_FALSE:[^,]+]]
// CHECK: [[CURRIED2_TRUE]]([[CURRIED2_METHOD:%.+]] : $@convention(objc_method) (@opened([[CURRIED2_EXISTENTIAL]]) Test) -> @unowned_inner_pointer UnsafeMutablePointer<()>):
// CHECK: [[CURRIED2_PARTIAL:%.+]] = partial_apply [[CURRIED2_METHOD]]([[CURRIED2_OBJ]]) : $@convention(objc_method) (@opened([[CURRIED2_EXISTENTIAL]]) Test) -> @unowned_inner_pointer UnsafeMutablePointer<()>
// CHECK: [[CURRIED2_THUNK:%.+]] = function_ref @_TTRXFo__dGSpT___XFo_iT__iGSpT___ : $@convention(thin) (@in (), @owned @callee_owned () -> UnsafeMutablePointer<()>) -> @out UnsafeMutablePointer<()>
// CHECK: = partial_apply [[CURRIED2_THUNK]]([[CURRIED2_PARTIAL]]) : $@convention(thin) (@in (), @owned @callee_owned () -> UnsafeMutablePointer<()>) -> @out UnsafeMutablePointer<()>
// CHECK: [[CURRIED2_FALSE]]:
curried2()
}
if let prop1 = obj.normalObjectProp {
// CHECK: dynamic_method_br [[PROP1_OBJ:%.+]] : $@opened([[PROP1_EXISTENTIAL:.+]]) Test, #Test.normalObjectProp!getter.1.foreign, [[PROP1_TRUE:[^,]+]], [[PROP1_FALSE:[^,]+]]
// CHECK: [[PROP1_TRUE]]([[PROP1_METHOD:%.+]] : $@convention(objc_method) (@opened([[PROP1_EXISTENTIAL]]) Test) -> @autoreleased AnyObject):
// CHECK: [[PROP1_PARTIAL:%.+]] = partial_apply [[PROP1_METHOD]]([[PROP1_OBJ]]) : $@convention(objc_method) (@opened([[PROP1_EXISTENTIAL]]) Test) -> @autoreleased AnyObject
// CHECK: = apply [[PROP1_PARTIAL]]() : $@callee_owned () -> @owned AnyObject
// CHECK: [[PROP1_FALSE]]:
_ = prop1
}
if let prop2 = obj.innerPointerProp {
// CHECK: dynamic_method_br [[PROP2_OBJ:%.+]] : $@opened([[PROP2_EXISTENTIAL:.+]]) Test, #Test.innerPointerProp!getter.1.foreign, [[PROP2_TRUE:[^,]+]], [[PROP2_FALSE:[^,]+]]
// CHECK: [[PROP2_TRUE]]([[PROP2_METHOD:%.+]] : $@convention(objc_method) (@opened([[PROP2_EXISTENTIAL]]) Test) -> @unowned_inner_pointer UnsafeMutablePointer<()>):
// CHECK: [[PROP2_PARTIAL:%.+]] = partial_apply [[PROP2_METHOD]]([[PROP2_OBJ]]) : $@convention(objc_method) (@opened([[PROP2_EXISTENTIAL]]) Test) -> @unowned_inner_pointer UnsafeMutablePointer<()>
// CHECK: = apply [[PROP2_PARTIAL]]() : $@callee_owned () -> UnsafeMutablePointer<()>
// CHECK: [[PROP2_FALSE]]:
_ = prop2
}
} // CHECK: {{^}$}}
6 changes: 3 additions & 3 deletions test/ClangModules/serialization.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: %target-swift-frontend -emit-module-path %t/SerializationHelper.swiftmodule -I %S/Inputs/custom-modules %S/Inputs/SerializationHelper.swift
// RUN: %target-swift-frontend -parse -I %t -I %S/Inputs/custom-modules %s -verify
// RUN: %target-swift-frontend -emit-module-path %t/SerializationHelper.swiftmodule -I %S/Inputs/custom-modules %S/Inputs/SerializationHelper.swift -sdk "" -disable-objc-attr-requires-foundation-module
// RUN: %target-swift-frontend -parse -sdk "" -I %t -I %S/Inputs/custom-modules %s -verify

// XFAIL: linux
// REQUIRES: objc_interop

import SerializationHelper

Expand Down