On Tue, Mar 29, 2011 at 4:45 PM, Eric Christopher <span dir="ltr"><<a href="mailto:echristo@apple.com">echristo@apple.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">><br>
> Project Outline:<br>
><br>
><br>
><br>
> Currently, the LLVM JIT serves as a management layer for the executed LLVM IR, it manages the compiled code and calls the LLVM code generator to do the real work. There are levels of optimizations for the LLVM code generator, and depends on how much optimizations the code generator is asked to do, the time taken may vary significantly. The adaptive compilation mechanism should be able to detect when a method is getting hot, compiling or recompiling it using the appropriate optimization levels. Moreover, this should happen transparently to the running application. In order to keep track of how many times a JITed function is called. This involves inserting instrumentational code into the function's LLVM bitcode before it is sent to the code generator. This code will increment a counter when the function is called. And when the counter reaches a threshold, the function gives control back to the LLVM JIT. Then the JIT will look at the hotness of all the methods and find the one that triggered the recompilation threshold. The JIT can then choose to raise the level of optimization based on the algorithm below or some other algorithms developed later.<br>

><br>
><br>
> IF (getCompilationCount(method) > 50 in the last 100 samples) = > Recompile at Aggressive<br>
> ELSE Recompile at the next optimization level.<br>
><br>
><br>
> Even though the invocation counting introduces a few lines of binary, but the advantages of adaptive optimization should far overweigh the extra few lines of binary introduced. Note the adaptive compilation framework I propose here is orthogonal to the LLVM profile-guided optimizations. The profile-guided optimization is a technique used to optimize code with some profiling or external information. But the adaptive compilation framework is concerned with level of optimizations instead of how the optimizations are to be performed.<br>

><br>
<br>
</div>So, one way that current projects use the JIT is via getPointerToFunction() which returns an address that can then be casted and called with the appropriate arguments. The compile task itself is often done on a separate thread. How would you deal with the updating problem in the calling application? What sort of use cases for the JIT have you looked at so far?<br>
</blockquote><div><br></div><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="border-collapse: collapse;">I assume the updating problem means the problem when a method gets recompiled. Here is an algorithm to deal with that. S</span><span class="Apple-style-span" style="border-collapse: collapse; "><span style="color: rgb(0, 0, 0); ">ay A calls B. </span></span><span class="Apple-style-span" style="border-collapse: collapse;">when B gets recompiled we patch B with </span><i style="border-collapse: collapse; color: rgb(0, 0, 0); ">br helper</i><span class="Apple-style-span" style="border-collapse: collapse;"> at the beginning of its code, then when A calls B, B branches to the helper and the helper patches the </span><i style="border-collapse: collapse; color: rgb(0, 0, 0); ">br B</i><span class="Apple-style-span" style="border-collapse: collapse;"> in A with </span><i style="border-collapse: collapse; color: rgb(0, 0, 0); ">br newB</i><span class="Apple-style-span" style="border-collapse: collapse;">. as we don't know all the callers of B, we have to wait until they call B to know who they are and patch them one-by-one. The helper can get the address of the </span><i style="border-collapse: collapse; color: rgb(0, 0, 0); ">br B</i><span class="Apple-style-span" style="border-collapse: collapse;"> in A from the link register or some specific registers or memory locations. For newly compiled code, the address of the newB can be used. There is another problem with recompilation. obsolete methods(methods that have recompiled copies) need to be recycled. In order to do that, we will need to keep a <i>br helper</i> in place of the old method and reclaim the old method body. </span></font></div>
<div class="gmail_quote"><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="color: rgb(80, 0, 80); font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><br>
</span></font></div><div class="gmail_quote"><font class="Apple-style-span" face="'courier new', monospace"><span class="Apple-style-span" style="font-size: 13px; border-collapse: collapse; ">As for use case, the LLVM JIT is used as an execution engine for a few number of ported languages, for example JIT compiler for PHP, in 2008 GSOC. </span><span class="Apple-style-span" style="border-collapse: collapse;"> There are also people using LLVM JIT for industry work, </span><a href="https://llvm.org/svn/llvm-project/www-pubs/trunk/2010-01-Wennborg-Thesis.pdf" target="_blank" style="border-collapse: collapse; font-size: 13px; ">https://llvm.org/svn/llvm-project/www-pubs/trunk/2010-01-Wennborg-Thesis.pdf</a>. <span class="Apple-style-span" style="border-collapse: collapse; font-size: 13px; ">As LLVM is growing more and more powerful, LLVM JIT will become more and more attractive to language designer and implementer. And I think that is one of the most important reasons we need to have an adaptive compilation framework. This framework can also work together with the LLVM profile-guided optimizations to make LLVM JIT a much faster execution engine. </span></font><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div class="im"><br>
><br>
> This is a relatively small project and does not involve a lot of coding, but good portion of the time will be spent benchmarking, tuning and experimenting with different algorithms, i.e. what would be the algorithm to raise the compilation level when a method recompilation threshold is reached, can we make this algorithm adaptive too, etc. Therefore, my timeline for the project is as follow<br>

><br>
><br>
> Week 1<br>
> Benchmarking the current LLVM JIT compiler, measuring compilation speed differences for different levels of compilation. This piece of information is required to understand why a heuristic will outperform others<br>

><br>
</div><div class="im">> Week 10 - 13<br>
> Benchmarking, tuning and experimenting with different recompilation algorithms. Typically benchmarking test cases would be<br>
><br>
<br>
</div>What do you have in mind for benchmarking? Which of the jitted problems were you looking at, or just running large programs through lli and that interface? (Which isn't threaded and therefore doesn't have the problems I mentioned above - it has other problems).<br>
</blockquote><div><span class="Apple-style-span" style="border-collapse: collapse; font-size: 13px; "><span style="border-collapse: collapse; font-size: 13px; "><font class="Apple-style-span" face="'courier new', monospace"><br>
</font></span></span></div><div><span class="Apple-style-span" style="border-collapse: collapse; font-size: 13px; "><font class="Apple-style-span" face="'courier new', monospace"><span style="border-collapse: collapse; font-size: 13px; ">Widely known benchmarks, such as SPEC CPU, would be good candidates.  In addition to these benchmarks, we may want to introduce some specific tests for Just-In-Time compilers, like ones with a small portions of the methods taking up 80%+ of the time and ones with all the methods spend about the same amount of time and ones in the middle of the two.  </span></font></span> </div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im"><br>
><br>
> Week 14<br>
> Test and organize code. Documentation<br>
><br>
<br>
</div>As a general note all of these things would need to be done during the project along with incremental changes made to the repository (on a branch if possible).<br>
<div class="im"><br>
> Overall Goals:<br>
><br>
><br>
> My main goal at the end of the summer is to have an automated profiling and adaptive compilation framework for the LLVM. Even though the performance improvements are still unclear at this point, I believe that this adaptive compilation framework will definitely give noticeable performance benefits, as the current JIT compilation is either too simple to give a reasonably fast code or too expensive to apply to all functions.<br>

<br>
</div>My comments above aside, I think this is a great idea for a project. It is aggressive so the amount of time you put in will likely be larger than a scaled back project.</blockquote><div><br></div><div><font class="Apple-style-span" face="'courier new', monospace">From the questions you asked, I now understand why this project might take more time than I originally anticipated. Thank You.</font></div>
<div><br></div><div>- Xin</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> </blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<font color="#888888"><br>
-eric</font></blockquote></div><br><br clear="all"><br>-- <br>Kind Regards <br><br>Xin Tong <br>