blob: 644c812940908198277e5babb24258833bdf6f46 [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;
33
34/// This class is a general helper class for creating context-global objects
35/// like types, attributes, and affine expressions.
36class Builder {
37public:
38 explicit Builder(MLIRContext *context) : context(context) {}
39 explicit Builder(Module *module);
40
41 MLIRContext *getContext() const { return context; }
42
43 // Types.
44 PrimitiveType *getAffineIntType();
45 PrimitiveType *getBF16Type();
46 PrimitiveType *getF16Type();
47 PrimitiveType *getF32Type();
48 PrimitiveType *getF64Type();
49 IntegerType *getIntegerType(unsigned width);
50 FunctionType *getFunctionType(ArrayRef<Type *> inputs,
51 ArrayRef<Type *> results);
52 VectorType *getVectorType(ArrayRef<unsigned> shape, Type *elementType);
53 RankedTensorType *getTensorType(ArrayRef<int> shape, Type *elementType);
54 UnrankedTensorType *getTensorType(Type *elementType);
55
56 // TODO: Helpers for affine map/exprs, etc.
57 // TODO: Helpers for attributes.
58 // TODO: Identifier
59 // TODO: createModule()
60protected:
61 MLIRContext *context;
62};
63
64/// This class helps build a CFGFunction. Instructions that are created are
65/// automatically inserted at an insertion point or added to the current basic
66/// block.
67class CFGFuncBuilder : public Builder {
68public:
69 CFGFuncBuilder(BasicBlock *block)
70 : Builder(block->getFunction()->getContext()),
71 function(block->getFunction()) {
72 setInsertionPoint(block);
73 }
74 CFGFuncBuilder(CFGFunction *function)
75 : Builder(function->getContext()), function(function) {}
76
77 /// Reset the insertion point to no location. Creating an operation without a
78 /// set insertion point is an error, but this can still be useful when the
79 /// current insertion point a builder refers to is being removed.
80 void clearInsertionPoint() {
81 this->block = nullptr;
82 insertPoint = BasicBlock::iterator();
83 }
84
85 /// Set the insertion point to the end of the specified block.
86 void setInsertionPoint(BasicBlock *block) {
87 this->block = block;
88 insertPoint = block->end();
89 }
90
91 OperationInst *createOperation(Identifier name,
92 ArrayRef<NamedAttribute> attributes) {
93 auto op = new OperationInst(name, attributes, context);
94 block->getOperations().push_back(op);
95 return op;
96 }
97
98 // Terminators.
99
100 ReturnInst *createReturnInst() { return insertTerminator(new ReturnInst()); }
101
102 BranchInst *createBranchInst(BasicBlock *dest) {
103 return insertTerminator(new BranchInst(dest));
104 }
105
106private:
107 template <typename T>
108 T *insertTerminator(T *term) {
109 block->setTerminator(term);
110 return term;
111 }
112
113 CFGFunction *function;
114 BasicBlock *block = nullptr;
115 BasicBlock::iterator insertPoint;
116};
117
118// TODO: MLFuncBuilder
119
120} // namespace mlir
121
122#endif