Ralf Karrenberg
2008-Dec-19 13:59 UTC
[LLVMdev] strange behaviour after extracting optimization pass code
Hi, I am expieriencing strange behaviour of llvm's optimization passes and I don't understand what I am doing wrong. Basically all I've done is extracting code for optimization of a llvm-function in a llvm-module and put it into a separate function for better readability. The original code looks like follows (and works as expected): ----------------------------- std::string functionName = "main"; llvm::Module* mod = some arbitrary valid modulepointer; llvm::ExistingModuleProvider mp(mod); llvm::FunctionPassManager fpm(&mp); fpm.add(new llvm::TargetData(mod)); fpm.add(llvm::createInstructionCombiningPass()); fpm.add(llvm::createReassociatePass()); fpm.add(llvm::createGVNPass()); fpm.add(llvm::createCFGSimplificationPass()); fpm.run(*f); //...get execution engine //... call execEngine->getPointerToFunction() //... execute function ----------------------------- Now if I extract exactly this code and put it into a different method like follows, it produces a giant pile of data garbage in the console and any calls receiving the module-pointer after the optimization lead to a segfault. ----------------------------- void optimizeFunction(std::string functionName, llvm::Module* mod) { llvm::Function* f = mod->getFunction(functionName); llvm::ExistingModuleProvider mp(mod); llvm::FunctionPassManager fpm(&mp); fpm.add(new llvm::TargetData(mod)); fpm.add(llvm::createInstructionCombiningPass()); fpm.add(llvm::createReassociatePass()); fpm.add(llvm::createGVNPass()); fpm.add(llvm::createCFGSimplificationPass()); fpm.run(*f); } //in main function: std::string functionName = "main"; llvm::Module* mod = some arbitrary valid modulepointer; optimizeFunction("main", mod); //...get execution engine //... call execEngine->getPointerToFunction() //... execute function ----------------------------- Can anyone help me out here? Thanks in advance :) Cheers, Ralf
Gordon Henriksen
2008-Dec-19 14:15 UTC
[LLVMdev] strange behaviour after extracting optimization pass code
On 2008-12-19, at 06:59, Ralf Karrenberg wrote:> I am expieriencing strange behaviour of llvm's optimization passes > and I don't understand what I am doing wrong. > Basically all I've done is extracting code for optimization of a > llvm-function in a llvm-module and put it into a separate function > for better readability. The original code looks like follows (and > works as expected):Hi Ralf, Your problem is that the ExistingModuleProvider takes ownership of the Module, deleting it when it goes out of scope. You would see the same behavior if you just offset the code with an unnamed block as shown below. The fix is simply to leave the 'mp' variable in the caller so it doesn't go out of scope before you're done with the Module. — Gordon> ----------------------------- > std::string functionName = "main"; > llvm::Module* mod = some arbitrary valid modulepointer;{> > llvm::ExistingModuleProvider mp(mod); > llvm::FunctionPassManager fpm(&mp); > fpm.add(new llvm::TargetData(mod)); > fpm.add(llvm::createInstructionCombiningPass()); > fpm.add(llvm::createReassociatePass()); > fpm.add(llvm::createGVNPass()); > fpm.add(llvm::createCFGSimplificationPass()); > fpm.run(*f);}> //...get execution engine > //... call execEngine->getPointerToFunction() > //... execute function
Ralf Karrenberg
2008-Dec-19 15:44 UTC
[LLVMdev] strange behaviour after extracting optimization pass code
Thanks a lot ! :) Gordon Henriksen schrieb:> On 2008-12-19, at 06:59, Ralf Karrenberg wrote: > > >> I am expieriencing strange behaviour of llvm's optimization passes >> and I don't understand what I am doing wrong. >> Basically all I've done is extracting code for optimization of a >> llvm-function in a llvm-module and put it into a separate function >> for better readability. The original code looks like follows (and >> works as expected): >> > > Hi Ralf, > > Your problem is that the ExistingModuleProvider takes ownership of the > Module, deleting it when it goes out of scope. You would see the same > behavior if you just offset the code with an unnamed block as shown > below. The fix is simply to leave the 'mp' variable in the caller so > it doesn't go out of scope before you're done with the Module. > > — Gordon > > >> ----------------------------- >> std::string functionName = "main"; >> llvm::Module* mod = some arbitrary valid modulepointer; >> > { > >> llvm::ExistingModuleProvider mp(mod); >> llvm::FunctionPassManager fpm(&mp); >> fpm.add(new llvm::TargetData(mod)); >> fpm.add(llvm::createInstructionCombiningPass()); >> fpm.add(llvm::createReassociatePass()); >> fpm.add(llvm::createGVNPass()); >> fpm.add(llvm::createCFGSimplificationPass()); >> fpm.run(*f); >> > } > >> //...get execution engine >> //... call execEngine->getPointerToFunction() >> //... execute function >> > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > >
Reasonably Related Threads
- [LLVMdev] strange behaviour after extracting optimization pass code
- [LLVMdev] strange behaviour after extracting optimization pass code
- [LLVMdev] Pass Scheduling Information without using opt
- [LLVMdev] Running a Local Buildbot
- systemctl restart changes permission.