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 const parser::Name GetLoopIndex(const parser::DoConstruct *x);
324 void CheckIterationVariables(const parser::OpenMPLoopConstruct &x);
325 std::int64_t GetOrdCollapseLevel(const parser::OpenMPLoopConstruct &x);
326 void CheckAssociatedLoopConstraints(const parser::OpenMPLoopConstruct &x);
327 void CheckScanModifier(const parser::OmpClause::Reduction &x);
328 void CheckDoacross(
329 const parser::OmpDoacross &doa, llvm::omp::Clause clauseId);
330 void CheckDistLinear(const parser::OpenMPLoopConstruct &x);
331 void CheckUnrollFullTripCount(const parser::OpenMPLoopConstruct &x);
332
333 void BeginMetadirectiveVariantScope();
334 void EndMetadirectiveVariantScope();
335
336 // check-omp-variant.cpp
337 void CheckMetadirectiveVariantsWithoutLoop(std::size_t firstVariant = 0);
338 void CheckOmpDeclareVariantDirective(
340 void CheckDeclareVariantUserConditions(const parser::OmpContextSelector &);
341 const std::list<parser::OmpTraitProperty> &GetTraitPropertyList(
343 std::optional<llvm::omp::Clause> GetClauseFromProperty(
345
346 void CheckTraitSelectorList(const std::list<parser::OmpTraitSelector> &);
347 void CheckContextSelectorSpecification(const parser::OmpContextSelector &);
348 void CheckTraitSetSelector(const parser::OmpTraitSetSelector &);
349 void CheckTraitScore(const parser::OmpTraitScore &);
350 bool VerifyTraitPropertyLists(
352 void CheckTraitSelector(
354 void CheckTraitADMO(
356 void CheckTraitCondition(
358 void CheckTraitDeviceNum(
360 void CheckTraitRequires(
362 void CheckTraitSimd(
364
365 // check-omp-syntax.cpp
366 bool VerifyClauseVersion(
367 parser::OmpDirectiveName dirName, const AppliedClauseInfo &info);
368 bool VerifyClauseRequired(parser::OmpDirectiveName dirName,
369 const AppliedClauseInfo &info, llvm::omp::Version version);
370 bool VerifyClauseUnique(parser::OmpDirectiveName dirName,
371 const AppliedClauseInfo &info, llvm::omp::Version version);
372 bool VerifyClauseExclusive(parser::OmpDirectiveName dirName,
373 const AppliedClauseInfo &info, llvm::omp::Version version);
374 bool VerifyClauseUltimate(parser::OmpDirectiveName dirName,
375 const AppliedClauseInfo &info, llvm::omp::Version version);
376 bool VerifyClauseSyntax(parser::OmpDirectiveName dirName,
377 const AppliedClauseInfo &info, llvm::omp::Version version);
378 void VerifyClauseSyntax(const parser::OmpDirectiveSpecification *beginSpec,
380
381 bool VerifyModifierVersion(parser::omp::WithSource<llvm::omp::Clause> clause,
382 const AppliedModifierInfo &info);
383 bool VerifyModifierRequired(parser::omp::WithSource<llvm::omp::Clause> clause,
384 const AppliedModifierInfo &info, llvm::omp::Version version);
385 bool VerifyModifierUnique(parser::omp::WithSource<llvm::omp::Clause> clause,
386 const AppliedModifierInfo &info, llvm::omp::Version version);
387 bool VerifyModifierExclusive(
389 const AppliedModifierInfo &info, llvm::omp::Version version);
390 bool VerifyModifierUltimate(parser::omp::WithSource<llvm::omp::Clause> clause,
391 const AppliedModifierInfo &info, llvm::omp::Version version);
392 bool VerifyModifierSyntax(parser::omp::WithSource<llvm::omp::Clause> clause,
393 const AppliedModifierInfo &info, llvm::omp::Version version);
394 void VerifyModifierSyntax(const parser::OmpClause &x);
395
396 void SetAllowedClauseOverride(llvm::omp::Clause clauseId,
397 llvm::omp::Directive dirId,
398 llvm::omp::Version since = llvm::omp::Version());
399
400 // check-omp-structure.cpp
401 using ClauseIterator =
402 decltype(std::declval<const parser::OmpClauseList>().v.begin());
403 bool IsAllowedClause(llvm::omp::Clause clauseId);
404 void CheckArgumentObjectKind(const parser::OmpClause &x);
405 void CheckDirectiveSpelling(
406 parser::CharBlock spelling, llvm::omp::Directive id);
407 void CheckCompoundDirective(
408 const parser::OmpDirectiveSpecification *beginSpec,
409 const parser::OmpDirectiveSpecification *endSpec);
410 void CheckDirectiveDeprecation(const parser::OpenMPConstruct &x);
411 void CheckDirectivePureSince(parser::CharBlock source,
412 llvm::omp::Directive id, const char *where,
414 void CheckDirectiveInPureProcedure(parser::CharBlock source,
415 llvm::omp::Directive id, const parser::OmpDirectiveSpecification &spec);
416 void CheckDirectiveInDoConcurrent(parser::CharBlock source,
417 llvm::omp::Directive id, const parser::OmpDirectiveSpecification &spec);
418 void AddClauses(llvm::iterator_range<ClauseIterator> beginClauses,
419 llvm::iterator_range<ClauseIterator> endClauses);
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
549 void EnterDirectiveNest(const int index) { directiveNest_[index]++; }
550 void ExitDirectiveNest(const int index) { directiveNest_[index]--; }
551 int GetDirectiveNest(const int index) { return directiveNest_[index]; }
552
553 bool deviceConstructFound_{false};
554 enum directiveNestType : int {
555 ApplyNest,
556 SIMDNest,
557 TargetBlockOnlyTeams,
558 TargetNest,
559 DeclarativeNest,
560 ContextSelectorNest,
561 MetadirectiveNest,
562 LastType = MetadirectiveNest,
563 };
564 int directiveNest_[LastType + 1] = {0};
565
566 std::set<std::pair<const Symbol *, const Symbol *>> declareVariantPairs_;
567 // For each variant procedure: its construct-selector-set as an ordered list
568 // of elements (a leaf construct directive plus a normalized rendering of any
569 // properties), and the source of the DECLARE VARIANT directive that first
570 // established the set.
571 std::map<const Symbol *,
572 std::pair<
575 declareVariantConstructSets_;
576 // Tracks the procedures that appear as a base in DECLARE VARIANT.
577 std::set<const Symbol *> declareVariantBases_;
578
579 int allocateDirectiveLevel_{0};
580 parser::CharBlock visitedAtomicSource_;
581
582 // Mapping of directive-name-modifier constituents to the sources of the
583 // IF clauses that referenced them. If there was no modifier, the entire
584 // directive is assumed to be listed.
585 std::map<llvm::omp::Directive, parser::CharBlock> ifLeafs_;
586
587 // Track symbols with temporary stack descriptors mapped in TARGET ENTER DATA
588 // and symbols mapped in TARGET EXIT DATA within the current function scope.
589 // Used to warn about potential issues with mapping temporary descriptors.
590 std::multimap<const Symbol *, parser::CharBlock> tempDescriptorEnterMaps_;
591 std::set<const Symbol *> tempDescriptorExitMaps_;
592
593 // Stack of nested DO loops and OpenMP constructs.
594 // This is used to verify DO loop nest for DOACROSS, and branches into
595 // and out of OpenMP constructs.
596 std::vector<LoopOrConstruct> constructStack_;
597 // Scopes for scoping units.
598 std::vector<const Scope *> scopeStack_;
599 // Stack of directive specifications (except for SECTION).
600 // This is to allow visitor functions to see all specified clauses, since
601 // they are only recorded in DirectiveContext as they are processed.
602 std::vector<const parser::OmpDirectiveSpecification *> dirStack_;
603
604 enum class PartKind : int {
605 // There are also other "parts", such as internal-subprogram-part, etc,
606 // but we're keeping track of these two for now.
607 SpecificationPart,
608 ExecutionPart,
609 };
610 std::vector<PartKind> partStack_;
611
612 struct MetadirectiveLoopVariant {
615 bool checkDefaultNoneInAssociatedLoop;
616 };
617 std::vector<MetadirectiveLoopVariant> metadirectiveLoopVariants_;
618 std::vector<std::size_t> metadirectiveVariantScopeStarts_;
619 const parser::traits::OmpContextSelectorSpecification *currentWhenSelector_{
620 nullptr};
621
622 std::multimap<const parser::Label,
623 std::pair<parser::CharBlock, const parser::OpenMPConstruct *>>
624 sourceLabels_;
625 std::map<const parser::Label,
626 std::pair<parser::CharBlock, const parser::OpenMPConstruct *>>
627 targetLabels_;
628 parser::CharBlock currentStatementSource_;
629};
630
631template <typename A>
632void OmpStructureChecker::Enter(const parser::Statement<A> &statement) {
633 currentStatementSource_ = statement.source;
634 // Keep track of the labels in all the labelled statements
635 if (statement.label) {
636 auto label{statement.label.value()};
637 // Get the context to check if the labelled statement is in an
638 // enclosing OpenMP construct
639 auto *thisConstruct{GetCurrentConstruct()};
640 targetLabels_.emplace(
641 label, std::make_pair(currentStatementSource_, thisConstruct));
642 // Check if a statement that causes a jump to the 'label'
643 // has already been encountered
644 auto range{sourceLabels_.equal_range(label)};
645 for (auto it{range.first}; it != range.second; ++it) {
646 // Check if both the statement with 'label' and the statement that
647 // causes a jump to the 'label' are in the same scope
648 CheckLabelContext(it->second.first, currentStatementSource_,
649 it->second.second, thisConstruct);
650 }
651 }
652}
653
656template <typename LessTy, typename RangeTy, typename IterTy>
657std::optional<IterTy> OmpStructureChecker::FindDuplicate(RangeTy &&range) {
658 // Deal with iterators, since the actual elements may be rvalues (i.e.
659 // have no addresses), for example with custom-constructed ranges that
660 // are not simple c.begin()..c.end().
661 std::set<IterTy, LessTy> uniq;
662 for (auto it{range.begin()}, end{range.end()}; it != end; ++it) {
663 if (!uniq.insert(it).second) {
664 return it;
665 }
666 }
667 return std::nullopt;
668}
669} // namespace Fortran::semantics
670#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:930
Definition FIRType.h:106
Definition OpenACC.h:20
Definition parse-tree.h:3555
Definition parse-tree.h:1952
Definition parse-tree.h:3565
Definition parse-tree.h:2236
Definition parse-tree.h:3075
Definition parse-tree.h:2564
Definition parse-tree.h:1862
Definition parse-tree.h:1901
Definition parse-tree.h:2379
Definition parse-tree.h:559
Definition parse-tree.h:3195
Definition parse-tree.h:3227
Definition parse-tree.h:468
Definition parse-tree.h:2983
Definition parse-tree.h:3008
Definition parse-tree.h:2997
Definition parse-tree.h:3019
Definition parse-tree.h:592
Definition parse-tree.h:5532
Definition parse-tree.h:5379
Definition parse-tree.h:5321
Definition parse-tree.h:5331
Definition parse-tree.h:5290
Definition parse-tree.h:5274
Definition parse-tree.h:5459
Definition parse-tree.h:5476
Definition parse-tree.h:5451
Definition parse-tree.h:3591
Definition parse-tree.h:5297
Definition parse-tree.h:4682
Definition parse-tree.h:5364
Definition parse-tree.h:5485
Definition parse-tree.h:5344
Definition parse-tree.h:3646
Definition parse-tree.h:3634
Definition parse-tree.h:3690
Definition parse-tree.h:5492
Definition parse-tree.h:5498
Definition parse-tree.h:5558
Definition parse-tree.h:5563
Definition parse-tree.h:5600
Definition parse-tree.h:5684
Definition parse-tree.h:5548
Definition parse-tree.h:5611
Definition parse-tree.h:5625
Definition parse-tree.h:5641
Definition parse-tree.h:5649
Definition parse-tree.h:5708
Definition parse-tree.h:5670
Definition parse-tree.h:5416
Definition parse-tree.h:576
Definition parse-tree.h:456
Definition parse-tree.h:362
Definition parse-tree.h:3061
Definition parse-tree.h:3214
Definition parse-tree.h:4101
Definition parse-tree.h:4168
Definition parse-tree.h:4178
Definition parse-tree.h:4187
Definition parse-tree.h:4222
Definition parse-tree.h:4385
Definition parse-tree.h:4447
Definition openmp-utils.h:114
Definition parse-tree.h:3828
Definition parse-tree.h:3794
Definition parse-tree.h:3867
Definition parse-tree.h:3889
Definition check-omp-atomic.cpp:242
Definition check-omp-structure.h:64
Definition check-omp-structure.h:58
Definition openmp-utils.h:494