blob: 8d27991b8fd01e88d56defdb8c857c56d6587ea3 [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 Lattner158e0a3e2018-07-08 20:51:38 -070038PrimitiveType *Builder::getAffineIntType() {
39 return Type::getAffineInt(context);
40}
41
42PrimitiveType *Builder::getBF16Type() { return Type::getBF16(context); }
43
44PrimitiveType *Builder::getF16Type() { return Type::getF16(context); }
45
46PrimitiveType *Builder::getF32Type() { return Type::getF32(context); }
47
48PrimitiveType *Builder::getF64Type() { return Type::getF64(context); }
49
50IntegerType *Builder::getIntegerType(unsigned width) {
51 return Type::getInteger(width, context);
52}
53
54FunctionType *Builder::getFunctionType(ArrayRef<Type *> inputs,
55 ArrayRef<Type *> results) {
56 return FunctionType::get(inputs, results, context);
57}
58
59VectorType *Builder::getVectorType(ArrayRef<unsigned> shape,
60 Type *elementType) {
61 return VectorType::get(shape, elementType);
62}
63
64RankedTensorType *Builder::getTensorType(ArrayRef<int> shape,
65 Type *elementType) {
66 return RankedTensorType::get(shape, elementType);
67}
68
69UnrankedTensorType *Builder::getTensorType(Type *elementType) {
70 return UnrankedTensorType::get(elementType);
71}
Chris Lattner1ac20cb2018-07-10 10:59:53 -070072
73//===----------------------------------------------------------------------===//
74// Attributes.
75//===----------------------------------------------------------------------===//
76
77BoolAttr *Builder::getBoolAttr(bool value) {
78 return BoolAttr::get(value, context);
79}
80
81IntegerAttr *Builder::getIntegerAttr(int64_t value) {
82 return IntegerAttr::get(value, context);
83}
84
85FloatAttr *Builder::getFloatAttr(double value) {
86 return FloatAttr::get(value, context);
87}
88
89StringAttr *Builder::getStringAttr(StringRef bytes) {
90 return StringAttr::get(bytes, context);
91}
92
93ArrayAttr *Builder::getArrayAttr(ArrayRef<Attribute *> value) {
94 return ArrayAttr::get(value, context);
95}
96
MLIR Teamb61885d2018-07-18 16:29:21 -070097AffineMapAttr *Builder::getAffineMapAttr(AffineMap *value) {
98 return AffineMapAttr::get(value, context);
99}
100
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700101//===----------------------------------------------------------------------===//
102// Affine Expressions and Affine Map.
103//===----------------------------------------------------------------------===//
104
105AffineMap *Builder::getAffineMap(unsigned dimCount, unsigned symbolCount,
Uday Bondhugula0115dbb2018-07-11 21:31:07 -0700106 ArrayRef<AffineExpr *> results,
107 ArrayRef<AffineExpr *> rangeSizes) {
108 return AffineMap::get(dimCount, symbolCount, results, rangeSizes, context);
Chris Lattner1ac20cb2018-07-10 10:59:53 -0700109}
110
111AffineDimExpr *Builder::getDimExpr(unsigned position) {
112 return AffineDimExpr::get(position, context);
113}
114
115AffineSymbolExpr *Builder::getSymbolExpr(unsigned position) {
116 return AffineSymbolExpr::get(position, context);
117}
118
119AffineConstantExpr *Builder::getConstantExpr(int64_t constant) {
120 return AffineConstantExpr::get(constant, context);
121}
122
123AffineExpr *Builder::getAddExpr(AffineExpr *lhs, AffineExpr *rhs) {
124 return AffineAddExpr::get(lhs, rhs, context);
125}
126
127AffineExpr *Builder::getSubExpr(AffineExpr *lhs, AffineExpr *rhs) {
128 return AffineSubExpr::get(lhs, rhs, context);
129}
130
131AffineExpr *Builder::getMulExpr(AffineExpr *lhs, AffineExpr *rhs) {
132 return AffineMulExpr::get(lhs, rhs, context);
133}
134
135AffineExpr *Builder::getModExpr(AffineExpr *lhs, AffineExpr *rhs) {
136 return AffineModExpr::get(lhs, rhs, context);
137}
138
139AffineExpr *Builder::getFloorDivExpr(AffineExpr *lhs, AffineExpr *rhs) {
140 return AffineFloorDivExpr::get(lhs, rhs, context);
141}
142
143AffineExpr *Builder::getCeilDivExpr(AffineExpr *lhs, AffineExpr *rhs) {
144 return AffineCeilDivExpr::get(lhs, rhs, context);
145}