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