| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 1 | //===- Ops.cpp - Loop MLIR Operations -------------------------------------===// |
| 2 | // |
| Mehdi Amini | 56222a0 | 2019-12-23 09:35:36 -0800 | [diff] [blame] | 3 | // 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 Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 6 | // |
| Mehdi Amini | 56222a0 | 2019-12-23 09:35:36 -0800 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 8 | |
| 9 | #include "mlir/Dialect/LoopOps/LoopOps.h" |
| River Riddle | ba0fa92 | 2019-08-19 11:00:47 -0700 | [diff] [blame] | 10 | #include "mlir/Dialect/StandardOps/Ops.h" |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 11 | #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 Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 21 | #include "mlir/Support/MathExtras.h" |
| 22 | #include "mlir/Support/STLExtras.h" |
| Stephan Herhut | b843cc5 | 2019-10-16 04:28:13 -0700 | [diff] [blame] | 23 | #include "mlir/Transforms/SideEffectsInterface.h" |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 24 | |
| 25 | using namespace mlir; |
| 26 | using namespace mlir::loop; |
| 27 | |
| 28 | //===----------------------------------------------------------------------===// |
| Stephan Herhut | b843cc5 | 2019-10-16 04:28:13 -0700 | [diff] [blame] | 29 | // LoopOpsDialect Interfaces |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | namespace { |
| 32 | |
| 33 | struct 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 Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 47 | // LoopOpsDialect |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | |
| 50 | LoopOpsDialect::LoopOpsDialect(MLIRContext *context) |
| 51 | : Dialect(getDialectNamespace(), context) { |
| 52 | addOperations< |
| 53 | #define GET_OP_LIST |
| 54 | #include "mlir/Dialect/LoopOps/LoopOps.cpp.inc" |
| 55 | >(); |
| Stephan Herhut | b843cc5 | 2019-10-16 04:28:13 -0700 | [diff] [blame] | 56 | addInterfaces<LoopSideEffectsInterface>(); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | // ForOp |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | |
| River Riddle | e62a695 | 2019-12-23 14:45:01 -0800 | [diff] [blame] | 63 | void ForOp::build(Builder *builder, OperationState &result, Value lb, Value ub, |
| 64 | Value step) { |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 65 | result.addOperands({lb, ub, step}); |
| 66 | Region *bodyRegion = result.addRegion(); |
| 67 | ForOp::ensureTerminator(*bodyRegion, *builder, result.location); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 68 | bodyRegion->front().addArgument(builder->getIndexType()); |
| 69 | } |
| 70 | |
| 71 | LogicalResult verify(ForOp op) { |
| River Riddle | 2bdf33c | 2020-01-11 08:54:04 -0800 | [diff] [blame] | 72 | if (auto cst = dyn_cast_or_null<ConstantIndexOp>(op.step().getDefiningOp())) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 73 | if (cst.getValue() <= 0) |
| Uday Bondhugula | ad6925f | 2019-10-22 07:34:24 -0700 | [diff] [blame] | 74 | return op.emitOpError("constant step operand must be positive"); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 75 | |
| 76 | // Check that the body defines as single block argument for the induction |
| 77 | // variable. |
| Nicolas Vasilache | 0002e29 | 2019-07-16 12:20:15 -0700 | [diff] [blame] | 78 | auto *body = op.getBody(); |
| River Riddle | 2bdf33c | 2020-01-11 08:54:04 -0800 | [diff] [blame] | 79 | if (body->getNumArguments() != 1 || !body->getArgument(0).getType().isIndex()) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 80 | return op.emitOpError("expected body to have a single index argument for " |
| 81 | "the induction variable"); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 82 | return success(); |
| 83 | } |
| 84 | |
| River Riddle | 3a643de | 2019-09-20 20:43:02 -0700 | [diff] [blame] | 85 | static void print(OpAsmPrinter &p, ForOp op) { |
| River Riddle | 2bdf33c | 2020-01-11 08:54:04 -0800 | [diff] [blame] | 86 | p << op.getOperationName() << " " << op.getInductionVar() << " = " |
| 87 | << op.lowerBound() << " to " << op.upperBound() << " step " << op.step(); |
| River Riddle | 3a643de | 2019-09-20 20:43:02 -0700 | [diff] [blame] | 88 | p.printRegion(op.region(), |
| 89 | /*printEntryBlockArgs=*/false, |
| 90 | /*printBlockTerminators=*/false); |
| 91 | p.printOptionalAttrDict(op.getAttrs()); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 94 | static ParseResult parseForOp(OpAsmParser &parser, OperationState &result) { |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 95 | auto &builder = parser.getBuilder(); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 96 | OpAsmParser::OperandType inductionVariable, lb, ub, step; |
| 97 | // Parse the induction variable followed by '='. |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 98 | if (parser.parseRegionArgument(inductionVariable) || parser.parseEqual()) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 99 | return failure(); |
| 100 | |
| 101 | // Parse loop bounds. |
| 102 | Type indexType = builder.getIndexType(); |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 103 | if (parser.parseOperand(lb) || |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 104 | parser.resolveOperand(lb, indexType, result.operands) || |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 105 | parser.parseKeyword("to") || parser.parseOperand(ub) || |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 106 | parser.resolveOperand(ub, indexType, result.operands) || |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 107 | parser.parseKeyword("step") || parser.parseOperand(step) || |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 108 | parser.resolveOperand(step, indexType, result.operands)) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 109 | return failure(); |
| 110 | |
| 111 | // Parse the body region. |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 112 | Region *body = result.addRegion(); |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 113 | if (parser.parseRegion(*body, inductionVariable, indexType)) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 114 | return failure(); |
| 115 | |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 116 | ForOp::ensureTerminator(*body, builder, result.location); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 117 | |
| 118 | // Parse the optional attribute list. |
| River Riddle | 8fa9d82 | 2019-11-05 13:32:07 -0800 | [diff] [blame] | 119 | if (parser.parseOptionalAttrDict(result.attributes)) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 120 | return failure(); |
| 121 | |
| 122 | return success(); |
| 123 | } |
| 124 | |
| Stephan Herhut | b843cc5 | 2019-10-16 04:28:13 -0700 | [diff] [blame] | 125 | Region &ForOp::getLoopBody() { return region(); } |
| 126 | |
| River Riddle | e62a695 | 2019-12-23 14:45:01 -0800 | [diff] [blame] | 127 | bool ForOp::isDefinedOutsideOfLoop(Value value) { |
| River Riddle | 2bdf33c | 2020-01-11 08:54:04 -0800 | [diff] [blame] | 128 | return !region().isAncestor(value.getParentRegion()); |
| Stephan Herhut | b843cc5 | 2019-10-16 04:28:13 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | LogicalResult ForOp::moveOutOfLoop(ArrayRef<Operation *> ops) { |
| River Riddle | 2bdf33c | 2020-01-11 08:54:04 -0800 | [diff] [blame] | 132 | for (auto op : ops) |
| Stephan Herhut | b843cc5 | 2019-10-16 04:28:13 -0700 | [diff] [blame] | 133 | op->moveBefore(this->getOperation()); |
| 134 | return success(); |
| 135 | } |
| 136 | |
| River Riddle | e62a695 | 2019-12-23 14:45:01 -0800 | [diff] [blame] | 137 | ForOp mlir::loop::getForInductionVarOwner(Value val) { |
| River Riddle | ab46543 | 2019-12-23 12:36:20 -0800 | [diff] [blame] | 138 | auto ivArg = val.dyn_cast<BlockArgument>(); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 139 | if (!ivArg) |
| 140 | return ForOp(); |
| River Riddle | 2bdf33c | 2020-01-11 08:54:04 -0800 | [diff] [blame] | 141 | assert(ivArg.getOwner() && "unlinked block argument"); |
| 142 | auto *containingInst = ivArg.getOwner()->getParentOp(); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 143 | return dyn_cast_or_null<ForOp>(containingInst); |
| 144 | } |
| 145 | |
| 146 | //===----------------------------------------------------------------------===// |
| 147 | // IfOp |
| 148 | //===----------------------------------------------------------------------===// |
| 149 | |
| River Riddle | e62a695 | 2019-12-23 14:45:01 -0800 | [diff] [blame] | 150 | void IfOp::build(Builder *builder, OperationState &result, Value cond, |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 151 | bool withElseRegion) { |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 152 | result.addOperands(cond); |
| 153 | Region *thenRegion = result.addRegion(); |
| 154 | Region *elseRegion = result.addRegion(); |
| 155 | IfOp::ensureTerminator(*thenRegion, *builder, result.location); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 156 | if (withElseRegion) |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 157 | IfOp::ensureTerminator(*elseRegion, *builder, result.location); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static LogicalResult verify(IfOp op) { |
| 161 | // Verify that the entry of each child region does not have arguments. |
| 162 | for (auto ®ion : op.getOperation()->getRegions()) { |
| 163 | if (region.empty()) |
| 164 | continue; |
| 165 | |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 166 | 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 Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 174 | static ParseResult parseIfOp(OpAsmParser &parser, OperationState &result) { |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 175 | // Create the regions for 'then'. |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 176 | result.regions.reserve(2); |
| 177 | Region *thenRegion = result.addRegion(); |
| 178 | Region *elseRegion = result.addRegion(); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 179 | |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 180 | auto &builder = parser.getBuilder(); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 181 | OpAsmParser::OperandType cond; |
| 182 | Type i1Type = builder.getIntegerType(1); |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 183 | if (parser.parseOperand(cond) || |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 184 | parser.resolveOperand(cond, i1Type, result.operands)) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 185 | return failure(); |
| 186 | |
| 187 | // Parse the 'then' region. |
| Adrian Kuegel | 018b042 | 2020-01-08 14:06:22 +0100 | [diff] [blame] | 188 | if (parser.parseRegion(*thenRegion, /*arguments=*/{}, /*argTypes=*/{})) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 189 | return failure(); |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 190 | IfOp::ensureTerminator(*thenRegion, parser.getBuilder(), result.location); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 191 | |
| 192 | // If we find an 'else' keyword then parse the 'else' region. |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 193 | if (!parser.parseOptionalKeyword("else")) { |
| Adrian Kuegel | 018b042 | 2020-01-08 14:06:22 +0100 | [diff] [blame] | 194 | if (parser.parseRegion(*elseRegion, /*arguments=*/{}, /*argTypes=*/{})) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 195 | return failure(); |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 196 | IfOp::ensureTerminator(*elseRegion, parser.getBuilder(), result.location); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // Parse the optional attribute list. |
| River Riddle | 8fa9d82 | 2019-11-05 13:32:07 -0800 | [diff] [blame] | 200 | if (parser.parseOptionalAttrDict(result.attributes)) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 201 | return failure(); |
| 202 | |
| 203 | return success(); |
| 204 | } |
| 205 | |
| River Riddle | 3a643de | 2019-09-20 20:43:02 -0700 | [diff] [blame] | 206 | static void print(OpAsmPrinter &p, IfOp op) { |
| River Riddle | 2bdf33c | 2020-01-11 08:54:04 -0800 | [diff] [blame] | 207 | p << IfOp::getOperationName() << " " << op.condition(); |
| River Riddle | 3a643de | 2019-09-20 20:43:02 -0700 | [diff] [blame] | 208 | p.printRegion(op.thenRegion(), |
| 209 | /*printEntryBlockArgs=*/false, |
| 210 | /*printBlockTerminators=*/false); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 211 | |
| 212 | // Print the 'else' regions if it exists and has a block. |
| 213 | auto &elseRegion = op.elseRegion(); |
| 214 | if (!elseRegion.empty()) { |
| River Riddle | 3a643de | 2019-09-20 20:43:02 -0700 | [diff] [blame] | 215 | p << " else"; |
| 216 | p.printRegion(elseRegion, |
| 217 | /*printEntryBlockArgs=*/false, |
| 218 | /*printBlockTerminators=*/false); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| River Riddle | 3a643de | 2019-09-20 20:43:02 -0700 | [diff] [blame] | 221 | p.printOptionalAttrDict(op.getAttrs()); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | //===----------------------------------------------------------------------===// |
| Adrian Kuegel | 018b042 | 2020-01-08 14:06:22 +0100 | [diff] [blame] | 225 | // ParallelOp |
| 226 | //===----------------------------------------------------------------------===// |
| 227 | |
| 228 | static 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 | |
| 274 | static 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 | |
| 323 | static 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 | |
| 338 | static 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 | })) |
| Adrian | 5a6eae3 | 2020-01-14 11:41:40 +0100 | [diff] [blame] | 348 | return op.emitOpError() |
| 349 | << "expects two arguments to reduce block of type " << type; |
| Adrian Kuegel | 018b042 | 2020-01-08 14:06:22 +0100 | [diff] [blame] | 350 | |
| 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 | |
| 359 | static 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 | |
| 380 | static 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 | |
| 390 | static 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 | |
| 401 | static 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 | |
| 412 | static void print(OpAsmPrinter &p, ReduceReturnOp op) { |
| 413 | p << op.getOperationName() << " " << op.result() << " : " |
| 414 | << op.result().getType(); |
| 415 | } |
| 416 | |
| 417 | //===----------------------------------------------------------------------===// |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 418 | // TableGen'd op method definitions |
| 419 | //===----------------------------------------------------------------------===// |
| 420 | |
| 421 | #define GET_OP_CLASSES |
| 422 | #include "mlir/Dialect/LoopOps/LoopOps.cpp.inc" |