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; |
| 28 | class Type; |
| 29 | class PrimitiveType; |
| 30 | class IntegerType; |
| 31 | class FunctionType; |
| 32 | class VectorType; |
| 33 | class RankedTensorType; |
| 34 | class UnrankedTensorType; |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 35 | class BoolAttr; |
| 36 | class IntegerAttr; |
| 37 | class FloatAttr; |
| 38 | class StringAttr; |
| 39 | class ArrayAttr; |
| 40 | class AffineMap; |
| 41 | class AffineExpr; |
| 42 | class AffineConstantExpr; |
| 43 | class AffineDimExpr; |
| 44 | class AffineSymbolExpr; |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 45 | |
| 46 | /// This class is a general helper class for creating context-global objects |
| 47 | /// like types, attributes, and affine expressions. |
| 48 | class Builder { |
| 49 | public: |
| 50 | explicit Builder(MLIRContext *context) : context(context) {} |
| 51 | explicit Builder(Module *module); |
| 52 | |
| 53 | MLIRContext *getContext() const { return context; } |
| 54 | |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 55 | Identifier getIdentifier(StringRef str); |
| 56 | Module *createModule(); |
| 57 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 58 | // Types. |
| 59 | PrimitiveType *getAffineIntType(); |
| 60 | PrimitiveType *getBF16Type(); |
| 61 | PrimitiveType *getF16Type(); |
| 62 | PrimitiveType *getF32Type(); |
| 63 | PrimitiveType *getF64Type(); |
| 64 | IntegerType *getIntegerType(unsigned width); |
| 65 | FunctionType *getFunctionType(ArrayRef<Type *> inputs, |
| 66 | ArrayRef<Type *> results); |
| 67 | VectorType *getVectorType(ArrayRef<unsigned> shape, Type *elementType); |
| 68 | RankedTensorType *getTensorType(ArrayRef<int> shape, Type *elementType); |
| 69 | UnrankedTensorType *getTensorType(Type *elementType); |
| 70 | |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 71 | // Attributes. |
| 72 | BoolAttr *getBoolAttr(bool value); |
| 73 | IntegerAttr *getIntegerAttr(int64_t value); |
| 74 | FloatAttr *getFloatAttr(double value); |
| 75 | StringAttr *getStringAttr(StringRef bytes); |
| 76 | ArrayAttr *getArrayAttr(ArrayRef<Attribute *> value); |
| 77 | |
| 78 | // Affine Expressions and Affine Map. |
| 79 | AffineMap *getAffineMap(unsigned dimCount, unsigned symbolCount, |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 80 | ArrayRef<AffineExpr *> results, |
| 81 | ArrayRef<AffineExpr *> rangeSizes); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 82 | AffineDimExpr *getDimExpr(unsigned position); |
| 83 | AffineSymbolExpr *getSymbolExpr(unsigned position); |
| 84 | AffineConstantExpr *getConstantExpr(int64_t constant); |
| 85 | AffineExpr *getAddExpr(AffineExpr *lhs, AffineExpr *rhs); |
| 86 | AffineExpr *getSubExpr(AffineExpr *lhs, AffineExpr *rhs); |
| 87 | AffineExpr *getMulExpr(AffineExpr *lhs, AffineExpr *rhs); |
| 88 | AffineExpr *getModExpr(AffineExpr *lhs, AffineExpr *rhs); |
| 89 | AffineExpr *getFloorDivExpr(AffineExpr *lhs, AffineExpr *rhs); |
| 90 | AffineExpr *getCeilDivExpr(AffineExpr *lhs, AffineExpr *rhs); |
| 91 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 92 | // TODO: Helpers for affine map/exprs, etc. |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 93 | protected: |
| 94 | MLIRContext *context; |
| 95 | }; |
| 96 | |
| 97 | /// This class helps build a CFGFunction. Instructions that are created are |
| 98 | /// automatically inserted at an insertion point or added to the current basic |
| 99 | /// block. |
| 100 | class CFGFuncBuilder : public Builder { |
| 101 | public: |
| 102 | CFGFuncBuilder(BasicBlock *block) |
| 103 | : Builder(block->getFunction()->getContext()), |
| 104 | function(block->getFunction()) { |
| 105 | setInsertionPoint(block); |
| 106 | } |
| 107 | CFGFuncBuilder(CFGFunction *function) |
| 108 | : Builder(function->getContext()), function(function) {} |
| 109 | |
| 110 | /// Reset the insertion point to no location. Creating an operation without a |
| 111 | /// set insertion point is an error, but this can still be useful when the |
| 112 | /// current insertion point a builder refers to is being removed. |
| 113 | void clearInsertionPoint() { |
| 114 | this->block = nullptr; |
| 115 | insertPoint = BasicBlock::iterator(); |
| 116 | } |
| 117 | |
| 118 | /// Set the insertion point to the end of the specified block. |
| 119 | void setInsertionPoint(BasicBlock *block) { |
| 120 | this->block = block; |
| 121 | insertPoint = block->end(); |
| 122 | } |
| 123 | |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame^] | 124 | OperationInst *createOperation(Identifier name, ArrayRef<CFGValue *> operands, |
| 125 | ArrayRef<Type *> resultTypes, |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 126 | ArrayRef<NamedAttribute> attributes) { |
Chris Lattner | 3b2ef76 | 2018-07-18 15:31:25 -0700 | [diff] [blame^] | 127 | auto op = |
| 128 | OperationInst::create(name, operands, resultTypes, attributes, context); |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 129 | block->getOperations().push_back(op); |
| 130 | return op; |
| 131 | } |
| 132 | |
| 133 | // Terminators. |
| 134 | |
| 135 | ReturnInst *createReturnInst() { return insertTerminator(new ReturnInst()); } |
| 136 | |
| 137 | BranchInst *createBranchInst(BasicBlock *dest) { |
| 138 | return insertTerminator(new BranchInst(dest)); |
| 139 | } |
| 140 | |
| 141 | private: |
| 142 | template <typename T> |
| 143 | T *insertTerminator(T *term) { |
| 144 | block->setTerminator(term); |
| 145 | return term; |
| 146 | } |
| 147 | |
| 148 | CFGFunction *function; |
| 149 | BasicBlock *block = nullptr; |
| 150 | BasicBlock::iterator insertPoint; |
| 151 | }; |
| 152 | |
Tatiana Shpeisman | 565b964 | 2018-07-16 11:47:09 -0700 | [diff] [blame] | 153 | /// This class helps build an MLFunction. Statements that are created are |
| 154 | /// automatically inserted at an insertion point or added to the current |
| 155 | /// statement block. |
| 156 | class MLFuncBuilder : public Builder { |
| 157 | public: |
| 158 | MLFuncBuilder(MLFunction *function) : Builder(function->getContext()) {} |
| 159 | |
| 160 | MLFuncBuilder(StmtBlock *block) : MLFuncBuilder(block->getFunction()) { |
| 161 | setInsertionPoint(block); |
| 162 | } |
| 163 | |
| 164 | /// Reset the insertion point to no location. Creating an operation without a |
| 165 | /// set insertion point is an error, but this can still be useful when the |
| 166 | /// current insertion point a builder refers to is being removed. |
| 167 | void clearInsertionPoint() { |
| 168 | this->block = nullptr; |
| 169 | insertPoint = StmtBlock::iterator(); |
| 170 | } |
| 171 | |
| 172 | /// Set the insertion point to the end of the specified block. |
| 173 | void setInsertionPoint(StmtBlock *block) { |
| 174 | this->block = block; |
| 175 | insertPoint = block->end(); |
| 176 | } |
| 177 | |
| 178 | OperationStmt *createOperation(Identifier name, |
| 179 | ArrayRef<NamedAttribute> attributes) { |
| 180 | auto op = new OperationStmt(name, attributes, context); |
| 181 | block->getStatements().push_back(op); |
| 182 | return op; |
| 183 | } |
| 184 | |
| 185 | ForStmt *createFor() { |
| 186 | auto stmt = new ForStmt(); |
| 187 | block->getStatements().push_back(stmt); |
| 188 | return stmt; |
| 189 | } |
| 190 | |
| 191 | IfStmt *createIf() { |
| 192 | auto stmt = new IfStmt(); |
| 193 | block->getStatements().push_back(stmt); |
| 194 | return stmt; |
| 195 | } |
| 196 | |
| 197 | private: |
| 198 | StmtBlock *block = nullptr; |
| 199 | StmtBlock::iterator insertPoint; |
| 200 | }; |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 201 | |
| 202 | } // namespace mlir |
| 203 | |
| 204 | #endif |