Skip to content

Commit 13dad3a

Browse files
committed
PerformanceInliner: tweak the inlining heuristic for callees which return an allocated object
There are examples of such functions, which were previously captured by `isPureCall` in the inliner. But the implementation of `isPureCall` was wrong. With the new (and correct) side effect analysis we need to correctly handle such functions in the inlining heuristic.
1 parent 82107c5 commit 13dad3a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

lib/SILOptimizer/Transforms/PerformanceInliner.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ bool SILPerformanceInliner::isProfitableToInline(
394394
// We will only inline if *ALL* dynamic accesses are
395395
// known and have no nested conflict
396396
bool AllAccessesBeneficialToInline = true;
397+
bool returnsAllocation = false;
397398

398399
// Calculate the inlining cost of the callee.
399400
int CalleeCost = 0;
@@ -510,6 +511,18 @@ bool SILPerformanceInliner::isProfitableToInline(
510511
AllAccessesBeneficialToInline = false;
511512
}
512513
}
514+
} else if (auto ri = dyn_cast<ReturnInst>(&I)) {
515+
SILValue retVal = ri->getOperand();
516+
if (auto *uci = dyn_cast<UpcastInst>(retVal))
517+
retVal = uci->getOperand();
518+
519+
// Inlining functions which return an allocated object or partial_apply
520+
// most likely has a benefit in the caller, because e.g. it can enable
521+
// de-virtualization.
522+
if (isa<AllocationInst>(retVal) || isa<PartialApplyInst>(retVal)) {
523+
BlockW.updateBenefit(Benefit, RemovedCallBenefit + 10);
524+
returnsAllocation = true;
525+
}
513526
}
514527
}
515528
// Don't count costs in blocks which are dead after inlining.
@@ -577,6 +590,8 @@ bool SILPerformanceInliner::isProfitableToInline(
577590

578591
if (isClassMethodAtOsize && Benefit > OSizeClassMethodBenefit) {
579592
Benefit = OSizeClassMethodBenefit;
593+
if (returnsAllocation)
594+
Benefit += 10;
580595
}
581596

582597
// This is the final inlining decision.

0 commit comments

Comments
 (0)