Skip to content

[LifetimeCompletion] Handle endAsyncLetLifetime. #72598

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
9 changes: 9 additions & 0 deletions include/swift/SIL/OwnershipUseVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@ bool OwnershipUseVisitor<Impl>::visitInnerBorrowScopeEnd(Operand *borrowEnd) {
// [nonescaping] def.
return handleUsePoint(borrowEnd, UseLifetimeConstraint::NonLifetimeEnding);
}
case OperandOwnership::InstantaneousUse: {
auto builtinUser = dyn_cast<BuiltinInst>(borrowEnd->getUser());
if (builtinUser && builtinUser->getBuiltinKind() ==
BuiltinValueKind::EndAsyncLetLifetime) {
return handleUsePoint(borrowEnd,
UseLifetimeConstraint::NonLifetimeEnding);
}
LLVM_FALLTHROUGH;
}
default:
llvm_unreachable("expected borrow scope end");
}
Expand Down
45 changes: 44 additions & 1 deletion test/SILOptimizer/ossa_lifetime_completion.sil
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// RUN: %target-sil-opt -enable-ossa-complete-lifetimes -test-runner -sil-disable-input-verify %s -o /dev/null 2>&1 | %FileCheck %s
// RUN: %target-sil-opt \
// RUN: -test-runner \
// RUN: -enable-ossa-complete-lifetimes \
// RUN: %s \
// RUN: -sil-disable-input-verify \
// RUN: -o /dev/null \
// RUN: 2>&1 | %FileCheck %s

sil_stage raw

Expand All @@ -11,6 +17,16 @@ public enum FakeOptional<T> {
case some(T)
}

sil @swift_asyncLet_finish : $@convention(thin) @async (Builtin.RawPointer, Builtin.RawPointer) -> ()
sil @swift_asyncLet_get : $@convention(thin) @async (Builtin.RawPointer, Builtin.RawPointer) -> ()

protocol Error {}

enum Optional<Wrapped> {
case none
case some(Wrapped)
}

// CHECK-LABEL: begin running test 1 of 1 on eagerConsumneOwnedArg: ossa-lifetime-completion with: @argument
// CHECK-LABEL: OSSA lifetime completion: %0 = argument of bb0 : $C
// CHECK: sil [ossa] @eagerConsumneOwnedArg : $@convention(thin) (@owned C) -> () {
Expand Down Expand Up @@ -455,3 +471,30 @@ right:
end_borrow %c : $C
unreachable
}

// Nothing to check here--the output is the same as the input. Verify that
// completing the lifetime of a value used by a startAsyncLetWithLocalBuffer
// doesn't crash when visiting an endAsyncLetLifetime.
sil [ossa] @async_let : $@convention(thin) @async () -> () {
bb0:
%result_addr = alloc_stack $()
%result_ptr = address_to_pointer %result_addr : $*() to $Builtin.RawPointer
%task_options = enum $Optional<Builtin.RawPointer>, #Optional.none!enumelt
%callee_pa = partial_apply [callee_guaranteed] undef() : $@convention(thin) @async @substituted <τ_0_0> () -> (@out τ_0_0, @error any Error) for <()>
%callee_noescape = convert_escape_to_noescape [not_guaranteed] %callee_pa : $@async @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error any Error) for <()>
to $@noescape @async @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error any Error) for <()>
specify_test "ossa-lifetime-completion %callee_noescape"
%async_let = builtin "startAsyncLetWithLocalBuffer"<()>(
%task_options : $Optional<Builtin.RawPointer>,
%callee_noescape : $@noescape @async @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error any Error) for <()>,
%result_ptr : $Builtin.RawPointer
) : $Builtin.RawPointer
%get = function_ref @swift_asyncLet_get : $@convention(thin) @async (Builtin.RawPointer, Builtin.RawPointer) -> ()
apply %get(%async_let, %result_ptr) : $@convention(thin) @async (Builtin.RawPointer, Builtin.RawPointer) -> ()
builtin "endAsyncLetLifetime"(%async_let : $Builtin.RawPointer) : $()
destroy_value %callee_noescape : $@noescape @async @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error any Error) for <()>
destroy_value %callee_pa : $@async @callee_guaranteed @substituted <τ_0_0> () -> (@out τ_0_0, @error any Error) for <()>
%retval = load [trivial] %result_addr : $*()
dealloc_stack %result_addr : $*()
return %retval : $()
}