Skip to content

Commit aaf6e97

Browse files
committed
[ClosureLifetimeFixup] Shrink arg lifetimes.
Rather than inserting dealloc_stacks at the end of blocks in the dominance frontier of the alloc_stack, walk backward in the block to the last user (if any) and insert after that (or at the beginning of the block if not). If the transitive uses aren't understood, just insert at the end of the blocks as before.
1 parent 9ad44a5 commit aaf6e97

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

lib/SILOptimizer/Utils/InstOptUtils.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,12 +1482,33 @@ void swift::insertDeallocOfCapturedArguments(PartialApplyInst *pai,
14821482
auto *asi = cast<AllocStackInst>(arg.get());
14831483
computeDominatedBoundaryBlocks(asi->getParent(), domInfo, boundary);
14841484

1485+
SmallVector<Operand *, 2> uses;
1486+
auto useFinding = findTransitiveUsesForAddress(asi, &uses);
1487+
InstructionSet users(asi->getFunction());
1488+
if (useFinding != AddressUseKind::Unknown) {
1489+
for (auto use : uses) {
1490+
users.insert(use->getUser());
1491+
}
1492+
}
1493+
14851494
for (auto *block : boundary) {
14861495
auto *terminator = block->getTerminator();
1487-
auto builder = SILBuilder(terminator);
14881496
if (isa<UnreachableInst>(terminator))
14891497
continue;
1490-
builder.createDeallocStack(CleanupLocation(terminator->getLoc()),
1498+
SILInstruction *insertionPoint = nullptr;
1499+
if (useFinding != AddressUseKind::Unknown) {
1500+
insertionPoint = &block->front();
1501+
for (auto &instruction : llvm::reverse(*block)) {
1502+
if (users.contains(&instruction)) {
1503+
insertionPoint = instruction.getNextInstruction();
1504+
break;
1505+
}
1506+
}
1507+
} else {
1508+
insertionPoint = terminator;
1509+
}
1510+
auto builder = SILBuilder(insertionPoint);
1511+
builder.createDeallocStack(CleanupLocation(insertionPoint->getLoc()),
14911512
arg.get());
14921513
}
14931514
}

0 commit comments

Comments
 (0)