This is an early-stage RFC whose purpose is to find out whether
support for arbitrary element types in Linalg named operations
is wanted by the community at all (or if the limitation to the currently
supported types is intentional) and to discuss possible directions to
add support for arbitrary types.
Background
Linalg named oeprations operations are currently limited to tensors
and memrefs composed of floating point, integer or index elements
and using any other element type triggers an assertion.
Checks for these types are hardcoded, but thanks to the abstractions
used for the definition and implementation of Linalg named operations, only
few places need to be modified to extend the set of supported element
types. In particular, supporting a new type requires a modification of
the helper methods from RegionBuilderHelper
in
mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
, which are invoked by the
region builders generated from the abstract YAML representation for
proper creation of appropriate scalar operations.
However, while this might be a viable approach for a small set of
built-in types, modifying Linalg itself does not seem reasonable for
users of MLIR that wish to add support for their own, externally
defined types.
Implementation goals
A better approach would allow users of MLIR to add support new types
without modification of the MLIR code base. Ideally, the
implementation supports:
-
Modularity: allow users of MLIR to add support for their own types
without modifying Linalg itself. -
Extensibility: allow for the support of any element type for any
Linalg named operation, as long as the scalar operations to implement the
Linalg operation with the element type exist (e.g., if an addition
and a multiplication operation exist for a scalar type, then it
should be possible to add support for the type forlinalg.matmul
). -
Selective extensibility: Since there is a variety of Linalg named
operations, supporting a new element type for all of them would
result in a substantial set of required scalar operations. This set
might not be available for a given type (e.g., themax
operation
missing for complex values). Therefore, it should be possible to add
support only for a subset of Linalg named operations for a new type.
Proposed direction for implementation
Add a new type trait for each arithmetic operator required by Linalg
and modify the helper methods of RegionBuilderHelper
, such that they
create the type-specific operation via a method of the type trait. For
example, RegionBuilderHelper::arithfn__add
would check that the type
implements the Addition
type trait and call a method that creates
the appropriate operation for the given operands.
The drawback of this implementation is that it requires a substantial
amount of new traits, which all need to be implemented for the
built-in floating point, integer and index types.