[Mlir-commits] [mlir] [MLIR][LLVMIR] Add support for intrinsics with metadata arguments (PR #200308)
Tobias Gysi
llvmlistbot at llvm.org
Fri May 29 00:42:54 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());
----------------
gysit wrote:
I know there is precedence but the coding guidelines don't allow recursion for this case I believe. Could you use a dfs with a while loop instead?
Unfortunately, LLVM metadata can be cyclic. We have some ways of modeling this for debug metadata but in general this cannot be expressed in MLIR. I don't think it is an issue for the kinds of metadata we want to represent here but we should probably add some check if a metadata node has been translated before and then bail out to avoid possible endless loops.
https://github.com/llvm/llvm-project/pull/200308
More information about the Mlir-commits
mailing list