River Riddle | 01c857b | 2020-03-30 19:25:00 | [diff] [blame] | 1 | # 'affine' Dialect |
River Riddle | 6f7470a | 2019-02-06 00:29:25 | [diff] [blame] | 2 | |
| 3 | This dialect provides a powerful abstraction for affine operations and analyses. |
| 4 | |
| 5 | [TOC] |
| 6 | |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 7 | ## Polyhedral Structures |
| 8 | |
| 9 | MLIR uses techniques from polyhedral compilation to make dependence analysis and |
| 10 | loop transformations efficient and reliable. This section introduces some of the |
| 11 | core concepts that are used throughout the document. |
| 12 | |
| 13 | ### Dimensions and Symbols |
| 14 | |
| 15 | Dimensions and symbols are the two kinds of identifiers that can appear in the |
Mogball | a54f4ea | 2021-10-12 23:14:57 | [diff] [blame] | 16 | polyhedral structures, and are always of [`index`](Builtin.md/#indextype) type. |
| 17 | Dimensions are declared in parentheses and symbols are declared in square |
MLIR Team | 5a91b98 | 2019-05-29 21:56:41 | [diff] [blame] | 18 | brackets. |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 19 | |
| 20 | Examples: |
| 21 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 22 | ```mlir |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 23 | // A 2d to 3d affine mapping. |
| 24 | // d0/d1 are dimensions, s0 is a symbol |
River Riddle | 4268e4f | 2020-01-13 21:12:37 | [diff] [blame] | 25 | #affine_map2to3 = affine_map<(d0, d1)[s0] -> (d0, d1 + s0, d1 - s0)> |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 26 | ``` |
| 27 | |
| 28 | Dimensional identifiers correspond to the dimensions of the underlying structure |
| 29 | being represented (a map, set, or more concretely a loop nest or a tensor); for |
| 30 | example, a three-dimensional loop nest has three dimensional identifiers. Symbol |
| 31 | identifiers represent an unknown quantity that can be treated as constant for a |
| 32 | region of interest. |
| 33 | |
| 34 | Dimensions and symbols are bound to SSA values by various operations in MLIR and |
| 35 | use the same parenthesized vs square bracket list to distinguish the two. |
| 36 | |
| 37 | Syntax: |
| 38 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 39 | ``` |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 40 | // Uses of SSA values that are passed to dimensional identifiers. |
| 41 | dim-use-list ::= `(` ssa-use-list? `)` |
| 42 | |
| 43 | // Uses of SSA values that are used to bind symbols. |
| 44 | symbol-use-list ::= `[` ssa-use-list? `]` |
| 45 | |
| 46 | // Most things that bind SSA values bind dimensions and symbols. |
| 47 | dim-and-symbol-use-list ::= dim-use-list symbol-use-list? |
| 48 | ``` |
| 49 | |
| 50 | SSA values bound to dimensions and symbols must always have 'index' type. |
| 51 | |
| 52 | Example: |
| 53 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 54 | ```mlir |
River Riddle | 4268e4f | 2020-01-13 21:12:37 | [diff] [blame] | 55 | #affine_map2to3 = affine_map<(d0, d1)[s0] -> (d0, d1 + s0, d1 - s0)> |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 56 | // Binds %N to the s0 symbol in affine_map2to3. |
Mogball | a54f4ea | 2021-10-12 23:14:57 | [diff] [blame] | 57 | %x = memref.alloc()[%N] : memref<40x50xf32, #affine_map2to3> |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 58 | ``` |
| 59 | |
| 60 | ### Restrictions on Dimensions and Symbols |
River Riddle | 6f7470a | 2019-02-06 00:29:25 | [diff] [blame] | 61 | |
| 62 | The affine dialect imposes certain restrictions on dimension and symbolic |
Uday Bondhugula | 4803453 | 2020-04-29 00:08:23 | [diff] [blame] | 63 | identifiers to enable powerful analysis and transformation. An SSA value's use |
Mogball | a54f4ea | 2021-10-12 23:14:57 | [diff] [blame] | 64 | can be bound to a symbolic identifier if that SSA value is either 1. a region |
| 65 | argument for an op with trait `AffineScope` (eg. `FuncOp`), 2. a value defined |
| 66 | at the top level of an `AffineScope` op (i.e., immediately enclosed by the |
| 67 | latter), 3. a value that dominates the `AffineScope` op enclosing the value's |
| 68 | use, 4. the result of a |
River Riddle | 23aa5a7 | 2022-02-26 22:49:54 | [diff] [blame^] | 69 | constant operation, 5. the result of an |
Mogball | a54f4ea | 2021-10-12 23:14:57 | [diff] [blame] | 70 | [`affine.apply` operation](#affineapply-affineapplyop) that recursively takes as |
| 71 | arguments any valid symbolic identifiers, or 6. the result of a |
| 72 | [`dim` operation](MemRef.md/#memrefdim-mlirmemrefdimop) on either a memref that |
| 73 | is an argument to a `AffineScope` op or a memref where the corresponding |
| 74 | dimension is either static or a dynamic one in turn bound to a valid symbol. |
Alex Zinenko | 36150c3 | 2020-06-15 09:40:56 | [diff] [blame] | 75 | *Note:* if the use of an SSA value is not contained in any op with the |
| 76 | `AffineScope` trait, only the rules 4-6 can be applied. |
Uday Bondhugula | 4803453 | 2020-04-29 00:08:23 | [diff] [blame] | 77 | |
| 78 | Note that as a result of rule (3) above, symbol validity is sensitive to the |
Mogball | a54f4ea | 2021-10-12 23:14:57 | [diff] [blame] | 79 | location of the SSA use. Dimensions may be bound not only to anything that a |
Uday Bondhugula | 4803453 | 2020-04-29 00:08:23 | [diff] [blame] | 80 | symbol is bound to, but also to induction variables of enclosing |
Markus Böck | 9b99336 | 2021-05-25 12:50:59 | [diff] [blame] | 81 | [`affine.for`](#affinefor-affineforop) and |
Mogball | a54f4ea | 2021-10-12 23:14:57 | [diff] [blame] | 82 | [`affine.parallel`](#affineparallel-affineparallelop) operations, and the result |
| 83 | of an [`affine.apply` operation](#affineapply-affineapplyop) (which recursively |
| 84 | may use other dimensions and symbols). |
River Riddle | 6f7470a | 2019-02-06 00:29:25 | [diff] [blame] | 85 | |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 86 | ### Affine Expressions |
| 87 | |
| 88 | Syntax: |
| 89 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 90 | ``` |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 91 | affine-expr ::= `(` affine-expr `)` |
| 92 | | affine-expr `+` affine-expr |
| 93 | | affine-expr `-` affine-expr |
| 94 | | `-`? integer-literal `*` affine-expr |
| 95 | | affine-expr `ceildiv` integer-literal |
| 96 | | affine-expr `floordiv` integer-literal |
| 97 | | affine-expr `mod` integer-literal |
| 98 | | `-`affine-expr |
| 99 | | bare-id |
| 100 | | `-`? integer-literal |
| 101 | |
Nicolas Vasilache | 47ec870 | 2020-03-10 19:10:34 | [diff] [blame] | 102 | multi-dim-affine-expr ::= `(` `)` |
| 103 | | `(` affine-expr (`,` affine-expr)* `)` |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 104 | ``` |
| 105 | |
| 106 | `ceildiv` is the ceiling function which maps the result of the division of its |
| 107 | first argument by its second argument to the smallest integer greater than or |
| 108 | equal to that result. `floordiv` is a function which maps the result of the |
| 109 | division of its first argument by its second argument to the largest integer |
| 110 | less than or equal to that result. `mod` is the modulo operation: since its |
| 111 | second argument is always positive, its results are always positive in our |
| 112 | usage. The `integer-literal` operand for ceildiv, floordiv, and mod is always |
| 113 | expected to be positive. `bare-id` is an identifier which must have type |
Markus Böck | 9b99336 | 2021-05-25 12:50:59 | [diff] [blame] | 114 | [index](Builtin.md/#indextype). The precedence of operations in an affine |
MLIR Team | 5a91b98 | 2019-05-29 21:56:41 | [diff] [blame] | 115 | expression are ordered from highest to lowest in the order: (1) |
| 116 | parenthesization, (2) negation, (3) modulo, multiplication, floordiv, and |
| 117 | ceildiv, and (4) addition and subtraction. All of these operators associate from |
| 118 | left to right. |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 119 | |
Mogball | a54f4ea | 2021-10-12 23:14:57 | [diff] [blame] | 120 | A *multidimensional affine expression* is a comma separated list of |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 121 | one-dimensional affine expressions, with the entire list enclosed in |
| 122 | parentheses. |
| 123 | |
| 124 | **Context:** An affine function, informally, is a linear function plus a |
lewuathe | 4ae7952 | 2021-01-06 01:59:45 | [diff] [blame] | 125 | constant. More formally, a function f defined on a vector $\vec{v} \in |
Mogball | a54f4ea | 2021-10-12 23:14:57 | [diff] [blame] | 126 | \mathbb{Z}^n$ is a multidimensional affine function of $\vec{v}$ if $f(\vec{v})$ |
| 127 | can be expressed in the form $M \vec{v} + \vec{c}$ where $M$ is a constant |
| 128 | matrix from $\mathbb{Z}^{m \times n}$ and $\vec{c}$ is a constant vector from |
| 129 | $\mathbb{Z}$. $m$ is the dimensionality of such an affine function. MLIR further |
| 130 | extends the definition of an affine function to allow 'floordiv', 'ceildiv', and |
| 131 | 'mod' with respect to positive integer constants. Such extensions to affine |
| 132 | functions have often been referred to as quasi-affine functions by the |
| 133 | polyhedral compiler community. MLIR uses the term 'affine map' to refer to these |
| 134 | multidimensional quasi-affine functions. As examples, $(i+j+1, j)$, $(i \mod 2, |
| 135 | j+i)$, $(j, i/4, i \mod 4)$, $(2i+1, j)$ are two-dimensional affine functions of |
| 136 | $(i, j)$, but $(i \cdot j, i^2)$, $(i \mod j, i/j)$ are not affine functions of |
| 137 | $(i, j)$. |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 138 | |
| 139 | ### Affine Maps |
| 140 | |
| 141 | Syntax: |
| 142 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 143 | ``` |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 144 | affine-map-inline |
| 145 | ::= dim-and-symbol-id-lists `->` multi-dim-affine-expr |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 146 | ``` |
| 147 | |
| 148 | The identifiers in the dimensions and symbols lists must be unique. These are |
MLIR Team | 5a91b98 | 2019-05-29 21:56:41 | [diff] [blame] | 149 | the only identifiers that may appear in 'multi-dim-affine-expr'. Affine maps |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 150 | with one or more symbols in its specification are known as "symbolic affine |
MLIR Team | 5a91b98 | 2019-05-29 21:56:41 | [diff] [blame] | 151 | maps", and those with no symbols as "non-symbolic affine maps". |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 152 | |
| 153 | **Context:** Affine maps are mathematical functions that transform a list of |
| 154 | dimension indices and symbols into a list of results, with affine expressions |
| 155 | combining the indices and symbols. Affine maps distinguish between |
| 156 | [indices and symbols](#dimensions-and-symbols) because indices are inputs to the |
reinerp | 02b3ea6 | 2019-10-18 17:28:02 | [diff] [blame] | 157 | affine map when the map is called (through an operation such as |
Mogball | a54f4ea | 2021-10-12 23:14:57 | [diff] [blame] | 158 | [affine.apply](#affineapply-affineapplyop)), whereas symbols are bound when the |
| 159 | map is established (e.g. when a memref is formed, establishing a memory |
| 160 | [layout map](Builtin.md/#layout-map)). |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 161 | |
| 162 | Affine maps are used for various core structures in MLIR. The restrictions we |
| 163 | impose on their form allows powerful analysis and transformation, while keeping |
| 164 | the representation closed with respect to several operations of interest. |
| 165 | |
| 166 | #### Named affine mappings |
| 167 | |
| 168 | Syntax: |
| 169 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 170 | ``` |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 171 | affine-map-id ::= `#` suffix-id |
| 172 | |
| 173 | // Definitions of affine maps are at the top of the file. |
| 174 | affine-map-def ::= affine-map-id `=` affine-map-inline |
| 175 | module-header-def ::= affine-map-def |
| 176 | |
| 177 | // Uses of affine maps may use the inline form or the named form. |
| 178 | affine-map ::= affine-map-id | affine-map-inline |
| 179 | ``` |
| 180 | |
| 181 | Affine mappings may be defined inline at the point of use, or may be hoisted to |
| 182 | the top of the file and given a name with an affine map definition, and used by |
| 183 | name. |
| 184 | |
| 185 | Examples: |
| 186 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 187 | ```mlir |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 188 | // Affine map out-of-line definition and usage example. |
River Riddle | 4268e4f | 2020-01-13 21:12:37 | [diff] [blame] | 189 | #affine_map42 = affine_map<(d0, d1)[s0] -> (d0, d0 + d1 + s0 floordiv 2)> |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 190 | |
| 191 | // Use an affine mapping definition in an alloc operation, binding the |
| 192 | // SSA value %N to the symbol s0. |
Mogball | a54f4ea | 2021-10-12 23:14:57 | [diff] [blame] | 193 | %a = memref.alloc()[%N] : memref<4x4xf32, #affine_map42> |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 194 | |
| 195 | // Same thing with an inline affine mapping definition. |
Mogball | a54f4ea | 2021-10-12 23:14:57 | [diff] [blame] | 196 | %b = memref.alloc()[%N] : memref<4x4xf32, affine_map<(d0, d1)[s0] -> (d0, d0 + d1 + s0 floordiv 2)>> |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 197 | ``` |
| 198 | |
| 199 | ### Semi-affine maps |
| 200 | |
| 201 | Semi-affine maps are extensions of affine maps to allow multiplication, |
| 202 | `floordiv`, `ceildiv`, and `mod` with respect to symbolic identifiers. |
| 203 | Semi-affine maps are thus a strict superset of affine maps. |
| 204 | |
| 205 | Syntax of semi-affine expressions: |
| 206 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 207 | ``` |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 208 | semi-affine-expr ::= `(` semi-affine-expr `)` |
| 209 | | semi-affine-expr `+` semi-affine-expr |
| 210 | | semi-affine-expr `-` semi-affine-expr |
| 211 | | symbol-or-const `*` semi-affine-expr |
MLIR Team | 3191f9c | 2019-08-16 18:00:31 | [diff] [blame] | 212 | | semi-affine-expr `ceildiv` symbol-or-const |
| 213 | | semi-affine-expr `floordiv` symbol-or-const |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 214 | | semi-affine-expr `mod` symbol-or-const |
| 215 | | bare-id |
| 216 | | `-`? integer-literal |
| 217 | |
| 218 | symbol-or-const ::= `-`? integer-literal | symbol-id |
| 219 | |
| 220 | multi-dim-semi-affine-expr ::= `(` semi-affine-expr (`,` semi-affine-expr)* `)` |
| 221 | ``` |
| 222 | |
| 223 | The precedence and associativity of operations in the syntax above is the same |
| 224 | as that for [affine expressions](#affine-expressions). |
| 225 | |
| 226 | Syntax of semi-affine maps: |
| 227 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 228 | ``` |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 229 | semi-affine-map-inline |
| 230 | ::= dim-and-symbol-id-lists `->` multi-dim-semi-affine-expr |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 231 | ``` |
| 232 | |
| 233 | Semi-affine maps may be defined inline at the point of use, or may be hoisted to |
| 234 | the top of the file and given a name with a semi-affine map definition, and used |
| 235 | by name. |
| 236 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 237 | ``` |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 238 | semi-affine-map-id ::= `#` suffix-id |
| 239 | |
| 240 | // Definitions of semi-affine maps are at the top of file. |
| 241 | semi-affine-map-def ::= semi-affine-map-id `=` semi-affine-map-inline |
| 242 | module-header-def ::= semi-affine-map-def |
| 243 | |
| 244 | // Uses of semi-affine maps may use the inline form or the named form. |
| 245 | semi-affine-map ::= semi-affine-map-id | semi-affine-map-inline |
| 246 | ``` |
| 247 | |
| 248 | ### Integer Sets |
| 249 | |
| 250 | An integer set is a conjunction of affine constraints on a list of identifiers. |
| 251 | The identifiers associated with the integer set are separated out into two |
| 252 | classes: the set's dimension identifiers, and the set's symbolic identifiers. |
| 253 | The set is viewed as being parametric on its symbolic identifiers. In the |
| 254 | syntax, the list of set's dimension identifiers are enclosed in parentheses |
| 255 | while its symbols are enclosed in square brackets. |
| 256 | |
| 257 | Syntax of affine constraints: |
| 258 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 259 | ``` |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 260 | affine-constraint ::= affine-expr `>=` `0` |
| 261 | | affine-expr `==` `0` |
| 262 | affine-constraint-conjunction ::= affine-constraint (`,` affine-constraint)* |
| 263 | ``` |
| 264 | |
| 265 | Integer sets may be defined inline at the point of use, or may be hoisted to the |
| 266 | top of the file and given a name with an integer set definition, and used by |
| 267 | name. |
| 268 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 269 | ``` |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 270 | integer-set-id ::= `#` suffix-id |
| 271 | |
| 272 | integer-set-inline |
| 273 | ::= dim-and-symbol-id-lists `:` '(' affine-constraint-conjunction? ')' |
| 274 | |
| 275 | // Declarations of integer sets are at the top of the file. |
| 276 | integer-set-decl ::= integer-set-id `=` integer-set-inline |
| 277 | |
| 278 | // Uses of integer sets may use the inline form or the named form. |
| 279 | integer-set ::= integer-set-id | integer-set-inline |
| 280 | ``` |
| 281 | |
| 282 | The dimensionality of an integer set is the number of identifiers appearing in |
| 283 | dimension list of the set. The affine-constraint non-terminals appearing in the |
| 284 | syntax above are only allowed to contain identifiers from dims and symbols. A |
| 285 | set with no constraints is a set that is unbounded along all of the set's |
| 286 | dimensions. |
| 287 | |
| 288 | Example: |
| 289 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 290 | ```mlir |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 291 | // A example two-dimensional integer set with two symbols. |
River Riddle | 4268e4f | 2020-01-13 21:12:37 | [diff] [blame] | 292 | #set42 = affine_set<(d0, d1)[s0, s1] |
| 293 | : (d0 >= 0, -d0 + s0 - 1 >= 0, d1 >= 0, -d1 + s1 - 1 >= 0)> |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 294 | |
River Riddle | e7d594b | 2019-07-03 20:21:24 | [diff] [blame] | 295 | // Inside a Region |
Andy Davis | 59100a0 | 2019-05-13 18:05:59 | [diff] [blame] | 296 | affine.if #set42(%i, %j)[%M, %N] { |
| 297 | ... |
| 298 | } |
| 299 | ``` |
| 300 | |
| 301 | `d0` and `d1` correspond to dimensional identifiers of the set, while `s0` and |
| 302 | `s1` are symbol identifiers. |
| 303 | |
River Riddle | 465ef55 | 2019-04-05 15:19:42 | [diff] [blame] | 304 | ## Operations |
River Riddle | 6f7470a | 2019-02-06 00:29:25 | [diff] [blame] | 305 | |
River Riddle | 16f27b7 | 2020-03-30 05:00:26 | [diff] [blame] | 306 | [include "Dialects/AffineOps.md"] |
River Riddle | 6f7470a | 2019-02-06 00:29:25 | [diff] [blame] | 307 | |
River Riddle | 16f27b7 | 2020-03-30 05:00:26 | [diff] [blame] | 308 | ### 'affine.load' operation |
Andy Davis | 1efc511 | 2019-11-06 22:31:02 | [diff] [blame] | 309 | |
| 310 | Syntax: |
| 311 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 312 | ``` |
Andy Davis | 1efc511 | 2019-11-06 22:31:02 | [diff] [blame] | 313 | operation ::= ssa-id `=` `affine.load` ssa-use `[` multi-dim-affine-map-of-ssa-ids `]` `:` memref-type |
| 314 | ``` |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 315 | |
| 316 | The `affine.load` op reads an element from a memref, where the index for each |
| 317 | memref dimension is an affine expression of loop induction variables and |
| 318 | symbols. The output of 'affine.load' is a new value with the same type as the |
| 319 | elements of the memref. An affine expression of loop IVs and symbols must be |
| 320 | specified for each dimension of the memref. The keyword 'symbol' can be used to |
| 321 | indicate SSA identifiers which are symbolic. |
Andy Davis | 1efc511 | 2019-11-06 22:31:02 | [diff] [blame] | 322 | |
| 323 | Example: |
| 324 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 325 | ```mlir |
Andy Davis | 1efc511 | 2019-11-06 22:31:02 | [diff] [blame] | 326 | |
| 327 | Example 1: |
| 328 | |
| 329 | %1 = affine.load %0[%i0 + 3, %i1 + 7] : memref<100x100xf32> |
| 330 | |
| 331 | Example 2: Uses 'symbol' keyword for symbols '%n' and '%m'. |
| 332 | |
| 333 | %1 = affine.load %0[%i0 + symbol(%n), %i1 + symbol(%m)] |
| 334 | : memref<100x100xf32> |
| 335 | |
| 336 | ``` |
| 337 | |
River Riddle | 16f27b7 | 2020-03-30 05:00:26 | [diff] [blame] | 338 | ### 'affine.store' operation |
Andy Davis | 1efc511 | 2019-11-06 22:31:02 | [diff] [blame] | 339 | |
| 340 | Syntax: |
| 341 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 342 | ``` |
Andy Davis | 1efc511 | 2019-11-06 22:31:02 | [diff] [blame] | 343 | operation ::= ssa-id `=` `affine.store` ssa-use, ssa-use `[` multi-dim-affine-map-of-ssa-ids `]` `:` memref-type |
| 344 | ``` |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 345 | |
| 346 | The `affine.store` op writes an element to a memref, where the index for each |
| 347 | memref dimension is an affine expression of loop induction variables and |
| 348 | symbols. The 'affine.store' op stores a new value which is the same type as the |
| 349 | elements of the memref. An affine expression of loop IVs and symbols must be |
| 350 | specified for each dimension of the memref. The keyword 'symbol' can be used to |
| 351 | indicate SSA identifiers which are symbolic. |
Andy Davis | 1efc511 | 2019-11-06 22:31:02 | [diff] [blame] | 352 | |
| 353 | Example: |
| 354 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 355 | ```mlir |
Andy Davis | 1efc511 | 2019-11-06 22:31:02 | [diff] [blame] | 356 | |
| 357 | Example 1: |
| 358 | |
| 359 | affine.store %v0, %0[%i0 + 3, %i1 + 7] : memref<100x100xf32> |
| 360 | |
| 361 | Example 2: Uses 'symbol' keyword for symbols '%n' and '%m'. |
| 362 | |
| 363 | affine.store %v0, %0[%i0 + symbol(%n), %i1 + symbol(%m)] |
| 364 | : memref<100x100xf32> |
| 365 | |
| 366 | ``` |
| 367 | |
River Riddle | 16f27b7 | 2020-03-30 05:00:26 | [diff] [blame] | 368 | ### 'affine.dma_start' operation |
Andy Davis | 1efc511 | 2019-11-06 22:31:02 | [diff] [blame] | 369 | |
| 370 | Syntax: |
| 371 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 372 | ``` |
Andy Davis | 1efc511 | 2019-11-06 22:31:02 | [diff] [blame] | 373 | operation ::= `affine.dma_Start` ssa-use `[` multi-dim-affine-map-of-ssa-ids `]`, `[` multi-dim-affine-map-of-ssa-ids `]`, `[` multi-dim-affine-map-of-ssa-ids `]`, ssa-use `:` memref-type |
| 374 | ``` |
| 375 | |
| 376 | The `affine.dma_start` op starts a non-blocking DMA operation that transfers |
| 377 | data from a source memref to a destination memref. The source and destination |
| 378 | memref need not be of the same dimensionality, but need to have the same |
Mogball | a54f4ea | 2021-10-12 23:14:57 | [diff] [blame] | 379 | elemental type. The operands include the source and destination memref's each |
| 380 | followed by its indices, size of the data transfer in terms of the number of |
| 381 | elements (of the elemental type of the memref), a tag memref with its indices, |
| 382 | and optionally at the end, a stride and a number_of_elements_per_stride |
| 383 | arguments. The tag location is used by an AffineDmaWaitOp to check for |
| 384 | completion. The indices of the source memref, destination memref, and the tag |
| 385 | memref have the same restrictions as any affine.load/store. In particular, index |
| 386 | for each memref dimension must be an affine expression of loop induction |
| 387 | variables and symbols. The optional stride arguments should be of 'index' type, |
| 388 | and specify a stride for the slower memory space (memory space with a lower |
| 389 | memory space id), transferring chunks of number_of_elements_per_stride every |
| 390 | stride until %num_elements are transferred. Either both or no stride arguments |
| 391 | should be specified. The value of 'num_elements' must be a multiple of |
Andy Davis | 1efc511 | 2019-11-06 22:31:02 | [diff] [blame] | 392 | 'number_of_elements_per_stride'. |
| 393 | |
Andy Davis | 1efc511 | 2019-11-06 22:31:02 | [diff] [blame] | 394 | Example: |
| 395 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 396 | ```mlir |
Andy Davis | 1efc511 | 2019-11-06 22:31:02 | [diff] [blame] | 397 | For example, a DmaStartOp operation that transfers 256 elements of a memref |
| 398 | '%src' in memory space 0 at indices [%i + 3, %j] to memref '%dst' in memory |
| 399 | space 1 at indices [%k + 7, %l], would be specified as follows: |
| 400 | |
Mogball | cb3aa49 | 2021-10-14 16:55:33 | [diff] [blame] | 401 | %num_elements = arith.constant 256 |
Mogball | a54f4ea | 2021-10-12 23:14:57 | [diff] [blame] | 402 | %idx = arith.constant 0 : index |
| 403 | %tag = memref.alloc() : memref<1xi32, 4> |
Andy Davis | 1efc511 | 2019-11-06 22:31:02 | [diff] [blame] | 404 | affine.dma_start %src[%i + 3, %j], %dst[%k + 7, %l], %tag[%idx], |
| 405 | %num_elements : |
| 406 | memref<40x128xf32, 0>, memref<2x1024xf32, 1>, memref<1xi32, 2> |
| 407 | |
| 408 | If %stride and %num_elt_per_stride are specified, the DMA is expected to |
| 409 | transfer %num_elt_per_stride elements every %stride elements apart from |
| 410 | memory space 0 until %num_elements are transferred. |
| 411 | |
| 412 | affine.dma_start %src[%i, %j], %dst[%k, %l], %tag[%idx], %num_elements, |
| 413 | %stride, %num_elt_per_stride : ... |
Andy Davis | 1efc511 | 2019-11-06 22:31:02 | [diff] [blame] | 414 | ``` |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 415 | |
River Riddle | 16f27b7 | 2020-03-30 05:00:26 | [diff] [blame] | 416 | ### 'affine.dma_wait' operation |
Andy Davis | 1efc511 | 2019-11-06 22:31:02 | [diff] [blame] | 417 | |
| 418 | Syntax: |
| 419 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 420 | ``` |
Andy Davis | 1efc511 | 2019-11-06 22:31:02 | [diff] [blame] | 421 | operation ::= `affine.dma_Start` ssa-use `[` multi-dim-affine-map-of-ssa-ids `]`, `[` multi-dim-affine-map-of-ssa-ids `]`, `[` multi-dim-affine-map-of-ssa-ids `]`, ssa-use `:` memref-type |
| 422 | ``` |
| 423 | |
| 424 | The `affine.dma_start` op blocks until the completion of a DMA operation |
Mogball | a54f4ea | 2021-10-12 23:14:57 | [diff] [blame] | 425 | associated with the tag element '%tag[%index]'. %tag is a memref, and %index has |
| 426 | to be an index with the same restrictions as any load/store index. In |
| 427 | particular, index for each memref dimension must be an affine expression of loop |
| 428 | induction variables and symbols. %num_elements is the number of elements |
Andy Davis | 1efc511 | 2019-11-06 22:31:02 | [diff] [blame] | 429 | associated with the DMA operation. For example: |
| 430 | |
| 431 | Example: |
| 432 | |
Alex Zinenko | ac48733 | 2019-12-10 11:00:29 | [diff] [blame] | 433 | ```mlir |
River Riddle | 16f27b7 | 2020-03-30 05:00:26 | [diff] [blame] | 434 | affine.dma_start %src[%i, %j], %dst[%k, %l], %tag[%index], %num_elements : |
| 435 | memref<2048xf32, 0>, memref<256xf32, 1>, memref<1xi32, 2> |
| 436 | ... |
| 437 | ... |
| 438 | affine.dma_wait %tag[%index], %num_elements : memref<1xi32, 2> |
Andy Davis | 1efc511 | 2019-11-06 22:31:02 | [diff] [blame] | 439 | ``` |