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