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