blob: 8e19eba911a9bbfaa85b393abfe999108c21048a [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 Riddle35807bc2019-12-22 21:59:55 -080063void ForOp::build(Builder *builder, OperationState &result, ValuePtr lb,
64 ValuePtr ub, ValuePtr 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) {
72 if (auto cst = dyn_cast_or_null<ConstantIndexOp>(op.step()->getDefiningOp()))
73 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();
Nicolas Vasilachecca53e82019-07-15 02:50:09 -070079 if (body->getNumArguments() != 1 ||
80 !body->getArgument(0)->getType().isIndex())
81 return op.emitOpError("expected body to have a single index argument for "
82 "the induction variable");
Nicolas Vasilachecca53e82019-07-15 02:50:09 -070083 return success();
84}
85
River Riddle3a643de2019-09-20 20:43:02 -070086static void print(OpAsmPrinter &p, ForOp op) {
87 p << op.getOperationName() << " " << *op.getInductionVar() << " = "
88 << *op.lowerBound() << " to " << *op.upperBound() << " step " << *op.step();
89 p.printRegion(op.region(),
90 /*printEntryBlockArgs=*/false,
91 /*printBlockTerminators=*/false);
92 p.printOptionalAttrDict(op.getAttrs());
Nicolas Vasilachecca53e82019-07-15 02:50:09 -070093}
94
River Riddle729727e2019-09-20 19:47:05 -070095static ParseResult parseForOp(OpAsmParser &parser, OperationState &result) {
River Riddle27975172019-09-20 11:36:49 -070096 auto &builder = parser.getBuilder();
Nicolas Vasilachecca53e82019-07-15 02:50:09 -070097 OpAsmParser::OperandType inductionVariable, lb, ub, step;
98 // Parse the induction variable followed by '='.
River Riddle27975172019-09-20 11:36:49 -070099 if (parser.parseRegionArgument(inductionVariable) || parser.parseEqual())
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700100 return failure();
101
102 // Parse loop bounds.
103 Type indexType = builder.getIndexType();
River Riddle27975172019-09-20 11:36:49 -0700104 if (parser.parseOperand(lb) ||
River Riddle729727e2019-09-20 19:47:05 -0700105 parser.resolveOperand(lb, indexType, result.operands) ||
River Riddle27975172019-09-20 11:36:49 -0700106 parser.parseKeyword("to") || parser.parseOperand(ub) ||
River Riddle729727e2019-09-20 19:47:05 -0700107 parser.resolveOperand(ub, indexType, result.operands) ||
River Riddle27975172019-09-20 11:36:49 -0700108 parser.parseKeyword("step") || parser.parseOperand(step) ||
River Riddle729727e2019-09-20 19:47:05 -0700109 parser.resolveOperand(step, indexType, result.operands))
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700110 return failure();
111
112 // Parse the body region.
River Riddle729727e2019-09-20 19:47:05 -0700113 Region *body = result.addRegion();
River Riddle27975172019-09-20 11:36:49 -0700114 if (parser.parseRegion(*body, inductionVariable, indexType))
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700115 return failure();
116
River Riddle729727e2019-09-20 19:47:05 -0700117 ForOp::ensureTerminator(*body, builder, result.location);
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700118
119 // Parse the optional attribute list.
River Riddle8fa9d822019-11-05 13:32:07 -0800120 if (parser.parseOptionalAttrDict(result.attributes))
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700121 return failure();
122
123 return success();
124}
125
Stephan Herhutb843cc52019-10-16 04:28:13 -0700126Region &ForOp::getLoopBody() { return region(); }
127
River Riddle35807bc2019-12-22 21:59:55 -0800128bool ForOp::isDefinedOutsideOfLoop(ValuePtr value) {
Stephan Herhutb843cc52019-10-16 04:28:13 -0700129 return !region().isAncestor(value->getParentRegion());
130}
131
132LogicalResult ForOp::moveOutOfLoop(ArrayRef<Operation *> ops) {
133 for (auto *op : ops)
134 op->moveBefore(this->getOperation());
135 return success();
136}
137
River Riddle35807bc2019-12-22 21:59:55 -0800138ForOp mlir::loop::getForInductionVarOwner(ValuePtr val) {
River Riddleab465432019-12-23 12:36:20 -0800139 auto ivArg = val.dyn_cast<BlockArgument>();
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700140 if (!ivArg)
141 return ForOp();
142 assert(ivArg->getOwner() && "unlinked block argument");
River Riddle1e429542019-08-09 20:07:25 -0700143 auto *containingInst = ivArg->getOwner()->getParentOp();
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700144 return dyn_cast_or_null<ForOp>(containingInst);
145}
146
147//===----------------------------------------------------------------------===//
148// IfOp
149//===----------------------------------------------------------------------===//
150
River Riddle35807bc2019-12-22 21:59:55 -0800151void IfOp::build(Builder *builder, OperationState &result, ValuePtr cond,
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700152 bool withElseRegion) {
River Riddle729727e2019-09-20 19:47:05 -0700153 result.addOperands(cond);
154 Region *thenRegion = result.addRegion();
155 Region *elseRegion = result.addRegion();
156 IfOp::ensureTerminator(*thenRegion, *builder, result.location);
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700157 if (withElseRegion)
River Riddle729727e2019-09-20 19:47:05 -0700158 IfOp::ensureTerminator(*elseRegion, *builder, result.location);
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700159}
160
161static LogicalResult verify(IfOp op) {
162 // Verify that the entry of each child region does not have arguments.
163 for (auto &region : op.getOperation()->getRegions()) {
164 if (region.empty())
165 continue;
166
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700167 for (auto &b : region)
168 if (b.getNumArguments() != 0)
169 return op.emitOpError(
170 "requires that child entry blocks have no arguments");
171 }
172 return success();
173}
174
River Riddle729727e2019-09-20 19:47:05 -0700175static ParseResult parseIfOp(OpAsmParser &parser, OperationState &result) {
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700176 // Create the regions for 'then'.
River Riddle729727e2019-09-20 19:47:05 -0700177 result.regions.reserve(2);
178 Region *thenRegion = result.addRegion();
179 Region *elseRegion = result.addRegion();
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700180
River Riddle27975172019-09-20 11:36:49 -0700181 auto &builder = parser.getBuilder();
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700182 OpAsmParser::OperandType cond;
183 Type i1Type = builder.getIntegerType(1);
River Riddle27975172019-09-20 11:36:49 -0700184 if (parser.parseOperand(cond) ||
River Riddle729727e2019-09-20 19:47:05 -0700185 parser.resolveOperand(cond, i1Type, result.operands))
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700186 return failure();
187
188 // Parse the 'then' region.
River Riddle27975172019-09-20 11:36:49 -0700189 if (parser.parseRegion(*thenRegion, {}, {}))
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700190 return failure();
River Riddle729727e2019-09-20 19:47:05 -0700191 IfOp::ensureTerminator(*thenRegion, parser.getBuilder(), result.location);
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700192
193 // If we find an 'else' keyword then parse the 'else' region.
River Riddle27975172019-09-20 11:36:49 -0700194 if (!parser.parseOptionalKeyword("else")) {
195 if (parser.parseRegion(*elseRegion, {}, {}))
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700196 return failure();
River Riddle729727e2019-09-20 19:47:05 -0700197 IfOp::ensureTerminator(*elseRegion, parser.getBuilder(), result.location);
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700198 }
199
200 // Parse the optional attribute list.
River Riddle8fa9d822019-11-05 13:32:07 -0800201 if (parser.parseOptionalAttrDict(result.attributes))
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700202 return failure();
203
204 return success();
205}
206
River Riddle3a643de2019-09-20 20:43:02 -0700207static void print(OpAsmPrinter &p, IfOp op) {
208 p << IfOp::getOperationName() << " " << *op.condition();
209 p.printRegion(op.thenRegion(),
210 /*printEntryBlockArgs=*/false,
211 /*printBlockTerminators=*/false);
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700212
213 // Print the 'else' regions if it exists and has a block.
214 auto &elseRegion = op.elseRegion();
215 if (!elseRegion.empty()) {
River Riddle3a643de2019-09-20 20:43:02 -0700216 p << " else";
217 p.printRegion(elseRegion,
218 /*printEntryBlockArgs=*/false,
219 /*printBlockTerminators=*/false);
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700220 }
221
River Riddle3a643de2019-09-20 20:43:02 -0700222 p.printOptionalAttrDict(op.getAttrs());
Nicolas Vasilachecca53e82019-07-15 02:50:09 -0700223}
224
225//===----------------------------------------------------------------------===//
226// TableGen'd op method definitions
227//===----------------------------------------------------------------------===//
228
229#define GET_OP_CLASSES
230#include "mlir/Dialect/LoopOps/LoopOps.cpp.inc"