user1203259
user1203259

Reputation: 95

Replacing instructions in LLVM

I want to replace the call to malloc with call to 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 get 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 of value with new value of different type!"' failed. Is it because the call to malloc is replaced with a function that has a different signature?

Upvotes: 3

Views: 2705

Answers (1)

Anton Korobeynikov
Anton Korobeynikov

Reputation: 9324

Almost. Call to malloc produce a value, your function - does not. So, you have to replace call with a load, not with another call

Also, looking into your code:

  • Do not play with instlists directly. Use IRBuilder + iterators instead
  • You can check for CallInst and declare var at the same time, no need to additional cast to Instruction.

Upvotes: 2

Related Questions