/
LoopOps.td
261 lines (224 loc) · 9.46 KB
/
LoopOps.td
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//===- Ops.td - Loop operation definitions ---------------*- tablegen -*-===//
//
// Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Defines MLIR loop operations.
//
//===----------------------------------------------------------------------===//
#ifndef LOOP_OPS
#define LOOP_OPS
include "mlir/IR/OpBase.td"
include "mlir/Transforms/LoopLikeInterface.td"
def Loop_Dialect : Dialect {
let name = "loop";
let cppNamespace = "";
}
// Base class for Loop dialect ops.
class Loop_Op<string mnemonic, list<OpTrait> traits = []> :
Op<Loop_Dialect, mnemonic, traits> {
// For every standard op, there needs to be a:
// * void print(OpAsmPrinter &p, ${C++ class of Op} op)
// * LogicalResult verify(${C++ class of Op} op)
// * ParseResult parse${C++ class of Op}(OpAsmParser &parser,
// OperationState &result)
// functions.
let printer = [{ return ::print(p, *this); }];
let verifier = [{ return ::verify(*this); }];
let parser = [{ return ::parse$cppClass(parser, result); }];
}
def ForOp : Loop_Op<"for",
[DeclareOpInterfaceMethods<LoopLikeOpInterface>,
SingleBlockImplicitTerminator<"TerminatorOp">]> {
let summary = "for operation";
let description = [{
The "loop.for" operation represents a loop nest taking 3 SSA value as
operands that represent the lower bound, upper bound and step respectively.
The operation defines an SSA value for its induction variable. It has one
region capturing the loop body. The induction variable is represented as an
argument of this region. This SSA value always has type index, which is the
size of the machine word. The step is a value of type index, required to be
positive.
The lower and upper bounds specify a half-open range: the range includes the
lower bound but does not include the upper bound.
The body region must contain exactly one block that terminates with
"loop.terminator". Calling ForOp::build will create such region and insert
the terminator, so will the parsing even in cases when it is absent from the
custom format. For example:
```mlir
loop.for %iv = %lb to %ub step %step {
... // body
}
```
}];
let arguments = (ins Index:$lowerBound, Index:$upperBound, Index:$step);
let regions = (region SizedRegion<1>:$region);
let skipDefaultBuilders = 1;
let builders = [
OpBuilder<"Builder *builder, OperationState &result, "
"Value lowerBound, Value upperBound, Value step">
];
let extraClassDeclaration = [{
Block *getBody() { return ®ion().front(); }
Value getInductionVar() { return getBody()->getArgument(0); }
OpBuilder getBodyBuilder() {
return OpBuilder(getBody(), std::prev(getBody()->end()));
}
void setLowerBound(Value bound) { getOperation()->setOperand(0, bound); }
void setUpperBound(Value bound) { getOperation()->setOperand(1, bound); }
void setStep(Value step) { getOperation()->setOperand(2, step); }
}];
}
def IfOp : Loop_Op<"if",
[SingleBlockImplicitTerminator<"TerminatorOp">]> {
let summary = "if-then-else operation";
let description = [{
The "loop.if" operation represents an if-then-else construct for
conditionally executing two regions of code. The operand to an if operation
is a boolean value. The operation produces no results. For example:
```mlir
loop.if %b {
...
} else {
...
}
```
The 'else' block is optional, and may be omitted. For
example:
```mlir
loop.if %b {
...
}
```
}];
let arguments = (ins I1:$condition);
let regions = (region SizedRegion<1>:$thenRegion, AnyRegion:$elseRegion);
let skipDefaultBuilders = 1;
let builders = [
OpBuilder<"Builder *builder, OperationState &result, "
"Value cond, bool withElseRegion">
];
let extraClassDeclaration = [{
OpBuilder getThenBodyBuilder() {
assert(!thenRegion().empty() && "Unexpected empty 'then' region.");
Block &body = thenRegion().front();
return OpBuilder(&body, std::prev(body.end()));
}
OpBuilder getElseBodyBuilder() {
assert(!elseRegion().empty() && "Unexpected empty 'else' region.");
Block &body = elseRegion().front();
return OpBuilder(&body, std::prev(body.end()));
}
}];
}
def ParallelOp : Loop_Op<"parallel",
[SameVariadicOperandSize, SingleBlockImplicitTerminator<"TerminatorOp">]> {
let summary = "parallel for operation";
let description = [{
The "loop.parallel" operation represents a loop nest taking 3 groups of SSA
values as operands that represent the lower bounds, upper bounds and steps,
respectively. The operation defines a variadic number of SSA values for its
induction variables. It has one region capturing the loop body. The
induction variables are represented as an argument of this region. These SSA
values always have type index, which is the size of the machine word. The
steps are values of type index, required to be positive.
The lower and upper bounds specify a half-open range: the range includes the
lower bound but does not include the upper bound.
Semantically we require that the iteration space can be iterated in any
order, and the loop body can be executed in parallel. If there are data
races, the behavior is undefined.
The parallel loop operation supports reduction of values produced by
individual iterations into a single result. This is modeled using the
loop.reduce operation (see loop.reduce for details). Each result of a
loop.parallel operation is associated with a reduce operation that is an
immediate child. Reduces are matched to result values in order of their
appearance in the body. Consequently, we require that the body region has
the same number of results as it has reduce operations.
The body region must contain exactly one block that terminates with
"loop.terminator". Parsing ParallelOp will create such region and insert the
terminator when it is absent from the custom format. For example:
```mlir
loop.parallel (%iv) = (%lb) to (%ub) step (%step) {
%zero = constant 0.0 : f32
loop.reduce(%zero) {
^bb0(%lhs : f32, %rhs: f32):
%res = addf %lhs, %rhs : f32
loop.reduce.return %res : f32
} : f32
}
```
}];
let arguments = (ins Variadic<Index>:$lowerBound,
Variadic<Index>:$upperBound,
Variadic<Index>:$step);
let results = (outs Variadic<AnyType>:$results);
let regions = (region SizedRegion<1>:$body);
}
def ReduceOp : Loop_Op<"reduce", [HasParent<"ParallelOp">]> {
let summary = "reduce operation for parallel for";
let description = [{
"loop.reduce" is an operation occuring inside "loop.parallel" operations. It
consists of one block with two arguments which have the same type as the
operand of "loop.reduce".
"loop.reduce" is used to model the value for reduction computations of a
"loop.parallel" operation. It has to appear as an immediate child of a
"loop.parallel" and is associated with a result value of its parent
operation.
Association is in the order of appearance in the body where the first result
of a parallel loop operation corresponds to the first "loop.reduce" in the
operation's body region. The reduce operation takes a single operand, which
is the value to be used in the reduction.
The reduce operation contains a region whose entry block expects two
arguments of the same type as the operand. As the iteration order of the
parallel loop and hence reduction order is unspecified, the result of
reduction may be non-deterministic unless the operation is associative and
commutative.
The result of the reduce operation's body must have the same type as the
operands and associated result value of the parallel loop operation.
Example:
```mlir
%zero = constant 0.0 : f32
loop.reduce(%zero) {
^bb0(%lhs : f32, %rhs: f32):
%res = addf %lhs, %rhs : f32
loop.reduce.return %res : f32
} : f32
```
}];
let arguments = (ins AnyType:$operand);
let regions = (region SizedRegion<1>:$reductionOperator);
}
def ReduceReturnOp :
Loop_Op<"reduce.return", [HasParent<"ReduceOp">, Terminator]> {
let summary = "terminator for reduce operation";
let description = [{
"loop.reduce.return" is a special terminator operation for the block inside
"loop.reduce". It terminates the region. It should have the same type as the
operand of "loop.reduce". Example for the custom format:
```mlir
loop.reduce.return %res : f32
```
}];
let arguments = (ins AnyType:$result);
}
def TerminatorOp : Loop_Op<"terminator", [Terminator]> {
let summary = "cf terminator operation";
let description = [{
"loop.terminator" is a special terminator operation for blocks inside
loops. It terminates the region. This operation does _not_ have a custom
syntax. However, `std` control operations omit the terminator in their
custom syntax for brevity.
```mlir
loop.terminator
```
}];
// No custom parsing/printing form.
let parser = ?;
let printer = ?;
// Fully specified by traits.
let verifier = ?;
}
#endif // LOOP_OPS