AllocStackToBox: fix a bug which results in a too early released captured variable

In case of a borrowed `alloc_box`, the optimization didn't look through the `begin_borrow` when calculating the final release of the box.
This resulted in inserting the destroy of the inserted `alloc_stack` too early.

rdar://97087762
This commit is contained in:
Erik Eckstein
2022-07-18 17:13:12 +02:00
parent 341bc34fe8
commit dc42c4c17d
2 changed files with 41 additions and 4 deletions

View File

@@ -53,9 +53,9 @@ static llvm::cl::opt<bool> AllocBoxToStackAnalyzeApply(
// SIL Utilities for alloc_box Promotion
//===----------------------------------------------------------------------===//
static SILValue stripOffCopyValue(SILValue V) {
while (auto *CVI = dyn_cast<CopyValueInst>(V)) {
V = CVI->getOperand();
static SILValue stripOffCopyAndBorrow(SILValue V) {
while (isa<CopyValueInst>(V) || isa<BeginBorrowInst>(V)) {
V = cast<SingleValueInstruction>(V)->getOperand(0);
}
return V;
}
@@ -127,7 +127,7 @@ static bool addLastRelease(SILValue V, SILBasicBlock *BB,
for (auto I = BB->rbegin(); I != BB->rend(); ++I) {
if (isa<StrongReleaseInst>(*I) || isa<DeallocBoxInst>(*I) ||
isa<DestroyValueInst>(*I)) {
if (stripOffCopyValue(I->getOperand(0)) != V)
if (stripOffCopyAndBorrow(I->getOperand(0)) != V)
continue;
Releases.push_back(&*I);