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;
33namespace omp {
35} // namespace omp
36} // namespace semantics
37
38namespace parser {
39struct OmpObject;
40struct OmpObjectList;
41} // namespace parser
42
43namespace lower {
45namespace pft {
46struct Evaluation;
47}
48
50
51namespace omp {
52
53struct DeclareTargetCaptureInfo {
54 mlir::omp::DeclareTargetCaptureClause clause;
55 bool automap = false;
56 const semantics::Symbol &symbol;
57
58 DeclareTargetCaptureInfo(mlir::omp::DeclareTargetCaptureClause c,
59 const semantics::Symbol &s, bool a = false)
60 : clause(c), automap(a), symbol(s) {}
61};
62
63// A small helper structure for keeping track of a component members MapInfoOp
64// and index data when lowering OpenMP map clauses. Keeps track of the
65// placement of the component in the derived type hierarchy it rests within,
66// alongside the generated mlir::omp::MapInfoOp for the mapped component.
67//
68// As an example of what the contents of this data structure may be like,
69// when provided the following derived type and map of that type:
70//
71// type :: bottom_layer
72// real(8) :: i2
73// real(4) :: array_i2(10)
74// real(4) :: array_j2(10)
75// end type bottom_layer
76//
77// type :: top_layer
78// real(4) :: i
79// integer(4) :: array_i(10)
80// real(4) :: j
81// type(bottom_layer) :: nested
82// integer, allocatable :: array_j(:)
83// integer(4) :: k
84// end type top_layer
85//
86// type(top_layer) :: top_dtype
87//
88// map(tofrom: top_dtype%nested%i2, top_dtype%k, top_dtype%nested%array_i2)
89//
90// We would end up with an OmpMapParentAndMemberData populated like below:
91//
92// memberPlacementIndices:
93// Vector 1: 3, 0
94// Vector 2: 5
95// Vector 3: 3, 1
96//
97// memberMap:
98// Entry 1: omp.map.info for "top_dtype%nested%i2"
99// Entry 2: omp.map.info for "top_dtype%k"
100// Entry 3: omp.map.info for "top_dtype%nested%array_i2"
101//
102// And this OmpMapParentAndMemberData would be accessed via the parent
103// symbol for top_dtype. Other parent derived type instances that have
104// members mapped would have there own OmpMapParentAndMemberData entry
105// accessed via their own symbol.
107 // The indices representing the component members placement in its derived
108 // type parents hierarchy.
109 llvm::SmallVector<llvm::SmallVector<int64_t>> memberPlacementIndices;
110
111 // Placement of the member in the member vector.
113
114 bool isDuplicateMemberMapInfo(llvm::SmallVectorImpl<int64_t> &memberIndices) {
115 return llvm::find_if(memberPlacementIndices, [&](auto &memberData) {
116 return llvm::equal(memberIndices, memberData);
117 }) != memberPlacementIndices.end();
118 }
119
120 void addChildIndexAndMapToParent(const omp::Object &object,
121 mlir::omp::MapInfoOp &mapOp,
123};
124
125void insertChildMapInfoIntoParent(
129 std::map<Object, OmpMapParentAndMemberData> &parentMemberIndices,
130 llvm::SmallVectorImpl<mlir::Value> &mapOperands,
131 llvm::SmallVectorImpl<Object> &mapObjects);
132
133void generateMemberPlacementIndices(
134 const Object &object, llvm::SmallVectorImpl<int64_t> &indices,
136
137bool isMemberOrParentAllocatableOrPointer(
138 const Object &object, Fortran::semantics::SemanticsContext &semaCtx);
139
140mlir::Value createParentSymAndGenIntermediateMaps(
141 mlir::Location clauseLocation, Fortran::lower::AbstractConverter &converter,
143 omp::ObjectList &objectList, llvm::SmallVectorImpl<int64_t> &indices,
144 OmpMapParentAndMemberData &parentMemberIndices, llvm::StringRef asFortran,
145 mlir::omp::ClauseMapFlags mapTypeBits);
146
147bool requiresImplicitDefaultDeclareMapper(
148 const semantics::DerivedTypeSpec &typeSpec);
149
150omp::ObjectList gatherObjectsOf(omp::Object derivedTypeMember,
152
153mlir::Type getLoopVarType(lower::AbstractConverter &converter,
154 std::size_t loopVarTypeSize);
155
157getIterationVariableSymbol(const lower::pft::Evaluation &eval);
158
159void gatherFuncAndVarSyms(
160 const ObjectList &objects, mlir::omp::DeclareTargetCaptureClause clause,
161 llvm::SmallVectorImpl<DeclareTargetCaptureInfo> &symbolAndClause,
162 bool automap = false);
163
164int64_t getCollapseValue(const List<Clause> &clauses);
165
166void genObjectList(const ObjectList &objects,
167 lower::AbstractConverter &converter,
168 llvm::SmallVectorImpl<mlir::Value> &operands);
169
170void lastprivateModifierNotSupported(const omp::clause::Lastprivate &lastp,
171 mlir::Location loc);
172
173pft::Evaluation *getNestedDoConstruct(pft::Evaluation &eval);
174
175int64_t collectLoopRelatedInfo(
176 lower::AbstractConverter &converter, mlir::Location currentLocation,
178 const omp::List<omp::Clause> &clauses,
179 mlir::omp::LoopRelatedClauseOps &result,
180 llvm::SmallVectorImpl<const semantics::Symbol *> &iv);
181
182void collectLoopRelatedInfo(
183 lower::AbstractConverter &converter, mlir::Location currentLocation,
185 std::int64_t collapseValue,
186 // const omp::List<omp::Clause> &clauses,
187 mlir::omp::LoopRelatedClauseOps &result,
188 llvm::SmallVectorImpl<const semantics::Symbol *> &iv);
189
190void collectTileSizesFromOpenMPConstruct(
191 const parser::OpenMPConstruct *ompCons,
192 llvm::SmallVectorImpl<int64_t> &tileSizes,
194
195mlir::Value genElementSizeInBytes(fir::FirOpBuilder &builder,
196 mlir::Location loc,
197 const mlir::DataLayout &dl,
198 hlfir::Entity entity);
199
200mlir::Value genAffinityAddr(Fortran::lower::AbstractConverter &converter,
201 const omp::Object &object,
203 mlir::Location loc);
204
205mlir::Value genAffinityLen(fir::FirOpBuilder &builder, mlir::Location loc,
206 const mlir::DataLayout &dl, hlfir::Entity entity,
208
210 mlir::Value lb;
211 mlir::Value ub;
212 mlir::Value step;
213 Fortran::semantics::Symbol *ivSym = nullptr;
214};
215
216bool hasIteratorIVReference(
217 const omp::Object &object,
218 const llvm::SmallPtrSetImpl<const Fortran::semantics::Symbol *> &ivSyms);
219
225void defaultMangler(Fortran::lower::AbstractConverter &converter,
226 std::string &mapperIdName, llvm::StringRef memberName);
227
228mlir::Value genIteratorCoordinate(Fortran::lower::AbstractConverter &converter,
229 hlfir::Entity entity,
231 mlir::Location loc);
232
251mlir::FlatSymbolRefAttr
252resolveMapperId(Fortran::lower::AbstractConverter &converter,
253 mlir::Location loc, const omp::Object &object,
254 llvm::StringRef mapperIdName,
255 mlir::omp::ClauseMapFlags mapTypeBits,
256 llvm::omp::Directive directive, bool hasParentObj);
257
258std::optional<llvm::SmallVector<mlir::Value>> getIteratorElementIndices(
259 Fortran::lower::AbstractConverter &converter, const omp::Object &object,
260 Fortran::lower::StatementContext &stmtCtx, mlir::Location loc);
261
266void collectEnclosingConstructTraits(
267 mlir::Operation *op,
268 llvm::SmallVectorImpl<llvm::omp::TraitProperty> &constructTraits);
269
273semantics::omp::OmpVariantMatchContext makeVariantMatchContext(
274 mlir::ModuleOp module,
276
277} // namespace omp
278} // namespace lower
279} // namespace Fortran
280
281#endif // FORTRAN_LOWER_OPENMPUTILS_H
Definition AbstractConverter.h:87
Definition StatementContext.h:46
Definition semantics.h:67
Definition symbol.h:893
Definition FIRBuilder.h:59
Definition HLFIRTools.h:52
Definition FIRType.h:106
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:3632
Definition parse-tree.h:3620
Definition parse-tree.h:5549