FLANG
ClauseProcessor.h
1//===-- Lower/OpenMP/ClauseProcessor.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// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10//
11//===----------------------------------------------------------------------===//
12#ifndef FORTRAN_LOWER_CLAUSEPROCESSOR_H
13#define FORTRAN_LOWER_CLAUSEPROCESSOR_H
14
15#include "ClauseFinder.h"
16#include "Utils.h"
17#include "flang/Lower/AbstractConverter.h"
18#include "flang/Lower/Bridge.h"
19#include "flang/Lower/DirectivesCommon.h"
20#include "flang/Lower/OpenMP/Clauses.h"
21#include "flang/Lower/Support/ReductionProcessor.h"
22#include "flang/Optimizer/Builder/Todo.h"
23#include "flang/Parser/char-block.h"
24#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
25
26namespace fir {
27class FirOpBuilder;
28} // namespace fir
29
30namespace Fortran {
31namespace lower {
32namespace omp {
33
34// Container type for tracking user specified Defaultmaps for a target region
35using DefaultMapsTy = std::map<clause::Defaultmap::VariableCategory,
36 clause::Defaultmap::ImplicitBehavior>;
37
52class ClauseProcessor {
53public:
54 ClauseProcessor(lower::AbstractConverter &converter,
56 const List<Clause> &clauses)
57 : converter(converter), semaCtx(semaCtx), clauses(clauses) {}
58
59 // 'Unique' clauses: They can appear at most once in the clause list.
60 bool processAlign(mlir::omp::AlignClauseOps &result) const;
61 bool processAllocator(lower::StatementContext &stmtCtx,
62 mlir::omp::AllocatorClauseOps &result) const;
63 bool processBind(mlir::omp::BindClauseOps &result) const;
64 bool processCancelDirectiveName(
65 mlir::omp::CancelDirectiveNameClauseOps &result) const;
66 bool
67 processCollapse(mlir::Location currentLocation, lower::pft::Evaluation &eval,
68 mlir::omp::LoopRelatedClauseOps &loopResult,
69 mlir::omp::CollapseClauseOps &collapseResult,
70 llvm::SmallVectorImpl<const semantics::Symbol *> &iv) const;
71 bool processSizes(StatementContext &stmtCtx,
72 mlir::omp::SizesClauseOps &result) const;
73 bool processLooprange(StatementContext &stmtCtx,
74 mlir::omp::LooprangeClauseOps &result,
75 int64_t &count) const;
76 bool processDevice(lower::StatementContext &stmtCtx,
77 mlir::omp::DeviceClauseOps &result) const;
78 bool processDeviceType(mlir::omp::DeviceTypeClauseOps &result) const;
79 bool processDistSchedule(lower::StatementContext &stmtCtx,
80 mlir::omp::DistScheduleClauseOps &result) const;
81 bool processExclusive(mlir::Location currentLocation,
82 mlir::omp::ExclusiveClauseOps &result) const;
83 bool processFilter(lower::StatementContext &stmtCtx,
84 mlir::omp::FilterClauseOps &result) const;
85 bool processFinal(lower::StatementContext &stmtCtx,
86 mlir::omp::FinalClauseOps &result) const;
87 bool processGrainsize(lower::StatementContext &stmtCtx,
88 mlir::omp::GrainsizeClauseOps &result) const;
89 bool
90 processHasDeviceAddr(lower::StatementContext &stmtCtx,
91 mlir::omp::HasDeviceAddrClauseOps &result,
92 llvm::SmallVectorImpl<Object> &hasDeviceObjects) const;
93 bool processHint(mlir::omp::HintClauseOps &result) const;
94 bool processInbranch(mlir::omp::InbranchClauseOps &result) const;
95 bool processInclusive(mlir::Location currentLocation,
96 mlir::omp::InclusiveClauseOps &result) const;
97 bool processInitializer(
98 lower::SymMap &symMap,
99 ReductionProcessor::GenInitValueCBTy &genInitValueCB,
100 const parser::OmpStylizedInstance *parserInitInstance = nullptr,
101 unsigned instanceIdx = 0) const;
102 bool processMergeable(mlir::omp::MergeableClauseOps &result) const;
103 bool processNogroup(mlir::omp::NogroupClauseOps &result) const;
104 bool processNotinbranch(mlir::omp::NotinbranchClauseOps &result) const;
105 bool processNowait(mlir::omp::NowaitClauseOps &result) const;
106 bool processNumTasks(lower::StatementContext &stmtCtx,
107 mlir::omp::NumTasksClauseOps &result) const;
108 bool processNumTeams(lower::StatementContext &stmtCtx,
109 mlir::omp::NumTeamsClauseOps &result) const;
110 bool processNumThreads(lower::StatementContext &stmtCtx,
111 mlir::omp::NumThreadsClauseOps &result) const;
112 bool processOrder(mlir::omp::OrderClauseOps &result) const;
113 bool processOrdered(mlir::omp::OrderedClauseOps &result) const;
114 bool processPartial(std::optional<int64_t> &result) const;
115 bool processPriority(lower::StatementContext &stmtCtx,
116 mlir::omp::PriorityClauseOps &result) const;
117 bool processProcBind(mlir::omp::ProcBindClauseOps &result) const;
118 bool processTileSizes(lower::pft::Evaluation &eval,
119 mlir::omp::LoopNestOperands &result) const;
120 bool processSafelen(mlir::omp::SafelenClauseOps &result) const;
121 bool processSchedule(lower::StatementContext &stmtCtx,
122 mlir::omp::ScheduleClauseOps &result) const;
123 bool processSimdlen(mlir::omp::SimdlenClauseOps &result) const;
124 bool processSimd(mlir::omp::OrderedRegionOperands &result) const;
125 bool processThreadLimit(lower::StatementContext &stmtCtx,
126 mlir::omp::ThreadLimitClauseOps &result) const;
127 bool processUntied(mlir::omp::UntiedClauseOps &result) const;
128 bool processDetach(mlir::omp::DetachClauseOps &result) const;
129 // 'Repeatable' clauses: They can appear multiple times in the clause list.
130 bool processAffinity(mlir::omp::AffinityClauseOps &result) const;
131 bool processAligned(mlir::omp::AlignedClauseOps &result) const;
132 bool processAllocate(mlir::omp::AllocateClauseOps &result) const;
133 bool processCopyin() const;
134 bool processCopyprivate(mlir::Location currentLocation,
135 mlir::omp::CopyprivateClauseOps &result) const;
136 bool processDefaultMap(lower::StatementContext &stmtCtx,
137 DefaultMapsTy &result) const;
138 bool processDepend(lower::SymMap &symMap, lower::StatementContext &stmtCtx,
139 mlir::omp::DependClauseOps &result) const;
140 bool
141 processDynGroupprivate(lower::StatementContext &stmtCtx,
142 mlir::omp::DynGroupprivateClauseOps &result) const;
143 bool
144 processEnter(llvm::SmallVectorImpl<DeclareTargetCaptureInfo> &result) const;
145 bool processIf(omp::clause::If::DirectiveNameModifier directiveName,
146 mlir::omp::IfClauseOps &result) const;
147 bool
148 processInReduction(mlir::Location currentLocation,
149 mlir::omp::InReductionClauseOps &result,
150 llvm::SmallVectorImpl<Object> &outReductionObjects) const;
151 bool processIsDevicePtr(lower::StatementContext &stmtCtx,
152 mlir::omp::IsDevicePtrClauseOps &result,
153 llvm::SmallVectorImpl<Object> &isDeviceObjects) const;
154 bool processLinear(mlir::omp::LinearClauseOps &result,
155 bool isDeclareSimd = false) const;
156 bool
157 processLink(llvm::SmallVectorImpl<DeclareTargetCaptureInfo> &result) const;
158
159 // This method is used to process a map clause.
160 // The optional parameter mapObjects is used to store the original Fortran
161 // objects for the map operands. It may be used later on to create the
162 // block_arguments for some of the directives that require it.
163 bool processMap(mlir::Location currentLocation,
165 mlir::omp::MapClauseOps &result,
166 llvm::omp::Directive directive = llvm::omp::OMPD_unknown,
167 llvm::SmallVectorImpl<Object> *mapObjects = nullptr) const;
168 bool processMotionClauses(lower::StatementContext &stmtCtx,
169 mlir::omp::MapClauseOps &result);
170 bool processNontemporal(mlir::omp::NontemporalClauseOps &result) const;
171 bool processReduction(mlir::Location currentLocation,
172 mlir::omp::ReductionClauseOps &result,
173 llvm::SmallVectorImpl<Object> &reductionObjects,
174 llvm::DenseMap<const semantics::Symbol *, mlir::Value>
175 *reductionVarCache = nullptr) const;
176 bool processTaskReduction(
177 mlir::Location currentLocation, mlir::omp::TaskReductionClauseOps &result,
178 llvm::SmallVectorImpl<Object> &outReductionObjects) const;
179 bool processTo(llvm::SmallVectorImpl<DeclareTargetCaptureInfo> &result) const;
180 bool
181 processUseDeviceAddr(lower::StatementContext &stmtCtx,
182 mlir::omp::UseDeviceAddrClauseOps &result,
183 llvm::SmallVectorImpl<Object> &useDeviceObjects) const;
184 bool
185 processUseDevicePtr(lower::StatementContext &stmtCtx,
186 mlir::omp::UseDevicePtrClauseOps &result,
187 llvm::SmallVectorImpl<Object> &useDeviceObjects) const;
188 bool processUniform(mlir::omp::UniformClauseOps &result) const;
189
190 // Call this method for these clauses that should be supported but are not
191 // implemented yet. It triggers a compilation error if any of the given
192 // clauses is found.
193 template <typename... Ts>
194 void processTODO(mlir::Location currentLocation,
195 llvm::omp::Directive directive) const;
196
197private:
198 using ClauseIterator = List<Clause>::const_iterator;
199
203 template <typename T>
204 const T *findUniqueClause(const parser::CharBlock **source = nullptr) const;
205
208 template <typename T>
209 bool findRepeatableClause(
210 std::function<void(const T &, const parser::CharBlock &source)>
211 callbackFn) const;
212
214 template <typename T>
215 bool markClauseOccurrence(mlir::UnitAttr &result) const;
216
217 void processMapObjects(
218 lower::StatementContext &stmtCtx, mlir::Location clauseLocation,
219 const omp::ObjectList &objects, mlir::omp::ClauseMapFlags mapTypeBits,
220 std::map<Object, OmpMapParentAndMemberData> &parentMemberIndices,
221 llvm::SmallVectorImpl<mlir::Value> &mapVars,
222 llvm::SmallVectorImpl<Object> &mapObjects,
223 llvm::StringRef mapperIdNameRef = "", bool isMotionModifier = false,
224 llvm::omp::Directive directive = llvm::omp::OMPD_unknown) const;
225
226 lower::AbstractConverter &converter;
228 List<Clause> clauses;
229};
230
231template <typename... Ts>
232void ClauseProcessor::processTODO(mlir::Location currentLocation,
233 llvm::omp::Directive directive) const {
234 auto checkUnhandledClause = [&](llvm::omp::Clause id, const auto *x) {
235 if (!x)
236 return;
237 unsigned version = semaCtx.langOptions().OpenMPVersion;
238 bool isSimdDirective = llvm::omp::getOpenMPDirectiveName(directive, version)
239 .upper()
240 .find("SIMD") != llvm::StringRef::npos;
241 if (!semaCtx.langOptions().OpenMPSimd || isSimdDirective)
242 TODO(currentLocation,
243 "Unhandled clause " + llvm::omp::getOpenMPClauseName(id).upper() +
244 " in " +
245 llvm::omp::getOpenMPDirectiveName(directive, version).upper() +
246 " construct");
247 };
248
249 for (ClauseIterator it = clauses.begin(); it != clauses.end(); ++it)
250 (checkUnhandledClause(it->id, std::get_if<Ts>(&it->u)), ...);
251}
252
253template <typename T>
254const T *
255ClauseProcessor::findUniqueClause(const parser::CharBlock **source) const {
256 return ClauseFinder::findUniqueClause<T>(clauses, source);
257}
258
259template <typename T>
260bool ClauseProcessor::findRepeatableClause(
261 std::function<void(const T &, const parser::CharBlock &source)> callbackFn)
262 const {
263 return ClauseFinder::findRepeatableClause<T>(clauses, callbackFn);
264}
265
266template <typename T>
267bool ClauseProcessor::markClauseOccurrence(mlir::UnitAttr &result) const {
268 if (findUniqueClause<T>()) {
269 result = converter.getFirOpBuilder().getUnitAttr();
270 return true;
271 }
272 return false;
273}
274
275} // namespace omp
276} // namespace lower
277} // namespace Fortran
278
279#endif // FORTRAN_LOWER_CLAUSEPROCESSOR_H
Definition AbstractConverter.h:87
Definition StatementContext.h:46
Definition SymbolMap.h:181
static bool findRepeatableClause(const List< Clause > &clauses, std::function< void(const T &, const parser::CharBlock &source)> callbackFn)
Definition ClauseFinder.h:54
static const T * findUniqueClause(const List< Clause > &clauses, const parser::CharBlock **source=nullptr)
Definition ClauseFinder.h:40
Definition char-block.h:26
Definition semantics.h:67
Definition FIRBuilder.h:59
Definition ParserActions.h:24
Definition bit-population-count.h:20
Definition AbstractConverter.h:37
Definition PFTBuilder.h:221
Definition parse-tree.h:3645