@@ -426,25 +426,6 @@ void CallSiteDescriptor::extendArgumentLifetime(SILValue Arg) const {
426
426
}
427
427
}
428
428
429
- static void specializeClosure (ClosureInfo &CInfo,
430
- CallSiteDescriptor &CallDesc) {
431
- auto NewFName = CallDesc.createName ();
432
- DEBUG (llvm::dbgs () << " Perform optimizations with new name " << NewFName
433
- << ' \n ' );
434
-
435
- // Then see if we already have a specialized version of this function in our
436
- // module.
437
- SILFunction *NewF = CInfo.Closure ->getModule ().lookUpFunction (NewFName);
438
-
439
- // If not, create a specialized version of ApplyCallee calling the closure
440
- // directly.
441
- if (!NewF)
442
- NewF = ClosureSpecCloner::cloneFunction (CallDesc, NewFName);
443
-
444
- // Rewrite the call
445
- rewriteApplyInst (CallDesc, NewF);
446
- }
447
-
448
429
static bool isSupportedClosure (const SILInstruction *Closure) {
449
430
if (!isSupportedClosureKind (Closure))
450
431
return false ;
@@ -700,7 +681,7 @@ class ClosureSpecializer {
700
681
void gatherCallSites (SILFunction *Caller,
701
682
llvm::SmallVectorImpl<ClosureInfo*> &ClosureCandidates,
702
683
llvm::DenseSet<FullApplySite> &MultipleClosureAI);
703
- bool specialize (SILFunction *Caller);
684
+ bool specialize (SILFunction *Caller, SILFunctionTransform *SFT );
704
685
705
686
ArrayRef<SILInstruction *> getPropagatedClosures () {
706
687
if (IsPropagatedClosuresUniqued)
@@ -831,7 +812,8 @@ void ClosureSpecializer::gatherCallSites(
831
812
}
832
813
}
833
814
834
- bool ClosureSpecializer::specialize (SILFunction *Caller) {
815
+ bool ClosureSpecializer::specialize (SILFunction *Caller,
816
+ SILFunctionTransform *SFT) {
835
817
DEBUG (llvm::dbgs () << " Optimizing callsites that take closure argument in "
836
818
<< Caller->getName () << ' \n ' );
837
819
@@ -849,7 +831,24 @@ bool ClosureSpecializer::specialize(SILFunction *Caller) {
849
831
if (MultipleClosureAI.count (CSDesc.getApplyInst ()))
850
832
continue ;
851
833
852
- specializeClosure (*CInfo, CSDesc);
834
+ auto NewFName = CSDesc.createName ();
835
+ DEBUG (llvm::dbgs () << " Perform optimizations with new name "
836
+ << NewFName << ' \n ' );
837
+
838
+ // Then see if we already have a specialized version of this function in
839
+ // our module.
840
+ SILFunction *NewF = CInfo->Closure ->getModule ().lookUpFunction (NewFName);
841
+
842
+ // If not, create a specialized version of ApplyCallee calling the closure
843
+ // directly.
844
+ if (!NewF) {
845
+ NewF = ClosureSpecCloner::cloneFunction (CSDesc, NewFName);
846
+ SFT->notifyPassManagerOfFunction (NewF);
847
+ }
848
+
849
+ // Rewrite the call
850
+ rewriteApplyInst (CSDesc, NewF);
851
+
853
852
PropagatedClosures.push_back (CSDesc.getClosure ());
854
853
Changed = true ;
855
854
}
@@ -864,57 +863,48 @@ bool ClosureSpecializer::specialize(SILFunction *Caller) {
864
863
865
864
namespace {
866
865
867
- class SILClosureSpecializerTransform : public SILModuleTransform {
866
+ class SILClosureSpecializerTransform : public SILFunctionTransform {
868
867
public:
869
868
SILClosureSpecializerTransform () {}
870
869
871
870
void run () override {
872
- auto *BCA = getAnalysis<BasicCalleeAnalysis> ();
871
+ SILFunction *F = getFunction ();
873
872
874
- bool Changed = false ;
875
- ClosureSpecializer C;
876
-
877
- BottomUpFunctionOrder Ordering (*getModule (), BCA);
878
-
879
- // Specialize going bottom-up.
880
- for (auto *F : Ordering.getFunctions ()) {
881
- // Don't optimize functions that are marked with the opt.never
882
- // attribute.
883
- if (!F->shouldOptimize ())
884
- return ;
885
-
886
- // If F is an external declaration, there is nothing to specialize.
887
- if (F->isExternalDeclaration ())
888
- continue ;
873
+ // Don't optimize functions that are marked with the opt.never
874
+ // attribute.
875
+ if (!F->shouldOptimize ())
876
+ return ;
889
877
890
- Changed |= C.specialize (F);
891
- }
878
+ // If F is an external declaration, there is nothing to specialize.
879
+ if (F->isExternalDeclaration ())
880
+ return ;
892
881
893
- // Invalidate everything since we delete calls as well as add new
894
- // calls and branches.
895
- if (Changed) {
896
- invalidateAnalysis (SILAnalysis::InvalidationKind::Everything);
897
- }
882
+ ClosureSpecializer C;
883
+ if (!C.specialize (F, this ))
884
+ return ;
898
885
899
886
// If for testing purposes we were asked to not eliminate dead closures,
900
887
// return.
901
- if (!EliminateDeadClosures)
902
- return ;
888
+ if (EliminateDeadClosures) {
889
+ // Otherwise, remove any local dead closures that are now dead since we
890
+ // specialized all of their uses.
891
+ DEBUG (llvm::dbgs () << " Trying to remove dead closures!\n " );
892
+ for (SILInstruction *Closure : C.getPropagatedClosures ()) {
893
+ DEBUG (llvm::dbgs () << " Visiting: " << *Closure);
894
+ if (!tryDeleteDeadClosure (Closure)) {
895
+ DEBUG (llvm::dbgs () << " Failed to delete closure!\n " );
896
+ NumPropagatedClosuresNotEliminated++;
897
+ continue ;
898
+ }
903
899
904
- // Otherwise, remove any local dead closures that are now dead since we
905
- // specialized all of their uses.
906
- DEBUG (llvm::dbgs () << " Trying to remove dead closures!\n " );
907
- for (SILInstruction *Closure : C.getPropagatedClosures ()) {
908
- DEBUG (llvm::dbgs () << " Visiting: " << *Closure);
909
- if (!tryDeleteDeadClosure (Closure)) {
910
- DEBUG (llvm::dbgs () << " Failed to delete closure!\n " );
911
- NumPropagatedClosuresNotEliminated++;
912
- continue ;
900
+ DEBUG (llvm::dbgs () << " Deleted closure!\n " );
901
+ ++NumPropagatedClosuresEliminated;
913
902
}
914
-
915
- DEBUG (llvm::dbgs () << " Deleted closure!\n " );
916
- ++NumPropagatedClosuresEliminated;
917
903
}
904
+
905
+ // Invalidate everything since we delete calls as well as add new
906
+ // calls and branches.
907
+ invalidateAnalysis (SILAnalysis::InvalidationKind::Everything);
918
908
}
919
909
920
910
StringRef getName () override { return " Closure Specialization" ; }
0 commit comments