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 std::pair<const parser::OmpClause *, const parser::OmpClause *>
400 FindMutuallyExclusiveClauses(llvm::omp::Clauses exclusive,
401 const std::vector<const parser::OmpClause *> &clauses);
402
403 const parser::OpenMPConstruct *GetCurrentConstruct() const;
404 void CheckSourceLabel(const parser::Label &);
405 void CheckLabelContext(const parser::CharBlock, const parser::CharBlock,
407 void ClearLabels();
408 void CheckMultipleOccurrence(semantics::UnorderedSymbolSet &listVars,
409 const std::list<parser::Name> &nameList, const parser::CharBlock &item,
410 const std::string &clauseName);
411 void CheckMultListItems();
412 void CheckStructureComponent(
413 const parser::OmpObject &object, llvm::omp::Clause clauseId);
414 void CheckStructureComponent(
415 const parser::OmpObjectList &objects, llvm::omp::Clause clauseId);
416 bool HasInvalidWorksharingNesting(
417 const parser::OmpDirectiveName &name, const llvm::omp::Directives &);
418
419 bool IsCloselyNestedRegion(const llvm::omp::Directives &set);
420 bool IsNestedInDirective(llvm::omp::Directive directive);
421 bool IsCombinedParallelWorksharing(llvm::omp::Directive directive) const;
422 bool InTargetRegion();
423 void HasInvalidTeamsNesting(
424 const llvm::omp::Directive &dir, const parser::CharBlock &source);
425 bool HasRequires(llvm::omp::Clause req);
426 void CheckAllowedMapTypes(
427 parser::OmpMapType::Value, llvm::ArrayRef<parser::OmpMapType::Value>);
428 void CheckCloseModifierOnMapMembers();
429
430 llvm::StringRef getClauseName(llvm::omp::Clause clause) override;
431 llvm::StringRef getDirectiveName(llvm::omp::Directive directive) override;
432
433 template < //
434 typename LessTy, typename RangeTy,
435 typename IterTy = decltype(std::declval<RangeTy>().begin())>
436 std::optional<IterTy> FindDuplicate(RangeTy &&);
437
438 void CheckDependList(const parser::DataRef &);
439 void CheckDimsModifier(parser::CharBlock source, size_t numValues,
440 const parser::OmpDimsModifier &x);
441 void CheckTypeParamInquiry(const parser::CharBlock &source,
442 const parser::OmpObject &object, llvm::omp::Directive dirId);
443 void CheckTypeParamInquiry(const parser::CharBlock &source,
444 const parser::OmpObject &object, llvm::omp::Clause clauseId);
445 void CheckVarIsNotPartOfAnotherVar(const parser::CharBlock &source,
446 const parser::OmpObject &object, llvm::StringRef clause = "",
447 bool checkSubstrings = true);
448 void CheckVarIsNotPartOfAnotherVar(const parser::CharBlock &source,
449 const parser::OmpObjectList &objList, llvm::StringRef clause = "",
450 bool checkSubstrings = true);
451 void CheckThreadprivateOrDeclareTargetVar(const parser::Designator &);
452 void CheckThreadprivateOrDeclareTargetVar(const parser::Name &);
453 void CheckThreadprivateOrDeclareTargetVar(const parser::OmpObject &);
454 void CheckThreadprivateOrDeclareTargetVar(const parser::OmpObjectList &);
455 void CheckIntentInPointer(SymbolSourceMap &, const llvm::omp::Clause);
456 void CheckProcedurePointer(SymbolSourceMap &, const llvm::omp::Clause);
457 void CheckCrayPointee(const parser::OmpObjectList &objectList,
458 llvm::StringRef clause, bool suggestToUseCrayPointer = true);
459 void GetSymbolsInObjectList(const parser::OmpObjectList &, SymbolSourceMap &);
460 void CheckDefaultNoneInAssociatedLoop(
462 UnorderedSymbolSet &diagnosed);
463 void CheckDefinableObjects(SymbolSourceMap &, const llvm::omp::Clause);
464 void CheckCopyingPolymorphicAllocatable(
465 SymbolSourceMap &, const llvm::omp::Clause);
466 void CheckPrivateSymbolsInOuterCxt(
467 SymbolSourceMap &, DirectivesClauseTriple &, const llvm::omp::Clause);
468 bool CheckTargetBlockOnlyTeams(const parser::Block &);
469 void CheckWorkshareBlockStmts(const parser::Block &, parser::CharBlock);
470 void CheckWorkdistributeBlockStmts(const parser::Block &, parser::CharBlock);
471 void CheckIndividualAllocateDirective(
472 const parser::OmpAllocateDirective &x, bool isExecutable);
473 void CheckExecutableAllocateDirective(const parser::OmpAllocateDirective &x);
474
475 void CheckUsesAllocatorsSpec(
477 void CheckUsesAllocatorsTraits(
478 const parser::OmpTraitsArray &traits, parser::CharBlock source);
479
480 void CheckIteratorRange(const parser::OmpIteratorSpecifier &x);
481 void CheckIteratorModifier(const parser::OmpIterator &x);
482
483 void CheckTargetNest(const parser::OpenMPConstruct &x);
484 void CheckTargetUpdate();
485 void CheckTaskgraph(const parser::OmpBlockConstruct &x);
486 void CheckTaskDependenceType(const parser::OmpTaskDependenceType::Value &x);
487 std::optional<llvm::omp::Directive> GetCancelType(
488 llvm::omp::Directive cancelDir, const parser::CharBlock &cancelSource,
489 const std::optional<parser::OmpClauseList> &maybeClauses);
490 void CheckCancellationNest(
491 const parser::CharBlock &source, llvm::omp::Directive type);
492 void CheckReductionObjects(
493 const parser::OmpObjectList &objects, llvm::omp::Clause clauseId);
494 void CheckSubstring(
495 const parser::OmpObjectList &objects, llvm::omp::Clause clauseId);
496 bool CheckReductionOperator(const parser::OmpReductionIdentifier &ident,
497 parser::CharBlock source, llvm::omp::Clause clauseId);
498 void CheckReductionObjectTypes(const parser::OmpObjectList &objects,
499 const parser::OmpReductionIdentifier &ident);
500 void CheckReductionModifier(const parser::OmpReductionModifier &);
501 void CheckLastprivateModifier(const parser::OmpLastprivateModifier &);
502 void CheckSingleConstruct(const parser::OmpBlockConstruct &x);
503 void CheckMasterNesting(const parser::OmpBlockConstruct &x);
504 void ChecksOnOrderedAsBlock();
505 void CheckBarrierNesting(const parser::OpenMPSimpleStandaloneConstruct &x);
506 void CheckScan(const parser::OpenMPSimpleStandaloneConstruct &x);
507 void ChecksOnOrderedAsStandalone();
508 void CheckOrderedDependClause(std::optional<std::int64_t> orderedValue);
509 void CheckReductionArraySection(
510 const parser::OmpObjectList &ompObjectList, llvm::omp::Clause clauseId);
511 void CheckArraySection(const parser::ArrayElement &arrayElement,
512 const parser::Name &name, const llvm::omp::Clause clause);
513 void CheckArraySectionStrides(const parser::DataRef &, llvm::omp::Clause);
514 void CheckLastPartRefForArraySection(
515 const parser::Designator &designator, llvm::omp::Clause clauseId);
516 void CheckSharedBindingInOuterContext(
517 const parser::OmpObjectList &ompObjectList);
518 void CheckIfContiguous(const parser::OmpObject &object);
519 const parser::Name *GetObjectName(const parser::OmpObject &object);
520 void CheckInitOnDepobj(const parser::OpenMPDepobjConstruct &depobj,
521 const parser::OmpClause &initClause);
522 void CheckAllowedRequiresClause(llvm::omp::Clause clause);
523 void AddEndDirectiveClauses(const parser::OmpClauseList &clauses);
524 void CheckTempDescriptorMappings();
525
526 void EnterDirectiveNest(const int index) { directiveNest_[index]++; }
527 void ExitDirectiveNest(const int index) { directiveNest_[index]--; }
528 int GetDirectiveNest(const int index) { return directiveNest_[index]; }
529
530 bool deviceConstructFound_{false};
531 enum directiveNestType : int {
532 ApplyNest,
533 SIMDNest,
534 TargetBlockOnlyTeams,
535 TargetNest,
536 DeclarativeNest,
537 ContextSelectorNest,
538 MetadirectiveNest,
539 LastType = MetadirectiveNest,
540 };
541 int directiveNest_[LastType + 1] = {0};
542
543 std::set<std::pair<const Symbol *, const Symbol *>> declareVariantPairs_;
544 // For each variant procedure: its construct-selector-set as an ordered list
545 // of elements (a leaf construct directive plus a normalized rendering of any
546 // properties), and the source of the DECLARE VARIANT directive that first
547 // established the set.
548 std::map<const Symbol *,
549 std::pair<
552 declareVariantConstructSets_;
553 // Tracks the procedures that appear as a base in DECLARE VARIANT.
554 std::set<const Symbol *> declareVariantBases_;
555
556 int allocateDirectiveLevel_{0};
557 parser::CharBlock visitedAtomicSource_;
558
559 // Mapping of directive-name-modifier constituents to the sources of the
560 // IF clauses that referenced them. If there was no modifier, the entire
561 // directive is assumed to be listed.
562 std::map<llvm::omp::Directive, parser::CharBlock> ifLeafs_;
563
564 // Track symbols with temporary stack descriptors mapped in TARGET ENTER DATA
565 // and symbols mapped in TARGET EXIT DATA within the current function scope.
566 // Used to warn about potential issues with mapping temporary descriptors.
567 std::multimap<const Symbol *, parser::CharBlock> tempDescriptorEnterMaps_;
568 std::set<const Symbol *> tempDescriptorExitMaps_;
569
570 // Stack of nested DO loops and OpenMP constructs.
571 // This is used to verify DO loop nest for DOACROSS, and branches into
572 // and out of OpenMP constructs.
573 std::vector<LoopOrConstruct> constructStack_;
574 // Scopes for scoping units.
575 std::vector<const Scope *> scopeStack_;
576 // Stack of directive specifications (except for SECTION).
577 // This is to allow visitor functions to see all specified clauses, since
578 // they are only recorded in DirectiveContext as they are processed.
579 std::vector<const parser::OmpDirectiveSpecification *> dirStack_;
580
581 enum class PartKind : int {
582 // There are also other "parts", such as internal-subprogram-part, etc,
583 // but we're keeping track of these two for now.
584 SpecificationPart,
585 ExecutionPart,
586 };
587 std::vector<PartKind> partStack_;
588
589 struct MetadirectiveLoopVariant {
592 bool checkDefaultNoneInAssociatedLoop;
593 };
594 std::vector<MetadirectiveLoopVariant> metadirectiveLoopVariants_;
595 std::vector<std::size_t> metadirectiveVariantScopeStarts_;
596 const parser::traits::OmpContextSelectorSpecification *currentWhenSelector_{
597 nullptr};
598
599 std::multimap<const parser::Label,
600 std::pair<parser::CharBlock, const parser::OpenMPConstruct *>>
601 sourceLabels_;
602 std::map<const parser::Label,
603 std::pair<parser::CharBlock, const parser::OpenMPConstruct *>>
604 targetLabels_;
605 parser::CharBlock currentStatementSource_;
606};
607
608template <typename A>
609void OmpStructureChecker::Enter(const parser::Statement<A> &statement) {
610 currentStatementSource_ = statement.source;
611 // Keep track of the labels in all the labelled statements
612 if (statement.label) {
613 auto label{statement.label.value()};
614 // Get the context to check if the labelled statement is in an
615 // enclosing OpenMP construct
616 auto *thisConstruct{GetCurrentConstruct()};
617 targetLabels_.emplace(
618 label, std::make_pair(currentStatementSource_, thisConstruct));
619 // Check if a statement that causes a jump to the 'label'
620 // has already been encountered
621 auto range{sourceLabels_.equal_range(label)};
622 for (auto it{range.first}; it != range.second; ++it) {
623 // Check if both the statement with 'label' and the statement that
624 // causes a jump to the 'label' are in the same scope
625 CheckLabelContext(it->second.first, currentStatementSource_,
626 it->second.second, thisConstruct);
627 }
628 }
629}
630
633template <typename LessTy, typename RangeTy, typename IterTy>
634std::optional<IterTy> OmpStructureChecker::FindDuplicate(RangeTy &&range) {
635 // Deal with iterators, since the actual elements may be rvalues (i.e.
636 // have no addresses), for example with custom-constructed ranges that
637 // are not simple c.begin()..c.end().
638 std::set<IterTy, LessTy> uniq;
639 for (auto it{range.begin()}, end{range.end()}; it != end; ++it) {
640 if (!uniq.insert(it).second) {
641 return it;
642 }
643 }
644 return std::nullopt;
645}
646} // namespace Fortran::semantics
647#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:485