<div dir="ltr"><div>I think the "Motivation" section explained that. I too first thought about "why not at IR?" but the reason looks like MIR, post-RA has the most accurate heuristics (best way to know looks like actually getting there).<br><br></div><div>Do you know if there is any experimental pass that relies on deriving heuristics by a feedback loop after letting, ie. a duplicate module/function/block continue past?<br><br></div><div>Regards,<br></div><div>Kevin<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 26 August 2016 at 14:36, Hal Finkel via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Jessica,<br>
<br>
This is quite interesting.<br>
<br>
Can you comment on why you started by doing this at the MI level, as opposed to the IR level. And at the MI level, why after RA instead of before RA?<br>
<br>
Perhaps the first question is another way of asking about how accurately we can model call-site code size at the IR level?<br>
<br>
Thanks in advance,<br>
Hal<br>
<div class="HOEnZb"><div class="h5"><br>
----- Original Message -----<br>
> From: "Jessica Paquette via llvm-dev" <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>><br>
> To: <a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
> Sent: Friday, August 26, 2016 4:26:09 PM<br>
> Subject: [llvm-dev] [RFC] Interprocedural MIR-level outlining pass<br>
><br>
> Hi everyone,<br>
><br>
> Since I haven't said anything on the mailing list before, a quick<br>
> introduction. I'm an intern at Apple, and over the summer I<br>
> implemented a<br>
> prototype for an outlining pass for code size in LLVM. Now I'm<br>
> seeking to<br>
> eventually upstream it. I have the preliminary code on GitHub right<br>
> now,<br>
> but it's still very prototypical (see the code section).<br>
><br>
> ==============================<wbr>==<br>
> Motivation<br>
> ==============================<wbr>==<br>
> The goal of the internship was to create an interprocedural pass that<br>
> would reduce code size as much as possible, perhaps at the cost of<br>
> some<br>
> performance. This would be useful to, say, embedded programmers who<br>
> only<br>
> have a few kilobytes to work with and a substantial amount of code to<br>
> fit<br>
> in that space.<br>
><br>
><br>
> ==============================<wbr>==<br>
> Approach and Initial Results<br>
> ==============================<wbr>==<br>
> To do this, we chose to implement an outliner. Outliners find<br>
> sequences of<br>
> instructions which would be better off as a function call, by some<br>
> measure<br>
> of "better". In this case, the measure of "better" is "makes code<br>
> smaller".<br>
><br>
><br>
> ==============================<wbr>==<br>
> Results<br>
> ==============================<wbr>==<br>
> These results are from a fairly recent 64-bit Intel processor, using<br>
> a<br>
> version of Clang equipped with the outliner prototype versus an<br>
> equivalent<br>
> version of Clang without the outliner.<br>
><br>
> CODE SIZE REDUCTION<br>
> For tests >=4 Kb in non-outlined size, the outliner currently<br>
> provides an<br>
> average of 12.94% code size reduction on the LLVM test suite in<br>
> comparison<br>
> to a default Clang, up to 51.4% code size reduction. In comparison to<br>
> a<br>
> Clang with -Oz, the outliner provides an average of a 1.29% code size<br>
> reduction, up to a 37% code size reduction. I believe that the -Oz<br>
> numbers<br>
> can be further improved by better tuning the outlining cost model.<br>
><br>
> EXECUTION TIME IMPACT<br>
> On average, the outliner increases execution time by 2% on the LLVM<br>
> test<br>
> suite, but has been also shown to improve exection time by up to 16%.<br>
> These results were from a fairly recent Intel processor, so the<br>
> results<br>
> may vary. Recent Intel processors have very low latency for function<br>
> calls, which may impact these results. Execution time improvements<br>
> are<br>
> likely dependent on the latency of function calls, instruction<br>
> caching<br>
> behaviour, and the execution frequency of the code being outlined. In<br>
> partucular, using a processor with heavy function call latency will<br>
> likely<br>
> increase execution time overhead.<br>
><br>
><br>
> ==============================<wbr>==<br>
> Implementation<br>
> ==============================<wbr>==<br>
> The outliner, in its current state, is a MachineModulePass. It finds<br>
> *identical* sequences of MIR, after register allocation, and pulls<br>
> them<br>
> out into their own functions. Thus, it's effectively assembly-level.<br>
> Ultimately, the algorithm used is general, so it can sit anywhere,<br>
> but MIR<br>
> was very convenient for the time being.<br>
><br>
> It requires two data structures.<br>
><br>
> 1. A generalized suffix tree<br>
> 2. A "terminated string"<br>
><br>
> 1: The generalized suffix tree is constructed using Ukkonen's linear<br>
> time<br>
> construction algorithm [1]. They require linear space and support<br>
> linear-time substring queries. In practice, the construction time for<br>
> the<br>
> suffix tree is the most time consuming part, but I haven't noticed a<br>
> difference in compilation time on, say, 12 MB .ll files.<br>
><br>
> 2: To support the suffix tree, we need a "terminated string." This is<br>
> a<br>
> generalized string with an unique terminator character appended to<br>
> the<br>
> end. TerminatedStrings can be built from any type.<br>
><br>
> The algorithm is then roughly as follows.<br>
><br>
> 1. For each MachineBasicBlock in the program, build a<br>
> TerminatedString for<br>
> that block.<br>
> 2. Build a suffix tree for that collection of strings.<br>
> 3. Query the suffix tree for the longest repeated substring and place<br>
> that<br>
> string in a candidate list. Repeat until none are found.<br>
> 4. Create functions for each candidate.<br>
> 5. Replace each candidate with a call to its respective function.<br>
><br>
> Currently, the program itself isn't stored in the suffix tree, but<br>
> rather<br>
> a "proxy string" of integers. This isn't necessary at the MIR level,<br>
> but<br>
> may be for an IR level extension of the algorithm.<br>
><br>
><br>
> ==============================<wbr>==<br>
> Challenges<br>
> ==============================<wbr>==<br>
> 1) MEMORY CONSUMPTION<br>
> Given a string of length n, a naive suffix tree implementation can<br>
> take up<br>
> to 40n bytes of memory. However, this number can be reduced to 20n<br>
> with a<br>
> bit of work [2]. Currently, the suffix tree stores the entire<br>
> program,<br>
> including instructions which really ought not to be outlined, such as<br>
> returns. These instructions should not be included in the final<br>
> implementation, but should rather act as terminators for the strings.<br>
> This<br>
> will likely curb memory consumption. Suffix trees have been used in<br>
> the<br>
> past in sliding-window-based compression schemes, which may serve as<br>
> a<br>
> source of inspiration for reducing memory overhead.[3]<br>
><br>
> Nonetheless, the outliner probably shouldn't be run unless it really<br>
> has<br>
> to be run. It will likely be used mostly in embedded spaces, where<br>
> the<br>
> programs have to fit into small devices anyway. Thus, memory overhead<br>
> for<br>
> the compiler likely won't be a problem. The outliner should only be<br>
> used<br>
> in -Oz compilations, and possibly should have its own flag.<br>
><br>
><br>
> 2) EXECUTION TIME<br>
> Currently, the outliner isn't tuned for preventing execution time<br>
> increases. There is an average of a 2% execution time hit on the<br>
> tests in<br>
> the LLVM test suite, with a few outliers of up to 30%. The outliers<br>
> are<br>
> tests which contain hot loops. The outliner really ought to be able<br>
> to use<br>
> profiling information and not outline from hot areas. Another<br>
> suggestion<br>
> people have given me is to add a "never outline" directive which<br>
> would<br>
> allow the user to say something along the lines of "this is a hot<br>
> loop,<br>
> please never outline from it".<br>
><br>
> It's also important to note that these numbers are coming from a<br>
> fairly<br>
> recent Intel processor.<br>
><br>
><br>
> 3) CONSTRAINTS ON INSTRUCTIONS<br>
> The outliner currently won't pull anything out of functions which use<br>
> a<br>
> red zone. It also won't pull anything out that uses the stack,<br>
> instruction<br>
> pointer, uses constant pool indices, CFI indices, jump table indices,<br>
> or<br>
> frame indices. This removes many opportunities for outlining which<br>
> would<br>
> likely be available at a higher level (such as IR). Thus, there's a<br>
> case<br>
> for moving this up to a higher level.<br>
><br>
><br>
> ==============================<wbr>==<br>
> Additional Applications<br>
> ==============================<wbr>==<br>
> The suffix tree itself could be used as a tool for finding<br>
> opportunities<br>
> to refactor code. For example, it could recognize places where the<br>
> user<br>
> likely copied and pasted some code. This could be run on codebases to<br>
> find<br>
> areas where people could manually outline things at the source level.<br>
><br>
> Using the terminated string class, it would also be possible to<br>
> implement<br>
> other string algorithms on code. This may open the door to new ways<br>
> to<br>
> analyze existing codebases.<br>
><br>
><br>
> ==============================<wbr>==<br>
> Roadmap<br>
> ==============================<wbr>==<br>
> The current outliner is *very* prototypical. The version I would want<br>
> to<br>
> upstream would be a new implementation. Here's what I'd like to<br>
> address<br>
> and accomplish.<br>
><br>
> 1. Ask "what does the LLVM community want from an outliner" and use<br>
> that<br>
> to drive development of the algorithm.<br>
> 2. Reimplement the outliner, perhaps using a less memory-intensve<br>
> data<br>
> structure like a suffix array.<br>
> 3. Begin adding features to the algorithm, for example:<br>
>     i.   Teaching the algorithm about hot/cold blocks of code and<br>
>     taking<br>
> that into account.<br>
>     ii.  Simple parameter passing.<br>
>     iii. Similar function outlining-- eg, noticing that two outlining<br>
> candidates are similar and can be merged into one function with some<br>
> control flow.<br>
><br>
><br>
> ==============================<wbr>==<br>
> Code<br>
> ==============================<wbr>==<br>
> Note: This code requires MachineModulePasses<br>
><br>
> * Main pass:<br>
> <a href="https://github.com/ornata/llvm/blob/master/lib/CodeGen/MachineOutliner.h" rel="noreferrer" target="_blank">https://github.com/ornata/<wbr>llvm/blob/master/lib/CodeGen/<wbr>MachineOutliner.h</a><br>
><br>
> * Suffix tree:<br>
> <a href="https://github.com/ornata/llvm/blob/master/include/llvm/ADT/SuffixTree.h" rel="noreferrer" target="_blank">https://github.com/ornata/<wbr>llvm/blob/master/include/llvm/<wbr>ADT/SuffixTree.h</a><br>
><br>
> * TerminatedString and TerminatedStringList:<br>
> <a href="https://github.com/ornata/llvm/blob/master/include/llvm/ADT/TerminatedString.h" rel="noreferrer" target="_blank">https://github.com/ornata/<wbr>llvm/blob/master/include/llvm/<wbr>ADT/TerminatedString.h</a><br>
><br>
> Here are a couple unit tests for the data structures.<br>
><br>
> * Suffix tree unit tests:<br>
> <a href="https://github.com/ornata/llvm/blob/master/unittests/ADT/SuffixTreeTest.cpp" rel="noreferrer" target="_blank">https://github.com/ornata/<wbr>llvm/blob/master/unittests/<wbr>ADT/SuffixTreeTest.cpp</a><br>
><br>
> * TerminatedString unit tests:<br>
> <a href="https://github.com/ornata/llvm/blob/master/unittests/ADT/TerminatedStringTest.cpp" rel="noreferrer" target="_blank">https://github.com/ornata/<wbr>llvm/blob/master/unittests/<wbr>ADT/TerminatedStringTest.cpp</a><br>
><br>
> * TerminatedStringList unit tests:<br>
> <a href="https://github.com/ornata/llvm/blob/master/unittests/ADT/TerminatedStringListTest.cpp" rel="noreferrer" target="_blank">https://github.com/ornata/<wbr>llvm/blob/master/unittests/<wbr>ADT/TerminatedStringListTest.<wbr>cpp</a><br>
><br>
><br>
> ==============================<wbr>==<br>
> References<br>
> ==============================<wbr>==<br>
> [1] Ukkonen's Algorithm:<br>
> <a href="https://www.cs.helsinki.fi/u/ukkonen/SuffixT1withFigs.pdf" rel="noreferrer" target="_blank">https://www.cs.helsinki.fi/u/<wbr>ukkonen/SuffixT1withFigs.pdf</a><br>
> [2] Suffix Trees and Suffix Arrays:<br>
> <a href="http://web.cs.iastate.edu/~cs548/suffix.pdf" rel="noreferrer" target="_blank">http://web.cs.iastate.edu/~<wbr>cs548/suffix.pdf</a><br>
> [3] Extended Application of Suffix Trees to Data Compression:<br>
> <a href="http://www.larsson.dogma.net/dccpaper.pdf" rel="noreferrer" target="_blank">http://www.larsson.dogma.net/<wbr>dccpaper.pdf</a><br>
><br>
><br>
> Thanks for reading,<br>
> Jessica<br>
><br>
> ______________________________<wbr>_________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
><br>
<br>
</div></div><span class="HOEnZb"><font color="#888888">--<br>
Hal Finkel<br>
Assistant Computational Scientist<br>
Leadership Computing Facility<br>
Argonne National Laboratory<br>
</font></span><div class="HOEnZb"><div class="h5">______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
</div></div></blockquote></div><br></div>