[LLVMdev] Optimization pass questions

Owen Anderson resistor at mac.com
Thu Aug 12 00:11:46 PDT 2010

On Aug 11, 2010, at 6:27 PM, Larry Gritz wrote:

> On Aug 11, 2010, at 5:57 PM, Tanya Lattner wrote:
>> Have you read this document?
>> http://llvm.org/docs/WritingAnLLVMPass.html#passtype
> Yes, but I didn't find it as instructive as I'd hoped.  The only two examples of pass sets I can find are the Kaleidoscope tutorial and StandardPasses.h (corresponding, I assume, to what llvm-gcc does).  Just looking at the two of these, some passes are done as function passes in one, but global passes in the other.  Is that generally ok?  I've experimented and some passes appear to work either way, others complain if you use it wrong.  I've found it hard to deduce, from just these examples and the docs, what the precise rules are, let alone best practices.

I think you're misunderstanding how passes are implemented.  A given pass is a FunctionPass, or a ModulePass, or a LoopPass, etc.  This is fixed in the source code, and doesn't change.

What I assume you're referring to is what kind of PassManager you use to run your passes.  You're way over-thinking the problem; just instantiate an instance of PassManager, add your passes, and run it on your Module.  PassManager's are hierarchical; if you have a "wider" pass manager and ask it to run a Pass of a "narrower" type (like running a FunctionPass on a ModulePassManager), it will implicitly create a hidden FunctionPassManager to run it for you. 

The takeaway is that, unless you need a lot more manual control, just use a basic PassManager.

As to pass orderings and dependencies, every pass informs the PassManager of its dependencies, and the PassManager will do its best to create an overall pass schedule to satisfy them.  That said, it's not THAT smart, so it is possible to request a set of passes in a particular order that confuses it and leaves it unable to create a valid schedule.

> Well just as an example, let's say you have a bunch of functions in a module, and you want to apply your function passes to each of them.
> * Do you need to construct a separate FPM for each function, or can you construct it once and use it in succession for each function in the module?

You should never be creating FPMs yourself unless you know exactly what you're doing.  Create a standard PassManager, and let it figure what to iterate over.  You should not be manually running it on individual functions unless you need that kind of manual control.

> * If the latter, do you doInitialization just once, then run() on each function, then doFinalization just once?  Or do you have to init/run/final separately for every function in the module?

Similarly, you should never be calling those methods.  Let the high-level PassManager do it for you.

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

More information about the llvm-dev mailing list