In my opinion:
1. Is metadata the right mechanism to use?
If your "other tool" is not a pass in itself, then yes, I think metadata is the best approach - keeps everything in the IR, easy to identify by eye, simple to manually add for testing, and - perhaps most importantly - does not collide with anything else, as long as you don't reuse existing metadata kinds.
However, if your "other tool" is a pass by itself, there's an alternative: you can make one pass dependent on the other, and than use information from the earlier directly in the later pass. The advantage is that you don't have to modify the IR.
2. How to use a custom metadata node?
Use the char*
variant of setMetadata
, like so:
LLVMContext& C = Inst->getContext();
MDNode* N = MDNode::get(C, MDString::get(C, "my md string content"));
Inst->setMetadata("my.md.name", N);
And if it's the first time the string is used in a setMetadata
, it will automatically register my.md.name
as a new kind in the module (it's actually consistent in the entire context, I believe). You can later on retrieve the string by using:
cast<MDString>(Inst->getMetadata("my.md.name")->getOperand(0))->getString();
If you want to invoke getMetadata
or setMetadata
repeatedly from the same scope, though, you can also use Module::getMDKindID
to just get the actual kind used, and use the variations of these methods that use the kind value.
Finally, be aware that you can limit the metadata node scope to be inside a function - use the MDNode::get(..., ..., true)
variant for that - though I never used it myself.