Skip to content

Commit 670f193

Browse files
committed
Split the method isProfitableToInline.
This commit refactors the parts of isProfitableToInline that compute the cost and benefit of inlining into a separate function. NFC.
1 parent 1dec2f1 commit 670f193

File tree

1 file changed

+52
-34
lines changed

1 file changed

+52
-34
lines changed

lib/SILOptimizer/IPO/PerformanceInliner.cpp

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ namespace {
234234

235235
SILFunction *getEligibleFunction(FullApplySite AI);
236236

237+
/// Return true if inlining this call site is profitable.
237238
bool isProfitableToInline(FullApplySite AI, unsigned loopDepthOfAI,
238239
DominanceAnalysis *DA,
239240
SILLoopAnalysis *LA,
@@ -724,59 +725,55 @@ static SILBasicBlock *getTakenBlock(TermInst *term,
724725
return nullptr;
725726
}
726727

727-
/// Return true if inlining this call site is profitable.
728-
bool SILPerformanceInliner::isProfitableToInline(FullApplySite AI,
729-
unsigned loopDepthOfAI,
730-
DominanceAnalysis *DA,
731-
SILLoopAnalysis *LA,
732-
ConstantTracker &callerTracker,
733-
unsigned &NumCallerBlocks) {
734-
SILFunction *Callee = AI.getCalleeFunction();
735-
736-
if (Callee->getInlineStrategy() == AlwaysInline)
737-
return true;
738-
739-
ConstantTracker constTracker(Callee, &callerTracker, AI);
740-
728+
729+
/// Return a pair of cost/benefit integers for the callee.
730+
/// The cost is the size added to the caller, and the benefit is the
731+
/// performance gain.
732+
static std::pair<unsigned, unsigned>
733+
getCostBenefitInfo(FullApplySite CallSite,
734+
unsigned loopDepthOfAI,
735+
DominanceAnalysis *DA,
736+
SILLoopAnalysis *LA,
737+
ConstantTracker &callerTracker,
738+
unsigned InlineCostThreshold) {
739+
SILFunction *Callee = CallSite.getCalleeFunction();
740+
ConstantTracker constTracker(Callee, &callerTracker, CallSite);
741741
DominanceInfo *DT = DA->get(Callee);
742742
SILLoopInfo *LI = LA->get(Callee);
743743

744744
DominanceOrder domOrder(&Callee->front(), DT, Callee->size());
745-
745+
746746
// Calculate the inlining cost of the callee.
747-
unsigned CalleeCost = 0;
747+
unsigned Cost = 0;
748748
unsigned Benefit = InlineCostThreshold > 0 ? InlineCostThreshold :
749749
RemovedCallBenefit;
750750
Benefit += loopDepthOfAI * LoopBenefitFactor;
751-
int testThreshold = TestThreshold;
752751

753752
while (SILBasicBlock *block = domOrder.getNext()) {
754753
constTracker.beginBlock();
755754
for (SILInstruction &I : *block) {
756755
constTracker.trackInst(&I);
757-
756+
758757
auto ICost = instructionInlineCost(I);
759-
760-
if (testThreshold >= 0) {
758+
759+
if (TestThreshold >= 0) {
761760
// We are in test-mode: use a simplified cost model.
762-
CalleeCost += testCost(&I);
761+
Cost += testCost(&I);
763762
} else {
764763
// Use the regular cost model.
765-
CalleeCost += unsigned(ICost);
764+
Cost += unsigned(ICost);
766765
}
767-
766+
768767
if (ApplyInst *AI = dyn_cast<ApplyInst>(&I)) {
769-
770768
// Check if the callee is passed as an argument. If so, increase the
771769
// threshold, because inlining will (probably) eliminate the closure.
772770
SILInstruction *def = constTracker.getDefInCaller(AI->getCallee());
773771
if (def && (isa<FunctionRefInst>(def) || isa<PartialApplyInst>(def))) {
774772

775773
DEBUG(llvm::dbgs() << " Boost: apply const function at"
776-
<< *AI);
774+
<< *AI);
777775
unsigned loopDepth = LI->getLoopDepth(block);
778776
Benefit += ConstCalleeBenefit + loopDepth * LoopBenefitFactor;
779-
testThreshold *= 2;
780777
}
781778
}
782779
}
@@ -788,17 +785,38 @@ bool SILPerformanceInliner::isProfitableToInline(FullApplySite AI,
788785
DEBUG(llvm::dbgs() << " Take bb" << takenBlock->getDebugID() <<
789786
" of" << *block->getTerminator());
790787
domOrder.pushChildrenIf(block, [=] (SILBasicBlock *child) {
791-
return child->getSinglePredecessor() != block || child == takenBlock;
792-
});
788+
return child->getSinglePredecessor() != block ||
789+
child == takenBlock;
790+
});
793791
} else {
794792
domOrder.pushChildren(block);
795793
}
796794
}
797795

798-
unsigned Threshold = Benefit; // The default.
799-
if (testThreshold >= 0) {
796+
return std::make_pair(Cost, Benefit);
797+
}
798+
799+
bool SILPerformanceInliner::isProfitableToInline(FullApplySite AI,
800+
unsigned loopDepthOfAI,
801+
DominanceAnalysis *DA,
802+
SILLoopAnalysis *LA,
803+
ConstantTracker &callerTracker,
804+
unsigned &NumCallerBlocks) {
805+
SILFunction *Callee = AI.getCalleeFunction();
806+
807+
if (Callee->getInlineStrategy() == AlwaysInline)
808+
return true;
809+
810+
unsigned Cost;
811+
unsigned Benefit;
812+
std::tie(Cost, Benefit) = getCostBenefitInfo(AI, loopDepthOfAI, DA, LA,
813+
callerTracker,
814+
InlineCostThreshold);
815+
// The default.
816+
unsigned Threshold = Benefit;
817+
if (TestThreshold >= 0) {
800818
// We are in testing mode.
801-
Threshold = testThreshold;
819+
Threshold = TestThreshold;
802820
} else if (AI.getFunction()->isThunk()) {
803821
// Only inline trivial functions into thunks (which will not increase the
804822
// code size).
@@ -817,13 +835,13 @@ bool SILPerformanceInliner::isProfitableToInline(FullApplySite AI,
817835
Threshold = TrivialFunctionThreshold;
818836
}
819837

820-
if (CalleeCost > Threshold) {
838+
if (Cost > Threshold) {
821839
DEBUG(llvm::dbgs() << " NO: Function too big to inline, "
822-
"cost: " << CalleeCost << ", threshold: " << Threshold << "\n");
840+
"cost: " << Cost << ", threshold: " << Threshold << "\n");
823841
return false;
824842
}
825843
DEBUG(llvm::dbgs() << " YES: ready to inline, "
826-
"cost: " << CalleeCost << ", threshold: " << Threshold << "\n");
844+
"cost: " << Cost << ", threshold: " << Threshold << "\n");
827845
NumCallerBlocks += Callee->size();
828846
return true;
829847
}

0 commit comments

Comments
 (0)