FLANG
check-omp-structure.h
1//===-- lib/Semantics/check-omp-structure.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// OpenMP structure validity check list
10// 1. invalid clauses on directive
11// 2. invalid repeated clauses on directive
12// 3. TODO: invalid nesting of regions
13
14#ifndef FORTRAN_SEMANTICS_CHECK_OMP_STRUCTURE_H_
15#define FORTRAN_SEMANTICS_CHECK_OMP_STRUCTURE_H_
16
17#include "check-directive-structure.h"
18#include "flang/Common/enum-set.h"
19#include "flang/Parser/parse-tree.h"
20#include "flang/Semantics/openmp-directive-sets.h"
21#include "flang/Semantics/semantics.h"
22
23using OmpClauseSet =
25
26#define GEN_FLANG_DIRECTIVE_CLAUSE_SETS
27#include "llvm/Frontend/OpenMP/OMP.inc"
28
29namespace llvm {
30namespace omp {
31static OmpClauseSet privateSet{
32 Clause::OMPC_private, Clause::OMPC_firstprivate, Clause::OMPC_lastprivate};
33static OmpClauseSet privateReductionSet{
34 OmpClauseSet{Clause::OMPC_reduction} | privateSet};
35// omp.td cannot differentiate allowed/not allowed clause list for few
36// directives for fortran. nowait is not allowed on begin directive clause list
37// for below list of directives. Directives with conflicting list of clauses are
38// included in below list.
39static const OmpDirectiveSet noWaitClauseNotAllowedSet{
40 Directive::OMPD_do,
41 Directive::OMPD_do_simd,
42 Directive::OMPD_sections,
43 Directive::OMPD_single,
44 Directive::OMPD_workshare,
45};
46} // namespace omp
47} // namespace llvm
48
49namespace Fortran::semantics {
50struct AnalyzedCondStmt;
51
52// Mapping from 'Symbol' to 'Source' to keep track of the variables
53// used in multiple clauses
54using SymbolSourceMap = std::multimap<const Symbol *, parser::CharBlock>;
55// Multimap to check the triple <current_dir, enclosing_dir, enclosing_clause>
56using DirectivesClauseTriple = std::multimap<llvm::omp::Directive,
57 std::pair<llvm::omp::Directive, const OmpClauseSet>>;
58
59using OmpStructureCheckerBase = DirectiveStructureChecker<llvm::omp::Directive,
60 llvm::omp::Clause, parser::OmpClause, llvm::omp::Clause_enumSize>;
61
62class OmpStructureChecker : public OmpStructureCheckerBase {
63public:
64 using Base = OmpStructureCheckerBase;
65
66 OmpStructureChecker(SemanticsContext &context);
67
68 using llvmOmpClause = const llvm::omp::Clause;
69
70 bool Enter(const parser::MainProgram &);
71 void Leave(const parser::MainProgram &);
72 bool Enter(const parser::BlockData &);
73 void Leave(const parser::BlockData &);
74 bool Enter(const parser::Module &);
75 void Leave(const parser::Module &);
76 bool Enter(const parser::Submodule &);
77 void Leave(const parser::Submodule &);
78 bool Enter(const parser::SubroutineStmt &);
79 bool Enter(const parser::EndSubroutineStmt &);
80 bool Enter(const parser::FunctionStmt &);
81 bool Enter(const parser::EndFunctionStmt &);
82 bool Enter(const parser::BlockConstruct &);
83 void Leave(const parser::BlockConstruct &);
84
85 void Enter(const parser::SpecificationPart &);
86 void Leave(const parser::SpecificationPart &);
87 void Enter(const parser::ExecutionPart &);
88 void Leave(const parser::ExecutionPart &);
89
90 void Enter(const parser::OpenMPConstruct &);
91 void Leave(const parser::OpenMPConstruct &);
92 void Enter(const parser::OpenMPInteropConstruct &);
93 void Leave(const parser::OpenMPInteropConstruct &);
94 void Enter(const parser::OpenMPDeclarativeConstruct &);
95 void Leave(const parser::OpenMPDeclarativeConstruct &);
96
97 void Enter(const parser::OpenMPMisplacedEndDirective &);
98 void Leave(const parser::OpenMPMisplacedEndDirective &);
99 void Enter(const parser::OpenMPInvalidDirective &);
100 void Leave(const parser::OpenMPInvalidDirective &);
101
102 void Enter(const parser::OpenMPLoopConstruct &);
103 void Leave(const parser::OpenMPLoopConstruct &);
104 void Enter(const parser::OmpEndLoopDirective &);
105 void Leave(const parser::OmpEndLoopDirective &);
106
107 void Enter(const parser::OpenMPAssumeConstruct &);
108 void Leave(const parser::OpenMPAssumeConstruct &);
109 void Enter(const parser::OpenMPDeclarativeAssumes &);
110 void Leave(const parser::OpenMPDeclarativeAssumes &);
111 void Enter(const parser::OmpBlockConstruct &);
112 void Leave(const parser::OmpBlockConstruct &);
113 void Leave(const parser::OmpBeginDirective &);
114 void Enter(const parser::OmpEndDirective &);
115 void Leave(const parser::OmpEndDirective &);
116
117 void Enter(const parser::OpenMPSectionsConstruct &);
118 void Leave(const parser::OpenMPSectionsConstruct &);
119 void Enter(const parser::OmpEndSectionsDirective &);
120 void Leave(const parser::OmpEndSectionsDirective &);
121
122 void Enter(const parser::OmpDeclareVariantDirective &);
123 void Leave(const parser::OmpDeclareVariantDirective &);
124 void Enter(const parser::OpenMPDeclareSimdConstruct &);
125 void Leave(const parser::OpenMPDeclareSimdConstruct &);
126 void Enter(const parser::OmpAllocateDirective &);
127 void Leave(const parser::OmpAllocateDirective &);
128 void Enter(const parser::OpenMPDeclareMapperConstruct &);
129 void Leave(const parser::OpenMPDeclareMapperConstruct &);
130 void Enter(const parser::OpenMPDeclareReductionConstruct &);
131 void Leave(const parser::OpenMPDeclareReductionConstruct &);
132 void Enter(const parser::OpenMPDeclareTargetConstruct &);
133 void Leave(const parser::OpenMPDeclareTargetConstruct &);
134 void Enter(const parser::OpenMPDepobjConstruct &);
135 void Leave(const parser::OpenMPDepobjConstruct &);
136 void Enter(const parser::OpenMPDispatchConstruct &);
137 void Leave(const parser::OpenMPDispatchConstruct &);
138 void Enter(const parser::OmpErrorDirective &);
139 void Leave(const parser::OmpErrorDirective &);
140 void Enter(const parser::OmpNothingDirective &);
141 void Leave(const parser::OmpNothingDirective &);
142 void Enter(const parser::OpenMPAllocatorsConstruct &);
143 void Leave(const parser::OpenMPAllocatorsConstruct &);
144 void Enter(const parser::OpenMPRequiresConstruct &);
145 void Leave(const parser::OpenMPRequiresConstruct &);
146 void Enter(const parser::OpenMPGroupprivate &);
147 void Leave(const parser::OpenMPGroupprivate &);
148 void Enter(const parser::OpenMPThreadprivate &);
149 void Leave(const parser::OpenMPThreadprivate &);
150
151 void Enter(const parser::OpenMPSimpleStandaloneConstruct &);
152 void Leave(const parser::OpenMPSimpleStandaloneConstruct &);
153 void Enter(const parser::OpenMPFlushConstruct &);
154 void Leave(const parser::OpenMPFlushConstruct &);
155 void Enter(const parser::OpenMPCancelConstruct &);
156 void Leave(const parser::OpenMPCancelConstruct &);
159 void Enter(const parser::OpenMPCriticalConstruct &);
160 void Leave(const parser::OpenMPCriticalConstruct &);
161 void Enter(const parser::OpenMPAtomicConstruct &);
162 void Leave(const parser::OpenMPAtomicConstruct &);
163
164 void Leave(const parser::OmpClauseList &);
165 void Enter(const parser::OmpClause &);
166
167 void Enter(const parser::DoConstruct &);
168 void Leave(const parser::DoConstruct &);
169
170 void Enter(const parser::OmpDirectiveSpecification &);
171 void Leave(const parser::OmpDirectiveSpecification &);
172
173 void Enter(const parser::OmpMetadirectiveDirective &);
174 void Leave(const parser::OmpMetadirectiveDirective &);
175
176 void Enter(const parser::OmpContextSelector &);
177 void Leave(const parser::OmpContextSelector &);
178
179#define GEN_FLANG_CLAUSE_CHECK_ENTER
180#include "llvm/Frontend/OpenMP/OMP.inc"
181
182private:
183 bool CheckAllowedClause(llvmOmpClause clause);
184 void CheckVariableListItem(const SymbolSourceMap &symbols);
185 void CheckDirectiveSpelling(
186 parser::CharBlock spelling, llvm::omp::Directive id);
187 void AnalyzeObject(const parser::OmpObject &object);
188 void AnalyzeObjects(const parser::OmpObjectList &objects);
189 void CheckMultipleOccurrence(semantics::UnorderedSymbolSet &listVars,
190 const std::list<parser::Name> &nameList, const parser::CharBlock &item,
191 const std::string &clauseName);
192 void CheckMultListItems();
193 void CheckStructureComponent(
194 const parser::OmpObjectList &objects, llvm::omp::Clause clauseId);
195 bool HasInvalidWorksharingNesting(
196 const parser::CharBlock &, const OmpDirectiveSet &);
197 bool IsCloselyNestedRegion(const OmpDirectiveSet &set);
198 bool IsNestedInDirective(llvm::omp::Directive directive);
199 bool InTargetRegion();
200 void HasInvalidTeamsNesting(
201 const llvm::omp::Directive &dir, const parser::CharBlock &source);
202 void HasInvalidDistributeNesting(const parser::OpenMPLoopConstruct &x);
203 void HasInvalidLoopBinding(const parser::OpenMPLoopConstruct &x);
204 bool HasRequires(llvm::omp::Clause req);
205 // specific clause related
206 void CheckAllowedMapTypes(
207 parser::OmpMapType::Value, llvm::ArrayRef<parser::OmpMapType::Value>);
208
209 const std::list<parser::OmpTraitProperty> &GetTraitPropertyList(
211 std::optional<llvm::omp::Clause> GetClauseFromProperty(
213
214 void CheckTraitSelectorList(const std::list<parser::OmpTraitSelector> &);
215 void CheckTraitSetSelector(const parser::OmpTraitSetSelector &);
216 void CheckTraitScore(const parser::OmpTraitScore &);
217 bool VerifyTraitPropertyLists(
219 void CheckTraitSelector(
221 void CheckTraitADMO(
223 void CheckTraitCondition(
225 void CheckTraitDeviceNum(
227 void CheckTraitRequires(
229 void CheckTraitSimd(
231
232 llvm::StringRef getClauseName(llvm::omp::Clause clause) override;
233 llvm::StringRef getDirectiveName(llvm::omp::Directive directive) override;
234
235 template < //
236 typename LessTy, typename RangeTy,
237 typename IterTy = decltype(std::declval<RangeTy>().begin())>
238 std::optional<IterTy> FindDuplicate(RangeTy &&);
239
240 void CheckDependList(const parser::DataRef &);
241 void CheckDependArraySection(
243 void CheckDoacross(const parser::OmpDoacross &doa);
244 bool IsDataRefTypeParamInquiry(const parser::DataRef *dataRef);
245 void CheckVarIsNotPartOfAnotherVar(const parser::CharBlock &source,
246 const parser::OmpObject &obj, llvm::StringRef clause = "");
247 void CheckVarIsNotPartOfAnotherVar(const parser::CharBlock &source,
248 const parser::OmpObjectList &objList, llvm::StringRef clause = "");
249 void CheckThreadprivateOrDeclareTargetVar(const parser::Designator &);
250 void CheckThreadprivateOrDeclareTargetVar(const parser::Name &);
251 void CheckThreadprivateOrDeclareTargetVar(const parser::OmpObject &);
252 void CheckThreadprivateOrDeclareTargetVar(const parser::OmpObjectList &);
253 void CheckSymbolName(
254 const parser::CharBlock &source, const parser::OmpObject &object);
255 void CheckSymbolNames(
256 const parser::CharBlock &source, const parser::OmpObjectList &objList);
257 void CheckIntentInPointer(SymbolSourceMap &, const llvm::omp::Clause);
258 void CheckProcedurePointer(SymbolSourceMap &, const llvm::omp::Clause);
259 void CheckCrayPointee(const parser::OmpObjectList &objectList,
260 llvm::StringRef clause, bool suggestToUseCrayPointer = true);
261 void GetSymbolsInObjectList(const parser::OmpObjectList &, SymbolSourceMap &);
262 void CheckDefinableObjects(SymbolSourceMap &, const llvm::omp::Clause);
263 void CheckCopyingPolymorphicAllocatable(
264 SymbolSourceMap &, const llvm::omp::Clause);
265 void CheckPrivateSymbolsInOuterCxt(
266 SymbolSourceMap &, DirectivesClauseTriple &, const llvm::omp::Clause);
267 const parser::Name GetLoopIndex(const parser::DoConstruct *x);
268 void SetLoopInfo(const parser::OpenMPLoopConstruct &x);
269 void CheckIsLoopIvPartOfClause(
270 llvmOmpClause clause, const parser::OmpObjectList &ompObjectList);
271 bool CheckTargetBlockOnlyTeams(const parser::Block &);
272 void CheckWorkshareBlockStmts(const parser::Block &, parser::CharBlock);
273 void CheckWorkdistributeBlockStmts(const parser::Block &, parser::CharBlock);
274 void CheckIndividualAllocateDirective(
275 const parser::OmpAllocateDirective &x, bool isExecutable);
276 void CheckExecutableAllocateDirective(const parser::OmpAllocateDirective &x);
277
278 void CheckIteratorRange(const parser::OmpIteratorSpecifier &x);
279 void CheckIteratorModifier(const parser::OmpIterator &x);
280 void CheckLoopItrVariableIsInt(const parser::OpenMPLoopConstruct &x);
281 void CheckDoWhile(const parser::OpenMPLoopConstruct &x);
282 void CheckAssociatedLoopConstraints(const parser::OpenMPLoopConstruct &x);
283 template <typename T, typename D> bool IsOperatorValid(const T &, const D &);
284
285 void CheckStorageOverlap(const evaluate::Expr<evaluate::SomeType> &,
287 void ErrorShouldBeVariable(const MaybeExpr &expr, parser::CharBlock source);
288 void CheckAtomicType(SymbolRef sym, parser::CharBlock source,
289 std::string_view name, bool checkTypeOnPointer = true);
290 void CheckAtomicVariable(const evaluate::Expr<evaluate::SomeType> &,
291 parser::CharBlock, bool checkTypeOnPointer = true);
292 std::pair<const parser::ExecutionPartConstruct *,
294 CheckUpdateCapture(const parser::ExecutionPartConstruct *ec1,
296 void CheckAtomicCaptureAssignment(const evaluate::Assignment &capture,
297 const SomeExpr &atom, parser::CharBlock source);
298 void CheckAtomicReadAssignment(
299 const evaluate::Assignment &read, parser::CharBlock source);
300 void CheckAtomicWriteAssignment(
301 const evaluate::Assignment &write, parser::CharBlock source);
302 std::optional<evaluate::Assignment> CheckAtomicUpdateAssignment(
303 const evaluate::Assignment &update, parser::CharBlock source);
304 std::pair<bool, bool> CheckAtomicUpdateAssignmentRhs(const SomeExpr &atom,
305 const SomeExpr &rhs, parser::CharBlock source, bool suppressDiagnostics);
306 void CheckAtomicConditionalUpdateAssignment(const SomeExpr &cond,
307 parser::CharBlock condSource, const evaluate::Assignment &assign,
308 parser::CharBlock assignSource);
309 void CheckAtomicConditionalUpdateStmt(
310 const AnalyzedCondStmt &update, parser::CharBlock source);
311 void CheckAtomicUpdateOnly(const parser::OpenMPAtomicConstruct &x,
312 const parser::Block &body, parser::CharBlock source);
313 void CheckAtomicConditionalUpdate(const parser::OpenMPAtomicConstruct &x,
314 const parser::Block &body, parser::CharBlock source);
315 void CheckAtomicUpdateCapture(const parser::OpenMPAtomicConstruct &x,
316 const parser::Block &body, parser::CharBlock source);
317 void CheckAtomicConditionalUpdateCapture(
318 const parser::OpenMPAtomicConstruct &x, const parser::Block &body,
319 parser::CharBlock source);
320 void CheckAtomicRead(const parser::OpenMPAtomicConstruct &x);
321 void CheckAtomicWrite(const parser::OpenMPAtomicConstruct &x);
322 void CheckAtomicUpdate(const parser::OpenMPAtomicConstruct &x);
323
324 void CheckScanModifier(const parser::OmpClause::Reduction &x);
325 void CheckLooprangeBounds(const parser::OpenMPLoopConstruct &x);
326 void CheckNestedFuse(const parser::OpenMPLoopConstruct &x);
327 void CheckDistLinear(const parser::OpenMPLoopConstruct &x);
328 void CheckSIMDNest(const parser::OpenMPConstruct &x);
329 void CheckNestedBlock(const parser::OpenMPLoopConstruct &x,
330 const parser::Block &body, size_t &nestedCount);
331 void CheckNestedConstruct(const parser::OpenMPLoopConstruct &x);
332 void CheckFullUnroll(const parser::OpenMPLoopConstruct &x);
333 void CheckTargetNest(const parser::OpenMPConstruct &x);
334 void CheckTargetUpdate();
335 void CheckTaskgraph(const parser::OmpBlockConstruct &x);
336 void CheckDependenceType(const parser::OmpDependenceType::Value &x);
337 void CheckTaskDependenceType(const parser::OmpTaskDependenceType::Value &x);
338 std::optional<llvm::omp::Directive> GetCancelType(
339 llvm::omp::Directive cancelDir, const parser::CharBlock &cancelSource,
340 const std::optional<parser::OmpClauseList> &maybeClauses);
341 void CheckCancellationNest(
342 const parser::CharBlock &source, llvm::omp::Directive type);
343 std::int64_t GetOrdCollapseLevel(const parser::OpenMPLoopConstruct &x);
344 void CheckReductionObjects(
345 const parser::OmpObjectList &objects, llvm::omp::Clause clauseId);
346 bool CheckReductionOperator(const parser::OmpReductionIdentifier &ident,
347 parser::CharBlock source, llvm::omp::Clause clauseId);
348 void CheckReductionObjectTypes(const parser::OmpObjectList &objects,
349 const parser::OmpReductionIdentifier &ident);
350 void CheckReductionModifier(const parser::OmpReductionModifier &);
351 void CheckLastprivateModifier(const parser::OmpLastprivateModifier &);
352 void CheckMasterNesting(const parser::OmpBlockConstruct &x);
353 void ChecksOnOrderedAsBlock();
354 void CheckBarrierNesting(const parser::OpenMPSimpleStandaloneConstruct &x);
355 void CheckScan(const parser::OpenMPSimpleStandaloneConstruct &x);
356 void ChecksOnOrderedAsStandalone();
357 void CheckOrderedDependClause(std::optional<std::int64_t> orderedValue);
358 void CheckReductionArraySection(
359 const parser::OmpObjectList &ompObjectList, llvm::omp::Clause clauseId);
360 void CheckArraySection(const parser::ArrayElement &arrayElement,
361 const parser::Name &name, const llvm::omp::Clause clause);
362 void CheckSharedBindingInOuterContext(
363 const parser::OmpObjectList &ompObjectList);
364 void CheckIfContiguous(const parser::OmpObject &object);
365 const parser::Name *GetObjectName(const parser::OmpObject &object);
366
367 void CheckAllowedRequiresClause(llvmOmpClause clause);
368 bool deviceConstructFound_{false};
369
370 void AddEndDirectiveClauses(const parser::OmpClauseList &clauses);
371
372 void EnterDirectiveNest(const int index) { directiveNest_[index]++; }
373 void ExitDirectiveNest(const int index) { directiveNest_[index]--; }
374 int GetDirectiveNest(const int index) { return directiveNest_[index]; }
375 inline void ErrIfAllocatableVariable(const parser::Variable &);
376 inline void ErrIfLHSAndRHSSymbolsMatch(
377 const parser::Variable &, const parser::Expr &);
378 inline void ErrIfNonScalarAssignmentStmt(
379 const parser::Variable &, const parser::Expr &);
380 enum directiveNestType : int {
381 SIMDNest,
382 TargetBlockOnlyTeams,
383 TargetNest,
384 DeclarativeNest,
385 ContextSelectorNest,
386 MetadirectiveNest,
387 LastType = MetadirectiveNest,
388 };
389 int directiveNest_[LastType + 1] = {0};
390
391 int allocateDirectiveLevel{0};
392 parser::CharBlock visitedAtomicSource_;
393 SymbolSourceMap deferredNonVariables_;
394
395 using LoopConstruct = std::variant<const parser::DoConstruct *,
397 std::vector<LoopConstruct> loopStack_;
398 // Scopes for scoping units.
399 std::vector<const Scope *> scopeStack_;
400
401 enum class PartKind : int {
402 // There are also other "parts", such as internal-subprogram-part, etc,
403 // but we're keeping track of these two for now.
404 SpecificationPart,
405 ExecutionPart,
406 };
407 std::vector<PartKind> partStack_;
408};
409
412template <typename LessTy, typename RangeTy, typename IterTy>
413std::optional<IterTy> OmpStructureChecker::FindDuplicate(RangeTy &&range) {
414 // Deal with iterators, since the actual elements may be rvalues (i.e.
415 // have no addresses), for example with custom-constructed ranges that
416 // are not simple c.begin()..c.end().
417 std::set<IterTy, LessTy> uniq;
418 for (auto it{range.begin()}, end{range.end()}; it != end; ++it) {
419 if (!uniq.insert(it).second) {
420 return it;
421 }
422 }
423 return std::nullopt;
424}
425
426} // namespace Fortran::semantics
427#endif // FORTRAN_SEMANTICS_CHECK_OMP_STRUCTURE_H_
Definition enum-set.h:28
Definition indirection.h:31
Definition expression.h:878
Definition common.h:214
Definition char-block.h:28
Definition check-directive-structure.h:183
Definition semantics.h:67
Definition FIRType.h:92
Definition parse-tree.h:1921
Definition parse-tree.h:2205
Definition parse-tree.h:3037
Definition parse-tree.h:1828
Definition parse-tree.h:1867
Definition parse-tree.h:2348
Definition parse-tree.h:554
Definition parse-tree.h:1710
Definition parse-tree.h:3159
Definition parse-tree.h:2946
Definition parse-tree.h:2981
Definition parse-tree.h:587
Definition parse-tree.h:5214
Definition parse-tree.h:5008
Definition parse-tree.h:5018
Definition parse-tree.h:4977
Definition parse-tree.h:4961
Definition parse-tree.h:4984
Definition parse-tree.h:4442
Definition parse-tree.h:5013
Definition parse-tree.h:5355
Definition parse-tree.h:5084
Definition parse-tree.h:5046
Definition parse-tree.h:5031
Definition parse-tree.h:5040
Definition parse-tree.h:3561
Definition parse-tree.h:3549
Definition parse-tree.h:3605
Definition parse-tree.h:5240
Definition parse-tree.h:5073
Definition parse-tree.h:5245
Definition parse-tree.h:5282
Definition parse-tree.h:5386
Definition parse-tree.h:5230
Definition parse-tree.h:5061
Definition parse-tree.h:5293
Definition parse-tree.h:5307
Definition parse-tree.h:5323
Definition parse-tree.h:5169
Definition parse-tree.h:5331
Definition parse-tree.h:5404
Definition parse-tree.h:5360
Definition parse-tree.h:5175
Definition parse-tree.h:5099
Definition parse-tree.h:5181
Definition parse-tree.h:451
Definition parse-tree.h:3023
Definition parse-tree.h:3178
Definition parse-tree.h:1875
Definition parse-tree.h:4060
Definition parse-tree.h:4070
Definition parse-tree.h:4078
Definition parse-tree.h:4177
Definition parse-tree.h:3751
Definition parse-tree.h:3717
Definition parse-tree.h:3790
Definition parse-tree.h:3812
Definition check-omp-atomic.cpp:242