Daniel Wilkerson
2009-Oct-03 22:06 UTC
[LLVMdev] code generation goes into an infinite loop
We seem to be creating an infinite loop in llvm while trying to implement the simplest possible code generation. Specifically, we are trying to emit code for the following program, as it is easily testable from the command-line: int main(int argc) { return argc; } We got our code generator to build and link with llvm. When we run it, it pauses when running the PrintModulePass and then exits with a bus error. I suspect that this is an infinite loop leading to a stack overflow. While many kinds of errors we could fix by looking at other people's examples or reading the docs, I suspect that this should be considered to be a bug in the verifier pass: clearly we generated wrong LLVM datastructures somehow and the verifier is not catching it before the emitter falls off a cliff. Please comment. Daniel // modified from: // llvm/docs/tutorial/JITTutorial1.html // make an LLVM module printf("%s:%d make module\n", __FILE__, __LINE__); // Module* Mod = makeLLVMModule(); llvm::Module *mod = new llvm::Module("test"); // make a function object printf("%s:%d make function\n", __FILE__, __LINE__); // Constant* c = mod->getOrInsertFunction // ("mul_add", // /*ret type*/ IntegerType::get(32), // /*args*/ IntegerType::get(32), // IntegerType::get(32), // IntegerType::get(32), // /*varargs terminated with null*/ NULL); llvm::Constant *c = mod->getOrInsertFunction ("main", // function name llvm::IntegerType::get(32), // return type llvm::IntegerType::get(32), // one argument (argc) NULL // terminate list of varargs ); llvm::Function *main_function = llvm::cast<llvm::Function>(c); main_function->setCallingConv(llvm::CallingConv::C); // make the body of the function printf("%s:%d make body\n", __FILE__, __LINE__); llvm::Function::arg_iterator args = main_function->arg_begin(); llvm::Value* arg1 = args++; arg1->setName("argc"); printf("%s:%d make block\n", __FILE__, __LINE__); llvm::BasicBlock *block = llvm::BasicBlock::Create("entry", main_function); llvm::IRBuilder<> builder(block); builder.CreateRet(arg1); // verify the module printf("%s:%d verify\n", __FILE__, __LINE__); verifyModule(*mod, llvm::PrintMessageAction); // render the module printf("%s:%d render\n", __FILE__, __LINE__); llvm::PassManager PM; llvm::ModulePass *pmp = llvm::createPrintModulePass(0); // (&llvm::cout); PM.add(pmp); PM.run(*mod); // delete the module printf("%s:%d delete module\n", __FILE__, __LINE__); delete mod;
On Oct 3, 2009, at 3:06 PM, Daniel Wilkerson wrote:> We seem to be creating an infinite loop in llvm while trying to > implement the simplest possible code generation. Specifically, we are > trying to emit code for the following program, as it is easily > testable from the command-line: > > int main(int argc) { return argc; } > > We got our code generator to build and link with llvm. When we run > it, it pauses when running the PrintModulePass and then exits with a > bus error. I suspect that this is an infinite loop leading to a stack > overflow.Are you able to run this under a debugger to locate the infinite loop? Dan
Maybe Matching Threads
- [LLVMdev] LLVM-Kaleidoscope tutorial
- [LLVMdev] LLVM Tutorial 1: Something got wrong?
- [LLVMdev] BlockAddress instruction is copied instead of cloned during module link?
- [LLVMdev] Load from abs address generated bad code on LLVM 2.4
- [LLVMdev] How to place call(s) to functions found in other llvm modules ???