Replacing instructions in LLVM

I want to replace the call to malloc with a call to the cumemhostalloc function.

float *h_A=(float *)malloc(size); 
should be replaced with
cuMemHostAlloc((void **)&h_A,size,2);

      

I use the following code for this:

*if (dyn_cast<CallInst> (j))
{
    Ip=cast<Instruction>(j);
    CastInst* ci_hp = new BitCastInst(ptr_h_A, PointerTy_23, "" );
    BB->getInstList().insert(Ip,ci_hp);
    errs()<<"\n Cast instruction is inserted"<<*ci_hp;
    li_size = new LoadInst(al_size, "", false);
    li_size->setAlignment(4);
    BB->getInstList().insert(Ip,li_size);
    errs()<<"\n Load instruction is inserted"<<*li_size;
    ConstantInt* const_int32_34 = ConstantInt::get(M->getContext(), APInt(32, StringRef("2"), 10));

    std::vector<Value*> cumemhaparams;
    cumemhaparams.push_back(ci_hp);
    cumemhaparams.push_back(li_size);
    cumemhaparams.push_back(const_int32_34);
    CallInst* cumemha = CallInst::Create(func_cuMemHostAlloc, cumemhaparams, "");
    cumemha->setCallingConv(CallingConv::C);
    cumemha->setTailCall(false);
    AttrListPtr cumemha_PAL;
    cumemha->setAttributes(cumemha_PAL);

    ReplaceInstWithInst(callinst->getParent()->getInstList(), j,cumemha);*
}

      

But I am getting the following error, / home / project / llvmfin / llvm -3.0.src / lib / VMCore / Value.cpp: 287: void llvm :: Value :: replaceAllUsesWith (llvm :: Value *): Assertion `New-> getType () == getType () && "replaceAllUses values ​​with a new value of a different type!" "failed. Is it because the malloc call is being replaced with a function with a different signature?

+3


source to share


1 answer


Nearly. A call to malloc produces a value, your function does not. So you have to replace the call with the load and not another call

Also, take a look at your code:



  • Don't play with Instlists directly. Use IRBuilder + iterators instead.
  • You can check CallInst and declare var at the same time, no need to add to the command.
+2


source







All Articles