| 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 | 35807bc | 2019-12-22 21:59:55 -0800 | [diff] [blame] | 63 | void ForOp::build(Builder *builder, OperationState &result, ValuePtr lb, |
| 64 | ValuePtr ub, ValuePtr 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) { |
| 72 | if (auto cst = dyn_cast_or_null<ConstantIndexOp>(op.step()->getDefiningOp())) |
| 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(); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 79 | 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 Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 83 | return success(); |
| 84 | } |
| 85 | |
| River Riddle | 3a643de | 2019-09-20 20:43:02 -0700 | [diff] [blame] | 86 | static 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 Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 95 | static ParseResult parseForOp(OpAsmParser &parser, OperationState &result) { |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 96 | auto &builder = parser.getBuilder(); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 97 | OpAsmParser::OperandType inductionVariable, lb, ub, step; |
| 98 | // Parse the induction variable followed by '='. |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 99 | if (parser.parseRegionArgument(inductionVariable) || parser.parseEqual()) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 100 | return failure(); |
| 101 | |
| 102 | // Parse loop bounds. |
| 103 | Type indexType = builder.getIndexType(); |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 104 | if (parser.parseOperand(lb) || |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 105 | parser.resolveOperand(lb, indexType, result.operands) || |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 106 | parser.parseKeyword("to") || parser.parseOperand(ub) || |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 107 | parser.resolveOperand(ub, indexType, result.operands) || |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 108 | parser.parseKeyword("step") || parser.parseOperand(step) || |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 109 | parser.resolveOperand(step, indexType, result.operands)) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 110 | return failure(); |
| 111 | |
| 112 | // Parse the body region. |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 113 | Region *body = result.addRegion(); |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 114 | if (parser.parseRegion(*body, inductionVariable, indexType)) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 115 | return failure(); |
| 116 | |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 117 | ForOp::ensureTerminator(*body, builder, result.location); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 118 | |
| 119 | // Parse the optional attribute list. |
| River Riddle | 8fa9d82 | 2019-11-05 13:32:07 -0800 | [diff] [blame] | 120 | if (parser.parseOptionalAttrDict(result.attributes)) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 121 | return failure(); |
| 122 | |
| 123 | return success(); |
| 124 | } |
| 125 | |
| Stephan Herhut | b843cc5 | 2019-10-16 04:28:13 -0700 | [diff] [blame] | 126 | Region &ForOp::getLoopBody() { return region(); } |
| 127 | |
| River Riddle | 35807bc | 2019-12-22 21:59:55 -0800 | [diff] [blame] | 128 | bool ForOp::isDefinedOutsideOfLoop(ValuePtr value) { |
| Stephan Herhut | b843cc5 | 2019-10-16 04:28:13 -0700 | [diff] [blame] | 129 | return !region().isAncestor(value->getParentRegion()); |
| 130 | } |
| 131 | |
| 132 | LogicalResult ForOp::moveOutOfLoop(ArrayRef<Operation *> ops) { |
| 133 | for (auto *op : ops) |
| 134 | op->moveBefore(this->getOperation()); |
| 135 | return success(); |
| 136 | } |
| 137 | |
| River Riddle | 35807bc | 2019-12-22 21:59:55 -0800 | [diff] [blame] | 138 | ForOp mlir::loop::getForInductionVarOwner(ValuePtr val) { |
| River Riddle | ab46543 | 2019-12-23 12:36:20 -0800 | [diff] [blame^] | 139 | auto ivArg = val.dyn_cast<BlockArgument>(); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 140 | if (!ivArg) |
| 141 | return ForOp(); |
| 142 | assert(ivArg->getOwner() && "unlinked block argument"); |
| River Riddle | 1e42954 | 2019-08-09 20:07:25 -0700 | [diff] [blame] | 143 | auto *containingInst = ivArg->getOwner()->getParentOp(); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 144 | return dyn_cast_or_null<ForOp>(containingInst); |
| 145 | } |
| 146 | |
| 147 | //===----------------------------------------------------------------------===// |
| 148 | // IfOp |
| 149 | //===----------------------------------------------------------------------===// |
| 150 | |
| River Riddle | 35807bc | 2019-12-22 21:59:55 -0800 | [diff] [blame] | 151 | void IfOp::build(Builder *builder, OperationState &result, ValuePtr cond, |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 152 | bool withElseRegion) { |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 153 | result.addOperands(cond); |
| 154 | Region *thenRegion = result.addRegion(); |
| 155 | Region *elseRegion = result.addRegion(); |
| 156 | IfOp::ensureTerminator(*thenRegion, *builder, result.location); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 157 | if (withElseRegion) |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 158 | IfOp::ensureTerminator(*elseRegion, *builder, result.location); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static LogicalResult verify(IfOp op) { |
| 162 | // Verify that the entry of each child region does not have arguments. |
| 163 | for (auto ®ion : op.getOperation()->getRegions()) { |
| 164 | if (region.empty()) |
| 165 | continue; |
| 166 | |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 167 | 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 Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 175 | static ParseResult parseIfOp(OpAsmParser &parser, OperationState &result) { |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 176 | // Create the regions for 'then'. |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 177 | result.regions.reserve(2); |
| 178 | Region *thenRegion = result.addRegion(); |
| 179 | Region *elseRegion = result.addRegion(); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 180 | |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 181 | auto &builder = parser.getBuilder(); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 182 | OpAsmParser::OperandType cond; |
| 183 | Type i1Type = builder.getIntegerType(1); |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 184 | if (parser.parseOperand(cond) || |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 185 | parser.resolveOperand(cond, i1Type, result.operands)) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 186 | return failure(); |
| 187 | |
| 188 | // Parse the 'then' region. |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 189 | if (parser.parseRegion(*thenRegion, {}, {})) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 190 | return failure(); |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 191 | IfOp::ensureTerminator(*thenRegion, parser.getBuilder(), result.location); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 192 | |
| 193 | // If we find an 'else' keyword then parse the 'else' region. |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 194 | if (!parser.parseOptionalKeyword("else")) { |
| 195 | if (parser.parseRegion(*elseRegion, {}, {})) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 196 | return failure(); |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 197 | IfOp::ensureTerminator(*elseRegion, parser.getBuilder(), result.location); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // Parse the optional attribute list. |
| River Riddle | 8fa9d82 | 2019-11-05 13:32:07 -0800 | [diff] [blame] | 201 | if (parser.parseOptionalAttrDict(result.attributes)) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 202 | return failure(); |
| 203 | |
| 204 | return success(); |
| 205 | } |
| 206 | |
| River Riddle | 3a643de | 2019-09-20 20:43:02 -0700 | [diff] [blame] | 207 | static void print(OpAsmPrinter &p, IfOp op) { |
| 208 | p << IfOp::getOperationName() << " " << *op.condition(); |
| 209 | p.printRegion(op.thenRegion(), |
| 210 | /*printEntryBlockArgs=*/false, |
| 211 | /*printBlockTerminators=*/false); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 212 | |
| 213 | // Print the 'else' regions if it exists and has a block. |
| 214 | auto &elseRegion = op.elseRegion(); |
| 215 | if (!elseRegion.empty()) { |
| River Riddle | 3a643de | 2019-09-20 20:43:02 -0700 | [diff] [blame] | 216 | p << " else"; |
| 217 | p.printRegion(elseRegion, |
| 218 | /*printEntryBlockArgs=*/false, |
| 219 | /*printBlockTerminators=*/false); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| River Riddle | 3a643de | 2019-09-20 20:43:02 -0700 | [diff] [blame] | 222 | p.printOptionalAttrDict(op.getAttrs()); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | //===----------------------------------------------------------------------===// |
| 226 | // TableGen'd op method definitions |
| 227 | //===----------------------------------------------------------------------===// |
| 228 | |
| 229 | #define GET_OP_CLASSES |
| 230 | #include "mlir/Dialect/LoopOps/LoopOps.cpp.inc" |