Skip to content

Fix mem2reg error due to multi block mem2reg with store_borrows #65367

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
Apr 22, 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
5 changes: 2 additions & 3 deletions lib/SILOptimizer/Transforms/SILMem2Reg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1328,13 +1328,12 @@ void StackAllocationPromoter::fixPhiPredBlock(BlockSetVector &phiBlocks,
LLVM_DEBUG(llvm::dbgs() << "*** Fixing the terminator " << *ti << ".\n");

LiveValues values = getEffectiveLiveOutValues(phiBlocks, predBlock);
auto ownedValues = values.getOwned();

LLVM_DEBUG(llvm::dbgs() << "*** Found the definition: "
<< ownedValues.stored);
<< values.getStored());

SmallVector<SILValue> vals;
vals.push_back(ownedValues.replacement(asi, nullptr));
vals.push_back(values.replacement(asi, nullptr));

addArgumentsToBranch(vals, destBlock, ti);
deleter.forceDelete(ti);
Expand Down
36 changes: 36 additions & 0 deletions test/SILOptimizer/mem2reg_borrows.sil
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,39 @@ bb3:
%retval = tuple ()
return %retval : $()
}

// Ensure we don't have an ownership verification error
sil [ossa] @test_store_borrow_phi : $@convention(thin) () -> () {
bb0:
br bb1

bb1:
%1 = apply undef() : $@convention(thin) () -> @owned Klass
%2 = begin_borrow %1 : $Klass
%3 = alloc_stack $Klass
%4 = store_borrow %2 to %3 : $*Klass
%5 = load_borrow %4 : $*Klass
%6 = apply undef(%5) : $@convention(thin) (@guaranteed Klass) -> ()
end_borrow %5 : $Klass
end_borrow %4 : $*Klass
cond_br undef, bb2, bb3

bb2:
br bb4

bb3:
br bb4

bb4:
dealloc_stack %3 : $*Klass
end_borrow %2 : $Klass
destroy_value %1 : $Klass
cond_br undef, bb5, bb6

bb5:
br bb1

bb6:
%17 = tuple ()
return %17 : $()
}
35 changes: 35 additions & 0 deletions test/SILOptimizer/mem2reg_lifetime_borrows.sil
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,38 @@ bb0(%0 : @guaranteed $WrapperStruct):
return %33 : $()
}

// Ensure we don't have an ownership verification error
sil [ossa] @test_store_borrow_phi : $@convention(thin) () -> () {
bb0:
br bb1

bb1:
%1 = apply undef() : $@convention(thin) () -> @owned Klass
%2 = begin_borrow %1 : $Klass
%3 = alloc_stack [lexical] $Klass
%4 = store_borrow %2 to %3 : $*Klass
%5 = load_borrow %4 : $*Klass
%6 = apply undef(%5) : $@convention(thin) (@guaranteed Klass) -> ()
end_borrow %5 : $Klass
end_borrow %4 : $*Klass
cond_br undef, bb2, bb3

bb2:
br bb4

bb3:
br bb4

bb4:
dealloc_stack %3 : $*Klass
end_borrow %2 : $Klass
destroy_value %1 : $Klass
cond_br undef, bb5, bb6

bb5:
br bb1

bb6:
%17 = tuple ()
return %17 : $()
}