blob: 4424bd37a85160b123098a9ecb06417fc6640424 [file] [log] [blame]
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001//===- 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 Lattner1ac20cb2018-07-10 10:59:53 -070019#include "mlir/IR/AffineExpr.h"
20#include "mlir/IR/AffineMap.h"
21#include "mlir/IR/Attributes.h"
Chris Lattner158e0a3e2018-07-08 20:51:38 -070022#include "mlir/IR/Module.h"
23#include "mlir/IR/Types.h"
24using namespace mlir;
25
26Builder::Builder(Module *module) : context(module->getContext()) {}
27
Chris Lattner1ac20cb2018-07-10 10:59:53 -070028Identifier Builder::getIdentifier(StringRef str) {
29 return Identifier::get(str, context);
30}
31
32Module *Builder::createModule() { return new Module(context); }
33
34//===----------------------------------------------------------------------===//
Chris Lattner158e0a3e2018-07-08 20:51:38 -070035// Types.
Chris Lattner1ac20cb2018-07-10 10:59:53 -070036//===----------------------------------------------------------------------===//
37
Chris Lattnerc3251192018-07-27 13:09:58 -070038FloatType *Builder::getBF16Type() { return Type::getBF16(context); }
Chris Lattner158e0a3e2018-07-08 20:51:38 -070039
Chris Lattnerc3251192018-07-27 13:09:58 -070040FloatType *Builder::getF16Type() { return Type::getF16(context); }
Chris Lattner158e0a3e2018-07-08 20:51:38 -070041
Chris Lattnerc3251192018-07-27 13:09:58 -070042FloatType *Builder::getF32Type() { return Type::getF32(context); }
Chris Lattner158e0a3e2018-07-08 20:51:38 -070043
Chris Lattnerc3251192018-07-27 13:09:58 -070044FloatType *Builder::getF64Type() { return Type::getF64(context); }
Chris Lattner158e0a3e2018-07-08 20:51:38 -070045
Chris Lattnerc3251192018-07-27 13:09:58 -070046OtherType *Builder::getAffineIntType() { return Type::getAffineInt(context); }
Chris Lattner158e0a3e2018-07-08 20:51:38 -070047
Chris Lattnerc3251192018-07-27 13:09:58 -070048OtherType *Builder::getTFControlType() { return Type::getTFControl(context); }
Jacques Pienaarc0d69302018-07-27 11:07:12 -070049
James Molloy72b0cbe2018-08-01 12:55:27 -070050OtherType *Builder::getTFStringType() { return Type::getTFString(context); }
51
Chris Lattner158e0a3e2018-07-08 20:51:38 -070052IntegerType *Builder::getIntegerType(unsigned width) {
53 return Type::getInteger(width, context);
54}
55
56FunctionType *Builder::getFunctionType(ArrayRef<Type *> inputs,
57 ArrayRef<Type *> results) {
58 return FunctionType::get(inputs, results, context);
59}
60
61VectorType *Builder::getVectorType(ArrayRef<unsigned> shape,
62 Type *elementType) {
63 return VectorType::get(shape, elementType);
64}
65
66RankedTensorType *Builder::getTensorType(ArrayRef<int> shape,
67 Type *elementType) {
68 return RankedTensorType::get(shape, elementType);
69}
70
71UnrankedTensorType *Builder::getTensorType(Type *elementType) {
72 return UnrankedTensorType::get(elementType);
73}
Chris Lattner1ac20cb2018-07-10 10:59:53 -070074
75//===----------------------------------------------------------------------===//
76// Attributes.
77//===----------------------------------------------------------------------===//
78
79BoolAttr *Builder::getBoolAttr(bool value) {
80 return BoolAttr::get(value, context);
81}
82
83IntegerAttr *Builder::getIntegerAttr(int64_t value) {
84 return IntegerAttr::get(value, context);
85}
86
87FloatAttr *Builder::getFloatAttr(double value) {
88 return FloatAttr::get(value, context);
89}
90
91StringAttr *Builder::getStringAttr(StringRef bytes) {
92 return StringAttr::get(bytes, context);
93}
94
95ArrayAttr *Builder::getArrayAttr(ArrayRef<Attribute *> value) {
96 return ArrayAttr::get(value, context);
97}
98
MLIR Teamb61885d2018-07-18 16:29:21 -070099AffineMapAttr *Builder::getAffineMapAttr(AffineMap *value) {
100 return AffineMapAttr::get(value, context);
101}
102
James Molloyf0d2f442018-08-03 01:54:46 -0700103TypeAttr *Builder::getTypeAttr(Type *type) {
104 return TypeAttr::get(type, context);
105}
106
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700107//===----------------------------------------------------------------------===//
108// Affine Expressions and Affine Map.
109//===----------------------------------------------------------------------===//
110
111AffineMap *Builder::getAffineMap(unsigned dimCount, unsigned symbolCount,
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700112 ArrayRef<AffineExpr *> results,
113 ArrayRef<AffineExpr *> rangeSizes) {
114 return AffineMap::get(dimCount, symbolCount, results, rangeSizes, context);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700115}
116
117AffineDimExpr *Builder::getDimExpr(unsigned position) {
118 return AffineDimExpr::get(position, context);
119}
120
121AffineSymbolExpr *Builder::getSymbolExpr(unsigned position) {
122 return AffineSymbolExpr::get(position, context);
123}
124
125AffineConstantExpr *Builder::getConstantExpr(int64_t constant) {
126 return AffineConstantExpr::get(constant, context);
127}
128
129AffineExpr *Builder::getAddExpr(AffineExpr *lhs, AffineExpr *rhs) {
Uday Bondhugulac1faf662018-07-19 14:08:50 -0700130 return AffineBinaryOpExpr::get(AffineExpr::Kind::Add, lhs, rhs, context);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700131}
132
133AffineExpr *Builder::getMulExpr(AffineExpr *lhs, AffineExpr *rhs) {
Uday Bondhugulac1faf662018-07-19 14:08:50 -0700134 return AffineBinaryOpExpr::get(AffineExpr::Kind::Mul, lhs, rhs, context);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700135}
136
137AffineExpr *Builder::getModExpr(AffineExpr *lhs, AffineExpr *rhs) {
Uday Bondhugulac1faf662018-07-19 14:08:50 -0700138 return AffineBinaryOpExpr::get(AffineExpr::Kind::Mod, lhs, rhs, context);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700139}
140
141AffineExpr *Builder::getFloorDivExpr(AffineExpr *lhs, AffineExpr *rhs) {
Uday Bondhugulac1faf662018-07-19 14:08:50 -0700142 return AffineBinaryOpExpr::get(AffineExpr::Kind::FloorDiv, lhs, rhs, context);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700143}
144
145AffineExpr *Builder::getCeilDivExpr(AffineExpr *lhs, AffineExpr *rhs) {
Uday Bondhugulac1faf662018-07-19 14:08:50 -0700146 return AffineBinaryOpExpr::get(AffineExpr::Kind::CeilDiv, lhs, rhs, context);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700147}
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -0700148
149//===----------------------------------------------------------------------===//
Tatiana Shpeisman6708b452018-07-24 10:15:13 -0700150// CFG function elements.
151//===----------------------------------------------------------------------===//
152
153// Basic block.
154BasicBlock *CFGFuncBuilder::createBlock() {
155 BasicBlock *b = new BasicBlock();
156 function->push_back(b);
157 setInsertionPoint(b);
158 return b;
159}
160
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700161/// Create an operation given the fields represented as an OperationState.
162OperationInst *CFGFuncBuilder::createOperation(const OperationState &state) {
163 SmallVector<CFGValue *, 8> operands;
164 operands.reserve(state.operands.size());
165 for (auto elt : state.operands)
166 operands.push_back(cast<CFGValue>(elt));
167
168 auto *op = OperationInst::create(state.name, operands, state.types,
169 state.attributes, context);
170 block->getOperations().insert(insertPoint, op);
171 return op;
172}
173
Tatiana Shpeisman6708b452018-07-24 10:15:13 -0700174//===----------------------------------------------------------------------===//
175// Statements.
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -0700176//===----------------------------------------------------------------------===//
177
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700178/// Create an operation given the fields represented as an OperationState.
179OperationStmt *MLFuncBuilder::createOperation(const OperationState &state) {
180 SmallVector<MLValue *, 8> operands;
181 operands.reserve(state.operands.size());
182 for (auto elt : state.operands)
183 operands.push_back(cast<MLValue>(elt));
184
185 auto *op = OperationStmt::create(state.name, operands, state.types,
186 state.attributes, context);
187 block->getStatements().insert(insertPoint, op);
188 return op;
189}
190
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -0700191ForStmt *MLFuncBuilder::createFor(AffineConstantExpr *lowerBound,
192 AffineConstantExpr *upperBound,
193 AffineConstantExpr *step) {
194 if (!step)
195 step = getConstantExpr(1);
Tatiana Shpeisman3838db72018-07-30 15:18:10 -0700196 auto *stmt = new ForStmt(lowerBound, upperBound, step, context);
Tatiana Shpeismand880b352018-07-31 23:14:16 -0700197 block->getStatements().insert(insertPoint, stmt);
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -0700198 return stmt;
199}