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