The goal to this RFC is to expose the type and attribute names in C++ as methods in the AbstractType
and AbstractAttribute
classes, and keep a map of names to abstract types/attributes in the MLIRContext
.
The accompanying PR is here: [mlir] Expose type and attribute names in the MLIRContext and abstract type/attr classes by math-fehr · Pull Request #72189 · llvm/llvm-project · GitHub
Motivation
While dialect type and attributes (those not in the builtin dialect) usually have the format !dialect.type<...>
, the "dialect.type"
string is not accessible from the Type
or Attribute
class, nor it is possible to get an abstract type or attribute from this string. While for most users this is not useful, it would be quite useful for PDL or IRDL.
For instance, this would allow us to specify the constraint “match any type cmath.complex
” with only the PDL/IRDL dialect, without using additional C++. Currently, this is not possible, as there is no way to get the right AbstractType
directly from anything else than an existing type or a TypeID
. While I have the example of PDL/IRDL in mind, there are probably other areas (notably when doing meta things on MLIR) where this could be useful.
Proposed changes
There are two main changes I would like to introduce. First, adding in the MLIRContext a field that would associate type/attribute names to their AbstractType
/AbstractAttribute
. Second, adding a method getName
to AbstractType
and AbstractAttribute
. These names should be in particular unique.
ODS-defined dialect types can directly emit these fields without any change, since most dialects use mnemonic
to set a name to a type/attribute. For cases where types and dialects do not have a mnemonic, typeName
or attrName
can be overridden to give them a name to emit. For C+±defined types and attributes, only one new field is needed, getTypeName
or getAttrName
.
Proposed type/attribute names
For most dialects, the names would be straightforward, which is dialect.mnemonic
. For builtin types and attributes, the name would be builtin.name
, where name
is the usual name (for instance vector
). However, for the case of unranked vector and unranked memrefs, the names would be builtin.unranked_vector
and builtin.unranked_memref
, as names should be unique per type. The only other example I have in mind for this kind of change is in the quant
dialect, which has two types that are parsed with quant.uniform
, so one is named quant.uniform
, and the other quant.uniform_per_axis
.