llvm instrumentation failed


I reviewed some questions about instrumentation and try to learn from them to do a simple instrumentation by writing a new pass.

For example, for every basicblock, I would like to assign a block id and call the printf or my own print function to handle the block id. Suppose the block id is a string.

My code is below:

int inst_blocks = 0;

LLVMContext& Ctx = M.getContext();

Constant* c = M.getOrInsertFunction(“printf”,Type::getVoidTy(Ctx), Type::getInt32Ty(Ctx), NULL);

Function* myprint = cast(c);

for (auto &F : M)

for (auto &BB : F) {


BasicBlock::iterator IP = BB.getFirstInsertionPt();

IRBuilder<> IRB(&(*IP));

Value * ID = IRB.CreateGlobalStringPtr(StringRef(std::to_string(inst_blocks)));

Value* args[] = {ID};

IRB.CreateCall(myprint, args);


I compiled the pass successfully and then try to use -Xclang -load -Xclang pass.so to compile my target C code. However, I come across the below exception

clang-4.0: /home/jmh/Downloads/llvm-4/llvm/lib/IR/Instructions.cpp:263: void llvm::CallInst::init(llvm::FunctionType*, llvm::Value*, llvm::ArrayRefllvm::Value*, llvm::ArrayRef<llvm::OperandBundleDefTllvm::Value* >, const llvm::Twine&): Assertion `(i >= FTy->getNumParams() || FTy->getParamType(i) == Args[i]->getType()) && “Calling a function with a bad signature!”’ failed.

I even do not know how to debug. Do I miss anything important? Thank you so much for your help