Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 1 | //===- Builders.h - Helpers for constructing MLIR Classes -------*- C++ -*-===// |
| 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 | #ifndef MLIR_IR_BUILDERS_H |
| 19 | #define MLIR_IR_BUILDERS_H |
| 20 | |
| 21 | #include "mlir/IR/CFGFunction.h" |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 22 | #include "mlir/IR/MLFunction.h" |
| 23 | #include "mlir/IR/Statements.h" |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 24 | |
| 25 | namespace mlir { |
| 26 | class MLIRContext; |
| 27 | class Module; |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 28 | class UnknownLoc; |
| 29 | class UniquedFilename; |
| 30 | class FileLineColLoc; |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 31 | class Type; |
| 32 | class PrimitiveType; |
| 33 | class IntegerType; |
| 34 | class FunctionType; |
| 35 | class VectorType; |
| 36 | class RankedTensorType; |
| 37 | class UnrankedTensorType; |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 38 | class BoolAttr; |
| 39 | class IntegerAttr; |
| 40 | class FloatAttr; |
| 41 | class StringAttr; |
James Molloy | f0d2f44 | 2018-08-03 01:54:46 -0700 | [diff] [blame] | 42 | class TypeAttr; |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 43 | class ArrayAttr; |
Chris Lattner | 4613d9e | 2018-08-19 21:17:22 -0700 | [diff] [blame] | 44 | class FunctionAttr; |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 45 | class AffineMapAttr; |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 46 | class AffineMap; |
| 47 | class AffineExpr; |
| 48 | class AffineConstantExpr; |
| 49 | class AffineDimExpr; |
| 50 | class AffineSymbolExpr; |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 51 | |
| 52 | /// This class is a general helper class for creating context-global objects |
| 53 | /// like types, attributes, and affine expressions. |
| 54 | class Builder { |
| 55 | public: |
| 56 | explicit Builder(MLIRContext *context) : context(context) {} |
| 57 | explicit Builder(Module *module); |
| 58 | |
| 59 | MLIRContext *getContext() const { return context; } |
| 60 | |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 61 | Identifier getIdentifier(StringRef str); |
| 62 | Module *createModule(); |
| 63 | |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 64 | // Locations. |
| 65 | UnknownLoc *getUnknownLoc(); |
| 66 | UniquedFilename getUniquedFilename(StringRef filename); |
| 67 | FileLineColLoc *getFileLineColLoc(UniquedFilename filename, unsigned line, |
| 68 | unsigned column); |
| 69 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 70 | // Types. |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 71 | FloatType *getBF16Type(); |
| 72 | FloatType *getF16Type(); |
| 73 | FloatType *getF32Type(); |
| 74 | FloatType *getF64Type(); |
| 75 | |
| 76 | OtherType *getAffineIntType(); |
| 77 | OtherType *getTFControlType(); |
James Molloy | 72b0cbe | 2018-08-01 12:55:27 -0700 | [diff] [blame] | 78 | OtherType *getTFStringType(); |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 79 | IntegerType *getIntegerType(unsigned width); |
| 80 | FunctionType *getFunctionType(ArrayRef<Type *> inputs, |
| 81 | ArrayRef<Type *> results); |
Jacques Pienaar | c03c695 | 2018-08-10 11:56:47 -0700 | [diff] [blame] | 82 | MemRefType *getMemRefType(ArrayRef<int> shape, Type *elementType, |
| 83 | ArrayRef<AffineMap *> affineMapComposition = {}, |
| 84 | unsigned memorySpace = 0); |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 85 | VectorType *getVectorType(ArrayRef<unsigned> shape, Type *elementType); |
| 86 | RankedTensorType *getTensorType(ArrayRef<int> shape, Type *elementType); |
| 87 | UnrankedTensorType *getTensorType(Type *elementType); |
| 88 | |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 89 | // Attributes. |
| 90 | BoolAttr *getBoolAttr(bool value); |
| 91 | IntegerAttr *getIntegerAttr(int64_t value); |
| 92 | FloatAttr *getFloatAttr(double value); |
| 93 | StringAttr *getStringAttr(StringRef bytes); |
| 94 | ArrayAttr *getArrayAttr(ArrayRef<Attribute *> value); |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 95 | AffineMapAttr *getAffineMapAttr(AffineMap *value); |
James Molloy | f0d2f44 | 2018-08-03 01:54:46 -0700 | [diff] [blame] | 96 | TypeAttr *getTypeAttr(Type *type); |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 97 | FunctionAttr *getFunctionAttr(const Function *value); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 98 | |
Uday Bondhugula | 4266fc7 | 2018-09-13 08:12:38 -0700 | [diff] [blame^] | 99 | // Affine expressions and affine maps. |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 100 | AffineDimExpr *getDimExpr(unsigned position); |
| 101 | AffineSymbolExpr *getSymbolExpr(unsigned position); |
| 102 | AffineConstantExpr *getConstantExpr(int64_t constant); |
| 103 | AffineExpr *getAddExpr(AffineExpr *lhs, AffineExpr *rhs); |
| 104 | AffineExpr *getSubExpr(AffineExpr *lhs, AffineExpr *rhs); |
| 105 | AffineExpr *getMulExpr(AffineExpr *lhs, AffineExpr *rhs); |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 106 | AffineExpr *getMulExpr(AffineExpr *lhs, int64_t rhs); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 107 | AffineExpr *getModExpr(AffineExpr *lhs, AffineExpr *rhs); |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 108 | AffineExpr *getModExpr(AffineExpr *lhs, uint64_t rhs); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 109 | AffineExpr *getFloorDivExpr(AffineExpr *lhs, AffineExpr *rhs); |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 110 | AffineExpr *getFloorDivExpr(AffineExpr *lhs, uint64_t rhs); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 111 | AffineExpr *getCeilDivExpr(AffineExpr *lhs, AffineExpr *rhs); |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame] | 112 | AffineExpr *getCeilDivExpr(AffineExpr *lhs, uint64_t rhs); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 113 | |
Uday Bondhugula | 4266fc7 | 2018-09-13 08:12:38 -0700 | [diff] [blame^] | 114 | /// Creates a sum of products affine expression from constant coefficients. |
| 115 | /// If c_0, c_1, ... are the coefficients in the order corresponding to |
| 116 | /// dimensions, symbols, and the constant term, create the affine expression: |
| 117 | /// expr = c_0*d0 + c_1*d1 + ... + c_{ndims-1}*d_{ndims-1} + c_{..}*s0 + |
| 118 | /// c_{..}*s1 + ... + const |
| 119 | AffineExpr *getAddMulPureAffineExpr(unsigned numDims, unsigned numSymbols, |
| 120 | ArrayRef<int64_t> coeffs); |
| 121 | |
| 122 | AffineMap *getAffineMap(unsigned dimCount, unsigned symbolCount, |
| 123 | ArrayRef<AffineExpr *> results, |
| 124 | ArrayRef<AffineExpr *> rangeSizes); |
Uday Bondhugula | bc53562 | 2018-08-07 14:24:38 -0700 | [diff] [blame] | 125 | |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 126 | // Special cases of affine maps and integer sets |
Uday Bondhugula | 4266fc7 | 2018-09-13 08:12:38 -0700 | [diff] [blame^] | 127 | /// Returns a single constant result affine map with 0 dimensions and 0 |
| 128 | /// symbols. One constant result: () -> (val). |
| 129 | AffineMap *getConstantAffineMap(int64_t val); |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 130 | // One dimension id identity map: (i) -> (i). |
| 131 | AffineMap *getDimIdentityMap(); |
| 132 | // One symbol identity map: ()[s] -> (s). |
| 133 | AffineMap *getSymbolIdentityMap(); |
| 134 | |
Uday Bondhugula | 4266fc7 | 2018-09-13 08:12:38 -0700 | [diff] [blame^] | 135 | // Integer set. |
| 136 | IntegerSet *getIntegerSet(unsigned dimCount, unsigned symbolCount, |
| 137 | ArrayRef<AffineExpr *> constraints, |
| 138 | ArrayRef<bool> isEq); |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 139 | // TODO: Helpers for affine map/exprs, etc. |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 140 | protected: |
| 141 | MLIRContext *context; |
| 142 | }; |
| 143 | |
| 144 | /// This class helps build a CFGFunction. Instructions that are created are |
| 145 | /// automatically inserted at an insertion point or added to the current basic |
| 146 | /// block. |
| 147 | class CFGFuncBuilder : public Builder { |
| 148 | public: |
Chris Lattner | 8174f3a | 2018-07-29 16:45:23 -0700 | [diff] [blame] | 149 | CFGFuncBuilder(BasicBlock *block, BasicBlock::iterator insertPoint) |
| 150 | : Builder(block->getFunction()->getContext()), |
| 151 | function(block->getFunction()) { |
| 152 | setInsertionPoint(block, insertPoint); |
| 153 | } |
| 154 | |
| 155 | CFGFuncBuilder(OperationInst *insertBefore) |
| 156 | : CFGFuncBuilder(insertBefore->getBlock(), |
| 157 | BasicBlock::iterator(insertBefore)) {} |
| 158 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 159 | CFGFuncBuilder(BasicBlock *block) |
| 160 | : Builder(block->getFunction()->getContext()), |
| 161 | function(block->getFunction()) { |
| 162 | setInsertionPoint(block); |
| 163 | } |
Chris Lattner | 8174f3a | 2018-07-29 16:45:23 -0700 | [diff] [blame] | 164 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 165 | CFGFuncBuilder(CFGFunction *function) |
| 166 | : Builder(function->getContext()), function(function) {} |
| 167 | |
| 168 | /// Reset the insertion point to no location. Creating an operation without a |
| 169 | /// set insertion point is an error, but this can still be useful when the |
| 170 | /// current insertion point a builder refers to is being removed. |
| 171 | void clearInsertionPoint() { |
| 172 | this->block = nullptr; |
| 173 | insertPoint = BasicBlock::iterator(); |
| 174 | } |
| 175 | |
Chris Lattner | 8174f3a | 2018-07-29 16:45:23 -0700 | [diff] [blame] | 176 | /// Set the insertion point to the specified location. |
| 177 | void setInsertionPoint(BasicBlock *block, BasicBlock::iterator insertPoint) { |
| 178 | assert(block->getFunction() == function && |
| 179 | "can't move to a different function"); |
| 180 | this->block = block; |
| 181 | this->insertPoint = insertPoint; |
| 182 | } |
| 183 | |
| 184 | /// Set the insertion point to the specified operation. |
| 185 | void setInsertionPoint(OperationInst *inst) { |
| 186 | setInsertionPoint(inst->getBlock(), BasicBlock::iterator(inst)); |
| 187 | } |
| 188 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 189 | /// Set the insertion point to the end of the specified block. |
| 190 | void setInsertionPoint(BasicBlock *block) { |
Chris Lattner | 8174f3a | 2018-07-29 16:45:23 -0700 | [diff] [blame] | 191 | setInsertionPoint(block, block->end()); |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 194 | void insert(OperationInst *opInst) { |
| 195 | block->getOperations().insert(insertPoint, opInst); |
| 196 | } |
| 197 | |
Chris Lattner | 8a9310a | 2018-08-24 21:13:19 -0700 | [diff] [blame] | 198 | /// Add new basic block and set the insertion point to the end of it. If an |
| 199 | /// 'insertBefore' basic block is passed, the block will be placed before the |
| 200 | /// specified block. If not, the block will be appended to the end of the |
| 201 | /// current function. |
| 202 | BasicBlock *createBlock(BasicBlock *insertBefore = nullptr); |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 203 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 204 | /// Create an operation given the fields represented as an OperationState. |
| 205 | OperationInst *createOperation(const OperationState &state); |
| 206 | |
Chris Lattner | 7879f84 | 2018-09-02 22:01:45 -0700 | [diff] [blame] | 207 | /// Create operation of specific op type at the current insertion point |
| 208 | /// without verifying to see if it is valid. |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 209 | template <typename OpTy, typename... Args> |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 210 | OpPointer<OpTy> create(Location *location, Args... args) { |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 211 | OperationState state(getContext(), location, OpTy::getOperationName()); |
Chris Lattner | 1eb7748 | 2018-08-22 19:25:49 -0700 | [diff] [blame] | 212 | OpTy::build(this, &state, args...); |
| 213 | auto *inst = createOperation(state); |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 214 | auto result = inst->template getAs<OpTy>(); |
| 215 | assert(result && "Builder didn't return the right type"); |
| 216 | return result; |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Chris Lattner | 7879f84 | 2018-09-02 22:01:45 -0700 | [diff] [blame] | 219 | /// Create operation of specific op type at the current insertion point. If |
| 220 | /// the result is an invalid op (the verifier hook fails), emit a the |
| 221 | /// specified error message and return null. |
| 222 | template <typename OpTy, typename... Args> |
Chris Lattner | 015100b | 2018-09-09 20:40:23 -0700 | [diff] [blame] | 223 | OpPointer<OpTy> createChecked(Location *location, Args... args) { |
Chris Lattner | 7879f84 | 2018-09-02 22:01:45 -0700 | [diff] [blame] | 224 | OperationState state(getContext(), location, OpTy::getOperationName()); |
| 225 | OpTy::build(this, &state, args...); |
| 226 | auto *inst = createOperation(state); |
Chris Lattner | 7879f84 | 2018-09-02 22:01:45 -0700 | [diff] [blame] | 227 | |
Chris Lattner | 015100b | 2018-09-09 20:40:23 -0700 | [diff] [blame] | 228 | // If the OperationInst we produce is valid, return it. |
| 229 | if (!OpTy::verifyInvariants(inst)) { |
| 230 | auto result = inst->template getAs<OpTy>(); |
| 231 | assert(result && "Builder didn't return the right type"); |
Chris Lattner | 7879f84 | 2018-09-02 22:01:45 -0700 | [diff] [blame] | 232 | return result; |
Chris Lattner | 015100b | 2018-09-09 20:40:23 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // Otherwise, the error message got emitted. Just remove the instruction |
| 236 | // we made. |
Chris Lattner | 7879f84 | 2018-09-02 22:01:45 -0700 | [diff] [blame] | 237 | inst->eraseFromBlock(); |
| 238 | return OpPointer<OpTy>(); |
| 239 | } |
| 240 | |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 241 | OperationInst *cloneOperation(const OperationInst &srcOpInst) { |
| 242 | auto *op = srcOpInst.clone(); |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 243 | insert(op); |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 244 | return op; |
| 245 | } |
| 246 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 247 | // Terminators. |
| 248 | |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 249 | ReturnInst *createReturn(Location *location, ArrayRef<CFGValue *> operands) { |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 250 | return insertTerminator(ReturnInst::create(location, operands)); |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 251 | } |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 252 | |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 253 | BranchInst *createBranch(Location *location, BasicBlock *dest, |
Chris Lattner | 8a9310a | 2018-08-24 21:13:19 -0700 | [diff] [blame] | 254 | ArrayRef<CFGValue *> operands = {}) { |
| 255 | return insertTerminator(BranchInst::create(location, dest, operands)); |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 258 | CondBranchInst *createCondBranch(Location *location, CFGValue *condition, |
Chris Lattner | 091a6b5 | 2018-08-23 14:58:27 -0700 | [diff] [blame] | 259 | BasicBlock *trueDest, |
| 260 | BasicBlock *falseDest) { |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 261 | return insertTerminator( |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 262 | CondBranchInst::create(location, condition, trueDest, falseDest)); |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 265 | private: |
| 266 | template <typename T> |
| 267 | T *insertTerminator(T *term) { |
| 268 | block->setTerminator(term); |
| 269 | return term; |
| 270 | } |
| 271 | |
| 272 | CFGFunction *function; |
| 273 | BasicBlock *block = nullptr; |
| 274 | BasicBlock::iterator insertPoint; |
| 275 | }; |
| 276 | |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 277 | /// This class helps build an MLFunction. Statements that are created are |
| 278 | /// automatically inserted at an insertion point or added to the current |
| 279 | /// statement block. |
| 280 | class MLFuncBuilder : public Builder { |
| 281 | public: |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 282 | /// Create ML function builder and set insertion point to the given statement, |
| 283 | /// which will cause subsequent insertions to go right before it. |
| 284 | MLFuncBuilder(Statement *stmt) |
| 285 | // TODO: Eliminate findFunction from this. |
| 286 | : Builder(stmt->findFunction()->getContext()) { |
| 287 | setInsertionPoint(stmt); |
| 288 | } |
| 289 | |
| 290 | MLFuncBuilder(StmtBlock *block, StmtBlock::iterator insertPoint) |
| 291 | // TODO: Eliminate findFunction from this. |
Tatiana Shpeisman | d880b35 | 2018-07-31 23:14:16 -0700 | [diff] [blame] | 292 | : Builder(block->findFunction()->getContext()) { |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 293 | setInsertionPoint(block, insertPoint); |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /// Reset the insertion point to no location. Creating an operation without a |
| 297 | /// set insertion point is an error, but this can still be useful when the |
| 298 | /// current insertion point a builder refers to is being removed. |
| 299 | void clearInsertionPoint() { |
| 300 | this->block = nullptr; |
| 301 | insertPoint = StmtBlock::iterator(); |
| 302 | } |
| 303 | |
Tatiana Shpeisman | d880b35 | 2018-07-31 23:14:16 -0700 | [diff] [blame] | 304 | /// Set the insertion point to the specified location. |
| 305 | /// Unlike CFGFuncBuilder, MLFuncBuilder allows to set insertion |
| 306 | /// point to a different function. |
| 307 | void setInsertionPoint(StmtBlock *block, StmtBlock::iterator insertPoint) { |
| 308 | // TODO: check that insertPoint is in this rather than some other block. |
| 309 | this->block = block; |
| 310 | this->insertPoint = insertPoint; |
| 311 | } |
| 312 | |
Uday Bondhugula | 4266fc7 | 2018-09-13 08:12:38 -0700 | [diff] [blame^] | 313 | /// Sets the insertion point to the specified operation, which will cause |
Uday Bondhugula | 6770171 | 2018-08-21 16:01:23 -0700 | [diff] [blame] | 314 | /// subsequent insertions to go right before it. |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 315 | void setInsertionPoint(Statement *stmt) { |
Tatiana Shpeisman | d880b35 | 2018-07-31 23:14:16 -0700 | [diff] [blame] | 316 | setInsertionPoint(stmt->getBlock(), StmtBlock::iterator(stmt)); |
| 317 | } |
| 318 | |
Uday Bondhugula | 4266fc7 | 2018-09-13 08:12:38 -0700 | [diff] [blame^] | 319 | /// Sets the insertion point to the start of the specified block. |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 320 | void setInsertionPointToStart(StmtBlock *block) { |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 321 | setInsertionPoint(block, block->begin()); |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Uday Bondhugula | 4266fc7 | 2018-09-13 08:12:38 -0700 | [diff] [blame^] | 324 | /// Sets the insertion point to the end of the specified block. |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 325 | void setInsertionPointToEnd(StmtBlock *block) { |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 326 | setInsertionPoint(block, block->end()); |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame] | 329 | /// Get the current insertion point of the builder. |
| 330 | StmtBlock::iterator getInsertionPoint() const { return insertPoint; } |
| 331 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 332 | /// Create an operation given the fields represented as an OperationState. |
| 333 | OperationStmt *createOperation(const OperationState &state); |
| 334 | |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 335 | /// Create operation of specific op type at the current insertion point. |
Jacques Pienaar | ac86d10 | 2018-08-03 08:16:37 -0700 | [diff] [blame] | 336 | template <typename OpTy, typename... Args> |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 337 | OpPointer<OpTy> create(Location *location, Args... args) { |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 338 | OperationState state(getContext(), location, OpTy::getOperationName()); |
Chris Lattner | 1eb7748 | 2018-08-22 19:25:49 -0700 | [diff] [blame] | 339 | OpTy::build(this, &state, args...); |
| 340 | auto *stmt = createOperation(state); |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 341 | auto result = stmt->template getAs<OpTy>(); |
| 342 | assert(result && "Builder didn't return the right type"); |
| 343 | return result; |
Jacques Pienaar | ac86d10 | 2018-08-03 08:16:37 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Uday Bondhugula | 4266fc7 | 2018-09-13 08:12:38 -0700 | [diff] [blame^] | 346 | /// Creates an operation of specific op type at the current insertion point. |
| 347 | /// If the result is an invalid op (the verifier hook fails), emit an error |
| 348 | /// and return null. |
Chris Lattner | 7879f84 | 2018-09-02 22:01:45 -0700 | [diff] [blame] | 349 | template <typename OpTy, typename... Args> |
Chris Lattner | 015100b | 2018-09-09 20:40:23 -0700 | [diff] [blame] | 350 | OpPointer<OpTy> createChecked(Location *location, Args... args) { |
Chris Lattner | 7879f84 | 2018-09-02 22:01:45 -0700 | [diff] [blame] | 351 | OperationState state(getContext(), location, OpTy::getOperationName()); |
| 352 | OpTy::build(this, &state, args...); |
| 353 | auto *stmt = createOperation(state); |
Chris Lattner | 7879f84 | 2018-09-02 22:01:45 -0700 | [diff] [blame] | 354 | |
Chris Lattner | 015100b | 2018-09-09 20:40:23 -0700 | [diff] [blame] | 355 | // If the OperationStmt we produce is valid, return it. |
| 356 | if (!OpTy::verifyInvariants(stmt)) { |
| 357 | auto result = stmt->template getAs<OpTy>(); |
| 358 | assert(result && "Builder didn't return the right type"); |
Chris Lattner | 7879f84 | 2018-09-02 22:01:45 -0700 | [diff] [blame] | 359 | return result; |
Chris Lattner | 015100b | 2018-09-09 20:40:23 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | // Otherwise, the error message got emitted. Just remove the statement |
| 363 | // we made. |
Chris Lattner | 7879f84 | 2018-09-02 22:01:45 -0700 | [diff] [blame] | 364 | stmt->eraseFromBlock(); |
| 365 | return OpPointer<OpTy>(); |
| 366 | } |
| 367 | |
Uday Bondhugula | 4266fc7 | 2018-09-13 08:12:38 -0700 | [diff] [blame^] | 368 | /// Creates a deep copy of the specified statement, remapping any operands |
| 369 | /// that use values outside of the statement using the map that is provided ( |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 370 | /// leaving them alone if no entry is present). Replaces references to cloned |
| 371 | /// sub-statements to the corresponding statement that is copied, and adds |
| 372 | /// those mappings to the map. |
| 373 | Statement *clone(const Statement &stmt, |
| 374 | OperationStmt::OperandMapTy &operandMapping) { |
| 375 | Statement *cloneStmt = stmt.clone(operandMapping, getContext()); |
Uday Bondhugula | 134154e | 2018-08-06 18:40:34 -0700 | [diff] [blame] | 376 | block->getStatements().insert(insertPoint, cloneStmt); |
| 377 | return cloneStmt; |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Uday Bondhugula | 4266fc7 | 2018-09-13 08:12:38 -0700 | [diff] [blame^] | 380 | // Creates a for statement. When step is not specified, it is set to 1. |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 381 | ForStmt *createFor(Location *location, ArrayRef<MLValue *> lbOperands, |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 382 | AffineMap *lbMap, ArrayRef<MLValue *> ubOperands, |
| 383 | AffineMap *ubMap, int64_t step = 1); |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 384 | |
Uday Bondhugula | 4266fc7 | 2018-09-13 08:12:38 -0700 | [diff] [blame^] | 385 | // Creates a for statement with known (constant) lower and upper bounds. |
| 386 | // Default step is 1. |
| 387 | ForStmt *createFor(Location *loc, int64_t lb, int64_t ub, int64_t step = 1); |
| 388 | |
| 389 | /// Creates if statement. |
Tatiana Shpeisman | c6aa35b | 2018-08-28 15:26:20 -0700 | [diff] [blame] | 390 | IfStmt *createIf(Location *location, ArrayRef<MLValue *> operands, |
| 391 | IntegerSet *set); |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 392 | |
| 393 | private: |
| 394 | StmtBlock *block = nullptr; |
| 395 | StmtBlock::iterator insertPoint; |
| 396 | }; |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 397 | |
| 398 | } // namespace mlir |
| 399 | |
| 400 | #endif |