This is an archive of the discontinued LLVM Phabricator instance.

[BFI] Add new LazyBFI analysis pass
ClosedPublic

Authored by anemet on Jul 8 2016, 9:04 AM.

Details

Summary

[BFI] Add new LazyBFI analysis pass

This is necessary for D21771. In order to add the hotness attribute to
optimization remarks we need BFI to be available in all passes that emit
optimization remarks.

However we don't want to pay for computing BFI unless the hotness
attribute is requested.

This is achieved by making BFI lazy at the very high-level through a new
analysis pass -- BFI is not calculated unless requested.

I am adding a test to check the laziness under D21771 where the first
user of the analysis is added.

Diff Detail

Repository
rL LLVM

Event Timeline

anemet updated this revision to Diff 63239.Jul 8 2016, 9:04 AM
anemet retitled this revision from to [BFI] Add option to lazily calculate BFI.
anemet updated this object.
anemet added reviewers: hfinkel, dexonsmith, davidxl.
anemet added a subscriber: llvm-commits.
davidxl edited edge metadata.Jul 8 2016, 12:32 PM

There are already too many classes related to BFI, so I am not a fan of introducing another LazyBFI.

I suggest just making BFI lazy itself (when getBlockFreq() is first called ...). In fact I think the laziness handling should be in BlocFrequencyInfoImpl class. By so doing, MBFI can get the laziness automatically as well.

dexonsmith edited edge metadata.Jul 8 2016, 12:36 PM
dexonsmith added a subscriber: dexonsmith.

David's suggested direction SGTM.

anemet added a comment.Jul 8 2016, 3:05 PM

There are already too many classes related to BFI, so I am not a fan of introducing another LazyBFI.

I suggest just making BFI lazy itself (when getBlockFreq() is first called ...). In fact I think the laziness handling should be in BlocFrequencyInfoImpl class. By so doing, MBFI can get the laziness automatically as well.

Moving it to MBFI, SGTM but I don't think that forcing the calculation of the frequencies in getBlockFreq is sufficient.

So just to be clear, you're suggesting sprinkling around in the code like this:

if (!Calculated)
  calculate()

in getBlockFreq, getBlockProfileCount, setBlockFreq, getFloatingBlockFreq, print and printBlockFreq?

I suggest just making BFI lazy itself (when getBlockFreq() is first called ...). In fact I think the laziness handling should be in BlocFrequencyInfoImpl class. By so doing, MBFI can get the laziness automatically as well.

I've tried and unfortunately, I don't think this will work. I saw two issues:

a. LoopInfo is freed by the time block frequencies are requested

This can be fixed by making users of BFI also dependent on LI but that's an overkill for existing users

b. The pass starts modifying the CFG and when it tries to update the frequencies LI and the CFG are already inconsistent

I think that these issues (there maybe more) are difficult to fix in the context of existing BFI's users. My new proposal is to introduce a new analysis, LazyBFI. Then, new users could opt in by complying with LBFI's requirements:

  1. require LI and BPI as well (I will add an 'required' enumerator)
  1. compute BFI before making any CFG changes

The analysis would effectively consist of the LazyBFI class I had earlier. It will only cover IR BFI for now but we could later template-ize it to also work with BFI.

I talked with Duncan about this and he was fine with this idea. Let me know your thoughts.

Adam

Just confirming my off-line conversation with Adam. LazyBFI needs a fundamentally different contract than the current BFI, and having clients opt-in seems right.

Long term: I imagine once the new pass manager is the one-true-way there could be a way to unify LazyBFI and BFI (it solves everything, right?), but I don't see how we could reasonably block improving optimization remarks on that.

The idea of lazyBFI analysis sounds good to me. Need to see what the actual
patch look like :)

David

anemet updated this revision to Diff 63738.Jul 12 2016, 3:29 PM
anemet edited edge metadata.

Expose LBFI as a new analysis pass.

Will also update the review description shortly.

anemet retitled this revision from [BFI] Add option to lazily calculate BFI to [BFI] Add new LazyBFI analysis pass.Jul 12 2016, 3:31 PM
anemet updated this object.
davidxl added inline comments.Jul 12 2016, 3:43 PM
include/llvm/Analysis/LazyBlockFrequencyInfo.h
22 ↗(On Diff #63738)

What is the use model for the new pass manager? Anything worth documenting?

91 ↗(On Diff #63738)

Can the wrapper class of BFI be 'inlined' into this class so that we don't need LBFI ?

lib/Analysis/LazyBlockFrequencyInfo.cpp
38 ↗(On Diff #63738)

Why not also implementing the new PM part of the support?

anemet added inline comments.Jul 12 2016, 3:56 PM
include/llvm/Analysis/LazyBlockFrequencyInfo.h
22 ↗(On Diff #63738)

I am not sure we will need this class with the new PM. Correct me if I am wrong but I thought as long as the client does not call something like getResult<BlockFrequencyAnalysis>(F), the analysis won't be populated. This is not the case for the old PM.

I was going to play with this after this patch and post follow-on patches if anything is required for the new PM.

Since the vectorizer is converted now, I should be able to try it there.

91 ↗(On Diff #63738)

It could certainly live nested in the pass, I would rather not remove it though; it nicely captures the single functionality it provides and the abstraction overhead should be removed by the compiler.

anemet updated this revision to Diff 63760.Jul 12 2016, 5:53 PM

Addresses David's comment. Make LBFI private. Add comment about presumed
direction for new PM.

davidxl accepted this revision.Jul 12 2016, 6:00 PM
davidxl edited edge metadata.

lgtm

This revision is now accepted and ready to land.Jul 12 2016, 6:00 PM
This revision was automatically updated to reflect the committed changes.