This is an archive of the discontinued LLVM Phabricator instance.

[InlineCost] Make cost-benefit decision explicit
ClosedPublic

Authored by wenlei on Mar 24 2021, 2:43 PM.

Details

Summary

With cost-benefit analysis for inlining, we bypass the cost-threshold by returning inline result from call analyzer early.

However the cost and threshold are still available from call analyzer, and when cost is actually higher than threshold, we incorrect set the reason.

The change makes the decision from cost-benefit analysis explicit. It's mostly NFC, except that it allows the priority-based sample loader inliner used by CSSPGO to use cost-benefit heuristic.

Diff Detail

Event Timeline

wenlei created this revision.Mar 24 2021, 2:43 PM
wenlei requested review of this revision.Mar 24 2021, 2:43 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 24 2021, 2:43 PM
davidxl added inline comments.Mar 24 2021, 2:59 PM
llvm/lib/Analysis/InlineCost.cpp
2635

Should this be folded into line 2622?

wenlei added inline comments.Mar 24 2021, 3:19 PM
llvm/lib/Analysis/InlineCost.cpp
2635

We wanted any positive cost-benefit decision to be distinguishable from a regular cost-threshold decision. This is because sample loader inlining looks at cost, and compare against its own threshold (SampleProfileLoader::shouldInlineCandidate). But we want cost-benefit decision to take precedence there too. I can give the three flat if some structure..

Perhaps it'd be good to make negative cost-benefit decision distinguishable too, by using never. An alternative is to add a field in InlineCost indicating a decision is made through cost-benefit analysis.

davidxl added inline comments.Mar 24 2021, 3:21 PM
llvm/lib/Analysis/InlineCost.cpp
2635

what I meant is re-organize your change a little:

if (ShouldInline.isSuccess()) {

if (CA.wasDecidedByCostBenefit() {
 ...
} 
if (CA.getCost() >= CA.getThreshold() ) {
}

}

wenlei added inline comments.Mar 24 2021, 3:28 PM
llvm/lib/Analysis/InlineCost.cpp
2635

How about this above the existing checks. On 2nd thought, I think it makes sense to make negative cost-benefit decision explicit too.

// Always make cost benefit based decision explicit.
// We use always/never here since threshold is not meaningful,
// as it's not what drives cost-benefit analysis.
if (CA.wasDecidedByCostBenefit()) {
  if (ShouldInline.isSuccess())
    return InlineCost::getAlways("benefit over cost");
  else
    return InlineCost::getNever("cost over benefit");
}
davidxl added inline comments.Mar 24 2021, 3:42 PM
llvm/lib/Analysis/InlineCost.cpp
2635

Sounds good.

wenlei updated this revision to Diff 333160.Mar 24 2021, 3:44 PM

Address David's feedback, also make negative decision explicit.

davidxl accepted this revision.Mar 24 2021, 3:46 PM

lgtm

This revision is now accepted and ready to land.Mar 24 2021, 3:46 PM
wenlei updated this revision to Diff 333165.Mar 24 2021, 4:10 PM

Rebase, update test.

This revision was landed with ongoing or failed builds.Mar 24 2021, 4:11 PM
This revision was automatically updated to reflect the committed changes.