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) 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
127 bool processDetach(mlir::omp::DetachClauseOps &result) const;
128 // 'Repeatable' clauses: They can appear multiple times in the clause list.
129 bool processAffinity(mlir::omp::AffinityClauseOps &result) const;
130 bool processAligned(mlir::omp::AlignedClauseOps &result) const;
131 bool processAllocate(mlir::omp::AllocateClauseOps &result) const;
132 bool processCopyin() const;
133 bool processCopyprivate(mlir::Location currentLocation,
134 mlir::omp::CopyprivateClauseOps &result) const;
135 bool processDefaultMap(lower::StatementContext &stmtCtx,
136 DefaultMapsTy &result) const;
137 bool processDepend(lower::SymMap &symMap, lower::StatementContext &stmtCtx,
138 mlir::omp::DependClauseOps &result) const;
139 bool
140 processEnter(llvm::SmallVectorImpl<DeclareTargetCaptureInfo> &result) const;
141 bool processIf(omp::clause::If::DirectiveNameModifier directiveName,
142 mlir::omp::IfClauseOps &result) const;
143 bool processInReduction(
144 mlir::Location currentLocation, mlir::omp::InReductionClauseOps &result,
145 llvm::SmallVectorImpl<const semantics::Symbol *> &outReductionSyms) const;
146 bool processIsDevicePtr(
147 lower::StatementContext &stmtCtx, mlir::omp::IsDevicePtrClauseOps &result,
148 llvm::SmallVectorImpl<const semantics::Symbol *> &isDeviceSyms) const;
149 bool processLinear(mlir::omp::LinearClauseOps &result,
150 bool isDeclareSimd = false) const;
151 bool
152 processLink(llvm::SmallVectorImpl<DeclareTargetCaptureInfo> &result) const;
153
154 // This method is used to process a map clause.
155 // The optional parameter mapSyms is used to store the original Fortran symbol
156 // for the map operands. It may be used later on to create the block_arguments
157 // for some of the directives that require it.
158 bool processMap(mlir::Location currentLocation,
160 mlir::omp::MapClauseOps &result,
161 llvm::omp::Directive directive = llvm::omp::OMPD_unknown,
162 llvm::SmallVectorImpl<const semantics::Symbol *> *mapSyms =
163 nullptr) const;
164 bool processMotionClauses(lower::StatementContext &stmtCtx,
165 mlir::omp::MapClauseOps &result);
166 bool processNontemporal(mlir::omp::NontemporalClauseOps &result) const;
167 bool processReduction(
168 mlir::Location currentLocation, mlir::omp::ReductionClauseOps &result,
169 llvm::SmallVectorImpl<const semantics::Symbol *> &reductionSyms) const;
170 bool processTaskReduction(
171 mlir::Location currentLocation, mlir::omp::TaskReductionClauseOps &result,
172 llvm::SmallVectorImpl<const semantics::Symbol *> &outReductionSyms) const;
173 bool processTo(llvm::SmallVectorImpl<DeclareTargetCaptureInfo> &result) const;
174 bool processUseDeviceAddr(
176 mlir::omp::UseDeviceAddrClauseOps &result,
177 llvm::SmallVectorImpl<const semantics::Symbol *> &useDeviceSyms) const;
178 bool processUseDevicePtr(
180 mlir::omp::UseDevicePtrClauseOps &result,
181 llvm::SmallVectorImpl<const semantics::Symbol *> &useDeviceSyms) const;
182 bool processUniform(mlir::omp::UniformClauseOps &result) const;
183
184 // Call this method for these clauses that should be supported but are not
185 // implemented yet. It triggers a compilation error if any of the given
186 // clauses is found.
187 template <typename... Ts>
188 void processTODO(mlir::Location currentLocation,
189 llvm::omp::Directive directive) const;
190
191private:
192 using ClauseIterator = List<Clause>::const_iterator;
193
197 template <typename T>
198 const T *findUniqueClause(const parser::CharBlock **source = nullptr) const;
199
202 template <typename T>
203 bool findRepeatableClause(
204 std::function<void(const T &, const parser::CharBlock &source)>
205 callbackFn) const;
206
208 template <typename T>
209 bool markClauseOccurrence(mlir::UnitAttr &result) const;
210
211 void processMapObjects(
212 lower::StatementContext &stmtCtx, mlir::Location clauseLocation,
213 const omp::ObjectList &objects, mlir::omp::ClauseMapFlags mapTypeBits,
214 std::map<Object, OmpMapParentAndMemberData> &parentMemberIndices,
215 llvm::SmallVectorImpl<mlir::Value> &mapVars,
216 llvm::SmallVectorImpl<const semantics::Symbol *> &mapSyms,
217 llvm::StringRef mapperIdNameRef = "", bool isMotionModifier = false,
218 llvm::omp::Directive directive = llvm::omp::OMPD_unknown) const;
219
220 lower::AbstractConverter &converter;
222 List<Clause> clauses;
223};
224
225template <typename... Ts>
226void ClauseProcessor::processTODO(mlir::Location currentLocation,
227 llvm::omp::Directive directive) const {
228 auto checkUnhandledClause = [&](llvm::omp::Clause id, const auto *x) {
229 if (!x)
230 return;
231 unsigned version = semaCtx.langOptions().OpenMPVersion;
232 bool isSimdDirective = llvm::omp::getOpenMPDirectiveName(directive, version)
233 .upper()
234 .find("SIMD") != llvm::StringRef::npos;
235 if (!semaCtx.langOptions().OpenMPSimd || isSimdDirective)
236 TODO(currentLocation,
237 "Unhandled clause " + llvm::omp::getOpenMPClauseName(id).upper() +
238 " in " +
239 llvm::omp::getOpenMPDirectiveName(directive, version).upper() +
240 " construct");
241 };
242
243 for (ClauseIterator it = clauses.begin(); it != clauses.end(); ++it)
244 (checkUnhandledClause(it->id, std::get_if<Ts>(&it->u)), ...);
245}
246
247template <typename T>
248const T *
249ClauseProcessor::findUniqueClause(const parser::CharBlock **source) const {
250 return ClauseFinder::findUniqueClause<T>(clauses, source);
251}
252
253template <typename T>
254bool ClauseProcessor::findRepeatableClause(
255 std::function<void(const T &, const parser::CharBlock &source)> callbackFn)
256 const {
257 return ClauseFinder::findRepeatableClause<T>(clauses, callbackFn);
258}
259
260template <typename T>
261bool ClauseProcessor::markClauseOccurrence(mlir::UnitAttr &result) const {
262 if (findUniqueClause<T>()) {
263 result = converter.getFirOpBuilder().getUnitAttr();
264 return true;
265 }
266 return false;
267}
268
269} // namespace omp
270} // namespace lower
271} // namespace Fortran
272
273#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