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 processBare(mlir::omp::BareClauseOps &result) const;
61 bool processBind(mlir::omp::BindClauseOps &result) const;
62 bool processCancelDirectiveName(
63 mlir::omp::CancelDirectiveNameClauseOps &result) const;
64 bool
65 processCollapse(mlir::Location currentLocation, lower::pft::Evaluation &eval,
66 mlir::omp::LoopRelatedClauseOps &loopResult,
67 mlir::omp::CollapseClauseOps &collapseResult,
68 llvm::SmallVectorImpl<const semantics::Symbol *> &iv) const;
69 bool processSizes(StatementContext &stmtCtx,
70 mlir::omp::SizesClauseOps &result) const;
71 bool processLooprange(StatementContext &stmtCtx,
72 mlir::omp::LooprangeClauseOps &result,
73 int64_t &count) const;
74 bool processDevice(lower::StatementContext &stmtCtx,
75 mlir::omp::DeviceClauseOps &result) const;
76 bool processDeviceType(mlir::omp::DeviceTypeClauseOps &result) const;
77 bool processDistSchedule(lower::StatementContext &stmtCtx,
78 mlir::omp::DistScheduleClauseOps &result) const;
79 bool processExclusive(mlir::Location currentLocation,
80 mlir::omp::ExclusiveClauseOps &result) const;
81 bool processFilter(lower::StatementContext &stmtCtx,
82 mlir::omp::FilterClauseOps &result) const;
83 bool processFinal(lower::StatementContext &stmtCtx,
84 mlir::omp::FinalClauseOps &result) const;
85 bool processGrainsize(lower::StatementContext &stmtCtx,
86 mlir::omp::GrainsizeClauseOps &result) const;
87 bool processHasDeviceAddr(
89 mlir::omp::HasDeviceAddrClauseOps &result,
90 llvm::SmallVectorImpl<const semantics::Symbol *> &hasDeviceSyms) const;
91 bool processHint(mlir::omp::HintClauseOps &result) const;
92 bool processInbranch(mlir::omp::InbranchClauseOps &result) const;
93 bool processInclusive(mlir::Location currentLocation,
94 mlir::omp::InclusiveClauseOps &result) const;
95 bool processInitializer(
96 lower::SymMap &symMap,
97 ReductionProcessor::GenInitValueCBTy &genInitValueCB) const;
98 bool processMergeable(mlir::omp::MergeableClauseOps &result) const;
99 bool processNogroup(mlir::omp::NogroupClauseOps &result) const;
100 bool processNotinbranch(mlir::omp::NotinbranchClauseOps &result) const;
101 bool processNowait(mlir::omp::NowaitClauseOps &result) const;
102 bool processNumTasks(lower::StatementContext &stmtCtx,
103 mlir::omp::NumTasksClauseOps &result) const;
104 bool processNumTeams(lower::StatementContext &stmtCtx,
105 mlir::omp::NumTeamsClauseOps &result) const;
106 bool processNumThreads(lower::StatementContext &stmtCtx,
107 mlir::omp::NumThreadsClauseOps &result) const;
108 bool processOrder(mlir::omp::OrderClauseOps &result) const;
109 bool processOrdered(mlir::omp::OrderedClauseOps &result) const;
110 bool processPriority(lower::StatementContext &stmtCtx,
111 mlir::omp::PriorityClauseOps &result) const;
112 bool processProcBind(mlir::omp::ProcBindClauseOps &result) const;
113 bool processTileSizes(lower::pft::Evaluation &eval,
114 mlir::omp::LoopNestOperands &result) const;
115 bool processSafelen(mlir::omp::SafelenClauseOps &result) const;
116 bool processSchedule(lower::StatementContext &stmtCtx,
117 mlir::omp::ScheduleClauseOps &result) const;
118 bool processSimdlen(mlir::omp::SimdlenClauseOps &result) const;
119 bool processThreadLimit(lower::StatementContext &stmtCtx,
120 mlir::omp::ThreadLimitClauseOps &result) const;
121 bool processUntied(mlir::omp::UntiedClauseOps &result) const;
122
123 bool processDetach(mlir::omp::DetachClauseOps &result) const;
124 // 'Repeatable' clauses: They can appear multiple times in the clause list.
125 bool processAffinity(mlir::omp::AffinityClauseOps &result) const;
126 bool processAligned(mlir::omp::AlignedClauseOps &result) const;
127 bool processAllocate(mlir::omp::AllocateClauseOps &result) const;
128 bool processCopyin() const;
129 bool processCopyprivate(mlir::Location currentLocation,
130 mlir::omp::CopyprivateClauseOps &result) const;
131 bool processDefaultMap(lower::StatementContext &stmtCtx,
132 DefaultMapsTy &result) const;
133 bool processDepend(lower::SymMap &symMap, lower::StatementContext &stmtCtx,
134 mlir::omp::DependClauseOps &result) const;
135 bool
136 processEnter(llvm::SmallVectorImpl<DeclareTargetCaptureInfo> &result) const;
137 bool processIf(omp::clause::If::DirectiveNameModifier directiveName,
138 mlir::omp::IfClauseOps &result) const;
139 bool processInReduction(
140 mlir::Location currentLocation, mlir::omp::InReductionClauseOps &result,
141 llvm::SmallVectorImpl<const semantics::Symbol *> &outReductionSyms) const;
142 bool processIsDevicePtr(
143 lower::StatementContext &stmtCtx, mlir::omp::IsDevicePtrClauseOps &result,
144 llvm::SmallVectorImpl<const semantics::Symbol *> &isDeviceSyms) const;
145 bool processLinear(mlir::omp::LinearClauseOps &result) const;
146 bool
147 processLink(llvm::SmallVectorImpl<DeclareTargetCaptureInfo> &result) const;
148
149 // This method is used to process a map clause.
150 // The optional parameter mapSyms is used to store the original Fortran symbol
151 // for the map operands. It may be used later on to create the block_arguments
152 // for some of the directives that require it.
153 bool processMap(mlir::Location currentLocation,
155 mlir::omp::MapClauseOps &result,
156 llvm::omp::Directive directive = llvm::omp::OMPD_unknown,
157 llvm::SmallVectorImpl<const semantics::Symbol *> *mapSyms =
158 nullptr) const;
159 bool processMotionClauses(lower::StatementContext &stmtCtx,
160 mlir::omp::MapClauseOps &result);
161 bool processNontemporal(mlir::omp::NontemporalClauseOps &result) const;
162 bool processReduction(
163 mlir::Location currentLocation, mlir::omp::ReductionClauseOps &result,
164 llvm::SmallVectorImpl<const semantics::Symbol *> &reductionSyms) const;
165 bool processTaskReduction(
166 mlir::Location currentLocation, mlir::omp::TaskReductionClauseOps &result,
167 llvm::SmallVectorImpl<const semantics::Symbol *> &outReductionSyms) const;
168 bool processTo(llvm::SmallVectorImpl<DeclareTargetCaptureInfo> &result) const;
169 bool processUseDeviceAddr(
171 mlir::omp::UseDeviceAddrClauseOps &result,
172 llvm::SmallVectorImpl<const semantics::Symbol *> &useDeviceSyms) const;
173 bool processUseDevicePtr(
175 mlir::omp::UseDevicePtrClauseOps &result,
176 llvm::SmallVectorImpl<const semantics::Symbol *> &useDeviceSyms) const;
177 bool processUniform(mlir::omp::UniformClauseOps &result) const;
178
179 // Call this method for these clauses that should be supported but are not
180 // implemented yet. It triggers a compilation error if any of the given
181 // clauses is found.
182 template <typename... Ts>
183 void processTODO(mlir::Location currentLocation,
184 llvm::omp::Directive directive) const;
185
186private:
187 using ClauseIterator = List<Clause>::const_iterator;
188
192 template <typename T>
193 const T *findUniqueClause(const parser::CharBlock **source = nullptr) const;
194
197 template <typename T>
198 bool findRepeatableClause(
199 std::function<void(const T &, const parser::CharBlock &source)>
200 callbackFn) const;
201
203 template <typename T>
204 bool markClauseOccurrence(mlir::UnitAttr &result) const;
205
206 void processMapObjects(
207 lower::StatementContext &stmtCtx, mlir::Location clauseLocation,
208 const omp::ObjectList &objects, mlir::omp::ClauseMapFlags mapTypeBits,
209 std::map<Object, OmpMapParentAndMemberData> &parentMemberIndices,
210 llvm::SmallVectorImpl<mlir::Value> &mapVars,
211 llvm::SmallVectorImpl<const semantics::Symbol *> &mapSyms,
212 llvm::StringRef mapperIdNameRef = "") const;
213
214 lower::AbstractConverter &converter;
216 List<Clause> clauses;
217};
218
219template <typename... Ts>
220void ClauseProcessor::processTODO(mlir::Location currentLocation,
221 llvm::omp::Directive directive) const {
222 auto checkUnhandledClause = [&](llvm::omp::Clause id, const auto *x) {
223 if (!x)
224 return;
225 unsigned version = semaCtx.langOptions().OpenMPVersion;
226 bool isSimdDirective = llvm::omp::getOpenMPDirectiveName(directive, version)
227 .upper()
228 .find("SIMD") != llvm::StringRef::npos;
229 if (!semaCtx.langOptions().OpenMPSimd || isSimdDirective)
230 TODO(currentLocation,
231 "Unhandled clause " + llvm::omp::getOpenMPClauseName(id).upper() +
232 " in " +
233 llvm::omp::getOpenMPDirectiveName(directive, version).upper() +
234 " construct");
235 };
236
237 for (ClauseIterator it = clauses.begin(); it != clauses.end(); ++it)
238 (checkUnhandledClause(it->id, std::get_if<Ts>(&it->u)), ...);
239}
240
241template <typename T>
242const T *
243ClauseProcessor::findUniqueClause(const parser::CharBlock **source) const {
244 return ClauseFinder::findUniqueClause<T>(clauses, source);
245}
246
247template <typename T>
248bool ClauseProcessor::findRepeatableClause(
249 std::function<void(const T &, const parser::CharBlock &source)> callbackFn)
250 const {
251 return ClauseFinder::findRepeatableClause<T>(clauses, callbackFn);
252}
253
254template <typename T>
255bool ClauseProcessor::markClauseOccurrence(mlir::UnitAttr &result) const {
256 if (findUniqueClause<T>()) {
257 result = converter.getFirOpBuilder().getUnitAttr();
258 return true;
259 }
260 return false;
261}
262
263} // namespace omp
264} // namespace lower
265} // namespace Fortran
266
267#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:55
Definition ParserActions.h:24
Definition bit-population-count.h:20
Definition AbstractConverter.h:37
Definition PFTBuilder.h:221