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