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
16namespace fir {
17
25 mlir::TypeRange type,
26 llvm::SmallVectorImpl<
27 mlir::SideEffects::EffectInstance<mlir::MemoryEffects::Effect>>
28 &effects) {
29 for (mlir::Type t : type) {
31 effects.emplace_back(mlir::MemoryEffects::Read::get(),
32 fir::VolatileMemoryResource::get());
33 effects.emplace_back(mlir::MemoryEffects::Write::get(),
34 fir::VolatileMemoryResource::get());
35 break;
36 }
37 }
38}
39
41inline bool isaCall(mlir::Operation *op) {
42 return mlir::isa<fir::CallOp>(op) || mlir::isa<fir::DispatchOp>(op) ||
43 mlir::isa<mlir::func::CallOp>(op) ||
44 mlir::isa<mlir::func::CallIndirectOp>(op);
45}
46
50inline bool impureCall(mlir::Operation *op) {
51 // Should we also auto-detect that the called function is pure if its
52 // arguments are not references? For now, rely on a "pure" attribute.
53 return op && isaCall(op) && !op->getAttr("pure");
54}
55
59inline bool pureCall(mlir::Operation *op) {
60 // Should we also auto-detect that the called function is pure if its
61 // arguments are not references? For now, rely on a "pure" attribute.
62 return op && isaCall(op) && op->getAttr("pure");
63}
64
70mlir::func::FuncOp createFuncOp(mlir::Location loc, mlir::ModuleOp module,
71 llvm::StringRef name, mlir::FunctionType type,
73 const mlir::SymbolTable *symbolTable = nullptr);
74
77fir::GlobalOp createGlobalOp(mlir::Location loc, mlir::ModuleOp module,
78 llvm::StringRef name, mlir::Type type,
79 llvm::ArrayRef<mlir::NamedAttribute> attrs = {},
80 const mlir::SymbolTable *symbolTable = nullptr);
81
83constexpr llvm::StringRef getContiguousAttrName() { return "fir.contiguous"; }
84
86constexpr llvm::StringRef getOptionalAttrName() { return "fir.optional"; }
87
89static constexpr llvm::StringRef getTargetAttrName() { return "fir.target"; }
90
92static constexpr llvm::StringRef getAsynchronousAttrName() {
93 return "fir.asynchronous";
94}
95
97static constexpr llvm::StringRef getVolatileAttrName() {
98 return "fir.volatile";
99}
100
103static constexpr llvm::StringRef getCharacterProcedureDummyAttrName() {
104 return "fir.char_proc";
105}
106
108static constexpr llvm::StringRef getSymbolAttrName() {
109 return "fir.bindc_name";
110}
111
113static constexpr llvm::StringRef getHostAssocAttrName() {
114 return "fir.host_assoc";
115}
116
118static constexpr llvm::StringRef getHostSymbolAttrName() {
119 return "fir.host_symbol";
120}
121
124static constexpr llvm::StringRef getInternalFuncNameAttrName() {
125 return "fir.internal_name";
126}
127
130static constexpr llvm::StringRef getHasLifetimeMarkerAttrName() {
131 return "fir.has_lifetime";
132}
133
136bool hasHostAssociationArgument(mlir::func::FuncOp func);
137
141inline bool isInternalProcedure(mlir::func::FuncOp func) {
142 return func->hasAttr(fir::getHostSymbolAttrName());
143}
144
153bool valueHasFirAttribute(mlir::Value value, llvm::StringRef attributeName);
154
165bool valueMayHaveFirAttributes(mlir::Value value,
166 llvm::ArrayRef<llvm::StringRef> attributeNames);
167
171bool anyFuncArgsHaveAttr(mlir::func::FuncOp func, llvm::StringRef attr);
172
174std::optional<std::int64_t> getIntIfConstant(mlir::Value value);
175
176static constexpr llvm::StringRef getAdaptToByRefAttrName() {
177 return "adapt.valuebyref";
178}
179
180static constexpr llvm::StringRef getFuncPureAttrName() {
181 return "fir.func_pure";
182}
183
184static constexpr llvm::StringRef getFuncElementalAttrName() {
185 return "fir.func_elemental";
186}
187
188static constexpr llvm::StringRef getFuncRecursiveAttrName() {
189 return "fir.func_recursive";
190}
191
192static constexpr llvm::StringRef getFortranProcedureFlagsAttrName() {
193 return "fir.proc_attrs";
194}
195
196// Attribute for an alloca that is a trivial adaptor for converting a value to
197// pass-by-ref semantics for a VALUE parameter. The optimizer may be able to
198// eliminate these.
199// Template is used to avoid compiler errors in places that don't include
200// FIRBuilder.h
201template <typename Builder>
202inline mlir::NamedAttribute getAdaptToByRefAttr(Builder &builder) {
203 return {mlir::StringAttr::get(builder.getContext(),
204 fir::getAdaptToByRefAttrName()),
205 builder.getUnitAttr()};
206}
207
208bool isDummyArgument(mlir::Value v);
209
210template <fir::FortranProcedureFlagsEnum Flag>
211inline bool hasProcedureAttr(fir::FortranProcedureFlagsEnumAttr flags) {
212 return flags && bitEnumContainsAny(flags.getValue(), Flag);
213}
214
215template <fir::FortranProcedureFlagsEnum Flag>
216inline bool hasProcedureAttr(mlir::Operation *op) {
217 if (auto firCallOp = mlir::dyn_cast<fir::CallOp>(op))
218 return hasProcedureAttr<Flag>(firCallOp.getProcedureAttrsAttr());
219 if (auto firCallOp = mlir::dyn_cast<fir::DispatchOp>(op))
220 return hasProcedureAttr<Flag>(firCallOp.getProcedureAttrsAttr());
221 return hasProcedureAttr<Flag>(
222 op->getAttrOfType<fir::FortranProcedureFlagsEnumAttr>(
223 getFortranProcedureFlagsAttrName()));
224}
225
226inline bool hasBindcAttr(mlir::Operation *op) {
227 return hasProcedureAttr<fir::FortranProcedureFlagsEnum::bind_c>(op);
228}
229
232std::optional<int64_t> getAllocaByteSize(fir::AllocaOp alloca,
233 const mlir::DataLayout &dl,
234 const fir::KindMapping &kindMap);
235
244bool reboxPreservesContinuity(fir::ReboxOp rebox,
245 bool mayHaveNonDefaultLowerBounds = true,
246 bool checkWhole = true);
247
253bool isContiguousEmbox(fir::EmboxOp embox, bool checkWhole = true);
254
255} // namespace fir
256
257#endif // FORTRAN_OPTIMIZER_DIALECT_FIROPSSUPPORT_H
Definition FIRType.h:89
Definition AbstractConverter.h:34
bool isContiguousEmbox(fir::EmboxOp embox, bool checkWhole=true)
Definition FIROps.cpp:2057
bool anyFuncArgsHaveAttr(mlir::func::FuncOp func, llvm::StringRef attr)
Definition FIROps.cpp:4851
bool isa_volatile_type(mlir::Type t)
Definition FIRType.cpp:741
constexpr llvm::StringRef getContiguousAttrName()
Attribute to mark Fortran entities with the CONTIGUOUS attribute.
Definition FIROpsSupport.h:83
bool reboxPreservesContinuity(fir::ReboxOp rebox, bool mayHaveNonDefaultLowerBounds=true, bool checkWhole=true)
Definition FIROps.cpp:4923
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:4723
bool hasHostAssociationArgument(mlir::func::FuncOp func)
Definition FIROps.cpp:4743
std::optional< int64_t > getAllocaByteSize(fir::AllocaOp alloca, const mlir::DataLayout &dl, const fir::KindMapping &kindMap)
Definition FIROps.cpp:4940
bool isInternalProcedure(mlir::func::FuncOp func)
Definition FIROpsSupport.h:141
constexpr llvm::StringRef getOptionalAttrName()
Attribute to mark Fortran entities with the OPTIONAL attribute.
Definition FIROpsSupport.h:86
bool pureCall(mlir::Operation *op)
Definition FIROpsSupport.h:59
void addVolatileMemoryEffects(mlir::TypeRange type, llvm::SmallVectorImpl< mlir::SideEffects::EffectInstance< mlir::MemoryEffects::Effect > > &effects)
Definition FIROpsSupport.h:24
std::optional< std::int64_t > getIntIfConstant(mlir::Value value)
Unwrap integer constant from an mlir::Value.
Definition FIROps.cpp:4858
bool valueHasFirAttribute(mlir::Value value, llvm::StringRef attributeName)
Definition FIROps.cpp:4844
bool valueMayHaveFirAttributes(mlir::Value value, llvm::ArrayRef< llvm::StringRef > attributeNames)
Definition FIROps.cpp:4837
bool impureCall(mlir::Operation *op)
Definition FIROpsSupport.h:50
bool isaCall(mlir::Operation *op)
Return true iff the Operation is a call.
Definition FIROpsSupport.h:41
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:4701