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