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