blob: 3e4bbd74e80039918cc418869965595686a8e49f [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
Uday Bondhugula15984952018-08-01 22:36:12 -070021#include "mlir/IR/Attributes.h"
Chris Lattner158e0a3e2018-07-08 20:51:38 -070022#include "mlir/IR/CFGFunction.h"
Tatiana Shpeisman565b9642018-07-16 11:47:09 -070023#include "mlir/IR/MLFunction.h"
24#include "mlir/IR/Statements.h"
Chris Lattner158e0a3e2018-07-08 20:51:38 -070025
26namespace mlir {
27class MLIRContext;
28class Module;
29class Type;
30class PrimitiveType;
31class IntegerType;
32class FunctionType;
33class VectorType;
34class RankedTensorType;
35class UnrankedTensorType;
Chris Lattner1ac20cb2018-07-10 10:59:53 -070036class BoolAttr;
37class IntegerAttr;
38class FloatAttr;
39class StringAttr;
James Molloyf0d2f442018-08-03 01:54:46 -070040class TypeAttr;
Chris Lattner1ac20cb2018-07-10 10:59:53 -070041class ArrayAttr;
MLIR Teamb61885d2018-07-18 16:29:21 -070042class AffineMapAttr;
Chris Lattner1ac20cb2018-07-10 10:59:53 -070043class AffineMap;
44class AffineExpr;
45class AffineConstantExpr;
46class AffineDimExpr;
47class AffineSymbolExpr;
Chris Lattner158e0a3e2018-07-08 20:51:38 -070048
49/// This class is a general helper class for creating context-global objects
50/// like types, attributes, and affine expressions.
51class Builder {
52public:
53 explicit Builder(MLIRContext *context) : context(context) {}
54 explicit Builder(Module *module);
55
56 MLIRContext *getContext() const { return context; }
57
Chris Lattner1ac20cb2018-07-10 10:59:53 -070058 Identifier getIdentifier(StringRef str);
59 Module *createModule();
60
Chris Lattner158e0a3e2018-07-08 20:51:38 -070061 // Types.
Chris Lattnerc3251192018-07-27 13:09:58 -070062 FloatType *getBF16Type();
63 FloatType *getF16Type();
64 FloatType *getF32Type();
65 FloatType *getF64Type();
66
67 OtherType *getAffineIntType();
68 OtherType *getTFControlType();
James Molloy72b0cbe2018-08-01 12:55:27 -070069 OtherType *getTFStringType();
Chris Lattner158e0a3e2018-07-08 20:51:38 -070070 IntegerType *getIntegerType(unsigned width);
71 FunctionType *getFunctionType(ArrayRef<Type *> inputs,
72 ArrayRef<Type *> results);
73 VectorType *getVectorType(ArrayRef<unsigned> shape, Type *elementType);
74 RankedTensorType *getTensorType(ArrayRef<int> shape, Type *elementType);
75 UnrankedTensorType *getTensorType(Type *elementType);
76
Chris Lattner1ac20cb2018-07-10 10:59:53 -070077 // Attributes.
78 BoolAttr *getBoolAttr(bool value);
79 IntegerAttr *getIntegerAttr(int64_t value);
80 FloatAttr *getFloatAttr(double value);
81 StringAttr *getStringAttr(StringRef bytes);
82 ArrayAttr *getArrayAttr(ArrayRef<Attribute *> value);
MLIR Teamb61885d2018-07-18 16:29:21 -070083 AffineMapAttr *getAffineMapAttr(AffineMap *value);
James Molloyf0d2f442018-08-03 01:54:46 -070084 TypeAttr *getTypeAttr(Type *type);
Chris Lattner1ac20cb2018-07-10 10:59:53 -070085
86 // Affine Expressions and Affine Map.
87 AffineMap *getAffineMap(unsigned dimCount, unsigned symbolCount,
Uday Bondhugula0115dbb2018-07-11 21:31:07 -070088 ArrayRef<AffineExpr *> results,
89 ArrayRef<AffineExpr *> rangeSizes);
Chris Lattner1ac20cb2018-07-10 10:59:53 -070090 AffineDimExpr *getDimExpr(unsigned position);
91 AffineSymbolExpr *getSymbolExpr(unsigned position);
92 AffineConstantExpr *getConstantExpr(int64_t constant);
93 AffineExpr *getAddExpr(AffineExpr *lhs, AffineExpr *rhs);
94 AffineExpr *getSubExpr(AffineExpr *lhs, AffineExpr *rhs);
95 AffineExpr *getMulExpr(AffineExpr *lhs, AffineExpr *rhs);
96 AffineExpr *getModExpr(AffineExpr *lhs, AffineExpr *rhs);
97 AffineExpr *getFloorDivExpr(AffineExpr *lhs, AffineExpr *rhs);
98 AffineExpr *getCeilDivExpr(AffineExpr *lhs, AffineExpr *rhs);
99
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700100 // TODO: Helpers for affine map/exprs, etc.
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700101protected:
102 MLIRContext *context;
103};
104
105/// This class helps build a CFGFunction. Instructions that are created are
106/// automatically inserted at an insertion point or added to the current basic
107/// block.
108class CFGFuncBuilder : public Builder {
109public:
Chris Lattner8174f3a2018-07-29 16:45:23 -0700110 CFGFuncBuilder(BasicBlock *block, BasicBlock::iterator insertPoint)
111 : Builder(block->getFunction()->getContext()),
112 function(block->getFunction()) {
113 setInsertionPoint(block, insertPoint);
114 }
115
116 CFGFuncBuilder(OperationInst *insertBefore)
117 : CFGFuncBuilder(insertBefore->getBlock(),
118 BasicBlock::iterator(insertBefore)) {}
119
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700120 CFGFuncBuilder(BasicBlock *block)
121 : Builder(block->getFunction()->getContext()),
122 function(block->getFunction()) {
123 setInsertionPoint(block);
124 }
Chris Lattner8174f3a2018-07-29 16:45:23 -0700125
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700126 CFGFuncBuilder(CFGFunction *function)
127 : Builder(function->getContext()), function(function) {}
128
129 /// Reset the insertion point to no location. Creating an operation without a
130 /// set insertion point is an error, but this can still be useful when the
131 /// current insertion point a builder refers to is being removed.
132 void clearInsertionPoint() {
133 this->block = nullptr;
134 insertPoint = BasicBlock::iterator();
135 }
136
Chris Lattner8174f3a2018-07-29 16:45:23 -0700137 /// Set the insertion point to the specified location.
138 void setInsertionPoint(BasicBlock *block, BasicBlock::iterator insertPoint) {
139 assert(block->getFunction() == function &&
140 "can't move to a different function");
141 this->block = block;
142 this->insertPoint = insertPoint;
143 }
144
145 /// Set the insertion point to the specified operation.
146 void setInsertionPoint(OperationInst *inst) {
147 setInsertionPoint(inst->getBlock(), BasicBlock::iterator(inst));
148 }
149
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700150 /// Set the insertion point to the end of the specified block.
151 void setInsertionPoint(BasicBlock *block) {
Chris Lattner8174f3a2018-07-29 16:45:23 -0700152 setInsertionPoint(block, block->end());
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700153 }
154
Tatiana Shpeisman6708b452018-07-24 10:15:13 -0700155 // Add new basic block and set the insertion point to the end of it.
156 BasicBlock *createBlock();
157
Chris Lattner8174f3a2018-07-29 16:45:23 -0700158 // Create an operation at the current insertion point.
Chris Lattner3b2ef762018-07-18 15:31:25 -0700159 OperationInst *createOperation(Identifier name, ArrayRef<CFGValue *> operands,
160 ArrayRef<Type *> resultTypes,
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700161 ArrayRef<NamedAttribute> attributes) {
Chris Lattner3b2ef762018-07-18 15:31:25 -0700162 auto op =
163 OperationInst::create(name, operands, resultTypes, attributes, context);
Chris Lattner8174f3a2018-07-29 16:45:23 -0700164 block->getOperations().insert(insertPoint, op);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700165 return op;
166 }
167
Uday Bondhugula15984952018-08-01 22:36:12 -0700168 OperationInst *cloneOperation(const OperationInst &srcOpInst) {
169 auto *op = srcOpInst.clone();
170 block->getOperations().insert(insertPoint, op);
171 return op;
172 }
173
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700174 // Terminators.
175
Chris Lattner40746442018-07-21 14:32:09 -0700176 ReturnInst *createReturnInst(ArrayRef<CFGValue *> operands) {
177 return insertTerminator(ReturnInst::create(operands));
178 }
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700179
180 BranchInst *createBranchInst(BasicBlock *dest) {
Chris Lattner40746442018-07-21 14:32:09 -0700181 return insertTerminator(BranchInst::create(dest));
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700182 }
183
James Molloy4f788372018-07-24 15:01:27 -0700184 CondBranchInst *createCondBranchInst(CFGValue *condition,
185 BasicBlock *trueDest,
186 BasicBlock *falseDest) {
187 return insertTerminator(
188 CondBranchInst::create(condition, trueDest, falseDest));
189 }
190
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700191private:
192 template <typename T>
193 T *insertTerminator(T *term) {
194 block->setTerminator(term);
195 return term;
196 }
197
198 CFGFunction *function;
199 BasicBlock *block = nullptr;
200 BasicBlock::iterator insertPoint;
201};
202
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700203/// This class helps build an MLFunction. Statements that are created are
204/// automatically inserted at an insertion point or added to the current
205/// statement block.
206class MLFuncBuilder : public Builder {
207public:
Tatiana Shpeismand880b352018-07-31 23:14:16 -0700208 /// Create ML function builder and set insertion point to the given
209 /// statement block, that is, given ML function, for statement or if statement
210 /// clause.
211 MLFuncBuilder(StmtBlock *block)
212 : Builder(block->findFunction()->getContext()) {
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700213 setInsertionPoint(block);
214 }
215
216 /// Reset the insertion point to no location. Creating an operation without a
217 /// set insertion point is an error, but this can still be useful when the
218 /// current insertion point a builder refers to is being removed.
219 void clearInsertionPoint() {
220 this->block = nullptr;
221 insertPoint = StmtBlock::iterator();
222 }
223
Tatiana Shpeismand880b352018-07-31 23:14:16 -0700224 /// Set the insertion point to the specified location.
225 /// Unlike CFGFuncBuilder, MLFuncBuilder allows to set insertion
226 /// point to a different function.
227 void setInsertionPoint(StmtBlock *block, StmtBlock::iterator insertPoint) {
228 // TODO: check that insertPoint is in this rather than some other block.
229 this->block = block;
230 this->insertPoint = insertPoint;
231 }
232
233 /// Set the insertion point to the specified operation.
234 void setInsertionPoint(OperationStmt *stmt) {
235 setInsertionPoint(stmt->getBlock(), StmtBlock::iterator(stmt));
236 }
237
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700238 /// Set the insertion point to the end of the specified block.
239 void setInsertionPoint(StmtBlock *block) {
240 this->block = block;
241 insertPoint = block->end();
242 }
243
Uday Bondhugula15984952018-08-01 22:36:12 -0700244 /// Set the insertion point at the beginning of the specified block.
245 void setInsertionPointAtStart(StmtBlock *block) {
246 this->block = block;
247 insertPoint = block->begin();
248 }
249
Tatiana Shpeisman60bf7be2018-07-26 18:09:20 -0700250 OperationStmt *createOperation(Identifier name, ArrayRef<MLValue *> operands,
251 ArrayRef<Type *> resultTypes,
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700252 ArrayRef<NamedAttribute> attributes) {
Uday Bondhugula9f7754e2018-07-31 14:26:07 -0700253 auto *op =
Tatiana Shpeisman60bf7be2018-07-26 18:09:20 -0700254 OperationStmt::create(name, operands, resultTypes, attributes, context);
Uday Bondhugula9f7754e2018-07-31 14:26:07 -0700255 block->getStatements().insert(insertPoint, op);
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700256 return op;
257 }
258
Uday Bondhugula15984952018-08-01 22:36:12 -0700259 OperationStmt *cloneOperation(const OperationStmt &srcOpStmt) {
260 auto *op = srcOpStmt.clone();
261 block->getStatements().insert(insertPoint, op);
262 return op;
263 }
264
Chris Lattner1604e472018-07-23 08:42:19 -0700265 // Creates for statement. When step is not specified, it is set to 1.
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -0700266 ForStmt *createFor(AffineConstantExpr *lowerBound,
267 AffineConstantExpr *upperBound,
268 AffineConstantExpr *step = nullptr);
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700269
270 IfStmt *createIf() {
Tatiana Shpeismand880b352018-07-31 23:14:16 -0700271 auto *stmt = new IfStmt();
272 block->getStatements().insert(insertPoint, stmt);
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700273 return stmt;
274 }
275
Uday Bondhugula15984952018-08-01 22:36:12 -0700276 // TODO: subsume with a generate create<ConstantInt>() method.
277 OperationStmt *createConstInt32Op(int value) {
278 std::pair<Identifier, Attribute *> namedAttr(
279 Identifier::get("value", context), getIntegerAttr(value));
280 auto *mlconst = createOperation(Identifier::get("constant", context), {},
281 {getIntegerType(32)}, {namedAttr});
282 return mlconst;
283 }
284
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700285private:
286 StmtBlock *block = nullptr;
287 StmtBlock::iterator insertPoint;
288};
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700289
290} // namespace mlir
291
292#endif