[LLVMdev] Adding function call in LLVM IR using IRBuilder causes assertion error

Óscar Fuentes ofv at wanadoo.es
Wed Nov 11 09:38:14 PST 2009


Marc Claesen <claesenm at gmail.com> writes:

> I'm trying to add function calls in the LLVM IR using the IRBuilder 
> class. I've read both tutorials about functions but I still get 
> assertion errors. I allocate memory for all function arguments and pass 
> them as explained in the tutorial.
>
> My code is (this function is supposed to add a call to f in bb at pos):
> void addCallSite(Function *f, BasicBlock *bb, BasicBlock::iterator pos) {
>   std::vector<Value*> args;
>   IRBuilder<> builder(bb,pos);
>   for(Function::arg_iterator argit = 
> f->arg_begin();argit!=f->arg_end();argit++){
>       AllocaInst* allocInst = builder.CreateAlloca(argit->getType());
>       args.push_back(allocInst);
>   }
>   builder.CreateCall(f,args.begin(),args.end());
> }
>
> This seems to work for functions without parameters (eg. int foo()), but 
> once a function has a parameter I get the following assertion error:
> <llvmpath>/lib/VMCore/Instructions.cpp:297: void 
> llvm::CallInst::init(llvm::Value*, llvm::Value* const*, unsigned int): 
> Assertion `(i >= FTy->getNumParams() || FTy->getParamType(i) == 
> Params[i]->getType()) && "Calling a function with a bad signature!"' failed.

CreateAlloca(someType) returns an AllocaInst* which has type
someType*. So if your function takes a parameter of type int, with this
chunk of code

       AllocaInst* allocInst = builder.CreateAlloca(argit->getType());
       args.push_back(allocInst);

you are passing an int*. This triggers the assert.

For now, forget about the allocas.

Another issue is that I don't see in your code the _values_ you want to
pass to the called function. Apparently you already have the function
declaration (Function *f) but you are creating a call to that function,
and you need the actual arguments (not the argument types):

inf foo(int); // declaration. you already have this elsewhere.

foo(10); // call. you need the value (10) to pass to the function.

-- 
Óscar




More information about the llvm-dev mailing list