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" |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 22 | #include "mlir/IR/Module.h" |
| 23 | #include "mlir/IR/Types.h" |
| 24 | using namespace mlir; |
| 25 | |
| 26 | Builder::Builder(Module *module) : context(module->getContext()) {} |
| 27 | |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 28 | Identifier Builder::getIdentifier(StringRef str) { |
| 29 | return Identifier::get(str, context); |
| 30 | } |
| 31 | |
| 32 | Module *Builder::createModule() { return new Module(context); } |
| 33 | |
| 34 | //===----------------------------------------------------------------------===// |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 35 | // Types. |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 36 | //===----------------------------------------------------------------------===// |
| 37 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 38 | PrimitiveType *Builder::getAffineIntType() { |
| 39 | return Type::getAffineInt(context); |
| 40 | } |
| 41 | |
| 42 | PrimitiveType *Builder::getBF16Type() { return Type::getBF16(context); } |
| 43 | |
| 44 | PrimitiveType *Builder::getF16Type() { return Type::getF16(context); } |
| 45 | |
| 46 | PrimitiveType *Builder::getF32Type() { return Type::getF32(context); } |
| 47 | |
| 48 | PrimitiveType *Builder::getF64Type() { return Type::getF64(context); } |
| 49 | |
Jacques Pienaar | c0d6930 | 2018-07-27 11:07:12 -0700 | [diff] [blame^] | 50 | PrimitiveType *Builder::getTFControlType() { |
| 51 | return Type::getTFControl(context); |
| 52 | } |
| 53 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 54 | IntegerType *Builder::getIntegerType(unsigned width) { |
| 55 | return Type::getInteger(width, context); |
| 56 | } |
| 57 | |
| 58 | FunctionType *Builder::getFunctionType(ArrayRef<Type *> inputs, |
| 59 | ArrayRef<Type *> results) { |
| 60 | return FunctionType::get(inputs, results, context); |
| 61 | } |
| 62 | |
| 63 | VectorType *Builder::getVectorType(ArrayRef<unsigned> shape, |
| 64 | Type *elementType) { |
| 65 | return VectorType::get(shape, elementType); |
| 66 | } |
| 67 | |
| 68 | RankedTensorType *Builder::getTensorType(ArrayRef<int> shape, |
| 69 | Type *elementType) { |
| 70 | return RankedTensorType::get(shape, elementType); |
| 71 | } |
| 72 | |
| 73 | UnrankedTensorType *Builder::getTensorType(Type *elementType) { |
| 74 | return UnrankedTensorType::get(elementType); |
| 75 | } |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 76 | |
| 77 | //===----------------------------------------------------------------------===// |
| 78 | // Attributes. |
| 79 | //===----------------------------------------------------------------------===// |
| 80 | |
| 81 | BoolAttr *Builder::getBoolAttr(bool value) { |
| 82 | return BoolAttr::get(value, context); |
| 83 | } |
| 84 | |
| 85 | IntegerAttr *Builder::getIntegerAttr(int64_t value) { |
| 86 | return IntegerAttr::get(value, context); |
| 87 | } |
| 88 | |
| 89 | FloatAttr *Builder::getFloatAttr(double value) { |
| 90 | return FloatAttr::get(value, context); |
| 91 | } |
| 92 | |
| 93 | StringAttr *Builder::getStringAttr(StringRef bytes) { |
| 94 | return StringAttr::get(bytes, context); |
| 95 | } |
| 96 | |
| 97 | ArrayAttr *Builder::getArrayAttr(ArrayRef<Attribute *> value) { |
| 98 | return ArrayAttr::get(value, context); |
| 99 | } |
| 100 | |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 101 | AffineMapAttr *Builder::getAffineMapAttr(AffineMap *value) { |
| 102 | return AffineMapAttr::get(value, context); |
| 103 | } |
| 104 | |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 105 | //===----------------------------------------------------------------------===// |
| 106 | // Affine Expressions and Affine Map. |
| 107 | //===----------------------------------------------------------------------===// |
| 108 | |
| 109 | AffineMap *Builder::getAffineMap(unsigned dimCount, unsigned symbolCount, |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 110 | ArrayRef<AffineExpr *> results, |
| 111 | ArrayRef<AffineExpr *> rangeSizes) { |
| 112 | return AffineMap::get(dimCount, symbolCount, results, rangeSizes, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | AffineDimExpr *Builder::getDimExpr(unsigned position) { |
| 116 | return AffineDimExpr::get(position, context); |
| 117 | } |
| 118 | |
| 119 | AffineSymbolExpr *Builder::getSymbolExpr(unsigned position) { |
| 120 | return AffineSymbolExpr::get(position, context); |
| 121 | } |
| 122 | |
| 123 | AffineConstantExpr *Builder::getConstantExpr(int64_t constant) { |
| 124 | return AffineConstantExpr::get(constant, context); |
| 125 | } |
| 126 | |
| 127 | AffineExpr *Builder::getAddExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 128 | return AffineBinaryOpExpr::get(AffineExpr::Kind::Add, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | AffineExpr *Builder::getMulExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 132 | return AffineBinaryOpExpr::get(AffineExpr::Kind::Mul, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | AffineExpr *Builder::getModExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 136 | return AffineBinaryOpExpr::get(AffineExpr::Kind::Mod, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | AffineExpr *Builder::getFloorDivExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 140 | return AffineBinaryOpExpr::get(AffineExpr::Kind::FloorDiv, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | AffineExpr *Builder::getCeilDivExpr(AffineExpr *lhs, AffineExpr *rhs) { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 144 | return AffineBinaryOpExpr::get(AffineExpr::Kind::CeilDiv, lhs, rhs, context); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 145 | } |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 146 | |
| 147 | //===----------------------------------------------------------------------===// |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 148 | // CFG function elements. |
| 149 | //===----------------------------------------------------------------------===// |
| 150 | |
| 151 | // Basic block. |
| 152 | BasicBlock *CFGFuncBuilder::createBlock() { |
| 153 | BasicBlock *b = new BasicBlock(); |
| 154 | function->push_back(b); |
| 155 | setInsertionPoint(b); |
| 156 | return b; |
| 157 | } |
| 158 | |
| 159 | //===----------------------------------------------------------------------===// |
| 160 | // Statements. |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 161 | //===----------------------------------------------------------------------===// |
| 162 | |
| 163 | ForStmt *MLFuncBuilder::createFor(AffineConstantExpr *lowerBound, |
| 164 | AffineConstantExpr *upperBound, |
| 165 | AffineConstantExpr *step) { |
| 166 | if (!step) |
| 167 | step = getConstantExpr(1); |
Tatiana Shpeisman | 6708b45 | 2018-07-24 10:15:13 -0700 | [diff] [blame] | 168 | auto *stmt = new ForStmt(lowerBound, upperBound, step); |
Tatiana Shpeisman | 1da50c4 | 2018-07-19 09:52:39 -0700 | [diff] [blame] | 169 | block->getStatements().push_back(stmt); |
| 170 | return stmt; |
| 171 | } |