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) const;
101 bool processMergeable(mlir::omp::MergeableClauseOps &result) const;
102 bool processNogroup(mlir::omp::NogroupClauseOps &result) const;
103 bool processNotinbranch(mlir::omp::NotinbranchClauseOps &result) const;
104 bool processNowait(mlir::omp::NowaitClauseOps &result) const;
105 bool processNumTasks(lower::StatementContext &stmtCtx,
106 mlir::omp::NumTasksClauseOps &result) const;
107 bool processNumTeams(lower::StatementContext &stmtCtx,
108 mlir::omp::NumTeamsClauseOps &result) const;
109 bool processNumThreads(lower::StatementContext &stmtCtx,
110 mlir::omp::NumThreadsClauseOps &result) const;
111 bool processOrder(mlir::omp::OrderClauseOps &result) const;
112 bool processOrdered(mlir::omp::OrderedClauseOps &result) const;
113 bool processPriority(lower::StatementContext &stmtCtx,
114 mlir::omp::PriorityClauseOps &result) const;
115 bool processProcBind(mlir::omp::ProcBindClauseOps &result) const;
116 bool processTileSizes(lower::pft::Evaluation &eval,
117 mlir::omp::LoopNestOperands &result) const;
118 bool processSafelen(mlir::omp::SafelenClauseOps &result) const;
119 bool processSchedule(lower::StatementContext &stmtCtx,
120 mlir::omp::ScheduleClauseOps &result) const;
121 bool processSimdlen(mlir::omp::SimdlenClauseOps &result) const;
122 bool processSimd(mlir::omp::OrderedRegionOperands &result) const;
123 bool processThreadLimit(lower::StatementContext &stmtCtx,
124 mlir::omp::ThreadLimitClauseOps &result) const;
125 bool processUntied(mlir::omp::UntiedClauseOps &result) const;
126 bool processDetach(mlir::omp::DetachClauseOps &result) const;
127 // 'Repeatable' clauses: They can appear multiple times in the clause list.
128 bool processAffinity(mlir::omp::AffinityClauseOps &result) const;
129 bool processAligned(mlir::omp::AlignedClauseOps &result) const;
130 bool processAllocate(mlir::omp::AllocateClauseOps &result) const;
131 bool processCopyin() const;
132 bool processCopyprivate(mlir::Location currentLocation,
133 mlir::omp::CopyprivateClauseOps &result) const;
134 bool processDefaultMap(lower::StatementContext &stmtCtx,
135 DefaultMapsTy &result) const;
136 bool processDepend(lower::SymMap &symMap, lower::StatementContext &stmtCtx,
137 mlir::omp::DependClauseOps &result) const;
138 bool
139 processDynGroupprivate(lower::StatementContext &stmtCtx,
140 mlir::omp::DynGroupprivateClauseOps &result) const;
141 bool
142 processEnter(llvm::SmallVectorImpl<DeclareTargetCaptureInfo> &result) const;
143 bool processIf(omp::clause::If::DirectiveNameModifier directiveName,
144 mlir::omp::IfClauseOps &result) const;
145 bool
146 processInReduction(mlir::Location currentLocation,
147 mlir::omp::InReductionClauseOps &result,
148 llvm::SmallVectorImpl<Object> &outReductionObjects) const;
149 bool processIsDevicePtr(lower::StatementContext &stmtCtx,
150 mlir::omp::IsDevicePtrClauseOps &result,
151 llvm::SmallVectorImpl<Object> &isDeviceObjects) const;
152 bool processLinear(mlir::omp::LinearClauseOps &result,
153 bool isDeclareSimd = false) const;
154 bool
155 processLink(llvm::SmallVectorImpl<DeclareTargetCaptureInfo> &result) const;
156
157 // This method is used to process a map clause.
158 // The optional parameter mapObjects is used to store the original Fortran
159 // objects for the map operands. It may be used later on to create the
160 // block_arguments for some of the directives that require it.
161 bool processMap(mlir::Location currentLocation,
163 mlir::omp::MapClauseOps &result,
164 llvm::omp::Directive directive = llvm::omp::OMPD_unknown,
165 llvm::SmallVectorImpl<Object> *mapObjects = nullptr) const;
166 bool processMotionClauses(lower::StatementContext &stmtCtx,
167 mlir::omp::MapClauseOps &result);
168 bool processNontemporal(mlir::omp::NontemporalClauseOps &result) const;
169 bool processReduction(mlir::Location currentLocation,
170 mlir::omp::ReductionClauseOps &result,
171 llvm::SmallVectorImpl<Object> &reductionObjects,
172 llvm::DenseMap<const semantics::Symbol *, mlir::Value>
173 *reductionVarCache = nullptr) const;
174 bool processTaskReduction(
175 mlir::Location currentLocation, mlir::omp::TaskReductionClauseOps &result,
176 llvm::SmallVectorImpl<Object> &outReductionObjects) const;
177 bool processTo(llvm::SmallVectorImpl<DeclareTargetCaptureInfo> &result) const;
178 bool
179 processUseDeviceAddr(lower::StatementContext &stmtCtx,
180 mlir::omp::UseDeviceAddrClauseOps &result,
181 llvm::SmallVectorImpl<Object> &useDeviceObjects) const;
182 bool
183 processUseDevicePtr(lower::StatementContext &stmtCtx,
184 mlir::omp::UseDevicePtrClauseOps &result,
185 llvm::SmallVectorImpl<Object> &useDeviceObjects) const;
186 bool processUniform(mlir::omp::UniformClauseOps &result) const;
187
188 // Call this method for these clauses that should be supported but are not
189 // implemented yet. It triggers a compilation error if any of the given
190 // clauses is found.
191 template <typename... Ts>
192 void processTODO(mlir::Location currentLocation,
193 llvm::omp::Directive directive) const;
194
195private:
196 using ClauseIterator = List<Clause>::const_iterator;
197
201 template <typename T>
202 const T *findUniqueClause(const parser::CharBlock **source = nullptr) const;
203
206 template <typename T>
207 bool findRepeatableClause(
208 std::function<void(const T &, const parser::CharBlock &source)>
209 callbackFn) const;
210
212 template <typename T>
213 bool markClauseOccurrence(mlir::UnitAttr &result) const;
214
215 void processMapObjects(
216 lower::StatementContext &stmtCtx, mlir::Location clauseLocation,
217 const omp::ObjectList &objects, mlir::omp::ClauseMapFlags mapTypeBits,
218 std::map<Object, OmpMapParentAndMemberData> &parentMemberIndices,
219 llvm::SmallVectorImpl<mlir::Value> &mapVars,
220 llvm::SmallVectorImpl<Object> &mapObjects,
221 llvm::StringRef mapperIdNameRef = "", bool isMotionModifier = false,
222 llvm::omp::Directive directive = llvm::omp::OMPD_unknown) const;
223
224 lower::AbstractConverter &converter;
226 List<Clause> clauses;
227};
228
229template <typename... Ts>
230void ClauseProcessor::processTODO(mlir::Location currentLocation,
231 llvm::omp::Directive directive) const {
232 auto checkUnhandledClause = [&](llvm::omp::Clause id, const auto *x) {
233 if (!x)
234 return;
235 unsigned version = semaCtx.langOptions().OpenMPVersion;
236 bool isSimdDirective = llvm::omp::getOpenMPDirectiveName(directive, version)
237 .upper()
238 .find("SIMD") != llvm::StringRef::npos;
239 if (!semaCtx.langOptions().OpenMPSimd || isSimdDirective)
240 TODO(currentLocation,
241 "Unhandled clause " + llvm::omp::getOpenMPClauseName(id).upper() +
242 " in " +
243 llvm::omp::getOpenMPDirectiveName(directive, version).upper() +
244 " construct");
245 };
246
247 for (ClauseIterator it = clauses.begin(); it != clauses.end(); ++it)
248 (checkUnhandledClause(it->id, std::get_if<Ts>(&it->u)), ...);
249}
250
251template <typename T>
252const T *
253ClauseProcessor::findUniqueClause(const parser::CharBlock **source) const {
254 return ClauseFinder::findUniqueClause<T>(clauses, source);
255}
256
257template <typename T>
258bool ClauseProcessor::findRepeatableClause(
259 std::function<void(const T &, const parser::CharBlock &source)> callbackFn)
260 const {
261 return ClauseFinder::findRepeatableClause<T>(clauses, callbackFn);
262}
263
264template <typename T>
265bool ClauseProcessor::markClauseOccurrence(mlir::UnitAttr &result) const {
266 if (findUniqueClause<T>()) {
267 result = converter.getFirOpBuilder().getUnitAttr();
268 return true;
269 }
270 return false;
271}
272
273} // namespace omp
274} // namespace lower
275} // namespace Fortran
276
277#endif // FORTRAN_LOWER_CLAUSEPROCESSOR_H
Definition AbstractConverter.h:87
Definition StatementContext.h:46
Definition SymbolMap.h:182
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:28
Definition semantics.h:68
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:3642