| 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. |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 188 | if (parser.parseRegion(*thenRegion, {}, {})) |
| 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")) { |
| 194 | if (parser.parseRegion(*elseRegion, {}, {})) |
| 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 | //===----------------------------------------------------------------------===// |
| 225 | // TableGen'd op method definitions |
| 226 | //===----------------------------------------------------------------------===// |
| 227 | |
| 228 | #define GET_OP_CLASSES |
| 229 | #include "mlir/Dialect/LoopOps/LoopOps.cpp.inc" |