Johns Paul Johns Paul - 1 year ago 120
C++ Question

How to generate metadata for 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}

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"));

Could someone tell me how to modify the above code to generate metadata of the required form

Answer Source

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::MDString::get(*context, "kernel"));
// get constant i32 1
Type *I32Ty = Type::getInt32Ty(*context);
Contant *One = ConstantInt::get(I32Ty, 1);
auto *Node =  MDTuple::get(Context, Ops);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download