blob: e6e4e4db622bd819888252b70852cacc7fceeb1b [file] [log] [blame]
Chris Lattner158e0a3e2018-07-08 20:51:38 -07001//===- Builders.h - Helpers for constructing MLIR Classes -------*- C++ -*-===//
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#ifndef MLIR_IR_BUILDERS_H
19#define MLIR_IR_BUILDERS_H
20
21#include "mlir/IR/CFGFunction.h"
Tatiana Shpeisman565b9642018-07-16 11:47:09 -070022#include "mlir/IR/MLFunction.h"
23#include "mlir/IR/Statements.h"
Chris Lattner158e0a3e2018-07-08 20:51:38 -070024
25namespace mlir {
26class MLIRContext;
27class Module;
28class Type;
29class PrimitiveType;
30class IntegerType;
31class FunctionType;
32class VectorType;
33class RankedTensorType;
34class UnrankedTensorType;
Chris Lattner1ac20cb2018-07-10 10:59:53 -070035class BoolAttr;
36class IntegerAttr;
37class FloatAttr;
38class StringAttr;
39class ArrayAttr;
MLIR Teamb61885d2018-07-18 16:29:21 -070040class AffineMapAttr;
Chris Lattner1ac20cb2018-07-10 10:59:53 -070041class AffineMap;
42class AffineExpr;
43class AffineConstantExpr;
44class AffineDimExpr;
45class AffineSymbolExpr;
Chris Lattner158e0a3e2018-07-08 20:51:38 -070046
47/// This class is a general helper class for creating context-global objects
48/// like types, attributes, and affine expressions.
49class Builder {
50public:
51 explicit Builder(MLIRContext *context) : context(context) {}
52 explicit Builder(Module *module);
53
54 MLIRContext *getContext() const { return context; }
55
Chris Lattner1ac20cb2018-07-10 10:59:53 -070056 Identifier getIdentifier(StringRef str);
57 Module *createModule();
58
Chris Lattner158e0a3e2018-07-08 20:51:38 -070059 // Types.
60 PrimitiveType *getAffineIntType();
61 PrimitiveType *getBF16Type();
62 PrimitiveType *getF16Type();
63 PrimitiveType *getF32Type();
64 PrimitiveType *getF64Type();
65 IntegerType *getIntegerType(unsigned width);
66 FunctionType *getFunctionType(ArrayRef<Type *> inputs,
67 ArrayRef<Type *> results);
68 VectorType *getVectorType(ArrayRef<unsigned> shape, Type *elementType);
69 RankedTensorType *getTensorType(ArrayRef<int> shape, Type *elementType);
70 UnrankedTensorType *getTensorType(Type *elementType);
71
Chris Lattner1ac20cb2018-07-10 10:59:53 -070072 // Attributes.
73 BoolAttr *getBoolAttr(bool value);
74 IntegerAttr *getIntegerAttr(int64_t value);
75 FloatAttr *getFloatAttr(double value);
76 StringAttr *getStringAttr(StringRef bytes);
77 ArrayAttr *getArrayAttr(ArrayRef<Attribute *> value);
MLIR Teamb61885d2018-07-18 16:29:21 -070078 AffineMapAttr *getAffineMapAttr(AffineMap *value);
Chris Lattner1ac20cb2018-07-10 10:59:53 -070079
80 // Affine Expressions and Affine Map.
81 AffineMap *getAffineMap(unsigned dimCount, unsigned symbolCount,
Uday Bondhugula0115dbb2018-07-11 21:31:07 -070082 ArrayRef<AffineExpr *> results,
83 ArrayRef<AffineExpr *> rangeSizes);
Chris Lattner1ac20cb2018-07-10 10:59:53 -070084 AffineDimExpr *getDimExpr(unsigned position);
85 AffineSymbolExpr *getSymbolExpr(unsigned position);
86 AffineConstantExpr *getConstantExpr(int64_t constant);
87 AffineExpr *getAddExpr(AffineExpr *lhs, AffineExpr *rhs);
88 AffineExpr *getSubExpr(AffineExpr *lhs, AffineExpr *rhs);
89 AffineExpr *getMulExpr(AffineExpr *lhs, AffineExpr *rhs);
90 AffineExpr *getModExpr(AffineExpr *lhs, AffineExpr *rhs);
91 AffineExpr *getFloorDivExpr(AffineExpr *lhs, AffineExpr *rhs);
92 AffineExpr *getCeilDivExpr(AffineExpr *lhs, AffineExpr *rhs);
93
Chris Lattner158e0a3e2018-07-08 20:51:38 -070094 // TODO: Helpers for affine map/exprs, etc.
Chris Lattner158e0a3e2018-07-08 20:51:38 -070095protected:
96 MLIRContext *context;
97};
98
99/// This class helps build a CFGFunction. Instructions that are created are
100/// automatically inserted at an insertion point or added to the current basic
101/// block.
102class CFGFuncBuilder : public Builder {
103public:
104 CFGFuncBuilder(BasicBlock *block)
105 : Builder(block->getFunction()->getContext()),
106 function(block->getFunction()) {
107 setInsertionPoint(block);
108 }
109 CFGFuncBuilder(CFGFunction *function)
110 : Builder(function->getContext()), function(function) {}
111
112 /// Reset the insertion point to no location. Creating an operation without a
113 /// set insertion point is an error, but this can still be useful when the
114 /// current insertion point a builder refers to is being removed.
115 void clearInsertionPoint() {
116 this->block = nullptr;
117 insertPoint = BasicBlock::iterator();
118 }
119
120 /// Set the insertion point to the end of the specified block.
121 void setInsertionPoint(BasicBlock *block) {
122 this->block = block;
123 insertPoint = block->end();
124 }
125
Tatiana Shpeisman6708b452018-07-24 10:15:13 -0700126 // Add new basic block and set the insertion point to the end of it.
127 BasicBlock *createBlock();
128
129 // Instructions.
Chris Lattner3b2ef762018-07-18 15:31:25 -0700130 OperationInst *createOperation(Identifier name, ArrayRef<CFGValue *> operands,
131 ArrayRef<Type *> resultTypes,
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700132 ArrayRef<NamedAttribute> attributes) {
Chris Lattner3b2ef762018-07-18 15:31:25 -0700133 auto op =
134 OperationInst::create(name, operands, resultTypes, attributes, context);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700135 block->getOperations().push_back(op);
136 return op;
137 }
138
139 // Terminators.
140
Chris Lattner40746442018-07-21 14:32:09 -0700141 ReturnInst *createReturnInst(ArrayRef<CFGValue *> operands) {
142 return insertTerminator(ReturnInst::create(operands));
143 }
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700144
145 BranchInst *createBranchInst(BasicBlock *dest) {
Chris Lattner40746442018-07-21 14:32:09 -0700146 return insertTerminator(BranchInst::create(dest));
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700147 }
148
149private:
150 template <typename T>
151 T *insertTerminator(T *term) {
152 block->setTerminator(term);
153 return term;
154 }
155
156 CFGFunction *function;
157 BasicBlock *block = nullptr;
158 BasicBlock::iterator insertPoint;
159};
160
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700161/// This class helps build an MLFunction. Statements that are created are
162/// automatically inserted at an insertion point or added to the current
163/// statement block.
164class MLFuncBuilder : public Builder {
165public:
166 MLFuncBuilder(MLFunction *function) : Builder(function->getContext()) {}
167
168 MLFuncBuilder(StmtBlock *block) : MLFuncBuilder(block->getFunction()) {
169 setInsertionPoint(block);
170 }
171
172 /// Reset the insertion point to no location. Creating an operation without a
173 /// set insertion point is an error, but this can still be useful when the
174 /// current insertion point a builder refers to is being removed.
175 void clearInsertionPoint() {
176 this->block = nullptr;
177 insertPoint = StmtBlock::iterator();
178 }
179
180 /// Set the insertion point to the end of the specified block.
181 void setInsertionPoint(StmtBlock *block) {
182 this->block = block;
183 insertPoint = block->end();
184 }
185
186 OperationStmt *createOperation(Identifier name,
187 ArrayRef<NamedAttribute> attributes) {
188 auto op = new OperationStmt(name, attributes, context);
189 block->getStatements().push_back(op);
190 return op;
191 }
192
Chris Lattner1604e472018-07-23 08:42:19 -0700193 // Creates for statement. When step is not specified, it is set to 1.
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -0700194 ForStmt *createFor(AffineConstantExpr *lowerBound,
195 AffineConstantExpr *upperBound,
196 AffineConstantExpr *step = nullptr);
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700197
198 IfStmt *createIf() {
199 auto stmt = new IfStmt();
200 block->getStatements().push_back(stmt);
201 return stmt;
202 }
203
204private:
205 StmtBlock *block = nullptr;
206 StmtBlock::iterator insertPoint;
207};
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700208
209} // namespace mlir
210
211#endif