Skip to content

Commit c873d84

Browse files
committed
Fix BorrowingOperand::getBorrowIntroducingUserResult()
To handle borrowing operands that produce a dependence value but do not create a nested borrow scope. This includes non-reborrow borrowed-from and guaranteed mark_dependence [nonescaping].
1 parent a2f2644 commit c873d84

File tree

6 files changed

+30
-30
lines changed

6 files changed

+30
-30
lines changed

include/swift/SIL/OwnershipUtils.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ struct BorrowingOperand {
428428
/// cannot be reborrowed.
429429
///
430430
/// If true, getBorrowIntroducingUserResult() can be called to acquire the
431-
/// BorrowedValue that introduces a new borrow scope.
431+
/// SILValue that introduces a new borrow scope.
432432
bool hasBorrowIntroducingUser() const {
433433
// TODO: Can we derive this by running a borrow introducer check ourselves?
434434
switch (kind) {
@@ -451,9 +451,11 @@ struct BorrowingOperand {
451451
llvm_unreachable("Covered switch isn't covered?!");
452452
}
453453

454-
/// If this operand's user has a borrowed value result return a valid
455-
/// BorrowedValue instance.
456-
BorrowedValue getBorrowIntroducingUserResult() const;
454+
/// If this operand's user has a single result that introduces the borrow
455+
/// scope, return the result value. If the result is scoped (begin_borrow)
456+
/// then it can be used to initialize a BorrowedValue. Some results, like
457+
/// guaranteed forwarding phis, are not scoped.
458+
SILValue getBorrowIntroducingUserResult() const;
457459

458460
/// Return the borrowing operand's value.
459461
SILValue getScopeIntroducingUserResult();

include/swift/SIL/SILInstruction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4769,7 +4769,7 @@ class BorrowedFromInst final : public InstructionBaseWithTrailingOperands<
47694769

47704770
public:
47714771

4772-
SILValue getBorrowedValue() {
4772+
SILValue getBorrowedValue() const {
47734773
return getAllOperands()[0].get();
47744774
}
47754775

lib/SIL/Utils/OwnershipLiveness.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ struct InteriorLivenessVisitor :
159159
/// Handles begin_borrow, load_borrow, store_borrow, begin_apply.
160160
bool handleInnerBorrow(BorrowingOperand borrowingOperand) {
161161
if (handleInnerScopeCallback) {
162-
auto value = borrowingOperand.getScopeIntroducingUserResult();
163-
if (value) {
162+
if (auto value = borrowingOperand.getScopeIntroducingUserResult()) {
164163
handleInnerScopeCallback(value);
165164
}
166165
}

lib/SIL/Utils/OwnershipUtils.cpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ bool swift::findPointerEscape(SILValue original) {
6060
}
6161
case OperandOwnership::Borrow: {
6262
auto borrowOp = BorrowingOperand(use);
63-
if (auto borrowValue = borrowOp.getBorrowIntroducingUserResult()) {
64-
worklist.pushIfNotVisited(borrowValue.value);
63+
if (auto result = borrowOp.getBorrowIntroducingUserResult()) {
64+
worklist.pushIfNotVisited(result);
6565
}
6666
break;
6767
}
@@ -548,7 +548,7 @@ findExtendedTransitiveGuaranteedUses(SILValue guaranteedValue,
548548
usePoints.pop_back();
549549
auto borrowedPhi =
550550
BorrowingOperand(reborrow).getBorrowIntroducingUserResult();
551-
reborrows.insert(borrowedPhi.value);
551+
reborrows.insert(borrowedPhi);
552552
};
553553
if (!findTransitiveGuaranteedUses(guaranteedValue, usePoints, visitReborrow))
554554
return false;
@@ -768,13 +768,15 @@ bool BorrowingOperand::visitExtendedScopeEndingUses(
768768
function_ref<bool(Operand *)> visitUnknownUse) const {
769769

770770
if (hasBorrowIntroducingUser()) {
771-
auto borrowedValue = getBorrowIntroducingUserResult();
772-
return borrowedValue.visitExtendedScopeEndingUses(visitor);
771+
auto result = getBorrowIntroducingUserResult();
772+
if (auto borrowedValue = BorrowedValue(result)) {
773+
return borrowedValue.visitExtendedScopeEndingUses(visitor);
774+
}
773775
}
774776
return visitScopeEndingUses(visitor, visitUnknownUse);
775777
}
776778

777-
BorrowedValue BorrowingOperand::getBorrowIntroducingUserResult() const {
779+
SILValue BorrowingOperand::getBorrowIntroducingUserResult() const {
778780
switch (kind) {
779781
case BorrowingOperandKind::Invalid:
780782
case BorrowingOperandKind::Apply:
@@ -785,20 +787,15 @@ BorrowedValue BorrowingOperand::getBorrowIntroducingUserResult() const {
785787
case BorrowingOperandKind::MarkDependenceNonEscaping:
786788
case BorrowingOperandKind::BeginAsyncLet:
787789
case BorrowingOperandKind::StoreBorrow:
788-
return BorrowedValue();
790+
return SILValue();
789791

790792
case BorrowingOperandKind::BeginBorrow:
791793
case BorrowingOperandKind::BorrowedFrom: {
792-
auto value = BorrowedValue(cast<SingleValueInstruction>(op->getUser()));
793-
assert(value);
794-
return value;
794+
return cast<SingleValueInstruction>(op->getUser());
795795
}
796796
case BorrowingOperandKind::Branch: {
797797
auto *bi = cast<BranchInst>(op->getUser());
798-
auto value =
799-
BorrowedValue(bi->getDestBB()->getArgument(op->getOperandNumber()));
800-
assert(value && "guaranteed-to-unowned conversion not allowed on branches");
801-
return value;
798+
return bi->getDestBB()->getArgument(op->getOperandNumber());
802799
}
803800
}
804801
llvm_unreachable("covered switch");
@@ -970,9 +967,9 @@ bool BorrowedValue::visitExtendedScopeEndingUses(
970967

971968
auto visitEnd = [&](Operand *scopeEndingUse) {
972969
if (scopeEndingUse->getOperandOwnership() == OperandOwnership::Reborrow) {
973-
auto borrowedValue =
970+
auto result =
974971
BorrowingOperand(scopeEndingUse).getBorrowIntroducingUserResult();
975-
reborrows.insert(borrowedValue.value);
972+
reborrows.insert(result);
976973
return true;
977974
}
978975
return visitor(scopeEndingUse);
@@ -997,9 +994,9 @@ bool BorrowedValue::visitTransitiveLifetimeEndingUses(
997994

998995
auto visitEnd = [&](Operand *scopeEndingUse) {
999996
if (scopeEndingUse->getOperandOwnership() == OperandOwnership::Reborrow) {
1000-
auto borrowedValue =
997+
auto result =
1001998
BorrowingOperand(scopeEndingUse).getBorrowIntroducingUserResult();
1002-
reborrows.insert(borrowedValue.value);
999+
reborrows.insert(result);
10031000
// visitor on the reborrow
10041001
return visitor(scopeEndingUse);
10051002
}
@@ -1050,8 +1047,8 @@ bool BorrowedValue::visitInteriorPointerOperandHelper(
10501047
break;
10511048
}
10521049

1053-
auto bv = borrowingOperand.getBorrowIntroducingUserResult();
1054-
for (auto *use : bv->getUses()) {
1050+
auto result = borrowingOperand.getBorrowIntroducingUserResult();
1051+
for (auto *use : result->getUses()) {
10551052
if (auto intPtrOperand = InteriorPointerOperand(use)) {
10561053
func(intPtrOperand);
10571054
continue;

lib/SILOptimizer/SILCombiner/SILCombinerBuiltinVisitors.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ SILCombiner::optimizeBuiltinCOWBufferForReadingOSSA(BuiltinInst *bi) {
119119
if (auto operand = BorrowingOperand(use)) {
120120
if (operand.isReborrow())
121121
return nullptr;
122-
auto bv = operand.getBorrowIntroducingUserResult();
123-
accumulatedBorrowedValues.push_back(bv);
122+
auto result = operand.getBorrowIntroducingUserResult();
123+
if (auto bv = BorrowedValue(result)) {
124+
accumulatedBorrowedValues.push_back(bv);
125+
}
124126
continue;
125127
}
126128

lib/SILOptimizer/Utils/OwnershipOptUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ void BorrowedLifetimeExtender::analyzeExtendedScope() {
618618
SWIFT_ASSERT_ONLY(reborrowedOperands.insert(endScope));
619619

620620
// TODO: if non-phi reborrows are added, handle multiple results.
621-
discoverReborrow(borrowingOper.getBorrowIntroducingUserResult().value);
621+
discoverReborrow(borrowingOper.getBorrowIntroducingUserResult());
622622
}
623623
return true;
624624
};

0 commit comments

Comments
 (0)