Son Tuan VU via llvm-dev
2018-Jun-12 13:46 UTC
[llvm-dev] ModulePass cannot be registered as EarlyAsPossible
Hello all, I've followed the example in https://github.com/CompilerTeaching/SimplePass/blob/master/SimplePass.cc in order to create a custom pass. The pass needs to be added before any transformation, so I used EP_EarlyAsPossible extension point to register it. Furthermore, I need to access to every GlobalVariable in the IR, so my pass has to be a ModulePass, like this: struct MyPass : public ModulePass { static char ID; MyPass(): ModulePass(ID) {} virtual bool runOnModule(Module &M) {...} ... } However, every time I try to access to the Module object M inside runOnModule(), clang just crashes. Even a debug message like *outs() << M.getName() << '\n';* would cause a segfault. So am I doing something wrong, like EP_EarlyAsPossible is really not to be used with ModulePasses, or is this rather a bug? In case this is not a bug, what would be the best way to manipulate an IR Module as it is coming right out of the frontend? Thanks for your help, Son Tuan Vu -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180612/8276709b/attachment.html>
Bevin Hansson via llvm-dev
2018-Jun-13 14:01 UTC
[llvm-dev] ModulePass cannot be registered as EarlyAsPossible
Hi, EP_EarlyAsPossible only works with FunctionPasses, yes. If you look at how it's used in PassManagerBuilder, it is only invoked from populateFunctionPassManager. The earliest you can add ModulePasses is with EP_ModuleOptimizerEarly. However, those passes are not added on O0. If the OptLevel is 0, you can instead add the passes to EP_EnabledOnOptLevel0. This might not be early enough for you, but those are the points that are available for module passes as far as I know. Regards, Bevin On 2018-06-12 15:46, Son Tuan VU via llvm-dev wrote:> Hello all, > > I've followed the example in > https://github.com/CompilerTeaching/SimplePass/blob/master/SimplePass.cc > in order to create a custom pass. > > The pass needs to be added before any transformation, so I used > EP_EarlyAsPossible extension point to register it. Furthermore, I need > to access to every GlobalVariable in the IR, so my pass has to be a > ModulePass, like this: > > struct MyPass : public ModulePass { > static char ID; > MyPass(): ModulePass(ID) {} > virtual bool runOnModule(Module &M) {...} > ... > } > > However, every time I try to access to the Module object M inside > runOnModule(), clang just crashes. Even a debug message like *outs() > << M.getName() << '\n';* would cause a segfault. So am I doing > something wrong, like EP_EarlyAsPossible is really not to be used with > ModulePasses, or is this rather a bug? > > In case this is not a bug, what would be the best way to manipulate an > IR Module as it is coming right out of the frontend? > > Thanks for your help, > > Son Tuan Vu > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180613/0d01234b/attachment.html>
Son Tuan VU via llvm-dev
2018-Jun-13 14:07 UTC
[llvm-dev] ModulePass cannot be registered as EarlyAsPossible
Hi Bevin, Thank you for your reply. Unfortunately, I was using EP_ModuleOptimizerEarly and I just realized that it is not early enough, since the FunctionPassManager does apply some basic optimization on the functions before any Module optimizations... So there's no way to insert a Module pass earlier than ModuleOptimizerEarly? I will try to hack it as Björn pointed out then. Thanks for your help, Son Tuan Vu On Wed, Jun 13, 2018 at 4:01 PM, Bevin Hansson via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hi, > > EP_EarlyAsPossible only works with FunctionPasses, yes. If you look at how > it's used in PassManagerBuilder, it is only invoked from > populateFunctionPassManager. > > The earliest you can add ModulePasses is with EP_ModuleOptimizerEarly. > However, those passes are not added on O0. If the OptLevel is 0, you can > instead add the passes to EP_EnabledOnOptLevel0. > > This might not be early enough for you, but those are the points that are > available for module passes as far as I know. > > Regards, > Bevin > > On 2018-06-12 15:46, Son Tuan VU via llvm-dev wrote: > > Hello all, > > I've followed the example in https://github.com/ > CompilerTeaching/SimplePass/blob/master/SimplePass.cc in order to create > a custom pass. > > The pass needs to be added before any transformation, so I used > EP_EarlyAsPossible extension point to register it. Furthermore, I need to > access to every GlobalVariable in the IR, so my pass has to be a > ModulePass, like this: > > struct MyPass : public ModulePass { > static char ID; > MyPass(): ModulePass(ID) {} > virtual bool runOnModule(Module &M) {...} > ... > } > > However, every time I try to access to the Module object M inside > runOnModule(), clang just crashes. Even a debug message like *outs() << > M.getName() << '\n';* would cause a segfault. So am I doing something > wrong, like EP_EarlyAsPossible is really not to be used with ModulePasses, > or is this rather a bug? > > In case this is not a bug, what would be the best way to manipulate an IR > Module as it is coming right out of the frontend? > > Thanks for your help, > > Son Tuan Vu > > > _______________________________________________ > LLVM Developers mailing listllvm-dev at lists.llvm.orghttp://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180613/d2cb26f8/attachment.html>