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