ConsistentProgrammer
ConsistentProgrammer

Reputation: 1324

What is LLVM metadata

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!

Upvotes: 5

Views: 4785

Answers (2)

Jason Yang
Jason Yang

Reputation: 51

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 ^^

Upvotes: 3

Eli Bendersky
Eli Bendersky

Reputation: 273686

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.

Upvotes: 8

Related Questions