You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[MLIR][Doc] Add documentation for OpAsmAttr/TypeInterface (llvm#140244)
After the introduction of OpAsmAttr/TypeInterface in llvm#121187llvm#124721,
the documentation for them could be updated along side the doc for
OpAsmDialectInterface.
llvm#127993 changed the trailing digit behavior for alias name generation.
Copy file name to clipboardExpand all lines: mlir/docs/DefiningDialects/Assembly.md
+52-14Lines changed: 52 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -4,29 +4,42 @@
4
4
5
5
## Generating Aliases
6
6
7
-
To reduce verbosity in the resulting assembly, `AsmPrinter` can generate aliases for frequently used types and attributes.
7
+
`AsmPrinter` can generate aliases for frequently used types and attributes when not printing them in generic form. For example, `!my_dialect.type<a=3,b=4,c=5,d=tuple,e=another_type>` and `#my_dialect.attr<a=3>` can be aliased to `!my_dialect_type` and `#my_dialect_attr`.
8
8
9
-
For example, `!my_dialect.type<a=3,b=4,c=5,d=tuple,e=another_type>` and `#my_dialect.attr<a=3>` can be aliased to `!my_dialect_type`and `#my_dialect_attr`, simplifying further references.
9
+
There are mainly two ways to hook into the `AsmPrinter`. One is the attribute/type interface and the other is the dialect interface.
10
10
11
-
To enable this, the owning dialect of these types/attributes can define an interface to hook into the `AsmPrinter`. This is effective only when the assembly is not printed in generic form.
11
+
The attribute/type interface is the first hook to check. If no such hook is found, or the hook returns `OverridableAlias` (see definition below), then dialect interfaces are involved.
12
+
13
+
The dialect interface for one specific dialect could generate alias for all types/attributes, even when it does not "own" them. The `AsmPrinter` checks all dialect interfaces based on their order of registration. For example, the default alias `map` for `builtin` attribute `AffineMapAttr` could be overriden by the dialect interface for `my_dialect` as custom dialect is often registered after the `builtin` dialect.
// Other dialects are allowed to provide alias for
27
-
// type/attribute not owned by them
28
-
// but the final result would depend on the registration order
29
-
// of these dialects in the MLIRContext
30
43
return AliasResult::FinalAlias;
31
44
}
32
45
return AliasResult::NoAlias;
@@ -47,8 +60,31 @@ void MyDialect::initialize() {
47
60
}
48
61
```
49
62
50
-
* If `getAlias` provides an alias with a trailing digit, `AsmPrinter` appends an underscore to avoid conflicts with autogenerated IDs.
51
-
* If multiple types/attributes have the same alias from `getAlias`, a number is appended to the alias to avoid conflicts.
63
+
### `OpAsmAttrInterface` and `OpAsmTypeInterface`
64
+
65
+
The easiest way to use these interfaces is toggling `genMnemonicAlias` in the tablegen file of the attribute/alias. It directly uses the mnemonic as alias. See [Defining Dialect Attributes and Types](/docs/DefiningDialects/AttributesAndTypes) for details.
66
+
67
+
If a more custom behavior is wanted, the following modification to the attribute/type should be made
68
+
69
+
1. Add `OpAsmAttrInterface` or `OpAsmTypeInterface` into its trait list.
70
+
2. Implement the `getAlias` method, either in tablegen or its cpp file.
@@ -98,6 +134,8 @@ Similarly, an `Operation` can suggest the name for its block arguments using `ge
98
134
For custom block names, `OpAsmOpInterface` has a method `getAsmBlockNames` so that
99
135
the operation can suggest a custom prefix instead of a generic `^bb0`.
100
136
137
+
Alternatively, `OpAsmTypeInterface` provides a `getAsmName` method for scenarios where the name could be inferred from its type.
138
+
101
139
## Defining Default Dialect
102
140
103
141
An `Operation` can indicate that the nested region in it has a default dialect prefix, and the operations in the region could elide the dialect prefix.
0 commit comments