FLANG
Utils.h
1//===-- Lower/OpenMP/Utils.h ------------------------------------*- 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_LOWER_OPENMPUTILS_H
10#define FORTRAN_LOWER_OPENMPUTILS_H
11
12#include "flang/Lower/OpenMP/Clauses.h"
13#include "flang/Optimizer/Builder/HLFIRTools.h"
14#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
15#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
16#include "mlir/IR/Location.h"
17#include "mlir/IR/Value.h"
18#include "llvm/Frontend/OpenMP/OMPContext.h"
19#include "llvm/Support/CommandLine.h"
20#include <cstdint>
21#include <optional>
22
23extern llvm::cl::opt<bool> treatIndexAsSection;
24
25namespace fir {
26class FirOpBuilder;
27class RecordType;
28} // namespace fir
29namespace Fortran {
30
31namespace semantics {
32class Symbol;
33} // namespace semantics
34
35namespace parser {
36struct OmpObject;
37struct OmpObjectList;
38} // namespace parser
39
40namespace lower {
42namespace pft {
43struct Evaluation;
44}
45
47
48namespace omp {
49
50struct DeclareTargetCaptureInfo {
51 mlir::omp::DeclareTargetCaptureClause clause;
52 bool automap = false;
53 const semantics::Symbol &symbol;
54
55 DeclareTargetCaptureInfo(mlir::omp::DeclareTargetCaptureClause c,
56 const semantics::Symbol &s, bool a = false)
57 : clause(c), automap(a), symbol(s) {}
58};
59
60// A small helper structure for keeping track of a component members MapInfoOp
61// and index data when lowering OpenMP map clauses. Keeps track of the
62// placement of the component in the derived type hierarchy it rests within,
63// alongside the generated mlir::omp::MapInfoOp for the mapped component.
64//
65// As an example of what the contents of this data structure may be like,
66// when provided the following derived type and map of that type:
67//
68// type :: bottom_layer
69// real(8) :: i2
70// real(4) :: array_i2(10)
71// real(4) :: array_j2(10)
72// end type bottom_layer
73//
74// type :: top_layer
75// real(4) :: i
76// integer(4) :: array_i(10)
77// real(4) :: j
78// type(bottom_layer) :: nested
79// integer, allocatable :: array_j(:)
80// integer(4) :: k
81// end type top_layer
82//
83// type(top_layer) :: top_dtype
84//
85// map(tofrom: top_dtype%nested%i2, top_dtype%k, top_dtype%nested%array_i2)
86//
87// We would end up with an OmpMapParentAndMemberData populated like below:
88//
89// memberPlacementIndices:
90// Vector 1: 3, 0
91// Vector 2: 5
92// Vector 3: 3, 1
93//
94// memberMap:
95// Entry 1: omp.map.info for "top_dtype%nested%i2"
96// Entry 2: omp.map.info for "top_dtype%k"
97// Entry 3: omp.map.info for "top_dtype%nested%array_i2"
98//
99// And this OmpMapParentAndMemberData would be accessed via the parent
100// symbol for top_dtype. Other parent derived type instances that have
101// members mapped would have there own OmpMapParentAndMemberData entry
102// accessed via their own symbol.
104 // The indices representing the component members placement in its derived
105 // type parents hierarchy.
106 llvm::SmallVector<llvm::SmallVector<int64_t>> memberPlacementIndices;
107
108 // Placement of the member in the member vector.
110
111 bool isDuplicateMemberMapInfo(llvm::SmallVectorImpl<int64_t> &memberIndices) {
112 return llvm::find_if(memberPlacementIndices, [&](auto &memberData) {
113 return llvm::equal(memberIndices, memberData);
114 }) != memberPlacementIndices.end();
115 }
116
117 void addChildIndexAndMapToParent(const omp::Object &object,
118 mlir::omp::MapInfoOp &mapOp,
120};
121
122void insertChildMapInfoIntoParent(
126 std::map<Object, OmpMapParentAndMemberData> &parentMemberIndices,
127 llvm::SmallVectorImpl<mlir::Value> &mapOperands,
128 llvm::SmallVectorImpl<Object> &mapObjects);
129
130void generateMemberPlacementIndices(
131 const Object &object, llvm::SmallVectorImpl<int64_t> &indices,
133
134bool isMemberOrParentAllocatableOrPointer(
135 const Object &object, Fortran::semantics::SemanticsContext &semaCtx);
136
137mlir::Value createParentSymAndGenIntermediateMaps(
138 mlir::Location clauseLocation, Fortran::lower::AbstractConverter &converter,
140 omp::ObjectList &objectList, llvm::SmallVectorImpl<int64_t> &indices,
141 OmpMapParentAndMemberData &parentMemberIndices, llvm::StringRef asFortran,
142 mlir::omp::ClauseMapFlags mapTypeBits);
143
144bool requiresImplicitDefaultDeclareMapper(
145 const semantics::DerivedTypeSpec &typeSpec);
146
147omp::ObjectList gatherObjectsOf(omp::Object derivedTypeMember,
149
150mlir::Type getLoopVarType(lower::AbstractConverter &converter,
151 std::size_t loopVarTypeSize);
152
154getIterationVariableSymbol(const lower::pft::Evaluation &eval);
155
156void gatherFuncAndVarSyms(
157 const ObjectList &objects, mlir::omp::DeclareTargetCaptureClause clause,
158 llvm::SmallVectorImpl<DeclareTargetCaptureInfo> &symbolAndClause,
159 bool automap = false);
160
161int64_t getCollapseValue(const List<Clause> &clauses);
162
163void genObjectList(const ObjectList &objects,
164 lower::AbstractConverter &converter,
165 llvm::SmallVectorImpl<mlir::Value> &operands);
166
167void lastprivateModifierNotSupported(const omp::clause::Lastprivate &lastp,
168 mlir::Location loc);
169
170pft::Evaluation *getNestedDoConstruct(pft::Evaluation &eval);
171
172int64_t collectLoopRelatedInfo(
173 lower::AbstractConverter &converter, mlir::Location currentLocation,
175 const omp::List<omp::Clause> &clauses,
176 mlir::omp::LoopRelatedClauseOps &result,
177 llvm::SmallVectorImpl<const semantics::Symbol *> &iv);
178
179void collectLoopRelatedInfo(
180 lower::AbstractConverter &converter, mlir::Location currentLocation,
182 std::int64_t collapseValue,
183 // const omp::List<omp::Clause> &clauses,
184 mlir::omp::LoopRelatedClauseOps &result,
185 llvm::SmallVectorImpl<const semantics::Symbol *> &iv);
186
187void collectTileSizesFromOpenMPConstruct(
188 const parser::OpenMPConstruct *ompCons,
189 llvm::SmallVectorImpl<int64_t> &tileSizes,
191
192mlir::Value genElementSizeInBytes(fir::FirOpBuilder &builder,
193 mlir::Location loc,
194 const mlir::DataLayout &dl,
195 hlfir::Entity entity);
196
197mlir::Value genAffinityAddr(Fortran::lower::AbstractConverter &converter,
198 const omp::Object &object,
200 mlir::Location loc);
201
202mlir::Value genAffinityLen(fir::FirOpBuilder &builder, mlir::Location loc,
203 const mlir::DataLayout &dl, hlfir::Entity entity,
205
207 mlir::Value lb;
208 mlir::Value ub;
209 mlir::Value step;
210 Fortran::semantics::Symbol *ivSym = nullptr;
211};
212
213bool hasIteratorIVReference(
214 const omp::Object &object,
215 const llvm::SmallPtrSetImpl<const Fortran::semantics::Symbol *> &ivSyms);
216
222void defaultMangler(Fortran::lower::AbstractConverter &converter,
223 std::string &mapperIdName, llvm::StringRef memberName);
224
225mlir::Value genIteratorCoordinate(Fortran::lower::AbstractConverter &converter,
226 hlfir::Entity entity,
228 mlir::Location loc);
229
248mlir::FlatSymbolRefAttr
249resolveMapperId(Fortran::lower::AbstractConverter &converter,
250 mlir::Location loc, const omp::Object &object,
251 llvm::StringRef mapperIdName,
252 mlir::omp::ClauseMapFlags mapTypeBits,
253 llvm::omp::Directive directive, bool hasParentObj);
254
255std::optional<llvm::SmallVector<mlir::Value>> getIteratorElementIndices(
256 Fortran::lower::AbstractConverter &converter, const omp::Object &object,
257 Fortran::lower::StatementContext &stmtCtx, mlir::Location loc);
258
263void collectEnclosingConstructTraits(
264 mlir::Operation *op,
265 llvm::SmallVectorImpl<llvm::omp::TraitProperty> &constructTraits);
266
269class FlangOMPContext final : public llvm::omp::OMPContext {
270public:
271 FlangOMPContext(mlir::ModuleOp module,
273 bool matchesISATrait(llvm::StringRef rawString) const override;
274
275private:
276 static bool isDeviceCompilation(mlir::ModuleOp module);
277 mlir::LLVM::TargetFeaturesAttr targetFeatures;
278};
279
280} // namespace omp
281} // namespace lower
282} // namespace Fortran
283
284#endif // FORTRAN_LOWER_OPENMPUTILS_H
Definition AbstractConverter.h:87
Definition StatementContext.h:46
Definition semantics.h:68
Definition symbol.h:881
Definition FIRBuilder.h:59
Definition HLFIRTools.h:52
Definition FIRType.h:103
Definition OpenACC.h:20
Definition ParserActions.h:24
Definition check-expression.h:19
Definition bit-population-count.h:20
Definition AbstractConverter.h:37
Definition PFTBuilder.h:221
Definition parse-tree.h:3628
Definition parse-tree.h:3616
Definition parse-tree.h:5517