Skip to content

Commit 704133c

Browse files
committed
[OwnershipUtils] Repurpose utility.
Replaced findInnerTransitiveGuaranteeedUsesOfBorrowedValue with findExtendedUsesOfSimpleBorrowedValue. Starting from a borrowed value, it finds all extended (i.e., seeing through copies) uses of the borrow and its projections within the simple (i.e. without considering reborrowing) borrow scope.
1 parent 78a2eeb commit 704133c

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

include/swift/SIL/OwnershipUtils.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,9 @@ inline bool isForwardingConsume(SILValue value) {
104104
bool findInnerTransitiveGuaranteedUses(
105105
SILValue guaranteedValue, SmallVectorImpl<Operand *> *usePoints = nullptr);
106106

107-
/// Like findInnerTransitiveGuaranteedUses except that rather than it being a
108-
/// precondition that the provided value not be a BorrowedValue, it is a [type-
109-
/// system-enforced] precondition that the provided value be a BorrowedValue.
110-
///
111-
/// TODO: Merge with findInnerTransitiveGuaranteedUses.
112-
bool findInnerTransitiveGuaranteedUsesOfBorrowedValue(
107+
/// Find all uses in the extended lifetime (i.e. including copies) of a simple
108+
/// (i.e. not reborrowed) borrow scope and its transitive uses.
109+
bool findExtendedUsesOfSimpleBorrowedValue(
113110
BorrowedValue borrowedValue,
114111
SmallVectorImpl<Operand *> *usePoints = nullptr);
115112

lib/SIL/Utils/OwnershipUtils.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,9 @@ bool swift::findInnerTransitiveGuaranteedUses(
195195
return true;
196196
}
197197

198-
/// Like findInnerTransitiveGuaranteedUses except that rather than it being a
199-
/// precondition that the provided value not be a BorrowedValue, it is a [type-
200-
/// system-enforced] precondition that the provided value be a BorrowedValue.
201-
///
202-
/// TODO: Merge with findInnerTransitiveGuaranteedUses. Note that at the moment
203-
/// the two are _almost_ identical, but not quite because the other has a
204-
/// #if 0 and not just leaf uses but ALL uses are recorded.
205-
bool swift::findInnerTransitiveGuaranteedUsesOfBorrowedValue(
198+
/// Find all uses in the extended lifetime (i.e. including copies) of a simple
199+
/// (i.e. not reborrowed) borrow scope and its transitive uses.
200+
bool swift::findExtendedUsesOfSimpleBorrowedValue(
206201
BorrowedValue borrowedValue, SmallVectorImpl<Operand *> *usePoints) {
207202

208203
auto recordUse = [&](Operand *use) {
@@ -220,21 +215,31 @@ bool swift::findInnerTransitiveGuaranteedUsesOfBorrowedValue(
220215
// membership check locally in this function (within a borrow scope) because
221216
// it isn't needed for the immediate uses, only the transitive uses.
222217
GraphNodeWorklist<Operand *, 8> worklist;
223-
for (Operand *use : borrowedValue.value->getUses()) {
224-
if (use->getOperandOwnership() != OperandOwnership::NonUse)
225-
worklist.insert(use);
226-
}
218+
auto addUsesToWorklist = [&worklist](SILValue value) {
219+
for (Operand *use : value->getUses()) {
220+
if (use->getOperandOwnership() != OperandOwnership::NonUse)
221+
worklist.insert(use);
222+
}
223+
};
224+
225+
addUsesToWorklist(borrowedValue.value);
227226

228227
// --- Transitively follow forwarded uses and look for escapes.
229228

230229
// usePoints grows in this loop.
231230
while (Operand *use = worklist.pop()) {
231+
if (auto *cvi = dyn_cast<CopyValueInst>(use->getUser())) {
232+
addUsesToWorklist(cvi);
233+
}
232234
switch (use->getOperandOwnership()) {
233235
case OperandOwnership::NonUse:
236+
break;
237+
234238
case OperandOwnership::TrivialUse:
235239
case OperandOwnership::ForwardingConsume:
236240
case OperandOwnership::DestroyingConsume:
237-
llvm_unreachable("this operand cannot handle an inner guaranteed use");
241+
recordUse(use);
242+
break;
238243

239244
case OperandOwnership::ForwardingUnowned:
240245
case OperandOwnership::PointerEscape:
@@ -264,6 +269,7 @@ bool swift::findInnerTransitiveGuaranteedUsesOfBorrowedValue(
264269
AddressUseKind::NonEscaping) {
265270
return false;
266271
}
272+
recordUse(use);
267273
break;
268274

269275
case OperandOwnership::ForwardingBorrow: {

lib/SILOptimizer/Utils/ShrinkBorrowScope.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ bool ShrinkBorrowScope::run() {
201201
}
202202

203203
bool ShrinkBorrowScope::populateUsers() {
204-
SmallVector<Operand *, 16> usePoints;
205-
if (!findInnerTransitiveGuaranteedUsesOfBorrowedValue(
206-
BorrowedValue(introducer), &usePoints)) {
204+
SmallVector<Operand *, 16> uses;
205+
if (!findExtendedUsesOfSimpleBorrowedValue(BorrowedValue(introducer),
206+
&uses)) {
207207
// If the value produced by begin_borrow escapes, don't shrink the borrow
208208
// scope.
209209
return false;

0 commit comments

Comments
 (0)