<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Thanks, Devang, this answer a LOT of my questions. I really appreciate the detailed answers.<div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>-- lg</div><div><br></div><div><br><div><div>On Aug 12, 2010, at 4:23 PM, Devang Patel wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Larry,<div><br></div><div><div class="gmail_quote">On Wed, Aug 11, 2010 at 4:55 PM, Larry Gritz <span dir="ltr"><<a href="mailto:lg@larrygritz.com">lg@larrygritz.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
I have a whole slew of questions about optimization passes. Answers to any or all would be extremely helpful:<br>
<br>
How important are doInitialization/doFinalization? </blockquote><div><br></div><div>Most of the passes do not use them.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
I can't detect any difference if I use them or not.</blockquote><div><br></div><div>Say, if you are writing a pass to operate on a function. Naturally, your pass is derived from FunctionPass and it is designed to operate on one function at a time. If you want to prepare the stage or collect some information from the module (which contains all the functions) before you modify any function using your pass then you use doInitialization hook. This is useful because the function passs's runOnFunction only gives you access to the function and not the module surrounding the function. Typically, if your function pass MyFP is operating on a Module M1 with two functions F1 ad F2 in it then execution sequence will be</div>
<div><br></div><div> MyFP->doInitialization(M1);</div><div> MyFP->runOnFunction(F1);</div><div> MyFP->runOnFunction(F2);</div><div> MyFP->doFinalization();</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Why does the function pass manager have doInitialization/doFinalization, but the global pass manager doesn't? </blockquote><div><br></div><div>The global pass already receives access to the module in runOnModule. Since there is not anything that surrounds Module so there is not any thing to initialize. </div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">If I am applying the function passes to many functions, do I doInitialization/run/doFinalization for each function I apply the passes to, or do I initialize/finalize just once?<br>
</blockquote><div><br></div><div>Initialize/finalize should only do things that are not specific to individual functions you are going to operate. See above execution sequence example.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Where is it documented which passes should be part of the global versus which should be function passes? </blockquote><div><br></div><div>I assume you're talking about PassManager vs FunctionPassManager here. </div><div>
<br></div><div>At the time of writing a pass it does not matter. The program construct your pass is operating on usually determines what your pass is derived from. If your pass operates on entire module then it is derived from ModulePass. If your pass operates one function at a time then it is derived from FunctionPass and so on. WritingAnLLVMPass.html explains this.</div>
<div><br></div><div>Once you have a pass, you can run your FunctionPass using either PassManager or FunctionPassManager. However you can not run a ModulePass using FunctionPassManager.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Some *appear* to work with either, some have errors if you use it wrong. I haven't found a way to tell without trying, and no indications that I'm not using it wrong but in a way that's too subtle to crash or catch it.<br>
<br>
Is the best protocol to do function passes first, then global? After that, should you do function passes again for good measure, or is that redundant? Does inlining go best in function or global? </blockquote><div><br>
</div><div>Standard Inlining is a module pass and it can not be run using FunctionPassManager. The docs explicitly lists limitation of a function pass. However....</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Does the strategy change if I really only need to directly call a few functions in the module, the others existing only in case they are needed by the dynamically-generated code that I'm JITing?<br>
<br>
It seems that the combinatorics of which passes to apply, and in which order (including doing some multiple times), are pretty daunting. Other than the Kaleidescope tutorial (too simple) and the StandardPasses.h (too complex?) I can't find any other examples, much less pro/con discussion or guidance. Does anybody have any pointers to web pages or other docs where people have shared their experiences walking through the space of possible pass combinations?<br>
</blockquote><div><br></div><div>I am afraid, there is not any simple doc or magic that explains how to find optimal pass sequence for the optimizer. StandardPasses sequences are result of lot of tuning and analysis of generated code. Your best bet is start using one of the standard pass sequence and add/remove passes to meet your needs.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">The application of all of this for me is to JIT dynamically-generated code, so in contrast to a fully offline compiler, I'm fairly sensitive to achieving a good balance of optimization of the JITed code versus time spent doing the optimizations. In case that changes any of your answers.<br>
<br></blockquote><div><br></div><div>At least one thing, you may not need traditional inliner which uses call graph from the entire module. You may want to take a look at BasicInliner.</div><div> </div></div>-<br>Devang<br>
</div>
</blockquote></div><br><div>
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; ">--<br>Larry Gritz<br><a href="mailto:lg@larrygritz.com">lg@larrygritz.com</a><br><br></span>
</div>
<br></div></body></html>