blob: 7e3e286b842fa2d78f029e51bc02ba57973c6c94 [file] [log] [blame]
Nicolas Vasilachecca53e82019-07-15 02:50:09 -07001//===- Ops.cpp - Loop MLIR Operations -------------------------------------===//
2//
Mehdi Amini56222a02019-12-23 09:35:36 -08003// Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Nicolas Vasilachecca53e82019-07-15 02:50:09 -07006//
Mehdi Amini56222a02019-12-23 09:35:36 -08007//===----------------------------------------------------------------------===//
Nicolas Vasilachecca53e82019-07-15 02:50:09 -07008
9#include "mlir/Dialect/LoopOps/LoopOps.h"
River Riddleba0fa922019-08-19 11:00:47 -070010#include "mlir/Dialect/StandardOps/Ops.h"
Nicolas Vasilachecca53e82019-07-15 02:50:09 -070011#include "mlir/IR/AffineExpr.h"
12#include "mlir/IR/AffineMap.h"
13#include "mlir/IR/Builders.h"
14#include "mlir/IR/Function.h"
15#include "mlir/IR/Matchers.h"
16#include "mlir/IR/Module.h"
17#include "mlir/IR/OpImplementation.h"
18#include "mlir/IR/PatternMatch.h"
19#include "mlir/IR/StandardTypes.h"
20#include "mlir/IR/Value.h"
Nicolas Vasilachecca53e82019-07-15 02:50:09 -070021#include "mlir/Support/MathExtras.h"
22#include "mlir/Support/STLExtras.h"
Stephan Herhutb843cc52019-10-16 04:28:13 -070023#include "mlir/Transforms/SideEffectsInterface.h"
Nicolas Vasilachecca53e82019-07-15 02:50:09 -070024
25using namespace mlir;
26using namespace mlir::loop;
27
28//===----------------------------------------------------------------------===//
Stephan Herhutb843cc52019-10-16 04:28:13 -070029// LoopOpsDialect Interfaces
30//===----------------------------------------------------------------------===//
31namespace {
32
33struct LoopSideEffectsInterface : public SideEffectsDialectInterface {
34 using SideEffectsDialectInterface::SideEffectsDialectInterface;
35
36 SideEffecting isSideEffecting(Operation *op) const override {
37 if (isa<IfOp>(op) || isa<ForOp>(op)) {
38 return Recursive;
39 }
40 return SideEffectsDialectInterface::isSideEffecting(op);
41 };
42};
43
44} // namespace
45
46//===----------------------------------------------------------------------===//
Nicolas Vasilachecca53e82019-07-15 02:50:09 -070047// LoopOpsDialect
48//===----------------------------------------------------------------------===//
49
50LoopOpsDialect::LoopOpsDialect(MLIRContext *context)
51 : Dialect(getDialectNamespace(), context) {
52 addOperations<
53#define GET_OP_LIST
54#include "mlir/Dialect/LoopOps/LoopOps.cpp.inc"
55 >();
Stephan Herhutb843cc52019-10-16 04:28:13 -070056 addInterfaces<LoopSideEffectsInterface>();
Nicolas Vasilachecca53e82019-07-15 02:50:09 -070057}
58
59//===----------------------------------------------------------------------===//
60// ForOp
61//===----------------------------------------------------------------------===//
62
River Riddlee62a6952019-12-23 14:45:01 -080063void ForOp::build(Builder *builder, OperationState &result, Value lb, Value ub,
64 Value step) {
River Riddle729727e2019-09-20 19:47:05 -070065 result.addOperands({lb, ub, step});
66 Region *bodyRegion = result.addRegion();
67 ForOp::ensureTerminator(*bodyRegion, *builder, result.location);
Nicolas Vasilachecca53e82019-07-15 02:50:09 -070068 bodyRegion->front().addArgument(builder->getIndexType());
69}
70
71LogicalResult verify(ForOp op) {
River Riddle2bdf33c2020-01-11 08:54:04 -080072 if (auto cst = dyn_cast_or_null<ConstantIndexOp>(op.step().getDefiningOp()))
Nicolas Vasilachecca53e82019-07-15 02:50:09 -070073 if (cst.getValue() <= 0)
Uday Bondhugulaad6925f2019-10-22 07:34:24 -070074 return op.emitOpError("constant step operand must be positive");
Nicolas Vasilachecca53e82019-07-15 02:50:09 -070075
76 // Check that the body defines as single block argument for the induction
77 // variable.
Nicolas Vasilache0002e292019-07-16 12:20:15 -070078 auto *body = op.getBody();
River Riddle2bdf33c2020-01-11 08:54:04 -080079 if (body->getNumArguments() != 1 || !body->getArgument(0).getType().isIndex())
Nicolas Vasilachecca53e82019-07-15 02:50:09 -070080 return op.emitOpError("expected body to have a single index argument for "
81 "the induction variable");
Nicolas Vasilachecca53e82019-07-15 02:50:09 -070082 return success();
83}
84
River Riddle3a643de2019-09-20 20:43:02 -070085static void print(OpAsmPrinter &p, ForOp op) {
River Riddle2bdf33c2020-01-11 08:54:04 -080086 p << op.getOperationName() << " " << op.getInductionVar() << " = "
87 << op.lowerBound() << " to " << op.upperBound() << " step " << op.step();
River Riddle3a643de2019-09-20 20:43:02 -070088 p.printRegion(op.region(),
89 /*printEntryBlockArgs=*/false,
90 /*printBlockTerminators=*/false);
91 p.printOptionalAttrDict(op.getAttrs());
Nicolas Vasilachecca53e82019-07-15 02:50:09 -070092}
93
River Riddle729727e2019-09-20 19:47:05 -070094static ParseResult parseForOp(OpAsmParser &parser, OperationState &result) {
River Riddle27975172019-09-20 11:36:49 -070095 auto &builder = parser.getBuilder();
Nicolas Vasilachecca53e82019-07-15 02:50:09 -070096 OpAsmParser::OperandType inductionVariable, lb, ub, step;
97 // Parse the induction variable followed by '='.
River Riddle27975172019-09-20 11:36:49 -070098 if (parser.parseRegionArgument(inductionVariable) || parser.parseEqual())
Nicolas Vasilachecca53e82019-07-15 02:50:09 -070099 return failure();
100
101 // Parse loop bounds.
102 Type indexType = builder.getIndexType();
River Riddle27975172019-09-20 11:36:49 -0700103 if (parser.parseOperand(lb) ||
River Riddle729727e2019-09-20 19:47:05 -0700104 parser.resolveOperand(lb, indexType, result.operands) ||
River Riddle27975172019-09-20 11:36:49 -0700105 parser.parseKeyword("to") || parser.parseOperand(ub) ||
River Riddle729727e2019-09-20 19:47:05 -0700106 parser.resolveOperand(ub, indexType, result.operands) ||
River Riddle27975172019-09-20 11:36:49 -0700107 parser.parseKeyword("step") || parser.parseOperand(step) ||
River Riddle729727e2019-09-20 19:47:05 -0700108 parser.resolveOperand(step, indexType, result.operands))
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700109 return failure();
110
111 // Parse the body region.
River Riddle729727e2019-09-20 19:47:05 -0700112 Region *body = result.addRegion();
River Riddle27975172019-09-20 11:36:49 -0700113 if (parser.parseRegion(*body, inductionVariable, indexType))
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700114 return failure();
115
River Riddle729727e2019-09-20 19:47:05 -0700116 ForOp::ensureTerminator(*body, builder, result.location);
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700117
118 // Parse the optional attribute list.
River Riddle8fa9d822019-11-05 13:32:07 -0800119 if (parser.parseOptionalAttrDict(result.attributes))
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700120 return failure();
121
122 return success();
123}
124
Stephan Herhutb843cc52019-10-16 04:28:13 -0700125Region &ForOp::getLoopBody() { return region(); }
126
River Riddlee62a6952019-12-23 14:45:01 -0800127bool ForOp::isDefinedOutsideOfLoop(Value value) {
River Riddle2bdf33c2020-01-11 08:54:04 -0800128 return !region().isAncestor(value.getParentRegion());
Stephan Herhutb843cc52019-10-16 04:28:13 -0700129}
130
131LogicalResult ForOp::moveOutOfLoop(ArrayRef<Operation *> ops) {
River Riddle2bdf33c2020-01-11 08:54:04 -0800132 for (auto op : ops)
Stephan Herhutb843cc52019-10-16 04:28:13 -0700133 op->moveBefore(this->getOperation());
134 return success();
135}
136
River Riddlee62a6952019-12-23 14:45:01 -0800137ForOp mlir::loop::getForInductionVarOwner(Value val) {
River Riddleab465432019-12-23 12:36:20 -0800138 auto ivArg = val.dyn_cast<BlockArgument>();
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700139 if (!ivArg)
140 return ForOp();
River Riddle2bdf33c2020-01-11 08:54:04 -0800141 assert(ivArg.getOwner() && "unlinked block argument");
142 auto *containingInst = ivArg.getOwner()->getParentOp();
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700143 return dyn_cast_or_null<ForOp>(containingInst);
144}
145
146//===----------------------------------------------------------------------===//
147// IfOp
148//===----------------------------------------------------------------------===//
149
River Riddlee62a6952019-12-23 14:45:01 -0800150void IfOp::build(Builder *builder, OperationState &result, Value cond,
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700151 bool withElseRegion) {
River Riddle729727e2019-09-20 19:47:05 -0700152 result.addOperands(cond);
153 Region *thenRegion = result.addRegion();
154 Region *elseRegion = result.addRegion();
155 IfOp::ensureTerminator(*thenRegion, *builder, result.location);
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700156 if (withElseRegion)
River Riddle729727e2019-09-20 19:47:05 -0700157 IfOp::ensureTerminator(*elseRegion, *builder, result.location);
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700158}
159
160static LogicalResult verify(IfOp op) {
161 // Verify that the entry of each child region does not have arguments.
162 for (auto &region : op.getOperation()->getRegions()) {
163 if (region.empty())
164 continue;
165
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700166 for (auto &b : region)
167 if (b.getNumArguments() != 0)
168 return op.emitOpError(
169 "requires that child entry blocks have no arguments");
170 }
171 return success();
172}
173
River Riddle729727e2019-09-20 19:47:05 -0700174static ParseResult parseIfOp(OpAsmParser &parser, OperationState &result) {
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700175 // Create the regions for 'then'.
River Riddle729727e2019-09-20 19:47:05 -0700176 result.regions.reserve(2);
177 Region *thenRegion = result.addRegion();
178 Region *elseRegion = result.addRegion();
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700179
River Riddle27975172019-09-20 11:36:49 -0700180 auto &builder = parser.getBuilder();
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700181 OpAsmParser::OperandType cond;
182 Type i1Type = builder.getIntegerType(1);
River Riddle27975172019-09-20 11:36:49 -0700183 if (parser.parseOperand(cond) ||
River Riddle729727e2019-09-20 19:47:05 -0700184 parser.resolveOperand(cond, i1Type, result.operands))
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700185 return failure();
186
187 // Parse the 'then' region.
Adrian Kuegel018b0422020-01-08 14:06:22 +0100188 if (parser.parseRegion(*thenRegion, /*arguments=*/{}, /*argTypes=*/{}))
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700189 return failure();
River Riddle729727e2019-09-20 19:47:05 -0700190 IfOp::ensureTerminator(*thenRegion, parser.getBuilder(), result.location);
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700191
192 // If we find an 'else' keyword then parse the 'else' region.
River Riddle27975172019-09-20 11:36:49 -0700193 if (!parser.parseOptionalKeyword("else")) {
Adrian Kuegel018b0422020-01-08 14:06:22 +0100194 if (parser.parseRegion(*elseRegion, /*arguments=*/{}, /*argTypes=*/{}))
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700195 return failure();
River Riddle729727e2019-09-20 19:47:05 -0700196 IfOp::ensureTerminator(*elseRegion, parser.getBuilder(), result.location);
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700197 }
198
199 // Parse the optional attribute list.
River Riddle8fa9d822019-11-05 13:32:07 -0800200 if (parser.parseOptionalAttrDict(result.attributes))
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700201 return failure();
202
203 return success();
204}
205
River Riddle3a643de2019-09-20 20:43:02 -0700206static void print(OpAsmPrinter &p, IfOp op) {
River Riddle2bdf33c2020-01-11 08:54:04 -0800207 p << IfOp::getOperationName() << " " << op.condition();
River Riddle3a643de2019-09-20 20:43:02 -0700208 p.printRegion(op.thenRegion(),
209 /*printEntryBlockArgs=*/false,
210 /*printBlockTerminators=*/false);
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700211
212 // Print the 'else' regions if it exists and has a block.
213 auto &elseRegion = op.elseRegion();
214 if (!elseRegion.empty()) {
River Riddle3a643de2019-09-20 20:43:02 -0700215 p << " else";
216 p.printRegion(elseRegion,
217 /*printEntryBlockArgs=*/false,
218 /*printBlockTerminators=*/false);
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700219 }
220
River Riddle3a643de2019-09-20 20:43:02 -0700221 p.printOptionalAttrDict(op.getAttrs());
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700222}
223
224//===----------------------------------------------------------------------===//
Adrian Kuegel018b0422020-01-08 14:06:22 +0100225// ParallelOp
226//===----------------------------------------------------------------------===//
227
228static LogicalResult verify(ParallelOp op) {
229 // Check that there is at least one value in lowerBound, upperBound and step.
230 // It is sufficient to test only step, because it is ensured already that the
231 // number of elements in lowerBound, upperBound and step are the same.
232 Operation::operand_range stepValues = op.step();
233 if (stepValues.empty())
234 return op.emitOpError(
235 "needs at least one tuple element for lowerBound, upperBound and step");
236
237 // Check whether all constant step values are positive.
238 for (Value stepValue : stepValues)
239 if (auto cst = dyn_cast_or_null<ConstantIndexOp>(stepValue.getDefiningOp()))
240 if (cst.getValue() <= 0)
241 return op.emitOpError("constant step operand must be positive");
242
243 // Check that the body defines the same number of block arguments as the
244 // number of tuple elements in step.
245 Block *body = &op.body().front();
246 if (body->getNumArguments() != stepValues.size())
247 return op.emitOpError(
248 "expects the same number of induction variables as bound and step "
249 "values");
250 for (auto arg : body->getArguments())
251 if (!arg.getType().isIndex())
252 return op.emitOpError(
253 "expects arguments for the induction variable to be of index type");
254
255 // Check that the number of results is the same as the number of ReduceOps.
256 SmallVector<ReduceOp, 4> reductions(body->getOps<ReduceOp>());
257 if (op.results().size() != reductions.size())
258 return op.emitOpError(
259 "expects number of results to be the same as number of reductions");
260
261 // Check that the types of the results and reductions are the same.
262 for (auto resultAndReduce : llvm::zip(op.results(), reductions)) {
263 auto resultType = std::get<0>(resultAndReduce).getType();
264 auto reduceOp = std::get<1>(resultAndReduce);
265 auto reduceType = reduceOp.operand().getType();
266 if (resultType != reduceType)
267 return reduceOp.emitOpError()
268 << "expects type of reduce to be the same as result type: "
269 << resultType;
270 }
271 return success();
272}
273
274static ParseResult parseParallelOp(OpAsmParser &parser,
275 OperationState &result) {
276 auto &builder = parser.getBuilder();
277 // Parse an opening `(` followed by induction variables followed by `)`
278 SmallVector<OpAsmParser::OperandType, 4> ivs;
279 if (parser.parseRegionArgumentList(ivs, /*requiredOperandCount=*/-1,
280 OpAsmParser::Delimiter::Paren))
281 return failure();
282
283 // Parse loop bounds.
284 SmallVector<OpAsmParser::OperandType, 4> lower;
285 if (parser.parseEqual() ||
286 parser.parseOperandList(lower, ivs.size(),
287 OpAsmParser::Delimiter::Paren) ||
288 parser.resolveOperands(lower, builder.getIndexType(), result.operands))
289 return failure();
290
291 SmallVector<OpAsmParser::OperandType, 4> upper;
292 if (parser.parseKeyword("to") ||
293 parser.parseOperandList(upper, ivs.size(),
294 OpAsmParser::Delimiter::Paren) ||
295 parser.resolveOperands(upper, builder.getIndexType(), result.operands))
296 return failure();
297
298 // Parse step value.
299 SmallVector<OpAsmParser::OperandType, 4> steps;
300 if (parser.parseKeyword("step") ||
301 parser.parseOperandList(steps, ivs.size(),
302 OpAsmParser::Delimiter::Paren) ||
303 parser.resolveOperands(steps, builder.getIndexType(), result.operands))
304 return failure();
305
306 // Now parse the body.
307 Region *body = result.addRegion();
308 SmallVector<Type, 4> types(ivs.size(), builder.getIndexType());
309 if (parser.parseRegion(*body, ivs, types))
310 return failure();
311
312 // Parse attributes and optional results (in case there is a reduce).
313 if (parser.parseOptionalAttrDict(result.attributes) ||
314 parser.parseOptionalColonTypeList(result.types))
315 return failure();
316
317 // Add a terminator if none was parsed.
318 ForOp::ensureTerminator(*body, builder, result.location);
319
320 return success();
321}
322
323static void print(OpAsmPrinter &p, ParallelOp op) {
324 p << op.getOperationName() << " (";
325 p.printOperands(op.body().front().getArguments());
326 p << ") = (" << op.lowerBound() << ") to (" << op.upperBound() << ") step ("
327 << op.step() << ")";
328 p.printRegion(op.body(), /*printEntryBlockArgs=*/false);
329 p.printOptionalAttrDict(op.getAttrs());
330 if (!op.results().empty())
331 p << " : " << op.getResultTypes();
332}
333
334//===----------------------------------------------------------------------===//
335// ReduceOp
336//===----------------------------------------------------------------------===//
337
338static LogicalResult verify(ReduceOp op) {
339 // The region of a ReduceOp has two arguments of the same type as its operand.
340 auto type = op.operand().getType();
341 Block &block = op.reductionOperator().front();
342 if (block.empty())
343 return op.emitOpError("the block inside reduce should not be empty");
344 if (block.getNumArguments() != 2 ||
345 llvm::any_of(block.getArguments(), [&](const BlockArgument &arg) {
346 return arg.getType() != type;
347 }))
Adrian5a6eae32020-01-14 11:41:40 +0100348 return op.emitOpError()
349 << "expects two arguments to reduce block of type " << type;
Adrian Kuegel018b0422020-01-08 14:06:22 +0100350
351 // Check that the block is terminated by a ReduceReturnOp.
352 if (!isa<ReduceReturnOp>(block.getTerminator()))
353 return op.emitOpError("the block inside reduce should be terminated with a "
354 "'loop.reduce.return' op");
355
356 return success();
357}
358
359static ParseResult parseReduceOp(OpAsmParser &parser, OperationState &result) {
360 // Parse an opening `(` followed by the reduced value followed by `)`
361 OpAsmParser::OperandType operand;
362 if (parser.parseLParen() || parser.parseOperand(operand) ||
363 parser.parseRParen())
364 return failure();
365
366 // Now parse the body.
367 Region *body = result.addRegion();
368 if (parser.parseRegion(*body, /*arguments=*/{}, /*argTypes=*/{}))
369 return failure();
370
371 // And the type of the operand (and also what reduce computes on).
372 Type resultType;
373 if (parser.parseColonType(resultType) ||
374 parser.resolveOperand(operand, resultType, result.operands))
375 return failure();
376
377 return success();
378}
379
380static void print(OpAsmPrinter &p, ReduceOp op) {
381 p << op.getOperationName() << "(" << op.operand() << ") ";
382 p.printRegion(op.reductionOperator());
383 p << " : " << op.operand().getType();
384}
385
386//===----------------------------------------------------------------------===//
387// ReduceReturnOp
388//===----------------------------------------------------------------------===//
389
390static LogicalResult verify(ReduceReturnOp op) {
391 // The type of the return value should be the same type as the type of the
392 // operand of the enclosing ReduceOp.
393 auto reduceOp = cast<ReduceOp>(op.getParentOp());
394 Type reduceType = reduceOp.operand().getType();
395 if (reduceType != op.result().getType())
396 return op.emitOpError() << "needs to have type " << reduceType
397 << " (the type of the enclosing ReduceOp)";
398 return success();
399}
400
401static ParseResult parseReduceReturnOp(OpAsmParser &parser,
402 OperationState &result) {
403 OpAsmParser::OperandType operand;
404 Type resultType;
405 if (parser.parseOperand(operand) || parser.parseColonType(resultType) ||
406 parser.resolveOperand(operand, resultType, result.operands))
407 return failure();
408
409 return success();
410}
411
412static void print(OpAsmPrinter &p, ReduceReturnOp op) {
413 p << op.getOperationName() << " " << op.result() << " : "
414 << op.result().getType();
415}
416
417//===----------------------------------------------------------------------===//
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700418// TableGen'd op method definitions
419//===----------------------------------------------------------------------===//
420
421#define GET_OP_CLASSES
422#include "mlir/Dialect/LoopOps/LoopOps.cpp.inc"