Skip to content

Commit f0bd583

Browse files
committed
[OwnershipUtils] Extracted utility for common use.
Promoted isRedundantLexicalBeginBorrow function to OwnershipUtils so that it can be used in more than just SemanticARCOpts.
1 parent 75b351e commit f0bd583

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

include/swift/SIL/OwnershipUtils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,14 @@ void visitTransitiveEndBorrows(
12061206
BorrowedValue beginBorrow,
12071207
function_ref<void(EndBorrowInst *)> visitEndBorrow);
12081208

1209+
/// Whether the specified lexical begin_borrow instruction is nested.
1210+
///
1211+
/// A begin_borrow [lexical] is nested if the borrowed value's lifetime is
1212+
/// guaranteed by another lexical scope. That happens if:
1213+
/// - the value is a guaranteed argument to the function
1214+
/// - the value is itself a begin_borrow [lexical]
1215+
bool isNestedLexicalBeginBorrow(BeginBorrowInst *bbi);
1216+
12091217
} // namespace swift
12101218

12111219
#endif

lib/SIL/Utils/OwnershipUtils.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,3 +1487,21 @@ void swift::visitTransitiveEndBorrows(
14871487
}
14881488
}
14891489
}
1490+
1491+
/// Whether the specified lexical begin_borrow instruction is nested.
1492+
///
1493+
/// A begin_borrow [lexical] is nested if the borrowed value's lifetime is
1494+
/// guaranteed by another lexical scope. That happens if:
1495+
/// - the value is a guaranteed argument to the function
1496+
/// - the value is itself a begin_borrow [lexical]
1497+
bool swift::isNestedLexicalBeginBorrow(BeginBorrowInst *bbi) {
1498+
assert(bbi->isLexical());
1499+
auto value = bbi->getOperand();
1500+
if (auto *outerBBI = dyn_cast<BeginBorrowInst>(value)) {
1501+
return outerBBI->isLexical();
1502+
}
1503+
if (auto *arg = dyn_cast<SILFunctionArgument>(value)) {
1504+
return arg->getOwnershipKind() == OwnershipKind::Guaranteed;
1505+
}
1506+
return false;
1507+
}

lib/SILOptimizer/SemanticARC/BorrowScopeOpts.cpp

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,14 @@
2424
using namespace swift;
2525
using namespace swift::semanticarc;
2626

27-
/// Whether the provided lexical begin_borrow instruction is redundant.
28-
///
29-
/// A begin_borrow [lexical] is redundant if the borrowed value's lifetime is
30-
/// otherwise guaranteed. That happens if:
31-
/// - the value is a guaranteed argument to the function
32-
/// - the value is itself a begin_borrow [lexical]
33-
static bool isRedundantLexicalBeginBorrow(BeginBorrowInst *bbi) {
34-
assert(bbi->isLexical());
35-
auto value = bbi->getOperand();
36-
if (auto *outerBBI = dyn_cast<BeginBorrowInst>(value)) {
37-
return outerBBI->isLexical();
38-
}
39-
if (auto *arg = dyn_cast<SILFunctionArgument>(value)) {
40-
return arg->getOwnershipKind() == OwnershipKind::Guaranteed;
41-
}
42-
return false;
43-
}
44-
4527
bool SemanticARCOptVisitor::visitBeginBorrowInst(BeginBorrowInst *bbi) {
4628
// Quickly check if we are supposed to perform this transformation.
4729
if (!ctx.shouldPerform(ARCTransformKind::RedundantBorrowScopeElimPeephole))
4830
return false;
4931

5032
// Non-redundant, lexical borrow scopes must remain in order to ensure that
5133
// value lifetimes are not observably shortened.
52-
if (bbi->isLexical() && !isRedundantLexicalBeginBorrow(bbi)) {
34+
if (bbi->isLexical() && !isNestedLexicalBeginBorrow(bbi)) {
5335
return false;
5436
}
5537

0 commit comments

Comments
 (0)