Skip to content

Commit 1e82006

Browse files
committed
[ShrinkBorrowScope] Don't hoist over begin_borrows.
While it is sometimes valid to hoist over begin_borrows of copies of the borrowee, it is not always valid, as the test case committed here illustrates. As a future optimization, we can reenable this hoisting with the appropriate condition.
1 parent 9285965 commit 1e82006

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

lib/SILOptimizer/Utils/ShrinkBorrowScope.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,6 @@ struct DeinitBarriers final {
124124
/// Blocks to "after the end" of which hoisting was able to proceed.
125125
BasicBlockSet hoistingReachesEndBlocks;
126126

127-
/// Borrows to be rewritten as borrows of %borrowee.
128-
SmallVector<BeginBorrowInst *, 4> borrows;
129-
130127
/// Copies to be rewritten as copies of %borrowee.
131128
SmallVector<CopyValueInst *, 4> copies;
132129

@@ -153,7 +150,7 @@ class DataFlow final {
153150
Usage const &uses;
154151
DeinitBarriers &result;
155152

156-
enum class Classification { Barrier, Borrow, Copy, Other };
153+
enum class Classification { Barrier, Copy, Other };
157154

158155
BackwardReachability<DataFlow> reachability;
159156

@@ -221,12 +218,7 @@ DataFlow::classifyInstruction(SILInstruction *instruction) {
221218
if (instruction == &context.introducer) {
222219
return Classification::Barrier;
223220
}
224-
if (auto *bbi = dyn_cast<BeginBorrowInst>(instruction)) {
225-
if (bbi->isLexical() &&
226-
isSimpleExtendedIntroducerDef(context, bbi->getOperand())) {
227-
return Classification::Borrow;
228-
}
229-
} else if (auto *cvi = dyn_cast<CopyValueInst>(instruction)) {
221+
if (auto *cvi = dyn_cast<CopyValueInst>(instruction)) {
230222
if (isSimpleExtendedIntroducerDef(context, cvi->getOperand())) {
231223
return Classification::Copy;
232224
}
@@ -244,7 +236,6 @@ bool DataFlow::classificationIsBarrier(Classification classification) {
244236
switch (classification) {
245237
case Classification::Barrier:
246238
return true;
247-
case Classification::Borrow:
248239
case Classification::Copy:
249240
case Classification::Other:
250241
return false;
@@ -259,9 +250,6 @@ void DataFlow::visitedInstruction(SILInstruction *instruction,
259250
case Classification::Barrier:
260251
result.barriers.push_back(instruction);
261252
return;
262-
case Classification::Borrow:
263-
result.borrows.push_back(cast<BeginBorrowInst>(instruction));
264-
return;
265253
case Classification::Copy:
266254
result.copies.push_back(cast<CopyValueInst>(instruction));
267255
return;
@@ -318,10 +306,6 @@ class Rewriter final {
318306
bool Rewriter::run() {
319307
bool madeChange = false;
320308

321-
for (auto *bbi : barriers.borrows) {
322-
bbi->setOperand(context.borrowee);
323-
madeChange = true;
324-
}
325309
for (auto *cvi : barriers.copies) {
326310
cvi->setOperand(context.borrowee);
327311
context.modifiedCopyValueInsts.push_back(cvi);

test/SILOptimizer/shrink_borrow_scope.sil

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,30 @@ exit(%value : @owned $C):
953953
return %retval : $()
954954
}
955955

956+
// Don't hoist over begin_borrows of copies.
957+
// CHECK-LABEL: sil [ossa] @dont_hoist_over_begin_borrow_copy : {{.*}} {
958+
// CHECK: begin_borrow
959+
// CHECK: copy_value
960+
// CHECK: begin_borrow
961+
// CHECK: end_borrow
962+
// CHECK-LABEL: } // end sil function 'dont_hoist_over_begin_borrow_copy'
963+
sil [ossa] @dont_hoist_over_begin_borrow_copy : $@convention(thin) (@owned C) -> (@owned C) {
964+
entry(%owned_in : @owned $C):
965+
%borrow_1 = begin_borrow [lexical] %owned_in : $C
966+
apply undef(%borrow_1) : $@convention(thin) (@guaranteed C) -> ()
967+
%copy_1_1 = copy_value %borrow_1 : $C
968+
%borrow_out = begin_borrow [lexical] %copy_1_1 : $C
969+
%copy_2 = copy_value %borrow_out : $C
970+
end_borrow %borrow_1 : $C
971+
destroy_value %owned_in : $C
972+
apply undef(%borrow_out) : $@convention(thin) (@guaranteed C) -> ()
973+
br exit(%copy_1_1 : $C, %borrow_out : $C, %copy_2 : $C)
974+
exit(%copy_1_2 : @owned $C, %borrow_in : @guaranteed $C, %copy_2_2 : @owned $C):
975+
end_borrow %borrow_in : $C
976+
destroy_value %copy_1_2 : $C
977+
return %copy_2_2 : $C
978+
}
979+
956980
// =============================================================================
957981
// instruction tests }}
958982
// =============================================================================

0 commit comments

Comments
 (0)