blob: 1453a4ac4d90447e8d435205e3b0066c65c1cbc0 [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"
22
23namespace mlir {
24class MLIRContext;
25class Module;
26class Type;
27class PrimitiveType;
28class IntegerType;
29class FunctionType;
30class VectorType;
31class RankedTensorType;
32class UnrankedTensorType;
Chris Lattner1ac20cb2018-07-10 10:59:53 -070033class BoolAttr;
34class IntegerAttr;
35class FloatAttr;
36class StringAttr;
37class ArrayAttr;
38class AffineMap;
39class AffineExpr;
40class AffineConstantExpr;
41class AffineDimExpr;
42class AffineSymbolExpr;
Chris Lattner158e0a3e2018-07-08 20:51:38 -070043
44/// This class is a general helper class for creating context-global objects
45/// like types, attributes, and affine expressions.
46class Builder {
47public:
48 explicit Builder(MLIRContext *context) : context(context) {}
49 explicit Builder(Module *module);
50
51 MLIRContext *getContext() const { return context; }
52
Chris Lattner1ac20cb2018-07-10 10:59:53 -070053 Identifier getIdentifier(StringRef str);
54 Module *createModule();
55
Chris Lattner158e0a3e2018-07-08 20:51:38 -070056 // Types.
57 PrimitiveType *getAffineIntType();
58 PrimitiveType *getBF16Type();
59 PrimitiveType *getF16Type();
60 PrimitiveType *getF32Type();
61 PrimitiveType *getF64Type();
62 IntegerType *getIntegerType(unsigned width);
63 FunctionType *getFunctionType(ArrayRef<Type *> inputs,
64 ArrayRef<Type *> results);
65 VectorType *getVectorType(ArrayRef<unsigned> shape, Type *elementType);
66 RankedTensorType *getTensorType(ArrayRef<int> shape, Type *elementType);
67 UnrankedTensorType *getTensorType(Type *elementType);
68
Chris Lattner1ac20cb2018-07-10 10:59:53 -070069 // Attributes.
70 BoolAttr *getBoolAttr(bool value);
71 IntegerAttr *getIntegerAttr(int64_t value);
72 FloatAttr *getFloatAttr(double value);
73 StringAttr *getStringAttr(StringRef bytes);
74 ArrayAttr *getArrayAttr(ArrayRef<Attribute *> value);
75
76 // Affine Expressions and Affine Map.
77 AffineMap *getAffineMap(unsigned dimCount, unsigned symbolCount,
Uday Bondhugula0115dbb2018-07-11 21:31:07 -070078 ArrayRef<AffineExpr *> results,
79 ArrayRef<AffineExpr *> rangeSizes);
Chris Lattner1ac20cb2018-07-10 10:59:53 -070080 AffineDimExpr *getDimExpr(unsigned position);
81 AffineSymbolExpr *getSymbolExpr(unsigned position);
82 AffineConstantExpr *getConstantExpr(int64_t constant);
83 AffineExpr *getAddExpr(AffineExpr *lhs, AffineExpr *rhs);
84 AffineExpr *getSubExpr(AffineExpr *lhs, AffineExpr *rhs);
85 AffineExpr *getMulExpr(AffineExpr *lhs, AffineExpr *rhs);
86 AffineExpr *getModExpr(AffineExpr *lhs, AffineExpr *rhs);
87 AffineExpr *getFloorDivExpr(AffineExpr *lhs, AffineExpr *rhs);
88 AffineExpr *getCeilDivExpr(AffineExpr *lhs, AffineExpr *rhs);
89
Chris Lattner158e0a3e2018-07-08 20:51:38 -070090 // TODO: Helpers for affine map/exprs, etc.
Chris Lattner158e0a3e2018-07-08 20:51:38 -070091protected:
92 MLIRContext *context;
93};
94
95/// This class helps build a CFGFunction. Instructions that are created are
96/// automatically inserted at an insertion point or added to the current basic
97/// block.
98class CFGFuncBuilder : public Builder {
99public:
100 CFGFuncBuilder(BasicBlock *block)
101 : Builder(block->getFunction()->getContext()),
102 function(block->getFunction()) {
103 setInsertionPoint(block);
104 }
105 CFGFuncBuilder(CFGFunction *function)
106 : Builder(function->getContext()), function(function) {}
107
108 /// Reset the insertion point to no location. Creating an operation without a
109 /// set insertion point is an error, but this can still be useful when the
110 /// current insertion point a builder refers to is being removed.
111 void clearInsertionPoint() {
112 this->block = nullptr;
113 insertPoint = BasicBlock::iterator();
114 }
115
116 /// Set the insertion point to the end of the specified block.
117 void setInsertionPoint(BasicBlock *block) {
118 this->block = block;
119 insertPoint = block->end();
120 }
121
122 OperationInst *createOperation(Identifier name,
123 ArrayRef<NamedAttribute> attributes) {
124 auto op = new OperationInst(name, attributes, context);
125 block->getOperations().push_back(op);
126 return op;
127 }
128
129 // Terminators.
130
131 ReturnInst *createReturnInst() { return insertTerminator(new ReturnInst()); }
132
133 BranchInst *createBranchInst(BasicBlock *dest) {
134 return insertTerminator(new BranchInst(dest));
135 }
136
137private:
138 template <typename T>
139 T *insertTerminator(T *term) {
140 block->setTerminator(term);
141 return term;
142 }
143
144 CFGFunction *function;
145 BasicBlock *block = nullptr;
146 BasicBlock::iterator insertPoint;
147};
148
149// TODO: MLFuncBuilder
150
151} // namespace mlir
152
153#endif