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/OpenMP/OpenMPDialect.h"
15#include "mlir/IR/Location.h"
16#include "mlir/IR/Value.h"
17#include "llvm/Frontend/OpenMP/OMPContext.h"
18#include "llvm/Support/CommandLine.h"
19#include <cstdint>
20#include <optional>
21
22extern llvm::cl::opt<bool> treatIndexAsSection;
23
24namespace fir {
25class FirOpBuilder;
26class RecordType;
27} // namespace fir
28namespace Fortran {
29
30namespace semantics {
31class Symbol;
32} // namespace semantics
33
34namespace parser {
35struct OmpObject;
36struct OmpObjectList;
37} // namespace parser
38
39namespace lower {
41namespace pft {
42struct Evaluation;
43}
44
46
47namespace omp {
48
49struct DeclareTargetCaptureInfo {
50 mlir::omp::DeclareTargetCaptureClause clause;
51 bool automap = false;
52 const semantics::Symbol &symbol;
53
54 DeclareTargetCaptureInfo(mlir::omp::DeclareTargetCaptureClause c,
55 const semantics::Symbol &s, bool a = false)
56 : clause(c), automap(a), symbol(s) {}
57};
58
59// A small helper structure for keeping track of a component members MapInfoOp
60// and index data when lowering OpenMP map clauses. Keeps track of the
61// placement of the component in the derived type hierarchy it rests within,
62// alongside the generated mlir::omp::MapInfoOp for the mapped component.
63//
64// As an example of what the contents of this data structure may be like,
65// when provided the following derived type and map of that type:
66//
67// type :: bottom_layer
68// real(8) :: i2
69// real(4) :: array_i2(10)
70// real(4) :: array_j2(10)
71// end type bottom_layer
72//
73// type :: top_layer
74// real(4) :: i
75// integer(4) :: array_i(10)
76// real(4) :: j
77// type(bottom_layer) :: nested
78// integer, allocatable :: array_j(:)
79// integer(4) :: k
80// end type top_layer
81//
82// type(top_layer) :: top_dtype
83//
84// map(tofrom: top_dtype%nested%i2, top_dtype%k, top_dtype%nested%array_i2)
85//
86// We would end up with an OmpMapParentAndMemberData populated like below:
87//
88// memberPlacementIndices:
89// Vector 1: 3, 0
90// Vector 2: 5
91// Vector 3: 3, 1
92//
93// memberMap:
94// Entry 1: omp.map.info for "top_dtype%nested%i2"
95// Entry 2: omp.map.info for "top_dtype%k"
96// Entry 3: omp.map.info for "top_dtype%nested%array_i2"
97//
98// And this OmpMapParentAndMemberData would be accessed via the parent
99// symbol for top_dtype. Other parent derived type instances that have
100// members mapped would have there own OmpMapParentAndMemberData entry
101// accessed via their own symbol.
103 // The indices representing the component members placement in its derived
104 // type parents hierarchy.
105 llvm::SmallVector<llvm::SmallVector<int64_t>> memberPlacementIndices;
106
107 // Placement of the member in the member vector.
109
110 bool isDuplicateMemberMapInfo(llvm::SmallVectorImpl<int64_t> &memberIndices) {
111 return llvm::find_if(memberPlacementIndices, [&](auto &memberData) {
112 return llvm::equal(memberIndices, memberData);
113 }) != memberPlacementIndices.end();
114 }
115
116 void addChildIndexAndMapToParent(const omp::Object &object,
117 mlir::omp::MapInfoOp &mapOp,
119};
120
121void insertChildMapInfoIntoParent(
125 std::map<Object, OmpMapParentAndMemberData> &parentMemberIndices,
126 llvm::SmallVectorImpl<mlir::Value> &mapOperands,
127 llvm::SmallVectorImpl<Object> &mapObjects);
128
129void generateMemberPlacementIndices(
130 const Object &object, llvm::SmallVectorImpl<int64_t> &indices,
132
133bool isMemberOrParentAllocatableOrPointer(
134 const Object &object, Fortran::semantics::SemanticsContext &semaCtx);
135
136mlir::Value createParentSymAndGenIntermediateMaps(
137 mlir::Location clauseLocation, Fortran::lower::AbstractConverter &converter,
139 omp::ObjectList &objectList, llvm::SmallVectorImpl<int64_t> &indices,
140 OmpMapParentAndMemberData &parentMemberIndices, llvm::StringRef asFortran,
141 mlir::omp::ClauseMapFlags mapTypeBits);
142
143bool requiresImplicitDefaultDeclareMapper(
144 const semantics::DerivedTypeSpec &typeSpec);
145
146omp::ObjectList gatherObjectsOf(omp::Object derivedTypeMember,
148
149mlir::Type getLoopVarType(lower::AbstractConverter &converter,
150 std::size_t loopVarTypeSize);
151
153getIterationVariableSymbol(const lower::pft::Evaluation &eval);
154
155void gatherFuncAndVarSyms(
156 const ObjectList &objects, mlir::omp::DeclareTargetCaptureClause clause,
157 llvm::SmallVectorImpl<DeclareTargetCaptureInfo> &symbolAndClause,
158 bool automap = false);
159
160int64_t getCollapseValue(const List<Clause> &clauses);
161
162void genObjectList(const ObjectList &objects,
163 lower::AbstractConverter &converter,
164 llvm::SmallVectorImpl<mlir::Value> &operands);
165
166void lastprivateModifierNotSupported(const omp::clause::Lastprivate &lastp,
167 mlir::Location loc);
168
169pft::Evaluation *getNestedDoConstruct(pft::Evaluation &eval);
170
171int64_t collectLoopRelatedInfo(
172 lower::AbstractConverter &converter, mlir::Location currentLocation,
174 const omp::List<omp::Clause> &clauses,
175 mlir::omp::LoopRelatedClauseOps &result,
176 llvm::SmallVectorImpl<const semantics::Symbol *> &iv);
177
178void collectLoopRelatedInfo(
179 lower::AbstractConverter &converter, mlir::Location currentLocation,
181 std::int64_t collapseValue,
182 // const omp::List<omp::Clause> &clauses,
183 mlir::omp::LoopRelatedClauseOps &result,
184 llvm::SmallVectorImpl<const semantics::Symbol *> &iv);
185
186void collectTileSizesFromOpenMPConstruct(
187 const parser::OpenMPConstruct *ompCons,
188 llvm::SmallVectorImpl<int64_t> &tileSizes,
190
191mlir::Value genElementSizeInBytes(fir::FirOpBuilder &builder,
192 mlir::Location loc,
193 const mlir::DataLayout &dl,
194 hlfir::Entity entity);
195
196mlir::Value genAffinityAddr(Fortran::lower::AbstractConverter &converter,
197 const omp::Object &object,
199 mlir::Location loc);
200
201mlir::Value genAffinityLen(fir::FirOpBuilder &builder, mlir::Location loc,
202 const mlir::DataLayout &dl, hlfir::Entity entity,
204
206 mlir::Value lb;
207 mlir::Value ub;
208 mlir::Value step;
209 Fortran::semantics::Symbol *ivSym = nullptr;
210};
211
212bool hasIteratorIVReference(
213 const omp::Object &object,
214 const llvm::SmallPtrSetImpl<const Fortran::semantics::Symbol *> &ivSyms);
215
221void defaultMangler(Fortran::lower::AbstractConverter &converter,
222 std::string &mapperIdName, llvm::StringRef memberName);
223
224mlir::Value genIteratorCoordinate(Fortran::lower::AbstractConverter &converter,
225 hlfir::Entity entity,
227 mlir::Location loc);
228
229std::optional<llvm::SmallVector<mlir::Value>> getIteratorElementIndices(
230 Fortran::lower::AbstractConverter &converter, const omp::Object &object,
231 Fortran::lower::StatementContext &stmtCtx, mlir::Location loc);
232
234llvm::omp::TraitSet
235mapTraitSet(parser::OmpTraitSetSelectorName::Value flangSet);
236
239llvm::omp::TraitSelector
240mapTraitSelector(const parser::OmpTraitSelectorName &name,
241 llvm::omp::TraitSet set);
242
247void makeVariantMatchInfo(llvm::omp::VariantMatchInfo &vmi,
248 const parser::modifier::OmpContextSelector &ctxSel,
250 mlir::Location loc,
251 const parser::ScalarExpr *&dynamicCondExpr);
252
253} // namespace omp
254} // namespace lower
255} // namespace Fortran
256
257#endif // FORTRAN_LOWER_OPENMPUTILS_H
Definition AbstractConverter.h:87
Definition StatementContext.h:46
Definition semantics.h:68
Definition symbol.h:854
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:3580
Definition parse-tree.h:3568
Definition parse-tree.h:5478
Definition parse-tree.h:3797