blob: ee3d881020000414c69f204bce98b67a56ea1310 [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.
Chris Lattnerc3251192018-07-27 13:09:58 -070060 FloatType *getBF16Type();
61 FloatType *getF16Type();
62 FloatType *getF32Type();
63 FloatType *getF64Type();
64
65 OtherType *getAffineIntType();
66 OtherType *getTFControlType();
Chris Lattner158e0a3e2018-07-08 20:51:38 -070067 IntegerType *getIntegerType(unsigned width);
68 FunctionType *getFunctionType(ArrayRef<Type *> inputs,
69 ArrayRef<Type *> results);
70 VectorType *getVectorType(ArrayRef<unsigned> shape, Type *elementType);
71 RankedTensorType *getTensorType(ArrayRef<int> shape, Type *elementType);
72 UnrankedTensorType *getTensorType(Type *elementType);
73
Chris Lattner1ac20cb2018-07-10 10:59:53 -070074 // Attributes.
75 BoolAttr *getBoolAttr(bool value);
76 IntegerAttr *getIntegerAttr(int64_t value);
77 FloatAttr *getFloatAttr(double value);
78 StringAttr *getStringAttr(StringRef bytes);
79 ArrayAttr *getArrayAttr(ArrayRef<Attribute *> value);
MLIR Teamb61885d2018-07-18 16:29:21 -070080 AffineMapAttr *getAffineMapAttr(AffineMap *value);
Chris Lattner1ac20cb2018-07-10 10:59:53 -070081
82 // Affine Expressions and Affine Map.
83 AffineMap *getAffineMap(unsigned dimCount, unsigned symbolCount,
Uday Bondhugula0115dbb2018-07-11 21:31:07 -070084 ArrayRef<AffineExpr *> results,
85 ArrayRef<AffineExpr *> rangeSizes);
Chris Lattner1ac20cb2018-07-10 10:59:53 -070086 AffineDimExpr *getDimExpr(unsigned position);
87 AffineSymbolExpr *getSymbolExpr(unsigned position);
88 AffineConstantExpr *getConstantExpr(int64_t constant);
89 AffineExpr *getAddExpr(AffineExpr *lhs, AffineExpr *rhs);
90 AffineExpr *getSubExpr(AffineExpr *lhs, AffineExpr *rhs);
91 AffineExpr *getMulExpr(AffineExpr *lhs, AffineExpr *rhs);
92 AffineExpr *getModExpr(AffineExpr *lhs, AffineExpr *rhs);
93 AffineExpr *getFloorDivExpr(AffineExpr *lhs, AffineExpr *rhs);
94 AffineExpr *getCeilDivExpr(AffineExpr *lhs, AffineExpr *rhs);
95
Chris Lattner158e0a3e2018-07-08 20:51:38 -070096 // TODO: Helpers for affine map/exprs, etc.
Chris Lattner158e0a3e2018-07-08 20:51:38 -070097protected:
98 MLIRContext *context;
99};
100
101/// This class helps build a CFGFunction. Instructions that are created are
102/// automatically inserted at an insertion point or added to the current basic
103/// block.
104class CFGFuncBuilder : public Builder {
105public:
106 CFGFuncBuilder(BasicBlock *block)
107 : Builder(block->getFunction()->getContext()),
108 function(block->getFunction()) {
109 setInsertionPoint(block);
110 }
111 CFGFuncBuilder(CFGFunction *function)
112 : Builder(function->getContext()), function(function) {}
113
114 /// Reset the insertion point to no location. Creating an operation without a
115 /// set insertion point is an error, but this can still be useful when the
116 /// current insertion point a builder refers to is being removed.
117 void clearInsertionPoint() {
118 this->block = nullptr;
119 insertPoint = BasicBlock::iterator();
120 }
121
122 /// Set the insertion point to the end of the specified block.
123 void setInsertionPoint(BasicBlock *block) {
124 this->block = block;
125 insertPoint = block->end();
126 }
127
Tatiana Shpeisman6708b452018-07-24 10:15:13 -0700128 // Add new basic block and set the insertion point to the end of it.
129 BasicBlock *createBlock();
130
131 // Instructions.
Chris Lattner3b2ef762018-07-18 15:31:25 -0700132 OperationInst *createOperation(Identifier name, ArrayRef<CFGValue *> operands,
133 ArrayRef<Type *> resultTypes,
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700134 ArrayRef<NamedAttribute> attributes) {
Chris Lattner3b2ef762018-07-18 15:31:25 -0700135 auto op =
136 OperationInst::create(name, operands, resultTypes, attributes, context);
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700137 block->getOperations().push_back(op);
138 return op;
139 }
140
141 // Terminators.
142
Chris Lattner40746442018-07-21 14:32:09 -0700143 ReturnInst *createReturnInst(ArrayRef<CFGValue *> operands) {
144 return insertTerminator(ReturnInst::create(operands));
145 }
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700146
147 BranchInst *createBranchInst(BasicBlock *dest) {
Chris Lattner40746442018-07-21 14:32:09 -0700148 return insertTerminator(BranchInst::create(dest));
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700149 }
150
James Molloy4f788372018-07-24 15:01:27 -0700151 CondBranchInst *createCondBranchInst(CFGValue *condition,
152 BasicBlock *trueDest,
153 BasicBlock *falseDest) {
154 return insertTerminator(
155 CondBranchInst::create(condition, trueDest, falseDest));
156 }
157
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700158private:
159 template <typename T>
160 T *insertTerminator(T *term) {
161 block->setTerminator(term);
162 return term;
163 }
164
165 CFGFunction *function;
166 BasicBlock *block = nullptr;
167 BasicBlock::iterator insertPoint;
168};
169
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700170/// This class helps build an MLFunction. Statements that are created are
171/// automatically inserted at an insertion point or added to the current
172/// statement block.
173class MLFuncBuilder : public Builder {
174public:
175 MLFuncBuilder(MLFunction *function) : Builder(function->getContext()) {}
176
177 MLFuncBuilder(StmtBlock *block) : MLFuncBuilder(block->getFunction()) {
178 setInsertionPoint(block);
179 }
180
181 /// Reset the insertion point to no location. Creating an operation without a
182 /// set insertion point is an error, but this can still be useful when the
183 /// current insertion point a builder refers to is being removed.
184 void clearInsertionPoint() {
185 this->block = nullptr;
186 insertPoint = StmtBlock::iterator();
187 }
188
189 /// Set the insertion point to the end of the specified block.
190 void setInsertionPoint(StmtBlock *block) {
191 this->block = block;
192 insertPoint = block->end();
193 }
194
Tatiana Shpeisman60bf7be2018-07-26 18:09:20 -0700195 OperationStmt *createOperation(Identifier name, ArrayRef<MLValue *> operands,
196 ArrayRef<Type *> resultTypes,
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700197 ArrayRef<NamedAttribute> attributes) {
Tatiana Shpeisman60bf7be2018-07-26 18:09:20 -0700198 auto op =
199 OperationStmt::create(name, operands, resultTypes, attributes, context);
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700200 block->getStatements().push_back(op);
201 return op;
202 }
203
Chris Lattner1604e472018-07-23 08:42:19 -0700204 // Creates for statement. When step is not specified, it is set to 1.
Tatiana Shpeisman1da50c42018-07-19 09:52:39 -0700205 ForStmt *createFor(AffineConstantExpr *lowerBound,
206 AffineConstantExpr *upperBound,
207 AffineConstantExpr *step = nullptr);
Tatiana Shpeisman565b9642018-07-16 11:47:09 -0700208
209 IfStmt *createIf() {
210 auto stmt = new IfStmt();
211 block->getStatements().push_back(stmt);
212 return stmt;
213 }
214
215private:
216 StmtBlock *block = nullptr;
217 StmtBlock::iterator insertPoint;
218};
Chris Lattner158e0a3e2018-07-08 20:51:38 -0700219
220} // namespace mlir
221
222#endif