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