[LLVMdev] Optimization pass questions

Devang Patel devang.patel at gmail.com
Thu Aug 12 16:23:54 PDT 2010


On Wed, Aug 11, 2010 at 4:55 PM, Larry Gritz <lg at larrygritz.com> wrote:

> I have a whole slew of questions about optimization passes.  Answers to any
> or all would be extremely helpful:
> How important are doInitialization/doFinalization?

Most of the passes do not use them.

>  I can't detect any difference if I use them or not.

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


>  Why does the function pass manager have doInitialization/doFinalization,
> but the global pass manager doesn't?

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

> 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?

Initialize/finalize should only do things that are not specific to
individual functions you are going to operate. See above execution sequence

> Where is it documented which passes should be part of the global versus
> which should be function passes?

I assume you're talking about PassManager vs FunctionPassManager here.

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.

Once you have a pass, you can run your FunctionPass using either PassManager
or FunctionPassManager. However you can not run a ModulePass using

>  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.
> 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?

Standard Inlining is a module pass and it can not be run using
FunctionPassManager. The docs explicitly lists limitation of a function
pass. However....

>  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?
> 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?

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

> 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.
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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100812/68724b26/attachment.html>

More information about the llvm-dev mailing list