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

Tobias Gysi llvmlistbot at llvm.org
Fri Mar 14 00:38:38 PDT 2025


================
@@ -13,13 +13,30 @@ include "mlir/Dialect/LLVMIR/LLVMDialect.td"
 include "mlir/Dialect/LLVMIR/LLVMInterfaces.td"
 include "mlir/IR/AttrTypeBase.td"
 include "mlir/IR/CommonAttrConstraints.td"
+include "mlir/IR/OpAsmInterface.td"
 
 // All of the attributes will extend this class.
 class LLVM_Attr<string name, string attrMnemonic,
                 list<Trait> traits = [],
                 string baseCppClass = "::mlir::Attribute">
-    : AttrDef<LLVM_Dialect, name, traits, baseCppClass> {
+    : AttrDef<LLVM_Dialect, name, traits # [OpAsmAttrInterface], baseCppClass> {
   let mnemonic = attrMnemonic;
+
+  // Custom flag for attribute to indicate whether it wants mnemonic alias.
+  int enableMnemonicAlias = 0;
+  // If the attribute already uses extraClassDeclaration, it should create
+  // its own OpAsmAttrInterface methods instead of relying on here as this
+  // part will be overriden.
+  let extraClassDeclaration = !if(!eq(enableMnemonicAlias, 1), [{
+    //===------------------------------------------------------------------===//
+    // OpAsmAttrInterface Methods
+    //===------------------------------------------------------------------===//
+
+    ::mlir::OpAsmAliasResult getAlias(::llvm::raw_ostream &os) const {
+      os << "}] # mnemonic # [{";
+      return ::mlir::OpAsmAliasResult::OverridableAlias;
+    }
----------------
gysit wrote:

It may still make sense to have some tablegen for a flag that can automatically generate a getAlias function that uses the mnemonic. Basically what is in the LLVM attribute base class now. Downstream users that do something more clever can still implement their own solution? 

I would assume that other dialects may also would like to have the same functionality?

However, if this is too complex we can also land the current solution. I am a bit worried that this will lead to unexpected behavior when someone overwrites the extraClassDeclaration. I do not see a better solution though (except for proper tablegen support).

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


More information about the Mlir-commits mailing list