Skip to content

Commit aa2e7f6

Browse files
authored
Merge pull request #4372 from eeckstein/function-passes
Convert 2 module passes to function passes
2 parents 7186858 + 87015f2 commit aa2e7f6

File tree

2 files changed

+72
-83
lines changed

2 files changed

+72
-83
lines changed

lib/SILOptimizer/IPO/CapturePropagation.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ STATISTIC(NumCapturesPropagated, "Number of constant captures propagated");
3131
namespace {
3232
/// Propagate constants through closure captures by specializing the partially
3333
/// applied function.
34-
class CapturePropagation : public SILModuleTransform
34+
class CapturePropagation : public SILFunctionTransform
3535
{
3636
public:
3737
void run() override;
@@ -237,7 +237,7 @@ SILFunction *CapturePropagation::specializeConstClosure(PartialApplyInst *PAI,
237237
CanSILFunctionType NewFTy =
238238
Lowering::adjustFunctionType(PAI->getType().castTo<SILFunctionType>(),
239239
SILFunctionType::Representation::Thin);
240-
SILFunction *NewF = getModule()->createFunction(
240+
SILFunction *NewF = OrigF->getModule().createFunction(
241241
SILLinkage::Shared, Name, NewFTy,
242242
/*contextGenericParams*/ nullptr, OrigF->getLocation(), OrigF->isBare(),
243243
OrigF->isTransparent(), Fragile, OrigF->isThunk(),
@@ -292,18 +292,17 @@ bool CapturePropagation::optimizePartialApply(PartialApplyInst *PAI) {
292292
if (PAI->hasSubstitutions())
293293
return false;
294294

295-
auto *FRI = dyn_cast<FunctionRefInst>(PAI->getCallee());
296-
if (!FRI)
295+
SILFunction *SubstF = PAI->getReferencedFunction();
296+
if (!SubstF)
297297
return false;
298298

299-
assert(!FRI->getFunctionType()->isPolymorphic() &&
299+
assert(!SubstF->getLoweredFunctionType()->isPolymorphic() &&
300300
"cannot specialize generic partial apply");
301301

302302
for (auto Arg : PAI->getArguments()) {
303303
if (!isConstant(Arg))
304304
return false;
305305
}
306-
SILFunction *SubstF = FRI->getReferencedFunction();
307306
if (SubstF->isExternalDeclaration() || !isProfitable(SubstF))
308307
return false;
309308

@@ -312,34 +311,34 @@ bool CapturePropagation::optimizePartialApply(PartialApplyInst *PAI) {
312311
++NumCapturesPropagated;
313312
SILFunction *NewF = specializeConstClosure(PAI, SubstF);
314313
rewritePartialApply(PAI, NewF);
314+
315+
notifyPassManagerOfFunction(NewF);
315316
return true;
316317
}
317318

318319
void CapturePropagation::run() {
319320
DominanceAnalysis *DA = PM->getAnalysis<DominanceAnalysis>();
321+
auto *F = getFunction();
320322
bool HasChanged = false;
321-
for (auto &F : *getModule()) {
322323

323-
// Don't optimize functions that are marked with the opt.never attribute.
324-
if (!F.shouldOptimize())
324+
// Don't optimize functions that are marked with the opt.never attribute.
325+
if (!F->shouldOptimize())
326+
return;
327+
328+
// Cache cold blocks per function.
329+
ColdBlockInfo ColdBlocks(DA);
330+
for (auto &BB : *F) {
331+
if (ColdBlocks.isCold(&BB))
325332
continue;
326333

327-
// Cache cold blocks per function.
328-
ColdBlockInfo ColdBlocks(DA);
329-
for (auto &BB : F) {
330-
if (ColdBlocks.isCold(&BB))
331-
continue;
332-
333-
auto I = BB.begin();
334-
while (I != BB.end()) {
335-
SILInstruction *Inst = &*I;
336-
++I;
337-
if (PartialApplyInst *PAI = dyn_cast<PartialApplyInst>(Inst))
338-
HasChanged |= optimizePartialApply(PAI);
339-
}
334+
auto I = BB.begin();
335+
while (I != BB.end()) {
336+
SILInstruction *Inst = &*I;
337+
++I;
338+
if (PartialApplyInst *PAI = dyn_cast<PartialApplyInst>(Inst))
339+
HasChanged |= optimizePartialApply(PAI);
340340
}
341341
}
342-
343342
if (HasChanged) {
344343
invalidateAnalysis(SILAnalysis::InvalidationKind::Everything);
345344
}

lib/SILOptimizer/IPO/ClosureSpecializer.cpp

Lines changed: 50 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -426,25 +426,6 @@ void CallSiteDescriptor::extendArgumentLifetime(SILValue Arg) const {
426426
}
427427
}
428428

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-
448429
static bool isSupportedClosure(const SILInstruction *Closure) {
449430
if (!isSupportedClosureKind(Closure))
450431
return false;
@@ -700,7 +681,7 @@ class ClosureSpecializer {
700681
void gatherCallSites(SILFunction *Caller,
701682
llvm::SmallVectorImpl<ClosureInfo*> &ClosureCandidates,
702683
llvm::DenseSet<FullApplySite> &MultipleClosureAI);
703-
bool specialize(SILFunction *Caller);
684+
bool specialize(SILFunction *Caller, SILFunctionTransform *SFT);
704685

705686
ArrayRef<SILInstruction *> getPropagatedClosures() {
706687
if (IsPropagatedClosuresUniqued)
@@ -831,7 +812,8 @@ void ClosureSpecializer::gatherCallSites(
831812
}
832813
}
833814

834-
bool ClosureSpecializer::specialize(SILFunction *Caller) {
815+
bool ClosureSpecializer::specialize(SILFunction *Caller,
816+
SILFunctionTransform *SFT) {
835817
DEBUG(llvm::dbgs() << "Optimizing callsites that take closure argument in "
836818
<< Caller->getName() << '\n');
837819

@@ -849,7 +831,24 @@ bool ClosureSpecializer::specialize(SILFunction *Caller) {
849831
if (MultipleClosureAI.count(CSDesc.getApplyInst()))
850832
continue;
851833

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+
853852
PropagatedClosures.push_back(CSDesc.getClosure());
854853
Changed = true;
855854
}
@@ -864,57 +863,48 @@ bool ClosureSpecializer::specialize(SILFunction *Caller) {
864863

865864
namespace {
866865

867-
class SILClosureSpecializerTransform : public SILModuleTransform {
866+
class SILClosureSpecializerTransform : public SILFunctionTransform {
868867
public:
869868
SILClosureSpecializerTransform() {}
870869

871870
void run() override {
872-
auto *BCA = getAnalysis<BasicCalleeAnalysis>();
871+
SILFunction *F = getFunction();
873872

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;
889877

890-
Changed |= C.specialize(F);
891-
}
878+
// If F is an external declaration, there is nothing to specialize.
879+
if (F->isExternalDeclaration())
880+
return;
892881

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;
898885

899886
// If for testing purposes we were asked to not eliminate dead closures,
900887
// 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+
}
903899

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;
913902
}
914-
915-
DEBUG(llvm::dbgs() << " Deleted closure!\n");
916-
++NumPropagatedClosuresEliminated;
917903
}
904+
905+
// Invalidate everything since we delete calls as well as add new
906+
// calls and branches.
907+
invalidateAnalysis(SILAnalysis::InvalidationKind::Everything);
918908
}
919909

920910
StringRef getName() override { return "Closure Specialization"; }

0 commit comments

Comments
 (0)