What is LLVM metadata

牧云@^-^@ 提交于 2019-12-07 20:51:53

问题


These might be very basic questions..

1) What is LLVM metadata and how do I use it in my program? I've read all the documentation, but I don't understand how to use it.

2) How to I add my personal metadata in a file?

Thanks in advance!


回答1:


The best source of information would be the blog post from 2010 that introduced metadata into LLVM IR - Extensible Metadata in LLVM IR. The first paragraph mentions the motivation:

This metadata could be used to influence language-specific optimization passes (for example, Type Based Alias Analysis in C), tag information for a custom code generator, or pass through information to link time optimization.

But reall, read all of if for the historical details.

The main "client" of metadata in LLVM is currently debug info. It's used by the front-end (e.g. Clang) to tag the LLVM IR it generates with debug information that correlates IR to the source code it came from. This same metadata is later translated to platform specific debug info such as DWARF by the code emitters.




回答2:


Here is a simple example:

  llvm::LLVMContext &Ctx = LLMod.getContext();
  llvm::IntegerType *Int32Ty = llvm::Type::getInt32Ty(Ctx);

  llvm::Metadata *MapleVerElts[] = {
    llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 0)),
    llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 1)),
  };

  llvm::NamedMDNode *MapleVerMD
      = LLMod.getOrInsertNamedMetadata("maple-as.version");
  MapleVerMD->addOperand(llvm::MDNode::get(Ctx, MapleVerElts));

  const MPLModule::FunctionListType &FuncList = Mod.getFunctionList();

  LLMod.dump();

And you may get the following output:

!maple-as.version = !{!0}

!0 = !{i32 0, i32 1}

Maybe it will give you a simple hint to use MetaData ^^



来源:https://stackoverflow.com/questions/19743861/what-is-llvm-metadata

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