[LLVMdev] How to call some transformation passes (LoopRotate and LoopUnroll) from my own pass

Jorge Navas jorge at clip.dia.fi.upm.es
Tue Aug 7 21:11:58 PDT 2012


Thanks!

I understand now much better.

Jorge
On Monday, August 6, 2012 at 10:25:24 (-0700), Andrew Trick wrote:
>> 
>> On Aug 6, 2012, at 6:04 AM, Jorge Navas <navas at comp.nus.edu.sg> wrote:
>> 
>> > 
>> > Hello,
>> > 
>> > I wrote my own pass which needs to do some loop unrolling.
>> > 
>> > I can perform loop unrolling via opt:
>> > 
>> > opt -mem2reg -loops -loop-simplify -loop-rotate -lcssa -loop-unroll
>> > -unroll-count=50 mytest.bc -o mytest.bc
>> > 
>> > This command works perfectly. 
>> > 
>> > However, what I really want is to produce the **same behavior** but
>> > from my own pass (i.e., I don't want to use opt). I wrote a Module
>> > pass which already calls other analysis and transformation passes in
>> > its getAnalysisUsage method:
>> > 
>> > 
>> > void MyPass::getAnalysisUsage(AnalysisUsage& AU) const {
>> >  AU.addRequired<LoopInfo>();
>> >  AU.addPreserved<LoopInfo>();
>> >  AU.addRequiredID(LoopSimplifyID);
>> >  AU.addPreservedID(LoopSimplifyID);
>> >  AU.addRequiredID(LCSSAID);
>> >  AU.addPreservedID(LCSSAID);
>> > }    
>> > 
>> > However, I couldn't figure out how to call the LoopRotate and
>> > LoopUnroll passes since I cannot use addRequiredID or addRequired for
>> > these two transformations.
>> > 
>> > In Scalar.h, I found:
>> > 
>> >> Pass *createLoopUnrollPass(int Threshold = -1, int Count = -1, int AllowPartial = -1);
>> >> Pass *createLoopRotatePass();
>> > 
>> > 
>> > Is to call these methods the way to go? How?, can somebody show me an
>> > excerpt?
>> 
>> The create*Pass methods give you a Pass instance that you can add to a pass manager. If you're replacing "opt", you should look at the opt driver, opt.cpp to see how to do this. You can call addRequired<X> from your pass to force the PassManager to run X before your pass.
>> 
>> If you want to do these transformations within a single pass, then the transformations need to be exposed as a utility. llvm::UnrollLoop() currently is, but LoopRotation is not. LoopRotation could be exposed as a utility (or you could copy the relevant code), but you should try not to do that. The llvm strategy is that loop rotation should do the right thing when it runs before your pass. If loop rotation needs more information to what you want it to do, then you can create a some new analysis that loop rotation requires.
>> 
>> You may want to try browsing the online docs, such as...
>> http://llvm.org/docs/WritingAnLLVMPass.html
>> 
>> Hope that helps.
>> 
>> -Andy



More information about the llvm-dev mailing list