Skip to content

Commit 8d97421

Browse files
committed
Revert "AllocBoxToStack: Remove bodies of closure functions left unused after specialization."
This reverts commit bd5f0a7.
1 parent b1f36d2 commit 8d97421

File tree

3 files changed

+8
-56
lines changed

3 files changed

+8
-56
lines changed

lib/SILOptimizer/Transforms/AllocBoxToStack.cpp

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,51 +1213,9 @@ static void rewriteApplySites(AllocBoxToStackState &pass) {
12131213
auto *FRI = cast<FunctionRefInst>(Apply.getCallee());
12141214
Apply.getInstruction()->eraseFromParent();
12151215

1216-
if (FRI->use_empty()) {
1217-
auto referencedFn = FRI->getReferencedFunction();
1216+
// TODO: Erase from module if there are no more uses.
1217+
if (FRI->use_empty())
12181218
FRI->eraseFromParent();
1219-
1220-
// TODO: Erase from module if there are no more uses.
1221-
// If the function has no remaining references, it should eventually
1222-
// be deleted. We can't do that from a function pass, since the function
1223-
// is still queued up for other passes to run after this one, but we
1224-
// can at least gut the implementation, since subsequent passes that
1225-
// rely on stack promotion to occur (particularly closure lifetime
1226-
// fixup and move-only checking) may not be able to proceed in a
1227-
// sensible way on the now non-canonical original implementation.
1228-
if (referencedFn->getRefCount() == 0
1229-
&& !isPossiblyUsedExternally(referencedFn->getLinkage(),
1230-
referencedFn->getModule().isWholeModule())) {
1231-
LLVM_DEBUG(llvm::dbgs() << "*** Deleting original function " << referencedFn->getName() << "'s body since it is unused");
1232-
// Remove all non-entry blocks.
1233-
auto entryBB = referencedFn->begin();
1234-
auto nextBB = std::next(entryBB);
1235-
1236-
while (nextBB != referencedFn->end()) {
1237-
auto thisBB = nextBB;
1238-
++nextBB;
1239-
thisBB->eraseFromParent();
1240-
}
1241-
1242-
// Rewrite the entry block to only contain an unreachable.
1243-
auto loc = entryBB->begin()->getLoc();
1244-
entryBB->eraseAllInstructions(referencedFn->getModule());
1245-
{
1246-
SILBuilder b(&*entryBB);
1247-
b.createUnreachable(loc);
1248-
}
1249-
1250-
// Refresh the CFG in case we removed any function calls.
1251-
pass.CFGChanged = true;
1252-
1253-
// If the function has shared linkage, reduce this version to private
1254-
// linkage, because we don't want the deleted-body form to win in any
1255-
// ODR shootouts.
1256-
if (referencedFn->getLinkage() == SILLinkage::Shared) {
1257-
referencedFn->setLinkage(SILLinkage::Private);
1258-
}
1259-
}
1260-
}
12611219
}
12621220
}
12631221

test/SILOptimizer/allocbox_to_stack.sil

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -651,13 +651,9 @@ bb0(%0 : $Int, %1 : $*S<T>):
651651
return %9 : $Bool
652652
}
653653

654-
// This closure body gets specialized with unboxed capture parameters, so
655-
// the original function body is stubbed out once the call sites are all
656-
// specialized.
657-
// CHECK-LABEL: sil private [_semantics "sil.optimizer.moveonly.diagnostic.ignore"] @closure1
658-
// CHECK: bb0
659-
// CHECK-NEXT: unreachable
654+
// CHECK-LABEL: sil shared [_semantics "sil.optimizer.moveonly.diagnostic.ignore"] @closure1
660655
sil shared @closure1 : $@convention(thin) <T where T : Count> (Int, @owned <τ_0_0 : Count> { var S<τ_0_0> } <T>) -> Bool {
656+
// CHECK: bb0
661657
bb0(%0 : $Int, %1 : $<τ_0_0 where τ_0_0 : Count> { var S<τ_0_0> } <T>):
662658
%2 = project_box %1 : $<τ_0_0 where τ_0_0 : Count> { var S<τ_0_0> } <T>, 0
663659
%3 = function_ref @inner : $@convention(thin) (@owned @callee_owned () -> Bool) -> Bool
@@ -668,6 +664,7 @@ bb0(%0 : $Int, %1 : $<τ_0_0 where τ_0_0 : Count> { var S<τ_0_0> } <T>):
668664
%7 = partial_apply %4<T>(%0, %5) : $@convention(thin) <τ_0_0 where τ_0_0 : Count> (Int, @owned <τ_0_0 : Count> { var S<τ_0_0> } <τ_0_0>) -> Bool
669665
%8 = apply %3(%7) : $@convention(thin) (@owned @callee_owned () -> Bool) -> Bool
670666
strong_release %1 : $<τ_0_0 where τ_0_0 : Count> { var S<τ_0_0> } <T>
667+
// CHECK: return
671668
return %8 : $Bool
672669
}
673670

test/SILOptimizer/allocbox_to_stack_ownership.sil

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -751,13 +751,9 @@ bb0(%0 : $Int, %1 : $*S<T>):
751751
return %9 : $Bool
752752
}
753753

754-
// This closure body gets specialized with unboxed capture parameters, so
755-
// the original function body is stubbed out once the call sites are all
756-
// specialized.
757-
// CHECK-LABEL: sil private [_semantics "sil.optimizer.moveonly.diagnostic.ignore"] [ossa] @closure1
758-
// CHECK: bb0
759-
// CHECK: unreachable
754+
// CHECK-LABEL: sil shared [_semantics "sil.optimizer.moveonly.diagnostic.ignore"] [ossa] @closure1
760755
sil shared [ossa] @closure1 : $@convention(thin) <T where T : Count> (Int, @owned <τ_0_0 : Count> { var S<τ_0_0> } <T>) -> Bool {
756+
// CHECK: bb0
761757
bb0(%0 : $Int, %1 : @owned $<τ_0_0 where τ_0_0 : Count> { var S<τ_0_0> } <T>):
762758
%2 = project_box %1 : $<τ_0_0 where τ_0_0 : Count> { var S<τ_0_0> } <T>, 0
763759
%3 = function_ref @inner : $@convention(thin) (@owned @callee_owned () -> Bool) -> Bool
@@ -768,6 +764,7 @@ bb0(%0 : $Int, %1 : @owned $<τ_0_0 where τ_0_0 : Count> { var S<τ_0_0> } <T>)
768764
%7 = partial_apply %4<T>(%0, %5) : $@convention(thin) <τ_0_0 where τ_0_0 : Count> (Int, @owned <τ_0_0 : Count> { var S<τ_0_0> } <τ_0_0>) -> Bool
769765
%8 = apply %3(%7) : $@convention(thin) (@owned @callee_owned () -> Bool) -> Bool
770766
destroy_value %1 : $<τ_0_0 where τ_0_0 : Count> { var S<τ_0_0> } <T>
767+
// CHECK: return
771768
return %8 : $Bool
772769
}
773770

0 commit comments

Comments
 (0)