替换LLVM中的指令

时间:2012-03-21 07:15:40

标签: llvm

我想通过调用cumemhostalloc函数替换对malloc的调用。

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

我使用以下代码,

*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);*
}

但我收到以下错误, /home/project/llvmf​​in/llvm-3.0.src/lib/VMCore/Value.cpp:287:void llvm :: Value :: replaceAllUsesWith(llvm :: Value *):断言`New-&gt; getType()== getType()&amp;&amp; “用不同类型的新价值替换所有价值!”'失败了。 是因为对malloc的调用被替换为具有不同签名的函数吗?

1 个答案:

答案 0 :(得分:2)

几乎。调用malloc产生一个值,你的函数 - 没有。因此,您必须用加载替换呼叫,而不是用另一个呼叫替换

另外,查看您的代码:

  • 不要直接使用instlists。改为使用IRBuilder +迭代器
  • 您可以检查CallInst并同时声明var,无需额外强制转换为指令。
相关问题