blob: 48e78c7ed8e11a490c2f6621021a97fcf715441c [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;
Chris Lattner4613d9e2018-08-19 21:17:22 -070042class FunctionAttr;
MLIR Teamb61885d2018-07-18 16:29:21 -070043class AffineMapAttr;
Chris Lattner1ac20cb2018-07-10 10:59:53 -070044class AffineMap;
45class AffineExpr;
46class AffineConstantExpr;
47class AffineDimExpr;
48class AffineSymbolExpr;
Chris Lattner158e0a3e2018-07-08 20:51:38 -070049
50/// This class is a general helper class for creating context-global objects
51/// like types, attributes, and affine expressions.
52class Builder {
53public:
54 explicit Builder(MLIRContext *context) : context(context) {}
55 explicit Builder(Module *module);
56
57 MLIRContext *getContext() const { return context; }
58
Chris Lattner1ac20cb2018-07-10 10:59:53 -070059 Identifier getIdentifier(StringRef str);
60 Module *createModule();
61
Chris Lattner158e0a3e2018-07-08 20:51:38 -070062 // Types.
Chris Lattnerc3251192018-07-27 13:09:58 -070063 FloatType *getBF16Type();
64 FloatType *getF16Type();
65 FloatType *getF32Type();
66 FloatType *getF64Type();
67
68 OtherType *getAffineIntType();
69 OtherType *getTFControlType();
James Molloy72b0cbe2018-08-01 12:55:27 -070070 OtherType *getTFStringType();
Chris Lattner158e0a3e2018-07-08 20:51:38 -070071 IntegerType *getIntegerType(unsigned width);
72 FunctionType *getFunctionType(ArrayRef<Type *> inputs,
73 ArrayRef<Type *> results);
Jacques Pienaarc03c6952018-08-10 11:56:47 -070074 MemRefType *getMemRefType(ArrayRef<int> shape, Type *elementType,
75 ArrayRef<AffineMap *> affineMapComposition = {},
76 unsigned memorySpace = 0);
Chris Lattner158e0a3e2018-07-08 20:51:38 -070077 VectorType *getVectorType(ArrayRef<unsigned> shape, Type *elementType);
78 RankedTensorType *getTensorType(ArrayRef<int> shape, Type *elementType);
79 UnrankedTensorType *getTensorType(Type *elementType);
80
Chris Lattner1ac20cb2018-07-10 10:59:53 -070081 // Attributes.
82 BoolAttr *getBoolAttr(bool value);
83 IntegerAttr *getIntegerAttr(int64_t value);
84 FloatAttr *getFloatAttr(double value);
85 StringAttr *getStringAttr(StringRef bytes);
86 ArrayAttr *getArrayAttr(ArrayRef<Attribute *> value);
MLIR Teamb61885d2018-07-18 16:29:21 -070087 AffineMapAttr *getAffineMapAttr(AffineMap *value);
James Molloyf0d2f442018-08-03 01:54:46 -070088 TypeAttr *getTypeAttr(Type *type);
Chris Lattner1aa46322018-08-21 17:55:22 -070089 FunctionAttr *getFunctionAttr(const Function *value);
Chris Lattner1ac20cb2018-07-10 10:59:53 -070090
91 // Affine Expressions and Affine Map.
92 AffineMap *getAffineMap(unsigned dimCount, unsigned symbolCount,
Uday Bondhugula0115dbb2018-07-11 21:31:07 -070093 ArrayRef<AffineExpr *> results,
94 ArrayRef<AffineExpr *> rangeSizes);
Chris Lattner1ac20cb2018-07-10 10:59:53 -070095 AffineDimExpr *getDimExpr(unsigned position);
96 AffineSymbolExpr *getSymbolExpr(unsigned position);
97 AffineConstantExpr *getConstantExpr(int64_t constant);
98 AffineExpr *getAddExpr(AffineExpr *lhs, AffineExpr *rhs);
99 AffineExpr *getSubExpr(AffineExpr *lhs, AffineExpr *rhs);
100 AffineExpr *getMulExpr(AffineExpr *lhs, AffineExpr *rhs);
101 AffineExpr *getModExpr(AffineExpr *lhs, AffineExpr *rhs);
102 AffineExpr *getFloorDivExpr(AffineExpr *lhs, AffineExpr *rhs);
103 AffineExpr *getCeilDivExpr(AffineExpr *lhs, AffineExpr *rhs);
104
Uday Bondhugulabc535622018-08-07 14:24:38 -0700105 // Integer set.
106 IntegerSet *getIntegerSet(unsigned dimCount, unsigned symbolCount,
107 ArrayRef<AffineExpr *> constraints,
108 ArrayRef<bool> isEq);
109
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700110 // TODO: Helpers for affine map/exprs, etc.
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700111protected:
112 MLIRContext *context;
113};
114
115/// This class helps build a CFGFunction. Instructions that are created are
116/// automatically inserted at an insertion point or added to the current basic
117/// block.
118class CFGFuncBuilder : public Builder {
119public:
Chris Lattner8174f3a2018-07-29 16:45:23 -0700120 CFGFuncBuilder(BasicBlock *block, BasicBlock::iterator insertPoint)
121 : Builder(block->getFunction()->getContext()),
122 function(block->getFunction()) {
123 setInsertionPoint(block, insertPoint);
124 }
125
126 CFGFuncBuilder(OperationInst *insertBefore)
127 : CFGFuncBuilder(insertBefore->getBlock(),
128 BasicBlock::iterator(insertBefore)) {}
129
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700130 CFGFuncBuilder(BasicBlock *block)
131 : Builder(block->getFunction()->getContext()),
132 function(block->getFunction()) {
133 setInsertionPoint(block);
134 }
Chris Lattner8174f3a2018-07-29 16:45:23 -0700135
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700136 CFGFuncBuilder(CFGFunction *function)
137 : Builder(function->getContext()), function(function) {}
138
139 /// Reset the insertion point to no location. Creating an operation without a
140 /// set insertion point is an error, but this can still be useful when the
141 /// current insertion point a builder refers to is being removed.
142 void clearInsertionPoint() {
143 this->block = nullptr;
144 insertPoint = BasicBlock::iterator();
145 }
146
Chris Lattner8174f3a2018-07-29 16:45:23 -0700147 /// Set the insertion point to the specified location.
148 void setInsertionPoint(BasicBlock *block, BasicBlock::iterator insertPoint) {
149 assert(block->getFunction() == function &&
150 "can't move to a different function");
151 this->block = block;
152 this->insertPoint = insertPoint;
153 }
154
155 /// Set the insertion point to the specified operation.
156 void setInsertionPoint(OperationInst *inst) {
157 setInsertionPoint(inst->getBlock(), BasicBlock::iterator(inst));
158 }
159
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700160 /// Set the insertion point to the end of the specified block.
161 void setInsertionPoint(BasicBlock *block) {
Chris Lattner8174f3a2018-07-29 16:45:23 -0700162 setInsertionPoint(block, block->end());
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700163 }
164
Chris Lattner992a1272018-08-07 12:02:37 -0700165 void insert(OperationInst *opInst) {
166 block->getOperations().insert(insertPoint, opInst);
167 }
168
Tatiana Shpeisman6708b452018-07-24 10:15:13 -0700169 // Add new basic block and set the insertion point to the end of it.
170 BasicBlock *createBlock();
171
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700172 /// Create an operation given the fields represented as an OperationState.
173 OperationInst *createOperation(const OperationState &state);
174
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700175 /// Create operation of specific op type at the current insertion point.
176 template <typename OpTy, typename... Args>
Chris Lattner1628fa02018-08-23 14:32:25 -0700177 OpPointer<OpTy> create(Attribute *location, Args... args) {
178 OperationState state(getContext(), location, OpTy::getOperationName());
Chris Lattner1eb77482018-08-22 19:25:49 -0700179 OpTy::build(this, &state, args...);
180 auto *inst = createOperation(state);
Chris Lattner992a1272018-08-07 12:02:37 -0700181 auto result = inst->template getAs<OpTy>();
182 assert(result && "Builder didn't return the right type");
183 return result;
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700184 }
185
Uday Bondhugula15984952018-08-01 22:36:12 -0700186 OperationInst *cloneOperation(const OperationInst &srcOpInst) {
187 auto *op = srcOpInst.clone();
Chris Lattner992a1272018-08-07 12:02:37 -0700188 insert(op);
Uday Bondhugula15984952018-08-01 22:36:12 -0700189 return op;
190 }
191
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700192 // Terminators.
193
Chris Lattner1628fa02018-08-23 14:32:25 -0700194 ReturnInst *createReturnInst(Attribute *location,
195 ArrayRef<CFGValue *> operands) {
196 return insertTerminator(ReturnInst::create(location, operands));
Chris Lattner40746442018-07-21 14:32:09 -0700197 }
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700198
Chris Lattner1628fa02018-08-23 14:32:25 -0700199 BranchInst *createBranchInst(Attribute *location, BasicBlock *dest) {
200 return insertTerminator(BranchInst::create(location, dest));
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700201 }
202
Chris Lattner1628fa02018-08-23 14:32:25 -0700203 CondBranchInst *createCondBranchInst(Attribute *location, CFGValue *condition,
James Molloy4f788372018-07-24 15:01:27 -0700204 BasicBlock *trueDest,
205 BasicBlock *falseDest) {
206 return insertTerminator(
Chris Lattner1628fa02018-08-23 14:32:25 -0700207 CondBranchInst::create(location, condition, trueDest, falseDest));
James Molloy4f788372018-07-24 15:01:27 -0700208 }
209
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700210private:
211 template <typename T>
212 T *insertTerminator(T *term) {
213 block->setTerminator(term);
214 return term;
215 }
216
217 CFGFunction *function;
218 BasicBlock *block = nullptr;
219 BasicBlock::iterator insertPoint;
220};
221
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700222/// This class helps build an MLFunction. Statements that are created are
223/// automatically inserted at an insertion point or added to the current
224/// statement block.
225class MLFuncBuilder : public Builder {
226public:
Chris Lattnere787b322018-08-08 11:14:57 -0700227 /// Create ML function builder and set insertion point to the given statement,
228 /// which will cause subsequent insertions to go right before it.
229 MLFuncBuilder(Statement *stmt)
230 // TODO: Eliminate findFunction from this.
231 : Builder(stmt->findFunction()->getContext()) {
232 setInsertionPoint(stmt);
233 }
234
235 MLFuncBuilder(StmtBlock *block, StmtBlock::iterator insertPoint)
236 // TODO: Eliminate findFunction from this.
Tatiana Shpeismand880b352018-07-31 23:14:16 -0700237 : Builder(block->findFunction()->getContext()) {
Chris Lattnere787b322018-08-08 11:14:57 -0700238 setInsertionPoint(block, insertPoint);
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700239 }
240
241 /// Reset the insertion point to no location. Creating an operation without a
242 /// set insertion point is an error, but this can still be useful when the
243 /// current insertion point a builder refers to is being removed.
244 void clearInsertionPoint() {
245 this->block = nullptr;
246 insertPoint = StmtBlock::iterator();
247 }
248
Tatiana Shpeismand880b352018-07-31 23:14:16 -0700249 /// Set the insertion point to the specified location.
250 /// Unlike CFGFuncBuilder, MLFuncBuilder allows to set insertion
251 /// point to a different function.
252 void setInsertionPoint(StmtBlock *block, StmtBlock::iterator insertPoint) {
253 // TODO: check that insertPoint is in this rather than some other block.
254 this->block = block;
255 this->insertPoint = insertPoint;
256 }
257
Uday Bondhugula67701712018-08-21 16:01:23 -0700258 /// Set the insertion point to the specified operation, which will cause
259 /// subsequent insertions to go right before it.
Chris Lattnere787b322018-08-08 11:14:57 -0700260 void setInsertionPoint(Statement *stmt) {
Tatiana Shpeismand880b352018-07-31 23:14:16 -0700261 setInsertionPoint(stmt->getBlock(), StmtBlock::iterator(stmt));
262 }
263
Chris Lattnere787b322018-08-08 11:14:57 -0700264 /// Set the insertion point to the start of the specified block.
265 void setInsertionPointToStart(StmtBlock *block) {
Uday Bondhugula15984952018-08-01 22:36:12 -0700266 this->block = block;
267 insertPoint = block->begin();
268 }
269
Chris Lattnere787b322018-08-08 11:14:57 -0700270 /// Set the insertion point to the end of the specified block.
271 void setInsertionPointToEnd(StmtBlock *block) {
272 this->block = block;
273 insertPoint = block->end();
274 }
275
Uday Bondhugula84b80952018-08-03 13:22:26 -0700276 /// Get the current insertion point of the builder.
277 StmtBlock::iterator getInsertionPoint() const { return insertPoint; }
278
Chris Lattnereed6c4d2018-08-07 09:12:35 -0700279 /// Create an operation given the fields represented as an OperationState.
280 OperationStmt *createOperation(const OperationState &state);
281
Chris Lattner992a1272018-08-07 12:02:37 -0700282 /// Create operation of specific op type at the current insertion point.
Jacques Pienaarac86d102018-08-03 08:16:37 -0700283 template <typename OpTy, typename... Args>
Chris Lattner1628fa02018-08-23 14:32:25 -0700284 OpPointer<OpTy> create(Attribute *location, Args... args) {
285 OperationState state(getContext(), location, OpTy::getOperationName());
Chris Lattner1eb77482018-08-22 19:25:49 -0700286 OpTy::build(this, &state, args...);
287 auto *stmt = createOperation(state);
Chris Lattner992a1272018-08-07 12:02:37 -0700288 auto result = stmt->template getAs<OpTy>();
289 assert(result && "Builder didn't return the right type");
290 return result;
Jacques Pienaarac86d102018-08-03 08:16:37 -0700291 }
292
Chris Lattnere787b322018-08-08 11:14:57 -0700293 /// Create a deep copy of the specified statement, remapping any operands that
294 /// use values outside of the statement using the map that is provided (
295 /// leaving them alone if no entry is present). Replaces references to cloned
296 /// sub-statements to the corresponding statement that is copied, and adds
297 /// those mappings to the map.
298 Statement *clone(const Statement &stmt,
299 OperationStmt::OperandMapTy &operandMapping) {
300 Statement *cloneStmt = stmt.clone(operandMapping, getContext());
Uday Bondhugula134154e2018-08-06 18:40:34 -0700301 block->getStatements().insert(insertPoint, cloneStmt);
302 return cloneStmt;
Uday Bondhugula84b80952018-08-03 13:22:26 -0700303 }
304
Chris Lattner1604e472018-07-23 08:42:19 -0700305 // Creates for statement. When step is not specified, it is set to 1.
Chris Lattner1628fa02018-08-23 14:32:25 -0700306 ForStmt *createFor(Attribute *location, AffineConstantExpr *lowerBound,
Uday Bondhugula67701712018-08-21 16:01:23 -0700307 AffineConstantExpr *upperBound, int64_t step = 1);
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700308
Chris Lattner1628fa02018-08-23 14:32:25 -0700309 IfStmt *createIf(Attribute *location, IntegerSet *condition);
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700310
311private:
312 StmtBlock *block = nullptr;
313 StmtBlock::iterator insertPoint;
314};
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700315
316} // namespace mlir
317
318#endif