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
19#include "flang/Parser/openmp-utils.h"
20#include "flang/Parser/parse-tree.h"
21#include "flang/Semantics/openmp-directive-sets.h"
22#include "flang/Semantics/semantics.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/iterator_range.h"
25#include "llvm/Frontend/OpenMP/OMP.h"
26#include "llvm/Frontend/OpenMP/OMPDescriptors.h"
27
28#include <cstddef>
29#include <functional>
30#include <list>
31#include <map>
32#include <optional>
33#include <set>
34#include <string>
35#include <string_view>
36#include <utility>
37#include <variant>
38#include <vector>
39
40#define GEN_FLANG_DIRECTIVE_CLAUSE_SETS
41#include "llvm/Frontend/OpenMP/OMP.inc"
42
43namespace llvm::omp {
44static Clauses privateSet{
45 Clause::OMPC_private, Clause::OMPC_firstprivate, Clause::OMPC_lastprivate};
46static Clauses privateReductionSet{
47 Clauses{Clause::OMPC_reduction} | privateSet};
48} // namespace llvm::omp
49
50namespace Fortran::semantics {
51struct AnalyzedCondStmt;
52
53namespace omp {
54struct LoopSequence;
55}
56
57// Support classes for verifying syntactic properties.
58template <typename ElemTy, typename SetsSetTy> struct AppliedElement {
60 SetsSetTy sets;
61 llvm::omp::Version version{};
62};
63
64template <typename ElemTy, typename SetsSetTy> struct AppliedElementInfo {
65 using ElementTy = AppliedElement<ElemTy, SetsSetTy>;
67};
68
69using AppliedModifierInfo =
71using AppliedClauseInfo =
73
74// Mapping from 'Symbol' to 'Source' to keep track of the variables
75// used in multiple clauses
76using SymbolSourceMap = std::multimap<const Symbol *, parser::CharBlock>;
77// Multimap to check the triple <current_dir, enclosing_dir, enclosing_clause>
78using DirectivesClauseTriple = std::multimap<llvm::omp::Directive,
79 std::pair<llvm::omp::Directive, const llvm::omp::Clauses>>;
80
81using OmpStructureCheckerBase = DirectiveStructureChecker<llvm::omp::Directive,
82 llvm::omp::Clause, parser::OmpClause, llvm::omp::Clauses>;
83
84template <>
85void IterateOverMembers(
86 const llvm::omp::Clauses &set, std::function<void(llvm::omp::Clause)> func);
87
88class OmpStructureChecker : public OmpStructureCheckerBase {
89public:
90 using Base = OmpStructureCheckerBase;
91
92 OmpStructureChecker(SemanticsContext &context);
93
94 void Enter(const parser::ProgramUnit &);
95 void Leave(const parser::ProgramUnit &);
96 void Enter(const parser::MainProgram &);
97 void Leave(const parser::MainProgram &);
98 void Enter(const parser::BlockData &);
99 void Leave(const parser::BlockData &);
100 void Enter(const parser::Module &);
101 void Leave(const parser::Module &);
102 void Enter(const parser::Submodule &);
103 void Leave(const parser::Submodule &);
104 void Enter(const parser::SubroutineStmt &);
105 void Enter(const parser::EndSubroutineStmt &);
106 void Enter(const parser::FunctionStmt &);
107 void Enter(const parser::EndFunctionStmt &);
108 void Enter(const parser::MpSubprogramStmt &);
109 void Enter(const parser::EndMpSubprogramStmt &);
110 void Enter(const parser::Block &);
111 void Leave(const parser::Block &);
112 void Enter(const parser::BlockConstruct &);
113 void Leave(const parser::BlockConstruct &);
114 void Enter(const parser::InternalSubprogram &);
115 void Enter(const parser::ModuleSubprogram &);
116 void Enter(const parser::ModuleSubprogramPart &);
117 void Enter(const parser::InterfaceBody &);
118 void Leave(const parser::InterfaceBody &);
119
120 void Enter(const parser::SpecificationPart &);
121 void Leave(const parser::SpecificationPart &);
122 void Enter(const parser::ExecutionPart &);
123 void Leave(const parser::ExecutionPart &);
124
125 void Enter(const parser::OpenMPConstruct &);
126 void Leave(const parser::OpenMPConstruct &);
127 void Enter(const parser::OpenMPDeclarativeConstruct &);
128 void Leave(const parser::OpenMPDeclarativeConstruct &);
129
130 void Enter(const parser::OpenMPMisplacedEndDirective &);
131 void Leave(const parser::OpenMPMisplacedEndDirective &);
132 void Enter(const parser::OpenMPInvalidDirective &);
133 void Leave(const parser::OpenMPInvalidDirective &);
134
135 void Enter(const parser::OpenMPLoopConstruct &);
136 void Leave(const parser::OpenMPLoopConstruct &);
137
138 void Enter(const parser::OpenMPInteropConstruct &);
139 void Enter(const parser::OmpBlockConstruct &);
140 void Leave(const parser::OmpBlockConstruct &);
141 void Enter(const parser::OmpBeginDirective &);
142 void Leave(const parser::OmpBeginDirective &);
143
144 void Enter(const parser::OpenMPSectionsConstruct &);
145
146 void Enter(const parser::OmpDeclareVariantDirective &);
147 void Enter(const parser::OmpDeclareSimdDirective &);
148 void Enter(const parser::OmpAllocateDirective &);
149 void Leave(const parser::OmpAllocateDirective &);
150 void Enter(const parser::OmpDeclareMapperDirective &);
151 void Enter(const parser::OmpDeclareReductionDirective &);
152 void Enter(const parser::OmpDeclareTargetDirective &);
153 void Leave(const parser::OmpDeclareTargetDirective &);
154 void Enter(const parser::OpenMPDepobjConstruct &);
155 void Enter(const parser::OpenMPDispatchConstruct &);
156 void Enter(const parser::OpenMPAllocatorsConstruct &);
157 void Enter(const parser::OmpRequiresDirective &);
158 void Enter(const parser::OmpGroupprivateDirective &);
159 void Leave(const parser::OmpThreadprivateDirective &);
160
161 void Enter(const parser::OpenMPSimpleStandaloneConstruct &);
162 void Leave(const parser::OpenMPSimpleStandaloneConstruct &);
163 void Leave(const parser::OpenMPFlushConstruct &);
164 void Enter(const parser::OpenMPCancelConstruct &);
166 void Enter(const parser::OpenMPCriticalConstruct &);
167 void Enter(const parser::OpenMPAtomicConstruct &);
168
169 void Enter(const parser::OmpClauseList &);
170 void Leave(const parser::OmpClauseList &);
171 void Enter(const parser::OmpClause &);
172
173 void Enter(const parser::DoConstruct &);
174 void Leave(const parser::DoConstruct &);
175
176 void Enter(const parser::OmpDirectiveSpecification &);
177 void Leave(const parser::OmpDirectiveSpecification &);
178
179 void Enter(const parser::OmpErrorDirective &);
180
181 void Enter(const parser::OmpMetadirectiveDirective &);
182 void Leave(const parser::OmpMetadirectiveDirective &);
183
184 void Enter(const parser::ExecutionPartConstruct &);
185 void Leave(const parser::OmpClause::When &);
186
187 void Enter(const parser::OmpContextSelector &);
188 void Leave(const parser::OmpContextSelector &);
189
190 void Enter(const parser::OmpLoopModifier &);
191
192 void Enter(const parser::OmpClause::Apply &);
193 void Leave(const parser::OmpClause::Apply &);
194
195 template <typename A> void Enter(const parser::Statement<A> &);
196 void Leave(const parser::GotoStmt &);
197 void Leave(const parser::ComputedGotoStmt &);
198 void Leave(const parser::ArithmeticIfStmt &);
199 void Leave(const parser::AssignedGotoStmt &);
200 void Leave(const parser::AltReturnSpec &);
201 void Leave(const parser::ErrLabel &);
202 void Leave(const parser::EndLabel &);
203 void Leave(const parser::EorLabel &);
204
205 void Enter(const parser::OmpClause::Affinity &x);
206 void Enter(const parser::OmpClause::Align &x);
207 void Enter(const parser::OmpClause::Aligned &x);
208 void Enter(const parser::OmpClause::Allocate &x);
209 void Enter(const parser::OmpClause::Allocator &x);
210 void Enter(const parser::OmpClause::At &x);
211 void Enter(const parser::OmpClause::AtomicDefaultMemOrder &x);
212 void Enter(const parser::OmpClause::CancellationConstructType &x);
213 void Enter(const parser::OmpClause::Collapse &x);
214 void Enter(const parser::OmpClause::Copyin &x);
215 void Enter(const parser::OmpClause::Copyprivate &x);
216 void Enter(const parser::OmpClause::Defaultmap &x);
217 void Enter(const parser::OmpClause::Depend &x);
218 void Enter(const parser::OmpClause::Depth &x);
219 void Enter(const parser::OmpClause::Destroy &x);
220 void Enter(const parser::OmpClause::Detach &x);
221 void Enter(const parser::OmpClause::DeviceSafesync &x);
222 void Enter(const parser::OmpClause::Device &x);
223 void Enter(const parser::OmpClause::Doacross &x);
224 void Enter(const parser::OmpClause::DynamicAllocators &x);
225 void Enter(const parser::OmpClause::Firstprivate &x);
226 void Enter(const parser::OmpClause::From &x);
227 void Enter(const parser::OmpClause::HasDeviceAddr &x);
228 void Enter(const parser::OmpClause::Hint &x);
229 void Enter(const parser::OmpClause::If &x);
230 void Enter(const parser::OmpClause::InReduction &x);
231 void Enter(const parser::OmpClause::IsDevicePtr &x);
232 void Enter(const parser::OmpClause::Lastprivate &x);
233 void Enter(const parser::OmpClause::Linear &x);
234 void Enter(const parser::OmpClause::Looprange &x);
235 void Enter(const parser::OmpClause::Map &x);
236 void Enter(const parser::OmpClause::Message &x);
237 void Enter(const parser::OmpClause::NumTeams &x);
238 void Enter(const parser::OmpClause::NumThreads &x);
239 void Enter(const parser::OmpClause::OmpxBare &x);
240 void Enter(const parser::OmpClause::OmpxDynCgroupMem &x);
241 void Enter(const parser::OmpClause::Ordered &x);
242 void Enter(const parser::OmpClause::Permutation &x);
243 void Enter(const parser::OmpClause::Priority &x);
244 void Enter(const parser::OmpClause::Private &x);
245 void Enter(const parser::OmpClause::Reduction &x);
246 void Enter(const parser::OmpClause::ReverseOffload &x);
247 void Enter(const parser::OmpClause::Safelen &x);
248 void Enter(const parser::OmpClause::Schedule &x);
249 void Enter(const parser::OmpClause::SelfMaps &x);
250 void Enter(const parser::OmpClause::Shared &x);
251 void Enter(const parser::OmpClause::Simdlen &x);
252 void Enter(const parser::OmpClause::Sizes &x);
253 void Enter(const parser::OmpClause::TaskReduction &x);
254 void Enter(const parser::OmpClause::ThreadLimit &x);
255 void Enter(const parser::OmpClause::To &x);
256 void Enter(const parser::OmpClause::UnifiedAddress &x);
257 void Enter(const parser::OmpClause::UnifiedSharedMemory &x);
258 void Enter(const parser::OmpClause::UpdateDependObjects &x);
259 void Enter(const parser::OmpClause::UseDeviceAddr &x);
260 void Enter(const parser::OmpClause::UseDevicePtr &x);
261 void Enter(const parser::OmpClause::UsesAllocators &x);
262 void Enter(const parser::OmpClause::When &x);
263
264private:
265 using LoopOrConstruct = std::variant<const parser::DoConstruct *,
267
268 // Most of these functions are defined in check-omp-structure.cpp, but
269 // some groups have their own files.
270
271 // check-omp-atomic.cpp
272 void CheckStorageOverlap(const evaluate::Expr<evaluate::SomeType> &,
274 bool isError = false);
275 void ErrorShouldBeVariable(const MaybeExpr &expr, parser::CharBlock source);
276 void CheckAtomicType(SymbolRef sym, parser::CharBlock source,
277 std::string_view name, bool checkTypeOnPointer = true);
278 void CheckAtomicVariable(const evaluate::Expr<evaluate::SomeType> &,
279 parser::CharBlock, bool checkTypeOnPointer = true);
280 std::pair<const parser::ExecutionPartConstruct *,
282 CheckUpdateCapture(const parser::ExecutionPartConstruct *ec1,
284 void CheckAtomicCaptureAssignment(const evaluate::Assignment &capture,
285 const SomeExpr &atom, parser::CharBlock source);
286 void CheckAtomicReadAssignment(
287 const evaluate::Assignment &read, parser::CharBlock source);
288 void CheckAtomicWriteAssignment(
289 const evaluate::Assignment &write, parser::CharBlock source);
290 std::optional<evaluate::Assignment> CheckAtomicUpdateAssignment(
291 const evaluate::Assignment &update, parser::CharBlock source);
292 std::pair<bool, bool> CheckAtomicUpdateAssignmentRhs(const SomeExpr &atom,
293 const SomeExpr &rhs, parser::CharBlock source, bool suppressDiagnostics);
294 void CheckAtomicConditionalUpdateAssignment(const SomeExpr &cond,
295 parser::CharBlock condSource, const evaluate::Assignment &assign,
296 parser::CharBlock assignSource);
297 void CheckAtomicConditionalUpdateStmt(
298 const AnalyzedCondStmt &update, parser::CharBlock source);
299 void CheckAtomicUpdateOnly(const parser::OpenMPAtomicConstruct &x,
300 const parser::Block &body, parser::CharBlock source);
301 void CheckAtomicConditionalUpdate(const parser::OpenMPAtomicConstruct &x,
302 const parser::Block &body, parser::CharBlock source);
303 void CheckAtomicUpdateCapture(const parser::OpenMPAtomicConstruct &x,
304 const parser::Block &body, parser::CharBlock source);
305 void CheckAtomicConditionalUpdateCapture(
306 const parser::OpenMPAtomicConstruct &x, const parser::Block &body,
307 parser::CharBlock source);
308 void CheckAtomicRead(const parser::OpenMPAtomicConstruct &x);
309 void CheckAtomicWrite(const parser::OpenMPAtomicConstruct &x);
310 void CheckAtomicUpdate(const parser::OpenMPAtomicConstruct &x);
311
312 // check-omp-loop.cpp
313 void HasInvalidDistributeNesting(const parser::OpenMPLoopConstruct &x);
314 void HasInvalidLoopBinding(const parser::OpenMPLoopConstruct &x);
315 void CheckSIMDNest(const parser::OpenMPConstruct &x);
316 void CheckRectangularNest(const parser::OmpDirectiveSpecification &spec,
317 const omp::LoopSequence &nest);
318 void CheckNestedConstruct(const parser::OpenMPLoopConstruct &x);
319 const parser::Name GetLoopIndex(const parser::DoConstruct *x);
320 void CheckIterationVariables(const parser::OpenMPLoopConstruct &x);
321 std::int64_t GetOrdCollapseLevel(const parser::OpenMPLoopConstruct &x);
322 void CheckAssociatedLoopConstraints(const parser::OpenMPLoopConstruct &x);
323 void CheckScanModifier(const parser::OmpClause::Reduction &x);
324 void CheckDoacross(
325 const parser::OmpDoacross &doa, llvm::omp::Clause clauseId);
326 void CheckDistLinear(const parser::OpenMPLoopConstruct &x);
327 void CheckUnrollFullTripCount(const parser::OpenMPLoopConstruct &x);
328
329 void BeginMetadirectiveVariantScope();
330 void EndMetadirectiveVariantScope();
331
332 // check-omp-variant.cpp
333 void CheckMetadirectiveVariantsWithoutLoop(std::size_t firstVariant = 0);
334 void CheckOmpDeclareVariantDirective(
336 void CheckDeclareVariantUserConditions(const parser::OmpContextSelector &);
337 const std::list<parser::OmpTraitProperty> &GetTraitPropertyList(
339 std::optional<llvm::omp::Clause> GetClauseFromProperty(
341
342 void CheckTraitSelectorList(const std::list<parser::OmpTraitSelector> &);
343 void CheckContextSelectorSpecification(const parser::OmpContextSelector &);
344 void CheckTraitSetSelector(const parser::OmpTraitSetSelector &);
345 void CheckTraitScore(const parser::OmpTraitScore &);
346 bool VerifyTraitPropertyLists(
348 void CheckTraitSelector(
350 void CheckTraitADMO(
352 void CheckTraitCondition(
354 void CheckTraitDeviceNum(
356 void CheckTraitRequires(
358 void CheckTraitSimd(
360
361 // check-omp-syntax.cpp
362 bool VerifyClauseVersion(
363 parser::OmpDirectiveName dirName, const AppliedClauseInfo &info);
364 bool VerifyClauseRequired(parser::OmpDirectiveName dirName,
365 const AppliedClauseInfo &info, llvm::omp::Version version);
366 bool VerifyClauseUnique(parser::OmpDirectiveName dirName,
367 const AppliedClauseInfo &info, llvm::omp::Version version);
368 bool VerifyClauseExclusive(parser::OmpDirectiveName dirName,
369 const AppliedClauseInfo &info, llvm::omp::Version version);
370 bool VerifyClauseUltimate(parser::OmpDirectiveName dirName,
371 const AppliedClauseInfo &info, llvm::omp::Version version);
372 bool VerifyClauseSyntax(parser::OmpDirectiveName dirName,
373 const AppliedClauseInfo &info, llvm::omp::Version version);
374 void VerifyClauseSyntax(const parser::OmpDirectiveSpecification *beginSpec,
376
377 bool VerifyModifierVersion(parser::omp::WithSource<llvm::omp::Clause> clause,
378 const AppliedModifierInfo &info);
379 bool VerifyModifierRequired(parser::omp::WithSource<llvm::omp::Clause> clause,
380 const AppliedModifierInfo &info, llvm::omp::Version version);
381 bool VerifyModifierUnique(parser::omp::WithSource<llvm::omp::Clause> clause,
382 const AppliedModifierInfo &info, llvm::omp::Version version);
383 bool VerifyModifierExclusive(
385 const AppliedModifierInfo &info, llvm::omp::Version version);
386 bool VerifyModifierUltimate(parser::omp::WithSource<llvm::omp::Clause> clause,
387 const AppliedModifierInfo &info, llvm::omp::Version version);
388 bool VerifyModifierSyntax(parser::omp::WithSource<llvm::omp::Clause> clause,
389 const AppliedModifierInfo &info, llvm::omp::Version version);
390 void VerifyModifierSyntax(const parser::OmpClause &x);
391
392 void SetAllowedClauseOverride(llvm::omp::Clause clauseId,
393 llvm::omp::Directive dirId,
394 llvm::omp::Version since = llvm::omp::Version());
395
396 // check-omp-structure.cpp
397 using ClauseIterator =
398 decltype(std::declval<const parser::OmpClauseList>().v.begin());
399 bool IsAllowedClause(llvm::omp::Clause clauseId);
400 void CheckArgumentObjectKind(const parser::OmpClause &x);
401 void CheckDirectiveSpelling(
402 parser::CharBlock spelling, llvm::omp::Directive id);
403 void CheckCompoundDirective(
404 const parser::OmpDirectiveSpecification *beginSpec,
405 const parser::OmpDirectiveSpecification *endSpec);
406 void CheckDirectiveDeprecation(const parser::OpenMPConstruct &x);
407 void CheckDirectivePureSince(parser::CharBlock source,
408 llvm::omp::Directive id, const char *where,
410 void CheckDirectiveInPureProcedure(parser::CharBlock source,
411 llvm::omp::Directive id, const parser::OmpDirectiveSpecification &spec);
412 void CheckDirectiveInDoConcurrent(parser::CharBlock source,
413 llvm::omp::Directive id, const parser::OmpDirectiveSpecification &spec);
414 void AddClauses(llvm::iterator_range<ClauseIterator> beginClauses,
415 llvm::iterator_range<ClauseIterator> endClauses);
416 void AnalyzeObject(const parser::OmpObject &object);
417 void AnalyzeSubstring(const parser::OmpObject &object);
418
419 const parser::OpenMPConstruct *GetCurrentConstruct() const;
420 void CheckSourceLabel(const parser::Label &);
421 void CheckLabelContext(const parser::CharBlock, const parser::CharBlock,
423 void ClearLabels();
424 void CheckMultipleOccurrence(semantics::UnorderedSymbolSet &listVars,
425 const std::list<parser::Name> &nameList, const parser::CharBlock &item,
426 const std::string &clauseName);
427 void CheckMultListItems();
428 void CheckStructureComponent(
429 const parser::OmpObject &object, llvm::omp::Clause clauseId);
430 void CheckStructureComponent(
431 const parser::OmpObjectList &objects, llvm::omp::Clause clauseId);
432 bool HasInvalidWorksharingNesting(
433 const parser::OmpDirectiveName &name, const llvm::omp::Directives &);
434
435 bool IsCloselyNestedRegion(const llvm::omp::Directives &set);
436 bool IsNestedInDirective(llvm::omp::Directive directive);
437 bool IsCombinedParallelWorksharing(llvm::omp::Directive directive) const;
438 bool InTargetRegion();
439 void HasInvalidTeamsNesting(
440 const llvm::omp::Directive &dir, const parser::CharBlock &source);
441 bool HasRequires(llvm::omp::Clause req);
442 void CheckAllowedMapTypes(
443 parser::OmpMapType::Value, llvm::ArrayRef<parser::OmpMapType::Value>);
444 void CheckCloseModifierOnMapMembers();
445
446 llvm::StringRef getClauseName(llvm::omp::Clause clause) override;
447 llvm::StringRef getDirectiveName(llvm::omp::Directive directive) override;
448
449 template < //
450 typename LessTy, typename RangeTy,
451 typename IterTy = decltype(std::declval<RangeTy>().begin())>
452 std::optional<IterTy> FindDuplicate(RangeTy &&);
453
454 void CheckDependList(const parser::DataRef &);
455 void CheckDimsModifier(parser::CharBlock source, size_t numValues,
456 const parser::OmpDimsModifier &x);
457 void CheckTypeParamInquiry(const parser::CharBlock &source,
458 const parser::OmpObject &object, llvm::omp::Directive dirId);
459 void CheckTypeParamInquiry(const parser::CharBlock &source,
460 const parser::OmpObject &object, llvm::omp::Clause clauseId);
461 void CheckVarIsNotPartOfAnotherVar(const parser::CharBlock &source,
462 const parser::OmpObject &object, llvm::StringRef clause = "",
463 bool checkSubstrings = true);
464 void CheckVarIsNotPartOfAnotherVar(const parser::CharBlock &source,
465 const parser::OmpObjectList &objList, llvm::StringRef clause = "",
466 bool checkSubstrings = true);
467 void CheckThreadprivateOrDeclareTargetVar(const parser::Designator &);
468 void CheckThreadprivateOrDeclareTargetVar(const parser::Name &);
469 void CheckThreadprivateOrDeclareTargetVar(const parser::OmpObject &);
470 void CheckThreadprivateOrDeclareTargetVar(const parser::OmpObjectList &);
471 void CheckIntentInPointer(SymbolSourceMap &, const llvm::omp::Clause);
472 void CheckProcedurePointer(SymbolSourceMap &, const llvm::omp::Clause);
473 void CheckCrayPointee(const parser::OmpObjectList &objectList,
474 llvm::StringRef clause, bool suggestToUseCrayPointer = true);
475 void GetSymbolsInObjectList(const parser::OmpObjectList &, SymbolSourceMap &);
476 void CheckDefaultNoneInAssociatedLoop(
478 UnorderedSymbolSet &diagnosed);
479 void CheckDefinableObjects(SymbolSourceMap &, const llvm::omp::Clause);
480 void CheckCopyingPolymorphicAllocatable(
481 SymbolSourceMap &, const llvm::omp::Clause);
482 void CheckPrivateSymbolsInOuterCxt(
483 SymbolSourceMap &, DirectivesClauseTriple &, const llvm::omp::Clause);
484 bool CheckTargetBlockOnlyTeams(const parser::Block &);
485 void CheckWorkshareBlockStmts(const parser::Block &, parser::CharBlock);
486 void CheckWorkdistributeBlockStmts(const parser::Block &, parser::CharBlock);
487 void CheckIndividualAllocateDirective(
488 const parser::OmpAllocateDirective &x, bool isExecutable);
489 void CheckExecutableAllocateDirective(const parser::OmpAllocateDirective &x);
490
491 void CheckUsesAllocatorsSpec(
493 void CheckUsesAllocatorsTraits(
494 const parser::OmpTraitsArray &traits, parser::CharBlock source);
495
496 void CheckIteratorRange(const parser::OmpIteratorSpecifier &x);
497 void CheckIteratorModifier(const parser::OmpIterator &x);
498
499 void CheckTargetNest(const parser::OpenMPConstruct &x);
500 void CheckTargetUpdate();
501 void CheckTaskgraph(const parser::OmpBlockConstruct &x);
502 void CheckTaskDependenceType(const parser::OmpTaskDependenceType::Value &x);
503 std::optional<llvm::omp::Directive> GetCancelType(
504 llvm::omp::Directive cancelDir, const parser::CharBlock &cancelSource,
505 const std::optional<parser::OmpClauseList> &maybeClauses);
506 void CheckCancellationNest(
507 const parser::CharBlock &source, llvm::omp::Directive type);
508 void CheckReductionObjects(
509 const parser::OmpObjectList &objects, llvm::omp::Clause clauseId);
510 void CheckSubstring(
511 const parser::OmpObjectList &objects, llvm::omp::Clause clauseId);
512 bool CheckReductionOperator(const parser::OmpReductionIdentifier &ident,
513 parser::CharBlock source, llvm::omp::Clause clauseId);
514 void CheckReductionObjectTypes(const parser::OmpObjectList &objects,
515 const parser::OmpReductionIdentifier &ident);
516 void CheckReductionModifier(const parser::OmpReductionModifier &);
517 void CheckLastprivateModifier(const parser::OmpLastprivateModifier &);
518 void CheckSingleConstruct(const parser::OmpBlockConstruct &x);
519 void CheckMasterNesting(const parser::OmpBlockConstruct &x);
520 void ChecksOnOrderedAsBlock();
521 void CheckBarrierNesting(const parser::OpenMPSimpleStandaloneConstruct &x);
522 void CheckScan(const parser::OpenMPSimpleStandaloneConstruct &x);
523 void ChecksOnOrderedAsStandalone();
524 void CheckOrderedDependClause(std::optional<std::int64_t> orderedValue);
525 void CheckReductionArraySection(
526 const parser::OmpObjectList &ompObjectList, llvm::omp::Clause clauseId);
527 void CheckArraySection(const parser::ArrayElement &arrayElement,
528 const parser::Name &name, const llvm::omp::Clause clause);
529 void CheckArraySectionStrides(const parser::DataRef &, llvm::omp::Clause);
530 void CheckLastPartRefForArraySection(
531 const parser::Designator &designator, llvm::omp::Clause clauseId);
532 void CheckSharedBindingInOuterContext(
533 const parser::OmpObjectList &ompObjectList);
534 void CheckIfContiguous(const parser::OmpObject &object);
535 const parser::Name *GetObjectName(const parser::OmpObject &object);
536 void CheckInitOnDepobj(const parser::OpenMPDepobjConstruct &depobj,
537 const parser::OmpClause &initClause);
538 void CheckAllowedRequiresClause(llvm::omp::Clause clause);
539 void AddEndDirectiveClauses(const parser::OmpClauseList &clauses);
540 void CheckTempDescriptorMappings();
541
542 void EnterDirectiveNest(const int index) { directiveNest_[index]++; }
543 void ExitDirectiveNest(const int index) { directiveNest_[index]--; }
544 int GetDirectiveNest(const int index) { return directiveNest_[index]; }
545
546 bool deviceConstructFound_{false};
547 enum directiveNestType : int {
548 ApplyNest,
549 SIMDNest,
550 TargetBlockOnlyTeams,
551 TargetNest,
552 DeclarativeNest,
553 ContextSelectorNest,
554 MetadirectiveNest,
555 LastType = MetadirectiveNest,
556 };
557 int directiveNest_[LastType + 1] = {0};
558
559 std::set<std::pair<const Symbol *, const Symbol *>> declareVariantPairs_;
560 // For each variant procedure: its construct-selector-set as an ordered list
561 // of elements (a leaf construct directive plus a normalized rendering of any
562 // properties), and the source of the DECLARE VARIANT directive that first
563 // established the set.
564 std::map<const Symbol *,
565 std::pair<
568 declareVariantConstructSets_;
569 // Tracks the procedures that appear as a base in DECLARE VARIANT.
570 std::set<const Symbol *> declareVariantBases_;
571
572 int allocateDirectiveLevel_{0};
573 parser::CharBlock visitedAtomicSource_;
574
575 // Mapping of directive-name-modifier constituents to the sources of the
576 // IF clauses that referenced them. If there was no modifier, the entire
577 // directive is assumed to be listed.
578 std::map<llvm::omp::Directive, parser::CharBlock> ifLeafs_;
579
580 // Track symbols with temporary stack descriptors mapped in TARGET ENTER DATA
581 // and symbols mapped in TARGET EXIT DATA within the current function scope.
582 // Used to warn about potential issues with mapping temporary descriptors.
583 std::multimap<const Symbol *, parser::CharBlock> tempDescriptorEnterMaps_;
584 std::set<const Symbol *> tempDescriptorExitMaps_;
585
586 // Stack of nested DO loops and OpenMP constructs.
587 // This is used to verify DO loop nest for DOACROSS, and branches into
588 // and out of OpenMP constructs.
589 std::vector<LoopOrConstruct> constructStack_;
590 // Scopes for scoping units.
591 std::vector<const Scope *> scopeStack_;
592 // Stack of directive specifications (except for SECTION).
593 // This is to allow visitor functions to see all specified clauses, since
594 // they are only recorded in DirectiveContext as they are processed.
595 std::vector<const parser::OmpDirectiveSpecification *> dirStack_;
596
597 enum class PartKind : int {
598 // There are also other "parts", such as internal-subprogram-part, etc,
599 // but we're keeping track of these two for now.
600 SpecificationPart,
601 ExecutionPart,
602 };
603 std::vector<PartKind> partStack_;
604
605 struct MetadirectiveLoopVariant {
608 bool checkDefaultNoneInAssociatedLoop;
609 };
610 std::vector<MetadirectiveLoopVariant> metadirectiveLoopVariants_;
611 std::vector<std::size_t> metadirectiveVariantScopeStarts_;
612 const parser::traits::OmpContextSelectorSpecification *currentWhenSelector_{
613 nullptr};
614
615 std::multimap<const parser::Label,
616 std::pair<parser::CharBlock, const parser::OpenMPConstruct *>>
617 sourceLabels_;
618 std::map<const parser::Label,
619 std::pair<parser::CharBlock, const parser::OpenMPConstruct *>>
620 targetLabels_;
621 parser::CharBlock currentStatementSource_;
622};
623
624template <typename A>
625void OmpStructureChecker::Enter(const parser::Statement<A> &statement) {
626 currentStatementSource_ = statement.source;
627 // Keep track of the labels in all the labelled statements
628 if (statement.label) {
629 auto label{statement.label.value()};
630 // Get the context to check if the labelled statement is in an
631 // enclosing OpenMP construct
632 auto *thisConstruct{GetCurrentConstruct()};
633 targetLabels_.emplace(
634 label, std::make_pair(currentStatementSource_, thisConstruct));
635 // Check if a statement that causes a jump to the 'label'
636 // has already been encountered
637 auto range{sourceLabels_.equal_range(label)};
638 for (auto it{range.first}; it != range.second; ++it) {
639 // Check if both the statement with 'label' and the statement that
640 // causes a jump to the 'label' are in the same scope
641 CheckLabelContext(it->second.first, currentStatementSource_,
642 it->second.second, thisConstruct);
643 }
644 }
645}
646
649template <typename LessTy, typename RangeTy, typename IterTy>
650std::optional<IterTy> OmpStructureChecker::FindDuplicate(RangeTy &&range) {
651 // Deal with iterators, since the actual elements may be rvalues (i.e.
652 // have no addresses), for example with custom-constructed ranges that
653 // are not simple c.begin()..c.end().
654 std::set<IterTy, LessTy> uniq;
655 for (auto it{range.begin()}, end{range.end()}; it != end; ++it) {
656 if (!uniq.insert(it).second) {
657 return it;
658 }
659 }
660 return std::nullopt;
661}
662} // namespace Fortran::semantics
663#endif // FORTRAN_SEMANTICS_CHECK_OMP_STRUCTURE_H_
Definition expression.h:923
Definition common.h:215
Definition char-block.h:27
Definition check-directive-structure.h:212
Definition semantics.h:71
Definition symbol.h:916
Definition FIRType.h:106
Definition OpenACC.h:20
Definition parse-tree.h:3555
Definition parse-tree.h:1952
Definition parse-tree.h:3565
Definition parse-tree.h:2236
Definition parse-tree.h:3075
Definition parse-tree.h:2564
Definition parse-tree.h:1862
Definition parse-tree.h:1901
Definition parse-tree.h:2379
Definition parse-tree.h:559
Definition parse-tree.h:3195
Definition parse-tree.h:3227
Definition parse-tree.h:468
Definition parse-tree.h:2983
Definition parse-tree.h:3008
Definition parse-tree.h:2997
Definition parse-tree.h:3019
Definition parse-tree.h:592
Definition parse-tree.h:5529
Definition parse-tree.h:5318
Definition parse-tree.h:5328
Definition parse-tree.h:5287
Definition parse-tree.h:5271
Definition parse-tree.h:5456
Definition parse-tree.h:5473
Definition parse-tree.h:5448
Definition parse-tree.h:3591
Definition parse-tree.h:5294
Definition parse-tree.h:4681
Definition parse-tree.h:5361
Definition parse-tree.h:5482
Definition parse-tree.h:5341
Definition parse-tree.h:3646
Definition parse-tree.h:3634
Definition parse-tree.h:3690
Definition parse-tree.h:5489
Definition parse-tree.h:5495
Definition parse-tree.h:5555
Definition parse-tree.h:5560
Definition parse-tree.h:5597
Definition parse-tree.h:5681
Definition parse-tree.h:5545
Definition parse-tree.h:5608
Definition parse-tree.h:5622
Definition parse-tree.h:5638
Definition parse-tree.h:5646
Definition parse-tree.h:5705
Definition parse-tree.h:5667
Definition parse-tree.h:5413
Definition parse-tree.h:576
Definition parse-tree.h:456
Definition parse-tree.h:362
Definition parse-tree.h:3061
Definition parse-tree.h:3214
Definition parse-tree.h:4101
Definition parse-tree.h:4168
Definition parse-tree.h:4178
Definition parse-tree.h:4187
Definition parse-tree.h:4222
Definition parse-tree.h:4385
Definition parse-tree.h:4447
Definition openmp-utils.h:115
Definition parse-tree.h:3828
Definition parse-tree.h:3794
Definition parse-tree.h:3867
Definition parse-tree.h:3889
Definition check-omp-atomic.cpp:242
Definition check-omp-structure.h:64
Definition check-omp-structure.h:58
Definition openmp-utils.h:489