How to attach metadata to LLVM IR using the C++ API?

江枫思渺然 提交于 2020-01-11 10:43:53

问题


Can anyone point me to a concrete example of attaching metadata to llvm-ir using the c++ api?

I've read this page http://llvm.org/docs/SourceLevelDebugging.html.

Thanks


回答1:


The above answer is not quite correct (or complete). You can also create metadata at the module level with just MDNode::get(...) which takes a context and an array of values to create metadata from. Named metadata is very heavyweight and you should only use it as an anchor for top level metadata values.

For attaching to instructions you do want to use the setMetadata call on the Instruction to set any particular metadata, however, you'll want to make sure you're using the correct context - otherwise you could overwrite other metadata.




回答2:


There are two things you can do.

  1. Attach metadata nodes to instructions (like the !dbg nodes from the link you referenced). For that, there's the Instruction::setMetadata method
  2. Create named metadata nodes in a Module, not attached to any particular instruction. For that, use Module::getOrInsertNamedMetadata.


来源:https://stackoverflow.com/questions/9525947/how-to-attach-metadata-to-llvm-ir-using-the-c-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!