Skip to content

Commit ef4b5e1

Browse files
committed
ClosureSpecializer: small refactoring
Get rid of the utility class ClosureSpecializer. NFC.
1 parent 7e38792 commit ef4b5e1

File tree

1 file changed

+51
-75
lines changed

1 file changed

+51
-75
lines changed

lib/SILOptimizer/IPO/ClosureSpecializer.cpp

Lines changed: 51 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -674,34 +674,64 @@ void ClosureSpecCloner::populateCloned() {
674674

675675
namespace {
676676

677-
class ClosureSpecializer {
678-
679-
/// A vector consisting of closures that we propagated. After
680-
std::vector<SILInstruction *> PropagatedClosures;
681-
bool IsPropagatedClosuresUniqued = false;
682-
683-
public:
684-
ClosureSpecializer() = default;
685-
677+
class SILClosureSpecializerTransform : public SILFunctionTransform {
686678
void gatherCallSites(SILFunction *Caller,
687679
llvm::SmallVectorImpl<ClosureInfo*> &ClosureCandidates,
688680
llvm::DenseSet<FullApplySite> &MultipleClosureAI);
689-
bool specialize(SILFunction *Caller, SILFunctionTransform *SFT);
681+
bool specialize(SILFunction *Caller,
682+
std::vector<SILInstruction *> &PropagatedClosures);
690683

691-
ArrayRef<SILInstruction *> getPropagatedClosures() {
692-
if (IsPropagatedClosuresUniqued)
693-
return PropagatedClosures;
684+
public:
685+
SILClosureSpecializerTransform() {}
686+
687+
void run() override;
688+
689+
StringRef getName() override { return "Closure Specialization"; }
690+
};
694691

692+
void SILClosureSpecializerTransform::run() {
693+
SILFunction *F = getFunction();
694+
695+
// Don't optimize functions that are marked with the opt.never
696+
// attribute.
697+
if (!F->shouldOptimize())
698+
return;
699+
700+
// If F is an external declaration, there is nothing to specialize.
701+
if (F->isExternalDeclaration())
702+
return;
703+
704+
std::vector<SILInstruction *> PropagatedClosures;
705+
706+
if (!specialize(F, PropagatedClosures))
707+
return;
708+
709+
// If for testing purposes we were asked to not eliminate dead closures,
710+
// return.
711+
if (EliminateDeadClosures) {
712+
// Otherwise, remove any local dead closures that are now dead since we
713+
// specialized all of their uses.
714+
DEBUG(llvm::dbgs() << "Trying to remove dead closures!\n");
695715
sortUnique(PropagatedClosures);
696-
IsPropagatedClosuresUniqued = true;
716+
for (SILInstruction *Closure : PropagatedClosures) {
717+
DEBUG(llvm::dbgs() << " Visiting: " << *Closure);
718+
if (!tryDeleteDeadClosure(Closure)) {
719+
DEBUG(llvm::dbgs() << " Failed to delete closure!\n");
720+
NumPropagatedClosuresNotEliminated++;
721+
continue;
722+
}
697723

698-
return PropagatedClosures;
724+
DEBUG(llvm::dbgs() << " Deleted closure!\n");
725+
++NumPropagatedClosuresEliminated;
726+
}
699727
}
700-
};
701728

702-
} // end anonymous namespace
729+
// Invalidate everything since we delete calls as well as add new
730+
// calls and branches.
731+
invalidateAnalysis(SILAnalysis::InvalidationKind::Everything);
732+
}
703733

704-
void ClosureSpecializer::gatherCallSites(
734+
void SILClosureSpecializerTransform::gatherCallSites(
705735
SILFunction *Caller,
706736
llvm::SmallVectorImpl<ClosureInfo*> &ClosureCandidates,
707737
llvm::DenseSet<FullApplySite> &MultipleClosureAI) {
@@ -824,8 +854,8 @@ void ClosureSpecializer::gatherCallSites(
824854
}
825855
}
826856

827-
bool ClosureSpecializer::specialize(SILFunction *Caller,
828-
SILFunctionTransform *SFT) {
857+
bool SILClosureSpecializerTransform::specialize(SILFunction *Caller,
858+
std::vector<SILInstruction *> &PropagatedClosures) {
829859
DEBUG(llvm::dbgs() << "Optimizing callsites that take closure argument in "
830860
<< Caller->getName() << '\n');
831861

@@ -855,7 +885,7 @@ bool ClosureSpecializer::specialize(SILFunction *Caller,
855885
// directly.
856886
if (!NewF) {
857887
NewF = ClosureSpecCloner::cloneFunction(CSDesc, NewFName);
858-
SFT->notifyPassManagerOfFunction(NewF, CSDesc.getApplyCallee());
888+
notifyPassManagerOfFunction(NewF, CSDesc.getApplyCallee());
859889
}
860890

861891
// Rewrite the call
@@ -869,62 +899,8 @@ bool ClosureSpecializer::specialize(SILFunction *Caller,
869899
return Changed;
870900
}
871901

872-
//===----------------------------------------------------------------------===//
873-
// Top Level Code
874-
//===----------------------------------------------------------------------===//
875-
876-
namespace {
877-
878-
class SILClosureSpecializerTransform : public SILFunctionTransform {
879-
public:
880-
SILClosureSpecializerTransform() {}
881-
882-
void run() override {
883-
SILFunction *F = getFunction();
884-
885-
// Don't optimize functions that are marked with the opt.never
886-
// attribute.
887-
if (!F->shouldOptimize())
888-
return;
889-
890-
// If F is an external declaration, there is nothing to specialize.
891-
if (F->isExternalDeclaration())
892-
return;
893-
894-
ClosureSpecializer C;
895-
if (!C.specialize(F, this))
896-
return;
897-
898-
// If for testing purposes we were asked to not eliminate dead closures,
899-
// return.
900-
if (EliminateDeadClosures) {
901-
// Otherwise, remove any local dead closures that are now dead since we
902-
// specialized all of their uses.
903-
DEBUG(llvm::dbgs() << "Trying to remove dead closures!\n");
904-
for (SILInstruction *Closure : C.getPropagatedClosures()) {
905-
DEBUG(llvm::dbgs() << " Visiting: " << *Closure);
906-
if (!tryDeleteDeadClosure(Closure)) {
907-
DEBUG(llvm::dbgs() << " Failed to delete closure!\n");
908-
NumPropagatedClosuresNotEliminated++;
909-
continue;
910-
}
911-
912-
DEBUG(llvm::dbgs() << " Deleted closure!\n");
913-
++NumPropagatedClosuresEliminated;
914-
}
915-
}
916-
917-
// Invalidate everything since we delete calls as well as add new
918-
// calls and branches.
919-
invalidateAnalysis(SILAnalysis::InvalidationKind::Everything);
920-
}
921-
922-
StringRef getName() override { return "Closure Specialization"; }
923-
};
924-
925902
} // end anonymous namespace
926903

927-
928904
SILTransform *swift::createClosureSpecializer() {
929905
return new SILClosureSpecializerTransform();
930906
}

0 commit comments

Comments
 (0)