RFC - Profile Guided Optimization in LLVM

I have started looking at the state of PGO (Profile Guided Optimization) in LLVM. I want to discuss my high-level plan and make sure I’m not missing anything interesting out. I appreciate any feedback on this, pointers to existing work, patches and anything related to PGO in LLVM.

I will be keeping changes to this plan in this web document

https://docs.google.com/document/d/1b2XFuOkR2K-Oao4u5fR3a9Ok83IB_W4EJWVmNak4GRE/pub

At a high-level, I would like the PGO harness to contain the following modules:

Profile generators

These modules represent sources of profile. Mostly, they work by instrumenting the user program to make it produce profile information. However, other sources of profile information (e.g., samples, hardware counters, static predictors) would be supported.

Profile Analysis Oracles

Profile information is loaded into the compiler and translated into analysis data which the optimizers can use. These oracles become the one and only source of profile information used by transformations. Direct access to the raw profile data generated externally is not allowed.

Translation from profile information into analysis can be done by adding IR metadata or altering compiler internal data structures directly. I prefer IR metadata because it simplifies debugging, unit testing and bug reproduction.

Analyses should be narrow in the specific type of information they provide (e.g., branch probability) and there should not be two different analyses that provide overlapping information. We could later provide broader analyses types by aggregating the existing ones.

Transformations

Transformations should naturally take advantage of profile information by consulting the analyses. The better information they get from the analysis oracles, the better their decisions.

My plan is to start by making sure that the infrastructure exists and provides the basic analyses.

I have two primary goals in this first phase:

1. Augment the PGO infrastructure where required.
  1. Fix existing transformations that are not taking advantage of profile data.

In evaluating and triaging the existing infrastructure, I will use test cases taken from GCC’s own testsuite, a collection of Google’s internal applications and any other code base folks consider useful.

In using GCC’s testsuite, my goal is not to mimic how GCC does its work, but make sure that the two compilers implement functionally equivalent transformations. That is, make sure that LLVM is not leaving optimization opportunities behind.

This may require implementing missing profile functionality. From a brief inspection of the code, most of the major ones seem to be there (edge, path, block). But I don’t know what state they are in.

Some of the properties I would like to maintain or add to the current framework:

- Profile data is never accessed directly by analyses and transformations. Rather, it is translated into IR metadata.
  • Graceful degradation in the presence of stale profiles. Old profile data should only result in degraded optimization opportunities. It should neither confuse the compiler nor cause erroneous code generation.

After the basic profile-based transformations are working, I would like to add new sources of profile. Mainly, I am thinking of implementing Auto FDO. FDO stands for Feedback Directed Optimization (both PGO and FDO tend to be used interchangeably in the GCC community). In this scheme, the compiler does not instrument the code. Rather, it uses an external sample collection tool (e.g., perf) to collect samples from the program’s execution. These samples are then converted to the format that the instrumented program would’ve emitted.

In terms of optimizations, our (Google) experience is that inlining is the key beneficiary of profile information. Particularly, in big C++ applications. I expect to focus most of my attention on the inliner.

Thanks. Diego.

Good grief. A whole lot of fail in my cut-n-paste job. Apologies. You can read from the above or this: At a high-level, I would like the PGO harness to contain the following modules: Profile generators These modules represent sources of profile. Mostly, they work by instrumenting the user program to make it produce profile information. However, other sources of profile information (e.g., samples, hardware counters, static predictors) would be supported. Profile Analysis Oracles Profile information is loaded into the compiler and translated into analysis data which the optimizers can use. These oracles become the one and only source of profile information used by transformations. Direct access to the raw profile data generated externally is not allowed. Translation from profile information into analysis can be done by adding IR metadata or altering compiler internal data structures directly. I prefer IR metadata because it simplifies debugging, unit testing and bug reproduction. Analyses should be narrow in the specific type of information they provide (e.g., branch probability) and there should not be two different analyses that provide overlapping information. We could later provide broader analyses types by aggregating the existing ones. Transformations Transformations should naturally take advantage of profile information by consulting the analyses. The better information they get from the analysis oracles, the better their decisions. My plan is to start by making sure that the infrastructure exists and provides the basic analyses. I have two primary goals in this first phase: 1- Augment the PGO infrastructure where required. 2- Fix existing transformations that are not taking advantage of profile data. In evaluating and triaging the existing infrastructure, I will use test cases taken from GCC’s own testsuite, a collection of Google’s internal applications and any other code base folks consider useful. In using GCC’s testsuite, my goal is not to mimic how GCC does its work, but make sure that the two compilers implement functionally equivalent transformations. That is, make sure that LLVM is not leaving optimization opportunities behind. This may require implementing missing profile functionality. From a brief inspection of the code, most of the major ones seem to be there (edge, path, block). But I don’t know what state they are in. Some of the properties I would like to maintain or add to the current framework: * Profile data is never accessed directly by analyses and transformations. Rather, it is translated into IR metadata. * Graceful degradation in the presence of stale profiles. Old profile data should only result in degraded optimization opportunities. It should neither confuse the compiler nor cause erroneous code generation. After the basic profile-based transformations are working, I would like to add new sources of profile. Mainly, I am thinking of implementing Auto FDO. FDO stands for Feedback Directed Optimization (both PGO and FDO tend to be used interchangeably in the GCC community). In this scheme, the compiler does not instrument the code. Rather, it uses an external sample collection tool (e.g., perf) to collect samples from the program’s execution. These samples are then converted to the format that the instrumented program would’ve emitted. In terms of optimizations, our (Google) experience is that inlining is the key beneficiary of profile information. Particularly, in big C++ applications. I expect to focus most of my attention on the inliner.

That sounds plausible to me. It seems like we might need a way of representing call graph profiling in addition to the existing branch probabilities?

FWIW, the greedy register allocator’s live range splitting algorithm is designed to consume profile information so it can push spill code into cold blocks. The primary interface is SpillPlacement::getBlockFrequency() which currently returns an estimate based on loop depth only.

/jakob

That sounds plausible to me. It seems like we might need a way of
representing call graph profiling in addition to the existing branch
probabilities?

Agreed. An important consideration here is WPO vs. LTO vs. TU-at-a-time
call graphs.

FWIW, the greedy register allocator’s live range splitting algorithm is
designed to consume profile information so it can push spill code into cold
blocks. The primary interface is SpillPlacement::getBlockFrequency() which
currently returns an estimate based on loop depth only.

It doesn't use MachineBlockFrequency? If it does, it will get a lot more
than loop depth: __builtin_expect, cold function attribute, and static
branch heuristics. If it doesn't it should, and then it will immediately
benefit from this.

Err, MachineBlockFrequencyInfo -- the analysis pass. Sorry.

It predates the block frequency interface. It just needs to be hooked up, patches welcome. It would also be nice to remove the floating point computations from the spill placement code.

Thanks,
/jakob

Cool, if Diego doesn't beat me to it, I may send you a patch as that seems
easy and obviously beneficial.

Sounds good.

The only complication is that the floats are spilling into RAGreedy where they are used in the cost model. I think they can simply be replaced with BlockFrequency everywhere.

If the block frequencies make sense, this should also be unnecessary:

struct SpillPlacement::Node {
  /// Scale - Inverse block frequency feeding into[0] or out of[1] the bundle.
  /// Ideally, these two numbers should be identical, but inaccuracies in the
  /// block frequency estimates means that we need to normalize ingoing and
  /// outgoing frequencies separately so they are commensurate.
  float Scale[2];

Thanks,
/jakob

After the basic profile-based transformations are working, I would like to
add new sources of profile. Mainly, I am thinking of implementing Auto
FDO.

For those who are not familiar with what autoFDO is -- Auto FDO is
originally called Sample Based FDO. Its main author is Dehao Chen @google,
and Robert Hundt is the one of the main pushers of technology in Google.
The latest incarnation of this technology uses LBR events available on
Nehalem and above.
http://www.computer.org/csdl/trans/tc/2013/02/ttc2013020376-abs.html

Cheers,

David

Unless you’re in a hurry, I’d rather tackle this one myself. Particularly considering that I’ve no idea what you two are yapping about, so it will be a good learning experience.

Bob and I were discussing this over lunch yesterday and he has some very good ideas, so I’ve CC’ed him to make sure he joins the thread.

Chad

Metadata on function definitions/declarations perhaps?

I agree---the call graph profiling is of critical importance. I guess we should first see what types of data we already know may be useful and see how we would represent them within the compiler.

Here's my list (in addition to the usual data gathered in such cases):

* Function call frequency:
   - How many times a function has been called?

* Function argument value profiling:
   - Is the function called with a small set of values for the parameters?
   - Is there any value that is particularly frequent for any of the arguments?
   - If the function takes pointers, are the pointers usually aligned?
   - If the function takes pointers, are they aliased?
   - etc.

* Branch profiling:
   - Is the branch executed (i.e. taken/not-taken) in the same way as the last time, or does it often change direction?

* Path profiling:
   - Correlation between branches, function calls, etc.

-Krzysztof

* Function argument value profiling:
- Is the function called with a small set of values for the parameters?
- Is there any value that is particularly frequent for any of the arguments?
- If the function takes pointers, are the pointers usually aligned?
- If the function takes pointers, are they aliased?
- etc.

More generally it can be useful to collect value data for things like:
- switch values, in order to split off one or more dominant values for an explicit check prior to a table look-up or binary-search through all the cases
- function pointers, to make it possible to explicitly check against a very common pointer value so that the generated code can do a direct call to, or inline, the specific function (including virtual functions)

* Path profiling:
- Correlation between branches, function calls, etc.

If interprocedural path profiling information is gathered, that should also be usable as a source for generating context-sensitive call graph profile data.

Apple folks are also gearing up to push on the PGO front. We are primarily interested in using instrumentation, rather than sampling, to collect profile info. However, I suspect the way profile ended up being used in the various optimization and codegen passes would be largely similar.

There is also some interests in pursuing profile directed specialization. But that can wait. I think it makes sense for us to get together and discuss our plans to make sure there won’t be duplication of efforts.

Evan

Yes. But I'd like to see us tackling these as part two of PGO push. The question is what designs we would have to decide on now to prevent re-design / re-implement later.

Evan

I didn't want to interfere with you in any way but I was working on this just this week, though from a completely different background:

In zlib's longest_match() (which happens to contain the hottest loop in deflate()) there's a loop with this layout:

do {
  if (something) continue;

  while (cond && cond && cond && cond && cond) { }

} while (something_else);

The inner while loop is freezing cold. This is one of the cases where the current estimation based on loop depth completely fails while the heuristics BlockFrequency is based on get it right. With the old spill weights we spilled parts of the "something_else" code above that had to be reloaded on every iteration so we could keep more data of the inner loop in registers, causing a huge slowdown.

I hacked up a patch and saw a whopping 5% improvement on deflate, bringing us a lot closer to GCC on this code. Other benchmarks I tried so far looked neutral or positive but sometimes I saw a large increase in spilling; bloating code size, that needs to be figured out.

This patch only does the grunt work of getting the BlockFrequency analysis into the spill placement code, it doesn't replace the use of floats in the register allocator. It's possible that some of the downstream calculations need an update to cope with the changed magnitude of the frequencies.

Do you want to take over this effort or should I poke more at it?

- Ben

block-frequency-spilling.patch (22.6 KB)

Excellent! We are initially interested in instrumentation, as well. This is where we draw most of our performance with GCC. Sampling is showing a lot of promise, however. And it really is not much different than instrumentation. Most of what changes is the source of profile data. Sure. My initial plan is fairly simple. Triage the existing instrumentation code and see what needs fixing. I’m starting this in the next week or so. What are your plans? Diego.

Since you've already started, it's easier if you poke more at it. Thanks. I've got a whole bunch of other things to go through.

Diego.

OK, will do.

Jakob any comments on the patch? The only interesting part is in LiveIntervals::getSpillWeight. Do we have to scale the values somehow there?

- Ben

Yes, BlockFrequency::getFrequency() is poorly named, it returns a fixpoint number. I think you should scale it to be relative to the entry block frequency.

+LiveIntervals::getSpillWeight(bool isDef, bool isUse, BlockFrequency freq) {
+ return (isDef + isUse) * freq.getFrequency();
}

This computation can overflow.

@@ -178,9 +180,10 @@ bool SpillPlacement::runOnMachineFunction(MachineFunction &mf) {

   // Compute total ingoing and outgoing block frequencies for all bundles.
   BlockFrequency.resize(mf.getNumBlockIDs());
+ MachineBlockFrequencyInfo &MBFI = getAnalysis<MachineBlockFrequencyInfo>();
   for (MachineFunction::iterator I = mf.begin(), E = mf.end(); I != E; ++I) {
- float Freq = LiveIntervals::getSpillWeight(true, false,
- loops->getLoopDepth(I));
+ float Freq =
+ LiveIntervals::getSpillWeight(true, false, MBFI.getBlockFreq(I));
     unsigned Num = I->getNumber();
     BlockFrequency[Num] = Freq;

I think you should leave LiveIntervals out of this and just the block frequency directly, scaled as above. The getSpillWeight() function is just used here to get the non-overflowing frequency approximation.

Otherwise, looks good.

Thanks,
/jakob