Skip to content

[ClosureSpecializer] Don't release trivial noescape try_apply argument twice. #66263

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 1 commit into from
Jun 1, 2023
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
3 changes: 2 additions & 1 deletion lib/SILOptimizer/IPO/ClosureSpecializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,8 @@ static void rewriteApplyInst(const CallSiteDescriptor &CSDesc,
// If we passed in the original closure as @owned, then insert a release
// right after NewAI. This is to balance the +1 from being an @owned
// argument to AI.
if (!CSDesc.isClosureConsumed() || !CSDesc.closureHasRefSemanticContext()) {
if (!CSDesc.isClosureConsumed() || CSDesc.isTrivialNoEscapeParameter() ||
!CSDesc.closureHasRefSemanticContext()) {
break;
}

Expand Down
65 changes: 65 additions & 0 deletions test/SILOptimizer/closure_specialize.sil
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,71 @@ bb0(%0 : $Int):
return %empty : $()
}

sil @testThrowingClosureConvertHelper : $@convention(thin) (Int) -> (Int, @error any Error)
sil [reabstraction_thunk] @reabstractionThunkThrowing : $@convention(thin) (@noescape @callee_guaranteed () -> (Int, @error any Error)) -> (@out Int, @error any Error)

sil @testClosureThunkNoEscapeThrowing : $@convention(thin) (@owned @noescape @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error any Error) for <Int>) -> (@out (), @error any Error) {
entry(%empty : $*(), %closure : $@noescape @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error any Error) for <Int>):
%out = alloc_stack $Int
try_apply %closure(%out) : $@noescape @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error any Error) for <Int>, normal bb1, error bb2

bb1(%ret : $()):
dealloc_stack %out : $*Int
store %ret to %empty : $*()
%retval = tuple ()
return %retval : $()

bb2(%error : $any Error):
dealloc_stack %out : $*Int
throw %error : $any Error
}

// CHECK-LABEL: sil @reabstractionThrowing : $@convention(thin) (Int) -> ((), @error any Error) {
// CHECK: [[HELPER:%[^,]+]] = function_ref @testThrowingClosureConvertHelper
// CHECK: [[SPECIALIZATION:%[^,]+]] = function_ref @$s32testClosureThunkNoEscapeThrowing0afB13ConvertHelperSiTf1nc_n
// CHECK: [[CLOSURE:%[^,]+]] = partial_apply [callee_guaranteed] [[HELPER]]
// CHECK: [[NOESCAPE_CLOSURE:%[^,]+]] = convert_escape_to_noescape [[CLOSURE]]
// CHECK: try_apply [[SPECIALIZATION]]{{.*}}normal [[NORMAL_BLOCK:bb[0-9]+]], error [[ERROR_BLOCK:bb[0-9]+]]
// CHECK: [[NORMAL_BLOCK]]
// CHECK: release_value [[CLOSURE]]
// CHECK-NOT: release_value [[CLOSURE]]
// CHECK: strong_release [[NOESCAPE_CLOSURE]]
// CHECK: [[ERROR_BLOCK]]
// CHECK: release_value [[CLOSURE]]
// CHECK-NOT: release_value [[CLOSURE]]
// CHECK: strong_release [[NOESCAPE_CLOSURE]]
// CHECK-LABEL: } // end sil function 'reabstractionThrowing'
sil @reabstractionThrowing : $(Int) -> ((), @error any Error) {
bb0(%value : $Int):
%testThrowingClosureConvertHelper = function_ref @testThrowingClosureConvertHelper : $@convention(thin) (Int) -> (Int, @error any Error)
%closure = partial_apply [callee_guaranteed] %testThrowingClosureConvertHelper(%value) : $@convention(thin) (Int) -> (Int, @error any Error)
%noescapeClosure = convert_escape_to_noescape %closure : $@callee_guaranteed () -> (Int, @error any Error) to $@noescape @callee_guaranteed () -> (Int, @error any Error)
%thunk = function_ref @reabstractionThunkThrowing : $@convention(thin) (@noescape @callee_guaranteed () -> (Int, @error any Error)) -> (@out Int, @error any Error)
%appliedThunk = partial_apply [callee_guaranteed] [on_stack] %thunk(%noescapeClosure) : $@convention(thin) (@noescape @callee_guaranteed () -> (Int, @error any Error)) -> (@out Int, @error any Error)

%dependency = mark_dependence %appliedThunk : $@noescape @callee_guaranteed () -> (@out Int, @error any Error) on %noescapeClosure : $@noescape @callee_guaranteed () -> (Int, @error any Error)
%generified = convert_function %dependency : $@noescape @callee_guaranteed () -> (@out Int, @error any Error) to $@noescape @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error any Error) for <Int>
%test = function_ref @testClosureThunkNoEscapeThrowing : $@convention(thin) (@owned @noescape @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error any Error) for <Int>) -> (@out (), @error any Error)
strong_retain %generified : $@noescape @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error any Error) for <Int>
%out = alloc_stack $()
try_apply %test(%out, %generified) : $@convention(thin) (@owned @noescape @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error any Error) for <Int>) -> (@out (), @error any Error), normal bb1, error bb2

bb1(%ret : $()):
dealloc_stack %out : $*()
release_value %closure : $@callee_guaranteed () -> (Int, @error any Error)
strong_release %noescapeClosure : $@noescape @callee_guaranteed () -> (Int, @error any Error)
dealloc_stack %appliedThunk : $@noescape @callee_guaranteed () -> (@out Int, @error any Error)
%empty = tuple ()
return %empty : $()

bb2(%error : $any Error):
dealloc_stack %out : $*()
release_value %closure : $@callee_guaranteed () -> (Int, @error any Error)
strong_release %noescapeClosure : $@noescape @callee_guaranteed () -> (Int, @error any Error)
dealloc_stack %appliedThunk : $@noescape @callee_guaranteed () -> (@out Int, @error any Error)
throw %error : $any Error
}

// Currently not supported cases.

sil @testClosureThunk4 : $@convention(thin) (@owned @callee_guaranteed () -> @out Int) -> @out Int {
Expand Down