blob: 110e44041e34cb5f42d93bc34482c7a21e8ec72b [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
62VectorType *Builder::getVectorType(ArrayRef<unsigned> shape,
63 Type *elementType) {
64 return VectorType::get(shape, elementType);
65}
66
67RankedTensorType *Builder::getTensorType(ArrayRef<int> shape,
68 Type *elementType) {
69 return RankedTensorType::get(shape, elementType);
70}
71
72UnrankedTensorType *Builder::getTensorType(Type *elementType) {
73 return UnrankedTensorType::get(elementType);
74}
Chris Lattner1ac20cb2018-07-10 10:59:53 -070075
76//===----------------------------------------------------------------------===//
77// Attributes.
78//===----------------------------------------------------------------------===//
79
80BoolAttr *Builder::getBoolAttr(bool value) {
81 return BoolAttr::get(value, context);
82}
83
84IntegerAttr *Builder::getIntegerAttr(int64_t value) {
85 return IntegerAttr::get(value, context);
86}
87
88FloatAttr *Builder::getFloatAttr(double value) {
89 return FloatAttr::get(value, context);
90}
91
92StringAttr *Builder::getStringAttr(StringRef bytes) {
93 return StringAttr::get(bytes, context);
94}
95
96ArrayAttr *Builder::getArrayAttr(ArrayRef<Attribute *> value) {
97 return ArrayAttr::get(value, context);
98}
99
MLIR Teamb61885d2018-07-18 16:29:21 -0700100AffineMapAttr *Builder::getAffineMapAttr(AffineMap *value) {
101 return AffineMapAttr::get(value, context);
102}
103
James Molloyf0d2f442018-08-03 01:54:46 -0700104TypeAttr *Builder::getTypeAttr(Type *type) {
105 return TypeAttr::get(type, context);
106}
107
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700108//===----------------------------------------------------------------------===//
Uday Bondhugulabc535622018-08-07 14:24:38 -0700109// Affine Expressions, Affine Maps, and Integet Sets.
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700110//===----------------------------------------------------------------------===//
111
112AffineMap *Builder::getAffineMap(unsigned dimCount, unsigned symbolCount,
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700113 ArrayRef<AffineExpr *> results,
114 ArrayRef<AffineExpr *> rangeSizes) {
115 return AffineMap::get(dimCount, symbolCount, results, rangeSizes, context);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700116}
117
118AffineDimExpr *Builder::getDimExpr(unsigned position) {
119 return AffineDimExpr::get(position, context);
120}
121
122AffineSymbolExpr *Builder::getSymbolExpr(unsigned position) {
123 return AffineSymbolExpr::get(position, context);
124}
125
126AffineConstantExpr *Builder::getConstantExpr(int64_t constant) {
127 return AffineConstantExpr::get(constant, context);
128}
129
130AffineExpr *Builder::getAddExpr(AffineExpr *lhs, AffineExpr *rhs) {
Uday Bondhugulac1faf662018-07-19 14:08:50 -0700131 return AffineBinaryOpExpr::get(AffineExpr::Kind::Add, lhs, rhs, context);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700132}
133
134AffineExpr *Builder::getMulExpr(AffineExpr *lhs, AffineExpr *rhs) {
Uday Bondhugulac1faf662018-07-19 14:08:50 -0700135 return AffineBinaryOpExpr::get(AffineExpr::Kind::Mul, lhs, rhs, context);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700136}
137
138AffineExpr *Builder::getModExpr(AffineExpr *lhs, AffineExpr *rhs) {
Uday Bondhugulac1faf662018-07-19 14:08:50 -0700139 return AffineBinaryOpExpr::get(AffineExpr::Kind::Mod, lhs, rhs, context);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700140}
141
142AffineExpr *Builder::getFloorDivExpr(AffineExpr *lhs, AffineExpr *rhs) {
Uday Bondhugulac1faf662018-07-19 14:08:50 -0700143 return AffineBinaryOpExpr::get(AffineExpr::Kind::FloorDiv, lhs, rhs, context);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700144}
145
146AffineExpr *Builder::getCeilDivExpr(AffineExpr *lhs, AffineExpr *rhs) {
Uday Bondhugulac1faf662018-07-19 14:08:50 -0700147 return AffineBinaryOpExpr::get(AffineExpr::Kind::CeilDiv, lhs, rhs, context);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700148}
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -0700149
Uday Bondhugulabc535622018-08-07 14:24:38 -0700150IntegerSet *Builder::getIntegerSet(unsigned dimCount, unsigned symbolCount,
151 ArrayRef<AffineExpr *> constraints,
152 ArrayRef<bool> isEq) {
153 return IntegerSet::get(dimCount, symbolCount, constraints, isEq, context);
154}
155
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -0700156//===----------------------------------------------------------------------===//
Tatiana Shpeisman6708b452018-07-24 10:15:13 -0700157// CFG function elements.
158//===----------------------------------------------------------------------===//
159
160// Basic block.
161BasicBlock *CFGFuncBuilder::createBlock() {
162 BasicBlock *b = new BasicBlock();
163 function->push_back(b);
164 setInsertionPoint(b);
165 return b;
166}
167
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700168/// Create an operation given the fields represented as an OperationState.
169OperationInst *CFGFuncBuilder::createOperation(const OperationState &state) {
170 SmallVector<CFGValue *, 8> operands;
171 operands.reserve(state.operands.size());
172 for (auto elt : state.operands)
173 operands.push_back(cast<CFGValue>(elt));
174
175 auto *op = OperationInst::create(state.name, operands, state.types,
176 state.attributes, context);
177 block->getOperations().insert(insertPoint, op);
178 return op;
179}
180
Tatiana Shpeisman6708b452018-07-24 10:15:13 -0700181//===----------------------------------------------------------------------===//
182// Statements.
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -0700183//===----------------------------------------------------------------------===//
184
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700185/// Create an operation given the fields represented as an OperationState.
186OperationStmt *MLFuncBuilder::createOperation(const OperationState &state) {
187 SmallVector<MLValue *, 8> operands;
188 operands.reserve(state.operands.size());
189 for (auto elt : state.operands)
190 operands.push_back(cast<MLValue>(elt));
191
192 auto *op = OperationStmt::create(state.name, operands, state.types,
193 state.attributes, context);
194 block->getStatements().insert(insertPoint, op);
195 return op;
196}
197
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -0700198ForStmt *MLFuncBuilder::createFor(AffineConstantExpr *lowerBound,
199 AffineConstantExpr *upperBound,
200 AffineConstantExpr *step) {
201 if (!step)
202 step = getConstantExpr(1);
Tatiana Shpeisman3838db72018-07-30 15:18:10 -0700203 auto *stmt = new ForStmt(lowerBound, upperBound, step, context);
Tatiana Shpeismand880b352018-07-31 23:14:16 -0700204 block->getStatements().insert(insertPoint, stmt);
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -0700205 return stmt;
206}