Skip to content

Commit e3fbadf

Browse files
committed
[SILOptimizer] Let visitTransitiveEndBorrows take SILValues.
Previously, visitTransitiveEndBorrows took BorrowedValues. However, there is at least one kind of borrow--namely, unchecked_ownership_conversion insts--that is not currently permitted by the BorrowedValue API. The long term fix is to make BorrowedValue handle such instructions. For now, change visitTransitiveEndBorrows to take SILValues so that unchecked_ownership_conversion can be passed to the API. rdar://87985420
1 parent 275a080 commit e3fbadf

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

include/swift/SIL/OwnershipUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,7 @@ void findTransitiveReborrowBaseValuePairs(
12031203
/// Given a begin of a borrow scope, visit all end_borrow users of the borrow or
12041204
/// its reborrows.
12051205
void visitTransitiveEndBorrows(
1206-
BorrowedValue beginBorrow,
1206+
SILValue value,
12071207
function_ref<void(EndBorrowInst *)> visitEndBorrow);
12081208

12091209
/// Whether the specified lexical begin_borrow instruction is nested.

lib/SIL/Utils/OwnershipUtils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,10 +1504,10 @@ void swift::findTransitiveReborrowBaseValuePairs(
15041504
}
15051505

15061506
void swift::visitTransitiveEndBorrows(
1507-
BorrowedValue beginBorrow,
1507+
SILValue value,
15081508
function_ref<void(EndBorrowInst *)> visitEndBorrow) {
15091509
GraphNodeWorklist<SILValue, 4> worklist;
1510-
worklist.insert(beginBorrow.value);
1510+
worklist.insert(value);
15111511

15121512
while (!worklist.empty()) {
15131513
auto val = worklist.pop();

lib/SILOptimizer/Analysis/MemoryBehavior.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ void AliasAnalysis::computeImmutableScope(SingleValueInstruction *beginScopeInst
623623
addEndScopeInst(endAccess);
624624
}
625625
} else {
626-
visitTransitiveEndBorrows(BorrowedValue(beginScopeInst), addEndScopeInst);
626+
visitTransitiveEndBorrows(beginScopeInst, addEndScopeInst);
627627
}
628628

629629
// Second step: walk up the control flow until the beginScopeInst and add

lib/SILOptimizer/Transforms/DeadCodeElimination.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ void DCE::markLive() {
292292
SmallVector<SILValue, 4> roots;
293293
findGuaranteedReferenceRoots(borrowInst->getOperand(), roots);
294294
for (auto root : roots) {
295-
visitTransitiveEndBorrows(BorrowedValue(root),
295+
visitTransitiveEndBorrows(root,
296296
[&](EndBorrowInst *endBorrow) {
297297
markInstructionLive(endBorrow);
298298
});

test/SILOptimizer/dead_code_elimination_nontrivial_ossa.sil

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,3 +938,37 @@ exit(%lifetime_2 : @guaranteed $Klass, %struct_lifetime_2 : @guaranteed $NonTriv
938938
%retval = tuple ()
939939
return %retval : $()
940940
}
941+
942+
sil [ossa] @testBorrowedSwitch : $@convention(thin) (Optional<@sil_unmanaged AnyObject>) -> () {
943+
bb0(%0 : $Optional<@sil_unmanaged AnyObject>):
944+
switch_enum %0 : $Optional<@sil_unmanaged AnyObject>, case #Optional.some!enumelt: bb1, case #Optional.none!enumelt: bb2
945+
946+
bb1(%2 : $@sil_unmanaged AnyObject):
947+
%3 = unmanaged_to_ref %2 : $@sil_unmanaged AnyObject to $AnyObject
948+
%4 = unchecked_ownership_conversion %3 : $AnyObject, @unowned to @guaranteed
949+
%5 = begin_borrow [lexical] %4 : $AnyObject
950+
end_borrow %5 : $AnyObject
951+
end_borrow %4 : $AnyObject
952+
br bb3
953+
954+
bb2:
955+
br bb3
956+
957+
bb3:
958+
%242 = tuple ()
959+
return %242 : $()
960+
}
961+
962+
// rdar://87985420 - crash in nullptr returned by findGuaranteedReferenceRoots
963+
// Test that a boorrow scope introduced by unchecked_ownership_conversion is not
964+
// deleted by DCE.
965+
sil [ossa] @testUncheckedConvertToGuaranteed : $@convention(thin) (@sil_unmanaged AnyObject) -> () {
966+
bb0(%0 : $@sil_unmanaged AnyObject):
967+
%3 = unmanaged_to_ref %0 : $@sil_unmanaged AnyObject to $AnyObject
968+
%4 = unchecked_ownership_conversion %3 : $AnyObject, @unowned to @guaranteed
969+
%5 = begin_borrow [lexical] %4 : $AnyObject
970+
end_borrow %5 : $AnyObject
971+
end_borrow %4 : $AnyObject
972+
%242 = tuple ()
973+
return %242 : $()
974+
}

0 commit comments

Comments
 (0)