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 | |
Jacques Pienaar | c03c695 | 2018-08-10 11:56:47 -0700 | [diff] [blame] | 62 | MemRefType *Builder::getMemRefType(ArrayRef<int> shape, Type *elementType, |
| 63 | ArrayRef<AffineMap *> affineMapComposition, |
| 64 | unsigned memorySpace) { |
| 65 | return MemRefType::get(shape, elementType, affineMapComposition, memorySpace); |
| 66 | } |
| 67 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 68 | VectorType *Builder::getVectorType(ArrayRef<unsigned> shape, |
| 69 | Type *elementType) { |
| 70 | return VectorType::get(shape, elementType); |
| 71 | } |
| 72 | |
| 73 | RankedTensorType *Builder::getTensorType(ArrayRef<int> shape, |
| 74 | Type *elementType) { |
| 75 | return RankedTensorType::get(shape, elementType); |
| 76 | } |
| 77 | |
| 78 | UnrankedTensorType *Builder::getTensorType(Type *elementType) { |
| 79 | return UnrankedTensorType::get(elementType); |
| 80 | } |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 81 | |
| 82 | //===----------------------------------------------------------------------===// |
| 83 | // Attributes. |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | |
| 86 | BoolAttr *Builder::getBoolAttr(bool value) { |
| 87 | return BoolAttr::get(value, context); |
| 88 | } |
| 89 | |
| 90 | IntegerAttr *Builder::getIntegerAttr(int64_t value) { |
| 91 | return IntegerAttr::get(value, context); |
| 92 | } |
| 93 | |
| 94 | FloatAttr *Builder::getFloatAttr(double value) { |
| 95 | return FloatAttr::get(value, context); |
| 96 | } |
| 97 | |
| 98 | StringAttr *Builder::getStringAttr(StringRef bytes) { |
| 99 | return StringAttr::get(bytes, context); |
| 100 | } |
| 101 | |
| 102 | ArrayAttr *Builder::getArrayAttr(ArrayRef<Attribute *> value) { |
| 103 | return ArrayAttr::get(value, context); |
| 104 | } |
| 105 | |
Uday Bondhugula | 6770171 | 2018-08-21 16:01:23 -0700 | [diff] [blame^] | 106 | AffineMapAttr *Builder::getAffineMapAttr(AffineMap *map) { |
| 107 | return AffineMapAttr::get(map, context); |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 108 | } |
| 109 | |
James Molloy | f0d2f44 | 2018-08-03 01:54:46 -0700 | [diff] [blame] | 110 | TypeAttr *Builder::getTypeAttr(Type *type) { |
| 111 | return TypeAttr::get(type, context); |
| 112 | } |
| 113 | |
Chris Lattner | 4613d9e | 2018-08-19 21:17:22 -0700 | [diff] [blame] | 114 | FunctionAttr *Builder::getFunctionAttr(Function *value) { |
| 115 | return FunctionAttr::get(value, context); |
| 116 | } |
| 117 | |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 118 | //===----------------------------------------------------------------------===// |
Uday Bondhugula | bc53562 | 2018-08-07 14:24:38 -0700 | [diff] [blame] | 119 | // Affine Expressions, Affine Maps, and Integet Sets. |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 120 | //===----------------------------------------------------------------------===// |
| 121 | |
| 122 | AffineMap *Builder::getAffineMap(unsigned dimCount, unsigned symbolCount, |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 123 | ArrayRef<AffineExpr *> results, |
| 124 | ArrayRef<AffineExpr *> rangeSizes) { |
| 125 | return AffineMap::get(dimCount, symbolCount, results, rangeSizes, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | AffineDimExpr *Builder::getDimExpr(unsigned position) { |
| 129 | return AffineDimExpr::get(position, context); |
| 130 | } |
| 131 | |
| 132 | AffineSymbolExpr *Builder::getSymbolExpr(unsigned position) { |
| 133 | return AffineSymbolExpr::get(position, context); |
| 134 | } |
| 135 | |
| 136 | AffineConstantExpr *Builder::getConstantExpr(int64_t constant) { |
| 137 | return AffineConstantExpr::get(constant, context); |
| 138 | } |
| 139 | |
| 140 | AffineExpr *Builder::getAddExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 141 | return AffineBinaryOpExpr::get(AffineExpr::Kind::Add, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | AffineExpr *Builder::getMulExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 145 | return AffineBinaryOpExpr::get(AffineExpr::Kind::Mul, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | AffineExpr *Builder::getModExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 149 | return AffineBinaryOpExpr::get(AffineExpr::Kind::Mod, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | AffineExpr *Builder::getFloorDivExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 153 | return AffineBinaryOpExpr::get(AffineExpr::Kind::FloorDiv, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | AffineExpr *Builder::getCeilDivExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 157 | return AffineBinaryOpExpr::get(AffineExpr::Kind::CeilDiv, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 158 | } |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 159 | |
Uday Bondhugula | bc53562 | 2018-08-07 14:24:38 -0700 | [diff] [blame] | 160 | IntegerSet *Builder::getIntegerSet(unsigned dimCount, unsigned symbolCount, |
| 161 | ArrayRef<AffineExpr *> constraints, |
| 162 | ArrayRef<bool> isEq) { |
| 163 | return IntegerSet::get(dimCount, symbolCount, constraints, isEq, context); |
| 164 | } |
| 165 | |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 166 | //===----------------------------------------------------------------------===// |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 167 | // CFG function elements. |
| 168 | //===----------------------------------------------------------------------===// |
| 169 | |
| 170 | // Basic block. |
| 171 | BasicBlock *CFGFuncBuilder::createBlock() { |
| 172 | BasicBlock *b = new BasicBlock(); |
| 173 | function->push_back(b); |
| 174 | setInsertionPoint(b); |
| 175 | return b; |
| 176 | } |
| 177 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 178 | /// Create an operation given the fields represented as an OperationState. |
| 179 | OperationInst *CFGFuncBuilder::createOperation(const OperationState &state) { |
| 180 | SmallVector<CFGValue *, 8> operands; |
| 181 | operands.reserve(state.operands.size()); |
| 182 | for (auto elt : state.operands) |
| 183 | operands.push_back(cast<CFGValue>(elt)); |
| 184 | |
| 185 | auto *op = OperationInst::create(state.name, operands, state.types, |
| 186 | state.attributes, context); |
| 187 | block->getOperations().insert(insertPoint, op); |
| 188 | return op; |
| 189 | } |
| 190 | |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 191 | //===----------------------------------------------------------------------===// |
| 192 | // Statements. |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 193 | //===----------------------------------------------------------------------===// |
| 194 | |
Chris Lattner | eed6c4d | 2018-08-07 09:12:35 -0700 | [diff] [blame] | 195 | /// Create an operation given the fields represented as an OperationState. |
| 196 | OperationStmt *MLFuncBuilder::createOperation(const OperationState &state) { |
| 197 | SmallVector<MLValue *, 8> operands; |
| 198 | operands.reserve(state.operands.size()); |
| 199 | for (auto elt : state.operands) |
| 200 | operands.push_back(cast<MLValue>(elt)); |
| 201 | |
| 202 | auto *op = OperationStmt::create(state.name, operands, state.types, |
| 203 | state.attributes, context); |
| 204 | block->getStatements().insert(insertPoint, op); |
| 205 | return op; |
| 206 | } |
| 207 | |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 208 | ForStmt *MLFuncBuilder::createFor(AffineConstantExpr *lowerBound, |
| 209 | AffineConstantExpr *upperBound, |
Uday Bondhugula | 6770171 | 2018-08-21 16:01:23 -0700 | [diff] [blame^] | 210 | int64_t step) { |
Tatiana Shpeisman | 3838db7 | 2018-07-30 15:18:10 -0700 | [diff] [blame] | 211 | auto *stmt = new ForStmt(lowerBound, upperBound, step, context); |
Tatiana Shpeisman | d880b35 | 2018-07-31 23:14:16 -0700 | [diff] [blame] | 212 | block->getStatements().insert(insertPoint, stmt); |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 213 | return stmt; |
| 214 | } |