<div dir="ltr"><div>> This is quite interesting. It suggests that with either a single large 
compilation unit, or when ThinLTO effectively creates one via lots of 
importing, that there is over-inlining of things that are presumably not
 as hot, hurting overall performance. E.g. since the inliner is bottom 
up, inlining of cold or lukewarm code might be preventing more important
 inlines further up the call chain, because the function becomes too 
large. With the split compilation units and more conservative importing,
 it is presumably importing and therefore inlining the hotter call edges
 more effectively. I know David has been looking at this type of 
situation in the inliner. <br></div><div><br></div><div>Yes, the data does seem to suggest that the higher availability of code can actually be detrimental to inlining effectiveness. It's especially interesting that this also seems to be true for the case where profiling data is available too where one might think that this additional information should allow the inliner to be at least as smart. However, these numbers are from a single codebase and its set of microbenchmarks, so they should be taken with a grain of salt. It might be an interesting lead though.<br><br>> 
Interesting. Does PGO mean PGO + ThinLTO here?

<br></div><div><br></div><div>For the cases with more than one compilation per crate: yes -- but in a slightly restricted sense. By default the Rust compiler performs ThinLTO among all compilation units that comprise a single crate. So ThinLTO is performed on all compilation units of the program; but instead of on the entire set it is done separately on N non-overlapping subsets (where N is the number of crates that make up the program).<br><br>The compiler treats PGO orthogonally, so in those cases where ThinLTO is the default, PGO is combined with ThinLTO.<br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Dec 2, 2019 at 3:59 PM Teresa Johnson <<a href="mailto:tejohnson@google.com">tejohnson@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr">Thanks for the update. One comment below. Teresa</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Dec 2, 2019 at 1:15 AM Michael Woerister via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">For anyone interested, I have a final update on this topic: I've come<br>
to the conclusion that, with the previously mentioned Cargo issue [1]<br>
fixed, profile-guided optimization now works as expected with Rust. I<br>
have a number of reasons to think so:<br>
<br>
- I did some semi-automated investigation of benchmarks that did not<br>
show much of a speedup and was not able to find any missing branch<br>
weights or function call counts. The concrete branch weights that are<br>
easy to predict (error paths in code that does not error during<br>
instrumentation runs) also looked correct to me. I subsequently added<br>
regression tests to the Rust compiler which make sure that branch<br>
weights are correct in a number of basic cases.<br>
<br>
- I also investigated indirect call promotion and it seems that<br>
idiomatic Rust code just contains very few indirect calls. I added<br>
regression tests that make sure that indirect call promotion is<br>
correctly performed for the two most common cases, calling through a<br>
function pointer and doing a dynamically dispatched method call.<br>
<br>
- Someone brought forth the hypothesis that Rust's much coarser<br>
compilation unit granularity might (partly) explain the difference of<br>
PGO effectiveness compared to C/C++ [2] -- and indeed my experiments<br>
seem to back this hypothesis up. When compiling Rust code for maximum<br>
performance, one usually lets the compiler generate a single object<br>
file per crate, which is equivalent to having a single object file per<br>
static library in C/C++. With this setup, PGO was only able to achieve<br>
an average 0.3% performance improvement in my benchmarks. However,<br>
increasing the number of object files to (roughly) one per source file<br>
led to an average performance improvement of 1.2%, that is, PGO made 4<br>
times as much of a difference. Reducing ThinLTO's import-instr-limit<br>
to 10 magnified the effect even more, making the PGO version about 4%<br>
faster than the non-PGO version, which is well within the range of<br>
improvement that one can expect from PGO. Interestingly, this last<br>
configuration with the stricter import limit was the most performant<br>
one, being also ~3% faster than the single compilation unit setup both<br>
with and without PGO.<br></blockquote><div><br></div><div>This is quite interesting. It suggests that with either a single large compilation unit, or when ThinLTO effectively creates one via lots of importing, that there is over-inlining of things that are presumably not as hot, hurting overall performance. E.g. since the inliner is bottom up, inlining of cold or lukewarm code might be preventing more important inlines further up the call chain, because the function becomes too large. With the split compilation units and more conservative importing, it is presumably importing and therefore inlining the hotter call edges more effectively. I know David has been looking at this type of situation in the inliner.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
In conclusion, (1) there is no evidence that the implementation is<br>
broken and (2) there are a number of cases and configurations that<br>
demonstrate that PGO *can* make as much of a difference as can be<br>
expected from it.<br>
<br>
[1] <a href="https://github.com/rust-lang/cargo/issues/7416" rel="noreferrer" target="_blank">https://github.com/rust-lang/cargo/issues/7416</a><br>
[2] <a href="https://internals.rust-lang.org/t/profile-guided-optimization-how-well-does-it-work-for-you/11108/11" rel="noreferrer" target="_blank">https://internals.rust-lang.org/t/profile-guided-optimization-how-well-does-it-work-for-you/11108/11</a><br>
<br>
On Tue, Sep 24, 2019 at 5:15 PM Michael Woerister<br>
<<a href="mailto:mwoerister@mozilla.com" target="_blank">mwoerister@mozilla.com</a>> wrote:<br>
><br>
> To give a little update here:<br>
><br>
> - I've been further investigating and found an issue [1] with the<br>
> Cargo build tool that most Rust projects use. This issue prevents all<br>
> projects using Cargo from properly using PGO because it causes symbol<br>
> names to be different between the generate and the use phase. With<br>
> this issue fixed the number of "No profile data available for<br>
> function" warnings goes down from 92369 to 1167 for the Firefox<br>
> codebase.<br>
><br>
> - I also found that the potential GNU ld bug mentioned above<br>
> apparently does not affect Firefox. The number of "No profile data<br>
> available for function" warnings is exactly the same for GNU ld and<br>
> LLD. I don't know yet where the remaining 1167 warnings come from<br>
> though.<br>
><br>
> - Unfortunately, even with all of the above fixes applied, my medium<br>
> sized benchmark still performs worse with PGO than without it. For my<br>
> tiny example [2] PGO reduces the number of branch misses by more than<br>
> 50%. For the medium sized benchmark, however, the PGO version has<br>
> slightly *more* branch misses. This seems to indicate that there is<br>
> still something wrong.<br>
><br>
> I will further investigate.<br>
><br>
> [1] <a href="https://github.com/rust-lang/cargo/issues/7416" rel="noreferrer" target="_blank">https://github.com/rust-lang/cargo/issues/7416</a><br>
> [2] <a href="https://github.com/michaelwoerister/rust-pgo-test-programs/tree/master/branch_weights/" rel="noreferrer" target="_blank">https://github.com/michaelwoerister/rust-pgo-test-programs/tree/master/branch_weights/</a><br>
><br>
><br>
> On Tue, Sep 17, 2019 at 6:16 PM Xinliang David Li <<a href="mailto:xinliangli@gmail.com" target="_blank">xinliangli@gmail.com</a>> wrote:<br>
> ><br>
> > You can check the difference of input args and object files to the linker.<br>
> ><br>
> > Regarding gnu ld, it is possible that it triggers another bug relating to start section and garbage collection. A previous bug is here: <a href="https://bugs.llvm.org/show_bug.cgi?id=25286" rel="noreferrer" target="_blank">https://bugs.llvm.org/show_bug.cgi?id=25286</a><br>
> ><br>
> > On Tue, Sep 17, 2019 at 8:39 AM Michael Woerister <<a href="mailto:mwoerister@mozilla.com" target="_blank">mwoerister@mozilla.com</a>> wrote:<br>
> >><br>
> >> Interestingly, a C version of the same test program [1] compiled with<br>
> >> Clang 8 does not have any problems with GNU ld: The `__llvm_prf_data`<br>
> >> section is the same size for all three linkers. It must be something<br>
> >> specific to the Rust compiler that's going wrong here.<br>
> >><br>
> >> [1] <a href="https://github.com/michaelwoerister/rust-pgo-test-programs/tree/master/cpp_branch_weights" rel="noreferrer" target="_blank">https://github.com/michaelwoerister/rust-pgo-test-programs/tree/master/cpp_branch_weights</a><br>
> >><br>
> >> On Tue, Sep 17, 2019 at 3:26 PM Michael Woerister<br>
> >> <<a href="mailto:mwoerister@mozilla.com" target="_blank">mwoerister@mozilla.com</a>> wrote:<br>
> >> ><br>
> >> > > Can you clarify if performance difference is caused by using different linkers at instrumentation build?<br>
> >> ><br>
> >> > Yes, good observation! Whether the bug occurs depends only on the<br>
> >> > linker used for creating the instrumented binary. The linker used<br>
> >> > during the "use" phase makes no difference.<br>
> >> ><br>
> >> > > If that is the case, try dump the sections of the resulting binary and compare __llvm_prf_** sections.<br>
> >> ><br>
> >> > For the final instrumented executable, it looks like the<br>
> >> > `__llvm_prf_data` section is 480 bytes large when using GNU ld, while<br>
> >> > it is 528 bytes for gold and lld. The size difference (48 bytes)<br>
> >> > incidentally is exactly the size of the `__llvm_prf_data` section in<br>
> >> > the object file containing the code that is later missing branch<br>
> >> > weights. It looks like the GNU linker loses the `__llvm_prf_data`<br>
> >> > section from that object file?<br>
> >> ><br>
> >> > > Also check the arguments passed to the linker. It should have -u__llvm_profile_runtime to force the profile runtime to be linked in.<br>
> >> ><br>
> >> > `-u__llvm_profile_runtime` is properly passed to the linker,<br>
> >> > regardless of which linker it is.<br>
> >> ><br>
> >> > On Mon, Sep 16, 2019 at 7:40 PM Xinliang David Li <<a href="mailto:xinliangli@gmail.com" target="_blank">xinliangli@gmail.com</a>> wrote:<br>
> >> > ><br>
> >> > > Can you clarify if performance difference is caused by using different linkers at instrumentation build?  If that is the case, try dump the sections of the resulting binary and compare __llvm_prf_** sections. Also check the arguments passed to the linker. It should have -u__llvm_profile_runtime   to force the profile runtime to be linked in.<br>
> >> > ><br>
> >> > > David<br>
> >> > ><br>
> >> > > On Mon, Sep 16, 2019 at 8:42 AM Michael Woerister via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
> >> > >><br>
> >> > >> So one interesting observation has already come out of this: I<br>
> >> > >> confirmed that `rustc` indeed uses `-ffunction-sections` and<br>
> >> > >> `-fdata-sections` on all platforms except for macOS. When trying out<br>
> >> > >> different linkers for a small test case [1], however, I found that<br>
> >> > >> there were rather large differences in execution time:<br>
> >> > >><br>
> >> > >> ld (no PGO) = 172 ms<br>
> >> > >> ld (PGO) = 196 ms<br>
> >> > >><br>
> >> > >> gold (no PGO) = 182 ms<br>
> >> > >> gold (PGO) = 141 ms<br>
> >> > >><br>
> >> > >> lld (no PGO) = 193 ms<br>
> >> > >> lld (PGO) = 171 ms<br>
> >> > >><br>
> >> > >> So `gold` and `lld` both profit from PGO quite a bit, while `ld`<br>
> >> > >> linked programs are slower with PGO. I then noticed that branch<br>
> >> > >> weights for `ld` were missing from most branches, while the counts for<br>
> >> > >> the other linkers are correct. All of this suggests to me that<br>
> >> > >> something goes wrong when `ld` tries to link in the profiling runtime.<br>
> >> > >><br>
> >> > >> I'll be investigating further.<br>
> >> > >><br>
> >> > >> [1] <a href="https://github.com/michaelwoerister/rust-pgo-test-programs/tree/master/branch_weights" rel="noreferrer" target="_blank">https://github.com/michaelwoerister/rust-pgo-test-programs/tree/master/branch_weights</a><br>
> >> > >><br>
> >> > >><br>
> >> > >> On Thu, Sep 12, 2019 at 6:31 PM Teresa Johnson <<a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a>> wrote:<br>
> >> > >> ><br>
> >> > >> ><br>
> >> > >> ><br>
> >> > >> > On Thu, Sep 12, 2019 at 8:18 AM Teresa Johnson <<a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a>> wrote:<br>
> >> > >> >><br>
> >> > >> >> I just have a couple suggestions off the top of my head:<br>
> >> > >> >> - have you tried using the new pass manager (-fexperimental-new-pass-manager)? That has access to additional analysis info during inlining and is able to make more precise PGO based inline decisions.<br>
> >> > >> ><br>
> >> > >> ><br>
> >> > >> > (although note the above shouldn't make the difference between no performance and a typical PGO performance boost)<br>
> >> > >> ><br>
> >> > >> > Another thing I just thought of - are you using -ffunction-sections and -fdata-sections? These will allow for PGO based function layout in the linker (assuming you are using lld or gold).<br>
> >> > >> ><br>
> >> > >> >> - have you tried collecting profile data with and without PGO to see if you can compare where cycles are being spent? That's my usual way of debugging performance differences related to inlining or profile changes.<br>
> >> > >> >> - just a comment that it is odd you are getting better performance without the pre-inlining - which typically helps because you get better context-sensitive profile info. Maybe sanity check that the pre inlining is kicking in for both the profile gen and use passes?<br>
> >> > >> >><br>
> >> > >> >> Teresa<br>
> >> > >> >><br>
> >> > >> >> On Thu, Sep 12, 2019 at 2:18 AM Michael Woerister via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
> >> > >> >>><br>
> >> > >> >>> Hi everyone,<br>
> >> > >> >>><br>
> >> > >> >>> As part of my work for Mozilla's Low Level Tools team I've<br>
> >> > >> >>> implemented PGO in the Rust compiler. The feature is<br>
> >> > >> >>> available since Rust 1.37 [1]. However, so far we have not<br>
> >> > >> >>> seen any actual performance gains from enabling PGO for<br>
> >> > >> >>> Rust code. Performance even seems to drop 1-3% with PGO<br>
> >> > >> >>> enabled. I wonder why that is and I'm hoping that someone<br>
> >> > >> >>> here might have experience debugging PGO effectiveness.<br>
> >> > >> >>><br>
> >> > >> >>><br>
> >> > >> >>> PGO in the Rust compiler<br>
> >> > >> >>> ------------------------<br>
> >> > >> >>><br>
> >> > >> >>> The Rust compiler uses IR-level instrumentation (the<br>
> >> > >> >>> equivalent of Clang's `-fprofile-generate`/`-fprofile-use`).<br>
> >> > >> >>> This has worked pretty well and even enables doing PGO for<br>
> >> > >> >>> mixed Rust/C++ codebases when also using Clang.<br>
> >> > >> >>><br>
> >> > >> >>> The Rust compiler has regression tests that make sure that:<br>
> >> > >> >>><br>
> >> > >> >>> - instrumentation shows up in LLVM IR for the `generate` phase,<br>
> >> > >> >>>   and that<br>
> >> > >> >>><br>
> >> > >> >>> - profiling data is actually used during the `use` phase, i.e.<br>
> >> > >> >>>   that cold functions get marked with `cold` and hot functions<br>
> >> > >> >>>   get marked with `inline`.<br>
> >> > >> >>><br>
> >> > >> >>> I also verified manually that `branch_weights` are being set<br>
> >> > >> >>> in IR. So, from my perspective, the PGO implementation does<br>
> >> > >> >>> what it is supposed to do.<br>
> >> > >> >>><br>
> >> > >> >>> However, as already mentioned, in all benchmarks I've seen so<br>
> >> > >> >>> far performance seems to stay the same at best and often even<br>
> >> > >> >>> suffers slightly. Which is suprising because for C++ code<br>
> >> > >> >>> using Clang's version of IR-level instrumentation & PGO brings<br>
> >> > >> >>> signifcant gains (up to 5-10% from what I've seen in<br>
> >> > >> >>> benchmarks for Firefox).<br>
> >> > >> >>><br>
> >> > >> >>> One thing we noticed early on is that disabling the<br>
> >> > >> >>> pre-inlining pass (`-disable-preinline`) seems to consistently<br>
> >> > >> >>> improve the situation for Rust code. Doing that we sometimes<br>
> >> > >> >>> see performance wins of almost 1% over not using PGO. This<br>
> >> > >> >>> again is very different to C++ where disabling this pass<br>
> >> > >> >>> causes dramatic performance loses for the Firefox benchmarks.<br>
> >> > >> >>> And 1% performance improvement is still well below<br>
> >> > >> >>> expectations, I think.<br>
> >> > >> >>><br>
> >> > >> >>> So my questions to you are:<br>
> >> > >> >>><br>
> >> > >> >>> - Has anybody here observed something similar while<br>
> >> > >> >>>   wokring on or with PGO?<br>
> >> > >> >>><br>
> >> > >> >>> - Are there certain known characteristics of LLVM IR code<br>
> >> > >> >>>   that inhibit PGO's effectiveness and that IR produced by<br>
> >> > >> >>>   `rustc` might exhibit?<br>
> >> > >> >>><br>
> >> > >> >>> - Does anybody know of a good source that describes how to<br>
> >> > >> >>>   effectively debug a problem like this?<br>
> >> > >> >>><br>
> >> > >> >>> - Does anybody know of a small example program in C/C++<br>
> >> > >> >>>   that is known to profit from PGO and that could be<br>
> >> > >> >>>   re-implemented in Rust for comparison?<br>
> >> > >> >>><br>
> >> > >> >>> Thanks a lot for reading! Any help is appreciated.<br>
> >> > >> >>><br>
> >> > >> >>> -Michael<br>
> >> > >> >>><br>
> >> > >> >>> [1] <a href="https://blog.rust-lang.org/2019/08/15/Rust-1.37.0.html#profile-guided-optimization" rel="noreferrer" target="_blank">https://blog.rust-lang.org/2019/08/15/Rust-1.37.0.html#profile-guided-optimization</a><br>
> >> > >> >>> _______________________________________________<br>
> >> > >> >>> LLVM Developers mailing list<br>
> >> > >> >>> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
> >> > >> >>> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
> >> > >> >><br>
> >> > >> >><br>
> >> > >> >><br>
> >> > >> >> --<br>
> >> > >> >> Teresa Johnson | Software Engineer | <a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a> |<br>
> >> > >> ><br>
> >> > >> ><br>
> >> > >> ><br>
> >> > >> > --<br>
> >> > >> > Teresa Johnson | Software Engineer | <a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a> |<br>
> >> > >> _______________________________________________<br>
> >> > >> LLVM Developers mailing list<br>
> >> > >> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
> >> > >> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr"><div dir="ltr"><div><span style="font-family:Times;font-size:medium"><table cellspacing="0" cellpadding="0"><tbody><tr style="color:rgb(85,85,85);font-family:sans-serif;font-size:small"><td style="border-top:2px solid rgb(213,15,37)" nowrap>Teresa Johnson |</td><td style="border-top:2px solid rgb(51,105,232)" nowrap> Software Engineer |</td><td style="border-top:2px solid rgb(0,153,57)" nowrap> <a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a> |</td><td style="border-top:2px solid rgb(238,178,17)" nowrap><br></td></tr></tbody></table></span></div></div></div></div>
</blockquote></div>