Skip to content

Commit e1cb0b5

Browse files
committed
[MemAccessUtils] Look thru nested for guar roots.
Previously, findGuaranteedReferenceRoots always stopped searching when finding a begin_borrow, because it's not an ownership-forwarding instruction. Here, it is conditionally allowed to keep search through the borrowee of that begin_borrow if it itself is guaranteed.
1 parent 7430c61 commit e1cb0b5

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

include/swift/SIL/MemAccessUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ SILValue findOwnershipReferenceRoot(SILValue ref);
206206

207207
/// Look through all ownership forwarding instructions to find the values which
208208
/// were originally borrowed.
209-
void findGuaranteedReferenceRoots(SILValue value,
209+
void findGuaranteedReferenceRoots(SILValue value, bool lookThroughNestedBorrows,
210210
SmallVectorImpl<SILValue> &roots);
211211

212212
/// Find the aggregate containing the first owned root of the

lib/SIL/Utils/MemAccessUtils.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,7 @@ SILValue swift::findOwnershipReferenceRoot(SILValue ref) {
861861
}
862862

863863
void swift::findGuaranteedReferenceRoots(SILValue value,
864+
bool lookThroughNestedBorrows,
864865
SmallVectorImpl<SILValue> &roots) {
865866
GraphNodeWorklist<SILValue, 4> worklist;
866867
auto addAllOperandsToWorklist = [&worklist](SILInstruction *inst) -> bool {
@@ -882,7 +883,16 @@ void swift::findGuaranteedReferenceRoots(SILValue value,
882883
}
883884
}
884885
} else if (auto *inst = value->getDefiningInstruction()) {
885-
if (auto *result =
886+
if (auto *bbi = dyn_cast<BeginBorrowInst>(inst)) {
887+
auto borrowee = bbi->getOperand();
888+
if (lookThroughNestedBorrows &&
889+
borrowee->getOwnershipKind() == OwnershipKind::Guaranteed) {
890+
// A nested borrow, the root guaranteed earlier in the use-def chain.
891+
worklist.insert(borrowee);
892+
}
893+
// The borrowee isn't guaranteed or we aren't looking through nested
894+
// borrows. Fall through to add the begin_borrow to roots.
895+
} else if (auto *result =
886896
dyn_cast<FirstArgOwnershipForwardingSingleValueInst>(inst)) {
887897
if (result->getNumOperands() > 0) {
888898
worklist.insert(result->getOperand(0));

lib/SILOptimizer/Transforms/DeadCodeElimination.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,9 @@ void DCE::markLive() {
292292
// Visit the end_borrows of all the borrow scopes that this
293293
// begin_borrow could be borrowing.
294294
SmallVector<SILValue, 4> roots;
295-
findGuaranteedReferenceRoots(borrowInst->getOperand(), roots);
295+
findGuaranteedReferenceRoots(borrowInst->getOperand(),
296+
/*lookThroughNestedBorrows=*/false,
297+
roots);
296298
for (auto root : roots) {
297299
visitTransitiveEndBorrows(root,
298300
[&](EndBorrowInst *endBorrow) {

0 commit comments

Comments
 (0)