@@ -1437,15 +1437,49 @@ class ExtendUnconsumedLiveness {
1437
1437
1438
1438
namespace {
1439
1439
1440
+ struct CopiedLoadBorrowEliminationState {
1441
+ SILFunction *fn;
1442
+ StackList<LoadBorrowInst *> targets;
1443
+
1444
+ CopiedLoadBorrowEliminationState (SILFunction *fn) : fn(fn), targets(fn) {}
1445
+
1446
+ void process () {
1447
+ if (targets.empty ())
1448
+ return ;
1449
+
1450
+ while (!targets.empty ()) {
1451
+ auto *lbi = targets.pop_back_val ();
1452
+ SILBuilderWithScope builder (lbi);
1453
+ SILValue li = builder.emitLoadValueOperation (
1454
+ lbi->getLoc (), lbi->getOperand (), LoadOwnershipQualifier::Copy);
1455
+ SILValue borrow = builder.createBeginBorrow (lbi->getLoc (), li);
1456
+
1457
+ for (auto *ebi : lbi->getEndBorrows ()) {
1458
+ auto *next = ebi->getNextInstruction ();
1459
+ SILBuilderWithScope builder (next);
1460
+ auto loc = RegularLocation::getAutoGeneratedLocation ();
1461
+ builder.emitDestroyValueOperation (loc, li);
1462
+ }
1463
+
1464
+ lbi->replaceAllUsesWith (borrow);
1465
+ lbi->eraseFromParent ();
1466
+ }
1467
+
1468
+ LLVM_DEBUG (llvm::dbgs () << " After Load Borrow Elim. Func Dump Start! " ;
1469
+ fn->print (llvm::dbgs ()));
1470
+ LLVM_DEBUG (llvm::dbgs () << " After Load Borrow Elim. Func Dump End!\n " );
1471
+ }
1472
+ };
1473
+
1440
1474
// / An early transform that we run to convert any load_borrow that are copied
1441
1475
// / directly or that have any subelement that is copied to a load [copy]. This
1442
1476
// / lets the rest of the optimization handle these as appropriate.
1443
1477
struct CopiedLoadBorrowEliminationVisitor final
1444
1478
: public TransitiveAddressWalker {
1445
- SILFunction *fn;
1446
- StackList<LoadBorrowInst *> targets;
1479
+ CopiedLoadBorrowEliminationState &state;
1447
1480
1448
- CopiedLoadBorrowEliminationVisitor (SILFunction *fn) : fn(fn), targets(fn) {}
1481
+ CopiedLoadBorrowEliminationVisitor (CopiedLoadBorrowEliminationState &state)
1482
+ : state(state) {}
1449
1483
1450
1484
bool visitUse (Operand *op) override {
1451
1485
LLVM_DEBUG (llvm::dbgs () << " CopiedLBElim visiting " ;
@@ -1531,36 +1565,9 @@ struct CopiedLoadBorrowEliminationVisitor final
1531
1565
if (!shouldConvertToLoadCopy)
1532
1566
return true ;
1533
1567
1534
- targets.push_back (lbi);
1568
+ state. targets .push_back (lbi);
1535
1569
return true ;
1536
1570
}
1537
-
1538
- void process () {
1539
- if (targets.empty ())
1540
- return ;
1541
-
1542
- while (!targets.empty ()) {
1543
- auto *lbi = targets.pop_back_val ();
1544
- SILBuilderWithScope builder (lbi);
1545
- SILValue li = builder.emitLoadValueOperation (
1546
- lbi->getLoc (), lbi->getOperand (), LoadOwnershipQualifier::Copy);
1547
- SILValue borrow = builder.createBeginBorrow (lbi->getLoc (), li);
1548
-
1549
- for (auto *ebi : lbi->getEndBorrows ()) {
1550
- auto *next = ebi->getNextInstruction ();
1551
- SILBuilderWithScope builder (next);
1552
- auto loc = RegularLocation::getAutoGeneratedLocation ();
1553
- builder.emitDestroyValueOperation (loc, li);
1554
- }
1555
-
1556
- lbi->replaceAllUsesWith (borrow);
1557
- lbi->eraseFromParent ();
1558
- }
1559
-
1560
- LLVM_DEBUG (llvm::dbgs () << " After Load Borrow Elim. Func Dump Start! " ;
1561
- fn->print (llvm::dbgs ()));
1562
- LLVM_DEBUG (llvm::dbgs () << " After Load Borrow Elim. Func Dump End!\n " );
1563
- }
1564
1571
};
1565
1572
1566
1573
} // namespace
@@ -3134,14 +3141,15 @@ bool MoveOnlyAddressCheckerPImpl::performSingleCheck(
3134
3141
// [copy] + begin_borrow for further processing. This just eliminates a case
3135
3142
// that the checker doesn't need to know about.
3136
3143
{
3137
- CopiedLoadBorrowEliminationVisitor copiedLoadBorrowEliminator (fn);
3144
+ CopiedLoadBorrowEliminationState state (markedAddress->getFunction ());
3145
+ CopiedLoadBorrowEliminationVisitor copiedLoadBorrowEliminator (state);
3138
3146
if (AddressUseKind::Unknown ==
3139
3147
std::move (copiedLoadBorrowEliminator).walk (markedAddress)) {
3140
3148
LLVM_DEBUG (llvm::dbgs () << " Failed copied load borrow eliminator visit: "
3141
3149
<< *markedAddress);
3142
3150
return false ;
3143
3151
}
3144
- copiedLoadBorrowEliminator .process ();
3152
+ state .process ();
3145
3153
}
3146
3154
3147
3155
// Then gather all uses of our address by walking from def->uses. We use this
0 commit comments