Skip to content

[5.6,SILOptimizer] Let visitTransitiveEndBorrows take SILValues. #41008

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
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
2 changes: 1 addition & 1 deletion include/swift/SIL/OwnershipUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,7 @@ void findTransitiveReborrowBaseValuePairs(
/// Given a begin of a borrow scope, visit all end_borrow users of the borrow or
/// its reborrows.
void visitTransitiveEndBorrows(
BorrowedValue beginBorrow,
SILValue value,
function_ref<void(EndBorrowInst *)> visitEndBorrow);

/// Whether the specified lexical begin_borrow instruction is nested.
Expand Down
4 changes: 2 additions & 2 deletions lib/SIL/Utils/OwnershipUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1504,10 +1504,10 @@ void swift::findTransitiveReborrowBaseValuePairs(
}

void swift::visitTransitiveEndBorrows(
BorrowedValue beginBorrow,
SILValue value,
function_ref<void(EndBorrowInst *)> visitEndBorrow) {
GraphNodeWorklist<SILValue, 4> worklist;
worklist.insert(beginBorrow.value);
worklist.insert(value);

while (!worklist.empty()) {
auto val = worklist.pop();
Expand Down
2 changes: 1 addition & 1 deletion lib/SILOptimizer/Analysis/MemoryBehavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ void AliasAnalysis::computeImmutableScope(SingleValueInstruction *beginScopeInst
addEndScopeInst(endAccess);
}
} else {
visitTransitiveEndBorrows(BorrowedValue(beginScopeInst), addEndScopeInst);
visitTransitiveEndBorrows(beginScopeInst, addEndScopeInst);
}

// Second step: walk up the control flow until the beginScopeInst and add
Expand Down
2 changes: 1 addition & 1 deletion lib/SILOptimizer/Transforms/DeadCodeElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ void DCE::markLive() {
SmallVector<SILValue, 4> roots;
findGuaranteedReferenceRoots(borrowInst->getOperand(), roots);
for (auto root : roots) {
visitTransitiveEndBorrows(BorrowedValue(root),
visitTransitiveEndBorrows(root,
[&](EndBorrowInst *endBorrow) {
markInstructionLive(endBorrow);
});
Expand Down
34 changes: 34 additions & 0 deletions test/SILOptimizer/dead_code_elimination_nontrivial_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -938,3 +938,37 @@ exit(%lifetime_2 : @guaranteed $Klass, %struct_lifetime_2 : @guaranteed $NonTriv
%retval = tuple ()
return %retval : $()
}

sil [ossa] @testBorrowedSwitch : $@convention(thin) (Optional<@sil_unmanaged AnyObject>) -> () {
bb0(%0 : $Optional<@sil_unmanaged AnyObject>):
switch_enum %0 : $Optional<@sil_unmanaged AnyObject>, case #Optional.some!enumelt: bb1, case #Optional.none!enumelt: bb2

bb1(%2 : $@sil_unmanaged AnyObject):
%3 = unmanaged_to_ref %2 : $@sil_unmanaged AnyObject to $AnyObject
%4 = unchecked_ownership_conversion %3 : $AnyObject, @unowned to @guaranteed
%5 = begin_borrow [lexical] %4 : $AnyObject
end_borrow %5 : $AnyObject
end_borrow %4 : $AnyObject
br bb3

bb2:
br bb3

bb3:
%242 = tuple ()
return %242 : $()
}

// rdar://87985420 - crash in nullptr returned by findGuaranteedReferenceRoots
// Test that a boorrow scope introduced by unchecked_ownership_conversion is not
// deleted by DCE.
sil [ossa] @testUncheckedConvertToGuaranteed : $@convention(thin) (@sil_unmanaged AnyObject) -> () {
bb0(%0 : $@sil_unmanaged AnyObject):
%3 = unmanaged_to_ref %0 : $@sil_unmanaged AnyObject to $AnyObject
%4 = unchecked_ownership_conversion %3 : $AnyObject, @unowned to @guaranteed
%5 = begin_borrow [lexical] %4 : $AnyObject
end_borrow %5 : $AnyObject
end_borrow %4 : $AnyObject
%242 = tuple ()
return %242 : $()
}