Skip to content

Commit 281da6b

Browse files
authored
Merge pull request swiftlang#30066 from gottesmm/pr-1130ca3a3a3a8cf94b05b9bb79cdecefe4a48833
2 parents 4893989 + 258f9be commit 281da6b

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

lib/SILOptimizer/Transforms/SemanticARCOpts.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -510,18 +510,22 @@ bool SemanticARCOptVisitor::visitBeginBorrowInst(BeginBorrowInst *bbi) {
510510
auto kind = bbi->getOperand().getOwnershipKind();
511511
SmallVector<EndBorrowInst *, 16> endBorrows;
512512
for (auto *op : bbi->getUses()) {
513-
auto *user = op->getUser();
514-
switch (user->getKind()) {
515-
case SILInstructionKind::EndBorrowInst:
516-
endBorrows.push_back(cast<EndBorrowInst>(user));
517-
break;
518-
default:
513+
if (!op->isConsumingUse()) {
519514
// Make sure that this operand can accept our arguments kind.
520515
auto map = op->getOwnershipKindMap();
521516
if (map.canAcceptKind(kind))
522517
continue;
523518
return false;
524519
}
520+
521+
// Otherwise, this borrow is being consumed. See if our consuming inst is an
522+
// end_borrow. If it isn't, then return false, this scope is
523+
// needed. Otherwise, add the end_borrow to our list of end borrows.
524+
auto *ebi = dyn_cast<EndBorrowInst>(op->getUser());
525+
if (!ebi) {
526+
return false;
527+
}
528+
endBorrows.push_back(ebi);
525529
}
526530

527531
// At this point, we know that the begin_borrow's operand can be

test/SILOptimizer/semantic-arc-opts.sil

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,3 +1285,20 @@ bb3:
12851285
%9999 = tuple()
12861286
return %9999 : $()
12871287
}
1288+
1289+
// TODO: We can support this with time.
1290+
//
1291+
// CHECK-LABEL: sil [ossa] @do_eliminate_begin_borrow_consumed_by_guaranteed_phi : $@convention(thin) (@owned Builtin.NativeObject) -> () {
1292+
// CHECK: begin_borrow
1293+
// CHECK: } // end sil function 'do_eliminate_begin_borrow_consumed_by_guaranteed_phi'
1294+
sil [ossa] @do_eliminate_begin_borrow_consumed_by_guaranteed_phi : $@convention(thin) (@owned Builtin.NativeObject) -> () {
1295+
bb0(%0 : @owned $Builtin.NativeObject):
1296+
%1 = begin_borrow %0 : $Builtin.NativeObject
1297+
br bb1(%1 : $Builtin.NativeObject)
1298+
1299+
bb1(%2 : @guaranteed $Builtin.NativeObject):
1300+
end_borrow %2 : $Builtin.NativeObject
1301+
destroy_value %0 : $Builtin.NativeObject
1302+
%9999 = tuple()
1303+
return %9999 : $()
1304+
}

0 commit comments

Comments
 (0)