@@ -61,45 +61,52 @@ def ForOp : Loop_Op<"for",
61
61
}
62
62
```
63
63
64
- "loop.for" can also operate on loop-carried variables and returns the final values
65
- after loop termination. The initial values of the variables are passed as additional SSA
66
- operands to the "loop.for" following the 3 loop control SSA values mentioned above
67
- (lower bound, upper bound and step). The operation region has equivalent arguments
68
- for each variable representing the value of the variable at the current iteration.
69
-
70
- The region must terminate with a "loop.yield" that passes all the current iteration
71
- variables to the next iteration, or to the "loop.for" result, if at the last iteration.
72
- "loop.for" results hold the final values after the last iteration.
64
+ "loop.for" can also operate on loop-carried variables and returns the final
65
+ values after loop termination. The initial values of the variables are
66
+ passed as additional SSA operands to the "loop.for" following the 3 loop
67
+ control SSA values mentioned above (lower bound, upper bound and step). The
68
+ operation region has equivalent arguments for each variable representing
69
+ the value of the variable at the current iteration.
70
+
71
+ The region must terminate with a "loop.yield" that passes all the current
72
+ iteration variables to the next iteration, or to the "loop.for" result, if
73
+ at the last iteration. "loop.for" results hold the final values after the
74
+ last iteration.
73
75
74
76
For example, to sum-reduce a memref:
75
77
76
78
```mlir
77
- func @reduce(%buffer: memref<1024xf32>, %lb: index, %ub: index, %step: index) -> (f32) {
79
+ func @reduce(%buffer: memref<1024xf32>, %lb: index,
80
+ %ub: index, %step: index) -> (f32) {
78
81
// Initial sum set to 0.
79
82
%sum_0 = constant 0.0 : f32
80
83
// iter_args binds initial values to the loop's region arguments.
81
- %sum = loop.for %iv = %lb to %ub step %step iter_args(%sum_iter = %sum_0) -> (f32) {
84
+ %sum = loop.for %iv = %lb to %ub step %step
85
+ iter_args(%sum_iter = %sum_0) -> (f32) {
82
86
%t = load %buffer[%iv] : memref<1024xf32>
83
87
%sum_next = addf %sum_iter, %t : f32
84
- // Yield current iteration sum to next iteration %sum_iter or to %sum if final iteration.
88
+ // Yield current iteration sum to next iteration %sum_iter or to %sum
89
+ // if final iteration.
85
90
loop.yield %sum_next : f32
86
91
}
87
92
return %sum : f32
88
93
}
89
94
```
90
95
91
96
If the "loop.for" defines any values, a yield must be explicitly present.
92
- The number and types of the "loop.for" results must match the initial values
93
- in the "iter_args" binding and the yield operands.
97
+ The number and types of the "loop.for" results must match the initial
98
+ values in the "iter_args" binding and the yield operands.
94
99
95
100
Another example with a nested "loop.if" (see "loop.if" for details)
96
101
to perform conditional reduction:
97
102
98
103
```mlir
99
- func @conditional_reduce(%buffer: memref<1024xf32>, %lb: index, %ub: index, %step: index) -> (f32) {
104
+ func @conditional_reduce(%buffer: memref<1024xf32>, %lb: index,
105
+ %ub: index, %step: index) -> (f32) {
100
106
%sum_0 = constant 0.0 : f32
101
107
%c0 = constant 0.0 : f32
102
- %sum = loop.for %iv = %lb to %ub step %step iter_args(%sum_iter = %sum_0) -> (f32) {
108
+ %sum = loop.for %iv = %lb to %ub step %step
109
+ iter_args(%sum_iter = %sum_0) -> (f32) {
103
110
%t = load %buffer[%iv] : memref<1024xf32>
104
111
%cond = cmpf "ugt", %t, %c0 : f32
105
112
%sum_next = loop.if %cond -> (f32) {
@@ -177,8 +184,8 @@ def IfOp : Loop_Op<"if",
177
184
}
178
185
```
179
186
180
- "loop.if" may also return results that are defined in its regions. The values
181
- defined are determined by which execution path is taken.
187
+ "loop.if" may also return results that are defined in its regions. The
188
+ values defined are determined by which execution path is taken.
182
189
For example:
183
190
```mlir
184
191
%x, %y = loop.if %b -> (f32, f32) {
@@ -260,7 +267,8 @@ def ParallelOp : Loop_Op<"parallel",
260
267
261
268
The body region must contain exactly one block that terminates with
262
269
"loop.yield" without operands. Parsing ParallelOp will create such a region
263
- and insert the terminator when it is absent from the custom format. For example:
270
+ and insert the terminator when it is absent from the custom format.
271
+ For example:
264
272
265
273
```mlir
266
274
loop.parallel (%iv) = (%lb) to (%ub) step (%step) {
@@ -354,8 +362,8 @@ def ReduceReturnOp :
354
362
let summary = "terminator for reduce operation";
355
363
let description = [{
356
364
"loop.reduce.return" is a special terminator operation for the block inside
357
- "loop.reduce". It terminates the region. It should have the same type as the
358
- operand of "loop.reduce". Example for the custom format:
365
+ "loop.reduce". It terminates the region. It should have the same type as
366
+ the operand of "loop.reduce". Example for the custom format:
359
367
360
368
```mlir
361
369
loop.reduce.return %res : f32
@@ -382,7 +390,8 @@ def YieldOp : Loop_Op<"yield", [Terminator]> {
382
390
383
391
let arguments = (ins Variadic<AnyType>:$results);
384
392
let builders = [
385
- OpBuilder<"Builder *builder, OperationState &result", [{ /* nothing to do */ }]>
393
+ OpBuilder<"Builder *builder, OperationState &result",
394
+ [{ /* nothing to do */ }]>
386
395
];
387
396
}
388
397
#endif // LOOP_OPS
0 commit comments