Right way to add metadata to LLVM Dialect and pass to LLVM IR


I am working on refactoring some LLVMIR code to MLIR-based (LLVM dialect).

There is some metadata I need to pass to the LLVM dialect, but I found there is no example or document explaining how to do it. I don’t think the metadata op is the right thing.

The old LLVMIR code is as below

Metadata *md_args[] = {
        MDString::get(ctx, "maxntidx"),
      mod_->getOrInsertNamedMetadata("nvvm.annotations")->addOperand(MDNode::get(ctx, md_args));

Could you please tell me the right way to pass metadata to the LLVM dialect, so that it can finally translate to LLVM IR with the same effect?

I also checked LLVM metadata support in MLIR, but because I am using the LLVM dialect to translate to LLVMIR directly, it seems that I can’t rebind a customized LLVMTranslationInterface to llvm dialect again.

My answer is essentially the same as in the thread you linked - LLVM metadata support in MLIR - #2 by ftynse. We don’t want to have arbitrary unverified metadata in MLIR so there almost no mechanisms to attach just a blob. You would need to model your metadata somehow in MLIR and provide a translation for them. It is difficult to understand what exactly these metadata are given your code snippet (which is also why we want it to be modeled properly), I can only suppose it has something to do with NVidia GPUs and can live in the nvvm dialect. Please elaborate on what is being modeled and somebody may be able to help you.

It is actually possible to have a fully custom LLVMTranslationInterface and use it in an out-of-tree binary as long as it doesn’t call mlir::registerLLVMDialectTranslation to register the default interface implementation. (You cannot convince mlir-translate to use the non-default implementation though.) However, I doubt this is necessary.

I see, thank you!