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 "TreeTransform.h" 16 #include "clang/AST/ASTContext.h" 17 #include "clang/AST/ASTMutationListener.h" 18 #include "clang/AST/Decl.h" 19 #include "clang/AST/DeclCXX.h" 20 #include "clang/AST/DeclOpenMP.h" 21 #include "clang/AST/StmtCXX.h" 22 #include "clang/AST/StmtOpenMP.h" 23 #include "clang/AST/StmtVisitor.h" 24 #include "clang/Basic/OpenMPKinds.h" 25 #include "clang/Basic/TargetInfo.h" 26 #include "clang/Lex/Preprocessor.h" 27 #include "clang/Sema/Initialization.h" 28 #include "clang/Sema/Lookup.h" 29 #include "clang/Sema/Scope.h" 30 #include "clang/Sema/ScopeInfo.h" 31 #include "clang/Sema/SemaInternal.h" 32 using namespace clang; 33 34 //===----------------------------------------------------------------------===// 35 // Stack of data-sharing attributes for variables 36 //===----------------------------------------------------------------------===// 37 38 namespace { 39 /// \brief Default data sharing attributes, which can be applied to directive. 40 enum DefaultDataSharingAttributes { 41 DSA_unspecified = 0, /// \brief Data sharing attribute not specified. 42 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'. 43 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'. 44 }; 45 46 template <class T> struct MatchesAny { 47 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {} 48 bool operator()(T Kind) { 49 for (auto KindEl : Arr) 50 if (KindEl == Kind) 51 return true; 52 return false; 53 } 54 55 private: 56 ArrayRef<T> Arr; 57 }; 58 struct MatchesAlways { 59 MatchesAlways() {} 60 template <class T> bool operator()(T) { return true; } 61 }; 62 63 typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause; 64 typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective; 65 66 /// \brief Stack for tracking declarations used in OpenMP directives and 67 /// clauses and their data-sharing attributes. 68 class DSAStackTy { 69 public: 70 struct DSAVarData { 71 OpenMPDirectiveKind DKind; 72 OpenMPClauseKind CKind; 73 DeclRefExpr *RefExpr; 74 SourceLocation ImplicitDSALoc; 75 DSAVarData() 76 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr), 77 ImplicitDSALoc() {} 78 }; 79 80 public: 81 struct MapInfo { 82 Expr *RefExpr; 83 }; 84 85 private: 86 struct DSAInfo { 87 OpenMPClauseKind Attributes; 88 DeclRefExpr *RefExpr; 89 }; 90 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy; 91 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy; 92 typedef llvm::DenseSet<VarDecl *> LoopControlVariablesSetTy; 93 typedef llvm::SmallDenseMap<VarDecl *, MapInfo, 64> MappedDeclsTy; 94 95 struct SharingMapTy { 96 DeclSAMapTy SharingMap; 97 AlignedMapTy AlignedMap; 98 MappedDeclsTy MappedDecls; 99 LoopControlVariablesSetTy LCVSet; 100 DefaultDataSharingAttributes DefaultAttr; 101 SourceLocation DefaultAttrLoc; 102 OpenMPDirectiveKind Directive; 103 DeclarationNameInfo DirectiveName; 104 Scope *CurScope; 105 SourceLocation ConstructLoc; 106 /// \brief first argument (Expr *) contains optional argument of the 107 /// 'ordered' clause, the second one is true if the regions has 'ordered' 108 /// clause, false otherwise. 109 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion; 110 bool NowaitRegion; 111 bool CancelRegion; 112 unsigned CollapseNumber; 113 SourceLocation InnerTeamsRegionLoc; 114 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 115 Scope *CurScope, SourceLocation Loc) 116 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified), 117 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope), 118 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false), 119 CancelRegion(false), CollapseNumber(1), InnerTeamsRegionLoc() {} 120 SharingMapTy() 121 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified), 122 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr), 123 ConstructLoc(), OrderedRegion(), NowaitRegion(false), 124 CancelRegion(false), CollapseNumber(1), InnerTeamsRegionLoc() {} 125 }; 126 127 typedef SmallVector<SharingMapTy, 64> StackTy; 128 129 /// \brief Stack of used declaration and their data-sharing attributes. 130 StackTy Stack; 131 /// \brief true, if check for DSA must be from parent directive, false, if 132 /// from current directive. 133 OpenMPClauseKind ClauseKindMode; 134 Sema &SemaRef; 135 bool ForceCapturing; 136 137 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator; 138 139 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D); 140 141 /// \brief Checks if the variable is a local for OpenMP region. 142 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter); 143 144 public: 145 explicit DSAStackTy(Sema &S) 146 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S), 147 ForceCapturing(false) {} 148 149 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 150 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 151 152 bool isForceVarCapturing() const { return ForceCapturing; } 153 void setForceVarCapturing(bool V) { ForceCapturing = V; } 154 155 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 156 Scope *CurScope, SourceLocation Loc) { 157 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc)); 158 Stack.back().DefaultAttrLoc = Loc; 159 } 160 161 void pop() { 162 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!"); 163 Stack.pop_back(); 164 } 165 166 /// \brief If 'aligned' declaration for given variable \a D was not seen yet, 167 /// add it and return NULL; otherwise return previous occurrence's expression 168 /// for diagnostics. 169 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE); 170 171 /// \brief Register specified variable as loop control variable. 172 void addLoopControlVariable(VarDecl *D); 173 /// \brief Check if the specified variable is a loop control variable for 174 /// current region. 175 bool isLoopControlVariable(VarDecl *D); 176 177 /// \brief Adds explicit data sharing attribute to the specified declaration. 178 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A); 179 180 /// \brief Returns data sharing attributes from top of the stack for the 181 /// specified declaration. 182 DSAVarData getTopDSA(VarDecl *D, bool FromParent); 183 /// \brief Returns data-sharing attributes for the specified declaration. 184 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent); 185 /// \brief Checks if the specified variables has data-sharing attributes which 186 /// match specified \a CPred predicate in any directive which matches \a DPred 187 /// predicate. 188 template <class ClausesPredicate, class DirectivesPredicate> 189 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred, 190 DirectivesPredicate DPred, bool FromParent); 191 /// \brief Checks if the specified variables has data-sharing attributes which 192 /// match specified \a CPred predicate in any innermost directive which 193 /// matches \a DPred predicate. 194 template <class ClausesPredicate, class DirectivesPredicate> 195 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred, 196 DirectivesPredicate DPred, 197 bool FromParent); 198 /// \brief Checks if the specified variables has explicit data-sharing 199 /// attributes which match specified \a CPred predicate at the specified 200 /// OpenMP region. 201 bool hasExplicitDSA(VarDecl *D, 202 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 203 unsigned Level); 204 205 /// \brief Returns true if the directive at level \Level matches in the 206 /// specified \a DPred predicate. 207 bool hasExplicitDirective( 208 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 209 unsigned Level); 210 211 /// \brief Finds a directive which matches specified \a DPred predicate. 212 template <class NamedDirectivesPredicate> 213 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent); 214 215 /// \brief Returns currently analyzed directive. 216 OpenMPDirectiveKind getCurrentDirective() const { 217 return Stack.back().Directive; 218 } 219 /// \brief Returns parent directive. 220 OpenMPDirectiveKind getParentDirective() const { 221 if (Stack.size() > 2) 222 return Stack[Stack.size() - 2].Directive; 223 return OMPD_unknown; 224 } 225 /// \brief Return the directive associated with the provided scope. 226 OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const; 227 228 /// \brief Set default data sharing attribute to none. 229 void setDefaultDSANone(SourceLocation Loc) { 230 Stack.back().DefaultAttr = DSA_none; 231 Stack.back().DefaultAttrLoc = Loc; 232 } 233 /// \brief Set default data sharing attribute to shared. 234 void setDefaultDSAShared(SourceLocation Loc) { 235 Stack.back().DefaultAttr = DSA_shared; 236 Stack.back().DefaultAttrLoc = Loc; 237 } 238 239 DefaultDataSharingAttributes getDefaultDSA() const { 240 return Stack.back().DefaultAttr; 241 } 242 SourceLocation getDefaultDSALocation() const { 243 return Stack.back().DefaultAttrLoc; 244 } 245 246 /// \brief Checks if the specified variable is a threadprivate. 247 bool isThreadPrivate(VarDecl *D) { 248 DSAVarData DVar = getTopDSA(D, false); 249 return isOpenMPThreadPrivate(DVar.CKind); 250 } 251 252 /// \brief Marks current region as ordered (it has an 'ordered' clause). 253 void setOrderedRegion(bool IsOrdered, Expr *Param) { 254 Stack.back().OrderedRegion.setInt(IsOrdered); 255 Stack.back().OrderedRegion.setPointer(Param); 256 } 257 /// \brief Returns true, if parent region is ordered (has associated 258 /// 'ordered' clause), false - otherwise. 259 bool isParentOrderedRegion() const { 260 if (Stack.size() > 2) 261 return Stack[Stack.size() - 2].OrderedRegion.getInt(); 262 return false; 263 } 264 /// \brief Returns optional parameter for the ordered region. 265 Expr *getParentOrderedRegionParam() const { 266 if (Stack.size() > 2) 267 return Stack[Stack.size() - 2].OrderedRegion.getPointer(); 268 return nullptr; 269 } 270 /// \brief Marks current region as nowait (it has a 'nowait' clause). 271 void setNowaitRegion(bool IsNowait = true) { 272 Stack.back().NowaitRegion = IsNowait; 273 } 274 /// \brief Returns true, if parent region is nowait (has associated 275 /// 'nowait' clause), false - otherwise. 276 bool isParentNowaitRegion() const { 277 if (Stack.size() > 2) 278 return Stack[Stack.size() - 2].NowaitRegion; 279 return false; 280 } 281 /// \brief Marks parent region as cancel region. 282 void setParentCancelRegion(bool Cancel = true) { 283 if (Stack.size() > 2) 284 Stack[Stack.size() - 2].CancelRegion = 285 Stack[Stack.size() - 2].CancelRegion || Cancel; 286 } 287 /// \brief Return true if current region has inner cancel construct. 288 bool isCancelRegion() const { 289 return Stack.back().CancelRegion; 290 } 291 292 /// \brief Set collapse value for the region. 293 void setCollapseNumber(unsigned Val) { Stack.back().CollapseNumber = Val; } 294 /// \brief Return collapse value for region. 295 unsigned getCollapseNumber() const { 296 return Stack.back().CollapseNumber; 297 } 298 299 /// \brief Marks current target region as one with closely nested teams 300 /// region. 301 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 302 if (Stack.size() > 2) 303 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc; 304 } 305 /// \brief Returns true, if current region has closely nested teams region. 306 bool hasInnerTeamsRegion() const { 307 return getInnerTeamsRegionLoc().isValid(); 308 } 309 /// \brief Returns location of the nested teams region (if any). 310 SourceLocation getInnerTeamsRegionLoc() const { 311 if (Stack.size() > 1) 312 return Stack.back().InnerTeamsRegionLoc; 313 return SourceLocation(); 314 } 315 316 Scope *getCurScope() const { return Stack.back().CurScope; } 317 Scope *getCurScope() { return Stack.back().CurScope; } 318 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; } 319 320 MapInfo getMapInfoForVar(VarDecl *VD) { 321 MapInfo VarMI = {0}; 322 for (auto Cnt = Stack.size() - 1; Cnt > 0; --Cnt) { 323 if (Stack[Cnt].MappedDecls.count(VD)) { 324 VarMI = Stack[Cnt].MappedDecls[VD]; 325 break; 326 } 327 } 328 return VarMI; 329 } 330 331 void addMapInfoForVar(VarDecl *VD, MapInfo MI) { 332 if (Stack.size() > 1) { 333 Stack.back().MappedDecls[VD] = MI; 334 } 335 } 336 337 MapInfo IsMappedInCurrentRegion(VarDecl *VD) { 338 assert(Stack.size() > 1 && "Target level is 0"); 339 MapInfo VarMI = {0}; 340 if (Stack.size() > 1 && Stack.back().MappedDecls.count(VD)) { 341 VarMI = Stack.back().MappedDecls[VD]; 342 } 343 return VarMI; 344 } 345 }; 346 bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) { 347 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task || 348 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown || 349 isOpenMPTaskLoopDirective(DKind); 350 } 351 } // namespace 352 353 DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter, 354 VarDecl *D) { 355 D = D->getCanonicalDecl(); 356 DSAVarData DVar; 357 if (Iter == std::prev(Stack.rend())) { 358 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 359 // in a region but not in construct] 360 // File-scope or namespace-scope variables referenced in called routines 361 // in the region are shared unless they appear in a threadprivate 362 // directive. 363 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D)) 364 DVar.CKind = OMPC_shared; 365 366 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 367 // in a region but not in construct] 368 // Variables with static storage duration that are declared in called 369 // routines in the region are shared. 370 if (D->hasGlobalStorage()) 371 DVar.CKind = OMPC_shared; 372 373 return DVar; 374 } 375 376 DVar.DKind = Iter->Directive; 377 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 378 // in a Construct, C/C++, predetermined, p.1] 379 // Variables with automatic storage duration that are declared in a scope 380 // inside the construct are private. 381 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() && 382 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) { 383 DVar.CKind = OMPC_private; 384 return DVar; 385 } 386 387 // Explicitly specified attributes and local variables with predetermined 388 // attributes. 389 if (Iter->SharingMap.count(D)) { 390 DVar.RefExpr = Iter->SharingMap[D].RefExpr; 391 DVar.CKind = Iter->SharingMap[D].Attributes; 392 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 393 return DVar; 394 } 395 396 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 397 // in a Construct, C/C++, implicitly determined, p.1] 398 // In a parallel or task construct, the data-sharing attributes of these 399 // variables are determined by the default clause, if present. 400 switch (Iter->DefaultAttr) { 401 case DSA_shared: 402 DVar.CKind = OMPC_shared; 403 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 404 return DVar; 405 case DSA_none: 406 return DVar; 407 case DSA_unspecified: 408 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 409 // in a Construct, implicitly determined, p.2] 410 // In a parallel construct, if no default clause is present, these 411 // variables are shared. 412 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 413 if (isOpenMPParallelDirective(DVar.DKind) || 414 isOpenMPTeamsDirective(DVar.DKind)) { 415 DVar.CKind = OMPC_shared; 416 return DVar; 417 } 418 419 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 420 // in a Construct, implicitly determined, p.4] 421 // In a task construct, if no default clause is present, a variable that in 422 // the enclosing context is determined to be shared by all implicit tasks 423 // bound to the current team is shared. 424 if (DVar.DKind == OMPD_task) { 425 DSAVarData DVarTemp; 426 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend(); 427 I != EE; ++I) { 428 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 429 // Referenced 430 // in a Construct, implicitly determined, p.6] 431 // In a task construct, if no default clause is present, a variable 432 // whose data-sharing attribute is not determined by the rules above is 433 // firstprivate. 434 DVarTemp = getDSA(I, D); 435 if (DVarTemp.CKind != OMPC_shared) { 436 DVar.RefExpr = nullptr; 437 DVar.DKind = OMPD_task; 438 DVar.CKind = OMPC_firstprivate; 439 return DVar; 440 } 441 if (isParallelOrTaskRegion(I->Directive)) 442 break; 443 } 444 DVar.DKind = OMPD_task; 445 DVar.CKind = 446 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 447 return DVar; 448 } 449 } 450 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 451 // in a Construct, implicitly determined, p.3] 452 // For constructs other than task, if no default clause is present, these 453 // variables inherit their data-sharing attributes from the enclosing 454 // context. 455 return getDSA(std::next(Iter), D); 456 } 457 458 DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) { 459 assert(Stack.size() > 1 && "Data sharing attributes stack is empty"); 460 D = D->getCanonicalDecl(); 461 auto It = Stack.back().AlignedMap.find(D); 462 if (It == Stack.back().AlignedMap.end()) { 463 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 464 Stack.back().AlignedMap[D] = NewDE; 465 return nullptr; 466 } else { 467 assert(It->second && "Unexpected nullptr expr in the aligned map"); 468 return It->second; 469 } 470 return nullptr; 471 } 472 473 void DSAStackTy::addLoopControlVariable(VarDecl *D) { 474 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); 475 D = D->getCanonicalDecl(); 476 Stack.back().LCVSet.insert(D); 477 } 478 479 bool DSAStackTy::isLoopControlVariable(VarDecl *D) { 480 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); 481 D = D->getCanonicalDecl(); 482 return Stack.back().LCVSet.count(D) > 0; 483 } 484 485 void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) { 486 D = D->getCanonicalDecl(); 487 if (A == OMPC_threadprivate) { 488 Stack[0].SharingMap[D].Attributes = A; 489 Stack[0].SharingMap[D].RefExpr = E; 490 } else { 491 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); 492 Stack.back().SharingMap[D].Attributes = A; 493 Stack.back().SharingMap[D].RefExpr = E; 494 } 495 } 496 497 bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) { 498 D = D->getCanonicalDecl(); 499 if (Stack.size() > 2) { 500 reverse_iterator I = Iter, E = std::prev(Stack.rend()); 501 Scope *TopScope = nullptr; 502 while (I != E && !isParallelOrTaskRegion(I->Directive)) { 503 ++I; 504 } 505 if (I == E) 506 return false; 507 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; 508 Scope *CurScope = getCurScope(); 509 while (CurScope != TopScope && !CurScope->isDeclScope(D)) { 510 CurScope = CurScope->getParent(); 511 } 512 return CurScope != TopScope; 513 } 514 return false; 515 } 516 517 /// \brief Build a variable declaration for OpenMP loop iteration variable. 518 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 519 StringRef Name, const AttrVec *Attrs = nullptr) { 520 DeclContext *DC = SemaRef.CurContext; 521 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 522 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 523 VarDecl *Decl = 524 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 525 if (Attrs) { 526 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 527 I != E; ++I) 528 Decl->addAttr(*I); 529 } 530 Decl->setImplicit(); 531 return Decl; 532 } 533 534 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 535 SourceLocation Loc, 536 bool RefersToCapture = false) { 537 D->setReferenced(); 538 D->markUsed(S.Context); 539 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 540 SourceLocation(), D, RefersToCapture, Loc, Ty, 541 VK_LValue); 542 } 543 544 DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) { 545 D = D->getCanonicalDecl(); 546 DSAVarData DVar; 547 548 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 549 // in a Construct, C/C++, predetermined, p.1] 550 // Variables appearing in threadprivate directives are threadprivate. 551 if ((D->getTLSKind() != VarDecl::TLS_None && 552 !(D->hasAttr<OMPThreadPrivateDeclAttr>() && 553 SemaRef.getLangOpts().OpenMPUseTLS && 554 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 555 (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() && 556 !D->isLocalVarDecl())) { 557 addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(), 558 D->getLocation()), 559 OMPC_threadprivate); 560 } 561 if (Stack[0].SharingMap.count(D)) { 562 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr; 563 DVar.CKind = OMPC_threadprivate; 564 return DVar; 565 } 566 567 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 568 // in a Construct, C/C++, predetermined, p.1] 569 // Variables with automatic storage duration that are declared in a scope 570 // inside the construct are private. 571 OpenMPDirectiveKind Kind = 572 FromParent ? getParentDirective() : getCurrentDirective(); 573 auto StartI = std::next(Stack.rbegin()); 574 auto EndI = std::prev(Stack.rend()); 575 if (FromParent && StartI != EndI) { 576 StartI = std::next(StartI); 577 } 578 if (!isParallelOrTaskRegion(Kind)) { 579 if (isOpenMPLocal(D, StartI) && 580 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto || 581 D->getStorageClass() == SC_None)) || 582 isa<ParmVarDecl>(D))) { 583 DVar.CKind = OMPC_private; 584 return DVar; 585 } 586 587 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 588 // in a Construct, C/C++, predetermined, p.4] 589 // Static data members are shared. 590 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 591 // in a Construct, C/C++, predetermined, p.7] 592 // Variables with static storage duration that are declared in a scope 593 // inside the construct are shared. 594 if (D->isStaticDataMember()) { 595 DSAVarData DVarTemp = 596 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent); 597 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 598 return DVar; 599 600 DVar.CKind = OMPC_shared; 601 return DVar; 602 } 603 } 604 605 QualType Type = D->getType().getNonReferenceType().getCanonicalType(); 606 bool IsConstant = Type.isConstant(SemaRef.getASTContext()); 607 Type = SemaRef.getASTContext().getBaseElementType(Type); 608 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 609 // in a Construct, C/C++, predetermined, p.6] 610 // Variables with const qualified type having no mutable member are 611 // shared. 612 CXXRecordDecl *RD = 613 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; 614 if (IsConstant && 615 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) { 616 // Variables with const-qualified type having no mutable member may be 617 // listed in a firstprivate clause, even if they are static data members. 618 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate), 619 MatchesAlways(), FromParent); 620 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr) 621 return DVar; 622 623 DVar.CKind = OMPC_shared; 624 return DVar; 625 } 626 627 // Explicitly specified attributes and local variables with predetermined 628 // attributes. 629 auto I = std::prev(StartI); 630 if (I->SharingMap.count(D)) { 631 DVar.RefExpr = I->SharingMap[D].RefExpr; 632 DVar.CKind = I->SharingMap[D].Attributes; 633 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 634 } 635 636 return DVar; 637 } 638 639 DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) { 640 D = D->getCanonicalDecl(); 641 auto StartI = Stack.rbegin(); 642 auto EndI = std::prev(Stack.rend()); 643 if (FromParent && StartI != EndI) { 644 StartI = std::next(StartI); 645 } 646 return getDSA(StartI, D); 647 } 648 649 template <class ClausesPredicate, class DirectivesPredicate> 650 DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred, 651 DirectivesPredicate DPred, 652 bool FromParent) { 653 D = D->getCanonicalDecl(); 654 auto StartI = std::next(Stack.rbegin()); 655 auto EndI = std::prev(Stack.rend()); 656 if (FromParent && StartI != EndI) { 657 StartI = std::next(StartI); 658 } 659 for (auto I = StartI, EE = EndI; I != EE; ++I) { 660 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive)) 661 continue; 662 DSAVarData DVar = getDSA(I, D); 663 if (CPred(DVar.CKind)) 664 return DVar; 665 } 666 return DSAVarData(); 667 } 668 669 template <class ClausesPredicate, class DirectivesPredicate> 670 DSAStackTy::DSAVarData 671 DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred, 672 DirectivesPredicate DPred, bool FromParent) { 673 D = D->getCanonicalDecl(); 674 auto StartI = std::next(Stack.rbegin()); 675 auto EndI = std::prev(Stack.rend()); 676 if (FromParent && StartI != EndI) { 677 StartI = std::next(StartI); 678 } 679 for (auto I = StartI, EE = EndI; I != EE; ++I) { 680 if (!DPred(I->Directive)) 681 break; 682 DSAVarData DVar = getDSA(I, D); 683 if (CPred(DVar.CKind)) 684 return DVar; 685 return DSAVarData(); 686 } 687 return DSAVarData(); 688 } 689 690 bool DSAStackTy::hasExplicitDSA( 691 VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 692 unsigned Level) { 693 if (CPred(ClauseKindMode)) 694 return true; 695 if (isClauseParsingMode()) 696 ++Level; 697 D = D->getCanonicalDecl(); 698 auto StartI = Stack.rbegin(); 699 auto EndI = std::prev(Stack.rend()); 700 if (std::distance(StartI, EndI) <= (int)Level) 701 return false; 702 std::advance(StartI, Level); 703 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr && 704 CPred(StartI->SharingMap[D].Attributes); 705 } 706 707 bool DSAStackTy::hasExplicitDirective( 708 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 709 unsigned Level) { 710 if (isClauseParsingMode()) 711 ++Level; 712 auto StartI = Stack.rbegin(); 713 auto EndI = std::prev(Stack.rend()); 714 if (std::distance(StartI, EndI) <= (int)Level) 715 return false; 716 std::advance(StartI, Level); 717 return DPred(StartI->Directive); 718 } 719 720 template <class NamedDirectivesPredicate> 721 bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) { 722 auto StartI = std::next(Stack.rbegin()); 723 auto EndI = std::prev(Stack.rend()); 724 if (FromParent && StartI != EndI) { 725 StartI = std::next(StartI); 726 } 727 for (auto I = StartI, EE = EndI; I != EE; ++I) { 728 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 729 return true; 730 } 731 return false; 732 } 733 734 OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const { 735 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I) 736 if (I->CurScope == S) 737 return I->Directive; 738 return OMPD_unknown; 739 } 740 741 void Sema::InitDataSharingAttributesStack() { 742 VarDataSharingAttributesStack = new DSAStackTy(*this); 743 } 744 745 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 746 747 bool Sema::IsOpenMPCapturedByRef(VarDecl *VD, 748 const CapturedRegionScopeInfo *RSI) { 749 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 750 751 auto &Ctx = getASTContext(); 752 bool IsByRef = true; 753 754 // Find the directive that is associated with the provided scope. 755 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope); 756 auto Ty = VD->getType(); 757 758 if (isOpenMPTargetDirective(DKind)) { 759 // This table summarizes how a given variable should be passed to the device 760 // given its type and the clauses where it appears. This table is based on 761 // the description in OpenMP 4.5 [2.10.4, target Construct] and 762 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 763 // 764 // ========================================================================= 765 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 766 // | |(tofrom:scalar)| | pvt | | | | 767 // ========================================================================= 768 // | scl | | | | - | | bycopy| 769 // | scl | | - | x | - | - | bycopy| 770 // | scl | | x | - | - | - | null | 771 // | scl | x | | | - | | byref | 772 // | scl | x | - | x | - | - | bycopy| 773 // | scl | x | x | - | - | - | null | 774 // | scl | | - | - | - | x | byref | 775 // | scl | x | - | - | - | x | byref | 776 // 777 // | agg | n.a. | | | - | | byref | 778 // | agg | n.a. | - | x | - | - | byref | 779 // | agg | n.a. | x | - | - | - | null | 780 // | agg | n.a. | - | - | - | x | byref | 781 // | agg | n.a. | - | - | - | x[] | byref | 782 // 783 // | ptr | n.a. | | | - | | bycopy| 784 // | ptr | n.a. | - | x | - | - | bycopy| 785 // | ptr | n.a. | x | - | - | - | null | 786 // | ptr | n.a. | - | - | - | x | byref | 787 // | ptr | n.a. | - | - | - | x[] | bycopy| 788 // | ptr | n.a. | - | - | x | | bycopy| 789 // | ptr | n.a. | - | - | x | x | bycopy| 790 // | ptr | n.a. | - | - | x | x[] | bycopy| 791 // ========================================================================= 792 // Legend: 793 // scl - scalar 794 // ptr - pointer 795 // agg - aggregate 796 // x - applies 797 // - - invalid in this combination 798 // [] - mapped with an array section 799 // byref - should be mapped by reference 800 // byval - should be mapped by value 801 // null - initialize a local variable to null on the device 802 // 803 // Observations: 804 // - All scalar declarations that show up in a map clause have to be passed 805 // by reference, because they may have been mapped in the enclosing data 806 // environment. 807 // - If the scalar value does not fit the size of uintptr, it has to be 808 // passed by reference, regardless the result in the table above. 809 // - For pointers mapped by value that have either an implicit map or an 810 // array section, the runtime library may pass the NULL value to the 811 // device instead of the value passed to it by the compiler. 812 813 // FIXME: Right now, only implicit maps are implemented. Properly mapping 814 // values requires having the map, private, and firstprivate clauses SEMA 815 // and parsing in place, which we don't yet. 816 817 if (Ty->isReferenceType()) 818 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 819 IsByRef = !Ty->isScalarType(); 820 } 821 822 // When passing data by value, we need to make sure it fits the uintptr size 823 // and alignment, because the runtime library only deals with uintptr types. 824 // If it does not fit the uintptr size, we need to pass the data by reference 825 // instead. 826 if (!IsByRef && 827 (Ctx.getTypeSizeInChars(Ty) > 828 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 829 Ctx.getDeclAlign(VD) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) 830 IsByRef = true; 831 832 return IsByRef; 833 } 834 835 bool Sema::IsOpenMPCapturedVar(VarDecl *VD) { 836 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 837 VD = VD->getCanonicalDecl(); 838 839 // If we are attempting to capture a global variable in a directive with 840 // 'target' we return true so that this global is also mapped to the device. 841 // 842 // FIXME: If the declaration is enclosed in a 'declare target' directive, 843 // then it should not be captured. Therefore, an extra check has to be 844 // inserted here once support for 'declare target' is added. 845 // 846 if (!VD->hasLocalStorage()) { 847 if (DSAStack->getCurrentDirective() == OMPD_target && 848 !DSAStack->isClauseParsingMode()) { 849 return true; 850 } 851 if (DSAStack->getCurScope() && 852 DSAStack->hasDirective( 853 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI, 854 SourceLocation Loc) -> bool { 855 return isOpenMPTargetDirective(K); 856 }, 857 false)) { 858 return true; 859 } 860 } 861 862 if (DSAStack->getCurrentDirective() != OMPD_unknown && 863 (!DSAStack->isClauseParsingMode() || 864 DSAStack->getParentDirective() != OMPD_unknown)) { 865 if (DSAStack->isLoopControlVariable(VD) || 866 (VD->hasLocalStorage() && 867 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) || 868 DSAStack->isForceVarCapturing()) 869 return true; 870 auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode()); 871 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind)) 872 return true; 873 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), 874 DSAStack->isClauseParsingMode()); 875 return DVarPrivate.CKind != OMPC_unknown; 876 } 877 return false; 878 } 879 880 bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) { 881 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 882 return DSAStack->hasExplicitDSA( 883 VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level); 884 } 885 886 bool Sema::isOpenMPTargetCapturedVar(VarDecl *VD, unsigned Level) { 887 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 888 // Return true if the current level is no longer enclosed in a target region. 889 890 return !VD->hasLocalStorage() && 891 DSAStack->hasExplicitDirective(isOpenMPTargetDirective, Level); 892 } 893 894 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 895 896 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 897 const DeclarationNameInfo &DirName, 898 Scope *CurScope, SourceLocation Loc) { 899 DSAStack->push(DKind, DirName, CurScope, Loc); 900 PushExpressionEvaluationContext(PotentiallyEvaluated); 901 } 902 903 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 904 DSAStack->setClauseParsingMode(K); 905 } 906 907 void Sema::EndOpenMPClause() { 908 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 909 } 910 911 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 912 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 913 // A variable of class type (or array thereof) that appears in a lastprivate 914 // clause requires an accessible, unambiguous default constructor for the 915 // class type, unless the list item is also specified in a firstprivate 916 // clause. 917 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 918 for (auto *C : D->clauses()) { 919 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 920 SmallVector<Expr *, 8> PrivateCopies; 921 for (auto *DE : Clause->varlists()) { 922 if (DE->isValueDependent() || DE->isTypeDependent()) { 923 PrivateCopies.push_back(nullptr); 924 continue; 925 } 926 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl()); 927 QualType Type = VD->getType().getNonReferenceType(); 928 auto DVar = DSAStack->getTopDSA(VD, false); 929 if (DVar.CKind == OMPC_lastprivate) { 930 // Generate helper private variable and initialize it with the 931 // default value. The address of the original variable is replaced 932 // by the address of the new private variable in CodeGen. This new 933 // variable is not added to IdResolver, so the code in the OpenMP 934 // region uses original variable for proper diagnostics. 935 auto *VDPrivate = buildVarDecl( 936 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 937 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr); 938 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false); 939 if (VDPrivate->isInvalidDecl()) 940 continue; 941 PrivateCopies.push_back(buildDeclRefExpr( 942 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 943 } else { 944 // The variable is also a firstprivate, so initialization sequence 945 // for private copy is generated already. 946 PrivateCopies.push_back(nullptr); 947 } 948 } 949 // Set initializers to private copies if no errors were found. 950 if (PrivateCopies.size() == Clause->varlist_size()) { 951 Clause->setPrivateCopies(PrivateCopies); 952 } 953 } 954 } 955 } 956 957 DSAStack->pop(); 958 DiscardCleanupsInEvaluationContext(); 959 PopExpressionEvaluationContext(); 960 } 961 962 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 963 Expr *NumIterations, Sema &SemaRef, 964 Scope *S); 965 966 namespace { 967 968 class VarDeclFilterCCC : public CorrectionCandidateCallback { 969 private: 970 Sema &SemaRef; 971 972 public: 973 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 974 bool ValidateCandidate(const TypoCorrection &Candidate) override { 975 NamedDecl *ND = Candidate.getCorrectionDecl(); 976 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) { 977 return VD->hasGlobalStorage() && 978 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 979 SemaRef.getCurScope()); 980 } 981 return false; 982 } 983 }; 984 } // namespace 985 986 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 987 CXXScopeSpec &ScopeSpec, 988 const DeclarationNameInfo &Id) { 989 LookupResult Lookup(*this, Id, LookupOrdinaryName); 990 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 991 992 if (Lookup.isAmbiguous()) 993 return ExprError(); 994 995 VarDecl *VD; 996 if (!Lookup.isSingleResult()) { 997 if (TypoCorrection Corrected = CorrectTypo( 998 Id, LookupOrdinaryName, CurScope, nullptr, 999 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) { 1000 diagnoseTypo(Corrected, 1001 PDiag(Lookup.empty() 1002 ? diag::err_undeclared_var_use_suggest 1003 : diag::err_omp_expected_var_arg_suggest) 1004 << Id.getName()); 1005 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 1006 } else { 1007 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 1008 : diag::err_omp_expected_var_arg) 1009 << Id.getName(); 1010 return ExprError(); 1011 } 1012 } else { 1013 if (!(VD = Lookup.getAsSingle<VarDecl>())) { 1014 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 1015 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 1016 return ExprError(); 1017 } 1018 } 1019 Lookup.suppressDiagnostics(); 1020 1021 // OpenMP [2.9.2, Syntax, C/C++] 1022 // Variables must be file-scope, namespace-scope, or static block-scope. 1023 if (!VD->hasGlobalStorage()) { 1024 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 1025 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal(); 1026 bool IsDecl = 1027 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1028 Diag(VD->getLocation(), 1029 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1030 << VD; 1031 return ExprError(); 1032 } 1033 1034 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 1035 NamedDecl *ND = cast<NamedDecl>(CanonicalVD); 1036 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 1037 // A threadprivate directive for file-scope variables must appear outside 1038 // any definition or declaration. 1039 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 1040 !getCurLexicalContext()->isTranslationUnit()) { 1041 Diag(Id.getLoc(), diag::err_omp_var_scope) 1042 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1043 bool IsDecl = 1044 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1045 Diag(VD->getLocation(), 1046 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1047 << VD; 1048 return ExprError(); 1049 } 1050 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 1051 // A threadprivate directive for static class member variables must appear 1052 // in the class definition, in the same scope in which the member 1053 // variables are declared. 1054 if (CanonicalVD->isStaticDataMember() && 1055 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 1056 Diag(Id.getLoc(), diag::err_omp_var_scope) 1057 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1058 bool IsDecl = 1059 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1060 Diag(VD->getLocation(), 1061 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1062 << VD; 1063 return ExprError(); 1064 } 1065 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 1066 // A threadprivate directive for namespace-scope variables must appear 1067 // outside any definition or declaration other than the namespace 1068 // definition itself. 1069 if (CanonicalVD->getDeclContext()->isNamespace() && 1070 (!getCurLexicalContext()->isFileContext() || 1071 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 1072 Diag(Id.getLoc(), diag::err_omp_var_scope) 1073 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1074 bool IsDecl = 1075 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1076 Diag(VD->getLocation(), 1077 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1078 << VD; 1079 return ExprError(); 1080 } 1081 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 1082 // A threadprivate directive for static block-scope variables must appear 1083 // in the scope of the variable and not in a nested scope. 1084 if (CanonicalVD->isStaticLocal() && CurScope && 1085 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 1086 Diag(Id.getLoc(), diag::err_omp_var_scope) 1087 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1088 bool IsDecl = 1089 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1090 Diag(VD->getLocation(), 1091 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1092 << VD; 1093 return ExprError(); 1094 } 1095 1096 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 1097 // A threadprivate directive must lexically precede all references to any 1098 // of the variables in its list. 1099 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) { 1100 Diag(Id.getLoc(), diag::err_omp_var_used) 1101 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1102 return ExprError(); 1103 } 1104 1105 QualType ExprType = VD->getType().getNonReferenceType(); 1106 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc()); 1107 return DE; 1108 } 1109 1110 Sema::DeclGroupPtrTy 1111 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 1112 ArrayRef<Expr *> VarList) { 1113 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 1114 CurContext->addDecl(D); 1115 return DeclGroupPtrTy::make(DeclGroupRef(D)); 1116 } 1117 return DeclGroupPtrTy(); 1118 } 1119 1120 namespace { 1121 class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> { 1122 Sema &SemaRef; 1123 1124 public: 1125 bool VisitDeclRefExpr(const DeclRefExpr *E) { 1126 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) { 1127 if (VD->hasLocalStorage()) { 1128 SemaRef.Diag(E->getLocStart(), 1129 diag::err_omp_local_var_in_threadprivate_init) 1130 << E->getSourceRange(); 1131 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 1132 << VD << VD->getSourceRange(); 1133 return true; 1134 } 1135 } 1136 return false; 1137 } 1138 bool VisitStmt(const Stmt *S) { 1139 for (auto Child : S->children()) { 1140 if (Child && Visit(Child)) 1141 return true; 1142 } 1143 return false; 1144 } 1145 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 1146 }; 1147 } // namespace 1148 1149 OMPThreadPrivateDecl * 1150 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 1151 SmallVector<Expr *, 8> Vars; 1152 for (auto &RefExpr : VarList) { 1153 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr); 1154 VarDecl *VD = cast<VarDecl>(DE->getDecl()); 1155 SourceLocation ILoc = DE->getExprLoc(); 1156 1157 QualType QType = VD->getType(); 1158 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 1159 // It will be analyzed later. 1160 Vars.push_back(DE); 1161 continue; 1162 } 1163 1164 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 1165 // A threadprivate variable must not have an incomplete type. 1166 if (RequireCompleteType(ILoc, VD->getType(), 1167 diag::err_omp_threadprivate_incomplete_type)) { 1168 continue; 1169 } 1170 1171 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 1172 // A threadprivate variable must not have a reference type. 1173 if (VD->getType()->isReferenceType()) { 1174 Diag(ILoc, diag::err_omp_ref_type_arg) 1175 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 1176 bool IsDecl = 1177 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1178 Diag(VD->getLocation(), 1179 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1180 << VD; 1181 continue; 1182 } 1183 1184 // Check if this is a TLS variable. If TLS is not being supported, produce 1185 // the corresponding diagnostic. 1186 if ((VD->getTLSKind() != VarDecl::TLS_None && 1187 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1188 getLangOpts().OpenMPUseTLS && 1189 getASTContext().getTargetInfo().isTLSSupported())) || 1190 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 1191 !VD->isLocalVarDecl())) { 1192 Diag(ILoc, diag::err_omp_var_thread_local) 1193 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 1194 bool IsDecl = 1195 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1196 Diag(VD->getLocation(), 1197 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1198 << VD; 1199 continue; 1200 } 1201 1202 // Check if initial value of threadprivate variable reference variable with 1203 // local storage (it is not supported by runtime). 1204 if (auto Init = VD->getAnyInitializer()) { 1205 LocalVarRefChecker Checker(*this); 1206 if (Checker.Visit(Init)) 1207 continue; 1208 } 1209 1210 Vars.push_back(RefExpr); 1211 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 1212 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 1213 Context, SourceRange(Loc, Loc))); 1214 if (auto *ML = Context.getASTMutationListener()) 1215 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 1216 } 1217 OMPThreadPrivateDecl *D = nullptr; 1218 if (!Vars.empty()) { 1219 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 1220 Vars); 1221 D->setAccess(AS_public); 1222 } 1223 return D; 1224 } 1225 1226 static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack, 1227 const VarDecl *VD, DSAStackTy::DSAVarData DVar, 1228 bool IsLoopIterVar = false) { 1229 if (DVar.RefExpr) { 1230 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 1231 << getOpenMPClauseName(DVar.CKind); 1232 return; 1233 } 1234 enum { 1235 PDSA_StaticMemberShared, 1236 PDSA_StaticLocalVarShared, 1237 PDSA_LoopIterVarPrivate, 1238 PDSA_LoopIterVarLinear, 1239 PDSA_LoopIterVarLastprivate, 1240 PDSA_ConstVarShared, 1241 PDSA_GlobalVarShared, 1242 PDSA_TaskVarFirstprivate, 1243 PDSA_LocalVarPrivate, 1244 PDSA_Implicit 1245 } Reason = PDSA_Implicit; 1246 bool ReportHint = false; 1247 auto ReportLoc = VD->getLocation(); 1248 if (IsLoopIterVar) { 1249 if (DVar.CKind == OMPC_private) 1250 Reason = PDSA_LoopIterVarPrivate; 1251 else if (DVar.CKind == OMPC_lastprivate) 1252 Reason = PDSA_LoopIterVarLastprivate; 1253 else 1254 Reason = PDSA_LoopIterVarLinear; 1255 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) { 1256 Reason = PDSA_TaskVarFirstprivate; 1257 ReportLoc = DVar.ImplicitDSALoc; 1258 } else if (VD->isStaticLocal()) 1259 Reason = PDSA_StaticLocalVarShared; 1260 else if (VD->isStaticDataMember()) 1261 Reason = PDSA_StaticMemberShared; 1262 else if (VD->isFileVarDecl()) 1263 Reason = PDSA_GlobalVarShared; 1264 else if (VD->getType().isConstant(SemaRef.getASTContext())) 1265 Reason = PDSA_ConstVarShared; 1266 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 1267 ReportHint = true; 1268 Reason = PDSA_LocalVarPrivate; 1269 } 1270 if (Reason != PDSA_Implicit) { 1271 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 1272 << Reason << ReportHint 1273 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 1274 } else if (DVar.ImplicitDSALoc.isValid()) { 1275 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 1276 << getOpenMPClauseName(DVar.CKind); 1277 } 1278 } 1279 1280 namespace { 1281 class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> { 1282 DSAStackTy *Stack; 1283 Sema &SemaRef; 1284 bool ErrorFound; 1285 CapturedStmt *CS; 1286 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate; 1287 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA; 1288 1289 public: 1290 void VisitDeclRefExpr(DeclRefExpr *E) { 1291 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 1292 // Skip internally declared variables. 1293 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD)) 1294 return; 1295 1296 auto DVar = Stack->getTopDSA(VD, false); 1297 // Check if the variable has explicit DSA set and stop analysis if it so. 1298 if (DVar.RefExpr) return; 1299 1300 auto ELoc = E->getExprLoc(); 1301 auto DKind = Stack->getCurrentDirective(); 1302 // The default(none) clause requires that each variable that is referenced 1303 // in the construct, and does not have a predetermined data-sharing 1304 // attribute, must have its data-sharing attribute explicitly determined 1305 // by being listed in a data-sharing attribute clause. 1306 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && 1307 isParallelOrTaskRegion(DKind) && 1308 VarsWithInheritedDSA.count(VD) == 0) { 1309 VarsWithInheritedDSA[VD] = E; 1310 return; 1311 } 1312 1313 // OpenMP [2.9.3.6, Restrictions, p.2] 1314 // A list item that appears in a reduction clause of the innermost 1315 // enclosing worksharing or parallel construct may not be accessed in an 1316 // explicit task. 1317 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction), 1318 [](OpenMPDirectiveKind K) -> bool { 1319 return isOpenMPParallelDirective(K) || 1320 isOpenMPWorksharingDirective(K) || 1321 isOpenMPTeamsDirective(K); 1322 }, 1323 false); 1324 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) { 1325 ErrorFound = true; 1326 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 1327 ReportOriginalDSA(SemaRef, Stack, VD, DVar); 1328 return; 1329 } 1330 1331 // Define implicit data-sharing attributes for task. 1332 DVar = Stack->getImplicitDSA(VD, false); 1333 if (DKind == OMPD_task && DVar.CKind != OMPC_shared) 1334 ImplicitFirstprivate.push_back(E); 1335 } 1336 } 1337 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 1338 for (auto *C : S->clauses()) { 1339 // Skip analysis of arguments of implicitly defined firstprivate clause 1340 // for task directives. 1341 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid())) 1342 for (auto *CC : C->children()) { 1343 if (CC) 1344 Visit(CC); 1345 } 1346 } 1347 } 1348 void VisitStmt(Stmt *S) { 1349 for (auto *C : S->children()) { 1350 if (C && !isa<OMPExecutableDirective>(C)) 1351 Visit(C); 1352 } 1353 } 1354 1355 bool isErrorFound() { return ErrorFound; } 1356 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; } 1357 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() { 1358 return VarsWithInheritedDSA; 1359 } 1360 1361 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 1362 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {} 1363 }; 1364 } // namespace 1365 1366 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 1367 switch (DKind) { 1368 case OMPD_parallel: { 1369 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1370 QualType KmpInt32PtrTy = 1371 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 1372 Sema::CapturedParamNameType Params[] = { 1373 std::make_pair(".global_tid.", KmpInt32PtrTy), 1374 std::make_pair(".bound_tid.", KmpInt32PtrTy), 1375 std::make_pair(StringRef(), QualType()) // __context with shared vars 1376 }; 1377 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1378 Params); 1379 break; 1380 } 1381 case OMPD_simd: { 1382 Sema::CapturedParamNameType Params[] = { 1383 std::make_pair(StringRef(), QualType()) // __context with shared vars 1384 }; 1385 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1386 Params); 1387 break; 1388 } 1389 case OMPD_for: { 1390 Sema::CapturedParamNameType Params[] = { 1391 std::make_pair(StringRef(), QualType()) // __context with shared vars 1392 }; 1393 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1394 Params); 1395 break; 1396 } 1397 case OMPD_for_simd: { 1398 Sema::CapturedParamNameType Params[] = { 1399 std::make_pair(StringRef(), QualType()) // __context with shared vars 1400 }; 1401 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1402 Params); 1403 break; 1404 } 1405 case OMPD_sections: { 1406 Sema::CapturedParamNameType Params[] = { 1407 std::make_pair(StringRef(), QualType()) // __context with shared vars 1408 }; 1409 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1410 Params); 1411 break; 1412 } 1413 case OMPD_section: { 1414 Sema::CapturedParamNameType Params[] = { 1415 std::make_pair(StringRef(), QualType()) // __context with shared vars 1416 }; 1417 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1418 Params); 1419 break; 1420 } 1421 case OMPD_single: { 1422 Sema::CapturedParamNameType Params[] = { 1423 std::make_pair(StringRef(), QualType()) // __context with shared vars 1424 }; 1425 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1426 Params); 1427 break; 1428 } 1429 case OMPD_master: { 1430 Sema::CapturedParamNameType Params[] = { 1431 std::make_pair(StringRef(), QualType()) // __context with shared vars 1432 }; 1433 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1434 Params); 1435 break; 1436 } 1437 case OMPD_critical: { 1438 Sema::CapturedParamNameType Params[] = { 1439 std::make_pair(StringRef(), QualType()) // __context with shared vars 1440 }; 1441 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1442 Params); 1443 break; 1444 } 1445 case OMPD_parallel_for: { 1446 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1447 QualType KmpInt32PtrTy = 1448 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 1449 Sema::CapturedParamNameType Params[] = { 1450 std::make_pair(".global_tid.", KmpInt32PtrTy), 1451 std::make_pair(".bound_tid.", KmpInt32PtrTy), 1452 std::make_pair(StringRef(), QualType()) // __context with shared vars 1453 }; 1454 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1455 Params); 1456 break; 1457 } 1458 case OMPD_parallel_for_simd: { 1459 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1460 QualType KmpInt32PtrTy = 1461 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 1462 Sema::CapturedParamNameType Params[] = { 1463 std::make_pair(".global_tid.", KmpInt32PtrTy), 1464 std::make_pair(".bound_tid.", KmpInt32PtrTy), 1465 std::make_pair(StringRef(), QualType()) // __context with shared vars 1466 }; 1467 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1468 Params); 1469 break; 1470 } 1471 case OMPD_parallel_sections: { 1472 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1473 QualType KmpInt32PtrTy = 1474 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 1475 Sema::CapturedParamNameType Params[] = { 1476 std::make_pair(".global_tid.", KmpInt32PtrTy), 1477 std::make_pair(".bound_tid.", KmpInt32PtrTy), 1478 std::make_pair(StringRef(), QualType()) // __context with shared vars 1479 }; 1480 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1481 Params); 1482 break; 1483 } 1484 case OMPD_task: { 1485 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1486 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()}; 1487 FunctionProtoType::ExtProtoInfo EPI; 1488 EPI.Variadic = true; 1489 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 1490 Sema::CapturedParamNameType Params[] = { 1491 std::make_pair(".global_tid.", KmpInt32Ty), 1492 std::make_pair(".part_id.", KmpInt32Ty), 1493 std::make_pair(".privates.", 1494 Context.VoidPtrTy.withConst().withRestrict()), 1495 std::make_pair( 1496 ".copy_fn.", 1497 Context.getPointerType(CopyFnType).withConst().withRestrict()), 1498 std::make_pair(StringRef(), QualType()) // __context with shared vars 1499 }; 1500 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1501 Params); 1502 // Mark this captured region as inlined, because we don't use outlined 1503 // function directly. 1504 getCurCapturedRegion()->TheCapturedDecl->addAttr( 1505 AlwaysInlineAttr::CreateImplicit( 1506 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange())); 1507 break; 1508 } 1509 case OMPD_ordered: { 1510 Sema::CapturedParamNameType Params[] = { 1511 std::make_pair(StringRef(), QualType()) // __context with shared vars 1512 }; 1513 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1514 Params); 1515 break; 1516 } 1517 case OMPD_atomic: { 1518 Sema::CapturedParamNameType Params[] = { 1519 std::make_pair(StringRef(), QualType()) // __context with shared vars 1520 }; 1521 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1522 Params); 1523 break; 1524 } 1525 case OMPD_target_data: 1526 case OMPD_target: { 1527 Sema::CapturedParamNameType Params[] = { 1528 std::make_pair(StringRef(), QualType()) // __context with shared vars 1529 }; 1530 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1531 Params); 1532 break; 1533 } 1534 case OMPD_teams: { 1535 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1536 QualType KmpInt32PtrTy = 1537 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 1538 Sema::CapturedParamNameType Params[] = { 1539 std::make_pair(".global_tid.", KmpInt32PtrTy), 1540 std::make_pair(".bound_tid.", KmpInt32PtrTy), 1541 std::make_pair(StringRef(), QualType()) // __context with shared vars 1542 }; 1543 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1544 Params); 1545 break; 1546 } 1547 case OMPD_taskgroup: { 1548 Sema::CapturedParamNameType Params[] = { 1549 std::make_pair(StringRef(), QualType()) // __context with shared vars 1550 }; 1551 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1552 Params); 1553 break; 1554 } 1555 case OMPD_taskloop: { 1556 Sema::CapturedParamNameType Params[] = { 1557 std::make_pair(StringRef(), QualType()) // __context with shared vars 1558 }; 1559 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1560 Params); 1561 break; 1562 } 1563 case OMPD_taskloop_simd: { 1564 Sema::CapturedParamNameType Params[] = { 1565 std::make_pair(StringRef(), QualType()) // __context with shared vars 1566 }; 1567 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1568 Params); 1569 break; 1570 } 1571 case OMPD_threadprivate: 1572 case OMPD_taskyield: 1573 case OMPD_barrier: 1574 case OMPD_taskwait: 1575 case OMPD_cancellation_point: 1576 case OMPD_cancel: 1577 case OMPD_flush: 1578 llvm_unreachable("OpenMP Directive is not allowed"); 1579 case OMPD_unknown: 1580 llvm_unreachable("Unknown OpenMP directive"); 1581 } 1582 } 1583 1584 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 1585 ArrayRef<OMPClause *> Clauses) { 1586 if (!S.isUsable()) { 1587 ActOnCapturedRegionError(); 1588 return StmtError(); 1589 } 1590 // This is required for proper codegen. 1591 for (auto *Clause : Clauses) { 1592 if (isOpenMPPrivate(Clause->getClauseKind()) || 1593 Clause->getClauseKind() == OMPC_copyprivate || 1594 (getLangOpts().OpenMPUseTLS && 1595 getASTContext().getTargetInfo().isTLSSupported() && 1596 Clause->getClauseKind() == OMPC_copyin)) { 1597 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 1598 // Mark all variables in private list clauses as used in inner region. 1599 for (auto *VarRef : Clause->children()) { 1600 if (auto *E = cast_or_null<Expr>(VarRef)) { 1601 MarkDeclarationsReferencedInExpr(E); 1602 } 1603 } 1604 DSAStack->setForceVarCapturing(/*V=*/false); 1605 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) && 1606 Clause->getClauseKind() == OMPC_schedule) { 1607 // Mark all variables in private list clauses as used in inner region. 1608 // Required for proper codegen of combined directives. 1609 // TODO: add processing for other clauses. 1610 if (auto *E = cast_or_null<Expr>( 1611 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) { 1612 MarkDeclarationsReferencedInExpr(E); 1613 } 1614 } 1615 } 1616 return ActOnCapturedRegionEnd(S.get()); 1617 } 1618 1619 static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, 1620 OpenMPDirectiveKind CurrentRegion, 1621 const DeclarationNameInfo &CurrentName, 1622 OpenMPDirectiveKind CancelRegion, 1623 SourceLocation StartLoc) { 1624 // Allowed nesting of constructs 1625 // +------------------+-----------------+------------------------------------+ 1626 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)| 1627 // +------------------+-----------------+------------------------------------+ 1628 // | parallel | parallel | * | 1629 // | parallel | for | * | 1630 // | parallel | for simd | * | 1631 // | parallel | master | * | 1632 // | parallel | critical | * | 1633 // | parallel | simd | * | 1634 // | parallel | sections | * | 1635 // | parallel | section | + | 1636 // | parallel | single | * | 1637 // | parallel | parallel for | * | 1638 // | parallel |parallel for simd| * | 1639 // | parallel |parallel sections| * | 1640 // | parallel | task | * | 1641 // | parallel | taskyield | * | 1642 // | parallel | barrier | * | 1643 // | parallel | taskwait | * | 1644 // | parallel | taskgroup | * | 1645 // | parallel | flush | * | 1646 // | parallel | ordered | + | 1647 // | parallel | atomic | * | 1648 // | parallel | target | * | 1649 // | parallel | teams | + | 1650 // | parallel | cancellation | | 1651 // | | point | ! | 1652 // | parallel | cancel | ! | 1653 // | parallel | taskloop | * | 1654 // | parallel | taskloop simd | * | 1655 // +------------------+-----------------+------------------------------------+ 1656 // | for | parallel | * | 1657 // | for | for | + | 1658 // | for | for simd | + | 1659 // | for | master | + | 1660 // | for | critical | * | 1661 // | for | simd | * | 1662 // | for | sections | + | 1663 // | for | section | + | 1664 // | for | single | + | 1665 // | for | parallel for | * | 1666 // | for |parallel for simd| * | 1667 // | for |parallel sections| * | 1668 // | for | task | * | 1669 // | for | taskyield | * | 1670 // | for | barrier | + | 1671 // | for | taskwait | * | 1672 // | for | taskgroup | * | 1673 // | for | flush | * | 1674 // | for | ordered | * (if construct is ordered) | 1675 // | for | atomic | * | 1676 // | for | target | * | 1677 // | for | teams | + | 1678 // | for | cancellation | | 1679 // | | point | ! | 1680 // | for | cancel | ! | 1681 // | for | taskloop | * | 1682 // | for | taskloop simd | * | 1683 // +------------------+-----------------+------------------------------------+ 1684 // | master | parallel | * | 1685 // | master | for | + | 1686 // | master | for simd | + | 1687 // | master | master | * | 1688 // | master | critical | * | 1689 // | master | simd | * | 1690 // | master | sections | + | 1691 // | master | section | + | 1692 // | master | single | + | 1693 // | master | parallel for | * | 1694 // | master |parallel for simd| * | 1695 // | master |parallel sections| * | 1696 // | master | task | * | 1697 // | master | taskyield | * | 1698 // | master | barrier | + | 1699 // | master | taskwait | * | 1700 // | master | taskgroup | * | 1701 // | master | flush | * | 1702 // | master | ordered | + | 1703 // | master | atomic | * | 1704 // | master | target | * | 1705 // | master | teams | + | 1706 // | master | cancellation | | 1707 // | | point | | 1708 // | master | cancel | | 1709 // | master | taskloop | * | 1710 // | master | taskloop simd | * | 1711 // +------------------+-----------------+------------------------------------+ 1712 // | critical | parallel | * | 1713 // | critical | for | + | 1714 // | critical | for simd | + | 1715 // | critical | master | * | 1716 // | critical | critical | * (should have different names) | 1717 // | critical | simd | * | 1718 // | critical | sections | + | 1719 // | critical | section | + | 1720 // | critical | single | + | 1721 // | critical | parallel for | * | 1722 // | critical |parallel for simd| * | 1723 // | critical |parallel sections| * | 1724 // | critical | task | * | 1725 // | critical | taskyield | * | 1726 // | critical | barrier | + | 1727 // | critical | taskwait | * | 1728 // | critical | taskgroup | * | 1729 // | critical | ordered | + | 1730 // | critical | atomic | * | 1731 // | critical | target | * | 1732 // | critical | teams | + | 1733 // | critical | cancellation | | 1734 // | | point | | 1735 // | critical | cancel | | 1736 // | critical | taskloop | * | 1737 // | critical | taskloop simd | * | 1738 // +------------------+-----------------+------------------------------------+ 1739 // | simd | parallel | | 1740 // | simd | for | | 1741 // | simd | for simd | | 1742 // | simd | master | | 1743 // | simd | critical | | 1744 // | simd | simd | | 1745 // | simd | sections | | 1746 // | simd | section | | 1747 // | simd | single | | 1748 // | simd | parallel for | | 1749 // | simd |parallel for simd| | 1750 // | simd |parallel sections| | 1751 // | simd | task | | 1752 // | simd | taskyield | | 1753 // | simd | barrier | | 1754 // | simd | taskwait | | 1755 // | simd | taskgroup | | 1756 // | simd | flush | | 1757 // | simd | ordered | + (with simd clause) | 1758 // | simd | atomic | | 1759 // | simd | target | | 1760 // | simd | teams | | 1761 // | simd | cancellation | | 1762 // | | point | | 1763 // | simd | cancel | | 1764 // | simd | taskloop | | 1765 // | simd | taskloop simd | | 1766 // +------------------+-----------------+------------------------------------+ 1767 // | for simd | parallel | | 1768 // | for simd | for | | 1769 // | for simd | for simd | | 1770 // | for simd | master | | 1771 // | for simd | critical | | 1772 // | for simd | simd | | 1773 // | for simd | sections | | 1774 // | for simd | section | | 1775 // | for simd | single | | 1776 // | for simd | parallel for | | 1777 // | for simd |parallel for simd| | 1778 // | for simd |parallel sections| | 1779 // | for simd | task | | 1780 // | for simd | taskyield | | 1781 // | for simd | barrier | | 1782 // | for simd | taskwait | | 1783 // | for simd | taskgroup | | 1784 // | for simd | flush | | 1785 // | for simd | ordered | + (with simd clause) | 1786 // | for simd | atomic | | 1787 // | for simd | target | | 1788 // | for simd | teams | | 1789 // | for simd | cancellation | | 1790 // | | point | | 1791 // | for simd | cancel | | 1792 // | for simd | taskloop | | 1793 // | for simd | taskloop simd | | 1794 // +------------------+-----------------+------------------------------------+ 1795 // | parallel for simd| parallel | | 1796 // | parallel for simd| for | | 1797 // | parallel for simd| for simd | | 1798 // | parallel for simd| master | | 1799 // | parallel for simd| critical | | 1800 // | parallel for simd| simd | | 1801 // | parallel for simd| sections | | 1802 // | parallel for simd| section | | 1803 // | parallel for simd| single | | 1804 // | parallel for simd| parallel for | | 1805 // | parallel for simd|parallel for simd| | 1806 // | parallel for simd|parallel sections| | 1807 // | parallel for simd| task | | 1808 // | parallel for simd| taskyield | | 1809 // | parallel for simd| barrier | | 1810 // | parallel for simd| taskwait | | 1811 // | parallel for simd| taskgroup | | 1812 // | parallel for simd| flush | | 1813 // | parallel for simd| ordered | + (with simd clause) | 1814 // | parallel for simd| atomic | | 1815 // | parallel for simd| target | | 1816 // | parallel for simd| teams | | 1817 // | parallel for simd| cancellation | | 1818 // | | point | | 1819 // | parallel for simd| cancel | | 1820 // | parallel for simd| taskloop | | 1821 // | parallel for simd| taskloop simd | | 1822 // +------------------+-----------------+------------------------------------+ 1823 // | sections | parallel | * | 1824 // | sections | for | + | 1825 // | sections | for simd | + | 1826 // | sections | master | + | 1827 // | sections | critical | * | 1828 // | sections | simd | * | 1829 // | sections | sections | + | 1830 // | sections | section | * | 1831 // | sections | single | + | 1832 // | sections | parallel for | * | 1833 // | sections |parallel for simd| * | 1834 // | sections |parallel sections| * | 1835 // | sections | task | * | 1836 // | sections | taskyield | * | 1837 // | sections | barrier | + | 1838 // | sections | taskwait | * | 1839 // | sections | taskgroup | * | 1840 // | sections | flush | * | 1841 // | sections | ordered | + | 1842 // | sections | atomic | * | 1843 // | sections | target | * | 1844 // | sections | teams | + | 1845 // | sections | cancellation | | 1846 // | | point | ! | 1847 // | sections | cancel | ! | 1848 // | sections | taskloop | * | 1849 // | sections | taskloop simd | * | 1850 // +------------------+-----------------+------------------------------------+ 1851 // | section | parallel | * | 1852 // | section | for | + | 1853 // | section | for simd | + | 1854 // | section | master | + | 1855 // | section | critical | * | 1856 // | section | simd | * | 1857 // | section | sections | + | 1858 // | section | section | + | 1859 // | section | single | + | 1860 // | section | parallel for | * | 1861 // | section |parallel for simd| * | 1862 // | section |parallel sections| * | 1863 // | section | task | * | 1864 // | section | taskyield | * | 1865 // | section | barrier | + | 1866 // | section | taskwait | * | 1867 // | section | taskgroup | * | 1868 // | section | flush | * | 1869 // | section | ordered | + | 1870 // | section | atomic | * | 1871 // | section | target | * | 1872 // | section | teams | + | 1873 // | section | cancellation | | 1874 // | | point | ! | 1875 // | section | cancel | ! | 1876 // | section | taskloop | * | 1877 // | section | taskloop simd | * | 1878 // +------------------+-----------------+------------------------------------+ 1879 // | single | parallel | * | 1880 // | single | for | + | 1881 // | single | for simd | + | 1882 // | single | master | + | 1883 // | single | critical | * | 1884 // | single | simd | * | 1885 // | single | sections | + | 1886 // | single | section | + | 1887 // | single | single | + | 1888 // | single | parallel for | * | 1889 // | single |parallel for simd| * | 1890 // | single |parallel sections| * | 1891 // | single | task | * | 1892 // | single | taskyield | * | 1893 // | single | barrier | + | 1894 // | single | taskwait | * | 1895 // | single | taskgroup | * | 1896 // | single | flush | * | 1897 // | single | ordered | + | 1898 // | single | atomic | * | 1899 // | single | target | * | 1900 // | single | teams | + | 1901 // | single | cancellation | | 1902 // | | point | | 1903 // | single | cancel | | 1904 // | single | taskloop | * | 1905 // | single | taskloop simd | * | 1906 // +------------------+-----------------+------------------------------------+ 1907 // | parallel for | parallel | * | 1908 // | parallel for | for | + | 1909 // | parallel for | for simd | + | 1910 // | parallel for | master | + | 1911 // | parallel for | critical | * | 1912 // | parallel for | simd | * | 1913 // | parallel for | sections | + | 1914 // | parallel for | section | + | 1915 // | parallel for | single | + | 1916 // | parallel for | parallel for | * | 1917 // | parallel for |parallel for simd| * | 1918 // | parallel for |parallel sections| * | 1919 // | parallel for | task | * | 1920 // | parallel for | taskyield | * | 1921 // | parallel for | barrier | + | 1922 // | parallel for | taskwait | * | 1923 // | parallel for | taskgroup | * | 1924 // | parallel for | flush | * | 1925 // | parallel for | ordered | * (if construct is ordered) | 1926 // | parallel for | atomic | * | 1927 // | parallel for | target | * | 1928 // | parallel for | teams | + | 1929 // | parallel for | cancellation | | 1930 // | | point | ! | 1931 // | parallel for | cancel | ! | 1932 // | parallel for | taskloop | * | 1933 // | parallel for | taskloop simd | * | 1934 // +------------------+-----------------+------------------------------------+ 1935 // | parallel sections| parallel | * | 1936 // | parallel sections| for | + | 1937 // | parallel sections| for simd | + | 1938 // | parallel sections| master | + | 1939 // | parallel sections| critical | + | 1940 // | parallel sections| simd | * | 1941 // | parallel sections| sections | + | 1942 // | parallel sections| section | * | 1943 // | parallel sections| single | + | 1944 // | parallel sections| parallel for | * | 1945 // | parallel sections|parallel for simd| * | 1946 // | parallel sections|parallel sections| * | 1947 // | parallel sections| task | * | 1948 // | parallel sections| taskyield | * | 1949 // | parallel sections| barrier | + | 1950 // | parallel sections| taskwait | * | 1951 // | parallel sections| taskgroup | * | 1952 // | parallel sections| flush | * | 1953 // | parallel sections| ordered | + | 1954 // | parallel sections| atomic | * | 1955 // | parallel sections| target | * | 1956 // | parallel sections| teams | + | 1957 // | parallel sections| cancellation | | 1958 // | | point | ! | 1959 // | parallel sections| cancel | ! | 1960 // | parallel sections| taskloop | * | 1961 // | parallel sections| taskloop simd | * | 1962 // +------------------+-----------------+------------------------------------+ 1963 // | task | parallel | * | 1964 // | task | for | + | 1965 // | task | for simd | + | 1966 // | task | master | + | 1967 // | task | critical | * | 1968 // | task | simd | * | 1969 // | task | sections | + | 1970 // | task | section | + | 1971 // | task | single | + | 1972 // | task | parallel for | * | 1973 // | task |parallel for simd| * | 1974 // | task |parallel sections| * | 1975 // | task | task | * | 1976 // | task | taskyield | * | 1977 // | task | barrier | + | 1978 // | task | taskwait | * | 1979 // | task | taskgroup | * | 1980 // | task | flush | * | 1981 // | task | ordered | + | 1982 // | task | atomic | * | 1983 // | task | target | * | 1984 // | task | teams | + | 1985 // | task | cancellation | | 1986 // | | point | ! | 1987 // | task | cancel | ! | 1988 // | task | taskloop | * | 1989 // | task | taskloop simd | * | 1990 // +------------------+-----------------+------------------------------------+ 1991 // | ordered | parallel | * | 1992 // | ordered | for | + | 1993 // | ordered | for simd | + | 1994 // | ordered | master | * | 1995 // | ordered | critical | * | 1996 // | ordered | simd | * | 1997 // | ordered | sections | + | 1998 // | ordered | section | + | 1999 // | ordered | single | + | 2000 // | ordered | parallel for | * | 2001 // | ordered |parallel for simd| * | 2002 // | ordered |parallel sections| * | 2003 // | ordered | task | * | 2004 // | ordered | taskyield | * | 2005 // | ordered | barrier | + | 2006 // | ordered | taskwait | * | 2007 // | ordered | taskgroup | * | 2008 // | ordered | flush | * | 2009 // | ordered | ordered | + | 2010 // | ordered | atomic | * | 2011 // | ordered | target | * | 2012 // | ordered | teams | + | 2013 // | ordered | cancellation | | 2014 // | | point | | 2015 // | ordered | cancel | | 2016 // | ordered | taskloop | * | 2017 // | ordered | taskloop simd | * | 2018 // +------------------+-----------------+------------------------------------+ 2019 // | atomic | parallel | | 2020 // | atomic | for | | 2021 // | atomic | for simd | | 2022 // | atomic | master | | 2023 // | atomic | critical | | 2024 // | atomic | simd | | 2025 // | atomic | sections | | 2026 // | atomic | section | | 2027 // | atomic | single | | 2028 // | atomic | parallel for | | 2029 // | atomic |parallel for simd| | 2030 // | atomic |parallel sections| | 2031 // | atomic | task | | 2032 // | atomic | taskyield | | 2033 // | atomic | barrier | | 2034 // | atomic | taskwait | | 2035 // | atomic | taskgroup | | 2036 // | atomic | flush | | 2037 // | atomic | ordered | | 2038 // | atomic | atomic | | 2039 // | atomic | target | | 2040 // | atomic | teams | | 2041 // | atomic | cancellation | | 2042 // | | point | | 2043 // | atomic | cancel | | 2044 // | atomic | taskloop | | 2045 // | atomic | taskloop simd | | 2046 // +------------------+-----------------+------------------------------------+ 2047 // | target | parallel | * | 2048 // | target | for | * | 2049 // | target | for simd | * | 2050 // | target | master | * | 2051 // | target | critical | * | 2052 // | target | simd | * | 2053 // | target | sections | * | 2054 // | target | section | * | 2055 // | target | single | * | 2056 // | target | parallel for | * | 2057 // | target |parallel for simd| * | 2058 // | target |parallel sections| * | 2059 // | target | task | * | 2060 // | target | taskyield | * | 2061 // | target | barrier | * | 2062 // | target | taskwait | * | 2063 // | target | taskgroup | * | 2064 // | target | flush | * | 2065 // | target | ordered | * | 2066 // | target | atomic | * | 2067 // | target | target | * | 2068 // | target | teams | * | 2069 // | target | cancellation | | 2070 // | | point | | 2071 // | target | cancel | | 2072 // | target | taskloop | * | 2073 // | target | taskloop simd | * | 2074 // +------------------+-----------------+------------------------------------+ 2075 // | teams | parallel | * | 2076 // | teams | for | + | 2077 // | teams | for simd | + | 2078 // | teams | master | + | 2079 // | teams | critical | + | 2080 // | teams | simd | + | 2081 // | teams | sections | + | 2082 // | teams | section | + | 2083 // | teams | single | + | 2084 // | teams | parallel for | * | 2085 // | teams |parallel for simd| * | 2086 // | teams |parallel sections| * | 2087 // | teams | task | + | 2088 // | teams | taskyield | + | 2089 // | teams | barrier | + | 2090 // | teams | taskwait | + | 2091 // | teams | taskgroup | + | 2092 // | teams | flush | + | 2093 // | teams | ordered | + | 2094 // | teams | atomic | + | 2095 // | teams | target | + | 2096 // | teams | teams | + | 2097 // | teams | cancellation | | 2098 // | | point | | 2099 // | teams | cancel | | 2100 // | teams | taskloop | + | 2101 // | teams | taskloop simd | + | 2102 // +------------------+-----------------+------------------------------------+ 2103 // | taskloop | parallel | * | 2104 // | taskloop | for | + | 2105 // | taskloop | for simd | + | 2106 // | taskloop | master | + | 2107 // | taskloop | critical | * | 2108 // | taskloop | simd | * | 2109 // | taskloop | sections | + | 2110 // | taskloop | section | + | 2111 // | taskloop | single | + | 2112 // | taskloop | parallel for | * | 2113 // | taskloop |parallel for simd| * | 2114 // | taskloop |parallel sections| * | 2115 // | taskloop | task | * | 2116 // | taskloop | taskyield | * | 2117 // | taskloop | barrier | + | 2118 // | taskloop | taskwait | * | 2119 // | taskloop | taskgroup | * | 2120 // | taskloop | flush | * | 2121 // | taskloop | ordered | + | 2122 // | taskloop | atomic | * | 2123 // | taskloop | target | * | 2124 // | taskloop | teams | + | 2125 // | taskloop | cancellation | | 2126 // | | point | | 2127 // | taskloop | cancel | | 2128 // | taskloop | taskloop | * | 2129 // +------------------+-----------------+------------------------------------+ 2130 // | taskloop simd | parallel | | 2131 // | taskloop simd | for | | 2132 // | taskloop simd | for simd | | 2133 // | taskloop simd | master | | 2134 // | taskloop simd | critical | | 2135 // | taskloop simd | simd | | 2136 // | taskloop simd | sections | | 2137 // | taskloop simd | section | | 2138 // | taskloop simd | single | | 2139 // | taskloop simd | parallel for | | 2140 // | taskloop simd |parallel for simd| | 2141 // | taskloop simd |parallel sections| | 2142 // | taskloop simd | task | | 2143 // | taskloop simd | taskyield | | 2144 // | taskloop simd | barrier | | 2145 // | taskloop simd | taskwait | | 2146 // | taskloop simd | taskgroup | | 2147 // | taskloop simd | flush | | 2148 // | taskloop simd | ordered | + (with simd clause) | 2149 // | taskloop simd | atomic | | 2150 // | taskloop simd | target | | 2151 // | taskloop simd | teams | | 2152 // | taskloop simd | cancellation | | 2153 // | | point | | 2154 // | taskloop simd | cancel | | 2155 // | taskloop simd | taskloop | | 2156 // | taskloop simd | taskloop simd | | 2157 // +------------------+-----------------+------------------------------------+ 2158 if (Stack->getCurScope()) { 2159 auto ParentRegion = Stack->getParentDirective(); 2160 bool NestingProhibited = false; 2161 bool CloseNesting = true; 2162 enum { 2163 NoRecommend, 2164 ShouldBeInParallelRegion, 2165 ShouldBeInOrderedRegion, 2166 ShouldBeInTargetRegion 2167 } Recommend = NoRecommend; 2168 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) { 2169 // OpenMP [2.16, Nesting of Regions] 2170 // OpenMP constructs may not be nested inside a simd region. 2171 // OpenMP [2.8.1,simd Construct, Restrictions] 2172 // An ordered construct with the simd clause is the only OpenMP construct 2173 // that can appear in the simd region. 2174 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd); 2175 return true; 2176 } 2177 if (ParentRegion == OMPD_atomic) { 2178 // OpenMP [2.16, Nesting of Regions] 2179 // OpenMP constructs may not be nested inside an atomic region. 2180 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 2181 return true; 2182 } 2183 if (CurrentRegion == OMPD_section) { 2184 // OpenMP [2.7.2, sections Construct, Restrictions] 2185 // Orphaned section directives are prohibited. That is, the section 2186 // directives must appear within the sections construct and must not be 2187 // encountered elsewhere in the sections region. 2188 if (ParentRegion != OMPD_sections && 2189 ParentRegion != OMPD_parallel_sections) { 2190 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 2191 << (ParentRegion != OMPD_unknown) 2192 << getOpenMPDirectiveName(ParentRegion); 2193 return true; 2194 } 2195 return false; 2196 } 2197 // Allow some constructs to be orphaned (they could be used in functions, 2198 // called from OpenMP regions with the required preconditions). 2199 if (ParentRegion == OMPD_unknown) 2200 return false; 2201 if (CurrentRegion == OMPD_cancellation_point || 2202 CurrentRegion == OMPD_cancel) { 2203 // OpenMP [2.16, Nesting of Regions] 2204 // A cancellation point construct for which construct-type-clause is 2205 // taskgroup must be nested inside a task construct. A cancellation 2206 // point construct for which construct-type-clause is not taskgroup must 2207 // be closely nested inside an OpenMP construct that matches the type 2208 // specified in construct-type-clause. 2209 // A cancel construct for which construct-type-clause is taskgroup must be 2210 // nested inside a task construct. A cancel construct for which 2211 // construct-type-clause is not taskgroup must be closely nested inside an 2212 // OpenMP construct that matches the type specified in 2213 // construct-type-clause. 2214 NestingProhibited = 2215 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) || 2216 (CancelRegion == OMPD_for && 2217 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) || 2218 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) || 2219 (CancelRegion == OMPD_sections && 2220 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 2221 ParentRegion == OMPD_parallel_sections))); 2222 } else if (CurrentRegion == OMPD_master) { 2223 // OpenMP [2.16, Nesting of Regions] 2224 // A master region may not be closely nested inside a worksharing, 2225 // atomic, or explicit task region. 2226 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 2227 ParentRegion == OMPD_task || 2228 isOpenMPTaskLoopDirective(ParentRegion); 2229 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 2230 // OpenMP [2.16, Nesting of Regions] 2231 // A critical region may not be nested (closely or otherwise) inside a 2232 // critical region with the same name. Note that this restriction is not 2233 // sufficient to prevent deadlock. 2234 SourceLocation PreviousCriticalLoc; 2235 bool DeadLock = 2236 Stack->hasDirective([CurrentName, &PreviousCriticalLoc]( 2237 OpenMPDirectiveKind K, 2238 const DeclarationNameInfo &DNI, 2239 SourceLocation Loc) 2240 ->bool { 2241 if (K == OMPD_critical && 2242 DNI.getName() == CurrentName.getName()) { 2243 PreviousCriticalLoc = Loc; 2244 return true; 2245 } else 2246 return false; 2247 }, 2248 false /* skip top directive */); 2249 if (DeadLock) { 2250 SemaRef.Diag(StartLoc, 2251 diag::err_omp_prohibited_region_critical_same_name) 2252 << CurrentName.getName(); 2253 if (PreviousCriticalLoc.isValid()) 2254 SemaRef.Diag(PreviousCriticalLoc, 2255 diag::note_omp_previous_critical_region); 2256 return true; 2257 } 2258 } else if (CurrentRegion == OMPD_barrier) { 2259 // OpenMP [2.16, Nesting of Regions] 2260 // A barrier region may not be closely nested inside a worksharing, 2261 // explicit task, critical, ordered, atomic, or master region. 2262 NestingProhibited = 2263 isOpenMPWorksharingDirective(ParentRegion) || 2264 ParentRegion == OMPD_task || ParentRegion == OMPD_master || 2265 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered || 2266 isOpenMPTaskLoopDirective(ParentRegion); 2267 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 2268 !isOpenMPParallelDirective(CurrentRegion)) { 2269 // OpenMP [2.16, Nesting of Regions] 2270 // A worksharing region may not be closely nested inside a worksharing, 2271 // explicit task, critical, ordered, atomic, or master region. 2272 NestingProhibited = 2273 isOpenMPWorksharingDirective(ParentRegion) || 2274 ParentRegion == OMPD_task || ParentRegion == OMPD_master || 2275 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered || 2276 isOpenMPTaskLoopDirective(ParentRegion); 2277 Recommend = ShouldBeInParallelRegion; 2278 } else if (CurrentRegion == OMPD_ordered) { 2279 // OpenMP [2.16, Nesting of Regions] 2280 // An ordered region may not be closely nested inside a critical, 2281 // atomic, or explicit task region. 2282 // An ordered region must be closely nested inside a loop region (or 2283 // parallel loop region) with an ordered clause. 2284 // OpenMP [2.8.1,simd Construct, Restrictions] 2285 // An ordered construct with the simd clause is the only OpenMP construct 2286 // that can appear in the simd region. 2287 NestingProhibited = ParentRegion == OMPD_critical || 2288 ParentRegion == OMPD_task || 2289 isOpenMPTaskLoopDirective(ParentRegion) || 2290 !(isOpenMPSimdDirective(ParentRegion) || 2291 Stack->isParentOrderedRegion()); 2292 Recommend = ShouldBeInOrderedRegion; 2293 } else if (isOpenMPTeamsDirective(CurrentRegion)) { 2294 // OpenMP [2.16, Nesting of Regions] 2295 // If specified, a teams construct must be contained within a target 2296 // construct. 2297 NestingProhibited = ParentRegion != OMPD_target; 2298 Recommend = ShouldBeInTargetRegion; 2299 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc()); 2300 } 2301 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) { 2302 // OpenMP [2.16, Nesting of Regions] 2303 // distribute, parallel, parallel sections, parallel workshare, and the 2304 // parallel loop and parallel loop SIMD constructs are the only OpenMP 2305 // constructs that can be closely nested in the teams region. 2306 // TODO: add distribute directive. 2307 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion); 2308 Recommend = ShouldBeInParallelRegion; 2309 } 2310 if (NestingProhibited) { 2311 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 2312 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend 2313 << getOpenMPDirectiveName(CurrentRegion); 2314 return true; 2315 } 2316 } 2317 return false; 2318 } 2319 2320 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 2321 ArrayRef<OMPClause *> Clauses, 2322 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 2323 bool ErrorFound = false; 2324 unsigned NamedModifiersNumber = 0; 2325 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers( 2326 OMPD_unknown + 1); 2327 SmallVector<SourceLocation, 4> NameModifierLoc; 2328 for (const auto *C : Clauses) { 2329 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 2330 // At most one if clause without a directive-name-modifier can appear on 2331 // the directive. 2332 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 2333 if (FoundNameModifiers[CurNM]) { 2334 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause) 2335 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 2336 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 2337 ErrorFound = true; 2338 } else if (CurNM != OMPD_unknown) { 2339 NameModifierLoc.push_back(IC->getNameModifierLoc()); 2340 ++NamedModifiersNumber; 2341 } 2342 FoundNameModifiers[CurNM] = IC; 2343 if (CurNM == OMPD_unknown) 2344 continue; 2345 // Check if the specified name modifier is allowed for the current 2346 // directive. 2347 // At most one if clause with the particular directive-name-modifier can 2348 // appear on the directive. 2349 bool MatchFound = false; 2350 for (auto NM : AllowedNameModifiers) { 2351 if (CurNM == NM) { 2352 MatchFound = true; 2353 break; 2354 } 2355 } 2356 if (!MatchFound) { 2357 S.Diag(IC->getNameModifierLoc(), 2358 diag::err_omp_wrong_if_directive_name_modifier) 2359 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 2360 ErrorFound = true; 2361 } 2362 } 2363 } 2364 // If any if clause on the directive includes a directive-name-modifier then 2365 // all if clauses on the directive must include a directive-name-modifier. 2366 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 2367 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 2368 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(), 2369 diag::err_omp_no_more_if_clause); 2370 } else { 2371 std::string Values; 2372 std::string Sep(", "); 2373 unsigned AllowedCnt = 0; 2374 unsigned TotalAllowedNum = 2375 AllowedNameModifiers.size() - NamedModifiersNumber; 2376 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 2377 ++Cnt) { 2378 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 2379 if (!FoundNameModifiers[NM]) { 2380 Values += "'"; 2381 Values += getOpenMPDirectiveName(NM); 2382 Values += "'"; 2383 if (AllowedCnt + 2 == TotalAllowedNum) 2384 Values += " or "; 2385 else if (AllowedCnt + 1 != TotalAllowedNum) 2386 Values += Sep; 2387 ++AllowedCnt; 2388 } 2389 } 2390 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(), 2391 diag::err_omp_unnamed_if_clause) 2392 << (TotalAllowedNum > 1) << Values; 2393 } 2394 for (auto Loc : NameModifierLoc) { 2395 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 2396 } 2397 ErrorFound = true; 2398 } 2399 return ErrorFound; 2400 } 2401 2402 StmtResult Sema::ActOnOpenMPExecutableDirective( 2403 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 2404 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 2405 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 2406 StmtResult Res = StmtError(); 2407 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 2408 StartLoc)) 2409 return StmtError(); 2410 2411 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 2412 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA; 2413 bool ErrorFound = false; 2414 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 2415 if (AStmt) { 2416 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 2417 2418 // Check default data sharing attributes for referenced variables. 2419 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 2420 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt()); 2421 if (DSAChecker.isErrorFound()) 2422 return StmtError(); 2423 // Generate list of implicitly defined firstprivate variables. 2424 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 2425 2426 if (!DSAChecker.getImplicitFirstprivate().empty()) { 2427 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 2428 DSAChecker.getImplicitFirstprivate(), SourceLocation(), 2429 SourceLocation(), SourceLocation())) { 2430 ClausesWithImplicit.push_back(Implicit); 2431 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 2432 DSAChecker.getImplicitFirstprivate().size(); 2433 } else 2434 ErrorFound = true; 2435 } 2436 } 2437 2438 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 2439 switch (Kind) { 2440 case OMPD_parallel: 2441 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 2442 EndLoc); 2443 AllowedNameModifiers.push_back(OMPD_parallel); 2444 break; 2445 case OMPD_simd: 2446 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 2447 VarsWithInheritedDSA); 2448 break; 2449 case OMPD_for: 2450 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 2451 VarsWithInheritedDSA); 2452 break; 2453 case OMPD_for_simd: 2454 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 2455 EndLoc, VarsWithInheritedDSA); 2456 break; 2457 case OMPD_sections: 2458 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 2459 EndLoc); 2460 break; 2461 case OMPD_section: 2462 assert(ClausesWithImplicit.empty() && 2463 "No clauses are allowed for 'omp section' directive"); 2464 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 2465 break; 2466 case OMPD_single: 2467 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 2468 EndLoc); 2469 break; 2470 case OMPD_master: 2471 assert(ClausesWithImplicit.empty() && 2472 "No clauses are allowed for 'omp master' directive"); 2473 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 2474 break; 2475 case OMPD_critical: 2476 assert(ClausesWithImplicit.empty() && 2477 "No clauses are allowed for 'omp critical' directive"); 2478 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc); 2479 break; 2480 case OMPD_parallel_for: 2481 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 2482 EndLoc, VarsWithInheritedDSA); 2483 AllowedNameModifiers.push_back(OMPD_parallel); 2484 break; 2485 case OMPD_parallel_for_simd: 2486 Res = ActOnOpenMPParallelForSimdDirective( 2487 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2488 AllowedNameModifiers.push_back(OMPD_parallel); 2489 break; 2490 case OMPD_parallel_sections: 2491 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 2492 StartLoc, EndLoc); 2493 AllowedNameModifiers.push_back(OMPD_parallel); 2494 break; 2495 case OMPD_task: 2496 Res = 2497 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 2498 AllowedNameModifiers.push_back(OMPD_task); 2499 break; 2500 case OMPD_taskyield: 2501 assert(ClausesWithImplicit.empty() && 2502 "No clauses are allowed for 'omp taskyield' directive"); 2503 assert(AStmt == nullptr && 2504 "No associated statement allowed for 'omp taskyield' directive"); 2505 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 2506 break; 2507 case OMPD_barrier: 2508 assert(ClausesWithImplicit.empty() && 2509 "No clauses are allowed for 'omp barrier' directive"); 2510 assert(AStmt == nullptr && 2511 "No associated statement allowed for 'omp barrier' directive"); 2512 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 2513 break; 2514 case OMPD_taskwait: 2515 assert(ClausesWithImplicit.empty() && 2516 "No clauses are allowed for 'omp taskwait' directive"); 2517 assert(AStmt == nullptr && 2518 "No associated statement allowed for 'omp taskwait' directive"); 2519 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 2520 break; 2521 case OMPD_taskgroup: 2522 assert(ClausesWithImplicit.empty() && 2523 "No clauses are allowed for 'omp taskgroup' directive"); 2524 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc); 2525 break; 2526 case OMPD_flush: 2527 assert(AStmt == nullptr && 2528 "No associated statement allowed for 'omp flush' directive"); 2529 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 2530 break; 2531 case OMPD_ordered: 2532 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 2533 EndLoc); 2534 break; 2535 case OMPD_atomic: 2536 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 2537 EndLoc); 2538 break; 2539 case OMPD_teams: 2540 Res = 2541 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 2542 break; 2543 case OMPD_target: 2544 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 2545 EndLoc); 2546 AllowedNameModifiers.push_back(OMPD_target); 2547 break; 2548 case OMPD_cancellation_point: 2549 assert(ClausesWithImplicit.empty() && 2550 "No clauses are allowed for 'omp cancellation point' directive"); 2551 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 2552 "cancellation point' directive"); 2553 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 2554 break; 2555 case OMPD_cancel: 2556 assert(AStmt == nullptr && 2557 "No associated statement allowed for 'omp cancel' directive"); 2558 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 2559 CancelRegion); 2560 AllowedNameModifiers.push_back(OMPD_cancel); 2561 break; 2562 case OMPD_target_data: 2563 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 2564 EndLoc); 2565 AllowedNameModifiers.push_back(OMPD_target_data); 2566 break; 2567 case OMPD_taskloop: 2568 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 2569 EndLoc, VarsWithInheritedDSA); 2570 AllowedNameModifiers.push_back(OMPD_taskloop); 2571 break; 2572 case OMPD_taskloop_simd: 2573 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 2574 EndLoc, VarsWithInheritedDSA); 2575 AllowedNameModifiers.push_back(OMPD_taskloop); 2576 break; 2577 case OMPD_threadprivate: 2578 llvm_unreachable("OpenMP Directive is not allowed"); 2579 case OMPD_unknown: 2580 llvm_unreachable("Unknown OpenMP directive"); 2581 } 2582 2583 for (auto P : VarsWithInheritedDSA) { 2584 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 2585 << P.first << P.second->getSourceRange(); 2586 } 2587 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound; 2588 2589 if (!AllowedNameModifiers.empty()) 2590 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 2591 ErrorFound; 2592 2593 if (ErrorFound) 2594 return StmtError(); 2595 return Res; 2596 } 2597 2598 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 2599 Stmt *AStmt, 2600 SourceLocation StartLoc, 2601 SourceLocation EndLoc) { 2602 if (!AStmt) 2603 return StmtError(); 2604 2605 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 2606 // 1.2.2 OpenMP Language Terminology 2607 // Structured block - An executable statement with a single entry at the 2608 // top and a single exit at the bottom. 2609 // The point of exit cannot be a branch out of the structured block. 2610 // longjmp() and throw() must not violate the entry/exit criteria. 2611 CS->getCapturedDecl()->setNothrow(); 2612 2613 getCurFunction()->setHasBranchProtectedScope(); 2614 2615 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 2616 DSAStack->isCancelRegion()); 2617 } 2618 2619 namespace { 2620 /// \brief Helper class for checking canonical form of the OpenMP loops and 2621 /// extracting iteration space of each loop in the loop nest, that will be used 2622 /// for IR generation. 2623 class OpenMPIterationSpaceChecker { 2624 /// \brief Reference to Sema. 2625 Sema &SemaRef; 2626 /// \brief A location for diagnostics (when there is no some better location). 2627 SourceLocation DefaultLoc; 2628 /// \brief A location for diagnostics (when increment is not compatible). 2629 SourceLocation ConditionLoc; 2630 /// \brief A source location for referring to loop init later. 2631 SourceRange InitSrcRange; 2632 /// \brief A source location for referring to condition later. 2633 SourceRange ConditionSrcRange; 2634 /// \brief A source location for referring to increment later. 2635 SourceRange IncrementSrcRange; 2636 /// \brief Loop variable. 2637 VarDecl *Var; 2638 /// \brief Reference to loop variable. 2639 DeclRefExpr *VarRef; 2640 /// \brief Lower bound (initializer for the var). 2641 Expr *LB; 2642 /// \brief Upper bound. 2643 Expr *UB; 2644 /// \brief Loop step (increment). 2645 Expr *Step; 2646 /// \brief This flag is true when condition is one of: 2647 /// Var < UB 2648 /// Var <= UB 2649 /// UB > Var 2650 /// UB >= Var 2651 bool TestIsLessOp; 2652 /// \brief This flag is true when condition is strict ( < or > ). 2653 bool TestIsStrictOp; 2654 /// \brief This flag is true when step is subtracted on each iteration. 2655 bool SubtractStep; 2656 2657 public: 2658 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc) 2659 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc), 2660 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()), 2661 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr), 2662 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false), 2663 TestIsStrictOp(false), SubtractStep(false) {} 2664 /// \brief Check init-expr for canonical loop form and save loop counter 2665 /// variable - #Var and its initialization value - #LB. 2666 bool CheckInit(Stmt *S, bool EmitDiags = true); 2667 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags 2668 /// for less/greater and for strict/non-strict comparison. 2669 bool CheckCond(Expr *S); 2670 /// \brief Check incr-expr for canonical loop form and return true if it 2671 /// does not conform, otherwise save loop step (#Step). 2672 bool CheckInc(Expr *S); 2673 /// \brief Return the loop counter variable. 2674 VarDecl *GetLoopVar() const { return Var; } 2675 /// \brief Return the reference expression to loop counter variable. 2676 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; } 2677 /// \brief Source range of the loop init. 2678 SourceRange GetInitSrcRange() const { return InitSrcRange; } 2679 /// \brief Source range of the loop condition. 2680 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; } 2681 /// \brief Source range of the loop increment. 2682 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; } 2683 /// \brief True if the step should be subtracted. 2684 bool ShouldSubtractStep() const { return SubtractStep; } 2685 /// \brief Build the expression to calculate the number of iterations. 2686 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const; 2687 /// \brief Build the precondition expression for the loops. 2688 Expr *BuildPreCond(Scope *S, Expr *Cond) const; 2689 /// \brief Build reference expression to the counter be used for codegen. 2690 Expr *BuildCounterVar() const; 2691 /// \brief Build reference expression to the private counter be used for 2692 /// codegen. 2693 Expr *BuildPrivateCounterVar() const; 2694 /// \brief Build initization of the counter be used for codegen. 2695 Expr *BuildCounterInit() const; 2696 /// \brief Build step of the counter be used for codegen. 2697 Expr *BuildCounterStep() const; 2698 /// \brief Return true if any expression is dependent. 2699 bool Dependent() const; 2700 2701 private: 2702 /// \brief Check the right-hand side of an assignment in the increment 2703 /// expression. 2704 bool CheckIncRHS(Expr *RHS); 2705 /// \brief Helper to set loop counter variable and its initializer. 2706 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB); 2707 /// \brief Helper to set upper bound. 2708 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR, 2709 SourceLocation SL); 2710 /// \brief Helper to set loop increment. 2711 bool SetStep(Expr *NewStep, bool Subtract); 2712 }; 2713 2714 bool OpenMPIterationSpaceChecker::Dependent() const { 2715 if (!Var) { 2716 assert(!LB && !UB && !Step); 2717 return false; 2718 } 2719 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) || 2720 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent()); 2721 } 2722 2723 template <typename T> 2724 static T *getExprAsWritten(T *E) { 2725 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E)) 2726 E = ExprTemp->getSubExpr(); 2727 2728 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 2729 E = MTE->GetTemporaryExpr(); 2730 2731 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 2732 E = Binder->getSubExpr(); 2733 2734 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 2735 E = ICE->getSubExprAsWritten(); 2736 return E->IgnoreParens(); 2737 } 2738 2739 bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, 2740 DeclRefExpr *NewVarRefExpr, 2741 Expr *NewLB) { 2742 // State consistency checking to ensure correct usage. 2743 assert(Var == nullptr && LB == nullptr && VarRef == nullptr && 2744 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 2745 if (!NewVar || !NewLB) 2746 return true; 2747 Var = NewVar; 2748 VarRef = NewVarRefExpr; 2749 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 2750 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 2751 if ((Ctor->isCopyOrMoveConstructor() || 2752 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 2753 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 2754 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 2755 LB = NewLB; 2756 return false; 2757 } 2758 2759 bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp, 2760 SourceRange SR, SourceLocation SL) { 2761 // State consistency checking to ensure correct usage. 2762 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr && 2763 !TestIsLessOp && !TestIsStrictOp); 2764 if (!NewUB) 2765 return true; 2766 UB = NewUB; 2767 TestIsLessOp = LessOp; 2768 TestIsStrictOp = StrictOp; 2769 ConditionSrcRange = SR; 2770 ConditionLoc = SL; 2771 return false; 2772 } 2773 2774 bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) { 2775 // State consistency checking to ensure correct usage. 2776 assert(Var != nullptr && LB != nullptr && Step == nullptr); 2777 if (!NewStep) 2778 return true; 2779 if (!NewStep->isValueDependent()) { 2780 // Check that the step is integer expression. 2781 SourceLocation StepLoc = NewStep->getLocStart(); 2782 ExprResult Val = 2783 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep); 2784 if (Val.isInvalid()) 2785 return true; 2786 NewStep = Val.get(); 2787 2788 // OpenMP [2.6, Canonical Loop Form, Restrictions] 2789 // If test-expr is of form var relational-op b and relational-op is < or 2790 // <= then incr-expr must cause var to increase on each iteration of the 2791 // loop. If test-expr is of form var relational-op b and relational-op is 2792 // > or >= then incr-expr must cause var to decrease on each iteration of 2793 // the loop. 2794 // If test-expr is of form b relational-op var and relational-op is < or 2795 // <= then incr-expr must cause var to decrease on each iteration of the 2796 // loop. If test-expr is of form b relational-op var and relational-op is 2797 // > or >= then incr-expr must cause var to increase on each iteration of 2798 // the loop. 2799 llvm::APSInt Result; 2800 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); 2801 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 2802 bool IsConstNeg = 2803 IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); 2804 bool IsConstPos = 2805 IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); 2806 bool IsConstZero = IsConstant && !Result.getBoolValue(); 2807 if (UB && (IsConstZero || 2808 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract)) 2809 : (IsConstPos || (IsUnsigned && !Subtract))))) { 2810 SemaRef.Diag(NewStep->getExprLoc(), 2811 diag::err_omp_loop_incr_not_compatible) 2812 << Var << TestIsLessOp << NewStep->getSourceRange(); 2813 SemaRef.Diag(ConditionLoc, 2814 diag::note_omp_loop_cond_requres_compatible_incr) 2815 << TestIsLessOp << ConditionSrcRange; 2816 return true; 2817 } 2818 if (TestIsLessOp == Subtract) { 2819 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, 2820 NewStep).get(); 2821 Subtract = !Subtract; 2822 } 2823 } 2824 2825 Step = NewStep; 2826 SubtractStep = Subtract; 2827 return false; 2828 } 2829 2830 bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) { 2831 // Check init-expr for canonical loop form and save loop counter 2832 // variable - #Var and its initialization value - #LB. 2833 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 2834 // var = lb 2835 // integer-type var = lb 2836 // random-access-iterator-type var = lb 2837 // pointer-type var = lb 2838 // 2839 if (!S) { 2840 if (EmitDiags) { 2841 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 2842 } 2843 return true; 2844 } 2845 InitSrcRange = S->getSourceRange(); 2846 if (Expr *E = dyn_cast<Expr>(S)) 2847 S = E->IgnoreParens(); 2848 if (auto BO = dyn_cast<BinaryOperator>(S)) { 2849 if (BO->getOpcode() == BO_Assign) 2850 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens())) 2851 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE, 2852 BO->getRHS()); 2853 } else if (auto DS = dyn_cast<DeclStmt>(S)) { 2854 if (DS->isSingleDecl()) { 2855 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 2856 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 2857 // Accept non-canonical init form here but emit ext. warning. 2858 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 2859 SemaRef.Diag(S->getLocStart(), 2860 diag::ext_omp_loop_not_canonical_init) 2861 << S->getSourceRange(); 2862 return SetVarAndLB(Var, nullptr, Var->getInit()); 2863 } 2864 } 2865 } 2866 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) 2867 if (CE->getOperator() == OO_Equal) 2868 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0))) 2869 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE, 2870 CE->getArg(1)); 2871 2872 if (EmitDiags) { 2873 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init) 2874 << S->getSourceRange(); 2875 } 2876 return true; 2877 } 2878 2879 /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the 2880 /// variable (which may be the loop variable) if possible. 2881 static const VarDecl *GetInitVarDecl(const Expr *E) { 2882 if (!E) 2883 return nullptr; 2884 E = getExprAsWritten(E); 2885 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 2886 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 2887 if ((Ctor->isCopyOrMoveConstructor() || 2888 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 2889 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 2890 E = CE->getArg(0)->IgnoreParenImpCasts(); 2891 auto DRE = dyn_cast_or_null<DeclRefExpr>(E); 2892 if (!DRE) 2893 return nullptr; 2894 return dyn_cast<VarDecl>(DRE->getDecl()); 2895 } 2896 2897 bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) { 2898 // Check test-expr for canonical form, save upper-bound UB, flags for 2899 // less/greater and for strict/non-strict comparison. 2900 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 2901 // var relational-op b 2902 // b relational-op var 2903 // 2904 if (!S) { 2905 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var; 2906 return true; 2907 } 2908 S = getExprAsWritten(S); 2909 SourceLocation CondLoc = S->getLocStart(); 2910 if (auto BO = dyn_cast<BinaryOperator>(S)) { 2911 if (BO->isRelationalOp()) { 2912 if (GetInitVarDecl(BO->getLHS()) == Var) 2913 return SetUB(BO->getRHS(), 2914 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 2915 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 2916 BO->getSourceRange(), BO->getOperatorLoc()); 2917 if (GetInitVarDecl(BO->getRHS()) == Var) 2918 return SetUB(BO->getLHS(), 2919 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 2920 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 2921 BO->getSourceRange(), BO->getOperatorLoc()); 2922 } 2923 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) { 2924 if (CE->getNumArgs() == 2) { 2925 auto Op = CE->getOperator(); 2926 switch (Op) { 2927 case OO_Greater: 2928 case OO_GreaterEqual: 2929 case OO_Less: 2930 case OO_LessEqual: 2931 if (GetInitVarDecl(CE->getArg(0)) == Var) 2932 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 2933 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 2934 CE->getOperatorLoc()); 2935 if (GetInitVarDecl(CE->getArg(1)) == Var) 2936 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 2937 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 2938 CE->getOperatorLoc()); 2939 break; 2940 default: 2941 break; 2942 } 2943 } 2944 } 2945 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 2946 << S->getSourceRange() << Var; 2947 return true; 2948 } 2949 2950 bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) { 2951 // RHS of canonical loop form increment can be: 2952 // var + incr 2953 // incr + var 2954 // var - incr 2955 // 2956 RHS = RHS->IgnoreParenImpCasts(); 2957 if (auto BO = dyn_cast<BinaryOperator>(RHS)) { 2958 if (BO->isAdditiveOp()) { 2959 bool IsAdd = BO->getOpcode() == BO_Add; 2960 if (GetInitVarDecl(BO->getLHS()) == Var) 2961 return SetStep(BO->getRHS(), !IsAdd); 2962 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var) 2963 return SetStep(BO->getLHS(), false); 2964 } 2965 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 2966 bool IsAdd = CE->getOperator() == OO_Plus; 2967 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 2968 if (GetInitVarDecl(CE->getArg(0)) == Var) 2969 return SetStep(CE->getArg(1), !IsAdd); 2970 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var) 2971 return SetStep(CE->getArg(0), false); 2972 } 2973 } 2974 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr) 2975 << RHS->getSourceRange() << Var; 2976 return true; 2977 } 2978 2979 bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) { 2980 // Check incr-expr for canonical loop form and return true if it 2981 // does not conform. 2982 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 2983 // ++var 2984 // var++ 2985 // --var 2986 // var-- 2987 // var += incr 2988 // var -= incr 2989 // var = var + incr 2990 // var = incr + var 2991 // var = var - incr 2992 // 2993 if (!S) { 2994 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var; 2995 return true; 2996 } 2997 IncrementSrcRange = S->getSourceRange(); 2998 S = S->IgnoreParens(); 2999 if (auto UO = dyn_cast<UnaryOperator>(S)) { 3000 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var) 3001 return SetStep( 3002 SemaRef.ActOnIntegerConstant(UO->getLocStart(), 3003 (UO->isDecrementOp() ? -1 : 1)).get(), 3004 false); 3005 } else if (auto BO = dyn_cast<BinaryOperator>(S)) { 3006 switch (BO->getOpcode()) { 3007 case BO_AddAssign: 3008 case BO_SubAssign: 3009 if (GetInitVarDecl(BO->getLHS()) == Var) 3010 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 3011 break; 3012 case BO_Assign: 3013 if (GetInitVarDecl(BO->getLHS()) == Var) 3014 return CheckIncRHS(BO->getRHS()); 3015 break; 3016 default: 3017 break; 3018 } 3019 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) { 3020 switch (CE->getOperator()) { 3021 case OO_PlusPlus: 3022 case OO_MinusMinus: 3023 if (GetInitVarDecl(CE->getArg(0)) == Var) 3024 return SetStep( 3025 SemaRef.ActOnIntegerConstant( 3026 CE->getLocStart(), 3027 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(), 3028 false); 3029 break; 3030 case OO_PlusEqual: 3031 case OO_MinusEqual: 3032 if (GetInitVarDecl(CE->getArg(0)) == Var) 3033 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 3034 break; 3035 case OO_Equal: 3036 if (GetInitVarDecl(CE->getArg(0)) == Var) 3037 return CheckIncRHS(CE->getArg(1)); 3038 break; 3039 default: 3040 break; 3041 } 3042 } 3043 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr) 3044 << S->getSourceRange() << Var; 3045 return true; 3046 } 3047 3048 namespace { 3049 // Transform variables declared in GNU statement expressions to new ones to 3050 // avoid crash on codegen. 3051 class TransformToNewDefs : public TreeTransform<TransformToNewDefs> { 3052 typedef TreeTransform<TransformToNewDefs> BaseTransform; 3053 3054 public: 3055 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {} 3056 3057 Decl *TransformDefinition(SourceLocation Loc, Decl *D) { 3058 if (auto *VD = cast<VarDecl>(D)) 3059 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) && 3060 !isa<ImplicitParamDecl>(D)) { 3061 auto *NewVD = VarDecl::Create( 3062 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(), 3063 VD->getLocation(), VD->getIdentifier(), VD->getType(), 3064 VD->getTypeSourceInfo(), VD->getStorageClass()); 3065 NewVD->setTSCSpec(VD->getTSCSpec()); 3066 NewVD->setInit(VD->getInit()); 3067 NewVD->setInitStyle(VD->getInitStyle()); 3068 NewVD->setExceptionVariable(VD->isExceptionVariable()); 3069 NewVD->setNRVOVariable(VD->isNRVOVariable()); 3070 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext()); 3071 NewVD->setConstexpr(VD->isConstexpr()); 3072 NewVD->setInitCapture(VD->isInitCapture()); 3073 NewVD->setPreviousDeclInSameBlockScope( 3074 VD->isPreviousDeclInSameBlockScope()); 3075 VD->getDeclContext()->addHiddenDecl(NewVD); 3076 if (VD->hasAttrs()) 3077 NewVD->setAttrs(VD->getAttrs()); 3078 transformedLocalDecl(VD, NewVD); 3079 return NewVD; 3080 } 3081 return BaseTransform::TransformDefinition(Loc, D); 3082 } 3083 3084 ExprResult TransformDeclRefExpr(DeclRefExpr *E) { 3085 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl())) 3086 if (E->getDecl() != NewD) { 3087 NewD->setReferenced(); 3088 NewD->markUsed(SemaRef.Context); 3089 return DeclRefExpr::Create( 3090 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(), 3091 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(), 3092 E->getNameInfo(), E->getType(), E->getValueKind()); 3093 } 3094 return BaseTransform::TransformDeclRefExpr(E); 3095 } 3096 }; 3097 } 3098 3099 /// \brief Build the expression to calculate the number of iterations. 3100 Expr * 3101 OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S, 3102 const bool LimitedType) const { 3103 TransformToNewDefs Transform(SemaRef); 3104 ExprResult Diff; 3105 auto VarType = Var->getType().getNonReferenceType(); 3106 if (VarType->isIntegerType() || VarType->isPointerType() || 3107 SemaRef.getLangOpts().CPlusPlus) { 3108 // Upper - Lower 3109 auto *UBExpr = TestIsLessOp ? UB : LB; 3110 auto *LBExpr = TestIsLessOp ? LB : UB; 3111 Expr *Upper = Transform.TransformExpr(UBExpr).get(); 3112 Expr *Lower = Transform.TransformExpr(LBExpr).get(); 3113 if (!Upper || !Lower) 3114 return nullptr; 3115 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(), 3116 Sema::AA_Converting, 3117 /*AllowExplicit=*/true) 3118 .get(); 3119 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(), 3120 Sema::AA_Converting, 3121 /*AllowExplicit=*/true) 3122 .get(); 3123 if (!Upper || !Lower) 3124 return nullptr; 3125 3126 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 3127 3128 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 3129 // BuildBinOp already emitted error, this one is to point user to upper 3130 // and lower bound, and to tell what is passed to 'operator-'. 3131 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx) 3132 << Upper->getSourceRange() << Lower->getSourceRange(); 3133 return nullptr; 3134 } 3135 } 3136 3137 if (!Diff.isUsable()) 3138 return nullptr; 3139 3140 // Upper - Lower [- 1] 3141 if (TestIsStrictOp) 3142 Diff = SemaRef.BuildBinOp( 3143 S, DefaultLoc, BO_Sub, Diff.get(), 3144 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 3145 if (!Diff.isUsable()) 3146 return nullptr; 3147 3148 // Upper - Lower [- 1] + Step 3149 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit()); 3150 if (NewStep.isInvalid()) 3151 return nullptr; 3152 NewStep = SemaRef.PerformImplicitConversion( 3153 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting, 3154 /*AllowExplicit=*/true); 3155 if (NewStep.isInvalid()) 3156 return nullptr; 3157 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 3158 if (!Diff.isUsable()) 3159 return nullptr; 3160 3161 // Parentheses (for dumping/debugging purposes only). 3162 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 3163 if (!Diff.isUsable()) 3164 return nullptr; 3165 3166 // (Upper - Lower [- 1] + Step) / Step 3167 NewStep = Transform.TransformExpr(Step->IgnoreImplicit()); 3168 if (NewStep.isInvalid()) 3169 return nullptr; 3170 NewStep = SemaRef.PerformImplicitConversion( 3171 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting, 3172 /*AllowExplicit=*/true); 3173 if (NewStep.isInvalid()) 3174 return nullptr; 3175 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 3176 if (!Diff.isUsable()) 3177 return nullptr; 3178 3179 // OpenMP runtime requires 32-bit or 64-bit loop variables. 3180 QualType Type = Diff.get()->getType(); 3181 auto &C = SemaRef.Context; 3182 bool UseVarType = VarType->hasIntegerRepresentation() && 3183 C.getTypeSize(Type) > C.getTypeSize(VarType); 3184 if (!Type->isIntegerType() || UseVarType) { 3185 unsigned NewSize = 3186 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 3187 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 3188 : Type->hasSignedIntegerRepresentation(); 3189 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 3190 Diff = SemaRef.PerformImplicitConversion( 3191 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 3192 if (!Diff.isUsable()) 3193 return nullptr; 3194 } 3195 if (LimitedType) { 3196 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 3197 if (NewSize != C.getTypeSize(Type)) { 3198 if (NewSize < C.getTypeSize(Type)) { 3199 assert(NewSize == 64 && "incorrect loop var size"); 3200 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 3201 << InitSrcRange << ConditionSrcRange; 3202 } 3203 QualType NewType = C.getIntTypeForBitwidth( 3204 NewSize, Type->hasSignedIntegerRepresentation() || 3205 C.getTypeSize(Type) < NewSize); 3206 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 3207 Sema::AA_Converting, true); 3208 if (!Diff.isUsable()) 3209 return nullptr; 3210 } 3211 } 3212 3213 return Diff.get(); 3214 } 3215 3216 Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const { 3217 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 3218 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 3219 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 3220 TransformToNewDefs Transform(SemaRef); 3221 3222 auto NewLB = Transform.TransformExpr(LB); 3223 auto NewUB = Transform.TransformExpr(UB); 3224 if (NewLB.isInvalid() || NewUB.isInvalid()) 3225 return Cond; 3226 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(), 3227 Sema::AA_Converting, 3228 /*AllowExplicit=*/true); 3229 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(), 3230 Sema::AA_Converting, 3231 /*AllowExplicit=*/true); 3232 if (NewLB.isInvalid() || NewUB.isInvalid()) 3233 return Cond; 3234 auto CondExpr = SemaRef.BuildBinOp( 3235 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE) 3236 : (TestIsStrictOp ? BO_GT : BO_GE), 3237 NewLB.get(), NewUB.get()); 3238 if (CondExpr.isUsable()) { 3239 CondExpr = SemaRef.PerformImplicitConversion( 3240 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 3241 /*AllowExplicit=*/true); 3242 } 3243 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 3244 // Otherwise use original loop conditon and evaluate it in runtime. 3245 return CondExpr.isUsable() ? CondExpr.get() : Cond; 3246 } 3247 3248 /// \brief Build reference expression to the counter be used for codegen. 3249 Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const { 3250 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(), 3251 DefaultLoc); 3252 } 3253 3254 Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const { 3255 if (Var && !Var->isInvalidDecl()) { 3256 auto Type = Var->getType().getNonReferenceType(); 3257 auto *PrivateVar = 3258 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(), 3259 Var->hasAttrs() ? &Var->getAttrs() : nullptr); 3260 if (PrivateVar->isInvalidDecl()) 3261 return nullptr; 3262 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 3263 } 3264 return nullptr; 3265 } 3266 3267 /// \brief Build initization of the counter be used for codegen. 3268 Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; } 3269 3270 /// \brief Build step of the counter be used for codegen. 3271 Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; } 3272 3273 /// \brief Iteration space of a single for loop. 3274 struct LoopIterationSpace { 3275 /// \brief Condition of the loop. 3276 Expr *PreCond; 3277 /// \brief This expression calculates the number of iterations in the loop. 3278 /// It is always possible to calculate it before starting the loop. 3279 Expr *NumIterations; 3280 /// \brief The loop counter variable. 3281 Expr *CounterVar; 3282 /// \brief Private loop counter variable. 3283 Expr *PrivateCounterVar; 3284 /// \brief This is initializer for the initial value of #CounterVar. 3285 Expr *CounterInit; 3286 /// \brief This is step for the #CounterVar used to generate its update: 3287 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 3288 Expr *CounterStep; 3289 /// \brief Should step be subtracted? 3290 bool Subtract; 3291 /// \brief Source range of the loop init. 3292 SourceRange InitSrcRange; 3293 /// \brief Source range of the loop condition. 3294 SourceRange CondSrcRange; 3295 /// \brief Source range of the loop increment. 3296 SourceRange IncSrcRange; 3297 }; 3298 3299 } // namespace 3300 3301 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 3302 assert(getLangOpts().OpenMP && "OpenMP is not active."); 3303 assert(Init && "Expected loop in canonical form."); 3304 unsigned CollapseIteration = DSAStack->getCollapseNumber(); 3305 if (CollapseIteration > 0 && 3306 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 3307 OpenMPIterationSpaceChecker ISC(*this, ForLoc); 3308 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) { 3309 DSAStack->addLoopControlVariable(ISC.GetLoopVar()); 3310 } 3311 DSAStack->setCollapseNumber(CollapseIteration - 1); 3312 } 3313 } 3314 3315 /// \brief Called on a for stmt to check and extract its iteration space 3316 /// for further processing (such as collapsing). 3317 static bool CheckOpenMPIterationSpace( 3318 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 3319 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 3320 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr, 3321 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA, 3322 LoopIterationSpace &ResultIterSpace) { 3323 // OpenMP [2.6, Canonical Loop Form] 3324 // for (init-expr; test-expr; incr-expr) structured-block 3325 auto For = dyn_cast_or_null<ForStmt>(S); 3326 if (!For) { 3327 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for) 3328 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 3329 << getOpenMPDirectiveName(DKind) << NestedLoopCount 3330 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 3331 if (NestedLoopCount > 1) { 3332 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 3333 SemaRef.Diag(DSA.getConstructLoc(), 3334 diag::note_omp_collapse_ordered_expr) 3335 << 2 << CollapseLoopCountExpr->getSourceRange() 3336 << OrderedLoopCountExpr->getSourceRange(); 3337 else if (CollapseLoopCountExpr) 3338 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 3339 diag::note_omp_collapse_ordered_expr) 3340 << 0 << CollapseLoopCountExpr->getSourceRange(); 3341 else 3342 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 3343 diag::note_omp_collapse_ordered_expr) 3344 << 1 << OrderedLoopCountExpr->getSourceRange(); 3345 } 3346 return true; 3347 } 3348 assert(For->getBody()); 3349 3350 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc()); 3351 3352 // Check init. 3353 auto Init = For->getInit(); 3354 if (ISC.CheckInit(Init)) { 3355 return true; 3356 } 3357 3358 bool HasErrors = false; 3359 3360 // Check loop variable's type. 3361 auto Var = ISC.GetLoopVar(); 3362 3363 // OpenMP [2.6, Canonical Loop Form] 3364 // Var is one of the following: 3365 // A variable of signed or unsigned integer type. 3366 // For C++, a variable of a random access iterator type. 3367 // For C, a variable of a pointer type. 3368 auto VarType = Var->getType().getNonReferenceType(); 3369 if (!VarType->isDependentType() && !VarType->isIntegerType() && 3370 !VarType->isPointerType() && 3371 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 3372 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type) 3373 << SemaRef.getLangOpts().CPlusPlus; 3374 HasErrors = true; 3375 } 3376 3377 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a 3378 // Construct 3379 // The loop iteration variable(s) in the associated for-loop(s) of a for or 3380 // parallel for construct is (are) private. 3381 // The loop iteration variable in the associated for-loop of a simd construct 3382 // with just one associated for-loop is linear with a constant-linear-step 3383 // that is the increment of the associated for-loop. 3384 // Exclude loop var from the list of variables with implicitly defined data 3385 // sharing attributes. 3386 VarsWithImplicitDSA.erase(Var); 3387 3388 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in 3389 // a Construct, C/C++]. 3390 // The loop iteration variable in the associated for-loop of a simd construct 3391 // with just one associated for-loop may be listed in a linear clause with a 3392 // constant-linear-step that is the increment of the associated for-loop. 3393 // The loop iteration variable(s) in the associated for-loop(s) of a for or 3394 // parallel for construct may be listed in a private or lastprivate clause. 3395 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false); 3396 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr(); 3397 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is 3398 // declared in the loop and it is predetermined as a private. 3399 auto PredeterminedCKind = 3400 isOpenMPSimdDirective(DKind) 3401 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate) 3402 : OMPC_private; 3403 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 3404 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) || 3405 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop) && 3406 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 3407 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate && 3408 DVar.CKind != OMPC_threadprivate)) && 3409 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) || 3410 DVar.RefExpr != nullptr)) { 3411 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa) 3412 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind) 3413 << getOpenMPClauseName(PredeterminedCKind); 3414 if (DVar.RefExpr == nullptr) 3415 DVar.CKind = PredeterminedCKind; 3416 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true); 3417 HasErrors = true; 3418 } else if (LoopVarRefExpr != nullptr) { 3419 // Make the loop iteration variable private (for worksharing constructs), 3420 // linear (for simd directives with the only one associated loop) or 3421 // lastprivate (for simd directives with several collapsed or ordered 3422 // loops). 3423 if (DVar.CKind == OMPC_unknown) 3424 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(), 3425 /*FromParent=*/false); 3426 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind); 3427 } 3428 3429 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 3430 3431 // Check test-expr. 3432 HasErrors |= ISC.CheckCond(For->getCond()); 3433 3434 // Check incr-expr. 3435 HasErrors |= ISC.CheckInc(For->getInc()); 3436 3437 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 3438 return HasErrors; 3439 3440 // Build the loop's iteration space representation. 3441 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond()); 3442 ResultIterSpace.NumIterations = ISC.BuildNumIterations( 3443 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) || 3444 isOpenMPTaskLoopDirective(DKind))); 3445 ResultIterSpace.CounterVar = ISC.BuildCounterVar(); 3446 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar(); 3447 ResultIterSpace.CounterInit = ISC.BuildCounterInit(); 3448 ResultIterSpace.CounterStep = ISC.BuildCounterStep(); 3449 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange(); 3450 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange(); 3451 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange(); 3452 ResultIterSpace.Subtract = ISC.ShouldSubtractStep(); 3453 3454 HasErrors |= (ResultIterSpace.PreCond == nullptr || 3455 ResultIterSpace.NumIterations == nullptr || 3456 ResultIterSpace.CounterVar == nullptr || 3457 ResultIterSpace.PrivateCounterVar == nullptr || 3458 ResultIterSpace.CounterInit == nullptr || 3459 ResultIterSpace.CounterStep == nullptr); 3460 3461 return HasErrors; 3462 } 3463 3464 /// \brief Build 'VarRef = Start. 3465 static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, 3466 ExprResult VarRef, ExprResult Start) { 3467 TransformToNewDefs Transform(SemaRef); 3468 // Build 'VarRef = Start. 3469 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit()); 3470 if (NewStart.isInvalid()) 3471 return ExprError(); 3472 NewStart = SemaRef.PerformImplicitConversion( 3473 NewStart.get(), Start.get()->IgnoreImplicit()->getType(), 3474 Sema::AA_Converting, 3475 /*AllowExplicit=*/true); 3476 if (NewStart.isInvalid()) 3477 return ExprError(); 3478 NewStart = SemaRef.PerformImplicitConversion( 3479 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 3480 /*AllowExplicit=*/true); 3481 if (!NewStart.isUsable()) 3482 return ExprError(); 3483 3484 auto Init = 3485 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 3486 return Init; 3487 } 3488 3489 /// \brief Build 'VarRef = Start + Iter * Step'. 3490 static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S, 3491 SourceLocation Loc, ExprResult VarRef, 3492 ExprResult Start, ExprResult Iter, 3493 ExprResult Step, bool Subtract) { 3494 // Add parentheses (for debugging purposes only). 3495 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 3496 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 3497 !Step.isUsable()) 3498 return ExprError(); 3499 3500 TransformToNewDefs Transform(SemaRef); 3501 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit()); 3502 if (NewStep.isInvalid()) 3503 return ExprError(); 3504 NewStep = SemaRef.PerformImplicitConversion( 3505 NewStep.get(), Step.get()->IgnoreImplicit()->getType(), 3506 Sema::AA_Converting, 3507 /*AllowExplicit=*/true); 3508 if (NewStep.isInvalid()) 3509 return ExprError(); 3510 ExprResult Update = 3511 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 3512 if (!Update.isUsable()) 3513 return ExprError(); 3514 3515 // Build 'VarRef = Start + Iter * Step'. 3516 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit()); 3517 if (NewStart.isInvalid()) 3518 return ExprError(); 3519 NewStart = SemaRef.PerformImplicitConversion( 3520 NewStart.get(), Start.get()->IgnoreImplicit()->getType(), 3521 Sema::AA_Converting, 3522 /*AllowExplicit=*/true); 3523 if (NewStart.isInvalid()) 3524 return ExprError(); 3525 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add), 3526 NewStart.get(), Update.get()); 3527 if (!Update.isUsable()) 3528 return ExprError(); 3529 3530 Update = SemaRef.PerformImplicitConversion( 3531 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 3532 if (!Update.isUsable()) 3533 return ExprError(); 3534 3535 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 3536 return Update; 3537 } 3538 3539 /// \brief Convert integer expression \a E to make it have at least \a Bits 3540 /// bits. 3541 static ExprResult WidenIterationCount(unsigned Bits, Expr *E, 3542 Sema &SemaRef) { 3543 if (E == nullptr) 3544 return ExprError(); 3545 auto &C = SemaRef.Context; 3546 QualType OldType = E->getType(); 3547 unsigned HasBits = C.getTypeSize(OldType); 3548 if (HasBits >= Bits) 3549 return ExprResult(E); 3550 // OK to convert to signed, because new type has more bits than old. 3551 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 3552 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 3553 true); 3554 } 3555 3556 /// \brief Check if the given expression \a E is a constant integer that fits 3557 /// into \a Bits bits. 3558 static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) { 3559 if (E == nullptr) 3560 return false; 3561 llvm::APSInt Result; 3562 if (E->isIntegerConstantExpr(Result, SemaRef.Context)) 3563 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); 3564 return false; 3565 } 3566 3567 /// \brief Called on a for stmt to check itself and nested loops (if any). 3568 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 3569 /// number of collapsed loops otherwise. 3570 static unsigned 3571 CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 3572 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 3573 DSAStackTy &DSA, 3574 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA, 3575 OMPLoopDirective::HelperExprs &Built) { 3576 unsigned NestedLoopCount = 1; 3577 if (CollapseLoopCountExpr) { 3578 // Found 'collapse' clause - calculate collapse number. 3579 llvm::APSInt Result; 3580 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) 3581 NestedLoopCount = Result.getLimitedValue(); 3582 } 3583 if (OrderedLoopCountExpr) { 3584 // Found 'ordered' clause - calculate collapse number. 3585 llvm::APSInt Result; 3586 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 3587 if (Result.getLimitedValue() < NestedLoopCount) { 3588 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 3589 diag::err_omp_wrong_ordered_loop_count) 3590 << OrderedLoopCountExpr->getSourceRange(); 3591 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 3592 diag::note_collapse_loop_count) 3593 << CollapseLoopCountExpr->getSourceRange(); 3594 } 3595 NestedLoopCount = Result.getLimitedValue(); 3596 } 3597 } 3598 // This is helper routine for loop directives (e.g., 'for', 'simd', 3599 // 'for simd', etc.). 3600 SmallVector<LoopIterationSpace, 4> IterSpaces; 3601 IterSpaces.resize(NestedLoopCount); 3602 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); 3603 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 3604 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt, 3605 NestedLoopCount, CollapseLoopCountExpr, 3606 OrderedLoopCountExpr, VarsWithImplicitDSA, 3607 IterSpaces[Cnt])) 3608 return 0; 3609 // Move on to the next nested for loop, or to the loop body. 3610 // OpenMP [2.8.1, simd construct, Restrictions] 3611 // All loops associated with the construct must be perfectly nested; that 3612 // is, there must be no intervening code nor any OpenMP directive between 3613 // any two loops. 3614 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); 3615 } 3616 3617 Built.clear(/* size */ NestedLoopCount); 3618 3619 if (SemaRef.CurContext->isDependentContext()) 3620 return NestedLoopCount; 3621 3622 // An example of what is generated for the following code: 3623 // 3624 // #pragma omp simd collapse(2) ordered(2) 3625 // for (i = 0; i < NI; ++i) 3626 // for (k = 0; k < NK; ++k) 3627 // for (j = J0; j < NJ; j+=2) { 3628 // <loop body> 3629 // } 3630 // 3631 // We generate the code below. 3632 // Note: the loop body may be outlined in CodeGen. 3633 // Note: some counters may be C++ classes, operator- is used to find number of 3634 // iterations and operator+= to calculate counter value. 3635 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 3636 // or i64 is currently supported). 3637 // 3638 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 3639 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 3640 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 3641 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 3642 // // similar updates for vars in clauses (e.g. 'linear') 3643 // <loop body (using local i and j)> 3644 // } 3645 // i = NI; // assign final values of counters 3646 // j = NJ; 3647 // 3648 3649 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 3650 // the iteration counts of the collapsed for loops. 3651 // Precondition tests if there is at least one iteration (all conditions are 3652 // true). 3653 auto PreCond = ExprResult(IterSpaces[0].PreCond); 3654 auto N0 = IterSpaces[0].NumIterations; 3655 ExprResult LastIteration32 = WidenIterationCount( 3656 32 /* Bits */, SemaRef.PerformImplicitConversion( 3657 N0->IgnoreImpCasts(), N0->getType(), 3658 Sema::AA_Converting, /*AllowExplicit=*/true) 3659 .get(), 3660 SemaRef); 3661 ExprResult LastIteration64 = WidenIterationCount( 3662 64 /* Bits */, SemaRef.PerformImplicitConversion( 3663 N0->IgnoreImpCasts(), N0->getType(), 3664 Sema::AA_Converting, /*AllowExplicit=*/true) 3665 .get(), 3666 SemaRef); 3667 3668 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 3669 return NestedLoopCount; 3670 3671 auto &C = SemaRef.Context; 3672 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 3673 3674 Scope *CurScope = DSA.getCurScope(); 3675 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 3676 if (PreCond.isUsable()) { 3677 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd, 3678 PreCond.get(), IterSpaces[Cnt].PreCond); 3679 } 3680 auto N = IterSpaces[Cnt].NumIterations; 3681 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 3682 if (LastIteration32.isUsable()) 3683 LastIteration32 = SemaRef.BuildBinOp( 3684 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(), 3685 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 3686 Sema::AA_Converting, 3687 /*AllowExplicit=*/true) 3688 .get()); 3689 if (LastIteration64.isUsable()) 3690 LastIteration64 = SemaRef.BuildBinOp( 3691 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(), 3692 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 3693 Sema::AA_Converting, 3694 /*AllowExplicit=*/true) 3695 .get()); 3696 } 3697 3698 // Choose either the 32-bit or 64-bit version. 3699 ExprResult LastIteration = LastIteration64; 3700 if (LastIteration32.isUsable() && 3701 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 3702 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 3703 FitsInto( 3704 32 /* Bits */, 3705 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 3706 LastIteration64.get(), SemaRef))) 3707 LastIteration = LastIteration32; 3708 3709 if (!LastIteration.isUsable()) 3710 return 0; 3711 3712 // Save the number of iterations. 3713 ExprResult NumIterations = LastIteration; 3714 { 3715 LastIteration = SemaRef.BuildBinOp( 3716 CurScope, SourceLocation(), BO_Sub, LastIteration.get(), 3717 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 3718 if (!LastIteration.isUsable()) 3719 return 0; 3720 } 3721 3722 // Calculate the last iteration number beforehand instead of doing this on 3723 // each iteration. Do not do this if the number of iterations may be kfold-ed. 3724 llvm::APSInt Result; 3725 bool IsConstant = 3726 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); 3727 ExprResult CalcLastIteration; 3728 if (!IsConstant) { 3729 SourceLocation SaveLoc; 3730 VarDecl *SaveVar = 3731 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(), 3732 ".omp.last.iteration"); 3733 ExprResult SaveRef = buildDeclRefExpr( 3734 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc); 3735 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign, 3736 SaveRef.get(), LastIteration.get()); 3737 LastIteration = SaveRef; 3738 3739 // Prepare SaveRef + 1. 3740 NumIterations = SemaRef.BuildBinOp( 3741 CurScope, SaveLoc, BO_Add, SaveRef.get(), 3742 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 3743 if (!NumIterations.isUsable()) 3744 return 0; 3745 } 3746 3747 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 3748 3749 QualType VType = LastIteration.get()->getType(); 3750 // Build variables passed into runtime, nesessary for worksharing directives. 3751 ExprResult LB, UB, IL, ST, EUB; 3752 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind)) { 3753 // Lower bound variable, initialized with zero. 3754 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 3755 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 3756 SemaRef.AddInitializerToDecl( 3757 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 3758 /*DirectInit*/ false, /*TypeMayContainAuto*/ false); 3759 3760 // Upper bound variable, initialized with last iteration number. 3761 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 3762 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 3763 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 3764 /*DirectInit*/ false, 3765 /*TypeMayContainAuto*/ false); 3766 3767 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 3768 // This will be used to implement clause 'lastprivate'. 3769 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 3770 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 3771 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 3772 SemaRef.AddInitializerToDecl( 3773 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 3774 /*DirectInit*/ false, /*TypeMayContainAuto*/ false); 3775 3776 // Stride variable returned by runtime (we initialize it to 1 by default). 3777 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride"); 3778 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc); 3779 SemaRef.AddInitializerToDecl( 3780 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 3781 /*DirectInit*/ false, /*TypeMayContainAuto*/ false); 3782 3783 // Build expression: UB = min(UB, LastIteration) 3784 // It is nesessary for CodeGen of directives with static scheduling. 3785 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 3786 UB.get(), LastIteration.get()); 3787 ExprResult CondOp = SemaRef.ActOnConditionalOp( 3788 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get()); 3789 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 3790 CondOp.get()); 3791 EUB = SemaRef.ActOnFinishFullExpr(EUB.get()); 3792 } 3793 3794 // Build the iteration variable and its initialization before loop. 3795 ExprResult IV; 3796 ExprResult Init; 3797 { 3798 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv"); 3799 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc); 3800 Expr *RHS = (isOpenMPWorksharingDirective(DKind) || 3801 isOpenMPTaskLoopDirective(DKind)) 3802 ? LB.get() 3803 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 3804 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 3805 Init = SemaRef.ActOnFinishFullExpr(Init.get()); 3806 } 3807 3808 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops. 3809 SourceLocation CondLoc; 3810 ExprResult Cond = 3811 (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind)) 3812 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get()) 3813 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 3814 NumIterations.get()); 3815 3816 // Loop increment (IV = IV + 1) 3817 SourceLocation IncLoc; 3818 ExprResult Inc = 3819 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 3820 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 3821 if (!Inc.isUsable()) 3822 return 0; 3823 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 3824 Inc = SemaRef.ActOnFinishFullExpr(Inc.get()); 3825 if (!Inc.isUsable()) 3826 return 0; 3827 3828 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 3829 // Used for directives with static scheduling. 3830 ExprResult NextLB, NextUB; 3831 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind)) { 3832 // LB + ST 3833 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 3834 if (!NextLB.isUsable()) 3835 return 0; 3836 // LB = LB + ST 3837 NextLB = 3838 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 3839 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get()); 3840 if (!NextLB.isUsable()) 3841 return 0; 3842 // UB + ST 3843 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 3844 if (!NextUB.isUsable()) 3845 return 0; 3846 // UB = UB + ST 3847 NextUB = 3848 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 3849 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get()); 3850 if (!NextUB.isUsable()) 3851 return 0; 3852 } 3853 3854 // Build updates and final values of the loop counters. 3855 bool HasErrors = false; 3856 Built.Counters.resize(NestedLoopCount); 3857 Built.Inits.resize(NestedLoopCount); 3858 Built.Updates.resize(NestedLoopCount); 3859 Built.Finals.resize(NestedLoopCount); 3860 { 3861 ExprResult Div; 3862 // Go from inner nested loop to outer. 3863 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) { 3864 LoopIterationSpace &IS = IterSpaces[Cnt]; 3865 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 3866 // Build: Iter = (IV / Div) % IS.NumIters 3867 // where Div is product of previous iterations' IS.NumIters. 3868 ExprResult Iter; 3869 if (Div.isUsable()) { 3870 Iter = 3871 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get()); 3872 } else { 3873 Iter = IV; 3874 assert((Cnt == (int)NestedLoopCount - 1) && 3875 "unusable div expected on first iteration only"); 3876 } 3877 3878 if (Cnt != 0 && Iter.isUsable()) 3879 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(), 3880 IS.NumIterations); 3881 if (!Iter.isUsable()) { 3882 HasErrors = true; 3883 break; 3884 } 3885 3886 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 3887 auto *CounterVar = buildDeclRefExpr( 3888 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()), 3889 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 3890 /*RefersToCapture=*/true); 3891 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 3892 IS.CounterInit); 3893 if (!Init.isUsable()) { 3894 HasErrors = true; 3895 break; 3896 } 3897 ExprResult Update = 3898 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar, 3899 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract); 3900 if (!Update.isUsable()) { 3901 HasErrors = true; 3902 break; 3903 } 3904 3905 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 3906 ExprResult Final = BuildCounterUpdate( 3907 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, 3908 IS.NumIterations, IS.CounterStep, IS.Subtract); 3909 if (!Final.isUsable()) { 3910 HasErrors = true; 3911 break; 3912 } 3913 3914 // Build Div for the next iteration: Div <- Div * IS.NumIters 3915 if (Cnt != 0) { 3916 if (Div.isUnset()) 3917 Div = IS.NumIterations; 3918 else 3919 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(), 3920 IS.NumIterations); 3921 3922 // Add parentheses (for debugging purposes only). 3923 if (Div.isUsable()) 3924 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get()); 3925 if (!Div.isUsable()) { 3926 HasErrors = true; 3927 break; 3928 } 3929 } 3930 if (!Update.isUsable() || !Final.isUsable()) { 3931 HasErrors = true; 3932 break; 3933 } 3934 // Save results 3935 Built.Counters[Cnt] = IS.CounterVar; 3936 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 3937 Built.Inits[Cnt] = Init.get(); 3938 Built.Updates[Cnt] = Update.get(); 3939 Built.Finals[Cnt] = Final.get(); 3940 } 3941 } 3942 3943 if (HasErrors) 3944 return 0; 3945 3946 // Save results 3947 Built.IterationVarRef = IV.get(); 3948 Built.LastIteration = LastIteration.get(); 3949 Built.NumIterations = NumIterations.get(); 3950 Built.CalcLastIteration = 3951 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get(); 3952 Built.PreCond = PreCond.get(); 3953 Built.Cond = Cond.get(); 3954 Built.Init = Init.get(); 3955 Built.Inc = Inc.get(); 3956 Built.LB = LB.get(); 3957 Built.UB = UB.get(); 3958 Built.IL = IL.get(); 3959 Built.ST = ST.get(); 3960 Built.EUB = EUB.get(); 3961 Built.NLB = NextLB.get(); 3962 Built.NUB = NextUB.get(); 3963 3964 return NestedLoopCount; 3965 } 3966 3967 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 3968 auto CollapseClauses = 3969 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 3970 if (CollapseClauses.begin() != CollapseClauses.end()) 3971 return (*CollapseClauses.begin())->getNumForLoops(); 3972 return nullptr; 3973 } 3974 3975 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 3976 auto OrderedClauses = 3977 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 3978 if (OrderedClauses.begin() != OrderedClauses.end()) 3979 return (*OrderedClauses.begin())->getNumForLoops(); 3980 return nullptr; 3981 } 3982 3983 static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen, 3984 const Expr *Safelen) { 3985 llvm::APSInt SimdlenRes, SafelenRes; 3986 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() || 3987 Simdlen->isInstantiationDependent() || 3988 Simdlen->containsUnexpandedParameterPack()) 3989 return false; 3990 if (Safelen->isValueDependent() || Safelen->isTypeDependent() || 3991 Safelen->isInstantiationDependent() || 3992 Safelen->containsUnexpandedParameterPack()) 3993 return false; 3994 Simdlen->EvaluateAsInt(SimdlenRes, S.Context); 3995 Safelen->EvaluateAsInt(SafelenRes, S.Context); 3996 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions] 3997 // If both simdlen and safelen clauses are specified, the value of the simdlen 3998 // parameter must be less than or equal to the value of the safelen parameter. 3999 if (SimdlenRes > SafelenRes) { 4000 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values) 4001 << Simdlen->getSourceRange() << Safelen->getSourceRange(); 4002 return true; 4003 } 4004 return false; 4005 } 4006 4007 StmtResult Sema::ActOnOpenMPSimdDirective( 4008 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 4009 SourceLocation EndLoc, 4010 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { 4011 if (!AStmt) 4012 return StmtError(); 4013 4014 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4015 OMPLoopDirective::HelperExprs B; 4016 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 4017 // define the nested loops number. 4018 unsigned NestedLoopCount = CheckOpenMPLoop( 4019 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 4020 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 4021 if (NestedLoopCount == 0) 4022 return StmtError(); 4023 4024 assert((CurContext->isDependentContext() || B.builtAll()) && 4025 "omp simd loop exprs were not built"); 4026 4027 if (!CurContext->isDependentContext()) { 4028 // Finalize the clauses that need pre-built expressions for CodeGen. 4029 for (auto C : Clauses) { 4030 if (auto LC = dyn_cast<OMPLinearClause>(C)) 4031 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 4032 B.NumIterations, *this, CurScope)) 4033 return StmtError(); 4034 } 4035 } 4036 4037 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions] 4038 // If both simdlen and safelen clauses are specified, the value of the simdlen 4039 // parameter must be less than or equal to the value of the safelen parameter. 4040 OMPSafelenClause *Safelen = nullptr; 4041 OMPSimdlenClause *Simdlen = nullptr; 4042 for (auto *Clause : Clauses) { 4043 if (Clause->getClauseKind() == OMPC_safelen) 4044 Safelen = cast<OMPSafelenClause>(Clause); 4045 else if (Clause->getClauseKind() == OMPC_simdlen) 4046 Simdlen = cast<OMPSimdlenClause>(Clause); 4047 if (Safelen && Simdlen) 4048 break; 4049 } 4050 if (Simdlen && Safelen && 4051 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(), 4052 Safelen->getSafelen())) 4053 return StmtError(); 4054 4055 getCurFunction()->setHasBranchProtectedScope(); 4056 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 4057 Clauses, AStmt, B); 4058 } 4059 4060 StmtResult Sema::ActOnOpenMPForDirective( 4061 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 4062 SourceLocation EndLoc, 4063 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { 4064 if (!AStmt) 4065 return StmtError(); 4066 4067 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4068 OMPLoopDirective::HelperExprs B; 4069 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 4070 // define the nested loops number. 4071 unsigned NestedLoopCount = CheckOpenMPLoop( 4072 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 4073 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 4074 if (NestedLoopCount == 0) 4075 return StmtError(); 4076 4077 assert((CurContext->isDependentContext() || B.builtAll()) && 4078 "omp for loop exprs were not built"); 4079 4080 if (!CurContext->isDependentContext()) { 4081 // Finalize the clauses that need pre-built expressions for CodeGen. 4082 for (auto C : Clauses) { 4083 if (auto LC = dyn_cast<OMPLinearClause>(C)) 4084 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 4085 B.NumIterations, *this, CurScope)) 4086 return StmtError(); 4087 } 4088 } 4089 4090 getCurFunction()->setHasBranchProtectedScope(); 4091 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 4092 Clauses, AStmt, B, DSAStack->isCancelRegion()); 4093 } 4094 4095 StmtResult Sema::ActOnOpenMPForSimdDirective( 4096 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 4097 SourceLocation EndLoc, 4098 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { 4099 if (!AStmt) 4100 return StmtError(); 4101 4102 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4103 OMPLoopDirective::HelperExprs B; 4104 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 4105 // define the nested loops number. 4106 unsigned NestedLoopCount = 4107 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 4108 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 4109 VarsWithImplicitDSA, B); 4110 if (NestedLoopCount == 0) 4111 return StmtError(); 4112 4113 assert((CurContext->isDependentContext() || B.builtAll()) && 4114 "omp for simd loop exprs were not built"); 4115 4116 if (!CurContext->isDependentContext()) { 4117 // Finalize the clauses that need pre-built expressions for CodeGen. 4118 for (auto C : Clauses) { 4119 if (auto LC = dyn_cast<OMPLinearClause>(C)) 4120 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 4121 B.NumIterations, *this, CurScope)) 4122 return StmtError(); 4123 } 4124 } 4125 4126 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions] 4127 // If both simdlen and safelen clauses are specified, the value of the simdlen 4128 // parameter must be less than or equal to the value of the safelen parameter. 4129 OMPSafelenClause *Safelen = nullptr; 4130 OMPSimdlenClause *Simdlen = nullptr; 4131 for (auto *Clause : Clauses) { 4132 if (Clause->getClauseKind() == OMPC_safelen) 4133 Safelen = cast<OMPSafelenClause>(Clause); 4134 else if (Clause->getClauseKind() == OMPC_simdlen) 4135 Simdlen = cast<OMPSimdlenClause>(Clause); 4136 if (Safelen && Simdlen) 4137 break; 4138 } 4139 if (Simdlen && Safelen && 4140 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(), 4141 Safelen->getSafelen())) 4142 return StmtError(); 4143 4144 getCurFunction()->setHasBranchProtectedScope(); 4145 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 4146 Clauses, AStmt, B); 4147 } 4148 4149 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 4150 Stmt *AStmt, 4151 SourceLocation StartLoc, 4152 SourceLocation EndLoc) { 4153 if (!AStmt) 4154 return StmtError(); 4155 4156 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4157 auto BaseStmt = AStmt; 4158 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 4159 BaseStmt = CS->getCapturedStmt(); 4160 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 4161 auto S = C->children(); 4162 if (S.begin() == S.end()) 4163 return StmtError(); 4164 // All associated statements must be '#pragma omp section' except for 4165 // the first one. 4166 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 4167 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 4168 if (SectionStmt) 4169 Diag(SectionStmt->getLocStart(), 4170 diag::err_omp_sections_substmt_not_section); 4171 return StmtError(); 4172 } 4173 cast<OMPSectionDirective>(SectionStmt) 4174 ->setHasCancel(DSAStack->isCancelRegion()); 4175 } 4176 } else { 4177 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt); 4178 return StmtError(); 4179 } 4180 4181 getCurFunction()->setHasBranchProtectedScope(); 4182 4183 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 4184 DSAStack->isCancelRegion()); 4185 } 4186 4187 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 4188 SourceLocation StartLoc, 4189 SourceLocation EndLoc) { 4190 if (!AStmt) 4191 return StmtError(); 4192 4193 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4194 4195 getCurFunction()->setHasBranchProtectedScope(); 4196 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 4197 4198 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 4199 DSAStack->isCancelRegion()); 4200 } 4201 4202 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 4203 Stmt *AStmt, 4204 SourceLocation StartLoc, 4205 SourceLocation EndLoc) { 4206 if (!AStmt) 4207 return StmtError(); 4208 4209 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4210 4211 getCurFunction()->setHasBranchProtectedScope(); 4212 4213 // OpenMP [2.7.3, single Construct, Restrictions] 4214 // The copyprivate clause must not be used with the nowait clause. 4215 OMPClause *Nowait = nullptr; 4216 OMPClause *Copyprivate = nullptr; 4217 for (auto *Clause : Clauses) { 4218 if (Clause->getClauseKind() == OMPC_nowait) 4219 Nowait = Clause; 4220 else if (Clause->getClauseKind() == OMPC_copyprivate) 4221 Copyprivate = Clause; 4222 if (Copyprivate && Nowait) { 4223 Diag(Copyprivate->getLocStart(), 4224 diag::err_omp_single_copyprivate_with_nowait); 4225 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here); 4226 return StmtError(); 4227 } 4228 } 4229 4230 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 4231 } 4232 4233 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 4234 SourceLocation StartLoc, 4235 SourceLocation EndLoc) { 4236 if (!AStmt) 4237 return StmtError(); 4238 4239 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4240 4241 getCurFunction()->setHasBranchProtectedScope(); 4242 4243 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 4244 } 4245 4246 StmtResult 4247 Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName, 4248 Stmt *AStmt, SourceLocation StartLoc, 4249 SourceLocation EndLoc) { 4250 if (!AStmt) 4251 return StmtError(); 4252 4253 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4254 4255 getCurFunction()->setHasBranchProtectedScope(); 4256 4257 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 4258 AStmt); 4259 } 4260 4261 StmtResult Sema::ActOnOpenMPParallelForDirective( 4262 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 4263 SourceLocation EndLoc, 4264 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { 4265 if (!AStmt) 4266 return StmtError(); 4267 4268 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 4269 // 1.2.2 OpenMP Language Terminology 4270 // Structured block - An executable statement with a single entry at the 4271 // top and a single exit at the bottom. 4272 // The point of exit cannot be a branch out of the structured block. 4273 // longjmp() and throw() must not violate the entry/exit criteria. 4274 CS->getCapturedDecl()->setNothrow(); 4275 4276 OMPLoopDirective::HelperExprs B; 4277 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 4278 // define the nested loops number. 4279 unsigned NestedLoopCount = 4280 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 4281 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 4282 VarsWithImplicitDSA, B); 4283 if (NestedLoopCount == 0) 4284 return StmtError(); 4285 4286 assert((CurContext->isDependentContext() || B.builtAll()) && 4287 "omp parallel for loop exprs were not built"); 4288 4289 if (!CurContext->isDependentContext()) { 4290 // Finalize the clauses that need pre-built expressions for CodeGen. 4291 for (auto C : Clauses) { 4292 if (auto LC = dyn_cast<OMPLinearClause>(C)) 4293 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 4294 B.NumIterations, *this, CurScope)) 4295 return StmtError(); 4296 } 4297 } 4298 4299 getCurFunction()->setHasBranchProtectedScope(); 4300 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc, 4301 NestedLoopCount, Clauses, AStmt, B, 4302 DSAStack->isCancelRegion()); 4303 } 4304 4305 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 4306 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 4307 SourceLocation EndLoc, 4308 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { 4309 if (!AStmt) 4310 return StmtError(); 4311 4312 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 4313 // 1.2.2 OpenMP Language Terminology 4314 // Structured block - An executable statement with a single entry at the 4315 // top and a single exit at the bottom. 4316 // The point of exit cannot be a branch out of the structured block. 4317 // longjmp() and throw() must not violate the entry/exit criteria. 4318 CS->getCapturedDecl()->setNothrow(); 4319 4320 OMPLoopDirective::HelperExprs B; 4321 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 4322 // define the nested loops number. 4323 unsigned NestedLoopCount = 4324 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 4325 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 4326 VarsWithImplicitDSA, B); 4327 if (NestedLoopCount == 0) 4328 return StmtError(); 4329 4330 if (!CurContext->isDependentContext()) { 4331 // Finalize the clauses that need pre-built expressions for CodeGen. 4332 for (auto C : Clauses) { 4333 if (auto LC = dyn_cast<OMPLinearClause>(C)) 4334 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 4335 B.NumIterations, *this, CurScope)) 4336 return StmtError(); 4337 } 4338 } 4339 4340 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions] 4341 // If both simdlen and safelen clauses are specified, the value of the simdlen 4342 // parameter must be less than or equal to the value of the safelen parameter. 4343 OMPSafelenClause *Safelen = nullptr; 4344 OMPSimdlenClause *Simdlen = nullptr; 4345 for (auto *Clause : Clauses) { 4346 if (Clause->getClauseKind() == OMPC_safelen) 4347 Safelen = cast<OMPSafelenClause>(Clause); 4348 else if (Clause->getClauseKind() == OMPC_simdlen) 4349 Simdlen = cast<OMPSimdlenClause>(Clause); 4350 if (Safelen && Simdlen) 4351 break; 4352 } 4353 if (Simdlen && Safelen && 4354 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(), 4355 Safelen->getSafelen())) 4356 return StmtError(); 4357 4358 getCurFunction()->setHasBranchProtectedScope(); 4359 return OMPParallelForSimdDirective::Create( 4360 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 4361 } 4362 4363 StmtResult 4364 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 4365 Stmt *AStmt, SourceLocation StartLoc, 4366 SourceLocation EndLoc) { 4367 if (!AStmt) 4368 return StmtError(); 4369 4370 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4371 auto BaseStmt = AStmt; 4372 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 4373 BaseStmt = CS->getCapturedStmt(); 4374 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 4375 auto S = C->children(); 4376 if (S.begin() == S.end()) 4377 return StmtError(); 4378 // All associated statements must be '#pragma omp section' except for 4379 // the first one. 4380 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 4381 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 4382 if (SectionStmt) 4383 Diag(SectionStmt->getLocStart(), 4384 diag::err_omp_parallel_sections_substmt_not_section); 4385 return StmtError(); 4386 } 4387 cast<OMPSectionDirective>(SectionStmt) 4388 ->setHasCancel(DSAStack->isCancelRegion()); 4389 } 4390 } else { 4391 Diag(AStmt->getLocStart(), 4392 diag::err_omp_parallel_sections_not_compound_stmt); 4393 return StmtError(); 4394 } 4395 4396 getCurFunction()->setHasBranchProtectedScope(); 4397 4398 return OMPParallelSectionsDirective::Create( 4399 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion()); 4400 } 4401 4402 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 4403 Stmt *AStmt, SourceLocation StartLoc, 4404 SourceLocation EndLoc) { 4405 if (!AStmt) 4406 return StmtError(); 4407 4408 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 4409 // 1.2.2 OpenMP Language Terminology 4410 // Structured block - An executable statement with a single entry at the 4411 // top and a single exit at the bottom. 4412 // The point of exit cannot be a branch out of the structured block. 4413 // longjmp() and throw() must not violate the entry/exit criteria. 4414 CS->getCapturedDecl()->setNothrow(); 4415 4416 getCurFunction()->setHasBranchProtectedScope(); 4417 4418 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 4419 DSAStack->isCancelRegion()); 4420 } 4421 4422 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 4423 SourceLocation EndLoc) { 4424 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 4425 } 4426 4427 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 4428 SourceLocation EndLoc) { 4429 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 4430 } 4431 4432 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 4433 SourceLocation EndLoc) { 4434 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 4435 } 4436 4437 StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt, 4438 SourceLocation StartLoc, 4439 SourceLocation EndLoc) { 4440 if (!AStmt) 4441 return StmtError(); 4442 4443 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4444 4445 getCurFunction()->setHasBranchProtectedScope(); 4446 4447 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt); 4448 } 4449 4450 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 4451 SourceLocation StartLoc, 4452 SourceLocation EndLoc) { 4453 assert(Clauses.size() <= 1 && "Extra clauses in flush directive"); 4454 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 4455 } 4456 4457 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 4458 Stmt *AStmt, 4459 SourceLocation StartLoc, 4460 SourceLocation EndLoc) { 4461 if (!AStmt) 4462 return StmtError(); 4463 4464 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4465 4466 getCurFunction()->setHasBranchProtectedScope(); 4467 4468 OMPThreadsClause *TC = nullptr; 4469 OMPSIMDClause *SC = nullptr; 4470 for (auto *C: Clauses) { 4471 if (C->getClauseKind() == OMPC_threads) 4472 TC = cast<OMPThreadsClause>(C); 4473 else if (C->getClauseKind() == OMPC_simd) 4474 SC = cast<OMPSIMDClause>(C); 4475 } 4476 4477 // TODO: this must happen only if 'threads' clause specified or if no clauses 4478 // is specified. 4479 if (auto *Param = DSAStack->getParentOrderedRegionParam()) { 4480 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc; 4481 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) << (TC != nullptr); 4482 Diag(Param->getLocStart(), diag::note_omp_ordered_param); 4483 return StmtError(); 4484 } 4485 if (!SC && isOpenMPSimdDirective(DSAStack->getParentDirective())) { 4486 // OpenMP [2.8.1,simd Construct, Restrictions] 4487 // An ordered construct with the simd clause is the only OpenMP construct 4488 // that can appear in the simd region. 4489 Diag(StartLoc, diag::err_omp_prohibited_region_simd); 4490 return StmtError(); 4491 } 4492 4493 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 4494 } 4495 4496 namespace { 4497 /// \brief Helper class for checking expression in 'omp atomic [update]' 4498 /// construct. 4499 class OpenMPAtomicUpdateChecker { 4500 /// \brief Error results for atomic update expressions. 4501 enum ExprAnalysisErrorCode { 4502 /// \brief A statement is not an expression statement. 4503 NotAnExpression, 4504 /// \brief Expression is not builtin binary or unary operation. 4505 NotABinaryOrUnaryExpression, 4506 /// \brief Unary operation is not post-/pre- increment/decrement operation. 4507 NotAnUnaryIncDecExpression, 4508 /// \brief An expression is not of scalar type. 4509 NotAScalarType, 4510 /// \brief A binary operation is not an assignment operation. 4511 NotAnAssignmentOp, 4512 /// \brief RHS part of the binary operation is not a binary expression. 4513 NotABinaryExpression, 4514 /// \brief RHS part is not additive/multiplicative/shift/biwise binary 4515 /// expression. 4516 NotABinaryOperator, 4517 /// \brief RHS binary operation does not have reference to the updated LHS 4518 /// part. 4519 NotAnUpdateExpression, 4520 /// \brief No errors is found. 4521 NoError 4522 }; 4523 /// \brief Reference to Sema. 4524 Sema &SemaRef; 4525 /// \brief A location for note diagnostics (when error is found). 4526 SourceLocation NoteLoc; 4527 /// \brief 'x' lvalue part of the source atomic expression. 4528 Expr *X; 4529 /// \brief 'expr' rvalue part of the source atomic expression. 4530 Expr *E; 4531 /// \brief Helper expression of the form 4532 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 4533 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 4534 Expr *UpdateExpr; 4535 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is 4536 /// important for non-associative operations. 4537 bool IsXLHSInRHSPart; 4538 BinaryOperatorKind Op; 4539 SourceLocation OpLoc; 4540 /// \brief true if the source expression is a postfix unary operation, false 4541 /// if it is a prefix unary operation. 4542 bool IsPostfixUpdate; 4543 4544 public: 4545 OpenMPAtomicUpdateChecker(Sema &SemaRef) 4546 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 4547 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 4548 /// \brief Check specified statement that it is suitable for 'atomic update' 4549 /// constructs and extract 'x', 'expr' and Operation from the original 4550 /// expression. If DiagId and NoteId == 0, then only check is performed 4551 /// without error notification. 4552 /// \param DiagId Diagnostic which should be emitted if error is found. 4553 /// \param NoteId Diagnostic note for the main error message. 4554 /// \return true if statement is not an update expression, false otherwise. 4555 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 4556 /// \brief Return the 'x' lvalue part of the source atomic expression. 4557 Expr *getX() const { return X; } 4558 /// \brief Return the 'expr' rvalue part of the source atomic expression. 4559 Expr *getExpr() const { return E; } 4560 /// \brief Return the update expression used in calculation of the updated 4561 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 4562 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 4563 Expr *getUpdateExpr() const { return UpdateExpr; } 4564 /// \brief Return true if 'x' is LHS in RHS part of full update expression, 4565 /// false otherwise. 4566 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 4567 4568 /// \brief true if the source expression is a postfix unary operation, false 4569 /// if it is a prefix unary operation. 4570 bool isPostfixUpdate() const { return IsPostfixUpdate; } 4571 4572 private: 4573 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 4574 unsigned NoteId = 0); 4575 }; 4576 } // namespace 4577 4578 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 4579 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 4580 ExprAnalysisErrorCode ErrorFound = NoError; 4581 SourceLocation ErrorLoc, NoteLoc; 4582 SourceRange ErrorRange, NoteRange; 4583 // Allowed constructs are: 4584 // x = x binop expr; 4585 // x = expr binop x; 4586 if (AtomicBinOp->getOpcode() == BO_Assign) { 4587 X = AtomicBinOp->getLHS(); 4588 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 4589 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 4590 if (AtomicInnerBinOp->isMultiplicativeOp() || 4591 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 4592 AtomicInnerBinOp->isBitwiseOp()) { 4593 Op = AtomicInnerBinOp->getOpcode(); 4594 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 4595 auto *LHS = AtomicInnerBinOp->getLHS(); 4596 auto *RHS = AtomicInnerBinOp->getRHS(); 4597 llvm::FoldingSetNodeID XId, LHSId, RHSId; 4598 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 4599 /*Canonical=*/true); 4600 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 4601 /*Canonical=*/true); 4602 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 4603 /*Canonical=*/true); 4604 if (XId == LHSId) { 4605 E = RHS; 4606 IsXLHSInRHSPart = true; 4607 } else if (XId == RHSId) { 4608 E = LHS; 4609 IsXLHSInRHSPart = false; 4610 } else { 4611 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 4612 ErrorRange = AtomicInnerBinOp->getSourceRange(); 4613 NoteLoc = X->getExprLoc(); 4614 NoteRange = X->getSourceRange(); 4615 ErrorFound = NotAnUpdateExpression; 4616 } 4617 } else { 4618 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 4619 ErrorRange = AtomicInnerBinOp->getSourceRange(); 4620 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 4621 NoteRange = SourceRange(NoteLoc, NoteLoc); 4622 ErrorFound = NotABinaryOperator; 4623 } 4624 } else { 4625 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 4626 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 4627 ErrorFound = NotABinaryExpression; 4628 } 4629 } else { 4630 ErrorLoc = AtomicBinOp->getExprLoc(); 4631 ErrorRange = AtomicBinOp->getSourceRange(); 4632 NoteLoc = AtomicBinOp->getOperatorLoc(); 4633 NoteRange = SourceRange(NoteLoc, NoteLoc); 4634 ErrorFound = NotAnAssignmentOp; 4635 } 4636 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 4637 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 4638 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 4639 return true; 4640 } else if (SemaRef.CurContext->isDependentContext()) 4641 E = X = UpdateExpr = nullptr; 4642 return ErrorFound != NoError; 4643 } 4644 4645 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 4646 unsigned NoteId) { 4647 ExprAnalysisErrorCode ErrorFound = NoError; 4648 SourceLocation ErrorLoc, NoteLoc; 4649 SourceRange ErrorRange, NoteRange; 4650 // Allowed constructs are: 4651 // x++; 4652 // x--; 4653 // ++x; 4654 // --x; 4655 // x binop= expr; 4656 // x = x binop expr; 4657 // x = expr binop x; 4658 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 4659 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 4660 if (AtomicBody->getType()->isScalarType() || 4661 AtomicBody->isInstantiationDependent()) { 4662 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 4663 AtomicBody->IgnoreParenImpCasts())) { 4664 // Check for Compound Assignment Operation 4665 Op = BinaryOperator::getOpForCompoundAssignment( 4666 AtomicCompAssignOp->getOpcode()); 4667 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 4668 E = AtomicCompAssignOp->getRHS(); 4669 X = AtomicCompAssignOp->getLHS(); 4670 IsXLHSInRHSPart = true; 4671 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 4672 AtomicBody->IgnoreParenImpCasts())) { 4673 // Check for Binary Operation 4674 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 4675 return true; 4676 } else if (auto *AtomicUnaryOp = 4677 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) { 4678 // Check for Unary Operation 4679 if (AtomicUnaryOp->isIncrementDecrementOp()) { 4680 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 4681 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 4682 OpLoc = AtomicUnaryOp->getOperatorLoc(); 4683 X = AtomicUnaryOp->getSubExpr(); 4684 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 4685 IsXLHSInRHSPart = true; 4686 } else { 4687 ErrorFound = NotAnUnaryIncDecExpression; 4688 ErrorLoc = AtomicUnaryOp->getExprLoc(); 4689 ErrorRange = AtomicUnaryOp->getSourceRange(); 4690 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 4691 NoteRange = SourceRange(NoteLoc, NoteLoc); 4692 } 4693 } else if (!AtomicBody->isInstantiationDependent()) { 4694 ErrorFound = NotABinaryOrUnaryExpression; 4695 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 4696 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 4697 } 4698 } else { 4699 ErrorFound = NotAScalarType; 4700 NoteLoc = ErrorLoc = AtomicBody->getLocStart(); 4701 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 4702 } 4703 } else { 4704 ErrorFound = NotAnExpression; 4705 NoteLoc = ErrorLoc = S->getLocStart(); 4706 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 4707 } 4708 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 4709 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 4710 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 4711 return true; 4712 } else if (SemaRef.CurContext->isDependentContext()) 4713 E = X = UpdateExpr = nullptr; 4714 if (ErrorFound == NoError && E && X) { 4715 // Build an update expression of form 'OpaqueValueExpr(x) binop 4716 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 4717 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 4718 auto *OVEX = new (SemaRef.getASTContext()) 4719 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); 4720 auto *OVEExpr = new (SemaRef.getASTContext()) 4721 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); 4722 auto Update = 4723 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 4724 IsXLHSInRHSPart ? OVEExpr : OVEX); 4725 if (Update.isInvalid()) 4726 return true; 4727 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 4728 Sema::AA_Casting); 4729 if (Update.isInvalid()) 4730 return true; 4731 UpdateExpr = Update.get(); 4732 } 4733 return ErrorFound != NoError; 4734 } 4735 4736 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 4737 Stmt *AStmt, 4738 SourceLocation StartLoc, 4739 SourceLocation EndLoc) { 4740 if (!AStmt) 4741 return StmtError(); 4742 4743 auto CS = cast<CapturedStmt>(AStmt); 4744 // 1.2.2 OpenMP Language Terminology 4745 // Structured block - An executable statement with a single entry at the 4746 // top and a single exit at the bottom. 4747 // The point of exit cannot be a branch out of the structured block. 4748 // longjmp() and throw() must not violate the entry/exit criteria. 4749 OpenMPClauseKind AtomicKind = OMPC_unknown; 4750 SourceLocation AtomicKindLoc; 4751 for (auto *C : Clauses) { 4752 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 4753 C->getClauseKind() == OMPC_update || 4754 C->getClauseKind() == OMPC_capture) { 4755 if (AtomicKind != OMPC_unknown) { 4756 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses) 4757 << SourceRange(C->getLocStart(), C->getLocEnd()); 4758 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause) 4759 << getOpenMPClauseName(AtomicKind); 4760 } else { 4761 AtomicKind = C->getClauseKind(); 4762 AtomicKindLoc = C->getLocStart(); 4763 } 4764 } 4765 } 4766 4767 auto Body = CS->getCapturedStmt(); 4768 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 4769 Body = EWC->getSubExpr(); 4770 4771 Expr *X = nullptr; 4772 Expr *V = nullptr; 4773 Expr *E = nullptr; 4774 Expr *UE = nullptr; 4775 bool IsXLHSInRHSPart = false; 4776 bool IsPostfixUpdate = false; 4777 // OpenMP [2.12.6, atomic Construct] 4778 // In the next expressions: 4779 // * x and v (as applicable) are both l-value expressions with scalar type. 4780 // * During the execution of an atomic region, multiple syntactic 4781 // occurrences of x must designate the same storage location. 4782 // * Neither of v and expr (as applicable) may access the storage location 4783 // designated by x. 4784 // * Neither of x and expr (as applicable) may access the storage location 4785 // designated by v. 4786 // * expr is an expression with scalar type. 4787 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 4788 // * binop, binop=, ++, and -- are not overloaded operators. 4789 // * The expression x binop expr must be numerically equivalent to x binop 4790 // (expr). This requirement is satisfied if the operators in expr have 4791 // precedence greater than binop, or by using parentheses around expr or 4792 // subexpressions of expr. 4793 // * The expression expr binop x must be numerically equivalent to (expr) 4794 // binop x. This requirement is satisfied if the operators in expr have 4795 // precedence equal to or greater than binop, or by using parentheses around 4796 // expr or subexpressions of expr. 4797 // * For forms that allow multiple occurrences of x, the number of times 4798 // that x is evaluated is unspecified. 4799 if (AtomicKind == OMPC_read) { 4800 enum { 4801 NotAnExpression, 4802 NotAnAssignmentOp, 4803 NotAScalarType, 4804 NotAnLValue, 4805 NoError 4806 } ErrorFound = NoError; 4807 SourceLocation ErrorLoc, NoteLoc; 4808 SourceRange ErrorRange, NoteRange; 4809 // If clause is read: 4810 // v = x; 4811 if (auto AtomicBody = dyn_cast<Expr>(Body)) { 4812 auto AtomicBinOp = 4813 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 4814 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 4815 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 4816 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 4817 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 4818 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 4819 if (!X->isLValue() || !V->isLValue()) { 4820 auto NotLValueExpr = X->isLValue() ? V : X; 4821 ErrorFound = NotAnLValue; 4822 ErrorLoc = AtomicBinOp->getExprLoc(); 4823 ErrorRange = AtomicBinOp->getSourceRange(); 4824 NoteLoc = NotLValueExpr->getExprLoc(); 4825 NoteRange = NotLValueExpr->getSourceRange(); 4826 } 4827 } else if (!X->isInstantiationDependent() || 4828 !V->isInstantiationDependent()) { 4829 auto NotScalarExpr = 4830 (X->isInstantiationDependent() || X->getType()->isScalarType()) 4831 ? V 4832 : X; 4833 ErrorFound = NotAScalarType; 4834 ErrorLoc = AtomicBinOp->getExprLoc(); 4835 ErrorRange = AtomicBinOp->getSourceRange(); 4836 NoteLoc = NotScalarExpr->getExprLoc(); 4837 NoteRange = NotScalarExpr->getSourceRange(); 4838 } 4839 } else if (!AtomicBody->isInstantiationDependent()) { 4840 ErrorFound = NotAnAssignmentOp; 4841 ErrorLoc = AtomicBody->getExprLoc(); 4842 ErrorRange = AtomicBody->getSourceRange(); 4843 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 4844 : AtomicBody->getExprLoc(); 4845 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 4846 : AtomicBody->getSourceRange(); 4847 } 4848 } else { 4849 ErrorFound = NotAnExpression; 4850 NoteLoc = ErrorLoc = Body->getLocStart(); 4851 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 4852 } 4853 if (ErrorFound != NoError) { 4854 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 4855 << ErrorRange; 4856 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 4857 << NoteRange; 4858 return StmtError(); 4859 } else if (CurContext->isDependentContext()) 4860 V = X = nullptr; 4861 } else if (AtomicKind == OMPC_write) { 4862 enum { 4863 NotAnExpression, 4864 NotAnAssignmentOp, 4865 NotAScalarType, 4866 NotAnLValue, 4867 NoError 4868 } ErrorFound = NoError; 4869 SourceLocation ErrorLoc, NoteLoc; 4870 SourceRange ErrorRange, NoteRange; 4871 // If clause is write: 4872 // x = expr; 4873 if (auto AtomicBody = dyn_cast<Expr>(Body)) { 4874 auto AtomicBinOp = 4875 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 4876 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 4877 X = AtomicBinOp->getLHS(); 4878 E = AtomicBinOp->getRHS(); 4879 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 4880 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 4881 if (!X->isLValue()) { 4882 ErrorFound = NotAnLValue; 4883 ErrorLoc = AtomicBinOp->getExprLoc(); 4884 ErrorRange = AtomicBinOp->getSourceRange(); 4885 NoteLoc = X->getExprLoc(); 4886 NoteRange = X->getSourceRange(); 4887 } 4888 } else if (!X->isInstantiationDependent() || 4889 !E->isInstantiationDependent()) { 4890 auto NotScalarExpr = 4891 (X->isInstantiationDependent() || X->getType()->isScalarType()) 4892 ? E 4893 : X; 4894 ErrorFound = NotAScalarType; 4895 ErrorLoc = AtomicBinOp->getExprLoc(); 4896 ErrorRange = AtomicBinOp->getSourceRange(); 4897 NoteLoc = NotScalarExpr->getExprLoc(); 4898 NoteRange = NotScalarExpr->getSourceRange(); 4899 } 4900 } else if (!AtomicBody->isInstantiationDependent()) { 4901 ErrorFound = NotAnAssignmentOp; 4902 ErrorLoc = AtomicBody->getExprLoc(); 4903 ErrorRange = AtomicBody->getSourceRange(); 4904 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 4905 : AtomicBody->getExprLoc(); 4906 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 4907 : AtomicBody->getSourceRange(); 4908 } 4909 } else { 4910 ErrorFound = NotAnExpression; 4911 NoteLoc = ErrorLoc = Body->getLocStart(); 4912 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 4913 } 4914 if (ErrorFound != NoError) { 4915 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 4916 << ErrorRange; 4917 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 4918 << NoteRange; 4919 return StmtError(); 4920 } else if (CurContext->isDependentContext()) 4921 E = X = nullptr; 4922 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 4923 // If clause is update: 4924 // x++; 4925 // x--; 4926 // ++x; 4927 // --x; 4928 // x binop= expr; 4929 // x = x binop expr; 4930 // x = expr binop x; 4931 OpenMPAtomicUpdateChecker Checker(*this); 4932 if (Checker.checkStatement( 4933 Body, (AtomicKind == OMPC_update) 4934 ? diag::err_omp_atomic_update_not_expression_statement 4935 : diag::err_omp_atomic_not_expression_statement, 4936 diag::note_omp_atomic_update)) 4937 return StmtError(); 4938 if (!CurContext->isDependentContext()) { 4939 E = Checker.getExpr(); 4940 X = Checker.getX(); 4941 UE = Checker.getUpdateExpr(); 4942 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 4943 } 4944 } else if (AtomicKind == OMPC_capture) { 4945 enum { 4946 NotAnAssignmentOp, 4947 NotACompoundStatement, 4948 NotTwoSubstatements, 4949 NotASpecificExpression, 4950 NoError 4951 } ErrorFound = NoError; 4952 SourceLocation ErrorLoc, NoteLoc; 4953 SourceRange ErrorRange, NoteRange; 4954 if (auto *AtomicBody = dyn_cast<Expr>(Body)) { 4955 // If clause is a capture: 4956 // v = x++; 4957 // v = x--; 4958 // v = ++x; 4959 // v = --x; 4960 // v = x binop= expr; 4961 // v = x = x binop expr; 4962 // v = x = expr binop x; 4963 auto *AtomicBinOp = 4964 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 4965 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 4966 V = AtomicBinOp->getLHS(); 4967 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 4968 OpenMPAtomicUpdateChecker Checker(*this); 4969 if (Checker.checkStatement( 4970 Body, diag::err_omp_atomic_capture_not_expression_statement, 4971 diag::note_omp_atomic_update)) 4972 return StmtError(); 4973 E = Checker.getExpr(); 4974 X = Checker.getX(); 4975 UE = Checker.getUpdateExpr(); 4976 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 4977 IsPostfixUpdate = Checker.isPostfixUpdate(); 4978 } else if (!AtomicBody->isInstantiationDependent()) { 4979 ErrorLoc = AtomicBody->getExprLoc(); 4980 ErrorRange = AtomicBody->getSourceRange(); 4981 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 4982 : AtomicBody->getExprLoc(); 4983 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 4984 : AtomicBody->getSourceRange(); 4985 ErrorFound = NotAnAssignmentOp; 4986 } 4987 if (ErrorFound != NoError) { 4988 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 4989 << ErrorRange; 4990 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 4991 return StmtError(); 4992 } else if (CurContext->isDependentContext()) { 4993 UE = V = E = X = nullptr; 4994 } 4995 } else { 4996 // If clause is a capture: 4997 // { v = x; x = expr; } 4998 // { v = x; x++; } 4999 // { v = x; x--; } 5000 // { v = x; ++x; } 5001 // { v = x; --x; } 5002 // { v = x; x binop= expr; } 5003 // { v = x; x = x binop expr; } 5004 // { v = x; x = expr binop x; } 5005 // { x++; v = x; } 5006 // { x--; v = x; } 5007 // { ++x; v = x; } 5008 // { --x; v = x; } 5009 // { x binop= expr; v = x; } 5010 // { x = x binop expr; v = x; } 5011 // { x = expr binop x; v = x; } 5012 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 5013 // Check that this is { expr1; expr2; } 5014 if (CS->size() == 2) { 5015 auto *First = CS->body_front(); 5016 auto *Second = CS->body_back(); 5017 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 5018 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 5019 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 5020 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 5021 // Need to find what subexpression is 'v' and what is 'x'. 5022 OpenMPAtomicUpdateChecker Checker(*this); 5023 bool IsUpdateExprFound = !Checker.checkStatement(Second); 5024 BinaryOperator *BinOp = nullptr; 5025 if (IsUpdateExprFound) { 5026 BinOp = dyn_cast<BinaryOperator>(First); 5027 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 5028 } 5029 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 5030 // { v = x; x++; } 5031 // { v = x; x--; } 5032 // { v = x; ++x; } 5033 // { v = x; --x; } 5034 // { v = x; x binop= expr; } 5035 // { v = x; x = x binop expr; } 5036 // { v = x; x = expr binop x; } 5037 // Check that the first expression has form v = x. 5038 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 5039 llvm::FoldingSetNodeID XId, PossibleXId; 5040 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 5041 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 5042 IsUpdateExprFound = XId == PossibleXId; 5043 if (IsUpdateExprFound) { 5044 V = BinOp->getLHS(); 5045 X = Checker.getX(); 5046 E = Checker.getExpr(); 5047 UE = Checker.getUpdateExpr(); 5048 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 5049 IsPostfixUpdate = true; 5050 } 5051 } 5052 if (!IsUpdateExprFound) { 5053 IsUpdateExprFound = !Checker.checkStatement(First); 5054 BinOp = nullptr; 5055 if (IsUpdateExprFound) { 5056 BinOp = dyn_cast<BinaryOperator>(Second); 5057 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 5058 } 5059 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 5060 // { x++; v = x; } 5061 // { x--; v = x; } 5062 // { ++x; v = x; } 5063 // { --x; v = x; } 5064 // { x binop= expr; v = x; } 5065 // { x = x binop expr; v = x; } 5066 // { x = expr binop x; v = x; } 5067 // Check that the second expression has form v = x. 5068 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 5069 llvm::FoldingSetNodeID XId, PossibleXId; 5070 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 5071 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 5072 IsUpdateExprFound = XId == PossibleXId; 5073 if (IsUpdateExprFound) { 5074 V = BinOp->getLHS(); 5075 X = Checker.getX(); 5076 E = Checker.getExpr(); 5077 UE = Checker.getUpdateExpr(); 5078 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 5079 IsPostfixUpdate = false; 5080 } 5081 } 5082 } 5083 if (!IsUpdateExprFound) { 5084 // { v = x; x = expr; } 5085 auto *FirstExpr = dyn_cast<Expr>(First); 5086 auto *SecondExpr = dyn_cast<Expr>(Second); 5087 if (!FirstExpr || !SecondExpr || 5088 !(FirstExpr->isInstantiationDependent() || 5089 SecondExpr->isInstantiationDependent())) { 5090 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 5091 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 5092 ErrorFound = NotAnAssignmentOp; 5093 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 5094 : First->getLocStart(); 5095 NoteRange = ErrorRange = FirstBinOp 5096 ? FirstBinOp->getSourceRange() 5097 : SourceRange(ErrorLoc, ErrorLoc); 5098 } else { 5099 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 5100 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 5101 ErrorFound = NotAnAssignmentOp; 5102 NoteLoc = ErrorLoc = SecondBinOp 5103 ? SecondBinOp->getOperatorLoc() 5104 : Second->getLocStart(); 5105 NoteRange = ErrorRange = 5106 SecondBinOp ? SecondBinOp->getSourceRange() 5107 : SourceRange(ErrorLoc, ErrorLoc); 5108 } else { 5109 auto *PossibleXRHSInFirst = 5110 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 5111 auto *PossibleXLHSInSecond = 5112 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 5113 llvm::FoldingSetNodeID X1Id, X2Id; 5114 PossibleXRHSInFirst->Profile(X1Id, Context, 5115 /*Canonical=*/true); 5116 PossibleXLHSInSecond->Profile(X2Id, Context, 5117 /*Canonical=*/true); 5118 IsUpdateExprFound = X1Id == X2Id; 5119 if (IsUpdateExprFound) { 5120 V = FirstBinOp->getLHS(); 5121 X = SecondBinOp->getLHS(); 5122 E = SecondBinOp->getRHS(); 5123 UE = nullptr; 5124 IsXLHSInRHSPart = false; 5125 IsPostfixUpdate = true; 5126 } else { 5127 ErrorFound = NotASpecificExpression; 5128 ErrorLoc = FirstBinOp->getExprLoc(); 5129 ErrorRange = FirstBinOp->getSourceRange(); 5130 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 5131 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 5132 } 5133 } 5134 } 5135 } 5136 } 5137 } else { 5138 NoteLoc = ErrorLoc = Body->getLocStart(); 5139 NoteRange = ErrorRange = 5140 SourceRange(Body->getLocStart(), Body->getLocStart()); 5141 ErrorFound = NotTwoSubstatements; 5142 } 5143 } else { 5144 NoteLoc = ErrorLoc = Body->getLocStart(); 5145 NoteRange = ErrorRange = 5146 SourceRange(Body->getLocStart(), Body->getLocStart()); 5147 ErrorFound = NotACompoundStatement; 5148 } 5149 if (ErrorFound != NoError) { 5150 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 5151 << ErrorRange; 5152 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 5153 return StmtError(); 5154 } else if (CurContext->isDependentContext()) { 5155 UE = V = E = X = nullptr; 5156 } 5157 } 5158 } 5159 5160 getCurFunction()->setHasBranchProtectedScope(); 5161 5162 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 5163 X, V, E, UE, IsXLHSInRHSPart, 5164 IsPostfixUpdate); 5165 } 5166 5167 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 5168 Stmt *AStmt, 5169 SourceLocation StartLoc, 5170 SourceLocation EndLoc) { 5171 if (!AStmt) 5172 return StmtError(); 5173 5174 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 5175 // 1.2.2 OpenMP Language Terminology 5176 // Structured block - An executable statement with a single entry at the 5177 // top and a single exit at the bottom. 5178 // The point of exit cannot be a branch out of the structured block. 5179 // longjmp() and throw() must not violate the entry/exit criteria. 5180 CS->getCapturedDecl()->setNothrow(); 5181 5182 // OpenMP [2.16, Nesting of Regions] 5183 // If specified, a teams construct must be contained within a target 5184 // construct. That target construct must contain no statements or directives 5185 // outside of the teams construct. 5186 if (DSAStack->hasInnerTeamsRegion()) { 5187 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true); 5188 bool OMPTeamsFound = true; 5189 if (auto *CS = dyn_cast<CompoundStmt>(S)) { 5190 auto I = CS->body_begin(); 5191 while (I != CS->body_end()) { 5192 auto OED = dyn_cast<OMPExecutableDirective>(*I); 5193 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) { 5194 OMPTeamsFound = false; 5195 break; 5196 } 5197 ++I; 5198 } 5199 assert(I != CS->body_end() && "Not found statement"); 5200 S = *I; 5201 } 5202 if (!OMPTeamsFound) { 5203 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 5204 Diag(DSAStack->getInnerTeamsRegionLoc(), 5205 diag::note_omp_nested_teams_construct_here); 5206 Diag(S->getLocStart(), diag::note_omp_nested_statement_here) 5207 << isa<OMPExecutableDirective>(S); 5208 return StmtError(); 5209 } 5210 } 5211 5212 getCurFunction()->setHasBranchProtectedScope(); 5213 5214 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 5215 } 5216 5217 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 5218 Stmt *AStmt, 5219 SourceLocation StartLoc, 5220 SourceLocation EndLoc) { 5221 if (!AStmt) 5222 return StmtError(); 5223 5224 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5225 5226 getCurFunction()->setHasBranchProtectedScope(); 5227 5228 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 5229 AStmt); 5230 } 5231 5232 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 5233 Stmt *AStmt, SourceLocation StartLoc, 5234 SourceLocation EndLoc) { 5235 if (!AStmt) 5236 return StmtError(); 5237 5238 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 5239 // 1.2.2 OpenMP Language Terminology 5240 // Structured block - An executable statement with a single entry at the 5241 // top and a single exit at the bottom. 5242 // The point of exit cannot be a branch out of the structured block. 5243 // longjmp() and throw() must not violate the entry/exit criteria. 5244 CS->getCapturedDecl()->setNothrow(); 5245 5246 getCurFunction()->setHasBranchProtectedScope(); 5247 5248 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 5249 } 5250 5251 StmtResult 5252 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 5253 SourceLocation EndLoc, 5254 OpenMPDirectiveKind CancelRegion) { 5255 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for && 5256 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) { 5257 Diag(StartLoc, diag::err_omp_wrong_cancel_region) 5258 << getOpenMPDirectiveName(CancelRegion); 5259 return StmtError(); 5260 } 5261 if (DSAStack->isParentNowaitRegion()) { 5262 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 5263 return StmtError(); 5264 } 5265 if (DSAStack->isParentOrderedRegion()) { 5266 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 5267 return StmtError(); 5268 } 5269 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 5270 CancelRegion); 5271 } 5272 5273 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 5274 SourceLocation StartLoc, 5275 SourceLocation EndLoc, 5276 OpenMPDirectiveKind CancelRegion) { 5277 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for && 5278 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) { 5279 Diag(StartLoc, diag::err_omp_wrong_cancel_region) 5280 << getOpenMPDirectiveName(CancelRegion); 5281 return StmtError(); 5282 } 5283 if (DSAStack->isParentNowaitRegion()) { 5284 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 5285 return StmtError(); 5286 } 5287 if (DSAStack->isParentOrderedRegion()) { 5288 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 5289 return StmtError(); 5290 } 5291 DSAStack->setParentCancelRegion(/*Cancel=*/true); 5292 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 5293 CancelRegion); 5294 } 5295 5296 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 5297 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5298 SourceLocation EndLoc, 5299 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { 5300 if (!AStmt) 5301 return StmtError(); 5302 5303 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5304 OMPLoopDirective::HelperExprs B; 5305 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5306 // define the nested loops number. 5307 unsigned NestedLoopCount = 5308 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 5309 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 5310 VarsWithImplicitDSA, B); 5311 if (NestedLoopCount == 0) 5312 return StmtError(); 5313 5314 assert((CurContext->isDependentContext() || B.builtAll()) && 5315 "omp for loop exprs were not built"); 5316 5317 getCurFunction()->setHasBranchProtectedScope(); 5318 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 5319 NestedLoopCount, Clauses, AStmt, B); 5320 } 5321 5322 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 5323 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5324 SourceLocation EndLoc, 5325 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) { 5326 if (!AStmt) 5327 return StmtError(); 5328 5329 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5330 OMPLoopDirective::HelperExprs B; 5331 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5332 // define the nested loops number. 5333 unsigned NestedLoopCount = 5334 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 5335 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 5336 VarsWithImplicitDSA, B); 5337 if (NestedLoopCount == 0) 5338 return StmtError(); 5339 5340 assert((CurContext->isDependentContext() || B.builtAll()) && 5341 "omp for loop exprs were not built"); 5342 5343 getCurFunction()->setHasBranchProtectedScope(); 5344 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 5345 NestedLoopCount, Clauses, AStmt, B); 5346 } 5347 5348 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 5349 SourceLocation StartLoc, 5350 SourceLocation LParenLoc, 5351 SourceLocation EndLoc) { 5352 OMPClause *Res = nullptr; 5353 switch (Kind) { 5354 case OMPC_final: 5355 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 5356 break; 5357 case OMPC_num_threads: 5358 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 5359 break; 5360 case OMPC_safelen: 5361 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 5362 break; 5363 case OMPC_simdlen: 5364 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 5365 break; 5366 case OMPC_collapse: 5367 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 5368 break; 5369 case OMPC_ordered: 5370 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 5371 break; 5372 case OMPC_device: 5373 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc); 5374 break; 5375 case OMPC_num_teams: 5376 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 5377 break; 5378 case OMPC_thread_limit: 5379 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 5380 break; 5381 case OMPC_priority: 5382 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 5383 break; 5384 case OMPC_grainsize: 5385 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 5386 break; 5387 case OMPC_if: 5388 case OMPC_default: 5389 case OMPC_proc_bind: 5390 case OMPC_schedule: 5391 case OMPC_private: 5392 case OMPC_firstprivate: 5393 case OMPC_lastprivate: 5394 case OMPC_shared: 5395 case OMPC_reduction: 5396 case OMPC_linear: 5397 case OMPC_aligned: 5398 case OMPC_copyin: 5399 case OMPC_copyprivate: 5400 case OMPC_nowait: 5401 case OMPC_untied: 5402 case OMPC_mergeable: 5403 case OMPC_threadprivate: 5404 case OMPC_flush: 5405 case OMPC_read: 5406 case OMPC_write: 5407 case OMPC_update: 5408 case OMPC_capture: 5409 case OMPC_seq_cst: 5410 case OMPC_depend: 5411 case OMPC_threads: 5412 case OMPC_simd: 5413 case OMPC_map: 5414 case OMPC_nogroup: 5415 case OMPC_unknown: 5416 llvm_unreachable("Clause is not allowed."); 5417 } 5418 return Res; 5419 } 5420 5421 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 5422 Expr *Condition, SourceLocation StartLoc, 5423 SourceLocation LParenLoc, 5424 SourceLocation NameModifierLoc, 5425 SourceLocation ColonLoc, 5426 SourceLocation EndLoc) { 5427 Expr *ValExpr = Condition; 5428 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 5429 !Condition->isInstantiationDependent() && 5430 !Condition->containsUnexpandedParameterPack()) { 5431 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(), 5432 Condition->getExprLoc(), Condition); 5433 if (Val.isInvalid()) 5434 return nullptr; 5435 5436 ValExpr = Val.get(); 5437 } 5438 5439 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc, 5440 NameModifierLoc, ColonLoc, EndLoc); 5441 } 5442 5443 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 5444 SourceLocation StartLoc, 5445 SourceLocation LParenLoc, 5446 SourceLocation EndLoc) { 5447 Expr *ValExpr = Condition; 5448 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 5449 !Condition->isInstantiationDependent() && 5450 !Condition->containsUnexpandedParameterPack()) { 5451 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(), 5452 Condition->getExprLoc(), Condition); 5453 if (Val.isInvalid()) 5454 return nullptr; 5455 5456 ValExpr = Val.get(); 5457 } 5458 5459 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc); 5460 } 5461 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 5462 Expr *Op) { 5463 if (!Op) 5464 return ExprError(); 5465 5466 class IntConvertDiagnoser : public ICEConvertDiagnoser { 5467 public: 5468 IntConvertDiagnoser() 5469 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 5470 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 5471 QualType T) override { 5472 return S.Diag(Loc, diag::err_omp_not_integral) << T; 5473 } 5474 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 5475 QualType T) override { 5476 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 5477 } 5478 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 5479 QualType T, 5480 QualType ConvTy) override { 5481 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 5482 } 5483 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 5484 QualType ConvTy) override { 5485 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 5486 << ConvTy->isEnumeralType() << ConvTy; 5487 } 5488 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 5489 QualType T) override { 5490 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 5491 } 5492 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 5493 QualType ConvTy) override { 5494 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 5495 << ConvTy->isEnumeralType() << ConvTy; 5496 } 5497 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 5498 QualType) override { 5499 llvm_unreachable("conversion functions are permitted"); 5500 } 5501 } ConvertDiagnoser; 5502 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 5503 } 5504 5505 static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, 5506 OpenMPClauseKind CKind, 5507 bool StrictlyPositive) { 5508 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 5509 !ValExpr->isInstantiationDependent()) { 5510 SourceLocation Loc = ValExpr->getExprLoc(); 5511 ExprResult Value = 5512 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 5513 if (Value.isInvalid()) 5514 return false; 5515 5516 ValExpr = Value.get(); 5517 // The expression must evaluate to a non-negative integer value. 5518 llvm::APSInt Result; 5519 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) && 5520 Result.isSigned() && 5521 !((!StrictlyPositive && Result.isNonNegative()) || 5522 (StrictlyPositive && Result.isStrictlyPositive()))) { 5523 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 5524 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 5525 << ValExpr->getSourceRange(); 5526 return false; 5527 } 5528 } 5529 return true; 5530 } 5531 5532 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 5533 SourceLocation StartLoc, 5534 SourceLocation LParenLoc, 5535 SourceLocation EndLoc) { 5536 Expr *ValExpr = NumThreads; 5537 5538 // OpenMP [2.5, Restrictions] 5539 // The num_threads expression must evaluate to a positive integer value. 5540 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 5541 /*StrictlyPositive=*/true)) 5542 return nullptr; 5543 5544 return new (Context) 5545 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc); 5546 } 5547 5548 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 5549 OpenMPClauseKind CKind) { 5550 if (!E) 5551 return ExprError(); 5552 if (E->isValueDependent() || E->isTypeDependent() || 5553 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 5554 return E; 5555 llvm::APSInt Result; 5556 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); 5557 if (ICE.isInvalid()) 5558 return ExprError(); 5559 if (!Result.isStrictlyPositive()) { 5560 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 5561 << getOpenMPClauseName(CKind) << 1 << E->getSourceRange(); 5562 return ExprError(); 5563 } 5564 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 5565 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 5566 << E->getSourceRange(); 5567 return ExprError(); 5568 } 5569 if (CKind == OMPC_collapse) 5570 DSAStack->setCollapseNumber(Result.getExtValue()); 5571 else if (CKind == OMPC_ordered) 5572 DSAStack->setCollapseNumber(Result.getExtValue()); 5573 return ICE; 5574 } 5575 5576 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 5577 SourceLocation LParenLoc, 5578 SourceLocation EndLoc) { 5579 // OpenMP [2.8.1, simd construct, Description] 5580 // The parameter of the safelen clause must be a constant 5581 // positive integer expression. 5582 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 5583 if (Safelen.isInvalid()) 5584 return nullptr; 5585 return new (Context) 5586 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 5587 } 5588 5589 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 5590 SourceLocation LParenLoc, 5591 SourceLocation EndLoc) { 5592 // OpenMP [2.8.1, simd construct, Description] 5593 // The parameter of the simdlen clause must be a constant 5594 // positive integer expression. 5595 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 5596 if (Simdlen.isInvalid()) 5597 return nullptr; 5598 return new (Context) 5599 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 5600 } 5601 5602 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 5603 SourceLocation StartLoc, 5604 SourceLocation LParenLoc, 5605 SourceLocation EndLoc) { 5606 // OpenMP [2.7.1, loop construct, Description] 5607 // OpenMP [2.8.1, simd construct, Description] 5608 // OpenMP [2.9.6, distribute construct, Description] 5609 // The parameter of the collapse clause must be a constant 5610 // positive integer expression. 5611 ExprResult NumForLoopsResult = 5612 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 5613 if (NumForLoopsResult.isInvalid()) 5614 return nullptr; 5615 return new (Context) 5616 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 5617 } 5618 5619 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 5620 SourceLocation EndLoc, 5621 SourceLocation LParenLoc, 5622 Expr *NumForLoops) { 5623 // OpenMP [2.7.1, loop construct, Description] 5624 // OpenMP [2.8.1, simd construct, Description] 5625 // OpenMP [2.9.6, distribute construct, Description] 5626 // The parameter of the ordered clause must be a constant 5627 // positive integer expression if any. 5628 if (NumForLoops && LParenLoc.isValid()) { 5629 ExprResult NumForLoopsResult = 5630 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 5631 if (NumForLoopsResult.isInvalid()) 5632 return nullptr; 5633 NumForLoops = NumForLoopsResult.get(); 5634 } else 5635 NumForLoops = nullptr; 5636 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops); 5637 return new (Context) 5638 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc); 5639 } 5640 5641 OMPClause *Sema::ActOnOpenMPSimpleClause( 5642 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 5643 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 5644 OMPClause *Res = nullptr; 5645 switch (Kind) { 5646 case OMPC_default: 5647 Res = 5648 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), 5649 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 5650 break; 5651 case OMPC_proc_bind: 5652 Res = ActOnOpenMPProcBindClause( 5653 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, 5654 LParenLoc, EndLoc); 5655 break; 5656 case OMPC_if: 5657 case OMPC_final: 5658 case OMPC_num_threads: 5659 case OMPC_safelen: 5660 case OMPC_simdlen: 5661 case OMPC_collapse: 5662 case OMPC_schedule: 5663 case OMPC_private: 5664 case OMPC_firstprivate: 5665 case OMPC_lastprivate: 5666 case OMPC_shared: 5667 case OMPC_reduction: 5668 case OMPC_linear: 5669 case OMPC_aligned: 5670 case OMPC_copyin: 5671 case OMPC_copyprivate: 5672 case OMPC_ordered: 5673 case OMPC_nowait: 5674 case OMPC_untied: 5675 case OMPC_mergeable: 5676 case OMPC_threadprivate: 5677 case OMPC_flush: 5678 case OMPC_read: 5679 case OMPC_write: 5680 case OMPC_update: 5681 case OMPC_capture: 5682 case OMPC_seq_cst: 5683 case OMPC_depend: 5684 case OMPC_device: 5685 case OMPC_threads: 5686 case OMPC_simd: 5687 case OMPC_map: 5688 case OMPC_num_teams: 5689 case OMPC_thread_limit: 5690 case OMPC_priority: 5691 case OMPC_grainsize: 5692 case OMPC_nogroup: 5693 case OMPC_unknown: 5694 llvm_unreachable("Clause is not allowed."); 5695 } 5696 return Res; 5697 } 5698 5699 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, 5700 SourceLocation KindKwLoc, 5701 SourceLocation StartLoc, 5702 SourceLocation LParenLoc, 5703 SourceLocation EndLoc) { 5704 if (Kind == OMPC_DEFAULT_unknown) { 5705 std::string Values; 5706 static_assert(OMPC_DEFAULT_unknown > 0, 5707 "OMPC_DEFAULT_unknown not greater than 0"); 5708 std::string Sep(", "); 5709 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) { 5710 Values += "'"; 5711 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i); 5712 Values += "'"; 5713 switch (i) { 5714 case OMPC_DEFAULT_unknown - 2: 5715 Values += " or "; 5716 break; 5717 case OMPC_DEFAULT_unknown - 1: 5718 break; 5719 default: 5720 Values += Sep; 5721 break; 5722 } 5723 } 5724 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 5725 << Values << getOpenMPClauseName(OMPC_default); 5726 return nullptr; 5727 } 5728 switch (Kind) { 5729 case OMPC_DEFAULT_none: 5730 DSAStack->setDefaultDSANone(KindKwLoc); 5731 break; 5732 case OMPC_DEFAULT_shared: 5733 DSAStack->setDefaultDSAShared(KindKwLoc); 5734 break; 5735 case OMPC_DEFAULT_unknown: 5736 llvm_unreachable("Clause kind is not allowed."); 5737 break; 5738 } 5739 return new (Context) 5740 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 5741 } 5742 5743 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, 5744 SourceLocation KindKwLoc, 5745 SourceLocation StartLoc, 5746 SourceLocation LParenLoc, 5747 SourceLocation EndLoc) { 5748 if (Kind == OMPC_PROC_BIND_unknown) { 5749 std::string Values; 5750 std::string Sep(", "); 5751 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) { 5752 Values += "'"; 5753 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i); 5754 Values += "'"; 5755 switch (i) { 5756 case OMPC_PROC_BIND_unknown - 2: 5757 Values += " or "; 5758 break; 5759 case OMPC_PROC_BIND_unknown - 1: 5760 break; 5761 default: 5762 Values += Sep; 5763 break; 5764 } 5765 } 5766 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 5767 << Values << getOpenMPClauseName(OMPC_proc_bind); 5768 return nullptr; 5769 } 5770 return new (Context) 5771 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 5772 } 5773 5774 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 5775 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr, 5776 SourceLocation StartLoc, SourceLocation LParenLoc, 5777 SourceLocation ArgumentLoc, SourceLocation DelimLoc, 5778 SourceLocation EndLoc) { 5779 OMPClause *Res = nullptr; 5780 switch (Kind) { 5781 case OMPC_schedule: 5782 Res = ActOnOpenMPScheduleClause( 5783 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc, 5784 LParenLoc, ArgumentLoc, DelimLoc, EndLoc); 5785 break; 5786 case OMPC_if: 5787 Res = 5788 ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument), Expr, 5789 StartLoc, LParenLoc, ArgumentLoc, DelimLoc, EndLoc); 5790 break; 5791 case OMPC_final: 5792 case OMPC_num_threads: 5793 case OMPC_safelen: 5794 case OMPC_simdlen: 5795 case OMPC_collapse: 5796 case OMPC_default: 5797 case OMPC_proc_bind: 5798 case OMPC_private: 5799 case OMPC_firstprivate: 5800 case OMPC_lastprivate: 5801 case OMPC_shared: 5802 case OMPC_reduction: 5803 case OMPC_linear: 5804 case OMPC_aligned: 5805 case OMPC_copyin: 5806 case OMPC_copyprivate: 5807 case OMPC_ordered: 5808 case OMPC_nowait: 5809 case OMPC_untied: 5810 case OMPC_mergeable: 5811 case OMPC_threadprivate: 5812 case OMPC_flush: 5813 case OMPC_read: 5814 case OMPC_write: 5815 case OMPC_update: 5816 case OMPC_capture: 5817 case OMPC_seq_cst: 5818 case OMPC_depend: 5819 case OMPC_device: 5820 case OMPC_threads: 5821 case OMPC_simd: 5822 case OMPC_map: 5823 case OMPC_num_teams: 5824 case OMPC_thread_limit: 5825 case OMPC_priority: 5826 case OMPC_grainsize: 5827 case OMPC_nogroup: 5828 case OMPC_unknown: 5829 llvm_unreachable("Clause is not allowed."); 5830 } 5831 return Res; 5832 } 5833 5834 OMPClause *Sema::ActOnOpenMPScheduleClause( 5835 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 5836 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 5837 SourceLocation EndLoc) { 5838 if (Kind == OMPC_SCHEDULE_unknown) { 5839 std::string Values; 5840 std::string Sep(", "); 5841 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) { 5842 Values += "'"; 5843 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i); 5844 Values += "'"; 5845 switch (i) { 5846 case OMPC_SCHEDULE_unknown - 2: 5847 Values += " or "; 5848 break; 5849 case OMPC_SCHEDULE_unknown - 1: 5850 break; 5851 default: 5852 Values += Sep; 5853 break; 5854 } 5855 } 5856 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 5857 << Values << getOpenMPClauseName(OMPC_schedule); 5858 return nullptr; 5859 } 5860 Expr *ValExpr = ChunkSize; 5861 Expr *HelperValExpr = nullptr; 5862 if (ChunkSize) { 5863 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 5864 !ChunkSize->isInstantiationDependent() && 5865 !ChunkSize->containsUnexpandedParameterPack()) { 5866 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); 5867 ExprResult Val = 5868 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 5869 if (Val.isInvalid()) 5870 return nullptr; 5871 5872 ValExpr = Val.get(); 5873 5874 // OpenMP [2.7.1, Restrictions] 5875 // chunk_size must be a loop invariant integer expression with a positive 5876 // value. 5877 llvm::APSInt Result; 5878 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 5879 if (Result.isSigned() && !Result.isStrictlyPositive()) { 5880 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 5881 << "schedule" << 1 << ChunkSize->getSourceRange(); 5882 return nullptr; 5883 } 5884 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) { 5885 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(), 5886 ChunkSize->getType(), ".chunk."); 5887 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(), 5888 ChunkSize->getExprLoc(), 5889 /*RefersToCapture=*/true); 5890 HelperValExpr = ImpVarRef; 5891 } 5892 } 5893 } 5894 5895 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, 5896 EndLoc, Kind, ValExpr, HelperValExpr); 5897 } 5898 5899 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 5900 SourceLocation StartLoc, 5901 SourceLocation EndLoc) { 5902 OMPClause *Res = nullptr; 5903 switch (Kind) { 5904 case OMPC_ordered: 5905 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 5906 break; 5907 case OMPC_nowait: 5908 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 5909 break; 5910 case OMPC_untied: 5911 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 5912 break; 5913 case OMPC_mergeable: 5914 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 5915 break; 5916 case OMPC_read: 5917 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 5918 break; 5919 case OMPC_write: 5920 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 5921 break; 5922 case OMPC_update: 5923 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 5924 break; 5925 case OMPC_capture: 5926 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 5927 break; 5928 case OMPC_seq_cst: 5929 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 5930 break; 5931 case OMPC_threads: 5932 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 5933 break; 5934 case OMPC_simd: 5935 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 5936 break; 5937 case OMPC_nogroup: 5938 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 5939 break; 5940 case OMPC_if: 5941 case OMPC_final: 5942 case OMPC_num_threads: 5943 case OMPC_safelen: 5944 case OMPC_simdlen: 5945 case OMPC_collapse: 5946 case OMPC_schedule: 5947 case OMPC_private: 5948 case OMPC_firstprivate: 5949 case OMPC_lastprivate: 5950 case OMPC_shared: 5951 case OMPC_reduction: 5952 case OMPC_linear: 5953 case OMPC_aligned: 5954 case OMPC_copyin: 5955 case OMPC_copyprivate: 5956 case OMPC_default: 5957 case OMPC_proc_bind: 5958 case OMPC_threadprivate: 5959 case OMPC_flush: 5960 case OMPC_depend: 5961 case OMPC_device: 5962 case OMPC_map: 5963 case OMPC_num_teams: 5964 case OMPC_thread_limit: 5965 case OMPC_priority: 5966 case OMPC_grainsize: 5967 case OMPC_unknown: 5968 llvm_unreachable("Clause is not allowed."); 5969 } 5970 return Res; 5971 } 5972 5973 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 5974 SourceLocation EndLoc) { 5975 DSAStack->setNowaitRegion(); 5976 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 5977 } 5978 5979 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 5980 SourceLocation EndLoc) { 5981 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 5982 } 5983 5984 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 5985 SourceLocation EndLoc) { 5986 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 5987 } 5988 5989 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 5990 SourceLocation EndLoc) { 5991 return new (Context) OMPReadClause(StartLoc, EndLoc); 5992 } 5993 5994 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 5995 SourceLocation EndLoc) { 5996 return new (Context) OMPWriteClause(StartLoc, EndLoc); 5997 } 5998 5999 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 6000 SourceLocation EndLoc) { 6001 return new (Context) OMPUpdateClause(StartLoc, EndLoc); 6002 } 6003 6004 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 6005 SourceLocation EndLoc) { 6006 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 6007 } 6008 6009 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 6010 SourceLocation EndLoc) { 6011 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 6012 } 6013 6014 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 6015 SourceLocation EndLoc) { 6016 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 6017 } 6018 6019 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 6020 SourceLocation EndLoc) { 6021 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 6022 } 6023 6024 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 6025 SourceLocation EndLoc) { 6026 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 6027 } 6028 6029 OMPClause *Sema::ActOnOpenMPVarListClause( 6030 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, 6031 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 6032 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec, 6033 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind, 6034 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier, 6035 OpenMPMapClauseKind MapType, SourceLocation DepLinMapLoc) { 6036 OMPClause *Res = nullptr; 6037 switch (Kind) { 6038 case OMPC_private: 6039 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 6040 break; 6041 case OMPC_firstprivate: 6042 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 6043 break; 6044 case OMPC_lastprivate: 6045 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 6046 break; 6047 case OMPC_shared: 6048 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 6049 break; 6050 case OMPC_reduction: 6051 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 6052 EndLoc, ReductionIdScopeSpec, ReductionId); 6053 break; 6054 case OMPC_linear: 6055 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, 6056 LinKind, DepLinMapLoc, ColonLoc, EndLoc); 6057 break; 6058 case OMPC_aligned: 6059 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, 6060 ColonLoc, EndLoc); 6061 break; 6062 case OMPC_copyin: 6063 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 6064 break; 6065 case OMPC_copyprivate: 6066 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 6067 break; 6068 case OMPC_flush: 6069 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 6070 break; 6071 case OMPC_depend: 6072 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList, 6073 StartLoc, LParenLoc, EndLoc); 6074 break; 6075 case OMPC_map: 6076 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, DepLinMapLoc, ColonLoc, 6077 VarList, StartLoc, LParenLoc, EndLoc); 6078 break; 6079 case OMPC_if: 6080 case OMPC_final: 6081 case OMPC_num_threads: 6082 case OMPC_safelen: 6083 case OMPC_simdlen: 6084 case OMPC_collapse: 6085 case OMPC_default: 6086 case OMPC_proc_bind: 6087 case OMPC_schedule: 6088 case OMPC_ordered: 6089 case OMPC_nowait: 6090 case OMPC_untied: 6091 case OMPC_mergeable: 6092 case OMPC_threadprivate: 6093 case OMPC_read: 6094 case OMPC_write: 6095 case OMPC_update: 6096 case OMPC_capture: 6097 case OMPC_seq_cst: 6098 case OMPC_device: 6099 case OMPC_threads: 6100 case OMPC_simd: 6101 case OMPC_num_teams: 6102 case OMPC_thread_limit: 6103 case OMPC_priority: 6104 case OMPC_grainsize: 6105 case OMPC_nogroup: 6106 case OMPC_unknown: 6107 llvm_unreachable("Clause is not allowed."); 6108 } 6109 return Res; 6110 } 6111 6112 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 6113 SourceLocation StartLoc, 6114 SourceLocation LParenLoc, 6115 SourceLocation EndLoc) { 6116 SmallVector<Expr *, 8> Vars; 6117 SmallVector<Expr *, 8> PrivateCopies; 6118 for (auto &RefExpr : VarList) { 6119 assert(RefExpr && "NULL expr in OpenMP private clause."); 6120 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 6121 // It will be analyzed later. 6122 Vars.push_back(RefExpr); 6123 PrivateCopies.push_back(nullptr); 6124 continue; 6125 } 6126 6127 SourceLocation ELoc = RefExpr->getExprLoc(); 6128 // OpenMP [2.1, C/C++] 6129 // A list item is a variable name. 6130 // OpenMP [2.9.3.3, Restrictions, p.1] 6131 // A variable that is part of another variable (as an array or 6132 // structure element) cannot appear in a private clause. 6133 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 6134 if (!DE || !isa<VarDecl>(DE->getDecl())) { 6135 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 6136 continue; 6137 } 6138 Decl *D = DE->getDecl(); 6139 VarDecl *VD = cast<VarDecl>(D); 6140 6141 QualType Type = VD->getType(); 6142 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 6143 // It will be analyzed later. 6144 Vars.push_back(DE); 6145 PrivateCopies.push_back(nullptr); 6146 continue; 6147 } 6148 6149 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 6150 // A variable that appears in a private clause must not have an incomplete 6151 // type or a reference type. 6152 if (RequireCompleteType(ELoc, Type, 6153 diag::err_omp_private_incomplete_type)) { 6154 continue; 6155 } 6156 Type = Type.getNonReferenceType(); 6157 6158 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 6159 // in a Construct] 6160 // Variables with the predetermined data-sharing attributes may not be 6161 // listed in data-sharing attributes clauses, except for the cases 6162 // listed below. For these exceptions only, listing a predetermined 6163 // variable in a data-sharing attribute clause is allowed and overrides 6164 // the variable's predetermined data-sharing attributes. 6165 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); 6166 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 6167 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 6168 << getOpenMPClauseName(OMPC_private); 6169 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6170 continue; 6171 } 6172 6173 // Variably modified types are not supported for tasks. 6174 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 6175 DSAStack->getCurrentDirective() == OMPD_task) { 6176 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 6177 << getOpenMPClauseName(OMPC_private) << Type 6178 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 6179 bool IsDecl = 6180 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 6181 Diag(VD->getLocation(), 6182 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 6183 << VD; 6184 continue; 6185 } 6186 6187 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 6188 // A variable of class type (or array thereof) that appears in a private 6189 // clause requires an accessible, unambiguous default constructor for the 6190 // class type. 6191 // Generate helper private variable and initialize it with the default 6192 // value. The address of the original variable is replaced by the address of 6193 // the new private variable in CodeGen. This new variable is not added to 6194 // IdResolver, so the code in the OpenMP region uses original variable for 6195 // proper diagnostics. 6196 Type = Type.getUnqualifiedType(); 6197 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName(), 6198 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 6199 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false); 6200 if (VDPrivate->isInvalidDecl()) 6201 continue; 6202 auto VDPrivateRefExpr = buildDeclRefExpr( 6203 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc()); 6204 6205 DSAStack->addDSA(VD, DE, OMPC_private); 6206 Vars.push_back(DE); 6207 PrivateCopies.push_back(VDPrivateRefExpr); 6208 } 6209 6210 if (Vars.empty()) 6211 return nullptr; 6212 6213 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 6214 PrivateCopies); 6215 } 6216 6217 namespace { 6218 class DiagsUninitializedSeveretyRAII { 6219 private: 6220 DiagnosticsEngine &Diags; 6221 SourceLocation SavedLoc; 6222 bool IsIgnored; 6223 6224 public: 6225 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 6226 bool IsIgnored) 6227 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 6228 if (!IsIgnored) { 6229 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 6230 /*Map*/ diag::Severity::Ignored, Loc); 6231 } 6232 } 6233 ~DiagsUninitializedSeveretyRAII() { 6234 if (!IsIgnored) 6235 Diags.popMappings(SavedLoc); 6236 } 6237 }; 6238 } 6239 6240 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 6241 SourceLocation StartLoc, 6242 SourceLocation LParenLoc, 6243 SourceLocation EndLoc) { 6244 SmallVector<Expr *, 8> Vars; 6245 SmallVector<Expr *, 8> PrivateCopies; 6246 SmallVector<Expr *, 8> Inits; 6247 bool IsImplicitClause = 6248 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 6249 auto ImplicitClauseLoc = DSAStack->getConstructLoc(); 6250 6251 for (auto &RefExpr : VarList) { 6252 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 6253 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 6254 // It will be analyzed later. 6255 Vars.push_back(RefExpr); 6256 PrivateCopies.push_back(nullptr); 6257 Inits.push_back(nullptr); 6258 continue; 6259 } 6260 6261 SourceLocation ELoc = 6262 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc(); 6263 // OpenMP [2.1, C/C++] 6264 // A list item is a variable name. 6265 // OpenMP [2.9.3.3, Restrictions, p.1] 6266 // A variable that is part of another variable (as an array or 6267 // structure element) cannot appear in a private clause. 6268 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 6269 if (!DE || !isa<VarDecl>(DE->getDecl())) { 6270 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 6271 continue; 6272 } 6273 Decl *D = DE->getDecl(); 6274 VarDecl *VD = cast<VarDecl>(D); 6275 6276 QualType Type = VD->getType(); 6277 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 6278 // It will be analyzed later. 6279 Vars.push_back(DE); 6280 PrivateCopies.push_back(nullptr); 6281 Inits.push_back(nullptr); 6282 continue; 6283 } 6284 6285 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 6286 // A variable that appears in a private clause must not have an incomplete 6287 // type or a reference type. 6288 if (RequireCompleteType(ELoc, Type, 6289 diag::err_omp_firstprivate_incomplete_type)) { 6290 continue; 6291 } 6292 Type = Type.getNonReferenceType(); 6293 6294 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 6295 // A variable of class type (or array thereof) that appears in a private 6296 // clause requires an accessible, unambiguous copy constructor for the 6297 // class type. 6298 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 6299 6300 // If an implicit firstprivate variable found it was checked already. 6301 if (!IsImplicitClause) { 6302 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); 6303 bool IsConstant = ElemType.isConstant(Context); 6304 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 6305 // A list item that specifies a given variable may not appear in more 6306 // than one clause on the same directive, except that a variable may be 6307 // specified in both firstprivate and lastprivate clauses. 6308 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 6309 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) { 6310 Diag(ELoc, diag::err_omp_wrong_dsa) 6311 << getOpenMPClauseName(DVar.CKind) 6312 << getOpenMPClauseName(OMPC_firstprivate); 6313 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6314 continue; 6315 } 6316 6317 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 6318 // in a Construct] 6319 // Variables with the predetermined data-sharing attributes may not be 6320 // listed in data-sharing attributes clauses, except for the cases 6321 // listed below. For these exceptions only, listing a predetermined 6322 // variable in a data-sharing attribute clause is allowed and overrides 6323 // the variable's predetermined data-sharing attributes. 6324 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 6325 // in a Construct, C/C++, p.2] 6326 // Variables with const-qualified type having no mutable member may be 6327 // listed in a firstprivate clause, even if they are static data members. 6328 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr && 6329 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 6330 Diag(ELoc, diag::err_omp_wrong_dsa) 6331 << getOpenMPClauseName(DVar.CKind) 6332 << getOpenMPClauseName(OMPC_firstprivate); 6333 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6334 continue; 6335 } 6336 6337 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 6338 // OpenMP [2.9.3.4, Restrictions, p.2] 6339 // A list item that is private within a parallel region must not appear 6340 // in a firstprivate clause on a worksharing construct if any of the 6341 // worksharing regions arising from the worksharing construct ever bind 6342 // to any of the parallel regions arising from the parallel construct. 6343 if (isOpenMPWorksharingDirective(CurrDir) && 6344 !isOpenMPParallelDirective(CurrDir)) { 6345 DVar = DSAStack->getImplicitDSA(VD, true); 6346 if (DVar.CKind != OMPC_shared && 6347 (isOpenMPParallelDirective(DVar.DKind) || 6348 DVar.DKind == OMPD_unknown)) { 6349 Diag(ELoc, diag::err_omp_required_access) 6350 << getOpenMPClauseName(OMPC_firstprivate) 6351 << getOpenMPClauseName(OMPC_shared); 6352 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6353 continue; 6354 } 6355 } 6356 // OpenMP [2.9.3.4, Restrictions, p.3] 6357 // A list item that appears in a reduction clause of a parallel construct 6358 // must not appear in a firstprivate clause on a worksharing or task 6359 // construct if any of the worksharing or task regions arising from the 6360 // worksharing or task construct ever bind to any of the parallel regions 6361 // arising from the parallel construct. 6362 // OpenMP [2.9.3.4, Restrictions, p.4] 6363 // A list item that appears in a reduction clause in worksharing 6364 // construct must not appear in a firstprivate clause in a task construct 6365 // encountered during execution of any of the worksharing regions arising 6366 // from the worksharing construct. 6367 if (CurrDir == OMPD_task) { 6368 DVar = 6369 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction), 6370 [](OpenMPDirectiveKind K) -> bool { 6371 return isOpenMPParallelDirective(K) || 6372 isOpenMPWorksharingDirective(K); 6373 }, 6374 false); 6375 if (DVar.CKind == OMPC_reduction && 6376 (isOpenMPParallelDirective(DVar.DKind) || 6377 isOpenMPWorksharingDirective(DVar.DKind))) { 6378 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 6379 << getOpenMPDirectiveName(DVar.DKind); 6380 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6381 continue; 6382 } 6383 } 6384 } 6385 6386 // Variably modified types are not supported for tasks. 6387 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 6388 DSAStack->getCurrentDirective() == OMPD_task) { 6389 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 6390 << getOpenMPClauseName(OMPC_firstprivate) << Type 6391 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 6392 bool IsDecl = 6393 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 6394 Diag(VD->getLocation(), 6395 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 6396 << VD; 6397 continue; 6398 } 6399 6400 Type = Type.getUnqualifiedType(); 6401 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(), 6402 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 6403 // Generate helper private variable and initialize it with the value of the 6404 // original variable. The address of the original variable is replaced by 6405 // the address of the new private variable in the CodeGen. This new variable 6406 // is not added to IdResolver, so the code in the OpenMP region uses 6407 // original variable for proper diagnostics and variable capturing. 6408 Expr *VDInitRefExpr = nullptr; 6409 // For arrays generate initializer for single element and replace it by the 6410 // original array element in CodeGen. 6411 if (Type->isArrayType()) { 6412 auto VDInit = 6413 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName()); 6414 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 6415 auto Init = DefaultLvalueConversion(VDInitRefExpr).get(); 6416 ElemType = ElemType.getUnqualifiedType(); 6417 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType, 6418 ".firstprivate.temp"); 6419 InitializedEntity Entity = 6420 InitializedEntity::InitializeVariable(VDInitTemp); 6421 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 6422 6423 InitializationSequence InitSeq(*this, Entity, Kind, Init); 6424 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 6425 if (Result.isInvalid()) 6426 VDPrivate->setInvalidDecl(); 6427 else 6428 VDPrivate->setInit(Result.getAs<Expr>()); 6429 // Remove temp variable declaration. 6430 Context.Deallocate(VDInitTemp); 6431 } else { 6432 auto *VDInit = 6433 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp"); 6434 VDInitRefExpr = 6435 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc()); 6436 AddInitializerToDecl(VDPrivate, 6437 DefaultLvalueConversion(VDInitRefExpr).get(), 6438 /*DirectInit=*/false, /*TypeMayContainAuto=*/false); 6439 } 6440 if (VDPrivate->isInvalidDecl()) { 6441 if (IsImplicitClause) { 6442 Diag(DE->getExprLoc(), 6443 diag::note_omp_task_predetermined_firstprivate_here); 6444 } 6445 continue; 6446 } 6447 CurContext->addDecl(VDPrivate); 6448 auto VDPrivateRefExpr = buildDeclRefExpr( 6449 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc()); 6450 DSAStack->addDSA(VD, DE, OMPC_firstprivate); 6451 Vars.push_back(DE); 6452 PrivateCopies.push_back(VDPrivateRefExpr); 6453 Inits.push_back(VDInitRefExpr); 6454 } 6455 6456 if (Vars.empty()) 6457 return nullptr; 6458 6459 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 6460 Vars, PrivateCopies, Inits); 6461 } 6462 6463 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList, 6464 SourceLocation StartLoc, 6465 SourceLocation LParenLoc, 6466 SourceLocation EndLoc) { 6467 SmallVector<Expr *, 8> Vars; 6468 SmallVector<Expr *, 8> SrcExprs; 6469 SmallVector<Expr *, 8> DstExprs; 6470 SmallVector<Expr *, 8> AssignmentOps; 6471 for (auto &RefExpr : VarList) { 6472 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 6473 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 6474 // It will be analyzed later. 6475 Vars.push_back(RefExpr); 6476 SrcExprs.push_back(nullptr); 6477 DstExprs.push_back(nullptr); 6478 AssignmentOps.push_back(nullptr); 6479 continue; 6480 } 6481 6482 SourceLocation ELoc = RefExpr->getExprLoc(); 6483 // OpenMP [2.1, C/C++] 6484 // A list item is a variable name. 6485 // OpenMP [2.14.3.5, Restrictions, p.1] 6486 // A variable that is part of another variable (as an array or structure 6487 // element) cannot appear in a lastprivate clause. 6488 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 6489 if (!DE || !isa<VarDecl>(DE->getDecl())) { 6490 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 6491 continue; 6492 } 6493 Decl *D = DE->getDecl(); 6494 VarDecl *VD = cast<VarDecl>(D); 6495 6496 QualType Type = VD->getType(); 6497 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 6498 // It will be analyzed later. 6499 Vars.push_back(DE); 6500 SrcExprs.push_back(nullptr); 6501 DstExprs.push_back(nullptr); 6502 AssignmentOps.push_back(nullptr); 6503 continue; 6504 } 6505 6506 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 6507 // A variable that appears in a lastprivate clause must not have an 6508 // incomplete type or a reference type. 6509 if (RequireCompleteType(ELoc, Type, 6510 diag::err_omp_lastprivate_incomplete_type)) { 6511 continue; 6512 } 6513 Type = Type.getNonReferenceType(); 6514 6515 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 6516 // in a Construct] 6517 // Variables with the predetermined data-sharing attributes may not be 6518 // listed in data-sharing attributes clauses, except for the cases 6519 // listed below. 6520 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); 6521 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 6522 DVar.CKind != OMPC_firstprivate && 6523 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 6524 Diag(ELoc, diag::err_omp_wrong_dsa) 6525 << getOpenMPClauseName(DVar.CKind) 6526 << getOpenMPClauseName(OMPC_lastprivate); 6527 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6528 continue; 6529 } 6530 6531 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 6532 // OpenMP [2.14.3.5, Restrictions, p.2] 6533 // A list item that is private within a parallel region, or that appears in 6534 // the reduction clause of a parallel construct, must not appear in a 6535 // lastprivate clause on a worksharing construct if any of the corresponding 6536 // worksharing regions ever binds to any of the corresponding parallel 6537 // regions. 6538 DSAStackTy::DSAVarData TopDVar = DVar; 6539 if (isOpenMPWorksharingDirective(CurrDir) && 6540 !isOpenMPParallelDirective(CurrDir)) { 6541 DVar = DSAStack->getImplicitDSA(VD, true); 6542 if (DVar.CKind != OMPC_shared) { 6543 Diag(ELoc, diag::err_omp_required_access) 6544 << getOpenMPClauseName(OMPC_lastprivate) 6545 << getOpenMPClauseName(OMPC_shared); 6546 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6547 continue; 6548 } 6549 } 6550 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 6551 // A variable of class type (or array thereof) that appears in a 6552 // lastprivate clause requires an accessible, unambiguous default 6553 // constructor for the class type, unless the list item is also specified 6554 // in a firstprivate clause. 6555 // A variable of class type (or array thereof) that appears in a 6556 // lastprivate clause requires an accessible, unambiguous copy assignment 6557 // operator for the class type. 6558 Type = Context.getBaseElementType(Type).getNonReferenceType(); 6559 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(), 6560 Type.getUnqualifiedType(), ".lastprivate.src", 6561 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 6562 auto *PseudoSrcExpr = buildDeclRefExpr( 6563 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc()); 6564 auto *DstVD = 6565 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst", 6566 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 6567 auto *PseudoDstExpr = 6568 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc()); 6569 // For arrays generate assignment operation for single element and replace 6570 // it by the original array element in CodeGen. 6571 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, 6572 PseudoDstExpr, PseudoSrcExpr); 6573 if (AssignmentOp.isInvalid()) 6574 continue; 6575 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 6576 /*DiscardedValue=*/true); 6577 if (AssignmentOp.isInvalid()) 6578 continue; 6579 6580 if (TopDVar.CKind != OMPC_firstprivate) 6581 DSAStack->addDSA(VD, DE, OMPC_lastprivate); 6582 Vars.push_back(DE); 6583 SrcExprs.push_back(PseudoSrcExpr); 6584 DstExprs.push_back(PseudoDstExpr); 6585 AssignmentOps.push_back(AssignmentOp.get()); 6586 } 6587 6588 if (Vars.empty()) 6589 return nullptr; 6590 6591 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 6592 Vars, SrcExprs, DstExprs, AssignmentOps); 6593 } 6594 6595 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 6596 SourceLocation StartLoc, 6597 SourceLocation LParenLoc, 6598 SourceLocation EndLoc) { 6599 SmallVector<Expr *, 8> Vars; 6600 for (auto &RefExpr : VarList) { 6601 assert(RefExpr && "NULL expr in OpenMP shared clause."); 6602 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 6603 // It will be analyzed later. 6604 Vars.push_back(RefExpr); 6605 continue; 6606 } 6607 6608 SourceLocation ELoc = RefExpr->getExprLoc(); 6609 // OpenMP [2.1, C/C++] 6610 // A list item is a variable name. 6611 // OpenMP [2.14.3.2, Restrictions, p.1] 6612 // A variable that is part of another variable (as an array or structure 6613 // element) cannot appear in a shared unless it is a static data member 6614 // of a C++ class. 6615 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 6616 if (!DE || !isa<VarDecl>(DE->getDecl())) { 6617 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 6618 continue; 6619 } 6620 Decl *D = DE->getDecl(); 6621 VarDecl *VD = cast<VarDecl>(D); 6622 6623 QualType Type = VD->getType(); 6624 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 6625 // It will be analyzed later. 6626 Vars.push_back(DE); 6627 continue; 6628 } 6629 6630 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 6631 // in a Construct] 6632 // Variables with the predetermined data-sharing attributes may not be 6633 // listed in data-sharing attributes clauses, except for the cases 6634 // listed below. For these exceptions only, listing a predetermined 6635 // variable in a data-sharing attribute clause is allowed and overrides 6636 // the variable's predetermined data-sharing attributes. 6637 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); 6638 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 6639 DVar.RefExpr) { 6640 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 6641 << getOpenMPClauseName(OMPC_shared); 6642 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6643 continue; 6644 } 6645 6646 DSAStack->addDSA(VD, DE, OMPC_shared); 6647 Vars.push_back(DE); 6648 } 6649 6650 if (Vars.empty()) 6651 return nullptr; 6652 6653 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 6654 } 6655 6656 namespace { 6657 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 6658 DSAStackTy *Stack; 6659 6660 public: 6661 bool VisitDeclRefExpr(DeclRefExpr *E) { 6662 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) { 6663 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false); 6664 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 6665 return false; 6666 if (DVar.CKind != OMPC_unknown) 6667 return true; 6668 DSAStackTy::DSAVarData DVarPrivate = 6669 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false); 6670 if (DVarPrivate.CKind != OMPC_unknown) 6671 return true; 6672 return false; 6673 } 6674 return false; 6675 } 6676 bool VisitStmt(Stmt *S) { 6677 for (auto Child : S->children()) { 6678 if (Child && Visit(Child)) 6679 return true; 6680 } 6681 return false; 6682 } 6683 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 6684 }; 6685 } // namespace 6686 6687 OMPClause *Sema::ActOnOpenMPReductionClause( 6688 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 6689 SourceLocation ColonLoc, SourceLocation EndLoc, 6690 CXXScopeSpec &ReductionIdScopeSpec, 6691 const DeclarationNameInfo &ReductionId) { 6692 // TODO: Allow scope specification search when 'declare reduction' is 6693 // supported. 6694 assert(ReductionIdScopeSpec.isEmpty() && 6695 "No support for scoped reduction identifiers yet."); 6696 6697 auto DN = ReductionId.getName(); 6698 auto OOK = DN.getCXXOverloadedOperator(); 6699 BinaryOperatorKind BOK = BO_Comma; 6700 6701 // OpenMP [2.14.3.6, reduction clause] 6702 // C 6703 // reduction-identifier is either an identifier or one of the following 6704 // operators: +, -, *, &, |, ^, && and || 6705 // C++ 6706 // reduction-identifier is either an id-expression or one of the following 6707 // operators: +, -, *, &, |, ^, && and || 6708 // FIXME: Only 'min' and 'max' identifiers are supported for now. 6709 switch (OOK) { 6710 case OO_Plus: 6711 case OO_Minus: 6712 BOK = BO_Add; 6713 break; 6714 case OO_Star: 6715 BOK = BO_Mul; 6716 break; 6717 case OO_Amp: 6718 BOK = BO_And; 6719 break; 6720 case OO_Pipe: 6721 BOK = BO_Or; 6722 break; 6723 case OO_Caret: 6724 BOK = BO_Xor; 6725 break; 6726 case OO_AmpAmp: 6727 BOK = BO_LAnd; 6728 break; 6729 case OO_PipePipe: 6730 BOK = BO_LOr; 6731 break; 6732 case OO_New: 6733 case OO_Delete: 6734 case OO_Array_New: 6735 case OO_Array_Delete: 6736 case OO_Slash: 6737 case OO_Percent: 6738 case OO_Tilde: 6739 case OO_Exclaim: 6740 case OO_Equal: 6741 case OO_Less: 6742 case OO_Greater: 6743 case OO_LessEqual: 6744 case OO_GreaterEqual: 6745 case OO_PlusEqual: 6746 case OO_MinusEqual: 6747 case OO_StarEqual: 6748 case OO_SlashEqual: 6749 case OO_PercentEqual: 6750 case OO_CaretEqual: 6751 case OO_AmpEqual: 6752 case OO_PipeEqual: 6753 case OO_LessLess: 6754 case OO_GreaterGreater: 6755 case OO_LessLessEqual: 6756 case OO_GreaterGreaterEqual: 6757 case OO_EqualEqual: 6758 case OO_ExclaimEqual: 6759 case OO_PlusPlus: 6760 case OO_MinusMinus: 6761 case OO_Comma: 6762 case OO_ArrowStar: 6763 case OO_Arrow: 6764 case OO_Call: 6765 case OO_Subscript: 6766 case OO_Conditional: 6767 case OO_Coawait: 6768 case NUM_OVERLOADED_OPERATORS: 6769 llvm_unreachable("Unexpected reduction identifier"); 6770 case OO_None: 6771 if (auto II = DN.getAsIdentifierInfo()) { 6772 if (II->isStr("max")) 6773 BOK = BO_GT; 6774 else if (II->isStr("min")) 6775 BOK = BO_LT; 6776 } 6777 break; 6778 } 6779 SourceRange ReductionIdRange; 6780 if (ReductionIdScopeSpec.isValid()) { 6781 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 6782 } 6783 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 6784 if (BOK == BO_Comma) { 6785 // Not allowed reduction identifier is found. 6786 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier) 6787 << ReductionIdRange; 6788 return nullptr; 6789 } 6790 6791 SmallVector<Expr *, 8> Vars; 6792 SmallVector<Expr *, 8> Privates; 6793 SmallVector<Expr *, 8> LHSs; 6794 SmallVector<Expr *, 8> RHSs; 6795 SmallVector<Expr *, 8> ReductionOps; 6796 for (auto RefExpr : VarList) { 6797 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 6798 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 6799 // It will be analyzed later. 6800 Vars.push_back(RefExpr); 6801 Privates.push_back(nullptr); 6802 LHSs.push_back(nullptr); 6803 RHSs.push_back(nullptr); 6804 ReductionOps.push_back(nullptr); 6805 continue; 6806 } 6807 6808 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 6809 RefExpr->isInstantiationDependent() || 6810 RefExpr->containsUnexpandedParameterPack()) { 6811 // It will be analyzed later. 6812 Vars.push_back(RefExpr); 6813 Privates.push_back(nullptr); 6814 LHSs.push_back(nullptr); 6815 RHSs.push_back(nullptr); 6816 ReductionOps.push_back(nullptr); 6817 continue; 6818 } 6819 6820 auto ELoc = RefExpr->getExprLoc(); 6821 auto ERange = RefExpr->getSourceRange(); 6822 // OpenMP [2.1, C/C++] 6823 // A list item is a variable or array section, subject to the restrictions 6824 // specified in Section 2.4 on page 42 and in each of the sections 6825 // describing clauses and directives for which a list appears. 6826 // OpenMP [2.14.3.3, Restrictions, p.1] 6827 // A variable that is part of another variable (as an array or 6828 // structure element) cannot appear in a private clause. 6829 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 6830 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr); 6831 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr); 6832 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) { 6833 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item) << ERange; 6834 continue; 6835 } 6836 QualType Type; 6837 VarDecl *VD = nullptr; 6838 if (DE) { 6839 auto D = DE->getDecl(); 6840 VD = cast<VarDecl>(D); 6841 Type = VD->getType(); 6842 } else if (ASE) { 6843 Type = ASE->getType(); 6844 auto *Base = ASE->getBase()->IgnoreParenImpCasts(); 6845 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 6846 Base = TempASE->getBase()->IgnoreParenImpCasts(); 6847 DE = dyn_cast<DeclRefExpr>(Base); 6848 if (DE) 6849 VD = dyn_cast<VarDecl>(DE->getDecl()); 6850 if (!VD) { 6851 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name) 6852 << 0 << Base->getSourceRange(); 6853 continue; 6854 } 6855 } else if (OASE) { 6856 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 6857 if (auto *ATy = BaseType->getAsArrayTypeUnsafe()) 6858 Type = ATy->getElementType(); 6859 else 6860 Type = BaseType->getPointeeType(); 6861 auto *Base = OASE->getBase()->IgnoreParenImpCasts(); 6862 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 6863 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 6864 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 6865 Base = TempASE->getBase()->IgnoreParenImpCasts(); 6866 DE = dyn_cast<DeclRefExpr>(Base); 6867 if (DE) 6868 VD = dyn_cast<VarDecl>(DE->getDecl()); 6869 if (!VD) { 6870 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name) 6871 << 1 << Base->getSourceRange(); 6872 continue; 6873 } 6874 } 6875 6876 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 6877 // A variable that appears in a private clause must not have an incomplete 6878 // type or a reference type. 6879 if (RequireCompleteType(ELoc, Type, 6880 diag::err_omp_reduction_incomplete_type)) 6881 continue; 6882 // OpenMP [2.14.3.6, reduction clause, Restrictions] 6883 // Arrays may not appear in a reduction clause. 6884 if (Type.getNonReferenceType()->isArrayType()) { 6885 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange; 6886 if (!ASE && !OASE) { 6887 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 6888 VarDecl::DeclarationOnly; 6889 Diag(VD->getLocation(), 6890 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 6891 << VD; 6892 } 6893 continue; 6894 } 6895 // OpenMP [2.14.3.6, reduction clause, Restrictions] 6896 // A list item that appears in a reduction clause must not be 6897 // const-qualified. 6898 if (Type.getNonReferenceType().isConstant(Context)) { 6899 Diag(ELoc, diag::err_omp_const_reduction_list_item) 6900 << getOpenMPClauseName(OMPC_reduction) << Type << ERange; 6901 if (!ASE && !OASE) { 6902 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 6903 VarDecl::DeclarationOnly; 6904 Diag(VD->getLocation(), 6905 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 6906 << VD; 6907 } 6908 continue; 6909 } 6910 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 6911 // If a list-item is a reference type then it must bind to the same object 6912 // for all threads of the team. 6913 if (!ASE && !OASE) { 6914 VarDecl *VDDef = VD->getDefinition(); 6915 if (Type->isReferenceType() && VDDef) { 6916 DSARefChecker Check(DSAStack); 6917 if (Check.Visit(VDDef->getInit())) { 6918 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange; 6919 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 6920 continue; 6921 } 6922 } 6923 } 6924 // OpenMP [2.14.3.6, reduction clause, Restrictions] 6925 // The type of a list item that appears in a reduction clause must be valid 6926 // for the reduction-identifier. For a max or min reduction in C, the type 6927 // of the list item must be an allowed arithmetic data type: char, int, 6928 // float, double, or _Bool, possibly modified with long, short, signed, or 6929 // unsigned. For a max or min reduction in C++, the type of the list item 6930 // must be an allowed arithmetic data type: char, wchar_t, int, float, 6931 // double, or bool, possibly modified with long, short, signed, or unsigned. 6932 if ((BOK == BO_GT || BOK == BO_LT) && 6933 !(Type->isScalarType() || 6934 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 6935 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 6936 << getLangOpts().CPlusPlus; 6937 if (!ASE && !OASE) { 6938 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 6939 VarDecl::DeclarationOnly; 6940 Diag(VD->getLocation(), 6941 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 6942 << VD; 6943 } 6944 continue; 6945 } 6946 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 6947 !getLangOpts().CPlusPlus && Type->isFloatingType()) { 6948 Diag(ELoc, diag::err_omp_clause_floating_type_arg); 6949 if (!ASE && !OASE) { 6950 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 6951 VarDecl::DeclarationOnly; 6952 Diag(VD->getLocation(), 6953 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 6954 << VD; 6955 } 6956 continue; 6957 } 6958 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 6959 // in a Construct] 6960 // Variables with the predetermined data-sharing attributes may not be 6961 // listed in data-sharing attributes clauses, except for the cases 6962 // listed below. For these exceptions only, listing a predetermined 6963 // variable in a data-sharing attribute clause is allowed and overrides 6964 // the variable's predetermined data-sharing attributes. 6965 // OpenMP [2.14.3.6, Restrictions, p.3] 6966 // Any number of reduction clauses can be specified on the directive, 6967 // but a list item can appear only once in the reduction clauses for that 6968 // directive. 6969 DSAStackTy::DSAVarData DVar; 6970 DVar = DSAStack->getTopDSA(VD, false); 6971 if (DVar.CKind == OMPC_reduction) { 6972 Diag(ELoc, diag::err_omp_once_referenced) 6973 << getOpenMPClauseName(OMPC_reduction); 6974 if (DVar.RefExpr) { 6975 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 6976 } 6977 } else if (DVar.CKind != OMPC_unknown) { 6978 Diag(ELoc, diag::err_omp_wrong_dsa) 6979 << getOpenMPClauseName(DVar.CKind) 6980 << getOpenMPClauseName(OMPC_reduction); 6981 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6982 continue; 6983 } 6984 6985 // OpenMP [2.14.3.6, Restrictions, p.1] 6986 // A list item that appears in a reduction clause of a worksharing 6987 // construct must be shared in the parallel regions to which any of the 6988 // worksharing regions arising from the worksharing construct bind. 6989 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 6990 if (isOpenMPWorksharingDirective(CurrDir) && 6991 !isOpenMPParallelDirective(CurrDir)) { 6992 DVar = DSAStack->getImplicitDSA(VD, true); 6993 if (DVar.CKind != OMPC_shared) { 6994 Diag(ELoc, diag::err_omp_required_access) 6995 << getOpenMPClauseName(OMPC_reduction) 6996 << getOpenMPClauseName(OMPC_shared); 6997 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6998 continue; 6999 } 7000 } 7001 7002 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 7003 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs", 7004 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 7005 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(), 7006 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 7007 auto PrivateTy = Type; 7008 if (OASE) { 7009 // For array sections only: 7010 // Create pseudo array type for private copy. The size for this array will 7011 // be generated during codegen. 7012 // For array subscripts or single variables Private Ty is the same as Type 7013 // (type of the variable or single array element). 7014 PrivateTy = Context.getVariableArrayType( 7015 Type, new (Context) OpaqueValueExpr(SourceLocation(), 7016 Context.getSizeType(), VK_RValue), 7017 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 7018 } 7019 // Private copy. 7020 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(), 7021 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 7022 // Add initializer for private variable. 7023 Expr *Init = nullptr; 7024 switch (BOK) { 7025 case BO_Add: 7026 case BO_Xor: 7027 case BO_Or: 7028 case BO_LOr: 7029 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 7030 if (Type->isScalarType() || Type->isAnyComplexType()) { 7031 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 7032 } 7033 break; 7034 case BO_Mul: 7035 case BO_LAnd: 7036 if (Type->isScalarType() || Type->isAnyComplexType()) { 7037 // '*' and '&&' reduction ops - initializer is '1'. 7038 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 7039 } 7040 break; 7041 case BO_And: { 7042 // '&' reduction op - initializer is '~0'. 7043 QualType OrigType = Type; 7044 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) { 7045 Type = ComplexTy->getElementType(); 7046 } 7047 if (Type->isRealFloatingType()) { 7048 llvm::APFloat InitValue = 7049 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type), 7050 /*isIEEE=*/true); 7051 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 7052 Type, ELoc); 7053 } else if (Type->isScalarType()) { 7054 auto Size = Context.getTypeSize(Type); 7055 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 7056 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 7057 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 7058 } 7059 if (Init && OrigType->isAnyComplexType()) { 7060 // Init = 0xFFFF + 0xFFFFi; 7061 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 7062 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 7063 } 7064 Type = OrigType; 7065 break; 7066 } 7067 case BO_LT: 7068 case BO_GT: { 7069 // 'min' reduction op - initializer is 'Largest representable number in 7070 // the reduction list item type'. 7071 // 'max' reduction op - initializer is 'Least representable number in 7072 // the reduction list item type'. 7073 if (Type->isIntegerType() || Type->isPointerType()) { 7074 bool IsSigned = Type->hasSignedIntegerRepresentation(); 7075 auto Size = Context.getTypeSize(Type); 7076 QualType IntTy = 7077 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 7078 llvm::APInt InitValue = 7079 (BOK != BO_LT) 7080 ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 7081 : llvm::APInt::getMinValue(Size) 7082 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 7083 : llvm::APInt::getMaxValue(Size); 7084 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 7085 if (Type->isPointerType()) { 7086 // Cast to pointer type. 7087 auto CastExpr = BuildCStyleCastExpr( 7088 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc), 7089 SourceLocation(), Init); 7090 if (CastExpr.isInvalid()) 7091 continue; 7092 Init = CastExpr.get(); 7093 } 7094 } else if (Type->isRealFloatingType()) { 7095 llvm::APFloat InitValue = llvm::APFloat::getLargest( 7096 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 7097 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 7098 Type, ELoc); 7099 } 7100 break; 7101 } 7102 case BO_PtrMemD: 7103 case BO_PtrMemI: 7104 case BO_MulAssign: 7105 case BO_Div: 7106 case BO_Rem: 7107 case BO_Sub: 7108 case BO_Shl: 7109 case BO_Shr: 7110 case BO_LE: 7111 case BO_GE: 7112 case BO_EQ: 7113 case BO_NE: 7114 case BO_AndAssign: 7115 case BO_XorAssign: 7116 case BO_OrAssign: 7117 case BO_Assign: 7118 case BO_AddAssign: 7119 case BO_SubAssign: 7120 case BO_DivAssign: 7121 case BO_RemAssign: 7122 case BO_ShlAssign: 7123 case BO_ShrAssign: 7124 case BO_Comma: 7125 llvm_unreachable("Unexpected reduction operation"); 7126 } 7127 if (Init) { 7128 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false, 7129 /*TypeMayContainAuto=*/false); 7130 } else 7131 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false); 7132 if (!RHSVD->hasInit()) { 7133 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type 7134 << ReductionIdRange; 7135 if (VD) { 7136 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 7137 VarDecl::DeclarationOnly; 7138 Diag(VD->getLocation(), 7139 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 7140 << VD; 7141 } 7142 continue; 7143 } 7144 // Store initializer for single element in private copy. Will be used during 7145 // codegen. 7146 PrivateVD->setInit(RHSVD->getInit()); 7147 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 7148 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc); 7149 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc); 7150 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc); 7151 ExprResult ReductionOp = 7152 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK, 7153 LHSDRE, RHSDRE); 7154 if (ReductionOp.isUsable()) { 7155 if (BOK != BO_LT && BOK != BO_GT) { 7156 ReductionOp = 7157 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), 7158 BO_Assign, LHSDRE, ReductionOp.get()); 7159 } else { 7160 auto *ConditionalOp = new (Context) ConditionalOperator( 7161 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(), 7162 RHSDRE, Type, VK_LValue, OK_Ordinary); 7163 ReductionOp = 7164 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), 7165 BO_Assign, LHSDRE, ConditionalOp); 7166 } 7167 ReductionOp = ActOnFinishFullExpr(ReductionOp.get()); 7168 } 7169 if (ReductionOp.isInvalid()) 7170 continue; 7171 7172 DSAStack->addDSA(VD, DE, OMPC_reduction); 7173 Vars.push_back(RefExpr); 7174 Privates.push_back(PrivateDRE); 7175 LHSs.push_back(LHSDRE); 7176 RHSs.push_back(RHSDRE); 7177 ReductionOps.push_back(ReductionOp.get()); 7178 } 7179 7180 if (Vars.empty()) 7181 return nullptr; 7182 7183 return OMPReductionClause::Create( 7184 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars, 7185 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates, 7186 LHSs, RHSs, ReductionOps); 7187 } 7188 7189 OMPClause *Sema::ActOnOpenMPLinearClause( 7190 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 7191 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 7192 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 7193 SmallVector<Expr *, 8> Vars; 7194 SmallVector<Expr *, 8> Privates; 7195 SmallVector<Expr *, 8> Inits; 7196 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 7197 LinKind == OMPC_LINEAR_unknown) { 7198 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 7199 LinKind = OMPC_LINEAR_val; 7200 } 7201 for (auto &RefExpr : VarList) { 7202 assert(RefExpr && "NULL expr in OpenMP linear clause."); 7203 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 7204 // It will be analyzed later. 7205 Vars.push_back(RefExpr); 7206 Privates.push_back(nullptr); 7207 Inits.push_back(nullptr); 7208 continue; 7209 } 7210 7211 // OpenMP [2.14.3.7, linear clause] 7212 // A list item that appears in a linear clause is subject to the private 7213 // clause semantics described in Section 2.14.3.3 on page 159 except as 7214 // noted. In addition, the value of the new list item on each iteration 7215 // of the associated loop(s) corresponds to the value of the original 7216 // list item before entering the construct plus the logical number of 7217 // the iteration times linear-step. 7218 7219 SourceLocation ELoc = RefExpr->getExprLoc(); 7220 // OpenMP [2.1, C/C++] 7221 // A list item is a variable name. 7222 // OpenMP [2.14.3.3, Restrictions, p.1] 7223 // A variable that is part of another variable (as an array or 7224 // structure element) cannot appear in a private clause. 7225 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 7226 if (!DE || !isa<VarDecl>(DE->getDecl())) { 7227 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 7228 continue; 7229 } 7230 7231 VarDecl *VD = cast<VarDecl>(DE->getDecl()); 7232 7233 // OpenMP [2.14.3.7, linear clause] 7234 // A list-item cannot appear in more than one linear clause. 7235 // A list-item that appears in a linear clause cannot appear in any 7236 // other data-sharing attribute clause. 7237 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); 7238 if (DVar.RefExpr) { 7239 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 7240 << getOpenMPClauseName(OMPC_linear); 7241 ReportOriginalDSA(*this, DSAStack, VD, DVar); 7242 continue; 7243 } 7244 7245 QualType QType = VD->getType(); 7246 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 7247 // It will be analyzed later. 7248 Vars.push_back(DE); 7249 Privates.push_back(nullptr); 7250 Inits.push_back(nullptr); 7251 continue; 7252 } 7253 7254 // A variable must not have an incomplete type or a reference type. 7255 if (RequireCompleteType(ELoc, QType, 7256 diag::err_omp_linear_incomplete_type)) { 7257 continue; 7258 } 7259 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 7260 !QType->isReferenceType()) { 7261 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 7262 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 7263 continue; 7264 } 7265 QType = QType.getNonReferenceType(); 7266 7267 // A list item must not be const-qualified. 7268 if (QType.isConstant(Context)) { 7269 Diag(ELoc, diag::err_omp_const_variable) 7270 << getOpenMPClauseName(OMPC_linear); 7271 bool IsDecl = 7272 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 7273 Diag(VD->getLocation(), 7274 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 7275 << VD; 7276 continue; 7277 } 7278 7279 // A list item must be of integral or pointer type. 7280 QType = QType.getUnqualifiedType().getCanonicalType(); 7281 const Type *Ty = QType.getTypePtrOrNull(); 7282 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && 7283 !Ty->isPointerType())) { 7284 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType; 7285 bool IsDecl = 7286 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 7287 Diag(VD->getLocation(), 7288 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 7289 << VD; 7290 continue; 7291 } 7292 7293 // Build private copy of original var. 7294 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(), 7295 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 7296 auto *PrivateRef = buildDeclRefExpr( 7297 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc()); 7298 // Build var to save initial value. 7299 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start"); 7300 Expr *InitExpr; 7301 if (LinKind == OMPC_LINEAR_uval) 7302 InitExpr = VD->getInit(); 7303 else 7304 InitExpr = DE; 7305 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 7306 /*DirectInit*/ false, /*TypeMayContainAuto*/ false); 7307 auto InitRef = buildDeclRefExpr( 7308 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc()); 7309 DSAStack->addDSA(VD, DE, OMPC_linear); 7310 Vars.push_back(DE); 7311 Privates.push_back(PrivateRef); 7312 Inits.push_back(InitRef); 7313 } 7314 7315 if (Vars.empty()) 7316 return nullptr; 7317 7318 Expr *StepExpr = Step; 7319 Expr *CalcStepExpr = nullptr; 7320 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 7321 !Step->isInstantiationDependent() && 7322 !Step->containsUnexpandedParameterPack()) { 7323 SourceLocation StepLoc = Step->getLocStart(); 7324 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 7325 if (Val.isInvalid()) 7326 return nullptr; 7327 StepExpr = Val.get(); 7328 7329 // Build var to save the step value. 7330 VarDecl *SaveVar = 7331 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 7332 ExprResult SaveRef = 7333 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 7334 ExprResult CalcStep = 7335 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 7336 CalcStep = ActOnFinishFullExpr(CalcStep.get()); 7337 7338 // Warn about zero linear step (it would be probably better specified as 7339 // making corresponding variables 'const'). 7340 llvm::APSInt Result; 7341 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); 7342 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) 7343 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] 7344 << (Vars.size() > 1); 7345 if (!IsConstant && CalcStep.isUsable()) { 7346 // Calculate the step beforehand instead of doing this on each iteration. 7347 // (This is not used if the number of iterations may be kfold-ed). 7348 CalcStepExpr = CalcStep.get(); 7349 } 7350 } 7351 7352 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 7353 ColonLoc, EndLoc, Vars, Privates, Inits, 7354 StepExpr, CalcStepExpr); 7355 } 7356 7357 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 7358 Expr *NumIterations, Sema &SemaRef, 7359 Scope *S) { 7360 // Walk the vars and build update/final expressions for the CodeGen. 7361 SmallVector<Expr *, 8> Updates; 7362 SmallVector<Expr *, 8> Finals; 7363 Expr *Step = Clause.getStep(); 7364 Expr *CalcStep = Clause.getCalcStep(); 7365 // OpenMP [2.14.3.7, linear clause] 7366 // If linear-step is not specified it is assumed to be 1. 7367 if (Step == nullptr) 7368 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 7369 else if (CalcStep) 7370 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 7371 bool HasErrors = false; 7372 auto CurInit = Clause.inits().begin(); 7373 auto CurPrivate = Clause.privates().begin(); 7374 auto LinKind = Clause.getModifier(); 7375 for (auto &RefExpr : Clause.varlists()) { 7376 Expr *InitExpr = *CurInit; 7377 7378 // Build privatized reference to the current linear var. 7379 auto DE = cast<DeclRefExpr>(RefExpr); 7380 Expr *CapturedRef; 7381 if (LinKind == OMPC_LINEAR_uval) 7382 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 7383 else 7384 CapturedRef = 7385 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 7386 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 7387 /*RefersToCapture=*/true); 7388 7389 // Build update: Var = InitExpr + IV * Step 7390 ExprResult Update = 7391 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, 7392 InitExpr, IV, Step, /* Subtract */ false); 7393 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(), 7394 /*DiscardedValue=*/true); 7395 7396 // Build final: Var = InitExpr + NumIterations * Step 7397 ExprResult Final = 7398 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 7399 InitExpr, NumIterations, Step, /* Subtract */ false); 7400 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(), 7401 /*DiscardedValue=*/true); 7402 if (!Update.isUsable() || !Final.isUsable()) { 7403 Updates.push_back(nullptr); 7404 Finals.push_back(nullptr); 7405 HasErrors = true; 7406 } else { 7407 Updates.push_back(Update.get()); 7408 Finals.push_back(Final.get()); 7409 } 7410 ++CurInit, ++CurPrivate; 7411 } 7412 Clause.setUpdates(Updates); 7413 Clause.setFinals(Finals); 7414 return HasErrors; 7415 } 7416 7417 OMPClause *Sema::ActOnOpenMPAlignedClause( 7418 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 7419 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 7420 7421 SmallVector<Expr *, 8> Vars; 7422 for (auto &RefExpr : VarList) { 7423 assert(RefExpr && "NULL expr in OpenMP aligned clause."); 7424 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 7425 // It will be analyzed later. 7426 Vars.push_back(RefExpr); 7427 continue; 7428 } 7429 7430 SourceLocation ELoc = RefExpr->getExprLoc(); 7431 // OpenMP [2.1, C/C++] 7432 // A list item is a variable name. 7433 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 7434 if (!DE || !isa<VarDecl>(DE->getDecl())) { 7435 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 7436 continue; 7437 } 7438 7439 VarDecl *VD = cast<VarDecl>(DE->getDecl()); 7440 7441 // OpenMP [2.8.1, simd construct, Restrictions] 7442 // The type of list items appearing in the aligned clause must be 7443 // array, pointer, reference to array, or reference to pointer. 7444 QualType QType = VD->getType(); 7445 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 7446 const Type *Ty = QType.getTypePtrOrNull(); 7447 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() && 7448 !Ty->isPointerType())) { 7449 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 7450 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange(); 7451 bool IsDecl = 7452 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 7453 Diag(VD->getLocation(), 7454 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 7455 << VD; 7456 continue; 7457 } 7458 7459 // OpenMP [2.8.1, simd construct, Restrictions] 7460 // A list-item cannot appear in more than one aligned clause. 7461 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) { 7462 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange(); 7463 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 7464 << getOpenMPClauseName(OMPC_aligned); 7465 continue; 7466 } 7467 7468 Vars.push_back(DE); 7469 } 7470 7471 // OpenMP [2.8.1, simd construct, Description] 7472 // The parameter of the aligned clause, alignment, must be a constant 7473 // positive integer expression. 7474 // If no optional parameter is specified, implementation-defined default 7475 // alignments for SIMD instructions on the target platforms are assumed. 7476 if (Alignment != nullptr) { 7477 ExprResult AlignResult = 7478 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 7479 if (AlignResult.isInvalid()) 7480 return nullptr; 7481 Alignment = AlignResult.get(); 7482 } 7483 if (Vars.empty()) 7484 return nullptr; 7485 7486 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 7487 EndLoc, Vars, Alignment); 7488 } 7489 7490 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 7491 SourceLocation StartLoc, 7492 SourceLocation LParenLoc, 7493 SourceLocation EndLoc) { 7494 SmallVector<Expr *, 8> Vars; 7495 SmallVector<Expr *, 8> SrcExprs; 7496 SmallVector<Expr *, 8> DstExprs; 7497 SmallVector<Expr *, 8> AssignmentOps; 7498 for (auto &RefExpr : VarList) { 7499 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 7500 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 7501 // It will be analyzed later. 7502 Vars.push_back(RefExpr); 7503 SrcExprs.push_back(nullptr); 7504 DstExprs.push_back(nullptr); 7505 AssignmentOps.push_back(nullptr); 7506 continue; 7507 } 7508 7509 SourceLocation ELoc = RefExpr->getExprLoc(); 7510 // OpenMP [2.1, C/C++] 7511 // A list item is a variable name. 7512 // OpenMP [2.14.4.1, Restrictions, p.1] 7513 // A list item that appears in a copyin clause must be threadprivate. 7514 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 7515 if (!DE || !isa<VarDecl>(DE->getDecl())) { 7516 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 7517 continue; 7518 } 7519 7520 Decl *D = DE->getDecl(); 7521 VarDecl *VD = cast<VarDecl>(D); 7522 7523 QualType Type = VD->getType(); 7524 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 7525 // It will be analyzed later. 7526 Vars.push_back(DE); 7527 SrcExprs.push_back(nullptr); 7528 DstExprs.push_back(nullptr); 7529 AssignmentOps.push_back(nullptr); 7530 continue; 7531 } 7532 7533 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 7534 // A list item that appears in a copyin clause must be threadprivate. 7535 if (!DSAStack->isThreadPrivate(VD)) { 7536 Diag(ELoc, diag::err_omp_required_access) 7537 << getOpenMPClauseName(OMPC_copyin) 7538 << getOpenMPDirectiveName(OMPD_threadprivate); 7539 continue; 7540 } 7541 7542 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 7543 // A variable of class type (or array thereof) that appears in a 7544 // copyin clause requires an accessible, unambiguous copy assignment 7545 // operator for the class type. 7546 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 7547 auto *SrcVD = 7548 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(), 7549 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 7550 auto *PseudoSrcExpr = buildDeclRefExpr( 7551 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 7552 auto *DstVD = 7553 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst", 7554 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 7555 auto *PseudoDstExpr = 7556 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 7557 // For arrays generate assignment operation for single element and replace 7558 // it by the original array element in CodeGen. 7559 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, 7560 PseudoDstExpr, PseudoSrcExpr); 7561 if (AssignmentOp.isInvalid()) 7562 continue; 7563 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 7564 /*DiscardedValue=*/true); 7565 if (AssignmentOp.isInvalid()) 7566 continue; 7567 7568 DSAStack->addDSA(VD, DE, OMPC_copyin); 7569 Vars.push_back(DE); 7570 SrcExprs.push_back(PseudoSrcExpr); 7571 DstExprs.push_back(PseudoDstExpr); 7572 AssignmentOps.push_back(AssignmentOp.get()); 7573 } 7574 7575 if (Vars.empty()) 7576 return nullptr; 7577 7578 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 7579 SrcExprs, DstExprs, AssignmentOps); 7580 } 7581 7582 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 7583 SourceLocation StartLoc, 7584 SourceLocation LParenLoc, 7585 SourceLocation EndLoc) { 7586 SmallVector<Expr *, 8> Vars; 7587 SmallVector<Expr *, 8> SrcExprs; 7588 SmallVector<Expr *, 8> DstExprs; 7589 SmallVector<Expr *, 8> AssignmentOps; 7590 for (auto &RefExpr : VarList) { 7591 assert(RefExpr && "NULL expr in OpenMP copyprivate clause."); 7592 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 7593 // It will be analyzed later. 7594 Vars.push_back(RefExpr); 7595 SrcExprs.push_back(nullptr); 7596 DstExprs.push_back(nullptr); 7597 AssignmentOps.push_back(nullptr); 7598 continue; 7599 } 7600 7601 SourceLocation ELoc = RefExpr->getExprLoc(); 7602 // OpenMP [2.1, C/C++] 7603 // A list item is a variable name. 7604 // OpenMP [2.14.4.1, Restrictions, p.1] 7605 // A list item that appears in a copyin clause must be threadprivate. 7606 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 7607 if (!DE || !isa<VarDecl>(DE->getDecl())) { 7608 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 7609 continue; 7610 } 7611 7612 Decl *D = DE->getDecl(); 7613 VarDecl *VD = cast<VarDecl>(D); 7614 7615 QualType Type = VD->getType(); 7616 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 7617 // It will be analyzed later. 7618 Vars.push_back(DE); 7619 SrcExprs.push_back(nullptr); 7620 DstExprs.push_back(nullptr); 7621 AssignmentOps.push_back(nullptr); 7622 continue; 7623 } 7624 7625 // OpenMP [2.14.4.2, Restrictions, p.2] 7626 // A list item that appears in a copyprivate clause may not appear in a 7627 // private or firstprivate clause on the single construct. 7628 if (!DSAStack->isThreadPrivate(VD)) { 7629 auto DVar = DSAStack->getTopDSA(VD, false); 7630 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 7631 DVar.RefExpr) { 7632 Diag(ELoc, diag::err_omp_wrong_dsa) 7633 << getOpenMPClauseName(DVar.CKind) 7634 << getOpenMPClauseName(OMPC_copyprivate); 7635 ReportOriginalDSA(*this, DSAStack, VD, DVar); 7636 continue; 7637 } 7638 7639 // OpenMP [2.11.4.2, Restrictions, p.1] 7640 // All list items that appear in a copyprivate clause must be either 7641 // threadprivate or private in the enclosing context. 7642 if (DVar.CKind == OMPC_unknown) { 7643 DVar = DSAStack->getImplicitDSA(VD, false); 7644 if (DVar.CKind == OMPC_shared) { 7645 Diag(ELoc, diag::err_omp_required_access) 7646 << getOpenMPClauseName(OMPC_copyprivate) 7647 << "threadprivate or private in the enclosing context"; 7648 ReportOriginalDSA(*this, DSAStack, VD, DVar); 7649 continue; 7650 } 7651 } 7652 } 7653 7654 // Variably modified types are not supported. 7655 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 7656 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 7657 << getOpenMPClauseName(OMPC_copyprivate) << Type 7658 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 7659 bool IsDecl = 7660 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 7661 Diag(VD->getLocation(), 7662 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 7663 << VD; 7664 continue; 7665 } 7666 7667 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 7668 // A variable of class type (or array thereof) that appears in a 7669 // copyin clause requires an accessible, unambiguous copy assignment 7670 // operator for the class type. 7671 Type = Context.getBaseElementType(Type.getNonReferenceType()) 7672 .getUnqualifiedType(); 7673 auto *SrcVD = 7674 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src", 7675 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 7676 auto *PseudoSrcExpr = 7677 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc()); 7678 auto *DstVD = 7679 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst", 7680 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 7681 auto *PseudoDstExpr = 7682 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc()); 7683 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, 7684 PseudoDstExpr, PseudoSrcExpr); 7685 if (AssignmentOp.isInvalid()) 7686 continue; 7687 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 7688 /*DiscardedValue=*/true); 7689 if (AssignmentOp.isInvalid()) 7690 continue; 7691 7692 // No need to mark vars as copyprivate, they are already threadprivate or 7693 // implicitly private. 7694 Vars.push_back(DE); 7695 SrcExprs.push_back(PseudoSrcExpr); 7696 DstExprs.push_back(PseudoDstExpr); 7697 AssignmentOps.push_back(AssignmentOp.get()); 7698 } 7699 7700 if (Vars.empty()) 7701 return nullptr; 7702 7703 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 7704 Vars, SrcExprs, DstExprs, AssignmentOps); 7705 } 7706 7707 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 7708 SourceLocation StartLoc, 7709 SourceLocation LParenLoc, 7710 SourceLocation EndLoc) { 7711 if (VarList.empty()) 7712 return nullptr; 7713 7714 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 7715 } 7716 7717 OMPClause * 7718 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind, 7719 SourceLocation DepLoc, SourceLocation ColonLoc, 7720 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 7721 SourceLocation LParenLoc, SourceLocation EndLoc) { 7722 if (DepKind == OMPC_DEPEND_unknown) { 7723 std::string Values; 7724 std::string Sep(", "); 7725 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) { 7726 Values += "'"; 7727 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i); 7728 Values += "'"; 7729 switch (i) { 7730 case OMPC_DEPEND_unknown - 2: 7731 Values += " or "; 7732 break; 7733 case OMPC_DEPEND_unknown - 1: 7734 break; 7735 default: 7736 Values += Sep; 7737 break; 7738 } 7739 } 7740 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 7741 << Values << getOpenMPClauseName(OMPC_depend); 7742 return nullptr; 7743 } 7744 SmallVector<Expr *, 8> Vars; 7745 for (auto &RefExpr : VarList) { 7746 assert(RefExpr && "NULL expr in OpenMP shared clause."); 7747 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 7748 // It will be analyzed later. 7749 Vars.push_back(RefExpr); 7750 continue; 7751 } 7752 7753 SourceLocation ELoc = RefExpr->getExprLoc(); 7754 // OpenMP [2.11.1.1, Restrictions, p.3] 7755 // A variable that is part of another variable (such as a field of a 7756 // structure) but is not an array element or an array section cannot appear 7757 // in a depend clause. 7758 auto *SimpleExpr = RefExpr->IgnoreParenCasts(); 7759 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr); 7760 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 7761 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); 7762 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 7763 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) || 7764 (ASE && !ASE->getBase()->getType()->isAnyPointerType() && 7765 !ASE->getBase()->getType()->isArrayType())) { 7766 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item) 7767 << RefExpr->getSourceRange(); 7768 continue; 7769 } 7770 7771 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 7772 } 7773 7774 if (Vars.empty()) 7775 return nullptr; 7776 7777 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind, 7778 DepLoc, ColonLoc, Vars); 7779 } 7780 7781 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc, 7782 SourceLocation LParenLoc, 7783 SourceLocation EndLoc) { 7784 Expr *ValExpr = Device; 7785 7786 // OpenMP [2.9.1, Restrictions] 7787 // The device expression must evaluate to a non-negative integer value. 7788 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 7789 /*StrictlyPositive=*/false)) 7790 return nullptr; 7791 7792 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc); 7793 } 7794 7795 static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc, 7796 DSAStackTy *Stack, CXXRecordDecl *RD) { 7797 if (!RD || RD->isInvalidDecl()) 7798 return true; 7799 7800 auto QTy = SemaRef.Context.getRecordType(RD); 7801 if (RD->isDynamicClass()) { 7802 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; 7803 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target); 7804 return false; 7805 } 7806 auto *DC = RD; 7807 bool IsCorrect = true; 7808 for (auto *I : DC->decls()) { 7809 if (I) { 7810 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) { 7811 if (MD->isStatic()) { 7812 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; 7813 SemaRef.Diag(MD->getLocation(), 7814 diag::note_omp_static_member_in_target); 7815 IsCorrect = false; 7816 } 7817 } else if (auto *VD = dyn_cast<VarDecl>(I)) { 7818 if (VD->isStaticDataMember()) { 7819 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; 7820 SemaRef.Diag(VD->getLocation(), 7821 diag::note_omp_static_member_in_target); 7822 IsCorrect = false; 7823 } 7824 } 7825 } 7826 } 7827 7828 for (auto &I : RD->bases()) { 7829 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack, 7830 I.getType()->getAsCXXRecordDecl())) 7831 IsCorrect = false; 7832 } 7833 return IsCorrect; 7834 } 7835 7836 static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 7837 DSAStackTy *Stack, QualType QTy) { 7838 NamedDecl *ND; 7839 if (QTy->isIncompleteType(&ND)) { 7840 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 7841 return false; 7842 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) { 7843 if (!RD->isInvalidDecl() && 7844 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD)) 7845 return false; 7846 } 7847 return true; 7848 } 7849 7850 OMPClause *Sema::ActOnOpenMPMapClause( 7851 OpenMPMapClauseKind MapTypeModifier, OpenMPMapClauseKind MapType, 7852 SourceLocation MapLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 7853 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 7854 SmallVector<Expr *, 4> Vars; 7855 7856 for (auto &RE : VarList) { 7857 assert(RE && "Null expr in omp map"); 7858 if (isa<DependentScopeDeclRefExpr>(RE)) { 7859 // It will be analyzed later. 7860 Vars.push_back(RE); 7861 continue; 7862 } 7863 SourceLocation ELoc = RE->getExprLoc(); 7864 7865 // OpenMP [2.14.5, Restrictions] 7866 // A variable that is part of another variable (such as field of a 7867 // structure) but is not an array element or an array section cannot appear 7868 // in a map clause. 7869 auto *VE = RE->IgnoreParenLValueCasts(); 7870 7871 if (VE->isValueDependent() || VE->isTypeDependent() || 7872 VE->isInstantiationDependent() || 7873 VE->containsUnexpandedParameterPack()) { 7874 // It will be analyzed later. 7875 Vars.push_back(RE); 7876 continue; 7877 } 7878 7879 auto *SimpleExpr = RE->IgnoreParenCasts(); 7880 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr); 7881 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 7882 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); 7883 7884 if (!RE->IgnoreParenImpCasts()->isLValue() || 7885 (!OASE && !ASE && !DE) || 7886 (DE && !isa<VarDecl>(DE->getDecl())) || 7887 (ASE && !ASE->getBase()->getType()->isAnyPointerType() && 7888 !ASE->getBase()->getType()->isArrayType())) { 7889 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item) 7890 << RE->getSourceRange(); 7891 continue; 7892 } 7893 7894 Decl *D = nullptr; 7895 if (DE) { 7896 D = DE->getDecl(); 7897 } else if (ASE) { 7898 auto *B = ASE->getBase()->IgnoreParenCasts(); 7899 D = dyn_cast<DeclRefExpr>(B)->getDecl(); 7900 } else if (OASE) { 7901 auto *B = OASE->getBase(); 7902 D = dyn_cast<DeclRefExpr>(B)->getDecl(); 7903 } 7904 assert(D && "Null decl on map clause."); 7905 auto *VD = cast<VarDecl>(D); 7906 7907 // OpenMP [2.14.5, Restrictions, p.8] 7908 // threadprivate variables cannot appear in a map clause. 7909 if (DSAStack->isThreadPrivate(VD)) { 7910 auto DVar = DSAStack->getTopDSA(VD, false); 7911 Diag(ELoc, diag::err_omp_threadprivate_in_map); 7912 ReportOriginalDSA(*this, DSAStack, VD, DVar); 7913 continue; 7914 } 7915 7916 // OpenMP [2.14.5, Restrictions, p.2] 7917 // At most one list item can be an array item derived from a given variable 7918 // in map clauses of the same construct. 7919 // OpenMP [2.14.5, Restrictions, p.3] 7920 // List items of map clauses in the same construct must not share original 7921 // storage. 7922 // OpenMP [2.14.5, Restrictions, C/C++, p.2] 7923 // A variable for which the type is pointer, reference to array, or 7924 // reference to pointer and an array section derived from that variable 7925 // must not appear as list items of map clauses of the same construct. 7926 DSAStackTy::MapInfo MI = DSAStack->IsMappedInCurrentRegion(VD); 7927 if (MI.RefExpr) { 7928 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc; 7929 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here) 7930 << MI.RefExpr->getSourceRange(); 7931 continue; 7932 } 7933 7934 // OpenMP [2.14.5, Restrictions, C/C++, p.3,4] 7935 // A variable for which the type is pointer, reference to array, or 7936 // reference to pointer must not appear as a list item if the enclosing 7937 // device data environment already contains an array section derived from 7938 // that variable. 7939 // An array section derived from a variable for which the type is pointer, 7940 // reference to array, or reference to pointer must not appear as a list 7941 // item if the enclosing device data environment already contains that 7942 // variable. 7943 QualType Type = VD->getType(); 7944 MI = DSAStack->getMapInfoForVar(VD); 7945 if (MI.RefExpr && (isa<DeclRefExpr>(MI.RefExpr->IgnoreParenLValueCasts()) != 7946 isa<DeclRefExpr>(VE)) && 7947 (Type->isPointerType() || Type->isReferenceType())) { 7948 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc; 7949 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here) 7950 << MI.RefExpr->getSourceRange(); 7951 continue; 7952 } 7953 7954 // OpenMP [2.14.5, Restrictions, C/C++, p.7] 7955 // A list item must have a mappable type. 7956 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this, 7957 DSAStack, Type)) 7958 continue; 7959 7960 Vars.push_back(RE); 7961 MI.RefExpr = RE; 7962 DSAStack->addMapInfoForVar(VD, MI); 7963 } 7964 if (Vars.empty()) 7965 return nullptr; 7966 7967 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 7968 MapTypeModifier, MapType, MapLoc); 7969 } 7970 7971 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 7972 SourceLocation StartLoc, 7973 SourceLocation LParenLoc, 7974 SourceLocation EndLoc) { 7975 Expr *ValExpr = NumTeams; 7976 7977 // OpenMP [teams Constrcut, Restrictions] 7978 // The num_teams expression must evaluate to a positive integer value. 7979 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 7980 /*StrictlyPositive=*/true)) 7981 return nullptr; 7982 7983 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc); 7984 } 7985 7986 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 7987 SourceLocation StartLoc, 7988 SourceLocation LParenLoc, 7989 SourceLocation EndLoc) { 7990 Expr *ValExpr = ThreadLimit; 7991 7992 // OpenMP [teams Constrcut, Restrictions] 7993 // The thread_limit expression must evaluate to a positive integer value. 7994 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 7995 /*StrictlyPositive=*/true)) 7996 return nullptr; 7997 7998 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc, 7999 EndLoc); 8000 } 8001 8002 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 8003 SourceLocation StartLoc, 8004 SourceLocation LParenLoc, 8005 SourceLocation EndLoc) { 8006 Expr *ValExpr = Priority; 8007 8008 // OpenMP [2.9.1, task Constrcut] 8009 // The priority-value is a non-negative numerical scalar expression. 8010 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority, 8011 /*StrictlyPositive=*/false)) 8012 return nullptr; 8013 8014 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc); 8015 } 8016 8017 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 8018 SourceLocation StartLoc, 8019 SourceLocation LParenLoc, 8020 SourceLocation EndLoc) { 8021 Expr *ValExpr = Grainsize; 8022 8023 // OpenMP [2.9.2, taskloop Constrcut] 8024 // The parameter of the grainsize clause must be a positive integer 8025 // expression. 8026 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize, 8027 /*StrictlyPositive=*/true)) 8028 return nullptr; 8029 8030 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc); 8031 } 8032