Skip to content

Commit a32d2e0

Browse files
authored
Merge pull request #66909 from gottesmm/release-5.9-rdar111060475
[5.9][move-only] Remove use after move in CopiedLoadBorrowEliminationVisitor.
2 parents db82b03 + 8d70f52 commit a32d2e0

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

lib/SILOptimizer/Mandatory/MoveOnlyAddressCheckerUtils.cpp

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,15 +1437,49 @@ class ExtendUnconsumedLiveness {
14371437

14381438
namespace {
14391439

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+
14401474
/// An early transform that we run to convert any load_borrow that are copied
14411475
/// directly or that have any subelement that is copied to a load [copy]. This
14421476
/// lets the rest of the optimization handle these as appropriate.
14431477
struct CopiedLoadBorrowEliminationVisitor final
14441478
: public TransitiveAddressWalker {
1445-
SILFunction *fn;
1446-
StackList<LoadBorrowInst *> targets;
1479+
CopiedLoadBorrowEliminationState &state;
14471480

1448-
CopiedLoadBorrowEliminationVisitor(SILFunction *fn) : fn(fn), targets(fn) {}
1481+
CopiedLoadBorrowEliminationVisitor(CopiedLoadBorrowEliminationState &state)
1482+
: state(state) {}
14491483

14501484
bool visitUse(Operand *op) override {
14511485
LLVM_DEBUG(llvm::dbgs() << "CopiedLBElim visiting ";
@@ -1531,36 +1565,9 @@ struct CopiedLoadBorrowEliminationVisitor final
15311565
if (!shouldConvertToLoadCopy)
15321566
return true;
15331567

1534-
targets.push_back(lbi);
1568+
state.targets.push_back(lbi);
15351569
return true;
15361570
}
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-
}
15641571
};
15651572

15661573
} // namespace
@@ -3134,14 +3141,15 @@ bool MoveOnlyAddressCheckerPImpl::performSingleCheck(
31343141
// [copy] + begin_borrow for further processing. This just eliminates a case
31353142
// that the checker doesn't need to know about.
31363143
{
3137-
CopiedLoadBorrowEliminationVisitor copiedLoadBorrowEliminator(fn);
3144+
CopiedLoadBorrowEliminationState state(markedAddress->getFunction());
3145+
CopiedLoadBorrowEliminationVisitor copiedLoadBorrowEliminator(state);
31383146
if (AddressUseKind::Unknown ==
31393147
std::move(copiedLoadBorrowEliminator).walk(markedAddress)) {
31403148
LLVM_DEBUG(llvm::dbgs() << "Failed copied load borrow eliminator visit: "
31413149
<< *markedAddress);
31423150
return false;
31433151
}
3144-
copiedLoadBorrowEliminator.process();
3152+
state.process();
31453153
}
31463154

31473155
// Then gather all uses of our address by walking from def->uses. We use this

0 commit comments

Comments
 (0)