Skip to content

[MoveOnlyAddressChecker] Don't complete reborrows or their adjacent phis. #73358

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
May 1, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -3998,14 +3998,20 @@ bool MoveOnlyAddressChecker::completeLifetimes() {
for (auto *block : postOrder->getPostOrder()) {
for (SILInstruction &inst : reverse(*block)) {
for (auto result : inst.getResults()) {
if (llvm::any_of(result->getUsers(),
[](auto *user) { return isa<BranchInst>(user); })) {
continue;
}
if (completion.completeOSSALifetime(result) ==
LifetimeCompletion::WasCompleted) {
changed = true;
}
}
}
for (SILArgument *arg : block->getArguments()) {
assert(!arg->isReborrow() && "reborrows not legal at this SIL stage");
if (arg->isReborrow()) {
continue;
}
if (completion.completeOSSALifetime(arg) ==
LifetimeCompletion::WasCompleted) {
changed = true;
Expand Down
33 changes: 33 additions & 0 deletions test/SILOptimizer/moveonly_addresschecker.sil
Original file line number Diff line number Diff line change
Expand Up @@ -871,3 +871,36 @@ exit:
%retval = tuple ()
return %retval : $()
}

// Test that we don't crash while attempting to complete reborrow lifetimes.
sil [ossa] @closure_lifetime_fixup_output : $@convention(thin) (@owned NonTrivialStructPair) -> () {
bb0(%pair : @owned $NonTrivialStructPair):
%none = enum $Optional<@callee_guaranteed () -> ()>, #Optional.none!enumelt
%none_borrow = begin_borrow %none : $Optional<@callee_guaranteed () -> ()>
%stack = alloc_stack $NonTrivialStructPair, let, name "job", argno 1
%addr = mark_unresolved_non_copyable_value [consumable_and_assignable] %stack : $*NonTrivialStructPair
store %pair to [init] %addr : $*NonTrivialStructPair
cond_br undef, bb1, bb3

bb1:
end_borrow %none_borrow : $Optional<@callee_guaranteed () -> ()>
%closure = partial_apply [callee_guaranteed] undef() : $@convention(thin) () -> ()
%some = enum $Optional<@callee_guaranteed () -> ()>, #Optional.some!enumelt, %closure : $@callee_guaranteed () -> ()
%some_borrow = begin_borrow %some : $Optional<@callee_guaranteed () -> ()>
br bb2

bb2:
br bb4(%some_borrow : $Optional<@callee_guaranteed () -> ()>, %some : $Optional<@callee_guaranteed () -> ()>)

bb3:
br bb4(%none_borrow : $Optional<@callee_guaranteed () -> ()>, %none : $Optional<@callee_guaranteed () -> ()>)

bb4(%reborrow : @reborrow @guaranteed $Optional<@callee_guaranteed () -> ()>, %maybe : @owned $Optional<@callee_guaranteed () -> ()>):
%borrow = borrowed %reborrow : $Optional<@callee_guaranteed () -> ()> from (%maybe : $Optional<@callee_guaranteed () -> ()>)
destroy_addr %addr : $*NonTrivialStructPair
dealloc_stack %stack : $*NonTrivialStructPair
end_borrow %borrow : $Optional<@callee_guaranteed () -> ()>
destroy_value %maybe : $Optional<@callee_guaranteed () -> ()>
%retval = tuple ()
return %retval : $()
}