1 //===-- lib/Semantics/check-omp-structure.cpp -----------------------------===// 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 #include "check-omp-structure.h" 10 #include "flang/Parser/parse-tree.h" 11 #include "flang/Semantics/tools.h" 12 #include <algorithm> 13 14 namespace Fortran::semantics { 15 16 // Use when clause falls under 'struct OmpClause' in 'parse-tree.h'. 17 #define CHECK_SIMPLE_CLAUSE(X, Y) \ 18 void OmpStructureChecker::Enter(const parser::OmpClause::X &) { \ 19 CheckAllowed(llvm::omp::Clause::Y); \ 20 } 21 22 #define CHECK_REQ_CONSTANT_SCALAR_INT_CLAUSE(X, Y) \ 23 void OmpStructureChecker::Enter(const parser::OmpClause::X &c) { \ 24 CheckAllowed(llvm::omp::Clause::Y); \ 25 RequiresConstantPositiveParameter(llvm::omp::Clause::Y, c.v); \ 26 } 27 28 #define CHECK_REQ_SCALAR_INT_CLAUSE(X, Y) \ 29 void OmpStructureChecker::Enter(const parser::OmpClause::X &c) { \ 30 CheckAllowed(llvm::omp::Clause::Y); \ 31 RequiresPositiveParameter(llvm::omp::Clause::Y, c.v); \ 32 } 33 34 // Use when clause don't falls under 'struct OmpClause' in 'parse-tree.h'. 35 #define CHECK_SIMPLE_PARSER_CLAUSE(X, Y) \ 36 void OmpStructureChecker::Enter(const parser::X &) { \ 37 CheckAllowed(llvm::omp::Y); \ 38 } 39 40 // 'OmpWorkshareBlockChecker' is used to check the validity of the assignment 41 // statements and the expressions enclosed in an OpenMP Workshare construct 42 class OmpWorkshareBlockChecker { 43 public: 44 OmpWorkshareBlockChecker(SemanticsContext &context, parser::CharBlock source) 45 : context_{context}, source_{source} {} 46 47 template <typename T> bool Pre(const T &) { return true; } 48 template <typename T> void Post(const T &) {} 49 50 bool Pre(const parser::AssignmentStmt &assignment) { 51 const auto &var{std::get<parser::Variable>(assignment.t)}; 52 const auto &expr{std::get<parser::Expr>(assignment.t)}; 53 const auto *lhs{GetExpr(var)}; 54 const auto *rhs{GetExpr(expr)}; 55 Tristate isDefined{semantics::IsDefinedAssignment( 56 lhs->GetType(), lhs->Rank(), rhs->GetType(), rhs->Rank())}; 57 if (isDefined == Tristate::Yes) { 58 context_.Say(expr.source, 59 "Defined assignment statement is not " 60 "allowed in a WORKSHARE construct"_err_en_US); 61 } 62 return true; 63 } 64 65 bool Pre(const parser::Expr &expr) { 66 if (const auto *e{GetExpr(expr)}) { 67 for (const Symbol &symbol : evaluate::CollectSymbols(*e)) { 68 const Symbol &root{GetAssociationRoot(symbol)}; 69 if (IsFunction(root) && 70 !(root.attrs().test(Attr::ELEMENTAL) || 71 root.attrs().test(Attr::INTRINSIC))) { 72 context_.Say(expr.source, 73 "User defined non-ELEMENTAL function " 74 "'%s' is not allowed in a WORKSHARE construct"_err_en_US, 75 root.name()); 76 } 77 } 78 } 79 return false; 80 } 81 82 private: 83 SemanticsContext &context_; 84 parser::CharBlock source_; 85 }; 86 87 class OmpCycleChecker { 88 public: 89 OmpCycleChecker(SemanticsContext &context, std::int64_t cycleLevel) 90 : context_{context}, cycleLevel_{cycleLevel} {} 91 92 template <typename T> bool Pre(const T &) { return true; } 93 template <typename T> void Post(const T &) {} 94 95 bool Pre(const parser::DoConstruct &dc) { 96 cycleLevel_--; 97 const auto &labelName{std::get<0>(std::get<0>(dc.t).statement.t)}; 98 if (labelName) { 99 labelNamesandLevels_.emplace(labelName.value().ToString(), cycleLevel_); 100 } 101 return true; 102 } 103 104 bool Pre(const parser::CycleStmt &cyclestmt) { 105 std::map<std::string, std::int64_t>::iterator it; 106 bool err{false}; 107 if (cyclestmt.v) { 108 it = labelNamesandLevels_.find(cyclestmt.v->source.ToString()); 109 err = (it != labelNamesandLevels_.end() && it->second > 0); 110 } 111 if (cycleLevel_ > 0 || err) { 112 context_.Say(*cycleSource_, 113 "CYCLE statement to non-innermost associated loop of an OpenMP DO construct"_err_en_US); 114 } 115 return true; 116 } 117 118 bool Pre(const parser::Statement<parser::ActionStmt> &actionstmt) { 119 cycleSource_ = &actionstmt.source; 120 return true; 121 } 122 123 private: 124 SemanticsContext &context_; 125 const parser::CharBlock *cycleSource_; 126 std::int64_t cycleLevel_; 127 std::map<std::string, std::int64_t> labelNamesandLevels_; 128 }; 129 130 bool OmpStructureChecker::IsCloselyNestedRegion(const OmpDirectiveSet &set) { 131 // Definition of close nesting: 132 // 133 // `A region nested inside another region with no parallel region nested 134 // between them` 135 // 136 // Examples: 137 // non-parallel construct 1 138 // non-parallel construct 2 139 // parallel construct 140 // construct 3 141 // In the above example, construct 3 is NOT closely nested inside construct 1 142 // or 2 143 // 144 // non-parallel construct 1 145 // non-parallel construct 2 146 // construct 3 147 // In the above example, construct 3 is closely nested inside BOTH construct 1 148 // and 2 149 // 150 // Algorithm: 151 // Starting from the parent context, Check in a bottom-up fashion, each level 152 // of the context stack. If we have a match for one of the (supplied) 153 // violating directives, `close nesting` is satisfied. If no match is there in 154 // the entire stack, `close nesting` is not satisfied. If at any level, a 155 // `parallel` region is found, `close nesting` is not satisfied. 156 157 if (CurrentDirectiveIsNested()) { 158 int index = dirContext_.size() - 2; 159 while (index != -1) { 160 if (set.test(dirContext_[index].directive)) { 161 return true; 162 } else if (llvm::omp::parallelSet.test(dirContext_[index].directive)) { 163 return false; 164 } 165 index--; 166 } 167 } 168 return false; 169 } 170 171 bool OmpStructureChecker::HasInvalidWorksharingNesting( 172 const parser::CharBlock &source, const OmpDirectiveSet &set) { 173 // set contains all the invalid closely nested directives 174 // for the given directive (`source` here) 175 if (IsCloselyNestedRegion(set)) { 176 context_.Say(source, 177 "A worksharing region may not be closely nested inside a " 178 "worksharing, explicit task, taskloop, critical, ordered, atomic, or " 179 "master region"_err_en_US); 180 return true; 181 } 182 return false; 183 } 184 185 void OmpStructureChecker::HasInvalidDistributeNesting( 186 const parser::OpenMPLoopConstruct &x) { 187 bool violation{false}; 188 189 OmpDirectiveSet distributeSet{llvm::omp::Directive::OMPD_distribute, 190 llvm::omp::Directive::OMPD_distribute_parallel_do, 191 llvm::omp::Directive::OMPD_distribute_parallel_do_simd, 192 llvm::omp::Directive::OMPD_distribute_parallel_for, 193 llvm::omp::Directive::OMPD_distribute_parallel_for_simd, 194 llvm::omp::Directive::OMPD_distribute_simd}; 195 196 const auto &beginLoopDir{std::get<parser::OmpBeginLoopDirective>(x.t)}; 197 const auto &beginDir{std::get<parser::OmpLoopDirective>(beginLoopDir.t)}; 198 if (distributeSet.test(beginDir.v)) { 199 // `distribute` region has to be nested 200 if (!CurrentDirectiveIsNested()) { 201 violation = true; 202 } else { 203 // `distribute` region has to be strictly nested inside `teams` 204 if (!llvm::omp::teamSet.test(GetContextParent().directive)) { 205 violation = true; 206 } 207 } 208 } 209 if (violation) { 210 context_.Say(beginDir.source, 211 "`DISTRIBUTE` region has to be strictly nested inside `TEAMS` region."_err_en_US); 212 } 213 } 214 215 void OmpStructureChecker::HasInvalidTeamsNesting( 216 const llvm::omp::Directive &dir, const parser::CharBlock &source) { 217 OmpDirectiveSet allowedSet{llvm::omp::Directive::OMPD_parallel, 218 llvm::omp::Directive::OMPD_parallel_do, 219 llvm::omp::Directive::OMPD_parallel_do_simd, 220 llvm::omp::Directive::OMPD_parallel_for, 221 llvm::omp::Directive::OMPD_parallel_for_simd, 222 llvm::omp::Directive::OMPD_parallel_master, 223 llvm::omp::Directive::OMPD_parallel_master_taskloop, 224 llvm::omp::Directive::OMPD_parallel_master_taskloop_simd, 225 llvm::omp::Directive::OMPD_parallel_sections, 226 llvm::omp::Directive::OMPD_parallel_workshare, 227 llvm::omp::Directive::OMPD_distribute, 228 llvm::omp::Directive::OMPD_distribute_parallel_do, 229 llvm::omp::Directive::OMPD_distribute_parallel_do_simd, 230 llvm::omp::Directive::OMPD_distribute_parallel_for, 231 llvm::omp::Directive::OMPD_distribute_parallel_for_simd, 232 llvm::omp::Directive::OMPD_distribute_simd}; 233 234 if (!allowedSet.test(dir)) { 235 context_.Say(source, 236 "Only `DISTRIBUTE` or `PARALLEL` regions are allowed to be strictly nested inside `TEAMS` region."_err_en_US); 237 } 238 } 239 240 void OmpStructureChecker::CheckPredefinedAllocatorRestriction( 241 const parser::CharBlock &source, const parser::Name &name) { 242 if (const auto *symbol{name.symbol}) { 243 const auto *commonBlock{FindCommonBlockContaining(*symbol)}; 244 const auto &scope{context_.FindScope(symbol->name())}; 245 const Scope &containingScope{GetProgramUnitContaining(scope)}; 246 if (!isPredefinedAllocator && 247 (IsSave(*symbol) || commonBlock || 248 containingScope.kind() == Scope::Kind::Module)) { 249 context_.Say(source, 250 "If list items within the ALLOCATE directive have the " 251 "SAVE attribute, are a common block name, or are " 252 "declared in the scope of a module, then only " 253 "predefined memory allocator parameters can be used " 254 "in the allocator clause"_err_en_US); 255 } 256 } 257 } 258 259 void OmpStructureChecker::CheckPredefinedAllocatorRestriction( 260 const parser::CharBlock &source, 261 const parser::OmpObjectList &ompObjectList) { 262 for (const auto &ompObject : ompObjectList.v) { 263 std::visit( 264 common::visitors{ 265 [&](const parser::Designator &designator) { 266 if (const auto *dataRef{ 267 std::get_if<parser::DataRef>(&designator.u)}) { 268 if (const auto *name{std::get_if<parser::Name>(&dataRef->u)}) { 269 CheckPredefinedAllocatorRestriction(source, *name); 270 } 271 } 272 }, 273 [&](const parser::Name &name) { 274 CheckPredefinedAllocatorRestriction(source, name); 275 }, 276 }, 277 ompObject.u); 278 } 279 } 280 281 void OmpStructureChecker::Enter(const parser::OpenMPConstruct &x) { 282 // Simd Construct with Ordered Construct Nesting check 283 // We cannot use CurrentDirectiveIsNested() here because 284 // PushContextAndClauseSets() has not been called yet, it is 285 // called individually for each construct. Therefore a 286 // dirContext_ size `1` means the current construct is nested 287 if (dirContext_.size() >= 1) { 288 if (GetSIMDNest() > 0) { 289 CheckSIMDNest(x); 290 } 291 } 292 } 293 294 void OmpStructureChecker::Enter(const parser::OpenMPLoopConstruct &x) { 295 const auto &beginLoopDir{std::get<parser::OmpBeginLoopDirective>(x.t)}; 296 const auto &beginDir{std::get<parser::OmpLoopDirective>(beginLoopDir.t)}; 297 298 // check matching, End directive is optional 299 if (const auto &endLoopDir{ 300 std::get<std::optional<parser::OmpEndLoopDirective>>(x.t)}) { 301 const auto &endDir{ 302 std::get<parser::OmpLoopDirective>(endLoopDir.value().t)}; 303 304 CheckMatching<parser::OmpLoopDirective>(beginDir, endDir); 305 } 306 307 PushContextAndClauseSets(beginDir.source, beginDir.v); 308 if (llvm::omp::simdSet.test(GetContext().directive)) { 309 EnterSIMDNest(); 310 } 311 312 if (beginDir.v == llvm::omp::Directive::OMPD_do) { 313 // 2.7.1 do-clause -> private-clause | 314 // firstprivate-clause | 315 // lastprivate-clause | 316 // linear-clause | 317 // reduction-clause | 318 // schedule-clause | 319 // collapse-clause | 320 // ordered-clause 321 322 // nesting check 323 HasInvalidWorksharingNesting( 324 beginDir.source, llvm::omp::nestedWorkshareErrSet); 325 } 326 SetLoopInfo(x); 327 328 if (const auto &doConstruct{ 329 std::get<std::optional<parser::DoConstruct>>(x.t)}) { 330 const auto &doBlock{std::get<parser::Block>(doConstruct->t)}; 331 CheckNoBranching(doBlock, beginDir.v, beginDir.source); 332 } 333 CheckDoWhile(x); 334 CheckLoopItrVariableIsInt(x); 335 CheckCycleConstraints(x); 336 HasInvalidDistributeNesting(x); 337 if (CurrentDirectiveIsNested() && 338 llvm::omp::teamSet.test(GetContextParent().directive)) { 339 HasInvalidTeamsNesting(beginDir.v, beginDir.source); 340 } 341 if ((beginDir.v == llvm::omp::Directive::OMPD_distribute_parallel_do_simd) || 342 (beginDir.v == llvm::omp::Directive::OMPD_distribute_simd)) { 343 CheckDistLinear(x); 344 } 345 } 346 const parser::Name OmpStructureChecker::GetLoopIndex( 347 const parser::DoConstruct *x) { 348 using Bounds = parser::LoopControl::Bounds; 349 return std::get<Bounds>(x->GetLoopControl()->u).name.thing; 350 } 351 void OmpStructureChecker::SetLoopInfo(const parser::OpenMPLoopConstruct &x) { 352 if (const auto &loopConstruct{ 353 std::get<std::optional<parser::DoConstruct>>(x.t)}) { 354 const parser::DoConstruct *loop{&*loopConstruct}; 355 if (loop && loop->IsDoNormal()) { 356 const parser::Name &itrVal{GetLoopIndex(loop)}; 357 SetLoopIv(itrVal.symbol); 358 } 359 } 360 } 361 void OmpStructureChecker::CheckDoWhile(const parser::OpenMPLoopConstruct &x) { 362 const auto &beginLoopDir{std::get<parser::OmpBeginLoopDirective>(x.t)}; 363 const auto &beginDir{std::get<parser::OmpLoopDirective>(beginLoopDir.t)}; 364 if (beginDir.v == llvm::omp::Directive::OMPD_do) { 365 if (const auto &doConstruct{ 366 std::get<std::optional<parser::DoConstruct>>(x.t)}) { 367 if (doConstruct.value().IsDoWhile()) { 368 const auto &doStmt{std::get<parser::Statement<parser::NonLabelDoStmt>>( 369 doConstruct.value().t)}; 370 context_.Say(doStmt.source, 371 "The DO loop cannot be a DO WHILE with DO directive."_err_en_US); 372 } 373 } 374 } 375 } 376 377 void OmpStructureChecker::CheckLoopItrVariableIsInt( 378 const parser::OpenMPLoopConstruct &x) { 379 if (const auto &loopConstruct{ 380 std::get<std::optional<parser::DoConstruct>>(x.t)}) { 381 382 for (const parser::DoConstruct *loop{&*loopConstruct}; loop;) { 383 if (loop->IsDoNormal()) { 384 const parser::Name &itrVal{GetLoopIndex(loop)}; 385 if (itrVal.symbol) { 386 const auto *type{itrVal.symbol->GetType()}; 387 if (!type->IsNumeric(TypeCategory::Integer)) { 388 context_.Say(itrVal.source, 389 "The DO loop iteration" 390 " variable must be of the type integer."_err_en_US, 391 itrVal.ToString()); 392 } 393 } 394 } 395 // Get the next DoConstruct if block is not empty. 396 const auto &block{std::get<parser::Block>(loop->t)}; 397 const auto it{block.begin()}; 398 loop = it != block.end() ? parser::Unwrap<parser::DoConstruct>(*it) 399 : nullptr; 400 } 401 } 402 } 403 404 void OmpStructureChecker::CheckSIMDNest(const parser::OpenMPConstruct &c) { 405 // Check the following: 406 // The only OpenMP constructs that can be encountered during execution of 407 // a simd region are the `atomic` construct, the `loop` construct, the `simd` 408 // construct and the `ordered` construct with the `simd` clause. 409 // TODO: Expand the check to include `LOOP` construct as well when it is 410 // supported. 411 412 // Check if the parent context has the SIMD clause 413 // Please note that we use GetContext() instead of GetContextParent() 414 // because PushContextAndClauseSets() has not been called on the 415 // current context yet. 416 // TODO: Check for declare simd regions. 417 bool eligibleSIMD{false}; 418 std::visit(Fortran::common::visitors{ 419 // Allow `!$OMP ORDERED SIMD` 420 [&](const parser::OpenMPBlockConstruct &c) { 421 const auto &beginBlockDir{ 422 std::get<parser::OmpBeginBlockDirective>(c.t)}; 423 const auto &beginDir{ 424 std::get<parser::OmpBlockDirective>(beginBlockDir.t)}; 425 if (beginDir.v == llvm::omp::Directive::OMPD_ordered) { 426 const auto &clauses{ 427 std::get<parser::OmpClauseList>(beginBlockDir.t)}; 428 for (const auto &clause : clauses.v) { 429 if (std::get_if<parser::OmpClause::Simd>(&clause.u)) { 430 eligibleSIMD = true; 431 break; 432 } 433 } 434 } 435 }, 436 [&](const parser::OpenMPSimpleStandaloneConstruct &c) { 437 const auto &dir{ 438 std::get<parser::OmpSimpleStandaloneDirective>(c.t)}; 439 if (dir.v == llvm::omp::Directive::OMPD_ordered) { 440 const auto &clauses{std::get<parser::OmpClauseList>(c.t)}; 441 for (const auto &clause : clauses.v) { 442 if (std::get_if<parser::OmpClause::Simd>(&clause.u)) { 443 eligibleSIMD = true; 444 break; 445 } 446 } 447 } 448 }, 449 // Allowing SIMD construct 450 [&](const parser::OpenMPLoopConstruct &c) { 451 const auto &beginLoopDir{ 452 std::get<parser::OmpBeginLoopDirective>(c.t)}; 453 const auto &beginDir{ 454 std::get<parser::OmpLoopDirective>(beginLoopDir.t)}; 455 if ((beginDir.v == llvm::omp::Directive::OMPD_simd) || 456 (beginDir.v == llvm::omp::Directive::OMPD_do_simd)) { 457 eligibleSIMD = true; 458 } 459 }, 460 [&](const parser::OpenMPAtomicConstruct &c) { 461 // Allow `!$OMP ATOMIC` 462 eligibleSIMD = true; 463 }, 464 [&](const auto &c) {}, 465 }, 466 c.u); 467 if (!eligibleSIMD) { 468 context_.Say(parser::FindSourceLocation(c), 469 "The only OpenMP constructs that can be encountered during execution " 470 "of a 'SIMD'" 471 " region are the `ATOMIC` construct, the `LOOP` construct, the `SIMD`" 472 " construct and the `ORDERED` construct with the `SIMD` clause."_err_en_US); 473 } 474 } 475 476 std::int64_t OmpStructureChecker::GetOrdCollapseLevel( 477 const parser::OpenMPLoopConstruct &x) { 478 const auto &beginLoopDir{std::get<parser::OmpBeginLoopDirective>(x.t)}; 479 const auto &clauseList{std::get<parser::OmpClauseList>(beginLoopDir.t)}; 480 std::int64_t orderedCollapseLevel{1}; 481 std::int64_t orderedLevel{0}; 482 std::int64_t collapseLevel{0}; 483 484 for (const auto &clause : clauseList.v) { 485 if (const auto *collapseClause{ 486 std::get_if<parser::OmpClause::Collapse>(&clause.u)}) { 487 if (const auto v{GetIntValue(collapseClause->v)}) { 488 collapseLevel = *v; 489 } 490 } 491 if (const auto *orderedClause{ 492 std::get_if<parser::OmpClause::Ordered>(&clause.u)}) { 493 if (const auto v{GetIntValue(orderedClause->v)}) { 494 orderedLevel = *v; 495 } 496 } 497 } 498 if (orderedLevel >= collapseLevel) { 499 orderedCollapseLevel = orderedLevel; 500 } else { 501 orderedCollapseLevel = collapseLevel; 502 } 503 return orderedCollapseLevel; 504 } 505 506 void OmpStructureChecker::CheckCycleConstraints( 507 const parser::OpenMPLoopConstruct &x) { 508 std::int64_t ordCollapseLevel{GetOrdCollapseLevel(x)}; 509 OmpCycleChecker ompCycleChecker{context_, ordCollapseLevel}; 510 parser::Walk(x, ompCycleChecker); 511 } 512 513 void OmpStructureChecker::CheckDistLinear( 514 const parser::OpenMPLoopConstruct &x) { 515 516 const auto &beginLoopDir{std::get<parser::OmpBeginLoopDirective>(x.t)}; 517 const auto &clauses{std::get<parser::OmpClauseList>(beginLoopDir.t)}; 518 519 semantics::UnorderedSymbolSet indexVars; 520 521 // Collect symbols of all the variables from linear clauses 522 for (const auto &clause : clauses.v) { 523 if (const auto *linearClause{ 524 std::get_if<parser::OmpClause::Linear>(&clause.u)}) { 525 526 std::list<parser::Name> values; 527 // Get the variant type 528 if (std::holds_alternative<parser::OmpLinearClause::WithModifier>( 529 linearClause->v.u)) { 530 const auto &withM{ 531 std::get<parser::OmpLinearClause::WithModifier>(linearClause->v.u)}; 532 values = withM.names; 533 } else { 534 const auto &withOutM{std::get<parser::OmpLinearClause::WithoutModifier>( 535 linearClause->v.u)}; 536 values = withOutM.names; 537 } 538 for (auto const &v : values) { 539 indexVars.insert(*(v.symbol)); 540 } 541 } 542 } 543 544 if (!indexVars.empty()) { 545 // Get collapse level, if given, to find which loops are "associated." 546 std::int64_t collapseVal{GetOrdCollapseLevel(x)}; 547 // Include the top loop if no collapse is specified 548 if (collapseVal == 0) { 549 collapseVal = 1; 550 } 551 552 // Match the loop index variables with the collected symbols from linear 553 // clauses. 554 if (const auto &loopConstruct{ 555 std::get<std::optional<parser::DoConstruct>>(x.t)}) { 556 for (const parser::DoConstruct *loop{&*loopConstruct}; loop;) { 557 if (loop->IsDoNormal()) { 558 const parser::Name &itrVal{GetLoopIndex(loop)}; 559 if (itrVal.symbol) { 560 // Remove the symbol from the collcted set 561 indexVars.erase(*(itrVal.symbol)); 562 } 563 collapseVal--; 564 if (collapseVal == 0) { 565 break; 566 } 567 } 568 // Get the next DoConstruct if block is not empty. 569 const auto &block{std::get<parser::Block>(loop->t)}; 570 const auto it{block.begin()}; 571 loop = it != block.end() ? parser::Unwrap<parser::DoConstruct>(*it) 572 : nullptr; 573 } 574 } 575 576 // Show error for the remaining variables 577 for (auto var : indexVars) { 578 const Symbol &root{GetAssociationRoot(var)}; 579 context_.Say(parser::FindSourceLocation(x), 580 "Variable '%s' not allowed in `LINEAR` clause, only loop iterator can be specified in `LINEAR` clause of a construct combined with `DISTRIBUTE`"_err_en_US, 581 root.name()); 582 } 583 } 584 } 585 586 void OmpStructureChecker::Leave(const parser::OpenMPLoopConstruct &) { 587 if (llvm::omp::simdSet.test(GetContext().directive)) { 588 ExitSIMDNest(); 589 } 590 dirContext_.pop_back(); 591 } 592 593 void OmpStructureChecker::Enter(const parser::OmpEndLoopDirective &x) { 594 const auto &dir{std::get<parser::OmpLoopDirective>(x.t)}; 595 ResetPartialContext(dir.source); 596 switch (dir.v) { 597 // 2.7.1 end-do -> END DO [nowait-clause] 598 // 2.8.3 end-do-simd -> END DO SIMD [nowait-clause] 599 case llvm::omp::Directive::OMPD_do: 600 case llvm::omp::Directive::OMPD_do_simd: 601 SetClauseSets(dir.v); 602 break; 603 default: 604 // no clauses are allowed 605 break; 606 } 607 } 608 609 void OmpStructureChecker::Enter(const parser::OpenMPBlockConstruct &x) { 610 const auto &beginBlockDir{std::get<parser::OmpBeginBlockDirective>(x.t)}; 611 const auto &endBlockDir{std::get<parser::OmpEndBlockDirective>(x.t)}; 612 const auto &beginDir{std::get<parser::OmpBlockDirective>(beginBlockDir.t)}; 613 const auto &endDir{std::get<parser::OmpBlockDirective>(endBlockDir.t)}; 614 const parser::Block &block{std::get<parser::Block>(x.t)}; 615 616 CheckMatching<parser::OmpBlockDirective>(beginDir, endDir); 617 618 PushContextAndClauseSets(beginDir.source, beginDir.v); 619 620 if (CurrentDirectiveIsNested()) { 621 CheckIfDoOrderedClause(beginDir); 622 if (llvm::omp::teamSet.test(GetContextParent().directive)) { 623 HasInvalidTeamsNesting(beginDir.v, beginDir.source); 624 } 625 if (GetContext().directive == llvm::omp::Directive::OMPD_master) { 626 CheckMasterNesting(x); 627 } 628 } 629 630 CheckNoBranching(block, beginDir.v, beginDir.source); 631 632 switch (beginDir.v) { 633 case llvm::omp::OMPD_workshare: 634 case llvm::omp::OMPD_parallel_workshare: 635 CheckWorkshareBlockStmts(block, beginDir.source); 636 HasInvalidWorksharingNesting( 637 beginDir.source, llvm::omp::nestedWorkshareErrSet); 638 break; 639 case llvm::omp::Directive::OMPD_single: 640 // TODO: This check needs to be extended while implementing nesting of 641 // regions checks. 642 HasInvalidWorksharingNesting( 643 beginDir.source, llvm::omp::nestedWorkshareErrSet); 644 break; 645 default: 646 break; 647 } 648 } 649 650 void OmpStructureChecker::CheckMasterNesting( 651 const parser::OpenMPBlockConstruct &x) { 652 // A MASTER region may not be `closely nested` inside a worksharing, loop, 653 // task, taskloop, or atomic region. 654 // TODO: Expand the check to include `LOOP` construct as well when it is 655 // supported. 656 if (IsCloselyNestedRegion(llvm::omp::nestedMasterErrSet)) { 657 context_.Say(parser::FindSourceLocation(x), 658 "`MASTER` region may not be closely nested inside of `WORKSHARING`, " 659 "`LOOP`, `TASK`, `TASKLOOP`," 660 " or `ATOMIC` region."_err_en_US); 661 } 662 } 663 664 void OmpStructureChecker::CheckIfDoOrderedClause( 665 const parser::OmpBlockDirective &blkDirective) { 666 if (blkDirective.v == llvm::omp::OMPD_ordered) { 667 // Loops 668 if (llvm::omp::doSet.test(GetContextParent().directive) && 669 !FindClauseParent(llvm::omp::Clause::OMPC_ordered)) { 670 context_.Say(blkDirective.source, 671 "The ORDERED clause must be present on the loop" 672 " construct if any ORDERED region ever binds" 673 " to a loop region arising from the loop construct."_err_en_US); 674 } 675 // Other disallowed nestings, these directives do not support 676 // ordered clause in them, so no need to check 677 else if (IsCloselyNestedRegion(llvm::omp::nestedOrderedErrSet)) { 678 context_.Say(blkDirective.source, 679 "`ORDERED` region may not be closely nested inside of " 680 "`CRITICAL`, `ORDERED`, explicit `TASK` or `TASKLOOP` region."_err_en_US); 681 } 682 } 683 } 684 685 void OmpStructureChecker::Leave(const parser::OpenMPBlockConstruct &) { 686 dirContext_.pop_back(); 687 } 688 689 void OmpStructureChecker::Enter(const parser::OpenMPSectionsConstruct &x) { 690 const auto &beginSectionsDir{ 691 std::get<parser::OmpBeginSectionsDirective>(x.t)}; 692 const auto &endSectionsDir{std::get<parser::OmpEndSectionsDirective>(x.t)}; 693 const auto &beginDir{ 694 std::get<parser::OmpSectionsDirective>(beginSectionsDir.t)}; 695 const auto &endDir{std::get<parser::OmpSectionsDirective>(endSectionsDir.t)}; 696 CheckMatching<parser::OmpSectionsDirective>(beginDir, endDir); 697 698 PushContextAndClauseSets(beginDir.source, beginDir.v); 699 const auto §ionBlocks{std::get<parser::OmpSectionBlocks>(x.t)}; 700 for (const auto &block : sectionBlocks.v) { 701 CheckNoBranching(block, beginDir.v, beginDir.source); 702 } 703 HasInvalidWorksharingNesting( 704 beginDir.source, llvm::omp::nestedWorkshareErrSet); 705 } 706 707 void OmpStructureChecker::Leave(const parser::OpenMPSectionsConstruct &) { 708 dirContext_.pop_back(); 709 } 710 711 void OmpStructureChecker::Enter(const parser::OmpEndSectionsDirective &x) { 712 const auto &dir{std::get<parser::OmpSectionsDirective>(x.t)}; 713 ResetPartialContext(dir.source); 714 switch (dir.v) { 715 // 2.7.2 end-sections -> END SECTIONS [nowait-clause] 716 case llvm::omp::Directive::OMPD_sections: 717 PushContextAndClauseSets( 718 dir.source, llvm::omp::Directive::OMPD_end_sections); 719 break; 720 default: 721 // no clauses are allowed 722 break; 723 } 724 } 725 726 // TODO: Verify the popping of dirContext requirement after nowait 727 // implementation, as there is an implicit barrier at the end of the worksharing 728 // constructs unless a nowait clause is specified. Only OMPD_end_sections is 729 // popped becuase it is pushed while entering the EndSectionsDirective. 730 void OmpStructureChecker::Leave(const parser::OmpEndSectionsDirective &x) { 731 if (GetContext().directive == llvm::omp::Directive::OMPD_end_sections) { 732 dirContext_.pop_back(); 733 } 734 } 735 736 void OmpStructureChecker::Enter(const parser::OpenMPDeclareSimdConstruct &x) { 737 const auto &dir{std::get<parser::Verbatim>(x.t)}; 738 PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_declare_simd); 739 } 740 741 void OmpStructureChecker::Leave(const parser::OpenMPDeclareSimdConstruct &) { 742 dirContext_.pop_back(); 743 } 744 745 void OmpStructureChecker::Enter(const parser::OpenMPDeclarativeAllocate &x) { 746 isPredefinedAllocator = true; 747 const auto &dir{std::get<parser::Verbatim>(x.t)}; 748 const auto &objectList{std::get<parser::OmpObjectList>(x.t)}; 749 PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_allocate); 750 CheckIsVarPartOfAnotherVar(dir.source, objectList); 751 } 752 753 void OmpStructureChecker::Leave(const parser::OpenMPDeclarativeAllocate &x) { 754 const auto &dir{std::get<parser::Verbatim>(x.t)}; 755 const auto &objectList{std::get<parser::OmpObjectList>(x.t)}; 756 CheckPredefinedAllocatorRestriction(dir.source, objectList); 757 dirContext_.pop_back(); 758 } 759 760 void OmpStructureChecker::Enter(const parser::OmpClause::Allocator &x) { 761 CheckAllowed(llvm::omp::Clause::OMPC_allocator); 762 // Note: Predefined allocators are stored in ScalarExpr as numbers 763 // whereas custom allocators are stored as strings, so if the ScalarExpr 764 // actually has an int value, then it must be a predefined allocator 765 isPredefinedAllocator = GetIntValue(x.v).has_value(); 766 RequiresPositiveParameter(llvm::omp::Clause::OMPC_allocator, x.v); 767 } 768 769 void OmpStructureChecker::Enter(const parser::OpenMPDeclareTargetConstruct &x) { 770 const auto &dir{std::get<parser::Verbatim>(x.t)}; 771 PushContext(dir.source, llvm::omp::Directive::OMPD_declare_target); 772 const auto &spec{std::get<parser::OmpDeclareTargetSpecifier>(x.t)}; 773 if (std::holds_alternative<parser::OmpDeclareTargetWithClause>(spec.u)) { 774 SetClauseSets(llvm::omp::Directive::OMPD_declare_target); 775 } 776 } 777 778 void OmpStructureChecker::Leave(const parser::OpenMPDeclareTargetConstruct &) { 779 dirContext_.pop_back(); 780 } 781 782 void OmpStructureChecker::Enter(const parser::OpenMPExecutableAllocate &x) { 783 isPredefinedAllocator = true; 784 const auto &dir{std::get<parser::Verbatim>(x.t)}; 785 const auto &objectList{std::get<std::optional<parser::OmpObjectList>>(x.t)}; 786 PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_allocate); 787 if (objectList) { 788 CheckIsVarPartOfAnotherVar(dir.source, *objectList); 789 } 790 } 791 792 void OmpStructureChecker::Leave(const parser::OpenMPExecutableAllocate &x) { 793 const auto &dir{std::get<parser::Verbatim>(x.t)}; 794 const auto &objectList{std::get<std::optional<parser::OmpObjectList>>(x.t)}; 795 if (objectList) 796 CheckPredefinedAllocatorRestriction(dir.source, *objectList); 797 dirContext_.pop_back(); 798 } 799 800 void OmpStructureChecker::CheckBarrierNesting( 801 const parser::OpenMPSimpleStandaloneConstruct &x) { 802 // A barrier region may not be `closely nested` inside a worksharing, loop, 803 // task, taskloop, critical, ordered, atomic, or master region. 804 // TODO: Expand the check to include `LOOP` construct as well when it is 805 // supported. 806 if (GetContext().directive == llvm::omp::Directive::OMPD_barrier) { 807 if (IsCloselyNestedRegion(llvm::omp::nestedBarrierErrSet)) { 808 context_.Say(parser::FindSourceLocation(x), 809 "`BARRIER` region may not be closely nested inside of `WORKSHARING`, " 810 "`LOOP`, `TASK`, `TASKLOOP`," 811 "`CRITICAL`, `ORDERED`, `ATOMIC` or `MASTER` region."_err_en_US); 812 } 813 } 814 } 815 816 void OmpStructureChecker::Enter( 817 const parser::OpenMPSimpleStandaloneConstruct &x) { 818 const auto &dir{std::get<parser::OmpSimpleStandaloneDirective>(x.t)}; 819 PushContextAndClauseSets(dir.source, dir.v); 820 CheckBarrierNesting(x); 821 } 822 823 void OmpStructureChecker::Leave( 824 const parser::OpenMPSimpleStandaloneConstruct &) { 825 dirContext_.pop_back(); 826 } 827 828 void OmpStructureChecker::Enter(const parser::OpenMPFlushConstruct &x) { 829 const auto &dir{std::get<parser::Verbatim>(x.t)}; 830 PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_flush); 831 } 832 833 void OmpStructureChecker::Leave(const parser::OpenMPFlushConstruct &x) { 834 if (FindClause(llvm::omp::Clause::OMPC_acquire) || 835 FindClause(llvm::omp::Clause::OMPC_release) || 836 FindClause(llvm::omp::Clause::OMPC_acq_rel)) { 837 if (const auto &flushList{ 838 std::get<std::optional<parser::OmpObjectList>>(x.t)}) { 839 context_.Say(parser::FindSourceLocation(flushList), 840 "If memory-order-clause is RELEASE, ACQUIRE, or ACQ_REL, list items " 841 "must not be specified on the FLUSH directive"_err_en_US); 842 } 843 } 844 dirContext_.pop_back(); 845 } 846 847 void OmpStructureChecker::Enter(const parser::OpenMPCancelConstruct &x) { 848 const auto &dir{std::get<parser::Verbatim>(x.t)}; 849 PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_cancel); 850 } 851 852 void OmpStructureChecker::Leave(const parser::OpenMPCancelConstruct &) { 853 dirContext_.pop_back(); 854 } 855 856 void OmpStructureChecker::Enter(const parser::OpenMPCriticalConstruct &x) { 857 const auto &dir{std::get<parser::OmpCriticalDirective>(x.t)}; 858 PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_critical); 859 const auto &block{std::get<parser::Block>(x.t)}; 860 CheckNoBranching(block, llvm::omp::Directive::OMPD_critical, dir.source); 861 } 862 863 void OmpStructureChecker::Leave(const parser::OpenMPCriticalConstruct &) { 864 dirContext_.pop_back(); 865 } 866 867 void OmpStructureChecker::Enter( 868 const parser::OpenMPCancellationPointConstruct &x) { 869 const auto &dir{std::get<parser::Verbatim>(x.t)}; 870 PushContextAndClauseSets( 871 dir.source, llvm::omp::Directive::OMPD_cancellation_point); 872 } 873 874 void OmpStructureChecker::Leave( 875 const parser::OpenMPCancellationPointConstruct &) { 876 dirContext_.pop_back(); 877 } 878 879 void OmpStructureChecker::Enter(const parser::OmpEndBlockDirective &x) { 880 const auto &dir{std::get<parser::OmpBlockDirective>(x.t)}; 881 ResetPartialContext(dir.source); 882 switch (dir.v) { 883 // 2.7.3 end-single-clause -> copyprivate-clause | 884 // nowait-clause 885 case llvm::omp::Directive::OMPD_single: 886 PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_end_single); 887 break; 888 // 2.7.4 end-workshare -> END WORKSHARE [nowait-clause] 889 case llvm::omp::Directive::OMPD_workshare: 890 PushContextAndClauseSets( 891 dir.source, llvm::omp::Directive::OMPD_end_workshare); 892 break; 893 default: 894 // no clauses are allowed 895 break; 896 } 897 } 898 899 // TODO: Verify the popping of dirContext requirement after nowait 900 // implementation, as there is an implicit barrier at the end of the worksharing 901 // constructs unless a nowait clause is specified. Only OMPD_end_single and 902 // end_workshareare popped as they are pushed while entering the 903 // EndBlockDirective. 904 void OmpStructureChecker::Leave(const parser::OmpEndBlockDirective &x) { 905 if ((GetContext().directive == llvm::omp::Directive::OMPD_end_single) || 906 (GetContext().directive == llvm::omp::Directive::OMPD_end_workshare)) { 907 dirContext_.pop_back(); 908 } 909 } 910 911 void OmpStructureChecker::Enter(const parser::OpenMPAtomicConstruct &x) { 912 std::visit( 913 common::visitors{ 914 [&](const auto &someAtomicConstruct) { 915 const auto &dir{std::get<parser::Verbatim>(someAtomicConstruct.t)}; 916 PushContextAndClauseSets( 917 dir.source, llvm::omp::Directive::OMPD_atomic); 918 }, 919 }, 920 x.u); 921 } 922 923 void OmpStructureChecker::Leave(const parser::OpenMPAtomicConstruct &) { 924 dirContext_.pop_back(); 925 } 926 927 // Clauses 928 // Mainly categorized as 929 // 1. Checks on 'OmpClauseList' from 'parse-tree.h'. 930 // 2. Checks on clauses which fall under 'struct OmpClause' from parse-tree.h. 931 // 3. Checks on clauses which are not in 'struct OmpClause' from parse-tree.h. 932 933 void OmpStructureChecker::Leave(const parser::OmpClauseList &) { 934 // 2.7.1 Loop Construct Restriction 935 if (llvm::omp::doSet.test(GetContext().directive)) { 936 if (auto *clause{FindClause(llvm::omp::Clause::OMPC_schedule)}) { 937 // only one schedule clause is allowed 938 const auto &schedClause{std::get<parser::OmpClause::Schedule>(clause->u)}; 939 if (ScheduleModifierHasType(schedClause.v, 940 parser::OmpScheduleModifierType::ModType::Nonmonotonic)) { 941 if (FindClause(llvm::omp::Clause::OMPC_ordered)) { 942 context_.Say(clause->source, 943 "The NONMONOTONIC modifier cannot be specified " 944 "if an ORDERED clause is specified"_err_en_US); 945 } 946 if (ScheduleModifierHasType(schedClause.v, 947 parser::OmpScheduleModifierType::ModType::Monotonic)) { 948 context_.Say(clause->source, 949 "The MONOTONIC and NONMONOTONIC modifiers " 950 "cannot be both specified"_err_en_US); 951 } 952 } 953 } 954 955 if (auto *clause{FindClause(llvm::omp::Clause::OMPC_ordered)}) { 956 // only one ordered clause is allowed 957 const auto &orderedClause{ 958 std::get<parser::OmpClause::Ordered>(clause->u)}; 959 960 if (orderedClause.v) { 961 CheckNotAllowedIfClause( 962 llvm::omp::Clause::OMPC_ordered, {llvm::omp::Clause::OMPC_linear}); 963 964 if (auto *clause2{FindClause(llvm::omp::Clause::OMPC_collapse)}) { 965 const auto &collapseClause{ 966 std::get<parser::OmpClause::Collapse>(clause2->u)}; 967 // ordered and collapse both have parameters 968 if (const auto orderedValue{GetIntValue(orderedClause.v)}) { 969 if (const auto collapseValue{GetIntValue(collapseClause.v)}) { 970 if (*orderedValue > 0 && *orderedValue < *collapseValue) { 971 context_.Say(clause->source, 972 "The parameter of the ORDERED clause must be " 973 "greater than or equal to " 974 "the parameter of the COLLAPSE clause"_err_en_US); 975 } 976 } 977 } 978 } 979 } 980 981 // TODO: ordered region binding check (requires nesting implementation) 982 } 983 } // doSet 984 985 // 2.8.1 Simd Construct Restriction 986 if (llvm::omp::simdSet.test(GetContext().directive)) { 987 if (auto *clause{FindClause(llvm::omp::Clause::OMPC_simdlen)}) { 988 if (auto *clause2{FindClause(llvm::omp::Clause::OMPC_safelen)}) { 989 const auto &simdlenClause{ 990 std::get<parser::OmpClause::Simdlen>(clause->u)}; 991 const auto &safelenClause{ 992 std::get<parser::OmpClause::Safelen>(clause2->u)}; 993 // simdlen and safelen both have parameters 994 if (const auto simdlenValue{GetIntValue(simdlenClause.v)}) { 995 if (const auto safelenValue{GetIntValue(safelenClause.v)}) { 996 if (*safelenValue > 0 && *simdlenValue > *safelenValue) { 997 context_.Say(clause->source, 998 "The parameter of the SIMDLEN clause must be less than or " 999 "equal to the parameter of the SAFELEN clause"_err_en_US); 1000 } 1001 } 1002 } 1003 } 1004 } 1005 // A list-item cannot appear in more than one aligned clause 1006 semantics::UnorderedSymbolSet alignedVars; 1007 auto clauseAll = FindClauses(llvm::omp::Clause::OMPC_aligned); 1008 for (auto itr = clauseAll.first; itr != clauseAll.second; ++itr) { 1009 const auto &alignedClause{ 1010 std::get<parser::OmpClause::Aligned>(itr->second->u)}; 1011 const auto &alignedNameList{ 1012 std::get<std::list<parser::Name>>(alignedClause.v.t)}; 1013 for (auto const &var : alignedNameList) { 1014 if (alignedVars.count(*(var.symbol)) == 1) { 1015 context_.Say(itr->second->source, 1016 "List item '%s' present at multiple ALIGNED clauses"_err_en_US, 1017 var.ToString()); 1018 break; 1019 } 1020 alignedVars.insert(*(var.symbol)); 1021 } 1022 } 1023 } // SIMD 1024 1025 // 2.7.3 Single Construct Restriction 1026 if (GetContext().directive == llvm::omp::Directive::OMPD_end_single) { 1027 CheckNotAllowedIfClause( 1028 llvm::omp::Clause::OMPC_copyprivate, {llvm::omp::Clause::OMPC_nowait}); 1029 } 1030 1031 CheckRequireAtLeastOneOf(); 1032 } 1033 1034 void OmpStructureChecker::Enter(const parser::OmpClause &x) { 1035 SetContextClause(x); 1036 } 1037 1038 // Following clauses do not have a separate node in parse-tree.h. 1039 CHECK_SIMPLE_CLAUSE(AcqRel, OMPC_acq_rel) 1040 CHECK_SIMPLE_CLAUSE(Acquire, OMPC_acquire) 1041 CHECK_SIMPLE_CLAUSE(AtomicDefaultMemOrder, OMPC_atomic_default_mem_order) 1042 CHECK_SIMPLE_CLAUSE(Affinity, OMPC_affinity) 1043 CHECK_SIMPLE_CLAUSE(Allocate, OMPC_allocate) 1044 CHECK_SIMPLE_CLAUSE(Capture, OMPC_capture) 1045 CHECK_SIMPLE_CLAUSE(Copyin, OMPC_copyin) 1046 CHECK_SIMPLE_CLAUSE(Default, OMPC_default) 1047 CHECK_SIMPLE_CLAUSE(Depobj, OMPC_depobj) 1048 CHECK_SIMPLE_CLAUSE(Destroy, OMPC_destroy) 1049 CHECK_SIMPLE_CLAUSE(Detach, OMPC_detach) 1050 CHECK_SIMPLE_CLAUSE(Device, OMPC_device) 1051 CHECK_SIMPLE_CLAUSE(DeviceType, OMPC_device_type) 1052 CHECK_SIMPLE_CLAUSE(DistSchedule, OMPC_dist_schedule) 1053 CHECK_SIMPLE_CLAUSE(DynamicAllocators, OMPC_dynamic_allocators) 1054 CHECK_SIMPLE_CLAUSE(Exclusive, OMPC_exclusive) 1055 CHECK_SIMPLE_CLAUSE(Final, OMPC_final) 1056 CHECK_SIMPLE_CLAUSE(Flush, OMPC_flush) 1057 CHECK_SIMPLE_CLAUSE(From, OMPC_from) 1058 CHECK_SIMPLE_CLAUSE(Full, OMPC_full) 1059 CHECK_SIMPLE_CLAUSE(Hint, OMPC_hint) 1060 CHECK_SIMPLE_CLAUSE(InReduction, OMPC_in_reduction) 1061 CHECK_SIMPLE_CLAUSE(Inclusive, OMPC_inclusive) 1062 CHECK_SIMPLE_CLAUSE(Match, OMPC_match) 1063 CHECK_SIMPLE_CLAUSE(Nontemporal, OMPC_nontemporal) 1064 CHECK_SIMPLE_CLAUSE(Order, OMPC_order) 1065 CHECK_SIMPLE_CLAUSE(Read, OMPC_read) 1066 CHECK_SIMPLE_CLAUSE(ReverseOffload, OMPC_reverse_offload) 1067 CHECK_SIMPLE_CLAUSE(Threadprivate, OMPC_threadprivate) 1068 CHECK_SIMPLE_CLAUSE(Threads, OMPC_threads) 1069 CHECK_SIMPLE_CLAUSE(Inbranch, OMPC_inbranch) 1070 CHECK_SIMPLE_CLAUSE(IsDevicePtr, OMPC_is_device_ptr) 1071 CHECK_SIMPLE_CLAUSE(Link, OMPC_link) 1072 CHECK_SIMPLE_CLAUSE(Mergeable, OMPC_mergeable) 1073 CHECK_SIMPLE_CLAUSE(Nogroup, OMPC_nogroup) 1074 CHECK_SIMPLE_CLAUSE(Notinbranch, OMPC_notinbranch) 1075 CHECK_SIMPLE_CLAUSE(Nowait, OMPC_nowait) 1076 CHECK_SIMPLE_CLAUSE(Partial, OMPC_partial) 1077 CHECK_SIMPLE_CLAUSE(ProcBind, OMPC_proc_bind) 1078 CHECK_SIMPLE_CLAUSE(Release, OMPC_release) 1079 CHECK_SIMPLE_CLAUSE(Relaxed, OMPC_relaxed) 1080 CHECK_SIMPLE_CLAUSE(SeqCst, OMPC_seq_cst) 1081 CHECK_SIMPLE_CLAUSE(Simd, OMPC_simd) 1082 CHECK_SIMPLE_CLAUSE(Sizes, OMPC_sizes) 1083 CHECK_SIMPLE_CLAUSE(TaskReduction, OMPC_task_reduction) 1084 CHECK_SIMPLE_CLAUSE(To, OMPC_to) 1085 CHECK_SIMPLE_CLAUSE(UnifiedAddress, OMPC_unified_address) 1086 CHECK_SIMPLE_CLAUSE(UnifiedSharedMemory, OMPC_unified_shared_memory) 1087 CHECK_SIMPLE_CLAUSE(Uniform, OMPC_uniform) 1088 CHECK_SIMPLE_CLAUSE(Unknown, OMPC_unknown) 1089 CHECK_SIMPLE_CLAUSE(Untied, OMPC_untied) 1090 CHECK_SIMPLE_CLAUSE(UseDevicePtr, OMPC_use_device_ptr) 1091 CHECK_SIMPLE_CLAUSE(UsesAllocators, OMPC_uses_allocators) 1092 CHECK_SIMPLE_CLAUSE(Update, OMPC_update) 1093 CHECK_SIMPLE_CLAUSE(UseDeviceAddr, OMPC_use_device_addr) 1094 CHECK_SIMPLE_CLAUSE(Write, OMPC_write) 1095 CHECK_SIMPLE_CLAUSE(Init, OMPC_init) 1096 CHECK_SIMPLE_CLAUSE(Use, OMPC_use) 1097 CHECK_SIMPLE_CLAUSE(Novariants, OMPC_novariants) 1098 CHECK_SIMPLE_CLAUSE(Nocontext, OMPC_nocontext) 1099 CHECK_SIMPLE_CLAUSE(Filter, OMPC_filter) 1100 1101 CHECK_REQ_SCALAR_INT_CLAUSE(Grainsize, OMPC_grainsize) 1102 CHECK_REQ_SCALAR_INT_CLAUSE(NumTasks, OMPC_num_tasks) 1103 CHECK_REQ_SCALAR_INT_CLAUSE(NumTeams, OMPC_num_teams) 1104 CHECK_REQ_SCALAR_INT_CLAUSE(NumThreads, OMPC_num_threads) 1105 CHECK_REQ_SCALAR_INT_CLAUSE(Priority, OMPC_priority) 1106 CHECK_REQ_SCALAR_INT_CLAUSE(ThreadLimit, OMPC_thread_limit) 1107 1108 CHECK_REQ_CONSTANT_SCALAR_INT_CLAUSE(Collapse, OMPC_collapse) 1109 CHECK_REQ_CONSTANT_SCALAR_INT_CLAUSE(Safelen, OMPC_safelen) 1110 CHECK_REQ_CONSTANT_SCALAR_INT_CLAUSE(Simdlen, OMPC_simdlen) 1111 1112 // Restrictions specific to each clause are implemented apart from the 1113 // generalized restrictions. 1114 void OmpStructureChecker::Enter(const parser::OmpClause::Reduction &x) { 1115 CheckAllowed(llvm::omp::Clause::OMPC_reduction); 1116 if (CheckReductionOperators(x)) { 1117 CheckReductionTypeList(x); 1118 } 1119 } 1120 bool OmpStructureChecker::CheckReductionOperators( 1121 const parser::OmpClause::Reduction &x) { 1122 1123 const auto &definedOp{std::get<0>(x.v.t)}; 1124 bool ok = false; 1125 std::visit( 1126 common::visitors{ 1127 [&](const parser::DefinedOperator &dOpr) { 1128 const auto &intrinsicOp{ 1129 std::get<parser::DefinedOperator::IntrinsicOperator>(dOpr.u)}; 1130 ok = CheckIntrinsicOperator(intrinsicOp); 1131 }, 1132 [&](const parser::ProcedureDesignator &procD) { 1133 const parser::Name *name{std::get_if<parser::Name>(&procD.u)}; 1134 if (name) { 1135 if (name->source == "max" || name->source == "min" || 1136 name->source == "iand" || name->source == "ior" || 1137 name->source == "ieor") { 1138 ok = true; 1139 } else { 1140 context_.Say(GetContext().clauseSource, 1141 "Invalid reduction identifier in REDUCTION clause."_err_en_US, 1142 ContextDirectiveAsFortran()); 1143 } 1144 } 1145 }, 1146 }, 1147 definedOp.u); 1148 1149 return ok; 1150 } 1151 bool OmpStructureChecker::CheckIntrinsicOperator( 1152 const parser::DefinedOperator::IntrinsicOperator &op) { 1153 1154 switch (op) { 1155 case parser::DefinedOperator::IntrinsicOperator::Add: 1156 case parser::DefinedOperator::IntrinsicOperator::Subtract: 1157 case parser::DefinedOperator::IntrinsicOperator::Multiply: 1158 case parser::DefinedOperator::IntrinsicOperator::AND: 1159 case parser::DefinedOperator::IntrinsicOperator::OR: 1160 case parser::DefinedOperator::IntrinsicOperator::EQV: 1161 case parser::DefinedOperator::IntrinsicOperator::NEQV: 1162 return true; 1163 default: 1164 context_.Say(GetContext().clauseSource, 1165 "Invalid reduction operator in REDUCTION clause."_err_en_US, 1166 ContextDirectiveAsFortran()); 1167 } 1168 return false; 1169 } 1170 1171 void OmpStructureChecker::CheckReductionTypeList( 1172 const parser::OmpClause::Reduction &x) { 1173 const auto &ompObjectList{std::get<parser::OmpObjectList>(x.v.t)}; 1174 CheckIntentInPointerAndDefinable( 1175 ompObjectList, llvm::omp::Clause::OMPC_reduction); 1176 CheckReductionArraySection(ompObjectList); 1177 CheckMultipleAppearanceAcrossContext(ompObjectList); 1178 } 1179 1180 void OmpStructureChecker::CheckIntentInPointerAndDefinable( 1181 const parser::OmpObjectList &objectList, const llvm::omp::Clause clause) { 1182 for (const auto &ompObject : objectList.v) { 1183 if (const auto *name{parser::Unwrap<parser::Name>(ompObject)}) { 1184 if (const auto *symbol{name->symbol}) { 1185 if (IsPointer(symbol->GetUltimate()) && 1186 IsIntentIn(symbol->GetUltimate())) { 1187 context_.Say(GetContext().clauseSource, 1188 "Pointer '%s' with the INTENT(IN) attribute may not appear " 1189 "in a %s clause"_err_en_US, 1190 symbol->name(), 1191 parser::ToUpperCaseLetters(getClauseName(clause).str())); 1192 } 1193 if (auto msg{ 1194 WhyNotModifiable(*symbol, context_.FindScope(name->source))}) { 1195 context_.Say(GetContext().clauseSource, 1196 "Variable '%s' on the %s clause is not definable"_err_en_US, 1197 symbol->name(), 1198 parser::ToUpperCaseLetters(getClauseName(clause).str())); 1199 } 1200 } 1201 } 1202 } 1203 } 1204 1205 void OmpStructureChecker::CheckReductionArraySection( 1206 const parser::OmpObjectList &ompObjectList) { 1207 for (const auto &ompObject : ompObjectList.v) { 1208 if (const auto *dataRef{parser::Unwrap<parser::DataRef>(ompObject)}) { 1209 if (const auto *arrayElement{ 1210 parser::Unwrap<parser::ArrayElement>(ompObject)}) { 1211 if (arrayElement) { 1212 CheckArraySection(*arrayElement, GetLastName(*dataRef), 1213 llvm::omp::Clause::OMPC_reduction); 1214 } 1215 } 1216 } 1217 } 1218 } 1219 1220 void OmpStructureChecker::CheckMultipleAppearanceAcrossContext( 1221 const parser::OmpObjectList &redObjectList) { 1222 // TODO: Verify the assumption here that the immediately enclosing region is 1223 // the parallel region to which the worksharing construct having reduction 1224 // binds to. 1225 if (auto *enclosingContext{GetEnclosingDirContext()}) { 1226 for (auto it : enclosingContext->clauseInfo) { 1227 llvmOmpClause type = it.first; 1228 const auto *clause = it.second; 1229 if (llvm::omp::privateReductionSet.test(type)) { 1230 if (const auto *objList{GetOmpObjectList(*clause)}) { 1231 for (const auto &ompObject : objList->v) { 1232 if (const auto *name{parser::Unwrap<parser::Name>(ompObject)}) { 1233 if (const auto *symbol{name->symbol}) { 1234 for (const auto &redOmpObject : redObjectList.v) { 1235 if (const auto *rname{ 1236 parser::Unwrap<parser::Name>(redOmpObject)}) { 1237 if (const auto *rsymbol{rname->symbol}) { 1238 if (rsymbol->name() == symbol->name()) { 1239 context_.Say(GetContext().clauseSource, 1240 "%s variable '%s' is %s in outer context must" 1241 " be shared in the parallel regions to which any" 1242 " of the worksharing regions arising from the " 1243 "worksharing" 1244 " construct bind."_err_en_US, 1245 parser::ToUpperCaseLetters( 1246 getClauseName(llvm::omp::Clause::OMPC_reduction) 1247 .str()), 1248 symbol->name(), 1249 parser::ToUpperCaseLetters( 1250 getClauseName(type).str())); 1251 } 1252 } 1253 } 1254 } 1255 } 1256 } 1257 } 1258 } 1259 } 1260 } 1261 } 1262 } 1263 1264 void OmpStructureChecker::Enter(const parser::OmpClause::Ordered &x) { 1265 CheckAllowed(llvm::omp::Clause::OMPC_ordered); 1266 // the parameter of ordered clause is optional 1267 if (const auto &expr{x.v}) { 1268 RequiresConstantPositiveParameter(llvm::omp::Clause::OMPC_ordered, *expr); 1269 // 2.8.3 Loop SIMD Construct Restriction 1270 if (llvm::omp::doSimdSet.test(GetContext().directive)) { 1271 context_.Say(GetContext().clauseSource, 1272 "No ORDERED clause with a parameter can be specified " 1273 "on the %s directive"_err_en_US, 1274 ContextDirectiveAsFortran()); 1275 } 1276 } 1277 } 1278 1279 void OmpStructureChecker::Enter(const parser::OmpClause::Shared &x) { 1280 CheckAllowed(llvm::omp::Clause::OMPC_shared); 1281 CheckIsVarPartOfAnotherVar(GetContext().clauseSource, x.v); 1282 } 1283 void OmpStructureChecker::Enter(const parser::OmpClause::Private &x) { 1284 CheckAllowed(llvm::omp::Clause::OMPC_private); 1285 CheckIsVarPartOfAnotherVar(GetContext().clauseSource, x.v); 1286 CheckIntentInPointer(x.v, llvm::omp::Clause::OMPC_private); 1287 } 1288 1289 bool OmpStructureChecker::IsDataRefTypeParamInquiry( 1290 const parser::DataRef *dataRef) { 1291 bool dataRefIsTypeParamInquiry{false}; 1292 if (const auto *structComp{ 1293 parser::Unwrap<parser::StructureComponent>(dataRef)}) { 1294 if (const auto *compSymbol{structComp->component.symbol}) { 1295 if (const auto *compSymbolMiscDetails{ 1296 std::get_if<MiscDetails>(&compSymbol->details())}) { 1297 const auto detailsKind = compSymbolMiscDetails->kind(); 1298 dataRefIsTypeParamInquiry = 1299 (detailsKind == MiscDetails::Kind::KindParamInquiry || 1300 detailsKind == MiscDetails::Kind::LenParamInquiry); 1301 } else if (compSymbol->has<TypeParamDetails>()) { 1302 dataRefIsTypeParamInquiry = true; 1303 } 1304 } 1305 } 1306 return dataRefIsTypeParamInquiry; 1307 } 1308 1309 void OmpStructureChecker::CheckIsVarPartOfAnotherVar( 1310 const parser::CharBlock &source, const parser::OmpObjectList &objList) { 1311 1312 for (const auto &ompObject : objList.v) { 1313 std::visit( 1314 common::visitors{ 1315 [&](const parser::Designator &designator) { 1316 if (const auto *dataRef{ 1317 std::get_if<parser::DataRef>(&designator.u)}) { 1318 if (IsDataRefTypeParamInquiry(dataRef)) { 1319 context_.Say(source, 1320 "A type parameter inquiry cannot appear in an ALLOCATE directive"_err_en_US); 1321 } else if (parser::Unwrap<parser::StructureComponent>( 1322 ompObject) || 1323 parser::Unwrap<parser::ArrayElement>(ompObject)) { 1324 context_.Say(source, 1325 "A variable that is part of another variable (as an " 1326 "array or structure element)" 1327 " cannot appear in a PRIVATE or SHARED clause or on the ALLOCATE directive."_err_en_US); 1328 } 1329 } 1330 }, 1331 [&](const parser::Name &name) {}, 1332 }, 1333 ompObject.u); 1334 } 1335 } 1336 1337 void OmpStructureChecker::Enter(const parser::OmpClause::Firstprivate &x) { 1338 CheckAllowed(llvm::omp::Clause::OMPC_firstprivate); 1339 CheckIsLoopIvPartOfClause(llvmOmpClause::OMPC_firstprivate, x.v); 1340 1341 SymbolSourceMap currSymbols; 1342 GetSymbolsInObjectList(x.v, currSymbols); 1343 1344 DirectivesClauseTriple dirClauseTriple; 1345 // Check firstprivate variables in worksharing constructs 1346 dirClauseTriple.emplace(llvm::omp::Directive::OMPD_do, 1347 std::make_pair( 1348 llvm::omp::Directive::OMPD_parallel, llvm::omp::privateReductionSet)); 1349 dirClauseTriple.emplace(llvm::omp::Directive::OMPD_sections, 1350 std::make_pair( 1351 llvm::omp::Directive::OMPD_parallel, llvm::omp::privateReductionSet)); 1352 dirClauseTriple.emplace(llvm::omp::Directive::OMPD_single, 1353 std::make_pair( 1354 llvm::omp::Directive::OMPD_parallel, llvm::omp::privateReductionSet)); 1355 // Check firstprivate variables in distribute construct 1356 dirClauseTriple.emplace(llvm::omp::Directive::OMPD_distribute, 1357 std::make_pair( 1358 llvm::omp::Directive::OMPD_teams, llvm::omp::privateReductionSet)); 1359 dirClauseTriple.emplace(llvm::omp::Directive::OMPD_distribute, 1360 std::make_pair(llvm::omp::Directive::OMPD_target_teams, 1361 llvm::omp::privateReductionSet)); 1362 // Check firstprivate variables in task and taskloop constructs 1363 dirClauseTriple.emplace(llvm::omp::Directive::OMPD_task, 1364 std::make_pair(llvm::omp::Directive::OMPD_parallel, 1365 OmpClauseSet{llvm::omp::Clause::OMPC_reduction})); 1366 dirClauseTriple.emplace(llvm::omp::Directive::OMPD_taskloop, 1367 std::make_pair(llvm::omp::Directive::OMPD_parallel, 1368 OmpClauseSet{llvm::omp::Clause::OMPC_reduction})); 1369 1370 CheckPrivateSymbolsInOuterCxt( 1371 currSymbols, dirClauseTriple, llvm::omp::Clause::OMPC_firstprivate); 1372 } 1373 1374 void OmpStructureChecker::CheckIsLoopIvPartOfClause( 1375 llvmOmpClause clause, const parser::OmpObjectList &ompObjectList) { 1376 for (const auto &ompObject : ompObjectList.v) { 1377 if (const parser::Name * name{parser::Unwrap<parser::Name>(ompObject)}) { 1378 if (name->symbol == GetContext().loopIV) { 1379 context_.Say(name->source, 1380 "DO iteration variable %s is not allowed in %s clause."_err_en_US, 1381 name->ToString(), 1382 parser::ToUpperCaseLetters(getClauseName(clause).str())); 1383 } 1384 } 1385 } 1386 } 1387 // Following clauses have a seperate node in parse-tree.h. 1388 // Atomic-clause 1389 CHECK_SIMPLE_PARSER_CLAUSE(OmpAtomicRead, OMPC_read) 1390 CHECK_SIMPLE_PARSER_CLAUSE(OmpAtomicWrite, OMPC_write) 1391 CHECK_SIMPLE_PARSER_CLAUSE(OmpAtomicUpdate, OMPC_update) 1392 CHECK_SIMPLE_PARSER_CLAUSE(OmpAtomicCapture, OMPC_capture) 1393 1394 void OmpStructureChecker::Leave(const parser::OmpAtomicRead &) { 1395 CheckNotAllowedIfClause(llvm::omp::Clause::OMPC_read, 1396 {llvm::omp::Clause::OMPC_release, llvm::omp::Clause::OMPC_acq_rel}); 1397 } 1398 void OmpStructureChecker::Leave(const parser::OmpAtomicWrite &) { 1399 CheckNotAllowedIfClause(llvm::omp::Clause::OMPC_write, 1400 {llvm::omp::Clause::OMPC_acquire, llvm::omp::Clause::OMPC_acq_rel}); 1401 } 1402 void OmpStructureChecker::Leave(const parser::OmpAtomicUpdate &) { 1403 CheckNotAllowedIfClause(llvm::omp::Clause::OMPC_update, 1404 {llvm::omp::Clause::OMPC_acquire, llvm::omp::Clause::OMPC_acq_rel}); 1405 } 1406 // OmpAtomic node represents atomic directive without atomic-clause. 1407 // atomic-clause - READ,WRITE,UPDATE,CAPTURE. 1408 void OmpStructureChecker::Leave(const parser::OmpAtomic &) { 1409 if (const auto *clause{FindClause(llvm::omp::Clause::OMPC_acquire)}) { 1410 context_.Say(clause->source, 1411 "Clause ACQUIRE is not allowed on the ATOMIC directive"_err_en_US); 1412 } 1413 if (const auto *clause{FindClause(llvm::omp::Clause::OMPC_acq_rel)}) { 1414 context_.Say(clause->source, 1415 "Clause ACQ_REL is not allowed on the ATOMIC directive"_err_en_US); 1416 } 1417 } 1418 // Restrictions specific to each clause are implemented apart from the 1419 // generalized restrictions. 1420 void OmpStructureChecker::Enter(const parser::OmpClause::Aligned &x) { 1421 CheckAllowed(llvm::omp::Clause::OMPC_aligned); 1422 1423 if (const auto &expr{ 1424 std::get<std::optional<parser::ScalarIntConstantExpr>>(x.v.t)}) { 1425 RequiresConstantPositiveParameter(llvm::omp::Clause::OMPC_aligned, *expr); 1426 } 1427 // 2.8.1 TODO: list-item attribute check 1428 } 1429 void OmpStructureChecker::Enter(const parser::OmpClause::Defaultmap &x) { 1430 CheckAllowed(llvm::omp::Clause::OMPC_defaultmap); 1431 using VariableCategory = parser::OmpDefaultmapClause::VariableCategory; 1432 if (!std::get<std::optional<VariableCategory>>(x.v.t)) { 1433 context_.Say(GetContext().clauseSource, 1434 "The argument TOFROM:SCALAR must be specified on the DEFAULTMAP " 1435 "clause"_err_en_US); 1436 } 1437 } 1438 void OmpStructureChecker::Enter(const parser::OmpClause::If &x) { 1439 CheckAllowed(llvm::omp::Clause::OMPC_if); 1440 using dirNameModifier = parser::OmpIfClause::DirectiveNameModifier; 1441 static std::unordered_map<dirNameModifier, OmpDirectiveSet> 1442 dirNameModifierMap{{dirNameModifier::Parallel, llvm::omp::parallelSet}, 1443 {dirNameModifier::Target, llvm::omp::targetSet}, 1444 {dirNameModifier::TargetEnterData, 1445 {llvm::omp::Directive::OMPD_target_enter_data}}, 1446 {dirNameModifier::TargetExitData, 1447 {llvm::omp::Directive::OMPD_target_exit_data}}, 1448 {dirNameModifier::TargetData, 1449 {llvm::omp::Directive::OMPD_target_data}}, 1450 {dirNameModifier::TargetUpdate, 1451 {llvm::omp::Directive::OMPD_target_update}}, 1452 {dirNameModifier::Task, {llvm::omp::Directive::OMPD_task}}, 1453 {dirNameModifier::Taskloop, llvm::omp::taskloopSet}}; 1454 if (const auto &directiveName{ 1455 std::get<std::optional<dirNameModifier>>(x.v.t)}) { 1456 auto search{dirNameModifierMap.find(*directiveName)}; 1457 if (search == dirNameModifierMap.end() || 1458 !search->second.test(GetContext().directive)) { 1459 context_ 1460 .Say(GetContext().clauseSource, 1461 "Unmatched directive name modifier %s on the IF clause"_err_en_US, 1462 parser::ToUpperCaseLetters( 1463 parser::OmpIfClause::EnumToString(*directiveName))) 1464 .Attach( 1465 GetContext().directiveSource, "Cannot apply to directive"_en_US); 1466 } 1467 } 1468 } 1469 1470 void OmpStructureChecker::Enter(const parser::OmpClause::Linear &x) { 1471 CheckAllowed(llvm::omp::Clause::OMPC_linear); 1472 1473 // 2.7 Loop Construct Restriction 1474 if ((llvm::omp::doSet | llvm::omp::simdSet).test(GetContext().directive)) { 1475 if (std::holds_alternative<parser::OmpLinearClause::WithModifier>(x.v.u)) { 1476 context_.Say(GetContext().clauseSource, 1477 "A modifier may not be specified in a LINEAR clause " 1478 "on the %s directive"_err_en_US, 1479 ContextDirectiveAsFortran()); 1480 } 1481 } 1482 } 1483 1484 void OmpStructureChecker::CheckAllowedMapTypes( 1485 const parser::OmpMapType::Type &type, 1486 const std::list<parser::OmpMapType::Type> &allowedMapTypeList) { 1487 const auto found{std::find( 1488 std::begin(allowedMapTypeList), std::end(allowedMapTypeList), type)}; 1489 if (found == std::end(allowedMapTypeList)) { 1490 std::string commaSeperatedMapTypes; 1491 llvm::interleave( 1492 allowedMapTypeList.begin(), allowedMapTypeList.end(), 1493 [&](const parser::OmpMapType::Type &mapType) { 1494 commaSeperatedMapTypes.append(parser::ToUpperCaseLetters( 1495 parser::OmpMapType::EnumToString(mapType))); 1496 }, 1497 [&] { commaSeperatedMapTypes.append(", "); }); 1498 context_.Say(GetContext().clauseSource, 1499 "Only the %s map types are permitted " 1500 "for MAP clauses on the %s directive"_err_en_US, 1501 commaSeperatedMapTypes, ContextDirectiveAsFortran()); 1502 } 1503 } 1504 1505 void OmpStructureChecker::Enter(const parser::OmpClause::Map &x) { 1506 CheckAllowed(llvm::omp::Clause::OMPC_map); 1507 1508 if (const auto &maptype{std::get<std::optional<parser::OmpMapType>>(x.v.t)}) { 1509 using Type = parser::OmpMapType::Type; 1510 const Type &type{std::get<Type>(maptype->t)}; 1511 switch (GetContext().directive) { 1512 case llvm::omp::Directive::OMPD_target: 1513 case llvm::omp::Directive::OMPD_target_teams: 1514 case llvm::omp::Directive::OMPD_target_teams_distribute: 1515 case llvm::omp::Directive::OMPD_target_teams_distribute_simd: 1516 case llvm::omp::Directive::OMPD_target_teams_distribute_parallel_do: 1517 case llvm::omp::Directive::OMPD_target_teams_distribute_parallel_do_simd: 1518 case llvm::omp::Directive::OMPD_target_data: 1519 CheckAllowedMapTypes( 1520 type, {Type::To, Type::From, Type::Tofrom, Type::Alloc}); 1521 break; 1522 case llvm::omp::Directive::OMPD_target_enter_data: 1523 CheckAllowedMapTypes(type, {Type::To, Type::Alloc}); 1524 break; 1525 case llvm::omp::Directive::OMPD_target_exit_data: 1526 CheckAllowedMapTypes(type, {Type::From, Type::Release, Type::Delete}); 1527 break; 1528 default: 1529 break; 1530 } 1531 } 1532 } 1533 1534 bool OmpStructureChecker::ScheduleModifierHasType( 1535 const parser::OmpScheduleClause &x, 1536 const parser::OmpScheduleModifierType::ModType &type) { 1537 const auto &modifier{ 1538 std::get<std::optional<parser::OmpScheduleModifier>>(x.t)}; 1539 if (modifier) { 1540 const auto &modType1{ 1541 std::get<parser::OmpScheduleModifier::Modifier1>(modifier->t)}; 1542 const auto &modType2{ 1543 std::get<std::optional<parser::OmpScheduleModifier::Modifier2>>( 1544 modifier->t)}; 1545 if (modType1.v.v == type || (modType2 && modType2->v.v == type)) { 1546 return true; 1547 } 1548 } 1549 return false; 1550 } 1551 void OmpStructureChecker::Enter(const parser::OmpClause::Schedule &x) { 1552 CheckAllowed(llvm::omp::Clause::OMPC_schedule); 1553 const parser::OmpScheduleClause &scheduleClause = x.v; 1554 1555 // 2.7 Loop Construct Restriction 1556 if (llvm::omp::doSet.test(GetContext().directive)) { 1557 const auto &kind{std::get<1>(scheduleClause.t)}; 1558 const auto &chunk{std::get<2>(scheduleClause.t)}; 1559 if (chunk) { 1560 if (kind == parser::OmpScheduleClause::ScheduleType::Runtime || 1561 kind == parser::OmpScheduleClause::ScheduleType::Auto) { 1562 context_.Say(GetContext().clauseSource, 1563 "When SCHEDULE clause has %s specified, " 1564 "it must not have chunk size specified"_err_en_US, 1565 parser::ToUpperCaseLetters( 1566 parser::OmpScheduleClause::EnumToString(kind))); 1567 } 1568 if (const auto &chunkExpr{std::get<std::optional<parser::ScalarIntExpr>>( 1569 scheduleClause.t)}) { 1570 RequiresPositiveParameter( 1571 llvm::omp::Clause::OMPC_schedule, *chunkExpr, "chunk size"); 1572 } 1573 } 1574 1575 if (ScheduleModifierHasType(scheduleClause, 1576 parser::OmpScheduleModifierType::ModType::Nonmonotonic)) { 1577 if (kind != parser::OmpScheduleClause::ScheduleType::Dynamic && 1578 kind != parser::OmpScheduleClause::ScheduleType::Guided) { 1579 context_.Say(GetContext().clauseSource, 1580 "The NONMONOTONIC modifier can only be specified with " 1581 "SCHEDULE(DYNAMIC) or SCHEDULE(GUIDED)"_err_en_US); 1582 } 1583 } 1584 } 1585 } 1586 1587 void OmpStructureChecker::Enter(const parser::OmpClause::Depend &x) { 1588 CheckAllowed(llvm::omp::Clause::OMPC_depend); 1589 if (const auto *inOut{std::get_if<parser::OmpDependClause::InOut>(&x.v.u)}) { 1590 const auto &designators{std::get<std::list<parser::Designator>>(inOut->t)}; 1591 for (const auto &ele : designators) { 1592 if (const auto *dataRef{std::get_if<parser::DataRef>(&ele.u)}) { 1593 CheckDependList(*dataRef); 1594 if (const auto *arr{ 1595 std::get_if<common::Indirection<parser::ArrayElement>>( 1596 &dataRef->u)}) { 1597 CheckArraySection(arr->value(), GetLastName(*dataRef), 1598 llvm::omp::Clause::OMPC_depend); 1599 } 1600 } 1601 } 1602 } 1603 } 1604 1605 void OmpStructureChecker::Enter(const parser::OmpClause::Copyprivate &x) { 1606 CheckAllowed(llvm::omp::Clause::OMPC_copyprivate); 1607 CheckIntentInPointer(x.v, llvm::omp::Clause::OMPC_copyprivate); 1608 } 1609 1610 void OmpStructureChecker::Enter(const parser::OmpClause::Lastprivate &x) { 1611 CheckAllowed(llvm::omp::Clause::OMPC_lastprivate); 1612 1613 DirectivesClauseTriple dirClauseTriple; 1614 SymbolSourceMap currSymbols; 1615 GetSymbolsInObjectList(x.v, currSymbols); 1616 CheckDefinableObjects(currSymbols, GetClauseKindForParserClass(x)); 1617 1618 // Check lastprivate variables in worksharing constructs 1619 dirClauseTriple.emplace(llvm::omp::Directive::OMPD_do, 1620 std::make_pair( 1621 llvm::omp::Directive::OMPD_parallel, llvm::omp::privateReductionSet)); 1622 dirClauseTriple.emplace(llvm::omp::Directive::OMPD_sections, 1623 std::make_pair( 1624 llvm::omp::Directive::OMPD_parallel, llvm::omp::privateReductionSet)); 1625 1626 CheckPrivateSymbolsInOuterCxt( 1627 currSymbols, dirClauseTriple, GetClauseKindForParserClass(x)); 1628 } 1629 1630 llvm::StringRef OmpStructureChecker::getClauseName(llvm::omp::Clause clause) { 1631 return llvm::omp::getOpenMPClauseName(clause); 1632 } 1633 1634 llvm::StringRef OmpStructureChecker::getDirectiveName( 1635 llvm::omp::Directive directive) { 1636 return llvm::omp::getOpenMPDirectiveName(directive); 1637 } 1638 1639 void OmpStructureChecker::CheckDependList(const parser::DataRef &d) { 1640 std::visit( 1641 common::visitors{ 1642 [&](const common::Indirection<parser::ArrayElement> &elem) { 1643 // Check if the base element is valid on Depend Clause 1644 CheckDependList(elem.value().base); 1645 }, 1646 [&](const common::Indirection<parser::StructureComponent> &) { 1647 context_.Say(GetContext().clauseSource, 1648 "A variable that is part of another variable " 1649 "(such as an element of a structure) but is not an array " 1650 "element or an array section cannot appear in a DEPEND " 1651 "clause"_err_en_US); 1652 }, 1653 [&](const common::Indirection<parser::CoindexedNamedObject> &) { 1654 context_.Say(GetContext().clauseSource, 1655 "Coarrays are not supported in DEPEND clause"_err_en_US); 1656 }, 1657 [&](const parser::Name &) { return; }, 1658 }, 1659 d.u); 1660 } 1661 1662 // Called from both Reduction and Depend clause. 1663 void OmpStructureChecker::CheckArraySection( 1664 const parser::ArrayElement &arrayElement, const parser::Name &name, 1665 const llvm::omp::Clause clause) { 1666 if (!arrayElement.subscripts.empty()) { 1667 for (const auto &subscript : arrayElement.subscripts) { 1668 if (const auto *triplet{ 1669 std::get_if<parser::SubscriptTriplet>(&subscript.u)}) { 1670 if (std::get<0>(triplet->t) && std::get<1>(triplet->t)) { 1671 const auto &lower{std::get<0>(triplet->t)}; 1672 const auto &upper{std::get<1>(triplet->t)}; 1673 if (lower && upper) { 1674 const auto lval{GetIntValue(lower)}; 1675 const auto uval{GetIntValue(upper)}; 1676 if (lval && uval && *uval < *lval) { 1677 context_.Say(GetContext().clauseSource, 1678 "'%s' in %s clause" 1679 " is a zero size array section"_err_en_US, 1680 name.ToString(), 1681 parser::ToUpperCaseLetters(getClauseName(clause).str())); 1682 break; 1683 } else if (std::get<2>(triplet->t)) { 1684 const auto &strideExpr{std::get<2>(triplet->t)}; 1685 if (strideExpr) { 1686 if (clause == llvm::omp::Clause::OMPC_depend) { 1687 context_.Say(GetContext().clauseSource, 1688 "Stride should not be specified for array section in " 1689 "DEPEND " 1690 "clause"_err_en_US); 1691 } 1692 const auto stride{GetIntValue(strideExpr)}; 1693 if ((stride && stride != 1)) { 1694 context_.Say(GetContext().clauseSource, 1695 "A list item that appears in a REDUCTION clause" 1696 " should have a contiguous storage array section."_err_en_US, 1697 ContextDirectiveAsFortran()); 1698 break; 1699 } 1700 } 1701 } 1702 } 1703 } 1704 } 1705 } 1706 } 1707 } 1708 1709 void OmpStructureChecker::CheckIntentInPointer( 1710 const parser::OmpObjectList &objectList, const llvm::omp::Clause clause) { 1711 SymbolSourceMap symbols; 1712 GetSymbolsInObjectList(objectList, symbols); 1713 for (auto it{symbols.begin()}; it != symbols.end(); ++it) { 1714 const auto *symbol{it->first}; 1715 const auto source{it->second}; 1716 if (IsPointer(*symbol) && IsIntentIn(*symbol)) { 1717 context_.Say(source, 1718 "Pointer '%s' with the INTENT(IN) attribute may not appear " 1719 "in a %s clause"_err_en_US, 1720 symbol->name(), 1721 parser::ToUpperCaseLetters(getClauseName(clause).str())); 1722 } 1723 } 1724 } 1725 1726 void OmpStructureChecker::GetSymbolsInObjectList( 1727 const parser::OmpObjectList &objectList, SymbolSourceMap &symbols) { 1728 for (const auto &ompObject : objectList.v) { 1729 if (const auto *name{parser::Unwrap<parser::Name>(ompObject)}) { 1730 if (const auto *symbol{name->symbol}) { 1731 if (const auto *commonBlockDetails{ 1732 symbol->detailsIf<CommonBlockDetails>()}) { 1733 for (const auto &object : commonBlockDetails->objects()) { 1734 symbols.emplace(&object->GetUltimate(), name->source); 1735 } 1736 } else { 1737 symbols.emplace(&symbol->GetUltimate(), name->source); 1738 } 1739 } 1740 } 1741 } 1742 } 1743 1744 void OmpStructureChecker::CheckDefinableObjects( 1745 SymbolSourceMap &symbols, const llvm::omp::Clause clause) { 1746 for (auto it{symbols.begin()}; it != symbols.end(); ++it) { 1747 const auto *symbol{it->first}; 1748 const auto source{it->second}; 1749 if (auto msg{WhyNotModifiable(*symbol, context_.FindScope(source))}) { 1750 context_ 1751 .Say(source, 1752 "Variable '%s' on the %s clause is not definable"_err_en_US, 1753 symbol->name(), 1754 parser::ToUpperCaseLetters(getClauseName(clause).str())) 1755 .Attach(source, std::move(*msg), symbol->name()); 1756 } 1757 } 1758 } 1759 1760 void OmpStructureChecker::CheckPrivateSymbolsInOuterCxt( 1761 SymbolSourceMap &currSymbols, DirectivesClauseTriple &dirClauseTriple, 1762 const llvm::omp::Clause currClause) { 1763 SymbolSourceMap enclosingSymbols; 1764 auto range{dirClauseTriple.equal_range(GetContext().directive)}; 1765 for (auto dirIter{range.first}; dirIter != range.second; ++dirIter) { 1766 auto enclosingDir{dirIter->second.first}; 1767 auto enclosingClauseSet{dirIter->second.second}; 1768 if (auto *enclosingContext{GetEnclosingContextWithDir(enclosingDir)}) { 1769 for (auto it{enclosingContext->clauseInfo.begin()}; 1770 it != enclosingContext->clauseInfo.end(); ++it) { 1771 if (enclosingClauseSet.test(it->first)) { 1772 if (const auto *ompObjectList{GetOmpObjectList(*it->second)}) { 1773 GetSymbolsInObjectList(*ompObjectList, enclosingSymbols); 1774 } 1775 } 1776 } 1777 1778 // Check if the symbols in current context are private in outer context 1779 for (auto iter{currSymbols.begin()}; iter != currSymbols.end(); ++iter) { 1780 const auto *symbol{iter->first}; 1781 const auto source{iter->second}; 1782 if (enclosingSymbols.find(symbol) != enclosingSymbols.end()) { 1783 context_.Say(source, 1784 "%s variable '%s' is PRIVATE in outer context"_err_en_US, 1785 parser::ToUpperCaseLetters(getClauseName(currClause).str()), 1786 symbol->name()); 1787 } 1788 } 1789 } 1790 } 1791 } 1792 1793 void OmpStructureChecker::CheckWorkshareBlockStmts( 1794 const parser::Block &block, parser::CharBlock source) { 1795 OmpWorkshareBlockChecker ompWorkshareBlockChecker{context_, source}; 1796 1797 for (auto it{block.begin()}; it != block.end(); ++it) { 1798 if (parser::Unwrap<parser::AssignmentStmt>(*it) || 1799 parser::Unwrap<parser::ForallStmt>(*it) || 1800 parser::Unwrap<parser::ForallConstruct>(*it) || 1801 parser::Unwrap<parser::WhereStmt>(*it) || 1802 parser::Unwrap<parser::WhereConstruct>(*it)) { 1803 parser::Walk(*it, ompWorkshareBlockChecker); 1804 } else if (const auto *ompConstruct{ 1805 parser::Unwrap<parser::OpenMPConstruct>(*it)}) { 1806 if (const auto *ompAtomicConstruct{ 1807 std::get_if<parser::OpenMPAtomicConstruct>(&ompConstruct->u)}) { 1808 // Check if assignment statements in the enclosing OpenMP Atomic 1809 // construct are allowed in the Workshare construct 1810 parser::Walk(*ompAtomicConstruct, ompWorkshareBlockChecker); 1811 } else if (const auto *ompCriticalConstruct{ 1812 std::get_if<parser::OpenMPCriticalConstruct>( 1813 &ompConstruct->u)}) { 1814 // All the restrictions on the Workshare construct apply to the 1815 // statements in the enclosing critical constructs 1816 const auto &criticalBlock{ 1817 std::get<parser::Block>(ompCriticalConstruct->t)}; 1818 CheckWorkshareBlockStmts(criticalBlock, source); 1819 } else { 1820 // Check if OpenMP constructs enclosed in the Workshare construct are 1821 // 'Parallel' constructs 1822 auto currentDir{llvm::omp::Directive::OMPD_unknown}; 1823 const OmpDirectiveSet parallelDirSet{ 1824 llvm::omp::Directive::OMPD_parallel, 1825 llvm::omp::Directive::OMPD_parallel_do, 1826 llvm::omp::Directive::OMPD_parallel_sections, 1827 llvm::omp::Directive::OMPD_parallel_workshare, 1828 llvm::omp::Directive::OMPD_parallel_do_simd}; 1829 1830 if (const auto *ompBlockConstruct{ 1831 std::get_if<parser::OpenMPBlockConstruct>(&ompConstruct->u)}) { 1832 const auto &beginBlockDir{ 1833 std::get<parser::OmpBeginBlockDirective>(ompBlockConstruct->t)}; 1834 const auto &beginDir{ 1835 std::get<parser::OmpBlockDirective>(beginBlockDir.t)}; 1836 currentDir = beginDir.v; 1837 } else if (const auto *ompLoopConstruct{ 1838 std::get_if<parser::OpenMPLoopConstruct>( 1839 &ompConstruct->u)}) { 1840 const auto &beginLoopDir{ 1841 std::get<parser::OmpBeginLoopDirective>(ompLoopConstruct->t)}; 1842 const auto &beginDir{ 1843 std::get<parser::OmpLoopDirective>(beginLoopDir.t)}; 1844 currentDir = beginDir.v; 1845 } else if (const auto *ompSectionsConstruct{ 1846 std::get_if<parser::OpenMPSectionsConstruct>( 1847 &ompConstruct->u)}) { 1848 const auto &beginSectionsDir{ 1849 std::get<parser::OmpBeginSectionsDirective>( 1850 ompSectionsConstruct->t)}; 1851 const auto &beginDir{ 1852 std::get<parser::OmpSectionsDirective>(beginSectionsDir.t)}; 1853 currentDir = beginDir.v; 1854 } 1855 1856 if (!parallelDirSet.test(currentDir)) { 1857 context_.Say(source, 1858 "OpenMP constructs enclosed in WORKSHARE construct may consist " 1859 "of ATOMIC, CRITICAL or PARALLEL constructs only"_err_en_US); 1860 } 1861 } 1862 } else { 1863 context_.Say(source, 1864 "The structured block in a WORKSHARE construct may consist of only " 1865 "SCALAR or ARRAY assignments, FORALL or WHERE statements, " 1866 "FORALL, WHERE, ATOMIC, CRITICAL or PARALLEL constructs"_err_en_US); 1867 } 1868 } 1869 } 1870 1871 const parser::OmpObjectList *OmpStructureChecker::GetOmpObjectList( 1872 const parser::OmpClause &clause) { 1873 1874 // Clauses with OmpObjectList as its data member 1875 using MemberObjectListClauses = std::tuple<parser::OmpClause::Copyprivate, 1876 parser::OmpClause::Copyin, parser::OmpClause::Firstprivate, 1877 parser::OmpClause::From, parser::OmpClause::Lastprivate, 1878 parser::OmpClause::Link, parser::OmpClause::Private, 1879 parser::OmpClause::Shared, parser::OmpClause::To>; 1880 1881 // Clauses with OmpObjectList in the tuple 1882 using TupleObjectListClauses = std::tuple<parser::OmpClause::Allocate, 1883 parser::OmpClause::Map, parser::OmpClause::Reduction>; 1884 1885 // TODO:: Generate the tuples using TableGen. 1886 // Handle other constructs with OmpObjectList such as OpenMPThreadprivate. 1887 return std::visit( 1888 common::visitors{ 1889 [&](const auto &x) -> const parser::OmpObjectList * { 1890 using Ty = std::decay_t<decltype(x)>; 1891 if constexpr (common::HasMember<Ty, MemberObjectListClauses>) { 1892 return &x.v; 1893 } else if constexpr (common::HasMember<Ty, 1894 TupleObjectListClauses>) { 1895 return &(std::get<parser::OmpObjectList>(x.v.t)); 1896 } else { 1897 return nullptr; 1898 } 1899 }, 1900 }, 1901 clause.u); 1902 } 1903 1904 } // namespace Fortran::semantics 1905