[MLIR][LLVM] Reject distinct generic metadata on import (#212455)

Reject distinct nodes when converting generic LLVM metadata into LLVM
dialect attributes because `MDNodeAttr` cannot preserve node identity.
Apply the policy recursively so top-level and nested nodes use the
existing unsupported-metadata failure path while finite acyclic uniqued
nodes remain importable.
This commit is contained in:
Akimasa Watanuki 2026-08-01 22:05:56 +09:00 committed by GitHub
parent 60f965b1f6
commit ecb50ab04f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 2 deletions

View file

@ -181,6 +181,9 @@ static Attribute convertMetadataToAttrImpl(
return MDFuncAttr::get(ctx, FlatSymbolRefAttr::get(ctx, fn->getName()));
}
if (auto *node = dyn_cast<llvm::MDNode>(md)) {
// Metadata attributes cannot preserve distinctness, so bail out.
if (node->isDistinct())
return {};
if (Attribute cached = attrMap.lookup(node))
return cached;
// If `node` is already on the current search path, this is a back-edge into
@ -206,8 +209,9 @@ static Attribute convertMetadataToAttrImpl(
/// Converts the metadata node `md` to the matching LLVM dialect metadata
/// attribute. Returns a null attribute for shapes that the dialect's
/// metadata-attribute hierarchy does not currently model, including cyclic
/// metadata graphs that the immutable metadata attributes cannot express.
/// metadata-attribute hierarchy does not currently model, including distinct
/// nodes and cyclic metadata graphs that the immutable metadata attributes
/// cannot express.
static Attribute convertMetadataToAttr(MLIRContext *ctx,
const llvm::Metadata *md) {
SmallPtrSet<const llvm::Metadata *, 8> path;

View file

@ -459,6 +459,31 @@ bb1:
; // -----
; CHECK: error: unsupported metadata: !{{[0-9]+}} = distinct !{!"sp"}
declare i32 @llvm.read_register.i32(metadata)
define i32 @distinct_metadata_as_value() {
%r = call i32 @llvm.read_register.i32(metadata !0)
ret i32 %r
}
!0 = distinct !{!"sp"}
; // -----
; CHECK: error: unsupported metadata: !{{[0-9]+}} = !{!{{[0-9]+}}}
declare i32 @llvm.read_register.i32(metadata)
define i32 @nested_distinct_metadata_as_value() {
%r = call i32 @llvm.read_register.i32(metadata !0)
ret i32 %r
}
!0 = !{!1}
!1 = distinct !{!"sp"}
; // -----
; CHECK: error: unsupported metadata: !{{[0-9]+}} = distinct !{!{{[0-9]+}}}
declare i32 @llvm.read_register.i32(metadata)