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#include "flang/Common/enum-set.h"
19#include "flang/Parser/parse-tree.h"
20#include "flang/Semantics/openmp-directive-sets.h"
21#include "flang/Semantics/semantics.h"
22
23using OmpClauseSet =
25
26#define GEN_FLANG_DIRECTIVE_CLAUSE_SETS
27#include "llvm/Frontend/OpenMP/OMP.inc"
28
29namespace llvm {
30namespace omp {
31static OmpClauseSet privateSet{
32 Clause::OMPC_private, Clause::OMPC_firstprivate, Clause::OMPC_lastprivate};
33static OmpClauseSet privateReductionSet{
34 OmpClauseSet{Clause::OMPC_reduction} | privateSet};
35// omp.td cannot differentiate allowed/not allowed clause list for few
36// directives for fortran. nowait is not allowed on begin directive clause list
37// for below list of directives. Directives with conflicting list of clauses are
38// included in below list.
39static const OmpDirectiveSet noWaitClauseNotAllowedSet{
40 Directive::OMPD_do,
41 Directive::OMPD_do_simd,
42 Directive::OMPD_sections,
43 Directive::OMPD_single,
44 Directive::OMPD_workshare,
45};
46} // namespace omp
47} // namespace llvm
48
49namespace Fortran::semantics {
50struct AnalyzedCondStmt;
51
52// Mapping from 'Symbol' to 'Source' to keep track of the variables
53// used in multiple clauses
54using SymbolSourceMap = std::multimap<const Symbol *, parser::CharBlock>;
55// Multimap to check the triple <current_dir, enclosing_dir, enclosing_clause>
56using DirectivesClauseTriple = std::multimap<llvm::omp::Directive,
57 std::pair<llvm::omp::Directive, const OmpClauseSet>>;
58
59using OmpStructureCheckerBase = DirectiveStructureChecker<llvm::omp::Directive,
60 llvm::omp::Clause, parser::OmpClause, llvm::omp::Clause_enumSize>;
61
62class OmpStructureChecker : public OmpStructureCheckerBase {
63public:
64 using Base = OmpStructureCheckerBase;
65
66 OmpStructureChecker(SemanticsContext &context);
67
68 using llvmOmpClause = const llvm::omp::Clause;
69
70 bool Enter(const parser::MainProgram &);
71 void Leave(const parser::MainProgram &);
72 bool Enter(const parser::BlockData &);
73 void Leave(const parser::BlockData &);
74 bool Enter(const parser::Module &);
75 void Leave(const parser::Module &);
76 bool Enter(const parser::Submodule &);
77 void Leave(const parser::Submodule &);
78 bool Enter(const parser::SubroutineStmt &);
79 bool Enter(const parser::EndSubroutineStmt &);
80 bool Enter(const parser::FunctionStmt &);
81 bool Enter(const parser::EndFunctionStmt &);
82 bool Enter(const parser::BlockConstruct &);
83 void Leave(const parser::BlockConstruct &);
84
85 void Enter(const parser::SpecificationPart &);
86 void Leave(const parser::SpecificationPart &);
87 void Enter(const parser::ExecutionPart &);
88 void Leave(const parser::ExecutionPart &);
89
90 void Enter(const parser::OpenMPConstruct &);
91 void Leave(const parser::OpenMPConstruct &);
92 void Enter(const parser::OpenMPInteropConstruct &);
93 void Leave(const parser::OpenMPInteropConstruct &);
94 void Enter(const parser::OpenMPDeclarativeConstruct &);
95 void Leave(const parser::OpenMPDeclarativeConstruct &);
96
97 void Enter(const parser::OpenMPMisplacedEndDirective &);
98 void Leave(const parser::OpenMPMisplacedEndDirective &);
99 void Enter(const parser::OpenMPInvalidDirective &);
100 void Leave(const parser::OpenMPInvalidDirective &);
101
102 void Enter(const parser::OpenMPLoopConstruct &);
103 void Leave(const parser::OpenMPLoopConstruct &);
104 void Enter(const parser::OmpEndLoopDirective &);
105 void Leave(const parser::OmpEndLoopDirective &);
106
107 void Enter(const parser::OpenMPAssumeConstruct &);
108 void Leave(const parser::OpenMPAssumeConstruct &);
109 void Enter(const parser::OpenMPDeclarativeAssumes &);
110 void Leave(const parser::OpenMPDeclarativeAssumes &);
111 void Enter(const parser::OmpBlockConstruct &);
112 void Leave(const parser::OmpBlockConstruct &);
113 void Leave(const parser::OmpBeginDirective &);
114 void Enter(const parser::OmpEndDirective &);
115 void Leave(const parser::OmpEndDirective &);
116
117 void Enter(const parser::OpenMPSectionsConstruct &);
118 void Leave(const parser::OpenMPSectionsConstruct &);
119 void Enter(const parser::OmpEndSectionsDirective &);
120 void Leave(const parser::OmpEndSectionsDirective &);
121
122 void Enter(const parser::OmpDeclareVariantDirective &);
123 void Leave(const parser::OmpDeclareVariantDirective &);
124 void Enter(const parser::OpenMPDeclareSimdConstruct &);
125 void Leave(const parser::OpenMPDeclareSimdConstruct &);
126 void Enter(const parser::OmpAllocateDirective &);
127 void Leave(const parser::OmpAllocateDirective &);
128 void Enter(const parser::OpenMPDeclareMapperConstruct &);
129 void Leave(const parser::OpenMPDeclareMapperConstruct &);
130 void Enter(const parser::OpenMPDeclareReductionConstruct &);
131 void Leave(const parser::OpenMPDeclareReductionConstruct &);
132 void Enter(const parser::OpenMPDeclareTargetConstruct &);
133 void Leave(const parser::OpenMPDeclareTargetConstruct &);
134 void Enter(const parser::OpenMPDepobjConstruct &);
135 void Leave(const parser::OpenMPDepobjConstruct &);
136 void Enter(const parser::OpenMPDispatchConstruct &);
137 void Leave(const parser::OpenMPDispatchConstruct &);
138 void Enter(const parser::OmpErrorDirective &);
139 void Leave(const parser::OmpErrorDirective &);
140 void Enter(const parser::OmpNothingDirective &);
141 void Leave(const parser::OmpNothingDirective &);
142 void Enter(const parser::OpenMPAllocatorsConstruct &);
143 void Leave(const parser::OpenMPAllocatorsConstruct &);
144 void Enter(const parser::OpenMPRequiresConstruct &);
145 void Leave(const parser::OpenMPRequiresConstruct &);
146 void Enter(const parser::OpenMPGroupprivate &);
147 void Leave(const parser::OpenMPGroupprivate &);
148 void Enter(const parser::OpenMPThreadprivate &);
149 void Leave(const parser::OpenMPThreadprivate &);
150
151 void Enter(const parser::OpenMPSimpleStandaloneConstruct &);
152 void Leave(const parser::OpenMPSimpleStandaloneConstruct &);
153 void Enter(const parser::OpenMPFlushConstruct &);
154 void Leave(const parser::OpenMPFlushConstruct &);
155 void Enter(const parser::OpenMPCancelConstruct &);
156 void Leave(const parser::OpenMPCancelConstruct &);
159 void Enter(const parser::OpenMPCriticalConstruct &);
160 void Leave(const parser::OpenMPCriticalConstruct &);
161 void Enter(const parser::OpenMPAtomicConstruct &);
162 void Leave(const parser::OpenMPAtomicConstruct &);
163
164 void Leave(const parser::OmpClauseList &);
165 void Enter(const parser::OmpClause &);
166
167 void Enter(const parser::DoConstruct &);
168 void Leave(const parser::DoConstruct &);
169
170 void Enter(const parser::OmpDirectiveSpecification &);
171 void Leave(const parser::OmpDirectiveSpecification &);
172
173 void Enter(const parser::OmpMetadirectiveDirective &);
174 void Leave(const parser::OmpMetadirectiveDirective &);
175
176 void Enter(const parser::OmpContextSelector &);
177 void Leave(const parser::OmpContextSelector &);
178
179#define GEN_FLANG_CLAUSE_CHECK_ENTER
180#include "llvm/Frontend/OpenMP/OMP.inc"
181
182private:
183 bool CheckAllowedClause(llvmOmpClause clause);
184 void CheckVariableListItem(const SymbolSourceMap &symbols);
185 void CheckDirectiveSpelling(
186 parser::CharBlock spelling, llvm::omp::Directive id);
187 void AnalyzeObject(const parser::OmpObject &object);
188 void AnalyzeObjects(const parser::OmpObjectList &objects);
189 void CheckMultipleOccurrence(semantics::UnorderedSymbolSet &listVars,
190 const std::list<parser::Name> &nameList, const parser::CharBlock &item,
191 const std::string &clauseName);
192 void CheckMultListItems();
193 void CheckStructureComponent(
194 const parser::OmpObjectList &objects, llvm::omp::Clause clauseId);
195 bool HasInvalidWorksharingNesting(
196 const parser::CharBlock &, const OmpDirectiveSet &);
197 bool IsCloselyNestedRegion(const OmpDirectiveSet &set);
198 bool IsNestedInDirective(llvm::omp::Directive directive);
199 bool InTargetRegion();
200 void HasInvalidTeamsNesting(
201 const llvm::omp::Directive &dir, const parser::CharBlock &source);
202 void HasInvalidDistributeNesting(const parser::OpenMPLoopConstruct &x);
203 void HasInvalidLoopBinding(const parser::OpenMPLoopConstruct &x);
204 bool HasRequires(llvm::omp::Clause req);
205 // specific clause related
206 void CheckAllowedMapTypes(
207 parser::OmpMapType::Value, llvm::ArrayRef<parser::OmpMapType::Value>);
208
209 const std::list<parser::OmpTraitProperty> &GetTraitPropertyList(
211 std::optional<llvm::omp::Clause> GetClauseFromProperty(
213
214 void CheckTraitSelectorList(const std::list<parser::OmpTraitSelector> &);
215 void CheckTraitSetSelector(const parser::OmpTraitSetSelector &);
216 void CheckTraitScore(const parser::OmpTraitScore &);
217 bool VerifyTraitPropertyLists(
219 void CheckTraitSelector(
221 void CheckTraitADMO(
223 void CheckTraitCondition(
225 void CheckTraitDeviceNum(
227 void CheckTraitRequires(
229 void CheckTraitSimd(
231
232 llvm::StringRef getClauseName(llvm::omp::Clause clause) override;
233 llvm::StringRef getDirectiveName(llvm::omp::Directive directive) override;
234
235 template < //
236 typename LessTy, typename RangeTy,
237 typename IterTy = decltype(std::declval<RangeTy>().begin())>
238 std::optional<IterTy> FindDuplicate(RangeTy &&);
239
240 void CheckDependList(const parser::DataRef &);
241 void CheckDependArraySection(
243 void CheckDoacross(const parser::OmpDoacross &doa);
244 void CheckDimsModifier(parser::CharBlock source, size_t numValues,
245 const parser::OmpDimsModifier &x);
246 bool IsDataRefTypeParamInquiry(const parser::DataRef *dataRef);
247 void CheckVarIsNotPartOfAnotherVar(const parser::CharBlock &source,
248 const parser::OmpObject &obj, llvm::StringRef clause = "");
249 void CheckVarIsNotPartOfAnotherVar(const parser::CharBlock &source,
250 const parser::OmpObjectList &objList, llvm::StringRef clause = "");
251 void CheckThreadprivateOrDeclareTargetVar(const parser::Designator &);
252 void CheckThreadprivateOrDeclareTargetVar(const parser::Name &);
253 void CheckThreadprivateOrDeclareTargetVar(const parser::OmpObject &);
254 void CheckThreadprivateOrDeclareTargetVar(const parser::OmpObjectList &);
255 void CheckSymbolName(
256 const parser::CharBlock &source, const parser::OmpObject &object);
257 void CheckSymbolNames(
258 const parser::CharBlock &source, const parser::OmpObjectList &objList);
259 void CheckIntentInPointer(SymbolSourceMap &, const llvm::omp::Clause);
260 void CheckProcedurePointer(SymbolSourceMap &, const llvm::omp::Clause);
261 void CheckCrayPointee(const parser::OmpObjectList &objectList,
262 llvm::StringRef clause, bool suggestToUseCrayPointer = true);
263 void GetSymbolsInObjectList(const parser::OmpObjectList &, SymbolSourceMap &);
264 void CheckDefinableObjects(SymbolSourceMap &, const llvm::omp::Clause);
265 void CheckCopyingPolymorphicAllocatable(
266 SymbolSourceMap &, const llvm::omp::Clause);
267 void CheckPrivateSymbolsInOuterCxt(
268 SymbolSourceMap &, DirectivesClauseTriple &, const llvm::omp::Clause);
269 const parser::Name GetLoopIndex(const parser::DoConstruct *x);
270 void SetLoopInfo(const parser::OpenMPLoopConstruct &x);
271 void CheckIsLoopIvPartOfClause(
272 llvmOmpClause clause, const parser::OmpObjectList &ompObjectList);
273 bool CheckTargetBlockOnlyTeams(const parser::Block &);
274 void CheckWorkshareBlockStmts(const parser::Block &, parser::CharBlock);
275 void CheckWorkdistributeBlockStmts(const parser::Block &, parser::CharBlock);
276 void CheckIndividualAllocateDirective(
277 const parser::OmpAllocateDirective &x, bool isExecutable);
278 void CheckExecutableAllocateDirective(const parser::OmpAllocateDirective &x);
279
280 void CheckIteratorRange(const parser::OmpIteratorSpecifier &x);
281 void CheckIteratorModifier(const parser::OmpIterator &x);
282 void CheckLoopItrVariableIsInt(const parser::OpenMPLoopConstruct &x);
283 void CheckDoWhile(const parser::OpenMPLoopConstruct &x);
284 void CheckAssociatedLoopConstraints(const parser::OpenMPLoopConstruct &x);
285 template <typename T, typename D> bool IsOperatorValid(const T &, const D &);
286
287 void CheckStorageOverlap(const evaluate::Expr<evaluate::SomeType> &,
289 void ErrorShouldBeVariable(const MaybeExpr &expr, parser::CharBlock source);
290 void CheckAtomicType(SymbolRef sym, parser::CharBlock source,
291 std::string_view name, bool checkTypeOnPointer = true);
292 void CheckAtomicVariable(const evaluate::Expr<evaluate::SomeType> &,
293 parser::CharBlock, bool checkTypeOnPointer = true);
294 std::pair<const parser::ExecutionPartConstruct *,
296 CheckUpdateCapture(const parser::ExecutionPartConstruct *ec1,
298 void CheckAtomicCaptureAssignment(const evaluate::Assignment &capture,
299 const SomeExpr &atom, parser::CharBlock source);
300 void CheckAtomicReadAssignment(
301 const evaluate::Assignment &read, parser::CharBlock source);
302 void CheckAtomicWriteAssignment(
303 const evaluate::Assignment &write, parser::CharBlock source);
304 std::optional<evaluate::Assignment> CheckAtomicUpdateAssignment(
305 const evaluate::Assignment &update, parser::CharBlock source);
306 std::pair<bool, bool> CheckAtomicUpdateAssignmentRhs(const SomeExpr &atom,
307 const SomeExpr &rhs, parser::CharBlock source, bool suppressDiagnostics);
308 void CheckAtomicConditionalUpdateAssignment(const SomeExpr &cond,
309 parser::CharBlock condSource, const evaluate::Assignment &assign,
310 parser::CharBlock assignSource);
311 void CheckAtomicConditionalUpdateStmt(
312 const AnalyzedCondStmt &update, parser::CharBlock source);
313 void CheckAtomicUpdateOnly(const parser::OpenMPAtomicConstruct &x,
314 const parser::Block &body, parser::CharBlock source);
315 void CheckAtomicConditionalUpdate(const parser::OpenMPAtomicConstruct &x,
316 const parser::Block &body, parser::CharBlock source);
317 void CheckAtomicUpdateCapture(const parser::OpenMPAtomicConstruct &x,
318 const parser::Block &body, parser::CharBlock source);
319 void CheckAtomicConditionalUpdateCapture(
320 const parser::OpenMPAtomicConstruct &x, const parser::Block &body,
321 parser::CharBlock source);
322 void CheckAtomicRead(const parser::OpenMPAtomicConstruct &x);
323 void CheckAtomicWrite(const parser::OpenMPAtomicConstruct &x);
324 void CheckAtomicUpdate(const parser::OpenMPAtomicConstruct &x);
325
326 void CheckScanModifier(const parser::OmpClause::Reduction &x);
327 void CheckLooprangeBounds(const parser::OpenMPLoopConstruct &x);
328 void CheckDistLinear(const parser::OpenMPLoopConstruct &x);
329 void CheckSIMDNest(const parser::OpenMPConstruct &x);
330 void CheckNestedBlock(
331 const parser::OpenMPLoopConstruct &x, const parser::Block &body);
332 void CheckNestedConstruct(const parser::OpenMPLoopConstruct &x);
333 void CheckFullUnroll(const parser::OpenMPLoopConstruct &x);
334 void CheckTargetNest(const parser::OpenMPConstruct &x);
335 void CheckTargetUpdate();
336 void CheckTaskgraph(const parser::OmpBlockConstruct &x);
337 void CheckDependenceType(const parser::OmpDependenceType::Value &x);
338 void CheckTaskDependenceType(const parser::OmpTaskDependenceType::Value &x);
339 std::optional<llvm::omp::Directive> GetCancelType(
340 llvm::omp::Directive cancelDir, const parser::CharBlock &cancelSource,
341 const std::optional<parser::OmpClauseList> &maybeClauses);
342 void CheckCancellationNest(
343 const parser::CharBlock &source, llvm::omp::Directive type);
344 std::int64_t GetOrdCollapseLevel(const parser::OpenMPLoopConstruct &x);
345 void CheckReductionObjects(
346 const parser::OmpObjectList &objects, llvm::omp::Clause clauseId);
347 bool CheckReductionOperator(const parser::OmpReductionIdentifier &ident,
348 parser::CharBlock source, llvm::omp::Clause clauseId);
349 void CheckReductionObjectTypes(const parser::OmpObjectList &objects,
350 const parser::OmpReductionIdentifier &ident);
351 void CheckReductionModifier(const parser::OmpReductionModifier &);
352 void CheckLastprivateModifier(const parser::OmpLastprivateModifier &);
353 void CheckMasterNesting(const parser::OmpBlockConstruct &x);
354 void ChecksOnOrderedAsBlock();
355 void CheckBarrierNesting(const parser::OpenMPSimpleStandaloneConstruct &x);
356 void CheckScan(const parser::OpenMPSimpleStandaloneConstruct &x);
357 void ChecksOnOrderedAsStandalone();
358 void CheckOrderedDependClause(std::optional<std::int64_t> orderedValue);
359 void CheckReductionArraySection(
360 const parser::OmpObjectList &ompObjectList, llvm::omp::Clause clauseId);
361 void CheckArraySection(const parser::ArrayElement &arrayElement,
362 const parser::Name &name, const llvm::omp::Clause clause);
363 void CheckSharedBindingInOuterContext(
364 const parser::OmpObjectList &ompObjectList);
365 void CheckIfContiguous(const parser::OmpObject &object);
366 const parser::Name *GetObjectName(const parser::OmpObject &object);
367
368 void CheckAllowedRequiresClause(llvmOmpClause clause);
369 bool deviceConstructFound_{false};
370
371 void AddEndDirectiveClauses(const parser::OmpClauseList &clauses);
372
373 void EnterDirectiveNest(const int index) { directiveNest_[index]++; }
374 void ExitDirectiveNest(const int index) { directiveNest_[index]--; }
375 int GetDirectiveNest(const int index) { return directiveNest_[index]; }
376 inline void ErrIfAllocatableVariable(const parser::Variable &);
377 inline void ErrIfLHSAndRHSSymbolsMatch(
378 const parser::Variable &, const parser::Expr &);
379 inline void ErrIfNonScalarAssignmentStmt(
380 const parser::Variable &, const parser::Expr &);
381 enum directiveNestType : int {
382 SIMDNest,
383 TargetBlockOnlyTeams,
384 TargetNest,
385 DeclarativeNest,
386 ContextSelectorNest,
387 MetadirectiveNest,
388 LastType = MetadirectiveNest,
389 };
390 int directiveNest_[LastType + 1] = {0};
391
392 int allocateDirectiveLevel{0};
393 parser::CharBlock visitedAtomicSource_;
394 SymbolSourceMap deferredNonVariables_;
395
396 using LoopConstruct = std::variant<const parser::DoConstruct *,
398 std::vector<LoopConstruct> loopStack_;
399 // Scopes for scoping units.
400 std::vector<const Scope *> scopeStack_;
401
402 enum class PartKind : int {
403 // There are also other "parts", such as internal-subprogram-part, etc,
404 // but we're keeping track of these two for now.
405 SpecificationPart,
406 ExecutionPart,
407 };
408 std::vector<PartKind> partStack_;
409};
410
413template <typename LessTy, typename RangeTy, typename IterTy>
414std::optional<IterTy> OmpStructureChecker::FindDuplicate(RangeTy &&range) {
415 // Deal with iterators, since the actual elements may be rvalues (i.e.
416 // have no addresses), for example with custom-constructed ranges that
417 // are not simple c.begin()..c.end().
418 std::set<IterTy, LessTy> uniq;
419 for (auto it{range.begin()}, end{range.end()}; it != end; ++it) {
420 if (!uniq.insert(it).second) {
421 return it;
422 }
423 }
424 return std::nullopt;
425}
426
427} // namespace Fortran::semantics
428#endif // FORTRAN_SEMANTICS_CHECK_OMP_STRUCTURE_H_
Definition enum-set.h:28
Definition indirection.h:31
Definition expression.h:878
Definition common.h:214
Definition char-block.h:28
Definition check-directive-structure.h:183
Definition semantics.h:67
Definition FIRType.h:92
Definition parse-tree.h:1921
Definition parse-tree.h:2205
Definition parse-tree.h:3037
Definition parse-tree.h:1828
Definition parse-tree.h:1867
Definition parse-tree.h:2348
Definition parse-tree.h:554
Definition parse-tree.h:1710
Definition parse-tree.h:3159
Definition parse-tree.h:2946
Definition parse-tree.h:2981
Definition parse-tree.h:587
Definition parse-tree.h:5283
Definition parse-tree.h:5077
Definition parse-tree.h:5087
Definition parse-tree.h:5046
Definition parse-tree.h:5030
Definition parse-tree.h:5053
Definition parse-tree.h:4476
Definition parse-tree.h:5082
Definition parse-tree.h:5424
Definition parse-tree.h:5153
Definition parse-tree.h:5115
Definition parse-tree.h:5100
Definition parse-tree.h:5109
Definition parse-tree.h:3561
Definition parse-tree.h:3549
Definition parse-tree.h:3605
Definition parse-tree.h:5309
Definition parse-tree.h:5142
Definition parse-tree.h:5314
Definition parse-tree.h:5351
Definition parse-tree.h:5455
Definition parse-tree.h:5299
Definition parse-tree.h:5130
Definition parse-tree.h:5362
Definition parse-tree.h:5376
Definition parse-tree.h:5392
Definition parse-tree.h:5238
Definition parse-tree.h:5400
Definition parse-tree.h:5473
Definition parse-tree.h:5429
Definition parse-tree.h:5244
Definition parse-tree.h:5168
Definition parse-tree.h:5250
Definition parse-tree.h:451
Definition parse-tree.h:3023
Definition parse-tree.h:3178
Definition parse-tree.h:1875
Definition parse-tree.h:3986
Definition parse-tree.h:4086
Definition parse-tree.h:4096
Definition parse-tree.h:4104
Definition parse-tree.h:4211
Definition parse-tree.h:3751
Definition parse-tree.h:3717
Definition parse-tree.h:3790
Definition parse-tree.h:3812
Definition check-omp-atomic.cpp:242