Reputation: 153
While the language reference mention a lot about the LLVM Metadata,
and I see some posts on SO - How to add a Metadata String to an LLVM module with the C++ API?
I also see some code in the llvm source- http://llvm.org/doxygen/DIBuilder_8cpp_source.html
However, they dont seem to mention how to create a MDNode containing a ConstantInt of a particular width.
Following is the relevant code (doesn't work ) -
std::vector<Metadata*> Elts =
{
ConstantInt::get(TheContext,APInt(returnType->getIntegerBitWidth(),decimal_val))
};
MDNode* Node = MDNode::get(TheContext, Elts);
callInst->setMetadata(LLVMContext::MD_range,Node);
Can anyone explain how this can be done ?
Thanks!
Upvotes: 1
Views: 909
Reputation: 153
So apparently I checked the llvm class hierarchy and checked the sub-classes under MetaData. I found a few classes and one of them was - ConstantAsMetadata
The change in the above code that works for me is -
std::vector<Metadata*> Elts = {
ConstantAsMetadata::get(ConstantInt::get(TheContext,APInt(returnType,0)) ),
ConstantAsMetadata::get(ConstantInt::get(TheContext,APInt(returnType,decimal_val)) )
};
Note- You should ( if you want correctness ) specify a pair of numbers for each range that you are trying to create.
If you are using the verifier pass provided by llvm , the above would not work/make sense if you just have a single ConstantInt inside the initializer. This is because of an assert inside the Verifier pass provided by the llvm.
Upvotes: 0
Reputation: 591
I have written this small post about how to insert metadata in LLVM IR. You can refer that. Basically you need to use ConstantAsMetadata to achieve this.
Upvotes: 2