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_if: 5385 case OMPC_default: 5386 case OMPC_proc_bind: 5387 case OMPC_schedule: 5388 case OMPC_private: 5389 case OMPC_firstprivate: 5390 case OMPC_lastprivate: 5391 case OMPC_shared: 5392 case OMPC_reduction: 5393 case OMPC_linear: 5394 case OMPC_aligned: 5395 case OMPC_copyin: 5396 case OMPC_copyprivate: 5397 case OMPC_nowait: 5398 case OMPC_untied: 5399 case OMPC_mergeable: 5400 case OMPC_threadprivate: 5401 case OMPC_flush: 5402 case OMPC_read: 5403 case OMPC_write: 5404 case OMPC_update: 5405 case OMPC_capture: 5406 case OMPC_seq_cst: 5407 case OMPC_depend: 5408 case OMPC_threads: 5409 case OMPC_simd: 5410 case OMPC_map: 5411 case OMPC_unknown: 5412 llvm_unreachable("Clause is not allowed."); 5413 } 5414 return Res; 5415 } 5416 5417 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 5418 Expr *Condition, SourceLocation StartLoc, 5419 SourceLocation LParenLoc, 5420 SourceLocation NameModifierLoc, 5421 SourceLocation ColonLoc, 5422 SourceLocation EndLoc) { 5423 Expr *ValExpr = Condition; 5424 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 5425 !Condition->isInstantiationDependent() && 5426 !Condition->containsUnexpandedParameterPack()) { 5427 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(), 5428 Condition->getExprLoc(), Condition); 5429 if (Val.isInvalid()) 5430 return nullptr; 5431 5432 ValExpr = Val.get(); 5433 } 5434 5435 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc, 5436 NameModifierLoc, ColonLoc, EndLoc); 5437 } 5438 5439 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 5440 SourceLocation StartLoc, 5441 SourceLocation LParenLoc, 5442 SourceLocation EndLoc) { 5443 Expr *ValExpr = Condition; 5444 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 5445 !Condition->isInstantiationDependent() && 5446 !Condition->containsUnexpandedParameterPack()) { 5447 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(), 5448 Condition->getExprLoc(), Condition); 5449 if (Val.isInvalid()) 5450 return nullptr; 5451 5452 ValExpr = Val.get(); 5453 } 5454 5455 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc); 5456 } 5457 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 5458 Expr *Op) { 5459 if (!Op) 5460 return ExprError(); 5461 5462 class IntConvertDiagnoser : public ICEConvertDiagnoser { 5463 public: 5464 IntConvertDiagnoser() 5465 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 5466 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 5467 QualType T) override { 5468 return S.Diag(Loc, diag::err_omp_not_integral) << T; 5469 } 5470 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 5471 QualType T) override { 5472 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 5473 } 5474 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 5475 QualType T, 5476 QualType ConvTy) override { 5477 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 5478 } 5479 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 5480 QualType ConvTy) override { 5481 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 5482 << ConvTy->isEnumeralType() << ConvTy; 5483 } 5484 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 5485 QualType T) override { 5486 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 5487 } 5488 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 5489 QualType ConvTy) override { 5490 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 5491 << ConvTy->isEnumeralType() << ConvTy; 5492 } 5493 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 5494 QualType) override { 5495 llvm_unreachable("conversion functions are permitted"); 5496 } 5497 } ConvertDiagnoser; 5498 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 5499 } 5500 5501 static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, 5502 OpenMPClauseKind CKind, 5503 bool StrictlyPositive) { 5504 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 5505 !ValExpr->isInstantiationDependent()) { 5506 SourceLocation Loc = ValExpr->getExprLoc(); 5507 ExprResult Value = 5508 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 5509 if (Value.isInvalid()) 5510 return false; 5511 5512 ValExpr = Value.get(); 5513 // The expression must evaluate to a non-negative integer value. 5514 llvm::APSInt Result; 5515 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) && 5516 Result.isSigned() && 5517 !((!StrictlyPositive && Result.isNonNegative()) || 5518 (StrictlyPositive && Result.isStrictlyPositive()))) { 5519 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 5520 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 5521 << ValExpr->getSourceRange(); 5522 return false; 5523 } 5524 } 5525 return true; 5526 } 5527 5528 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 5529 SourceLocation StartLoc, 5530 SourceLocation LParenLoc, 5531 SourceLocation EndLoc) { 5532 Expr *ValExpr = NumThreads; 5533 5534 // OpenMP [2.5, Restrictions] 5535 // The num_threads expression must evaluate to a positive integer value. 5536 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 5537 /*StrictlyPositive=*/true)) 5538 return nullptr; 5539 5540 return new (Context) 5541 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc); 5542 } 5543 5544 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 5545 OpenMPClauseKind CKind) { 5546 if (!E) 5547 return ExprError(); 5548 if (E->isValueDependent() || E->isTypeDependent() || 5549 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 5550 return E; 5551 llvm::APSInt Result; 5552 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); 5553 if (ICE.isInvalid()) 5554 return ExprError(); 5555 if (!Result.isStrictlyPositive()) { 5556 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 5557 << getOpenMPClauseName(CKind) << 1 << E->getSourceRange(); 5558 return ExprError(); 5559 } 5560 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 5561 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 5562 << E->getSourceRange(); 5563 return ExprError(); 5564 } 5565 if (CKind == OMPC_collapse) 5566 DSAStack->setCollapseNumber(Result.getExtValue()); 5567 else if (CKind == OMPC_ordered) 5568 DSAStack->setCollapseNumber(Result.getExtValue()); 5569 return ICE; 5570 } 5571 5572 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 5573 SourceLocation LParenLoc, 5574 SourceLocation EndLoc) { 5575 // OpenMP [2.8.1, simd construct, Description] 5576 // The parameter of the safelen clause must be a constant 5577 // positive integer expression. 5578 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 5579 if (Safelen.isInvalid()) 5580 return nullptr; 5581 return new (Context) 5582 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 5583 } 5584 5585 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 5586 SourceLocation LParenLoc, 5587 SourceLocation EndLoc) { 5588 // OpenMP [2.8.1, simd construct, Description] 5589 // The parameter of the simdlen clause must be a constant 5590 // positive integer expression. 5591 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 5592 if (Simdlen.isInvalid()) 5593 return nullptr; 5594 return new (Context) 5595 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 5596 } 5597 5598 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 5599 SourceLocation StartLoc, 5600 SourceLocation LParenLoc, 5601 SourceLocation EndLoc) { 5602 // OpenMP [2.7.1, loop construct, Description] 5603 // OpenMP [2.8.1, simd construct, Description] 5604 // OpenMP [2.9.6, distribute construct, Description] 5605 // The parameter of the collapse clause must be a constant 5606 // positive integer expression. 5607 ExprResult NumForLoopsResult = 5608 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 5609 if (NumForLoopsResult.isInvalid()) 5610 return nullptr; 5611 return new (Context) 5612 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 5613 } 5614 5615 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 5616 SourceLocation EndLoc, 5617 SourceLocation LParenLoc, 5618 Expr *NumForLoops) { 5619 // OpenMP [2.7.1, loop construct, Description] 5620 // OpenMP [2.8.1, simd construct, Description] 5621 // OpenMP [2.9.6, distribute construct, Description] 5622 // The parameter of the ordered clause must be a constant 5623 // positive integer expression if any. 5624 if (NumForLoops && LParenLoc.isValid()) { 5625 ExprResult NumForLoopsResult = 5626 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 5627 if (NumForLoopsResult.isInvalid()) 5628 return nullptr; 5629 NumForLoops = NumForLoopsResult.get(); 5630 } else 5631 NumForLoops = nullptr; 5632 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops); 5633 return new (Context) 5634 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc); 5635 } 5636 5637 OMPClause *Sema::ActOnOpenMPSimpleClause( 5638 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 5639 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 5640 OMPClause *Res = nullptr; 5641 switch (Kind) { 5642 case OMPC_default: 5643 Res = 5644 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), 5645 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 5646 break; 5647 case OMPC_proc_bind: 5648 Res = ActOnOpenMPProcBindClause( 5649 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, 5650 LParenLoc, EndLoc); 5651 break; 5652 case OMPC_if: 5653 case OMPC_final: 5654 case OMPC_num_threads: 5655 case OMPC_safelen: 5656 case OMPC_simdlen: 5657 case OMPC_collapse: 5658 case OMPC_schedule: 5659 case OMPC_private: 5660 case OMPC_firstprivate: 5661 case OMPC_lastprivate: 5662 case OMPC_shared: 5663 case OMPC_reduction: 5664 case OMPC_linear: 5665 case OMPC_aligned: 5666 case OMPC_copyin: 5667 case OMPC_copyprivate: 5668 case OMPC_ordered: 5669 case OMPC_nowait: 5670 case OMPC_untied: 5671 case OMPC_mergeable: 5672 case OMPC_threadprivate: 5673 case OMPC_flush: 5674 case OMPC_read: 5675 case OMPC_write: 5676 case OMPC_update: 5677 case OMPC_capture: 5678 case OMPC_seq_cst: 5679 case OMPC_depend: 5680 case OMPC_device: 5681 case OMPC_threads: 5682 case OMPC_simd: 5683 case OMPC_map: 5684 case OMPC_num_teams: 5685 case OMPC_thread_limit: 5686 case OMPC_priority: 5687 case OMPC_unknown: 5688 llvm_unreachable("Clause is not allowed."); 5689 } 5690 return Res; 5691 } 5692 5693 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, 5694 SourceLocation KindKwLoc, 5695 SourceLocation StartLoc, 5696 SourceLocation LParenLoc, 5697 SourceLocation EndLoc) { 5698 if (Kind == OMPC_DEFAULT_unknown) { 5699 std::string Values; 5700 static_assert(OMPC_DEFAULT_unknown > 0, 5701 "OMPC_DEFAULT_unknown not greater than 0"); 5702 std::string Sep(", "); 5703 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) { 5704 Values += "'"; 5705 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i); 5706 Values += "'"; 5707 switch (i) { 5708 case OMPC_DEFAULT_unknown - 2: 5709 Values += " or "; 5710 break; 5711 case OMPC_DEFAULT_unknown - 1: 5712 break; 5713 default: 5714 Values += Sep; 5715 break; 5716 } 5717 } 5718 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 5719 << Values << getOpenMPClauseName(OMPC_default); 5720 return nullptr; 5721 } 5722 switch (Kind) { 5723 case OMPC_DEFAULT_none: 5724 DSAStack->setDefaultDSANone(KindKwLoc); 5725 break; 5726 case OMPC_DEFAULT_shared: 5727 DSAStack->setDefaultDSAShared(KindKwLoc); 5728 break; 5729 case OMPC_DEFAULT_unknown: 5730 llvm_unreachable("Clause kind is not allowed."); 5731 break; 5732 } 5733 return new (Context) 5734 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 5735 } 5736 5737 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, 5738 SourceLocation KindKwLoc, 5739 SourceLocation StartLoc, 5740 SourceLocation LParenLoc, 5741 SourceLocation EndLoc) { 5742 if (Kind == OMPC_PROC_BIND_unknown) { 5743 std::string Values; 5744 std::string Sep(", "); 5745 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) { 5746 Values += "'"; 5747 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i); 5748 Values += "'"; 5749 switch (i) { 5750 case OMPC_PROC_BIND_unknown - 2: 5751 Values += " or "; 5752 break; 5753 case OMPC_PROC_BIND_unknown - 1: 5754 break; 5755 default: 5756 Values += Sep; 5757 break; 5758 } 5759 } 5760 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 5761 << Values << getOpenMPClauseName(OMPC_proc_bind); 5762 return nullptr; 5763 } 5764 return new (Context) 5765 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 5766 } 5767 5768 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 5769 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr, 5770 SourceLocation StartLoc, SourceLocation LParenLoc, 5771 SourceLocation ArgumentLoc, SourceLocation DelimLoc, 5772 SourceLocation EndLoc) { 5773 OMPClause *Res = nullptr; 5774 switch (Kind) { 5775 case OMPC_schedule: 5776 Res = ActOnOpenMPScheduleClause( 5777 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc, 5778 LParenLoc, ArgumentLoc, DelimLoc, EndLoc); 5779 break; 5780 case OMPC_if: 5781 Res = 5782 ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument), Expr, 5783 StartLoc, LParenLoc, ArgumentLoc, DelimLoc, EndLoc); 5784 break; 5785 case OMPC_final: 5786 case OMPC_num_threads: 5787 case OMPC_safelen: 5788 case OMPC_simdlen: 5789 case OMPC_collapse: 5790 case OMPC_default: 5791 case OMPC_proc_bind: 5792 case OMPC_private: 5793 case OMPC_firstprivate: 5794 case OMPC_lastprivate: 5795 case OMPC_shared: 5796 case OMPC_reduction: 5797 case OMPC_linear: 5798 case OMPC_aligned: 5799 case OMPC_copyin: 5800 case OMPC_copyprivate: 5801 case OMPC_ordered: 5802 case OMPC_nowait: 5803 case OMPC_untied: 5804 case OMPC_mergeable: 5805 case OMPC_threadprivate: 5806 case OMPC_flush: 5807 case OMPC_read: 5808 case OMPC_write: 5809 case OMPC_update: 5810 case OMPC_capture: 5811 case OMPC_seq_cst: 5812 case OMPC_depend: 5813 case OMPC_device: 5814 case OMPC_threads: 5815 case OMPC_simd: 5816 case OMPC_map: 5817 case OMPC_num_teams: 5818 case OMPC_thread_limit: 5819 case OMPC_priority: 5820 case OMPC_unknown: 5821 llvm_unreachable("Clause is not allowed."); 5822 } 5823 return Res; 5824 } 5825 5826 OMPClause *Sema::ActOnOpenMPScheduleClause( 5827 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 5828 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 5829 SourceLocation EndLoc) { 5830 if (Kind == OMPC_SCHEDULE_unknown) { 5831 std::string Values; 5832 std::string Sep(", "); 5833 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) { 5834 Values += "'"; 5835 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i); 5836 Values += "'"; 5837 switch (i) { 5838 case OMPC_SCHEDULE_unknown - 2: 5839 Values += " or "; 5840 break; 5841 case OMPC_SCHEDULE_unknown - 1: 5842 break; 5843 default: 5844 Values += Sep; 5845 break; 5846 } 5847 } 5848 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 5849 << Values << getOpenMPClauseName(OMPC_schedule); 5850 return nullptr; 5851 } 5852 Expr *ValExpr = ChunkSize; 5853 Expr *HelperValExpr = nullptr; 5854 if (ChunkSize) { 5855 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 5856 !ChunkSize->isInstantiationDependent() && 5857 !ChunkSize->containsUnexpandedParameterPack()) { 5858 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); 5859 ExprResult Val = 5860 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 5861 if (Val.isInvalid()) 5862 return nullptr; 5863 5864 ValExpr = Val.get(); 5865 5866 // OpenMP [2.7.1, Restrictions] 5867 // chunk_size must be a loop invariant integer expression with a positive 5868 // value. 5869 llvm::APSInt Result; 5870 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 5871 if (Result.isSigned() && !Result.isStrictlyPositive()) { 5872 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 5873 << "schedule" << 1 << ChunkSize->getSourceRange(); 5874 return nullptr; 5875 } 5876 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) { 5877 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(), 5878 ChunkSize->getType(), ".chunk."); 5879 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(), 5880 ChunkSize->getExprLoc(), 5881 /*RefersToCapture=*/true); 5882 HelperValExpr = ImpVarRef; 5883 } 5884 } 5885 } 5886 5887 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, 5888 EndLoc, Kind, ValExpr, HelperValExpr); 5889 } 5890 5891 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 5892 SourceLocation StartLoc, 5893 SourceLocation EndLoc) { 5894 OMPClause *Res = nullptr; 5895 switch (Kind) { 5896 case OMPC_ordered: 5897 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 5898 break; 5899 case OMPC_nowait: 5900 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 5901 break; 5902 case OMPC_untied: 5903 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 5904 break; 5905 case OMPC_mergeable: 5906 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 5907 break; 5908 case OMPC_read: 5909 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 5910 break; 5911 case OMPC_write: 5912 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 5913 break; 5914 case OMPC_update: 5915 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 5916 break; 5917 case OMPC_capture: 5918 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 5919 break; 5920 case OMPC_seq_cst: 5921 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 5922 break; 5923 case OMPC_threads: 5924 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 5925 break; 5926 case OMPC_simd: 5927 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 5928 break; 5929 case OMPC_if: 5930 case OMPC_final: 5931 case OMPC_num_threads: 5932 case OMPC_safelen: 5933 case OMPC_simdlen: 5934 case OMPC_collapse: 5935 case OMPC_schedule: 5936 case OMPC_private: 5937 case OMPC_firstprivate: 5938 case OMPC_lastprivate: 5939 case OMPC_shared: 5940 case OMPC_reduction: 5941 case OMPC_linear: 5942 case OMPC_aligned: 5943 case OMPC_copyin: 5944 case OMPC_copyprivate: 5945 case OMPC_default: 5946 case OMPC_proc_bind: 5947 case OMPC_threadprivate: 5948 case OMPC_flush: 5949 case OMPC_depend: 5950 case OMPC_device: 5951 case OMPC_map: 5952 case OMPC_num_teams: 5953 case OMPC_thread_limit: 5954 case OMPC_priority: 5955 case OMPC_unknown: 5956 llvm_unreachable("Clause is not allowed."); 5957 } 5958 return Res; 5959 } 5960 5961 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 5962 SourceLocation EndLoc) { 5963 DSAStack->setNowaitRegion(); 5964 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 5965 } 5966 5967 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 5968 SourceLocation EndLoc) { 5969 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 5970 } 5971 5972 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 5973 SourceLocation EndLoc) { 5974 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 5975 } 5976 5977 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 5978 SourceLocation EndLoc) { 5979 return new (Context) OMPReadClause(StartLoc, EndLoc); 5980 } 5981 5982 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 5983 SourceLocation EndLoc) { 5984 return new (Context) OMPWriteClause(StartLoc, EndLoc); 5985 } 5986 5987 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 5988 SourceLocation EndLoc) { 5989 return new (Context) OMPUpdateClause(StartLoc, EndLoc); 5990 } 5991 5992 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 5993 SourceLocation EndLoc) { 5994 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 5995 } 5996 5997 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 5998 SourceLocation EndLoc) { 5999 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 6000 } 6001 6002 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 6003 SourceLocation EndLoc) { 6004 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 6005 } 6006 6007 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 6008 SourceLocation EndLoc) { 6009 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 6010 } 6011 6012 OMPClause *Sema::ActOnOpenMPVarListClause( 6013 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, 6014 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 6015 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec, 6016 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind, 6017 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier, 6018 OpenMPMapClauseKind MapType, SourceLocation DepLinMapLoc) { 6019 OMPClause *Res = nullptr; 6020 switch (Kind) { 6021 case OMPC_private: 6022 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 6023 break; 6024 case OMPC_firstprivate: 6025 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 6026 break; 6027 case OMPC_lastprivate: 6028 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 6029 break; 6030 case OMPC_shared: 6031 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 6032 break; 6033 case OMPC_reduction: 6034 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 6035 EndLoc, ReductionIdScopeSpec, ReductionId); 6036 break; 6037 case OMPC_linear: 6038 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, 6039 LinKind, DepLinMapLoc, ColonLoc, EndLoc); 6040 break; 6041 case OMPC_aligned: 6042 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, 6043 ColonLoc, EndLoc); 6044 break; 6045 case OMPC_copyin: 6046 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 6047 break; 6048 case OMPC_copyprivate: 6049 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 6050 break; 6051 case OMPC_flush: 6052 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 6053 break; 6054 case OMPC_depend: 6055 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList, 6056 StartLoc, LParenLoc, EndLoc); 6057 break; 6058 case OMPC_map: 6059 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, DepLinMapLoc, ColonLoc, 6060 VarList, StartLoc, LParenLoc, EndLoc); 6061 break; 6062 case OMPC_if: 6063 case OMPC_final: 6064 case OMPC_num_threads: 6065 case OMPC_safelen: 6066 case OMPC_simdlen: 6067 case OMPC_collapse: 6068 case OMPC_default: 6069 case OMPC_proc_bind: 6070 case OMPC_schedule: 6071 case OMPC_ordered: 6072 case OMPC_nowait: 6073 case OMPC_untied: 6074 case OMPC_mergeable: 6075 case OMPC_threadprivate: 6076 case OMPC_read: 6077 case OMPC_write: 6078 case OMPC_update: 6079 case OMPC_capture: 6080 case OMPC_seq_cst: 6081 case OMPC_device: 6082 case OMPC_threads: 6083 case OMPC_simd: 6084 case OMPC_num_teams: 6085 case OMPC_thread_limit: 6086 case OMPC_priority: 6087 case OMPC_unknown: 6088 llvm_unreachable("Clause is not allowed."); 6089 } 6090 return Res; 6091 } 6092 6093 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 6094 SourceLocation StartLoc, 6095 SourceLocation LParenLoc, 6096 SourceLocation EndLoc) { 6097 SmallVector<Expr *, 8> Vars; 6098 SmallVector<Expr *, 8> PrivateCopies; 6099 for (auto &RefExpr : VarList) { 6100 assert(RefExpr && "NULL expr in OpenMP private clause."); 6101 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 6102 // It will be analyzed later. 6103 Vars.push_back(RefExpr); 6104 PrivateCopies.push_back(nullptr); 6105 continue; 6106 } 6107 6108 SourceLocation ELoc = RefExpr->getExprLoc(); 6109 // OpenMP [2.1, C/C++] 6110 // A list item is a variable name. 6111 // OpenMP [2.9.3.3, Restrictions, p.1] 6112 // A variable that is part of another variable (as an array or 6113 // structure element) cannot appear in a private clause. 6114 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 6115 if (!DE || !isa<VarDecl>(DE->getDecl())) { 6116 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 6117 continue; 6118 } 6119 Decl *D = DE->getDecl(); 6120 VarDecl *VD = cast<VarDecl>(D); 6121 6122 QualType Type = VD->getType(); 6123 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 6124 // It will be analyzed later. 6125 Vars.push_back(DE); 6126 PrivateCopies.push_back(nullptr); 6127 continue; 6128 } 6129 6130 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 6131 // A variable that appears in a private clause must not have an incomplete 6132 // type or a reference type. 6133 if (RequireCompleteType(ELoc, Type, 6134 diag::err_omp_private_incomplete_type)) { 6135 continue; 6136 } 6137 Type = Type.getNonReferenceType(); 6138 6139 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 6140 // in a Construct] 6141 // Variables with the predetermined data-sharing attributes may not be 6142 // listed in data-sharing attributes clauses, except for the cases 6143 // listed below. For these exceptions only, listing a predetermined 6144 // variable in a data-sharing attribute clause is allowed and overrides 6145 // the variable's predetermined data-sharing attributes. 6146 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); 6147 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 6148 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 6149 << getOpenMPClauseName(OMPC_private); 6150 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6151 continue; 6152 } 6153 6154 // Variably modified types are not supported for tasks. 6155 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 6156 DSAStack->getCurrentDirective() == OMPD_task) { 6157 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 6158 << getOpenMPClauseName(OMPC_private) << Type 6159 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 6160 bool IsDecl = 6161 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 6162 Diag(VD->getLocation(), 6163 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 6164 << VD; 6165 continue; 6166 } 6167 6168 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 6169 // A variable of class type (or array thereof) that appears in a private 6170 // clause requires an accessible, unambiguous default constructor for the 6171 // class type. 6172 // Generate helper private variable and initialize it with the default 6173 // value. The address of the original variable is replaced by the address of 6174 // the new private variable in CodeGen. This new variable is not added to 6175 // IdResolver, so the code in the OpenMP region uses original variable for 6176 // proper diagnostics. 6177 Type = Type.getUnqualifiedType(); 6178 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName(), 6179 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 6180 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false); 6181 if (VDPrivate->isInvalidDecl()) 6182 continue; 6183 auto VDPrivateRefExpr = buildDeclRefExpr( 6184 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc()); 6185 6186 DSAStack->addDSA(VD, DE, OMPC_private); 6187 Vars.push_back(DE); 6188 PrivateCopies.push_back(VDPrivateRefExpr); 6189 } 6190 6191 if (Vars.empty()) 6192 return nullptr; 6193 6194 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 6195 PrivateCopies); 6196 } 6197 6198 namespace { 6199 class DiagsUninitializedSeveretyRAII { 6200 private: 6201 DiagnosticsEngine &Diags; 6202 SourceLocation SavedLoc; 6203 bool IsIgnored; 6204 6205 public: 6206 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 6207 bool IsIgnored) 6208 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 6209 if (!IsIgnored) { 6210 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 6211 /*Map*/ diag::Severity::Ignored, Loc); 6212 } 6213 } 6214 ~DiagsUninitializedSeveretyRAII() { 6215 if (!IsIgnored) 6216 Diags.popMappings(SavedLoc); 6217 } 6218 }; 6219 } 6220 6221 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 6222 SourceLocation StartLoc, 6223 SourceLocation LParenLoc, 6224 SourceLocation EndLoc) { 6225 SmallVector<Expr *, 8> Vars; 6226 SmallVector<Expr *, 8> PrivateCopies; 6227 SmallVector<Expr *, 8> Inits; 6228 bool IsImplicitClause = 6229 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 6230 auto ImplicitClauseLoc = DSAStack->getConstructLoc(); 6231 6232 for (auto &RefExpr : VarList) { 6233 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 6234 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 6235 // It will be analyzed later. 6236 Vars.push_back(RefExpr); 6237 PrivateCopies.push_back(nullptr); 6238 Inits.push_back(nullptr); 6239 continue; 6240 } 6241 6242 SourceLocation ELoc = 6243 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc(); 6244 // OpenMP [2.1, C/C++] 6245 // A list item is a variable name. 6246 // OpenMP [2.9.3.3, Restrictions, p.1] 6247 // A variable that is part of another variable (as an array or 6248 // structure element) cannot appear in a private clause. 6249 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 6250 if (!DE || !isa<VarDecl>(DE->getDecl())) { 6251 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 6252 continue; 6253 } 6254 Decl *D = DE->getDecl(); 6255 VarDecl *VD = cast<VarDecl>(D); 6256 6257 QualType Type = VD->getType(); 6258 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 6259 // It will be analyzed later. 6260 Vars.push_back(DE); 6261 PrivateCopies.push_back(nullptr); 6262 Inits.push_back(nullptr); 6263 continue; 6264 } 6265 6266 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 6267 // A variable that appears in a private clause must not have an incomplete 6268 // type or a reference type. 6269 if (RequireCompleteType(ELoc, Type, 6270 diag::err_omp_firstprivate_incomplete_type)) { 6271 continue; 6272 } 6273 Type = Type.getNonReferenceType(); 6274 6275 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 6276 // A variable of class type (or array thereof) that appears in a private 6277 // clause requires an accessible, unambiguous copy constructor for the 6278 // class type. 6279 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 6280 6281 // If an implicit firstprivate variable found it was checked already. 6282 if (!IsImplicitClause) { 6283 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); 6284 bool IsConstant = ElemType.isConstant(Context); 6285 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 6286 // A list item that specifies a given variable may not appear in more 6287 // than one clause on the same directive, except that a variable may be 6288 // specified in both firstprivate and lastprivate clauses. 6289 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 6290 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) { 6291 Diag(ELoc, diag::err_omp_wrong_dsa) 6292 << getOpenMPClauseName(DVar.CKind) 6293 << getOpenMPClauseName(OMPC_firstprivate); 6294 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6295 continue; 6296 } 6297 6298 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 6299 // in a Construct] 6300 // Variables with the predetermined data-sharing attributes may not be 6301 // listed in data-sharing attributes clauses, except for the cases 6302 // listed below. For these exceptions only, listing a predetermined 6303 // variable in a data-sharing attribute clause is allowed and overrides 6304 // the variable's predetermined data-sharing attributes. 6305 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 6306 // in a Construct, C/C++, p.2] 6307 // Variables with const-qualified type having no mutable member may be 6308 // listed in a firstprivate clause, even if they are static data members. 6309 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr && 6310 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 6311 Diag(ELoc, diag::err_omp_wrong_dsa) 6312 << getOpenMPClauseName(DVar.CKind) 6313 << getOpenMPClauseName(OMPC_firstprivate); 6314 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6315 continue; 6316 } 6317 6318 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 6319 // OpenMP [2.9.3.4, Restrictions, p.2] 6320 // A list item that is private within a parallel region must not appear 6321 // in a firstprivate clause on a worksharing construct if any of the 6322 // worksharing regions arising from the worksharing construct ever bind 6323 // to any of the parallel regions arising from the parallel construct. 6324 if (isOpenMPWorksharingDirective(CurrDir) && 6325 !isOpenMPParallelDirective(CurrDir)) { 6326 DVar = DSAStack->getImplicitDSA(VD, true); 6327 if (DVar.CKind != OMPC_shared && 6328 (isOpenMPParallelDirective(DVar.DKind) || 6329 DVar.DKind == OMPD_unknown)) { 6330 Diag(ELoc, diag::err_omp_required_access) 6331 << getOpenMPClauseName(OMPC_firstprivate) 6332 << getOpenMPClauseName(OMPC_shared); 6333 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6334 continue; 6335 } 6336 } 6337 // OpenMP [2.9.3.4, Restrictions, p.3] 6338 // A list item that appears in a reduction clause of a parallel construct 6339 // must not appear in a firstprivate clause on a worksharing or task 6340 // construct if any of the worksharing or task regions arising from the 6341 // worksharing or task construct ever bind to any of the parallel regions 6342 // arising from the parallel construct. 6343 // OpenMP [2.9.3.4, Restrictions, p.4] 6344 // A list item that appears in a reduction clause in worksharing 6345 // construct must not appear in a firstprivate clause in a task construct 6346 // encountered during execution of any of the worksharing regions arising 6347 // from the worksharing construct. 6348 if (CurrDir == OMPD_task) { 6349 DVar = 6350 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction), 6351 [](OpenMPDirectiveKind K) -> bool { 6352 return isOpenMPParallelDirective(K) || 6353 isOpenMPWorksharingDirective(K); 6354 }, 6355 false); 6356 if (DVar.CKind == OMPC_reduction && 6357 (isOpenMPParallelDirective(DVar.DKind) || 6358 isOpenMPWorksharingDirective(DVar.DKind))) { 6359 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 6360 << getOpenMPDirectiveName(DVar.DKind); 6361 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6362 continue; 6363 } 6364 } 6365 } 6366 6367 // Variably modified types are not supported for tasks. 6368 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 6369 DSAStack->getCurrentDirective() == OMPD_task) { 6370 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 6371 << getOpenMPClauseName(OMPC_firstprivate) << Type 6372 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 6373 bool IsDecl = 6374 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 6375 Diag(VD->getLocation(), 6376 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 6377 << VD; 6378 continue; 6379 } 6380 6381 Type = Type.getUnqualifiedType(); 6382 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(), 6383 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 6384 // Generate helper private variable and initialize it with the value of the 6385 // original variable. The address of the original variable is replaced by 6386 // the address of the new private variable in the CodeGen. This new variable 6387 // is not added to IdResolver, so the code in the OpenMP region uses 6388 // original variable for proper diagnostics and variable capturing. 6389 Expr *VDInitRefExpr = nullptr; 6390 // For arrays generate initializer for single element and replace it by the 6391 // original array element in CodeGen. 6392 if (Type->isArrayType()) { 6393 auto VDInit = 6394 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName()); 6395 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 6396 auto Init = DefaultLvalueConversion(VDInitRefExpr).get(); 6397 ElemType = ElemType.getUnqualifiedType(); 6398 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType, 6399 ".firstprivate.temp"); 6400 InitializedEntity Entity = 6401 InitializedEntity::InitializeVariable(VDInitTemp); 6402 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 6403 6404 InitializationSequence InitSeq(*this, Entity, Kind, Init); 6405 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 6406 if (Result.isInvalid()) 6407 VDPrivate->setInvalidDecl(); 6408 else 6409 VDPrivate->setInit(Result.getAs<Expr>()); 6410 // Remove temp variable declaration. 6411 Context.Deallocate(VDInitTemp); 6412 } else { 6413 auto *VDInit = 6414 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp"); 6415 VDInitRefExpr = 6416 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc()); 6417 AddInitializerToDecl(VDPrivate, 6418 DefaultLvalueConversion(VDInitRefExpr).get(), 6419 /*DirectInit=*/false, /*TypeMayContainAuto=*/false); 6420 } 6421 if (VDPrivate->isInvalidDecl()) { 6422 if (IsImplicitClause) { 6423 Diag(DE->getExprLoc(), 6424 diag::note_omp_task_predetermined_firstprivate_here); 6425 } 6426 continue; 6427 } 6428 CurContext->addDecl(VDPrivate); 6429 auto VDPrivateRefExpr = buildDeclRefExpr( 6430 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc()); 6431 DSAStack->addDSA(VD, DE, OMPC_firstprivate); 6432 Vars.push_back(DE); 6433 PrivateCopies.push_back(VDPrivateRefExpr); 6434 Inits.push_back(VDInitRefExpr); 6435 } 6436 6437 if (Vars.empty()) 6438 return nullptr; 6439 6440 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 6441 Vars, PrivateCopies, Inits); 6442 } 6443 6444 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList, 6445 SourceLocation StartLoc, 6446 SourceLocation LParenLoc, 6447 SourceLocation EndLoc) { 6448 SmallVector<Expr *, 8> Vars; 6449 SmallVector<Expr *, 8> SrcExprs; 6450 SmallVector<Expr *, 8> DstExprs; 6451 SmallVector<Expr *, 8> AssignmentOps; 6452 for (auto &RefExpr : VarList) { 6453 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 6454 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 6455 // It will be analyzed later. 6456 Vars.push_back(RefExpr); 6457 SrcExprs.push_back(nullptr); 6458 DstExprs.push_back(nullptr); 6459 AssignmentOps.push_back(nullptr); 6460 continue; 6461 } 6462 6463 SourceLocation ELoc = RefExpr->getExprLoc(); 6464 // OpenMP [2.1, C/C++] 6465 // A list item is a variable name. 6466 // OpenMP [2.14.3.5, Restrictions, p.1] 6467 // A variable that is part of another variable (as an array or structure 6468 // element) cannot appear in a lastprivate clause. 6469 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 6470 if (!DE || !isa<VarDecl>(DE->getDecl())) { 6471 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 6472 continue; 6473 } 6474 Decl *D = DE->getDecl(); 6475 VarDecl *VD = cast<VarDecl>(D); 6476 6477 QualType Type = VD->getType(); 6478 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 6479 // It will be analyzed later. 6480 Vars.push_back(DE); 6481 SrcExprs.push_back(nullptr); 6482 DstExprs.push_back(nullptr); 6483 AssignmentOps.push_back(nullptr); 6484 continue; 6485 } 6486 6487 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 6488 // A variable that appears in a lastprivate clause must not have an 6489 // incomplete type or a reference type. 6490 if (RequireCompleteType(ELoc, Type, 6491 diag::err_omp_lastprivate_incomplete_type)) { 6492 continue; 6493 } 6494 Type = Type.getNonReferenceType(); 6495 6496 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 6497 // in a Construct] 6498 // Variables with the predetermined data-sharing attributes may not be 6499 // listed in data-sharing attributes clauses, except for the cases 6500 // listed below. 6501 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); 6502 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 6503 DVar.CKind != OMPC_firstprivate && 6504 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 6505 Diag(ELoc, diag::err_omp_wrong_dsa) 6506 << getOpenMPClauseName(DVar.CKind) 6507 << getOpenMPClauseName(OMPC_lastprivate); 6508 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6509 continue; 6510 } 6511 6512 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 6513 // OpenMP [2.14.3.5, Restrictions, p.2] 6514 // A list item that is private within a parallel region, or that appears in 6515 // the reduction clause of a parallel construct, must not appear in a 6516 // lastprivate clause on a worksharing construct if any of the corresponding 6517 // worksharing regions ever binds to any of the corresponding parallel 6518 // regions. 6519 DSAStackTy::DSAVarData TopDVar = DVar; 6520 if (isOpenMPWorksharingDirective(CurrDir) && 6521 !isOpenMPParallelDirective(CurrDir)) { 6522 DVar = DSAStack->getImplicitDSA(VD, true); 6523 if (DVar.CKind != OMPC_shared) { 6524 Diag(ELoc, diag::err_omp_required_access) 6525 << getOpenMPClauseName(OMPC_lastprivate) 6526 << getOpenMPClauseName(OMPC_shared); 6527 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6528 continue; 6529 } 6530 } 6531 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 6532 // A variable of class type (or array thereof) that appears in a 6533 // lastprivate clause requires an accessible, unambiguous default 6534 // constructor for the class type, unless the list item is also specified 6535 // in a firstprivate clause. 6536 // A variable of class type (or array thereof) that appears in a 6537 // lastprivate clause requires an accessible, unambiguous copy assignment 6538 // operator for the class type. 6539 Type = Context.getBaseElementType(Type).getNonReferenceType(); 6540 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(), 6541 Type.getUnqualifiedType(), ".lastprivate.src", 6542 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 6543 auto *PseudoSrcExpr = buildDeclRefExpr( 6544 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc()); 6545 auto *DstVD = 6546 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst", 6547 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 6548 auto *PseudoDstExpr = 6549 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc()); 6550 // For arrays generate assignment operation for single element and replace 6551 // it by the original array element in CodeGen. 6552 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, 6553 PseudoDstExpr, PseudoSrcExpr); 6554 if (AssignmentOp.isInvalid()) 6555 continue; 6556 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 6557 /*DiscardedValue=*/true); 6558 if (AssignmentOp.isInvalid()) 6559 continue; 6560 6561 if (TopDVar.CKind != OMPC_firstprivate) 6562 DSAStack->addDSA(VD, DE, OMPC_lastprivate); 6563 Vars.push_back(DE); 6564 SrcExprs.push_back(PseudoSrcExpr); 6565 DstExprs.push_back(PseudoDstExpr); 6566 AssignmentOps.push_back(AssignmentOp.get()); 6567 } 6568 6569 if (Vars.empty()) 6570 return nullptr; 6571 6572 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 6573 Vars, SrcExprs, DstExprs, AssignmentOps); 6574 } 6575 6576 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 6577 SourceLocation StartLoc, 6578 SourceLocation LParenLoc, 6579 SourceLocation EndLoc) { 6580 SmallVector<Expr *, 8> Vars; 6581 for (auto &RefExpr : VarList) { 6582 assert(RefExpr && "NULL expr in OpenMP shared clause."); 6583 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 6584 // It will be analyzed later. 6585 Vars.push_back(RefExpr); 6586 continue; 6587 } 6588 6589 SourceLocation ELoc = RefExpr->getExprLoc(); 6590 // OpenMP [2.1, C/C++] 6591 // A list item is a variable name. 6592 // OpenMP [2.14.3.2, Restrictions, p.1] 6593 // A variable that is part of another variable (as an array or structure 6594 // element) cannot appear in a shared unless it is a static data member 6595 // of a C++ class. 6596 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 6597 if (!DE || !isa<VarDecl>(DE->getDecl())) { 6598 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 6599 continue; 6600 } 6601 Decl *D = DE->getDecl(); 6602 VarDecl *VD = cast<VarDecl>(D); 6603 6604 QualType Type = VD->getType(); 6605 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 6606 // It will be analyzed later. 6607 Vars.push_back(DE); 6608 continue; 6609 } 6610 6611 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 6612 // in a Construct] 6613 // Variables with the predetermined data-sharing attributes may not be 6614 // listed in data-sharing attributes clauses, except for the cases 6615 // listed below. For these exceptions only, listing a predetermined 6616 // variable in a data-sharing attribute clause is allowed and overrides 6617 // the variable's predetermined data-sharing attributes. 6618 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); 6619 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 6620 DVar.RefExpr) { 6621 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 6622 << getOpenMPClauseName(OMPC_shared); 6623 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6624 continue; 6625 } 6626 6627 DSAStack->addDSA(VD, DE, OMPC_shared); 6628 Vars.push_back(DE); 6629 } 6630 6631 if (Vars.empty()) 6632 return nullptr; 6633 6634 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 6635 } 6636 6637 namespace { 6638 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 6639 DSAStackTy *Stack; 6640 6641 public: 6642 bool VisitDeclRefExpr(DeclRefExpr *E) { 6643 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) { 6644 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false); 6645 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 6646 return false; 6647 if (DVar.CKind != OMPC_unknown) 6648 return true; 6649 DSAStackTy::DSAVarData DVarPrivate = 6650 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false); 6651 if (DVarPrivate.CKind != OMPC_unknown) 6652 return true; 6653 return false; 6654 } 6655 return false; 6656 } 6657 bool VisitStmt(Stmt *S) { 6658 for (auto Child : S->children()) { 6659 if (Child && Visit(Child)) 6660 return true; 6661 } 6662 return false; 6663 } 6664 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 6665 }; 6666 } // namespace 6667 6668 OMPClause *Sema::ActOnOpenMPReductionClause( 6669 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 6670 SourceLocation ColonLoc, SourceLocation EndLoc, 6671 CXXScopeSpec &ReductionIdScopeSpec, 6672 const DeclarationNameInfo &ReductionId) { 6673 // TODO: Allow scope specification search when 'declare reduction' is 6674 // supported. 6675 assert(ReductionIdScopeSpec.isEmpty() && 6676 "No support for scoped reduction identifiers yet."); 6677 6678 auto DN = ReductionId.getName(); 6679 auto OOK = DN.getCXXOverloadedOperator(); 6680 BinaryOperatorKind BOK = BO_Comma; 6681 6682 // OpenMP [2.14.3.6, reduction clause] 6683 // C 6684 // reduction-identifier is either an identifier or one of the following 6685 // operators: +, -, *, &, |, ^, && and || 6686 // C++ 6687 // reduction-identifier is either an id-expression or one of the following 6688 // operators: +, -, *, &, |, ^, && and || 6689 // FIXME: Only 'min' and 'max' identifiers are supported for now. 6690 switch (OOK) { 6691 case OO_Plus: 6692 case OO_Minus: 6693 BOK = BO_Add; 6694 break; 6695 case OO_Star: 6696 BOK = BO_Mul; 6697 break; 6698 case OO_Amp: 6699 BOK = BO_And; 6700 break; 6701 case OO_Pipe: 6702 BOK = BO_Or; 6703 break; 6704 case OO_Caret: 6705 BOK = BO_Xor; 6706 break; 6707 case OO_AmpAmp: 6708 BOK = BO_LAnd; 6709 break; 6710 case OO_PipePipe: 6711 BOK = BO_LOr; 6712 break; 6713 case OO_New: 6714 case OO_Delete: 6715 case OO_Array_New: 6716 case OO_Array_Delete: 6717 case OO_Slash: 6718 case OO_Percent: 6719 case OO_Tilde: 6720 case OO_Exclaim: 6721 case OO_Equal: 6722 case OO_Less: 6723 case OO_Greater: 6724 case OO_LessEqual: 6725 case OO_GreaterEqual: 6726 case OO_PlusEqual: 6727 case OO_MinusEqual: 6728 case OO_StarEqual: 6729 case OO_SlashEqual: 6730 case OO_PercentEqual: 6731 case OO_CaretEqual: 6732 case OO_AmpEqual: 6733 case OO_PipeEqual: 6734 case OO_LessLess: 6735 case OO_GreaterGreater: 6736 case OO_LessLessEqual: 6737 case OO_GreaterGreaterEqual: 6738 case OO_EqualEqual: 6739 case OO_ExclaimEqual: 6740 case OO_PlusPlus: 6741 case OO_MinusMinus: 6742 case OO_Comma: 6743 case OO_ArrowStar: 6744 case OO_Arrow: 6745 case OO_Call: 6746 case OO_Subscript: 6747 case OO_Conditional: 6748 case OO_Coawait: 6749 case NUM_OVERLOADED_OPERATORS: 6750 llvm_unreachable("Unexpected reduction identifier"); 6751 case OO_None: 6752 if (auto II = DN.getAsIdentifierInfo()) { 6753 if (II->isStr("max")) 6754 BOK = BO_GT; 6755 else if (II->isStr("min")) 6756 BOK = BO_LT; 6757 } 6758 break; 6759 } 6760 SourceRange ReductionIdRange; 6761 if (ReductionIdScopeSpec.isValid()) { 6762 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 6763 } 6764 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 6765 if (BOK == BO_Comma) { 6766 // Not allowed reduction identifier is found. 6767 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier) 6768 << ReductionIdRange; 6769 return nullptr; 6770 } 6771 6772 SmallVector<Expr *, 8> Vars; 6773 SmallVector<Expr *, 8> Privates; 6774 SmallVector<Expr *, 8> LHSs; 6775 SmallVector<Expr *, 8> RHSs; 6776 SmallVector<Expr *, 8> ReductionOps; 6777 for (auto RefExpr : VarList) { 6778 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 6779 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 6780 // It will be analyzed later. 6781 Vars.push_back(RefExpr); 6782 Privates.push_back(nullptr); 6783 LHSs.push_back(nullptr); 6784 RHSs.push_back(nullptr); 6785 ReductionOps.push_back(nullptr); 6786 continue; 6787 } 6788 6789 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 6790 RefExpr->isInstantiationDependent() || 6791 RefExpr->containsUnexpandedParameterPack()) { 6792 // It will be analyzed later. 6793 Vars.push_back(RefExpr); 6794 Privates.push_back(nullptr); 6795 LHSs.push_back(nullptr); 6796 RHSs.push_back(nullptr); 6797 ReductionOps.push_back(nullptr); 6798 continue; 6799 } 6800 6801 auto ELoc = RefExpr->getExprLoc(); 6802 auto ERange = RefExpr->getSourceRange(); 6803 // OpenMP [2.1, C/C++] 6804 // A list item is a variable or array section, subject to the restrictions 6805 // specified in Section 2.4 on page 42 and in each of the sections 6806 // describing clauses and directives for which a list appears. 6807 // OpenMP [2.14.3.3, Restrictions, p.1] 6808 // A variable that is part of another variable (as an array or 6809 // structure element) cannot appear in a private clause. 6810 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 6811 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr); 6812 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr); 6813 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) { 6814 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item) << ERange; 6815 continue; 6816 } 6817 QualType Type; 6818 VarDecl *VD = nullptr; 6819 if (DE) { 6820 auto D = DE->getDecl(); 6821 VD = cast<VarDecl>(D); 6822 Type = VD->getType(); 6823 } else if (ASE) { 6824 Type = ASE->getType(); 6825 auto *Base = ASE->getBase()->IgnoreParenImpCasts(); 6826 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 6827 Base = TempASE->getBase()->IgnoreParenImpCasts(); 6828 DE = dyn_cast<DeclRefExpr>(Base); 6829 if (DE) 6830 VD = dyn_cast<VarDecl>(DE->getDecl()); 6831 if (!VD) { 6832 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name) 6833 << 0 << Base->getSourceRange(); 6834 continue; 6835 } 6836 } else if (OASE) { 6837 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 6838 if (auto *ATy = BaseType->getAsArrayTypeUnsafe()) 6839 Type = ATy->getElementType(); 6840 else 6841 Type = BaseType->getPointeeType(); 6842 auto *Base = OASE->getBase()->IgnoreParenImpCasts(); 6843 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 6844 Base = TempOASE->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 << 1 << Base->getSourceRange(); 6853 continue; 6854 } 6855 } 6856 6857 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 6858 // A variable that appears in a private clause must not have an incomplete 6859 // type or a reference type. 6860 if (RequireCompleteType(ELoc, Type, 6861 diag::err_omp_reduction_incomplete_type)) 6862 continue; 6863 // OpenMP [2.14.3.6, reduction clause, Restrictions] 6864 // Arrays may not appear in a reduction clause. 6865 if (Type.getNonReferenceType()->isArrayType()) { 6866 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange; 6867 if (!ASE && !OASE) { 6868 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 6869 VarDecl::DeclarationOnly; 6870 Diag(VD->getLocation(), 6871 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 6872 << VD; 6873 } 6874 continue; 6875 } 6876 // OpenMP [2.14.3.6, reduction clause, Restrictions] 6877 // A list item that appears in a reduction clause must not be 6878 // const-qualified. 6879 if (Type.getNonReferenceType().isConstant(Context)) { 6880 Diag(ELoc, diag::err_omp_const_reduction_list_item) 6881 << getOpenMPClauseName(OMPC_reduction) << Type << ERange; 6882 if (!ASE && !OASE) { 6883 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 6884 VarDecl::DeclarationOnly; 6885 Diag(VD->getLocation(), 6886 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 6887 << VD; 6888 } 6889 continue; 6890 } 6891 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 6892 // If a list-item is a reference type then it must bind to the same object 6893 // for all threads of the team. 6894 if (!ASE && !OASE) { 6895 VarDecl *VDDef = VD->getDefinition(); 6896 if (Type->isReferenceType() && VDDef) { 6897 DSARefChecker Check(DSAStack); 6898 if (Check.Visit(VDDef->getInit())) { 6899 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange; 6900 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 6901 continue; 6902 } 6903 } 6904 } 6905 // OpenMP [2.14.3.6, reduction clause, Restrictions] 6906 // The type of a list item that appears in a reduction clause must be valid 6907 // for the reduction-identifier. For a max or min reduction in C, the type 6908 // of the list item must be an allowed arithmetic data type: char, int, 6909 // float, double, or _Bool, possibly modified with long, short, signed, or 6910 // unsigned. For a max or min reduction in C++, the type of the list item 6911 // must be an allowed arithmetic data type: char, wchar_t, int, float, 6912 // double, or bool, possibly modified with long, short, signed, or unsigned. 6913 if ((BOK == BO_GT || BOK == BO_LT) && 6914 !(Type->isScalarType() || 6915 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 6916 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 6917 << getLangOpts().CPlusPlus; 6918 if (!ASE && !OASE) { 6919 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 6920 VarDecl::DeclarationOnly; 6921 Diag(VD->getLocation(), 6922 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 6923 << VD; 6924 } 6925 continue; 6926 } 6927 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 6928 !getLangOpts().CPlusPlus && Type->isFloatingType()) { 6929 Diag(ELoc, diag::err_omp_clause_floating_type_arg); 6930 if (!ASE && !OASE) { 6931 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 6932 VarDecl::DeclarationOnly; 6933 Diag(VD->getLocation(), 6934 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 6935 << VD; 6936 } 6937 continue; 6938 } 6939 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 6940 // in a Construct] 6941 // Variables with the predetermined data-sharing attributes may not be 6942 // listed in data-sharing attributes clauses, except for the cases 6943 // listed below. For these exceptions only, listing a predetermined 6944 // variable in a data-sharing attribute clause is allowed and overrides 6945 // the variable's predetermined data-sharing attributes. 6946 // OpenMP [2.14.3.6, Restrictions, p.3] 6947 // Any number of reduction clauses can be specified on the directive, 6948 // but a list item can appear only once in the reduction clauses for that 6949 // directive. 6950 DSAStackTy::DSAVarData DVar; 6951 DVar = DSAStack->getTopDSA(VD, false); 6952 if (DVar.CKind == OMPC_reduction) { 6953 Diag(ELoc, diag::err_omp_once_referenced) 6954 << getOpenMPClauseName(OMPC_reduction); 6955 if (DVar.RefExpr) { 6956 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 6957 } 6958 } else if (DVar.CKind != OMPC_unknown) { 6959 Diag(ELoc, diag::err_omp_wrong_dsa) 6960 << getOpenMPClauseName(DVar.CKind) 6961 << getOpenMPClauseName(OMPC_reduction); 6962 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6963 continue; 6964 } 6965 6966 // OpenMP [2.14.3.6, Restrictions, p.1] 6967 // A list item that appears in a reduction clause of a worksharing 6968 // construct must be shared in the parallel regions to which any of the 6969 // worksharing regions arising from the worksharing construct bind. 6970 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 6971 if (isOpenMPWorksharingDirective(CurrDir) && 6972 !isOpenMPParallelDirective(CurrDir)) { 6973 DVar = DSAStack->getImplicitDSA(VD, true); 6974 if (DVar.CKind != OMPC_shared) { 6975 Diag(ELoc, diag::err_omp_required_access) 6976 << getOpenMPClauseName(OMPC_reduction) 6977 << getOpenMPClauseName(OMPC_shared); 6978 ReportOriginalDSA(*this, DSAStack, VD, DVar); 6979 continue; 6980 } 6981 } 6982 6983 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 6984 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs", 6985 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 6986 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(), 6987 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 6988 auto PrivateTy = Type; 6989 if (OASE) { 6990 // For array sections only: 6991 // Create pseudo array type for private copy. The size for this array will 6992 // be generated during codegen. 6993 // For array subscripts or single variables Private Ty is the same as Type 6994 // (type of the variable or single array element). 6995 PrivateTy = Context.getVariableArrayType( 6996 Type, new (Context) OpaqueValueExpr(SourceLocation(), 6997 Context.getSizeType(), VK_RValue), 6998 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 6999 } 7000 // Private copy. 7001 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(), 7002 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 7003 // Add initializer for private variable. 7004 Expr *Init = nullptr; 7005 switch (BOK) { 7006 case BO_Add: 7007 case BO_Xor: 7008 case BO_Or: 7009 case BO_LOr: 7010 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 7011 if (Type->isScalarType() || Type->isAnyComplexType()) { 7012 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 7013 } 7014 break; 7015 case BO_Mul: 7016 case BO_LAnd: 7017 if (Type->isScalarType() || Type->isAnyComplexType()) { 7018 // '*' and '&&' reduction ops - initializer is '1'. 7019 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 7020 } 7021 break; 7022 case BO_And: { 7023 // '&' reduction op - initializer is '~0'. 7024 QualType OrigType = Type; 7025 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) { 7026 Type = ComplexTy->getElementType(); 7027 } 7028 if (Type->isRealFloatingType()) { 7029 llvm::APFloat InitValue = 7030 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type), 7031 /*isIEEE=*/true); 7032 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 7033 Type, ELoc); 7034 } else if (Type->isScalarType()) { 7035 auto Size = Context.getTypeSize(Type); 7036 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 7037 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 7038 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 7039 } 7040 if (Init && OrigType->isAnyComplexType()) { 7041 // Init = 0xFFFF + 0xFFFFi; 7042 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 7043 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 7044 } 7045 Type = OrigType; 7046 break; 7047 } 7048 case BO_LT: 7049 case BO_GT: { 7050 // 'min' reduction op - initializer is 'Largest representable number in 7051 // the reduction list item type'. 7052 // 'max' reduction op - initializer is 'Least representable number in 7053 // the reduction list item type'. 7054 if (Type->isIntegerType() || Type->isPointerType()) { 7055 bool IsSigned = Type->hasSignedIntegerRepresentation(); 7056 auto Size = Context.getTypeSize(Type); 7057 QualType IntTy = 7058 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 7059 llvm::APInt InitValue = 7060 (BOK != BO_LT) 7061 ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 7062 : llvm::APInt::getMinValue(Size) 7063 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 7064 : llvm::APInt::getMaxValue(Size); 7065 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 7066 if (Type->isPointerType()) { 7067 // Cast to pointer type. 7068 auto CastExpr = BuildCStyleCastExpr( 7069 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc), 7070 SourceLocation(), Init); 7071 if (CastExpr.isInvalid()) 7072 continue; 7073 Init = CastExpr.get(); 7074 } 7075 } else if (Type->isRealFloatingType()) { 7076 llvm::APFloat InitValue = llvm::APFloat::getLargest( 7077 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 7078 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 7079 Type, ELoc); 7080 } 7081 break; 7082 } 7083 case BO_PtrMemD: 7084 case BO_PtrMemI: 7085 case BO_MulAssign: 7086 case BO_Div: 7087 case BO_Rem: 7088 case BO_Sub: 7089 case BO_Shl: 7090 case BO_Shr: 7091 case BO_LE: 7092 case BO_GE: 7093 case BO_EQ: 7094 case BO_NE: 7095 case BO_AndAssign: 7096 case BO_XorAssign: 7097 case BO_OrAssign: 7098 case BO_Assign: 7099 case BO_AddAssign: 7100 case BO_SubAssign: 7101 case BO_DivAssign: 7102 case BO_RemAssign: 7103 case BO_ShlAssign: 7104 case BO_ShrAssign: 7105 case BO_Comma: 7106 llvm_unreachable("Unexpected reduction operation"); 7107 } 7108 if (Init) { 7109 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false, 7110 /*TypeMayContainAuto=*/false); 7111 } else 7112 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false); 7113 if (!RHSVD->hasInit()) { 7114 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type 7115 << ReductionIdRange; 7116 if (VD) { 7117 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 7118 VarDecl::DeclarationOnly; 7119 Diag(VD->getLocation(), 7120 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 7121 << VD; 7122 } 7123 continue; 7124 } 7125 // Store initializer for single element in private copy. Will be used during 7126 // codegen. 7127 PrivateVD->setInit(RHSVD->getInit()); 7128 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 7129 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc); 7130 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc); 7131 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc); 7132 ExprResult ReductionOp = 7133 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK, 7134 LHSDRE, RHSDRE); 7135 if (ReductionOp.isUsable()) { 7136 if (BOK != BO_LT && BOK != BO_GT) { 7137 ReductionOp = 7138 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), 7139 BO_Assign, LHSDRE, ReductionOp.get()); 7140 } else { 7141 auto *ConditionalOp = new (Context) ConditionalOperator( 7142 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(), 7143 RHSDRE, Type, VK_LValue, OK_Ordinary); 7144 ReductionOp = 7145 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), 7146 BO_Assign, LHSDRE, ConditionalOp); 7147 } 7148 ReductionOp = ActOnFinishFullExpr(ReductionOp.get()); 7149 } 7150 if (ReductionOp.isInvalid()) 7151 continue; 7152 7153 DSAStack->addDSA(VD, DE, OMPC_reduction); 7154 Vars.push_back(RefExpr); 7155 Privates.push_back(PrivateDRE); 7156 LHSs.push_back(LHSDRE); 7157 RHSs.push_back(RHSDRE); 7158 ReductionOps.push_back(ReductionOp.get()); 7159 } 7160 7161 if (Vars.empty()) 7162 return nullptr; 7163 7164 return OMPReductionClause::Create( 7165 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars, 7166 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates, 7167 LHSs, RHSs, ReductionOps); 7168 } 7169 7170 OMPClause *Sema::ActOnOpenMPLinearClause( 7171 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 7172 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 7173 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 7174 SmallVector<Expr *, 8> Vars; 7175 SmallVector<Expr *, 8> Privates; 7176 SmallVector<Expr *, 8> Inits; 7177 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 7178 LinKind == OMPC_LINEAR_unknown) { 7179 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 7180 LinKind = OMPC_LINEAR_val; 7181 } 7182 for (auto &RefExpr : VarList) { 7183 assert(RefExpr && "NULL expr in OpenMP linear clause."); 7184 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 7185 // It will be analyzed later. 7186 Vars.push_back(RefExpr); 7187 Privates.push_back(nullptr); 7188 Inits.push_back(nullptr); 7189 continue; 7190 } 7191 7192 // OpenMP [2.14.3.7, linear clause] 7193 // A list item that appears in a linear clause is subject to the private 7194 // clause semantics described in Section 2.14.3.3 on page 159 except as 7195 // noted. In addition, the value of the new list item on each iteration 7196 // of the associated loop(s) corresponds to the value of the original 7197 // list item before entering the construct plus the logical number of 7198 // the iteration times linear-step. 7199 7200 SourceLocation ELoc = RefExpr->getExprLoc(); 7201 // OpenMP [2.1, C/C++] 7202 // A list item is a variable name. 7203 // OpenMP [2.14.3.3, Restrictions, p.1] 7204 // A variable that is part of another variable (as an array or 7205 // structure element) cannot appear in a private clause. 7206 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 7207 if (!DE || !isa<VarDecl>(DE->getDecl())) { 7208 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 7209 continue; 7210 } 7211 7212 VarDecl *VD = cast<VarDecl>(DE->getDecl()); 7213 7214 // OpenMP [2.14.3.7, linear clause] 7215 // A list-item cannot appear in more than one linear clause. 7216 // A list-item that appears in a linear clause cannot appear in any 7217 // other data-sharing attribute clause. 7218 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false); 7219 if (DVar.RefExpr) { 7220 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 7221 << getOpenMPClauseName(OMPC_linear); 7222 ReportOriginalDSA(*this, DSAStack, VD, DVar); 7223 continue; 7224 } 7225 7226 QualType QType = VD->getType(); 7227 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 7228 // It will be analyzed later. 7229 Vars.push_back(DE); 7230 Privates.push_back(nullptr); 7231 Inits.push_back(nullptr); 7232 continue; 7233 } 7234 7235 // A variable must not have an incomplete type or a reference type. 7236 if (RequireCompleteType(ELoc, QType, 7237 diag::err_omp_linear_incomplete_type)) { 7238 continue; 7239 } 7240 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 7241 !QType->isReferenceType()) { 7242 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 7243 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 7244 continue; 7245 } 7246 QType = QType.getNonReferenceType(); 7247 7248 // A list item must not be const-qualified. 7249 if (QType.isConstant(Context)) { 7250 Diag(ELoc, diag::err_omp_const_variable) 7251 << getOpenMPClauseName(OMPC_linear); 7252 bool IsDecl = 7253 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 7254 Diag(VD->getLocation(), 7255 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 7256 << VD; 7257 continue; 7258 } 7259 7260 // A list item must be of integral or pointer type. 7261 QType = QType.getUnqualifiedType().getCanonicalType(); 7262 const Type *Ty = QType.getTypePtrOrNull(); 7263 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && 7264 !Ty->isPointerType())) { 7265 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType; 7266 bool IsDecl = 7267 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 7268 Diag(VD->getLocation(), 7269 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 7270 << VD; 7271 continue; 7272 } 7273 7274 // Build private copy of original var. 7275 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(), 7276 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 7277 auto *PrivateRef = buildDeclRefExpr( 7278 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc()); 7279 // Build var to save initial value. 7280 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start"); 7281 Expr *InitExpr; 7282 if (LinKind == OMPC_LINEAR_uval) 7283 InitExpr = VD->getInit(); 7284 else 7285 InitExpr = DE; 7286 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 7287 /*DirectInit*/ false, /*TypeMayContainAuto*/ false); 7288 auto InitRef = buildDeclRefExpr( 7289 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc()); 7290 DSAStack->addDSA(VD, DE, OMPC_linear); 7291 Vars.push_back(DE); 7292 Privates.push_back(PrivateRef); 7293 Inits.push_back(InitRef); 7294 } 7295 7296 if (Vars.empty()) 7297 return nullptr; 7298 7299 Expr *StepExpr = Step; 7300 Expr *CalcStepExpr = nullptr; 7301 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 7302 !Step->isInstantiationDependent() && 7303 !Step->containsUnexpandedParameterPack()) { 7304 SourceLocation StepLoc = Step->getLocStart(); 7305 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 7306 if (Val.isInvalid()) 7307 return nullptr; 7308 StepExpr = Val.get(); 7309 7310 // Build var to save the step value. 7311 VarDecl *SaveVar = 7312 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 7313 ExprResult SaveRef = 7314 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 7315 ExprResult CalcStep = 7316 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 7317 CalcStep = ActOnFinishFullExpr(CalcStep.get()); 7318 7319 // Warn about zero linear step (it would be probably better specified as 7320 // making corresponding variables 'const'). 7321 llvm::APSInt Result; 7322 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); 7323 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) 7324 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] 7325 << (Vars.size() > 1); 7326 if (!IsConstant && CalcStep.isUsable()) { 7327 // Calculate the step beforehand instead of doing this on each iteration. 7328 // (This is not used if the number of iterations may be kfold-ed). 7329 CalcStepExpr = CalcStep.get(); 7330 } 7331 } 7332 7333 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 7334 ColonLoc, EndLoc, Vars, Privates, Inits, 7335 StepExpr, CalcStepExpr); 7336 } 7337 7338 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 7339 Expr *NumIterations, Sema &SemaRef, 7340 Scope *S) { 7341 // Walk the vars and build update/final expressions for the CodeGen. 7342 SmallVector<Expr *, 8> Updates; 7343 SmallVector<Expr *, 8> Finals; 7344 Expr *Step = Clause.getStep(); 7345 Expr *CalcStep = Clause.getCalcStep(); 7346 // OpenMP [2.14.3.7, linear clause] 7347 // If linear-step is not specified it is assumed to be 1. 7348 if (Step == nullptr) 7349 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 7350 else if (CalcStep) 7351 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 7352 bool HasErrors = false; 7353 auto CurInit = Clause.inits().begin(); 7354 auto CurPrivate = Clause.privates().begin(); 7355 auto LinKind = Clause.getModifier(); 7356 for (auto &RefExpr : Clause.varlists()) { 7357 Expr *InitExpr = *CurInit; 7358 7359 // Build privatized reference to the current linear var. 7360 auto DE = cast<DeclRefExpr>(RefExpr); 7361 Expr *CapturedRef; 7362 if (LinKind == OMPC_LINEAR_uval) 7363 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 7364 else 7365 CapturedRef = 7366 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 7367 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 7368 /*RefersToCapture=*/true); 7369 7370 // Build update: Var = InitExpr + IV * Step 7371 ExprResult Update = 7372 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, 7373 InitExpr, IV, Step, /* Subtract */ false); 7374 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(), 7375 /*DiscardedValue=*/true); 7376 7377 // Build final: Var = InitExpr + NumIterations * Step 7378 ExprResult Final = 7379 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 7380 InitExpr, NumIterations, Step, /* Subtract */ false); 7381 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(), 7382 /*DiscardedValue=*/true); 7383 if (!Update.isUsable() || !Final.isUsable()) { 7384 Updates.push_back(nullptr); 7385 Finals.push_back(nullptr); 7386 HasErrors = true; 7387 } else { 7388 Updates.push_back(Update.get()); 7389 Finals.push_back(Final.get()); 7390 } 7391 ++CurInit, ++CurPrivate; 7392 } 7393 Clause.setUpdates(Updates); 7394 Clause.setFinals(Finals); 7395 return HasErrors; 7396 } 7397 7398 OMPClause *Sema::ActOnOpenMPAlignedClause( 7399 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 7400 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 7401 7402 SmallVector<Expr *, 8> Vars; 7403 for (auto &RefExpr : VarList) { 7404 assert(RefExpr && "NULL expr in OpenMP aligned clause."); 7405 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 7406 // It will be analyzed later. 7407 Vars.push_back(RefExpr); 7408 continue; 7409 } 7410 7411 SourceLocation ELoc = RefExpr->getExprLoc(); 7412 // OpenMP [2.1, C/C++] 7413 // A list item is a variable name. 7414 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 7415 if (!DE || !isa<VarDecl>(DE->getDecl())) { 7416 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 7417 continue; 7418 } 7419 7420 VarDecl *VD = cast<VarDecl>(DE->getDecl()); 7421 7422 // OpenMP [2.8.1, simd construct, Restrictions] 7423 // The type of list items appearing in the aligned clause must be 7424 // array, pointer, reference to array, or reference to pointer. 7425 QualType QType = VD->getType(); 7426 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 7427 const Type *Ty = QType.getTypePtrOrNull(); 7428 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() && 7429 !Ty->isPointerType())) { 7430 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 7431 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange(); 7432 bool IsDecl = 7433 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 7434 Diag(VD->getLocation(), 7435 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 7436 << VD; 7437 continue; 7438 } 7439 7440 // OpenMP [2.8.1, simd construct, Restrictions] 7441 // A list-item cannot appear in more than one aligned clause. 7442 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) { 7443 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange(); 7444 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 7445 << getOpenMPClauseName(OMPC_aligned); 7446 continue; 7447 } 7448 7449 Vars.push_back(DE); 7450 } 7451 7452 // OpenMP [2.8.1, simd construct, Description] 7453 // The parameter of the aligned clause, alignment, must be a constant 7454 // positive integer expression. 7455 // If no optional parameter is specified, implementation-defined default 7456 // alignments for SIMD instructions on the target platforms are assumed. 7457 if (Alignment != nullptr) { 7458 ExprResult AlignResult = 7459 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 7460 if (AlignResult.isInvalid()) 7461 return nullptr; 7462 Alignment = AlignResult.get(); 7463 } 7464 if (Vars.empty()) 7465 return nullptr; 7466 7467 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 7468 EndLoc, Vars, Alignment); 7469 } 7470 7471 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 7472 SourceLocation StartLoc, 7473 SourceLocation LParenLoc, 7474 SourceLocation EndLoc) { 7475 SmallVector<Expr *, 8> Vars; 7476 SmallVector<Expr *, 8> SrcExprs; 7477 SmallVector<Expr *, 8> DstExprs; 7478 SmallVector<Expr *, 8> AssignmentOps; 7479 for (auto &RefExpr : VarList) { 7480 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 7481 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 7482 // It will be analyzed later. 7483 Vars.push_back(RefExpr); 7484 SrcExprs.push_back(nullptr); 7485 DstExprs.push_back(nullptr); 7486 AssignmentOps.push_back(nullptr); 7487 continue; 7488 } 7489 7490 SourceLocation ELoc = RefExpr->getExprLoc(); 7491 // OpenMP [2.1, C/C++] 7492 // A list item is a variable name. 7493 // OpenMP [2.14.4.1, Restrictions, p.1] 7494 // A list item that appears in a copyin clause must be threadprivate. 7495 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 7496 if (!DE || !isa<VarDecl>(DE->getDecl())) { 7497 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 7498 continue; 7499 } 7500 7501 Decl *D = DE->getDecl(); 7502 VarDecl *VD = cast<VarDecl>(D); 7503 7504 QualType Type = VD->getType(); 7505 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 7506 // It will be analyzed later. 7507 Vars.push_back(DE); 7508 SrcExprs.push_back(nullptr); 7509 DstExprs.push_back(nullptr); 7510 AssignmentOps.push_back(nullptr); 7511 continue; 7512 } 7513 7514 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 7515 // A list item that appears in a copyin clause must be threadprivate. 7516 if (!DSAStack->isThreadPrivate(VD)) { 7517 Diag(ELoc, diag::err_omp_required_access) 7518 << getOpenMPClauseName(OMPC_copyin) 7519 << getOpenMPDirectiveName(OMPD_threadprivate); 7520 continue; 7521 } 7522 7523 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 7524 // A variable of class type (or array thereof) that appears in a 7525 // copyin clause requires an accessible, unambiguous copy assignment 7526 // operator for the class type. 7527 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 7528 auto *SrcVD = 7529 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(), 7530 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 7531 auto *PseudoSrcExpr = buildDeclRefExpr( 7532 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 7533 auto *DstVD = 7534 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst", 7535 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 7536 auto *PseudoDstExpr = 7537 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 7538 // For arrays generate assignment operation for single element and replace 7539 // it by the original array element in CodeGen. 7540 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, 7541 PseudoDstExpr, PseudoSrcExpr); 7542 if (AssignmentOp.isInvalid()) 7543 continue; 7544 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 7545 /*DiscardedValue=*/true); 7546 if (AssignmentOp.isInvalid()) 7547 continue; 7548 7549 DSAStack->addDSA(VD, DE, OMPC_copyin); 7550 Vars.push_back(DE); 7551 SrcExprs.push_back(PseudoSrcExpr); 7552 DstExprs.push_back(PseudoDstExpr); 7553 AssignmentOps.push_back(AssignmentOp.get()); 7554 } 7555 7556 if (Vars.empty()) 7557 return nullptr; 7558 7559 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 7560 SrcExprs, DstExprs, AssignmentOps); 7561 } 7562 7563 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 7564 SourceLocation StartLoc, 7565 SourceLocation LParenLoc, 7566 SourceLocation EndLoc) { 7567 SmallVector<Expr *, 8> Vars; 7568 SmallVector<Expr *, 8> SrcExprs; 7569 SmallVector<Expr *, 8> DstExprs; 7570 SmallVector<Expr *, 8> AssignmentOps; 7571 for (auto &RefExpr : VarList) { 7572 assert(RefExpr && "NULL expr in OpenMP copyprivate clause."); 7573 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 7574 // It will be analyzed later. 7575 Vars.push_back(RefExpr); 7576 SrcExprs.push_back(nullptr); 7577 DstExprs.push_back(nullptr); 7578 AssignmentOps.push_back(nullptr); 7579 continue; 7580 } 7581 7582 SourceLocation ELoc = RefExpr->getExprLoc(); 7583 // OpenMP [2.1, C/C++] 7584 // A list item is a variable name. 7585 // OpenMP [2.14.4.1, Restrictions, p.1] 7586 // A list item that appears in a copyin clause must be threadprivate. 7587 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 7588 if (!DE || !isa<VarDecl>(DE->getDecl())) { 7589 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 7590 continue; 7591 } 7592 7593 Decl *D = DE->getDecl(); 7594 VarDecl *VD = cast<VarDecl>(D); 7595 7596 QualType Type = VD->getType(); 7597 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 7598 // It will be analyzed later. 7599 Vars.push_back(DE); 7600 SrcExprs.push_back(nullptr); 7601 DstExprs.push_back(nullptr); 7602 AssignmentOps.push_back(nullptr); 7603 continue; 7604 } 7605 7606 // OpenMP [2.14.4.2, Restrictions, p.2] 7607 // A list item that appears in a copyprivate clause may not appear in a 7608 // private or firstprivate clause on the single construct. 7609 if (!DSAStack->isThreadPrivate(VD)) { 7610 auto DVar = DSAStack->getTopDSA(VD, false); 7611 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 7612 DVar.RefExpr) { 7613 Diag(ELoc, diag::err_omp_wrong_dsa) 7614 << getOpenMPClauseName(DVar.CKind) 7615 << getOpenMPClauseName(OMPC_copyprivate); 7616 ReportOriginalDSA(*this, DSAStack, VD, DVar); 7617 continue; 7618 } 7619 7620 // OpenMP [2.11.4.2, Restrictions, p.1] 7621 // All list items that appear in a copyprivate clause must be either 7622 // threadprivate or private in the enclosing context. 7623 if (DVar.CKind == OMPC_unknown) { 7624 DVar = DSAStack->getImplicitDSA(VD, false); 7625 if (DVar.CKind == OMPC_shared) { 7626 Diag(ELoc, diag::err_omp_required_access) 7627 << getOpenMPClauseName(OMPC_copyprivate) 7628 << "threadprivate or private in the enclosing context"; 7629 ReportOriginalDSA(*this, DSAStack, VD, DVar); 7630 continue; 7631 } 7632 } 7633 } 7634 7635 // Variably modified types are not supported. 7636 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 7637 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 7638 << getOpenMPClauseName(OMPC_copyprivate) << Type 7639 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 7640 bool IsDecl = 7641 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 7642 Diag(VD->getLocation(), 7643 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 7644 << VD; 7645 continue; 7646 } 7647 7648 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 7649 // A variable of class type (or array thereof) that appears in a 7650 // copyin clause requires an accessible, unambiguous copy assignment 7651 // operator for the class type. 7652 Type = Context.getBaseElementType(Type.getNonReferenceType()) 7653 .getUnqualifiedType(); 7654 auto *SrcVD = 7655 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src", 7656 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 7657 auto *PseudoSrcExpr = 7658 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc()); 7659 auto *DstVD = 7660 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst", 7661 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 7662 auto *PseudoDstExpr = 7663 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc()); 7664 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, 7665 PseudoDstExpr, PseudoSrcExpr); 7666 if (AssignmentOp.isInvalid()) 7667 continue; 7668 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 7669 /*DiscardedValue=*/true); 7670 if (AssignmentOp.isInvalid()) 7671 continue; 7672 7673 // No need to mark vars as copyprivate, they are already threadprivate or 7674 // implicitly private. 7675 Vars.push_back(DE); 7676 SrcExprs.push_back(PseudoSrcExpr); 7677 DstExprs.push_back(PseudoDstExpr); 7678 AssignmentOps.push_back(AssignmentOp.get()); 7679 } 7680 7681 if (Vars.empty()) 7682 return nullptr; 7683 7684 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 7685 Vars, SrcExprs, DstExprs, AssignmentOps); 7686 } 7687 7688 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 7689 SourceLocation StartLoc, 7690 SourceLocation LParenLoc, 7691 SourceLocation EndLoc) { 7692 if (VarList.empty()) 7693 return nullptr; 7694 7695 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 7696 } 7697 7698 OMPClause * 7699 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind, 7700 SourceLocation DepLoc, SourceLocation ColonLoc, 7701 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 7702 SourceLocation LParenLoc, SourceLocation EndLoc) { 7703 if (DepKind == OMPC_DEPEND_unknown) { 7704 std::string Values; 7705 std::string Sep(", "); 7706 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) { 7707 Values += "'"; 7708 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i); 7709 Values += "'"; 7710 switch (i) { 7711 case OMPC_DEPEND_unknown - 2: 7712 Values += " or "; 7713 break; 7714 case OMPC_DEPEND_unknown - 1: 7715 break; 7716 default: 7717 Values += Sep; 7718 break; 7719 } 7720 } 7721 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 7722 << Values << getOpenMPClauseName(OMPC_depend); 7723 return nullptr; 7724 } 7725 SmallVector<Expr *, 8> Vars; 7726 for (auto &RefExpr : VarList) { 7727 assert(RefExpr && "NULL expr in OpenMP shared clause."); 7728 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 7729 // It will be analyzed later. 7730 Vars.push_back(RefExpr); 7731 continue; 7732 } 7733 7734 SourceLocation ELoc = RefExpr->getExprLoc(); 7735 // OpenMP [2.11.1.1, Restrictions, p.3] 7736 // A variable that is part of another variable (such as a field of a 7737 // structure) but is not an array element or an array section cannot appear 7738 // in a depend clause. 7739 auto *SimpleExpr = RefExpr->IgnoreParenCasts(); 7740 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr); 7741 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 7742 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); 7743 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 7744 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) || 7745 (ASE && !ASE->getBase()->getType()->isAnyPointerType() && 7746 !ASE->getBase()->getType()->isArrayType())) { 7747 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item) 7748 << RefExpr->getSourceRange(); 7749 continue; 7750 } 7751 7752 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 7753 } 7754 7755 if (Vars.empty()) 7756 return nullptr; 7757 7758 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind, 7759 DepLoc, ColonLoc, Vars); 7760 } 7761 7762 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc, 7763 SourceLocation LParenLoc, 7764 SourceLocation EndLoc) { 7765 Expr *ValExpr = Device; 7766 7767 // OpenMP [2.9.1, Restrictions] 7768 // The device expression must evaluate to a non-negative integer value. 7769 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 7770 /*StrictlyPositive=*/false)) 7771 return nullptr; 7772 7773 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc); 7774 } 7775 7776 static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc, 7777 DSAStackTy *Stack, CXXRecordDecl *RD) { 7778 if (!RD || RD->isInvalidDecl()) 7779 return true; 7780 7781 auto QTy = SemaRef.Context.getRecordType(RD); 7782 if (RD->isDynamicClass()) { 7783 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; 7784 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target); 7785 return false; 7786 } 7787 auto *DC = RD; 7788 bool IsCorrect = true; 7789 for (auto *I : DC->decls()) { 7790 if (I) { 7791 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) { 7792 if (MD->isStatic()) { 7793 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; 7794 SemaRef.Diag(MD->getLocation(), 7795 diag::note_omp_static_member_in_target); 7796 IsCorrect = false; 7797 } 7798 } else if (auto *VD = dyn_cast<VarDecl>(I)) { 7799 if (VD->isStaticDataMember()) { 7800 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; 7801 SemaRef.Diag(VD->getLocation(), 7802 diag::note_omp_static_member_in_target); 7803 IsCorrect = false; 7804 } 7805 } 7806 } 7807 } 7808 7809 for (auto &I : RD->bases()) { 7810 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack, 7811 I.getType()->getAsCXXRecordDecl())) 7812 IsCorrect = false; 7813 } 7814 return IsCorrect; 7815 } 7816 7817 static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 7818 DSAStackTy *Stack, QualType QTy) { 7819 NamedDecl *ND; 7820 if (QTy->isIncompleteType(&ND)) { 7821 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 7822 return false; 7823 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) { 7824 if (!RD->isInvalidDecl() && 7825 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD)) 7826 return false; 7827 } 7828 return true; 7829 } 7830 7831 OMPClause *Sema::ActOnOpenMPMapClause( 7832 OpenMPMapClauseKind MapTypeModifier, OpenMPMapClauseKind MapType, 7833 SourceLocation MapLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 7834 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 7835 SmallVector<Expr *, 4> Vars; 7836 7837 for (auto &RE : VarList) { 7838 assert(RE && "Null expr in omp map"); 7839 if (isa<DependentScopeDeclRefExpr>(RE)) { 7840 // It will be analyzed later. 7841 Vars.push_back(RE); 7842 continue; 7843 } 7844 SourceLocation ELoc = RE->getExprLoc(); 7845 7846 // OpenMP [2.14.5, Restrictions] 7847 // A variable that is part of another variable (such as field of a 7848 // structure) but is not an array element or an array section cannot appear 7849 // in a map clause. 7850 auto *VE = RE->IgnoreParenLValueCasts(); 7851 7852 if (VE->isValueDependent() || VE->isTypeDependent() || 7853 VE->isInstantiationDependent() || 7854 VE->containsUnexpandedParameterPack()) { 7855 // It will be analyzed later. 7856 Vars.push_back(RE); 7857 continue; 7858 } 7859 7860 auto *SimpleExpr = RE->IgnoreParenCasts(); 7861 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr); 7862 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 7863 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); 7864 7865 if (!RE->IgnoreParenImpCasts()->isLValue() || 7866 (!OASE && !ASE && !DE) || 7867 (DE && !isa<VarDecl>(DE->getDecl())) || 7868 (ASE && !ASE->getBase()->getType()->isAnyPointerType() && 7869 !ASE->getBase()->getType()->isArrayType())) { 7870 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item) 7871 << RE->getSourceRange(); 7872 continue; 7873 } 7874 7875 Decl *D = nullptr; 7876 if (DE) { 7877 D = DE->getDecl(); 7878 } else if (ASE) { 7879 auto *B = ASE->getBase()->IgnoreParenCasts(); 7880 D = dyn_cast<DeclRefExpr>(B)->getDecl(); 7881 } else if (OASE) { 7882 auto *B = OASE->getBase(); 7883 D = dyn_cast<DeclRefExpr>(B)->getDecl(); 7884 } 7885 assert(D && "Null decl on map clause."); 7886 auto *VD = cast<VarDecl>(D); 7887 7888 // OpenMP [2.14.5, Restrictions, p.8] 7889 // threadprivate variables cannot appear in a map clause. 7890 if (DSAStack->isThreadPrivate(VD)) { 7891 auto DVar = DSAStack->getTopDSA(VD, false); 7892 Diag(ELoc, diag::err_omp_threadprivate_in_map); 7893 ReportOriginalDSA(*this, DSAStack, VD, DVar); 7894 continue; 7895 } 7896 7897 // OpenMP [2.14.5, Restrictions, p.2] 7898 // At most one list item can be an array item derived from a given variable 7899 // in map clauses of the same construct. 7900 // OpenMP [2.14.5, Restrictions, p.3] 7901 // List items of map clauses in the same construct must not share original 7902 // storage. 7903 // OpenMP [2.14.5, Restrictions, C/C++, p.2] 7904 // A variable for which the type is pointer, reference to array, or 7905 // reference to pointer and an array section derived from that variable 7906 // must not appear as list items of map clauses of the same construct. 7907 DSAStackTy::MapInfo MI = DSAStack->IsMappedInCurrentRegion(VD); 7908 if (MI.RefExpr) { 7909 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc; 7910 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here) 7911 << MI.RefExpr->getSourceRange(); 7912 continue; 7913 } 7914 7915 // OpenMP [2.14.5, Restrictions, C/C++, p.3,4] 7916 // A variable for which the type is pointer, reference to array, or 7917 // reference to pointer must not appear as a list item if the enclosing 7918 // device data environment already contains an array section derived from 7919 // that variable. 7920 // An array section derived from a variable for which the type is pointer, 7921 // reference to array, or reference to pointer must not appear as a list 7922 // item if the enclosing device data environment already contains that 7923 // variable. 7924 QualType Type = VD->getType(); 7925 MI = DSAStack->getMapInfoForVar(VD); 7926 if (MI.RefExpr && (isa<DeclRefExpr>(MI.RefExpr->IgnoreParenLValueCasts()) != 7927 isa<DeclRefExpr>(VE)) && 7928 (Type->isPointerType() || Type->isReferenceType())) { 7929 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc; 7930 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here) 7931 << MI.RefExpr->getSourceRange(); 7932 continue; 7933 } 7934 7935 // OpenMP [2.14.5, Restrictions, C/C++, p.7] 7936 // A list item must have a mappable type. 7937 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this, 7938 DSAStack, Type)) 7939 continue; 7940 7941 Vars.push_back(RE); 7942 MI.RefExpr = RE; 7943 DSAStack->addMapInfoForVar(VD, MI); 7944 } 7945 if (Vars.empty()) 7946 return nullptr; 7947 7948 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 7949 MapTypeModifier, MapType, MapLoc); 7950 } 7951 7952 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 7953 SourceLocation StartLoc, 7954 SourceLocation LParenLoc, 7955 SourceLocation EndLoc) { 7956 Expr *ValExpr = NumTeams; 7957 7958 // OpenMP [teams Constrcut, Restrictions] 7959 // The num_teams expression must evaluate to a positive integer value. 7960 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 7961 /*StrictlyPositive=*/true)) 7962 return nullptr; 7963 7964 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc); 7965 } 7966 7967 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 7968 SourceLocation StartLoc, 7969 SourceLocation LParenLoc, 7970 SourceLocation EndLoc) { 7971 Expr *ValExpr = ThreadLimit; 7972 7973 // OpenMP [teams Constrcut, Restrictions] 7974 // The thread_limit expression must evaluate to a positive integer value. 7975 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 7976 /*StrictlyPositive=*/true)) 7977 return nullptr; 7978 7979 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc, 7980 EndLoc); 7981 } 7982 7983 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 7984 SourceLocation StartLoc, 7985 SourceLocation LParenLoc, 7986 SourceLocation EndLoc) { 7987 Expr *ValExpr = Priority; 7988 7989 // OpenMP [2.9.1, task Constrcut] 7990 // The priority-value is a non-negative numerical scalar expression. 7991 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority, 7992 /*StrictlyPositive=*/false)) 7993 return nullptr; 7994 7995 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc); 7996 } 7997