qbi*_*bit 5 c++ cuda llvm llvm-ir
我正在尝试检索传递给调用的指针的名称cudaMalloc。
CallInst *CUMallocCI = ... ; // CI of cudaMalloc call
Value *Ptr = CUMallocCI->getOperand(0);
if (AllocaInst *AI = dyn_cast<AllocaInst>(Ptr) != nullptr) {
errs() << AI->getName() << "\n";
}
Run Code Online (Sandbox Code Playgroud)
然而上面只是打印一个空行。是否可以从该分配中获取指针名称?
这是相关的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)
Run Code Online (Sandbox Code Playgroud)
回答我自己的问题。事实证明,有一个llvm.dbg.declare与 alloca 相对应的调用 (DbgDeclareInst),但它可能出现在调用函数的基本块中的任何位置。可能是在第一次使用这个 Alloca 值之后?没有把握。无论如何,我的解决方案是搜索DbgDeclareInst指令,检查它是否是一个AllocaInst,如果是,则将该分配器与感兴趣的分配器进行比较,如果相等则获取变量名称。像这样的东西:
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";
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1043 次 |
| 最近记录: |