本文介绍了LLVM检索AllocaInst的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取传递给调用。

I am trying to retrieve the name of the pointer passed to a cudaMalloc call.

CallInst *CUMallocCI = ... ; // CI of cudaMalloc call
Value *Ptr = CUMallocCI->getOperand(0);
if (AllocaInst *AI = dyn_cast<AllocaInst>(Ptr) != nullptr) {
  errs() << AI->getName() << "\n";
}

上面的内容只是打印一个空行。

The above however just prints an empty line. Is is possible to get the pointer name out of this alloca?

这是相关的IR:

%28 = alloca i8*, align 8
...
...
call void @llvm.dbg.declare(metadata i8** %28, metadata !926, metadata !DIExpression()), !dbg !927
%257 = call i32 @cudaMalloc(i8** %28, i64 1), !dbg !928
...
...
!926 = !DILocalVariable(name: "d_over", scope: !677, file: !3, line: 191, type: !22)
!927 = !DILocation(line: 191, column: 10, scope: !677)


推荐答案

回答我自己的问题。事实证明,对应于alloca的 llvm.dbg.declare 调用(DbgDeclareInst),但它可能出现在调用者函数的基本块中的任何位置。可能是在首次使用此Alloca值之后?不确定。无论如何,我的解决方案是搜索 DbgDeclareInst 指令,检查它是否用于 AllocaInst ,如果比较具有感兴趣的alloca的alloca,如果相等,则获取变量名称。像这样的东西:

Answering my own question. It turns out that there is an llvm.dbg.declare call (DbgDeclareInst) corresponding to the alloca but it may appear anywhere in the caller function's basic blocks. Probably it comes after the first use of this Alloca value? Not sure. In any case, my solution is to search for DbgDeclareInst instructions, check if it is for an AllocaInst and if so compare that alloca with the alloca of interest and if equal get the variable name. Something like this:

CallInst *CUMallocCI = ... ; // CI of cudaMalloc call
Value *Ptr = CUMallocCI->getOperand(0);
if (AllocaInst *AI = dyn_cast<AllocaInst>(Ptr) != nullptr) {
  if ( !AI->hasName() ) {
    // Function this AllocaInst belongs
    Function *Caller = AI->getParent()->getParent();
    // Search for llvm.dbg.declare
    for ( BasicBlock& BB : *Caller)
      for (Instruction &I : BB) {
        if ( DbgDeclareInst *dbg = dyn_cast<DbgDeclareInst>(&I))
          // found. is it for an AllocaInst?
          if ( AllocaInst *dbgAI = dyn_cast<AllocaInst>(dbg->getAddress()))
            // is it for our AllocaInst?
            if (dbgAI == AI)
              if (DILocalVariable *varMD = dbg->getVariable()) // probably not needed?
                errs() << varMD->getName() << "\n";
  } else {
    errs() << AI->getName() << "\n";
  }
}

这篇关于LLVM检索AllocaInst的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-11 04:45