@@ -674,34 +674,64 @@ void ClosureSpecCloner::populateCloned() {
674
674
675
675
namespace {
676
676
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 {
686
678
void gatherCallSites (SILFunction *Caller,
687
679
llvm::SmallVectorImpl<ClosureInfo*> &ClosureCandidates,
688
680
llvm::DenseSet<FullApplySite> &MultipleClosureAI);
689
- bool specialize (SILFunction *Caller, SILFunctionTransform *SFT);
681
+ bool specialize (SILFunction *Caller,
682
+ std::vector<SILInstruction *> &PropagatedClosures);
690
683
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
+ };
694
691
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 " );
695
715
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
+ }
697
723
698
- return PropagatedClosures;
724
+ DEBUG (llvm::dbgs () << " Deleted closure!\n " );
725
+ ++NumPropagatedClosuresEliminated;
726
+ }
699
727
}
700
- };
701
728
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
+ }
703
733
704
- void ClosureSpecializer ::gatherCallSites (
734
+ void SILClosureSpecializerTransform ::gatherCallSites (
705
735
SILFunction *Caller,
706
736
llvm::SmallVectorImpl<ClosureInfo*> &ClosureCandidates,
707
737
llvm::DenseSet<FullApplySite> &MultipleClosureAI) {
@@ -824,8 +854,8 @@ void ClosureSpecializer::gatherCallSites(
824
854
}
825
855
}
826
856
827
- bool ClosureSpecializer ::specialize (SILFunction *Caller,
828
- SILFunctionTransform *SFT ) {
857
+ bool SILClosureSpecializerTransform ::specialize (SILFunction *Caller,
858
+ std::vector<SILInstruction *> &PropagatedClosures ) {
829
859
DEBUG (llvm::dbgs () << " Optimizing callsites that take closure argument in "
830
860
<< Caller->getName () << ' \n ' );
831
861
@@ -855,7 +885,7 @@ bool ClosureSpecializer::specialize(SILFunction *Caller,
855
885
// directly.
856
886
if (!NewF) {
857
887
NewF = ClosureSpecCloner::cloneFunction (CSDesc, NewFName);
858
- SFT-> notifyPassManagerOfFunction (NewF, CSDesc.getApplyCallee ());
888
+ notifyPassManagerOfFunction (NewF, CSDesc.getApplyCallee ());
859
889
}
860
890
861
891
// Rewrite the call
@@ -869,62 +899,8 @@ bool ClosureSpecializer::specialize(SILFunction *Caller,
869
899
return Changed;
870
900
}
871
901
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
-
925
902
} // end anonymous namespace
926
903
927
-
928
904
SILTransform *swift::createClosureSpecializer () {
929
905
return new SILClosureSpecializerTransform ();
930
906
}
0 commit comments