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#include "llvm/ADT/iterator_range.h"
23
24using OmpClauseSet =
26
27#define GEN_FLANG_DIRECTIVE_CLAUSE_SETS
28#include "llvm/Frontend/OpenMP/OMP.inc"
29
30namespace llvm {
31namespace omp {
32static OmpClauseSet privateSet{
33 Clause::OMPC_private, Clause::OMPC_firstprivate, Clause::OMPC_lastprivate};
34static OmpClauseSet privateReductionSet{
35 OmpClauseSet{Clause::OMPC_reduction} | privateSet};
36} // namespace omp
37} // namespace llvm
38
39namespace Fortran::semantics {
40struct AnalyzedCondStmt;
41
42namespace omp {
43struct LoopSequence;
44}
45
46// Mapping from 'Symbol' to 'Source' to keep track of the variables
47// used in multiple clauses
48using SymbolSourceMap = std::multimap<const Symbol *, parser::CharBlock>;
49// Multimap to check the triple <current_dir, enclosing_dir, enclosing_clause>
50using DirectivesClauseTriple = std::multimap<llvm::omp::Directive,
51 std::pair<llvm::omp::Directive, const OmpClauseSet>>;
52
53using OmpStructureCheckerBase = DirectiveStructureChecker<llvm::omp::Directive,
54 llvm::omp::Clause, parser::OmpClause, llvm::omp::Clause_enumSize>;
55
56class OmpStructureChecker : public OmpStructureCheckerBase {
57public:
58 using Base = OmpStructureCheckerBase;
59
60 OmpStructureChecker(SemanticsContext &context);
61
62 void Enter(const parser::ProgramUnit &);
63 void Leave(const parser::ProgramUnit &);
64 void Enter(const parser::MainProgram &);
65 void Leave(const parser::MainProgram &);
66 void Enter(const parser::BlockData &);
67 void Leave(const parser::BlockData &);
68 void Enter(const parser::Module &);
69 void Leave(const parser::Module &);
70 void Enter(const parser::Submodule &);
71 void Leave(const parser::Submodule &);
72 void Enter(const parser::SubroutineStmt &);
73 void Enter(const parser::EndSubroutineStmt &);
74 void Enter(const parser::FunctionStmt &);
75 void Enter(const parser::EndFunctionStmt &);
76 void Enter(const parser::MpSubprogramStmt &);
77 void Enter(const parser::EndMpSubprogramStmt &);
78 void Enter(const parser::Block &);
79 void Leave(const parser::Block &);
80 void Enter(const parser::BlockConstruct &);
81 void Leave(const parser::BlockConstruct &);
82 void Enter(const parser::InternalSubprogram &);
83 void Enter(const parser::ModuleSubprogram &);
84 void Enter(const parser::ModuleSubprogramPart &);
85 void Enter(const parser::InterfaceBody &);
86 void Leave(const parser::InterfaceBody &);
87
88 void Enter(const parser::SpecificationPart &);
89 void Leave(const parser::SpecificationPart &);
90 void Enter(const parser::ExecutionPart &);
91 void Leave(const parser::ExecutionPart &);
92
93 void Enter(const parser::OpenMPConstruct &);
94 void Leave(const parser::OpenMPConstruct &);
95 void Enter(const parser::OpenMPDeclarativeConstruct &);
96 void Leave(const parser::OpenMPDeclarativeConstruct &);
97
98 void Enter(const parser::OpenMPMisplacedEndDirective &);
99 void Leave(const parser::OpenMPMisplacedEndDirective &);
100 void Enter(const parser::OpenMPInvalidDirective &);
101 void Leave(const parser::OpenMPInvalidDirective &);
102
103 void Enter(const parser::OpenMPLoopConstruct &);
104 void Leave(const parser::OpenMPLoopConstruct &);
105
106 void Enter(const parser::OpenMPInteropConstruct &);
107 void Enter(const parser::OmpBlockConstruct &);
108 void Leave(const parser::OmpBlockConstruct &);
109 void Enter(const parser::OmpBeginDirective &);
110 void Leave(const parser::OmpBeginDirective &);
111
112 void Enter(const parser::OpenMPSectionsConstruct &);
113
114 void Enter(const parser::OmpDeclareVariantDirective &);
115 void Enter(const parser::OmpDeclareSimdDirective &);
116 void Enter(const parser::OmpAllocateDirective &);
117 void Leave(const parser::OmpAllocateDirective &);
118 void Enter(const parser::OmpDeclareMapperDirective &);
119 void Enter(const parser::OmpDeclareReductionDirective &);
120 void Enter(const parser::OmpDeclareTargetDirective &);
121 void Leave(const parser::OmpDeclareTargetDirective &);
122 void Enter(const parser::OpenMPDepobjConstruct &);
123 void Enter(const parser::OpenMPDispatchConstruct &);
124 void Enter(const parser::OpenMPAllocatorsConstruct &);
125 void Enter(const parser::OmpRequiresDirective &);
126 void Enter(const parser::OmpGroupprivateDirective &);
127 void Leave(const parser::OmpThreadprivateDirective &);
128
129 void Enter(const parser::OpenMPSimpleStandaloneConstruct &);
130 void Leave(const parser::OpenMPSimpleStandaloneConstruct &);
131 void Leave(const parser::OpenMPFlushConstruct &);
132 void Enter(const parser::OpenMPCancelConstruct &);
134 void Enter(const parser::OpenMPCriticalConstruct &);
135 void Enter(const parser::OpenMPAtomicConstruct &);
136
137 void Enter(const parser::OmpClauseList &);
138 void Leave(const parser::OmpClauseList &);
139 void Enter(const parser::OmpClause &);
140
141 void Enter(const parser::DoConstruct &);
142 void Leave(const parser::DoConstruct &);
143
144 void Enter(const parser::OmpDirectiveSpecification &);
145 void Leave(const parser::OmpDirectiveSpecification &);
146
147 void Enter(const parser::OmpMetadirectiveDirective &);
148 void Leave(const parser::OmpMetadirectiveDirective &);
149
150 void Enter(const parser::ExecutionPartConstruct &);
151 void Leave(const parser::OmpClause::When &);
152
153 void Enter(const parser::OmpContextSelector &);
154 void Leave(const parser::OmpContextSelector &);
155
156 void Enter(const parser::OmpLoopModifier &);
157
158 void Enter(const parser::OmpClause::Apply &);
159 void Leave(const parser::OmpClause::Apply &);
160
161 template <typename A> void Enter(const parser::Statement<A> &);
162 void Leave(const parser::GotoStmt &);
163 void Leave(const parser::ComputedGotoStmt &);
164 void Leave(const parser::ArithmeticIfStmt &);
165 void Leave(const parser::AssignedGotoStmt &);
166 void Leave(const parser::AltReturnSpec &);
167 void Leave(const parser::ErrLabel &);
168 void Leave(const parser::EndLabel &);
169 void Leave(const parser::EorLabel &);
170
171 void Enter(const parser::OmpClause::Affinity &x);
172 void Enter(const parser::OmpClause::Align &x);
173 void Enter(const parser::OmpClause::Aligned &x);
174 void Enter(const parser::OmpClause::Allocate &x);
175 void Enter(const parser::OmpClause::Allocator &x);
176 void Enter(const parser::OmpClause::At &x);
177 void Enter(const parser::OmpClause::AtomicDefaultMemOrder &x);
178 void Enter(const parser::OmpClause::CancellationConstructType &x);
179 void Enter(const parser::OmpClause::Collapse &x);
180 void Enter(const parser::OmpClause::Copyin &x);
181 void Enter(const parser::OmpClause::Copyprivate &x);
182 void Enter(const parser::OmpClause::Defaultmap &x);
183 void Enter(const parser::OmpClause::Depend &x);
184 void Enter(const parser::OmpClause::Depth &x);
185 void Enter(const parser::OmpClause::Destroy &x);
186 void Enter(const parser::OmpClause::Detach &x);
187 void Enter(const parser::OmpClause::DeviceSafesync &x);
188 void Enter(const parser::OmpClause::Device &x);
189 void Enter(const parser::OmpClause::Doacross &x);
190 void Enter(const parser::OmpClause::DynamicAllocators &x);
191 void Enter(const parser::OmpClause::DynGroupprivate &x);
192 void Enter(const parser::OmpClause::Enter &x);
193 void Enter(const parser::OmpClause::Firstprivate &x);
194 void Enter(const parser::OmpClause::From &x);
195 void Enter(const parser::OmpClause::HasDeviceAddr &x);
196 void Enter(const parser::OmpClause::Hint &x);
197 void Enter(const parser::OmpClause::If &x);
198 void Enter(const parser::OmpClause::InReduction &x);
199 void Enter(const parser::OmpClause::IsDevicePtr &x);
200 void Enter(const parser::OmpClause::Lastprivate &x);
201 void Enter(const parser::OmpClause::Linear &x);
202 void Enter(const parser::OmpClause::Looprange &x);
203 void Enter(const parser::OmpClause::Map &x);
204 void Enter(const parser::OmpClause::NumTeams &x);
205 void Enter(const parser::OmpClause::NumThreads &x);
206 void Enter(const parser::OmpClause::OmpxBare &x);
207 void Enter(const parser::OmpClause::OmpxDynCgroupMem &x);
208 void Enter(const parser::OmpClause::Ordered &x);
209 void Enter(const parser::OmpClause::Permutation &x);
210 void Enter(const parser::OmpClause::Priority &x);
211 void Enter(const parser::OmpClause::Private &x);
212 void Enter(const parser::OmpClause::Reduction &x);
213 void Enter(const parser::OmpClause::ReverseOffload &x);
214 void Enter(const parser::OmpClause::Safelen &x);
215 void Enter(const parser::OmpClause::Schedule &x);
216 void Enter(const parser::OmpClause::SelfMaps &x);
217 void Enter(const parser::OmpClause::Shared &x);
218 void Enter(const parser::OmpClause::Simdlen &x);
219 void Enter(const parser::OmpClause::Sizes &x);
220 void Enter(const parser::OmpClause::TaskReduction &x);
221 void Enter(const parser::OmpClause::ThreadLimit &x);
222 void Enter(const parser::OmpClause::To &x);
223 void Enter(const parser::OmpClause::UnifiedAddress &x);
224 void Enter(const parser::OmpClause::UnifiedSharedMemory &x);
225 void Enter(const parser::OmpClause::Update &x);
226 void Enter(const parser::OmpClause::UseDeviceAddr &x);
227 void Enter(const parser::OmpClause::UseDevicePtr &x);
228 void Enter(const parser::OmpClause::When &x);
229
230private:
231 using LoopOrConstruct = std::variant<const parser::DoConstruct *,
233
234 // Most of these functions are defined in check-omp-structure.cpp, but
235 // some groups have their own files.
236
237 // check-omp-atomic.cpp
238 void CheckStorageOverlap(const evaluate::Expr<evaluate::SomeType> &,
240 void ErrorShouldBeVariable(const MaybeExpr &expr, parser::CharBlock source);
241 void CheckAtomicType(SymbolRef sym, parser::CharBlock source,
242 std::string_view name, bool checkTypeOnPointer = true);
243 void CheckAtomicVariable(const evaluate::Expr<evaluate::SomeType> &,
244 parser::CharBlock, bool checkTypeOnPointer = true);
245 std::pair<const parser::ExecutionPartConstruct *,
247 CheckUpdateCapture(const parser::ExecutionPartConstruct *ec1,
249 void CheckAtomicCaptureAssignment(const evaluate::Assignment &capture,
250 const SomeExpr &atom, parser::CharBlock source);
251 void CheckAtomicReadAssignment(
252 const evaluate::Assignment &read, parser::CharBlock source);
253 void CheckAtomicWriteAssignment(
254 const evaluate::Assignment &write, parser::CharBlock source);
255 std::optional<evaluate::Assignment> CheckAtomicUpdateAssignment(
256 const evaluate::Assignment &update, parser::CharBlock source);
257 std::pair<bool, bool> CheckAtomicUpdateAssignmentRhs(const SomeExpr &atom,
258 const SomeExpr &rhs, parser::CharBlock source, bool suppressDiagnostics);
259 void CheckAtomicConditionalUpdateAssignment(const SomeExpr &cond,
260 parser::CharBlock condSource, const evaluate::Assignment &assign,
261 parser::CharBlock assignSource);
262 void CheckAtomicConditionalUpdateStmt(
263 const AnalyzedCondStmt &update, parser::CharBlock source);
264 void CheckAtomicUpdateOnly(const parser::OpenMPAtomicConstruct &x,
265 const parser::Block &body, parser::CharBlock source);
266 void CheckAtomicConditionalUpdate(const parser::OpenMPAtomicConstruct &x,
267 const parser::Block &body, parser::CharBlock source);
268 void CheckAtomicUpdateCapture(const parser::OpenMPAtomicConstruct &x,
269 const parser::Block &body, parser::CharBlock source);
270 void CheckAtomicConditionalUpdateCapture(
271 const parser::OpenMPAtomicConstruct &x, const parser::Block &body,
272 parser::CharBlock source);
273 void CheckAtomicRead(const parser::OpenMPAtomicConstruct &x);
274 void CheckAtomicWrite(const parser::OpenMPAtomicConstruct &x);
275 void CheckAtomicUpdate(const parser::OpenMPAtomicConstruct &x);
276
277 // check-omp-loop.cpp
278 void HasInvalidDistributeNesting(const parser::OpenMPLoopConstruct &x);
279 void HasInvalidLoopBinding(const parser::OpenMPLoopConstruct &x);
280 void CheckSIMDNest(const parser::OpenMPConstruct &x);
281 void CheckRectangularNest(const parser::OmpDirectiveSpecification &spec,
282 const omp::LoopSequence &nest);
283 void CheckNestedConstruct(const parser::OpenMPLoopConstruct &x);
284 const parser::Name GetLoopIndex(const parser::DoConstruct *x);
285 void CheckIterationVariables(const parser::OpenMPLoopConstruct &x);
286 std::int64_t GetOrdCollapseLevel(const parser::OpenMPLoopConstruct &x);
287 void CheckAssociatedLoopConstraints(const parser::OpenMPLoopConstruct &x);
288 void CheckScanModifier(const parser::OmpClause::Reduction &x);
289 void CheckDistLinear(const parser::OpenMPLoopConstruct &x);
290
291 void BeginMetadirectiveVariantScope();
292 void EndMetadirectiveVariantScope();
293
294 // check-omp-variant.cpp
295 void CheckMetadirectiveVariantsWithoutLoop(std::size_t firstVariant = 0);
296 void CheckOmpDeclareVariantDirective(
298 void CheckDeclareVariantUserConditions(const parser::OmpContextSelector &);
299 const std::list<parser::OmpTraitProperty> &GetTraitPropertyList(
301 std::optional<llvm::omp::Clause> GetClauseFromProperty(
303
304 void CheckTraitSelectorList(const std::list<parser::OmpTraitSelector> &);
305 void CheckContextSelectorSpecification(const parser::OmpContextSelector &);
306 void CheckTraitSetSelector(const parser::OmpTraitSetSelector &);
307 void CheckTraitScore(const parser::OmpTraitScore &);
308 bool VerifyTraitPropertyLists(
310 void CheckTraitSelector(
312 void CheckTraitADMO(
314 void CheckTraitCondition(
316 void CheckTraitDeviceNum(
318 void CheckTraitRequires(
320 void CheckTraitSimd(
322
323 // check-omp-structure.cpp
324 using ClauseIterator =
325 decltype(std::declval<const parser::OmpClauseList>().v.begin());
326 bool IsAllowedClause(llvm::omp::Clause clauseId);
327 bool CheckAllowedClause(llvm::omp::Clause clauseId,
328 parser::CharBlock clauseSource, llvm::omp::Directive dirId);
329 void CheckArgumentObjectKind(const parser::OmpClause &x);
330 void CheckDirectiveSpelling(
331 parser::CharBlock spelling, llvm::omp::Directive id);
332 void CheckDirectiveDeprecation(const parser::OpenMPConstruct &x);
333 void CheckClauses(parser::OmpDirectiveName dirName,
334 llvm::iterator_range<ClauseIterator> beginClauses,
335 llvm::iterator_range<ClauseIterator> endClauses);
336 void AnalyzeObject(const parser::OmpObject &object);
337 std::pair<const parser::OmpClause *, const parser::OmpClause *>
338 FindMutuallyExclusiveClauses(OmpClauseSet exclusive,
339 const std::vector<const parser::OmpClause *> &clauses);
340
341 const parser::OpenMPConstruct *GetCurrentConstruct() const;
342 void CheckSourceLabel(const parser::Label &);
343 void CheckLabelContext(const parser::CharBlock, const parser::CharBlock,
345 void ClearLabels();
346 void CheckMultipleOccurrence(semantics::UnorderedSymbolSet &listVars,
347 const std::list<parser::Name> &nameList, const parser::CharBlock &item,
348 const std::string &clauseName);
349 void CheckMultListItems();
350 void CheckStructureComponent(
351 const parser::OmpObject &object, llvm::omp::Clause clauseId);
352 void CheckStructureComponent(
353 const parser::OmpObjectList &objects, llvm::omp::Clause clauseId);
354 bool HasInvalidWorksharingNesting(
355 const parser::OmpDirectiveName &name, const OmpDirectiveSet &);
356
357 bool IsCloselyNestedRegion(const OmpDirectiveSet &set);
358 bool IsNestedInDirective(llvm::omp::Directive directive);
359 bool IsCombinedParallelWorksharing(llvm::omp::Directive directive) const;
360 bool InTargetRegion();
361 void HasInvalidTeamsNesting(
362 const llvm::omp::Directive &dir, const parser::CharBlock &source);
363 bool HasRequires(llvm::omp::Clause req);
364 void CheckAllowedMapTypes(
365 parser::OmpMapType::Value, llvm::ArrayRef<parser::OmpMapType::Value>);
366
367 llvm::StringRef getClauseName(llvm::omp::Clause clause) override;
368 llvm::StringRef getDirectiveName(llvm::omp::Directive directive) override;
369
370 template < //
371 typename LessTy, typename RangeTy,
372 typename IterTy = decltype(std::declval<RangeTy>().begin())>
373 std::optional<IterTy> FindDuplicate(RangeTy &&);
374
375 void CheckDependList(const parser::DataRef &);
376 void CheckDoacross(const parser::OmpDoacross &doa);
377 void CheckDimsModifier(parser::CharBlock source, size_t numValues,
378 const parser::OmpDimsModifier &x);
379 void CheckTypeParamInquiry(const parser::CharBlock &source,
380 const parser::OmpObject &object, llvm::omp::Directive dirId);
381 void CheckTypeParamInquiry(const parser::CharBlock &source,
382 const parser::OmpObject &object, llvm::omp::Clause clauseId);
383 void CheckVarIsNotPartOfAnotherVar(const parser::CharBlock &source,
384 const parser::OmpObject &object, llvm::StringRef clause = "");
385 void CheckVarIsNotPartOfAnotherVar(const parser::CharBlock &source,
386 const parser::OmpObjectList &objList, llvm::StringRef clause = "");
387 void CheckThreadprivateOrDeclareTargetVar(const parser::Designator &);
388 void CheckThreadprivateOrDeclareTargetVar(const parser::Name &);
389 void CheckThreadprivateOrDeclareTargetVar(const parser::OmpObject &);
390 void CheckThreadprivateOrDeclareTargetVar(const parser::OmpObjectList &);
391 void CheckIntentInPointer(SymbolSourceMap &, const llvm::omp::Clause);
392 void CheckProcedurePointer(SymbolSourceMap &, const llvm::omp::Clause);
393 void CheckCrayPointee(const parser::OmpObjectList &objectList,
394 llvm::StringRef clause, bool suggestToUseCrayPointer = true);
395 void GetSymbolsInObjectList(const parser::OmpObjectList &, SymbolSourceMap &);
396 void CheckDefinableObjects(SymbolSourceMap &, const llvm::omp::Clause);
397 void CheckCopyingPolymorphicAllocatable(
398 SymbolSourceMap &, const llvm::omp::Clause);
399 void CheckPrivateSymbolsInOuterCxt(
400 SymbolSourceMap &, DirectivesClauseTriple &, const llvm::omp::Clause);
401 bool CheckTargetBlockOnlyTeams(const parser::Block &);
402 void CheckWorkshareBlockStmts(const parser::Block &, parser::CharBlock);
403 void CheckWorkdistributeBlockStmts(const parser::Block &, parser::CharBlock);
404 void CheckIndividualAllocateDirective(
405 const parser::OmpAllocateDirective &x, bool isExecutable);
406 void CheckExecutableAllocateDirective(const parser::OmpAllocateDirective &x);
407
408 void CheckIteratorRange(const parser::OmpIteratorSpecifier &x);
409 void CheckIteratorModifier(const parser::OmpIterator &x);
410
411 void CheckTargetNest(const parser::OpenMPConstruct &x);
412 void CheckTargetUpdate();
413 void CheckTaskgraph(const parser::OmpBlockConstruct &x);
414 void CheckDependenceType(const parser::OmpDependenceType::Value &x);
415 void CheckTaskDependenceType(const parser::OmpTaskDependenceType::Value &x);
416 std::optional<llvm::omp::Directive> GetCancelType(
417 llvm::omp::Directive cancelDir, const parser::CharBlock &cancelSource,
418 const std::optional<parser::OmpClauseList> &maybeClauses);
419 void CheckCancellationNest(
420 const parser::CharBlock &source, llvm::omp::Directive type);
421 void CheckReductionObjects(
422 const parser::OmpObjectList &objects, llvm::omp::Clause clauseId);
423 bool CheckReductionOperator(const parser::OmpReductionIdentifier &ident,
424 parser::CharBlock source, llvm::omp::Clause clauseId);
425 void CheckReductionObjectTypes(const parser::OmpObjectList &objects,
426 const parser::OmpReductionIdentifier &ident);
427 void CheckReductionModifier(const parser::OmpReductionModifier &);
428 void CheckLastprivateModifier(const parser::OmpLastprivateModifier &);
429 void CheckSingleConstruct(const parser::OmpBlockConstruct &x);
430 void CheckMasterNesting(const parser::OmpBlockConstruct &x);
431 void ChecksOnOrderedAsBlock();
432 void CheckBarrierNesting(const parser::OpenMPSimpleStandaloneConstruct &x);
433 void CheckScan(const parser::OpenMPSimpleStandaloneConstruct &x);
434 void ChecksOnOrderedAsStandalone();
435 void CheckOrderedDependClause(std::optional<std::int64_t> orderedValue);
436 void CheckReductionArraySection(
437 const parser::OmpObjectList &ompObjectList, llvm::omp::Clause clauseId);
438 void CheckArraySection(const parser::ArrayElement &arrayElement,
439 const parser::Name &name, const llvm::omp::Clause clause);
440 void CheckLastPartRefForArraySection(
441 const parser::Designator &designator, llvm::omp::Clause clauseId);
442 void CheckSharedBindingInOuterContext(
443 const parser::OmpObjectList &ompObjectList);
444 void CheckIfContiguous(const parser::OmpObject &object);
445 const parser::Name *GetObjectName(const parser::OmpObject &object);
446 void CheckInitOnDepobj(const parser::OpenMPDepobjConstruct &depobj,
447 const parser::OmpClause &initClause);
448 void CheckAllowedRequiresClause(llvm::omp::Clause clause);
449 void AddEndDirectiveClauses(const parser::OmpClauseList &clauses);
450 void CheckTempDescriptorMappings();
451
452 void EnterDirectiveNest(const int index) { directiveNest_[index]++; }
453 void ExitDirectiveNest(const int index) { directiveNest_[index]--; }
454 int GetDirectiveNest(const int index) { return directiveNest_[index]; }
455
456 bool deviceConstructFound_{false};
457 enum directiveNestType : int {
458 ApplyNest,
459 SIMDNest,
460 TargetBlockOnlyTeams,
461 TargetNest,
462 DeclarativeNest,
463 ContextSelectorNest,
464 MetadirectiveNest,
465 LastType = MetadirectiveNest,
466 };
467 int directiveNest_[LastType + 1] = {0};
468
469 std::set<std::pair<const Symbol *, const Symbol *>> declareVariantPairs_;
470
471 int allocateDirectiveLevel_{0};
472 parser::CharBlock visitedAtomicSource_;
473
474 // Mapping of directive-name-modifier constituents to the sources of the
475 // IF clauses that referenced them. If there was no modifier, the entire
476 // directive is assumed to be listed.
477 std::map<llvm::omp::Directive, parser::CharBlock> ifLeafs_;
478
479 // Track symbols with temporary stack descriptors mapped in TARGET ENTER DATA
480 // and symbols mapped in TARGET EXIT DATA within the current function scope.
481 // Used to warn about potential issues with mapping temporary descriptors.
482 std::multimap<const Symbol *, parser::CharBlock> tempDescriptorEnterMaps_;
483 std::set<const Symbol *> tempDescriptorExitMaps_;
484
485 // Stack of nested DO loops and OpenMP constructs.
486 // This is used to verify DO loop nest for DOACROSS, and branches into
487 // and out of OpenMP constructs.
488 std::vector<LoopOrConstruct> constructStack_;
489 // Scopes for scoping units.
490 std::vector<const Scope *> scopeStack_;
491 // Stack of directive specifications (except for SECTION).
492 // This is to allow visitor functions to see all specified clauses, since
493 // they are only recorded in DirectiveContext as they are processed.
494 std::vector<const parser::OmpDirectiveSpecification *> dirStack_;
495
496 enum class PartKind : int {
497 // There are also other "parts", such as internal-subprogram-part, etc,
498 // but we're keeping track of these two for now.
499 SpecificationPart,
500 ExecutionPart,
501 };
502 std::vector<PartKind> partStack_;
503
504 struct MetadirectiveLoopVariant {
507 };
508 std::vector<MetadirectiveLoopVariant> metadirectiveLoopVariants_;
509 std::vector<std::size_t> metadirectiveVariantScopeStarts_;
510 const parser::traits::OmpContextSelectorSpecification *currentWhenSelector_{
511 nullptr};
512
513 std::multimap<const parser::Label,
514 std::pair<parser::CharBlock, const parser::OpenMPConstruct *>>
515 sourceLabels_;
516 std::map<const parser::Label,
517 std::pair<parser::CharBlock, const parser::OpenMPConstruct *>>
518 targetLabels_;
519 parser::CharBlock currentStatementSource_;
520};
521
522template <typename A>
523void OmpStructureChecker::Enter(const parser::Statement<A> &statement) {
524 currentStatementSource_ = statement.source;
525 // Keep track of the labels in all the labelled statements
526 if (statement.label) {
527 auto label{statement.label.value()};
528 // Get the context to check if the labelled statement is in an
529 // enclosing OpenMP construct
530 auto *thisConstruct{GetCurrentConstruct()};
531 targetLabels_.emplace(
532 label, std::make_pair(currentStatementSource_, thisConstruct));
533 // Check if a statement that causes a jump to the 'label'
534 // has already been encountered
535 auto range{sourceLabels_.equal_range(label)};
536 for (auto it{range.first}; it != range.second; ++it) {
537 // Check if both the statement with 'label' and the statement that
538 // causes a jump to the 'label' are in the same scope
539 CheckLabelContext(it->second.first, currentStatementSource_,
540 it->second.second, thisConstruct);
541 }
542 }
543}
544
547template <typename LessTy, typename RangeTy, typename IterTy>
548std::optional<IterTy> OmpStructureChecker::FindDuplicate(RangeTy &&range) {
549 // Deal with iterators, since the actual elements may be rvalues (i.e.
550 // have no addresses), for example with custom-constructed ranges that
551 // are not simple c.begin()..c.end().
552 std::set<IterTy, LessTy> uniq;
553 for (auto it{range.begin()}, end{range.end()}; it != end; ++it) {
554 if (!uniq.insert(it).second) {
555 return it;
556 }
557 }
558 return std::nullopt;
559}
560} // namespace Fortran::semantics
561#endif // FORTRAN_SEMANTICS_CHECK_OMP_STRUCTURE_H_
Definition enum-set.h:28
Definition expression.h:920
Definition common.h:215
Definition char-block.h:26
Definition check-directive-structure.h:208
Definition semantics.h:67
Definition FIRType.h:106
Definition parse-tree.h:3541
Definition parse-tree.h:1938
Definition parse-tree.h:3551
Definition parse-tree.h:2222
Definition parse-tree.h:3061
Definition parse-tree.h:2550
Definition parse-tree.h:1848
Definition parse-tree.h:1887
Definition parse-tree.h:2365
Definition parse-tree.h:558
Definition parse-tree.h:3181
Definition parse-tree.h:3213
Definition parse-tree.h:467
Definition parse-tree.h:2969
Definition parse-tree.h:2994
Definition parse-tree.h:2983
Definition parse-tree.h:3005
Definition parse-tree.h:591
Definition parse-tree.h:5397
Definition parse-tree.h:5186
Definition parse-tree.h:5196
Definition parse-tree.h:5155
Definition parse-tree.h:5139
Definition parse-tree.h:5324
Definition parse-tree.h:5341
Definition parse-tree.h:5316
Definition parse-tree.h:3577
Definition parse-tree.h:5162
Definition parse-tree.h:4586
Definition parse-tree.h:5350
Definition parse-tree.h:5209
Definition parse-tree.h:3632
Definition parse-tree.h:3620
Definition parse-tree.h:3676
Definition parse-tree.h:5357
Definition parse-tree.h:5363
Definition parse-tree.h:5423
Definition parse-tree.h:5428
Definition parse-tree.h:5465
Definition parse-tree.h:5549
Definition parse-tree.h:5413
Definition parse-tree.h:5476
Definition parse-tree.h:5490
Definition parse-tree.h:5506
Definition parse-tree.h:5514
Definition parse-tree.h:5567
Definition parse-tree.h:5535
Definition parse-tree.h:5281
Definition parse-tree.h:575
Definition parse-tree.h:455
Definition parse-tree.h:361
Definition parse-tree.h:3047
Definition parse-tree.h:3200
Definition parse-tree.h:4060
Definition parse-tree.h:4126
Definition parse-tree.h:4136
Definition parse-tree.h:4144
Definition parse-tree.h:4168
Definition parse-tree.h:4303
Definition parse-tree.h:3813
Definition parse-tree.h:3779
Definition parse-tree.h:3852
Definition parse-tree.h:3874
Definition check-omp-atomic.cpp:242
Definition openmp-utils.h:407