Skip to content

Fix LoadBorrowImmutabilityChecker to handle reborrows #59393

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 3 commits into from
Jun 12, 2022
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
6 changes: 3 additions & 3 deletions lib/SIL/Verifier/LoadBorrowImmutabilityChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,9 @@ bool LoadBorrowImmutabilityAnalysis::isImmutable(LoadBorrowInst *lbi) {
// cases to try and exhaustively identify if those writes overlap with our
// load_borrow.
SmallVector<Operand *, 8> endBorrowUses;
transform(lbi->getUsersOfType<EndBorrowInst>(),
std::back_inserter(endBorrowUses),
[](EndBorrowInst *ebi) { return &ebi->getAllOperands()[0]; });
visitTransitiveEndBorrows(lbi, [&](EndBorrowInst *endBorrow) {
endBorrowUses.push_back(&endBorrow->getOperandRef());
});

switch (accessPath.getStorage().getKind()) {
case AccessStorage::Nested: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,23 @@ bb0(%0 : @owned $K):
return %r : $()
}

sil @use_guaranteed : $@convention(thin) (@guaranteed K) -> ()

sil [ossa] @test_write_reborrow : $@convention(thin) (@owned K, @owned K) -> () {
bb0(%0 : @owned $K, %1 : @owned $K):
%stk = alloc_stack [lexical] $K
store %0 to [init] %stk : $*K
%ld1 = load_borrow %stk : $*K
br bb2(%ld1 : $K)

bb2(%ld : @guaranteed $K):
%3 = function_ref @use_guaranteed : $@convention(thin) (@guaranteed K) -> ()
%4 = apply %3(%ld) : $@convention(thin) (@guaranteed K) -> ()
end_borrow %ld : $K
store %1 to [assign] %stk : $*K
destroy_addr %stk : $*K
dealloc_stack %stk : $*K
%6 = tuple ()
return %6 : $()
}

27 changes: 27 additions & 0 deletions test/SIL/OwnershipVerifier/load_borrow_verify_errors.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: %target-sil-opt %s -verify-continue-on-failure=true -o /dev/null 2>&1 | %FileCheck %s

class Klass {}

sil @use_guaranteed : $@convention(thin) (@guaranteed Klass) -> ()

// Write: store {{.*}} [assign] {{.*}}
// CHECK: Begin Error in function test_write_reborrow
// CHECK: SIL verification failed: Found load borrow that is invalidated by a local write?!: loadBorrowImmutabilityAnalysis.isImmutable(LBI)
// CHECK: End Error in function test_write_reborrow
sil [ossa] @test_write_reborrow : $@convention(thin) (@owned Klass, @owned Klass) -> () {
bb0(%0 : @owned $Klass, %1 : @owned $Klass):
%stk = alloc_stack [lexical] $Klass
store %0 to [init] %stk : $*Klass
%ld1 = load_borrow %stk : $*Klass
br bb2(%ld1 : $Klass)

bb2(%ld : @guaranteed $Klass):
store %1 to [assign] %stk : $*Klass
%3 = function_ref @use_guaranteed : $@convention(thin) (@guaranteed Klass) -> ()
%4 = apply %3(%ld) : $@convention(thin) (@guaranteed Klass) -> ()
end_borrow %ld : $Klass
destroy_addr %stk : $*Klass
dealloc_stack %stk : $*Klass
%6 = tuple ()
return %6 : $()
}