[Mlir-commits] [mlir] [MLIR][LLVMIR] Add support for intrinsics with metadata arguments (PR #200308)

Andy Kaylor llvmlistbot at llvm.org
Tue Jun 2 17:58:01 PDT 2026


================
@@ -149,6 +149,42 @@ static LogicalResult convertInstructionImpl(OpBuilder &odsBuilder,
   return failure();
 }
 
+/// Recursively converts an `llvm::Metadata` node to the matching LLVM dialect
+/// metadata attribute. Returns a null attribute for shapes that the dialect's
+/// metadata-attribute hierarchy does not currently model.
+static Attribute convertMetadataToAttr(MLIRContext *ctx,
+                                       const llvm::Metadata *md) {
+  if (!md)
+    return {};
+  if (auto *mdStr = dyn_cast<llvm::MDString>(md))
+    return MDStringAttr::get(ctx, StringAttr::get(ctx, mdStr->getString()));
+  if (auto *cam = dyn_cast<llvm::ConstantAsMetadata>(md)) {
+    auto *ci = dyn_cast<llvm::ConstantInt>(cam->getValue());
+    if (!ci)
+      return {};
+    auto intType = IntegerType::get(ctx, ci->getBitWidth());
+    return MDConstantAttr::get(ctx, IntegerAttr::get(intType, ci->getValue()));
+  }
+  if (auto *vam = dyn_cast<llvm::ValueAsMetadata>(md)) {
+    auto *fn = dyn_cast<llvm::Function>(vam->getValue());
+    if (!fn)
+      return {};
+    return MDFuncAttr::get(ctx, FlatSymbolRefAttr::get(ctx, fn->getName()));
+  }
+  if (auto *node = dyn_cast<llvm::MDNode>(md)) {
+    SmallVector<Attribute> operands;
+    operands.reserve(node->getNumOperands());
+    for (const llvm::MDOperand &op : node->operands()) {
+      Attribute opAttr = convertMetadataToAttr(ctx, op.get());
----------------
andykaylor wrote:

Good catch. I was focused on the constrained intrinsics, which take a very simple metadata argument, and when I generalized this I overlooked the possibility of a cycle. It doesn't look like there is a way to represent the cyclic metadata yet, so I'm going to just have this emit an error. The only case I saw that is likely to hit this is `llvm.experimental.noalias.scope.decl` which already has a custom importer that drops the instruction.

https://github.com/llvm/llvm-project/pull/200308


More information about the Mlir-commits mailing list