【问题标题】:Replacing instructions in LLVM替换 LLVM 中的指令
【发布时间】:2012-04-04 05:29:58
【问题描述】:

我想用对 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->getType() == getType () && "replaceAllUses of value with new value of different type!"' 失败。 是不是因为对 malloc 的调用被替换为具有不同签名的函数?

【问题讨论】:

  • 您能详细说明您的评论吗?如何用负载代替呼叫?是否可以先插入对 cumemhostalloc 的调用,用 cumemhostalloc 替换所有 malloc 的使用,然后删除对 malloc 的调用?我应该使用什么说明?

标签: llvm


【解决方案1】:

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

另外,查看您的代码:

  • 不要直接玩 instlists。改用 IRBuilder + 迭代器
  • 您可以同时检查 CallInst 和声明 var,无需额外转换为 Instruction。

【讨论】:

    猜你喜欢
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 2017-09-04
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    相关资源
    最近更新 更多