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