Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 1 | //===- Builders.cpp - Helpers for constructing MLIR Classes ---------------===// |
| 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 | #include "mlir/IR/Builders.h" |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 19 | #include "mlir/IR/AffineExpr.h" |
| 20 | #include "mlir/IR/AffineMap.h" |
| 21 | #include "mlir/IR/Attributes.h" |
Uday Bondhugula | bc53562 | 2018-08-07 14:24:38 -0700 | [diff] [blame^] | 22 | #include "mlir/IR/IntegerSet.h" |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 23 | #include "mlir/IR/Module.h" |
| 24 | #include "mlir/IR/Types.h" |
| 25 | using namespace mlir; |
| 26 | |
| 27 | Builder::Builder(Module *module) : context(module->getContext()) {} |
| 28 | |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 29 | Identifier Builder::getIdentifier(StringRef str) { |
| 30 | return Identifier::get(str, context); |
| 31 | } |
| 32 | |
| 33 | Module *Builder::createModule() { return new Module(context); } |
| 34 | |
| 35 | //===----------------------------------------------------------------------===// |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 36 | // Types. |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 37 | //===----------------------------------------------------------------------===// |
| 38 | |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 39 | FloatType *Builder::getBF16Type() { return Type::getBF16(context); } |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 40 | |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 41 | FloatType *Builder::getF16Type() { return Type::getF16(context); } |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 42 | |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 43 | FloatType *Builder::getF32Type() { return Type::getF32(context); } |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 44 | |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 45 | FloatType *Builder::getF64Type() { return Type::getF64(context); } |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 46 | |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 47 | OtherType *Builder::getAffineIntType() { return Type::getAffineInt(context); } |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 48 | |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 49 | OtherType *Builder::getTFControlType() { return Type::getTFControl(context); } |
Jacques Pienaar | c0d6930 | 2018-07-27 11:07:12 -0700 | [diff] [blame] | 50 | |
James Molloy | 72b0cbe | 2018-08-01 12:55:27 -0700 | [diff] [blame] | 51 | OtherType *Builder::getTFStringType() { return Type::getTFString(context); } |
| 52 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 53 | IntegerType *Builder::getIntegerType(unsigned width) { |
| 54 | return Type::getInteger(width, context); |
| 55 | } |
| 56 | |
| 57 | FunctionType *Builder::getFunctionType(ArrayRef<Type *> inputs, |
| 58 | ArrayRef<Type *> results) { |
| 59 | return FunctionType::get(inputs, results, context); |
| 60 | } |
| 61 | |
| 62 | VectorType *Builder::getVectorType(ArrayRef<unsigned> shape, |
| 63 | Type *elementType) { |
| 64 | return VectorType::get(shape, elementType); |
| 65 | } |
| 66 | |
| 67 | RankedTensorType *Builder::getTensorType(ArrayRef<int> shape, |
| 68 | Type *elementType) { |
| 69 | return RankedTensorType::get(shape, elementType); |
| 70 | } |
| 71 | |
| 72 | UnrankedTensorType *Builder::getTensorType(Type *elementType) { |
| 73 | return UnrankedTensorType::get(elementType); |
| 74 | } |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 75 | |
| 76 | //===----------------------------------------------------------------------===// |
| 77 | // Attributes. |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | |
| 80 | BoolAttr *Builder::getBoolAttr(bool value) { |
| 81 | return BoolAttr::get(value, context); |
| 82 | } |
| 83 | |
| 84 | IntegerAttr *Builder::getIntegerAttr(int64_t value) { |
| 85 | return IntegerAttr::get(value, context); |
| 86 | } |
| 87 | |
| 88 | FloatAttr *Builder::getFloatAttr(double value) { |
| 89 | return FloatAttr::get(value, context); |
| 90 | } |
| 91 | |
| 92 | StringAttr *Builder::getStringAttr(StringRef bytes) { |
| 93 | return StringAttr::get(bytes, context); |
| 94 | } |
| 95 | |
| 96 | ArrayAttr *Builder::getArrayAttr(ArrayRef<Attribute *> value) { |
| 97 | return ArrayAttr::get(value, context); |
| 98 | } |
| 99 | |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 100 | AffineMapAttr *Builder::getAffineMapAttr(AffineMap *value) { |
| 101 | return AffineMapAttr::get(value, context); |
| 102 | } |
| 103 | |
James Molloy | f0d2f44 | 2018-08-03 01:54:46 -0700 | [diff] [blame] | 104 | TypeAttr *Builder::getTypeAttr(Type *type) { |
| 105 | return TypeAttr::get(type, context); |
| 106 | } |
| 107 | |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 108 | //===----------------------------------------------------------------------===// |
Uday Bondhugula | bc53562 | 2018-08-07 14:24:38 -0700 | [diff] [blame^] | 109 | // Affine Expressions, Affine Maps, and Integet Sets. |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 110 | //===----------------------------------------------------------------------===// |
| 111 | |
| 112 | AffineMap *Builder::getAffineMap(unsigned dimCount, unsigned symbolCount, |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 113 | ArrayRef<AffineExpr *> results, |
| 114 | ArrayRef<AffineExpr *> rangeSizes) { |
| 115 | return AffineMap::get(dimCount, symbolCount, results, rangeSizes, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | AffineDimExpr *Builder::getDimExpr(unsigned position) { |
| 119 | return AffineDimExpr::get(position, context); |
| 120 | } |
| 121 | |
| 122 | AffineSymbolExpr *Builder::getSymbolExpr(unsigned position) { |
| 123 | return AffineSymbolExpr::get(position, context); |
| 124 | } |
| 125 | |
| 126 | AffineConstantExpr *Builder::getConstantExpr(int64_t constant) { |
| 127 | return AffineConstantExpr::get(constant, context); |
| 128 | } |
| 129 | |
| 130 | AffineExpr *Builder::getAddExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 131 | return AffineBinaryOpExpr::get(AffineExpr::Kind::Add, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | AffineExpr *Builder::getMulExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 135 | return AffineBinaryOpExpr::get(AffineExpr::Kind::Mul, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | AffineExpr *Builder::getModExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 139 | return AffineBinaryOpExpr::get(AffineExpr::Kind::Mod, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | AffineExpr *Builder::getFloorDivExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 143 | return AffineBinaryOpExpr::get(AffineExpr::Kind::FloorDiv, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | AffineExpr *Builder::getCeilDivExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 147 | return AffineBinaryOpExpr::get(AffineExpr::Kind::CeilDiv, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 148 | } |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 149 | |
Uday Bondhugula | bc53562 | 2018-08-07 14:24:38 -0700 | [diff] [blame^] | 150 | IntegerSet *Builder::getIntegerSet(unsigned dimCount, unsigned symbolCount, |
| 151 | ArrayRef<AffineExpr *> constraints, |
| 152 | ArrayRef<bool> isEq) { |
| 153 | return IntegerSet::get(dimCount, symbolCount, constraints, isEq, context); |
| 154 | } |
| 155 | |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 156 | //===----------------------------------------------------------------------===// |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 157 | // CFG function elements. |
| 158 | //===----------------------------------------------------------------------===// |
| 159 | |
| 160 | // Basic block. |
| 161 | BasicBlock *CFGFuncBuilder::createBlock() { |
| 162 | BasicBlock *b = new BasicBlock(); |
| 163 | function->push_back(b); |
| 164 | setInsertionPoint(b); |
| 165 | return b; |
| 166 | } |
| 167 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 168 | /// Create an operation given the fields represented as an OperationState. |
| 169 | OperationInst *CFGFuncBuilder::createOperation(const OperationState &state) { |
| 170 | SmallVector<CFGValue *, 8> operands; |
| 171 | operands.reserve(state.operands.size()); |
| 172 | for (auto elt : state.operands) |
| 173 | operands.push_back(cast<CFGValue>(elt)); |
| 174 | |
| 175 | auto *op = OperationInst::create(state.name, operands, state.types, |
| 176 | state.attributes, context); |
| 177 | block->getOperations().insert(insertPoint, op); |
| 178 | return op; |
| 179 | } |
| 180 | |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 181 | //===----------------------------------------------------------------------===// |
| 182 | // Statements. |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 183 | //===----------------------------------------------------------------------===// |
| 184 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 185 | /// Create an operation given the fields represented as an OperationState. |
| 186 | OperationStmt *MLFuncBuilder::createOperation(const OperationState &state) { |
| 187 | SmallVector<MLValue *, 8> operands; |
| 188 | operands.reserve(state.operands.size()); |
| 189 | for (auto elt : state.operands) |
| 190 | operands.push_back(cast<MLValue>(elt)); |
| 191 | |
| 192 | auto *op = OperationStmt::create(state.name, operands, state.types, |
| 193 | state.attributes, context); |
| 194 | block->getStatements().insert(insertPoint, op); |
| 195 | return op; |
| 196 | } |
| 197 | |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 198 | ForStmt *MLFuncBuilder::createFor(AffineConstantExpr *lowerBound, |
| 199 | AffineConstantExpr *upperBound, |
| 200 | AffineConstantExpr *step) { |
| 201 | if (!step) |
| 202 | step = getConstantExpr(1); |
Tatiana Shpeisman | 3838db7 | 2018-07-30 15:18:10 -0700 | [diff] [blame] | 203 | auto *stmt = new ForStmt(lowerBound, upperBound, step, context); |
Tatiana Shpeisman | d880b35 | 2018-07-31 23:14:16 -0700 | [diff] [blame] | 204 | block->getStatements().insert(insertPoint, stmt); |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 205 | return stmt; |
| 206 | } |