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 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 100 | // TODO: Helpers for affine map/exprs, etc. |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 101 | protected: |
| 102 | MLIRContext *context; |
| 103 | }; |
| 104 | |
| 105 | /// This class helps build a CFGFunction. Instructions that are created are |
| 106 | /// automatically inserted at an insertion point or added to the current basic |
| 107 | /// block. |
| 108 | class CFGFuncBuilder : public Builder { |
| 109 | public: |
Chris Lattner | 8174f3a | 2018-07-29 16:45:23 -0700 | [diff] [blame] | 110 | CFGFuncBuilder(BasicBlock *block, BasicBlock::iterator insertPoint) |
| 111 | : Builder(block->getFunction()->getContext()), |
| 112 | function(block->getFunction()) { |
| 113 | setInsertionPoint(block, insertPoint); |
| 114 | } |
| 115 | |
| 116 | CFGFuncBuilder(OperationInst *insertBefore) |
| 117 | : CFGFuncBuilder(insertBefore->getBlock(), |
| 118 | BasicBlock::iterator(insertBefore)) {} |
| 119 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 120 | CFGFuncBuilder(BasicBlock *block) |
| 121 | : Builder(block->getFunction()->getContext()), |
| 122 | function(block->getFunction()) { |
| 123 | setInsertionPoint(block); |
| 124 | } |
Chris Lattner | 8174f3a | 2018-07-29 16:45:23 -0700 | [diff] [blame] | 125 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 126 | CFGFuncBuilder(CFGFunction *function) |
| 127 | : Builder(function->getContext()), function(function) {} |
| 128 | |
| 129 | /// Reset the insertion point to no location. Creating an operation without a |
| 130 | /// set insertion point is an error, but this can still be useful when the |
| 131 | /// current insertion point a builder refers to is being removed. |
| 132 | void clearInsertionPoint() { |
| 133 | this->block = nullptr; |
| 134 | insertPoint = BasicBlock::iterator(); |
| 135 | } |
| 136 | |
Chris Lattner | 8174f3a | 2018-07-29 16:45:23 -0700 | [diff] [blame] | 137 | /// Set the insertion point to the specified location. |
| 138 | void setInsertionPoint(BasicBlock *block, BasicBlock::iterator insertPoint) { |
| 139 | assert(block->getFunction() == function && |
| 140 | "can't move to a different function"); |
| 141 | this->block = block; |
| 142 | this->insertPoint = insertPoint; |
| 143 | } |
| 144 | |
| 145 | /// Set the insertion point to the specified operation. |
| 146 | void setInsertionPoint(OperationInst *inst) { |
| 147 | setInsertionPoint(inst->getBlock(), BasicBlock::iterator(inst)); |
| 148 | } |
| 149 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 150 | /// Set the insertion point to the end of the specified block. |
| 151 | void setInsertionPoint(BasicBlock *block) { |
Chris Lattner | 8174f3a | 2018-07-29 16:45:23 -0700 | [diff] [blame] | 152 | setInsertionPoint(block, block->end()); |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 155 | // Add new basic block and set the insertion point to the end of it. |
| 156 | BasicBlock *createBlock(); |
| 157 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame^] | 158 | // TODO(clattner): remove this. |
| 159 | /// Create an operation at the current insertion point. |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 160 | OperationInst *createOperation(Identifier name, ArrayRef<CFGValue *> operands, |
| 161 | ArrayRef<Type *> resultTypes, |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 162 | ArrayRef<NamedAttribute> attributes) { |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame] | 163 | auto op = |
| 164 | OperationInst::create(name, operands, resultTypes, attributes, context); |
Chris Lattner | 8174f3a | 2018-07-29 16:45:23 -0700 | [diff] [blame] | 165 | block->getOperations().insert(insertPoint, op); |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 166 | return op; |
| 167 | } |
| 168 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame^] | 169 | /// Create an operation given the fields represented as an OperationState. |
| 170 | OperationInst *createOperation(const OperationState &state); |
| 171 | |
| 172 | // TODO(clattner): rework build to return an OperationState so the |
| 173 | // implementations can moved out of line. |
| 174 | /// Create operation of specific op type at the current insertion point. |
| 175 | template <typename OpTy, typename... Args> |
| 176 | OpPointer<OpTy> create(Args... args) { |
| 177 | return OpTy::build(this, args...); |
| 178 | } |
| 179 | |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 180 | OperationInst *cloneOperation(const OperationInst &srcOpInst) { |
| 181 | auto *op = srcOpInst.clone(); |
| 182 | block->getOperations().insert(insertPoint, op); |
| 183 | return op; |
| 184 | } |
| 185 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 186 | // Terminators. |
| 187 | |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 188 | ReturnInst *createReturnInst(ArrayRef<CFGValue *> operands) { |
| 189 | return insertTerminator(ReturnInst::create(operands)); |
| 190 | } |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 191 | |
| 192 | BranchInst *createBranchInst(BasicBlock *dest) { |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 193 | return insertTerminator(BranchInst::create(dest)); |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 194 | } |
| 195 | |
James Molloy | 4f78837 | 2018-07-24 15:01:27 -0700 | [diff] [blame] | 196 | CondBranchInst *createCondBranchInst(CFGValue *condition, |
| 197 | BasicBlock *trueDest, |
| 198 | BasicBlock *falseDest) { |
| 199 | return insertTerminator( |
| 200 | CondBranchInst::create(condition, trueDest, falseDest)); |
| 201 | } |
| 202 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 203 | private: |
| 204 | template <typename T> |
| 205 | T *insertTerminator(T *term) { |
| 206 | block->setTerminator(term); |
| 207 | return term; |
| 208 | } |
| 209 | |
| 210 | CFGFunction *function; |
| 211 | BasicBlock *block = nullptr; |
| 212 | BasicBlock::iterator insertPoint; |
| 213 | }; |
| 214 | |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 215 | /// This class helps build an MLFunction. Statements that are created are |
| 216 | /// automatically inserted at an insertion point or added to the current |
| 217 | /// statement block. |
| 218 | class MLFuncBuilder : public Builder { |
| 219 | public: |
Tatiana Shpeisman | d880b35 | 2018-07-31 23:14:16 -0700 | [diff] [blame] | 220 | /// Create ML function builder and set insertion point to the given |
| 221 | /// statement block, that is, given ML function, for statement or if statement |
| 222 | /// clause. |
| 223 | MLFuncBuilder(StmtBlock *block) |
| 224 | : Builder(block->findFunction()->getContext()) { |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 225 | setInsertionPoint(block); |
| 226 | } |
| 227 | |
| 228 | /// Reset the insertion point to no location. Creating an operation without a |
| 229 | /// set insertion point is an error, but this can still be useful when the |
| 230 | /// current insertion point a builder refers to is being removed. |
| 231 | void clearInsertionPoint() { |
| 232 | this->block = nullptr; |
| 233 | insertPoint = StmtBlock::iterator(); |
| 234 | } |
| 235 | |
Tatiana Shpeisman | d880b35 | 2018-07-31 23:14:16 -0700 | [diff] [blame] | 236 | /// Set the insertion point to the specified location. |
| 237 | /// Unlike CFGFuncBuilder, MLFuncBuilder allows to set insertion |
| 238 | /// point to a different function. |
| 239 | void setInsertionPoint(StmtBlock *block, StmtBlock::iterator insertPoint) { |
| 240 | // TODO: check that insertPoint is in this rather than some other block. |
| 241 | this->block = block; |
| 242 | this->insertPoint = insertPoint; |
| 243 | } |
| 244 | |
| 245 | /// Set the insertion point to the specified operation. |
| 246 | void setInsertionPoint(OperationStmt *stmt) { |
| 247 | setInsertionPoint(stmt->getBlock(), StmtBlock::iterator(stmt)); |
| 248 | } |
| 249 | |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 250 | /// Set the insertion point to the end of the specified block. |
| 251 | void setInsertionPoint(StmtBlock *block) { |
| 252 | this->block = block; |
| 253 | insertPoint = block->end(); |
| 254 | } |
| 255 | |
Uday Bondhugula | 1598495 | 2018-08-01 22:36:12 -0700 | [diff] [blame] | 256 | /// Set the insertion point at the beginning of the specified block. |
| 257 | void setInsertionPointAtStart(StmtBlock *block) { |
| 258 | this->block = block; |
| 259 | insertPoint = block->begin(); |
| 260 | } |
| 261 | |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame] | 262 | /// Get the current insertion point of the builder. |
| 263 | StmtBlock::iterator getInsertionPoint() const { return insertPoint; } |
| 264 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame^] | 265 | // TODO(clattner): remove this. |
Tatiana Shpeisman | 60bf7be | 2018-07-26 18:09:20 -0700 | [diff] [blame] | 266 | OperationStmt *createOperation(Identifier name, ArrayRef<MLValue *> operands, |
| 267 | ArrayRef<Type *> resultTypes, |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 268 | ArrayRef<NamedAttribute> attributes) { |
Uday Bondhugula | 9f7754e | 2018-07-31 14:26:07 -0700 | [diff] [blame] | 269 | auto *op = |
Tatiana Shpeisman | 60bf7be | 2018-07-26 18:09:20 -0700 | [diff] [blame] | 270 | OperationStmt::create(name, operands, resultTypes, attributes, context); |
Uday Bondhugula | 9f7754e | 2018-07-31 14:26:07 -0700 | [diff] [blame] | 271 | block->getStatements().insert(insertPoint, op); |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 272 | return op; |
| 273 | } |
| 274 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame^] | 275 | /// Create an operation given the fields represented as an OperationState. |
| 276 | OperationStmt *createOperation(const OperationState &state); |
| 277 | |
| 278 | // TODO(clattner): rework build to return an OperationState so the |
| 279 | // implementations can moved out of line. |
Jacques Pienaar | ac86d10 | 2018-08-03 08:16:37 -0700 | [diff] [blame] | 280 | // Create operation of specific op type at the current insertion point. |
| 281 | template <typename OpTy, typename... Args> |
| 282 | OpPointer<OpTy> create(Args... args) { |
| 283 | return OpTy::build(this, args...); |
| 284 | } |
| 285 | |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame] | 286 | Statement *clone(const Statement &stmt) { |
Uday Bondhugula | 134154e | 2018-08-06 18:40:34 -0700 | [diff] [blame] | 287 | Statement *cloneStmt = stmt.clone(); |
| 288 | block->getStatements().insert(insertPoint, cloneStmt); |
| 289 | return cloneStmt; |
Uday Bondhugula | 84b8095 | 2018-08-03 13:22:26 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Chris Lattner | 1604e47 | 2018-07-23 08:42:19 -0700 | [diff] [blame] | 292 | // 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] | 293 | ForStmt *createFor(AffineConstantExpr *lowerBound, |
| 294 | AffineConstantExpr *upperBound, |
| 295 | AffineConstantExpr *step = nullptr); |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 296 | |
| 297 | IfStmt *createIf() { |
Tatiana Shpeisman | d880b35 | 2018-07-31 23:14:16 -0700 | [diff] [blame] | 298 | auto *stmt = new IfStmt(); |
| 299 | block->getStatements().insert(insertPoint, stmt); |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 300 | return stmt; |
| 301 | } |
| 302 | |
| 303 | private: |
| 304 | StmtBlock *block = nullptr; |
| 305 | StmtBlock::iterator insertPoint; |
| 306 | }; |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 307 | |
| 308 | } // namespace mlir |
| 309 | |
| 310 | #endif |