blob: 730fe7df4d412e66e954e58d6c2ddf4e7f1d8bf6 [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,
78 ArrayRef<AffineExpr *> results);
79 AffineDimExpr *getDimExpr(unsigned position);
80 AffineSymbolExpr *getSymbolExpr(unsigned position);
81 AffineConstantExpr *getConstantExpr(int64_t constant);
82 AffineExpr *getAddExpr(AffineExpr *lhs, AffineExpr *rhs);
83 AffineExpr *getSubExpr(AffineExpr *lhs, AffineExpr *rhs);
84 AffineExpr *getMulExpr(AffineExpr *lhs, AffineExpr *rhs);
85 AffineExpr *getModExpr(AffineExpr *lhs, AffineExpr *rhs);
86 AffineExpr *getFloorDivExpr(AffineExpr *lhs, AffineExpr *rhs);
87 AffineExpr *getCeilDivExpr(AffineExpr *lhs, AffineExpr *rhs);
88
Chris Lattner158e0a3e2018-07-08 20:51:38 -070089 // TODO: Helpers for affine map/exprs, etc.
Chris Lattner158e0a3e2018-07-08 20:51:38 -070090protected:
91 MLIRContext *context;
92};
93
94/// This class helps build a CFGFunction. Instructions that are created are
95/// automatically inserted at an insertion point or added to the current basic
96/// block.
97class CFGFuncBuilder : public Builder {
98public:
99 CFGFuncBuilder(BasicBlock *block)
100 : Builder(block->getFunction()->getContext()),
101 function(block->getFunction()) {
102 setInsertionPoint(block);
103 }
104 CFGFuncBuilder(CFGFunction *function)
105 : Builder(function->getContext()), function(function) {}
106
107 /// Reset the insertion point to no location. Creating an operation without a
108 /// set insertion point is an error, but this can still be useful when the
109 /// current insertion point a builder refers to is being removed.
110 void clearInsertionPoint() {
111 this->block = nullptr;
112 insertPoint = BasicBlock::iterator();
113 }
114
115 /// Set the insertion point to the end of the specified block.
116 void setInsertionPoint(BasicBlock *block) {
117 this->block = block;
118 insertPoint = block->end();
119 }
120
121 OperationInst *createOperation(Identifier name,
122 ArrayRef<NamedAttribute> attributes) {
123 auto op = new OperationInst(name, attributes, context);
124 block->getOperations().push_back(op);
125 return op;
126 }
127
128 // Terminators.
129
130 ReturnInst *createReturnInst() { return insertTerminator(new ReturnInst()); }
131
132 BranchInst *createBranchInst(BasicBlock *dest) {
133 return insertTerminator(new BranchInst(dest));
134 }
135
136private:
137 template <typename T>
138 T *insertTerminator(T *term) {
139 block->setTerminator(term);
140 return term;
141 }
142
143 CFGFunction *function;
144 BasicBlock *block = nullptr;
145 BasicBlock::iterator insertPoint;
146};
147
148// TODO: MLFuncBuilder
149
150} // namespace mlir
151
152#endif