This is an archive of the discontinued LLVM Phabricator instance.

[llvm][Inline] Add interface to return cost-benefit stuff

Authored by taolq on Jul 2 2021, 7:43 AM.



Return cost-benefit stuff which is computed by cost-benefit analysis.

Diff Detail

Event Timeline

taolq created this revision.Jul 2 2021, 7:43 AM
taolq requested review of this revision.Jul 2 2021, 7:43 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 2 2021, 7:43 AM
taolq edited the summary of this revision. (Show Details)Jul 2 2021, 7:46 AM
taolq added reviewers: kazu, mtrofin, teemperor.
mtrofin added inline comments.Jul 2 2021, 8:46 AM

why do you need the 2 of them? (I don't see a user for either... well, ok, the second calls the first, but other than that)

I think it would be good to hook up to where you want to use this. In particular, if the user will always want the InlineCost, too, it wouldn't be ideal if the callsite analysis happened twice (once for InlineCost, once for the cost benefit pair). So one way you could expose the cost/benefit pair is by having it as a field in the InlineCost; at that point, too, you could either just have the 'cost' and 'benefit' as fields of InlineCost, or see if the struct design I mention below makes more sense.


Nit: the problem with std::pair where both elements are of the same type is that it's not clear to the reader which is which, without reading comments - which is added work (also comments go out of sync eventually).

Alternative: define a little struct with named fields, makes it all crystal-clear; additionally, when using an IDE, autocompletion makes it very easily discoverable.

Even better alternative, from maintainability perspective: define the struct, and make the fields const and set them through the ctor. So then a reader understands that the object, once constructed, won't mutate, so it removes a concern from understanding the code (basically, the values could only have come from when it was constructed, so less code to chase around)

taolq updated this revision to Diff 357238.Jul 8 2021, 8:49 AM
  • add struct to represent cost-benefit pair
  • remove redundant interface to return cost-benefit pair
mtrofin added inline comments.Jul 8 2021, 9:46 AM

const, both here and below.

taolq added inline comments.Jul 8 2021, 5:02 PM

I have been trying to use const, but it still doesn't go well. It still needs an assignment operation.
In ReplayInlineAdvisor::getAdviceImpl

Optional<InlineCost> InlineRecommended = None;
if (Iter != InlineSitesFromRemarks.end()) {
  InlineRecommended = llvm::InlineCost::getAlways("found in replay");

By the way, it is always passed by value, just like the Cost or Threshold.
So I think it is safe to use w/o const, right?

mtrofin added inline comments.Jul 8 2021, 6:37 PM

It's not about safety, it's readability and maintainability: avoiding folks later having to chase down fields to figure out where they are set.

A way to address this would be to make CostBenefitPair a class, expose the fields through const accessors, and then you can have the default assignment operator.

taolq updated this revision to Diff 357545.Jul 9 2021, 9:33 AM
taolq marked an inline comment as done.
  • use const accessors to keep readability and maintainability
mtrofin accepted this revision.Jul 9 2021, 11:39 AM

lgtm, one comment


you can return const APInt &, I think APInt is a bit larger than a pointer

This revision is now accepted and ready to land.Jul 9 2021, 11:39 AM
taolq updated this revision to Diff 357697.Jul 9 2021, 11:53 PM
taolq marked an inline comment as done.
  • return const APInt&
This revision was automatically updated to reflect the committed changes.