| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 1 | //===- Ops.cpp - Loop MLIR Operations -------------------------------------===// |
| 2 | // |
| 3 | // Copyright 2019 The MLIR Authors. |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | // ============================================================================= |
| 17 | |
| 18 | #include "mlir/Dialect/LoopOps/LoopOps.h" |
| River Riddle | ba0fa92 | 2019-08-19 11:00:47 -0700 | [diff] [blame] | 19 | #include "mlir/Dialect/StandardOps/Ops.h" |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 20 | #include "mlir/IR/AffineExpr.h" |
| 21 | #include "mlir/IR/AffineMap.h" |
| 22 | #include "mlir/IR/Builders.h" |
| 23 | #include "mlir/IR/Function.h" |
| 24 | #include "mlir/IR/Matchers.h" |
| 25 | #include "mlir/IR/Module.h" |
| 26 | #include "mlir/IR/OpImplementation.h" |
| 27 | #include "mlir/IR/PatternMatch.h" |
| 28 | #include "mlir/IR/StandardTypes.h" |
| 29 | #include "mlir/IR/Value.h" |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 30 | #include "mlir/Support/MathExtras.h" |
| 31 | #include "mlir/Support/STLExtras.h" |
| Stephan Herhut | b843cc5 | 2019-10-16 04:28:13 -0700 | [diff] [blame^] | 32 | #include "mlir/Transforms/SideEffectsInterface.h" |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 33 | |
| 34 | using namespace mlir; |
| 35 | using namespace mlir::loop; |
| 36 | |
| 37 | //===----------------------------------------------------------------------===// |
| Stephan Herhut | b843cc5 | 2019-10-16 04:28:13 -0700 | [diff] [blame^] | 38 | // LoopOpsDialect Interfaces |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | namespace { |
| 41 | |
| 42 | struct LoopSideEffectsInterface : public SideEffectsDialectInterface { |
| 43 | using SideEffectsDialectInterface::SideEffectsDialectInterface; |
| 44 | |
| 45 | SideEffecting isSideEffecting(Operation *op) const override { |
| 46 | if (isa<IfOp>(op) || isa<ForOp>(op)) { |
| 47 | return Recursive; |
| 48 | } |
| 49 | return SideEffectsDialectInterface::isSideEffecting(op); |
| 50 | }; |
| 51 | }; |
| 52 | |
| 53 | } // namespace |
| 54 | |
| 55 | //===----------------------------------------------------------------------===// |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 56 | // LoopOpsDialect |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | |
| 59 | LoopOpsDialect::LoopOpsDialect(MLIRContext *context) |
| 60 | : Dialect(getDialectNamespace(), context) { |
| 61 | addOperations< |
| 62 | #define GET_OP_LIST |
| 63 | #include "mlir/Dialect/LoopOps/LoopOps.cpp.inc" |
| 64 | >(); |
| Stephan Herhut | b843cc5 | 2019-10-16 04:28:13 -0700 | [diff] [blame^] | 65 | addInterfaces<LoopSideEffectsInterface>(); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | //===----------------------------------------------------------------------===// |
| 69 | // ForOp |
| 70 | //===----------------------------------------------------------------------===// |
| 71 | |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 72 | void ForOp::build(Builder *builder, OperationState &result, Value *lb, |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 73 | Value *ub, Value *step) { |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 74 | result.addOperands({lb, ub, step}); |
| 75 | Region *bodyRegion = result.addRegion(); |
| 76 | ForOp::ensureTerminator(*bodyRegion, *builder, result.location); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 77 | bodyRegion->front().addArgument(builder->getIndexType()); |
| 78 | } |
| 79 | |
| 80 | LogicalResult verify(ForOp op) { |
| 81 | if (auto cst = dyn_cast_or_null<ConstantIndexOp>(op.step()->getDefiningOp())) |
| 82 | if (cst.getValue() <= 0) |
| 83 | return op.emitOpError("constant step operand must be nonnegative"); |
| 84 | |
| 85 | // Check that the body defines as single block argument for the induction |
| 86 | // variable. |
| Nicolas Vasilache | 0002e29 | 2019-07-16 12:20:15 -0700 | [diff] [blame] | 87 | auto *body = op.getBody(); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 88 | if (body->getNumArguments() != 1 || |
| 89 | !body->getArgument(0)->getType().isIndex()) |
| 90 | return op.emitOpError("expected body to have a single index argument for " |
| 91 | "the induction variable"); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 92 | return success(); |
| 93 | } |
| 94 | |
| River Riddle | 3a643de | 2019-09-20 20:43:02 -0700 | [diff] [blame] | 95 | static void print(OpAsmPrinter &p, ForOp op) { |
| 96 | p << op.getOperationName() << " " << *op.getInductionVar() << " = " |
| 97 | << *op.lowerBound() << " to " << *op.upperBound() << " step " << *op.step(); |
| 98 | p.printRegion(op.region(), |
| 99 | /*printEntryBlockArgs=*/false, |
| 100 | /*printBlockTerminators=*/false); |
| 101 | p.printOptionalAttrDict(op.getAttrs()); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 104 | static ParseResult parseForOp(OpAsmParser &parser, OperationState &result) { |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 105 | auto &builder = parser.getBuilder(); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 106 | OpAsmParser::OperandType inductionVariable, lb, ub, step; |
| 107 | // Parse the induction variable followed by '='. |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 108 | if (parser.parseRegionArgument(inductionVariable) || parser.parseEqual()) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 109 | return failure(); |
| 110 | |
| 111 | // Parse loop bounds. |
| 112 | Type indexType = builder.getIndexType(); |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 113 | if (parser.parseOperand(lb) || |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 114 | parser.resolveOperand(lb, indexType, result.operands) || |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 115 | parser.parseKeyword("to") || parser.parseOperand(ub) || |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 116 | parser.resolveOperand(ub, indexType, result.operands) || |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 117 | parser.parseKeyword("step") || parser.parseOperand(step) || |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 118 | parser.resolveOperand(step, indexType, result.operands)) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 119 | return failure(); |
| 120 | |
| 121 | // Parse the body region. |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 122 | Region *body = result.addRegion(); |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 123 | if (parser.parseRegion(*body, inductionVariable, indexType)) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 124 | return failure(); |
| 125 | |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 126 | ForOp::ensureTerminator(*body, builder, result.location); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 127 | |
| 128 | // Parse the optional attribute list. |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 129 | if (parser.parseOptionalAttributeDict(result.attributes)) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 130 | return failure(); |
| 131 | |
| 132 | return success(); |
| 133 | } |
| 134 | |
| Stephan Herhut | b843cc5 | 2019-10-16 04:28:13 -0700 | [diff] [blame^] | 135 | Region &ForOp::getLoopBody() { return region(); } |
| 136 | |
| 137 | bool ForOp::isDefinedOutsideOfLoop(Value *value) { |
| 138 | return !region().isAncestor(value->getParentRegion()); |
| 139 | } |
| 140 | |
| 141 | LogicalResult ForOp::moveOutOfLoop(ArrayRef<Operation *> ops) { |
| 142 | for (auto *op : ops) |
| 143 | op->moveBefore(this->getOperation()); |
| 144 | return success(); |
| 145 | } |
| 146 | |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 147 | ForOp mlir::loop::getForInductionVarOwner(Value *val) { |
| 148 | auto *ivArg = dyn_cast<BlockArgument>(val); |
| 149 | if (!ivArg) |
| 150 | return ForOp(); |
| 151 | assert(ivArg->getOwner() && "unlinked block argument"); |
| River Riddle | 1e42954 | 2019-08-09 20:07:25 -0700 | [diff] [blame] | 152 | auto *containingInst = ivArg->getOwner()->getParentOp(); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 153 | return dyn_cast_or_null<ForOp>(containingInst); |
| 154 | } |
| 155 | |
| 156 | //===----------------------------------------------------------------------===// |
| 157 | // IfOp |
| 158 | //===----------------------------------------------------------------------===// |
| 159 | |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 160 | void IfOp::build(Builder *builder, OperationState &result, Value *cond, |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 161 | bool withElseRegion) { |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 162 | result.addOperands(cond); |
| 163 | Region *thenRegion = result.addRegion(); |
| 164 | Region *elseRegion = result.addRegion(); |
| 165 | IfOp::ensureTerminator(*thenRegion, *builder, result.location); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 166 | if (withElseRegion) |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 167 | IfOp::ensureTerminator(*elseRegion, *builder, result.location); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static LogicalResult verify(IfOp op) { |
| 171 | // Verify that the entry of each child region does not have arguments. |
| 172 | for (auto ®ion : op.getOperation()->getRegions()) { |
| 173 | if (region.empty()) |
| 174 | continue; |
| 175 | |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 176 | for (auto &b : region) |
| 177 | if (b.getNumArguments() != 0) |
| 178 | return op.emitOpError( |
| 179 | "requires that child entry blocks have no arguments"); |
| 180 | } |
| 181 | return success(); |
| 182 | } |
| 183 | |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 184 | static ParseResult parseIfOp(OpAsmParser &parser, OperationState &result) { |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 185 | // Create the regions for 'then'. |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 186 | result.regions.reserve(2); |
| 187 | Region *thenRegion = result.addRegion(); |
| 188 | Region *elseRegion = result.addRegion(); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 189 | |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 190 | auto &builder = parser.getBuilder(); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 191 | OpAsmParser::OperandType cond; |
| 192 | Type i1Type = builder.getIntegerType(1); |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 193 | if (parser.parseOperand(cond) || |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 194 | parser.resolveOperand(cond, i1Type, result.operands)) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 195 | return failure(); |
| 196 | |
| 197 | // Parse the 'then' region. |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 198 | if (parser.parseRegion(*thenRegion, {}, {})) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 199 | return failure(); |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 200 | IfOp::ensureTerminator(*thenRegion, parser.getBuilder(), result.location); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 201 | |
| 202 | // If we find an 'else' keyword then parse the 'else' region. |
| River Riddle | 2797517 | 2019-09-20 11:36:49 -0700 | [diff] [blame] | 203 | if (!parser.parseOptionalKeyword("else")) { |
| 204 | if (parser.parseRegion(*elseRegion, {}, {})) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 205 | return failure(); |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 206 | IfOp::ensureTerminator(*elseRegion, parser.getBuilder(), result.location); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // Parse the optional attribute list. |
| River Riddle | 729727e | 2019-09-20 19:47:05 -0700 | [diff] [blame] | 210 | if (parser.parseOptionalAttributeDict(result.attributes)) |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 211 | return failure(); |
| 212 | |
| 213 | return success(); |
| 214 | } |
| 215 | |
| River Riddle | 3a643de | 2019-09-20 20:43:02 -0700 | [diff] [blame] | 216 | static void print(OpAsmPrinter &p, IfOp op) { |
| 217 | p << IfOp::getOperationName() << " " << *op.condition(); |
| 218 | p.printRegion(op.thenRegion(), |
| 219 | /*printEntryBlockArgs=*/false, |
| 220 | /*printBlockTerminators=*/false); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 221 | |
| 222 | // Print the 'else' regions if it exists and has a block. |
| 223 | auto &elseRegion = op.elseRegion(); |
| 224 | if (!elseRegion.empty()) { |
| River Riddle | 3a643de | 2019-09-20 20:43:02 -0700 | [diff] [blame] | 225 | p << " else"; |
| 226 | p.printRegion(elseRegion, |
| 227 | /*printEntryBlockArgs=*/false, |
| 228 | /*printBlockTerminators=*/false); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| River Riddle | 3a643de | 2019-09-20 20:43:02 -0700 | [diff] [blame] | 231 | p.printOptionalAttrDict(op.getAttrs()); |
| Nicolas Vasilache | cca53e8 | 2019-07-15 02:50:09 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | //===----------------------------------------------------------------------===// |
| 235 | // TableGen'd op method definitions |
| 236 | //===----------------------------------------------------------------------===// |
| 237 | |
| 238 | #define GET_OP_CLASSES |
| 239 | #include "mlir/Dialect/LoopOps/LoopOps.cpp.inc" |