Skip to content

Convert 2 module passes to function passes #4372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions lib/SILOptimizer/IPO/CapturePropagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ STATISTIC(NumCapturesPropagated, "Number of constant captures propagated");
namespace {
/// Propagate constants through closure captures by specializing the partially
/// applied function.
class CapturePropagation : public SILModuleTransform
class CapturePropagation : public SILFunctionTransform
{
public:
void run() override;
Expand Down Expand Up @@ -237,7 +237,7 @@ SILFunction *CapturePropagation::specializeConstClosure(PartialApplyInst *PAI,
CanSILFunctionType NewFTy =
Lowering::adjustFunctionType(PAI->getType().castTo<SILFunctionType>(),
SILFunctionType::Representation::Thin);
SILFunction *NewF = getModule()->createFunction(
SILFunction *NewF = OrigF->getModule().createFunction(
SILLinkage::Shared, Name, NewFTy,
/*contextGenericParams*/ nullptr, OrigF->getLocation(), OrigF->isBare(),
OrigF->isTransparent(), Fragile, OrigF->isThunk(),
Expand Down Expand Up @@ -292,18 +292,17 @@ bool CapturePropagation::optimizePartialApply(PartialApplyInst *PAI) {
if (PAI->hasSubstitutions())
return false;

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

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

for (auto Arg : PAI->getArguments()) {
if (!isConstant(Arg))
return false;
}
SILFunction *SubstF = FRI->getReferencedFunction();
if (SubstF->isExternalDeclaration() || !isProfitable(SubstF))
return false;

Expand All @@ -312,34 +311,34 @@ bool CapturePropagation::optimizePartialApply(PartialApplyInst *PAI) {
++NumCapturesPropagated;
SILFunction *NewF = specializeConstClosure(PAI, SubstF);
rewritePartialApply(PAI, NewF);

notifyPassManagerOfFunction(NewF);
return true;
}

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

// Don't optimize functions that are marked with the opt.never attribute.
if (!F.shouldOptimize())
// Don't optimize functions that are marked with the opt.never attribute.
if (!F->shouldOptimize())
return;

// Cache cold blocks per function.
ColdBlockInfo ColdBlocks(DA);
for (auto &BB : *F) {
if (ColdBlocks.isCold(&BB))
continue;

// Cache cold blocks per function.
ColdBlockInfo ColdBlocks(DA);
for (auto &BB : F) {
if (ColdBlocks.isCold(&BB))
continue;

auto I = BB.begin();
while (I != BB.end()) {
SILInstruction *Inst = &*I;
++I;
if (PartialApplyInst *PAI = dyn_cast<PartialApplyInst>(Inst))
HasChanged |= optimizePartialApply(PAI);
}
auto I = BB.begin();
while (I != BB.end()) {
SILInstruction *Inst = &*I;
++I;
if (PartialApplyInst *PAI = dyn_cast<PartialApplyInst>(Inst))
HasChanged |= optimizePartialApply(PAI);
}
}

if (HasChanged) {
invalidateAnalysis(SILAnalysis::InvalidationKind::Everything);
}
Expand Down
110 changes: 50 additions & 60 deletions lib/SILOptimizer/IPO/ClosureSpecializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,25 +426,6 @@ void CallSiteDescriptor::extendArgumentLifetime(SILValue Arg) const {
}
}

static void specializeClosure(ClosureInfo &CInfo,
CallSiteDescriptor &CallDesc) {
auto NewFName = CallDesc.createName();
DEBUG(llvm::dbgs() << " Perform optimizations with new name " << NewFName
<< '\n');

// Then see if we already have a specialized version of this function in our
// module.
SILFunction *NewF = CInfo.Closure->getModule().lookUpFunction(NewFName);

// If not, create a specialized version of ApplyCallee calling the closure
// directly.
if (!NewF)
NewF = ClosureSpecCloner::cloneFunction(CallDesc, NewFName);

// Rewrite the call
rewriteApplyInst(CallDesc, NewF);
}

static bool isSupportedClosure(const SILInstruction *Closure) {
if (!isSupportedClosureKind(Closure))
return false;
Expand Down Expand Up @@ -700,7 +681,7 @@ class ClosureSpecializer {
void gatherCallSites(SILFunction *Caller,
llvm::SmallVectorImpl<ClosureInfo*> &ClosureCandidates,
llvm::DenseSet<FullApplySite> &MultipleClosureAI);
bool specialize(SILFunction *Caller);
bool specialize(SILFunction *Caller, SILFunctionTransform *SFT);

ArrayRef<SILInstruction *> getPropagatedClosures() {
if (IsPropagatedClosuresUniqued)
Expand Down Expand Up @@ -831,7 +812,8 @@ void ClosureSpecializer::gatherCallSites(
}
}

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

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

specializeClosure(*CInfo, CSDesc);
auto NewFName = CSDesc.createName();
DEBUG(llvm::dbgs() << " Perform optimizations with new name "
<< NewFName << '\n');

// Then see if we already have a specialized version of this function in
// our module.
SILFunction *NewF = CInfo->Closure->getModule().lookUpFunction(NewFName);

// If not, create a specialized version of ApplyCallee calling the closure
// directly.
if (!NewF) {
NewF = ClosureSpecCloner::cloneFunction(CSDesc, NewFName);
SFT->notifyPassManagerOfFunction(NewF);
}

// Rewrite the call
rewriteApplyInst(CSDesc, NewF);

PropagatedClosures.push_back(CSDesc.getClosure());
Changed = true;
}
Expand All @@ -864,57 +863,48 @@ bool ClosureSpecializer::specialize(SILFunction *Caller) {

namespace {

class SILClosureSpecializerTransform : public SILModuleTransform {
class SILClosureSpecializerTransform : public SILFunctionTransform {
public:
SILClosureSpecializerTransform() {}

void run() override {
auto *BCA = getAnalysis<BasicCalleeAnalysis>();
SILFunction *F = getFunction();

bool Changed = false;
ClosureSpecializer C;

BottomUpFunctionOrder Ordering(*getModule(), BCA);

// Specialize going bottom-up.
for (auto *F : Ordering.getFunctions()) {
// Don't optimize functions that are marked with the opt.never
// attribute.
if (!F->shouldOptimize())
return;

// If F is an external declaration, there is nothing to specialize.
if (F->isExternalDeclaration())
continue;
// Don't optimize functions that are marked with the opt.never
// attribute.
if (!F->shouldOptimize())
return;

Changed |= C.specialize(F);
}
// If F is an external declaration, there is nothing to specialize.
if (F->isExternalDeclaration())
return;

// Invalidate everything since we delete calls as well as add new
// calls and branches.
if (Changed) {
invalidateAnalysis(SILAnalysis::InvalidationKind::Everything);
}
ClosureSpecializer C;
if (!C.specialize(F, this))
return;

// If for testing purposes we were asked to not eliminate dead closures,
// return.
if (!EliminateDeadClosures)
return;
if (EliminateDeadClosures) {
// Otherwise, remove any local dead closures that are now dead since we
// specialized all of their uses.
DEBUG(llvm::dbgs() << "Trying to remove dead closures!\n");
for (SILInstruction *Closure : C.getPropagatedClosures()) {
DEBUG(llvm::dbgs() << " Visiting: " << *Closure);
if (!tryDeleteDeadClosure(Closure)) {
DEBUG(llvm::dbgs() << " Failed to delete closure!\n");
NumPropagatedClosuresNotEliminated++;
continue;
}

// Otherwise, remove any local dead closures that are now dead since we
// specialized all of their uses.
DEBUG(llvm::dbgs() << "Trying to remove dead closures!\n");
for (SILInstruction *Closure : C.getPropagatedClosures()) {
DEBUG(llvm::dbgs() << " Visiting: " << *Closure);
if (!tryDeleteDeadClosure(Closure)) {
DEBUG(llvm::dbgs() << " Failed to delete closure!\n");
NumPropagatedClosuresNotEliminated++;
continue;
DEBUG(llvm::dbgs() << " Deleted closure!\n");
++NumPropagatedClosuresEliminated;
}

DEBUG(llvm::dbgs() << " Deleted closure!\n");
++NumPropagatedClosuresEliminated;
}

// Invalidate everything since we delete calls as well as add new
// calls and branches.
invalidateAnalysis(SILAnalysis::InvalidationKind::Everything);
}

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