Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 1 | //===- Statement.cpp - MLIR Statement Classes ----------------------------===// |
| 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 | |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 18 | #include "mlir/IR/AffineMap.h" |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 19 | #include "mlir/IR/MLFunction.h" |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 20 | #include "mlir/IR/MLIRContext.h" |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 21 | #include "mlir/IR/StandardOps.h" |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 22 | #include "mlir/IR/Statements.h" |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 23 | #include "mlir/IR/StmtVisitor.h" |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 24 | #include "llvm/ADT/DenseMap.h" |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 25 | |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 26 | using namespace mlir; |
| 27 | |
| 28 | //===----------------------------------------------------------------------===// |
Tatiana Shpeisman | 60bf7be | 2018-07-26 18:09:20 -0700 | [diff] [blame] | 29 | // StmtResult |
| 30 | //===------------------------------------------------------------------===// |
| 31 | |
| 32 | /// Return the result number of this result. |
| 33 | unsigned StmtResult::getResultNumber() const { |
| 34 | // Results are always stored consecutively, so use pointer subtraction to |
| 35 | // figure out what number this is. |
| 36 | return this - &getOwner()->getStmtResults()[0]; |
| 37 | } |
| 38 | |
| 39 | //===----------------------------------------------------------------------===// |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 40 | // Statement |
| 41 | //===------------------------------------------------------------------===// |
| 42 | |
| 43 | // Statements are deleted through the destroy() member because we don't have |
| 44 | // a virtual destructor. |
| 45 | Statement::~Statement() { |
| 46 | assert(block == nullptr && "statement destroyed but still in a block"); |
| 47 | } |
| 48 | |
| 49 | /// Destroy this statement or one of its subclasses. |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 50 | void Statement::destroy() { |
| 51 | switch (this->getKind()) { |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 52 | case Kind::Operation: |
Tatiana Shpeisman | 60bf7be | 2018-07-26 18:09:20 -0700 | [diff] [blame] | 53 | cast<OperationStmt>(this)->destroy(); |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 54 | break; |
| 55 | case Kind::For: |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 56 | delete cast<ForStmt>(this); |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 57 | break; |
| 58 | case Kind::If: |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 59 | delete cast<IfStmt>(this); |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 60 | break; |
| 61 | } |
| 62 | } |
| 63 | |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 64 | /// Return the context this operation is associated with. |
| 65 | MLIRContext *Statement::getContext() const { |
| 66 | // Work a bit to avoid calling findFunction() and getting its context. |
| 67 | switch (getKind()) { |
| 68 | case Kind::Operation: |
| 69 | return cast<OperationStmt>(this)->getContext(); |
| 70 | case Kind::For: |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 71 | return cast<ForStmt>(this)->getContext(); |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 72 | case Kind::If: |
| 73 | // TODO(shpeisman): When if statement has value operands, we can get a |
| 74 | // context from their type. |
| 75 | return findFunction()->getContext(); |
| 76 | } |
| 77 | } |
| 78 | |
Tatiana Shpeisman | c335d18 | 2018-08-03 11:12:34 -0700 | [diff] [blame] | 79 | Statement *Statement::getParentStmt() const { |
| 80 | return block ? block->getParentStmt() : nullptr; |
| 81 | } |
Tatiana Shpeisman | d880b35 | 2018-07-31 23:14:16 -0700 | [diff] [blame] | 82 | |
| 83 | MLFunction *Statement::findFunction() const { |
Tatiana Shpeisman | c335d18 | 2018-08-03 11:12:34 -0700 | [diff] [blame] | 84 | return block ? block->findFunction() : nullptr; |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 87 | bool Statement::isInnermost() const { |
| 88 | struct NestedLoopCounter : public StmtWalker<NestedLoopCounter> { |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 89 | unsigned numNestedLoops; |
| 90 | NestedLoopCounter() : numNestedLoops(0) {} |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 91 | void walkForStmt(const ForStmt *fs) { numNestedLoops++; } |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | NestedLoopCounter nlc; |
Uday Bondhugula | 081d9e7 | 2018-07-27 10:58:14 -0700 | [diff] [blame] | 95 | nlc.walk(const_cast<Statement *>(this)); |
| 96 | return nlc.numNestedLoops == 1; |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 99 | MLValue *Statement::getOperand(unsigned idx) { |
| 100 | return getStmtOperand(idx).get(); |
| 101 | } |
| 102 | |
| 103 | const MLValue *Statement::getOperand(unsigned idx) const { |
| 104 | return getStmtOperand(idx).get(); |
| 105 | } |
| 106 | |
| 107 | void Statement::setOperand(unsigned idx, MLValue *value) { |
| 108 | getStmtOperand(idx).set(value); |
| 109 | } |
| 110 | |
| 111 | unsigned Statement::getNumOperands() const { |
| 112 | switch (getKind()) { |
| 113 | case Kind::Operation: |
| 114 | return cast<OperationStmt>(this)->getNumOperands(); |
| 115 | case Kind::For: |
| 116 | return cast<ForStmt>(this)->getNumOperands(); |
| 117 | case Kind::If: |
| 118 | // TODO: query IfStmt once it has operands. |
| 119 | return 0; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | MutableArrayRef<StmtOperand> Statement::getStmtOperands() { |
| 124 | switch (getKind()) { |
| 125 | case Kind::Operation: |
| 126 | return cast<OperationStmt>(this)->getStmtOperands(); |
| 127 | case Kind::For: |
| 128 | return cast<ForStmt>(this)->getStmtOperands(); |
| 129 | case Kind::If: |
| 130 | // TODO: query IfStmt once it has operands. |
| 131 | return {}; |
| 132 | } |
| 133 | } |
| 134 | |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 135 | /// Emit a note about this statement, reporting up to any diagnostic |
| 136 | /// handlers that may be listening. |
| 137 | void Statement::emitNote(const Twine &message) const { |
| 138 | getContext()->emitDiagnostic(getLoc(), message, |
| 139 | MLIRContext::DiagnosticKind::Note); |
| 140 | } |
| 141 | |
| 142 | /// Emit a warning about this statement, reporting up to any diagnostic |
| 143 | /// handlers that may be listening. |
| 144 | void Statement::emitWarning(const Twine &message) const { |
| 145 | getContext()->emitDiagnostic(getLoc(), message, |
| 146 | MLIRContext::DiagnosticKind::Warning); |
| 147 | } |
| 148 | |
| 149 | /// Emit an error about fatal conditions with this statement, reporting up to |
| 150 | /// any diagnostic handlers that may be listening. NOTE: This may terminate |
| 151 | /// the containing application, only use when the IR is in an inconsistent |
| 152 | /// state. |
| 153 | void Statement::emitError(const Twine &message) const { |
| 154 | getContext()->emitDiagnostic(getLoc(), message, |
| 155 | MLIRContext::DiagnosticKind::Error); |
| 156 | } |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 157 | //===----------------------------------------------------------------------===// |
| 158 | // ilist_traits for Statement |
| 159 | //===----------------------------------------------------------------------===// |
| 160 | |
| 161 | StmtBlock *llvm::ilist_traits<::mlir::Statement>::getContainingBlock() { |
| 162 | size_t Offset( |
| 163 | size_t(&((StmtBlock *)nullptr->*StmtBlock::getSublistAccess(nullptr)))); |
| 164 | iplist<Statement> *Anchor(static_cast<iplist<Statement> *>(this)); |
| 165 | return reinterpret_cast<StmtBlock *>(reinterpret_cast<char *>(Anchor) - |
| 166 | Offset); |
| 167 | } |
| 168 | |
| 169 | /// This is a trait method invoked when a statement is added to a block. We |
| 170 | /// keep the block pointer up to date. |
| 171 | void llvm::ilist_traits<::mlir::Statement>::addNodeToList(Statement *stmt) { |
| 172 | assert(!stmt->getBlock() && "already in a statement block!"); |
| 173 | stmt->block = getContainingBlock(); |
| 174 | } |
| 175 | |
| 176 | /// This is a trait method invoked when a statement is removed from a block. |
| 177 | /// We keep the block pointer up to date. |
| 178 | void llvm::ilist_traits<::mlir::Statement>::removeNodeFromList( |
| 179 | Statement *stmt) { |
| 180 | assert(stmt->block && "not already in a statement block!"); |
| 181 | stmt->block = nullptr; |
| 182 | } |
| 183 | |
| 184 | /// This is a trait method invoked when a statement is moved from one block |
| 185 | /// to another. We keep the block pointer up to date. |
| 186 | void llvm::ilist_traits<::mlir::Statement>::transferNodesFromList( |
| 187 | ilist_traits<Statement> &otherList, stmt_iterator first, |
| 188 | stmt_iterator last) { |
| 189 | // If we are transferring statements within the same block, the block |
| 190 | // pointer doesn't need to be updated. |
| 191 | StmtBlock *curParent = getContainingBlock(); |
| 192 | if (curParent == otherList.getContainingBlock()) |
| 193 | return; |
| 194 | |
| 195 | // Update the 'block' member of each statement. |
| 196 | for (; first != last; ++first) |
| 197 | first->block = curParent; |
| 198 | } |
| 199 | |
Uday Bondhugula | 0b4059b | 2018-07-24 20:01:16 -0700 | [diff] [blame] | 200 | /// Remove this statement (and its descendants) from its StmtBlock and delete |
| 201 | /// all of them. |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 202 | void Statement::eraseFromBlock() { |
| 203 | assert(getBlock() && "Statement has no block"); |
| 204 | getBlock()->getStatements().erase(this); |
| 205 | } |
| 206 | |
| 207 | //===----------------------------------------------------------------------===// |
Tatiana Shpeisman | 60bf7be | 2018-07-26 18:09:20 -0700 | [diff] [blame] | 208 | // OperationStmt |
| 209 | //===----------------------------------------------------------------------===// |
| 210 | |
| 211 | /// Create a new OperationStmt with the specific fields. |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame^] | 212 | OperationStmt *OperationStmt::create(Location *location, Identifier name, |
Tatiana Shpeisman | 60bf7be | 2018-07-26 18:09:20 -0700 | [diff] [blame] | 213 | ArrayRef<MLValue *> operands, |
| 214 | ArrayRef<Type *> resultTypes, |
| 215 | ArrayRef<NamedAttribute> attributes, |
| 216 | MLIRContext *context) { |
| 217 | auto byteSize = totalSizeToAlloc<StmtOperand, StmtResult>(operands.size(), |
| 218 | resultTypes.size()); |
| 219 | void *rawMem = malloc(byteSize); |
| 220 | |
| 221 | // Initialize the OperationStmt part of the statement. |
| 222 | auto stmt = ::new (rawMem) OperationStmt( |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 223 | location, name, operands.size(), resultTypes.size(), attributes, context); |
Tatiana Shpeisman | 60bf7be | 2018-07-26 18:09:20 -0700 | [diff] [blame] | 224 | |
| 225 | // Initialize the operands and results. |
| 226 | auto stmtOperands = stmt->getStmtOperands(); |
| 227 | for (unsigned i = 0, e = operands.size(); i != e; ++i) |
| 228 | new (&stmtOperands[i]) StmtOperand(stmt, operands[i]); |
| 229 | |
| 230 | auto stmtResults = stmt->getStmtResults(); |
| 231 | for (unsigned i = 0, e = resultTypes.size(); i != e; ++i) |
| 232 | new (&stmtResults[i]) StmtResult(resultTypes[i], stmt); |
| 233 | return stmt; |
| 234 | } |
| 235 | |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame^] | 236 | OperationStmt::OperationStmt(Location *location, Identifier name, |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 237 | unsigned numOperands, unsigned numResults, |
Tatiana Shpeisman | 60bf7be | 2018-07-26 18:09:20 -0700 | [diff] [blame] | 238 | ArrayRef<NamedAttribute> attributes, |
| 239 | MLIRContext *context) |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 240 | : Operation(/*isInstruction=*/false, name, attributes, context), |
| 241 | Statement(Kind::Operation, location), numOperands(numOperands), |
Tatiana Shpeisman | 60bf7be | 2018-07-26 18:09:20 -0700 | [diff] [blame] | 242 | numResults(numResults) {} |
| 243 | |
| 244 | OperationStmt::~OperationStmt() { |
| 245 | // Explicitly run the destructors for the operands and results. |
| 246 | for (auto &operand : getStmtOperands()) |
| 247 | operand.~StmtOperand(); |
| 248 | |
| 249 | for (auto &result : getStmtResults()) |
| 250 | result.~StmtResult(); |
| 251 | } |
| 252 | |
| 253 | void OperationStmt::destroy() { |
| 254 | this->~OperationStmt(); |
| 255 | free(this); |
| 256 | } |
| 257 | |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 258 | /// Return the context this operation is associated with. |
| 259 | MLIRContext *OperationStmt::getContext() const { |
| 260 | // If we have a result or operand type, that is a constant time way to get |
| 261 | // to the context. |
| 262 | if (getNumResults()) |
| 263 | return getResult(0)->getType()->getContext(); |
| 264 | if (getNumOperands()) |
| 265 | return getOperand(0)->getType()->getContext(); |
| 266 | |
| 267 | // In the very odd case where we have no operands or results, fall back to |
| 268 | // doing a find. |
| 269 | return findFunction()->getContext(); |
| 270 | } |
| 271 | |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 272 | bool OperationStmt::isReturn() const { return is<ReturnOp>(); } |
| 273 | |
Tatiana Shpeisman | 60bf7be | 2018-07-26 18:09:20 -0700 | [diff] [blame] | 274 | //===----------------------------------------------------------------------===// |
Tatiana Shpeisman | 3838db7 | 2018-07-30 15:18:10 -0700 | [diff] [blame] | 275 | // ForStmt |
| 276 | //===----------------------------------------------------------------------===// |
| 277 | |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame^] | 278 | ForStmt *ForStmt::create(Location *location, ArrayRef<MLValue *> lbOperands, |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 279 | AffineMap *lbMap, ArrayRef<MLValue *> ubOperands, |
| 280 | AffineMap *ubMap, int64_t step, MLIRContext *context) { |
| 281 | assert(lbOperands.size() == lbMap->getNumOperands() && |
| 282 | "lower bound operand count does not match the affine map"); |
| 283 | assert(ubOperands.size() == ubMap->getNumOperands() && |
| 284 | "upper bound operand count does not match the affine map"); |
| 285 | |
| 286 | unsigned numOperands = lbOperands.size() + ubOperands.size(); |
| 287 | ForStmt *stmt = |
| 288 | new ForStmt(location, numOperands, lbMap, ubMap, step, context); |
| 289 | |
| 290 | unsigned i = 0; |
| 291 | for (unsigned e = lbOperands.size(); i != e; ++i) |
| 292 | stmt->operands.emplace_back(StmtOperand(stmt, lbOperands[i])); |
| 293 | |
| 294 | for (unsigned j = 0, e = ubOperands.size(); j != e; ++i, ++j) |
| 295 | stmt->operands.emplace_back(StmtOperand(stmt, ubOperands[j])); |
| 296 | |
| 297 | return stmt; |
| 298 | } |
| 299 | |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame^] | 300 | ForStmt::ForStmt(Location *location, unsigned numOperands, AffineMap *lbMap, |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 301 | AffineMap *ubMap, int64_t step, MLIRContext *context) |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 302 | : Statement(Kind::For, location), |
Tatiana Shpeisman | c9c4b34 | 2018-07-31 07:40:14 -0700 | [diff] [blame] | 303 | MLValue(MLValueKind::ForStmt, Type::getAffineInt(context)), |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 304 | StmtBlock(StmtBlockKind::For), lbMap(lbMap), ubMap(ubMap), step(step) {} |
| 305 | |
| 306 | const AffineBound ForStmt::getLowerBound() const { |
| 307 | return AffineBound(*this, 0, lbMap->getNumOperands(), lbMap); |
| 308 | } |
| 309 | |
| 310 | const AffineBound ForStmt::getUpperBound() const { |
| 311 | return AffineBound(*this, lbMap->getNumOperands(), getNumOperands(), ubMap); |
| 312 | } |
| 313 | |
| 314 | void ForStmt::setLowerBound(ArrayRef<MLValue *> operands, AffineMap *map) { |
| 315 | // TODO: handle the case when number of existing or new operands is non-zero. |
| 316 | assert(getNumOperands() == 0 && operands.empty()); |
| 317 | |
| 318 | this->lbMap = map; |
| 319 | } |
| 320 | |
| 321 | void ForStmt::setUpperBound(ArrayRef<MLValue *> operands, AffineMap *map) { |
| 322 | // TODO: handle the case when number of existing or new operands is non-zero. |
| 323 | assert(getNumOperands() == 0 && operands.empty()); |
| 324 | |
| 325 | this->ubMap = map; |
| 326 | } |
| 327 | |
| 328 | bool ForStmt::hasConstantLowerBound() const { |
| 329 | return lbMap->isSingleConstant(); |
| 330 | } |
| 331 | |
| 332 | bool ForStmt::hasConstantUpperBound() const { |
| 333 | return ubMap->isSingleConstant(); |
| 334 | } |
| 335 | |
| 336 | int64_t ForStmt::getConstantLowerBound() const { |
| 337 | return lbMap->getSingleConstantValue(); |
| 338 | } |
| 339 | |
| 340 | int64_t ForStmt::getConstantUpperBound() const { |
| 341 | return ubMap->getSingleConstantValue(); |
| 342 | } |
| 343 | |
| 344 | void ForStmt::setConstantLowerBound(int64_t value) { |
| 345 | MLIRContext *context = getContext(); |
| 346 | auto *expr = AffineConstantExpr::get(value, context); |
| 347 | setLowerBound({}, AffineMap::get(0, 0, expr, {}, context)); |
| 348 | } |
| 349 | |
| 350 | void ForStmt::setConstantUpperBound(int64_t value) { |
| 351 | MLIRContext *context = getContext(); |
| 352 | auto *expr = AffineConstantExpr::get(value, context); |
| 353 | setUpperBound({}, AffineMap::get(0, 0, expr, {}, context)); |
| 354 | } |
Tatiana Shpeisman | 3838db7 | 2018-07-30 15:18:10 -0700 | [diff] [blame] | 355 | |
| 356 | //===----------------------------------------------------------------------===// |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 357 | // IfStmt |
| 358 | //===----------------------------------------------------------------------===// |
| 359 | |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame^] | 360 | IfStmt::IfStmt(Location *location, IntegerSet *condition) |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 361 | : Statement(Kind::If, location), thenClause(new IfClause(this)), |
| 362 | elseClause(nullptr), condition(condition) {} |
| 363 | |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 364 | IfStmt::~IfStmt() { |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 365 | delete thenClause; |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 366 | if (elseClause) |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 367 | delete elseClause; |
Uday Bondhugula | bc53562 | 2018-08-07 14:24:38 -0700 | [diff] [blame] | 368 | // An IfStmt's IntegerSet 'condition' should not be deleted since it is |
| 369 | // allocated through MLIRContext's bump pointer allocator. |
Tatiana Shpeisman | 1bcfe98 | 2018-07-13 13:03:13 -0700 | [diff] [blame] | 370 | } |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame] | 371 | |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 372 | //===----------------------------------------------------------------------===// |
| 373 | // Statement Cloning |
| 374 | //===----------------------------------------------------------------------===// |
| 375 | |
| 376 | /// Create a deep copy of this statement, remapping any operands that use |
| 377 | /// values outside of the statement using the map that is provided (leaving |
| 378 | /// them alone if no entry is present). Replaces references to cloned |
| 379 | /// sub-statements to the corresponding statement that is copied, and adds |
| 380 | /// those mappings to the map. |
| 381 | Statement *Statement::clone(DenseMap<const MLValue *, MLValue *> &operandMap, |
| 382 | MLIRContext *context) const { |
| 383 | // If the specified value is in operandMap, return the remapped value. |
| 384 | // Otherwise return the value itself. |
| 385 | auto remapOperand = [&](const MLValue *value) -> MLValue * { |
| 386 | auto it = operandMap.find(value); |
| 387 | return it != operandMap.end() ? it->second : const_cast<MLValue *>(value); |
| 388 | }; |
| 389 | |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 390 | SmallVector<MLValue *, 8> operands; |
| 391 | operands.reserve(getNumOperands()); |
| 392 | for (auto *opValue : getOperands()) |
| 393 | operands.push_back(remapOperand(opValue)); |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 394 | |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 395 | if (auto *opStmt = dyn_cast<OperationStmt>(this)) { |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 396 | SmallVector<Type *, 8> resultTypes; |
| 397 | resultTypes.reserve(opStmt->getNumResults()); |
| 398 | for (auto *result : opStmt->getResults()) |
| 399 | resultTypes.push_back(result->getType()); |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 400 | auto *newOp = |
| 401 | OperationStmt::create(getLoc(), opStmt->getName(), operands, |
| 402 | resultTypes, opStmt->getAttrs(), context); |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 403 | // Remember the mapping of any results. |
| 404 | for (unsigned i = 0, e = opStmt->getNumResults(); i != e; ++i) |
| 405 | operandMap[opStmt->getResult(i)] = newOp->getResult(i); |
| 406 | return newOp; |
| 407 | } |
| 408 | |
| 409 | if (auto *forStmt = dyn_cast<ForStmt>(this)) { |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 410 | auto *lbMap = forStmt->getLowerBoundMap(); |
| 411 | auto *ubMap = forStmt->getUpperBoundMap(); |
| 412 | |
| 413 | auto *newFor = ForStmt::create( |
| 414 | getLoc(), |
| 415 | ArrayRef<MLValue *>(operands).take_front(lbMap->getNumOperands()), |
| 416 | lbMap, ArrayRef<MLValue *>(operands).take_back(ubMap->getNumOperands()), |
| 417 | ubMap, forStmt->getStep(), context); |
| 418 | |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 419 | // Remember the induction variable mapping. |
| 420 | operandMap[forStmt] = newFor; |
| 421 | |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 422 | // Recursively clone the body of the for loop. |
| 423 | for (auto &subStmt : *forStmt) |
| 424 | newFor->push_back(subStmt.clone(operandMap, context)); |
| 425 | |
| 426 | return newFor; |
| 427 | } |
| 428 | |
| 429 | // Otherwise, we must have an If statement. |
| 430 | auto *ifStmt = cast<IfStmt>(this); |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 431 | auto *newIf = new IfStmt(getLoc(), ifStmt->getCondition()); |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 432 | |
| 433 | // TODO: remap operands with remapOperand when if statements have them. |
| 434 | |
| 435 | auto *resultThen = newIf->getThen(); |
| 436 | for (auto &childStmt : *ifStmt->getThen()) |
| 437 | resultThen->push_back(childStmt.clone(operandMap, context)); |
| 438 | |
| 439 | if (ifStmt->hasElse()) { |
| 440 | auto *resultElse = newIf->createElse(); |
| 441 | for (auto &childStmt : *ifStmt->getElse()) |
| 442 | resultElse->push_back(childStmt.clone(operandMap, context)); |
| 443 | } |
| 444 | |
| 445 | return newIf; |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame] | 446 | } |