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 | |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 21 | #include "mlir/IR/Attributes.h" |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 22 | #include "mlir/IR/CFGFunction.h" |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 23 | #include "mlir/IR/MLFunction.h" |
| 24 | #include "mlir/IR/Statements.h" |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 25 | |
| 26 | namespace mlir { |
| 27 | class MLIRContext; |
| 28 | class Module; |
| 29 | class Type; |
| 30 | class PrimitiveType; |
| 31 | class IntegerType; |
| 32 | class FunctionType; |
| 33 | class VectorType; |
| 34 | class RankedTensorType; |
| 35 | class UnrankedTensorType; |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 36 | class BoolAttr; |
| 37 | class IntegerAttr; |
| 38 | class FloatAttr; |
| 39 | class StringAttr; |
James Molloy | f0d2f44 | 2018-08-03 01:54:46 -0700 | [diff] [blame] | 40 | class TypeAttr; |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 41 | class ArrayAttr; |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 42 | class AffineMapAttr; |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 43 | class AffineMap; |
| 44 | class AffineExpr; |
| 45 | class AffineConstantExpr; |
| 46 | class AffineDimExpr; |
| 47 | class AffineSymbolExpr; |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 48 | |
| 49 | /// This class is a general helper class for creating context-global objects |
| 50 | /// like types, attributes, and affine expressions. |
| 51 | class Builder { |
| 52 | public: |
| 53 | explicit Builder(MLIRContext *context) : context(context) {} |
| 54 | explicit Builder(Module *module); |
| 55 | |
| 56 | MLIRContext *getContext() const { return context; } |
| 57 | |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 58 | Identifier getIdentifier(StringRef str); |
| 59 | Module *createModule(); |
| 60 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 61 | // Types. |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 62 | FloatType *getBF16Type(); |
| 63 | FloatType *getF16Type(); |
| 64 | FloatType *getF32Type(); |
| 65 | FloatType *getF64Type(); |
| 66 | |
| 67 | OtherType *getAffineIntType(); |
| 68 | OtherType *getTFControlType(); |
James Molloy | 72b0cbe | 2018-08-01 12:55:27 -0700 | [diff] [blame] | 69 | OtherType *getTFStringType(); |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 70 | IntegerType *getIntegerType(unsigned width); |
| 71 | FunctionType *getFunctionType(ArrayRef<Type *> inputs, |
| 72 | ArrayRef<Type *> results); |
Jacques Pienaar | c03c695 | 2018-08-10 11:56:47 -0700 | [diff] [blame^] | 73 | MemRefType *getMemRefType(ArrayRef<int> shape, Type *elementType, |
| 74 | ArrayRef<AffineMap *> affineMapComposition = {}, |
| 75 | unsigned memorySpace = 0); |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 76 | VectorType *getVectorType(ArrayRef<unsigned> shape, Type *elementType); |
| 77 | RankedTensorType *getTensorType(ArrayRef<int> shape, Type *elementType); |
| 78 | UnrankedTensorType *getTensorType(Type *elementType); |
| 79 | |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 80 | // Attributes. |
| 81 | BoolAttr *getBoolAttr(bool value); |
| 82 | IntegerAttr *getIntegerAttr(int64_t value); |
| 83 | FloatAttr *getFloatAttr(double value); |
| 84 | StringAttr *getStringAttr(StringRef bytes); |
| 85 | ArrayAttr *getArrayAttr(ArrayRef<Attribute *> value); |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 86 | AffineMapAttr *getAffineMapAttr(AffineMap *value); |
James Molloy | f0d2f44 | 2018-08-03 01:54:46 -0700 | [diff] [blame] | 87 | TypeAttr *getTypeAttr(Type *type); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 88 | |
| 89 | // Affine Expressions and Affine Map. |
| 90 | AffineMap *getAffineMap(unsigned dimCount, unsigned symbolCount, |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 91 | ArrayRef<AffineExpr *> results, |
| 92 | ArrayRef<AffineExpr *> rangeSizes); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 93 | AffineDimExpr *getDimExpr(unsigned position); |
| 94 | AffineSymbolExpr *getSymbolExpr(unsigned position); |
| 95 | AffineConstantExpr *getConstantExpr(int64_t constant); |
| 96 | AffineExpr *getAddExpr(AffineExpr *lhs, AffineExpr *rhs); |
| 97 | AffineExpr *getSubExpr(AffineExpr *lhs, AffineExpr *rhs); |
| 98 | AffineExpr *getMulExpr(AffineExpr *lhs, AffineExpr *rhs); |
| 99 | AffineExpr *getModExpr(AffineExpr *lhs, AffineExpr *rhs); |
| 100 | AffineExpr *getFloorDivExpr(AffineExpr *lhs, AffineExpr *rhs); |
| 101 | AffineExpr *getCeilDivExpr(AffineExpr *lhs, AffineExpr *rhs); |
| 102 | |
Uday Bondhugula | bc53562 | 2018-08-07 14:24:38 -0700 | [diff] [blame] | 103 | // Integer set. |
| 104 | IntegerSet *getIntegerSet(unsigned dimCount, unsigned symbolCount, |
| 105 | ArrayRef<AffineExpr *> constraints, |
| 106 | ArrayRef<bool> isEq); |
| 107 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 108 | // TODO: Helpers for affine map/exprs, etc. |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 109 | protected: |
| 110 | MLIRContext *context; |
| 111 | }; |
| 112 | |
| 113 | /// This class helps build a CFGFunction. Instructions that are created are |
| 114 | /// automatically inserted at an insertion point or added to the current basic |
| 115 | /// block. |
| 116 | class CFGFuncBuilder : public Builder { |
| 117 | public: |
Chris Lattner | 8174f3a | 2018-07-29 16:45:23 -0700 | [diff] [blame] | 118 | CFGFuncBuilder(BasicBlock *block, BasicBlock::iterator insertPoint) |
| 119 | : Builder(block->getFunction()->getContext()), |
| 120 | function(block->getFunction()) { |
| 121 | setInsertionPoint(block, insertPoint); |
| 122 | } |
| 123 | |
| 124 | CFGFuncBuilder(OperationInst *insertBefore) |
| 125 | : CFGFuncBuilder(insertBefore->getBlock(), |
| 126 | BasicBlock::iterator(insertBefore)) {} |
| 127 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 128 | CFGFuncBuilder(BasicBlock *block) |
| 129 | : Builder(block->getFunction()->getContext()), |
| 130 | function(block->getFunction()) { |
| 131 | setInsertionPoint(block); |
| 132 | } |
Chris Lattner | 8174f3a | 2018-07-29 16:45:23 -0700 | [diff] [blame] | 133 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 134 | CFGFuncBuilder(CFGFunction *function) |
| 135 | : Builder(function->getContext()), function(function) {} |
| 136 | |
| 137 | /// Reset the insertion point to no location. Creating an operation without a |
| 138 | /// set insertion point is an error, but this can still be useful when the |
| 139 | /// current insertion point a builder refers to is being removed. |
| 140 | void clearInsertionPoint() { |
| 141 | this->block = nullptr; |
| 142 | insertPoint = BasicBlock::iterator(); |
| 143 | } |
| 144 | |
Chris Lattner | 8174f3a | 2018-07-29 16:45:23 -0700 | [diff] [blame] | 145 | /// Set the insertion point to the specified location. |
| 146 | void setInsertionPoint(BasicBlock *block, BasicBlock::iterator insertPoint) { |
| 147 | assert(block->getFunction() == function && |
| 148 | "can't move to a different function"); |
| 149 | this->block = block; |
| 150 | this->insertPoint = insertPoint; |
| 151 | } |
| 152 | |
| 153 | /// Set the insertion point to the specified operation. |
| 154 | void setInsertionPoint(OperationInst *inst) { |
| 155 | setInsertionPoint(inst->getBlock(), BasicBlock::iterator(inst)); |
| 156 | } |
| 157 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 158 | /// Set the insertion point to the end of the specified block. |
| 159 | void setInsertionPoint(BasicBlock *block) { |
Chris Lattner | 8174f3a | 2018-07-29 16:45:23 -0700 | [diff] [blame] | 160 | setInsertionPoint(block, block->end()); |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 163 | void insert(OperationInst *opInst) { |
| 164 | block->getOperations().insert(insertPoint, opInst); |
| 165 | } |
| 166 | |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 167 | // Add new basic block and set the insertion point to the end of it. |
| 168 | BasicBlock *createBlock(); |
| 169 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 170 | /// Create an operation given the fields represented as an OperationState. |
| 171 | OperationInst *createOperation(const OperationState &state); |
| 172 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 173 | /// Create operation of specific op type at the current insertion point. |
| 174 | template <typename OpTy, typename... Args> |
| 175 | OpPointer<OpTy> create(Args... args) { |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 176 | auto *inst = createOperation(OpTy::build(this, args...)); |
| 177 | auto result = inst->template getAs<OpTy>(); |
| 178 | assert(result && "Builder didn't return the right type"); |
| 179 | return result; |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 182 | OperationInst *cloneOperation(const OperationInst &srcOpInst) { |
| 183 | auto *op = srcOpInst.clone(); |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 184 | insert(op); |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 185 | return op; |
| 186 | } |
| 187 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 188 | // Terminators. |
| 189 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 190 | ReturnInst *createReturnInst(ArrayRef<CFGValue *> operands) { |
| 191 | return insertTerminator(ReturnInst::create(operands)); |
| 192 | } |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 193 | |
| 194 | BranchInst *createBranchInst(BasicBlock *dest) { |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 195 | return insertTerminator(BranchInst::create(dest)); |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 196 | } |
| 197 | |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 198 | CondBranchInst *createCondBranchInst(CFGValue *condition, |
| 199 | BasicBlock *trueDest, |
| 200 | BasicBlock *falseDest) { |
| 201 | return insertTerminator( |
| 202 | CondBranchInst::create(condition, trueDest, falseDest)); |
| 203 | } |
| 204 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 205 | private: |
| 206 | template <typename T> |
| 207 | T *insertTerminator(T *term) { |
| 208 | block->setTerminator(term); |
| 209 | return term; |
| 210 | } |
| 211 | |
| 212 | CFGFunction *function; |
| 213 | BasicBlock *block = nullptr; |
| 214 | BasicBlock::iterator insertPoint; |
| 215 | }; |
| 216 | |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 217 | /// This class helps build an MLFunction. Statements that are created are |
| 218 | /// automatically inserted at an insertion point or added to the current |
| 219 | /// statement block. |
| 220 | class MLFuncBuilder : public Builder { |
| 221 | public: |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 222 | /// Create ML function builder and set insertion point to the given statement, |
| 223 | /// which will cause subsequent insertions to go right before it. |
| 224 | MLFuncBuilder(Statement *stmt) |
| 225 | // TODO: Eliminate findFunction from this. |
| 226 | : Builder(stmt->findFunction()->getContext()) { |
| 227 | setInsertionPoint(stmt); |
| 228 | } |
| 229 | |
| 230 | MLFuncBuilder(StmtBlock *block, StmtBlock::iterator insertPoint) |
| 231 | // TODO: Eliminate findFunction from this. |
Tatiana Shpeisman | d880b35 | 2018-07-31 23:14:16 -0700 | [diff] [blame] | 232 | : Builder(block->findFunction()->getContext()) { |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 233 | setInsertionPoint(block, insertPoint); |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /// Reset the insertion point to no location. Creating an operation without a |
| 237 | /// set insertion point is an error, but this can still be useful when the |
| 238 | /// current insertion point a builder refers to is being removed. |
| 239 | void clearInsertionPoint() { |
| 240 | this->block = nullptr; |
| 241 | insertPoint = StmtBlock::iterator(); |
| 242 | } |
| 243 | |
Tatiana Shpeisman | d880b35 | 2018-07-31 23:14:16 -0700 | [diff] [blame] | 244 | /// Set the insertion point to the specified location. |
| 245 | /// Unlike CFGFuncBuilder, MLFuncBuilder allows to set insertion |
| 246 | /// point to a different function. |
| 247 | void setInsertionPoint(StmtBlock *block, StmtBlock::iterator insertPoint) { |
| 248 | // TODO: check that insertPoint is in this rather than some other block. |
| 249 | this->block = block; |
| 250 | this->insertPoint = insertPoint; |
| 251 | } |
| 252 | |
| 253 | /// Set the insertion point to the specified operation. |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 254 | void setInsertionPoint(Statement *stmt) { |
Tatiana Shpeisman | d880b35 | 2018-07-31 23:14:16 -0700 | [diff] [blame] | 255 | setInsertionPoint(stmt->getBlock(), StmtBlock::iterator(stmt)); |
| 256 | } |
| 257 | |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 258 | /// Set the insertion point to the start of the specified block. |
| 259 | void setInsertionPointToStart(StmtBlock *block) { |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 260 | this->block = block; |
| 261 | insertPoint = block->begin(); |
| 262 | } |
| 263 | |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 264 | /// Set the insertion point to the end of the specified block. |
| 265 | void setInsertionPointToEnd(StmtBlock *block) { |
| 266 | this->block = block; |
| 267 | insertPoint = block->end(); |
| 268 | } |
| 269 | |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame] | 270 | /// Get the current insertion point of the builder. |
| 271 | StmtBlock::iterator getInsertionPoint() const { return insertPoint; } |
| 272 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 273 | /// Create an operation given the fields represented as an OperationState. |
| 274 | OperationStmt *createOperation(const OperationState &state); |
| 275 | |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 276 | /// Create operation of specific op type at the current insertion point. |
Jacques Pienaar | ac86d10 | 2018-08-03 08:16:37 -0700 | [diff] [blame] | 277 | template <typename OpTy, typename... Args> |
| 278 | OpPointer<OpTy> create(Args... args) { |
Chris Lattner | 992a127 | 2018-08-07 12:02:37 -0700 | [diff] [blame] | 279 | auto stmt = createOperation(OpTy::build(this, args...)); |
| 280 | auto result = stmt->template getAs<OpTy>(); |
| 281 | assert(result && "Builder didn't return the right type"); |
| 282 | return result; |
Jacques Pienaar | ac86d10 | 2018-08-03 08:16:37 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Chris Lattner | e787b32 | 2018-08-08 11:14:57 -0700 | [diff] [blame] | 285 | /// Create a deep copy of the specified statement, remapping any operands that |
| 286 | /// use values outside of the statement using the map that is provided ( |
| 287 | /// leaving them alone if no entry is present). Replaces references to cloned |
| 288 | /// sub-statements to the corresponding statement that is copied, and adds |
| 289 | /// those mappings to the map. |
| 290 | Statement *clone(const Statement &stmt, |
| 291 | OperationStmt::OperandMapTy &operandMapping) { |
| 292 | Statement *cloneStmt = stmt.clone(operandMapping, getContext()); |
Uday Bondhugula | 134154e | 2018-08-06 18:40:34 -0700 | [diff] [blame] | 293 | block->getStatements().insert(insertPoint, cloneStmt); |
| 294 | return cloneStmt; |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Chris Lattner | 1604e47 | 2018-07-23 08:42:19 -0700 | [diff] [blame] | 297 | // Creates for statement. When step is not specified, it is set to 1. |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 298 | ForStmt *createFor(AffineConstantExpr *lowerBound, |
| 299 | AffineConstantExpr *upperBound, |
| 300 | AffineConstantExpr *step = nullptr); |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 301 | |
Uday Bondhugula | bc53562 | 2018-08-07 14:24:38 -0700 | [diff] [blame] | 302 | IfStmt *createIf(IntegerSet *condition) { |
| 303 | auto *stmt = new IfStmt(condition); |
Tatiana Shpeisman | d880b35 | 2018-07-31 23:14:16 -0700 | [diff] [blame] | 304 | block->getStatements().insert(insertPoint, stmt); |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 305 | return stmt; |
| 306 | } |
| 307 | |
| 308 | private: |
| 309 | StmtBlock *block = nullptr; |
| 310 | StmtBlock::iterator insertPoint; |
| 311 | }; |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 312 | |
| 313 | } // namespace mlir |
| 314 | |
| 315 | #endif |