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