FLANG
FIROpsSupport.h
1//===-- Optimizer/Dialect/FIROpsSupport.h -- FIR op support -----*- 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#ifndef FORTRAN_OPTIMIZER_DIALECT_FIROPSSUPPORT_H
10#define FORTRAN_OPTIMIZER_DIALECT_FIROPSSUPPORT_H
11
12#include "flang/Optimizer/Dialect/FIROps.h"
13#include "mlir/Dialect/Func/IR/FuncOps.h"
14#include "mlir/IR/BuiltinOps.h"
15#include "llvm/ADT/APInt.h"
16
17namespace fir {
18
26 mlir::TypeRange type,
28 mlir::SideEffects::EffectInstance<mlir::MemoryEffects::Effect>>
29 &effects) {
30 for (mlir::Type t : type) {
32 effects.emplace_back(mlir::MemoryEffects::Read::get(),
33 fir::VolatileMemoryResource::get());
34 effects.emplace_back(mlir::MemoryEffects::Write::get(),
35 fir::VolatileMemoryResource::get());
36 break;
37 }
38 }
39}
40
42inline bool isaCall(mlir::Operation *op) {
43 return mlir::isa<fir::CallOp>(op) || mlir::isa<fir::DispatchOp>(op) ||
44 mlir::isa<mlir::func::CallOp>(op) ||
45 mlir::isa<mlir::func::CallIndirectOp>(op);
46}
47
51inline bool impureCall(mlir::Operation *op) {
52 // Should we also auto-detect that the called function is pure if its
53 // arguments are not references? For now, rely on a "pure" attribute.
54 return op && isaCall(op) && !op->getAttr("pure");
55}
56
60inline bool pureCall(mlir::Operation *op) {
61 // Should we also auto-detect that the called function is pure if its
62 // arguments are not references? For now, rely on a "pure" attribute.
63 return op && isaCall(op) && op->getAttr("pure");
64}
65
71mlir::func::FuncOp createFuncOp(mlir::Location loc, mlir::ModuleOp module,
72 llvm::StringRef name, mlir::FunctionType type,
74 const mlir::SymbolTable *symbolTable = nullptr);
75
78fir::GlobalOp createGlobalOp(mlir::Location loc, mlir::ModuleOp module,
79 llvm::StringRef name, mlir::Type type,
80 llvm::ArrayRef<mlir::NamedAttribute> attrs = {},
81 const mlir::SymbolTable *symbolTable = nullptr);
82
84constexpr llvm::StringRef getContiguousAttrName() { return "fir.contiguous"; }
85
87constexpr llvm::StringRef getOptionalAttrName() { return "fir.optional"; }
88
90static constexpr llvm::StringRef getTargetAttrName() { return "fir.target"; }
91
93static constexpr llvm::StringRef getAsynchronousAttrName() {
94 return "fir.asynchronous";
95}
96
98static constexpr llvm::StringRef getVolatileAttrName() {
99 return "fir.volatile";
100}
101
108static constexpr llvm::StringRef getReadOnlyAttrName() {
109 return "fir.read_only";
110}
111
116static constexpr llvm::StringRef getFortranAttrsAttrName() {
117 return "fir.fortran_attrs";
118}
119
122static constexpr llvm::StringRef getCharacterProcedureDummyAttrName() {
123 return "fir.char_proc";
124}
125
127static constexpr llvm::StringRef getSymbolAttrName() {
128 return "fir.bindc_name";
129}
130
132static constexpr llvm::StringRef getHostAssocAttrName() {
133 return "fir.host_assoc";
134}
135
137static constexpr llvm::StringRef getHostSymbolAttrName() {
138 return "fir.host_symbol";
139}
140
145static constexpr llvm::StringRef getDefiningSubmoduleAttrName() {
146 return "fir.defining_submodule";
147}
148
151static constexpr llvm::StringRef getInternalFuncNameAttrName() {
152 return "fir.internal_name";
153}
154
157static constexpr llvm::StringRef getHasLifetimeMarkerAttrName() {
158 return "fir.has_lifetime";
159}
160
162static constexpr llvm::StringRef getAccessGroupsAttrName() {
163 return "access_groups";
164}
165
170static constexpr llvm::StringRef getUniqNameAttrName() { return "uniq_name"; }
171
173constexpr llvm::StringRef getCorankAttrName() { return "fir.corank"; }
174
177bool hasHostAssociationArgument(mlir::func::FuncOp func);
178
182inline bool isInternalProcedure(mlir::func::FuncOp func) {
183 return func->hasAttr(fir::getHostSymbolAttrName());
184}
185
194bool valueHasFirAttribute(mlir::Value value, llvm::StringRef attributeName);
195
206bool valueMayHaveFirAttributes(mlir::Value value,
207 llvm::ArrayRef<llvm::StringRef> attributeNames);
208
212bool anyFuncArgsHaveAttr(mlir::func::FuncOp func, llvm::StringRef attr);
213
215std::optional<llvm::APInt> getIntIfConstant(mlir::Value value);
216
217static constexpr llvm::StringRef getAdaptToByRefAttrName() {
218 return "adapt.valuebyref";
219}
220
221static constexpr llvm::StringRef getFuncPureAttrName() {
222 return "fir.func_pure";
223}
224
225static constexpr llvm::StringRef getFuncElementalAttrName() {
226 return "fir.func_elemental";
227}
228
229static constexpr llvm::StringRef getFuncRecursiveAttrName() {
230 return "fir.func_recursive";
231}
232
233static constexpr llvm::StringRef getFortranProcedureFlagsAttrName() {
234 return "fir.proc_attrs";
235}
236
237// Attribute for an alloca that is a trivial adaptor for converting a value to
238// pass-by-ref semantics for a VALUE parameter. The optimizer may be able to
239// eliminate these.
240// Template is used to avoid compiler errors in places that don't include
241// FIRBuilder.h
242template <typename Builder>
243inline mlir::NamedAttribute getAdaptToByRefAttr(Builder &builder) {
244 return {mlir::StringAttr::get(builder.getContext(),
245 fir::getAdaptToByRefAttrName()),
246 builder.getUnitAttr()};
247}
248
249bool isDummyArgument(mlir::Value v);
250
252enum class FortranDummyIntent { In, Out, InOut };
253
258inline std::optional<FortranDummyIntent>
259getFortranDummyIntent(mlir::func::FuncOp callee, unsigned argIdx) {
260 if (!callee || argIdx >= callee.getNumArguments())
261 return std::nullopt;
262 auto attrs = callee.getArgAttrOfType<FortranVariableFlagsAttr>(
263 argIdx, getFortranAttrsAttrName());
264 if (!attrs)
265 return std::nullopt;
266 switch (attrs.getFlags()) {
267 case FortranVariableFlagsEnum::intent_in:
268 return FortranDummyIntent::In;
269 case FortranVariableFlagsEnum::intent_out:
270 return FortranDummyIntent::Out;
271 case FortranVariableFlagsEnum::intent_inout:
272 return FortranDummyIntent::InOut;
273 default:
274 return std::nullopt;
275 }
276}
277
278template <fir::FortranProcedureFlagsEnum Flag>
279inline bool hasProcedureAttr(fir::FortranProcedureFlagsEnumAttr flags) {
280 return flags && bitEnumContainsAny(flags.getValue(), Flag);
281}
282
283template <fir::FortranProcedureFlagsEnum Flag>
284inline bool hasProcedureAttr(mlir::Operation *op) {
285 if (auto firCallOp = mlir::dyn_cast<fir::CallOp>(op))
286 return hasProcedureAttr<Flag>(firCallOp.getProcedureAttrsAttr());
287 if (auto firCallOp = mlir::dyn_cast<fir::DispatchOp>(op))
288 return hasProcedureAttr<Flag>(firCallOp.getProcedureAttrsAttr());
289 return hasProcedureAttr<Flag>(
290 op->getAttrOfType<fir::FortranProcedureFlagsEnumAttr>(
291 getFortranProcedureFlagsAttrName()));
292}
293
294inline bool hasBindcAttr(mlir::Operation *op) {
295 return hasProcedureAttr<fir::FortranProcedureFlagsEnum::bind_c>(op);
296}
297
300std::optional<int64_t> getAllocaByteSize(fir::AllocaOp alloca,
301 const mlir::DataLayout &dl,
302 const fir::KindMapping &kindMap);
303
312bool reboxPreservesContinuity(fir::ReboxOp rebox,
313 bool mayHaveNonDefaultLowerBounds = true,
314 bool checkWhole = true);
315
321bool isContiguousEmbox(fir::EmboxOp embox, bool checkWhole = true);
322
327bool isInOffloadRegion(mlir::Operation *op);
328
329} // namespace fir
330
331#endif // FORTRAN_OPTIMIZER_DIALECT_FIROPSSUPPORT_H
Definition FIRType.h:106
Definition OpenACC.h:22
Definition AbstractConverter.h:37
constexpr llvm::StringRef getCorankAttrName()
Attribute to mark coarray Fortran entities with the CORANK attribute.
Definition FIROpsSupport.h:173
bool isContiguousEmbox(fir::EmboxOp embox, bool checkWhole=true)
Definition FIROps.cpp:2857
bool anyFuncArgsHaveAttr(mlir::func::FuncOp func, llvm::StringRef attr)
Definition FIROps.cpp:6162
bool isa_volatile_type(mlir::Type t)
Definition FIRType.cpp:768
constexpr llvm::StringRef getContiguousAttrName()
Attribute to mark Fortran entities with the CONTIGUOUS attribute.
Definition FIROpsSupport.h:84
bool reboxPreservesContinuity(fir::ReboxOp rebox, bool mayHaveNonDefaultLowerBounds=true, bool checkWhole=true)
Definition FIROps.cpp:6234
fir::GlobalOp createGlobalOp(mlir::Location loc, mlir::ModuleOp module, llvm::StringRef name, mlir::Type type, llvm::ArrayRef< mlir::NamedAttribute > attrs={}, const mlir::SymbolTable *symbolTable=nullptr)
Definition FIROps.cpp:6026
bool hasHostAssociationArgument(mlir::func::FuncOp func)
Definition FIROps.cpp:6046
std::optional< int64_t > getAllocaByteSize(fir::AllocaOp alloca, const mlir::DataLayout &dl, const fir::KindMapping &kindMap)
Definition FIROps.cpp:6251
bool isInternalProcedure(mlir::func::FuncOp func)
Definition FIROpsSupport.h:182
std::optional< llvm::APInt > getIntIfConstant(mlir::Value value)
Unwrap an integer constant from an mlir::Value as an APInt.
Definition FIROps.cpp:6169
constexpr llvm::StringRef getOptionalAttrName()
Attribute to mark Fortran entities with the OPTIONAL attribute.
Definition FIROpsSupport.h:87
bool pureCall(mlir::Operation *op)
Definition FIROpsSupport.h:60
void addVolatileMemoryEffects(mlir::TypeRange type, llvm::SmallVectorImpl< mlir::SideEffects::EffectInstance< mlir::MemoryEffects::Effect > > &effects)
Definition FIROpsSupport.h:25
bool isInOffloadRegion(mlir::Operation *op)
Definition FIROps.cpp:7053
bool valueHasFirAttribute(mlir::Value value, llvm::StringRef attributeName)
Definition FIROps.cpp:6155
bool valueMayHaveFirAttributes(mlir::Value value, llvm::ArrayRef< llvm::StringRef > attributeNames)
Definition FIROps.cpp:6148
bool impureCall(mlir::Operation *op)
Definition FIROpsSupport.h:51
bool isaCall(mlir::Operation *op)
Return true iff the Operation is a call.
Definition FIROpsSupport.h:42
FortranDummyIntent
Intent of function argument argIdx.
Definition FIROpsSupport.h:252
std::optional< FortranDummyIntent > getFortranDummyIntent(mlir::func::FuncOp callee, unsigned argIdx)
Definition FIROpsSupport.h:259
mlir::func::FuncOp createFuncOp(mlir::Location loc, mlir::ModuleOp module, llvm::StringRef name, mlir::FunctionType type, llvm::ArrayRef< mlir::NamedAttribute > attrs={}, const mlir::SymbolTable *symbolTable=nullptr)
Definition FIROps.cpp:6004