[Mlir-commits] [mlir] [MLIR][TableGen] Add genMnemonicAlias field for OpAsm{Type, Attr}Interface (PR #131504)

Hongren Zheng llvmlistbot at llvm.org
Tue Mar 18 07:06:13 PDT 2025


ZenithalHourlyRate wrote:

> When this field is used, we should auto-add the Asm interface the generated definition (we shouldn't require the user to do it)

I still could not figure out how to do that with simple changes to tablegen files.

### Append the interface directly to AttrOrTypeDef trait list

```diff
-  list<Trait> traits = defTraits;
+  list<Trait> traits = defTraits #
+    !if(!eq(genMnemonicAlias, 1), 
+      !if(!eq(valueType, "Attr"),
+        [OpAsmAttrInterface],
+        [OpAsmTypeInterface]),
+      []);
```

This will form a cyclic dependency. `Interfaces.td` includes `AttrTypeBase.td`, and `OpAsmInterface.td` needs `Interfaces.td`. `OpAsmInterface.td` included in `AttrTypeBase.td` is a cyclic dependency.

### Append the trait using NativeTrait

```diff
-  list<Trait> traits = defTraits;
+  list<Trait> traits = defTraits #
+    !if(!eq(genMnemonicAlias, 1), 
+      !if(!eq(valueType, "Attr"),
+        [NativeAttrTrait<"OpAsmAttrInterface">],
+        [NativeTypeTrait<"OpAsmTypeInterface">]),
+      []);
```

In generated file `NativeTrait` is translated into `mlir::AttributeTrait::OpAsmAttrInterface`, however the real trait is `mlir::OpAsmAttrInterface::Trait`. Again we can not use `InterfaceTrait` here as `Interfaces.td` depends on `AttrTypeBase.td`. We may move `InterfaceTrait` to `Traits.td` but that is also quite a violation of its name (it should stay in `Interfaces.td`)

### Append the trait manually

```diff
-  list<Trait> traits = defTraits;
+  list<Trait> traits = defTraits #
+    !if(!eq(genMnemonicAlias, 1), 
+      !if(!eq(valueType, "Attr"),
+        [CustomTraitClass<"OpAsmAttrInterface::Trait">],
+        [CustomTraitClass<"OpAsmTypeInterface::Trait">]),
+      []);
```

We could hack to make it work like get a `CustomTraitClass` inside `AttrTypeBase.td` which defines the trait name so the generated code uses the correct trait. However this is too delicate and prone to break.

### Append it in `mlir-tblgen`

This is quite counter-intuitive, as ideally the behavior should be specified in tablegen instead of being inside tblgen code.

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


More information about the Mlir-commits mailing list