1 //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// \file 10 /// \brief This file implements semantic analysis for OpenMP directives and 11 /// clauses. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/ASTMutationListener.h" 17 #include "clang/AST/Decl.h" 18 #include "clang/AST/DeclCXX.h" 19 #include "clang/AST/DeclOpenMP.h" 20 #include "clang/AST/StmtCXX.h" 21 #include "clang/AST/StmtOpenMP.h" 22 #include "clang/AST/StmtVisitor.h" 23 #include "clang/Basic/OpenMPKinds.h" 24 #include "clang/Lex/Preprocessor.h" 25 #include "clang/Sema/Initialization.h" 26 #include "clang/Sema/Lookup.h" 27 #include "clang/Sema/Scope.h" 28 #include "clang/Sema/ScopeInfo.h" 29 #include "clang/Sema/SemaInternal.h" 30 using namespace clang; 31 32 //===----------------------------------------------------------------------===// 33 // Stack of data-sharing attributes for variables 34 //===----------------------------------------------------------------------===// 35 36 namespace { 37 /// \brief Default data sharing attributes, which can be applied to directive. 38 enum DefaultDataSharingAttributes { 39 DSA_unspecified = 0, /// \brief Data sharing attribute not specified. 40 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'. 41 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'. 42 }; 43 44 template <class T> struct MatchesAny { 45 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {} 46 bool operator()(T Kind) { 47 for (auto KindEl : Arr) 48 if (KindEl == Kind) 49 return true; 50 return false; 51 } 52 53 private: 54 ArrayRef<T> Arr; 55 }; 56 struct MatchesAlways { 57 MatchesAlways() {} 58 template <class T> bool operator()(T) { return true; } 59 }; 60 61 typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause; 62 typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective; 63 64 /// \brief Stack for tracking declarations used in OpenMP directives and 65 /// clauses and their data-sharing attributes. 66 class DSAStackTy { 67 public: 68 struct DSAVarData { 69 OpenMPDirectiveKind DKind; 70 OpenMPClauseKind CKind; 71 DeclRefExpr *RefExpr; 72 SourceLocation ImplicitDSALoc; 73 DSAVarData() 74 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr), 75 ImplicitDSALoc() {} 76 }; 77 78 private: 79 struct DSAInfo { 80 OpenMPClauseKind Attributes; 81 DeclRefExpr *RefExpr; 82 }; 83 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy; 84 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy; 85 86 struct SharingMapTy { 87 DeclSAMapTy SharingMap; 88 AlignedMapTy AlignedMap; 89 DefaultDataSharingAttributes DefaultAttr; 90 SourceLocation DefaultAttrLoc; 91 OpenMPDirectiveKind Directive; 92 DeclarationNameInfo DirectiveName; 93 Scope *CurScope; 94 SourceLocation ConstructLoc; 95 bool OrderedRegion; 96 SourceLocation InnerTeamsRegionLoc; 97 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 98 Scope *CurScope, SourceLocation Loc) 99 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified), 100 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope), 101 ConstructLoc(Loc), OrderedRegion(false), InnerTeamsRegionLoc() {} 102 SharingMapTy() 103 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified), 104 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr), 105 ConstructLoc(), OrderedRegion(false), InnerTeamsRegionLoc() {} 106 }; 107 108 typedef SmallVector<SharingMapTy, 64> StackTy; 109 110 /// \brief Stack of used declaration and their data-sharing attributes. 111 StackTy Stack; 112 Sema &SemaRef; 113 114 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator; 115 116 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D); 117 118 /// \brief Checks if the variable is a local for OpenMP region. 119 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter); 120 121 public: 122 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {} 123 124 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 125 Scope *CurScope, SourceLocation Loc) { 126 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc)); 127 Stack.back().DefaultAttrLoc = Loc; 128 } 129 130 void pop() { 131 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!"); 132 Stack.pop_back(); 133 } 134 135 /// \brief If 'aligned' declaration for given variable \a D was not seen yet, 136 /// add it and return NULL; otherwise return previous occurrence's expression 137 /// for diagnostics. 138 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE); 139 140 /// \brief Adds explicit data sharing attribute to the specified declaration. 141 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A); 142 143 /// \brief Returns data sharing attributes from top of the stack for the 144 /// specified declaration. 145 DSAVarData getTopDSA(VarDecl *D, bool FromParent); 146 /// \brief Returns data-sharing attributes for the specified declaration. 147 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent); 148 /// \brief Checks if the specified variables has data-sharing attributes which 149 /// match specified \a CPred predicate in any directive which matches \a DPred 150 /// predicate. 151 template <class ClausesPredicate, class DirectivesPredicate> 152 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred, 153 DirectivesPredicate DPred, bool FromParent); 154 /// \brief Checks if the specified variables has data-sharing attributes which 155 /// match specified \a CPred predicate in any innermost directive which 156 /// matches \a DPred predicate. 157 template <class ClausesPredicate, class DirectivesPredicate> 158 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred, 159 DirectivesPredicate DPred, 160 bool FromParent); 161 /// \brief Finds a directive which matches specified \a DPred predicate. 162 template <class NamedDirectivesPredicate> 163 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent); 164 165 /// \brief Returns currently analyzed directive. 166 OpenMPDirectiveKind getCurrentDirective() const { 167 return Stack.back().Directive; 168 } 169 /// \brief Returns parent directive. 170 OpenMPDirectiveKind getParentDirective() const { 171 if (Stack.size() > 2) 172 return Stack[Stack.size() - 2].Directive; 173 return OMPD_unknown; 174 } 175 176 /// \brief Set default data sharing attribute to none. 177 void setDefaultDSANone(SourceLocation Loc) { 178 Stack.back().DefaultAttr = DSA_none; 179 Stack.back().DefaultAttrLoc = Loc; 180 } 181 /// \brief Set default data sharing attribute to shared. 182 void setDefaultDSAShared(SourceLocation Loc) { 183 Stack.back().DefaultAttr = DSA_shared; 184 Stack.back().DefaultAttrLoc = Loc; 185 } 186 187 DefaultDataSharingAttributes getDefaultDSA() const { 188 return Stack.back().DefaultAttr; 189 } 190 SourceLocation getDefaultDSALocation() const { 191 return Stack.back().DefaultAttrLoc; 192 } 193 194 /// \brief Checks if the specified variable is a threadprivate. 195 bool isThreadPrivate(VarDecl *D) { 196 DSAVarData DVar = getTopDSA(D, false); 197 return isOpenMPThreadPrivate(DVar.CKind); 198 } 199 200 /// \brief Marks current region as ordered (it has an 'ordered' clause). 201 void setOrderedRegion(bool IsOrdered = true) { 202 Stack.back().OrderedRegion = IsOrdered; 203 } 204 /// \brief Returns true, if parent region is ordered (has associated 205 /// 'ordered' clause), false - otherwise. 206 bool isParentOrderedRegion() const { 207 if (Stack.size() > 2) 208 return Stack[Stack.size() - 2].OrderedRegion; 209 return false; 210 } 211 212 /// \brief Marks current target region as one with closely nested teams 213 /// region. 214 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 215 if (Stack.size() > 2) 216 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc; 217 } 218 /// \brief Returns true, if current region has closely nested teams region. 219 bool hasInnerTeamsRegion() const { 220 return getInnerTeamsRegionLoc().isValid(); 221 } 222 /// \brief Returns location of the nested teams region (if any). 223 SourceLocation getInnerTeamsRegionLoc() const { 224 if (Stack.size() > 1) 225 return Stack.back().InnerTeamsRegionLoc; 226 return SourceLocation(); 227 } 228 229 Scope *getCurScope() const { return Stack.back().CurScope; } 230 Scope *getCurScope() { return Stack.back().CurScope; } 231 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; } 232 }; 233 bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) { 234 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task || 235 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown; 236 } 237 } // namespace 238 239 DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter, 240 VarDecl *D) { 241 DSAVarData DVar; 242 if (Iter == std::prev(Stack.rend())) { 243 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 244 // in a region but not in construct] 245 // File-scope or namespace-scope variables referenced in called routines 246 // in the region are shared unless they appear in a threadprivate 247 // directive. 248 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D)) 249 DVar.CKind = OMPC_shared; 250 251 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 252 // in a region but not in construct] 253 // Variables with static storage duration that are declared in called 254 // routines in the region are shared. 255 if (D->hasGlobalStorage()) 256 DVar.CKind = OMPC_shared; 257 258 return DVar; 259 } 260 261 DVar.DKind = Iter->Directive; 262 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 263 // in a Construct, C/C++, predetermined, p.1] 264 // Variables with automatic storage duration that are declared in a scope 265 // inside the construct are private. 266 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() && 267 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) { 268 DVar.CKind = OMPC_private; 269 return DVar; 270 } 271 272 // Explicitly specified attributes and local variables with predetermined 273 // attributes. 274 if (Iter->SharingMap.count(D)) { 275 DVar.RefExpr = Iter->SharingMap[D].RefExpr; 276 DVar.CKind = Iter->SharingMap[D].Attributes; 277 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 278 return DVar; 279 } 280 281 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 282 // in a Construct, C/C++, implicitly determined, p.1] 283 // In a parallel or task construct, the data-sharing attributes of these 284 // variables are determined by the default clause, if present. 285 switch (Iter->DefaultAttr) { 286 case DSA_shared: 287 DVar.CKind = OMPC_shared; 288 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 289 return DVar; 290 case DSA_none: 291 return DVar; 292 case DSA_unspecified: 293 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 294 // in a Construct, implicitly determined, p.2] 295 // In a parallel construct, if no default clause is present, these 296 // variables are shared. 297 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 298 if (isOpenMPParallelDirective(DVar.DKind) || 299 isOpenMPTeamsDirective(DVar.DKind)) { 300 DVar.CKind = OMPC_shared; 301 return DVar; 302 } 303 304 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 305 // in a Construct, implicitly determined, p.4] 306 // In a task construct, if no default clause is present, a variable that in 307 // the enclosing context is determined to be shared by all implicit tasks 308 // bound to the current team is shared. 309 if (DVar.DKind == OMPD_task) { 310 DSAVarData DVarTemp; 311 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend(); 312 I != EE; ++I) { 313 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 314 // Referenced 315 // in a Construct, implicitly determined, p.6] 316 // In a task construct, if no default clause is present, a variable 317 // whose data-sharing attribute is not determined by the rules above is 318 // firstprivate. 319 DVarTemp = getDSA(I, D); 320 if (DVarTemp.CKind != OMPC_shared) { 321 DVar.RefExpr = nullptr; 322 DVar.DKind = OMPD_task; 323 DVar.CKind = OMPC_firstprivate; 324 return DVar; 325 } 326 if (isParallelOrTaskRegion(I->Directive)) 327 break; 328 } 329 DVar.DKind = OMPD_task; 330 DVar.CKind = 331 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 332 return DVar; 333 } 334 } 335 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 336 // in a Construct, implicitly determined, p.3] 337 // For constructs other than task, if no default clause is present, these 338 // variables inherit their data-sharing attributes from the enclosing 339 // context. 340 return getDSA(std::next(Iter), D); 341 } 342 343 DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) { 344 assert(Stack.size() > 1 && "Data sharing attributes stack is empty"); 345 auto It = Stack.back().AlignedMap.find(D); 346 if (It == Stack.back().AlignedMap.end()) { 347 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 348 Stack.back().AlignedMap[D] = NewDE; 349 return nullptr; 350 } else { 351 assert(It->second && "Unexpected nullptr expr in the aligned map"); 352 return It->second; 353 } 354 return nullptr; 355 } 356 357 void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) { 358 if (A == OMPC_threadprivate) { 359 Stack[0].SharingMap[D].Attributes = A; 360 Stack[0].SharingMap[D].RefExpr = E; 361 } else { 362 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); 363 Stack.back().SharingMap[D].Attributes = A; 364 Stack.back().SharingMap[D].RefExpr = E; 365 } 366 } 367 368 bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) { 369 if (Stack.size() > 2) { 370 reverse_iterator I = Iter, E = std::prev(Stack.rend()); 371 Scope *TopScope = nullptr; 372 while (I != E && !isParallelOrTaskRegion(I->Directive)) { 373 ++I; 374 } 375 if (I == E) 376 return false; 377 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; 378 Scope *CurScope = getCurScope(); 379 while (CurScope != TopScope && !CurScope->isDeclScope(D)) { 380 CurScope = CurScope->getParent(); 381 } 382 return CurScope != TopScope; 383 } 384 return false; 385 } 386 387 DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) { 388 DSAVarData DVar; 389 390 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 391 // in a Construct, C/C++, predetermined, p.1] 392 // Variables appearing in threadprivate directives are threadprivate. 393 if (D->getTLSKind() != VarDecl::TLS_None || 394 D->getStorageClass() == SC_Register) { 395 DVar.CKind = OMPC_threadprivate; 396 return DVar; 397 } 398 if (Stack[0].SharingMap.count(D)) { 399 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr; 400 DVar.CKind = OMPC_threadprivate; 401 return DVar; 402 } 403 404 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 405 // in a Construct, C/C++, predetermined, p.1] 406 // Variables with automatic storage duration that are declared in a scope 407 // inside the construct are private. 408 OpenMPDirectiveKind Kind = 409 FromParent ? getParentDirective() : getCurrentDirective(); 410 auto StartI = std::next(Stack.rbegin()); 411 auto EndI = std::prev(Stack.rend()); 412 if (FromParent && StartI != EndI) { 413 StartI = std::next(StartI); 414 } 415 if (!isParallelOrTaskRegion(Kind)) { 416 if (isOpenMPLocal(D, StartI) && 417 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto || 418 D->getStorageClass() == SC_None)) || 419 isa<ParmVarDecl>(D))) { 420 DVar.CKind = OMPC_private; 421 return DVar; 422 } 423 424 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 425 // in a Construct, C/C++, predetermined, p.4] 426 // Static data members are shared. 427 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 428 // in a Construct, C/C++, predetermined, p.7] 429 // Variables with static storage duration that are declared in a scope 430 // inside the construct are shared. 431 if (D->isStaticDataMember() || D->isStaticLocal()) { 432 DSAVarData DVarTemp = 433 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent); 434 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 435 return DVar; 436 437 DVar.CKind = OMPC_shared; 438 return DVar; 439 } 440 } 441 442 QualType Type = D->getType().getNonReferenceType().getCanonicalType(); 443 bool IsConstant = Type.isConstant(SemaRef.getASTContext()); 444 while (Type->isArrayType()) { 445 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType(); 446 Type = ElemType.getNonReferenceType().getCanonicalType(); 447 } 448 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 449 // in a Construct, C/C++, predetermined, p.6] 450 // Variables with const qualified type having no mutable member are 451 // shared. 452 CXXRecordDecl *RD = 453 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; 454 if (IsConstant && 455 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) { 456 // Variables with const-qualified type having no mutable member may be 457 // listed in a firstprivate clause, even if they are static data members. 458 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate), 459 MatchesAlways(), FromParent); 460 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr) 461 return DVar; 462 463 DVar.CKind = OMPC_shared; 464 return DVar; 465 } 466 467 // Explicitly specified attributes and local variables with predetermined 468 // attributes. 469 auto I = std::prev(StartI); 470 if (I->SharingMap.count(D)) { 471 DVar.RefExpr = I->SharingMap[D].RefExpr; 472 DVar.CKind = I->SharingMap[D].Attributes; 473 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 474 } 475 476 return DVar; 477 } 478 479 DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) { 480 auto StartI = Stack.rbegin(); 481 auto EndI = std::prev(Stack.rend()); 482 if (FromParent && StartI != EndI) { 483 StartI = std::next(StartI); 484 } 485 return getDSA(StartI, D); 486 } 487 488 template <class ClausesPredicate, class DirectivesPredicate> 489 DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred, 490 DirectivesPredicate DPred, 491 bool FromParent) { 492 auto StartI = std::next(Stack.rbegin()); 493 auto EndI = std::prev(Stack.rend()); 494 if (FromParent && StartI != EndI) { 495 StartI = std::next(StartI); 496 } 497 for (auto I = StartI, EE = EndI; I != EE; ++I) { 498 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive)) 499 continue; 500 DSAVarData DVar = getDSA(I, D); 501 if (CPred(DVar.CKind)) 502 return DVar; 503 } 504 return DSAVarData(); 505 } 506 507 template <class ClausesPredicate, class DirectivesPredicate> 508 DSAStackTy::DSAVarData 509 DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred, 510 DirectivesPredicate DPred, bool FromParent) { 511 auto StartI = std::next(Stack.rbegin()); 512 auto EndI = std::prev(Stack.rend()); 513 if (FromParent && StartI != EndI) { 514 StartI = std::next(StartI); 515 } 516 for (auto I = StartI, EE = EndI; I != EE; ++I) { 517 if (!DPred(I->Directive)) 518 break; 519 DSAVarData DVar = getDSA(I, D); 520 if (CPred(DVar.CKind)) 521 return DVar; 522 return DSAVarData(); 523 } 524 return DSAVarData(); 525 } 526 527 template <class NamedDirectivesPredicate> 528 bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) { 529 auto StartI = std::next(Stack.rbegin()); 530 auto EndI = std::prev(Stack.rend()); 531 if (FromParent && StartI != EndI) { 532 StartI = std::next(StartI); 533 } 534 for (auto I = StartI, EE = EndI; I != EE; ++I) { 535 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 536 return true; 537 } 538 return false; 539 } 540 541 void Sema::InitDataSharingAttributesStack() { 542 VarDataSharingAttributesStack = new DSAStackTy(*this); 543 } 544 545 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 546 547 bool Sema::IsOpenMPCapturedVar(VarDecl *VD) { 548 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 549 if (DSAStack->getCurrentDirective() != OMPD_unknown) { 550 auto DVarPrivate = DSAStack->getTopDSA(VD, /*FromParent=*/false); 551 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind)) 552 return true; 553 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), 554 /*FromParent=*/false); 555 return DVarPrivate.CKind != OMPC_unknown; 556 } 557 return false; 558 } 559 560 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 561 562 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 563 const DeclarationNameInfo &DirName, 564 Scope *CurScope, SourceLocation Loc) { 565 DSAStack->push(DKind, DirName, CurScope, Loc); 566 PushExpressionEvaluationContext(PotentiallyEvaluated); 567 } 568 569 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 570 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 571 // A variable of class type (or array thereof) that appears in a lastprivate 572 // clause requires an accessible, unambiguous default constructor for the 573 // class type, unless the list item is also specified in a firstprivate 574 // clause. 575 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 576 for (auto C : D->clauses()) { 577 if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) { 578 for (auto VarRef : Clause->varlists()) { 579 if (VarRef->isValueDependent() || VarRef->isTypeDependent()) 580 continue; 581 auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl()); 582 auto DVar = DSAStack->getTopDSA(VD, false); 583 if (DVar.CKind == OMPC_lastprivate) { 584 SourceLocation ELoc = VarRef->getExprLoc(); 585 auto Type = VarRef->getType(); 586 if (Type->isArrayType()) 587 Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0); 588 CXXRecordDecl *RD = 589 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; 590 // FIXME This code must be replaced by actual constructing of the 591 // lastprivate variable. 592 if (RD) { 593 CXXConstructorDecl *CD = LookupDefaultConstructor(RD); 594 PartialDiagnostic PD = 595 PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); 596 if (!CD || 597 CheckConstructorAccess( 598 ELoc, CD, InitializedEntity::InitializeTemporary(Type), 599 CD->getAccess(), PD) == AR_inaccessible || 600 CD->isDeleted()) { 601 Diag(ELoc, diag::err_omp_required_method) 602 << getOpenMPClauseName(OMPC_lastprivate) << 0; 603 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 604 VarDecl::DeclarationOnly; 605 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl 606 : diag::note_defined_here) 607 << VD; 608 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 609 continue; 610 } 611 MarkFunctionReferenced(ELoc, CD); 612 DiagnoseUseOfDecl(CD, ELoc); 613 } 614 } 615 } 616 } 617 } 618 } 619 620 DSAStack->pop(); 621 DiscardCleanupsInEvaluationContext(); 622 PopExpressionEvaluationContext(); 623 } 624 625 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 626 Expr *NumIterations, Sema &SemaRef, 627 Scope *S); 628 629 namespace { 630 631 class VarDeclFilterCCC : public CorrectionCandidateCallback { 632 private: 633 Sema &SemaRef; 634 635 public: 636 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 637 bool ValidateCandidate(const TypoCorrection &Candidate) override { 638 NamedDecl *ND = Candidate.getCorrectionDecl(); 639 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) { 640 return VD->hasGlobalStorage() && 641 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 642 SemaRef.getCurScope()); 643 } 644 return false; 645 } 646 }; 647 } // namespace 648 649 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 650 CXXScopeSpec &ScopeSpec, 651 const DeclarationNameInfo &Id) { 652 LookupResult Lookup(*this, Id, LookupOrdinaryName); 653 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 654 655 if (Lookup.isAmbiguous()) 656 return ExprError(); 657 658 VarDecl *VD; 659 if (!Lookup.isSingleResult()) { 660 if (TypoCorrection Corrected = CorrectTypo( 661 Id, LookupOrdinaryName, CurScope, nullptr, 662 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) { 663 diagnoseTypo(Corrected, 664 PDiag(Lookup.empty() 665 ? diag::err_undeclared_var_use_suggest 666 : diag::err_omp_expected_var_arg_suggest) 667 << Id.getName()); 668 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 669 } else { 670 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 671 : diag::err_omp_expected_var_arg) 672 << Id.getName(); 673 return ExprError(); 674 } 675 } else { 676 if (!(VD = Lookup.getAsSingle<VarDecl>())) { 677 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 678 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 679 return ExprError(); 680 } 681 } 682 Lookup.suppressDiagnostics(); 683 684 // OpenMP [2.9.2, Syntax, C/C++] 685 // Variables must be file-scope, namespace-scope, or static block-scope. 686 if (!VD->hasGlobalStorage()) { 687 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 688 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal(); 689 bool IsDecl = 690 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 691 Diag(VD->getLocation(), 692 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 693 << VD; 694 return ExprError(); 695 } 696 697 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 698 NamedDecl *ND = cast<NamedDecl>(CanonicalVD); 699 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 700 // A threadprivate directive for file-scope variables must appear outside 701 // any definition or declaration. 702 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 703 !getCurLexicalContext()->isTranslationUnit()) { 704 Diag(Id.getLoc(), diag::err_omp_var_scope) 705 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 706 bool IsDecl = 707 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 708 Diag(VD->getLocation(), 709 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 710 << VD; 711 return ExprError(); 712 } 713 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 714 // A threadprivate directive for static class member variables must appear 715 // in the class definition, in the same scope in which the member 716 // variables are declared. 717 if (CanonicalVD->isStaticDataMember() && 718 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 719 Diag(Id.getLoc(), diag::err_omp_var_scope) 720 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 721 bool IsDecl = 722 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 723 Diag(VD->getLocation(), 724 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 725 << VD; 726 return ExprError(); 727 } 728 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 729 // A threadprivate directive for namespace-scope variables must appear 730 // outside any definition or declaration other than the namespace 731 // definition itself. 732 if (CanonicalVD->getDeclContext()->isNamespace() && 733 (!getCurLexicalContext()->isFileContext() || 734 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 735 Diag(Id.getLoc(), diag::err_omp_var_scope) 736 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 737 bool IsDecl = 738 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 739 Diag(VD->getLocation(), 740 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 741 << VD; 742 return ExprError(); 743 } 744 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 745 // A threadprivate directive for static block-scope variables must appear 746 // in the scope of the variable and not in a nested scope. 747 if (CanonicalVD->isStaticLocal() && CurScope && 748 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 749 Diag(Id.getLoc(), diag::err_omp_var_scope) 750 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 751 bool IsDecl = 752 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 753 Diag(VD->getLocation(), 754 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 755 << VD; 756 return ExprError(); 757 } 758 759 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 760 // A threadprivate directive must lexically precede all references to any 761 // of the variables in its list. 762 if (VD->isUsed()) { 763 Diag(Id.getLoc(), diag::err_omp_var_used) 764 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 765 return ExprError(); 766 } 767 768 QualType ExprType = VD->getType().getNonReferenceType(); 769 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc()); 770 return DE; 771 } 772 773 Sema::DeclGroupPtrTy 774 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 775 ArrayRef<Expr *> VarList) { 776 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 777 CurContext->addDecl(D); 778 return DeclGroupPtrTy::make(DeclGroupRef(D)); 779 } 780 return DeclGroupPtrTy(); 781 } 782 783 namespace { 784 class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> { 785 Sema &SemaRef; 786 787 public: 788 bool VisitDeclRefExpr(const DeclRefExpr *E) { 789 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) { 790 if (VD->hasLocalStorage()) { 791 SemaRef.Diag(E->getLocStart(), 792 diag::err_omp_local_var_in_threadprivate_init) 793 << E->getSourceRange(); 794 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 795 << VD << VD->getSourceRange(); 796 return true; 797 } 798 } 799 return false; 800 } 801 bool VisitStmt(const Stmt *S) { 802 for (auto Child : S->children()) { 803 if (Child && Visit(Child)) 804 return true; 805 } 806 return false; 807 } 808 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 809 }; 810 } // namespace 811 812 OMPThreadPrivateDecl * 813 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 814 SmallVector<Expr *, 8> Vars; 815 for (auto &RefExpr : VarList) { 816 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr); 817 VarDecl *VD = cast<VarDecl>(DE->getDecl()); 818 SourceLocation ILoc = DE->getExprLoc(); 819 820 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 821 // A threadprivate variable must not have an incomplete type. 822 if (RequireCompleteType(ILoc, VD->getType(), 823 diag::err_omp_threadprivate_incomplete_type)) { 824 continue; 825 } 826 827 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 828 // A threadprivate variable must not have a reference type. 829 if (VD->getType()->isReferenceType()) { 830 Diag(ILoc, diag::err_omp_ref_type_arg) 831 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 832 bool IsDecl = 833 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 834 Diag(VD->getLocation(), 835 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 836 << VD; 837 continue; 838 } 839 840 // Check if this is a TLS variable. 841 if (VD->getTLSKind() != VarDecl::TLS_None || 842 VD->getStorageClass() == SC_Register) { 843 Diag(ILoc, diag::err_omp_var_thread_local) 844 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 845 bool IsDecl = 846 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 847 Diag(VD->getLocation(), 848 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 849 << VD; 850 continue; 851 } 852 853 // Check if initial value of threadprivate variable reference variable with 854 // local storage (it is not supported by runtime). 855 if (auto Init = VD->getAnyInitializer()) { 856 LocalVarRefChecker Checker(*this); 857 if (Checker.Visit(Init)) 858 continue; 859 } 860 861 Vars.push_back(RefExpr); 862 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 863 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 864 Context, SourceRange(Loc, Loc))); 865 if (auto *ML = Context.getASTMutationListener()) 866 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 867 } 868 OMPThreadPrivateDecl *D = nullptr; 869 if (!Vars.empty()) { 870 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 871 Vars); 872 D->setAccess(AS_public); 873 } 874 return D; 875 } 876 877 static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack, 878 const VarDecl *VD, DSAStackTy::DSAVarData DVar, 879 bool IsLoopIterVar = false) { 880 if (DVar.RefExpr) { 881 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 882 << getOpenMPClauseName(DVar.CKind); 883 return; 884 } 885 enum { 886 PDSA_StaticMemberShared, 887 PDSA_StaticLocalVarShared, 888 PDSA_LoopIterVarPrivate, 889 PDSA_LoopIterVarLinear, 890 PDSA_LoopIterVarLastprivate, 891 PDSA_ConstVarShared, 892 PDSA_GlobalVarShared, 893 PDSA_TaskVarFirstprivate, 894 PDSA_LocalVarPrivate, 895 PDSA_Implicit 896 } Reason = PDSA_Implicit; 897 bool ReportHint = false; 898 auto ReportLoc = VD->getLocation(); 899 if (IsLoopIterVar) { 900 if (DVar.CKind == OMPC_private) 901 Reason = PDSA_LoopIterVarPrivate; 902 else if (DVar.CKind == OMPC_lastprivate) 903 Reason = PDSA_LoopIterVarLastprivate; 904 else 905 Reason = PDSA_LoopIterVarLinear; 906 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) { 907 Reason = PDSA_TaskVarFirstprivate; 908 ReportLoc = DVar.ImplicitDSALoc; 909 } else if (VD->isStaticLocal()) 910 Reason = PDSA_StaticLocalVarShared; 911 else if (VD->isStaticDataMember()) 912 Reason = PDSA_StaticMemberShared; 913 else if (VD->isFileVarDecl()) 914 Reason = PDSA_GlobalVarShared; 915 else if (VD->getType().isConstant(SemaRef.getASTContext())) 916 Reason = PDSA_ConstVarShared; 917 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 918 ReportHint = true; 919 Reason = PDSA_LocalVarPrivate; 920 } 921 if (Reason != PDSA_Implicit) { 922 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 923 << Reason << ReportHint 924 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 925 } else if (DVar.ImplicitDSALoc.isValid()) { 926 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 927 << getOpenMPClauseName(DVar.CKind); 928 } 929 } 930 931 namespace { 932 class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> { 933 DSAStackTy *Stack; 934 Sema &SemaRef; 935 bool ErrorFound; 936 CapturedStmt *CS; 937 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate; 938 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA; 939 940 public: 941 void VisitDeclRefExpr(DeclRefExpr *E) { 942 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 943 // Skip internally declared variables. 944 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD)) 945 return; 946 947 auto DVar = Stack->getTopDSA(VD, false); 948 // Check if the variable has explicit DSA set and stop analysis if it so. 949 if (DVar.RefExpr) return; 950 951 auto ELoc = E->getExprLoc(); 952 auto DKind = Stack->getCurrentDirective(); 953 // The default(none) clause requires that each variable that is referenced 954 // in the construct, and does not have a predetermined data-sharing 955 // attribute, must have its data-sharing attribute explicitly determined 956 // by being listed in a data-sharing attribute clause. 957 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && 958 isParallelOrTaskRegion(DKind) && 959 VarsWithInheritedDSA.count(VD) == 0) { 960 VarsWithInheritedDSA[VD] = E; 961 return; 962 } 963 964 // OpenMP [2.9.3.6, Restrictions, p.2] 965 // A list item that appears in a reduction clause of the innermost 966 // enclosing worksharing or parallel construct may not be accessed in an 967 // explicit task. 968 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction), 969 [](OpenMPDirectiveKind K) -> bool { 970 return isOpenMPParallelDirective(K) || 971 isOpenMPWorksharingDirective(K) || 972 isOpenMPTeamsDirective(K); 973 }, 974 false); 975 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) { 976 ErrorFound = true; 977 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 978 ReportOriginalDSA(SemaRef, Stack, VD, DVar); 979 return; 980 } 981 982 // Define implicit data-sharing attributes for task. 983 DVar = Stack->getImplicitDSA(VD, false); 984 if (DKind == OMPD_task && DVar.CKind != OMPC_shared) 985 ImplicitFirstprivate.push_back(E); 986 } 987 } 988 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 989 for (auto *C : S->clauses()) { 990 // Skip analysis of arguments of implicitly defined firstprivate clause 991 // for task directives. 992 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid())) 993 for (auto *CC : C->children()) { 994 if (CC) 995 Visit(CC); 996 } 997 } 998 } 999 void VisitStmt(Stmt *S) { 1000 for (auto *C : S->children()) { 1001 if (C && !isa<OMPExecutableDirective>(C)) 1002 Visit(C); 1003 } 1004 } 1005 1006 bool isErrorFound() { return ErrorFound; } 1007 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; } 1008 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() { 1009 return VarsWithInheritedDSA; 1010 } 1011 1012 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 1013 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {} 1014 }; 1015 } // namespace 1016 1017 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 1018 switch (DKind) { 1019 case OMPD_parallel: { 1020 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1021 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty); 1022 Sema::CapturedParamNameType Params[] = { 1023 std::make_pair(".global_tid.", KmpInt32PtrTy), 1024 std::make_pair(".bound_tid.", KmpInt32PtrTy), 1025 std::make_pair(StringRef(), QualType()) // __context with shared vars 1026 }; 1027 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1028 Params); 1029 break; 1030 } 1031 case OMPD_simd: { 1032 Sema::CapturedParamNameType Params[] = { 1033 std::make_pair(StringRef(), QualType()) // __context with shared vars 1034 }; 1035 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1036 Params); 1037 break; 1038 } 1039 case OMPD_for: { 1040 Sema::CapturedParamNameType Params[] = { 1041 std::make_pair(StringRef(), QualType()) // __context with shared vars 1042 }; 1043 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1044 Params); 1045 break; 1046 } 1047 case OMPD_for_simd: { 1048 Sema::CapturedParamNameType Params[] = { 1049 std::make_pair(StringRef(), QualType()) // __context with shared vars 1050 }; 1051 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1052 Params); 1053 break; 1054 } 1055 case OMPD_sections: { 1056 Sema::CapturedParamNameType Params[] = { 1057 std::make_pair(StringRef(), QualType()) // __context with shared vars 1058 }; 1059 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1060 Params); 1061 break; 1062 } 1063 case OMPD_section: { 1064 Sema::CapturedParamNameType Params[] = { 1065 std::make_pair(StringRef(), QualType()) // __context with shared vars 1066 }; 1067 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1068 Params); 1069 break; 1070 } 1071 case OMPD_single: { 1072 Sema::CapturedParamNameType Params[] = { 1073 std::make_pair(StringRef(), QualType()) // __context with shared vars 1074 }; 1075 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1076 Params); 1077 break; 1078 } 1079 case OMPD_master: { 1080 Sema::CapturedParamNameType Params[] = { 1081 std::make_pair(StringRef(), QualType()) // __context with shared vars 1082 }; 1083 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1084 Params); 1085 break; 1086 } 1087 case OMPD_critical: { 1088 Sema::CapturedParamNameType Params[] = { 1089 std::make_pair(StringRef(), QualType()) // __context with shared vars 1090 }; 1091 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1092 Params); 1093 break; 1094 } 1095 case OMPD_parallel_for: { 1096 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1097 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty); 1098 Sema::CapturedParamNameType Params[] = { 1099 std::make_pair(".global_tid.", KmpInt32PtrTy), 1100 std::make_pair(".bound_tid.", KmpInt32PtrTy), 1101 std::make_pair(StringRef(), QualType()) // __context with shared vars 1102 }; 1103 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1104 Params); 1105 break; 1106 } 1107 case OMPD_parallel_for_simd: { 1108 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1109 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty); 1110 Sema::CapturedParamNameType Params[] = { 1111 std::make_pair(".global_tid.", KmpInt32PtrTy), 1112 std::make_pair(".bound_tid.", KmpInt32PtrTy), 1113 std::make_pair(StringRef(), QualType()) // __context with shared vars 1114 }; 1115 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1116 Params); 1117 break; 1118 } 1119 case OMPD_parallel_sections: { 1120 Sema::CapturedParamNameType Params[] = { 1121 std::make_pair(StringRef(), QualType()) // __context with shared vars 1122 }; 1123 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1124 Params); 1125 break; 1126 } 1127 case OMPD_task: { 1128 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1129 Sema::CapturedParamNameType Params[] = { 1130 std::make_pair(".global_tid.", KmpInt32Ty), 1131 std::make_pair(".part_id.", KmpInt32Ty), 1132 std::make_pair(StringRef(), QualType()) // __context with shared vars 1133 }; 1134 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1135 Params); 1136 // Mark this captured region as inlined, because we don't use outlined 1137 // function directly. 1138 getCurCapturedRegion()->TheCapturedDecl->addAttr( 1139 AlwaysInlineAttr::CreateImplicit( 1140 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange())); 1141 break; 1142 } 1143 case OMPD_ordered: { 1144 Sema::CapturedParamNameType Params[] = { 1145 std::make_pair(StringRef(), QualType()) // __context with shared vars 1146 }; 1147 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1148 Params); 1149 break; 1150 } 1151 case OMPD_atomic: { 1152 Sema::CapturedParamNameType Params[] = { 1153 std::make_pair(StringRef(), QualType()) // __context with shared vars 1154 }; 1155 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1156 Params); 1157 break; 1158 } 1159 case OMPD_target: { 1160 Sema::CapturedParamNameType Params[] = { 1161 std::make_pair(StringRef(), QualType()) // __context with shared vars 1162 }; 1163 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1164 Params); 1165 break; 1166 } 1167 case OMPD_teams: { 1168 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1169 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty); 1170 Sema::CapturedParamNameType Params[] = { 1171 std::make_pair(".global_tid.", KmpInt32PtrTy), 1172 std::make_pair(".bound_tid.", KmpInt32PtrTy), 1173 std::make_pair(StringRef(), QualType()) // __context with shared vars 1174 }; 1175 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1176 Params); 1177 break; 1178 } 1179 case OMPD_threadprivate: 1180 case OMPD_taskyield: 1181 case OMPD_barrier: 1182 case OMPD_taskwait: 1183 case OMPD_flush: 1184 llvm_unreachable("OpenMP Directive is not allowed"); 1185 case OMPD_unknown: 1186 llvm_unreachable("Unknown OpenMP directive"); 1187 } 1188 } 1189 1190 static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, 1191 OpenMPDirectiveKind CurrentRegion, 1192 const DeclarationNameInfo &CurrentName, 1193 SourceLocation StartLoc) { 1194 // Allowed nesting of constructs 1195 // +------------------+-----------------+------------------------------------+ 1196 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)| 1197 // +------------------+-----------------+------------------------------------+ 1198 // | parallel | parallel | * | 1199 // | parallel | for | * | 1200 // | parallel | for simd | * | 1201 // | parallel | master | * | 1202 // | parallel | critical | * | 1203 // | parallel | simd | * | 1204 // | parallel | sections | * | 1205 // | parallel | section | + | 1206 // | parallel | single | * | 1207 // | parallel | parallel for | * | 1208 // | parallel |parallel for simd| * | 1209 // | parallel |parallel sections| * | 1210 // | parallel | task | * | 1211 // | parallel | taskyield | * | 1212 // | parallel | barrier | * | 1213 // | parallel | taskwait | * | 1214 // | parallel | flush | * | 1215 // | parallel | ordered | + | 1216 // | parallel | atomic | * | 1217 // | parallel | target | * | 1218 // | parallel | teams | + | 1219 // +------------------+-----------------+------------------------------------+ 1220 // | for | parallel | * | 1221 // | for | for | + | 1222 // | for | for simd | + | 1223 // | for | master | + | 1224 // | for | critical | * | 1225 // | for | simd | * | 1226 // | for | sections | + | 1227 // | for | section | + | 1228 // | for | single | + | 1229 // | for | parallel for | * | 1230 // | for |parallel for simd| * | 1231 // | for |parallel sections| * | 1232 // | for | task | * | 1233 // | for | taskyield | * | 1234 // | for | barrier | + | 1235 // | for | taskwait | * | 1236 // | for | flush | * | 1237 // | for | ordered | * (if construct is ordered) | 1238 // | for | atomic | * | 1239 // | for | target | * | 1240 // | for | teams | + | 1241 // +------------------+-----------------+------------------------------------+ 1242 // | master | parallel | * | 1243 // | master | for | + | 1244 // | master | for simd | + | 1245 // | master | master | * | 1246 // | master | critical | * | 1247 // | master | simd | * | 1248 // | master | sections | + | 1249 // | master | section | + | 1250 // | master | single | + | 1251 // | master | parallel for | * | 1252 // | master |parallel for simd| * | 1253 // | master |parallel sections| * | 1254 // | master | task | * | 1255 // | master | taskyield | * | 1256 // | master | barrier | + | 1257 // | master | taskwait | * | 1258 // | master | flush | * | 1259 // | master | ordered | + | 1260 // | master | atomic | * | 1261 // | master | target | * | 1262 // | master | teams | + | 1263 // +------------------+-----------------+------------------------------------+ 1264 // | critical | parallel | * | 1265 // | critical | for | + | 1266 // | critical | for simd | + | 1267 // | critical | master | * | 1268 // | critical | critical | * (should have different names) | 1269 // | critical | simd | * | 1270 // | critical | sections | + | 1271 // | critical | section | + | 1272 // | critical | single | + | 1273 // | critical | parallel for | * | 1274 // | critical |parallel for simd| * | 1275 // | critical |parallel sections| * | 1276 // | critical | task | * | 1277 // | critical | taskyield | * | 1278 // | critical | barrier | + | 1279 // | critical | taskwait | * | 1280 // | critical | ordered | + | 1281 // | critical | atomic | * | 1282 // | critical | target | * | 1283 // | critical | teams | + | 1284 // +------------------+-----------------+------------------------------------+ 1285 // | simd | parallel | | 1286 // | simd | for | | 1287 // | simd | for simd | | 1288 // | simd | master | | 1289 // | simd | critical | | 1290 // | simd | simd | | 1291 // | simd | sections | | 1292 // | simd | section | | 1293 // | simd | single | | 1294 // | simd | parallel for | | 1295 // | simd |parallel for simd| | 1296 // | simd |parallel sections| | 1297 // | simd | task | | 1298 // | simd | taskyield | | 1299 // | simd | barrier | | 1300 // | simd | taskwait | | 1301 // | simd | flush | | 1302 // | simd | ordered | | 1303 // | simd | atomic | | 1304 // | simd | target | | 1305 // | simd | teams | | 1306 // +------------------+-----------------+------------------------------------+ 1307 // | for simd | parallel | | 1308 // | for simd | for | | 1309 // | for simd | for simd | | 1310 // | for simd | master | | 1311 // | for simd | critical | | 1312 // | for simd | simd | | 1313 // | for simd | sections | | 1314 // | for simd | section | | 1315 // | for simd | single | | 1316 // | for simd | parallel for | | 1317 // | for simd |parallel for simd| | 1318 // | for simd |parallel sections| | 1319 // | for simd | task | | 1320 // | for simd | taskyield | | 1321 // | for simd | barrier | | 1322 // | for simd | taskwait | | 1323 // | for simd | flush | | 1324 // | for simd | ordered | | 1325 // | for simd | atomic | | 1326 // | for simd | target | | 1327 // | for simd | teams | | 1328 // +------------------+-----------------+------------------------------------+ 1329 // | parallel for simd| parallel | | 1330 // | parallel for simd| for | | 1331 // | parallel for simd| for simd | | 1332 // | parallel for simd| master | | 1333 // | parallel for simd| critical | | 1334 // | parallel for simd| simd | | 1335 // | parallel for simd| sections | | 1336 // | parallel for simd| section | | 1337 // | parallel for simd| single | | 1338 // | parallel for simd| parallel for | | 1339 // | parallel for simd|parallel for simd| | 1340 // | parallel for simd|parallel sections| | 1341 // | parallel for simd| task | | 1342 // | parallel for simd| taskyield | | 1343 // | parallel for simd| barrier | | 1344 // | parallel for simd| taskwait | | 1345 // | parallel for simd| flush | | 1346 // | parallel for simd| ordered | | 1347 // | parallel for simd| atomic | | 1348 // | parallel for simd| target | | 1349 // | parallel for simd| teams | | 1350 // +------------------+-----------------+------------------------------------+ 1351 // | sections | parallel | * | 1352 // | sections | for | + | 1353 // | sections | for simd | + | 1354 // | sections | master | + | 1355 // | sections | critical | * | 1356 // | sections | simd | * | 1357 // | sections | sections | + | 1358 // | sections | section | * | 1359 // | sections | single | + | 1360 // | sections | parallel for | * | 1361 // | sections |parallel for simd| * | 1362 // | sections |parallel sections| * | 1363 // | sections | task | * | 1364 // | sections | taskyield | * | 1365 // | sections | barrier | + | 1366 // | sections | taskwait | * | 1367 // | sections | flush | * | 1368 // | sections | ordered | + | 1369 // | sections | atomic | * | 1370 // | sections | target | * | 1371 // | sections | teams | + | 1372 // +------------------+-----------------+------------------------------------+ 1373 // | section | parallel | * | 1374 // | section | for | + | 1375 // | section | for simd | + | 1376 // | section | master | + | 1377 // | section | critical | * | 1378 // | section | simd | * | 1379 // | section | sections | + | 1380 // | section | section | + | 1381 // | section | single | + | 1382 // | section | parallel for | * | 1383 // | section |parallel for simd| * | 1384 // | section |parallel sections| * | 1385 // | section | task | * | 1386 // | section | taskyield | * | 1387 // | section | barrier | + | 1388 // | section | taskwait | * | 1389 // | section | flush | * | 1390 // | section | ordered | + | 1391 // | section | atomic | * | 1392 // | section | target | * | 1393 // | section | teams | + | 1394 // +------------------+-----------------+------------------------------------+ 1395 // | single | parallel | * | 1396 // | single | for | + | 1397 // | single | for simd | + | 1398 // | single | master | + | 1399 // | single | critical | * | 1400 // | single | simd | * | 1401 // | single | sections | + | 1402 // | single | section | + | 1403 // | single | single | + | 1404 // | single | parallel for | * | 1405 // | single |parallel for simd| * | 1406 // | single |parallel sections| * | 1407 // | single | task | * | 1408 // | single | taskyield | * | 1409 // | single | barrier | + | 1410 // | single | taskwait | * | 1411 // | single | flush | * | 1412 // | single | ordered | + | 1413 // | single | atomic | * | 1414 // | single | target | * | 1415 // | single | teams | + | 1416 // +------------------+-----------------+------------------------------------+ 1417 // | parallel for | parallel | * | 1418 // | parallel for | for | + | 1419 // | parallel for | for simd | + | 1420 // | parallel for | master | + | 1421 // | parallel for | critical | * | 1422 // | parallel for | simd | * | 1423 // | parallel for | sections | + | 1424 // | parallel for | section | + | 1425 // | parallel for | single | + | 1426 // | parallel for | parallel for | * | 1427 // | parallel for |parallel for simd| * | 1428 // | parallel for |parallel sections| * | 1429 // | parallel for | task | * | 1430 // | parallel for | taskyield | * | 1431 // | parallel for | barrier | + | 1432 // | parallel for | taskwait | * | 1433 // | parallel for | flush | * | 1434 // | parallel for | ordered | * (if construct is ordered) | 1435 // | parallel for | atomic | * | 1436 // | parallel for | target | * | 1437 // | parallel for | teams | + | 1438 // +------------------+-----------------+------------------------------------+ 1439 // | parallel sections| parallel | * | 1440 // | parallel sections| for | + | 1441 // | parallel sections| for simd | + | 1442 // | parallel sections| master | + | 1443 // | parallel sections| critical | + | 1444 // | parallel sections| simd | * | 1445 // | parallel sections| sections | + | 1446 // | parallel sections| section | * | 1447 // | parallel sections| single | + | 1448 // | parallel sections| parallel for | * | 1449 // | parallel sections|parallel for simd| * | 1450 // | parallel sections|parallel sections| * | 1451 // | parallel sections| task | * | 1452 // | parallel sections| taskyield | * | 1453 // | parallel sections| barrier | + | 1454 // | parallel sections| taskwait | * | 1455 // | parallel sections| flush | * | 1456 // | parallel sections| ordered | + | 1457 // | parallel sections| atomic | * | 1458 // | parallel sections| target | * | 1459 // | parallel sections| teams | + | 1460 // +------------------+-----------------+------------------------------------+ 1461 // | task | parallel | * | 1462 // | task | for | + | 1463 // | task | for simd | + | 1464 // | task | master | + | 1465 // | task | critical | * | 1466 // | task | simd | * | 1467 // | task | sections | + | 1468 // | task | section | + | 1469 // | task | single | + | 1470 // | task | parallel for | * | 1471 // | task |parallel for simd| * | 1472 // | task |parallel sections| * | 1473 // | task | task | * | 1474 // | task | taskyield | * | 1475 // | task | barrier | + | 1476 // | task | taskwait | * | 1477 // | task | flush | * | 1478 // | task | ordered | + | 1479 // | task | atomic | * | 1480 // | task | target | * | 1481 // | task | teams | + | 1482 // +------------------+-----------------+------------------------------------+ 1483 // | ordered | parallel | * | 1484 // | ordered | for | + | 1485 // | ordered | for simd | + | 1486 // | ordered | master | * | 1487 // | ordered | critical | * | 1488 // | ordered | simd | * | 1489 // | ordered | sections | + | 1490 // | ordered | section | + | 1491 // | ordered | single | + | 1492 // | ordered | parallel for | * | 1493 // | ordered |parallel for simd| * | 1494 // | ordered |parallel sections| * | 1495 // | ordered | task | * | 1496 // | ordered | taskyield | * | 1497 // | ordered | barrier | + | 1498 // | ordered | taskwait | * | 1499 // | ordered | flush | * | 1500 // | ordered | ordered | + | 1501 // | ordered | atomic | * | 1502 // | ordered | target | * | 1503 // | ordered | teams | + | 1504 // +------------------+-----------------+------------------------------------+ 1505 // | atomic | parallel | | 1506 // | atomic | for | | 1507 // | atomic | for simd | | 1508 // | atomic | master | | 1509 // | atomic | critical | | 1510 // | atomic | simd | | 1511 // | atomic | sections | | 1512 // | atomic | section | | 1513 // | atomic | single | | 1514 // | atomic | parallel for | | 1515 // | atomic |parallel for simd| | 1516 // | atomic |parallel sections| | 1517 // | atomic | task | | 1518 // | atomic | taskyield | | 1519 // | atomic | barrier | | 1520 // | atomic | taskwait | | 1521 // | atomic | flush | | 1522 // | atomic | ordered | | 1523 // | atomic | atomic | | 1524 // | atomic | target | | 1525 // | atomic | teams | | 1526 // +------------------+-----------------+------------------------------------+ 1527 // | target | parallel | * | 1528 // | target | for | * | 1529 // | target | for simd | * | 1530 // | target | master | * | 1531 // | target | critical | * | 1532 // | target | simd | * | 1533 // | target | sections | * | 1534 // | target | section | * | 1535 // | target | single | * | 1536 // | target | parallel for | * | 1537 // | target |parallel for simd| * | 1538 // | target |parallel sections| * | 1539 // | target | task | * | 1540 // | target | taskyield | * | 1541 // | target | barrier | * | 1542 // | target | taskwait | * | 1543 // | target | flush | * | 1544 // | target | ordered | * | 1545 // | target | atomic | * | 1546 // | target | target | * | 1547 // | target | teams | * | 1548 // +------------------+-----------------+------------------------------------+ 1549 // | teams | parallel | * | 1550 // | teams | for | + | 1551 // | teams | for simd | + | 1552 // | teams | master | + | 1553 // | teams | critical | + | 1554 // | teams | simd | + | 1555 // | teams | sections | + | 1556 // | teams | section | + | 1557 // | teams | single | + | 1558 // | teams | parallel for | * | 1559 // | teams |parallel for simd| * | 1560 // | teams |parallel sections| * | 1561 // | teams | task | + | 1562 // | teams | taskyield | + | 1563 // | teams | barrier | + | 1564 // | teams | taskwait | + | 1565 // | teams | flush | + | 1566 // | teams | ordered | + | 1567 // | teams | atomic | + | 1568 // | teams | target | + | 1569 // | teams | teams | + | 1570 // +------------------+-----------------+------------------------------------+ 1571 if (Stack->getCurScope()) { 1572 auto ParentRegion = Stack->getParentDirective(); 1573 bool NestingProhibited = false; 1574 bool CloseNesting = true; 1575 enum { 1576 NoRecommend, 1577 ShouldBeInParallelRegion, 1578 ShouldBeInOrderedRegion, 1579 ShouldBeInTargetRegion 1580 } Recommend = NoRecommend; 1581 if (isOpenMPSimdDirective(ParentRegion)) { 1582 // OpenMP [2.16, Nesting of Regions] 1583 // OpenMP constructs may not be nested inside a simd region. 1584 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd); 1585 return true; 1586 } 1587 if (ParentRegion == OMPD_atomic) { 1588 // OpenMP [2.16, Nesting of Regions] 1589 // OpenMP constructs may not be nested inside an atomic region. 1590 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 1591 return true; 1592 } 1593 if (CurrentRegion == OMPD_section) { 1594 // OpenMP [2.7.2, sections Construct, Restrictions] 1595 // Orphaned section directives are prohibited. That is, the section 1596 // directives must appear within the sections construct and must not be 1597 // encountered elsewhere in the sections region. 1598 if (ParentRegion != OMPD_sections && 1599 ParentRegion != OMPD_parallel_sections) { 1600 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 1601 << (ParentRegion != OMPD_unknown) 1602 << getOpenMPDirectiveName(ParentRegion); 1603 return true; 1604 } 1605 return false; 1606 } 1607 // Allow some constructs to be orphaned (they could be used in functions, 1608 // called from OpenMP regions with the required preconditions). 1609 if (ParentRegion == OMPD_unknown) 1610 return false; 1611 if (CurrentRegion == OMPD_master) { 1612 // OpenMP [2.16, Nesting of Regions] 1613 // A master region may not be closely nested inside a worksharing, 1614 // atomic, or explicit task region. 1615 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 1616 ParentRegion == OMPD_task; 1617 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 1618 // OpenMP [2.16, Nesting of Regions] 1619 // A critical region may not be nested (closely or otherwise) inside a 1620 // critical region with the same name. Note that this restriction is not 1621 // sufficient to prevent deadlock. 1622 SourceLocation PreviousCriticalLoc; 1623 bool DeadLock = 1624 Stack->hasDirective([CurrentName, &PreviousCriticalLoc]( 1625 OpenMPDirectiveKind K, 1626 const DeclarationNameInfo &DNI, 1627 SourceLocation Loc) 1628 ->bool { 1629 if (K == OMPD_critical && 1630 DNI.getName() == CurrentName.getName()) { 1631 PreviousCriticalLoc = Loc; 1632 return true; 1633 } else 1634 return false; 1635 }, 1636 false /* skip top directive */); 1637 if (DeadLock) { 1638 SemaRef.Diag(StartLoc, 1639 diag::err_omp_prohibited_region_critical_same_name) 1640 << CurrentName.getName(); 1641 if (PreviousCriticalLoc.isValid()) 1642 SemaRef.Diag(PreviousCriticalLoc, 1643 diag::note_omp_previous_critical_region); 1644 return true; 1645 } 1646 } else if (CurrentRegion == OMPD_barrier) { 1647 // OpenMP [2.16, Nesting of Regions] 1648 // A barrier region may not be closely nested inside a worksharing, 1649 // explicit task, critical, ordered, atomic, or master region. 1650 NestingProhibited = 1651 isOpenMPWorksharingDirective(ParentRegion) || 1652 ParentRegion == OMPD_task || ParentRegion == OMPD_master || 1653 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered; 1654 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 1655 !isOpenMPParallelDirective(CurrentRegion)) { 1656 // OpenMP [2.16, Nesting of Regions] 1657 // A worksharing region may not be closely nested inside a worksharing, 1658 // explicit task, critical, ordered, atomic, or master region. 1659 NestingProhibited = 1660 isOpenMPWorksharingDirective(ParentRegion) || 1661 ParentRegion == OMPD_task || ParentRegion == OMPD_master || 1662 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered; 1663 Recommend = ShouldBeInParallelRegion; 1664 } else if (CurrentRegion == OMPD_ordered) { 1665 // OpenMP [2.16, Nesting of Regions] 1666 // An ordered region may not be closely nested inside a critical, 1667 // atomic, or explicit task region. 1668 // An ordered region must be closely nested inside a loop region (or 1669 // parallel loop region) with an ordered clause. 1670 NestingProhibited = ParentRegion == OMPD_critical || 1671 ParentRegion == OMPD_task || 1672 !Stack->isParentOrderedRegion(); 1673 Recommend = ShouldBeInOrderedRegion; 1674 } else if (isOpenMPTeamsDirective(CurrentRegion)) { 1675 // OpenMP [2.16, Nesting of Regions] 1676 // If specified, a teams construct must be contained within a target 1677 // construct. 1678 NestingProhibited = ParentRegion != OMPD_target; 1679 Recommend = ShouldBeInTargetRegion; 1680 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc()); 1681 } 1682 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) { 1683 // OpenMP [2.16, Nesting of Regions] 1684 // distribute, parallel, parallel sections, parallel workshare, and the 1685 // parallel loop and parallel loop SIMD constructs are the only OpenMP 1686 // constructs that can be closely nested in the teams region. 1687 // TODO: add distribute directive. 1688 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion); 1689 Recommend = ShouldBeInParallelRegion; 1690 } 1691 if (NestingProhibited) { 1692 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 1693 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend 1694 << getOpenMPDirectiveName(CurrentRegion); 1695 return true; 1696 } 1697 } 1698 return false; 1699 } 1700 1701 StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind, 1702 const DeclarationNameInfo &DirName, 1703 ArrayRef<OMPClause *> Clauses, 1704 Stmt *AStmt, 1705 SourceLocation StartLoc, 1706 SourceLocation EndLoc) { 1707 StmtResult Res = StmtError(); 1708 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, StartLoc)) 1709 return StmtError(); 1710 1711 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 1712 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA; 1713 bool ErrorFound = false; 1714 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 1715 if (AStmt) { 1716 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 1717 1718 // Check default data sharing attributes for referenced variables. 1719 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 1720 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt()); 1721 if (DSAChecker.isErrorFound()) 1722 return StmtError(); 1723 // Generate list of implicitly defined firstprivate variables. 1724 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 1725 1726 if (!DSAChecker.getImplicitFirstprivate().empty()) { 1727 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 1728 DSAChecker.getImplicitFirstprivate(), SourceLocation(), 1729 SourceLocation(), SourceLocation())) { 1730 ClausesWithImplicit.push_back(Implicit); 1731 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 1732 DSAChecker.getImplicitFirstprivate().size(); 1733 } else 1734 ErrorFound = true; 1735 } 1736 } 1737 1738 switch (Kind) { 1739 case OMPD_parallel: 1740 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 1741 EndLoc); 1742 break; 1743 case OMPD_simd: 1744 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 1745 VarsWithInheritedDSA); 1746 break; 1747 case OMPD_for: 1748 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 1749 VarsWithInheritedDSA); 1750 break; 1751 case OMPD_for_simd: 1752 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 1753 EndLoc, VarsWithInheritedDSA); 1754 break; 1755 case OMPD_sections: 1756 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 1757 EndLoc); 1758 break; 1759 case OMPD_section: 1760 assert(ClausesWithImplicit.empty() && 1761 "No clauses are allowed for 'omp section' directive"); 1762 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 1763 break; 1764 case OMPD_single: 1765 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 1766 EndLoc); 1767 break; 1768 case OMPD_master: 1769 assert(ClausesWithImplicit.empty() && 1770 "No clauses are allowed for 'omp master' directive"); 1771 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 1772 break; 1773 case OMPD_critical: 1774 assert(ClausesWithImplicit.empty() && 1775 "No clauses are allowed for 'omp critical' directive"); 1776 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc); 1777 break; 1778 case OMPD_parallel_for: 1779 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 1780 EndLoc, VarsWithInheritedDSA); 1781 break; 1782 case OMPD_parallel_for_simd: 1783 Res = ActOnOpenMPParallelForSimdDirective( 1784 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 1785 break; 1786 case OMPD_parallel_sections: 1787 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 1788 StartLoc, EndLoc); 1789 break; 1790 case OMPD_task: 1791 Res = 1792 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 1793 break; 1794 case OMPD_taskyield: 1795 assert(ClausesWithImplicit.empty() && 1796 "No clauses are allowed for 'omp taskyield' directive"); 1797 assert(AStmt == nullptr && 1798 "No associated statement allowed for 'omp taskyield' directive"); 1799 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 1800 break; 1801 case OMPD_barrier: 1802 assert(ClausesWithImplicit.empty() && 1803 "No clauses are allowed for 'omp barrier' directive"); 1804 assert(AStmt == nullptr && 1805 "No associated statement allowed for 'omp barrier' directive"); 1806 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 1807 break; 1808 case OMPD_taskwait: 1809 assert(ClausesWithImplicit.empty() && 1810 "No clauses are allowed for 'omp taskwait' directive"); 1811 assert(AStmt == nullptr && 1812 "No associated statement allowed for 'omp taskwait' directive"); 1813 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 1814 break; 1815 case OMPD_flush: 1816 assert(AStmt == nullptr && 1817 "No associated statement allowed for 'omp flush' directive"); 1818 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 1819 break; 1820 case OMPD_ordered: 1821 assert(ClausesWithImplicit.empty() && 1822 "No clauses are allowed for 'omp ordered' directive"); 1823 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc); 1824 break; 1825 case OMPD_atomic: 1826 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 1827 EndLoc); 1828 break; 1829 case OMPD_teams: 1830 Res = 1831 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 1832 break; 1833 case OMPD_target: 1834 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 1835 EndLoc); 1836 break; 1837 case OMPD_threadprivate: 1838 llvm_unreachable("OpenMP Directive is not allowed"); 1839 case OMPD_unknown: 1840 llvm_unreachable("Unknown OpenMP directive"); 1841 } 1842 1843 for (auto P : VarsWithInheritedDSA) { 1844 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 1845 << P.first << P.second->getSourceRange(); 1846 } 1847 if (!VarsWithInheritedDSA.empty()) 1848 return StmtError(); 1849 1850 if (ErrorFound) 1851 return StmtError(); 1852 return Res; 1853 } 1854 1855 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 1856 Stmt *AStmt, 1857 SourceLocation StartLoc, 1858 SourceLocation EndLoc) { 1859 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 1860 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 1861 // 1.2.2 OpenMP Language Terminology 1862 // Structured block - An executable statement with a single entry at the 1863 // top and a single exit at the bottom. 1864 // The point of exit cannot be a branch out of the structured block. 1865 // longjmp() and throw() must not violate the entry/exit criteria. 1866 CS->getCapturedDecl()->setNothrow(); 1867 1868 getCurFunction()->setHasBranchProtectedScope(); 1869 1870 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, 1871 AStmt); 1872 } 1873 1874 namespace { 1875 /// \brief Helper class for checking canonical form of the OpenMP loops and 1876 /// extracting iteration space of each loop in the loop nest, that will be used 1877 /// for IR generation. 1878 class OpenMPIterationSpaceChecker { 1879 /// \brief Reference to Sema. 1880 Sema &SemaRef; 1881 /// \brief A location for diagnostics (when there is no some better location). 1882 SourceLocation DefaultLoc; 1883 /// \brief A location for diagnostics (when increment is not compatible). 1884 SourceLocation ConditionLoc; 1885 /// \brief A source location for referring to loop init later. 1886 SourceRange InitSrcRange; 1887 /// \brief A source location for referring to condition later. 1888 SourceRange ConditionSrcRange; 1889 /// \brief A source location for referring to increment later. 1890 SourceRange IncrementSrcRange; 1891 /// \brief Loop variable. 1892 VarDecl *Var; 1893 /// \brief Reference to loop variable. 1894 DeclRefExpr *VarRef; 1895 /// \brief Lower bound (initializer for the var). 1896 Expr *LB; 1897 /// \brief Upper bound. 1898 Expr *UB; 1899 /// \brief Loop step (increment). 1900 Expr *Step; 1901 /// \brief This flag is true when condition is one of: 1902 /// Var < UB 1903 /// Var <= UB 1904 /// UB > Var 1905 /// UB >= Var 1906 bool TestIsLessOp; 1907 /// \brief This flag is true when condition is strict ( < or > ). 1908 bool TestIsStrictOp; 1909 /// \brief This flag is true when step is subtracted on each iteration. 1910 bool SubtractStep; 1911 1912 public: 1913 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc) 1914 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc), 1915 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()), 1916 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr), 1917 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false), 1918 TestIsStrictOp(false), SubtractStep(false) {} 1919 /// \brief Check init-expr for canonical loop form and save loop counter 1920 /// variable - #Var and its initialization value - #LB. 1921 bool CheckInit(Stmt *S); 1922 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags 1923 /// for less/greater and for strict/non-strict comparison. 1924 bool CheckCond(Expr *S); 1925 /// \brief Check incr-expr for canonical loop form and return true if it 1926 /// does not conform, otherwise save loop step (#Step). 1927 bool CheckInc(Expr *S); 1928 /// \brief Return the loop counter variable. 1929 VarDecl *GetLoopVar() const { return Var; } 1930 /// \brief Return the reference expression to loop counter variable. 1931 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; } 1932 /// \brief Source range of the loop init. 1933 SourceRange GetInitSrcRange() const { return InitSrcRange; } 1934 /// \brief Source range of the loop condition. 1935 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; } 1936 /// \brief Source range of the loop increment. 1937 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; } 1938 /// \brief True if the step should be subtracted. 1939 bool ShouldSubtractStep() const { return SubtractStep; } 1940 /// \brief Build the expression to calculate the number of iterations. 1941 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const; 1942 /// \brief Build reference expression to the counter be used for codegen. 1943 Expr *BuildCounterVar() const; 1944 /// \brief Build initization of the counter be used for codegen. 1945 Expr *BuildCounterInit() const; 1946 /// \brief Build step of the counter be used for codegen. 1947 Expr *BuildCounterStep() const; 1948 /// \brief Return true if any expression is dependent. 1949 bool Dependent() const; 1950 1951 private: 1952 /// \brief Check the right-hand side of an assignment in the increment 1953 /// expression. 1954 bool CheckIncRHS(Expr *RHS); 1955 /// \brief Helper to set loop counter variable and its initializer. 1956 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB); 1957 /// \brief Helper to set upper bound. 1958 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR, 1959 const SourceLocation &SL); 1960 /// \brief Helper to set loop increment. 1961 bool SetStep(Expr *NewStep, bool Subtract); 1962 }; 1963 1964 bool OpenMPIterationSpaceChecker::Dependent() const { 1965 if (!Var) { 1966 assert(!LB && !UB && !Step); 1967 return false; 1968 } 1969 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) || 1970 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent()); 1971 } 1972 1973 bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, 1974 DeclRefExpr *NewVarRefExpr, 1975 Expr *NewLB) { 1976 // State consistency checking to ensure correct usage. 1977 assert(Var == nullptr && LB == nullptr && VarRef == nullptr && 1978 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 1979 if (!NewVar || !NewLB) 1980 return true; 1981 Var = NewVar; 1982 VarRef = NewVarRefExpr; 1983 LB = NewLB; 1984 return false; 1985 } 1986 1987 bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp, 1988 const SourceRange &SR, 1989 const SourceLocation &SL) { 1990 // State consistency checking to ensure correct usage. 1991 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr && 1992 !TestIsLessOp && !TestIsStrictOp); 1993 if (!NewUB) 1994 return true; 1995 UB = NewUB; 1996 TestIsLessOp = LessOp; 1997 TestIsStrictOp = StrictOp; 1998 ConditionSrcRange = SR; 1999 ConditionLoc = SL; 2000 return false; 2001 } 2002 2003 bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) { 2004 // State consistency checking to ensure correct usage. 2005 assert(Var != nullptr && LB != nullptr && Step == nullptr); 2006 if (!NewStep) 2007 return true; 2008 if (!NewStep->isValueDependent()) { 2009 // Check that the step is integer expression. 2010 SourceLocation StepLoc = NewStep->getLocStart(); 2011 ExprResult Val = 2012 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep); 2013 if (Val.isInvalid()) 2014 return true; 2015 NewStep = Val.get(); 2016 2017 // OpenMP [2.6, Canonical Loop Form, Restrictions] 2018 // If test-expr is of form var relational-op b and relational-op is < or 2019 // <= then incr-expr must cause var to increase on each iteration of the 2020 // loop. If test-expr is of form var relational-op b and relational-op is 2021 // > or >= then incr-expr must cause var to decrease on each iteration of 2022 // the loop. 2023 // If test-expr is of form b relational-op var and relational-op is < or 2024 // <= then incr-expr must cause var to decrease on each iteration of the 2025 // loop. If test-expr is of form b relational-op var and relational-op is 2026 // > or >= then incr-expr must cause var to increase on each iteration of 2027 // the loop. 2028 llvm::APSInt Result; 2029 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); 2030 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 2031 bool IsConstNeg = 2032 IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); 2033 bool IsConstPos = 2034 IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); 2035 bool IsConstZero = IsConstant && !Result.getBoolValue(); 2036 if (UB && (IsConstZero || 2037 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract)) 2038 : (IsConstPos || (IsUnsigned && !Subtract))))) { 2039 SemaRef.Diag(NewStep->getExprLoc(), 2040 diag::err_omp_loop_incr_not_compatible) 2041 << Var << TestIsLessOp << NewStep->getSourceRange(); 2042 SemaRef.Diag(ConditionLoc, 2043 diag::note_omp_loop_cond_requres_compatible_incr) 2044 << TestIsLessOp << ConditionSrcRange; 2045 return true; 2046 } 2047 if (TestIsLessOp == Subtract) { 2048 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, 2049 NewStep).get(); 2050 Subtract = !Subtract; 2051 } 2052 } 2053 2054 Step = NewStep; 2055 SubtractStep = Subtract; 2056 return false; 2057 } 2058 2059 bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) { 2060 // Check init-expr for canonical loop form and save loop counter 2061 // variable - #Var and its initialization value - #LB. 2062 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 2063 // var = lb 2064 // integer-type var = lb 2065 // random-access-iterator-type var = lb 2066 // pointer-type var = lb 2067 // 2068 if (!S) { 2069 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 2070 return true; 2071 } 2072 InitSrcRange = S->getSourceRange(); 2073 if (Expr *E = dyn_cast<Expr>(S)) 2074 S = E->IgnoreParens(); 2075 if (auto BO = dyn_cast<BinaryOperator>(S)) { 2076 if (BO->getOpcode() == BO_Assign) 2077 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens())) 2078 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE, 2079 BO->getRHS()); 2080 } else if (auto DS = dyn_cast<DeclStmt>(S)) { 2081 if (DS->isSingleDecl()) { 2082 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 2083 if (Var->hasInit()) { 2084 // Accept non-canonical init form here but emit ext. warning. 2085 if (Var->getInitStyle() != VarDecl::CInit) 2086 SemaRef.Diag(S->getLocStart(), 2087 diag::ext_omp_loop_not_canonical_init) 2088 << S->getSourceRange(); 2089 return SetVarAndLB(Var, nullptr, Var->getInit()); 2090 } 2091 } 2092 } 2093 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) 2094 if (CE->getOperator() == OO_Equal) 2095 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0))) 2096 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE, 2097 CE->getArg(1)); 2098 2099 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init) 2100 << S->getSourceRange(); 2101 return true; 2102 } 2103 2104 /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the 2105 /// variable (which may be the loop variable) if possible. 2106 static const VarDecl *GetInitVarDecl(const Expr *E) { 2107 if (!E) 2108 return nullptr; 2109 E = E->IgnoreParenImpCasts(); 2110 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 2111 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 2112 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 && 2113 CE->getArg(0) != nullptr) 2114 E = CE->getArg(0)->IgnoreParenImpCasts(); 2115 auto DRE = dyn_cast_or_null<DeclRefExpr>(E); 2116 if (!DRE) 2117 return nullptr; 2118 return dyn_cast<VarDecl>(DRE->getDecl()); 2119 } 2120 2121 bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) { 2122 // Check test-expr for canonical form, save upper-bound UB, flags for 2123 // less/greater and for strict/non-strict comparison. 2124 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 2125 // var relational-op b 2126 // b relational-op var 2127 // 2128 if (!S) { 2129 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var; 2130 return true; 2131 } 2132 S = S->IgnoreParenImpCasts(); 2133 SourceLocation CondLoc = S->getLocStart(); 2134 if (auto BO = dyn_cast<BinaryOperator>(S)) { 2135 if (BO->isRelationalOp()) { 2136 if (GetInitVarDecl(BO->getLHS()) == Var) 2137 return SetUB(BO->getRHS(), 2138 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 2139 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 2140 BO->getSourceRange(), BO->getOperatorLoc()); 2141 if (GetInitVarDecl(BO->getRHS()) == Var) 2142 return SetUB(BO->getLHS(), 2143 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 2144 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 2145 BO->getSourceRange(), BO->getOperatorLoc()); 2146 } 2147 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) { 2148 if (CE->getNumArgs() == 2) { 2149 auto Op = CE->getOperator(); 2150 switch (Op) { 2151 case OO_Greater: 2152 case OO_GreaterEqual: 2153 case OO_Less: 2154 case OO_LessEqual: 2155 if (GetInitVarDecl(CE->getArg(0)) == Var) 2156 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 2157 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 2158 CE->getOperatorLoc()); 2159 if (GetInitVarDecl(CE->getArg(1)) == Var) 2160 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 2161 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 2162 CE->getOperatorLoc()); 2163 break; 2164 default: 2165 break; 2166 } 2167 } 2168 } 2169 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 2170 << S->getSourceRange() << Var; 2171 return true; 2172 } 2173 2174 bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) { 2175 // RHS of canonical loop form increment can be: 2176 // var + incr 2177 // incr + var 2178 // var - incr 2179 // 2180 RHS = RHS->IgnoreParenImpCasts(); 2181 if (auto BO = dyn_cast<BinaryOperator>(RHS)) { 2182 if (BO->isAdditiveOp()) { 2183 bool IsAdd = BO->getOpcode() == BO_Add; 2184 if (GetInitVarDecl(BO->getLHS()) == Var) 2185 return SetStep(BO->getRHS(), !IsAdd); 2186 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var) 2187 return SetStep(BO->getLHS(), false); 2188 } 2189 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 2190 bool IsAdd = CE->getOperator() == OO_Plus; 2191 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 2192 if (GetInitVarDecl(CE->getArg(0)) == Var) 2193 return SetStep(CE->getArg(1), !IsAdd); 2194 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var) 2195 return SetStep(CE->getArg(0), false); 2196 } 2197 } 2198 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr) 2199 << RHS->getSourceRange() << Var; 2200 return true; 2201 } 2202 2203 bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) { 2204 // Check incr-expr for canonical loop form and return true if it 2205 // does not conform. 2206 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 2207 // ++var 2208 // var++ 2209 // --var 2210 // var-- 2211 // var += incr 2212 // var -= incr 2213 // var = var + incr 2214 // var = incr + var 2215 // var = var - incr 2216 // 2217 if (!S) { 2218 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var; 2219 return true; 2220 } 2221 IncrementSrcRange = S->getSourceRange(); 2222 S = S->IgnoreParens(); 2223 if (auto UO = dyn_cast<UnaryOperator>(S)) { 2224 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var) 2225 return SetStep( 2226 SemaRef.ActOnIntegerConstant(UO->getLocStart(), 2227 (UO->isDecrementOp() ? -1 : 1)).get(), 2228 false); 2229 } else if (auto BO = dyn_cast<BinaryOperator>(S)) { 2230 switch (BO->getOpcode()) { 2231 case BO_AddAssign: 2232 case BO_SubAssign: 2233 if (GetInitVarDecl(BO->getLHS()) == Var) 2234 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 2235 break; 2236 case BO_Assign: 2237 if (GetInitVarDecl(BO->getLHS()) == Var) 2238 return CheckIncRHS(BO->getRHS()); 2239 break; 2240 default: 2241 break; 2242 } 2243 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) { 2244 switch (CE->getOperator()) { 2245 case OO_PlusPlus: 2246 case OO_MinusMinus: 2247 if (GetInitVarDecl(CE->getArg(0)) == Var) 2248 return SetStep( 2249 SemaRef.ActOnIntegerConstant( 2250 CE->getLocStart(), 2251 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(), 2252 false); 2253 break; 2254 case OO_PlusEqual: 2255 case OO_MinusEqual: 2256 if (GetInitVarDecl(CE->getArg(0)) == Var) 2257 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 2258 break; 2259 case OO_Equal: 2260 if (GetInitVarDecl(CE->getArg(0)) == Var) 2261 return CheckIncRHS(CE->getArg(1)); 2262 break; 2263 default: 2264 break; 2265 } 2266 } 2267 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr) 2268 << S->getSourceRange() << Var; 2269 return true; 2270 } 2271 2272 /// \brief Build the expression to calculate the number of iterations. 2273 Expr * 2274 OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S, 2275 const bool LimitedType) const { 2276 ExprResult Diff; 2277 if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() || 2278 SemaRef.getLangOpts().CPlusPlus) { 2279 // Upper - Lower 2280 Expr *Upper = TestIsLessOp ? UB : LB; 2281 Expr *Lower = TestIsLessOp ? LB : UB; 2282 2283 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 2284 2285 if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) { 2286 // BuildBinOp already emitted error, this one is to point user to upper 2287 // and lower bound, and to tell what is passed to 'operator-'. 2288 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx) 2289 << Upper->getSourceRange() << Lower->getSourceRange(); 2290 return nullptr; 2291 } 2292 } 2293 2294 if (!Diff.isUsable()) 2295 return nullptr; 2296 2297 // Upper - Lower [- 1] 2298 if (TestIsStrictOp) 2299 Diff = SemaRef.BuildBinOp( 2300 S, DefaultLoc, BO_Sub, Diff.get(), 2301 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 2302 if (!Diff.isUsable()) 2303 return nullptr; 2304 2305 // Upper - Lower [- 1] + Step 2306 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), 2307 Step->IgnoreImplicit()); 2308 if (!Diff.isUsable()) 2309 return nullptr; 2310 2311 // Parentheses (for dumping/debugging purposes only). 2312 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 2313 if (!Diff.isUsable()) 2314 return nullptr; 2315 2316 // (Upper - Lower [- 1] + Step) / Step 2317 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), 2318 Step->IgnoreImplicit()); 2319 if (!Diff.isUsable()) 2320 return nullptr; 2321 2322 // OpenMP runtime requires 32-bit or 64-bit loop variables. 2323 if (LimitedType) { 2324 auto &C = SemaRef.Context; 2325 QualType Type = Diff.get()->getType(); 2326 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 2327 if (NewSize != C.getTypeSize(Type)) { 2328 if (NewSize < C.getTypeSize(Type)) { 2329 assert(NewSize == 64 && "incorrect loop var size"); 2330 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 2331 << InitSrcRange << ConditionSrcRange; 2332 } 2333 QualType NewType = C.getIntTypeForBitwidth( 2334 NewSize, Type->hasSignedIntegerRepresentation()); 2335 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 2336 Sema::AA_Converting, true); 2337 if (!Diff.isUsable()) 2338 return nullptr; 2339 } 2340 } 2341 2342 return Diff.get(); 2343 } 2344 2345 /// \brief Build reference expression to the counter be used for codegen. 2346 Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const { 2347 return DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), 2348 GetIncrementSrcRange().getBegin(), Var, false, 2349 DefaultLoc, Var->getType(), VK_LValue); 2350 } 2351 2352 /// \brief Build initization of the counter be used for codegen. 2353 Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; } 2354 2355 /// \brief Build step of the counter be used for codegen. 2356 Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; } 2357 2358 /// \brief Iteration space of a single for loop. 2359 struct LoopIterationSpace { 2360 /// \brief This expression calculates the number of iterations in the loop. 2361 /// It is always possible to calculate it before starting the loop. 2362 Expr *NumIterations; 2363 /// \brief The loop counter variable. 2364 Expr *CounterVar; 2365 /// \brief This is initializer for the initial value of #CounterVar. 2366 Expr *CounterInit; 2367 /// \brief This is step for the #CounterVar used to generate its update: 2368 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 2369 Expr *CounterStep; 2370 /// \brief Should step be subtracted? 2371 bool Subtract; 2372 /// \brief Source range of the loop init. 2373 SourceRange InitSrcRange; 2374 /// \brief Source range of the loop condition. 2375 SourceRange CondSrcRange; 2376 /// \brief Source range of the loop increment. 2377 SourceRange IncSrcRange; 2378 }; 2379 2380 } // namespace 2381 2382 /// \brief Called on a for stmt to check and extract its iteration space 2383 /// for further processing (such as collapsing). 2384 static bool CheckOpenMPIterationSpace( 2385 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 2386 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 2387 Expr *NestedLoopCountExpr, 2388 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA, 2389 LoopIterationSpace &ResultIterSpace) { 2390 // OpenMP [2.6, Canonical Loop Form] 2391 // for (init-expr; test-expr; incr-expr) structured-block 2392 auto For = dyn_cast_or_null<ForStmt>(S); 2393 if (!For) { 2394 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for) 2395 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind) 2396 << NestedLoopCount << (CurrentNestedLoopCount > 0) 2397 << CurrentNestedLoopCount; 2398 if (NestedLoopCount > 1) 2399 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(), 2400 diag::note_omp_collapse_expr) 2401 << NestedLoopCountExpr->getSourceRange(); 2402 return true; 2403 } 2404 assert(For->getBody()); 2405 2406 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc()); 2407 2408 // Check init. 2409 auto Init = For->getInit(); 2410 if (ISC.CheckInit(Init)) { 2411 return true; 2412 } 2413 2414 bool HasErrors = false; 2415 2416 // Check loop variable's type. 2417 auto Var = ISC.GetLoopVar(); 2418 2419 // OpenMP [2.6, Canonical Loop Form] 2420 // Var is one of the following: 2421 // A variable of signed or unsigned integer type. 2422 // For C++, a variable of a random access iterator type. 2423 // For C, a variable of a pointer type. 2424 auto VarType = Var->getType(); 2425 if (!VarType->isDependentType() && !VarType->isIntegerType() && 2426 !VarType->isPointerType() && 2427 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 2428 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type) 2429 << SemaRef.getLangOpts().CPlusPlus; 2430 HasErrors = true; 2431 } 2432 2433 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a 2434 // Construct 2435 // The loop iteration variable(s) in the associated for-loop(s) of a for or 2436 // parallel for construct is (are) private. 2437 // The loop iteration variable in the associated for-loop of a simd construct 2438 // with just one associated for-loop is linear with a constant-linear-step 2439 // that is the increment of the associated for-loop. 2440 // Exclude loop var from the list of variables with implicitly defined data 2441 // sharing attributes. 2442 VarsWithImplicitDSA.erase(Var); 2443 2444 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in 2445 // a Construct, C/C++]. 2446 // The loop iteration variable in the associated for-loop of a simd construct 2447 // with just one associated for-loop may be listed in a linear clause with a 2448 // constant-linear-step that is the increment of the associated for-loop. 2449 // The loop iteration variable(s) in the associated for-loop(s) of a for or 2450 // parallel for construct may be listed in a private or lastprivate clause. 2451 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false); 2452 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr(); 2453 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is 2454 // declared in the loop and it is predetermined as a private. 2455 auto PredeterminedCKind = 2456 isOpenMPSimdDirective(DKind) 2457 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate) 2458 : OMPC_private; 2459 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 2460 DVar.CKind != PredeterminedCKind) || 2461 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) && 2462 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private && 2463 DVar.CKind != OMPC_lastprivate)) && 2464 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 2465 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa) 2466 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind) 2467 << getOpenMPClauseName(PredeterminedCKind); 2468 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true); 2469 HasErrors = true; 2470 } else if (LoopVarRefExpr != nullptr) { 2471 // Make the loop iteration variable private (for worksharing constructs), 2472 // linear (for simd directives with the only one associated loop) or 2473 // lastprivate (for simd directives with several collapsed loops). 2474 // FIXME: the next check and error message must be removed once the 2475 // capturing of global variables in loops is fixed. 2476 if (DVar.CKind == OMPC_unknown) 2477 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(), 2478 /*FromParent=*/false); 2479 if (!Var->hasLocalStorage() && DVar.CKind == OMPC_unknown) { 2480 SemaRef.Diag(Init->getLocStart(), diag::err_omp_global_loop_var_dsa) 2481 << getOpenMPClauseName(PredeterminedCKind) 2482 << getOpenMPDirectiveName(DKind); 2483 HasErrors = true; 2484 } else 2485 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind); 2486 } 2487 2488 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 2489 2490 // Check test-expr. 2491 HasErrors |= ISC.CheckCond(For->getCond()); 2492 2493 // Check incr-expr. 2494 HasErrors |= ISC.CheckInc(For->getInc()); 2495 2496 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 2497 return HasErrors; 2498 2499 // Build the loop's iteration space representation. 2500 ResultIterSpace.NumIterations = ISC.BuildNumIterations( 2501 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind)); 2502 ResultIterSpace.CounterVar = ISC.BuildCounterVar(); 2503 ResultIterSpace.CounterInit = ISC.BuildCounterInit(); 2504 ResultIterSpace.CounterStep = ISC.BuildCounterStep(); 2505 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange(); 2506 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange(); 2507 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange(); 2508 ResultIterSpace.Subtract = ISC.ShouldSubtractStep(); 2509 2510 HasErrors |= (ResultIterSpace.NumIterations == nullptr || 2511 ResultIterSpace.CounterVar == nullptr || 2512 ResultIterSpace.CounterInit == nullptr || 2513 ResultIterSpace.CounterStep == nullptr); 2514 2515 return HasErrors; 2516 } 2517 2518 /// \brief Build a variable declaration for OpenMP loop iteration variable. 2519 static VarDecl *BuildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 2520 StringRef Name) { 2521 DeclContext *DC = SemaRef.CurContext; 2522 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 2523 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 2524 VarDecl *Decl = 2525 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 2526 Decl->setImplicit(); 2527 return Decl; 2528 } 2529 2530 /// \brief Build 'VarRef = Start + Iter * Step'. 2531 static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S, 2532 SourceLocation Loc, ExprResult VarRef, 2533 ExprResult Start, ExprResult Iter, 2534 ExprResult Step, bool Subtract) { 2535 // Add parentheses (for debugging purposes only). 2536 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 2537 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 2538 !Step.isUsable()) 2539 return ExprError(); 2540 2541 ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), 2542 Step.get()->IgnoreImplicit()); 2543 if (!Update.isUsable()) 2544 return ExprError(); 2545 2546 // Build 'VarRef = Start + Iter * Step'. 2547 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add), 2548 Start.get()->IgnoreImplicit(), Update.get()); 2549 if (!Update.isUsable()) 2550 return ExprError(); 2551 2552 Update = SemaRef.PerformImplicitConversion( 2553 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 2554 if (!Update.isUsable()) 2555 return ExprError(); 2556 2557 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 2558 return Update; 2559 } 2560 2561 /// \brief Convert integer expression \a E to make it have at least \a Bits 2562 /// bits. 2563 static ExprResult WidenIterationCount(unsigned Bits, Expr *E, 2564 Sema &SemaRef) { 2565 if (E == nullptr) 2566 return ExprError(); 2567 auto &C = SemaRef.Context; 2568 QualType OldType = E->getType(); 2569 unsigned HasBits = C.getTypeSize(OldType); 2570 if (HasBits >= Bits) 2571 return ExprResult(E); 2572 // OK to convert to signed, because new type has more bits than old. 2573 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 2574 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 2575 true); 2576 } 2577 2578 /// \brief Check if the given expression \a E is a constant integer that fits 2579 /// into \a Bits bits. 2580 static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) { 2581 if (E == nullptr) 2582 return false; 2583 llvm::APSInt Result; 2584 if (E->isIntegerConstantExpr(Result, SemaRef.Context)) 2585 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); 2586 return false; 2587 } 2588 2589 /// \brief Called on a for stmt to check itself and nested loops (if any). 2590 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 2591 /// number of collapsed loops otherwise. 2592 static unsigned 2593 CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr, 2594 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA, 2595 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA, 2596 OMPLoopDirective::HelperExprs &Built) { 2597 unsigned NestedLoopCount = 1; 2598 if (NestedLoopCountExpr) { 2599 // Found 'collapse' clause - calculate collapse number. 2600 llvm::APSInt Result; 2601 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) 2602 NestedLoopCount = Result.getLimitedValue(); 2603 } 2604 // This is helper routine for loop directives (e.g., 'for', 'simd', 2605 // 'for simd', etc.). 2606 SmallVector<LoopIterationSpace, 4> IterSpaces; 2607 IterSpaces.resize(NestedLoopCount); 2608 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); 2609 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 2610 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt, 2611 NestedLoopCount, NestedLoopCountExpr, 2612 VarsWithImplicitDSA, IterSpaces[Cnt])) 2613 return 0; 2614 // Move on to the next nested for loop, or to the loop body. 2615 // OpenMP [2.8.1, simd construct, Restrictions] 2616 // All loops associated with the construct must be perfectly nested; that 2617 // is, there must be no intervening code nor any OpenMP directive between 2618 // any two loops. 2619 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); 2620 } 2621 2622 Built.clear(/* size */ NestedLoopCount); 2623 2624 if (SemaRef.CurContext->isDependentContext()) 2625 return NestedLoopCount; 2626 2627 // An example of what is generated for the following code: 2628 // 2629 // #pragma omp simd collapse(2) 2630 // for (i = 0; i < NI; ++i) 2631 // for (j = J0; j < NJ; j+=2) { 2632 // <loop body> 2633 // } 2634 // 2635 // We generate the code below. 2636 // Note: the loop body may be outlined in CodeGen. 2637 // Note: some counters may be C++ classes, operator- is used to find number of 2638 // iterations and operator+= to calculate counter value. 2639 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 2640 // or i64 is currently supported). 2641 // 2642 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 2643 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 2644 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 2645 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 2646 // // similar updates for vars in clauses (e.g. 'linear') 2647 // <loop body (using local i and j)> 2648 // } 2649 // i = NI; // assign final values of counters 2650 // j = NJ; 2651 // 2652 2653 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 2654 // the iteration counts of the collapsed for loops. 2655 auto N0 = IterSpaces[0].NumIterations; 2656 ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef); 2657 ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef); 2658 2659 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 2660 return NestedLoopCount; 2661 2662 auto &C = SemaRef.Context; 2663 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 2664 2665 Scope *CurScope = DSA.getCurScope(); 2666 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 2667 auto N = IterSpaces[Cnt].NumIterations; 2668 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 2669 if (LastIteration32.isUsable()) 2670 LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul, 2671 LastIteration32.get(), N); 2672 if (LastIteration64.isUsable()) 2673 LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul, 2674 LastIteration64.get(), N); 2675 } 2676 2677 // Choose either the 32-bit or 64-bit version. 2678 ExprResult LastIteration = LastIteration64; 2679 if (LastIteration32.isUsable() && 2680 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 2681 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 2682 FitsInto( 2683 32 /* Bits */, 2684 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 2685 LastIteration64.get(), SemaRef))) 2686 LastIteration = LastIteration32; 2687 2688 if (!LastIteration.isUsable()) 2689 return 0; 2690 2691 // Save the number of iterations. 2692 ExprResult NumIterations = LastIteration; 2693 { 2694 LastIteration = SemaRef.BuildBinOp( 2695 CurScope, SourceLocation(), BO_Sub, LastIteration.get(), 2696 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 2697 if (!LastIteration.isUsable()) 2698 return 0; 2699 } 2700 2701 // Calculate the last iteration number beforehand instead of doing this on 2702 // each iteration. Do not do this if the number of iterations may be kfold-ed. 2703 llvm::APSInt Result; 2704 bool IsConstant = 2705 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); 2706 ExprResult CalcLastIteration; 2707 if (!IsConstant) { 2708 SourceLocation SaveLoc; 2709 VarDecl *SaveVar = 2710 BuildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(), 2711 ".omp.last.iteration"); 2712 ExprResult SaveRef = SemaRef.BuildDeclRefExpr( 2713 SaveVar, LastIteration.get()->getType(), VK_LValue, SaveLoc); 2714 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign, 2715 SaveRef.get(), LastIteration.get()); 2716 LastIteration = SaveRef; 2717 2718 // Prepare SaveRef + 1. 2719 NumIterations = SemaRef.BuildBinOp( 2720 CurScope, SaveLoc, BO_Add, SaveRef.get(), 2721 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 2722 if (!NumIterations.isUsable()) 2723 return 0; 2724 } 2725 2726 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 2727 2728 // Precondition tests if there is at least one iteration (LastIteration > 0). 2729 ExprResult PreCond = SemaRef.BuildBinOp( 2730 CurScope, InitLoc, BO_GT, LastIteration.get(), 2731 SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get()); 2732 2733 QualType VType = LastIteration.get()->getType(); 2734 // Build variables passed into runtime, nesessary for worksharing directives. 2735 ExprResult LB, UB, IL, ST, EUB; 2736 if (isOpenMPWorksharingDirective(DKind)) { 2737 // Lower bound variable, initialized with zero. 2738 VarDecl *LBDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 2739 LB = SemaRef.BuildDeclRefExpr(LBDecl, VType, VK_LValue, InitLoc); 2740 SemaRef.AddInitializerToDecl( 2741 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 2742 /*DirectInit*/ false, /*TypeMayContainAuto*/ false); 2743 2744 // Upper bound variable, initialized with last iteration number. 2745 VarDecl *UBDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 2746 UB = SemaRef.BuildDeclRefExpr(UBDecl, VType, VK_LValue, InitLoc); 2747 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 2748 /*DirectInit*/ false, 2749 /*TypeMayContainAuto*/ false); 2750 2751 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 2752 // This will be used to implement clause 'lastprivate'. 2753 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 2754 VarDecl *ILDecl = BuildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 2755 IL = SemaRef.BuildDeclRefExpr(ILDecl, Int32Ty, VK_LValue, InitLoc); 2756 SemaRef.AddInitializerToDecl( 2757 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 2758 /*DirectInit*/ false, /*TypeMayContainAuto*/ false); 2759 2760 // Stride variable returned by runtime (we initialize it to 1 by default). 2761 VarDecl *STDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.stride"); 2762 ST = SemaRef.BuildDeclRefExpr(STDecl, VType, VK_LValue, InitLoc); 2763 SemaRef.AddInitializerToDecl( 2764 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 2765 /*DirectInit*/ false, /*TypeMayContainAuto*/ false); 2766 2767 // Build expression: UB = min(UB, LastIteration) 2768 // It is nesessary for CodeGen of directives with static scheduling. 2769 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 2770 UB.get(), LastIteration.get()); 2771 ExprResult CondOp = SemaRef.ActOnConditionalOp( 2772 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get()); 2773 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 2774 CondOp.get()); 2775 EUB = SemaRef.ActOnFinishFullExpr(EUB.get()); 2776 } 2777 2778 // Build the iteration variable and its initialization before loop. 2779 ExprResult IV; 2780 ExprResult Init; 2781 { 2782 VarDecl *IVDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.iv"); 2783 IV = SemaRef.BuildDeclRefExpr(IVDecl, VType, VK_LValue, InitLoc); 2784 Expr *RHS = isOpenMPWorksharingDirective(DKind) 2785 ? LB.get() 2786 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 2787 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 2788 Init = SemaRef.ActOnFinishFullExpr(Init.get()); 2789 } 2790 2791 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops. 2792 SourceLocation CondLoc; 2793 ExprResult Cond = 2794 isOpenMPWorksharingDirective(DKind) 2795 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get()) 2796 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 2797 NumIterations.get()); 2798 // Loop condition with 1 iteration separated (IV < LastIteration) 2799 ExprResult SeparatedCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, 2800 IV.get(), LastIteration.get()); 2801 2802 // Loop increment (IV = IV + 1) 2803 SourceLocation IncLoc; 2804 ExprResult Inc = 2805 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 2806 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 2807 if (!Inc.isUsable()) 2808 return 0; 2809 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 2810 Inc = SemaRef.ActOnFinishFullExpr(Inc.get()); 2811 if (!Inc.isUsable()) 2812 return 0; 2813 2814 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 2815 // Used for directives with static scheduling. 2816 ExprResult NextLB, NextUB; 2817 if (isOpenMPWorksharingDirective(DKind)) { 2818 // LB + ST 2819 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 2820 if (!NextLB.isUsable()) 2821 return 0; 2822 // LB = LB + ST 2823 NextLB = 2824 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 2825 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get()); 2826 if (!NextLB.isUsable()) 2827 return 0; 2828 // UB + ST 2829 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 2830 if (!NextUB.isUsable()) 2831 return 0; 2832 // UB = UB + ST 2833 NextUB = 2834 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 2835 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get()); 2836 if (!NextUB.isUsable()) 2837 return 0; 2838 } 2839 2840 // Build updates and final values of the loop counters. 2841 bool HasErrors = false; 2842 Built.Counters.resize(NestedLoopCount); 2843 Built.Updates.resize(NestedLoopCount); 2844 Built.Finals.resize(NestedLoopCount); 2845 { 2846 ExprResult Div; 2847 // Go from inner nested loop to outer. 2848 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) { 2849 LoopIterationSpace &IS = IterSpaces[Cnt]; 2850 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 2851 // Build: Iter = (IV / Div) % IS.NumIters 2852 // where Div is product of previous iterations' IS.NumIters. 2853 ExprResult Iter; 2854 if (Div.isUsable()) { 2855 Iter = 2856 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get()); 2857 } else { 2858 Iter = IV; 2859 assert((Cnt == (int)NestedLoopCount - 1) && 2860 "unusable div expected on first iteration only"); 2861 } 2862 2863 if (Cnt != 0 && Iter.isUsable()) 2864 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(), 2865 IS.NumIterations); 2866 if (!Iter.isUsable()) { 2867 HasErrors = true; 2868 break; 2869 } 2870 2871 // Build update: IS.CounterVar = IS.Start + Iter * IS.Step 2872 ExprResult Update = 2873 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, IS.CounterVar, 2874 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract); 2875 if (!Update.isUsable()) { 2876 HasErrors = true; 2877 break; 2878 } 2879 2880 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 2881 ExprResult Final = BuildCounterUpdate( 2882 SemaRef, CurScope, UpdLoc, IS.CounterVar, IS.CounterInit, 2883 IS.NumIterations, IS.CounterStep, IS.Subtract); 2884 if (!Final.isUsable()) { 2885 HasErrors = true; 2886 break; 2887 } 2888 2889 // Build Div for the next iteration: Div <- Div * IS.NumIters 2890 if (Cnt != 0) { 2891 if (Div.isUnset()) 2892 Div = IS.NumIterations; 2893 else 2894 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(), 2895 IS.NumIterations); 2896 2897 // Add parentheses (for debugging purposes only). 2898 if (Div.isUsable()) 2899 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get()); 2900 if (!Div.isUsable()) { 2901 HasErrors = true; 2902 break; 2903 } 2904 } 2905 if (!Update.isUsable() || !Final.isUsable()) { 2906 HasErrors = true; 2907 break; 2908 } 2909 // Save results 2910 Built.Counters[Cnt] = IS.CounterVar; 2911 Built.Updates[Cnt] = Update.get(); 2912 Built.Finals[Cnt] = Final.get(); 2913 } 2914 } 2915 2916 if (HasErrors) 2917 return 0; 2918 2919 // Save results 2920 Built.IterationVarRef = IV.get(); 2921 Built.LastIteration = LastIteration.get(); 2922 Built.NumIterations = NumIterations.get(); 2923 Built.CalcLastIteration = CalcLastIteration.get(); 2924 Built.PreCond = PreCond.get(); 2925 Built.Cond = Cond.get(); 2926 Built.SeparatedCond = SeparatedCond.get(); 2927 Built.Init = Init.get(); 2928 Built.Inc = Inc.get(); 2929 Built.LB = LB.get(); 2930 Built.UB = UB.get(); 2931 Built.IL = IL.get(); 2932 Built.ST = ST.get(); 2933 Built.EUB = EUB.get(); 2934 Built.NLB = NextLB.get(); 2935 Built.NUB = NextUB.get(); 2936 2937 return NestedLoopCount; 2938 } 2939 2940 static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 2941 auto CollapseFilter = [](const OMPClause *C) -> bool { 2942 return C->getClauseKind() == OMPC_collapse; 2943 }; 2944 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I( 2945 Clauses, CollapseFilter); 2946 if (I) 2947 return cast<OMPCollapseClause>(*I)->getNumForLoops(); 2948 return nullptr; 2949 } 2950 2951 StmtResult Sema::ActOnOpenMPSimdDirective( 2952 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 2953 SourceLocation EndLoc, 2954 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { 2955 OMPLoopDirective::HelperExprs B; 2956 // In presence of clause 'collapse', it will define the nested loops number. 2957 unsigned NestedLoopCount = 2958 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this, 2959 *DSAStack, VarsWithImplicitDSA, B); 2960 if (NestedLoopCount == 0) 2961 return StmtError(); 2962 2963 assert((CurContext->isDependentContext() || B.builtAll()) && 2964 "omp simd loop exprs were not built"); 2965 2966 if (!CurContext->isDependentContext()) { 2967 // Finalize the clauses that need pre-built expressions for CodeGen. 2968 for (auto C : Clauses) { 2969 if (auto LC = dyn_cast<OMPLinearClause>(C)) 2970 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 2971 B.NumIterations, *this, CurScope)) 2972 return StmtError(); 2973 } 2974 } 2975 2976 getCurFunction()->setHasBranchProtectedScope(); 2977 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 2978 Clauses, AStmt, B); 2979 } 2980 2981 StmtResult Sema::ActOnOpenMPForDirective( 2982 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 2983 SourceLocation EndLoc, 2984 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { 2985 OMPLoopDirective::HelperExprs B; 2986 // In presence of clause 'collapse', it will define the nested loops number. 2987 unsigned NestedLoopCount = 2988 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this, 2989 *DSAStack, VarsWithImplicitDSA, B); 2990 if (NestedLoopCount == 0) 2991 return StmtError(); 2992 2993 assert((CurContext->isDependentContext() || B.builtAll()) && 2994 "omp for loop exprs were not built"); 2995 2996 getCurFunction()->setHasBranchProtectedScope(); 2997 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 2998 Clauses, AStmt, B); 2999 } 3000 3001 StmtResult Sema::ActOnOpenMPForSimdDirective( 3002 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 3003 SourceLocation EndLoc, 3004 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { 3005 OMPLoopDirective::HelperExprs B; 3006 // In presence of clause 'collapse', it will define the nested loops number. 3007 unsigned NestedLoopCount = 3008 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt, 3009 *this, *DSAStack, VarsWithImplicitDSA, B); 3010 if (NestedLoopCount == 0) 3011 return StmtError(); 3012 3013 assert((CurContext->isDependentContext() || B.builtAll()) && 3014 "omp for simd loop exprs were not built"); 3015 3016 getCurFunction()->setHasBranchProtectedScope(); 3017 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 3018 Clauses, AStmt, B); 3019 } 3020 3021 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 3022 Stmt *AStmt, 3023 SourceLocation StartLoc, 3024 SourceLocation EndLoc) { 3025 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3026 auto BaseStmt = AStmt; 3027 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 3028 BaseStmt = CS->getCapturedStmt(); 3029 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 3030 auto S = C->children(); 3031 if (!S) 3032 return StmtError(); 3033 // All associated statements must be '#pragma omp section' except for 3034 // the first one. 3035 for (++S; S; ++S) { 3036 auto SectionStmt = *S; 3037 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 3038 if (SectionStmt) 3039 Diag(SectionStmt->getLocStart(), 3040 diag::err_omp_sections_substmt_not_section); 3041 return StmtError(); 3042 } 3043 } 3044 } else { 3045 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt); 3046 return StmtError(); 3047 } 3048 3049 getCurFunction()->setHasBranchProtectedScope(); 3050 3051 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, 3052 AStmt); 3053 } 3054 3055 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 3056 SourceLocation StartLoc, 3057 SourceLocation EndLoc) { 3058 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3059 3060 getCurFunction()->setHasBranchProtectedScope(); 3061 3062 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt); 3063 } 3064 3065 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 3066 Stmt *AStmt, 3067 SourceLocation StartLoc, 3068 SourceLocation EndLoc) { 3069 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3070 3071 getCurFunction()->setHasBranchProtectedScope(); 3072 3073 // OpenMP [2.7.3, single Construct, Restrictions] 3074 // The copyprivate clause must not be used with the nowait clause. 3075 OMPClause *Nowait = nullptr; 3076 OMPClause *Copyprivate = nullptr; 3077 for (auto *Clause : Clauses) { 3078 if (Clause->getClauseKind() == OMPC_nowait) 3079 Nowait = Clause; 3080 else if (Clause->getClauseKind() == OMPC_copyprivate) 3081 Copyprivate = Clause; 3082 if (Copyprivate && Nowait) { 3083 Diag(Copyprivate->getLocStart(), 3084 diag::err_omp_single_copyprivate_with_nowait); 3085 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here); 3086 return StmtError(); 3087 } 3088 } 3089 3090 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 3091 } 3092 3093 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 3094 SourceLocation StartLoc, 3095 SourceLocation EndLoc) { 3096 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3097 3098 getCurFunction()->setHasBranchProtectedScope(); 3099 3100 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 3101 } 3102 3103 StmtResult 3104 Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName, 3105 Stmt *AStmt, SourceLocation StartLoc, 3106 SourceLocation EndLoc) { 3107 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3108 3109 getCurFunction()->setHasBranchProtectedScope(); 3110 3111 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 3112 AStmt); 3113 } 3114 3115 StmtResult Sema::ActOnOpenMPParallelForDirective( 3116 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 3117 SourceLocation EndLoc, 3118 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { 3119 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3120 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 3121 // 1.2.2 OpenMP Language Terminology 3122 // Structured block - An executable statement with a single entry at the 3123 // top and a single exit at the bottom. 3124 // The point of exit cannot be a branch out of the structured block. 3125 // longjmp() and throw() must not violate the entry/exit criteria. 3126 CS->getCapturedDecl()->setNothrow(); 3127 3128 OMPLoopDirective::HelperExprs B; 3129 // In presence of clause 'collapse', it will define the nested loops number. 3130 unsigned NestedLoopCount = 3131 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt, 3132 *this, *DSAStack, VarsWithImplicitDSA, B); 3133 if (NestedLoopCount == 0) 3134 return StmtError(); 3135 3136 assert((CurContext->isDependentContext() || B.builtAll()) && 3137 "omp parallel for loop exprs were not built"); 3138 3139 getCurFunction()->setHasBranchProtectedScope(); 3140 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc, 3141 NestedLoopCount, Clauses, AStmt, B); 3142 } 3143 3144 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 3145 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 3146 SourceLocation EndLoc, 3147 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { 3148 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3149 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 3150 // 1.2.2 OpenMP Language Terminology 3151 // Structured block - An executable statement with a single entry at the 3152 // top and a single exit at the bottom. 3153 // The point of exit cannot be a branch out of the structured block. 3154 // longjmp() and throw() must not violate the entry/exit criteria. 3155 CS->getCapturedDecl()->setNothrow(); 3156 3157 OMPLoopDirective::HelperExprs B; 3158 // In presence of clause 'collapse', it will define the nested loops number. 3159 unsigned NestedLoopCount = 3160 CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses), 3161 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 3162 if (NestedLoopCount == 0) 3163 return StmtError(); 3164 3165 getCurFunction()->setHasBranchProtectedScope(); 3166 return OMPParallelForSimdDirective::Create( 3167 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 3168 } 3169 3170 StmtResult 3171 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 3172 Stmt *AStmt, SourceLocation StartLoc, 3173 SourceLocation EndLoc) { 3174 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3175 auto BaseStmt = AStmt; 3176 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 3177 BaseStmt = CS->getCapturedStmt(); 3178 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 3179 auto S = C->children(); 3180 if (!S) 3181 return StmtError(); 3182 // All associated statements must be '#pragma omp section' except for 3183 // the first one. 3184 for (++S; S; ++S) { 3185 auto SectionStmt = *S; 3186 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 3187 if (SectionStmt) 3188 Diag(SectionStmt->getLocStart(), 3189 diag::err_omp_parallel_sections_substmt_not_section); 3190 return StmtError(); 3191 } 3192 } 3193 } else { 3194 Diag(AStmt->getLocStart(), 3195 diag::err_omp_parallel_sections_not_compound_stmt); 3196 return StmtError(); 3197 } 3198 3199 getCurFunction()->setHasBranchProtectedScope(); 3200 3201 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc, 3202 Clauses, AStmt); 3203 } 3204 3205 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 3206 Stmt *AStmt, SourceLocation StartLoc, 3207 SourceLocation EndLoc) { 3208 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3209 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 3210 // 1.2.2 OpenMP Language Terminology 3211 // Structured block - An executable statement with a single entry at the 3212 // top and a single exit at the bottom. 3213 // The point of exit cannot be a branch out of the structured block. 3214 // longjmp() and throw() must not violate the entry/exit criteria. 3215 CS->getCapturedDecl()->setNothrow(); 3216 3217 getCurFunction()->setHasBranchProtectedScope(); 3218 3219 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 3220 } 3221 3222 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 3223 SourceLocation EndLoc) { 3224 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 3225 } 3226 3227 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 3228 SourceLocation EndLoc) { 3229 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 3230 } 3231 3232 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 3233 SourceLocation EndLoc) { 3234 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 3235 } 3236 3237 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 3238 SourceLocation StartLoc, 3239 SourceLocation EndLoc) { 3240 assert(Clauses.size() <= 1 && "Extra clauses in flush directive"); 3241 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 3242 } 3243 3244 StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt, 3245 SourceLocation StartLoc, 3246 SourceLocation EndLoc) { 3247 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3248 3249 getCurFunction()->setHasBranchProtectedScope(); 3250 3251 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt); 3252 } 3253 3254 namespace { 3255 /// \brief Helper class for checking expression in 'omp atomic [update]' 3256 /// construct. 3257 class OpenMPAtomicUpdateChecker { 3258 /// \brief Error results for atomic update expressions. 3259 enum ExprAnalysisErrorCode { 3260 /// \brief A statement is not an expression statement. 3261 NotAnExpression, 3262 /// \brief Expression is not builtin binary or unary operation. 3263 NotABinaryOrUnaryExpression, 3264 /// \brief Unary operation is not post-/pre- increment/decrement operation. 3265 NotAnUnaryIncDecExpression, 3266 /// \brief An expression is not of scalar type. 3267 NotAScalarType, 3268 /// \brief A binary operation is not an assignment operation. 3269 NotAnAssignmentOp, 3270 /// \brief RHS part of the binary operation is not a binary expression. 3271 NotABinaryExpression, 3272 /// \brief RHS part is not additive/multiplicative/shift/biwise binary 3273 /// expression. 3274 NotABinaryOperator, 3275 /// \brief RHS binary operation does not have reference to the updated LHS 3276 /// part. 3277 NotAnUpdateExpression, 3278 /// \brief No errors is found. 3279 NoError 3280 }; 3281 /// \brief Reference to Sema. 3282 Sema &SemaRef; 3283 /// \brief A location for note diagnostics (when error is found). 3284 SourceLocation NoteLoc; 3285 /// \brief Atomic operation supposed to be performed on source expression. 3286 BinaryOperatorKind OpKind; 3287 /// \brief 'x' lvalue part of the source atomic expression. 3288 Expr *X; 3289 /// \brief 'x' rvalue part of the source atomic expression, used in the right 3290 /// hand side of the expression. We need this to properly generate RHS part of 3291 /// the source expression (x = x'rval' binop expr or x = expr binop x'rval'). 3292 Expr *XRVal; 3293 /// \brief 'expr' rvalue part of the source atomic expression. 3294 Expr *E; 3295 3296 public: 3297 OpenMPAtomicUpdateChecker(Sema &SemaRef) 3298 : SemaRef(SemaRef), OpKind(BO_PtrMemD), X(nullptr), XRVal(nullptr), 3299 E(nullptr) {} 3300 /// \brief Check specified statement that it is suitable for 'atomic update' 3301 /// constructs and extract 'x', 'expr' and Operation from the original 3302 /// expression. 3303 /// \param DiagId Diagnostic which should be emitted if error is found. 3304 /// \param NoteId Diagnostic note for the main error message. 3305 /// \return true if statement is not an update expression, false otherwise. 3306 bool checkStatement(Stmt *S, unsigned DiagId, unsigned NoteId); 3307 /// \brief Return the 'x' lvalue part of the source atomic expression. 3308 Expr *getX() const { return X; } 3309 /// \brief Return the 'x' rvalue part of the source atomic expression, used in 3310 /// the RHS part of the source expression. 3311 Expr *getXRVal() const { return XRVal; } 3312 /// \brief Return the 'expr' rvalue part of the source atomic expression. 3313 Expr *getExpr() const { return E; } 3314 /// \brief Return required atomic operation. 3315 BinaryOperatorKind getOpKind() const {return OpKind;} 3316 private: 3317 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId, 3318 unsigned NoteId); 3319 }; 3320 } // namespace 3321 3322 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 3323 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 3324 ExprAnalysisErrorCode ErrorFound = NoError; 3325 SourceLocation ErrorLoc, NoteLoc; 3326 SourceRange ErrorRange, NoteRange; 3327 // Allowed constructs are: 3328 // x = x binop expr; 3329 // x = expr binop x; 3330 if (AtomicBinOp->getOpcode() == BO_Assign) { 3331 X = AtomicBinOp->getLHS(); 3332 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 3333 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 3334 if (AtomicInnerBinOp->isMultiplicativeOp() || 3335 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 3336 AtomicInnerBinOp->isBitwiseOp()) { 3337 OpKind = AtomicInnerBinOp->getOpcode(); 3338 auto *LHS = AtomicInnerBinOp->getLHS(); 3339 auto *RHS = AtomicInnerBinOp->getRHS(); 3340 llvm::FoldingSetNodeID XId, LHSId, RHSId; 3341 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 3342 /*Canonical=*/true); 3343 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 3344 /*Canonical=*/true); 3345 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 3346 /*Canonical=*/true); 3347 if (XId == LHSId) { 3348 E = RHS; 3349 XRVal = LHS; 3350 } else if (XId == RHSId) { 3351 E = LHS; 3352 XRVal = RHS; 3353 } else { 3354 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 3355 ErrorRange = AtomicInnerBinOp->getSourceRange(); 3356 NoteLoc = X->getExprLoc(); 3357 NoteRange = X->getSourceRange(); 3358 ErrorFound = NotAnUpdateExpression; 3359 } 3360 } else { 3361 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 3362 ErrorRange = AtomicInnerBinOp->getSourceRange(); 3363 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 3364 NoteRange = SourceRange(NoteLoc, NoteLoc); 3365 ErrorFound = NotABinaryOperator; 3366 } 3367 } else { 3368 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 3369 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 3370 ErrorFound = NotABinaryExpression; 3371 } 3372 } else { 3373 ErrorLoc = AtomicBinOp->getExprLoc(); 3374 ErrorRange = AtomicBinOp->getSourceRange(); 3375 NoteLoc = AtomicBinOp->getOperatorLoc(); 3376 NoteRange = SourceRange(NoteLoc, NoteLoc); 3377 ErrorFound = NotAnAssignmentOp; 3378 } 3379 if (ErrorFound != NoError) { 3380 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 3381 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 3382 return true; 3383 } else if (SemaRef.CurContext->isDependentContext()) 3384 E = X = XRVal = nullptr; 3385 return false; 3386 } 3387 3388 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 3389 unsigned NoteId) { 3390 ExprAnalysisErrorCode ErrorFound = NoError; 3391 SourceLocation ErrorLoc, NoteLoc; 3392 SourceRange ErrorRange, NoteRange; 3393 // Allowed constructs are: 3394 // x++; 3395 // x--; 3396 // ++x; 3397 // --x; 3398 // x binop= expr; 3399 // x = x binop expr; 3400 // x = expr binop x; 3401 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 3402 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 3403 if (AtomicBody->getType()->isScalarType() || 3404 AtomicBody->isInstantiationDependent()) { 3405 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 3406 AtomicBody->IgnoreParenImpCasts())) { 3407 // Check for Compound Assignment Operation 3408 OpKind = BinaryOperator::getOpForCompoundAssignment( 3409 AtomicCompAssignOp->getOpcode()); 3410 X = AtomicCompAssignOp->getLHS(); 3411 XRVal = SemaRef.PerformImplicitConversion( 3412 X, AtomicCompAssignOp->getComputationLHSType(), 3413 Sema::AA_Casting, /*AllowExplicit=*/true).get(); 3414 E = AtomicCompAssignOp->getRHS(); 3415 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 3416 AtomicBody->IgnoreParenImpCasts())) { 3417 // Check for Binary Operation 3418 return checkBinaryOperation(AtomicBinOp, DiagId, NoteId); 3419 } else if (auto *AtomicUnaryOp = 3420 // Check for Binary Operation 3421 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) { 3422 // Check for Unary Operation 3423 if (AtomicUnaryOp->isIncrementDecrementOp()) { 3424 OpKind = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 3425 XRVal = X = AtomicUnaryOp->getSubExpr(); 3426 E = SemaRef.ActOnIntegerConstant(AtomicUnaryOp->getOperatorLoc(), 1) 3427 .get(); 3428 } else { 3429 ErrorFound = NotAnUnaryIncDecExpression; 3430 ErrorLoc = AtomicUnaryOp->getExprLoc(); 3431 ErrorRange = AtomicUnaryOp->getSourceRange(); 3432 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 3433 NoteRange = SourceRange(NoteLoc, NoteLoc); 3434 } 3435 } else { 3436 ErrorFound = NotABinaryOrUnaryExpression; 3437 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 3438 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 3439 } 3440 } else { 3441 ErrorFound = NotAScalarType; 3442 NoteLoc = ErrorLoc = AtomicBody->getLocStart(); 3443 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 3444 } 3445 } else { 3446 ErrorFound = NotAnExpression; 3447 NoteLoc = ErrorLoc = S->getLocStart(); 3448 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 3449 } 3450 if (ErrorFound != NoError) { 3451 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 3452 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 3453 return true; 3454 } else if (SemaRef.CurContext->isDependentContext()) 3455 E = X = XRVal = nullptr; 3456 return false; 3457 } 3458 3459 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 3460 Stmt *AStmt, 3461 SourceLocation StartLoc, 3462 SourceLocation EndLoc) { 3463 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3464 auto CS = cast<CapturedStmt>(AStmt); 3465 // 1.2.2 OpenMP Language Terminology 3466 // Structured block - An executable statement with a single entry at the 3467 // top and a single exit at the bottom. 3468 // The point of exit cannot be a branch out of the structured block. 3469 // longjmp() and throw() must not violate the entry/exit criteria. 3470 // TODO further analysis of associated statements and clauses. 3471 OpenMPClauseKind AtomicKind = OMPC_unknown; 3472 SourceLocation AtomicKindLoc; 3473 for (auto *C : Clauses) { 3474 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 3475 C->getClauseKind() == OMPC_update || 3476 C->getClauseKind() == OMPC_capture) { 3477 if (AtomicKind != OMPC_unknown) { 3478 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses) 3479 << SourceRange(C->getLocStart(), C->getLocEnd()); 3480 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause) 3481 << getOpenMPClauseName(AtomicKind); 3482 } else { 3483 AtomicKind = C->getClauseKind(); 3484 AtomicKindLoc = C->getLocStart(); 3485 } 3486 } 3487 } 3488 3489 auto Body = CS->getCapturedStmt(); 3490 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 3491 Body = EWC->getSubExpr(); 3492 3493 BinaryOperatorKind OpKind = BO_PtrMemD; 3494 Expr *X = nullptr; 3495 Expr *XRVal = nullptr; 3496 Expr *V = nullptr; 3497 Expr *E = nullptr; 3498 // OpenMP [2.12.6, atomic Construct] 3499 // In the next expressions: 3500 // * x and v (as applicable) are both l-value expressions with scalar type. 3501 // * During the execution of an atomic region, multiple syntactic 3502 // occurrences of x must designate the same storage location. 3503 // * Neither of v and expr (as applicable) may access the storage location 3504 // designated by x. 3505 // * Neither of x and expr (as applicable) may access the storage location 3506 // designated by v. 3507 // * expr is an expression with scalar type. 3508 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 3509 // * binop, binop=, ++, and -- are not overloaded operators. 3510 // * The expression x binop expr must be numerically equivalent to x binop 3511 // (expr). This requirement is satisfied if the operators in expr have 3512 // precedence greater than binop, or by using parentheses around expr or 3513 // subexpressions of expr. 3514 // * The expression expr binop x must be numerically equivalent to (expr) 3515 // binop x. This requirement is satisfied if the operators in expr have 3516 // precedence equal to or greater than binop, or by using parentheses around 3517 // expr or subexpressions of expr. 3518 // * For forms that allow multiple occurrences of x, the number of times 3519 // that x is evaluated is unspecified. 3520 enum { 3521 NotAnExpression, 3522 NotAnAssignmentOp, 3523 NotAScalarType, 3524 NotAnLValue, 3525 NoError 3526 } ErrorFound = NoError; 3527 if (AtomicKind == OMPC_read) { 3528 SourceLocation ErrorLoc, NoteLoc; 3529 SourceRange ErrorRange, NoteRange; 3530 // If clause is read: 3531 // v = x; 3532 if (auto AtomicBody = dyn_cast<Expr>(Body)) { 3533 auto AtomicBinOp = 3534 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 3535 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 3536 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 3537 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 3538 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 3539 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 3540 if (!X->isLValue() || !V->isLValue()) { 3541 auto NotLValueExpr = X->isLValue() ? V : X; 3542 ErrorFound = NotAnLValue; 3543 ErrorLoc = AtomicBinOp->getExprLoc(); 3544 ErrorRange = AtomicBinOp->getSourceRange(); 3545 NoteLoc = NotLValueExpr->getExprLoc(); 3546 NoteRange = NotLValueExpr->getSourceRange(); 3547 } 3548 } else if (!X->isInstantiationDependent() || 3549 !V->isInstantiationDependent()) { 3550 auto NotScalarExpr = 3551 (X->isInstantiationDependent() || X->getType()->isScalarType()) 3552 ? V 3553 : X; 3554 ErrorFound = NotAScalarType; 3555 ErrorLoc = AtomicBinOp->getExprLoc(); 3556 ErrorRange = AtomicBinOp->getSourceRange(); 3557 NoteLoc = NotScalarExpr->getExprLoc(); 3558 NoteRange = NotScalarExpr->getSourceRange(); 3559 } 3560 } else { 3561 ErrorFound = NotAnAssignmentOp; 3562 ErrorLoc = AtomicBody->getExprLoc(); 3563 ErrorRange = AtomicBody->getSourceRange(); 3564 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 3565 : AtomicBody->getExprLoc(); 3566 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 3567 : AtomicBody->getSourceRange(); 3568 } 3569 } else { 3570 ErrorFound = NotAnExpression; 3571 NoteLoc = ErrorLoc = Body->getLocStart(); 3572 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 3573 } 3574 if (ErrorFound != NoError) { 3575 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 3576 << ErrorRange; 3577 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 3578 << NoteRange; 3579 return StmtError(); 3580 } else if (CurContext->isDependentContext()) 3581 V = X = nullptr; 3582 } else if (AtomicKind == OMPC_write) { 3583 SourceLocation ErrorLoc, NoteLoc; 3584 SourceRange ErrorRange, NoteRange; 3585 // If clause is write: 3586 // x = expr; 3587 if (auto AtomicBody = dyn_cast<Expr>(Body)) { 3588 auto AtomicBinOp = 3589 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 3590 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 3591 X = AtomicBinOp->getLHS(); 3592 E = AtomicBinOp->getRHS(); 3593 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 3594 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 3595 if (!X->isLValue()) { 3596 ErrorFound = NotAnLValue; 3597 ErrorLoc = AtomicBinOp->getExprLoc(); 3598 ErrorRange = AtomicBinOp->getSourceRange(); 3599 NoteLoc = X->getExprLoc(); 3600 NoteRange = X->getSourceRange(); 3601 } 3602 } else if (!X->isInstantiationDependent() || 3603 !E->isInstantiationDependent()) { 3604 auto NotScalarExpr = 3605 (X->isInstantiationDependent() || X->getType()->isScalarType()) 3606 ? E 3607 : X; 3608 ErrorFound = NotAScalarType; 3609 ErrorLoc = AtomicBinOp->getExprLoc(); 3610 ErrorRange = AtomicBinOp->getSourceRange(); 3611 NoteLoc = NotScalarExpr->getExprLoc(); 3612 NoteRange = NotScalarExpr->getSourceRange(); 3613 } 3614 } else { 3615 ErrorFound = NotAnAssignmentOp; 3616 ErrorLoc = AtomicBody->getExprLoc(); 3617 ErrorRange = AtomicBody->getSourceRange(); 3618 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 3619 : AtomicBody->getExprLoc(); 3620 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 3621 : AtomicBody->getSourceRange(); 3622 } 3623 } else { 3624 ErrorFound = NotAnExpression; 3625 NoteLoc = ErrorLoc = Body->getLocStart(); 3626 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 3627 } 3628 if (ErrorFound != NoError) { 3629 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 3630 << ErrorRange; 3631 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 3632 << NoteRange; 3633 return StmtError(); 3634 } else if (CurContext->isDependentContext()) 3635 E = X = nullptr; 3636 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 3637 // If clause is update: 3638 // x++; 3639 // x--; 3640 // ++x; 3641 // --x; 3642 // x binop= expr; 3643 // x = x binop expr; 3644 // x = expr binop x; 3645 OpenMPAtomicUpdateChecker Checker(*this); 3646 if (Checker.checkStatement( 3647 Body, (AtomicKind == OMPC_update) 3648 ? diag::err_omp_atomic_update_not_expression_statement 3649 : diag::err_omp_atomic_not_expression_statement, 3650 diag::note_omp_atomic_update)) 3651 return StmtError(); 3652 if (!CurContext->isDependentContext()) { 3653 E = Checker.getExpr(); 3654 X = Checker.getX(); 3655 XRVal = Checker.getXRVal(); 3656 OpKind = Checker.getOpKind(); 3657 } 3658 } else if (AtomicKind == OMPC_capture) { 3659 if (isa<Expr>(Body) && !isa<BinaryOperator>(Body)) { 3660 Diag(Body->getLocStart(), 3661 diag::err_omp_atomic_capture_not_expression_statement); 3662 return StmtError(); 3663 } else if (!isa<Expr>(Body) && !isa<CompoundStmt>(Body)) { 3664 Diag(Body->getLocStart(), 3665 diag::err_omp_atomic_capture_not_compound_statement); 3666 return StmtError(); 3667 } 3668 } 3669 3670 getCurFunction()->setHasBranchProtectedScope(); 3671 3672 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 3673 OpKind, X, XRVal, V, E); 3674 } 3675 3676 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 3677 Stmt *AStmt, 3678 SourceLocation StartLoc, 3679 SourceLocation EndLoc) { 3680 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3681 3682 // OpenMP [2.16, Nesting of Regions] 3683 // If specified, a teams construct must be contained within a target 3684 // construct. That target construct must contain no statements or directives 3685 // outside of the teams construct. 3686 if (DSAStack->hasInnerTeamsRegion()) { 3687 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true); 3688 bool OMPTeamsFound = true; 3689 if (auto *CS = dyn_cast<CompoundStmt>(S)) { 3690 auto I = CS->body_begin(); 3691 while (I != CS->body_end()) { 3692 auto OED = dyn_cast<OMPExecutableDirective>(*I); 3693 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) { 3694 OMPTeamsFound = false; 3695 break; 3696 } 3697 ++I; 3698 } 3699 assert(I != CS->body_end() && "Not found statement"); 3700 S = *I; 3701 } 3702 if (!OMPTeamsFound) { 3703 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 3704 Diag(DSAStack->getInnerTeamsRegionLoc(), 3705 diag::note_omp_nested_teams_construct_here); 3706 Diag(S->getLocStart(), diag::note_omp_nested_statement_here) 3707 << isa<OMPExecutableDirective>(S); 3708 return StmtError(); 3709 } 3710 } 3711 3712 getCurFunction()->setHasBranchProtectedScope(); 3713 3714 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 3715 } 3716 3717 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 3718 Stmt *AStmt, SourceLocation StartLoc, 3719 SourceLocation EndLoc) { 3720 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3721 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 3722 // 1.2.2 OpenMP Language Terminology 3723 // Structured block - An executable statement with a single entry at the 3724 // top and a single exit at the bottom. 3725 // The point of exit cannot be a branch out of the structured block. 3726 // longjmp() and throw() must not violate the entry/exit criteria. 3727 CS->getCapturedDecl()->setNothrow(); 3728 3729 getCurFunction()->setHasBranchProtectedScope(); 3730 3731 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 3732 } 3733 3734 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 3735 SourceLocation StartLoc, 3736 SourceLocation LParenLoc, 3737 SourceLocation EndLoc) { 3738 OMPClause *Res = nullptr; 3739 switch (Kind) { 3740 case OMPC_if: 3741 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc); 3742 break; 3743 case OMPC_final: 3744 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 3745 break; 3746 case OMPC_num_threads: 3747 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 3748 break; 3749 case OMPC_safelen: 3750 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 3751 break; 3752 case OMPC_collapse: 3753 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 3754 break; 3755 case OMPC_default: 3756 case OMPC_proc_bind: 3757 case OMPC_schedule: 3758 case OMPC_private: 3759 case OMPC_firstprivate: 3760 case OMPC_lastprivate: 3761 case OMPC_shared: 3762 case OMPC_reduction: 3763 case OMPC_linear: 3764 case OMPC_aligned: 3765 case OMPC_copyin: 3766 case OMPC_copyprivate: 3767 case OMPC_ordered: 3768 case OMPC_nowait: 3769 case OMPC_untied: 3770 case OMPC_mergeable: 3771 case OMPC_threadprivate: 3772 case OMPC_flush: 3773 case OMPC_read: 3774 case OMPC_write: 3775 case OMPC_update: 3776 case OMPC_capture: 3777 case OMPC_seq_cst: 3778 case OMPC_unknown: 3779 llvm_unreachable("Clause is not allowed."); 3780 } 3781 return Res; 3782 } 3783 3784 OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc, 3785 SourceLocation LParenLoc, 3786 SourceLocation EndLoc) { 3787 Expr *ValExpr = Condition; 3788 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 3789 !Condition->isInstantiationDependent() && 3790 !Condition->containsUnexpandedParameterPack()) { 3791 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(), 3792 Condition->getExprLoc(), Condition); 3793 if (Val.isInvalid()) 3794 return nullptr; 3795 3796 ValExpr = Val.get(); 3797 } 3798 3799 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc); 3800 } 3801 3802 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 3803 SourceLocation StartLoc, 3804 SourceLocation LParenLoc, 3805 SourceLocation EndLoc) { 3806 Expr *ValExpr = Condition; 3807 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 3808 !Condition->isInstantiationDependent() && 3809 !Condition->containsUnexpandedParameterPack()) { 3810 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(), 3811 Condition->getExprLoc(), Condition); 3812 if (Val.isInvalid()) 3813 return nullptr; 3814 3815 ValExpr = Val.get(); 3816 } 3817 3818 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc); 3819 } 3820 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 3821 Expr *Op) { 3822 if (!Op) 3823 return ExprError(); 3824 3825 class IntConvertDiagnoser : public ICEConvertDiagnoser { 3826 public: 3827 IntConvertDiagnoser() 3828 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 3829 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 3830 QualType T) override { 3831 return S.Diag(Loc, diag::err_omp_not_integral) << T; 3832 } 3833 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 3834 QualType T) override { 3835 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 3836 } 3837 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 3838 QualType T, 3839 QualType ConvTy) override { 3840 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 3841 } 3842 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 3843 QualType ConvTy) override { 3844 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 3845 << ConvTy->isEnumeralType() << ConvTy; 3846 } 3847 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 3848 QualType T) override { 3849 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 3850 } 3851 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 3852 QualType ConvTy) override { 3853 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 3854 << ConvTy->isEnumeralType() << ConvTy; 3855 } 3856 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 3857 QualType) override { 3858 llvm_unreachable("conversion functions are permitted"); 3859 } 3860 } ConvertDiagnoser; 3861 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 3862 } 3863 3864 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 3865 SourceLocation StartLoc, 3866 SourceLocation LParenLoc, 3867 SourceLocation EndLoc) { 3868 Expr *ValExpr = NumThreads; 3869 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() && 3870 !NumThreads->containsUnexpandedParameterPack()) { 3871 SourceLocation NumThreadsLoc = NumThreads->getLocStart(); 3872 ExprResult Val = 3873 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads); 3874 if (Val.isInvalid()) 3875 return nullptr; 3876 3877 ValExpr = Val.get(); 3878 3879 // OpenMP [2.5, Restrictions] 3880 // The num_threads expression must evaluate to a positive integer value. 3881 llvm::APSInt Result; 3882 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() && 3883 !Result.isStrictlyPositive()) { 3884 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause) 3885 << "num_threads" << NumThreads->getSourceRange(); 3886 return nullptr; 3887 } 3888 } 3889 3890 return new (Context) 3891 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc); 3892 } 3893 3894 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 3895 OpenMPClauseKind CKind) { 3896 if (!E) 3897 return ExprError(); 3898 if (E->isValueDependent() || E->isTypeDependent() || 3899 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 3900 return E; 3901 llvm::APSInt Result; 3902 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); 3903 if (ICE.isInvalid()) 3904 return ExprError(); 3905 if (!Result.isStrictlyPositive()) { 3906 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 3907 << getOpenMPClauseName(CKind) << E->getSourceRange(); 3908 return ExprError(); 3909 } 3910 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 3911 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 3912 << E->getSourceRange(); 3913 return ExprError(); 3914 } 3915 return ICE; 3916 } 3917 3918 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 3919 SourceLocation LParenLoc, 3920 SourceLocation EndLoc) { 3921 // OpenMP [2.8.1, simd construct, Description] 3922 // The parameter of the safelen clause must be a constant 3923 // positive integer expression. 3924 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 3925 if (Safelen.isInvalid()) 3926 return nullptr; 3927 return new (Context) 3928 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 3929 } 3930 3931 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 3932 SourceLocation StartLoc, 3933 SourceLocation LParenLoc, 3934 SourceLocation EndLoc) { 3935 // OpenMP [2.7.1, loop construct, Description] 3936 // OpenMP [2.8.1, simd construct, Description] 3937 // OpenMP [2.9.6, distribute construct, Description] 3938 // The parameter of the collapse clause must be a constant 3939 // positive integer expression. 3940 ExprResult NumForLoopsResult = 3941 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 3942 if (NumForLoopsResult.isInvalid()) 3943 return nullptr; 3944 return new (Context) 3945 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 3946 } 3947 3948 OMPClause *Sema::ActOnOpenMPSimpleClause( 3949 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 3950 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 3951 OMPClause *Res = nullptr; 3952 switch (Kind) { 3953 case OMPC_default: 3954 Res = 3955 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), 3956 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 3957 break; 3958 case OMPC_proc_bind: 3959 Res = ActOnOpenMPProcBindClause( 3960 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, 3961 LParenLoc, EndLoc); 3962 break; 3963 case OMPC_if: 3964 case OMPC_final: 3965 case OMPC_num_threads: 3966 case OMPC_safelen: 3967 case OMPC_collapse: 3968 case OMPC_schedule: 3969 case OMPC_private: 3970 case OMPC_firstprivate: 3971 case OMPC_lastprivate: 3972 case OMPC_shared: 3973 case OMPC_reduction: 3974 case OMPC_linear: 3975 case OMPC_aligned: 3976 case OMPC_copyin: 3977 case OMPC_copyprivate: 3978 case OMPC_ordered: 3979 case OMPC_nowait: 3980 case OMPC_untied: 3981 case OMPC_mergeable: 3982 case OMPC_threadprivate: 3983 case OMPC_flush: 3984 case OMPC_read: 3985 case OMPC_write: 3986 case OMPC_update: 3987 case OMPC_capture: 3988 case OMPC_seq_cst: 3989 case OMPC_unknown: 3990 llvm_unreachable("Clause is not allowed."); 3991 } 3992 return Res; 3993 } 3994 3995 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, 3996 SourceLocation KindKwLoc, 3997 SourceLocation StartLoc, 3998 SourceLocation LParenLoc, 3999 SourceLocation EndLoc) { 4000 if (Kind == OMPC_DEFAULT_unknown) { 4001 std::string Values; 4002 static_assert(OMPC_DEFAULT_unknown > 0, 4003 "OMPC_DEFAULT_unknown not greater than 0"); 4004 std::string Sep(", "); 4005 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) { 4006 Values += "'"; 4007 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i); 4008 Values += "'"; 4009 switch (i) { 4010 case OMPC_DEFAULT_unknown - 2: 4011 Values += " or "; 4012 break; 4013 case OMPC_DEFAULT_unknown - 1: 4014 break; 4015 default: 4016 Values += Sep; 4017 break; 4018 } 4019 } 4020 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 4021 << Values << getOpenMPClauseName(OMPC_default); 4022 return nullptr; 4023 } 4024 switch (Kind) { 4025 case OMPC_DEFAULT_none: 4026 DSAStack->setDefaultDSANone(KindKwLoc); 4027 break; 4028 case OMPC_DEFAULT_shared: 4029 DSAStack->setDefaultDSAShared(KindKwLoc); 4030 break; 4031 case OMPC_DEFAULT_unknown: 4032 llvm_unreachable("Clause kind is not allowed."); 4033 break; 4034 } 4035 return new (Context) 4036 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 4037 } 4038 4039 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, 4040 SourceLocation KindKwLoc, 4041 SourceLocation StartLoc, 4042 SourceLocation LParenLoc, 4043 SourceLocation EndLoc) { 4044 if (Kind == OMPC_PROC_BIND_unknown) { 4045 std::string Values; 4046 std::string Sep(", "); 4047 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) { 4048 Values += "'"; 4049 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i); 4050 Values += "'"; 4051 switch (i) { 4052 case OMPC_PROC_BIND_unknown - 2: 4053 Values += " or "; 4054 break; 4055 case OMPC_PROC_BIND_unknown - 1: 4056 break; 4057 default: 4058 Values += Sep; 4059 break; 4060 } 4061 } 4062 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 4063 << Values << getOpenMPClauseName(OMPC_proc_bind); 4064 return nullptr; 4065 } 4066 return new (Context) 4067 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 4068 } 4069 4070 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 4071 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr, 4072 SourceLocation StartLoc, SourceLocation LParenLoc, 4073 SourceLocation ArgumentLoc, SourceLocation CommaLoc, 4074 SourceLocation EndLoc) { 4075 OMPClause *Res = nullptr; 4076 switch (Kind) { 4077 case OMPC_schedule: 4078 Res = ActOnOpenMPScheduleClause( 4079 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc, 4080 LParenLoc, ArgumentLoc, CommaLoc, EndLoc); 4081 break; 4082 case OMPC_if: 4083 case OMPC_final: 4084 case OMPC_num_threads: 4085 case OMPC_safelen: 4086 case OMPC_collapse: 4087 case OMPC_default: 4088 case OMPC_proc_bind: 4089 case OMPC_private: 4090 case OMPC_firstprivate: 4091 case OMPC_lastprivate: 4092 case OMPC_shared: 4093 case OMPC_reduction: 4094 case OMPC_linear: 4095 case OMPC_aligned: 4096 case OMPC_copyin: 4097 case OMPC_copyprivate: 4098 case OMPC_ordered: 4099 case OMPC_nowait: 4100 case OMPC_untied: 4101 case OMPC_mergeable: 4102 case OMPC_threadprivate: 4103 case OMPC_flush: 4104 case OMPC_read: 4105 case OMPC_write: 4106 case OMPC_update: 4107 case OMPC_capture: 4108 case OMPC_seq_cst: 4109 case OMPC_unknown: 4110 llvm_unreachable("Clause is not allowed."); 4111 } 4112 return Res; 4113 } 4114 4115 OMPClause *Sema::ActOnOpenMPScheduleClause( 4116 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 4117 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 4118 SourceLocation EndLoc) { 4119 if (Kind == OMPC_SCHEDULE_unknown) { 4120 std::string Values; 4121 std::string Sep(", "); 4122 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) { 4123 Values += "'"; 4124 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i); 4125 Values += "'"; 4126 switch (i) { 4127 case OMPC_SCHEDULE_unknown - 2: 4128 Values += " or "; 4129 break; 4130 case OMPC_SCHEDULE_unknown - 1: 4131 break; 4132 default: 4133 Values += Sep; 4134 break; 4135 } 4136 } 4137 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 4138 << Values << getOpenMPClauseName(OMPC_schedule); 4139 return nullptr; 4140 } 4141 Expr *ValExpr = ChunkSize; 4142 if (ChunkSize) { 4143 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 4144 !ChunkSize->isInstantiationDependent() && 4145 !ChunkSize->containsUnexpandedParameterPack()) { 4146 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); 4147 ExprResult Val = 4148 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 4149 if (Val.isInvalid()) 4150 return nullptr; 4151 4152 ValExpr = Val.get(); 4153 4154 // OpenMP [2.7.1, Restrictions] 4155 // chunk_size must be a loop invariant integer expression with a positive 4156 // value. 4157 llvm::APSInt Result; 4158 if (ValExpr->isIntegerConstantExpr(Result, Context) && 4159 Result.isSigned() && !Result.isStrictlyPositive()) { 4160 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 4161 << "schedule" << ChunkSize->getSourceRange(); 4162 return nullptr; 4163 } 4164 } 4165 } 4166 4167 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, 4168 EndLoc, Kind, ValExpr); 4169 } 4170 4171 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 4172 SourceLocation StartLoc, 4173 SourceLocation EndLoc) { 4174 OMPClause *Res = nullptr; 4175 switch (Kind) { 4176 case OMPC_ordered: 4177 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 4178 break; 4179 case OMPC_nowait: 4180 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 4181 break; 4182 case OMPC_untied: 4183 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 4184 break; 4185 case OMPC_mergeable: 4186 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 4187 break; 4188 case OMPC_read: 4189 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 4190 break; 4191 case OMPC_write: 4192 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 4193 break; 4194 case OMPC_update: 4195 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 4196 break; 4197 case OMPC_capture: 4198 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 4199 break; 4200 case OMPC_seq_cst: 4201 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 4202 break; 4203 case OMPC_if: 4204 case OMPC_final: 4205 case OMPC_num_threads: 4206 case OMPC_safelen: 4207 case OMPC_collapse: 4208 case OMPC_schedule: 4209 case OMPC_private: 4210 case OMPC_firstprivate: 4211 case OMPC_lastprivate: 4212 case OMPC_shared: 4213 case OMPC_reduction: 4214 case OMPC_linear: 4215 case OMPC_aligned: 4216 case OMPC_copyin: 4217 case OMPC_copyprivate: 4218 case OMPC_default: 4219 case OMPC_proc_bind: 4220 case OMPC_threadprivate: 4221 case OMPC_flush: 4222 case OMPC_unknown: 4223 llvm_unreachable("Clause is not allowed."); 4224 } 4225 return Res; 4226 } 4227 4228 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 4229 SourceLocation EndLoc) { 4230 DSAStack->setOrderedRegion(); 4231 return new (Context) OMPOrderedClause(StartLoc, EndLoc); 4232 } 4233 4234 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 4235 SourceLocation EndLoc) { 4236 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 4237 } 4238 4239 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 4240 SourceLocation EndLoc) { 4241 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 4242 } 4243 4244 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 4245 SourceLocation EndLoc) { 4246 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 4247 } 4248 4249 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 4250 SourceLocation EndLoc) { 4251 return new (Context) OMPReadClause(StartLoc, EndLoc); 4252 } 4253 4254 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 4255 SourceLocation EndLoc) { 4256 return new (Context) OMPWriteClause(StartLoc, EndLoc); 4257 } 4258 4259 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 4260 SourceLocation EndLoc) { 4261 return new (Context) OMPUpdateClause(StartLoc, EndLoc); 4262 } 4263 4264 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 4265 SourceLocation EndLoc) { 4266 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 4267 } 4268 4269 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 4270 SourceLocation EndLoc) { 4271 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 4272 } 4273 4274 OMPClause *Sema::ActOnOpenMPVarListClause( 4275 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, 4276 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 4277 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec, 4278 const DeclarationNameInfo &ReductionId) { 4279 OMPClause *Res = nullptr; 4280 switch (Kind) { 4281 case OMPC_private: 4282 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 4283 break; 4284 case OMPC_firstprivate: 4285 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 4286 break; 4287 case OMPC_lastprivate: 4288 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 4289 break; 4290 case OMPC_shared: 4291 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 4292 break; 4293 case OMPC_reduction: 4294 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 4295 EndLoc, ReductionIdScopeSpec, ReductionId); 4296 break; 4297 case OMPC_linear: 4298 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, 4299 ColonLoc, EndLoc); 4300 break; 4301 case OMPC_aligned: 4302 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, 4303 ColonLoc, EndLoc); 4304 break; 4305 case OMPC_copyin: 4306 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 4307 break; 4308 case OMPC_copyprivate: 4309 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 4310 break; 4311 case OMPC_flush: 4312 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 4313 break; 4314 case OMPC_if: 4315 case OMPC_final: 4316 case OMPC_num_threads: 4317 case OMPC_safelen: 4318 case OMPC_collapse: 4319 case OMPC_default: 4320 case OMPC_proc_bind: 4321 case OMPC_schedule: 4322 case OMPC_ordered: 4323 case OMPC_nowait: 4324 case OMPC_untied: 4325 case OMPC_mergeable: 4326 case OMPC_threadprivate: 4327 case OMPC_read: 4328 case OMPC_write: 4329 case OMPC_update: 4330 case OMPC_capture: 4331 case OMPC_seq_cst: 4332 case OMPC_unknown: 4333 llvm_unreachable("Clause is not allowed."); 4334 } 4335 return Res; 4336 } 4337 4338 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 4339 SourceLocation StartLoc, 4340 SourceLocation LParenLoc, 4341 SourceLocation EndLoc) { 4342 SmallVector<Expr *, 8> Vars; 4343 SmallVector<Expr *, 8> PrivateCopies; 4344 for (auto &RefExpr : VarList) { 4345 assert(RefExpr && "NULL expr in OpenMP private clause."); 4346 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 4347 // It will be analyzed later. 4348 Vars.push_back(RefExpr); 4349 PrivateCopies.push_back(nullptr); 4350 continue; 4351 } 4352 4353 SourceLocation ELoc = RefExpr->getExprLoc(); 4354 // OpenMP [2.1, C/C++] 4355 // A list item is a variable name. 4356 // OpenMP [2.9.3.3, Restrictions, p.1] 4357 // A variable that is part of another variable (as an array or 4358 // structure element) cannot appear in a private clause. 4359 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 4360 if (!DE || !isa<VarDecl>(DE->getDecl())) { 4361 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 4362 continue; 4363 } 4364 Decl *D = DE->getDecl(); 4365 VarDecl *VD = cast<VarDecl>(D); 4366 4367 QualType Type = VD->getType(); 4368 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 4369 // It will be analyzed later. 4370 Vars.push_back(DE); 4371 PrivateCopies.push_back(nullptr); 4372 continue; 4373 } 4374 4375 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 4376 // A variable that appears in a private clause must not have an incomplete 4377 // type or a reference type. 4378 if (RequireCompleteType(ELoc, Type, 4379 diag::err_omp_private_incomplete_type)) { 4380 continue; 4381 } 4382 if (Type->isReferenceType()) { 4383 Diag(ELoc, diag::err_omp_clause_ref_type_arg) 4384 << getOpenMPClauseName(OMPC_private) << Type; 4385 bool IsDecl = 4386 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 4387 Diag(VD->getLocation(), 4388 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 4389 << VD; 4390 continue; 4391 } 4392 4393 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 4394 // A variable of class type (or array thereof) that appears in a private 4395 // clause requires an accessible, unambiguous default constructor for the 4396 // class type. 4397 while (Type->isArrayType()) { 4398 Type = cast<ArrayType>(Type.getTypePtr())->getElementType(); 4399 } 4400 4401 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 4402 // in a Construct] 4403 // Variables with the predetermined data-sharing attributes may not be 4404 // listed in data-sharing attributes clauses, except for the cases 4405 // listed below. For these exceptions only, listing a predetermined 4406 // variable in a data-sharing attribute clause is allowed and overrides 4407 // the variable's predetermined data-sharing attributes. 4408 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); 4409 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 4410 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 4411 << getOpenMPClauseName(OMPC_private); 4412 ReportOriginalDSA(*this, DSAStack, VD, DVar); 4413 continue; 4414 } 4415 4416 // Generate helper private variable and initialize it with the default 4417 // value. The address of the original variable is replaced by the address of 4418 // the new private variable in CodeGen. This new variable is not added to 4419 // IdResolver, so the code in the OpenMP region uses original variable for 4420 // proper diagnostics. 4421 auto VDPrivate = 4422 VarDecl::Create(Context, CurContext, DE->getLocStart(), 4423 DE->getExprLoc(), VD->getIdentifier(), VD->getType(), 4424 VD->getTypeSourceInfo(), /*S*/ SC_Auto); 4425 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto*/ false); 4426 if (VDPrivate->isInvalidDecl()) 4427 continue; 4428 CurContext->addDecl(VDPrivate); 4429 auto VDPrivateRefExpr = 4430 DeclRefExpr::Create(Context, /*QualifierLoc*/ NestedNameSpecifierLoc(), 4431 /*TemplateKWLoc*/ SourceLocation(), VDPrivate, 4432 /*RefersToEnclosingVariableOrCapture*/ false, 4433 /*NameLoc*/ SourceLocation(), DE->getType(), 4434 /*VK*/ VK_LValue); 4435 4436 DSAStack->addDSA(VD, DE, OMPC_private); 4437 Vars.push_back(DE); 4438 PrivateCopies.push_back(VDPrivateRefExpr); 4439 } 4440 4441 if (Vars.empty()) 4442 return nullptr; 4443 4444 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 4445 PrivateCopies); 4446 } 4447 4448 namespace { 4449 class DiagsUninitializedSeveretyRAII { 4450 private: 4451 DiagnosticsEngine &Diags; 4452 SourceLocation SavedLoc; 4453 bool IsIgnored; 4454 4455 public: 4456 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 4457 bool IsIgnored) 4458 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 4459 if (!IsIgnored) { 4460 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 4461 /*Map*/ diag::Severity::Ignored, Loc); 4462 } 4463 } 4464 ~DiagsUninitializedSeveretyRAII() { 4465 if (!IsIgnored) 4466 Diags.popMappings(SavedLoc); 4467 } 4468 }; 4469 } 4470 4471 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 4472 SourceLocation StartLoc, 4473 SourceLocation LParenLoc, 4474 SourceLocation EndLoc) { 4475 SmallVector<Expr *, 8> Vars; 4476 SmallVector<Expr *, 8> PrivateCopies; 4477 SmallVector<Expr *, 8> Inits; 4478 bool IsImplicitClause = 4479 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 4480 auto ImplicitClauseLoc = DSAStack->getConstructLoc(); 4481 4482 for (auto &RefExpr : VarList) { 4483 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 4484 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 4485 // It will be analyzed later. 4486 Vars.push_back(RefExpr); 4487 PrivateCopies.push_back(nullptr); 4488 Inits.push_back(nullptr); 4489 continue; 4490 } 4491 4492 SourceLocation ELoc = 4493 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc(); 4494 // OpenMP [2.1, C/C++] 4495 // A list item is a variable name. 4496 // OpenMP [2.9.3.3, Restrictions, p.1] 4497 // A variable that is part of another variable (as an array or 4498 // structure element) cannot appear in a private clause. 4499 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 4500 if (!DE || !isa<VarDecl>(DE->getDecl())) { 4501 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 4502 continue; 4503 } 4504 Decl *D = DE->getDecl(); 4505 VarDecl *VD = cast<VarDecl>(D); 4506 4507 QualType Type = VD->getType(); 4508 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 4509 // It will be analyzed later. 4510 Vars.push_back(DE); 4511 PrivateCopies.push_back(nullptr); 4512 Inits.push_back(nullptr); 4513 continue; 4514 } 4515 4516 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 4517 // A variable that appears in a private clause must not have an incomplete 4518 // type or a reference type. 4519 if (RequireCompleteType(ELoc, Type, 4520 diag::err_omp_firstprivate_incomplete_type)) { 4521 continue; 4522 } 4523 if (Type->isReferenceType()) { 4524 if (IsImplicitClause) { 4525 Diag(ImplicitClauseLoc, 4526 diag::err_omp_task_predetermined_firstprivate_ref_type_arg) 4527 << Type; 4528 Diag(RefExpr->getExprLoc(), diag::note_used_here); 4529 } else { 4530 Diag(ELoc, diag::err_omp_clause_ref_type_arg) 4531 << getOpenMPClauseName(OMPC_firstprivate) << Type; 4532 } 4533 bool IsDecl = 4534 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 4535 Diag(VD->getLocation(), 4536 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 4537 << VD; 4538 continue; 4539 } 4540 4541 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 4542 // A variable of class type (or array thereof) that appears in a private 4543 // clause requires an accessible, unambiguous copy constructor for the 4544 // class type. 4545 Type = Context.getBaseElementType(Type); 4546 4547 // If an implicit firstprivate variable found it was checked already. 4548 if (!IsImplicitClause) { 4549 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); 4550 Type = Type.getNonReferenceType().getCanonicalType(); 4551 bool IsConstant = Type.isConstant(Context); 4552 Type = Context.getBaseElementType(Type); 4553 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 4554 // A list item that specifies a given variable may not appear in more 4555 // than one clause on the same directive, except that a variable may be 4556 // specified in both firstprivate and lastprivate clauses. 4557 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 4558 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) { 4559 Diag(ELoc, diag::err_omp_wrong_dsa) 4560 << getOpenMPClauseName(DVar.CKind) 4561 << getOpenMPClauseName(OMPC_firstprivate); 4562 ReportOriginalDSA(*this, DSAStack, VD, DVar); 4563 continue; 4564 } 4565 4566 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 4567 // in a Construct] 4568 // Variables with the predetermined data-sharing attributes may not be 4569 // listed in data-sharing attributes clauses, except for the cases 4570 // listed below. For these exceptions only, listing a predetermined 4571 // variable in a data-sharing attribute clause is allowed and overrides 4572 // the variable's predetermined data-sharing attributes. 4573 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 4574 // in a Construct, C/C++, p.2] 4575 // Variables with const-qualified type having no mutable member may be 4576 // listed in a firstprivate clause, even if they are static data members. 4577 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr && 4578 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 4579 Diag(ELoc, diag::err_omp_wrong_dsa) 4580 << getOpenMPClauseName(DVar.CKind) 4581 << getOpenMPClauseName(OMPC_firstprivate); 4582 ReportOriginalDSA(*this, DSAStack, VD, DVar); 4583 continue; 4584 } 4585 4586 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 4587 // OpenMP [2.9.3.4, Restrictions, p.2] 4588 // A list item that is private within a parallel region must not appear 4589 // in a firstprivate clause on a worksharing construct if any of the 4590 // worksharing regions arising from the worksharing construct ever bind 4591 // to any of the parallel regions arising from the parallel construct. 4592 if (isOpenMPWorksharingDirective(CurrDir) && 4593 !isOpenMPParallelDirective(CurrDir)) { 4594 DVar = DSAStack->getImplicitDSA(VD, true); 4595 if (DVar.CKind != OMPC_shared && 4596 (isOpenMPParallelDirective(DVar.DKind) || 4597 DVar.DKind == OMPD_unknown)) { 4598 Diag(ELoc, diag::err_omp_required_access) 4599 << getOpenMPClauseName(OMPC_firstprivate) 4600 << getOpenMPClauseName(OMPC_shared); 4601 ReportOriginalDSA(*this, DSAStack, VD, DVar); 4602 continue; 4603 } 4604 } 4605 // OpenMP [2.9.3.4, Restrictions, p.3] 4606 // A list item that appears in a reduction clause of a parallel construct 4607 // must not appear in a firstprivate clause on a worksharing or task 4608 // construct if any of the worksharing or task regions arising from the 4609 // worksharing or task construct ever bind to any of the parallel regions 4610 // arising from the parallel construct. 4611 // OpenMP [2.9.3.4, Restrictions, p.4] 4612 // A list item that appears in a reduction clause in worksharing 4613 // construct must not appear in a firstprivate clause in a task construct 4614 // encountered during execution of any of the worksharing regions arising 4615 // from the worksharing construct. 4616 if (CurrDir == OMPD_task) { 4617 DVar = 4618 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction), 4619 [](OpenMPDirectiveKind K) -> bool { 4620 return isOpenMPParallelDirective(K) || 4621 isOpenMPWorksharingDirective(K); 4622 }, 4623 false); 4624 if (DVar.CKind == OMPC_reduction && 4625 (isOpenMPParallelDirective(DVar.DKind) || 4626 isOpenMPWorksharingDirective(DVar.DKind))) { 4627 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 4628 << getOpenMPDirectiveName(DVar.DKind); 4629 ReportOriginalDSA(*this, DSAStack, VD, DVar); 4630 continue; 4631 } 4632 } 4633 } 4634 4635 Type = Type.getUnqualifiedType(); 4636 auto VDPrivate = VarDecl::Create(Context, CurContext, DE->getLocStart(), 4637 ELoc, VD->getIdentifier(), VD->getType(), 4638 VD->getTypeSourceInfo(), /*S*/ SC_Auto); 4639 // Generate helper private variable and initialize it with the value of the 4640 // original variable. The address of the original variable is replaced by 4641 // the address of the new private variable in the CodeGen. This new variable 4642 // is not added to IdResolver, so the code in the OpenMP region uses 4643 // original variable for proper diagnostics and variable capturing. 4644 Expr *VDInitRefExpr = nullptr; 4645 // For arrays generate initializer for single element and replace it by the 4646 // original array element in CodeGen. 4647 if (DE->getType()->isArrayType()) { 4648 auto VDInit = VarDecl::Create(Context, CurContext, DE->getLocStart(), 4649 ELoc, VD->getIdentifier(), Type, 4650 VD->getTypeSourceInfo(), /*S*/ SC_Auto); 4651 CurContext->addHiddenDecl(VDInit); 4652 VDInitRefExpr = DeclRefExpr::Create( 4653 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(), 4654 /*TemplateKWLoc*/ SourceLocation(), VDInit, 4655 /*RefersToEnclosingVariableOrCapture*/ true, ELoc, Type, 4656 /*VK*/ VK_LValue); 4657 VDInit->setIsUsed(); 4658 auto Init = DefaultLvalueConversion(VDInitRefExpr).get(); 4659 InitializedEntity Entity = InitializedEntity::InitializeVariable(VDInit); 4660 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 4661 4662 InitializationSequence InitSeq(*this, Entity, Kind, Init); 4663 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 4664 if (Result.isInvalid()) 4665 VDPrivate->setInvalidDecl(); 4666 else 4667 VDPrivate->setInit(Result.getAs<Expr>()); 4668 } else { 4669 AddInitializerToDecl( 4670 VDPrivate, 4671 DefaultLvalueConversion( 4672 DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 4673 SourceLocation(), DE->getDecl(), 4674 /*RefersToEnclosingVariableOrCapture=*/true, 4675 DE->getExprLoc(), DE->getType(), 4676 /*VK=*/VK_LValue)).get(), 4677 /*DirectInit=*/false, /*TypeMayContainAuto=*/false); 4678 } 4679 if (VDPrivate->isInvalidDecl()) { 4680 if (IsImplicitClause) { 4681 Diag(DE->getExprLoc(), 4682 diag::note_omp_task_predetermined_firstprivate_here); 4683 } 4684 continue; 4685 } 4686 CurContext->addDecl(VDPrivate); 4687 auto VDPrivateRefExpr = 4688 DeclRefExpr::Create(Context, /*QualifierLoc*/ NestedNameSpecifierLoc(), 4689 /*TemplateKWLoc*/ SourceLocation(), VDPrivate, 4690 /*RefersToEnclosingVariableOrCapture*/ false, 4691 DE->getLocStart(), DE->getType(), 4692 /*VK*/ VK_LValue); 4693 DSAStack->addDSA(VD, DE, OMPC_firstprivate); 4694 Vars.push_back(DE); 4695 PrivateCopies.push_back(VDPrivateRefExpr); 4696 Inits.push_back(VDInitRefExpr); 4697 } 4698 4699 if (Vars.empty()) 4700 return nullptr; 4701 4702 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 4703 Vars, PrivateCopies, Inits); 4704 } 4705 4706 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList, 4707 SourceLocation StartLoc, 4708 SourceLocation LParenLoc, 4709 SourceLocation EndLoc) { 4710 SmallVector<Expr *, 8> Vars; 4711 for (auto &RefExpr : VarList) { 4712 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 4713 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 4714 // It will be analyzed later. 4715 Vars.push_back(RefExpr); 4716 continue; 4717 } 4718 4719 SourceLocation ELoc = RefExpr->getExprLoc(); 4720 // OpenMP [2.1, C/C++] 4721 // A list item is a variable name. 4722 // OpenMP [2.14.3.5, Restrictions, p.1] 4723 // A variable that is part of another variable (as an array or structure 4724 // element) cannot appear in a lastprivate clause. 4725 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 4726 if (!DE || !isa<VarDecl>(DE->getDecl())) { 4727 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 4728 continue; 4729 } 4730 Decl *D = DE->getDecl(); 4731 VarDecl *VD = cast<VarDecl>(D); 4732 4733 QualType Type = VD->getType(); 4734 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 4735 // It will be analyzed later. 4736 Vars.push_back(DE); 4737 continue; 4738 } 4739 4740 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 4741 // A variable that appears in a lastprivate clause must not have an 4742 // incomplete type or a reference type. 4743 if (RequireCompleteType(ELoc, Type, 4744 diag::err_omp_lastprivate_incomplete_type)) { 4745 continue; 4746 } 4747 if (Type->isReferenceType()) { 4748 Diag(ELoc, diag::err_omp_clause_ref_type_arg) 4749 << getOpenMPClauseName(OMPC_lastprivate) << Type; 4750 bool IsDecl = 4751 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 4752 Diag(VD->getLocation(), 4753 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 4754 << VD; 4755 continue; 4756 } 4757 4758 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 4759 // in a Construct] 4760 // Variables with the predetermined data-sharing attributes may not be 4761 // listed in data-sharing attributes clauses, except for the cases 4762 // listed below. 4763 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); 4764 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 4765 DVar.CKind != OMPC_firstprivate && 4766 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 4767 Diag(ELoc, diag::err_omp_wrong_dsa) 4768 << getOpenMPClauseName(DVar.CKind) 4769 << getOpenMPClauseName(OMPC_lastprivate); 4770 ReportOriginalDSA(*this, DSAStack, VD, DVar); 4771 continue; 4772 } 4773 4774 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 4775 // OpenMP [2.14.3.5, Restrictions, p.2] 4776 // A list item that is private within a parallel region, or that appears in 4777 // the reduction clause of a parallel construct, must not appear in a 4778 // lastprivate clause on a worksharing construct if any of the corresponding 4779 // worksharing regions ever binds to any of the corresponding parallel 4780 // regions. 4781 if (isOpenMPWorksharingDirective(CurrDir) && 4782 !isOpenMPParallelDirective(CurrDir)) { 4783 DVar = DSAStack->getImplicitDSA(VD, true); 4784 if (DVar.CKind != OMPC_shared) { 4785 Diag(ELoc, diag::err_omp_required_access) 4786 << getOpenMPClauseName(OMPC_lastprivate) 4787 << getOpenMPClauseName(OMPC_shared); 4788 ReportOriginalDSA(*this, DSAStack, VD, DVar); 4789 continue; 4790 } 4791 } 4792 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 4793 // A variable of class type (or array thereof) that appears in a 4794 // lastprivate clause requires an accessible, unambiguous default 4795 // constructor for the class type, unless the list item is also specified 4796 // in a firstprivate clause. 4797 // A variable of class type (or array thereof) that appears in a 4798 // lastprivate clause requires an accessible, unambiguous copy assignment 4799 // operator for the class type. 4800 while (Type.getNonReferenceType()->isArrayType()) 4801 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr()) 4802 ->getElementType(); 4803 CXXRecordDecl *RD = getLangOpts().CPlusPlus 4804 ? Type.getNonReferenceType()->getAsCXXRecordDecl() 4805 : nullptr; 4806 // FIXME This code must be replaced by actual copying and destructing of the 4807 // lastprivate variable. 4808 if (RD) { 4809 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0); 4810 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess()); 4811 if (MD) { 4812 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible || 4813 MD->isDeleted()) { 4814 Diag(ELoc, diag::err_omp_required_method) 4815 << getOpenMPClauseName(OMPC_lastprivate) << 2; 4816 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 4817 VarDecl::DeclarationOnly; 4818 Diag(VD->getLocation(), 4819 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 4820 << VD; 4821 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 4822 continue; 4823 } 4824 MarkFunctionReferenced(ELoc, MD); 4825 DiagnoseUseOfDecl(MD, ELoc); 4826 } 4827 4828 CXXDestructorDecl *DD = RD->getDestructor(); 4829 if (DD) { 4830 PartialDiagnostic PD = 4831 PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); 4832 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible || 4833 DD->isDeleted()) { 4834 Diag(ELoc, diag::err_omp_required_method) 4835 << getOpenMPClauseName(OMPC_lastprivate) << 4; 4836 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 4837 VarDecl::DeclarationOnly; 4838 Diag(VD->getLocation(), 4839 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 4840 << VD; 4841 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 4842 continue; 4843 } 4844 MarkFunctionReferenced(ELoc, DD); 4845 DiagnoseUseOfDecl(DD, ELoc); 4846 } 4847 } 4848 4849 if (DVar.CKind != OMPC_firstprivate) 4850 DSAStack->addDSA(VD, DE, OMPC_lastprivate); 4851 Vars.push_back(DE); 4852 } 4853 4854 if (Vars.empty()) 4855 return nullptr; 4856 4857 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 4858 Vars); 4859 } 4860 4861 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 4862 SourceLocation StartLoc, 4863 SourceLocation LParenLoc, 4864 SourceLocation EndLoc) { 4865 SmallVector<Expr *, 8> Vars; 4866 for (auto &RefExpr : VarList) { 4867 assert(RefExpr && "NULL expr in OpenMP shared clause."); 4868 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 4869 // It will be analyzed later. 4870 Vars.push_back(RefExpr); 4871 continue; 4872 } 4873 4874 SourceLocation ELoc = RefExpr->getExprLoc(); 4875 // OpenMP [2.1, C/C++] 4876 // A list item is a variable name. 4877 // OpenMP [2.14.3.2, Restrictions, p.1] 4878 // A variable that is part of another variable (as an array or structure 4879 // element) cannot appear in a shared unless it is a static data member 4880 // of a C++ class. 4881 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 4882 if (!DE || !isa<VarDecl>(DE->getDecl())) { 4883 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 4884 continue; 4885 } 4886 Decl *D = DE->getDecl(); 4887 VarDecl *VD = cast<VarDecl>(D); 4888 4889 QualType Type = VD->getType(); 4890 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 4891 // It will be analyzed later. 4892 Vars.push_back(DE); 4893 continue; 4894 } 4895 4896 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 4897 // in a Construct] 4898 // Variables with the predetermined data-sharing attributes may not be 4899 // listed in data-sharing attributes clauses, except for the cases 4900 // listed below. For these exceptions only, listing a predetermined 4901 // variable in a data-sharing attribute clause is allowed and overrides 4902 // the variable's predetermined data-sharing attributes. 4903 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); 4904 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 4905 DVar.RefExpr) { 4906 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 4907 << getOpenMPClauseName(OMPC_shared); 4908 ReportOriginalDSA(*this, DSAStack, VD, DVar); 4909 continue; 4910 } 4911 4912 DSAStack->addDSA(VD, DE, OMPC_shared); 4913 Vars.push_back(DE); 4914 } 4915 4916 if (Vars.empty()) 4917 return nullptr; 4918 4919 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 4920 } 4921 4922 namespace { 4923 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 4924 DSAStackTy *Stack; 4925 4926 public: 4927 bool VisitDeclRefExpr(DeclRefExpr *E) { 4928 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) { 4929 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false); 4930 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 4931 return false; 4932 if (DVar.CKind != OMPC_unknown) 4933 return true; 4934 DSAStackTy::DSAVarData DVarPrivate = 4935 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false); 4936 if (DVarPrivate.CKind != OMPC_unknown) 4937 return true; 4938 return false; 4939 } 4940 return false; 4941 } 4942 bool VisitStmt(Stmt *S) { 4943 for (auto Child : S->children()) { 4944 if (Child && Visit(Child)) 4945 return true; 4946 } 4947 return false; 4948 } 4949 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 4950 }; 4951 } // namespace 4952 4953 OMPClause *Sema::ActOnOpenMPReductionClause( 4954 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 4955 SourceLocation ColonLoc, SourceLocation EndLoc, 4956 CXXScopeSpec &ReductionIdScopeSpec, 4957 const DeclarationNameInfo &ReductionId) { 4958 // TODO: Allow scope specification search when 'declare reduction' is 4959 // supported. 4960 assert(ReductionIdScopeSpec.isEmpty() && 4961 "No support for scoped reduction identifiers yet."); 4962 4963 auto DN = ReductionId.getName(); 4964 auto OOK = DN.getCXXOverloadedOperator(); 4965 BinaryOperatorKind BOK = BO_Comma; 4966 4967 // OpenMP [2.14.3.6, reduction clause] 4968 // C 4969 // reduction-identifier is either an identifier or one of the following 4970 // operators: +, -, *, &, |, ^, && and || 4971 // C++ 4972 // reduction-identifier is either an id-expression or one of the following 4973 // operators: +, -, *, &, |, ^, && and || 4974 // FIXME: Only 'min' and 'max' identifiers are supported for now. 4975 switch (OOK) { 4976 case OO_Plus: 4977 case OO_Minus: 4978 BOK = BO_AddAssign; 4979 break; 4980 case OO_Star: 4981 BOK = BO_MulAssign; 4982 break; 4983 case OO_Amp: 4984 BOK = BO_AndAssign; 4985 break; 4986 case OO_Pipe: 4987 BOK = BO_OrAssign; 4988 break; 4989 case OO_Caret: 4990 BOK = BO_XorAssign; 4991 break; 4992 case OO_AmpAmp: 4993 BOK = BO_LAnd; 4994 break; 4995 case OO_PipePipe: 4996 BOK = BO_LOr; 4997 break; 4998 default: 4999 if (auto II = DN.getAsIdentifierInfo()) { 5000 if (II->isStr("max")) 5001 BOK = BO_GT; 5002 else if (II->isStr("min")) 5003 BOK = BO_LT; 5004 } 5005 break; 5006 } 5007 SourceRange ReductionIdRange; 5008 if (ReductionIdScopeSpec.isValid()) { 5009 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 5010 } 5011 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 5012 if (BOK == BO_Comma) { 5013 // Not allowed reduction identifier is found. 5014 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier) 5015 << ReductionIdRange; 5016 return nullptr; 5017 } 5018 5019 SmallVector<Expr *, 8> Vars; 5020 for (auto RefExpr : VarList) { 5021 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 5022 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 5023 // It will be analyzed later. 5024 Vars.push_back(RefExpr); 5025 continue; 5026 } 5027 5028 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 5029 RefExpr->isInstantiationDependent() || 5030 RefExpr->containsUnexpandedParameterPack()) { 5031 // It will be analyzed later. 5032 Vars.push_back(RefExpr); 5033 continue; 5034 } 5035 5036 auto ELoc = RefExpr->getExprLoc(); 5037 auto ERange = RefExpr->getSourceRange(); 5038 // OpenMP [2.1, C/C++] 5039 // A list item is a variable or array section, subject to the restrictions 5040 // specified in Section 2.4 on page 42 and in each of the sections 5041 // describing clauses and directives for which a list appears. 5042 // OpenMP [2.14.3.3, Restrictions, p.1] 5043 // A variable that is part of another variable (as an array or 5044 // structure element) cannot appear in a private clause. 5045 auto DE = dyn_cast<DeclRefExpr>(RefExpr); 5046 if (!DE || !isa<VarDecl>(DE->getDecl())) { 5047 Diag(ELoc, diag::err_omp_expected_var_name) << ERange; 5048 continue; 5049 } 5050 auto D = DE->getDecl(); 5051 auto VD = cast<VarDecl>(D); 5052 auto Type = VD->getType(); 5053 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 5054 // A variable that appears in a private clause must not have an incomplete 5055 // type or a reference type. 5056 if (RequireCompleteType(ELoc, Type, 5057 diag::err_omp_reduction_incomplete_type)) 5058 continue; 5059 // OpenMP [2.14.3.6, reduction clause, Restrictions] 5060 // Arrays may not appear in a reduction clause. 5061 if (Type.getNonReferenceType()->isArrayType()) { 5062 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange; 5063 bool IsDecl = 5064 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 5065 Diag(VD->getLocation(), 5066 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 5067 << VD; 5068 continue; 5069 } 5070 // OpenMP [2.14.3.6, reduction clause, Restrictions] 5071 // A list item that appears in a reduction clause must not be 5072 // const-qualified. 5073 if (Type.getNonReferenceType().isConstant(Context)) { 5074 Diag(ELoc, diag::err_omp_const_variable) 5075 << getOpenMPClauseName(OMPC_reduction) << Type << ERange; 5076 bool IsDecl = 5077 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 5078 Diag(VD->getLocation(), 5079 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 5080 << VD; 5081 continue; 5082 } 5083 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 5084 // If a list-item is a reference type then it must bind to the same object 5085 // for all threads of the team. 5086 VarDecl *VDDef = VD->getDefinition(); 5087 if (Type->isReferenceType() && VDDef) { 5088 DSARefChecker Check(DSAStack); 5089 if (Check.Visit(VDDef->getInit())) { 5090 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange; 5091 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 5092 continue; 5093 } 5094 } 5095 // OpenMP [2.14.3.6, reduction clause, Restrictions] 5096 // The type of a list item that appears in a reduction clause must be valid 5097 // for the reduction-identifier. For a max or min reduction in C, the type 5098 // of the list item must be an allowed arithmetic data type: char, int, 5099 // float, double, or _Bool, possibly modified with long, short, signed, or 5100 // unsigned. For a max or min reduction in C++, the type of the list item 5101 // must be an allowed arithmetic data type: char, wchar_t, int, float, 5102 // double, or bool, possibly modified with long, short, signed, or unsigned. 5103 if ((BOK == BO_GT || BOK == BO_LT) && 5104 !(Type->isScalarType() || 5105 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 5106 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 5107 << getLangOpts().CPlusPlus; 5108 bool IsDecl = 5109 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 5110 Diag(VD->getLocation(), 5111 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 5112 << VD; 5113 continue; 5114 } 5115 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 5116 !getLangOpts().CPlusPlus && Type->isFloatingType()) { 5117 Diag(ELoc, diag::err_omp_clause_floating_type_arg); 5118 bool IsDecl = 5119 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 5120 Diag(VD->getLocation(), 5121 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 5122 << VD; 5123 continue; 5124 } 5125 bool Suppress = getDiagnostics().getSuppressAllDiagnostics(); 5126 getDiagnostics().setSuppressAllDiagnostics(true); 5127 ExprResult ReductionOp = 5128 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK, 5129 RefExpr, RefExpr); 5130 getDiagnostics().setSuppressAllDiagnostics(Suppress); 5131 if (ReductionOp.isInvalid()) { 5132 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type 5133 << ReductionIdRange; 5134 bool IsDecl = 5135 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 5136 Diag(VD->getLocation(), 5137 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 5138 << VD; 5139 continue; 5140 } 5141 5142 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 5143 // in a Construct] 5144 // Variables with the predetermined data-sharing attributes may not be 5145 // listed in data-sharing attributes clauses, except for the cases 5146 // listed below. For these exceptions only, listing a predetermined 5147 // variable in a data-sharing attribute clause is allowed and overrides 5148 // the variable's predetermined data-sharing attributes. 5149 // OpenMP [2.14.3.6, Restrictions, p.3] 5150 // Any number of reduction clauses can be specified on the directive, 5151 // but a list item can appear only once in the reduction clauses for that 5152 // directive. 5153 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); 5154 if (DVar.CKind == OMPC_reduction) { 5155 Diag(ELoc, diag::err_omp_once_referenced) 5156 << getOpenMPClauseName(OMPC_reduction); 5157 if (DVar.RefExpr) { 5158 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 5159 } 5160 } else if (DVar.CKind != OMPC_unknown) { 5161 Diag(ELoc, diag::err_omp_wrong_dsa) 5162 << getOpenMPClauseName(DVar.CKind) 5163 << getOpenMPClauseName(OMPC_reduction); 5164 ReportOriginalDSA(*this, DSAStack, VD, DVar); 5165 continue; 5166 } 5167 5168 // OpenMP [2.14.3.6, Restrictions, p.1] 5169 // A list item that appears in a reduction clause of a worksharing 5170 // construct must be shared in the parallel regions to which any of the 5171 // worksharing regions arising from the worksharing construct bind. 5172 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 5173 if (isOpenMPWorksharingDirective(CurrDir) && 5174 !isOpenMPParallelDirective(CurrDir)) { 5175 DVar = DSAStack->getImplicitDSA(VD, true); 5176 if (DVar.CKind != OMPC_shared) { 5177 Diag(ELoc, diag::err_omp_required_access) 5178 << getOpenMPClauseName(OMPC_reduction) 5179 << getOpenMPClauseName(OMPC_shared); 5180 ReportOriginalDSA(*this, DSAStack, VD, DVar); 5181 continue; 5182 } 5183 } 5184 5185 CXXRecordDecl *RD = getLangOpts().CPlusPlus 5186 ? Type.getNonReferenceType()->getAsCXXRecordDecl() 5187 : nullptr; 5188 // FIXME This code must be replaced by actual constructing/destructing of 5189 // the reduction variable. 5190 if (RD) { 5191 CXXConstructorDecl *CD = LookupDefaultConstructor(RD); 5192 PartialDiagnostic PD = 5193 PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); 5194 if (!CD || 5195 CheckConstructorAccess(ELoc, CD, 5196 InitializedEntity::InitializeTemporary(Type), 5197 CD->getAccess(), PD) == AR_inaccessible || 5198 CD->isDeleted()) { 5199 Diag(ELoc, diag::err_omp_required_method) 5200 << getOpenMPClauseName(OMPC_reduction) << 0; 5201 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 5202 VarDecl::DeclarationOnly; 5203 Diag(VD->getLocation(), 5204 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 5205 << VD; 5206 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 5207 continue; 5208 } 5209 MarkFunctionReferenced(ELoc, CD); 5210 DiagnoseUseOfDecl(CD, ELoc); 5211 5212 CXXDestructorDecl *DD = RD->getDestructor(); 5213 if (DD) { 5214 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible || 5215 DD->isDeleted()) { 5216 Diag(ELoc, diag::err_omp_required_method) 5217 << getOpenMPClauseName(OMPC_reduction) << 4; 5218 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 5219 VarDecl::DeclarationOnly; 5220 Diag(VD->getLocation(), 5221 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 5222 << VD; 5223 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 5224 continue; 5225 } 5226 MarkFunctionReferenced(ELoc, DD); 5227 DiagnoseUseOfDecl(DD, ELoc); 5228 } 5229 } 5230 5231 DSAStack->addDSA(VD, DE, OMPC_reduction); 5232 Vars.push_back(DE); 5233 } 5234 5235 if (Vars.empty()) 5236 return nullptr; 5237 5238 return OMPReductionClause::Create( 5239 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars, 5240 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId); 5241 } 5242 5243 OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step, 5244 SourceLocation StartLoc, 5245 SourceLocation LParenLoc, 5246 SourceLocation ColonLoc, 5247 SourceLocation EndLoc) { 5248 SmallVector<Expr *, 8> Vars; 5249 SmallVector<Expr *, 8> Inits; 5250 for (auto &RefExpr : VarList) { 5251 assert(RefExpr && "NULL expr in OpenMP linear clause."); 5252 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 5253 // It will be analyzed later. 5254 Vars.push_back(RefExpr); 5255 Inits.push_back(nullptr); 5256 continue; 5257 } 5258 5259 // OpenMP [2.14.3.7, linear clause] 5260 // A list item that appears in a linear clause is subject to the private 5261 // clause semantics described in Section 2.14.3.3 on page 159 except as 5262 // noted. In addition, the value of the new list item on each iteration 5263 // of the associated loop(s) corresponds to the value of the original 5264 // list item before entering the construct plus the logical number of 5265 // the iteration times linear-step. 5266 5267 SourceLocation ELoc = RefExpr->getExprLoc(); 5268 // OpenMP [2.1, C/C++] 5269 // A list item is a variable name. 5270 // OpenMP [2.14.3.3, Restrictions, p.1] 5271 // A variable that is part of another variable (as an array or 5272 // structure element) cannot appear in a private clause. 5273 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 5274 if (!DE || !isa<VarDecl>(DE->getDecl())) { 5275 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 5276 continue; 5277 } 5278 5279 VarDecl *VD = cast<VarDecl>(DE->getDecl()); 5280 5281 // OpenMP [2.14.3.7, linear clause] 5282 // A list-item cannot appear in more than one linear clause. 5283 // A list-item that appears in a linear clause cannot appear in any 5284 // other data-sharing attribute clause. 5285 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); 5286 if (DVar.RefExpr) { 5287 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 5288 << getOpenMPClauseName(OMPC_linear); 5289 ReportOriginalDSA(*this, DSAStack, VD, DVar); 5290 continue; 5291 } 5292 5293 QualType QType = VD->getType(); 5294 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 5295 // It will be analyzed later. 5296 Vars.push_back(DE); 5297 Inits.push_back(nullptr); 5298 continue; 5299 } 5300 5301 // A variable must not have an incomplete type or a reference type. 5302 if (RequireCompleteType(ELoc, QType, 5303 diag::err_omp_linear_incomplete_type)) { 5304 continue; 5305 } 5306 if (QType->isReferenceType()) { 5307 Diag(ELoc, diag::err_omp_clause_ref_type_arg) 5308 << getOpenMPClauseName(OMPC_linear) << QType; 5309 bool IsDecl = 5310 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 5311 Diag(VD->getLocation(), 5312 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 5313 << VD; 5314 continue; 5315 } 5316 5317 // A list item must not be const-qualified. 5318 if (QType.isConstant(Context)) { 5319 Diag(ELoc, diag::err_omp_const_variable) 5320 << getOpenMPClauseName(OMPC_linear); 5321 bool IsDecl = 5322 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 5323 Diag(VD->getLocation(), 5324 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 5325 << VD; 5326 continue; 5327 } 5328 5329 // A list item must be of integral or pointer type. 5330 QType = QType.getUnqualifiedType().getCanonicalType(); 5331 const Type *Ty = QType.getTypePtrOrNull(); 5332 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && 5333 !Ty->isPointerType())) { 5334 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType; 5335 bool IsDecl = 5336 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 5337 Diag(VD->getLocation(), 5338 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 5339 << VD; 5340 continue; 5341 } 5342 5343 // Build var to save initial value. 5344 VarDecl *Init = BuildVarDecl(*this, ELoc, DE->getType(), ".linear.start"); 5345 AddInitializerToDecl(Init, DefaultLvalueConversion(DE).get(), 5346 /*DirectInit*/ false, /*TypeMayContainAuto*/ false); 5347 CurContext->addDecl(Init); 5348 Init->setIsUsed(); 5349 auto InitRef = DeclRefExpr::Create( 5350 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(), 5351 /*TemplateKWLoc*/ SourceLocation(), Init, 5352 /*isEnclosingLocal*/ false, DE->getLocStart(), DE->getType(), 5353 /*VK*/ VK_LValue); 5354 DSAStack->addDSA(VD, DE, OMPC_linear); 5355 Vars.push_back(DE); 5356 Inits.push_back(InitRef); 5357 } 5358 5359 if (Vars.empty()) 5360 return nullptr; 5361 5362 Expr *StepExpr = Step; 5363 Expr *CalcStepExpr = nullptr; 5364 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 5365 !Step->isInstantiationDependent() && 5366 !Step->containsUnexpandedParameterPack()) { 5367 SourceLocation StepLoc = Step->getLocStart(); 5368 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 5369 if (Val.isInvalid()) 5370 return nullptr; 5371 StepExpr = Val.get(); 5372 5373 // Build var to save the step value. 5374 VarDecl *SaveVar = 5375 BuildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 5376 CurContext->addDecl(SaveVar); 5377 SaveVar->setIsUsed(); 5378 ExprResult SaveRef = 5379 BuildDeclRefExpr(SaveVar, StepExpr->getType(), VK_LValue, StepLoc); 5380 ExprResult CalcStep = 5381 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 5382 5383 // Warn about zero linear step (it would be probably better specified as 5384 // making corresponding variables 'const'). 5385 llvm::APSInt Result; 5386 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); 5387 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) 5388 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] 5389 << (Vars.size() > 1); 5390 if (!IsConstant && CalcStep.isUsable()) { 5391 // Calculate the step beforehand instead of doing this on each iteration. 5392 // (This is not used if the number of iterations may be kfold-ed). 5393 CalcStepExpr = CalcStep.get(); 5394 } 5395 } 5396 5397 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc, 5398 Vars, Inits, StepExpr, CalcStepExpr); 5399 } 5400 5401 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 5402 Expr *NumIterations, Sema &SemaRef, 5403 Scope *S) { 5404 // Walk the vars and build update/final expressions for the CodeGen. 5405 SmallVector<Expr *, 8> Updates; 5406 SmallVector<Expr *, 8> Finals; 5407 Expr *Step = Clause.getStep(); 5408 Expr *CalcStep = Clause.getCalcStep(); 5409 // OpenMP [2.14.3.7, linear clause] 5410 // If linear-step is not specified it is assumed to be 1. 5411 if (Step == nullptr) 5412 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 5413 else if (CalcStep) 5414 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 5415 bool HasErrors = false; 5416 auto CurInit = Clause.inits().begin(); 5417 for (auto &RefExpr : Clause.varlists()) { 5418 Expr *InitExpr = *CurInit; 5419 5420 // Build privatized reference to the current linear var. 5421 auto DE = cast<DeclRefExpr>(RefExpr); 5422 auto PrivateRef = DeclRefExpr::Create( 5423 SemaRef.Context, /*QualifierLoc*/ DE->getQualifierLoc(), 5424 /*TemplateKWLoc*/ SourceLocation(), DE->getDecl(), 5425 /* RefersToEnclosingVariableOrCapture */ true, DE->getLocStart(), 5426 DE->getType(), /*VK*/ VK_LValue); 5427 5428 // Build update: Var = InitExpr + IV * Step 5429 ExprResult Update = 5430 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef, 5431 InitExpr, IV, Step, /* Subtract */ false); 5432 Update = SemaRef.ActOnFinishFullExpr(Update.get()); 5433 5434 // Build final: Var = InitExpr + NumIterations * Step 5435 ExprResult Final = 5436 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), RefExpr, InitExpr, 5437 NumIterations, Step, /* Subtract */ false); 5438 Final = SemaRef.ActOnFinishFullExpr(Final.get()); 5439 if (!Update.isUsable() || !Final.isUsable()) { 5440 Updates.push_back(nullptr); 5441 Finals.push_back(nullptr); 5442 HasErrors = true; 5443 } else { 5444 Updates.push_back(Update.get()); 5445 Finals.push_back(Final.get()); 5446 } 5447 ++CurInit; 5448 } 5449 Clause.setUpdates(Updates); 5450 Clause.setFinals(Finals); 5451 return HasErrors; 5452 } 5453 5454 OMPClause *Sema::ActOnOpenMPAlignedClause( 5455 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 5456 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 5457 5458 SmallVector<Expr *, 8> Vars; 5459 for (auto &RefExpr : VarList) { 5460 assert(RefExpr && "NULL expr in OpenMP aligned clause."); 5461 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 5462 // It will be analyzed later. 5463 Vars.push_back(RefExpr); 5464 continue; 5465 } 5466 5467 SourceLocation ELoc = RefExpr->getExprLoc(); 5468 // OpenMP [2.1, C/C++] 5469 // A list item is a variable name. 5470 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 5471 if (!DE || !isa<VarDecl>(DE->getDecl())) { 5472 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 5473 continue; 5474 } 5475 5476 VarDecl *VD = cast<VarDecl>(DE->getDecl()); 5477 5478 // OpenMP [2.8.1, simd construct, Restrictions] 5479 // The type of list items appearing in the aligned clause must be 5480 // array, pointer, reference to array, or reference to pointer. 5481 QualType QType = DE->getType() 5482 .getNonReferenceType() 5483 .getUnqualifiedType() 5484 .getCanonicalType(); 5485 const Type *Ty = QType.getTypePtrOrNull(); 5486 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() && 5487 !Ty->isPointerType())) { 5488 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 5489 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange(); 5490 bool IsDecl = 5491 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 5492 Diag(VD->getLocation(), 5493 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 5494 << VD; 5495 continue; 5496 } 5497 5498 // OpenMP [2.8.1, simd construct, Restrictions] 5499 // A list-item cannot appear in more than one aligned clause. 5500 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) { 5501 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange(); 5502 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 5503 << getOpenMPClauseName(OMPC_aligned); 5504 continue; 5505 } 5506 5507 Vars.push_back(DE); 5508 } 5509 5510 // OpenMP [2.8.1, simd construct, Description] 5511 // The parameter of the aligned clause, alignment, must be a constant 5512 // positive integer expression. 5513 // If no optional parameter is specified, implementation-defined default 5514 // alignments for SIMD instructions on the target platforms are assumed. 5515 if (Alignment != nullptr) { 5516 ExprResult AlignResult = 5517 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 5518 if (AlignResult.isInvalid()) 5519 return nullptr; 5520 Alignment = AlignResult.get(); 5521 } 5522 if (Vars.empty()) 5523 return nullptr; 5524 5525 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 5526 EndLoc, Vars, Alignment); 5527 } 5528 5529 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 5530 SourceLocation StartLoc, 5531 SourceLocation LParenLoc, 5532 SourceLocation EndLoc) { 5533 SmallVector<Expr *, 8> Vars; 5534 for (auto &RefExpr : VarList) { 5535 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 5536 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 5537 // It will be analyzed later. 5538 Vars.push_back(RefExpr); 5539 continue; 5540 } 5541 5542 SourceLocation ELoc = RefExpr->getExprLoc(); 5543 // OpenMP [2.1, C/C++] 5544 // A list item is a variable name. 5545 // OpenMP [2.14.4.1, Restrictions, p.1] 5546 // A list item that appears in a copyin clause must be threadprivate. 5547 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 5548 if (!DE || !isa<VarDecl>(DE->getDecl())) { 5549 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 5550 continue; 5551 } 5552 5553 Decl *D = DE->getDecl(); 5554 VarDecl *VD = cast<VarDecl>(D); 5555 5556 QualType Type = VD->getType(); 5557 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 5558 // It will be analyzed later. 5559 Vars.push_back(DE); 5560 continue; 5561 } 5562 5563 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 5564 // A list item that appears in a copyin clause must be threadprivate. 5565 if (!DSAStack->isThreadPrivate(VD)) { 5566 Diag(ELoc, diag::err_omp_required_access) 5567 << getOpenMPClauseName(OMPC_copyin) 5568 << getOpenMPDirectiveName(OMPD_threadprivate); 5569 continue; 5570 } 5571 5572 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 5573 // A variable of class type (or array thereof) that appears in a 5574 // copyin clause requires an accessible, unambiguous copy assignment 5575 // operator for the class type. 5576 Type = Context.getBaseElementType(Type); 5577 CXXRecordDecl *RD = 5578 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; 5579 // FIXME This code must be replaced by actual assignment of the 5580 // threadprivate variable. 5581 if (RD) { 5582 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0); 5583 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess()); 5584 if (MD) { 5585 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible || 5586 MD->isDeleted()) { 5587 Diag(ELoc, diag::err_omp_required_method) 5588 << getOpenMPClauseName(OMPC_copyin) << 2; 5589 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 5590 VarDecl::DeclarationOnly; 5591 Diag(VD->getLocation(), 5592 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 5593 << VD; 5594 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 5595 continue; 5596 } 5597 MarkFunctionReferenced(ELoc, MD); 5598 DiagnoseUseOfDecl(MD, ELoc); 5599 } 5600 } 5601 5602 DSAStack->addDSA(VD, DE, OMPC_copyin); 5603 Vars.push_back(DE); 5604 } 5605 5606 if (Vars.empty()) 5607 return nullptr; 5608 5609 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 5610 } 5611 5612 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 5613 SourceLocation StartLoc, 5614 SourceLocation LParenLoc, 5615 SourceLocation EndLoc) { 5616 SmallVector<Expr *, 8> Vars; 5617 SmallVector<Expr *, 8> SrcExprs; 5618 SmallVector<Expr *, 8> DstExprs; 5619 SmallVector<Expr *, 8> AssignmentOps; 5620 for (auto &RefExpr : VarList) { 5621 assert(RefExpr && "NULL expr in OpenMP copyprivate clause."); 5622 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 5623 // It will be analyzed later. 5624 Vars.push_back(RefExpr); 5625 SrcExprs.push_back(nullptr); 5626 DstExprs.push_back(nullptr); 5627 AssignmentOps.push_back(nullptr); 5628 continue; 5629 } 5630 5631 SourceLocation ELoc = RefExpr->getExprLoc(); 5632 // OpenMP [2.1, C/C++] 5633 // A list item is a variable name. 5634 // OpenMP [2.14.4.1, Restrictions, p.1] 5635 // A list item that appears in a copyin clause must be threadprivate. 5636 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 5637 if (!DE || !isa<VarDecl>(DE->getDecl())) { 5638 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 5639 continue; 5640 } 5641 5642 Decl *D = DE->getDecl(); 5643 VarDecl *VD = cast<VarDecl>(D); 5644 5645 QualType Type = VD->getType(); 5646 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 5647 // It will be analyzed later. 5648 Vars.push_back(DE); 5649 SrcExprs.push_back(nullptr); 5650 DstExprs.push_back(nullptr); 5651 AssignmentOps.push_back(nullptr); 5652 continue; 5653 } 5654 5655 // OpenMP [2.14.4.2, Restrictions, p.2] 5656 // A list item that appears in a copyprivate clause may not appear in a 5657 // private or firstprivate clause on the single construct. 5658 if (!DSAStack->isThreadPrivate(VD)) { 5659 auto DVar = DSAStack->getTopDSA(VD, false); 5660 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 5661 DVar.RefExpr) { 5662 Diag(ELoc, diag::err_omp_wrong_dsa) 5663 << getOpenMPClauseName(DVar.CKind) 5664 << getOpenMPClauseName(OMPC_copyprivate); 5665 ReportOriginalDSA(*this, DSAStack, VD, DVar); 5666 continue; 5667 } 5668 5669 // OpenMP [2.11.4.2, Restrictions, p.1] 5670 // All list items that appear in a copyprivate clause must be either 5671 // threadprivate or private in the enclosing context. 5672 if (DVar.CKind == OMPC_unknown) { 5673 DVar = DSAStack->getImplicitDSA(VD, false); 5674 if (DVar.CKind == OMPC_shared) { 5675 Diag(ELoc, diag::err_omp_required_access) 5676 << getOpenMPClauseName(OMPC_copyprivate) 5677 << "threadprivate or private in the enclosing context"; 5678 ReportOriginalDSA(*this, DSAStack, VD, DVar); 5679 continue; 5680 } 5681 } 5682 } 5683 5684 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 5685 // A variable of class type (or array thereof) that appears in a 5686 // copyin clause requires an accessible, unambiguous copy assignment 5687 // operator for the class type. 5688 auto *SrcVD = BuildVarDecl(*this, DE->getLocStart(), VD->getType(), 5689 ".copyprivate.src"); 5690 auto *PseudoSrcExpr = BuildDeclRefExpr(SrcVD, DE->getType(), VK_LValue, 5691 DE->getExprLoc()).get(); 5692 auto *DstVD = BuildVarDecl(*this, DE->getLocStart(), VD->getType(), 5693 ".copyprivate.dst"); 5694 auto *PseudoDstExpr = BuildDeclRefExpr(DstVD, DE->getType(), VK_LValue, 5695 DE->getExprLoc()).get(); 5696 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, 5697 PseudoDstExpr, PseudoSrcExpr); 5698 if (AssignmentOp.isInvalid()) 5699 continue; 5700 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 5701 /*DiscardedValue=*/true); 5702 if (AssignmentOp.isInvalid()) 5703 continue; 5704 5705 // No need to mark vars as copyprivate, they are already threadprivate or 5706 // implicitly private. 5707 Vars.push_back(DE); 5708 SrcExprs.push_back(PseudoSrcExpr); 5709 DstExprs.push_back(PseudoDstExpr); 5710 AssignmentOps.push_back(AssignmentOp.get()); 5711 } 5712 5713 if (Vars.empty()) 5714 return nullptr; 5715 5716 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 5717 Vars, SrcExprs, DstExprs, AssignmentOps); 5718 } 5719 5720 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 5721 SourceLocation StartLoc, 5722 SourceLocation LParenLoc, 5723 SourceLocation EndLoc) { 5724 if (VarList.empty()) 5725 return nullptr; 5726 5727 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 5728 } 5729 5730