blob: 5fce94cb4ad5080915f22c14e5f4b8f9da498e0a [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"
Uday Bondhugulabc535622018-08-07 14:24:38 -070022#include "mlir/IR/IntegerSet.h"
Chris Lattner158e0a3e2018-07-08 20:51:38 -070023#include "mlir/IR/Module.h"
24#include "mlir/IR/Types.h"
25using namespace mlir;
26
27Builder::Builder(Module *module) : context(module->getContext()) {}
28
Chris Lattner1ac20cb2018-07-10 10:59:53 -070029Identifier Builder::getIdentifier(StringRef str) {
30 return Identifier::get(str, context);
31}
32
33Module *Builder::createModule() { return new Module(context); }
34
35//===----------------------------------------------------------------------===//
Chris Lattner158e0a3e2018-07-08 20:51:38 -070036// Types.
Chris Lattner1ac20cb2018-07-10 10:59:53 -070037//===----------------------------------------------------------------------===//
38
Chris Lattnerc3251192018-07-27 13:09:58 -070039FloatType *Builder::getBF16Type() { return Type::getBF16(context); }
Chris Lattner158e0a3e2018-07-08 20:51:38 -070040
Chris Lattnerc3251192018-07-27 13:09:58 -070041FloatType *Builder::getF16Type() { return Type::getF16(context); }
Chris Lattner158e0a3e2018-07-08 20:51:38 -070042
Chris Lattnerc3251192018-07-27 13:09:58 -070043FloatType *Builder::getF32Type() { return Type::getF32(context); }
Chris Lattner158e0a3e2018-07-08 20:51:38 -070044
Chris Lattnerc3251192018-07-27 13:09:58 -070045FloatType *Builder::getF64Type() { return Type::getF64(context); }
Chris Lattner158e0a3e2018-07-08 20:51:38 -070046
Chris Lattnerc3251192018-07-27 13:09:58 -070047OtherType *Builder::getAffineIntType() { return Type::getAffineInt(context); }
Chris Lattner158e0a3e2018-07-08 20:51:38 -070048
Chris Lattnerc3251192018-07-27 13:09:58 -070049OtherType *Builder::getTFControlType() { return Type::getTFControl(context); }
Jacques Pienaarc0d69302018-07-27 11:07:12 -070050
James Molloy72b0cbe2018-08-01 12:55:27 -070051OtherType *Builder::getTFStringType() { return Type::getTFString(context); }
52
Chris Lattner158e0a3e2018-07-08 20:51:38 -070053IntegerType *Builder::getIntegerType(unsigned width) {
54 return Type::getInteger(width, context);
55}
56
57FunctionType *Builder::getFunctionType(ArrayRef<Type *> inputs,
58 ArrayRef<Type *> results) {
59 return FunctionType::get(inputs, results, context);
60}
61
Jacques Pienaarc03c6952018-08-10 11:56:47 -070062MemRefType *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 Lattner158e0a3e2018-07-08 20:51:38 -070068VectorType *Builder::getVectorType(ArrayRef<unsigned> shape,
69 Type *elementType) {
70 return VectorType::get(shape, elementType);
71}
72
73RankedTensorType *Builder::getTensorType(ArrayRef<int> shape,
74 Type *elementType) {
75 return RankedTensorType::get(shape, elementType);
76}
77
78UnrankedTensorType *Builder::getTensorType(Type *elementType) {
79 return UnrankedTensorType::get(elementType);
80}
Chris Lattner1ac20cb2018-07-10 10:59:53 -070081
82//===----------------------------------------------------------------------===//
83// Attributes.
84//===----------------------------------------------------------------------===//
85
86BoolAttr *Builder::getBoolAttr(bool value) {
87 return BoolAttr::get(value, context);
88}
89
90IntegerAttr *Builder::getIntegerAttr(int64_t value) {
91 return IntegerAttr::get(value, context);
92}
93
94FloatAttr *Builder::getFloatAttr(double value) {
95 return FloatAttr::get(value, context);
96}
97
98StringAttr *Builder::getStringAttr(StringRef bytes) {
99 return StringAttr::get(bytes, context);
100}
101
102ArrayAttr *Builder::getArrayAttr(ArrayRef<Attribute *> value) {
103 return ArrayAttr::get(value, context);
104}
105
MLIR Teamb61885d2018-07-18 16:29:21 -0700106AffineMapAttr *Builder::getAffineMapAttr(AffineMap *value) {
107 return AffineMapAttr::get(value, context);
108}
109
James Molloyf0d2f442018-08-03 01:54:46 -0700110TypeAttr *Builder::getTypeAttr(Type *type) {
111 return TypeAttr::get(type, context);
112}
113
Chris Lattner4613d9e2018-08-19 21:17:22 -0700114FunctionAttr *Builder::getFunctionAttr(Function *value) {
115 return FunctionAttr::get(value, context);
116}
117
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700118//===----------------------------------------------------------------------===//
Uday Bondhugulabc535622018-08-07 14:24:38 -0700119// Affine Expressions, Affine Maps, and Integet Sets.
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700120//===----------------------------------------------------------------------===//
121
122AffineMap *Builder::getAffineMap(unsigned dimCount, unsigned symbolCount,
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700123 ArrayRef<AffineExpr *> results,
124 ArrayRef<AffineExpr *> rangeSizes) {
125 return AffineMap::get(dimCount, symbolCount, results, rangeSizes, context);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700126}
127
128AffineDimExpr *Builder::getDimExpr(unsigned position) {
129 return AffineDimExpr::get(position, context);
130}
131
132AffineSymbolExpr *Builder::getSymbolExpr(unsigned position) {
133 return AffineSymbolExpr::get(position, context);
134}
135
136AffineConstantExpr *Builder::getConstantExpr(int64_t constant) {
137 return AffineConstantExpr::get(constant, context);
138}
139
140AffineExpr *Builder::getAddExpr(AffineExpr *lhs, AffineExpr *rhs) {
Uday Bondhugulac1faf662018-07-19 14:08:50 -0700141 return AffineBinaryOpExpr::get(AffineExpr::Kind::Add, lhs, rhs, context);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700142}
143
144AffineExpr *Builder::getMulExpr(AffineExpr *lhs, AffineExpr *rhs) {
Uday Bondhugulac1faf662018-07-19 14:08:50 -0700145 return AffineBinaryOpExpr::get(AffineExpr::Kind::Mul, lhs, rhs, context);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700146}
147
148AffineExpr *Builder::getModExpr(AffineExpr *lhs, AffineExpr *rhs) {
Uday Bondhugulac1faf662018-07-19 14:08:50 -0700149 return AffineBinaryOpExpr::get(AffineExpr::Kind::Mod, lhs, rhs, context);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700150}
151
152AffineExpr *Builder::getFloorDivExpr(AffineExpr *lhs, AffineExpr *rhs) {
Uday Bondhugulac1faf662018-07-19 14:08:50 -0700153 return AffineBinaryOpExpr::get(AffineExpr::Kind::FloorDiv, lhs, rhs, context);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700154}
155
156AffineExpr *Builder::getCeilDivExpr(AffineExpr *lhs, AffineExpr *rhs) {
Uday Bondhugulac1faf662018-07-19 14:08:50 -0700157 return AffineBinaryOpExpr::get(AffineExpr::Kind::CeilDiv, lhs, rhs, context);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700158}
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -0700159
Uday Bondhugulabc535622018-08-07 14:24:38 -0700160IntegerSet *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 Shpeisman1da50c42018-07-19 09:52:39 -0700166//===----------------------------------------------------------------------===//
Tatiana Shpeisman6708b452018-07-24 10:15:13 -0700167// CFG function elements.
168//===----------------------------------------------------------------------===//
169
170// Basic block.
171BasicBlock *CFGFuncBuilder::createBlock() {
172 BasicBlock *b = new BasicBlock();
173 function->push_back(b);
174 setInsertionPoint(b);
175 return b;
176}
177
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700178/// Create an operation given the fields represented as an OperationState.
179OperationInst *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 Shpeisman6708b452018-07-24 10:15:13 -0700191//===----------------------------------------------------------------------===//
192// Statements.
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -0700193//===----------------------------------------------------------------------===//
194
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700195/// Create an operation given the fields represented as an OperationState.
196OperationStmt *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 Shpeisman1da50c42018-07-19 09:52:39 -0700208ForStmt *MLFuncBuilder::createFor(AffineConstantExpr *lowerBound,
209 AffineConstantExpr *upperBound,
210 AffineConstantExpr *step) {
211 if (!step)
212 step = getConstantExpr(1);
Tatiana Shpeisman3838db72018-07-30 15:18:10 -0700213 auto *stmt = new ForStmt(lowerBound, upperBound, step, context);
Tatiana Shpeismand880b352018-07-31 23:14:16 -0700214 block->getStatements().insert(insertPoint, stmt);
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -0700215 return stmt;
216}