I’m trying to add metadata nodes to a program, either onto the instructions or as global metadata. How do I do this with the LLVM C API? It now provides a function LLVMAddNamedMetadataOperand (as found from this question) but I can’t seem to see how to use it. This is bound to addNamedMetadataOperand in the llvm-fs bindings. I tried this:
addNamedMetadataOperand myModule "foobar" (mDString "cat" 3u)
expecting it to make some metadata node called foobar but it doesn’t work – complains about cast errors. I thought maybe you were supposed to use addNamedMetadataOperand on an instruction, so I tried:
let ret = buildRet bldr (constInt i32 0UL)
addNamedMetadataOperand myModule "foobar" ret
but it didn’t like this either.
I added two new “F# friendly functions”: mdNode and mdNodeInContext in this commit. With that commit I can modify your example code to:
Which gives the bitcode:
I used getMDKindID rather than one of the pre-defined MD kinds because when I was using 0u I was getting no metadata output. I haven’t looked deep into why but from looking at http://llvm.org/docs/LangRef.html#metadata it seems that the predefined metadata types have some constraints that the instruction it was applied to wasn’t meeting. Anyhow, let me know if you see more problems with this. It’s not a part of the API that I’m using at the moment but I do want it to work as well as possible.