2020-05-27 23:45:55 +08:00
|
|
|
# Operation Definition Specification (ODS)
|
2019-05-14 05:39:27 +08:00
|
|
|
|
|
|
|
In addition to specializing the `mlir::Op` C++ template, MLIR also supports
|
2020-10-19 08:20:53 +08:00
|
|
|
defining operations and data types in a table-driven manner. This is achieved
|
|
|
|
via [TableGen][TableGen], which is both a generic language and its tooling to
|
2019-05-14 05:39:27 +08:00
|
|
|
maintain records of domain-specific information. Facts regarding an operation
|
2021-01-07 06:08:03 +08:00
|
|
|
are specified concisely into a TableGen record, which will be expanded into an
|
|
|
|
equivalent `mlir::Op` C++ template specialization at compiler build time.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
2019-09-22 02:38:41 +08:00
|
|
|
This manual explains in detail all the available mechanisms for defining
|
2019-05-14 05:39:27 +08:00
|
|
|
operations in such a table-driven manner. It aims to be a specification instead
|
2021-01-07 06:08:03 +08:00
|
|
|
of a tutorial. Please refer to
|
|
|
|
[Quickstart tutorial to adding MLIR graph rewrite](Tutorials/QuickstartRewrites.md)
|
|
|
|
for the latter.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
In addition to detailing each mechanism, this manual also tries to capture best
|
|
|
|
practices. They are rendered as quoted bullet points.
|
2019-01-16 00:30:49 +08:00
|
|
|
|
|
|
|
## Motivation
|
|
|
|
|
|
|
|
MLIR allows pluggable dialects, and dialects contain, among others, a list of
|
2019-01-17 06:03:11 +08:00
|
|
|
operations. This open and extensible ecosystem leads to the "stringly" type IR
|
|
|
|
problem, e.g., repetitive string comparisons during optimization and analysis
|
2019-05-14 05:39:27 +08:00
|
|
|
passes, unintuitive accessor methods (e.g., generic/error prone `getOperand(3)`
|
|
|
|
vs self-documenting `getStride()`) with more generic return types, verbose and
|
2022-04-07 19:11:11 +08:00
|
|
|
generic constructors without default arguments, verbose textual IR dumps, and so
|
2021-01-07 06:08:03 +08:00
|
|
|
on. Furthermore, operation verification is:
|
2019-05-14 05:39:27 +08:00
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
1. best case: a central string-to-verification-function map,
|
|
|
|
1. middle case: duplication of verification across the code base, or
|
|
|
|
1. worst case: no verification functions.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
|
|
|
The fix is to support defining ops in a table-driven manner. Then for each
|
|
|
|
dialect, we can have a central place that contains everything you need to know
|
|
|
|
about each op, including its constraints, custom assembly form, etc. This
|
|
|
|
description is also used to generate helper functions and classes to allow
|
|
|
|
building, verification, parsing, printing, analysis, and many more.
|
|
|
|
|
|
|
|
## Benefits
|
|
|
|
|
|
|
|
Compared to the C++ template, this table-driven approach has several benefits
|
|
|
|
including but not limited to:
|
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
* **Single source of truth**: We strive to encode all facts regarding an
|
|
|
|
operation into the record, so that readers don't need to jump among code
|
|
|
|
snippets to fully understand an operation.
|
|
|
|
* **Removing boilerplate**: We can automatically generate
|
|
|
|
operand/attribute/result getter methods, operation build methods, operation
|
|
|
|
verify methods, and many more utilities from the record. This greatly
|
|
|
|
reduces the boilerplate needed for defining a new op.
|
|
|
|
* **Facilitating auto-generation**: The usage of these operation information
|
|
|
|
records are by no means limited to op definition itself. We can use them to
|
|
|
|
drive the auto-generation of many other components, like computation graph
|
|
|
|
serialization.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
|
|
|
## TableGen Syntax
|
|
|
|
|
|
|
|
We use TableGen as the language for specifying operation information. TableGen
|
|
|
|
itself just provides syntax for writing records; the syntax and constructs
|
2022-04-07 19:11:11 +08:00
|
|
|
allowed in a TableGen file (typically with the filename suffix `.td`) can be found
|
2020-09-22 01:56:06 +08:00
|
|
|
[here][TableGenProgRef].
|
2019-05-14 05:39:27 +08:00
|
|
|
|
2019-12-06 21:58:59 +08:00
|
|
|
* TableGen `class` is similar to C++ class; it can be templated and
|
|
|
|
subclassed.
|
|
|
|
* TableGen `def` is similar to C++ object; it can be declared by specializing
|
|
|
|
a TableGen `class` (e.g., `def MyDef : MyClass<...>;`) or completely
|
|
|
|
independently (e.g., `def MyDef;`). It cannot be further templated or
|
|
|
|
subclassed.
|
|
|
|
* TableGen `dag` is a dedicated type for directed acyclic graph of elements. A
|
|
|
|
`dag` has one operator and zero or more arguments. Its syntax is `(operator
|
|
|
|
arg0, arg1, argN)`. The operator can be any TableGen `def`; an argument can
|
|
|
|
be anything, including `dag` itself. We can have names attached to both the
|
|
|
|
operator and the arguments like `(MyOp:$op_name MyArg:$arg_name)`.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
2020-09-22 01:56:06 +08:00
|
|
|
Please see the [language reference][TableGenProgRef] to learn about all the
|
2019-05-14 05:39:27 +08:00
|
|
|
types and expressions supported by TableGen.
|
|
|
|
|
|
|
|
## Operation Definition
|
|
|
|
|
|
|
|
MLIR defines several common constructs to help operation definition and provide
|
|
|
|
their semantics via a special [TableGen backend][TableGenBackend]:
|
|
|
|
[`OpDefinitionsGen`][OpDefinitionsGen]. These constructs are defined in
|
2022-04-07 19:11:11 +08:00
|
|
|
[`OpBase.td`][OpBase]. The main ones are:
|
2019-05-14 05:39:27 +08:00
|
|
|
|
2019-10-16 02:22:53 +08:00
|
|
|
* The `Op` class: It is the main construct for defining operations. All facts
|
|
|
|
regarding the operation are specified when specializing this class, with the
|
|
|
|
help of the following constructs.
|
|
|
|
* The `Dialect` class: Operations belonging to one logical group are placed in
|
|
|
|
the same dialect. The `Dialect` class contains dialect-level information.
|
|
|
|
* The `OpTrait` class hierarchy: They are used to specify special properties
|
|
|
|
and constraints of the operation, including whether the operation has side
|
|
|
|
effect or whether its output has the same shape as the input.
|
2021-09-14 04:42:24 +08:00
|
|
|
* The `ins`/`outs` marker: These are two special markers builtin to the
|
2022-04-07 19:11:11 +08:00
|
|
|
`OpDefinitionsGen` backend. They lead to the definitions of operands/attributes
|
2019-10-16 02:22:53 +08:00
|
|
|
and results respectively.
|
|
|
|
* The `TypeConstraint` class hierarchy: They are used to specify the
|
|
|
|
constraints over operands or results. A notable subclass hierarchy is
|
|
|
|
`Type`, which stands for constraints for common C++ types.
|
|
|
|
* The `AttrConstraint` class hierarchy: They are used to specify the
|
|
|
|
constraints over attributes. A notable subclass hierarchy is `Attr`, which
|
|
|
|
stands for constraints for attributes whose values are of common types.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
|
|
|
An operation is defined by specializing the `Op` class with concrete contents
|
|
|
|
for all the fields it requires. For example, `tf.AvgPool` is defined as
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
```tablegen
|
|
|
|
def TF_AvgPoolOp : TF_Op<"AvgPool", [NoSideEffect]> {
|
|
|
|
let summary = "Performs average pooling on the input.";
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
let description = [{
|
|
|
|
Each entry in `output` is the mean of the corresponding size `ksize`
|
|
|
|
window in `value`.
|
|
|
|
}];
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
let arguments = (ins
|
|
|
|
TF_FpTensor:$value,
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
Confined<I64ArrayAttr, [ArrayMinCount<4>]>:$ksize,
|
|
|
|
Confined<I64ArrayAttr, [ArrayMinCount<4>]>:$strides,
|
|
|
|
TF_AnyStrAttrOf<["SAME", "VALID"]>:$padding,
|
2019-10-21 00:44:06 +08:00
|
|
|
DefaultValuedAttr<TF_ConvertDataFormatAttr, "NHWC">:$data_format
|
2019-05-14 05:39:27 +08:00
|
|
|
);
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
let results = (outs
|
|
|
|
TF_FpTensor:$output
|
|
|
|
);
|
|
|
|
|
|
|
|
TF_DerivedOperandTypeAttr T = TF_DerivedOperandTypeAttr<0>;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
In the following we describe all the fields needed. Please see the definition of
|
|
|
|
the `Op` class for the complete list of fields supported.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
|
|
|
### Operation name
|
|
|
|
|
2022-04-07 19:11:11 +08:00
|
|
|
The operation name is a unique identifier for the operation within MLIR, e.g.,
|
2019-05-21 00:33:10 +08:00
|
|
|
`tf.Add` for addition operation in the TensorFlow dialect. This is the
|
|
|
|
equivalent of the mnemonic in assembly language. It is used for parsing and
|
|
|
|
printing in the textual format. It is also used for pattern matching in graph
|
|
|
|
rewrites.
|
|
|
|
|
|
|
|
The full operation name is composed of the dialect name and the op name, with
|
|
|
|
the former provided via the dialect and the latter provided as the second
|
|
|
|
template parameter to the `Op` class.
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
### Operation documentation
|
|
|
|
|
2020-04-29 13:47:35 +08:00
|
|
|
This includes both a one-line `summary` and a longer human-readable
|
2019-05-14 05:39:27 +08:00
|
|
|
`description`. They will be used to drive automatic generation of dialect
|
|
|
|
documentation. They need to be provided in the operation's definition body:
|
2019-01-16 00:30:49 +08:00
|
|
|
|
|
|
|
```tablegen
|
2019-05-14 05:39:27 +08:00
|
|
|
let summary = "...";
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
let description = [{
|
|
|
|
...
|
|
|
|
}];
|
|
|
|
```
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
`description` should be written in Markdown syntax.
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
Placing the documentation at the beginning is recommended since it helps in
|
|
|
|
understanding the operation.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
> * Place documentation at the beginning of the operation definition
|
|
|
|
> * The summary should be short and concise. It should be a one-liner without
|
|
|
|
> trailing punctuation. Put expanded explanation in description.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
|
|
|
### Operation arguments
|
|
|
|
|
|
|
|
There are two kinds of arguments: operands and attributes. Operands are runtime
|
|
|
|
values produced by other ops; while attributes are compile-time known constant
|
|
|
|
values, including two categories:
|
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
1. Natural attributes: these attributes affect the behavior of the operations
|
|
|
|
(e.g., padding for convolution);
|
|
|
|
1. Derived attributes: these attributes are not needed to define the operation
|
|
|
|
but are instead derived from information of the operation. E.g., the output
|
|
|
|
shape of type. This is mostly used for convenience interface generation or
|
|
|
|
interaction with other frameworks/translation.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
All derived attributes should be materializable as an Attribute. That is,
|
|
|
|
even though they are not materialized, it should be possible to store as an
|
|
|
|
attribute.
|
2020-03-25 01:18:46 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
Both operands and attributes are specified inside the `dag`-typed `arguments`,
|
|
|
|
led by `ins`:
|
|
|
|
|
|
|
|
```tablegen
|
|
|
|
let arguments = (ins
|
|
|
|
<type-constraint>:$<operand-name>,
|
|
|
|
...
|
|
|
|
<attr-constraint>:$<attr-name>,
|
|
|
|
...
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
|
|
|
Here `<type-constraint>` is a TableGen `def` from the `TypeConstraint` class
|
|
|
|
hierarchy. Similarly, `<attr-constraint>` is a TableGen `def` from the
|
|
|
|
`AttrConstraint` class hierarchy. See [Constraints](#constraints) for more
|
|
|
|
information.
|
|
|
|
|
|
|
|
There is no requirements on the relative order of operands and attributes; they
|
2019-12-10 02:28:58 +08:00
|
|
|
can mix freely. The relative order of operands themselves matters. From each
|
|
|
|
named argument a named getter will be generated that returns the argument with
|
2021-01-07 06:08:03 +08:00
|
|
|
the return type (in the case of attributes the return type will be constructed
|
|
|
|
from the storage type, while for operands it will be `Value`). Each attribute's
|
|
|
|
raw value (e.g., as stored) can also be accessed via generated `<name>Attr`
|
2022-04-07 19:11:11 +08:00
|
|
|
getters for use in transformation passes where the more user-friendly return
|
2021-01-07 06:08:03 +08:00
|
|
|
type is less suitable.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
2022-04-07 19:11:11 +08:00
|
|
|
All the arguments should be named to:
|
|
|
|
- provide documentation,
|
|
|
|
- drive auto-generation of getter methods, and
|
|
|
|
- provide a handle to reference for other places like constraints.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
|
|
|
#### Variadic operands
|
|
|
|
|
|
|
|
To declare a variadic operand, wrap the `TypeConstraint` for the operand with
|
|
|
|
`Variadic<...>`.
|
|
|
|
|
2019-12-06 21:58:59 +08:00
|
|
|
Normally operations have no variadic operands or just one variadic operand. For
|
|
|
|
the latter case, it is easy to deduce which dynamic operands are for the static
|
2022-04-07 19:11:11 +08:00
|
|
|
variadic operand definition. However, if an operation has more than one variable
|
2020-04-11 05:11:45 +08:00
|
|
|
length operands (either optional or variadic), it would be impossible to
|
|
|
|
attribute dynamic operands to the corresponding static variadic operand
|
|
|
|
definitions without further information from the operation. Therefore, either
|
|
|
|
the `SameVariadicOperandSize` or `AttrSizedOperandSegments` trait is needed to
|
|
|
|
indicate that all variable length operands have the same number of dynamic
|
|
|
|
values.
|
|
|
|
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-24 04:23:09 +08:00
|
|
|
#### VariadicOfVariadic operands
|
|
|
|
|
|
|
|
To declare a variadic operand that has a variadic number of sub-ranges, wrap the
|
|
|
|
`TypeConstraint` for the operand with `VariadicOfVariadic<...,
|
|
|
|
"<segment-attribute-name>">`.
|
|
|
|
|
|
|
|
The second field of the `VariadicOfVariadic` is the name of an `I32ElementsAttr`
|
|
|
|
argument that contains the sizes of the variadic sub-ranges. This attribute will
|
|
|
|
be used when determining the size of sub-ranges, or when updating the size of
|
|
|
|
sub-ranges.
|
|
|
|
|
2020-04-11 05:11:45 +08:00
|
|
|
#### Optional operands
|
|
|
|
|
|
|
|
To declare an optional operand, wrap the `TypeConstraint` for the operand with
|
|
|
|
`Optional<...>`.
|
|
|
|
|
|
|
|
Normally operations have no optional operands or just one optional operand. For
|
|
|
|
the latter case, it is easy to deduce which dynamic operands are for the static
|
2022-04-07 19:11:11 +08:00
|
|
|
operand definition. However, if an operation has more than one variable length
|
2020-04-11 05:11:45 +08:00
|
|
|
operands (either optional or variadic), it would be impossible to attribute
|
|
|
|
dynamic operands to the corresponding static variadic operand definitions
|
|
|
|
without further information from the operation. Therefore, either the
|
|
|
|
`SameVariadicOperandSize` or `AttrSizedOperandSegments` trait is needed to
|
|
|
|
indicate that all variable length operands have the same number of dynamic
|
|
|
|
values.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
|
|
|
#### Optional attributes
|
|
|
|
|
|
|
|
To declare an optional attribute, wrap the `AttrConstraint` for the attribute
|
|
|
|
with `OptionalAttr<...>`.
|
|
|
|
|
|
|
|
#### Attributes with default values
|
|
|
|
|
|
|
|
To declare an attribute with a default value, wrap the `AttrConstraint` for the
|
|
|
|
attribute with `DefaultValuedAttr<..., "...">`.
|
|
|
|
|
|
|
|
The second parameter to `DefaultValuedAttr` should be a string containing the
|
|
|
|
C++ default value. For example, a float default value should be specified as
|
|
|
|
like `"0.5f"`, and an integer array default value should be specified as like
|
|
|
|
`"{1, 2, 3}"`.
|
|
|
|
|
|
|
|
#### Confining attributes
|
|
|
|
|
|
|
|
`Confined` is provided as a general mechanism to help modelling further
|
|
|
|
constraints on attributes beyond the ones brought by value types. You can use
|
|
|
|
`Confined` to compose complex constraints out of more primitive ones. For
|
2019-12-19 01:59:37 +08:00
|
|
|
example, a 32-bit integer attribute whose minimum value must be 10 can be
|
2019-05-14 05:39:27 +08:00
|
|
|
expressed as `Confined<I32Attr, [IntMinValue<10>]>`.
|
|
|
|
|
|
|
|
Right now, the following primitive constraints are supported:
|
|
|
|
|
2019-12-19 01:59:37 +08:00
|
|
|
* `IntMinValue<N>`: Specifying an integer attribute to be greater than or
|
|
|
|
equal to `N`
|
|
|
|
* `IntMaxValue<N>`: Specifying an integer attribute to be less than or equal
|
|
|
|
to `N`
|
|
|
|
* `ArrayMinCount<N>`: Specifying an array attribute to have at least `N`
|
|
|
|
elements
|
|
|
|
* `IntArrayNthElemEq<I, N>`: Specifying an integer array attribute's `I`-th
|
|
|
|
element to be equal to `N`
|
|
|
|
* `IntArrayNthElemMinValue<I, N>`: Specifying an integer array attribute's
|
|
|
|
`I`-th element to be greater than or equal to `N`
|
2019-05-14 05:39:27 +08:00
|
|
|
|
|
|
|
TODO: Design and implement more primitive constraints
|
|
|
|
|
2020-04-05 16:03:24 +08:00
|
|
|
### Operation regions
|
|
|
|
|
|
|
|
The regions of an operation are specified inside of the `dag`-typed `regions`,
|
|
|
|
led by `region`:
|
|
|
|
|
|
|
|
```tablegen
|
|
|
|
let regions = (region
|
|
|
|
<region-constraint>:$<region-name>,
|
|
|
|
...
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Variadic regions
|
|
|
|
|
|
|
|
Similar to the `Variadic` class used for variadic operands and results,
|
|
|
|
`VariadicRegion<...>` can be used for regions. Variadic regions can currently
|
|
|
|
only be specified as the last region in the regions list.
|
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
### Operation results
|
|
|
|
|
|
|
|
Similar to operands, results are specified inside the `dag`-typed `results`, led
|
|
|
|
by `outs`:
|
|
|
|
|
2019-12-04 20:58:12 +08:00
|
|
|
```tablegen
|
2019-05-14 05:39:27 +08:00
|
|
|
let results = (outs
|
|
|
|
<type-constraint>:$<result-name>,
|
|
|
|
...
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Variadic results
|
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
Similar to variadic operands, `Variadic<...>` can also be used for results. And
|
|
|
|
similarly, `SameVariadicResultSize` for multiple variadic results in the same
|
|
|
|
operation.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
2020-02-22 05:19:50 +08:00
|
|
|
### Operation successors
|
|
|
|
|
|
|
|
For terminator operations, the successors are specified inside of the
|
|
|
|
`dag`-typed `successors`, led by `successor`:
|
|
|
|
|
|
|
|
```tablegen
|
|
|
|
let successors = (successor
|
|
|
|
<successor-constraint>:$<successor-name>,
|
|
|
|
...
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Variadic successors
|
|
|
|
|
|
|
|
Similar to the `Variadic` class used for variadic operands and results,
|
|
|
|
`VariadicSuccessor<...>` can be used for successors. Variadic successors can
|
|
|
|
currently only be specified as the last successor in the successor list.
|
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
### Operation traits and constraints
|
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
Traits are operation properties that affect syntax or semantics. MLIR C++ models
|
|
|
|
various traits in the `mlir::OpTrait` namespace.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
2021-05-25 00:40:39 +08:00
|
|
|
Both operation traits, [interfaces](Interfaces.md/#utilizing-the-ods-framework),
|
2020-12-10 07:33:49 +08:00
|
|
|
and constraints involving multiple operands/attributes/results are provided as
|
2021-09-14 04:42:24 +08:00
|
|
|
the third template parameter to the `Op` class. They should be deriving from
|
2020-12-10 07:33:49 +08:00
|
|
|
the `OpTrait` class. See [Constraints](#constraints) for more information.
|
2020-01-22 01:40:22 +08:00
|
|
|
|
2019-12-03 01:33:24 +08:00
|
|
|
### Builder methods
|
2019-05-14 05:39:27 +08:00
|
|
|
|
2019-12-03 01:33:24 +08:00
|
|
|
For each operation, there are a few builders automatically generated based on
|
|
|
|
the arguments and returns types. For example, given the following op definition:
|
2019-05-14 05:39:27 +08:00
|
|
|
|
2019-12-03 01:33:24 +08:00
|
|
|
```tablegen
|
|
|
|
def MyOp : ... {
|
|
|
|
let arguments = (ins
|
|
|
|
I32:$i32_operand,
|
|
|
|
F32:$f32_operand,
|
|
|
|
...,
|
2019-05-14 05:39:27 +08:00
|
|
|
|
2019-12-03 01:33:24 +08:00
|
|
|
I32Attr:$i32_attr,
|
|
|
|
F32Attr:$f32_attr,
|
|
|
|
...
|
|
|
|
);
|
|
|
|
|
|
|
|
let results = (outs
|
|
|
|
I32:$i32_result,
|
|
|
|
F32:$f32_result,
|
|
|
|
...
|
|
|
|
);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
The following builders are generated:
|
|
|
|
|
|
|
|
```c++
|
|
|
|
// All result-types/operands/attributes have one aggregate parameter.
|
2020-04-23 22:02:46 +08:00
|
|
|
static void build(OpBuilder &odsBuilder, OperationState &odsState,
|
2022-04-26 03:00:20 +08:00
|
|
|
TypeRange resultTypes,
|
2019-12-13 02:35:40 +08:00
|
|
|
ValueRange operands,
|
2019-05-14 05:39:27 +08:00
|
|
|
ArrayRef<NamedAttribute> attributes);
|
2019-12-03 01:33:24 +08:00
|
|
|
|
|
|
|
// Each result-type/operand/attribute has a separate parameter. The parameters
|
|
|
|
// for attributes are of mlir::Attribute types.
|
2020-04-23 22:02:46 +08:00
|
|
|
static void build(OpBuilder &odsBuilder, OperationState &odsState,
|
2019-12-03 01:33:24 +08:00
|
|
|
Type i32_result, Type f32_result, ...,
|
2019-12-24 06:45:01 +08:00
|
|
|
Value i32_operand, Value f32_operand, ...,
|
2019-12-03 01:33:24 +08:00
|
|
|
IntegerAttr i32_attr, FloatAttr f32_attr, ...);
|
|
|
|
|
|
|
|
// Each result-type/operand/attribute has a separate parameter. The parameters
|
|
|
|
// for attributes are raw values unwrapped with mlir::Attribute instances.
|
|
|
|
// (Note that this builder will not always be generated. See the following
|
|
|
|
// explanation for more details.)
|
2020-04-23 22:02:46 +08:00
|
|
|
static void build(OpBuilder &odsBuilder, OperationState &odsState,
|
2019-12-03 01:33:24 +08:00
|
|
|
Type i32_result, Type f32_result, ...,
|
2019-12-24 06:45:01 +08:00
|
|
|
Value i32_operand, Value f32_operand, ...,
|
2019-12-03 01:33:24 +08:00
|
|
|
APInt i32_attr, StringRef f32_attr, ...);
|
|
|
|
|
2019-12-13 02:35:40 +08:00
|
|
|
// Each operand/attribute has a separate parameter but result type is aggregate.
|
2020-04-23 22:02:46 +08:00
|
|
|
static void build(OpBuilder &odsBuilder, OperationState &odsState,
|
2022-04-26 03:00:20 +08:00
|
|
|
TypeRange resultTypes,
|
2019-12-24 06:45:01 +08:00
|
|
|
Value i32_operand, Value f32_operand, ...,
|
2019-12-13 02:35:40 +08:00
|
|
|
IntegerAttr i32_attr, FloatAttr f32_attr, ...);
|
|
|
|
|
|
|
|
// All operands/attributes have aggregate parameters.
|
2020-05-27 23:45:55 +08:00
|
|
|
// Generated if return type can be inferred.
|
2020-04-23 22:02:46 +08:00
|
|
|
static void build(OpBuilder &odsBuilder, OperationState &odsState,
|
2020-05-27 23:45:55 +08:00
|
|
|
ValueRange operands, ArrayRef<NamedAttribute> attributes);
|
2019-12-13 02:35:40 +08:00
|
|
|
|
|
|
|
// (And manually specified builders depending on the specific op.)
|
2019-05-14 05:39:27 +08:00
|
|
|
```
|
|
|
|
|
2019-12-03 01:33:24 +08:00
|
|
|
The first form provides basic uniformity so that we can create ops using the
|
2019-05-14 05:39:27 +08:00
|
|
|
same form regardless of the exact op. This is particularly useful for
|
|
|
|
implementing declarative pattern rewrites.
|
|
|
|
|
2022-04-07 19:11:11 +08:00
|
|
|
The second and third forms are good for use in manually written code, given that
|
2019-12-03 01:33:24 +08:00
|
|
|
they provide better guarantee via signatures.
|
|
|
|
|
|
|
|
The third form will be generated if any of the op's attribute has different
|
|
|
|
`Attr.returnType` from `Attr.storageType` and we know how to build an attribute
|
|
|
|
from an unwrapped value (i.e., `Attr.constBuilderCall` is defined.)
|
|
|
|
Additionally, for the third form, if an attribute appearing later in the
|
|
|
|
`arguments` list has a default value, the default value will be supplied in the
|
|
|
|
declaration. This works for `BoolAttr`, `StrAttr`, `EnumAttr` for now and the
|
2022-04-07 19:11:11 +08:00
|
|
|
list can grow in the future. So if possible, the default-valued attribute should be
|
2019-12-03 01:33:24 +08:00
|
|
|
placed at the end of the `arguments` list to leverage this feature. (This
|
|
|
|
behavior is essentially due to C++ function parameter default value placement
|
|
|
|
restrictions.) Otherwise, the builder of the third form will still be generated
|
|
|
|
but default values for the attributes not at the end of the `arguments` list
|
|
|
|
will not be supplied in the builder's signature.
|
|
|
|
|
2022-04-07 19:11:11 +08:00
|
|
|
ODS will generate a builder that doesn't require the return type specified if
|
2020-05-27 23:45:55 +08:00
|
|
|
|
|
|
|
* Op implements InferTypeOpInterface interface;
|
|
|
|
* All return types are either buildable types or are the same as a given
|
|
|
|
operand (e.g., `AllTypesMatch` constraint between operand and result);
|
|
|
|
|
2019-12-03 01:33:24 +08:00
|
|
|
And there may potentially exist other builders depending on the specific op;
|
|
|
|
please refer to the
|
|
|
|
[generated C++ file](#run-mlir-tblgen-to-see-the-generated-content) for the
|
|
|
|
complete list.
|
|
|
|
|
|
|
|
#### Custom builder methods
|
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
However, if the above cases cannot satisfy all needs, you can define additional
|
2020-10-16 17:40:34 +08:00
|
|
|
convenience build methods in the `builders` field as follows.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
2020-10-16 17:40:34 +08:00
|
|
|
```tablegen
|
|
|
|
def MyOp : Op<"my_op", []> {
|
|
|
|
let arguments = (ins F32Attr:$attr);
|
|
|
|
|
|
|
|
let builders = [
|
2021-03-03 22:53:09 +08:00
|
|
|
OpBuilder<(ins "float":$val)>
|
2020-10-16 17:40:34 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
The `builders` field is a list of custom builders that are added to the Op
|
|
|
|
class. In this example, we provide a convenience builder that takes a floating
|
|
|
|
point value instead of an attribute. The `ins` prefix is common to many function
|
|
|
|
declarations in ODS, which use a TableGen [`dag`](#tablegen-syntax). What
|
|
|
|
follows is a comma-separated list of types (quoted string) and names prefixed
|
|
|
|
with the `$` sign. This will generate the declaration of a builder method that
|
|
|
|
looks like:
|
|
|
|
|
|
|
|
```c++
|
|
|
|
class MyOp : /*...*/ {
|
|
|
|
/*...*/
|
|
|
|
static void build(::mlir::OpBuilder &builder, ::mlir::OperationState &state,
|
|
|
|
float val);
|
|
|
|
};
|
|
|
|
```
|
2019-05-14 05:39:27 +08:00
|
|
|
|
2020-10-16 17:40:34 +08:00
|
|
|
Note that the method has two additional leading arguments. These arguments are
|
|
|
|
useful to construct the operation. In particular, the method must populate
|
|
|
|
`state` with attributes, operands, regions and result types of the operation to
|
|
|
|
be constructed. `builder` can be used to construct any IR objects that belong to
|
|
|
|
the Op, such as types or nested operations. Since the type and name are
|
|
|
|
generated as is in the C++ code, they should be valid C++ constructs for a type
|
|
|
|
(in the namespace of the Op) and an identifier (e.g., `class` is not a valid
|
|
|
|
identifier).
|
|
|
|
|
|
|
|
Implementations of the builder can be provided directly in ODS, using TableGen
|
|
|
|
code block as follows.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
|
|
|
```tablegen
|
|
|
|
def MyOp : Op<"my_op", []> {
|
|
|
|
let arguments = (ins F32Attr:$attr);
|
|
|
|
|
2020-10-16 17:40:34 +08:00
|
|
|
let builders = [
|
2021-03-03 22:53:09 +08:00
|
|
|
OpBuilder<(ins "float":$val), [{
|
2020-10-16 17:40:34 +08:00
|
|
|
$_state.addAttribute("attr", $_builder.getF32FloatAttr(val));
|
|
|
|
}]>
|
|
|
|
];
|
2019-05-14 05:39:27 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-10-16 17:40:34 +08:00
|
|
|
The equivalents of `builder` and `state` arguments are available as `$_builder`
|
|
|
|
and `$_state` special variables. The named arguments listed in the `ins` part
|
|
|
|
are available directly, e.g. `val`. The body of the builder will be generated by
|
|
|
|
substituting special variables and should otherwise be valid C++. While there is
|
|
|
|
no limitation on the code size, we encourage one to define only short builders
|
|
|
|
inline in ODS and put definitions of longer builders in C++ files.
|
|
|
|
|
|
|
|
Finally, if some arguments need a default value, they can be defined using
|
|
|
|
`CArg` to wrap the type and this value as follows.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
|
|
|
```tablegen
|
2020-10-16 17:40:34 +08:00
|
|
|
def MyOp : Op<"my_op", []> {
|
|
|
|
let arguments = (ins F32Attr:$attr);
|
2019-05-14 05:39:27 +08:00
|
|
|
|
|
|
|
let builders = [
|
2021-03-03 22:53:09 +08:00
|
|
|
OpBuilder<(ins CArg<"float", "0.5f">:$val), [{
|
2020-09-23 01:04:21 +08:00
|
|
|
$_state.addAttribute("attr", $_builder.getF32FloatAttr(val));
|
2019-12-18 02:25:19 +08:00
|
|
|
}]>
|
2019-09-27 22:46:40 +08:00
|
|
|
];
|
2019-01-16 00:30:49 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-10-16 17:40:34 +08:00
|
|
|
The generated code will use default value in the declaration, but not in the
|
|
|
|
definition, as required by C++.
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
```c++
|
2020-11-05 02:08:34 +08:00
|
|
|
/// Header file.
|
2020-10-16 17:40:34 +08:00
|
|
|
class MyOp : /*...*/ {
|
|
|
|
/*...*/
|
|
|
|
static void build(::mlir::OpBuilder &builder, ::mlir::OperationState &state,
|
|
|
|
float val = 0.5f);
|
|
|
|
};
|
|
|
|
|
2020-11-05 02:08:34 +08:00
|
|
|
/// Source file.
|
2020-10-16 17:40:34 +08:00
|
|
|
MyOp::build(::mlir::OpBuilder &builder, ::mlir::OperationState &state,
|
|
|
|
float val) {
|
2020-04-23 22:02:46 +08:00
|
|
|
state.addAttribute("attr", builder.getF32FloatAttr(val));
|
2019-05-14 05:39:27 +08:00
|
|
|
}
|
|
|
|
```
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2020-10-16 17:40:34 +08:00
|
|
|
**Deprecated:** `OpBuilder` class allows one to specify the custom builder
|
|
|
|
signature as a raw string, without separating parameters into different `dag`
|
|
|
|
arguments. It also supports leading parameters of `OpBuilder &` and
|
|
|
|
`OperationState &` types, which will be used instead of the autogenerated ones
|
|
|
|
if present.
|
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
### Custom parser and printer methods
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
Functions to parse and print the operation's custom assembly form.
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
### Custom verifier code
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
Verification code will be automatically generated for
|
2021-01-07 06:08:03 +08:00
|
|
|
[constraints](#constraints) specified on various entities of the op. To perform
|
|
|
|
_additional_ verification, you can use
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
```tablegen
|
2022-02-02 07:01:30 +08:00
|
|
|
let hasVerifier = 1;
|
2022-02-26 02:17:30 +08:00
|
|
|
let hasRegionVerifier = 1;
|
|
|
|
```
|
|
|
|
|
2022-03-11 06:10:45 +08:00
|
|
|
This will generate `LogicalResult verify()`/`LogicalResult verifyRegions()`
|
|
|
|
method declarations on the op class that can be defined with any additional
|
|
|
|
verification constraints. For verificaiton which needs to access the nested
|
|
|
|
operations, you should use `hasRegionVerifier` to ensure that it won't access
|
|
|
|
any ill-formed operation. Except that, The other verifications can be
|
|
|
|
implemented with `hasVerifier`. Check the next section for the execution order
|
|
|
|
of these verification methods.
|
2022-02-26 02:17:30 +08:00
|
|
|
|
|
|
|
#### Verification Ordering
|
|
|
|
|
|
|
|
The verification of an operation involves several steps,
|
|
|
|
|
|
|
|
1. StructuralOpTrait will be verified first, they can be run independently.
|
2022-04-07 19:11:11 +08:00
|
|
|
2. `verifyInvariants` which is constructed by ODS, it verifies the type,
|
2022-02-26 02:17:30 +08:00
|
|
|
attributes, .etc.
|
2022-04-07 19:11:11 +08:00
|
|
|
3. Other Traits/Interfaces that have marked their verifier as `verifyTrait` or
|
2022-02-26 02:17:30 +08:00
|
|
|
`verifyWithRegions=0`.
|
2022-04-07 19:11:11 +08:00
|
|
|
4. Custom verifier which is defined in the op and has been marked `hasVerifier=1`
|
2022-02-26 02:17:30 +08:00
|
|
|
|
|
|
|
If an operation has regions, then it may have the second phase,
|
|
|
|
|
|
|
|
1. Traits/Interfaces that have marked their verifier as `verifyRegionTrait` or
|
|
|
|
`verifyWithRegions=1`. This implies the verifier needs to access the
|
|
|
|
operations in its regions.
|
2022-04-07 19:11:11 +08:00
|
|
|
2. Custom verifier which is defined in the op and has been marked
|
2022-02-26 02:17:30 +08:00
|
|
|
`hasRegionVerifier=1`
|
|
|
|
|
|
|
|
Note that the second phase will be run after the operations in the region are
|
|
|
|
verified. Verifiers further down the order can rely on certain invariants being
|
|
|
|
verified by a previous verifier and do not need to re-verify them.
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2022-03-07 23:49:46 +08:00
|
|
|
#### Emitting diagnostics in custom verifiers
|
|
|
|
|
|
|
|
Custom verifiers should avoid printing operations using custom operation
|
|
|
|
printers, because they require the printed operation (and sometimes its parent
|
|
|
|
operation) to be verified first. In particular, when emitting diagnostics,
|
|
|
|
custom verifiers should use the `Error` severity level, which prints operations
|
|
|
|
in generic form by default, and avoid using lower severity levels (`Note`,
|
|
|
|
`Remark`, `Warning`).
|
|
|
|
|
2020-02-06 02:28:30 +08:00
|
|
|
### Declarative Assembly Format
|
|
|
|
|
|
|
|
The custom assembly form of the operation may be specified in a declarative
|
|
|
|
string that matches the operations operands, attributes, etc. With the ability
|
|
|
|
to express additional information that needs to be parsed to build the
|
|
|
|
operation:
|
|
|
|
|
|
|
|
```tablegen
|
|
|
|
def CallOp : Std_Op<"call", ...> {
|
|
|
|
let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<AnyType>:$args);
|
|
|
|
let results = (outs Variadic<AnyType>);
|
|
|
|
|
|
|
|
let assemblyFormat = [{
|
|
|
|
$callee `(` $args `)` attr-dict `:` functional-type($args, results)
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
The format is comprised of three components:
|
|
|
|
|
|
|
|
#### Directives
|
|
|
|
|
|
|
|
A directive is a type of builtin function, with an optional set of arguments.
|
|
|
|
The available directives are as follows:
|
|
|
|
|
2020-02-22 05:19:26 +08:00
|
|
|
* `attr-dict`
|
2020-02-06 02:28:30 +08:00
|
|
|
|
2020-02-22 05:19:26 +08:00
|
|
|
- Represents the attribute dictionary of the operation.
|
2020-02-06 02:28:30 +08:00
|
|
|
|
2020-02-22 05:19:26 +08:00
|
|
|
* `attr-dict-with-keyword`
|
2020-02-06 02:28:30 +08:00
|
|
|
|
2020-02-22 05:19:26 +08:00
|
|
|
- Represents the attribute dictionary of the operation, but prefixes the
|
|
|
|
dictionary with an `attributes` keyword.
|
2020-02-06 02:28:30 +08:00
|
|
|
|
2020-09-01 03:33:36 +08:00
|
|
|
* `custom` < UserDirective > ( Params )
|
|
|
|
|
|
|
|
- Represents a custom directive implemented by the user in C++.
|
|
|
|
- See the [Custom Directives](#custom-directives) section below for more
|
|
|
|
details.
|
|
|
|
|
2020-02-22 05:19:26 +08:00
|
|
|
* `functional-type` ( inputs , results )
|
|
|
|
|
|
|
|
- Formats the `inputs` and `results` arguments as a
|
2021-05-25 00:40:39 +08:00
|
|
|
[function type](Dialects/Builtin.md/#functiontype).
|
2020-02-22 05:19:26 +08:00
|
|
|
- The constraints on `inputs` and `results` are the same as the `input` of
|
|
|
|
the `type` directive.
|
|
|
|
|
2022-02-17 12:54:10 +08:00
|
|
|
* `oilist` ( \`keyword\` elements | \`otherKeyword\` elements ...)
|
|
|
|
|
|
|
|
- Represents an optional order-independent list of clauses. Each clause
|
|
|
|
has a keyword and corresponding assembly format.
|
|
|
|
- Each clause can appear 0 or 1 time (in any order).
|
|
|
|
- Only literals, types and variables can be used within an oilist element.
|
|
|
|
- All the variables must be optional or variadic.
|
|
|
|
|
2020-02-22 05:19:26 +08:00
|
|
|
* `operands`
|
|
|
|
|
|
|
|
- Represents all of the operands of an operation.
|
|
|
|
|
2021-02-10 06:32:15 +08:00
|
|
|
* `ref` ( input )
|
|
|
|
|
|
|
|
- Represents a reference to the a variable or directive, that must have
|
|
|
|
already been resolved, that may be used as a parameter to a `custom`
|
|
|
|
directive.
|
|
|
|
- Used to pass previously parsed entities to custom directives.
|
|
|
|
- The input may be any directive or variable, aside from `functional-type`
|
|
|
|
and `custom`.
|
|
|
|
|
2020-09-01 03:33:55 +08:00
|
|
|
* `regions`
|
|
|
|
|
|
|
|
- Represents all of the regions of an operation.
|
|
|
|
|
2020-02-22 05:19:26 +08:00
|
|
|
* `results`
|
|
|
|
|
|
|
|
- Represents all of the results of an operation.
|
|
|
|
|
2020-02-22 05:20:06 +08:00
|
|
|
* `successors`
|
|
|
|
|
|
|
|
- Represents all of the successors of an operation.
|
|
|
|
|
2020-02-22 05:19:26 +08:00
|
|
|
* `type` ( input )
|
|
|
|
|
|
|
|
- Represents the type of the given input.
|
|
|
|
- `input` must be either an operand or result [variable](#variables), the
|
|
|
|
`operands` directive, or the `results` directive.
|
2020-02-06 02:28:30 +08:00
|
|
|
|
2022-01-11 09:26:44 +08:00
|
|
|
* `qualified` ( type_or_attribute )
|
|
|
|
|
|
|
|
- Wraps a `type` directive or an attribute parameter.
|
|
|
|
- Used to force printing the type or attribute prefixed with its dialect
|
|
|
|
and mnemonic. For example the `vector.multi_reduction` operation has a
|
|
|
|
`kind` attribute ; by default the declarative assembly will print:
|
|
|
|
`vector.multi_reduction <minf>, ...` but using `qualified($kind)` in the
|
|
|
|
declarative assembly format will print it instead as:
|
|
|
|
`vector.multi_reduction #vector.kind<minf>, ...`.
|
|
|
|
|
2020-02-06 02:28:30 +08:00
|
|
|
#### Literals
|
|
|
|
|
|
|
|
A literal is either a keyword or punctuation surrounded by \`\`.
|
|
|
|
|
|
|
|
The following are the set of valid punctuation:
|
2020-09-01 03:33:55 +08:00
|
|
|
|
2020-11-12 01:01:39 +08:00
|
|
|
`:`, `,`, `=`, `<`, `>`, `(`, `)`, `{`, `}`, `[`, `]`, `->`, `?`, `+`, `*`
|
2020-02-06 02:28:30 +08:00
|
|
|
|
2020-12-15 03:53:34 +08:00
|
|
|
The following are valid whitespace punctuation:
|
|
|
|
|
|
|
|
`\n`, ` `
|
|
|
|
|
|
|
|
The `\n` literal emits a newline an indents to the start of the operation. An
|
|
|
|
example is shown below:
|
|
|
|
|
|
|
|
```tablegen
|
|
|
|
let assemblyFormat = [{
|
|
|
|
`{` `\n` ` ` ` ` `this_is_on_a_newline` `\n` `}` attr-dict
|
|
|
|
}];
|
|
|
|
```
|
|
|
|
|
|
|
|
```mlir
|
|
|
|
%results = my.operation {
|
|
|
|
this_is_on_a_newline
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
An empty literal \`\` may be used to remove a space that is inserted implicitly
|
|
|
|
after certain literal elements, such as `)`/`]`/etc. For example, "`]`" may
|
|
|
|
result in an output of `]` it is not the last element in the format. "`]` \`\`"
|
|
|
|
would trim the trailing space in this situation.
|
|
|
|
|
2020-02-06 02:28:30 +08:00
|
|
|
#### Variables
|
|
|
|
|
|
|
|
A variable is an entity that has been registered on the operation itself, i.e.
|
2020-09-01 03:33:55 +08:00
|
|
|
an argument(attribute or operand), region, result, successor, etc. In the
|
|
|
|
`CallOp` example above, the variables would be `$callee` and `$args`.
|
2020-02-06 02:28:30 +08:00
|
|
|
|
2020-02-09 02:01:17 +08:00
|
|
|
Attribute variables are printed with their respective value type, unless that
|
|
|
|
value type is buildable. In those cases, the type of the attribute is elided.
|
|
|
|
|
2020-09-01 03:33:36 +08:00
|
|
|
#### Custom Directives
|
|
|
|
|
|
|
|
The declarative assembly format specification allows for handling a large
|
|
|
|
majority of the common cases when formatting an operation. For the operations
|
|
|
|
that require or desire specifying parts of the operation in a form not supported
|
|
|
|
by the declarative syntax, custom directives may be specified. A custom
|
|
|
|
directive essentially allows for users to use C++ for printing and parsing
|
|
|
|
subsections of an otherwise declaratively specified format. Looking at the
|
|
|
|
specification of a custom directive above:
|
|
|
|
|
|
|
|
```
|
|
|
|
custom-directive ::= `custom` `<` UserDirective `>` `(` Params `)`
|
|
|
|
```
|
|
|
|
|
|
|
|
A custom directive has two main parts: The `UserDirective` and the `Params`. A
|
|
|
|
custom directive is transformed into a call to a `print*` and a `parse*` method
|
|
|
|
when generating the C++ code for the format. The `UserDirective` is an
|
|
|
|
identifier used as a suffix to these two calls, i.e., `custom<MyDirective>(...)`
|
2020-10-29 03:03:15 +08:00
|
|
|
would result in calls to `parseMyDirective` and `printMyDirective` within the
|
2020-09-01 03:33:36 +08:00
|
|
|
parser and printer respectively. `Params` may be any combination of variables
|
2020-10-28 09:01:44 +08:00
|
|
|
(i.e. Attribute, Operand, Successor, etc.), type directives, and `attr-dict`.
|
2021-01-07 06:08:03 +08:00
|
|
|
The type directives must refer to a variable, but that variable need not also be
|
|
|
|
a parameter to the custom directive.
|
2020-09-01 03:33:36 +08:00
|
|
|
|
2020-10-28 09:01:44 +08:00
|
|
|
The arguments to the `parse<UserDirective>` method are firstly a reference to
|
|
|
|
the `OpAsmParser`(`OpAsmParser &`), and secondly a set of output parameters
|
2020-09-01 03:33:36 +08:00
|
|
|
corresponding to the parameters specified in the format. The mapping of
|
|
|
|
declarative parameter to `parse` method argument is detailed below:
|
|
|
|
|
|
|
|
* Attribute Variables
|
|
|
|
- Single: `<Attribute-Storage-Type>(e.g. Attribute) &`
|
|
|
|
- Optional: `<Attribute-Storage-Type>(e.g. Attribute) &`
|
|
|
|
* Operand Variables
|
2022-03-22 04:42:13 +08:00
|
|
|
- Single: `OpAsmParser::UnresolvedOperand &`
|
|
|
|
- Optional: `Optional<OpAsmParser::UnresolvedOperand> &`
|
|
|
|
- Variadic: `SmallVectorImpl<OpAsmParser::UnresolvedOperand> &`
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-24 04:23:09 +08:00
|
|
|
- VariadicOfVariadic:
|
2022-03-22 04:42:13 +08:00
|
|
|
`SmallVectorImpl<SmallVector<OpAsmParser::UnresolvedOperand>> &`
|
2021-02-10 06:32:15 +08:00
|
|
|
* Ref Directives
|
|
|
|
- A reference directive is passed to the parser using the same mapping as
|
|
|
|
the input operand. For example, a single region would be passed as a
|
|
|
|
`Region &`.
|
2020-09-01 03:33:55 +08:00
|
|
|
* Region Variables
|
|
|
|
- Single: `Region &`
|
|
|
|
- Variadic: `SmallVectorImpl<std::unique_ptr<Region>> &`
|
2020-09-01 03:33:36 +08:00
|
|
|
* Successor Variables
|
|
|
|
- Single: `Block *&`
|
|
|
|
- Variadic: `SmallVectorImpl<Block *> &`
|
|
|
|
* Type Directives
|
|
|
|
- Single: `Type &`
|
|
|
|
- Optional: `Type &`
|
|
|
|
- Variadic: `SmallVectorImpl<Type> &`
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-24 04:23:09 +08:00
|
|
|
- VariadicOfVariadic: `SmallVectorImpl<SmallVector<Type>> &`
|
2020-10-28 09:01:44 +08:00
|
|
|
* `attr-dict` Directive: `NamedAttrList &`
|
2020-09-01 03:33:36 +08:00
|
|
|
|
|
|
|
When a variable is optional, the value should only be specified if the variable
|
|
|
|
is present. Otherwise, the value should remain `None` or null.
|
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
The arguments to the `print<UserDirective>` method is firstly a reference to the
|
|
|
|
`OpAsmPrinter`(`OpAsmPrinter &`), second the op (e.g. `FooOp op` which can be
|
|
|
|
`Operation *op` alternatively), and finally a set of output parameters
|
2020-09-01 03:33:36 +08:00
|
|
|
corresponding to the parameters specified in the format. The mapping of
|
|
|
|
declarative parameter to `print` method argument is detailed below:
|
|
|
|
|
|
|
|
* Attribute Variables
|
|
|
|
- Single: `<Attribute-Storage-Type>(e.g. Attribute)`
|
|
|
|
- Optional: `<Attribute-Storage-Type>(e.g. Attribute)`
|
|
|
|
* Operand Variables
|
|
|
|
- Single: `Value`
|
|
|
|
- Optional: `Value`
|
|
|
|
- Variadic: `OperandRange`
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-24 04:23:09 +08:00
|
|
|
- VariadicOfVariadic: `OperandRangeRange`
|
2021-02-10 06:32:15 +08:00
|
|
|
* Ref Directives
|
|
|
|
- A reference directive is passed to the printer using the same mapping as
|
|
|
|
the input operand. For example, a single region would be passed as a
|
|
|
|
`Region &`.
|
2020-09-01 03:33:55 +08:00
|
|
|
* Region Variables
|
|
|
|
- Single: `Region &`
|
|
|
|
- Variadic: `MutableArrayRef<Region>`
|
2020-09-01 03:33:36 +08:00
|
|
|
* Successor Variables
|
|
|
|
- Single: `Block *`
|
|
|
|
- Variadic: `SuccessorRange`
|
|
|
|
* Type Directives
|
|
|
|
- Single: `Type`
|
|
|
|
- Optional: `Type`
|
|
|
|
- Variadic: `TypeRange`
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-24 04:23:09 +08:00
|
|
|
- VariadicOfVariadic: `TypeRangeRange`
|
2020-12-18 09:10:12 +08:00
|
|
|
* `attr-dict` Directive: `DictionaryAttr`
|
2020-09-01 03:33:36 +08:00
|
|
|
|
|
|
|
When a variable is optional, the provided value may be null.
|
|
|
|
|
2020-02-22 05:19:15 +08:00
|
|
|
#### Optional Groups
|
|
|
|
|
|
|
|
In certain situations operations may have "optional" information, e.g.
|
[mlir] NFC: Fix trivial typos in documents
Fix trivial typos
Reviewers: mravishankar, antiagainst, ftynse
Reviewed By: ftynse
Subscribers: ftynse, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, Joonsoo, bader, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76347
2020-03-18 17:38:55 +08:00
|
|
|
attributes or an empty set of variadic operands. In these situations a section
|
2020-02-22 05:19:15 +08:00
|
|
|
of the assembly format can be marked as `optional` based on the presence of this
|
2021-03-23 09:07:09 +08:00
|
|
|
information. An optional group is defined as follows:
|
|
|
|
|
|
|
|
```
|
|
|
|
optional-group: `(` elements `)` (`:` `(` else-elements `)`)? `?`
|
|
|
|
```
|
|
|
|
|
|
|
|
The `elements` of an optional group have the following requirements:
|
2020-02-22 05:19:15 +08:00
|
|
|
|
2020-09-01 03:33:55 +08:00
|
|
|
* The first element of the group must either be a attribute, literal, operand,
|
|
|
|
or region.
|
2020-02-22 05:19:15 +08:00
|
|
|
- This is because the first element must be optionally parsable.
|
2021-01-23 04:07:07 +08:00
|
|
|
* Exactly one argument variable or type directive within the group must be
|
|
|
|
marked as the anchor of the group.
|
2020-02-22 05:19:15 +08:00
|
|
|
- The anchor is the element whose presence controls whether the group
|
|
|
|
should be printed/parsed.
|
|
|
|
- An element is marked as the anchor by adding a trailing `^`.
|
|
|
|
- The first element is *not* required to be the anchor of the group.
|
2020-09-01 03:33:55 +08:00
|
|
|
- When a non-variadic region anchors a group, the detector for printing
|
|
|
|
the group is if the region is empty.
|
2020-09-01 03:33:36 +08:00
|
|
|
* Literals, variables, custom directives, and type directives are the only
|
|
|
|
valid elements within the group.
|
2020-02-22 05:19:15 +08:00
|
|
|
- Any attribute variable may be used, but only optional attributes can be
|
|
|
|
marked as the anchor.
|
2021-01-23 04:07:07 +08:00
|
|
|
- Only variadic or optional results and operand arguments and can be used.
|
2020-09-01 03:33:55 +08:00
|
|
|
- All region variables can be used. When a non-variable length region is
|
|
|
|
used, if the group is not present the region is empty.
|
2020-02-22 05:19:15 +08:00
|
|
|
|
2022-02-27 06:49:54 +08:00
|
|
|
An example of an operation with an optional group is `func.return`, which has a
|
2020-02-22 05:19:15 +08:00
|
|
|
variadic number of operands.
|
|
|
|
|
2020-08-04 05:20:50 +08:00
|
|
|
```tablegen
|
2020-02-22 05:19:15 +08:00
|
|
|
def ReturnOp : ... {
|
|
|
|
let arguments = (ins Variadic<AnyType>:$operands);
|
|
|
|
|
|
|
|
// We only print the operands and types if there are a non-zero number
|
|
|
|
// of operands.
|
|
|
|
let assemblyFormat = "attr-dict ($operands^ `:` type($operands))?";
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-08-04 05:20:50 +08:00
|
|
|
##### Unit Attributes
|
|
|
|
|
2021-05-25 00:40:39 +08:00
|
|
|
In MLIR, the [`unit` Attribute](Dialects/Builtin.md/#unitattr) is special in that it
|
2020-08-04 05:20:50 +08:00
|
|
|
only has one possible value, i.e. it derives meaning from its existence. When a
|
|
|
|
unit attribute is used to anchor an optional group and is not the first element
|
|
|
|
of the group, the presence of the unit attribute can be directly correlated with
|
|
|
|
the presence of the optional group itself. As such, in these situations the unit
|
|
|
|
attribute will not be printed or present in the output and will be automatically
|
|
|
|
inferred when parsing by the presence of the optional group itself.
|
|
|
|
|
|
|
|
For example, the following operation:
|
|
|
|
|
|
|
|
```tablegen
|
|
|
|
def FooOp : ... {
|
|
|
|
let arguments = (ins UnitAttr:$is_read_only);
|
|
|
|
|
|
|
|
let assemblyFormat = "attr-dict (`is_read_only` $is_read_only^)?";
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
would be formatted as such:
|
|
|
|
|
|
|
|
```mlir
|
|
|
|
// When the unit attribute is present:
|
|
|
|
foo.op is_read_only
|
|
|
|
|
|
|
|
// When the unit attribute is not present:
|
|
|
|
foo.op
|
|
|
|
```
|
|
|
|
|
2021-03-23 09:07:09 +08:00
|
|
|
##### Optional "else" Group
|
|
|
|
|
|
|
|
Optional groups also have support for an "else" group of elements. These are
|
|
|
|
elements that are parsed/printed if the `anchor` element of the optional group
|
|
|
|
is *not* present. Unlike the main element group, the "else" group has no
|
|
|
|
restriction on the first element and none of the elements may act as the
|
|
|
|
`anchor` for the optional. An example is shown below:
|
|
|
|
|
|
|
|
```tablegen
|
|
|
|
def FooOp : ... {
|
|
|
|
let arguments = (ins UnitAttr:$foo);
|
|
|
|
|
|
|
|
let assemblyFormat = "attr-dict (`foo_is_present` $foo^):(`foo_is_absent`)?";
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
would be formatted as such:
|
|
|
|
|
|
|
|
```mlir
|
|
|
|
// When the `foo` attribute is present:
|
|
|
|
foo.op foo_is_present
|
|
|
|
|
|
|
|
// When the `foo` attribute is not present:
|
|
|
|
foo.op foo_is_absent
|
|
|
|
```
|
|
|
|
|
2020-02-06 02:28:30 +08:00
|
|
|
#### Requirements
|
|
|
|
|
|
|
|
The format specification has a certain set of requirements that must be adhered
|
|
|
|
to:
|
|
|
|
|
2020-09-01 03:33:55 +08:00
|
|
|
1. The output and operation name are never shown as they are fixed and cannot
|
|
|
|
be altered.
|
|
|
|
1. All operands within the operation must appear within the format, either
|
|
|
|
individually or with the `operands` directive.
|
|
|
|
1. All regions within the operation must appear within the format, either
|
|
|
|
individually or with the `regions` directive.
|
|
|
|
1. All successors within the operation must appear within the format, either
|
|
|
|
individually or with the `successors` directive.
|
|
|
|
1. All operand and result types must appear within the format using the various
|
|
|
|
`type` directives, either individually or with the `operands` or `results`
|
|
|
|
directives.
|
|
|
|
1. The `attr-dict` directive must always be present.
|
|
|
|
1. Must not contain overlapping information; e.g. multiple instances of
|
|
|
|
'attr-dict', types, operands, etc.
|
|
|
|
- Note that `attr-dict` does not overlap with individual attributes. These
|
|
|
|
attributes will simply be elided when printing the attribute dictionary.
|
2020-02-06 02:28:30 +08:00
|
|
|
|
[mlir] NFC: fix trivial typo in documents
Reviewers: mravishankar, antiagainst, nicolasvasilache, herhut, aartbik, mehdi_amini, bondhugula
Reviewed By: mehdi_amini, bondhugula
Subscribers: bondhugula, jdoerfert, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, csigg, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, Joonsoo, bader, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76993
2020-03-29 02:20:02 +08:00
|
|
|
##### Type Inference
|
2020-02-06 02:28:30 +08:00
|
|
|
|
|
|
|
One requirement of the format is that the types of operands and results must
|
|
|
|
always be present. In certain instances, the type of a variable may be deduced
|
|
|
|
via type constraints or other information available. In these cases, the type of
|
|
|
|
that variable may be elided from the format.
|
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
* Buildable Types
|
2020-02-06 02:28:30 +08:00
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
Some type constraints may only have one representation, allowing for them to be
|
|
|
|
directly buildable; for example the `I32` or `Index` types. Types in `ODS` may
|
|
|
|
mark themselves as buildable by setting the `builderCall` field or inheriting
|
|
|
|
from the `BuildableType` class.
|
2020-02-06 02:28:30 +08:00
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
* Trait Equality Constraints
|
2020-02-06 02:28:30 +08:00
|
|
|
|
|
|
|
There are many operations that have known type equality constraints registered
|
|
|
|
as traits on the operation; for example the true, false, and result values of a
|
|
|
|
`select` operation often have the same type. The assembly format may inspect
|
|
|
|
these equal constraints to discern the types of missing variables. The currently
|
2021-01-07 06:08:03 +08:00
|
|
|
supported traits are: `AllTypesMatch`, `TypesMatchWith`, `SameTypeOperands`, and
|
|
|
|
`SameOperandsAndResultType`.
|
2020-02-06 02:28:30 +08:00
|
|
|
|
2021-10-07 08:50:38 +08:00
|
|
|
* InferTypeOpInterface
|
|
|
|
|
|
|
|
Operations that implement `InferTypeOpInterface` can omit their result types in
|
|
|
|
their assembly format since the result types can be inferred from the operands.
|
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
### `hasCanonicalizer`
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
This boolean field indicate whether canonicalization patterns have been defined
|
|
|
|
for this operation. If it is `1`, then `::getCanonicalizationPatterns()` should
|
|
|
|
be defined.
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2021-03-23 13:15:39 +08:00
|
|
|
### `hasCanonicalizeMethod`
|
|
|
|
|
|
|
|
When this boolean field is set to `true`, it indicates that the op implements a
|
|
|
|
`canonicalize` method for simple "matchAndRewrite" style canonicalization
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-24 04:23:09 +08:00
|
|
|
patterns. If `hasCanonicalizer` is 0, then an implementation of
|
2021-03-23 13:15:39 +08:00
|
|
|
`::getCanonicalizationPatterns()` is implemented to call this function.
|
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
### `hasFolder`
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
This boolean field indicate whether general folding rules have been defined for
|
|
|
|
this operation. If it is `1`, then `::fold()` should be defined.
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
### Extra declarations
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
One of the goals of table-driven op definition is to auto-generate as much logic
|
2019-09-22 02:38:41 +08:00
|
|
|
and methods needed for each op as possible. With that said, there will always be
|
2019-05-14 05:39:27 +08:00
|
|
|
long-tail cases that won't be covered. For such cases, you can use
|
|
|
|
`extraClassDeclaration`. Code in `extraClassDeclaration` will be copied
|
|
|
|
literally to the generated C++ op class.
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
Note that `extraClassDeclaration` is a mechanism intended for long-tail cases by
|
|
|
|
power users; for not-yet-implemented widely-applicable cases, improving the
|
2019-05-14 05:39:27 +08:00
|
|
|
infrastructure is preferable.
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2022-01-06 09:42:12 +08:00
|
|
|
### Extra definitions
|
|
|
|
|
|
|
|
When defining base op classes in TableGen that are inherited many times by
|
|
|
|
different ops, users may want to provide common definitions of utility and
|
|
|
|
interface functions. However, many of these definitions may not be desirable or
|
|
|
|
possible in `extraClassDeclaration`, which append them to the op's C++ class
|
|
|
|
declaration. In these cases, users can add an `extraClassDefinition` to define
|
|
|
|
code that is added to the generated source file inside the op's C++ namespace.
|
|
|
|
The substitution `$cppClass` is replaced by the op's C++ class name.
|
|
|
|
|
2019-05-21 00:33:10 +08:00
|
|
|
### Generated C++ code
|
|
|
|
|
|
|
|
[OpDefinitionsGen][OpDefinitionsGen] processes the op definition spec file and
|
|
|
|
generates two files containing the corresponding C++ code: one for declarations,
|
|
|
|
the other for definitions. The former is generated via the `-gen-op-decls`
|
|
|
|
command-line option, while the latter is via the `-gen-op-defs` option.
|
|
|
|
|
|
|
|
The definition file contains all the op method definitions, which can be
|
2019-06-03 23:03:20 +08:00
|
|
|
included and enabled by defining `GET_OP_CLASSES`. For each operation,
|
|
|
|
OpDefinitionsGen generates an operation class and an
|
|
|
|
[operand adaptor](#operand-adaptors) class. Besides, it also contains a
|
|
|
|
comma-separated list of all defined ops, which can be included and enabled by
|
|
|
|
defining `GET_OP_LIST`.
|
2019-05-21 00:33:10 +08:00
|
|
|
|
2019-06-03 23:03:20 +08:00
|
|
|
#### Class name and namespaces
|
2019-05-21 00:33:10 +08:00
|
|
|
|
|
|
|
For each operation, its generated C++ class name is the symbol `def`ed with
|
2021-01-07 06:08:03 +08:00
|
|
|
TableGen with dialect prefix removed. The first `_` serves as the delimiter. For
|
|
|
|
example, for `def TF_AddOp`, the C++ class name would be `AddOp`. We remove the
|
|
|
|
`TF` prefix because it is for scoping ops; other dialects may as well define
|
|
|
|
their own `AddOp`s.
|
2019-05-21 00:33:10 +08:00
|
|
|
|
|
|
|
The namespaces of the generated C++ class will come from the dialect's
|
2021-01-07 06:08:03 +08:00
|
|
|
`cppNamespace` field. For example, if a dialect's `cppNamespace` is `A::B`, then
|
|
|
|
an op of that dialect will be placed in `namespace A { namespace B { ... } }`.
|
|
|
|
If a dialect does not specify a `cppNamespace`, we then use the dialect's name
|
|
|
|
as the namespace.
|
2019-05-21 00:33:10 +08:00
|
|
|
|
|
|
|
This means the qualified name of the generated C++ class does not necessarily
|
|
|
|
match exactly with the operation name as explained in
|
|
|
|
[Operation name](#operation-name). This is to allow flexible naming to satisfy
|
|
|
|
coding style requirements.
|
|
|
|
|
2019-06-03 23:03:20 +08:00
|
|
|
#### Operand adaptors
|
|
|
|
|
|
|
|
For each operation, we automatically generate an _operand adaptor_. This class
|
|
|
|
solves the problem of accessing operands provided as a list of `Value`s without
|
|
|
|
using "magic" constants. The operand adaptor takes a reference to an array of
|
2019-12-24 06:45:01 +08:00
|
|
|
`Value` and provides methods with the same names as those in the operation class
|
|
|
|
to access them. For example, for a binary arithmetic operation, it may provide
|
|
|
|
`.lhs()` to access the first operand and `.rhs()` to access the second operand.
|
2019-06-03 23:03:20 +08:00
|
|
|
|
|
|
|
The operand adaptor class lives in the same namespace as the operation class,
|
2020-06-15 21:01:31 +08:00
|
|
|
and has the name of the operation followed by `Adaptor` as well as an alias
|
|
|
|
`Adaptor` inside the op class.
|
2019-06-03 23:03:20 +08:00
|
|
|
|
|
|
|
Operand adaptors can be used in function templates that also process operations:
|
|
|
|
|
|
|
|
```c++
|
|
|
|
template <typename BinaryOpTy>
|
2019-12-24 06:45:01 +08:00
|
|
|
std::pair<Value, Value> zip(BinaryOpTy &&op) {
|
2019-06-03 23:03:20 +08:00
|
|
|
return std::make_pair(op.lhs(), op.rhs());;
|
|
|
|
}
|
|
|
|
|
2019-12-24 06:45:01 +08:00
|
|
|
void process(AddOp op, ArrayRef<Value> newOperands) {
|
2019-06-03 23:03:20 +08:00
|
|
|
zip(op);
|
2020-06-15 21:01:31 +08:00
|
|
|
zip(Adaptor<AddOp>(newOperands));
|
2019-06-03 23:03:20 +08:00
|
|
|
/*...*/
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
## Constraints
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
Constraint is a core concept in table-driven operation definition: operation
|
|
|
|
verification and graph operation matching are all based on satisfying
|
|
|
|
constraints. So both the operation definition and rewrite rules specification
|
|
|
|
significantly involve writing constraints. We have the `Constraint` class in
|
2021-09-14 04:42:24 +08:00
|
|
|
[`OpBase.td`][OpBase] as the common base class for all constraints.
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
An operation's constraint can cover different range; it may
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
* Only concern a single attribute (e.g. being a 32-bit integer greater than
|
|
|
|
5),
|
|
|
|
* Multiple operands and results (e.g., the 1st result's shape must be the same
|
|
|
|
as the 1st operand), or
|
|
|
|
* Intrinsic to the operation itself (e.g., having no side effect).
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
We call them as single-entity constraint, multi-entity constraint, and traits,
|
|
|
|
respectively.
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
### Single-entity constraint
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
Constraints scoped to a single operand, attribute, or result are specified at
|
|
|
|
the entity's declaration place as described in
|
|
|
|
[Operation arguments](#operation-arguments) and
|
|
|
|
[Operation results](#operation-results).
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
To help modelling constraints of common types, a set of `TypeConstraint`s are
|
|
|
|
created; they are the `Type` subclass hierarchy. It includes `F32` for the
|
2021-01-07 06:08:03 +08:00
|
|
|
constraints of being a float, `TensorOf<[F32]>` for the constraints of being a
|
|
|
|
float tensor, and so on.
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
Similarly, a set of `AttrConstraint`s are created for helping modelling
|
|
|
|
constraints of common attribute kinds. They are the `Attr` subclass hierarchy.
|
2019-09-26 02:57:13 +08:00
|
|
|
It includes `F32Attr` for the constraints of being a float attribute,
|
2019-05-14 05:39:27 +08:00
|
|
|
`F32ArrayAttr` for the constraints of being a float array attribute, and so on.
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
### Multi-entity constraint
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
Constraints involving more than one operand/attribute/result are quite common on
|
|
|
|
operations, like the element type and shape relation between operands and
|
2019-05-14 05:39:27 +08:00
|
|
|
results. These constraints should be specified as the `Op` class template
|
|
|
|
parameter as described in
|
|
|
|
[Operation traits and constraints](#operation-traits-and-constraints).
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
Multi-entity constraints are modeled as `PredOpTrait` (a subclass of `OpTrait`)
|
|
|
|
in [`OpBase.td`][OpBase].A bunch of constraint primitives are provided to help
|
|
|
|
specification. See [`OpBase.td`][OpBase] for the complete list.
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
### Trait
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
Traits are intrinsic properties of the operation like having side effect or not,
|
|
|
|
commutative or not, whether is a terminator, etc. These constraints should be
|
|
|
|
specified as the `Op` class template parameter as described in
|
|
|
|
[Operation traits and constraints](#operation-traits-and-constraints).
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
Traits are modeled as `NativeOpTrait` (a subclass of `OpTrait`) in
|
|
|
|
[`OpBase.td`][OpBase]. They are backed and will be translated into the
|
|
|
|
corresponding C++ `mlir::OpTrait` classes.
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
### How to specify new constraint
|
|
|
|
|
|
|
|
To write a constraint, you need to provide its predicates and give it a
|
|
|
|
descriptive name. Predicates, modeled with the `Pred` class, are the workhorse
|
|
|
|
for composing constraints. The predicate for a constraint is typically built up
|
|
|
|
in a nested manner, using the two categories of predicates:
|
|
|
|
|
2019-05-21 01:31:32 +08:00
|
|
|
1. `CPred`: the primitive leaf predicate.
|
|
|
|
2. Compound predicate: a predicate composed from child predicates using
|
|
|
|
predicate combiners (conjunction: `And`, disjunction: `Or`, negation: `Neg`,
|
|
|
|
substitution: `SubstLeaves`, concatenation: `Concat`).
|
2019-05-14 05:39:27 +08:00
|
|
|
|
|
|
|
`CPred` is the basis for composing more complex predicates. It is the "atom"
|
2021-01-07 06:08:03 +08:00
|
|
|
predicate from the perspective of TableGen and the "interface" between TableGen
|
|
|
|
and C++. What is inside is already C++ code, which will be treated as opaque
|
|
|
|
strings with special placeholders to be substituted.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
|
|
|
You can put any C++ code that returns a boolean value inside a `CPred`,
|
2021-01-07 06:08:03 +08:00
|
|
|
including evaluating expressions, calling functions, calling class methods, and
|
|
|
|
so on.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
|
|
|
To help interaction with the C++ environment, there are a few special
|
|
|
|
placeholders provided to refer to entities in the context where this predicate
|
2021-01-07 06:08:03 +08:00
|
|
|
is used. They serve as "hooks" to the enclosing environment. This includes
|
2019-05-14 05:39:27 +08:00
|
|
|
`$_builder`, `$_op`, and `$_self`:
|
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
* `$_builder` will be replaced by a `mlir::Builder` instance so that you can
|
|
|
|
access common build methods.
|
|
|
|
* `$_op` will be replaced by the current operation so that you can access
|
|
|
|
information of the current operation.
|
|
|
|
* `$_self` will be replaced with the entity this predicate is attached to.
|
|
|
|
E.g., `BoolAttr` is an attribute constraint that wraps a
|
2021-09-28 04:26:47 +08:00
|
|
|
`CPred<"$_self.isa<BoolAttr>()">`. Then for `BoolAttr:$attr`,`$_self` will be
|
2021-01-07 06:08:03 +08:00
|
|
|
replaced by `$attr`. For type constraints, it's a little bit special since
|
|
|
|
we want the constraints on each type definition reads naturally and we want
|
|
|
|
to attach type constraints directly to an operand/result, `$_self` will be
|
|
|
|
replaced by the operand/result's type. E.g., for `F32` in `F32:$operand`,
|
2021-09-14 04:42:24 +08:00
|
|
|
its `$_self` will be expanded as `operand(...).getType()`.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
2020-07-07 16:35:23 +08:00
|
|
|
TODO: Reconsider the leading symbol for special placeholders. Eventually we want
|
2021-09-14 04:42:24 +08:00
|
|
|
to allow referencing operand/result `$-name`s; such `$-name`s can start with
|
2020-07-07 16:35:23 +08:00
|
|
|
underscore.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
|
|
|
For example, to write an attribute `attr` is an `IntegerAttr`, in C++ you can
|
|
|
|
just call `attr.isa<IntegerAttr>()`. The code can be wrapped in a `CPred` as
|
|
|
|
`$_self.isa<IntegerAttr>()`, with `$_self` as the special placeholder to be
|
|
|
|
replaced by the current attribute `attr` at expansion time.
|
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
For more complicated predicates, you can wrap it in a single `CPred`, or you can
|
|
|
|
use predicate combiners to combine them. For example, to write the constraint
|
|
|
|
that an attribute `attr` is a 32-bit or 64-bit integer, you can write it as
|
2019-01-16 00:30:49 +08:00
|
|
|
|
|
|
|
```tablegen
|
2019-05-21 01:31:32 +08:00
|
|
|
And<[
|
2019-05-14 05:39:27 +08:00
|
|
|
CPred<"$_self.isa<IntegerAttr>()">,
|
2019-05-21 01:31:32 +08:00
|
|
|
Or<[
|
2019-05-14 05:39:27 +08:00
|
|
|
CPred<"$_self.cast<IntegerAttr>().getType().isInteger(32)">,
|
|
|
|
CPred<"$_self.cast<IntegerAttr>().getType().isInteger(64)">
|
|
|
|
]>
|
|
|
|
]>
|
2019-01-16 00:30:49 +08:00
|
|
|
```
|
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
(Note that the above is just to show with a familiar example how you can use
|
|
|
|
`CPred` and predicate combiners to write complicated predicates. For integer
|
|
|
|
attributes specifically, [`OpBase.td`][OpBase] already defines `I32Attr` and
|
2019-05-21 01:31:32 +08:00
|
|
|
`I64Attr`. So you can actually reuse them to write it as `Or<[I32Attr.predicate,
|
|
|
|
I64Attr.predicate]>`.)
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
TODO: Build up a library of reusable primitive constraints
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
If the predicate is very complex to write with `CPred` together with predicate
|
2021-01-07 06:08:03 +08:00
|
|
|
combiners, you can also write it as a normal C++ function and use the `CPred` as
|
|
|
|
a way to "invoke" the function. For example, to verify an attribute `attr` has
|
|
|
|
some property, you can write a C++ function like
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
```cpp
|
|
|
|
bool HasSomeProperty(Attribute attr) { ... }
|
|
|
|
```
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
and then define the op as:
|
2019-01-16 00:30:49 +08:00
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
```tablegen
|
|
|
|
def HasSomeProperty : AttrConstraint<CPred<"HasSomeProperty($_self)">,
|
2019-08-22 08:45:06 +08:00
|
|
|
"has some property">;
|
2019-05-14 05:39:27 +08:00
|
|
|
|
|
|
|
def MyOp : Op<...> {
|
|
|
|
let arguments = (ins
|
|
|
|
...
|
|
|
|
HasSomeProperty:$attr
|
|
|
|
);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
As to whether we should define the predicate using a single `CPred` wrapping the
|
|
|
|
whole expression, multiple `CPred`s with predicate combiners, or a single
|
2019-05-14 05:39:27 +08:00
|
|
|
`CPred` "invoking" a function, there are no clear-cut criteria. Defining using
|
2019-09-26 02:57:13 +08:00
|
|
|
`CPred` and predicate combiners is preferable since it exposes more information
|
2019-05-14 05:39:27 +08:00
|
|
|
(instead hiding all the logic behind a C++ function) into the op definition spec
|
2021-01-07 06:08:03 +08:00
|
|
|
so that it can potentially drive more auto-generation cases. But it will require
|
|
|
|
a nice library of common predicates as the building blocks to avoid the
|
2019-05-14 05:39:27 +08:00
|
|
|
duplication, which is being worked on right now.
|
|
|
|
|
|
|
|
## Attribute Definition
|
|
|
|
|
2020-04-14 02:54:09 +08:00
|
|
|
An attribute is a compile-time known constant of an operation.
|
|
|
|
|
|
|
|
ODS provides attribute wrappers over C++ attribute classes. There are a few
|
|
|
|
common C++ [attribute classes][AttrClasses] defined in MLIR's core IR library
|
2021-01-07 06:08:03 +08:00
|
|
|
and one is free to define dialect-specific attribute classes. ODS allows one to
|
|
|
|
use these attributes in TableGen to define operations, potentially with more
|
|
|
|
fine-grained constraints. For example, `StrAttr` directly maps to `StringAttr`;
|
|
|
|
`F32Attr`/`F64Attr` requires the `FloatAttr` to additionally be of a certain
|
|
|
|
bitwidth.
|
2020-04-14 02:54:09 +08:00
|
|
|
|
|
|
|
ODS attributes are defined as having a storage type (corresponding to a backing
|
|
|
|
`mlir::Attribute` that _stores_ the attribute), a return type (corresponding to
|
2021-09-14 04:42:24 +08:00
|
|
|
the C++ _return_ type of the generated helper getters) as well as a method
|
2020-04-14 02:54:09 +08:00
|
|
|
to convert between the internal storage and the helper method.
|
|
|
|
|
|
|
|
### Attribute decorators
|
|
|
|
|
2020-08-27 02:50:14 +08:00
|
|
|
There are a few important attribute adapters/decorators/modifiers that can be
|
2020-04-14 02:54:09 +08:00
|
|
|
applied to ODS attributes to specify common additional properties like
|
|
|
|
optionality, default values, etc.:
|
|
|
|
|
|
|
|
* `DefaultValuedAttr`: specifies the
|
|
|
|
[default value](#attributes-with-default-values) for an attribute.
|
2020-04-29 13:47:35 +08:00
|
|
|
* `OptionalAttr`: specifies an attribute as [optional](#optional-attributes).
|
2020-04-14 02:54:09 +08:00
|
|
|
* `Confined`: adapts an attribute with
|
|
|
|
[further constraints](#confining-attributes).
|
|
|
|
|
2019-06-08 23:39:07 +08:00
|
|
|
### Enum attributes
|
|
|
|
|
2020-04-29 13:47:35 +08:00
|
|
|
Some attributes can only take values from a predefined enum, e.g., the
|
2019-12-06 21:58:59 +08:00
|
|
|
comparison kind of a comparison op. To define such attributes, ODS provides
|
[mlir][ods] Remove StrEnumAttr
StrEnumAttr has been deprecated in favour of EnumAttr, a solution based on AttrDef (https://reviews.llvm.org/D115181). This patch removes StrEnumAttr, along with all the custom ODS logic required to handle it.
See https://discourse.llvm.org/t/psa-stop-using-strenumattr-do-use-enumattr/5710 on how to transition to EnumAttr. In short,
```
// Before
def MyEnumAttr : StrEnumAttr<"MyEnum", "", [
StrEnumAttrCase<"A">,
StrEnumAttrCase<"B">
]>;
// After (pick an integer enum of your choice)
def MyEnum : I32EnumAttr<"MyEnum", "", [
I32EnumAttrCase<"A", 0>,
I32EnumAttrCase<"B", 1>
]> {
// Don't generate a C++ class! We want to use the AttrDef
let genSpecializedAttr = 0;
}
// Define the AttrDef
def MyEnum : EnumAttr<MyDialect, MyEnum, "my_enum">;
```
Reviewed By: rriddle, jpienaar
Differential Revision: https://reviews.llvm.org/D120834
2022-03-03 02:00:05 +08:00
|
|
|
several mechanisms: `IntEnumAttr`, and `BitEnumAttr`.
|
2019-11-02 02:17:23 +08:00
|
|
|
|
|
|
|
* `IntEnumAttr`: each enum case is an integer, the attribute is stored as a
|
|
|
|
[`IntegerAttr`][IntegerAttr] in the op.
|
2022-01-27 05:00:21 +08:00
|
|
|
* `BitEnumAttr`: each enum case is a either the empty case, a single bit,
|
|
|
|
or a group of single bits, and the attribute is stored as a
|
2019-11-02 02:17:23 +08:00
|
|
|
[`IntegerAttr`][IntegerAttr] in the op.
|
|
|
|
|
2019-11-20 21:37:49 +08:00
|
|
|
All these `*EnumAttr` attributes require fully specifying all of the allowed
|
2019-11-02 02:17:23 +08:00
|
|
|
cases via their corresponding `*EnumAttrCase`. With this, ODS is able to
|
|
|
|
generate additional verification to only accept allowed cases. To facilitate the
|
|
|
|
interaction between `*EnumAttr`s and their C++ consumers, the
|
|
|
|
[`EnumsGen`][EnumsGen] TableGen backend can generate a few common utilities: a
|
|
|
|
C++ enum class, `llvm::DenseMapInfo` for the enum class, conversion functions
|
|
|
|
from/to strings. This is controlled via the `-gen-enum-decls` and
|
|
|
|
`-gen-enum-defs` command-line options of `mlir-tblgen`.
|
2019-06-08 23:39:07 +08:00
|
|
|
|
|
|
|
For example, given the following `EnumAttr`:
|
|
|
|
|
|
|
|
```tablegen
|
2019-11-02 02:17:23 +08:00
|
|
|
def Case15: I32EnumAttrCase<"Case15", 15>;
|
|
|
|
def Case20: I32EnumAttrCase<"Case20", 20>;
|
2019-06-08 23:39:07 +08:00
|
|
|
|
2019-11-02 02:17:23 +08:00
|
|
|
def MyIntEnum: I32EnumAttr<"MyIntEnum", "An example int enum",
|
|
|
|
[Case15, Case20]> {
|
2019-06-08 23:39:07 +08:00
|
|
|
let cppNamespace = "Outer::Inner";
|
|
|
|
let stringToSymbolFnName = "ConvertToEnum";
|
|
|
|
let symbolToStringFnName = "ConvertToString";
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
The following will be generated via `mlir-tblgen -gen-enum-decls`:
|
|
|
|
|
|
|
|
```c++
|
|
|
|
namespace Outer {
|
|
|
|
namespace Inner {
|
2019-11-02 02:17:23 +08:00
|
|
|
// An example int enum
|
|
|
|
enum class MyIntEnum : uint32_t {
|
|
|
|
Case15 = 15,
|
|
|
|
Case20 = 20,
|
2019-06-08 23:39:07 +08:00
|
|
|
};
|
|
|
|
|
2019-11-02 02:17:23 +08:00
|
|
|
llvm::Optional<MyIntEnum> symbolizeMyIntEnum(uint32_t);
|
|
|
|
llvm::StringRef ConvertToString(MyIntEnum);
|
|
|
|
llvm::Optional<MyIntEnum> ConvertToEnum(llvm::StringRef);
|
|
|
|
inline constexpr unsigned getMaxEnumValForMyIntEnum() {
|
|
|
|
return 20;
|
|
|
|
}
|
|
|
|
|
2019-06-08 23:39:07 +08:00
|
|
|
} // namespace Inner
|
|
|
|
} // namespace Outer
|
|
|
|
|
|
|
|
namespace llvm {
|
2019-11-02 02:17:23 +08:00
|
|
|
template<> struct DenseMapInfo<Outer::Inner::MyIntEnum> {
|
|
|
|
using StorageInfo = llvm::DenseMapInfo<uint32_t>;
|
2019-06-08 23:39:07 +08:00
|
|
|
|
2019-11-02 02:17:23 +08:00
|
|
|
static inline Outer::Inner::MyIntEnum getEmptyKey() {
|
|
|
|
return static_cast<Outer::Inner::MyIntEnum>(StorageInfo::getEmptyKey());
|
2019-06-08 23:39:07 +08:00
|
|
|
}
|
|
|
|
|
2019-11-02 02:17:23 +08:00
|
|
|
static inline Outer::Inner::MyIntEnum getTombstoneKey() {
|
|
|
|
return static_cast<Outer::Inner::MyIntEnum>(StorageInfo::getTombstoneKey());
|
2019-06-08 23:39:07 +08:00
|
|
|
}
|
|
|
|
|
2019-11-02 02:17:23 +08:00
|
|
|
static unsigned getHashValue(const Outer::Inner::MyIntEnum &val) {
|
|
|
|
return StorageInfo::getHashValue(static_cast<uint32_t>(val));
|
2019-06-08 23:39:07 +08:00
|
|
|
}
|
|
|
|
|
2019-11-02 02:17:23 +08:00
|
|
|
static bool isEqual(const Outer::Inner::MyIntEnum &lhs, const Outer::Inner::MyIntEnum &rhs) {
|
2019-06-08 23:39:07 +08:00
|
|
|
return lhs == rhs;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
The following will be generated via `mlir-tblgen -gen-enum-defs`:
|
|
|
|
|
|
|
|
```c++
|
|
|
|
namespace Outer {
|
|
|
|
namespace Inner {
|
2019-11-02 02:17:23 +08:00
|
|
|
llvm::StringRef ConvertToString(MyIntEnum val) {
|
2019-06-08 23:39:07 +08:00
|
|
|
switch (val) {
|
2019-11-02 02:17:23 +08:00
|
|
|
case MyIntEnum::Case15: return "Case15";
|
|
|
|
case MyIntEnum::Case20: return "Case20";
|
2019-06-08 23:39:07 +08:00
|
|
|
}
|
2019-11-02 02:17:23 +08:00
|
|
|
return "";
|
2019-06-08 23:39:07 +08:00
|
|
|
}
|
|
|
|
|
2019-11-02 02:17:23 +08:00
|
|
|
llvm::Optional<MyIntEnum> ConvertToEnum(llvm::StringRef str) {
|
|
|
|
return llvm::StringSwitch<llvm::Optional<MyIntEnum>>(str)
|
|
|
|
.Case("Case15", MyIntEnum::Case15)
|
|
|
|
.Case("Case20", MyIntEnum::Case20)
|
2019-06-08 23:39:07 +08:00
|
|
|
.Default(llvm::None);
|
|
|
|
}
|
2019-11-02 02:17:23 +08:00
|
|
|
llvm::Optional<MyIntEnum> symbolizeMyIntEnum(uint32_t value) {
|
|
|
|
switch (value) {
|
|
|
|
case 15: return MyIntEnum::Case15;
|
|
|
|
case 20: return MyIntEnum::Case20;
|
|
|
|
default: return llvm::None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 23:39:07 +08:00
|
|
|
} // namespace Inner
|
|
|
|
} // namespace Outer
|
|
|
|
```
|
|
|
|
|
2019-11-02 02:17:23 +08:00
|
|
|
Similarly for the following `BitEnumAttr` definition:
|
|
|
|
|
|
|
|
```tablegen
|
2022-01-27 05:00:21 +08:00
|
|
|
def None: BitEnumAttrCaseNone<"None">;
|
|
|
|
def Bit0: BitEnumAttrCaseBit<"Bit0", 0>;
|
|
|
|
def Bit1: BitEnumAttrCaseBit<"Bit1", 1>;
|
|
|
|
def Bit2: BitEnumAttrCaseBit<"Bit2", 2>;
|
|
|
|
def Bit3: BitEnumAttrCaseBit<"Bit3", 3>;
|
2019-11-02 02:17:23 +08:00
|
|
|
|
|
|
|
def MyBitEnum: BitEnumAttr<"MyBitEnum", "An example bit enum",
|
2022-01-27 05:00:21 +08:00
|
|
|
[None, Bit0, Bit1, Bit2, Bit3]>;
|
2019-11-02 02:17:23 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
We can have:
|
|
|
|
|
|
|
|
```c++
|
|
|
|
// An example bit enum
|
|
|
|
enum class MyBitEnum : uint32_t {
|
|
|
|
None = 0,
|
2022-01-27 05:00:21 +08:00
|
|
|
Bit0 = 1,
|
|
|
|
Bit1 = 2,
|
|
|
|
Bit2 = 4,
|
|
|
|
Bit3 = 8,
|
2019-11-02 02:17:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
llvm::Optional<MyBitEnum> symbolizeMyBitEnum(uint32_t);
|
|
|
|
std::string stringifyMyBitEnum(MyBitEnum);
|
|
|
|
llvm::Optional<MyBitEnum> symbolizeMyBitEnum(llvm::StringRef);
|
|
|
|
inline MyBitEnum operator|(MyBitEnum lhs, MyBitEnum rhs) {
|
|
|
|
return static_cast<MyBitEnum>(static_cast<uint32_t>(lhs) | static_cast<uint32_t>(rhs));
|
|
|
|
}
|
|
|
|
inline MyBitEnum operator&(MyBitEnum lhs, MyBitEnum rhs) {
|
|
|
|
return static_cast<MyBitEnum>(static_cast<uint32_t>(lhs) & static_cast<uint32_t>(rhs));
|
|
|
|
}
|
|
|
|
inline bool bitEnumContains(MyBitEnum bits, MyBitEnum bit) {
|
|
|
|
return (static_cast<uint32_t>(bits) & static_cast<uint32_t>(bit)) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
template<> struct DenseMapInfo<::MyBitEnum> {
|
|
|
|
using StorageInfo = llvm::DenseMapInfo<uint32_t>;
|
|
|
|
|
|
|
|
static inline ::MyBitEnum getEmptyKey() {
|
|
|
|
return static_cast<::MyBitEnum>(StorageInfo::getEmptyKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline ::MyBitEnum getTombstoneKey() {
|
|
|
|
return static_cast<::MyBitEnum>(StorageInfo::getTombstoneKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getHashValue(const ::MyBitEnum &val) {
|
|
|
|
return StorageInfo::getHashValue(static_cast<uint32_t>(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isEqual(const ::MyBitEnum &lhs, const ::MyBitEnum &rhs) {
|
|
|
|
return lhs == rhs;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
|
|
|
```c++
|
|
|
|
std::string stringifyMyBitEnum(MyBitEnum symbol) {
|
|
|
|
auto val = static_cast<uint32_t>(symbol);
|
2022-01-27 05:00:21 +08:00
|
|
|
assert(15u == (15u | val) && "invalid bits set in bit enum");
|
2019-11-02 02:17:23 +08:00
|
|
|
// Special case for all bits unset.
|
|
|
|
if (val == 0) return "None";
|
|
|
|
llvm::SmallVector<llvm::StringRef, 2> strs;
|
2022-01-27 05:00:21 +08:00
|
|
|
if (1u == (1u & val)) { strs.push_back("Bit0"); }
|
|
|
|
if (2u == (2u & val)) { strs.push_back("Bit1"); }
|
|
|
|
if (4u == (4u & val)) { strs.push_back("Bit2"); }
|
|
|
|
if (8u == (8u & val)) { strs.push_back("Bit3"); }
|
|
|
|
|
2019-11-02 02:17:23 +08:00
|
|
|
return llvm::join(strs, "|");
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Optional<MyBitEnum> symbolizeMyBitEnum(llvm::StringRef str) {
|
|
|
|
// Special case for all bits unset.
|
|
|
|
if (str == "None") return MyBitEnum::None;
|
|
|
|
|
|
|
|
llvm::SmallVector<llvm::StringRef, 2> symbols;
|
|
|
|
str.split(symbols, "|");
|
|
|
|
|
|
|
|
uint32_t val = 0;
|
|
|
|
for (auto symbol : symbols) {
|
|
|
|
auto bit = llvm::StringSwitch<llvm::Optional<uint32_t>>(symbol)
|
2022-01-27 05:00:21 +08:00
|
|
|
.Case("Bit0", 1)
|
|
|
|
.Case("Bit1", 2)
|
|
|
|
.Case("Bit2", 4)
|
|
|
|
.Case("Bit3", 8)
|
2019-11-02 02:17:23 +08:00
|
|
|
.Default(llvm::None);
|
|
|
|
if (bit) { val |= *bit; } else { return llvm::None; }
|
|
|
|
}
|
|
|
|
return static_cast<MyBitEnum>(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Optional<MyBitEnum> symbolizeMyBitEnum(uint32_t value) {
|
|
|
|
// Special case for all bits unset.
|
|
|
|
if (value == 0) return MyBitEnum::None;
|
|
|
|
|
2022-01-27 05:00:21 +08:00
|
|
|
if (value & ~(1u | 2u | 4u | 8u)) return llvm::None;
|
2019-11-02 02:17:23 +08:00
|
|
|
return static_cast<MyBitEnum>(value);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-11-14 20:25:56 +08:00
|
|
|
## Debugging Tips
|
|
|
|
|
|
|
|
### Run `mlir-tblgen` to see the generated content
|
|
|
|
|
2021-01-07 06:08:03 +08:00
|
|
|
TableGen syntax sometimes can be obscure; reading the generated content can be a
|
|
|
|
very helpful way to understand and debug issues. To build `mlir-tblgen`, run
|
2019-11-14 20:25:56 +08:00
|
|
|
`cmake --build . --target mlir-tblgen` in your build directory and find the
|
|
|
|
`mlir-tblgen` binary in the `bin/` subdirectory. All the supported generators
|
|
|
|
can be found via `mlir-tblgen --help`. For example, `--gen-op-decls` and
|
2021-05-25 00:40:39 +08:00
|
|
|
`--gen-op-defs` as explained in [Generated C++ code](#generated-c-code).
|
2019-11-14 20:25:56 +08:00
|
|
|
|
|
|
|
To see the generated code, invoke `mlir-tblgen` with a specific generator by
|
|
|
|
providing include paths via `-I`. For example,
|
|
|
|
|
|
|
|
```sh
|
|
|
|
# To see op C++ class declaration
|
|
|
|
mlir-tblgen --gen-op-decls -I /path/to/mlir/include /path/to/input/td/file
|
|
|
|
# To see op C++ class definition
|
|
|
|
mlir-tblgen --gen-op-defs -I /path/to/mlir/include /path/to/input/td/file
|
|
|
|
# To see op documentation
|
2020-03-25 02:57:13 +08:00
|
|
|
mlir-tblgen --gen-dialect-doc -I /path/to/mlir/include /path/to/input/td/file
|
2019-11-14 20:25:56 +08:00
|
|
|
|
|
|
|
# To see op interface C++ class declaration
|
|
|
|
mlir-tblgen --gen-op-interface-decls -I /path/to/mlir/include /path/to/input/td/file
|
|
|
|
# To see op interface C++ class definition
|
|
|
|
mlir-tblgen --gen-op-interface-defs -I /path/to/mlir/include /path/to/input/td/file
|
|
|
|
# To see op interface documentation
|
|
|
|
mlir-tblgen --gen-op-interface-doc -I /path/to/mlir/include /path/to/input/td/file
|
|
|
|
```
|
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
## Appendix
|
|
|
|
|
2022-03-23 02:08:36 +08:00
|
|
|
### Reporting deprecation
|
|
|
|
|
|
|
|
Classes/defs can be marked as deprecated by using the `Deprecate` helper class,
|
|
|
|
e.g.,
|
|
|
|
|
2022-05-17 07:45:51 +08:00
|
|
|
```tablegen
|
2022-03-23 02:08:36 +08:00
|
|
|
def OpTraitA : NativeOpTrait<"OpTraitA">, Deprecated<"use `bar` instead">;
|
|
|
|
```
|
|
|
|
|
|
|
|
would result in marking `OpTraitA` as deprecated and mlir-tblgen can emit a
|
|
|
|
warning (default) or error (depending on `-on-deprecated` flag) to make
|
|
|
|
deprecated state known.
|
|
|
|
|
2019-05-14 05:39:27 +08:00
|
|
|
### Requirements and existing mechanisms analysis
|
|
|
|
|
2021-09-14 04:42:24 +08:00
|
|
|
The op description should be as declarative as possible to allow a wide range of
|
2019-05-14 05:39:27 +08:00
|
|
|
tools to work with them and query methods generated from them. In particular
|
2021-01-07 06:08:03 +08:00
|
|
|
this means specifying traits, constraints and shape inference information in a
|
|
|
|
way that is easily analyzable (e.g., avoid opaque calls to C++ functions where
|
2019-05-14 05:39:27 +08:00
|
|
|
possible).
|
|
|
|
|
|
|
|
We considered the approaches of several contemporary systems and focused on
|
|
|
|
requirements that were desirable:
|
|
|
|
|
2019-12-06 21:58:59 +08:00
|
|
|
* Ops registered using a registry separate from C++ code.
|
|
|
|
* Unknown ops are allowed in MLIR, so ops need not be registered. The
|
|
|
|
ability of the compiler to optimize those ops or graphs containing those
|
|
|
|
ops is constrained but correct.
|
|
|
|
* The current proposal does not include a runtime op description, but it
|
|
|
|
does not preclude such description, it can be added later.
|
|
|
|
* The op registry is essential for generating C++ classes that make
|
|
|
|
manipulating ops, verifying correct construction etc. in C++ easier by
|
|
|
|
providing a typed representation and accessors.
|
|
|
|
* The op registry will be defined in
|
|
|
|
[TableGen](https://llvm.org/docs/TableGen/index.html) and be used to
|
|
|
|
generate C++ classes and utility functions
|
|
|
|
(builder/verifier/parser/printer).
|
|
|
|
* TableGen is a modelling specification language used by LLVM's backends
|
|
|
|
and fits in well with trait-based modelling. This is an implementation
|
|
|
|
decision and there are alternative ways of doing this. But the
|
|
|
|
specification language is good for the requirements of modelling the
|
|
|
|
traits (as seen from usage in LLVM processor backend modelling) and easy
|
|
|
|
to extend, so a practical choice. If another good option comes up, we
|
|
|
|
will consider it.
|
|
|
|
* MLIR allows both defined and undefined ops.
|
|
|
|
* Defined ops should have fixed semantics and could have a corresponding
|
2021-05-21 18:27:56 +08:00
|
|
|
reference implementation defined.
|
2019-12-06 21:58:59 +08:00
|
|
|
* Dialects are under full control of the dialect owner and normally live
|
|
|
|
with the framework of the dialect.
|
|
|
|
* The op's traits (e.g., commutative) are modelled along with the op in the
|
|
|
|
registry.
|
|
|
|
* The op's operand/return type constraints are modelled along with the op in
|
2020-01-09 10:48:38 +08:00
|
|
|
the registry (see [Shape inference](ShapeInference.md) discussion below),
|
2019-12-06 21:58:59 +08:00
|
|
|
this allows (e.g.) optimized concise syntax in textual dumps.
|
|
|
|
* Behavior of the op is documented along with the op with a summary and a
|
|
|
|
description. The description is written in markdown and extracted for
|
|
|
|
inclusion in the generated LangRef section of the dialect.
|
|
|
|
* The generic assembly form of printing and parsing is available as normal,
|
|
|
|
but a custom parser and printer can either be specified or automatically
|
|
|
|
generated from an optional string representation showing the mapping of the
|
|
|
|
"assembly" string to operands/type.
|
|
|
|
* Parser-level remappings (e.g., `eq` to enum) will be supported as part
|
|
|
|
of the parser generation.
|
|
|
|
* Matching patterns are specified separately from the op description.
|
|
|
|
* Contrasted with LLVM there is no "base" set of ops that every backend
|
|
|
|
needs to be aware of. Instead there are many different dialects and the
|
|
|
|
transformations/legalizations between these dialects form a graph of
|
|
|
|
transformations.
|
|
|
|
* Reference implementation may be provided along with the op definition.
|
|
|
|
|
|
|
|
* The reference implementation may be in terms of either standard ops or
|
|
|
|
other reference implementations.
|
2019-05-14 05:39:27 +08:00
|
|
|
|
|
|
|
TODO: document expectation if the dependent op's definition changes.
|
|
|
|
|
|
|
|
[TableGen]: https://llvm.org/docs/TableGen/index.html
|
2020-09-22 01:56:06 +08:00
|
|
|
[TableGenProgRef]: https://llvm.org/docs/TableGen/ProgRef.html
|
2019-05-14 05:39:27 +08:00
|
|
|
[TableGenBackend]: https://llvm.org/docs/TableGen/BackEnds.html#introduction
|
2021-02-01 15:24:21 +08:00
|
|
|
[OpBase]: https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/IR/OpBase.td
|
|
|
|
[OpDefinitionsGen]: https://github.com/llvm/llvm-project/blob/main/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
|
|
|
|
[EnumsGen]: https://github.com/llvm/llvm-project/blob/main/mlir/tools/mlir-tblgen/EnumsGen.cpp
|
2021-05-25 00:40:39 +08:00
|
|
|
[StringAttr]: Dialects/Builtin.md/#stringattr
|
|
|
|
[IntegerAttr]: Dialects/Builtin.md/#integertype
|
2021-02-01 15:24:21 +08:00
|
|
|
[AttrClasses]: https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/IR/Attributes.h
|