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 "flang/Common/enum-set.h" 18 #include "flang/Parser/parse-tree.h" 19 #include "flang/Semantics/semantics.h" 20 21 namespace Fortran::semantics { 22 23 ENUM_CLASS(OmpDirective, ATOMIC, BARRIER, CANCEL, CANCELLATION_POINT, CRITICAL, 24 DECLARE_REDUCTION, DECLARE_SIMD, DECLARE_TARGET, DISTRIBUTE, 25 DISTRIBUTE_PARALLEL_DO, DISTRIBUTE_PARALLEL_DO_SIMD, DISTRIBUTE_SIMD, DO, 26 DO_SIMD, END_CRITICAL, END_DO, END_DO_SIMD, END_SECTIONS, END_SINGLE, 27 END_WORKSHARE, FLUSH, MASTER, ORDERED, PARALLEL, PARALLEL_DO, 28 PARALLEL_DO_SIMD, PARALLEL_SECTIONS, PARALLEL_WORKSHARE, SECTION, SECTIONS, 29 SIMD, SINGLE, TARGET, TARGET_DATA, TARGET_ENTER_DATA, TARGET_EXIT_DATA, 30 TARGET_PARALLEL, TARGET_PARALLEL_DO, TARGET_PARALLEL_DO_SIMD, TARGET_SIMD, 31 TARGET_TEAMS, TARGET_TEAMS_DISTRIBUTE, TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO, 32 TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD, TARGET_TEAMS_DISTRIBUTE_SIMD, 33 TARGET_UPDATE, TASK, TASKGROUP, TASKLOOP, TASKLOOP_SIMD, TASKWAIT, 34 TASKYIELD, TEAMS, TEAMS_DISTRIBUTE, TEAMS_DISTRIBUTE_PARALLEL_DO, 35 TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD, TEAMS_DISTRIBUTE_SIMD, THREADPRIVATE, 36 WORKSHARE) 37 38 using OmpDirectiveSet = common::EnumSet<OmpDirective, OmpDirective_enumSize>; 39 40 ENUM_CLASS(OmpClause, ALIGNED, COLLAPSE, COPYIN, COPYPRIVATE, DEFAULT, 41 DEFAULTMAP, DEPEND, DEVICE, DIST_SCHEDULE, FINAL, FIRSTPRIVATE, FROM, 42 GRAINSIZE, IF, INBRANCH, IS_DEVICE_PTR, LASTPRIVATE, LINEAR, LINK, MAP, 43 MERGEABLE, NOGROUP, NOTINBRANCH, NOWAIT, NUM_TASKS, NUM_TEAMS, NUM_THREADS, 44 ORDERED, PRIORITY, PRIVATE, PROC_BIND, REDUCTION, SAFELEN, SCHEDULE, SHARED, 45 SIMD, SIMDLEN, THREAD_LIMIT, THREADS, TO, UNIFORM, UNTIED, USE_DEVICE_PTR) 46 47 using OmpClauseSet = common::EnumSet<OmpClause, OmpClause_enumSize>; 48 49 static constexpr OmpDirectiveSet parallelSet{ 50 OmpDirective::DISTRIBUTE_PARALLEL_DO, 51 OmpDirective::DISTRIBUTE_PARALLEL_DO_SIMD, OmpDirective::PARALLEL, 52 OmpDirective::PARALLEL_DO, OmpDirective::PARALLEL_DO_SIMD, 53 OmpDirective::PARALLEL_SECTIONS, OmpDirective::PARALLEL_WORKSHARE, 54 OmpDirective::TARGET_PARALLEL, OmpDirective::TARGET_PARALLEL_DO, 55 OmpDirective::TARGET_PARALLEL_DO_SIMD, 56 OmpDirective::TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO, 57 OmpDirective::TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD, 58 OmpDirective::TEAMS_DISTRIBUTE_PARALLEL_DO, 59 OmpDirective::TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD}; 60 static constexpr OmpDirectiveSet doSet{OmpDirective::DISTRIBUTE_PARALLEL_DO, 61 OmpDirective::DISTRIBUTE_PARALLEL_DO_SIMD, OmpDirective::PARALLEL_DO, 62 OmpDirective::PARALLEL_DO_SIMD, OmpDirective::DO, OmpDirective::DO_SIMD, 63 OmpDirective::TARGET_PARALLEL_DO, OmpDirective::TARGET_PARALLEL_DO_SIMD, 64 OmpDirective::TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO, 65 OmpDirective::TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD, 66 OmpDirective::TEAMS_DISTRIBUTE_PARALLEL_DO, 67 OmpDirective::TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD}; 68 static constexpr OmpDirectiveSet doSimdSet{ 69 OmpDirective::DISTRIBUTE_PARALLEL_DO_SIMD, OmpDirective::PARALLEL_DO_SIMD, 70 OmpDirective::DO_SIMD, OmpDirective::TARGET_PARALLEL_DO_SIMD, 71 OmpDirective::TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD, 72 OmpDirective::TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD}; 73 static constexpr OmpDirectiveSet taskloopSet{ 74 OmpDirective::TASKLOOP, OmpDirective::TASKLOOP_SIMD}; 75 static constexpr OmpDirectiveSet targetSet{OmpDirective::TARGET, 76 OmpDirective::TARGET_PARALLEL, OmpDirective::TARGET_PARALLEL_DO, 77 OmpDirective::TARGET_PARALLEL_DO_SIMD, OmpDirective::TARGET_SIMD, 78 OmpDirective::TARGET_TEAMS, OmpDirective::TARGET_TEAMS_DISTRIBUTE, 79 OmpDirective::TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO, 80 OmpDirective::TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD, 81 OmpDirective::TARGET_TEAMS_DISTRIBUTE_SIMD}; 82 static constexpr OmpDirectiveSet simdSet{ 83 OmpDirective::DISTRIBUTE_PARALLEL_DO_SIMD, OmpDirective::DISTRIBUTE_SIMD, 84 OmpDirective::PARALLEL_DO_SIMD, OmpDirective::DO_SIMD, OmpDirective::SIMD, 85 OmpDirective::TARGET_PARALLEL_DO_SIMD, 86 OmpDirective::TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD, 87 OmpDirective::TARGET_TEAMS_DISTRIBUTE_SIMD, OmpDirective::TARGET_SIMD, 88 OmpDirective::TASKLOOP_SIMD, 89 OmpDirective::TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD, 90 OmpDirective::TEAMS_DISTRIBUTE_SIMD}; 91 static constexpr OmpDirectiveSet taskGeneratingSet{ 92 OmpDirectiveSet{OmpDirective::TASK} | taskloopSet}; 93 94 class OmpStructureChecker : public virtual BaseChecker { 95 public: 96 OmpStructureChecker(SemanticsContext &context) : context_{context} {} 97 98 void Enter(const parser::OpenMPConstruct &); 99 void Enter(const parser::OpenMPLoopConstruct &); 100 void Leave(const parser::OpenMPLoopConstruct &); 101 void Enter(const parser::OmpEndLoopDirective &); 102 103 void Enter(const parser::OpenMPBlockConstruct &); 104 void Leave(const parser::OpenMPBlockConstruct &); 105 void Enter(const parser::OmpEndBlockDirective &); 106 107 void Enter(const parser::OpenMPSectionsConstruct &); 108 void Leave(const parser::OpenMPSectionsConstruct &); 109 void Enter(const parser::OmpEndSectionsDirective &); 110 111 void Enter(const parser::OpenMPDeclareSimdConstruct &); 112 void Leave(const parser::OpenMPDeclareSimdConstruct &); 113 void Enter(const parser::OpenMPDeclareTargetConstruct &); 114 void Leave(const parser::OpenMPDeclareTargetConstruct &); 115 116 void Enter(const parser::OpenMPSimpleStandaloneConstruct &); 117 void Leave(const parser::OpenMPSimpleStandaloneConstruct &); 118 void Enter(const parser::OpenMPFlushConstruct &); 119 void Leave(const parser::OpenMPFlushConstruct &); 120 void Enter(const parser::OpenMPCancelConstruct &); 121 void Leave(const parser::OpenMPCancelConstruct &); 122 void Enter(const parser::OpenMPCancellationPointConstruct &); 123 void Leave(const parser::OpenMPCancellationPointConstruct &); 124 125 void Leave(const parser::OmpClauseList &); 126 void Enter(const parser::OmpClause &); 127 void Enter(const parser::OmpNowait &); 128 void Enter(const parser::OmpClause::Inbranch &); 129 void Enter(const parser::OmpClause::Mergeable &); 130 void Enter(const parser::OmpClause::Nogroup &); 131 void Enter(const parser::OmpClause::Notinbranch &); 132 void Enter(const parser::OmpClause::Untied &); 133 void Enter(const parser::OmpClause::Collapse &); 134 void Enter(const parser::OmpClause::Copyin &); 135 void Enter(const parser::OmpClause::Copyprivate &); 136 void Enter(const parser::OmpClause::Device &); 137 void Enter(const parser::OmpClause::DistSchedule &); 138 void Enter(const parser::OmpClause::Final &); 139 void Enter(const parser::OmpClause::Firstprivate &); 140 void Enter(const parser::OmpClause::From &); 141 void Enter(const parser::OmpClause::Grainsize &); 142 void Enter(const parser::OmpClause::Lastprivate &); 143 void Enter(const parser::OmpClause::NumTasks &); 144 void Enter(const parser::OmpClause::NumTeams &); 145 void Enter(const parser::OmpClause::NumThreads &); 146 void Enter(const parser::OmpClause::Ordered &); 147 void Enter(const parser::OmpClause::Priority &); 148 void Enter(const parser::OmpClause::Private &); 149 void Enter(const parser::OmpClause::Safelen &); 150 void Enter(const parser::OmpClause::Shared &); 151 void Enter(const parser::OmpClause::Simdlen &); 152 void Enter(const parser::OmpClause::ThreadLimit &); 153 void Enter(const parser::OmpClause::To &); 154 void Enter(const parser::OmpClause::Link &); 155 void Enter(const parser::OmpClause::Uniform &); 156 void Enter(const parser::OmpClause::UseDevicePtr &); 157 void Enter(const parser::OmpClause::IsDevicePtr &); 158 159 void Enter(const parser::OmpAlignedClause &); 160 void Enter(const parser::OmpDefaultClause &); 161 void Enter(const parser::OmpDefaultmapClause &); 162 void Enter(const parser::OmpDependClause &); 163 void Enter(const parser::OmpIfClause &); 164 void Enter(const parser::OmpLinearClause &); 165 void Enter(const parser::OmpMapClause &); 166 void Enter(const parser::OmpProcBindClause &); 167 void Enter(const parser::OmpReductionClause &); 168 void Enter(const parser::OmpScheduleClause &); 169 170 private: 171 struct OmpContext { 172 OmpContext(parser::CharBlock source, OmpDirective d) 173 : directiveSource{source}, directive{d} {} 174 parser::CharBlock directiveSource{nullptr}; 175 parser::CharBlock clauseSource{nullptr}; 176 OmpDirective directive; 177 OmpClauseSet allowedClauses{}; 178 OmpClauseSet allowedOnceClauses{}; 179 OmpClauseSet allowedExclusiveClauses{}; 180 OmpClauseSet requiredClauses{}; 181 182 const parser::OmpClause *clause{nullptr}; 183 std::multimap<OmpClause, const parser::OmpClause *> clauseInfo; 184 }; 185 // back() is the top of the stack 186 OmpContext &GetContext() { 187 CHECK(!ompContext_.empty()); 188 return ompContext_.back(); 189 } 190 // reset source location, check information, and 191 // collected information for END directive 192 void ResetPartialContext(const parser::CharBlock &source) { 193 CHECK(!ompContext_.empty()); 194 SetContextDirectiveSource(source); 195 GetContext().allowedClauses = {}; 196 GetContext().allowedOnceClauses = {}; 197 GetContext().allowedExclusiveClauses = {}; 198 GetContext().requiredClauses = {}; 199 GetContext().clauseInfo = {}; 200 } 201 void SetContextDirectiveSource(const parser::CharBlock &directive) { 202 GetContext().directiveSource = directive; 203 } 204 void SetContextClause(const parser::OmpClause &clause) { 205 GetContext().clauseSource = clause.source; 206 GetContext().clause = &clause; 207 } 208 void SetContextDirectiveEnum(OmpDirective dir) { 209 GetContext().directive = dir; 210 } 211 void SetContextAllowed(const OmpClauseSet &allowed) { 212 GetContext().allowedClauses = allowed; 213 } 214 void SetContextAllowedOnce(const OmpClauseSet &allowedOnce) { 215 GetContext().allowedOnceClauses = allowedOnce; 216 } 217 void SetContextAllowedExclusive(const OmpClauseSet &allowedExclusive) { 218 GetContext().allowedExclusiveClauses = allowedExclusive; 219 } 220 void SetContextRequired(const OmpClauseSet &required) { 221 GetContext().requiredClauses = required; 222 } 223 void SetContextClauseInfo(OmpClause type) { 224 GetContext().clauseInfo.emplace(type, GetContext().clause); 225 } 226 const parser::OmpClause *FindClause(OmpClause type) { 227 auto it{GetContext().clauseInfo.find(type)}; 228 if (it != GetContext().clauseInfo.end()) { 229 return it->second; 230 } 231 return nullptr; 232 } 233 void PushContext(const parser::CharBlock &source, OmpDirective dir) { 234 ompContext_.emplace_back(source, dir); 235 } 236 237 void RequiresConstantPositiveParameter( 238 const OmpClause &clause, const parser::ScalarIntConstantExpr &i); 239 void RequiresPositiveParameter( 240 const OmpClause &clause, const parser::ScalarIntExpr &i); 241 242 bool CurrentDirectiveIsNested() { return ompContext_.size() > 0; }; 243 bool HasInvalidWorksharingNesting( 244 const parser::CharBlock &, const OmpDirectiveSet &); 245 void CheckAllowed(OmpClause); 246 void CheckRequired(OmpClause); 247 std::string ContextDirectiveAsFortran(); 248 void SayNotMatching(const parser::CharBlock &, const parser::CharBlock &); 249 template <typename A, typename B, typename C> 250 const A &CheckMatching(const B &beginDir, const C &endDir) { 251 const A &begin{std::get<A>(beginDir.t)}; 252 const A &end{std::get<A>(endDir.t)}; 253 if (begin.v != end.v) { 254 SayNotMatching(begin.source, end.source); 255 } 256 return begin; 257 } 258 259 // specific clause related 260 bool ScheduleModifierHasType(const parser::OmpScheduleClause &, 261 const parser::OmpScheduleModifierType::ModType &); 262 263 SemanticsContext &context_; 264 std::vector<OmpContext> ompContext_; // used as a stack 265 }; 266 } // namespace Fortran::semantics 267 #endif // FORTRAN_SEMANTICS_CHECK_OMP_STRUCTURE_H_ 268