[Mlir-commits] [mlir] [mlir][MLProgram][NFC] Migrate to OpAsmAttrInterface for ASM alias generation (PR #130481)

Hongren Zheng llvmlistbot at llvm.org
Sun Mar 9 04:01:49 PDT 2025


https://github.com/ZenithalHourlyRate created https://github.com/llvm/llvm-project/pull/130481

After the introduction of `OpAsmAttrInterface`, it is favorable to migrate code using `OpAsmDialectInterface` for ASM alias generation, which lives in `Dialect.cpp`, to use `OpAsmAttrInterface`, which lives in `Attrs.td`. In this way, attribute behavior is placed near its tablegen definition and people won't need to go through other files to know what other (unexpected) hooks comes into play.

See #124721 for the interface itself and #128191 and #130479 for prior migrations.

Note that `MLProgramOpAsmInterface` has no content now. However, if we delete it, a failure related to dialect resource handling will occur

```
within split at llvm-project/mlir/test/IR/invalid-file-metadata.mlir:60 offset :7:7: error: unexpected error: unexpected 'resource' section for dialect 'ml_program'
```

To support resource such interface must be registered.

>From 9a160a7de8be6c60f5e5302b83b844c581f5b554 Mon Sep 17 00:00:00 2001
From: Zenithal <i at zenithal.me>
Date: Sun, 9 Mar 2025 10:56:21 +0000
Subject: [PATCH] [mlir][MLProgram][NFC] Migrate to OpAsmAttrInterface for ASM
 alias generation

---
 .../Dialect/MLProgram/IR/MLProgramAttributes.td    | 14 +++++++++++++-
 mlir/lib/Dialect/MLProgram/IR/MLProgramDialect.cpp |  8 --------
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/mlir/include/mlir/Dialect/MLProgram/IR/MLProgramAttributes.td b/mlir/include/mlir/Dialect/MLProgram/IR/MLProgramAttributes.td
index eb6e293bbf4f6..2dc5727060064 100644
--- a/mlir/include/mlir/Dialect/MLProgram/IR/MLProgramAttributes.td
+++ b/mlir/include/mlir/Dialect/MLProgram/IR/MLProgramAttributes.td
@@ -11,6 +11,7 @@
 
 include "mlir/IR/AttrTypeBase.td"
 include "mlir/IR/BuiltinAttributeInterfaces.td"
+include "mlir/IR/OpAsmInterface.td"
 include "mlir/Dialect/MLProgram/IR/MLProgramBase.td"
 
 // Base class for MLProgram dialect attributes.
@@ -23,7 +24,7 @@ class MLProgram_Attr<string name, list<Trait> traits = []>
 // ExternAttr
 //===----------------------------------------------------------------------===//
 
-def MLProgram_ExternAttr : MLProgram_Attr<"Extern", [TypedAttrInterface]> {
+def MLProgram_ExternAttr : MLProgram_Attr<"Extern", [TypedAttrInterface, OpAsmAttrInterface]> {
   let summary = "Value used for a global signalling external resolution";
   let description = [{
   When used as the value for a GlobalOp, this indicates that the actual
@@ -40,6 +41,17 @@ def MLProgram_ExternAttr : MLProgram_Attr<"Extern", [TypedAttrInterface]> {
   let parameters = (ins AttributeSelfTypeParameter<"">:$type);
   let mnemonic = "extern";
   let assemblyFormat = "";
+
+  let extraClassDeclaration = [{
+    //===------------------------------------------------------------------===//
+    // OpAsmAttrInterface Methods
+    //===------------------------------------------------------------------===//
+
+    ::mlir::OpAsmAliasResult getAlias(::llvm::raw_ostream &os) const {
+      os << "}] # mnemonic # [{";
+      return ::mlir::OpAsmAliasResult::OverridableAlias;
+    }
+  }];
 }
 
 #endif // MLPROGRAM_ATTRIBUTES
diff --git a/mlir/lib/Dialect/MLProgram/IR/MLProgramDialect.cpp b/mlir/lib/Dialect/MLProgram/IR/MLProgramDialect.cpp
index bda1032ed9884..0b1561213165b 100644
--- a/mlir/lib/Dialect/MLProgram/IR/MLProgramDialect.cpp
+++ b/mlir/lib/Dialect/MLProgram/IR/MLProgramDialect.cpp
@@ -39,14 +39,6 @@ struct MLProgramInlinerInterface : public DialectInlinerInterface {
 
 struct MLProgramOpAsmDialectInterface : public OpAsmDialectInterface {
   using OpAsmDialectInterface::OpAsmDialectInterface;
-
-  AliasResult getAlias(Attribute attr, raw_ostream &os) const override {
-    if (llvm::isa<ExternAttr>(attr)) {
-      os << "extern";
-      return AliasResult::OverridableAlias;
-    }
-    return AliasResult::NoAlias;
-  }
 };
 } // namespace
 



More information about the Mlir-commits mailing list