替换LLVM中的指令
问题描述:
我想用调用cumemhostalloc函数将调用替换为malloc。替换LLVM中的指令
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/llvmfin/llvm-3.0.src/lib/VMCore/Value.cpp :287:void llvm :: Value :: replaceAllUsesWith(llvm :: Value *):断言`New-> getType()== getType()& &“replaceAllUses使用不同类型的新值的值”失败。 是否因为对malloc的调用被替换为具有不同签名的函数?
答
差不多。调用malloc产生一个值,你的函数 - 不。所以,你必须带负荷更换电话,不与其他呼叫
同时,寻找到你的代码:
- 不要instlists直接播放。使用IRBuilder +迭代器代替
- 您可以检查CallInst并同时声明var,无需额外强制转换为Instruction。
请问您能评论吗?如何用负载替换呼叫?是否有可能首先将调用插入到cumemhostalloc中,将malloc的所有用法替换为cumemhostalloc,然后删除对malloc的调用?我应该使用什么指示? – user1203259 2012-03-21 09:58:56