@@ -283,37 +283,33 @@ void DCE::markLive() {
283
283
break ;
284
284
}
285
285
case SILInstructionKind::BeginBorrowInst: {
286
- // Currently we only support borrows of owned values.
287
- // Nested borrow handling can be complex in the presence of reborrows.
288
- // So it is not handled currently.
289
286
auto *borrowInst = cast<BeginBorrowInst>(&I);
287
+ // Populate guaranteedPhiDependencies for this borrowInst
288
+ findGuaranteedPhiDependencies (BorrowedValue (borrowInst));
289
+ auto disableBorrowDCE = [&](SILValue borrow) {
290
+ visitTransitiveEndBorrows (borrow, [&](EndBorrowInst *endBorrow) {
291
+ markInstructionLive (endBorrow);
292
+ });
293
+ };
294
+ // If we have a begin_borrow of a @guaranteed operand, disable DCE'ing
295
+ // of parent borrow scopes. Dead reborrows needs complex handling, whuch
296
+ // is why it is disabled for now.
290
297
if (borrowInst->getOperand ()->getOwnershipKind () ==
291
298
OwnershipKind::Guaranteed) {
292
- // Visit the end_borrows of all the borrow scopes that this
293
- // begin_borrow could be borrowing.
294
299
SmallVector<SILValue, 4 > roots;
295
300
findGuaranteedReferenceRoots (borrowInst->getOperand (),
296
301
/* lookThroughNestedBorrows=*/ false ,
297
302
roots);
303
+ // Visit the end_borrows of all the borrow scopes that this
304
+ // begin_borrow could be borrowing, and mark them live.
298
305
for (auto root : roots) {
299
- visitTransitiveEndBorrows (root,
300
- [&](EndBorrowInst *endBorrow) {
301
- markInstructionLive (endBorrow);
302
- });
306
+ disableBorrowDCE (root);
303
307
}
304
- continue ;
305
308
}
306
- // Populate guaranteedPhiDependencies for this borrow
307
- findGuaranteedPhiDependencies (BorrowedValue (borrowInst));
308
- // Don't optimize a borrow scope if it is lexical or has a pointer
309
- // escape.
309
+ // If we have a lexical borrow scope or a pointer escape, disable DCE.
310
310
if (borrowInst->isLexical () ||
311
311
hasPointerEscape (BorrowedValue (borrowInst))) {
312
- // Visit all end_borrows and mark them live
313
- visitTransitiveEndBorrows (borrowInst, [&](EndBorrowInst *endBorrow) {
314
- markInstructionLive (endBorrow);
315
- });
316
- continue ;
312
+ disableBorrowDCE (borrowInst);
317
313
}
318
314
break ;
319
315
}
0 commit comments