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 processBare(mlir::omp::BareClauseOps &result) const;
64 bool processBind(mlir::omp::BindClauseOps &result) const;
65 bool processCancelDirectiveName(
66 mlir::omp::CancelDirectiveNameClauseOps &result) const;
67 bool
68 processCollapse(mlir::Location currentLocation, lower::pft::Evaluation &eval,
69 mlir::omp::LoopRelatedClauseOps &loopResult,
70 mlir::omp::CollapseClauseOps &collapseResult,
71 llvm::SmallVectorImpl<const semantics::Symbol *> &iv) const;
72 bool processSizes(StatementContext &stmtCtx,
73 mlir::omp::SizesClauseOps &result) const;
74 bool processLooprange(StatementContext &stmtCtx,
75 mlir::omp::LooprangeClauseOps &result,
76 int64_t &count) const;
77 bool processDevice(lower::StatementContext &stmtCtx,
78 mlir::omp::DeviceClauseOps &result) const;
79 bool processDeviceType(mlir::omp::DeviceTypeClauseOps &result) const;
80 bool processDistSchedule(lower::StatementContext &stmtCtx,
81 mlir::omp::DistScheduleClauseOps &result) const;
82 bool processExclusive(mlir::Location currentLocation,
83 mlir::omp::ExclusiveClauseOps &result) const;
84 bool processFilter(lower::StatementContext &stmtCtx,
85 mlir::omp::FilterClauseOps &result) const;
86 bool processFinal(lower::StatementContext &stmtCtx,
87 mlir::omp::FinalClauseOps &result) const;
88 bool processGrainsize(lower::StatementContext &stmtCtx,
89 mlir::omp::GrainsizeClauseOps &result) const;
90 bool processHasDeviceAddr(
92 mlir::omp::HasDeviceAddrClauseOps &result,
93 llvm::SmallVectorImpl<const semantics::Symbol *> &hasDeviceSyms) const;
94 bool processHint(mlir::omp::HintClauseOps &result) const;
95 bool processInbranch(mlir::omp::InbranchClauseOps &result) const;
96 bool processInclusive(mlir::Location currentLocation,
97 mlir::omp::InclusiveClauseOps &result) const;
98 bool processInitializer(
99 lower::SymMap &symMap,
100 ReductionProcessor::GenInitValueCBTy &genInitValueCB,
101 const parser::OmpStylizedInstance *parserInitInstance = nullptr) 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 processPriority(lower::StatementContext &stmtCtx,
115 mlir::omp::PriorityClauseOps &result) const;
116 bool processProcBind(mlir::omp::ProcBindClauseOps &result) const;
117 bool processTileSizes(lower::pft::Evaluation &eval,
118 mlir::omp::LoopNestOperands &result) const;
119 bool processSafelen(mlir::omp::SafelenClauseOps &result) const;
120 bool processSchedule(lower::StatementContext &stmtCtx,
121 mlir::omp::ScheduleClauseOps &result) const;
122 bool processSimdlen(mlir::omp::SimdlenClauseOps &result) const;
123 bool processSimd(mlir::omp::OrderedRegionOperands &result) const;
124 bool processThreadLimit(lower::StatementContext &stmtCtx,
125 mlir::omp::ThreadLimitClauseOps &result) const;
126 bool processUntied(mlir::omp::UntiedClauseOps &result) const;
127
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 processEnter(llvm::SmallVectorImpl<DeclareTargetCaptureInfo> &result) const;
142 bool processIf(omp::clause::If::DirectiveNameModifier directiveName,
143 mlir::omp::IfClauseOps &result) const;
144 bool processInReduction(
145 mlir::Location currentLocation, mlir::omp::InReductionClauseOps &result,
146 llvm::SmallVectorImpl<const semantics::Symbol *> &outReductionSyms) const;
147 bool processIsDevicePtr(
148 lower::StatementContext &stmtCtx, mlir::omp::IsDevicePtrClauseOps &result,
149 llvm::SmallVectorImpl<const semantics::Symbol *> &isDeviceSyms) const;
150 bool processLinear(mlir::omp::LinearClauseOps &result,
151 bool isDeclareSimd = false) const;
152 bool
153 processLink(llvm::SmallVectorImpl<DeclareTargetCaptureInfo> &result) const;
154
155 // This method is used to process a map clause.
156 // The optional parameter mapSyms is used to store the original Fortran symbol
157 // for the map operands. It may be used later on to create the block_arguments
158 // for some of the directives that require it.
159 bool processMap(mlir::Location currentLocation,
161 mlir::omp::MapClauseOps &result,
162 llvm::omp::Directive directive = llvm::omp::OMPD_unknown,
163 llvm::SmallVectorImpl<const semantics::Symbol *> *mapSyms =
164 nullptr) const;
165 bool processMotionClauses(lower::StatementContext &stmtCtx,
166 mlir::omp::MapClauseOps &result);
167 bool processNontemporal(mlir::omp::NontemporalClauseOps &result) const;
168 bool processReduction(
169 mlir::Location currentLocation, mlir::omp::ReductionClauseOps &result,
170 llvm::SmallVectorImpl<const semantics::Symbol *> &reductionSyms,
171 llvm::DenseMap<const semantics::Symbol *, mlir::Value>
172 *reductionVarCache = nullptr) const;
173 bool processTaskReduction(
174 mlir::Location currentLocation, mlir::omp::TaskReductionClauseOps &result,
175 llvm::SmallVectorImpl<const semantics::Symbol *> &outReductionSyms) const;
176 bool processTo(llvm::SmallVectorImpl<DeclareTargetCaptureInfo> &result) const;
177 bool processUseDeviceAddr(
179 mlir::omp::UseDeviceAddrClauseOps &result,
180 llvm::SmallVectorImpl<const semantics::Symbol *> &useDeviceSyms) const;
181 bool processUseDevicePtr(
183 mlir::omp::UseDevicePtrClauseOps &result,
184 llvm::SmallVectorImpl<const semantics::Symbol *> &useDeviceSyms) const;
185 bool processUniform(mlir::omp::UniformClauseOps &result) const;
186
187 // Call this method for these clauses that should be supported but are not
188 // implemented yet. It triggers a compilation error if any of the given
189 // clauses is found.
190 template <typename... Ts>
191 void processTODO(mlir::Location currentLocation,
192 llvm::omp::Directive directive) const;
193
194private:
195 using ClauseIterator = List<Clause>::const_iterator;
196
200 template <typename T>
201 const T *findUniqueClause(const parser::CharBlock **source = nullptr) const;
202
205 template <typename T>
206 bool findRepeatableClause(
207 std::function<void(const T &, const parser::CharBlock &source)>
208 callbackFn) const;
209
211 template <typename T>
212 bool markClauseOccurrence(mlir::UnitAttr &result) const;
213
214 void processMapObjects(
215 lower::StatementContext &stmtCtx, mlir::Location clauseLocation,
216 const omp::ObjectList &objects, mlir::omp::ClauseMapFlags mapTypeBits,
217 std::map<Object, OmpMapParentAndMemberData> &parentMemberIndices,
218 llvm::SmallVectorImpl<mlir::Value> &mapVars,
219 llvm::SmallVectorImpl<const semantics::Symbol *> &mapSyms,
220 llvm::StringRef mapperIdNameRef = "", bool isMotionModifier = false,
221 llvm::omp::Directive directive = llvm::omp::OMPD_unknown) const;
222
223 lower::AbstractConverter &converter;
225 List<Clause> clauses;
226};
227
228template <typename... Ts>
229void ClauseProcessor::processTODO(mlir::Location currentLocation,
230 llvm::omp::Directive directive) const {
231 auto checkUnhandledClause = [&](llvm::omp::Clause id, const auto *x) {
232 if (!x)
233 return;
234 unsigned version = semaCtx.langOptions().OpenMPVersion;
235 bool isSimdDirective = llvm::omp::getOpenMPDirectiveName(directive, version)
236 .upper()
237 .find("SIMD") != llvm::StringRef::npos;
238 if (!semaCtx.langOptions().OpenMPSimd || isSimdDirective)
239 TODO(currentLocation,
240 "Unhandled clause " + llvm::omp::getOpenMPClauseName(id).upper() +
241 " in " +
242 llvm::omp::getOpenMPDirectiveName(directive, version).upper() +
243 " construct");
244 };
245
246 for (ClauseIterator it = clauses.begin(); it != clauses.end(); ++it)
247 (checkUnhandledClause(it->id, std::get_if<Ts>(&it->u)), ...);
248}
249
250template <typename T>
251const T *
252ClauseProcessor::findUniqueClause(const parser::CharBlock **source) const {
253 return ClauseFinder::findUniqueClause<T>(clauses, source);
254}
255
256template <typename T>
257bool ClauseProcessor::findRepeatableClause(
258 std::function<void(const T &, const parser::CharBlock &source)> callbackFn)
259 const {
260 return ClauseFinder::findRepeatableClause<T>(clauses, callbackFn);
261}
262
263template <typename T>
264bool ClauseProcessor::markClauseOccurrence(mlir::UnitAttr &result) const {
265 if (findUniqueClause<T>()) {
266 result = converter.getFirOpBuilder().getUnitAttr();
267 return true;
268 }
269 return false;
270}
271
272} // namespace omp
273} // namespace lower
274} // namespace Fortran
275
276#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:67
Definition FIRBuilder.h:56
Definition ParserActions.h:24
Definition bit-population-count.h:20
Definition AbstractConverter.h:37
Definition PFTBuilder.h:221
Definition parse-tree.h:3587