Skip to content

Commit 9ee5d6b

Browse files
authored
Merge pull request #70176 from atrick/fix-rewrite-pa-undef
Fix tryRewriteToPartialApplyStack to handle undef
2 parents 28e6369 + 224c379 commit 9ee5d6b

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/SILOptimizer/Mandatory/ClosureLifetimeFixup.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,10 +896,11 @@ static SILValue tryRewriteToPartialApplyStack(
896896
SILBuilderWithScope builder(std::next(destroy->getIterator()));
897897
// This getCapturedArg hack attempts to perfectly compensate for all the
898898
// other hacks involved in gathering new arguments above.
899+
// argValue may be 'undef'
899900
auto getArgToDestroy = [&](SILValue argValue) -> SILValue {
900901
// A MoveOnlyWrapperToCopyableValueInst may produce a trivial value. Be
901902
// careful not to emit an extra destroy of the original.
902-
if (argValue->getType().isTrivial(argValue->getFunction()))
903+
if (argValue->getType().isTrivial(destroy->getFunction()))
903904
return SILValue();
904905

905906
// We may have inserted a new begin_borrow->moveonlywrapper_to_copyvalue

test/SILOptimizer/closure-lifetime-fixup.sil

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,36 @@ bb0(%0 : @noImplicitCopy @_eagerMove $Int):
348348
dealloc_stack %1 : $*@moveOnly Int
349349
return %14 : $Int
350350
}
351+
352+
struct _UnsafeContinuation<T, R> {}
353+
354+
sil @testCapturedUndefClosure : $@convention(thin) @substituted <τ_0_0> (_UnsafeContinuation<τ_0_0, any Error>, Int) -> () for <Int>
355+
sil @testCapturedUndefTakeClosure : $@convention(thin) <τ_0_0> (@guaranteed @noescape @callee_guaranteed @substituted <τ_0_0> (_UnsafeContinuation<τ_0_0, any Error>) -> () for <τ_0_0>) -> (@out τ_0_0, @error any Error)
356+
357+
// insertDestroyOfCapturedArguments should not crash on 'undef'
358+
//
359+
// CHECK-LABEL: sil hidden [ossa] @testCapturedUndef : $@convention(thin) (Int) -> (Int, @error any Error) {
360+
// CHECK: partial_apply [callee_guaranteed] [on_stack] %{{.*}}(undef)
361+
// CHECK-LABEL: } // end sil function 'testCapturedUndef'
362+
sil hidden [ossa] @testCapturedUndef : $@convention(thin) (Int) -> (Int, @error any Error) {
363+
bb0(%0 : $Int):
364+
%5 = alloc_stack $Int
365+
%6 = function_ref @testCapturedUndefClosure : $@convention(thin) @substituted <τ_0_0> (_UnsafeContinuation<τ_0_0, any Error>, Int) -> () for <Int>
366+
%7 = partial_apply [callee_guaranteed] %6(undef) : $@convention(thin) @substituted <τ_0_0> (_UnsafeContinuation<τ_0_0, any Error>, Int) -> () for <Int>
367+
%8 = convert_escape_to_noescape [not_guaranteed] %7 : $@callee_guaranteed @substituted <τ_0_0> (_UnsafeContinuation<τ_0_0, any Error>) -> () for <Int> to $@noescape @callee_guaranteed @substituted <τ_0_0> (_UnsafeContinuation<τ_0_0, any Error>) -> () for <Int>
368+
destroy_value %7 : $@callee_guaranteed @substituted <τ_0_0> (_UnsafeContinuation<τ_0_0, any Error>) -> () for <Int>
369+
370+
%10 = function_ref @testCapturedUndefTakeClosure : $@convention(thin) <τ_0_0> (@guaranteed @noescape @callee_guaranteed @substituted <τ_0_0> (_UnsafeContinuation<τ_0_0, any Error>) -> () for <τ_0_0>) -> (@out τ_0_0, @error any Error)
371+
try_apply %10<Int>(%5, %8) : $@convention(thin) <τ_0_0> (@guaranteed @noescape @callee_guaranteed @substituted <τ_0_0> (_UnsafeContinuation<τ_0_0, any Error>) -> () for <τ_0_0>) -> (@out τ_0_0, @error any Error), normal bb1, error bb2
372+
373+
bb1(%12 : $()):
374+
destroy_value %8 : $@noescape @callee_guaranteed @substituted <τ_0_0> (_UnsafeContinuation<τ_0_0, any Error>) -> () for <Int>
375+
%15 = load [trivial] %5 : $*Int
376+
dealloc_stack %5 : $*Int
377+
return %15 : $Int
378+
379+
bb2(%19 : @owned $any Error):
380+
destroy_value %8 : $@noescape @callee_guaranteed @substituted <τ_0_0> (_UnsafeContinuation<τ_0_0, any Error>) -> () for <Int>
381+
dealloc_stack %5 : $*Int
382+
throw %19 : $any Error
383+
}

0 commit comments

Comments
 (0)