Skip to content

Commit a718e7d

Browse files
committed
[ownership] Centralize the concept of isReborrow on BorrowingOperand.
A reborrow occurs when a Borrowing Operand ends the lifetime of a borrowed value and propagates forward a new guaranteed value that continues the guaranteed lifetime of the value.
1 parent c2b3be4 commit a718e7d

File tree

5 files changed

+9
-17
lines changed

5 files changed

+9
-17
lines changed

include/swift/SIL/OwnershipUtils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ struct BorrowingOperand {
178178

179179
/// Returns true if this borrow scope operand consumes guaranteed
180180
/// values and produces a new scope afterwards.
181-
bool consumesGuaranteedValues() const {
181+
///
182+
/// TODO: tuple, struct, destructure_tuple, destructure_struct.
183+
bool isReborrow() const {
182184
switch (kind) {
183185
case BorrowingOperandKind::BeginBorrow:
184186
case BorrowingOperandKind::BeginApply:

lib/SIL/Utils/OwnershipUtils.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,6 @@ bool swift::isOwnershipForwardingInst(SILInstruction *i) {
125125
return isOwnershipForwardingValueKind(SILNodeKind(i->getKind()));
126126
}
127127

128-
bool swift::isReborrowInstruction(const SILInstruction *i) {
129-
switch (i->getKind()) {
130-
case SILInstructionKind::BranchInst:
131-
return true;
132-
default:
133-
return false;
134-
}
135-
}
136-
137128
//===----------------------------------------------------------------------===//
138129
// Borrowing Operand
139130
//===----------------------------------------------------------------------===//
@@ -247,7 +238,7 @@ void BorrowingOperand::visitConsumingUsesOfBorrowIntroducingUserResults(
247238
// single guaranteed scope.
248239
value.visitLocalScopeEndingUses([&](Operand *valueUser) {
249240
if (auto subBorrowScopeOp = BorrowingOperand::get(valueUser)) {
250-
if (subBorrowScopeOp->consumesGuaranteedValues()) {
241+
if (subBorrowScopeOp->isReborrow()) {
251242
subBorrowScopeOp->visitUserResultConsumingUses(func);
252243
return;
253244
}

lib/SIL/Verifier/LinearLifetimeChecker.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ void State::checkForSameBlockUseAfterFree(Operand *consumingUse,
313313
}) == userBlock->end()) {
314314
continue;
315315
}
316-
} else if (isReborrowInstruction(consumingUse->getUser())) {
316+
} else if (auto borrowingOperand = BorrowingOperand::get(consumingUse)) {
317+
assert(borrowingOperand->isReborrow());
317318
continue;
318319
}
319320

lib/SIL/Verifier/ReborrowVerifier.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,9 @@ void ReborrowVerifier::verifyReborrows(BorrowingOperand initialScopedOperand,
5252
std::tie(borrowLifetimeEndOp, baseVal) = worklist.pop_back_val();
5353
auto *borrowLifetimeEndUser = borrowLifetimeEndOp->getUser();
5454

55-
// TODO: Add a ReborrowOperand ADT if we need to treat more instructions as
56-
// a reborrow
57-
if (!isReborrowInstruction(borrowLifetimeEndUser)) {
55+
auto borrowingOperand = BorrowingOperand::get(borrowLifetimeEndOp);
56+
if (!borrowingOperand || !borrowingOperand->isReborrow())
5857
continue;
59-
}
6058

6159
if (isVisitedOp(borrowLifetimeEndOp, baseVal))
6260
continue;

lib/SIL/Verifier/SILOwnershipVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ bool SILValueOwnershipChecker::gatherUsers(
354354
// BorrowScopeOperand and if so, add its end scope instructions as
355355
// implicit regular users of our value.
356356
if (auto scopedOperand = BorrowingOperand::get(op)) {
357-
assert(!scopedOperand->consumesGuaranteedValues());
357+
assert(!scopedOperand->isReborrow());
358358

359359
std::function<void(Operand *)> onError = [&](Operand *op) {
360360
errorBuilder.handleMalformedSIL([&] {

0 commit comments

Comments
 (0)