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