blob: cc3e21a2ad94a4b30b55402389d72de357549623 [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();
Jacques Pienaarc0d69302018-07-27 11:07:12 -070065 PrimitiveType *getTFControlType();
Chris Lattner158e0a3e2018-07-08 20:51:38 -070066 IntegerType *getIntegerType(unsigned width);
67 FunctionType *getFunctionType(ArrayRef<Type *> inputs,
68 ArrayRef<Type *> results);
69 VectorType *getVectorType(ArrayRef<unsigned> shape, Type *elementType);
70 RankedTensorType *getTensorType(ArrayRef<int> shape, Type *elementType);
71 UnrankedTensorType *getTensorType(Type *elementType);
72
Chris Lattner1ac20cb2018-07-10 10:59:53 -070073 // Attributes.
74 BoolAttr *getBoolAttr(bool value);
75 IntegerAttr *getIntegerAttr(int64_t value);
76 FloatAttr *getFloatAttr(double value);
77 StringAttr *getStringAttr(StringRef bytes);
78 ArrayAttr *getArrayAttr(ArrayRef<Attribute *> value);
MLIR Teamb61885d2018-07-18 16:29:21 -070079 AffineMapAttr *getAffineMapAttr(AffineMap *value);
Chris Lattner1ac20cb2018-07-10 10:59:53 -070080
81 // Affine Expressions and Affine Map.
82 AffineMap *getAffineMap(unsigned dimCount, unsigned symbolCount,
Uday Bondhugula0115dbb2018-07-11 21:31:07 -070083 ArrayRef<AffineExpr *> results,
84 ArrayRef<AffineExpr *> rangeSizes);
Chris Lattner1ac20cb2018-07-10 10:59:53 -070085 AffineDimExpr *getDimExpr(unsigned position);
86 AffineSymbolExpr *getSymbolExpr(unsigned position);
87 AffineConstantExpr *getConstantExpr(int64_t constant);
88 AffineExpr *getAddExpr(AffineExpr *lhs, AffineExpr *rhs);
89 AffineExpr *getSubExpr(AffineExpr *lhs, AffineExpr *rhs);
90 AffineExpr *getMulExpr(AffineExpr *lhs, AffineExpr *rhs);
91 AffineExpr *getModExpr(AffineExpr *lhs, AffineExpr *rhs);
92 AffineExpr *getFloorDivExpr(AffineExpr *lhs, AffineExpr *rhs);
93 AffineExpr *getCeilDivExpr(AffineExpr *lhs, AffineExpr *rhs);
94
Chris Lattner158e0a3e2018-07-08 20:51:38 -070095 // TODO: Helpers for affine map/exprs, etc.
Chris Lattner158e0a3e2018-07-08 20:51:38 -070096protected:
97 MLIRContext *context;
98};
99
100/// This class helps build a CFGFunction. Instructions that are created are
101/// automatically inserted at an insertion point or added to the current basic
102/// block.
103class CFGFuncBuilder : public Builder {
104public:
105 CFGFuncBuilder(BasicBlock *block)
106 : Builder(block->getFunction()->getContext()),
107 function(block->getFunction()) {
108 setInsertionPoint(block);
109 }
110 CFGFuncBuilder(CFGFunction *function)
111 : Builder(function->getContext()), function(function) {}
112
113 /// Reset the insertion point to no location. Creating an operation without a
114 /// set insertion point is an error, but this can still be useful when the
115 /// current insertion point a builder refers to is being removed.
116 void clearInsertionPoint() {
117 this->block = nullptr;
118 insertPoint = BasicBlock::iterator();
119 }
120
121 /// Set the insertion point to the end of the specified block.
122 void setInsertionPoint(BasicBlock *block) {
123 this->block = block;
124 insertPoint = block->end();
125 }
126
Tatiana Shpeisman6708b452018-07-24 10:15:13 -0700127 // Add new basic block and set the insertion point to the end of it.
128 BasicBlock *createBlock();
129
130 // Instructions.
Chris Lattner3b2ef762018-07-18 15:31:25 -0700131 OperationInst *createOperation(Identifier name, ArrayRef<CFGValue *> operands,
132 ArrayRef<Type *> resultTypes,
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700133 ArrayRef<NamedAttribute> attributes) {
Chris Lattner3b2ef762018-07-18 15:31:25 -0700134 auto op =
135 OperationInst::create(name, operands, resultTypes, attributes, context);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700136 block->getOperations().push_back(op);
137 return op;
138 }
139
140 // Terminators.
141
Chris Lattner40746442018-07-21 14:32:09 -0700142 ReturnInst *createReturnInst(ArrayRef<CFGValue *> operands) {
143 return insertTerminator(ReturnInst::create(operands));
144 }
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700145
146 BranchInst *createBranchInst(BasicBlock *dest) {
Chris Lattner40746442018-07-21 14:32:09 -0700147 return insertTerminator(BranchInst::create(dest));
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700148 }
149
James Molloy4f788372018-07-24 15:01:27 -0700150 CondBranchInst *createCondBranchInst(CFGValue *condition,
151 BasicBlock *trueDest,
152 BasicBlock *falseDest) {
153 return insertTerminator(
154 CondBranchInst::create(condition, trueDest, falseDest));
155 }
156
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700157private:
158 template <typename T>
159 T *insertTerminator(T *term) {
160 block->setTerminator(term);
161 return term;
162 }
163
164 CFGFunction *function;
165 BasicBlock *block = nullptr;
166 BasicBlock::iterator insertPoint;
167};
168
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700169/// This class helps build an MLFunction. Statements that are created are
170/// automatically inserted at an insertion point or added to the current
171/// statement block.
172class MLFuncBuilder : public Builder {
173public:
174 MLFuncBuilder(MLFunction *function) : Builder(function->getContext()) {}
175
176 MLFuncBuilder(StmtBlock *block) : MLFuncBuilder(block->getFunction()) {
177 setInsertionPoint(block);
178 }
179
180 /// Reset the insertion point to no location. Creating an operation without a
181 /// set insertion point is an error, but this can still be useful when the
182 /// current insertion point a builder refers to is being removed.
183 void clearInsertionPoint() {
184 this->block = nullptr;
185 insertPoint = StmtBlock::iterator();
186 }
187
188 /// Set the insertion point to the end of the specified block.
189 void setInsertionPoint(StmtBlock *block) {
190 this->block = block;
191 insertPoint = block->end();
192 }
193
Tatiana Shpeisman60bf7be2018-07-26 18:09:20 -0700194 OperationStmt *createOperation(Identifier name, ArrayRef<MLValue *> operands,
195 ArrayRef<Type *> resultTypes,
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700196 ArrayRef<NamedAttribute> attributes) {
Tatiana Shpeisman60bf7be2018-07-26 18:09:20 -0700197 auto op =
198 OperationStmt::create(name, operands, resultTypes, attributes, context);
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700199 block->getStatements().push_back(op);
200 return op;
201 }
202
Chris Lattner1604e472018-07-23 08:42:19 -0700203 // Creates for statement. When step is not specified, it is set to 1.
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -0700204 ForStmt *createFor(AffineConstantExpr *lowerBound,
205 AffineConstantExpr *upperBound,
206 AffineConstantExpr *step = nullptr);
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700207
208 IfStmt *createIf() {
209 auto stmt = new IfStmt();
210 block->getStatements().push_back(stmt);
211 return stmt;
212 }
213
214private:
215 StmtBlock *block = nullptr;
216 StmtBlock::iterator insertPoint;
217};
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700218
219} // namespace mlir
220
221#endif