Skip to content

Commit 63e43b6

Browse files
committed
[ownership] Rename BorrowingOperand::visitLocalEndScope{Instruction,Use}s(Operand *)
This originally trafficked in Instructions and for some reason the name was never changed. I also changed the result type to be a bool and added the ability for the passed in closure to signal failure (and iteration stop) by returning false. This also makes it possible to use visitLocalEndScopeUses in if statements which can be useful.
1 parent 5666408 commit 63e43b6

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

include/swift/SIL/OwnershipUtils.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@ struct BorrowingOperand {
179179
/// Example: An apply performs an instantaneous recursive borrow of a
180180
/// guaranteed value but a begin_apply borrows the value over the entire
181181
/// region of code corresponding to the coroutine.
182-
void visitLocalEndScopeInstructions(function_ref<void(Operand *)> func) const;
182+
///
183+
/// NOTE: Return false from func to stop iterating. Returns false if the
184+
/// closure requested to stop early.
185+
bool visitLocalEndScopeUses(function_ref<bool(Operand *)> func) const;
183186

184187
/// Returns true if this borrow scope operand consumes guaranteed
185188
/// values and produces a new scope afterwards.

lib/SIL/Utils/OwnershipUtils.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,31 +161,33 @@ llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os,
161161
return os;
162162
}
163163

164-
void BorrowingOperand::visitLocalEndScopeInstructions(
165-
function_ref<void(Operand *)> func) const {
164+
bool BorrowingOperand::visitLocalEndScopeUses(
165+
function_ref<bool(Operand *)> func) const {
166166
switch (kind) {
167167
case BorrowingOperandKind::BeginBorrow:
168168
for (auto *use : cast<BeginBorrowInst>(op->getUser())->getUses()) {
169169
if (use->isLifetimeEnding()) {
170-
func(use);
170+
if (!func(use))
171+
return false;
171172
}
172173
}
173-
return;
174+
return true;
174175
case BorrowingOperandKind::BeginApply: {
175176
auto *user = cast<BeginApplyInst>(op->getUser());
176177
for (auto *use : user->getTokenResult()->getUses()) {
177-
func(use);
178+
if (!func(use))
179+
return false;
178180
}
179-
return;
181+
return true;
180182
}
181183
// These are instantaneous borrow scopes so there aren't any special end
182184
// scope instructions.
183185
case BorrowingOperandKind::Apply:
184186
case BorrowingOperandKind::TryApply:
185187
case BorrowingOperandKind::Yield:
186-
return;
188+
return true;
187189
case BorrowingOperandKind::Branch:
188-
return;
190+
return true;
189191
}
190192
}
191193

@@ -267,7 +269,10 @@ void BorrowingOperand::visitUserResultConsumingUses(
267269
void BorrowingOperand::getImplicitUses(
268270
SmallVectorImpl<Operand *> &foundUses,
269271
std::function<void(Operand *)> *errorFunction) const {
270-
visitLocalEndScopeInstructions([&](Operand *op) { foundUses.push_back(op); });
272+
visitLocalEndScopeUses([&](Operand *op) {
273+
foundUses.push_back(op);
274+
return true;
275+
});
271276
}
272277

273278
//===----------------------------------------------------------------------===//

lib/SIL/Verifier/ReborrowVerifier.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ void ReborrowVerifier::verifyReborrows(BorrowingOperand initialScopedOperand,
4242
SILValue value) {
4343
SmallVector<std::tuple<Operand *, SILValue>, 4> worklist;
4444
// Initialize the worklist with borrow lifetime ending uses
45-
initialScopedOperand.visitLocalEndScopeInstructions([&](Operand *op) {
45+
initialScopedOperand.visitLocalEndScopeUses([&](Operand *op) {
4646
worklist.emplace_back(op, value);
47+
return true;
4748
});
4849

4950
while (!worklist.empty()) {

0 commit comments

Comments
 (0)