Johns Paul
Johns Paul

Reputation: 673

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"));
nvvmMetadataNode->addOperand(MDNOdeNVVM);

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

Upvotes: 2

Views: 914

Answers (1)

Joky
Joky

Reputation: 1628

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

Upvotes: 1

Related Questions