<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi Teresa,<div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Aug 15, 2015, at 5:31 PM, Teresa Johnson <<a href="mailto:tejohnson@google.com" class="">tejohnson@google.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hi Mehdi,<div class=""><br class=""></div><div class="">Saw David's response but wanted to add a bit more below.<br class=""><div class="gmail_extra"><br class=""><div class="gmail_quote">On Fri, Aug 14, 2015 at 11:14 PM, Mehdi Amini <span dir="ltr" class=""><<a href="mailto:mehdi.amini@apple.com" target="_blank" class="">mehdi.amini@apple.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><br class=""><div class=""><span class=""><blockquote type="cite" class=""><div class="">On Aug 14, 2015, at 10:41 PM, Teresa Johnson <<a href="mailto:tejohnson@google.com" target="_blank" class="">tejohnson@google.com</a>> wrote:</div><br class=""><div class=""><br class=""><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><div class="gmail_quote" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">On Fri, Aug 14, 2015 at 3:35 PM, Mehdi Amini<span class=""> </span><span dir="ltr" class=""><<a href="mailto:mehdi.amini@apple.com" target="_blank" class="">mehdi.amini@apple.com</a>></span><span class=""> </span>wrote:<br class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="">Hi Teresa,<br class=""><br class="">Thanks for layout down a detailed proposal on top of the slides, it is very instructive and very pleasant to read.<br class=""></span></blockquote><div class=""><br class=""></div><div class="">Hi Mehdi,</div><div class=""><br class=""></div><div class="">Thanks!</div><div class=""> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class=""><br class="">I have a few questions, none of which touches the ELF aspect! :)<br class="">I apologize if you already addressed them and I missed it (if you can refer me to any past discussion that would be nice).<br class=""><br class="">1) I haven’t seen mention of how you plan to split the pipeline (what will be done at compile-time vs what will be done at link time), is the plan do it the same way as -flto does today (AFAIK almost complete -O3 during compilation, and the specific LTO pipeline during link time)?<br class=""></span></blockquote><div class=""><br class=""></div><div class="">Yes, the first stage is very similar to LTO, so just like a "-c -flto" compile (which could do -O2 or -O3 optimization before emitting the bitcode), the main difference being the function summary and index emitted along with the module bitcode. Unlike LTO the initial link of the bitcode files just produces the combined function summary and index, but doesn't do any optimization. After that, the LTO pipeline will be executed during the parallel backend stage, where each module is compiled separately and does importing.</div><div class=""> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class=""><br class="">2) It seems to me that there is a major difference in the optimization pipeline with the regular pipeline and LTO vs your proposal. When the inliner runs, we usually have all the callees (and their callees, transitively) already “optimized” (not completely, but fairly intensively).</span></blockquote><div class=""><br class=""></div><div class="">I assume you are referring to the optimization/inlining done in the initial (-c -flto -O2) compile. That part will be the same with ThinLTO.</div></div></div></blockquote><div class=""><br class=""></div></span><div class="">I was really thinking about the LTO phase, when you do cross-module inlining. This is done in parallel backends if I understand correctly how ThinLTO works. </div></div></div></blockquote><div class=""><br class=""></div><div class="">Yes, each of the modules runs the LTO pipeline during the parallel backend, with the scope expanded to include any imported functions.</div><div class=""><br class=""></div><div class=""> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class=""><div class="">Which means you can’t intrinsically follow the SCC order that is used by LTO.</div></div></div></blockquote><div class=""><br class=""></div><div class="">The optimization can still operate on the SCC, but the scope of optimization depends on how much importing was done. Essentially, the LTO pipeline doesn't change, but there is an importing pass prior to the inlining SCC pass which enlargens the scope for inlining, etc, beyond that of the single module. Everything starting with inlining behaves the same as a normal LTO pass pipeline. The trick is to devise importing thresholds and heuristics based on the summary information (and callsite information) that causes enough functions to be imported such that profitable inlining and other cross-module optimizations occur. But not so much importing that results in importing of functions that aren't inlined and are simply discarded afterwards, unnecessarily bloating the backend compilation in time and memory </div><div class=""> <br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class=""><div class="">But basically this is what I describe in more details just below.</div><span class=""><br class=""><blockquote type="cite" class=""><div class=""><div class="gmail_quote" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div class=""> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="">In your flow, a function imported from another module will be considered by the inliner *before* it has inlined cross-module its callees.<br class=""></span></blockquote><div class=""><br class=""></div><div class="">The importing pass will be prior to inlining in the backend LTO pipeline. So let's say we have a case where module<span class=""> </span><a href="http://a.cc/" target="_blank" class="">A.cc</a><span class=""> </span>contains a() which calls b() from module<span class=""> </span><a href="http://b.cc/" target="_blank" class="">B.cc</a>, which in turn calls c() from module<span class=""> </span><a href="http://c.cc/" target="_blank" class="">C.cc</a>. When compiling A.o in the backend phase, the importer will see the call to b(), and if it thinks inlining is likely it will import it, exposing the call to c(), which will then similarly be considered for importing. So when the inliner pass is run it will be similar to LTO for the a()->b()->c() call chain, assuming importing was deemed profitable for all of the cross-module callees.</div></div></div></blockquote><div class=""><br class=""></div></span><div class="">So to be clear with the sequence of what happens, if you’re doing what the regular pipeline does (assuming O3):</div><div class=""><br class=""></div><div class="">1) Function c() will be considered by the inliner (assuming no calllees available)</div><div class=""><br class=""></div><div class="">2) It will be optimized by running the following passes (+ implicit analyses):</div><div class=""><br class=""></div><div class="">SROA, Early CSE, Jump Threading, Value Propagation, Simplify the CFG, Combine redundant instructions, Tail Call Elimination, Simplify the CFG, Reassociate expressions, Canonicalize natural loops, Loop-Closed SSA Form Pass, Rotate Loops, Loop Invariant Code Motion, Unswitch loops, Combine redundant instructions, Scalar Evolution Analysis, Canonicalize natural loops, Loop-Closed SSA Form Pass, Induction Variable Simplification, Recognize loop idioms, Delete dead loops, Unroll loops, MergedLoadStoreMotion, Memory Dependence Analysis, Global Value Numbering, MemCpy Optimization, Sparse Conditional Constant Propagation, Combine redundant instructions, Jump Threading, Value Propagation, Dead Store Elimination, Aggressive Dead Code Elimination, Simplify the CFG, Combine redundant instructions.</div><div class=""><br class=""></div><div class="">3) Function b will be considered by the inliner, and may or may not inline c()</div><div class=""><br class=""></div>4) Function b will be optimized using the same set of passes as function c() during step 2).</div><div class=""><br class=""></div><div class="">5) Only now Function a() will be considered by the inliner and may or may not inline b().</div><div class=""><br class=""></div><div class="">6) Function a will be optimized using the same set of passes as function c() during step 2).</div></div></blockquote><div class=""><br class=""></div><div class="">I believe what you have described above is the non-LTO -O3 -c pipeline, is that correct? The LTO pipeline appears to do inlining across all the SCCs, then does a round each of Global Variable Optimization and Dead Global Optimization before building the callgraph again for downstream optimizations. </div></div></div></div></div></div></blockquote><div><br class=""></div><div><br class=""></div><div>Good point, I didn’t see before that the LTO pipeline is different. It hasn’t been touched in years and I wonder if it is not just a bug. I don’t see any reason for it to be different than the regular pipeline? It seems I have some experiments to run...</div><div><br class=""></div><div><br class=""></div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><div class="gmail_extra"><div class="gmail_quote"><div class=""><br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Note that with ThinLTO, you have in parallel at the same time the module <a href="http://b.cc/" target="_blank" class="">B.cc</a> processing and steps 1, 2, 3, and 4 are also performed on the same IR. They may not end-up with the same result as c() may have some callees available that were not available during <a href="http://a.cc/" target="_blank" class="">A.cc</a> compilation. It means that c() might be inlined in b() when processing <a href="http://b.cc/" target="_blank" class="">B.cc</a> but not when processing <a href="http://a.cc/" target="_blank" class="">A.cc</a>, and as a consequence maybe the version of b() in <a href="http://b.cc/" target="_blank" class="">B.cc</a> could have been inlined in a() but not the version of b() in <a href="http://a.cc/" target="_blank" class="">A.cc</a>.</div><div class="">(I hope it’s not too confused, I may have to provide an example).</div><div class=""><br class=""></div><div class="">And of course step 1 and 2 are also performed at the same time for module <a href="http://c.cc/" target="_blank" class="">C.cc</a>, and may give again a different result.</div><div class=""><br class=""></div><div class="">My observation is that the increased parallelism you have with ThinLTO comes with some (non-negligible?) redundant work, and you need this redundant work to have keep some quality for the inliner.</div></div></blockquote><div class=""><br class=""></div><div class="">So it is true that when compiling <a href="http://A.cc" class="">A.cc</a> through the parallel backend (from bitcode down to object code), it may import b() and c() and make different inlining decisions than when b() is compiled in <a href="http://B.cc" class="">B.cc</a>'s backend compilation, etc. And this also results in some duplication of work. But each parallel backend invocation is smaller/faster than a full LTO backend, so you get much better scalability at the cost of some duplicate work. And also true that the inlining decisions may not end up the same as in a full blown LTO compilation. From the prototype results I got it does look like some simple heuristics can give a lot of the LTO benefit though, and hopefully can be improved with more summary information and tuning.</div><div class=""><br class=""></div><div class="">Let me know if I haven't answered your concern or misunderstood your example though!</div><div class=""><br class=""></div></div></div></div></div></div></blockquote><div><br class=""></div><div>You understood correctly :)</div><div><br class=""></div><div>Trying to figure out the ThinLTO limitations compared to (Fat)LTO, I was trying to figure out what result would ThinLTO give on the basic clang example: <a href="http://llvm.org/docs/LinkTimeOptimization.html" class="">http://llvm.org/docs/LinkTimeOptimization.html</a></div><div><br class=""></div><div>The regular LTO performs this (copy/pasted from the page):</div><div><br class=""></div><div><div class=""><span class="Apple-tab-span" style="white-space:pre"> </span>• In this example, the linker recognizes that foo2() is an externally visible symbol defined in LLVM bitcode file. The linker completes its usual symbol resolution pass and finds that foo2() is not used anywhere. This information is used by the LLVM optimizer and it removes foo2().<br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre"> </span>• As soon as foo2() is removed, the optimizer recognizes that condition i < 0 is always false, which means foo3() is never used. Hence, the optimizer also removes foo3().<br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre"> </span>• And this in turn, enables linker to remove foo4().</div></div><div><br class=""></div><div>In ThinLTO, it seems to me that to be able to import foo1 in main.c, the static in a.c has to be promoted to a global which would kill the optimization described, is it correct?</div><div><br class=""></div><div>Thanks,</div><div> </div><div><br class=""></div><div>— </div><div>Mehdi</div><div><br class=""></div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class=""><br class=""></div><div class=""><span class=""><div class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div class="gmail_quote" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div class=""><br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class=""><br class="">3) The incremental aspect is not addressed, is it something that you thought about or plan to improve in a future version? I have some ideas on this topic that I’d like to explore, and ThinLTO seems an interesting design to play with that.<br class=""></span></blockquote><div class=""><br class=""></div><div class="">For the bitcode files produced by the first -c compile phase the incremental compiles work as normal. But I assume you are talking about incremental compiles for the backend LTO part. Some incremental compilation can be achieved by computing and saving the dependence set of modules each module is allowed to import from, using profile information or heuristics in the linker/plugin stage.</div><div class=""><br class=""></div><div class="">I'd be interested in any thoughts you have on enabling incremental compilation for ThinLTO or LTO in general.</div></div></div></blockquote><div class=""><br class=""></div></span><div class="">I’ll let you know if I manage to get a reasonable sketch on this topic!</div></div></div></blockquote><div class=""><br class=""></div><div class="">Great, thanks.</div><div class=""> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class=""><span class=""><br class=""><blockquote type="cite" class=""><div class=""><div class="gmail_quote" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div class=""><br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class=""><br class="">4) Is your prototype implementation available online?<br class=""></span></blockquote><div class=""><br class=""></div><div class="">I haven't made it available as it needed a cleanup and had some prototype aspects like writing the function index to a side text file instead of in the module with the bitcode. I've been working instead on a cleaner implementation that I've started to send for review. I saw you added yourself to a few of the patches and left some review comments - thanks. I will be working on responding to the comments and updating the patches next.</div></div></div></blockquote><div class=""><br class=""></div></span><div class="">I saw the patches on Phabricator after asking the question here :)</div><div class="">I was more interesting to hack around a working prototype to experiment the potential for incremental compilation. But not a big deal, I’ll follow the landing of patches!</div></div></div></blockquote><div class=""><br class=""></div><div class="">Sounds good, I am hoping to get patches ready pretty quickly once the basic infrastructure is reviewed and in.</div><div class=""><br class=""></div><div class="">Teresa</div><div class=""> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class=""><div class=""><div class="h5"><div class=""><br class=""></div><div class="">Thanks,</div><div class=""><br class=""></div><div class="">— </div><div class="">Mehdi</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div class="gmail_quote" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class=""><br class=""><br class=""><br class="">> On May 28, 2015, at 2:10 PM, Teresa Johnson <<a href="mailto:tejohnson@google.com" target="_blank" class="">tejohnson@google.com</a>> wrote:<br class="">><br class=""></span><div class=""><div class="">> As promised, here is an new version of the ThinLTO RFC, updated based<br class="">> on some of the comments, questions and feedback from the first RFC.<br class="">> Hopefully we have addressed many of these, and as noted below, will<br class="">> fork some of the detailed discussion on particular aspects into<br class="">> separate design doc threads. Please send any additional feedback and<br class="">> questions on the overall design.<br class="">> Thanks!<br class="">> Teresa<br class="">><br class="">><br class="">> Updated RFC to discuss plans for implementing ThinLTO upstream,<br class="">> reflecting feedback and discussion from initial RFC<br class="">> (<a href="http://lists.cs.uiuc.edu/pipermail/llvmdev/2015-May/085557.html" rel="noreferrer" target="_blank" class="">http://lists.cs.uiuc.edu/pipermail/llvmdev/2015-May/085557.html</a>). As<br class="">> discussed in the earlier thread and below, more detailed design<br class="">> documents for several pieces (native object format, linkage type<br class="">> changes and static promotions, etc) are in progress and will be sent<br class="">> separately. This RFC covers the overall design and the breakdown of<br class="">> work at a higher level.<br class="">><br class="">><br class="">> Background on ThinLTO can be found in slides from EuroLLVM 2015:<br class="">><span class=""> </span><a href="https://drive.google.com/open?id=0B036uwnWM6RWWER1ZEl5SUNENjQ&authuser=0" rel="noreferrer" target="_blank" class="">https://drive.google.com/open?id=0B036uwnWM6RWWER1ZEl5SUNENjQ&authuser=0</a><br class="">> As described in the talk, we have a prototype implementation, and<br class="">> would like to start staging patches upstream. This RFC describes a<br class="">> breakdown of the major pieces. We would like to commit upstream<br class="">> gradually in several stages, with all functionality off by default.<br class="">> The core ThinLTO importing support and tuning will require frequent<br class="">> change and iteration during testing and tuning, and for that part we<br class="">> would like to commit rapidly (off by default). See the proposed staged<br class="">> implementation described in the Implementation Plan section.<br class="">><br class="">><br class="">> ThinLTO Overview<br class="">> ==================<br class="">><br class="">><br class="">> See the talk slides linked above for more details. The following is a<br class="">> high-level overview of the motivation.<br class="">><br class="">><br class="">> Cross Module Optimization (CMO) is an effective means for improving<br class="">> runtime performance, by extending the scope of optimizations across<br class="">> source module boundaries. Without CMO, the compiler is limited to<br class="">> optimizing within the scope of single source modules. Two solutions<br class="">> for enabling CMO are Link-Time Optimization (LTO), which is currently<br class="">> supported in LLVM and GCC, and Lightweight-Interprocedural<br class="">> Optimization (LIPO). However, each of these solutions has limitations<br class="">> that prevent it from being enabled by default. ThinLTO is a new<br class="">> approach that attempts to address these limitations, with a goal of<br class="">> being enabled more broadly. ThinLTO is designed with many of the same<br class="">> principals as LIPO, and therefore its advantages, without any of its<br class="">> inherent weakness. Unlike in LIPO where the module group decision is<br class="">> made at profile training runtime, ThinLTO makes the decision at<br class="">> compile time, but in a lazy mode that facilitates large scale<br class="">> parallelism. LTO implementations all contain a serial IPA/IPO step<br class="">> that is both memory intensive and slow, limiting usability on both<br class="">> smaller workstations and huge applications. In contrast, the ThinLTO<br class="">> serial linker plugin phase is designed to be razor thin and blazingly<br class="">> fast. By default this step only does minimal preparation work to<br class="">> enable the parallel lazy importing performed later. ThinLTO aims to be<br class="">> scalable like a regular O2 build, enabling CMO on machines without<br class="">> large memory configurations, while also integrating well with<br class="">> distributed build systems. Results from early prototyping on SPEC<br class="">> cpu2006 C++ benchmarks are in line with expectations that ThinLTO can<br class="">> scale like O2 while enabling much of the CMO performed during a full<br class="">> LTO build.<br class="">><br class="">><br class="">> A ThinLTO build is divided into 3 phases, which are referred to in the<br class="">> following implementation plan:<br class="">> 1. phase-1: IR and Function Summary Generation (-c compile)<br class="">> 2. phase-2: Thin Linker Plugin Layer (thin archive linker step)<br class="">> 3. phase-3: Parallel Backend with Demand-Driven Importing<br class="">><br class="">><br class="">> Implementation Plan<br class="">> ====================<br class="">><br class="">><br class="">> This section gives a high-level breakdown of the ThinLTO support that<br class="">> will be added, in roughly the order that the patches would be staged.<br class="">> The patches are divided into three stages. The first stage contains a<br class="">> minimal amount of preparation work that is not ThinLTO-specific. The<br class="">> second stage contains most of the infrastructure for ThinLTO, which<br class="">> will be off by default. The third stage includes<br class="">> enhancements/improvements/tunings that can be performed after the main<br class="">> ThinLTO infrastructure is in.<br class="">><br class="">><br class="">> The second and third implementation stages will initially be very<br class="">> volatile, requiring a lot of iterations and tuning with large apps to<br class="">> get stabilized. Therefore it will be important to do fast commits for<br class="">> these implementation stages.<br class="">><br class="">><br class="">> 1. Stage 1: Preparation<br class="">> ------------------------------------<br class="">><br class="">><br class="">> The first planned sets of patches are enablers for ThinLTO work:<br class="">><br class="">><br class="">> a. LTO directory structure<br class="">><br class="">><br class="">> Restructure the LTO directory to remove circular dependence when<br class="">> ThinLTO pass added. Because ThinLTO is being implemented as a SCC pass<br class="">> within Transforms/IPO, and leverages the LTOModule class for linking<br class="">> in functions from modules, IPO then requires the LTO library. This<br class="">> creates a circular dependence between LTO and IPO. To break that, we<br class="">> need to split the lib/LTO directory/library into lib/LTO/CodeGen and<br class="">> lib/LTO/Module, containing LTOCodeGenerator and LTOModule,<br class="">> respectively. Only LTOCodeGenerator has a dependence on IPO, removing<br class="">> the circular dependence.<br class="">><br class="">><br class="">> Note that libLTO and llvm-lto use LTOModule/LTOCodeGenerator, whereas<br class="">> the gold plugin uses lib/Object/IRObject and lib/Linker directly. The<br class="">> use of LTOModule in the ThinLTO pass is a convenience, but could be<br class="">> avoided by using the IRObject/Linker methods directly if that is<br class="">> preferred.<br class="">><br class="">><br class="">> b. Native object wrapper generation support<br class="">><br class="">><br class="">> Implement native-object wrapped bitcode writer. The main goal is to<br class="">> more easily interact with existing native tools such as $AR, $NM, “$LD<br class="">> -r”, $OBJCOPY, and $RANLIB, without requiring the build system to find<br class="">> and pass the plugin as an option. We plan to emit the phase-1 bitcode<br class="">> wrapped in native object format via the .llvmbc section, along with a<br class="">> symbol table. We will implement ELF first, but subsequently extend<br class="">> support to COFF and Mach-O. Additionally, we also want to avoid doing<br class="">> partial LTO/ThinLTO across files linked with “$LD -r” (i.e. the<br class="">> resulting object file should still contain native object-wrapped<br class="">> bitcode to enable ThinLTO at the full link step). I will send a<br class="">> separate design document for these changes, including the format of<br class="">> the symtab and function index/summary section, but the following is a<br class="">> high-level motivation and overview.<br class="">><br class="">><br class="">> Note that support for ThinLTO using bitcode can be added as a<br class="">> follow-on under an option, so that bitcode-aware tools do not need to<br class="">> use the wrapper. Under the bitcode-only option, the symbol table will<br class="">> be replaced by the bitcode form of the function index and summary<br class="">> section, which can be encoded as a new bitcode block type. Changes<br class="">> should be made to the gold plugin to avoid partial link of bitcode<br class="">> files under “$LD -r” (emitting bitcode rather than compiling all the<br class="">> way down to native code, which is how ld64 behaves on Darwin as per<br class="">> dexonsmith).<br class="">><br class="">><br class="">> Advantages of using native object format:<br class="">> * Out of the box interoperability with existing native build tools<br class="">> ($AR, $NM, “$LD -r”, $OBJCOPY, and $RANLIB) which may not currently<br class="">> know how to locate/pass the appropriate plugin.<br class="">> * There is precedence in using this format: other compilers also wrap<br class="">> intermediate LTO files (probably related to the above advantage)[1].<br class="">> * Tools that modify symbol linkage and visibility (e.g. $OBJCOPY and<br class="">> “$LD -r”) can mark the change in the symbol table without needing to<br class="">> parse/change/encode bitcode. The change can be propagated to bitcode<br class="">> by the ThinLTO backend.<br class="">> * Some tools only need to read/write the symtab and can avoid<br class="">> parsing/encoding bitcode (e.g. $NM, $OBJCOPY).<br class="">> * The second phase of ThinLTO does not need to parse the bitcode when<br class="">> creating the combined function index.<br class="">><br class="">><br class="">> Disadvantages of using native object format:<br class="">> * Unnecessary when using plugins with plugin-aware native tools, or<br class="">> LLVM’s custom tools.<br class="">> * Slightly increase disk storage and I/O from symtab. However, with<br class="">> our design the symtab is leveraged to hold function indexing info<br class="">> required for ThinLTO. The I/O for some build tools and build steps can<br class="">> actually be reduced as there is no need to read the bitcode, as<br class="">> described above.<br class="">><br class="">><br class="">> Support was added to LLVM for reading native object-wrapped bitcode<br class="">> (<a href="http://reviews.llvm.org/rL218078" rel="noreferrer" target="_blank" class="">http://reviews.llvm.org/rL218078</a>), but there does not yet exist<br class="">> support in LLVM/Clang for emitting bitcode wrapped in native object<br class="">> format. I plan to add support for optionally generating bitcode in an<br class="">> native object file containing a single .llvmbc section holding the<br class="">> bitcode. Specifically, the patch would add new options<br class="">> “emit-llvm-native-object” (object file) and corresponding<br class="">> “emit-llvm-native-assembly” (textual assembly code equivalent).<br class="">> Eventually these would be automatically triggered under “-fthinlto -c”<br class="">> and “-fthinlto -S”, respectively.<br class="">><br class="">><br class="">> Additionally, a symbol table will be generated in the native object<br class="">> file, holding the function symbols within the bitcode. This<br class="">> facilitates handling archives of the native object-wrapped bitcode<br class="">> created with $AR, since the archive will have a symbol table as well.<br class="">> The archive symbol table enables gold to extract and pass to the<br class="">> plugin the constituent native object-wrapped bitcode files. To support<br class="">> the concatenated llvmbc section generated by “$LD -r”, some handling<br class="">> needs to be added to gold and to the backend driver to process each<br class="">> original module’s bitcode.<br class="">><br class="">><br class="">> The function index/summary will later be added as a special native<br class="">> object section alongside the .llvmbc sections. The offset and size of<br class="">> the corresponding function summary can be placed in the associated<br class="">> symtab entry. As noted above, a separate design document will be sent<br class="">> for the native object format changes.<br class="">><br class="">><br class="">> 2. Stage 2: ThinLTO Infrastructure<br class="">> ------------------------------------------------------<br class="">><br class="">><br class="">> The next set of patches adds the base implementation of the ThinLTO<br class="">> infrastructure, specifically those required to make ThinLTO functional<br class="">> and generate correct but not necessarily high-performing binaries.<br class="">><br class="">><br class="">> a. Clang/LLVM/gold linker options<br class="">><br class="">><br class="">> An early set of clang/llvm patches is needed to provide options to<br class="">> enable ThinLTO (off by default), so that the rest of the<br class="">> implementation can be disabled by default as it is added.<br class="">> Specifically, clang options -fthinlto (used instead of -flto) will<br class="">> cause clang to invoke the phase-1 emission of LLVM bitcode and<br class="">> function summary/index on a compile step, and pass the appropriate<br class="">> option to the gold plugin on a link step. The -thinlto option will be<br class="">> added to the gold plugin and llvm-lto tool to launch the phase-2 thin<br class="">> archive step. The -thinlto-be option will also be added to clang to<br class="">> invoke it as a phase-3 parallel backend instance with a bitcode file<br class="">> as input.<br class="">><br class="">><br class="">> b. Thin-archive linking support in Gold plugin and llvm-lto<br class="">><br class="">><br class="">> Under the new plugin option (see above), the plugin needs to perform<br class="">> the phase-2 (thin archive) link which simply emits a combined function<br class="">> index from the linked modules, without actually performing the normal<br class="">> link. Corresponding support should be added to the standalone llvm-lto<br class="">> tool to enable testing/debugging without involving the linker and<br class="">> plugin.<br class="">><br class="">><br class="">> c. ThinLTO backend support<br class="">><br class="">><br class="">> Support for invoking a phase-3 backend invocation (including<br class="">> importing) on a module should be added to the clang driver under the<br class="">> new option. The main change under the option is to instantiate a<br class="">> Linker object used to manage the process of linking imported functions<br class="">> into the module, efficient read of the combined function index, and<br class="">> enable the ThinLTO import pass.<br class="">><br class="">><br class="">> d. Function index/summary support<br class="">><br class="">><br class="">> This includes infrastructure for writing and reading the function<br class="">> index/summary section. As noted earlier this will be encoded in a<br class="">> special section within the native object file for the module,<br class="">> alongside the .llvmbc section containing the bitcode. The thin archive<br class="">> (combined function index) generated by phase-2 of ThinLTO simply<br class="">> contains all of the function index/summary sections across the linked<br class="">> modules, organized for efficient function lookup. As mentioned earlier<br class="">> when discussing the native object wrapper format, a separate design<br class="">> document will be sent for this format.<br class="">><br class="">><br class="">> Each function available for importing from the module contains an<br class="">> entry in the module’s function index/summary section and in the<br class="">> resulting combined function index. Each function entry contains that<br class="">> function’s offset within the bitcode file, used to efficiently locate<br class="">> and quickly import just that function (see below in 2e for more<br class="">> details on the importing mechanics). The entry also contains summary<br class="">> information (e.g. basic information determined during parsing such as<br class="">> the number of instructions in the function), that will be used to help<br class="">> guide later import decisions. Because the contents of this section<br class="">> will change frequently during ThinLTO tuning, it should also be marked<br class="">> with a version id for backwards compatibility or version checking.<br class="">><br class="">><br class="">> e. ThinLTO importing support<br class="">><br class="">><br class="">> Support for the mechanics of importing functions from other modules,<br class="">> which can go in gradually as a set of patches since it will be off by<br class="">> default (the ThinLTO pass itself discussed below in 2f).<br class="">><br class="">><br class="">> Note that ThinLTO function importing is iterative, and we may import<br class="">> from a number of modules in an interleaved fashion. For example,<br class="">> assume we have hot call chains a()->b1()->c() and a()->b2()->d(),<br class="">> where functions a(), b1()/b2(), c() and d() are from modules A, B, C<br class="">> and D, respectively. When performing ThinLTO backend compilation of<br class="">> module A, we may decide to import in the following order (based on<br class="">> callsite and function summary info):<br class="">> 1. B::b1() # exposes call to c()<br class="">> 2. C::c()<br class="">> 3. B::b2() # exposes call to d()<br class="">> 4. D::d()<br class="">> For this reason, ThinLTO importing is different than regular LTO<br class="">> bitcode reading and linking, which reads and links in a module in its<br class="">> entirety on a single pass through each module (notice in the above<br class="">> example the imports of the two module B functions have an intervening<br class="">> import from module C). As a result, for example, the existing support<br class="">> for lazy metadata parsing that delays it until the first function is<br class="">> materialized can’t be leveraged (metadata handling is discussed more<br class="">> below in 2h). Therefore, the ThinLTO importing pass instantiates a new<br class="">> BitcodeReader and LTOModule object for each function we decide to<br class="">> import, parsing only what is needed and linking in just that function.<br class="">> This is fast and efficient as found in the prototype results shown in<br class="">> the linked EuroLLVM slides.<br class="">><br class="">><br class="">> Separate patches can include:<br class="">><br class="">><br class="">> * BitcodeReader changes to use function index to import/deserialize<br class="">> single function of interest (small changes, leverages existing lazy<br class="">> function streamer support). The declarations and other symbol table<br class="">> info in the bitcode must be reloaded, but the bitcode parsing can stop<br class="">> once the first function body is hit. We simply set up an entry in the<br class="">> lazy streamer’s DeferredFunctionInfo function index map from the<br class="">> bitcode index that was saved in the ThinLTO function summary (and<br class="">> therefore don’t need to build up this function index structure through<br class="">> repeated calls to RememberAndSkipFunctionBody via<br class="">> FindFunctionInStream).<br class="">> * Minor LTOModule changes to pass the ThinLTO function to import and<br class="">> its index into bitcode reader (see 1a for discussion on LTOModule<br class="">> use).<br class="">> * Marking of imported functions. Most handling for ThinLTO imported<br class="">> functions will simply rely on applying the appropriate linkage type.<br class="">> But it is useful to know which functions were imported, both for<br class="">> compiler debugging and and verification, and possibly to modify some<br class="">> optimization heuristics along with the summary information. This can<br class="">> be in-memory initially, but IR support may be required in order to<br class="">> support streaming bitcode out and back in again after importing.<br class="">> * ModuleLinker changes to do ThinLTO-specific symbol linking and<br class="">> static promotion when necessary. The linkage type of imported<br class="">> non-local functions and variables changes to<br class="">> AvailableExternallyLinkage, for example. Statics must be promoted in<br class="">> certain cases, and accordingly renamed in consistent ways. Read-write<br class="">> or address-taken static variables must always be promoted. Other<br class="">> discardable functions, i.e. link-once such as comdats, will be force<br class="">> imported on reference by another imported function. We are working on<br class="">> a separate design document describing these changes in more detail<br class="">> with examples, as a more detailed discussion of these changes is<br class="">> beyond the scope of this RFC.<br class="">> * GlobalDCE changes to support removing imported non-local functions<br class="">> that were not inlined and imported non-local variables, which are<br class="">> marked AvailableExternallyLinkage (very small changes to existing pass<br class="">> logic). As discussed in the original RFC threads, currently GlobalDCE<br class="">> does not remove referenced AvailableExternallyLinkage functions.<br class="">> Instead, these are suppressed later during code generation. It isn’t<br class="">> clear that these functions are useful past the first call to<br class="">> GlobalDCE, which is after inlining, GlobalOpt and IPSCCP (so<br class="">> presumably after inter procedural constant prop, etc). Patch with<br class="">> these changes in testing as discussed in this thread:<br class="">><span class=""> </span><a href="http://lists.cs.uiuc.edu/pipermail/llvmdev/2015-May/085807.html" rel="noreferrer" target="_blank" class="">http://lists.cs.uiuc.edu/pipermail/llvmdev/2015-May/085807.html</a>.<br class="">><br class="">><br class="">> f. ThinLTO Import Driver SCC pass<br class="">><br class="">><br class="">> Adds Transforms/IPO/ThinLTO.cpp with framework for doing ThinLTO via<br class="">> an SCC pass, enabled only under the -fthinlto-be option. The pass<br class="">> includes utilizing the thin archive[2] (combined global function<br class="">> index/summary), import decision heuristics, invocation of<br class="">> LTOModule/ModuleLinker routines that perform the import, and any<br class="">> necessary callgraph updates and verification.<br class="">><br class="">><br class="">> g. Backend Driver<br class="">><br class="">><br class="">> For a single node build, the gold plugin will initially exec the<br class="">> backend processes directly, with the amount of parallelism controlled<br class="">> via an option and/or env variable. It is also possible to leverage<br class="">> existing single node build system task dispatching mechanisms such as<br class="">> Unix Makefiles, Ninja, etc., where the plugin can simply write a build<br class="">> file and fork the parallel backend instances directly under an<br class="">> appropriate option. We will also initially add support for our<br class="">> distributed build system as described below under 3c.<br class="">><br class="">><br class="">> h. Lazy Debug Metadata Linking<br class="">><br class="">><br class="">> The prototype implementation included lazy importing of module-level<br class="">> metadata during the ThinLTO pass finalization (i.e. after all function<br class="">> importing is complete). This actually applies to all module-level<br class="">> metadata, not just debug, although it is the largest. This can be<br class="">> added as a separate set of patches, and the detailed design will be<br class="">> sent with those. Includes changes to BitcodeReader, ValueMapper, and<br class="">> the ModuleLinker classes. As described in 2e, due to the<br class="">> iterative/interleaved nature of ThinLTO importing, the bitcode parsing<br class="">> is structured differently than LTO where a single pass over each<br class="">> module can be performed to parse and materialize all functions and<br class="">> metadata. Therefore, the lazy metadata parsing support in<br class="">> BitcodeReader, which parses all the metadata once the first function<br class="">> is materialized, are not applicable. We may instantiate a<br class="">> BitcodeReader multiple times for a module, if multiple functions are<br class="">> eventually imported, and we need a way to suture up the metadata to<br class="">> the functions imported by an earlier BitcodeReader instantiation. The<br class="">> high level summary is that during the initial import we leave the<br class="">> temporary metadata on the instructions that were imported, but save<br class="">> the index used by the bitcode reader used to correlate with the<br class="">> metadata when it is ready (i.e. the MDValuePtrs index), and skip the<br class="">> metadata parsing. During the ThinLTO pass finalization we parse just<br class="">> the metadata, and suture it up during metadata value mapping using the<br class="">> saved index. As mentioned earlier, this will be described in more<br class="">> detail when the patches are ready.<br class="">><br class="">><br class="">> 3. Stage 3: ThinLTO Tuning and Enhancements<br class="">> -------------------------------------------------------------------------<br class="">><br class="">><br class="">> This refers to the patches that are not required for ThinLTO to work,<br class="">> but rather to improve compile time, memory, run-time performance and<br class="">> usability.<br class="">><br class="">><br class="">> a. Import Tuning<br class="">><br class="">><br class="">> Tuning the import strategy will be an iterative process that will<br class="">> continue to be refined over time. It involves several different types<br class="">> of changes: adding support for recording additional metrics in the<br class="">> function summary, such as profile data and optional heavier-weight IPA<br class="">> analyses, and tuning the import heuristics based on the summary and<br class="">> callsite context.<br class="">><br class="">><br class="">> b. Combined Function Index Pruning<br class="">><br class="">><br class="">> The combined function index can be pruned of functions that are<br class="">> unlikely to benefit from being imported. For example, during the<br class="">> phase-2 thin archive plug step we can safely omit large and (with<br class="">> profile data) cold functions, which are unlikely to benefit from being<br class="">> inlined. Additionally, all but one copy of comdat functions can be<br class="">> suppressed.<br class="">><br class="">><br class="">> c. Distributed Build System Integration<br class="">><br class="">><br class="">> For a distributed build system such as Bazel (<a href="http://bazel.io/" rel="noreferrer" target="_blank" class="">http://bazel.io/</a>), the<br class="">> gold plugin should write the parallel backend invocations into a build<br class="">> file, including the mapping from the IR file to the real object file<br class="">> path, and exit. Additional work needs to be done in the distributed<br class="">> build system itself to distribute and dispatch the parallel backend<br class="">> jobs to the build cluster.<br class="">><br class="">><br class="">> d. Dependence Tracking and Incremental Compiles<br class="">><br class="">><br class="">> In order to support build systems that stage from local disks or<br class="">> network storage, the plugin will optionally support computation of<br class="">> dependent sets of IR files that each module may import from. This can<br class="">> be computed from profile data, if it exists, or from the symbol table<br class="">> and heuristics if not. These dependence sets also enable support for<br class="">> incremental backend compiles.<br class="">><br class="">><br class="">> ________________<br class="">> [1] The following compilers currently wrap intermediate LTO files in<br class="">> native object format: GCC fat and non-fat objects (with a custom<br class="">> symtab), Intel icc non-fat (IR-only) objects (with a full native<br class="">> symtab), HP’s aCC non-fat objects (with full native symtab), IBM xlC<br class="">> both fat and non-fat objects (with full native symtab).<br class="">> [2] The “thin archive” here (also referred to as a combined function<br class="">> index) has some similarities to the AR tool thin archive format, but<br class="">> is not exactly the same. Both contain the symtab and not the code, but<br class="">> the ThinLTO combined function index contains the summary sections as<br class="">> well.<br class="">><br class="">> --<br class="">> Teresa Johnson | Software Engineer |<span class=""> </span><a href="mailto:tejohnson@google.com" target="_blank" class="">tejohnson@google.com</a><span class=""> </span>|<span class=""> </span><a href="tel:408-460-2413" value="+14084602413" target="_blank" class="">408-460-2413</a><br class="">><br class=""></div></div><div class=""><div class="">> _______________________________________________<br class="">> LLVM Developers mailing list<br class="">><span class=""> </span><a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank" class="">LLVMdev@cs.uiuc.edu</a> <a href="http://llvm.cs.uiuc.edu/" rel="noreferrer" target="_blank" class="">http://llvm.cs.uiuc.edu</a><br class="">><span class=""> </span><a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" rel="noreferrer" target="_blank" class="">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br class=""><br class=""></div></div></blockquote></div><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><br clear="all" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><br class=""></div><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important" class="">--<span class=""> </span></span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><span style="font-family:Times;font-size:inherit" class=""><table cellspacing="0" cellpadding="0" class=""><tbody class=""><tr style="color:rgb(85,85,85);font-family:sans-serif;font-size:small" class=""><td nowrap="" style="border-top-style:solid;border-top-color:rgb(213,15,37);border-top-width:2px" class="">Teresa Johnson |</td><td nowrap="" style="border-top-style:solid;border-top-color:rgb(51,105,232);border-top-width:2px" class=""> Software Engineer |</td><td nowrap="" style="border-top-style:solid;border-top-color:rgb(0,153,57);border-top-width:2px" class=""> <a href="mailto:tejohnson@google.com" target="_blank" class="">tejohnson@google.com</a> |</td><td nowrap="" style="border-top-style:solid;border-top-color:rgb(238,178,17);border-top-width:2px" class=""> <a href="tel:408-460-2413" value="+14084602413" target="_blank" class="">408-460-2413</a></td></tr></tbody></table></span></div></div></blockquote></div></div></div><br class=""></div></blockquote></div><br class=""><br clear="all" class=""><div class=""><br class=""></div>-- <br class=""><div class="gmail_signature"><span style="font-family: Times; font-size: inherit;" class=""><table cellspacing="0" cellpadding="0" class=""><tbody class=""><tr style="color:rgb(85,85,85);font-family:sans-serif;font-size:small" class=""><td nowrap="" style="border-top-style:solid;border-top-color:rgb(213,15,37);border-top-width:2px" class="">Teresa Johnson |</td><td nowrap="" style="border-top-style:solid;border-top-color:rgb(51,105,232);border-top-width:2px" class=""> Software Engineer |</td><td nowrap="" style="border-top-style:solid;border-top-color:rgb(0,153,57);border-top-width:2px" class=""> <a href="mailto:tejohnson@google.com" target="_blank" class="">tejohnson@google.com</a> |</td><td nowrap="" style="border-top-style:solid;border-top-color:rgb(238,178,17);border-top-width:2px" class=""> 408-460-2413</td></tr></tbody></table></span></div>
</div></div></div>
</div></blockquote></div><br class=""></div></body></html>