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 | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 23 | #include "mlir/IR/Location.h" |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 24 | #include "mlir/IR/Module.h" |
| 25 | #include "mlir/IR/Types.h" |
| 26 | using namespace mlir; |
| 27 | |
| 28 | Builder::Builder(Module *module) : context(module->getContext()) {} |
| 29 | |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 30 | Identifier Builder::getIdentifier(StringRef str) { |
| 31 | return Identifier::get(str, context); |
| 32 | } |
| 33 | |
| 34 | Module *Builder::createModule() { return new Module(context); } |
| 35 | |
| 36 | //===----------------------------------------------------------------------===// |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 37 | // Locations. |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
| 40 | UnknownLoc *Builder::getUnknownLoc() { return UnknownLoc::get(context); } |
| 41 | |
| 42 | UniquedFilename Builder::getUniquedFilename(StringRef filename) { |
| 43 | return UniquedFilename::get(filename, context); |
| 44 | } |
| 45 | |
| 46 | FileLineColLoc *Builder::getFileLineColLoc(UniquedFilename filename, |
| 47 | unsigned line, unsigned column) { |
| 48 | return FileLineColLoc::get(filename, line, column, context); |
| 49 | } |
| 50 | |
| 51 | //===----------------------------------------------------------------------===// |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 52 | // Types. |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 53 | //===----------------------------------------------------------------------===// |
| 54 | |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 55 | FloatType *Builder::getBF16Type() { return Type::getBF16(context); } |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 56 | |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 57 | FloatType *Builder::getF16Type() { return Type::getF16(context); } |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 58 | |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 59 | FloatType *Builder::getF32Type() { return Type::getF32(context); } |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 60 | |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 61 | FloatType *Builder::getF64Type() { return Type::getF64(context); } |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 62 | |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 63 | OtherType *Builder::getAffineIntType() { return Type::getAffineInt(context); } |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 64 | |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 65 | OtherType *Builder::getTFControlType() { return Type::getTFControl(context); } |
Jacques Pienaar | c0d6930 | 2018-07-27 11:07:12 -0700 | [diff] [blame] | 66 | |
James Molloy | 72b0cbe | 2018-08-01 12:55:27 -0700 | [diff] [blame] | 67 | OtherType *Builder::getTFStringType() { return Type::getTFString(context); } |
| 68 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 69 | IntegerType *Builder::getIntegerType(unsigned width) { |
| 70 | return Type::getInteger(width, context); |
| 71 | } |
| 72 | |
| 73 | FunctionType *Builder::getFunctionType(ArrayRef<Type *> inputs, |
| 74 | ArrayRef<Type *> results) { |
| 75 | return FunctionType::get(inputs, results, context); |
| 76 | } |
| 77 | |
Jacques Pienaar | c03c695 | 2018-08-10 11:56:47 -0700 | [diff] [blame] | 78 | MemRefType *Builder::getMemRefType(ArrayRef<int> shape, Type *elementType, |
| 79 | ArrayRef<AffineMap *> affineMapComposition, |
| 80 | unsigned memorySpace) { |
| 81 | return MemRefType::get(shape, elementType, affineMapComposition, memorySpace); |
| 82 | } |
| 83 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 84 | VectorType *Builder::getVectorType(ArrayRef<unsigned> shape, |
| 85 | Type *elementType) { |
| 86 | return VectorType::get(shape, elementType); |
| 87 | } |
| 88 | |
| 89 | RankedTensorType *Builder::getTensorType(ArrayRef<int> shape, |
| 90 | Type *elementType) { |
| 91 | return RankedTensorType::get(shape, elementType); |
| 92 | } |
| 93 | |
| 94 | UnrankedTensorType *Builder::getTensorType(Type *elementType) { |
| 95 | return UnrankedTensorType::get(elementType); |
| 96 | } |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 97 | |
| 98 | //===----------------------------------------------------------------------===// |
| 99 | // Attributes. |
| 100 | //===----------------------------------------------------------------------===// |
| 101 | |
| 102 | BoolAttr *Builder::getBoolAttr(bool value) { |
| 103 | return BoolAttr::get(value, context); |
| 104 | } |
| 105 | |
| 106 | IntegerAttr *Builder::getIntegerAttr(int64_t value) { |
| 107 | return IntegerAttr::get(value, context); |
| 108 | } |
| 109 | |
| 110 | FloatAttr *Builder::getFloatAttr(double value) { |
| 111 | return FloatAttr::get(value, context); |
| 112 | } |
| 113 | |
| 114 | StringAttr *Builder::getStringAttr(StringRef bytes) { |
| 115 | return StringAttr::get(bytes, context); |
| 116 | } |
| 117 | |
| 118 | ArrayAttr *Builder::getArrayAttr(ArrayRef<Attribute *> value) { |
| 119 | return ArrayAttr::get(value, context); |
| 120 | } |
| 121 | |
Uday Bondhugula | 6770171 | 2018-08-21 16:01:23 -0700 | [diff] [blame] | 122 | AffineMapAttr *Builder::getAffineMapAttr(AffineMap *map) { |
| 123 | return AffineMapAttr::get(map, context); |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 124 | } |
| 125 | |
James Molloy | f0d2f44 | 2018-08-03 01:54:46 -0700 | [diff] [blame] | 126 | TypeAttr *Builder::getTypeAttr(Type *type) { |
| 127 | return TypeAttr::get(type, context); |
| 128 | } |
| 129 | |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 130 | FunctionAttr *Builder::getFunctionAttr(const Function *value) { |
Chris Lattner | 4613d9e | 2018-08-19 21:17:22 -0700 | [diff] [blame] | 131 | return FunctionAttr::get(value, context); |
| 132 | } |
| 133 | |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 134 | //===----------------------------------------------------------------------===// |
Uday Bondhugula | bc53562 | 2018-08-07 14:24:38 -0700 | [diff] [blame] | 135 | // Affine Expressions, Affine Maps, and Integet Sets. |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 136 | //===----------------------------------------------------------------------===// |
| 137 | |
| 138 | AffineMap *Builder::getAffineMap(unsigned dimCount, unsigned symbolCount, |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 139 | ArrayRef<AffineExpr *> results, |
| 140 | ArrayRef<AffineExpr *> rangeSizes) { |
| 141 | return AffineMap::get(dimCount, symbolCount, results, rangeSizes, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | AffineDimExpr *Builder::getDimExpr(unsigned position) { |
| 145 | return AffineDimExpr::get(position, context); |
| 146 | } |
| 147 | |
| 148 | AffineSymbolExpr *Builder::getSymbolExpr(unsigned position) { |
| 149 | return AffineSymbolExpr::get(position, context); |
| 150 | } |
| 151 | |
| 152 | AffineConstantExpr *Builder::getConstantExpr(int64_t constant) { |
| 153 | return AffineConstantExpr::get(constant, context); |
| 154 | } |
| 155 | |
| 156 | AffineExpr *Builder::getAddExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 157 | return AffineBinaryOpExpr::get(AffineExpr::Kind::Add, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | AffineExpr *Builder::getMulExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 161 | return AffineBinaryOpExpr::get(AffineExpr::Kind::Mul, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | AffineExpr *Builder::getModExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 165 | return AffineBinaryOpExpr::get(AffineExpr::Kind::Mod, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | AffineExpr *Builder::getFloorDivExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 169 | return AffineBinaryOpExpr::get(AffineExpr::Kind::FloorDiv, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | AffineExpr *Builder::getCeilDivExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 173 | return AffineBinaryOpExpr::get(AffineExpr::Kind::CeilDiv, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 174 | } |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 175 | |
Uday Bondhugula | bc53562 | 2018-08-07 14:24:38 -0700 | [diff] [blame] | 176 | IntegerSet *Builder::getIntegerSet(unsigned dimCount, unsigned symbolCount, |
| 177 | ArrayRef<AffineExpr *> constraints, |
| 178 | ArrayRef<bool> isEq) { |
| 179 | return IntegerSet::get(dimCount, symbolCount, constraints, isEq, context); |
| 180 | } |
| 181 | |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 182 | AffineMap *Builder::getConstantMap(int64_t val) { |
| 183 | return AffineMap::get(0, 0, getConstantExpr(val), {}, context); |
| 184 | } |
| 185 | |
| 186 | AffineMap *Builder::getDimIdentityMap() { |
| 187 | return AffineMap::get(1, 0, getDimExpr(0), {}, context); |
| 188 | } |
| 189 | |
| 190 | AffineMap *Builder::getSymbolIdentityMap() { |
| 191 | return AffineMap::get(0, 1, getSymbolExpr(0), {}, context); |
| 192 | } |
| 193 | |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 194 | //===----------------------------------------------------------------------===// |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 195 | // CFG function elements. |
| 196 | //===----------------------------------------------------------------------===// |
| 197 | |
Chris Lattner | 8a9310a | 2018-08-24 21:13:19 -0700 | [diff] [blame] | 198 | /// Add new basic block and set the insertion point to the end of it. If an |
| 199 | /// 'insertBefore' basic block is passed, the block will be placed before the |
| 200 | /// specified block. If not, the block will be appended to the end of the |
| 201 | /// current function. |
| 202 | BasicBlock *CFGFuncBuilder::createBlock(BasicBlock *insertBefore) { |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 203 | BasicBlock *b = new BasicBlock(); |
Chris Lattner | 8a9310a | 2018-08-24 21:13:19 -0700 | [diff] [blame] | 204 | |
| 205 | // If we are supposed to insert before a specific block, do so, otherwise add |
| 206 | // the block to the end of the function. |
| 207 | if (insertBefore) |
| 208 | function->getBlocks().insert(CFGFunction::iterator(insertBefore), b); |
| 209 | else |
| 210 | function->push_back(b); |
| 211 | |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 212 | setInsertionPoint(b); |
| 213 | return b; |
| 214 | } |
| 215 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 216 | /// Create an operation given the fields represented as an OperationState. |
| 217 | OperationInst *CFGFuncBuilder::createOperation(const OperationState &state) { |
| 218 | SmallVector<CFGValue *, 8> operands; |
| 219 | operands.reserve(state.operands.size()); |
| 220 | for (auto elt : state.operands) |
| 221 | operands.push_back(cast<CFGValue>(elt)); |
| 222 | |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 223 | auto *op = OperationInst::create(state.location, state.name, operands, |
| 224 | state.types, state.attributes, context); |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 225 | block->getOperations().insert(insertPoint, op); |
| 226 | return op; |
| 227 | } |
| 228 | |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 229 | //===----------------------------------------------------------------------===// |
| 230 | // Statements. |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 231 | //===----------------------------------------------------------------------===// |
| 232 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 233 | /// Create an operation given the fields represented as an OperationState. |
| 234 | OperationStmt *MLFuncBuilder::createOperation(const OperationState &state) { |
| 235 | SmallVector<MLValue *, 8> operands; |
| 236 | operands.reserve(state.operands.size()); |
| 237 | for (auto elt : state.operands) |
| 238 | operands.push_back(cast<MLValue>(elt)); |
| 239 | |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 240 | auto *op = OperationStmt::create(state.location, state.name, operands, |
| 241 | state.types, state.attributes, context); |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 242 | block->getStatements().insert(insertPoint, op); |
| 243 | return op; |
| 244 | } |
| 245 | |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 246 | ForStmt *MLFuncBuilder::createFor(Location *location, |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 247 | ArrayRef<MLValue *> lbOperands, |
| 248 | AffineMap *lbMap, |
| 249 | ArrayRef<MLValue *> ubOperands, |
| 250 | AffineMap *ubMap, int64_t step) { |
| 251 | auto *stmt = ForStmt::create(location, lbOperands, lbMap, ubOperands, ubMap, |
| 252 | step, context); |
Chris Lattner | 1628fa0 | 2018-08-23 14:32:25 -0700 | [diff] [blame] | 253 | block->getStatements().insert(insertPoint, stmt); |
| 254 | return stmt; |
| 255 | } |
| 256 | |
Tatiana Shpeisman | c6aa35b | 2018-08-28 15:26:20 -0700 | [diff] [blame] | 257 | IfStmt *MLFuncBuilder::createIf(Location *location, |
| 258 | ArrayRef<MLValue *> operands, IntegerSet *set) { |
| 259 | auto *stmt = IfStmt::create(location, operands, set); |
Tatiana Shpeisman | d880b35 | 2018-07-31 23:14:16 -0700 | [diff] [blame] | 260 | block->getStatements().insert(insertPoint, stmt); |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 261 | return stmt; |
| 262 | } |