如何为LLVM IR生成元数据? [英] How to generate metadata for LLVM IR?
问题描述
我正在尝试为我生成的LLVM IR生成元数据.我想生成以下格式的元数据:
I am trying to generate a metadata for the LLVM IR i have generated. I want to generate a metadata of the form :
!nvvm.annotations = !{!0}
!0 = metadata !{void ()* @foo, metadata !"kernel", i32 1}
foo是我的LLVM IR中的函数.现在,我只能生成以下格式的元数据:
Where foo is a function in my LLVM IR. Right now I am only able to generate a metadata of the form:
!nvvm.annotations = !{!0}
!0 = !{!"kernel"}
在上面的元数据生成中,我使用了以下代码.
I used the following code for the above metadata generation.
char metaDataArgument[512];
sprintf(metaDataArgument, "%s", pipelineKernelName);
llvm::NamedMDNode *nvvmMetadataNode = LLVMModule->getOrInsertNamedMetadata("nvvm.annotations");
llvm::MDNode *MDNOdeNVVM = llvm::MDNode::get(*context, llvm::MDString::get(*context, "kernel"));
nvvmMetadataNode->addOperand(MDNOdeNVVM);
有人可以告诉我如何修改以上代码以生成所需格式的元数据
Could someone tell me how to modify the above code to generate metadata of the required form
推荐答案
您的元数据将是一个包含3个元素的元组.第一个是全局值,在元数据层次结构中插入时将其包装为"ValueAsMetadata"(由于GlobalValues是常量,因此我们可以使用Constant子类).第二个是MDString,您得到了这个.最后一个包装为ConstantAsMetadata
.
这应该看起来像下面的
Your metadata will be a tuple with 3 elements. The first one is a global value, which is wrapped when insert in the metadata hierarchy as "ValueAsMetadata" (we can use the Constant subclass since GlobalValues are constant). The second is a MDString, you got this one. The last one is wrapped as a ConstantAsMetadata
.
This should look approximately like the follow
SmallVector<Metadata *, 32> Ops; // Tuple operands
GlobalValue *Foo = Mod.getNamedValue("foo);
if (!Foo) report_fatal_error("Expected foo..");
Ops.push_back(llvm::ValueAsMetadata::getConstant(Foo));
Ops.push_back(llvm::MDString::get(*context, "kernel"));
// get constant i32 1
Type *I32Ty = Type::getInt32Ty(*context);
Contant *One = ConstantInt::get(I32Ty, 1);
Ops.push_back(llvm::ValueAsMetadata::getConstant(One));
auto *Node = MDTuple::get(Context, Ops);
这篇关于如何为LLVM IR生成元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!