Revert "AllocBoxToStack: Remove bodies of closure functions left unused after specialization."

This reverts commit bd5f0a7af0.
This commit is contained in:
Joe Groff
2023-07-10 12:43:21 -07:00
parent b1f36d2c6d
commit 8d974212f1
3 changed files with 8 additions and 56 deletions

View File

@@ -1213,51 +1213,9 @@ static void rewriteApplySites(AllocBoxToStackState &pass) {
auto *FRI = cast<FunctionRefInst>(Apply.getCallee());
Apply.getInstruction()->eraseFromParent();
if (FRI->use_empty()) {
auto referencedFn = FRI->getReferencedFunction();
// TODO: Erase from module if there are no more uses.
if (FRI->use_empty())
FRI->eraseFromParent();
// TODO: Erase from module if there are no more uses.
// If the function has no remaining references, it should eventually
// be deleted. We can't do that from a function pass, since the function
// is still queued up for other passes to run after this one, but we
// can at least gut the implementation, since subsequent passes that
// rely on stack promotion to occur (particularly closure lifetime
// fixup and move-only checking) may not be able to proceed in a
// sensible way on the now non-canonical original implementation.
if (referencedFn->getRefCount() == 0
&& !isPossiblyUsedExternally(referencedFn->getLinkage(),
referencedFn->getModule().isWholeModule())) {
LLVM_DEBUG(llvm::dbgs() << "*** Deleting original function " << referencedFn->getName() << "'s body since it is unused");
// Remove all non-entry blocks.
auto entryBB = referencedFn->begin();
auto nextBB = std::next(entryBB);
while (nextBB != referencedFn->end()) {
auto thisBB = nextBB;
++nextBB;
thisBB->eraseFromParent();
}
// Rewrite the entry block to only contain an unreachable.
auto loc = entryBB->begin()->getLoc();
entryBB->eraseAllInstructions(referencedFn->getModule());
{
SILBuilder b(&*entryBB);
b.createUnreachable(loc);
}
// Refresh the CFG in case we removed any function calls.
pass.CFGChanged = true;
// If the function has shared linkage, reduce this version to private
// linkage, because we don't want the deleted-body form to win in any
// ODR shootouts.
if (referencedFn->getLinkage() == SILLinkage::Shared) {
referencedFn->setLinkage(SILLinkage::Private);
}
}
}
}
}