FLANG

FIRBuilder.h
1 //===-- Lower/FirBuilder.h -- FIR operation builder -------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Builder routines for constructing the FIR dialect of MLIR. As FIR is a
10 // dialect of MLIR, it makes extensive use of MLIR interfaces and MLIR's coding
11 // style (https://mlir.llvm.org/getting_started/DeveloperGuide/) is used in this
12 // module.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef FORTRAN_LOWER_FIRBUILDER_H
17 #define FORTRAN_LOWER_FIRBUILDER_H
18
19 #include "flang/Common/reference.h"
20 #include "flang/Optimizer/Dialect/FIROps.h"
21 #include "flang/Optimizer/Dialect/FIRType.h"
22 #include "flang/Optimizer/Support/KindMapping.h"
23 #include "mlir/IR/Builders.h"
24 #include "mlir/IR/Function.h"
25 #include "mlir/IR/Module.h"
26 #include "llvm/ADT/DenseMap.h"
27 #include "llvm/ADT/Optional.h"
28
29 namespace Fortran::lower {
30
31 class AbstractConverter;
32
33 //===----------------------------------------------------------------------===//
34 // FirOpBuilder
35 //===----------------------------------------------------------------------===//
36
39 class FirOpBuilder : public mlir::OpBuilder {
40 public:
41 explicit FirOpBuilder(mlir::Operation *op, const fir::KindMapping &kindMap)
42 : OpBuilder{op}, kindMap{kindMap} {}
43
45 mlir::Region &getRegion() { return *getBlock()->getParent(); }
46
48 mlir::ModuleOp getModule() {
49 return getRegion().getParentOfType<mlir::ModuleOp>();
50 }
51
53 mlir::FuncOp getFunction() {
54 return getRegion().getParentOfType<mlir::FuncOp>();
55 }
56
58 const fir::KindMapping &getKindMap() { return kindMap; }
59
64 mlir::Value convertWithSemantics(mlir::Location loc, mlir::Type toTy,
65 mlir::Value val);
66
68 mlir::Block *getEntryBlock() { return &getFunction().front(); }
69
71 mlir::Type getRefType(mlir::Type eleTy);
72
75 mlir::Value createNullConstant(mlir::Location loc);
76
78 mlir::Value createIntegerConstant(mlir::Location loc, mlir::Type integerType,
79 std::int64_t i);
80
81 mlir::Value createRealConstant(mlir::Location loc, mlir::Type realType,
82 const llvm::APFloat &val);
84 mlir::Value createRealZeroConstant(mlir::Location loc, mlir::Type realType);
85
88 mlir::Value allocateLocal(mlir::Location loc, mlir::Type ty,
89 llvm::StringRef nm,
90 llvm::ArrayRef<mlir::Value> shape,
91 bool asTarget = false);
92
96 mlir::Value createTemporary(mlir::Location loc, mlir::Type type,
97 llvm::StringRef name = {},
98 llvm::ArrayRef<mlir::Value> shape = {});
99
101 mlir::Value createTemporary(mlir::Location loc, mlir::Type type,
102 llvm::ArrayRef<mlir::Value> shape) {
103 return createTemporary(loc, type, llvm::StringRef{}, shape);
104 }
105
107 fir::GlobalOp createGlobal(mlir::Location loc, mlir::Type type,
108 llvm::StringRef name,
109 mlir::StringAttr linkage = {},
110 mlir::Attribute value = {}, bool isConst = false);
111
112 fir::GlobalOp createGlobal(mlir::Location loc, mlir::Type type,
113 llvm::StringRef name, bool isConst,
114 std::function<void(FirOpBuilder &)> bodyBuilder,
115 mlir::StringAttr linkage = {});
116
118 fir::GlobalOp createGlobalConstant(mlir::Location loc, mlir::Type type,
119 llvm::StringRef name,
120 mlir::StringAttr linkage = {},
121 mlir::Attribute value = {}) {
122 return createGlobal(loc, type, name, linkage, value, /*isConst=*/true);
123 }
124
125 fir::GlobalOp
126 createGlobalConstant(mlir::Location loc, mlir::Type type,
127 llvm::StringRef name,
128 std::function<void(FirOpBuilder &)> bodyBuilder,
129 mlir::StringAttr linkage = {}) {
130 return createGlobal(loc, type, name, /*isConst=*/true, bodyBuilder,
131 linkage);
132 }
133
135 fir::StringLitOp createStringLit(mlir::Location loc, mlir::Type eleTy,
136 llvm::StringRef string);
137
140 mlir::FuncOp getNamedFunction(llvm::StringRef name) {
141 return getNamedFunction(getModule(), name);
142 }
143
144 static mlir::FuncOp getNamedFunction(mlir::ModuleOp module,
145 llvm::StringRef name);
146
147 fir::GlobalOp getNamedGlobal(llvm::StringRef name) {
148 return getNamedGlobal(getModule(), name);
149 }
150
151 static fir::GlobalOp getNamedGlobal(mlir::ModuleOp module,
152 llvm::StringRef name);
153
155 mlir::Value createConvert(mlir::Location loc, mlir::Type toTy,
156 mlir::Value val);
157
160 mlir::FuncOp createFunction(mlir::Location loc, llvm::StringRef name,
161 mlir::FunctionType ty) {
162 return createFunction(loc, getModule(), name, ty);
163 }
164
165 static mlir::FuncOp createFunction(mlir::Location loc, mlir::ModuleOp module,
166 llvm::StringRef name,
167 mlir::FunctionType ty);
168
171 mlir::FuncOp addNamedFunction(mlir::Location loc, llvm::StringRef name,
172 mlir::FunctionType ty) {
173 if (auto func = getNamedFunction(name))
174 return func;
175 return createFunction(loc, name, ty);
176 }
177
178 static mlir::FuncOp addNamedFunction(mlir::Location loc,
179 mlir::ModuleOp module,
180 llvm::StringRef name,
181 mlir::FunctionType ty) {
182 if (auto func = getNamedFunction(module, name))
183 return func;
184 return createFunction(loc, module, name, ty);
185 }
186
188 mlir::Value convertToIndexType(mlir::Location loc, mlir::Value val) {
189 return createConvert(loc, getIndexType(), val);
190 }
191
192 private:
193 const fir::KindMapping &kindMap;
194 };
195
196 } // namespace Fortran::lower
197
198 #endif // FORTRAN_LOWER_FIRBUILDER_H
Fortran::lower::FirOpBuilder::createStringLit
fir::StringLitOp createStringLit(mlir::Location loc, mlir::Type eleTy, llvm::StringRef string)
Convert a StringRef string into a fir::StringLitOp.
Definition: FIRBuilder.cpp:174
Fortran::lower::FirOpBuilder::getRegion
mlir::Region & getRegion()
Get the current Region of the insertion point.
Definition: FIRBuilder.h:45
Fortran::lower::FirOpBuilder::createNullConstant
mlir::Value createNullConstant(mlir::Location loc)
Definition: FIRBuilder.cpp:42
Fortran::lower::FirOpBuilder::getFunction
mlir::FuncOp getFunction()
Get the current Function.
Definition: FIRBuilder.h:53
Fortran::lower::FirOpBuilder::getRefType
mlir::Type getRefType(mlir::Type eleTy)
Safely create a reference type to the type eleTy.
Definition: FIRBuilder.cpp:36
Fortran::lower::FirOpBuilder::getModule
mlir::ModuleOp getModule()
Get the current Module.
Definition: FIRBuilder.h:48
Fortran::lower::FirOpBuilder::createTemporary
mlir::Value createTemporary(mlir::Location loc, mlir::Type type, llvm::StringRef name={}, llvm::ArrayRef< mlir::Value > shape={})
Definition: FIRBuilder.cpp:90
Fortran::lower::FirOpBuilder::createTemporary
mlir::Value createTemporary(mlir::Location loc, mlir::Type type, llvm::ArrayRef< mlir::Value > shape)
Create an unnamed and untracked temporary on the stack.
Definition: FIRBuilder.h:101
Fortran::lower::FirOpBuilder::convertToIndexType
mlir::Value convertToIndexType(mlir::Location loc, mlir::Value val)
Cast the input value to IndexType.
Definition: FIRBuilder.h:188
Fortran::lower::FirOpBuilder::getEntryBlock
mlir::Block * getEntryBlock()
Get the entry block of the current Function.
Definition: FIRBuilder.h:68
Fortran::lower::FirOpBuilder::createGlobal
fir::GlobalOp createGlobal(mlir::Location loc, mlir::Type type, llvm::StringRef name, mlir::StringAttr linkage={}, mlir::Attribute value={}, bool isConst=false)
Create a global value.
Definition: FIRBuilder.cpp:106
Fortran::lower::FirOpBuilder::createIntegerConstant
mlir::Value createIntegerConstant(mlir::Location loc, mlir::Type integerType, std::int64_t i)
Create an integer constant of type type and value i.
Definition: FIRBuilder.cpp:49
Fortran::lower::FirOpBuilder::addNamedFunction
mlir::FuncOp addNamedFunction(mlir::Location loc, llvm::StringRef name, mlir::FunctionType ty)
Definition: FIRBuilder.h:171
Fortran::lower::FirOpBuilder::getNamedFunction
mlir::FuncOp getNamedFunction(llvm::StringRef name)
Definition: FIRBuilder.h:140
llvm::ArrayRef
Definition: FIRType.h:20
Fortran::lower::FirOpBuilder::createRealZeroConstant
mlir::Value createRealZeroConstant(mlir::Location loc, mlir::Type realType)
Create a real constant of type realType with a value zero.
Definition: FIRBuilder.cpp:60
fir::KindMapping
Definition: KindMapping.h:43
Fortran::lower::FirOpBuilder::getKindMap
const fir::KindMapping & getKindMap()
Get a reference to the kind map.
Definition: FIRBuilder.h:58
Fortran::lower::FirOpBuilder::convertWithSemantics
mlir::Value convertWithSemantics(mlir::Location loc, mlir::Type toTy, mlir::Value val)
Definition: FIRBuilder.cpp:138
Fortran::lower::FirOpBuilder::createConvert
mlir::Value createConvert(mlir::Location loc, mlir::Type toTy, mlir::Value val)
Lazy creation of fir.convert op.
Definition: FIRBuilder.cpp:166
Fortran::lower::FirOpBuilder::allocateLocal
mlir::Value allocateLocal(mlir::Location loc, mlir::Type ty, llvm::StringRef nm, llvm::ArrayRef< mlir::Value > shape, bool asTarget=false)
Definition: FIRBuilder.cpp:73
Fortran::lower::FirOpBuilder::createFunction
mlir::FuncOp createFunction(mlir::Location loc, llvm::StringRef name, mlir::FunctionType ty)
Definition: FIRBuilder.h:160
Fortran::lower::FirOpBuilder
Definition: FIRBuilder.h:39
Fortran::lower::FirOpBuilder::createGlobalConstant
fir::GlobalOp createGlobalConstant(mlir::Location loc, mlir::Type type, llvm::StringRef name, mlir::StringAttr linkage={}, mlir::Attribute value={})
Create a global constant (read-only) value.
Definition: FIRBuilder.h:118