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/CXXInheritance.h" 19 #include "clang/AST/Decl.h" 20 #include "clang/AST/DeclCXX.h" 21 #include "clang/AST/DeclOpenMP.h" 22 #include "clang/AST/StmtCXX.h" 23 #include "clang/AST/StmtOpenMP.h" 24 #include "clang/AST/StmtVisitor.h" 25 #include "clang/AST/TypeOrdering.h" 26 #include "clang/Basic/OpenMPKinds.h" 27 #include "clang/Basic/TargetInfo.h" 28 #include "clang/Lex/Preprocessor.h" 29 #include "clang/Sema/Initialization.h" 30 #include "clang/Sema/Lookup.h" 31 #include "clang/Sema/Scope.h" 32 #include "clang/Sema/ScopeInfo.h" 33 #include "clang/Sema/SemaInternal.h" 34 using namespace clang; 35 36 //===----------------------------------------------------------------------===// 37 // Stack of data-sharing attributes for variables 38 //===----------------------------------------------------------------------===// 39 40 namespace { 41 /// \brief Default data sharing attributes, which can be applied to directive. 42 enum DefaultDataSharingAttributes { 43 DSA_unspecified = 0, /// \brief Data sharing attribute not specified. 44 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'. 45 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'. 46 }; 47 48 /// \brief Stack for tracking declarations used in OpenMP directives and 49 /// clauses and their data-sharing attributes. 50 class DSAStackTy final { 51 public: 52 struct DSAVarData final { 53 OpenMPDirectiveKind DKind = OMPD_unknown; 54 OpenMPClauseKind CKind = OMPC_unknown; 55 Expr *RefExpr = nullptr; 56 DeclRefExpr *PrivateCopy = nullptr; 57 SourceLocation ImplicitDSALoc; 58 DSAVarData() {} 59 }; 60 61 private: 62 struct DSAInfo final { 63 OpenMPClauseKind Attributes = OMPC_unknown; 64 /// Pointer to a reference expression and a flag which shows that the 65 /// variable is marked as lastprivate(true) or not (false). 66 llvm::PointerIntPair<Expr *, 1, bool> RefExpr; 67 DeclRefExpr *PrivateCopy = nullptr; 68 }; 69 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy; 70 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy; 71 typedef std::pair<unsigned, VarDecl *> LCDeclInfo; 72 typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy; 73 typedef llvm::DenseMap< 74 ValueDecl *, OMPClauseMappableExprCommon::MappableExprComponentLists> 75 MappedExprComponentsTy; 76 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>> 77 CriticalsWithHintsTy; 78 79 struct SharingMapTy final { 80 DeclSAMapTy SharingMap; 81 AlignedMapTy AlignedMap; 82 MappedExprComponentsTy MappedExprComponents; 83 LoopControlVariablesMapTy LCVMap; 84 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified; 85 SourceLocation DefaultAttrLoc; 86 OpenMPDirectiveKind Directive = OMPD_unknown; 87 DeclarationNameInfo DirectiveName; 88 Scope *CurScope = nullptr; 89 SourceLocation ConstructLoc; 90 /// \brief first argument (Expr *) contains optional argument of the 91 /// 'ordered' clause, the second one is true if the regions has 'ordered' 92 /// clause, false otherwise. 93 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion; 94 bool NowaitRegion = false; 95 bool CancelRegion = false; 96 unsigned AssociatedLoops = 1; 97 SourceLocation InnerTeamsRegionLoc; 98 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 99 Scope *CurScope, SourceLocation Loc) 100 : Directive(DKind), DirectiveName(Name), CurScope(CurScope), 101 ConstructLoc(Loc) {} 102 SharingMapTy() {} 103 }; 104 105 typedef SmallVector<SharingMapTy, 4> StackTy; 106 107 /// \brief Stack of used declaration and their data-sharing attributes. 108 StackTy Stack; 109 /// \brief true, if check for DSA must be from parent directive, false, if 110 /// from current directive. 111 OpenMPClauseKind ClauseKindMode = OMPC_unknown; 112 Sema &SemaRef; 113 bool ForceCapturing = false; 114 CriticalsWithHintsTy Criticals; 115 116 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator; 117 118 DSAVarData getDSA(StackTy::reverse_iterator& Iter, ValueDecl *D); 119 120 /// \brief Checks if the variable is a local for OpenMP region. 121 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter); 122 123 public: 124 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {} 125 126 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 127 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 128 129 bool isForceVarCapturing() const { return ForceCapturing; } 130 void setForceVarCapturing(bool V) { ForceCapturing = V; } 131 132 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 133 Scope *CurScope, SourceLocation Loc) { 134 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc)); 135 Stack.back().DefaultAttrLoc = Loc; 136 } 137 138 void pop() { 139 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!"); 140 Stack.pop_back(); 141 } 142 143 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) { 144 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint); 145 } 146 const std::pair<OMPCriticalDirective *, llvm::APSInt> 147 getCriticalWithHint(const DeclarationNameInfo &Name) const { 148 auto I = Criticals.find(Name.getAsString()); 149 if (I != Criticals.end()) 150 return I->second; 151 return std::make_pair(nullptr, llvm::APSInt()); 152 } 153 /// \brief If 'aligned' declaration for given variable \a D was not seen yet, 154 /// add it and return NULL; otherwise return previous occurrence's expression 155 /// for diagnostics. 156 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE); 157 158 /// \brief Register specified variable as loop control variable. 159 void addLoopControlVariable(ValueDecl *D, VarDecl *Capture); 160 /// \brief Check if the specified variable is a loop control variable for 161 /// current region. 162 /// \return The index of the loop control variable in the list of associated 163 /// for-loops (from outer to inner). 164 LCDeclInfo isLoopControlVariable(ValueDecl *D); 165 /// \brief Check if the specified variable is a loop control variable for 166 /// parent region. 167 /// \return The index of the loop control variable in the list of associated 168 /// for-loops (from outer to inner). 169 LCDeclInfo isParentLoopControlVariable(ValueDecl *D); 170 /// \brief Get the loop control variable for the I-th loop (or nullptr) in 171 /// parent directive. 172 ValueDecl *getParentLoopControlVariable(unsigned I); 173 174 /// \brief Adds explicit data sharing attribute to the specified declaration. 175 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A, 176 DeclRefExpr *PrivateCopy = nullptr); 177 178 /// \brief Returns data sharing attributes from top of the stack for the 179 /// specified declaration. 180 DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 181 /// \brief Returns data-sharing attributes for the specified declaration. 182 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent); 183 /// \brief Checks if the specified variables has data-sharing attributes which 184 /// match specified \a CPred predicate in any directive which matches \a DPred 185 /// predicate. 186 DSAVarData hasDSA(ValueDecl *D, 187 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 188 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 189 bool FromParent); 190 /// \brief Checks if the specified variables has data-sharing attributes which 191 /// match specified \a CPred predicate in any innermost directive which 192 /// matches \a DPred predicate. 193 DSAVarData 194 hasInnermostDSA(ValueDecl *D, 195 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 196 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 197 bool FromParent); 198 /// \brief Checks if the specified variables has explicit data-sharing 199 /// attributes which match specified \a CPred predicate at the specified 200 /// OpenMP region. 201 bool hasExplicitDSA(ValueDecl *D, 202 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 203 unsigned Level, bool NotLastprivate = false); 204 205 /// \brief Returns true if the directive at level \Level matches in the 206 /// specified \a DPred predicate. 207 bool hasExplicitDirective( 208 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 209 unsigned Level); 210 211 /// \brief Finds a directive which matches specified \a DPred predicate. 212 bool hasDirective(const llvm::function_ref<bool(OpenMPDirectiveKind, 213 const DeclarationNameInfo &, 214 SourceLocation)> &DPred, 215 bool FromParent); 216 217 /// \brief Returns currently analyzed directive. 218 OpenMPDirectiveKind getCurrentDirective() const { 219 return Stack.back().Directive; 220 } 221 /// \brief Returns parent directive. 222 OpenMPDirectiveKind getParentDirective() const { 223 if (Stack.size() > 2) 224 return Stack[Stack.size() - 2].Directive; 225 return OMPD_unknown; 226 } 227 228 /// \brief Set default data sharing attribute to none. 229 void setDefaultDSANone(SourceLocation Loc) { 230 Stack.back().DefaultAttr = DSA_none; 231 Stack.back().DefaultAttrLoc = Loc; 232 } 233 /// \brief Set default data sharing attribute to shared. 234 void setDefaultDSAShared(SourceLocation Loc) { 235 Stack.back().DefaultAttr = DSA_shared; 236 Stack.back().DefaultAttrLoc = Loc; 237 } 238 239 DefaultDataSharingAttributes getDefaultDSA() const { 240 return Stack.back().DefaultAttr; 241 } 242 SourceLocation getDefaultDSALocation() const { 243 return Stack.back().DefaultAttrLoc; 244 } 245 246 /// \brief Checks if the specified variable is a threadprivate. 247 bool isThreadPrivate(VarDecl *D) { 248 DSAVarData DVar = getTopDSA(D, false); 249 return isOpenMPThreadPrivate(DVar.CKind); 250 } 251 252 /// \brief Marks current region as ordered (it has an 'ordered' clause). 253 void setOrderedRegion(bool IsOrdered, Expr *Param) { 254 Stack.back().OrderedRegion.setInt(IsOrdered); 255 Stack.back().OrderedRegion.setPointer(Param); 256 } 257 /// \brief Returns true, if parent region is ordered (has associated 258 /// 'ordered' clause), false - otherwise. 259 bool isParentOrderedRegion() const { 260 if (Stack.size() > 2) 261 return Stack[Stack.size() - 2].OrderedRegion.getInt(); 262 return false; 263 } 264 /// \brief Returns optional parameter for the ordered region. 265 Expr *getParentOrderedRegionParam() const { 266 if (Stack.size() > 2) 267 return Stack[Stack.size() - 2].OrderedRegion.getPointer(); 268 return nullptr; 269 } 270 /// \brief Marks current region as nowait (it has a 'nowait' clause). 271 void setNowaitRegion(bool IsNowait = true) { 272 Stack.back().NowaitRegion = IsNowait; 273 } 274 /// \brief Returns true, if parent region is nowait (has associated 275 /// 'nowait' clause), false - otherwise. 276 bool isParentNowaitRegion() const { 277 if (Stack.size() > 2) 278 return Stack[Stack.size() - 2].NowaitRegion; 279 return false; 280 } 281 /// \brief Marks parent region as cancel region. 282 void setParentCancelRegion(bool Cancel = true) { 283 if (Stack.size() > 2) 284 Stack[Stack.size() - 2].CancelRegion = 285 Stack[Stack.size() - 2].CancelRegion || Cancel; 286 } 287 /// \brief Return true if current region has inner cancel construct. 288 bool isCancelRegion() const { 289 return Stack.back().CancelRegion; 290 } 291 292 /// \brief Set collapse value for the region. 293 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; } 294 /// \brief Return collapse value for region. 295 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; } 296 297 /// \brief Marks current target region as one with closely nested teams 298 /// region. 299 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 300 if (Stack.size() > 2) 301 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc; 302 } 303 /// \brief Returns true, if current region has closely nested teams region. 304 bool hasInnerTeamsRegion() const { 305 return getInnerTeamsRegionLoc().isValid(); 306 } 307 /// \brief Returns location of the nested teams region (if any). 308 SourceLocation getInnerTeamsRegionLoc() const { 309 if (Stack.size() > 1) 310 return Stack.back().InnerTeamsRegionLoc; 311 return SourceLocation(); 312 } 313 314 Scope *getCurScope() const { return Stack.back().CurScope; } 315 Scope *getCurScope() { return Stack.back().CurScope; } 316 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; } 317 318 // Do the check specified in \a Check to all component lists and return true 319 // if any issue is found. 320 bool checkMappableExprComponentListsForDecl( 321 ValueDecl *VD, bool CurrentRegionOnly, 322 const llvm::function_ref<bool( 323 OMPClauseMappableExprCommon::MappableExprComponentListRef)> &Check) { 324 auto SI = Stack.rbegin(); 325 auto SE = Stack.rend(); 326 327 if (SI == SE) 328 return false; 329 330 if (CurrentRegionOnly) { 331 SE = std::next(SI); 332 } else { 333 ++SI; 334 } 335 336 for (; SI != SE; ++SI) { 337 auto MI = SI->MappedExprComponents.find(VD); 338 if (MI != SI->MappedExprComponents.end()) 339 for (auto &L : MI->second) 340 if (Check(L)) 341 return true; 342 } 343 return false; 344 } 345 346 // Create a new mappable expression component list associated with a given 347 // declaration and initialize it with the provided list of components. 348 void addMappableExpressionComponents( 349 ValueDecl *VD, 350 OMPClauseMappableExprCommon::MappableExprComponentListRef Components) { 351 assert(Stack.size() > 1 && 352 "Not expecting to retrieve components from a empty stack!"); 353 auto &MEC = Stack.back().MappedExprComponents[VD]; 354 // Create new entry and append the new components there. 355 MEC.resize(MEC.size() + 1); 356 MEC.back().append(Components.begin(), Components.end()); 357 } 358 359 unsigned getNestingLevel() const { 360 assert(Stack.size() > 1); 361 return Stack.size() - 2; 362 } 363 }; 364 bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) { 365 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) || 366 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown; 367 } 368 } // namespace 369 370 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 371 auto *VD = dyn_cast<VarDecl>(D); 372 auto *FD = dyn_cast<FieldDecl>(D); 373 if (VD != nullptr) { 374 VD = VD->getCanonicalDecl(); 375 D = VD; 376 } else { 377 assert(FD); 378 FD = FD->getCanonicalDecl(); 379 D = FD; 380 } 381 return D; 382 } 383 384 DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator& Iter, 385 ValueDecl *D) { 386 D = getCanonicalDecl(D); 387 auto *VD = dyn_cast<VarDecl>(D); 388 auto *FD = dyn_cast<FieldDecl>(D); 389 DSAVarData DVar; 390 if (Iter == std::prev(Stack.rend())) { 391 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 392 // in a region but not in construct] 393 // File-scope or namespace-scope variables referenced in called routines 394 // in the region are shared unless they appear in a threadprivate 395 // directive. 396 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D)) 397 DVar.CKind = OMPC_shared; 398 399 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 400 // in a region but not in construct] 401 // Variables with static storage duration that are declared in called 402 // routines in the region are shared. 403 if (VD && VD->hasGlobalStorage()) 404 DVar.CKind = OMPC_shared; 405 406 // Non-static data members are shared by default. 407 if (FD) 408 DVar.CKind = OMPC_shared; 409 410 return DVar; 411 } 412 413 DVar.DKind = Iter->Directive; 414 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 415 // in a Construct, C/C++, predetermined, p.1] 416 // Variables with automatic storage duration that are declared in a scope 417 // inside the construct are private. 418 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 419 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 420 DVar.CKind = OMPC_private; 421 return DVar; 422 } 423 424 // Explicitly specified attributes and local variables with predetermined 425 // attributes. 426 if (Iter->SharingMap.count(D)) { 427 DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer(); 428 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy; 429 DVar.CKind = Iter->SharingMap[D].Attributes; 430 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 431 return DVar; 432 } 433 434 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 435 // in a Construct, C/C++, implicitly determined, p.1] 436 // In a parallel or task construct, the data-sharing attributes of these 437 // variables are determined by the default clause, if present. 438 switch (Iter->DefaultAttr) { 439 case DSA_shared: 440 DVar.CKind = OMPC_shared; 441 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 442 return DVar; 443 case DSA_none: 444 return DVar; 445 case DSA_unspecified: 446 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 447 // in a Construct, implicitly determined, p.2] 448 // In a parallel construct, if no default clause is present, these 449 // variables are shared. 450 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 451 if (isOpenMPParallelDirective(DVar.DKind) || 452 isOpenMPTeamsDirective(DVar.DKind)) { 453 DVar.CKind = OMPC_shared; 454 return DVar; 455 } 456 457 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 458 // in a Construct, implicitly determined, p.4] 459 // In a task construct, if no default clause is present, a variable that in 460 // the enclosing context is determined to be shared by all implicit tasks 461 // bound to the current team is shared. 462 if (isOpenMPTaskingDirective(DVar.DKind)) { 463 DSAVarData DVarTemp; 464 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend(); 465 I != EE; ++I) { 466 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 467 // Referenced in a Construct, implicitly determined, p.6] 468 // In a task construct, if no default clause is present, a variable 469 // whose data-sharing attribute is not determined by the rules above is 470 // firstprivate. 471 DVarTemp = getDSA(I, D); 472 if (DVarTemp.CKind != OMPC_shared) { 473 DVar.RefExpr = nullptr; 474 DVar.CKind = OMPC_firstprivate; 475 return DVar; 476 } 477 if (isParallelOrTaskRegion(I->Directive)) 478 break; 479 } 480 DVar.CKind = 481 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 482 return DVar; 483 } 484 } 485 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 486 // in a Construct, implicitly determined, p.3] 487 // For constructs other than task, if no default clause is present, these 488 // variables inherit their data-sharing attributes from the enclosing 489 // context. 490 return getDSA(++Iter, D); 491 } 492 493 Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) { 494 assert(Stack.size() > 1 && "Data sharing attributes stack is empty"); 495 D = getCanonicalDecl(D); 496 auto It = Stack.back().AlignedMap.find(D); 497 if (It == Stack.back().AlignedMap.end()) { 498 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 499 Stack.back().AlignedMap[D] = NewDE; 500 return nullptr; 501 } else { 502 assert(It->second && "Unexpected nullptr expr in the aligned map"); 503 return It->second; 504 } 505 return nullptr; 506 } 507 508 void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) { 509 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); 510 D = getCanonicalDecl(D); 511 Stack.back().LCVMap.insert( 512 std::make_pair(D, LCDeclInfo(Stack.back().LCVMap.size() + 1, Capture))); 513 } 514 515 DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) { 516 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); 517 D = getCanonicalDecl(D); 518 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D] 519 : LCDeclInfo(0, nullptr); 520 } 521 522 DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) { 523 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty"); 524 D = getCanonicalDecl(D); 525 return Stack[Stack.size() - 2].LCVMap.count(D) > 0 526 ? Stack[Stack.size() - 2].LCVMap[D] 527 : LCDeclInfo(0, nullptr); 528 } 529 530 ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) { 531 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty"); 532 if (Stack[Stack.size() - 2].LCVMap.size() < I) 533 return nullptr; 534 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) { 535 if (Pair.second.first == I) 536 return Pair.first; 537 } 538 return nullptr; 539 } 540 541 void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A, 542 DeclRefExpr *PrivateCopy) { 543 D = getCanonicalDecl(D); 544 if (A == OMPC_threadprivate) { 545 auto &Data = Stack[0].SharingMap[D]; 546 Data.Attributes = A; 547 Data.RefExpr.setPointer(E); 548 Data.PrivateCopy = nullptr; 549 } else { 550 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); 551 auto &Data = Stack.back().SharingMap[D]; 552 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 553 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 554 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 555 (isLoopControlVariable(D).first && A == OMPC_private)); 556 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 557 Data.RefExpr.setInt(/*IntVal=*/true); 558 return; 559 } 560 const bool IsLastprivate = 561 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 562 Data.Attributes = A; 563 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 564 Data.PrivateCopy = PrivateCopy; 565 if (PrivateCopy) { 566 auto &Data = Stack.back().SharingMap[PrivateCopy->getDecl()]; 567 Data.Attributes = A; 568 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 569 Data.PrivateCopy = nullptr; 570 } 571 } 572 } 573 574 bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) { 575 D = D->getCanonicalDecl(); 576 if (Stack.size() > 2) { 577 reverse_iterator I = Iter, E = std::prev(Stack.rend()); 578 Scope *TopScope = nullptr; 579 while (I != E && !isParallelOrTaskRegion(I->Directive)) { 580 ++I; 581 } 582 if (I == E) 583 return false; 584 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; 585 Scope *CurScope = getCurScope(); 586 while (CurScope != TopScope && !CurScope->isDeclScope(D)) { 587 CurScope = CurScope->getParent(); 588 } 589 return CurScope != TopScope; 590 } 591 return false; 592 } 593 594 /// \brief Build a variable declaration for OpenMP loop iteration variable. 595 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 596 StringRef Name, const AttrVec *Attrs = nullptr) { 597 DeclContext *DC = SemaRef.CurContext; 598 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 599 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 600 VarDecl *Decl = 601 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 602 if (Attrs) { 603 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 604 I != E; ++I) 605 Decl->addAttr(*I); 606 } 607 Decl->setImplicit(); 608 return Decl; 609 } 610 611 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 612 SourceLocation Loc, 613 bool RefersToCapture = false) { 614 D->setReferenced(); 615 D->markUsed(S.Context); 616 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 617 SourceLocation(), D, RefersToCapture, Loc, Ty, 618 VK_LValue); 619 } 620 621 DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) { 622 D = getCanonicalDecl(D); 623 DSAVarData DVar; 624 625 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 626 // in a Construct, C/C++, predetermined, p.1] 627 // Variables appearing in threadprivate directives are threadprivate. 628 auto *VD = dyn_cast<VarDecl>(D); 629 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 630 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 631 SemaRef.getLangOpts().OpenMPUseTLS && 632 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 633 (VD && VD->getStorageClass() == SC_Register && 634 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 635 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 636 D->getLocation()), 637 OMPC_threadprivate); 638 } 639 if (Stack[0].SharingMap.count(D)) { 640 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr.getPointer(); 641 DVar.CKind = OMPC_threadprivate; 642 return DVar; 643 } 644 645 if (Stack.size() == 1) { 646 // Not in OpenMP execution region and top scope was already checked. 647 return DVar; 648 } 649 650 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 651 // in a Construct, C/C++, predetermined, p.4] 652 // Static data members are shared. 653 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 654 // in a Construct, C/C++, predetermined, p.7] 655 // Variables with static storage duration that are declared in a scope 656 // inside the construct are shared. 657 auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; }; 658 if (VD && VD->isStaticDataMember()) { 659 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent); 660 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 661 return DVar; 662 663 DVar.CKind = OMPC_shared; 664 return DVar; 665 } 666 667 QualType Type = D->getType().getNonReferenceType().getCanonicalType(); 668 bool IsConstant = Type.isConstant(SemaRef.getASTContext()); 669 Type = SemaRef.getASTContext().getBaseElementType(Type); 670 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 671 // in a Construct, C/C++, predetermined, p.6] 672 // Variables with const qualified type having no mutable member are 673 // shared. 674 CXXRecordDecl *RD = 675 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; 676 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 677 if (auto *CTD = CTSD->getSpecializedTemplate()) 678 RD = CTD->getTemplatedDecl(); 679 if (IsConstant && 680 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() && 681 RD->hasMutableFields())) { 682 // Variables with const-qualified type having no mutable member may be 683 // listed in a firstprivate clause, even if they are static data members. 684 DSAVarData DVarTemp = hasDSA( 685 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; }, 686 MatchesAlways, FromParent); 687 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr) 688 return DVar; 689 690 DVar.CKind = OMPC_shared; 691 return DVar; 692 } 693 694 // Explicitly specified attributes and local variables with predetermined 695 // attributes. 696 auto StartI = std::next(Stack.rbegin()); 697 auto EndI = std::prev(Stack.rend()); 698 if (FromParent && StartI != EndI) { 699 StartI = std::next(StartI); 700 } 701 auto I = std::prev(StartI); 702 if (I->SharingMap.count(D)) { 703 DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer(); 704 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy; 705 DVar.CKind = I->SharingMap[D].Attributes; 706 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 707 } 708 709 return DVar; 710 } 711 712 DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 713 bool FromParent) { 714 D = getCanonicalDecl(D); 715 auto StartI = Stack.rbegin(); 716 auto EndI = std::prev(Stack.rend()); 717 if (FromParent && StartI != EndI) { 718 StartI = std::next(StartI); 719 } 720 return getDSA(StartI, D); 721 } 722 723 DSAStackTy::DSAVarData 724 DSAStackTy::hasDSA(ValueDecl *D, 725 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 726 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 727 bool FromParent) { 728 D = getCanonicalDecl(D); 729 auto StartI = std::next(Stack.rbegin()); 730 auto EndI = Stack.rend(); 731 if (FromParent && StartI != EndI) { 732 StartI = std::next(StartI); 733 } 734 for (auto I = StartI, EE = EndI; I != EE; ++I) { 735 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive)) 736 continue; 737 DSAVarData DVar = getDSA(I, D); 738 if (CPred(DVar.CKind)) 739 return DVar; 740 } 741 return DSAVarData(); 742 } 743 744 DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 745 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 746 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 747 bool FromParent) { 748 D = getCanonicalDecl(D); 749 auto StartI = std::next(Stack.rbegin()); 750 auto EndI = Stack.rend(); 751 if (FromParent && StartI != EndI) { 752 StartI = std::next(StartI); 753 } 754 for (auto I = StartI, EE = EndI; I != EE; ++I) { 755 if (!DPred(I->Directive)) 756 break; 757 DSAVarData DVar = getDSA(I, D); 758 if (CPred(DVar.CKind)) 759 return DVar; 760 return DSAVarData(); 761 } 762 return DSAVarData(); 763 } 764 765 bool DSAStackTy::hasExplicitDSA( 766 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 767 unsigned Level, bool NotLastprivate) { 768 if (CPred(ClauseKindMode)) 769 return true; 770 D = getCanonicalDecl(D); 771 auto StartI = std::next(Stack.begin()); 772 auto EndI = Stack.end(); 773 if (std::distance(StartI, EndI) <= (int)Level) 774 return false; 775 std::advance(StartI, Level); 776 return (StartI->SharingMap.count(D) > 0) && 777 StartI->SharingMap[D].RefExpr.getPointer() && 778 CPred(StartI->SharingMap[D].Attributes) && 779 (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt()); 780 } 781 782 bool DSAStackTy::hasExplicitDirective( 783 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 784 unsigned Level) { 785 auto StartI = std::next(Stack.begin()); 786 auto EndI = Stack.end(); 787 if (std::distance(StartI, EndI) <= (int)Level) 788 return false; 789 std::advance(StartI, Level); 790 return DPred(StartI->Directive); 791 } 792 793 bool DSAStackTy::hasDirective( 794 const llvm::function_ref<bool(OpenMPDirectiveKind, 795 const DeclarationNameInfo &, SourceLocation)> 796 &DPred, 797 bool FromParent) { 798 auto StartI = std::next(Stack.rbegin()); 799 auto EndI = std::prev(Stack.rend()); 800 if (FromParent && StartI != EndI) { 801 StartI = std::next(StartI); 802 } 803 for (auto I = StartI, EE = EndI; I != EE; ++I) { 804 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 805 return true; 806 } 807 return false; 808 } 809 810 void Sema::InitDataSharingAttributesStack() { 811 VarDataSharingAttributesStack = new DSAStackTy(*this); 812 } 813 814 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 815 816 bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) { 817 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 818 819 auto &Ctx = getASTContext(); 820 bool IsByRef = true; 821 822 // Find the directive that is associated with the provided scope. 823 auto Ty = D->getType(); 824 825 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 826 // This table summarizes how a given variable should be passed to the device 827 // given its type and the clauses where it appears. This table is based on 828 // the description in OpenMP 4.5 [2.10.4, target Construct] and 829 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 830 // 831 // ========================================================================= 832 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 833 // | |(tofrom:scalar)| | pvt | | | | 834 // ========================================================================= 835 // | scl | | | | - | | bycopy| 836 // | scl | | - | x | - | - | bycopy| 837 // | scl | | x | - | - | - | null | 838 // | scl | x | | | - | | byref | 839 // | scl | x | - | x | - | - | bycopy| 840 // | scl | x | x | - | - | - | null | 841 // | scl | | - | - | - | x | byref | 842 // | scl | x | - | - | - | x | byref | 843 // 844 // | agg | n.a. | | | - | | byref | 845 // | agg | n.a. | - | x | - | - | byref | 846 // | agg | n.a. | x | - | - | - | null | 847 // | agg | n.a. | - | - | - | x | byref | 848 // | agg | n.a. | - | - | - | x[] | byref | 849 // 850 // | ptr | n.a. | | | - | | bycopy| 851 // | ptr | n.a. | - | x | - | - | bycopy| 852 // | ptr | n.a. | x | - | - | - | null | 853 // | ptr | n.a. | - | - | - | x | byref | 854 // | ptr | n.a. | - | - | - | x[] | bycopy| 855 // | ptr | n.a. | - | - | x | | bycopy| 856 // | ptr | n.a. | - | - | x | x | bycopy| 857 // | ptr | n.a. | - | - | x | x[] | bycopy| 858 // ========================================================================= 859 // Legend: 860 // scl - scalar 861 // ptr - pointer 862 // agg - aggregate 863 // x - applies 864 // - - invalid in this combination 865 // [] - mapped with an array section 866 // byref - should be mapped by reference 867 // byval - should be mapped by value 868 // null - initialize a local variable to null on the device 869 // 870 // Observations: 871 // - All scalar declarations that show up in a map clause have to be passed 872 // by reference, because they may have been mapped in the enclosing data 873 // environment. 874 // - If the scalar value does not fit the size of uintptr, it has to be 875 // passed by reference, regardless the result in the table above. 876 // - For pointers mapped by value that have either an implicit map or an 877 // array section, the runtime library may pass the NULL value to the 878 // device instead of the value passed to it by the compiler. 879 880 881 if (Ty->isReferenceType()) 882 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 883 884 // Locate map clauses and see if the variable being captured is referred to 885 // in any of those clauses. Here we only care about variables, not fields, 886 // because fields are part of aggregates. 887 bool IsVariableUsedInMapClause = false; 888 bool IsVariableAssociatedWithSection = false; 889 890 DSAStack->checkMappableExprComponentListsForDecl( 891 D, /*CurrentRegionOnly=*/true, 892 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef 893 MapExprComponents) { 894 895 auto EI = MapExprComponents.rbegin(); 896 auto EE = MapExprComponents.rend(); 897 898 assert(EI != EE && "Invalid map expression!"); 899 900 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 901 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 902 903 ++EI; 904 if (EI == EE) 905 return false; 906 907 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 908 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 909 isa<MemberExpr>(EI->getAssociatedExpression())) { 910 IsVariableAssociatedWithSection = true; 911 // There is nothing more we need to know about this variable. 912 return true; 913 } 914 915 // Keep looking for more map info. 916 return false; 917 }); 918 919 if (IsVariableUsedInMapClause) { 920 // If variable is identified in a map clause it is always captured by 921 // reference except if it is a pointer that is dereferenced somehow. 922 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 923 } else { 924 // By default, all the data that has a scalar type is mapped by copy. 925 IsByRef = !Ty->isScalarType(); 926 } 927 } 928 929 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 930 IsByRef = !DSAStack->hasExplicitDSA( 931 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; }, 932 Level, /*NotLastprivate=*/true); 933 } 934 935 // When passing data by copy, we need to make sure it fits the uintptr size 936 // and alignment, because the runtime library only deals with uintptr types. 937 // If it does not fit the uintptr size, we need to pass the data by reference 938 // instead. 939 if (!IsByRef && 940 (Ctx.getTypeSizeInChars(Ty) > 941 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 942 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 943 IsByRef = true; 944 } 945 946 return IsByRef; 947 } 948 949 unsigned Sema::getOpenMPNestingLevel() const { 950 assert(getLangOpts().OpenMP); 951 return DSAStack->getNestingLevel(); 952 } 953 954 VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) { 955 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 956 D = getCanonicalDecl(D); 957 958 // If we are attempting to capture a global variable in a directive with 959 // 'target' we return true so that this global is also mapped to the device. 960 // 961 // FIXME: If the declaration is enclosed in a 'declare target' directive, 962 // then it should not be captured. Therefore, an extra check has to be 963 // inserted here once support for 'declare target' is added. 964 // 965 auto *VD = dyn_cast<VarDecl>(D); 966 if (VD && !VD->hasLocalStorage()) { 967 if (DSAStack->getCurrentDirective() == OMPD_target && 968 !DSAStack->isClauseParsingMode()) 969 return VD; 970 if (DSAStack->getCurScope() && 971 DSAStack->hasDirective( 972 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 973 SourceLocation) -> bool { 974 return isOpenMPTargetExecutionDirective(K); 975 }, 976 false)) 977 return VD; 978 } 979 980 if (DSAStack->getCurrentDirective() != OMPD_unknown && 981 (!DSAStack->isClauseParsingMode() || 982 DSAStack->getParentDirective() != OMPD_unknown)) { 983 auto &&Info = DSAStack->isLoopControlVariable(D); 984 if (Info.first || 985 (VD && VD->hasLocalStorage() && 986 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) || 987 (VD && DSAStack->isForceVarCapturing())) 988 return VD ? VD : Info.second; 989 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 990 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind)) 991 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 992 DVarPrivate = DSAStack->hasDSA( 993 D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; }, 994 DSAStack->isClauseParsingMode()); 995 if (DVarPrivate.CKind != OMPC_unknown) 996 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 997 } 998 return nullptr; 999 } 1000 1001 bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) { 1002 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1003 return DSAStack->hasExplicitDSA( 1004 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level); 1005 } 1006 1007 bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) { 1008 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1009 // Return true if the current level is no longer enclosed in a target region. 1010 1011 auto *VD = dyn_cast<VarDecl>(D); 1012 return VD && !VD->hasLocalStorage() && 1013 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 1014 Level); 1015 } 1016 1017 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 1018 1019 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 1020 const DeclarationNameInfo &DirName, 1021 Scope *CurScope, SourceLocation Loc) { 1022 DSAStack->push(DKind, DirName, CurScope, Loc); 1023 PushExpressionEvaluationContext(PotentiallyEvaluated); 1024 } 1025 1026 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 1027 DSAStack->setClauseParsingMode(K); 1028 } 1029 1030 void Sema::EndOpenMPClause() { 1031 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 1032 } 1033 1034 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 1035 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 1036 // A variable of class type (or array thereof) that appears in a lastprivate 1037 // clause requires an accessible, unambiguous default constructor for the 1038 // class type, unless the list item is also specified in a firstprivate 1039 // clause. 1040 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 1041 for (auto *C : D->clauses()) { 1042 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 1043 SmallVector<Expr *, 8> PrivateCopies; 1044 for (auto *DE : Clause->varlists()) { 1045 if (DE->isValueDependent() || DE->isTypeDependent()) { 1046 PrivateCopies.push_back(nullptr); 1047 continue; 1048 } 1049 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 1050 VarDecl *VD = cast<VarDecl>(DRE->getDecl()); 1051 QualType Type = VD->getType().getNonReferenceType(); 1052 auto DVar = DSAStack->getTopDSA(VD, false); 1053 if (DVar.CKind == OMPC_lastprivate) { 1054 // Generate helper private variable and initialize it with the 1055 // default value. The address of the original variable is replaced 1056 // by the address of the new private variable in CodeGen. This new 1057 // variable is not added to IdResolver, so the code in the OpenMP 1058 // region uses original variable for proper diagnostics. 1059 auto *VDPrivate = buildVarDecl( 1060 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 1061 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr); 1062 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false); 1063 if (VDPrivate->isInvalidDecl()) 1064 continue; 1065 PrivateCopies.push_back(buildDeclRefExpr( 1066 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 1067 } else { 1068 // The variable is also a firstprivate, so initialization sequence 1069 // for private copy is generated already. 1070 PrivateCopies.push_back(nullptr); 1071 } 1072 } 1073 // Set initializers to private copies if no errors were found. 1074 if (PrivateCopies.size() == Clause->varlist_size()) 1075 Clause->setPrivateCopies(PrivateCopies); 1076 } 1077 } 1078 } 1079 1080 DSAStack->pop(); 1081 DiscardCleanupsInEvaluationContext(); 1082 PopExpressionEvaluationContext(); 1083 } 1084 1085 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 1086 Expr *NumIterations, Sema &SemaRef, 1087 Scope *S, DSAStackTy *Stack); 1088 1089 namespace { 1090 1091 class VarDeclFilterCCC : public CorrectionCandidateCallback { 1092 private: 1093 Sema &SemaRef; 1094 1095 public: 1096 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 1097 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1098 NamedDecl *ND = Candidate.getCorrectionDecl(); 1099 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) { 1100 return VD->hasGlobalStorage() && 1101 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1102 SemaRef.getCurScope()); 1103 } 1104 return false; 1105 } 1106 }; 1107 1108 class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback { 1109 private: 1110 Sema &SemaRef; 1111 1112 public: 1113 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 1114 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1115 NamedDecl *ND = Candidate.getCorrectionDecl(); 1116 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) { 1117 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1118 SemaRef.getCurScope()); 1119 } 1120 return false; 1121 } 1122 }; 1123 1124 } // namespace 1125 1126 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 1127 CXXScopeSpec &ScopeSpec, 1128 const DeclarationNameInfo &Id) { 1129 LookupResult Lookup(*this, Id, LookupOrdinaryName); 1130 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 1131 1132 if (Lookup.isAmbiguous()) 1133 return ExprError(); 1134 1135 VarDecl *VD; 1136 if (!Lookup.isSingleResult()) { 1137 if (TypoCorrection Corrected = CorrectTypo( 1138 Id, LookupOrdinaryName, CurScope, nullptr, 1139 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) { 1140 diagnoseTypo(Corrected, 1141 PDiag(Lookup.empty() 1142 ? diag::err_undeclared_var_use_suggest 1143 : diag::err_omp_expected_var_arg_suggest) 1144 << Id.getName()); 1145 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 1146 } else { 1147 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 1148 : diag::err_omp_expected_var_arg) 1149 << Id.getName(); 1150 return ExprError(); 1151 } 1152 } else { 1153 if (!(VD = Lookup.getAsSingle<VarDecl>())) { 1154 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 1155 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 1156 return ExprError(); 1157 } 1158 } 1159 Lookup.suppressDiagnostics(); 1160 1161 // OpenMP [2.9.2, Syntax, C/C++] 1162 // Variables must be file-scope, namespace-scope, or static block-scope. 1163 if (!VD->hasGlobalStorage()) { 1164 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 1165 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal(); 1166 bool IsDecl = 1167 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1168 Diag(VD->getLocation(), 1169 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1170 << VD; 1171 return ExprError(); 1172 } 1173 1174 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 1175 NamedDecl *ND = cast<NamedDecl>(CanonicalVD); 1176 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 1177 // A threadprivate directive for file-scope variables must appear outside 1178 // any definition or declaration. 1179 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 1180 !getCurLexicalContext()->isTranslationUnit()) { 1181 Diag(Id.getLoc(), diag::err_omp_var_scope) 1182 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1183 bool IsDecl = 1184 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1185 Diag(VD->getLocation(), 1186 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1187 << VD; 1188 return ExprError(); 1189 } 1190 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 1191 // A threadprivate directive for static class member variables must appear 1192 // in the class definition, in the same scope in which the member 1193 // variables are declared. 1194 if (CanonicalVD->isStaticDataMember() && 1195 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 1196 Diag(Id.getLoc(), diag::err_omp_var_scope) 1197 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1198 bool IsDecl = 1199 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1200 Diag(VD->getLocation(), 1201 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1202 << VD; 1203 return ExprError(); 1204 } 1205 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 1206 // A threadprivate directive for namespace-scope variables must appear 1207 // outside any definition or declaration other than the namespace 1208 // definition itself. 1209 if (CanonicalVD->getDeclContext()->isNamespace() && 1210 (!getCurLexicalContext()->isFileContext() || 1211 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 1212 Diag(Id.getLoc(), diag::err_omp_var_scope) 1213 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1214 bool IsDecl = 1215 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1216 Diag(VD->getLocation(), 1217 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1218 << VD; 1219 return ExprError(); 1220 } 1221 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 1222 // A threadprivate directive for static block-scope variables must appear 1223 // in the scope of the variable and not in a nested scope. 1224 if (CanonicalVD->isStaticLocal() && CurScope && 1225 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 1226 Diag(Id.getLoc(), diag::err_omp_var_scope) 1227 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1228 bool IsDecl = 1229 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1230 Diag(VD->getLocation(), 1231 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1232 << VD; 1233 return ExprError(); 1234 } 1235 1236 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 1237 // A threadprivate directive must lexically precede all references to any 1238 // of the variables in its list. 1239 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) { 1240 Diag(Id.getLoc(), diag::err_omp_var_used) 1241 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1242 return ExprError(); 1243 } 1244 1245 QualType ExprType = VD->getType().getNonReferenceType(); 1246 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 1247 SourceLocation(), VD, 1248 /*RefersToEnclosingVariableOrCapture=*/false, 1249 Id.getLoc(), ExprType, VK_LValue); 1250 } 1251 1252 Sema::DeclGroupPtrTy 1253 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 1254 ArrayRef<Expr *> VarList) { 1255 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 1256 CurContext->addDecl(D); 1257 return DeclGroupPtrTy::make(DeclGroupRef(D)); 1258 } 1259 return nullptr; 1260 } 1261 1262 namespace { 1263 class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> { 1264 Sema &SemaRef; 1265 1266 public: 1267 bool VisitDeclRefExpr(const DeclRefExpr *E) { 1268 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) { 1269 if (VD->hasLocalStorage()) { 1270 SemaRef.Diag(E->getLocStart(), 1271 diag::err_omp_local_var_in_threadprivate_init) 1272 << E->getSourceRange(); 1273 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 1274 << VD << VD->getSourceRange(); 1275 return true; 1276 } 1277 } 1278 return false; 1279 } 1280 bool VisitStmt(const Stmt *S) { 1281 for (auto Child : S->children()) { 1282 if (Child && Visit(Child)) 1283 return true; 1284 } 1285 return false; 1286 } 1287 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 1288 }; 1289 } // namespace 1290 1291 OMPThreadPrivateDecl * 1292 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 1293 SmallVector<Expr *, 8> Vars; 1294 for (auto &RefExpr : VarList) { 1295 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr); 1296 VarDecl *VD = cast<VarDecl>(DE->getDecl()); 1297 SourceLocation ILoc = DE->getExprLoc(); 1298 1299 // Mark variable as used. 1300 VD->setReferenced(); 1301 VD->markUsed(Context); 1302 1303 QualType QType = VD->getType(); 1304 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 1305 // It will be analyzed later. 1306 Vars.push_back(DE); 1307 continue; 1308 } 1309 1310 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 1311 // A threadprivate variable must not have an incomplete type. 1312 if (RequireCompleteType(ILoc, VD->getType(), 1313 diag::err_omp_threadprivate_incomplete_type)) { 1314 continue; 1315 } 1316 1317 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 1318 // A threadprivate variable must not have a reference type. 1319 if (VD->getType()->isReferenceType()) { 1320 Diag(ILoc, diag::err_omp_ref_type_arg) 1321 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 1322 bool IsDecl = 1323 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1324 Diag(VD->getLocation(), 1325 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1326 << VD; 1327 continue; 1328 } 1329 1330 // Check if this is a TLS variable. If TLS is not being supported, produce 1331 // the corresponding diagnostic. 1332 if ((VD->getTLSKind() != VarDecl::TLS_None && 1333 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1334 getLangOpts().OpenMPUseTLS && 1335 getASTContext().getTargetInfo().isTLSSupported())) || 1336 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 1337 !VD->isLocalVarDecl())) { 1338 Diag(ILoc, diag::err_omp_var_thread_local) 1339 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 1340 bool IsDecl = 1341 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1342 Diag(VD->getLocation(), 1343 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1344 << VD; 1345 continue; 1346 } 1347 1348 // Check if initial value of threadprivate variable reference variable with 1349 // local storage (it is not supported by runtime). 1350 if (auto Init = VD->getAnyInitializer()) { 1351 LocalVarRefChecker Checker(*this); 1352 if (Checker.Visit(Init)) 1353 continue; 1354 } 1355 1356 Vars.push_back(RefExpr); 1357 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 1358 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 1359 Context, SourceRange(Loc, Loc))); 1360 if (auto *ML = Context.getASTMutationListener()) 1361 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 1362 } 1363 OMPThreadPrivateDecl *D = nullptr; 1364 if (!Vars.empty()) { 1365 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 1366 Vars); 1367 D->setAccess(AS_public); 1368 } 1369 return D; 1370 } 1371 1372 static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack, 1373 const ValueDecl *D, DSAStackTy::DSAVarData DVar, 1374 bool IsLoopIterVar = false) { 1375 if (DVar.RefExpr) { 1376 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 1377 << getOpenMPClauseName(DVar.CKind); 1378 return; 1379 } 1380 enum { 1381 PDSA_StaticMemberShared, 1382 PDSA_StaticLocalVarShared, 1383 PDSA_LoopIterVarPrivate, 1384 PDSA_LoopIterVarLinear, 1385 PDSA_LoopIterVarLastprivate, 1386 PDSA_ConstVarShared, 1387 PDSA_GlobalVarShared, 1388 PDSA_TaskVarFirstprivate, 1389 PDSA_LocalVarPrivate, 1390 PDSA_Implicit 1391 } Reason = PDSA_Implicit; 1392 bool ReportHint = false; 1393 auto ReportLoc = D->getLocation(); 1394 auto *VD = dyn_cast<VarDecl>(D); 1395 if (IsLoopIterVar) { 1396 if (DVar.CKind == OMPC_private) 1397 Reason = PDSA_LoopIterVarPrivate; 1398 else if (DVar.CKind == OMPC_lastprivate) 1399 Reason = PDSA_LoopIterVarLastprivate; 1400 else 1401 Reason = PDSA_LoopIterVarLinear; 1402 } else if (isOpenMPTaskingDirective(DVar.DKind) && 1403 DVar.CKind == OMPC_firstprivate) { 1404 Reason = PDSA_TaskVarFirstprivate; 1405 ReportLoc = DVar.ImplicitDSALoc; 1406 } else if (VD && VD->isStaticLocal()) 1407 Reason = PDSA_StaticLocalVarShared; 1408 else if (VD && VD->isStaticDataMember()) 1409 Reason = PDSA_StaticMemberShared; 1410 else if (VD && VD->isFileVarDecl()) 1411 Reason = PDSA_GlobalVarShared; 1412 else if (D->getType().isConstant(SemaRef.getASTContext())) 1413 Reason = PDSA_ConstVarShared; 1414 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 1415 ReportHint = true; 1416 Reason = PDSA_LocalVarPrivate; 1417 } 1418 if (Reason != PDSA_Implicit) { 1419 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 1420 << Reason << ReportHint 1421 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 1422 } else if (DVar.ImplicitDSALoc.isValid()) { 1423 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 1424 << getOpenMPClauseName(DVar.CKind); 1425 } 1426 } 1427 1428 namespace { 1429 class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> { 1430 DSAStackTy *Stack; 1431 Sema &SemaRef; 1432 bool ErrorFound; 1433 CapturedStmt *CS; 1434 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate; 1435 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA; 1436 1437 public: 1438 void VisitDeclRefExpr(DeclRefExpr *E) { 1439 if (E->isTypeDependent() || E->isValueDependent() || 1440 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 1441 return; 1442 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 1443 // Skip internally declared variables. 1444 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD)) 1445 return; 1446 1447 auto DVar = Stack->getTopDSA(VD, false); 1448 // Check if the variable has explicit DSA set and stop analysis if it so. 1449 if (DVar.RefExpr) return; 1450 1451 auto ELoc = E->getExprLoc(); 1452 auto DKind = Stack->getCurrentDirective(); 1453 // The default(none) clause requires that each variable that is referenced 1454 // in the construct, and does not have a predetermined data-sharing 1455 // attribute, must have its data-sharing attribute explicitly determined 1456 // by being listed in a data-sharing attribute clause. 1457 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && 1458 isParallelOrTaskRegion(DKind) && 1459 VarsWithInheritedDSA.count(VD) == 0) { 1460 VarsWithInheritedDSA[VD] = E; 1461 return; 1462 } 1463 1464 // OpenMP [2.9.3.6, Restrictions, p.2] 1465 // A list item that appears in a reduction clause of the innermost 1466 // enclosing worksharing or parallel construct may not be accessed in an 1467 // explicit task. 1468 DVar = Stack->hasInnermostDSA( 1469 VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; }, 1470 [](OpenMPDirectiveKind K) -> bool { 1471 return isOpenMPParallelDirective(K) || 1472 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 1473 }, 1474 false); 1475 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 1476 ErrorFound = true; 1477 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 1478 ReportOriginalDSA(SemaRef, Stack, VD, DVar); 1479 return; 1480 } 1481 1482 // Define implicit data-sharing attributes for task. 1483 DVar = Stack->getImplicitDSA(VD, false); 1484 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 1485 !Stack->isLoopControlVariable(VD).first) 1486 ImplicitFirstprivate.push_back(E); 1487 } 1488 } 1489 void VisitMemberExpr(MemberExpr *E) { 1490 if (E->isTypeDependent() || E->isValueDependent() || 1491 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 1492 return; 1493 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 1494 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { 1495 auto DVar = Stack->getTopDSA(FD, false); 1496 // Check if the variable has explicit DSA set and stop analysis if it 1497 // so. 1498 if (DVar.RefExpr) 1499 return; 1500 1501 auto ELoc = E->getExprLoc(); 1502 auto DKind = Stack->getCurrentDirective(); 1503 // OpenMP [2.9.3.6, Restrictions, p.2] 1504 // A list item that appears in a reduction clause of the innermost 1505 // enclosing worksharing or parallel construct may not be accessed in 1506 // an explicit task. 1507 DVar = Stack->hasInnermostDSA( 1508 FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; }, 1509 [](OpenMPDirectiveKind K) -> bool { 1510 return isOpenMPParallelDirective(K) || 1511 isOpenMPWorksharingDirective(K) || 1512 isOpenMPTeamsDirective(K); 1513 }, 1514 false); 1515 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 1516 ErrorFound = true; 1517 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 1518 ReportOriginalDSA(SemaRef, Stack, FD, DVar); 1519 return; 1520 } 1521 1522 // Define implicit data-sharing attributes for task. 1523 DVar = Stack->getImplicitDSA(FD, false); 1524 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 1525 !Stack->isLoopControlVariable(FD).first) 1526 ImplicitFirstprivate.push_back(E); 1527 } 1528 } 1529 } 1530 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 1531 for (auto *C : S->clauses()) { 1532 // Skip analysis of arguments of implicitly defined firstprivate clause 1533 // for task directives. 1534 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid())) 1535 for (auto *CC : C->children()) { 1536 if (CC) 1537 Visit(CC); 1538 } 1539 } 1540 } 1541 void VisitStmt(Stmt *S) { 1542 for (auto *C : S->children()) { 1543 if (C && !isa<OMPExecutableDirective>(C)) 1544 Visit(C); 1545 } 1546 } 1547 1548 bool isErrorFound() { return ErrorFound; } 1549 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; } 1550 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() { 1551 return VarsWithInheritedDSA; 1552 } 1553 1554 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 1555 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {} 1556 }; 1557 } // namespace 1558 1559 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 1560 switch (DKind) { 1561 case OMPD_parallel: { 1562 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1563 QualType KmpInt32PtrTy = 1564 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 1565 Sema::CapturedParamNameType Params[] = { 1566 std::make_pair(".global_tid.", KmpInt32PtrTy), 1567 std::make_pair(".bound_tid.", KmpInt32PtrTy), 1568 std::make_pair(StringRef(), QualType()) // __context with shared vars 1569 }; 1570 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1571 Params); 1572 break; 1573 } 1574 case OMPD_simd: { 1575 Sema::CapturedParamNameType Params[] = { 1576 std::make_pair(StringRef(), QualType()) // __context with shared vars 1577 }; 1578 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1579 Params); 1580 break; 1581 } 1582 case OMPD_for: { 1583 Sema::CapturedParamNameType Params[] = { 1584 std::make_pair(StringRef(), QualType()) // __context with shared vars 1585 }; 1586 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1587 Params); 1588 break; 1589 } 1590 case OMPD_for_simd: { 1591 Sema::CapturedParamNameType Params[] = { 1592 std::make_pair(StringRef(), QualType()) // __context with shared vars 1593 }; 1594 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1595 Params); 1596 break; 1597 } 1598 case OMPD_sections: { 1599 Sema::CapturedParamNameType Params[] = { 1600 std::make_pair(StringRef(), QualType()) // __context with shared vars 1601 }; 1602 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1603 Params); 1604 break; 1605 } 1606 case OMPD_section: { 1607 Sema::CapturedParamNameType Params[] = { 1608 std::make_pair(StringRef(), QualType()) // __context with shared vars 1609 }; 1610 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1611 Params); 1612 break; 1613 } 1614 case OMPD_single: { 1615 Sema::CapturedParamNameType Params[] = { 1616 std::make_pair(StringRef(), QualType()) // __context with shared vars 1617 }; 1618 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1619 Params); 1620 break; 1621 } 1622 case OMPD_master: { 1623 Sema::CapturedParamNameType Params[] = { 1624 std::make_pair(StringRef(), QualType()) // __context with shared vars 1625 }; 1626 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1627 Params); 1628 break; 1629 } 1630 case OMPD_critical: { 1631 Sema::CapturedParamNameType Params[] = { 1632 std::make_pair(StringRef(), QualType()) // __context with shared vars 1633 }; 1634 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1635 Params); 1636 break; 1637 } 1638 case OMPD_parallel_for: { 1639 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1640 QualType KmpInt32PtrTy = 1641 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 1642 Sema::CapturedParamNameType Params[] = { 1643 std::make_pair(".global_tid.", KmpInt32PtrTy), 1644 std::make_pair(".bound_tid.", KmpInt32PtrTy), 1645 std::make_pair(StringRef(), QualType()) // __context with shared vars 1646 }; 1647 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1648 Params); 1649 break; 1650 } 1651 case OMPD_parallel_for_simd: { 1652 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1653 QualType KmpInt32PtrTy = 1654 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 1655 Sema::CapturedParamNameType Params[] = { 1656 std::make_pair(".global_tid.", KmpInt32PtrTy), 1657 std::make_pair(".bound_tid.", KmpInt32PtrTy), 1658 std::make_pair(StringRef(), QualType()) // __context with shared vars 1659 }; 1660 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1661 Params); 1662 break; 1663 } 1664 case OMPD_parallel_sections: { 1665 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1666 QualType KmpInt32PtrTy = 1667 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 1668 Sema::CapturedParamNameType Params[] = { 1669 std::make_pair(".global_tid.", KmpInt32PtrTy), 1670 std::make_pair(".bound_tid.", KmpInt32PtrTy), 1671 std::make_pair(StringRef(), QualType()) // __context with shared vars 1672 }; 1673 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1674 Params); 1675 break; 1676 } 1677 case OMPD_task: { 1678 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1679 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()}; 1680 FunctionProtoType::ExtProtoInfo EPI; 1681 EPI.Variadic = true; 1682 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 1683 Sema::CapturedParamNameType Params[] = { 1684 std::make_pair(".global_tid.", KmpInt32Ty), 1685 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)), 1686 std::make_pair(".privates.", Context.VoidPtrTy.withConst()), 1687 std::make_pair(".copy_fn.", 1688 Context.getPointerType(CopyFnType).withConst()), 1689 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 1690 std::make_pair(StringRef(), QualType()) // __context with shared vars 1691 }; 1692 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1693 Params); 1694 // Mark this captured region as inlined, because we don't use outlined 1695 // function directly. 1696 getCurCapturedRegion()->TheCapturedDecl->addAttr( 1697 AlwaysInlineAttr::CreateImplicit( 1698 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange())); 1699 break; 1700 } 1701 case OMPD_ordered: { 1702 Sema::CapturedParamNameType Params[] = { 1703 std::make_pair(StringRef(), QualType()) // __context with shared vars 1704 }; 1705 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1706 Params); 1707 break; 1708 } 1709 case OMPD_atomic: { 1710 Sema::CapturedParamNameType Params[] = { 1711 std::make_pair(StringRef(), QualType()) // __context with shared vars 1712 }; 1713 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1714 Params); 1715 break; 1716 } 1717 case OMPD_target_data: 1718 case OMPD_target: 1719 case OMPD_target_parallel: 1720 case OMPD_target_parallel_for: { 1721 Sema::CapturedParamNameType Params[] = { 1722 std::make_pair(StringRef(), QualType()) // __context with shared vars 1723 }; 1724 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1725 Params); 1726 break; 1727 } 1728 case OMPD_teams: { 1729 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1730 QualType KmpInt32PtrTy = 1731 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 1732 Sema::CapturedParamNameType Params[] = { 1733 std::make_pair(".global_tid.", KmpInt32PtrTy), 1734 std::make_pair(".bound_tid.", KmpInt32PtrTy), 1735 std::make_pair(StringRef(), QualType()) // __context with shared vars 1736 }; 1737 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1738 Params); 1739 break; 1740 } 1741 case OMPD_taskgroup: { 1742 Sema::CapturedParamNameType Params[] = { 1743 std::make_pair(StringRef(), QualType()) // __context with shared vars 1744 }; 1745 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1746 Params); 1747 break; 1748 } 1749 case OMPD_taskloop: 1750 case OMPD_taskloop_simd: { 1751 QualType KmpInt32Ty = 1752 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); 1753 QualType KmpUInt64Ty = 1754 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 1755 QualType KmpInt64Ty = 1756 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 1757 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()}; 1758 FunctionProtoType::ExtProtoInfo EPI; 1759 EPI.Variadic = true; 1760 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 1761 Sema::CapturedParamNameType Params[] = { 1762 std::make_pair(".global_tid.", KmpInt32Ty), 1763 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)), 1764 std::make_pair(".privates.", 1765 Context.VoidPtrTy.withConst().withRestrict()), 1766 std::make_pair( 1767 ".copy_fn.", 1768 Context.getPointerType(CopyFnType).withConst().withRestrict()), 1769 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 1770 std::make_pair(".lb.", KmpUInt64Ty), 1771 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty), 1772 std::make_pair(".liter.", KmpInt32Ty), 1773 std::make_pair(StringRef(), QualType()) // __context with shared vars 1774 }; 1775 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1776 Params); 1777 // Mark this captured region as inlined, because we don't use outlined 1778 // function directly. 1779 getCurCapturedRegion()->TheCapturedDecl->addAttr( 1780 AlwaysInlineAttr::CreateImplicit( 1781 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange())); 1782 break; 1783 } 1784 case OMPD_distribute: { 1785 Sema::CapturedParamNameType Params[] = { 1786 std::make_pair(StringRef(), QualType()) // __context with shared vars 1787 }; 1788 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1789 Params); 1790 break; 1791 } 1792 case OMPD_threadprivate: 1793 case OMPD_taskyield: 1794 case OMPD_barrier: 1795 case OMPD_taskwait: 1796 case OMPD_cancellation_point: 1797 case OMPD_cancel: 1798 case OMPD_flush: 1799 case OMPD_target_enter_data: 1800 case OMPD_target_exit_data: 1801 case OMPD_declare_reduction: 1802 case OMPD_declare_simd: 1803 case OMPD_declare_target: 1804 case OMPD_end_declare_target: 1805 llvm_unreachable("OpenMP Directive is not allowed"); 1806 case OMPD_unknown: 1807 llvm_unreachable("Unknown OpenMP directive"); 1808 } 1809 } 1810 1811 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 1812 Expr *CaptureExpr, bool WithInit, 1813 bool AsExpression) { 1814 assert(CaptureExpr); 1815 ASTContext &C = S.getASTContext(); 1816 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 1817 QualType Ty = Init->getType(); 1818 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 1819 if (S.getLangOpts().CPlusPlus) 1820 Ty = C.getLValueReferenceType(Ty); 1821 else { 1822 Ty = C.getPointerType(Ty); 1823 ExprResult Res = 1824 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 1825 if (!Res.isUsable()) 1826 return nullptr; 1827 Init = Res.get(); 1828 } 1829 WithInit = true; 1830 } 1831 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty); 1832 if (!WithInit) 1833 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange())); 1834 S.CurContext->addHiddenDecl(CED); 1835 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false, 1836 /*TypeMayContainAuto=*/true); 1837 return CED; 1838 } 1839 1840 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 1841 bool WithInit) { 1842 OMPCapturedExprDecl *CD; 1843 if (auto *VD = S.IsOpenMPCapturedDecl(D)) 1844 CD = cast<OMPCapturedExprDecl>(VD); 1845 else 1846 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 1847 /*AsExpression=*/false); 1848 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 1849 CaptureExpr->getExprLoc()); 1850 } 1851 1852 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 1853 if (!Ref) { 1854 auto *CD = 1855 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."), 1856 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true); 1857 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 1858 CaptureExpr->getExprLoc()); 1859 } 1860 ExprResult Res = Ref; 1861 if (!S.getLangOpts().CPlusPlus && 1862 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 1863 Ref->getType()->isPointerType()) 1864 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 1865 if (!Res.isUsable()) 1866 return ExprError(); 1867 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get()); 1868 } 1869 1870 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 1871 ArrayRef<OMPClause *> Clauses) { 1872 if (!S.isUsable()) { 1873 ActOnCapturedRegionError(); 1874 return StmtError(); 1875 } 1876 1877 OMPOrderedClause *OC = nullptr; 1878 OMPScheduleClause *SC = nullptr; 1879 SmallVector<OMPLinearClause *, 4> LCs; 1880 // This is required for proper codegen. 1881 for (auto *Clause : Clauses) { 1882 if (isOpenMPPrivate(Clause->getClauseKind()) || 1883 Clause->getClauseKind() == OMPC_copyprivate || 1884 (getLangOpts().OpenMPUseTLS && 1885 getASTContext().getTargetInfo().isTLSSupported() && 1886 Clause->getClauseKind() == OMPC_copyin)) { 1887 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 1888 // Mark all variables in private list clauses as used in inner region. 1889 for (auto *VarRef : Clause->children()) { 1890 if (auto *E = cast_or_null<Expr>(VarRef)) { 1891 MarkDeclarationsReferencedInExpr(E); 1892 } 1893 } 1894 DSAStack->setForceVarCapturing(/*V=*/false); 1895 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) { 1896 // Mark all variables in private list clauses as used in inner region. 1897 // Required for proper codegen of combined directives. 1898 // TODO: add processing for other clauses. 1899 if (auto *C = OMPClauseWithPreInit::get(Clause)) { 1900 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 1901 for (auto *D : DS->decls()) 1902 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 1903 } 1904 } 1905 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 1906 if (auto *E = C->getPostUpdateExpr()) 1907 MarkDeclarationsReferencedInExpr(E); 1908 } 1909 } 1910 if (Clause->getClauseKind() == OMPC_schedule) 1911 SC = cast<OMPScheduleClause>(Clause); 1912 else if (Clause->getClauseKind() == OMPC_ordered) 1913 OC = cast<OMPOrderedClause>(Clause); 1914 else if (Clause->getClauseKind() == OMPC_linear) 1915 LCs.push_back(cast<OMPLinearClause>(Clause)); 1916 } 1917 bool ErrorFound = false; 1918 // OpenMP, 2.7.1 Loop Construct, Restrictions 1919 // The nonmonotonic modifier cannot be specified if an ordered clause is 1920 // specified. 1921 if (SC && 1922 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 1923 SC->getSecondScheduleModifier() == 1924 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 1925 OC) { 1926 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 1927 ? SC->getFirstScheduleModifierLoc() 1928 : SC->getSecondScheduleModifierLoc(), 1929 diag::err_omp_schedule_nonmonotonic_ordered) 1930 << SourceRange(OC->getLocStart(), OC->getLocEnd()); 1931 ErrorFound = true; 1932 } 1933 if (!LCs.empty() && OC && OC->getNumForLoops()) { 1934 for (auto *C : LCs) { 1935 Diag(C->getLocStart(), diag::err_omp_linear_ordered) 1936 << SourceRange(OC->getLocStart(), OC->getLocEnd()); 1937 } 1938 ErrorFound = true; 1939 } 1940 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 1941 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 1942 OC->getNumForLoops()) { 1943 Diag(OC->getLocStart(), diag::err_omp_ordered_simd) 1944 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 1945 ErrorFound = true; 1946 } 1947 if (ErrorFound) { 1948 ActOnCapturedRegionError(); 1949 return StmtError(); 1950 } 1951 return ActOnCapturedRegionEnd(S.get()); 1952 } 1953 1954 static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, 1955 OpenMPDirectiveKind CurrentRegion, 1956 const DeclarationNameInfo &CurrentName, 1957 OpenMPDirectiveKind CancelRegion, 1958 SourceLocation StartLoc) { 1959 // Allowed nesting of constructs 1960 // +------------------+-----------------+------------------------------------+ 1961 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)| 1962 // +------------------+-----------------+------------------------------------+ 1963 // | parallel | parallel | * | 1964 // | parallel | for | * | 1965 // | parallel | for simd | * | 1966 // | parallel | master | * | 1967 // | parallel | critical | * | 1968 // | parallel | simd | * | 1969 // | parallel | sections | * | 1970 // | parallel | section | + | 1971 // | parallel | single | * | 1972 // | parallel | parallel for | * | 1973 // | parallel |parallel for simd| * | 1974 // | parallel |parallel sections| * | 1975 // | parallel | task | * | 1976 // | parallel | taskyield | * | 1977 // | parallel | barrier | * | 1978 // | parallel | taskwait | * | 1979 // | parallel | taskgroup | * | 1980 // | parallel | flush | * | 1981 // | parallel | ordered | + | 1982 // | parallel | atomic | * | 1983 // | parallel | target | * | 1984 // | parallel | target parallel | * | 1985 // | parallel | target parallel | * | 1986 // | | for | | 1987 // | parallel | target enter | * | 1988 // | | data | | 1989 // | parallel | target exit | * | 1990 // | | data | | 1991 // | parallel | teams | + | 1992 // | parallel | cancellation | | 1993 // | | point | ! | 1994 // | parallel | cancel | ! | 1995 // | parallel | taskloop | * | 1996 // | parallel | taskloop simd | * | 1997 // | parallel | distribute | | 1998 // +------------------+-----------------+------------------------------------+ 1999 // | for | parallel | * | 2000 // | for | for | + | 2001 // | for | for simd | + | 2002 // | for | master | + | 2003 // | for | critical | * | 2004 // | for | simd | * | 2005 // | for | sections | + | 2006 // | for | section | + | 2007 // | for | single | + | 2008 // | for | parallel for | * | 2009 // | for |parallel for simd| * | 2010 // | for |parallel sections| * | 2011 // | for | task | * | 2012 // | for | taskyield | * | 2013 // | for | barrier | + | 2014 // | for | taskwait | * | 2015 // | for | taskgroup | * | 2016 // | for | flush | * | 2017 // | for | ordered | * (if construct is ordered) | 2018 // | for | atomic | * | 2019 // | for | target | * | 2020 // | for | target parallel | * | 2021 // | for | target parallel | * | 2022 // | | for | | 2023 // | for | target enter | * | 2024 // | | data | | 2025 // | for | target exit | * | 2026 // | | data | | 2027 // | for | teams | + | 2028 // | for | cancellation | | 2029 // | | point | ! | 2030 // | for | cancel | ! | 2031 // | for | taskloop | * | 2032 // | for | taskloop simd | * | 2033 // | for | distribute | | 2034 // +------------------+-----------------+------------------------------------+ 2035 // | master | parallel | * | 2036 // | master | for | + | 2037 // | master | for simd | + | 2038 // | master | master | * | 2039 // | master | critical | * | 2040 // | master | simd | * | 2041 // | master | sections | + | 2042 // | master | section | + | 2043 // | master | single | + | 2044 // | master | parallel for | * | 2045 // | master |parallel for simd| * | 2046 // | master |parallel sections| * | 2047 // | master | task | * | 2048 // | master | taskyield | * | 2049 // | master | barrier | + | 2050 // | master | taskwait | * | 2051 // | master | taskgroup | * | 2052 // | master | flush | * | 2053 // | master | ordered | + | 2054 // | master | atomic | * | 2055 // | master | target | * | 2056 // | master | target parallel | * | 2057 // | master | target parallel | * | 2058 // | | for | | 2059 // | master | target enter | * | 2060 // | | data | | 2061 // | master | target exit | * | 2062 // | | data | | 2063 // | master | teams | + | 2064 // | master | cancellation | | 2065 // | | point | | 2066 // | master | cancel | | 2067 // | master | taskloop | * | 2068 // | master | taskloop simd | * | 2069 // | master | distribute | | 2070 // +------------------+-----------------+------------------------------------+ 2071 // | critical | parallel | * | 2072 // | critical | for | + | 2073 // | critical | for simd | + | 2074 // | critical | master | * | 2075 // | critical | critical | * (should have different names) | 2076 // | critical | simd | * | 2077 // | critical | sections | + | 2078 // | critical | section | + | 2079 // | critical | single | + | 2080 // | critical | parallel for | * | 2081 // | critical |parallel for simd| * | 2082 // | critical |parallel sections| * | 2083 // | critical | task | * | 2084 // | critical | taskyield | * | 2085 // | critical | barrier | + | 2086 // | critical | taskwait | * | 2087 // | critical | taskgroup | * | 2088 // | critical | ordered | + | 2089 // | critical | atomic | * | 2090 // | critical | target | * | 2091 // | critical | target parallel | * | 2092 // | critical | target parallel | * | 2093 // | | for | | 2094 // | critical | target enter | * | 2095 // | | data | | 2096 // | critical | target exit | * | 2097 // | | data | | 2098 // | critical | teams | + | 2099 // | critical | cancellation | | 2100 // | | point | | 2101 // | critical | cancel | | 2102 // | critical | taskloop | * | 2103 // | critical | taskloop simd | * | 2104 // | critical | distribute | | 2105 // +------------------+-----------------+------------------------------------+ 2106 // | simd | parallel | | 2107 // | simd | for | | 2108 // | simd | for simd | | 2109 // | simd | master | | 2110 // | simd | critical | | 2111 // | simd | simd | * | 2112 // | simd | sections | | 2113 // | simd | section | | 2114 // | simd | single | | 2115 // | simd | parallel for | | 2116 // | simd |parallel for simd| | 2117 // | simd |parallel sections| | 2118 // | simd | task | | 2119 // | simd | taskyield | | 2120 // | simd | barrier | | 2121 // | simd | taskwait | | 2122 // | simd | taskgroup | | 2123 // | simd | flush | | 2124 // | simd | ordered | + (with simd clause) | 2125 // | simd | atomic | | 2126 // | simd | target | | 2127 // | simd | target parallel | | 2128 // | simd | target parallel | | 2129 // | | for | | 2130 // | simd | target enter | | 2131 // | | data | | 2132 // | simd | target exit | | 2133 // | | data | | 2134 // | simd | teams | | 2135 // | simd | cancellation | | 2136 // | | point | | 2137 // | simd | cancel | | 2138 // | simd | taskloop | | 2139 // | simd | taskloop simd | | 2140 // | simd | distribute | | 2141 // +------------------+-----------------+------------------------------------+ 2142 // | for simd | parallel | | 2143 // | for simd | for | | 2144 // | for simd | for simd | | 2145 // | for simd | master | | 2146 // | for simd | critical | | 2147 // | for simd | simd | * | 2148 // | for simd | sections | | 2149 // | for simd | section | | 2150 // | for simd | single | | 2151 // | for simd | parallel for | | 2152 // | for simd |parallel for simd| | 2153 // | for simd |parallel sections| | 2154 // | for simd | task | | 2155 // | for simd | taskyield | | 2156 // | for simd | barrier | | 2157 // | for simd | taskwait | | 2158 // | for simd | taskgroup | | 2159 // | for simd | flush | | 2160 // | for simd | ordered | + (with simd clause) | 2161 // | for simd | atomic | | 2162 // | for simd | target | | 2163 // | for simd | target parallel | | 2164 // | for simd | target parallel | | 2165 // | | for | | 2166 // | for simd | target enter | | 2167 // | | data | | 2168 // | for simd | target exit | | 2169 // | | data | | 2170 // | for simd | teams | | 2171 // | for simd | cancellation | | 2172 // | | point | | 2173 // | for simd | cancel | | 2174 // | for simd | taskloop | | 2175 // | for simd | taskloop simd | | 2176 // | for simd | distribute | | 2177 // +------------------+-----------------+------------------------------------+ 2178 // | parallel for simd| parallel | | 2179 // | parallel for simd| for | | 2180 // | parallel for simd| for simd | | 2181 // | parallel for simd| master | | 2182 // | parallel for simd| critical | | 2183 // | parallel for simd| simd | * | 2184 // | parallel for simd| sections | | 2185 // | parallel for simd| section | | 2186 // | parallel for simd| single | | 2187 // | parallel for simd| parallel for | | 2188 // | parallel for simd|parallel for simd| | 2189 // | parallel for simd|parallel sections| | 2190 // | parallel for simd| task | | 2191 // | parallel for simd| taskyield | | 2192 // | parallel for simd| barrier | | 2193 // | parallel for simd| taskwait | | 2194 // | parallel for simd| taskgroup | | 2195 // | parallel for simd| flush | | 2196 // | parallel for simd| ordered | + (with simd clause) | 2197 // | parallel for simd| atomic | | 2198 // | parallel for simd| target | | 2199 // | parallel for simd| target parallel | | 2200 // | parallel for simd| target parallel | | 2201 // | | for | | 2202 // | parallel for simd| target enter | | 2203 // | | data | | 2204 // | parallel for simd| target exit | | 2205 // | | data | | 2206 // | parallel for simd| teams | | 2207 // | parallel for simd| cancellation | | 2208 // | | point | | 2209 // | parallel for simd| cancel | | 2210 // | parallel for simd| taskloop | | 2211 // | parallel for simd| taskloop simd | | 2212 // | parallel for simd| distribute | | 2213 // +------------------+-----------------+------------------------------------+ 2214 // | sections | parallel | * | 2215 // | sections | for | + | 2216 // | sections | for simd | + | 2217 // | sections | master | + | 2218 // | sections | critical | * | 2219 // | sections | simd | * | 2220 // | sections | sections | + | 2221 // | sections | section | * | 2222 // | sections | single | + | 2223 // | sections | parallel for | * | 2224 // | sections |parallel for simd| * | 2225 // | sections |parallel sections| * | 2226 // | sections | task | * | 2227 // | sections | taskyield | * | 2228 // | sections | barrier | + | 2229 // | sections | taskwait | * | 2230 // | sections | taskgroup | * | 2231 // | sections | flush | * | 2232 // | sections | ordered | + | 2233 // | sections | atomic | * | 2234 // | sections | target | * | 2235 // | sections | target parallel | * | 2236 // | sections | target parallel | * | 2237 // | | for | | 2238 // | sections | target enter | * | 2239 // | | data | | 2240 // | sections | target exit | * | 2241 // | | data | | 2242 // | sections | teams | + | 2243 // | sections | cancellation | | 2244 // | | point | ! | 2245 // | sections | cancel | ! | 2246 // | sections | taskloop | * | 2247 // | sections | taskloop simd | * | 2248 // | sections | distribute | | 2249 // +------------------+-----------------+------------------------------------+ 2250 // | section | parallel | * | 2251 // | section | for | + | 2252 // | section | for simd | + | 2253 // | section | master | + | 2254 // | section | critical | * | 2255 // | section | simd | * | 2256 // | section | sections | + | 2257 // | section | section | + | 2258 // | section | single | + | 2259 // | section | parallel for | * | 2260 // | section |parallel for simd| * | 2261 // | section |parallel sections| * | 2262 // | section | task | * | 2263 // | section | taskyield | * | 2264 // | section | barrier | + | 2265 // | section | taskwait | * | 2266 // | section | taskgroup | * | 2267 // | section | flush | * | 2268 // | section | ordered | + | 2269 // | section | atomic | * | 2270 // | section | target | * | 2271 // | section | target parallel | * | 2272 // | section | target parallel | * | 2273 // | | for | | 2274 // | section | target enter | * | 2275 // | | data | | 2276 // | section | target exit | * | 2277 // | | data | | 2278 // | section | teams | + | 2279 // | section | cancellation | | 2280 // | | point | ! | 2281 // | section | cancel | ! | 2282 // | section | taskloop | * | 2283 // | section | taskloop simd | * | 2284 // | section | distribute | | 2285 // +------------------+-----------------+------------------------------------+ 2286 // | single | parallel | * | 2287 // | single | for | + | 2288 // | single | for simd | + | 2289 // | single | master | + | 2290 // | single | critical | * | 2291 // | single | simd | * | 2292 // | single | sections | + | 2293 // | single | section | + | 2294 // | single | single | + | 2295 // | single | parallel for | * | 2296 // | single |parallel for simd| * | 2297 // | single |parallel sections| * | 2298 // | single | task | * | 2299 // | single | taskyield | * | 2300 // | single | barrier | + | 2301 // | single | taskwait | * | 2302 // | single | taskgroup | * | 2303 // | single | flush | * | 2304 // | single | ordered | + | 2305 // | single | atomic | * | 2306 // | single | target | * | 2307 // | single | target parallel | * | 2308 // | single | target parallel | * | 2309 // | | for | | 2310 // | single | target enter | * | 2311 // | | data | | 2312 // | single | target exit | * | 2313 // | | data | | 2314 // | single | teams | + | 2315 // | single | cancellation | | 2316 // | | point | | 2317 // | single | cancel | | 2318 // | single | taskloop | * | 2319 // | single | taskloop simd | * | 2320 // | single | distribute | | 2321 // +------------------+-----------------+------------------------------------+ 2322 // | parallel for | parallel | * | 2323 // | parallel for | for | + | 2324 // | parallel for | for simd | + | 2325 // | parallel for | master | + | 2326 // | parallel for | critical | * | 2327 // | parallel for | simd | * | 2328 // | parallel for | sections | + | 2329 // | parallel for | section | + | 2330 // | parallel for | single | + | 2331 // | parallel for | parallel for | * | 2332 // | parallel for |parallel for simd| * | 2333 // | parallel for |parallel sections| * | 2334 // | parallel for | task | * | 2335 // | parallel for | taskyield | * | 2336 // | parallel for | barrier | + | 2337 // | parallel for | taskwait | * | 2338 // | parallel for | taskgroup | * | 2339 // | parallel for | flush | * | 2340 // | parallel for | ordered | * (if construct is ordered) | 2341 // | parallel for | atomic | * | 2342 // | parallel for | target | * | 2343 // | parallel for | target parallel | * | 2344 // | parallel for | target parallel | * | 2345 // | | for | | 2346 // | parallel for | target enter | * | 2347 // | | data | | 2348 // | parallel for | target exit | * | 2349 // | | data | | 2350 // | parallel for | teams | + | 2351 // | parallel for | cancellation | | 2352 // | | point | ! | 2353 // | parallel for | cancel | ! | 2354 // | parallel for | taskloop | * | 2355 // | parallel for | taskloop simd | * | 2356 // | parallel for | distribute | | 2357 // +------------------+-----------------+------------------------------------+ 2358 // | parallel sections| parallel | * | 2359 // | parallel sections| for | + | 2360 // | parallel sections| for simd | + | 2361 // | parallel sections| master | + | 2362 // | parallel sections| critical | + | 2363 // | parallel sections| simd | * | 2364 // | parallel sections| sections | + | 2365 // | parallel sections| section | * | 2366 // | parallel sections| single | + | 2367 // | parallel sections| parallel for | * | 2368 // | parallel sections|parallel for simd| * | 2369 // | parallel sections|parallel sections| * | 2370 // | parallel sections| task | * | 2371 // | parallel sections| taskyield | * | 2372 // | parallel sections| barrier | + | 2373 // | parallel sections| taskwait | * | 2374 // | parallel sections| taskgroup | * | 2375 // | parallel sections| flush | * | 2376 // | parallel sections| ordered | + | 2377 // | parallel sections| atomic | * | 2378 // | parallel sections| target | * | 2379 // | parallel sections| target parallel | * | 2380 // | parallel sections| target parallel | * | 2381 // | | for | | 2382 // | parallel sections| target enter | * | 2383 // | | data | | 2384 // | parallel sections| target exit | * | 2385 // | | data | | 2386 // | parallel sections| teams | + | 2387 // | parallel sections| cancellation | | 2388 // | | point | ! | 2389 // | parallel sections| cancel | ! | 2390 // | parallel sections| taskloop | * | 2391 // | parallel sections| taskloop simd | * | 2392 // | parallel sections| distribute | | 2393 // +------------------+-----------------+------------------------------------+ 2394 // | task | parallel | * | 2395 // | task | for | + | 2396 // | task | for simd | + | 2397 // | task | master | + | 2398 // | task | critical | * | 2399 // | task | simd | * | 2400 // | task | sections | + | 2401 // | task | section | + | 2402 // | task | single | + | 2403 // | task | parallel for | * | 2404 // | task |parallel for simd| * | 2405 // | task |parallel sections| * | 2406 // | task | task | * | 2407 // | task | taskyield | * | 2408 // | task | barrier | + | 2409 // | task | taskwait | * | 2410 // | task | taskgroup | * | 2411 // | task | flush | * | 2412 // | task | ordered | + | 2413 // | task | atomic | * | 2414 // | task | target | * | 2415 // | task | target parallel | * | 2416 // | task | target parallel | * | 2417 // | | for | | 2418 // | task | target enter | * | 2419 // | | data | | 2420 // | task | target exit | * | 2421 // | | data | | 2422 // | task | teams | + | 2423 // | task | cancellation | | 2424 // | | point | ! | 2425 // | task | cancel | ! | 2426 // | task | taskloop | * | 2427 // | task | taskloop simd | * | 2428 // | task | distribute | | 2429 // +------------------+-----------------+------------------------------------+ 2430 // | ordered | parallel | * | 2431 // | ordered | for | + | 2432 // | ordered | for simd | + | 2433 // | ordered | master | * | 2434 // | ordered | critical | * | 2435 // | ordered | simd | * | 2436 // | ordered | sections | + | 2437 // | ordered | section | + | 2438 // | ordered | single | + | 2439 // | ordered | parallel for | * | 2440 // | ordered |parallel for simd| * | 2441 // | ordered |parallel sections| * | 2442 // | ordered | task | * | 2443 // | ordered | taskyield | * | 2444 // | ordered | barrier | + | 2445 // | ordered | taskwait | * | 2446 // | ordered | taskgroup | * | 2447 // | ordered | flush | * | 2448 // | ordered | ordered | + | 2449 // | ordered | atomic | * | 2450 // | ordered | target | * | 2451 // | ordered | target parallel | * | 2452 // | ordered | target parallel | * | 2453 // | | for | | 2454 // | ordered | target enter | * | 2455 // | | data | | 2456 // | ordered | target exit | * | 2457 // | | data | | 2458 // | ordered | teams | + | 2459 // | ordered | cancellation | | 2460 // | | point | | 2461 // | ordered | cancel | | 2462 // | ordered | taskloop | * | 2463 // | ordered | taskloop simd | * | 2464 // | ordered | distribute | | 2465 // +------------------+-----------------+------------------------------------+ 2466 // | atomic | parallel | | 2467 // | atomic | for | | 2468 // | atomic | for simd | | 2469 // | atomic | master | | 2470 // | atomic | critical | | 2471 // | atomic | simd | | 2472 // | atomic | sections | | 2473 // | atomic | section | | 2474 // | atomic | single | | 2475 // | atomic | parallel for | | 2476 // | atomic |parallel for simd| | 2477 // | atomic |parallel sections| | 2478 // | atomic | task | | 2479 // | atomic | taskyield | | 2480 // | atomic | barrier | | 2481 // | atomic | taskwait | | 2482 // | atomic | taskgroup | | 2483 // | atomic | flush | | 2484 // | atomic | ordered | | 2485 // | atomic | atomic | | 2486 // | atomic | target | | 2487 // | atomic | target parallel | | 2488 // | atomic | target parallel | | 2489 // | | for | | 2490 // | atomic | target enter | | 2491 // | | data | | 2492 // | atomic | target exit | | 2493 // | | data | | 2494 // | atomic | teams | | 2495 // | atomic | cancellation | | 2496 // | | point | | 2497 // | atomic | cancel | | 2498 // | atomic | taskloop | | 2499 // | atomic | taskloop simd | | 2500 // | atomic | distribute | | 2501 // +------------------+-----------------+------------------------------------+ 2502 // | target | parallel | * | 2503 // | target | for | * | 2504 // | target | for simd | * | 2505 // | target | master | * | 2506 // | target | critical | * | 2507 // | target | simd | * | 2508 // | target | sections | * | 2509 // | target | section | * | 2510 // | target | single | * | 2511 // | target | parallel for | * | 2512 // | target |parallel for simd| * | 2513 // | target |parallel sections| * | 2514 // | target | task | * | 2515 // | target | taskyield | * | 2516 // | target | barrier | * | 2517 // | target | taskwait | * | 2518 // | target | taskgroup | * | 2519 // | target | flush | * | 2520 // | target | ordered | * | 2521 // | target | atomic | * | 2522 // | target | target | | 2523 // | target | target parallel | | 2524 // | target | target parallel | | 2525 // | | for | | 2526 // | target | target enter | | 2527 // | | data | | 2528 // | target | target exit | | 2529 // | | data | | 2530 // | target | teams | * | 2531 // | target | cancellation | | 2532 // | | point | | 2533 // | target | cancel | | 2534 // | target | taskloop | * | 2535 // | target | taskloop simd | * | 2536 // | target | distribute | | 2537 // +------------------+-----------------+------------------------------------+ 2538 // | target parallel | parallel | * | 2539 // | target parallel | for | * | 2540 // | target parallel | for simd | * | 2541 // | target parallel | master | * | 2542 // | target parallel | critical | * | 2543 // | target parallel | simd | * | 2544 // | target parallel | sections | * | 2545 // | target parallel | section | * | 2546 // | target parallel | single | * | 2547 // | target parallel | parallel for | * | 2548 // | target parallel |parallel for simd| * | 2549 // | target parallel |parallel sections| * | 2550 // | target parallel | task | * | 2551 // | target parallel | taskyield | * | 2552 // | target parallel | barrier | * | 2553 // | target parallel | taskwait | * | 2554 // | target parallel | taskgroup | * | 2555 // | target parallel | flush | * | 2556 // | target parallel | ordered | * | 2557 // | target parallel | atomic | * | 2558 // | target parallel | target | | 2559 // | target parallel | target parallel | | 2560 // | target parallel | target parallel | | 2561 // | | for | | 2562 // | target parallel | target enter | | 2563 // | | data | | 2564 // | target parallel | target exit | | 2565 // | | data | | 2566 // | target parallel | teams | | 2567 // | target parallel | cancellation | | 2568 // | | point | ! | 2569 // | target parallel | cancel | ! | 2570 // | target parallel | taskloop | * | 2571 // | target parallel | taskloop simd | * | 2572 // | target parallel | distribute | | 2573 // +------------------+-----------------+------------------------------------+ 2574 // | target parallel | parallel | * | 2575 // | for | | | 2576 // | target parallel | for | * | 2577 // | for | | | 2578 // | target parallel | for simd | * | 2579 // | for | | | 2580 // | target parallel | master | * | 2581 // | for | | | 2582 // | target parallel | critical | * | 2583 // | for | | | 2584 // | target parallel | simd | * | 2585 // | for | | | 2586 // | target parallel | sections | * | 2587 // | for | | | 2588 // | target parallel | section | * | 2589 // | for | | | 2590 // | target parallel | single | * | 2591 // | for | | | 2592 // | target parallel | parallel for | * | 2593 // | for | | | 2594 // | target parallel |parallel for simd| * | 2595 // | for | | | 2596 // | target parallel |parallel sections| * | 2597 // | for | | | 2598 // | target parallel | task | * | 2599 // | for | | | 2600 // | target parallel | taskyield | * | 2601 // | for | | | 2602 // | target parallel | barrier | * | 2603 // | for | | | 2604 // | target parallel | taskwait | * | 2605 // | for | | | 2606 // | target parallel | taskgroup | * | 2607 // | for | | | 2608 // | target parallel | flush | * | 2609 // | for | | | 2610 // | target parallel | ordered | * | 2611 // | for | | | 2612 // | target parallel | atomic | * | 2613 // | for | | | 2614 // | target parallel | target | | 2615 // | for | | | 2616 // | target parallel | target parallel | | 2617 // | for | | | 2618 // | target parallel | target parallel | | 2619 // | for | for | | 2620 // | target parallel | target enter | | 2621 // | for | data | | 2622 // | target parallel | target exit | | 2623 // | for | data | | 2624 // | target parallel | teams | | 2625 // | for | | | 2626 // | target parallel | cancellation | | 2627 // | for | point | ! | 2628 // | target parallel | cancel | ! | 2629 // | for | | | 2630 // | target parallel | taskloop | * | 2631 // | for | | | 2632 // | target parallel | taskloop simd | * | 2633 // | for | | | 2634 // | target parallel | distribute | | 2635 // | for | | | 2636 // +------------------+-----------------+------------------------------------+ 2637 // | teams | parallel | * | 2638 // | teams | for | + | 2639 // | teams | for simd | + | 2640 // | teams | master | + | 2641 // | teams | critical | + | 2642 // | teams | simd | + | 2643 // | teams | sections | + | 2644 // | teams | section | + | 2645 // | teams | single | + | 2646 // | teams | parallel for | * | 2647 // | teams |parallel for simd| * | 2648 // | teams |parallel sections| * | 2649 // | teams | task | + | 2650 // | teams | taskyield | + | 2651 // | teams | barrier | + | 2652 // | teams | taskwait | + | 2653 // | teams | taskgroup | + | 2654 // | teams | flush | + | 2655 // | teams | ordered | + | 2656 // | teams | atomic | + | 2657 // | teams | target | + | 2658 // | teams | target parallel | + | 2659 // | teams | target parallel | + | 2660 // | | for | | 2661 // | teams | target enter | + | 2662 // | | data | | 2663 // | teams | target exit | + | 2664 // | | data | | 2665 // | teams | teams | + | 2666 // | teams | cancellation | | 2667 // | | point | | 2668 // | teams | cancel | | 2669 // | teams | taskloop | + | 2670 // | teams | taskloop simd | + | 2671 // | teams | distribute | ! | 2672 // +------------------+-----------------+------------------------------------+ 2673 // | taskloop | parallel | * | 2674 // | taskloop | for | + | 2675 // | taskloop | for simd | + | 2676 // | taskloop | master | + | 2677 // | taskloop | critical | * | 2678 // | taskloop | simd | * | 2679 // | taskloop | sections | + | 2680 // | taskloop | section | + | 2681 // | taskloop | single | + | 2682 // | taskloop | parallel for | * | 2683 // | taskloop |parallel for simd| * | 2684 // | taskloop |parallel sections| * | 2685 // | taskloop | task | * | 2686 // | taskloop | taskyield | * | 2687 // | taskloop | barrier | + | 2688 // | taskloop | taskwait | * | 2689 // | taskloop | taskgroup | * | 2690 // | taskloop | flush | * | 2691 // | taskloop | ordered | + | 2692 // | taskloop | atomic | * | 2693 // | taskloop | target | * | 2694 // | taskloop | target parallel | * | 2695 // | taskloop | target parallel | * | 2696 // | | for | | 2697 // | taskloop | target enter | * | 2698 // | | data | | 2699 // | taskloop | target exit | * | 2700 // | | data | | 2701 // | taskloop | teams | + | 2702 // | taskloop | cancellation | | 2703 // | | point | | 2704 // | taskloop | cancel | | 2705 // | taskloop | taskloop | * | 2706 // | taskloop | distribute | | 2707 // +------------------+-----------------+------------------------------------+ 2708 // | taskloop simd | parallel | | 2709 // | taskloop simd | for | | 2710 // | taskloop simd | for simd | | 2711 // | taskloop simd | master | | 2712 // | taskloop simd | critical | | 2713 // | taskloop simd | simd | * | 2714 // | taskloop simd | sections | | 2715 // | taskloop simd | section | | 2716 // | taskloop simd | single | | 2717 // | taskloop simd | parallel for | | 2718 // | taskloop simd |parallel for simd| | 2719 // | taskloop simd |parallel sections| | 2720 // | taskloop simd | task | | 2721 // | taskloop simd | taskyield | | 2722 // | taskloop simd | barrier | | 2723 // | taskloop simd | taskwait | | 2724 // | taskloop simd | taskgroup | | 2725 // | taskloop simd | flush | | 2726 // | taskloop simd | ordered | + (with simd clause) | 2727 // | taskloop simd | atomic | | 2728 // | taskloop simd | target | | 2729 // | taskloop simd | target parallel | | 2730 // | taskloop simd | target parallel | | 2731 // | | for | | 2732 // | taskloop simd | target enter | | 2733 // | | data | | 2734 // | taskloop simd | target exit | | 2735 // | | data | | 2736 // | taskloop simd | teams | | 2737 // | taskloop simd | cancellation | | 2738 // | | point | | 2739 // | taskloop simd | cancel | | 2740 // | taskloop simd | taskloop | | 2741 // | taskloop simd | taskloop simd | | 2742 // | taskloop simd | distribute | | 2743 // +------------------+-----------------+------------------------------------+ 2744 // | distribute | parallel | * | 2745 // | distribute | for | * | 2746 // | distribute | for simd | * | 2747 // | distribute | master | * | 2748 // | distribute | critical | * | 2749 // | distribute | simd | * | 2750 // | distribute | sections | * | 2751 // | distribute | section | * | 2752 // | distribute | single | * | 2753 // | distribute | parallel for | * | 2754 // | distribute |parallel for simd| * | 2755 // | distribute |parallel sections| * | 2756 // | distribute | task | * | 2757 // | distribute | taskyield | * | 2758 // | distribute | barrier | * | 2759 // | distribute | taskwait | * | 2760 // | distribute | taskgroup | * | 2761 // | distribute | flush | * | 2762 // | distribute | ordered | + | 2763 // | distribute | atomic | * | 2764 // | distribute | target | | 2765 // | distribute | target parallel | | 2766 // | distribute | target parallel | | 2767 // | | for | | 2768 // | distribute | target enter | | 2769 // | | data | | 2770 // | distribute | target exit | | 2771 // | | data | | 2772 // | distribute | teams | | 2773 // | distribute | cancellation | + | 2774 // | | point | | 2775 // | distribute | cancel | + | 2776 // | distribute | taskloop | * | 2777 // | distribute | taskloop simd | * | 2778 // | distribute | distribute | | 2779 // +------------------+-----------------+------------------------------------+ 2780 if (Stack->getCurScope()) { 2781 auto ParentRegion = Stack->getParentDirective(); 2782 auto OffendingRegion = ParentRegion; 2783 bool NestingProhibited = false; 2784 bool CloseNesting = true; 2785 enum { 2786 NoRecommend, 2787 ShouldBeInParallelRegion, 2788 ShouldBeInOrderedRegion, 2789 ShouldBeInTargetRegion, 2790 ShouldBeInTeamsRegion 2791 } Recommend = NoRecommend; 2792 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered && 2793 CurrentRegion != OMPD_simd) { 2794 // OpenMP [2.16, Nesting of Regions] 2795 // OpenMP constructs may not be nested inside a simd region. 2796 // OpenMP [2.8.1,simd Construct, Restrictions] 2797 // An ordered construct with the simd clause is the only OpenMP construct 2798 // that can appear in the simd region. 2799 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd); 2800 return true; 2801 } 2802 if (ParentRegion == OMPD_atomic) { 2803 // OpenMP [2.16, Nesting of Regions] 2804 // OpenMP constructs may not be nested inside an atomic region. 2805 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 2806 return true; 2807 } 2808 if (CurrentRegion == OMPD_section) { 2809 // OpenMP [2.7.2, sections Construct, Restrictions] 2810 // Orphaned section directives are prohibited. That is, the section 2811 // directives must appear within the sections construct and must not be 2812 // encountered elsewhere in the sections region. 2813 if (ParentRegion != OMPD_sections && 2814 ParentRegion != OMPD_parallel_sections) { 2815 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 2816 << (ParentRegion != OMPD_unknown) 2817 << getOpenMPDirectiveName(ParentRegion); 2818 return true; 2819 } 2820 return false; 2821 } 2822 // Allow some constructs to be orphaned (they could be used in functions, 2823 // called from OpenMP regions with the required preconditions). 2824 if (ParentRegion == OMPD_unknown) 2825 return false; 2826 if (CurrentRegion == OMPD_cancellation_point || 2827 CurrentRegion == OMPD_cancel) { 2828 // OpenMP [2.16, Nesting of Regions] 2829 // A cancellation point construct for which construct-type-clause is 2830 // taskgroup must be nested inside a task construct. A cancellation 2831 // point construct for which construct-type-clause is not taskgroup must 2832 // be closely nested inside an OpenMP construct that matches the type 2833 // specified in construct-type-clause. 2834 // A cancel construct for which construct-type-clause is taskgroup must be 2835 // nested inside a task construct. A cancel construct for which 2836 // construct-type-clause is not taskgroup must be closely nested inside an 2837 // OpenMP construct that matches the type specified in 2838 // construct-type-clause. 2839 NestingProhibited = 2840 !((CancelRegion == OMPD_parallel && 2841 (ParentRegion == OMPD_parallel || 2842 ParentRegion == OMPD_target_parallel)) || 2843 (CancelRegion == OMPD_for && 2844 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 2845 ParentRegion == OMPD_target_parallel_for)) || 2846 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) || 2847 (CancelRegion == OMPD_sections && 2848 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 2849 ParentRegion == OMPD_parallel_sections))); 2850 } else if (CurrentRegion == OMPD_master) { 2851 // OpenMP [2.16, Nesting of Regions] 2852 // A master region may not be closely nested inside a worksharing, 2853 // atomic, or explicit task region. 2854 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 2855 isOpenMPTaskingDirective(ParentRegion); 2856 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 2857 // OpenMP [2.16, Nesting of Regions] 2858 // A critical region may not be nested (closely or otherwise) inside a 2859 // critical region with the same name. Note that this restriction is not 2860 // sufficient to prevent deadlock. 2861 SourceLocation PreviousCriticalLoc; 2862 bool DeadLock = 2863 Stack->hasDirective([CurrentName, &PreviousCriticalLoc]( 2864 OpenMPDirectiveKind K, 2865 const DeclarationNameInfo &DNI, 2866 SourceLocation Loc) 2867 ->bool { 2868 if (K == OMPD_critical && 2869 DNI.getName() == CurrentName.getName()) { 2870 PreviousCriticalLoc = Loc; 2871 return true; 2872 } else 2873 return false; 2874 }, 2875 false /* skip top directive */); 2876 if (DeadLock) { 2877 SemaRef.Diag(StartLoc, 2878 diag::err_omp_prohibited_region_critical_same_name) 2879 << CurrentName.getName(); 2880 if (PreviousCriticalLoc.isValid()) 2881 SemaRef.Diag(PreviousCriticalLoc, 2882 diag::note_omp_previous_critical_region); 2883 return true; 2884 } 2885 } else if (CurrentRegion == OMPD_barrier) { 2886 // OpenMP [2.16, Nesting of Regions] 2887 // A barrier region may not be closely nested inside a worksharing, 2888 // explicit task, critical, ordered, atomic, or master region. 2889 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 2890 isOpenMPTaskingDirective(ParentRegion) || 2891 ParentRegion == OMPD_master || 2892 ParentRegion == OMPD_critical || 2893 ParentRegion == OMPD_ordered; 2894 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 2895 !isOpenMPParallelDirective(CurrentRegion)) { 2896 // OpenMP [2.16, Nesting of Regions] 2897 // A worksharing region may not be closely nested inside a worksharing, 2898 // explicit task, critical, ordered, atomic, or master region. 2899 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 2900 isOpenMPTaskingDirective(ParentRegion) || 2901 ParentRegion == OMPD_master || 2902 ParentRegion == OMPD_critical || 2903 ParentRegion == OMPD_ordered; 2904 Recommend = ShouldBeInParallelRegion; 2905 } else if (CurrentRegion == OMPD_ordered) { 2906 // OpenMP [2.16, Nesting of Regions] 2907 // An ordered region may not be closely nested inside a critical, 2908 // atomic, or explicit task region. 2909 // An ordered region must be closely nested inside a loop region (or 2910 // parallel loop region) with an ordered clause. 2911 // OpenMP [2.8.1,simd Construct, Restrictions] 2912 // An ordered construct with the simd clause is the only OpenMP construct 2913 // that can appear in the simd region. 2914 NestingProhibited = ParentRegion == OMPD_critical || 2915 isOpenMPTaskingDirective(ParentRegion) || 2916 !(isOpenMPSimdDirective(ParentRegion) || 2917 Stack->isParentOrderedRegion()); 2918 Recommend = ShouldBeInOrderedRegion; 2919 } else if (isOpenMPTeamsDirective(CurrentRegion)) { 2920 // OpenMP [2.16, Nesting of Regions] 2921 // If specified, a teams construct must be contained within a target 2922 // construct. 2923 NestingProhibited = ParentRegion != OMPD_target; 2924 Recommend = ShouldBeInTargetRegion; 2925 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc()); 2926 } 2927 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) { 2928 // OpenMP [2.16, Nesting of Regions] 2929 // distribute, parallel, parallel sections, parallel workshare, and the 2930 // parallel loop and parallel loop SIMD constructs are the only OpenMP 2931 // constructs that can be closely nested in the teams region. 2932 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 2933 !isOpenMPDistributeDirective(CurrentRegion); 2934 Recommend = ShouldBeInParallelRegion; 2935 } 2936 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) { 2937 // OpenMP 4.5 [2.17 Nesting of Regions] 2938 // The region associated with the distribute construct must be strictly 2939 // nested inside a teams region 2940 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion); 2941 Recommend = ShouldBeInTeamsRegion; 2942 } 2943 if (!NestingProhibited && 2944 (isOpenMPTargetExecutionDirective(CurrentRegion) || 2945 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 2946 // OpenMP 4.5 [2.17 Nesting of Regions] 2947 // If a target, target update, target data, target enter data, or 2948 // target exit data construct is encountered during execution of a 2949 // target region, the behavior is unspecified. 2950 NestingProhibited = Stack->hasDirective( 2951 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 2952 SourceLocation) -> bool { 2953 if (isOpenMPTargetExecutionDirective(K)) { 2954 OffendingRegion = K; 2955 return true; 2956 } else 2957 return false; 2958 }, 2959 false /* don't skip top directive */); 2960 CloseNesting = false; 2961 } 2962 if (NestingProhibited) { 2963 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 2964 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 2965 << Recommend << getOpenMPDirectiveName(CurrentRegion); 2966 return true; 2967 } 2968 } 2969 return false; 2970 } 2971 2972 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 2973 ArrayRef<OMPClause *> Clauses, 2974 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 2975 bool ErrorFound = false; 2976 unsigned NamedModifiersNumber = 0; 2977 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers( 2978 OMPD_unknown + 1); 2979 SmallVector<SourceLocation, 4> NameModifierLoc; 2980 for (const auto *C : Clauses) { 2981 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 2982 // At most one if clause without a directive-name-modifier can appear on 2983 // the directive. 2984 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 2985 if (FoundNameModifiers[CurNM]) { 2986 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause) 2987 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 2988 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 2989 ErrorFound = true; 2990 } else if (CurNM != OMPD_unknown) { 2991 NameModifierLoc.push_back(IC->getNameModifierLoc()); 2992 ++NamedModifiersNumber; 2993 } 2994 FoundNameModifiers[CurNM] = IC; 2995 if (CurNM == OMPD_unknown) 2996 continue; 2997 // Check if the specified name modifier is allowed for the current 2998 // directive. 2999 // At most one if clause with the particular directive-name-modifier can 3000 // appear on the directive. 3001 bool MatchFound = false; 3002 for (auto NM : AllowedNameModifiers) { 3003 if (CurNM == NM) { 3004 MatchFound = true; 3005 break; 3006 } 3007 } 3008 if (!MatchFound) { 3009 S.Diag(IC->getNameModifierLoc(), 3010 diag::err_omp_wrong_if_directive_name_modifier) 3011 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 3012 ErrorFound = true; 3013 } 3014 } 3015 } 3016 // If any if clause on the directive includes a directive-name-modifier then 3017 // all if clauses on the directive must include a directive-name-modifier. 3018 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 3019 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 3020 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(), 3021 diag::err_omp_no_more_if_clause); 3022 } else { 3023 std::string Values; 3024 std::string Sep(", "); 3025 unsigned AllowedCnt = 0; 3026 unsigned TotalAllowedNum = 3027 AllowedNameModifiers.size() - NamedModifiersNumber; 3028 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 3029 ++Cnt) { 3030 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 3031 if (!FoundNameModifiers[NM]) { 3032 Values += "'"; 3033 Values += getOpenMPDirectiveName(NM); 3034 Values += "'"; 3035 if (AllowedCnt + 2 == TotalAllowedNum) 3036 Values += " or "; 3037 else if (AllowedCnt + 1 != TotalAllowedNum) 3038 Values += Sep; 3039 ++AllowedCnt; 3040 } 3041 } 3042 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(), 3043 diag::err_omp_unnamed_if_clause) 3044 << (TotalAllowedNum > 1) << Values; 3045 } 3046 for (auto Loc : NameModifierLoc) { 3047 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 3048 } 3049 ErrorFound = true; 3050 } 3051 return ErrorFound; 3052 } 3053 3054 StmtResult Sema::ActOnOpenMPExecutableDirective( 3055 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 3056 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 3057 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 3058 StmtResult Res = StmtError(); 3059 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 3060 StartLoc)) 3061 return StmtError(); 3062 3063 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 3064 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA; 3065 bool ErrorFound = false; 3066 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 3067 if (AStmt) { 3068 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3069 3070 // Check default data sharing attributes for referenced variables. 3071 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 3072 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt()); 3073 if (DSAChecker.isErrorFound()) 3074 return StmtError(); 3075 // Generate list of implicitly defined firstprivate variables. 3076 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 3077 3078 if (!DSAChecker.getImplicitFirstprivate().empty()) { 3079 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 3080 DSAChecker.getImplicitFirstprivate(), SourceLocation(), 3081 SourceLocation(), SourceLocation())) { 3082 ClausesWithImplicit.push_back(Implicit); 3083 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 3084 DSAChecker.getImplicitFirstprivate().size(); 3085 } else 3086 ErrorFound = true; 3087 } 3088 } 3089 3090 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 3091 switch (Kind) { 3092 case OMPD_parallel: 3093 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 3094 EndLoc); 3095 AllowedNameModifiers.push_back(OMPD_parallel); 3096 break; 3097 case OMPD_simd: 3098 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 3099 VarsWithInheritedDSA); 3100 break; 3101 case OMPD_for: 3102 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 3103 VarsWithInheritedDSA); 3104 break; 3105 case OMPD_for_simd: 3106 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 3107 EndLoc, VarsWithInheritedDSA); 3108 break; 3109 case OMPD_sections: 3110 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 3111 EndLoc); 3112 break; 3113 case OMPD_section: 3114 assert(ClausesWithImplicit.empty() && 3115 "No clauses are allowed for 'omp section' directive"); 3116 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 3117 break; 3118 case OMPD_single: 3119 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 3120 EndLoc); 3121 break; 3122 case OMPD_master: 3123 assert(ClausesWithImplicit.empty() && 3124 "No clauses are allowed for 'omp master' directive"); 3125 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 3126 break; 3127 case OMPD_critical: 3128 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 3129 StartLoc, EndLoc); 3130 break; 3131 case OMPD_parallel_for: 3132 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 3133 EndLoc, VarsWithInheritedDSA); 3134 AllowedNameModifiers.push_back(OMPD_parallel); 3135 break; 3136 case OMPD_parallel_for_simd: 3137 Res = ActOnOpenMPParallelForSimdDirective( 3138 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3139 AllowedNameModifiers.push_back(OMPD_parallel); 3140 break; 3141 case OMPD_parallel_sections: 3142 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 3143 StartLoc, EndLoc); 3144 AllowedNameModifiers.push_back(OMPD_parallel); 3145 break; 3146 case OMPD_task: 3147 Res = 3148 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 3149 AllowedNameModifiers.push_back(OMPD_task); 3150 break; 3151 case OMPD_taskyield: 3152 assert(ClausesWithImplicit.empty() && 3153 "No clauses are allowed for 'omp taskyield' directive"); 3154 assert(AStmt == nullptr && 3155 "No associated statement allowed for 'omp taskyield' directive"); 3156 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 3157 break; 3158 case OMPD_barrier: 3159 assert(ClausesWithImplicit.empty() && 3160 "No clauses are allowed for 'omp barrier' directive"); 3161 assert(AStmt == nullptr && 3162 "No associated statement allowed for 'omp barrier' directive"); 3163 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 3164 break; 3165 case OMPD_taskwait: 3166 assert(ClausesWithImplicit.empty() && 3167 "No clauses are allowed for 'omp taskwait' directive"); 3168 assert(AStmt == nullptr && 3169 "No associated statement allowed for 'omp taskwait' directive"); 3170 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 3171 break; 3172 case OMPD_taskgroup: 3173 assert(ClausesWithImplicit.empty() && 3174 "No clauses are allowed for 'omp taskgroup' directive"); 3175 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc); 3176 break; 3177 case OMPD_flush: 3178 assert(AStmt == nullptr && 3179 "No associated statement allowed for 'omp flush' directive"); 3180 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 3181 break; 3182 case OMPD_ordered: 3183 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 3184 EndLoc); 3185 break; 3186 case OMPD_atomic: 3187 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 3188 EndLoc); 3189 break; 3190 case OMPD_teams: 3191 Res = 3192 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 3193 break; 3194 case OMPD_target: 3195 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 3196 EndLoc); 3197 AllowedNameModifiers.push_back(OMPD_target); 3198 break; 3199 case OMPD_target_parallel: 3200 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 3201 StartLoc, EndLoc); 3202 AllowedNameModifiers.push_back(OMPD_target); 3203 AllowedNameModifiers.push_back(OMPD_parallel); 3204 break; 3205 case OMPD_target_parallel_for: 3206 Res = ActOnOpenMPTargetParallelForDirective( 3207 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3208 AllowedNameModifiers.push_back(OMPD_target); 3209 AllowedNameModifiers.push_back(OMPD_parallel); 3210 break; 3211 case OMPD_cancellation_point: 3212 assert(ClausesWithImplicit.empty() && 3213 "No clauses are allowed for 'omp cancellation point' directive"); 3214 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 3215 "cancellation point' directive"); 3216 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 3217 break; 3218 case OMPD_cancel: 3219 assert(AStmt == nullptr && 3220 "No associated statement allowed for 'omp cancel' directive"); 3221 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 3222 CancelRegion); 3223 AllowedNameModifiers.push_back(OMPD_cancel); 3224 break; 3225 case OMPD_target_data: 3226 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 3227 EndLoc); 3228 AllowedNameModifiers.push_back(OMPD_target_data); 3229 break; 3230 case OMPD_target_enter_data: 3231 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 3232 EndLoc); 3233 AllowedNameModifiers.push_back(OMPD_target_enter_data); 3234 break; 3235 case OMPD_target_exit_data: 3236 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 3237 EndLoc); 3238 AllowedNameModifiers.push_back(OMPD_target_exit_data); 3239 break; 3240 case OMPD_taskloop: 3241 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 3242 EndLoc, VarsWithInheritedDSA); 3243 AllowedNameModifiers.push_back(OMPD_taskloop); 3244 break; 3245 case OMPD_taskloop_simd: 3246 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 3247 EndLoc, VarsWithInheritedDSA); 3248 AllowedNameModifiers.push_back(OMPD_taskloop); 3249 break; 3250 case OMPD_distribute: 3251 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 3252 EndLoc, VarsWithInheritedDSA); 3253 break; 3254 case OMPD_declare_target: 3255 case OMPD_end_declare_target: 3256 case OMPD_threadprivate: 3257 case OMPD_declare_reduction: 3258 case OMPD_declare_simd: 3259 llvm_unreachable("OpenMP Directive is not allowed"); 3260 case OMPD_unknown: 3261 llvm_unreachable("Unknown OpenMP directive"); 3262 } 3263 3264 for (auto P : VarsWithInheritedDSA) { 3265 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 3266 << P.first << P.second->getSourceRange(); 3267 } 3268 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound; 3269 3270 if (!AllowedNameModifiers.empty()) 3271 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 3272 ErrorFound; 3273 3274 if (ErrorFound) 3275 return StmtError(); 3276 return Res; 3277 } 3278 3279 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 3280 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 3281 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 3282 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 3283 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 3284 assert(Aligneds.size() == Alignments.size()); 3285 assert(Linears.size() == LinModifiers.size()); 3286 assert(Linears.size() == Steps.size()); 3287 if (!DG || DG.get().isNull()) 3288 return DeclGroupPtrTy(); 3289 3290 if (!DG.get().isSingleDecl()) { 3291 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd); 3292 return DG; 3293 } 3294 auto *ADecl = DG.get().getSingleDecl(); 3295 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 3296 ADecl = FTD->getTemplatedDecl(); 3297 3298 auto *FD = dyn_cast<FunctionDecl>(ADecl); 3299 if (!FD) { 3300 Diag(ADecl->getLocation(), diag::err_omp_function_expected); 3301 return DeclGroupPtrTy(); 3302 } 3303 3304 // OpenMP [2.8.2, declare simd construct, Description] 3305 // The parameter of the simdlen clause must be a constant positive integer 3306 // expression. 3307 ExprResult SL; 3308 if (Simdlen) 3309 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 3310 // OpenMP [2.8.2, declare simd construct, Description] 3311 // The special this pointer can be used as if was one of the arguments to the 3312 // function in any of the linear, aligned, or uniform clauses. 3313 // The uniform clause declares one or more arguments to have an invariant 3314 // value for all concurrent invocations of the function in the execution of a 3315 // single SIMD loop. 3316 llvm::DenseMap<Decl *, Expr *> UniformedArgs; 3317 Expr *UniformedLinearThis = nullptr; 3318 for (auto *E : Uniforms) { 3319 E = E->IgnoreParenImpCasts(); 3320 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) 3321 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 3322 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 3323 FD->getParamDecl(PVD->getFunctionScopeIndex()) 3324 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 3325 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E)); 3326 continue; 3327 } 3328 if (isa<CXXThisExpr>(E)) { 3329 UniformedLinearThis = E; 3330 continue; 3331 } 3332 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 3333 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 3334 } 3335 // OpenMP [2.8.2, declare simd construct, Description] 3336 // The aligned clause declares that the object to which each list item points 3337 // is aligned to the number of bytes expressed in the optional parameter of 3338 // the aligned clause. 3339 // The special this pointer can be used as if was one of the arguments to the 3340 // function in any of the linear, aligned, or uniform clauses. 3341 // The type of list items appearing in the aligned clause must be array, 3342 // pointer, reference to array, or reference to pointer. 3343 llvm::DenseMap<Decl *, Expr *> AlignedArgs; 3344 Expr *AlignedThis = nullptr; 3345 for (auto *E : Aligneds) { 3346 E = E->IgnoreParenImpCasts(); 3347 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) 3348 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 3349 auto *CanonPVD = PVD->getCanonicalDecl(); 3350 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 3351 FD->getParamDecl(PVD->getFunctionScopeIndex()) 3352 ->getCanonicalDecl() == CanonPVD) { 3353 // OpenMP [2.8.1, simd construct, Restrictions] 3354 // A list-item cannot appear in more than one aligned clause. 3355 if (AlignedArgs.count(CanonPVD) > 0) { 3356 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 3357 << 1 << E->getSourceRange(); 3358 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 3359 diag::note_omp_explicit_dsa) 3360 << getOpenMPClauseName(OMPC_aligned); 3361 continue; 3362 } 3363 AlignedArgs[CanonPVD] = E; 3364 QualType QTy = PVD->getType() 3365 .getNonReferenceType() 3366 .getUnqualifiedType() 3367 .getCanonicalType(); 3368 const Type *Ty = QTy.getTypePtrOrNull(); 3369 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 3370 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 3371 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 3372 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 3373 } 3374 continue; 3375 } 3376 } 3377 if (isa<CXXThisExpr>(E)) { 3378 if (AlignedThis) { 3379 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 3380 << 2 << E->getSourceRange(); 3381 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 3382 << getOpenMPClauseName(OMPC_aligned); 3383 } 3384 AlignedThis = E; 3385 continue; 3386 } 3387 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 3388 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 3389 } 3390 // The optional parameter of the aligned clause, alignment, must be a constant 3391 // positive integer expression. If no optional parameter is specified, 3392 // implementation-defined default alignments for SIMD instructions on the 3393 // target platforms are assumed. 3394 SmallVector<Expr *, 4> NewAligns; 3395 for (auto *E : Alignments) { 3396 ExprResult Align; 3397 if (E) 3398 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 3399 NewAligns.push_back(Align.get()); 3400 } 3401 // OpenMP [2.8.2, declare simd construct, Description] 3402 // The linear clause declares one or more list items to be private to a SIMD 3403 // lane and to have a linear relationship with respect to the iteration space 3404 // of a loop. 3405 // The special this pointer can be used as if was one of the arguments to the 3406 // function in any of the linear, aligned, or uniform clauses. 3407 // When a linear-step expression is specified in a linear clause it must be 3408 // either a constant integer expression or an integer-typed parameter that is 3409 // specified in a uniform clause on the directive. 3410 llvm::DenseMap<Decl *, Expr *> LinearArgs; 3411 const bool IsUniformedThis = UniformedLinearThis != nullptr; 3412 auto MI = LinModifiers.begin(); 3413 for (auto *E : Linears) { 3414 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 3415 ++MI; 3416 E = E->IgnoreParenImpCasts(); 3417 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) 3418 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 3419 auto *CanonPVD = PVD->getCanonicalDecl(); 3420 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 3421 FD->getParamDecl(PVD->getFunctionScopeIndex()) 3422 ->getCanonicalDecl() == CanonPVD) { 3423 // OpenMP [2.15.3.7, linear Clause, Restrictions] 3424 // A list-item cannot appear in more than one linear clause. 3425 if (LinearArgs.count(CanonPVD) > 0) { 3426 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3427 << getOpenMPClauseName(OMPC_linear) 3428 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 3429 Diag(LinearArgs[CanonPVD]->getExprLoc(), 3430 diag::note_omp_explicit_dsa) 3431 << getOpenMPClauseName(OMPC_linear); 3432 continue; 3433 } 3434 // Each argument can appear in at most one uniform or linear clause. 3435 if (UniformedArgs.count(CanonPVD) > 0) { 3436 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3437 << getOpenMPClauseName(OMPC_linear) 3438 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 3439 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 3440 diag::note_omp_explicit_dsa) 3441 << getOpenMPClauseName(OMPC_uniform); 3442 continue; 3443 } 3444 LinearArgs[CanonPVD] = E; 3445 if (E->isValueDependent() || E->isTypeDependent() || 3446 E->isInstantiationDependent() || 3447 E->containsUnexpandedParameterPack()) 3448 continue; 3449 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 3450 PVD->getOriginalType()); 3451 continue; 3452 } 3453 } 3454 if (isa<CXXThisExpr>(E)) { 3455 if (UniformedLinearThis) { 3456 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3457 << getOpenMPClauseName(OMPC_linear) 3458 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 3459 << E->getSourceRange(); 3460 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 3461 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 3462 : OMPC_linear); 3463 continue; 3464 } 3465 UniformedLinearThis = E; 3466 if (E->isValueDependent() || E->isTypeDependent() || 3467 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 3468 continue; 3469 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 3470 E->getType()); 3471 continue; 3472 } 3473 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 3474 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 3475 } 3476 Expr *Step = nullptr; 3477 Expr *NewStep = nullptr; 3478 SmallVector<Expr *, 4> NewSteps; 3479 for (auto *E : Steps) { 3480 // Skip the same step expression, it was checked already. 3481 if (Step == E || !E) { 3482 NewSteps.push_back(E ? NewStep : nullptr); 3483 continue; 3484 } 3485 Step = E; 3486 if (auto *DRE = dyn_cast<DeclRefExpr>(Step)) 3487 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 3488 auto *CanonPVD = PVD->getCanonicalDecl(); 3489 if (UniformedArgs.count(CanonPVD) == 0) { 3490 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 3491 << Step->getSourceRange(); 3492 } else if (E->isValueDependent() || E->isTypeDependent() || 3493 E->isInstantiationDependent() || 3494 E->containsUnexpandedParameterPack() || 3495 CanonPVD->getType()->hasIntegerRepresentation()) 3496 NewSteps.push_back(Step); 3497 else { 3498 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 3499 << Step->getSourceRange(); 3500 } 3501 continue; 3502 } 3503 NewStep = Step; 3504 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 3505 !Step->isInstantiationDependent() && 3506 !Step->containsUnexpandedParameterPack()) { 3507 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 3508 .get(); 3509 if (NewStep) 3510 NewStep = VerifyIntegerConstantExpression(NewStep).get(); 3511 } 3512 NewSteps.push_back(NewStep); 3513 } 3514 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 3515 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 3516 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 3517 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 3518 const_cast<Expr **>(Linears.data()), Linears.size(), 3519 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 3520 NewSteps.data(), NewSteps.size(), SR); 3521 ADecl->addAttr(NewAttr); 3522 return ConvertDeclToDeclGroup(ADecl); 3523 } 3524 3525 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 3526 Stmt *AStmt, 3527 SourceLocation StartLoc, 3528 SourceLocation EndLoc) { 3529 if (!AStmt) 3530 return StmtError(); 3531 3532 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 3533 // 1.2.2 OpenMP Language Terminology 3534 // Structured block - An executable statement with a single entry at the 3535 // top and a single exit at the bottom. 3536 // The point of exit cannot be a branch out of the structured block. 3537 // longjmp() and throw() must not violate the entry/exit criteria. 3538 CS->getCapturedDecl()->setNothrow(); 3539 3540 getCurFunction()->setHasBranchProtectedScope(); 3541 3542 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 3543 DSAStack->isCancelRegion()); 3544 } 3545 3546 namespace { 3547 /// \brief Helper class for checking canonical form of the OpenMP loops and 3548 /// extracting iteration space of each loop in the loop nest, that will be used 3549 /// for IR generation. 3550 class OpenMPIterationSpaceChecker { 3551 /// \brief Reference to Sema. 3552 Sema &SemaRef; 3553 /// \brief A location for diagnostics (when there is no some better location). 3554 SourceLocation DefaultLoc; 3555 /// \brief A location for diagnostics (when increment is not compatible). 3556 SourceLocation ConditionLoc; 3557 /// \brief A source location for referring to loop init later. 3558 SourceRange InitSrcRange; 3559 /// \brief A source location for referring to condition later. 3560 SourceRange ConditionSrcRange; 3561 /// \brief A source location for referring to increment later. 3562 SourceRange IncrementSrcRange; 3563 /// \brief Loop variable. 3564 ValueDecl *LCDecl = nullptr; 3565 /// \brief Reference to loop variable. 3566 Expr *LCRef = nullptr; 3567 /// \brief Lower bound (initializer for the var). 3568 Expr *LB = nullptr; 3569 /// \brief Upper bound. 3570 Expr *UB = nullptr; 3571 /// \brief Loop step (increment). 3572 Expr *Step = nullptr; 3573 /// \brief This flag is true when condition is one of: 3574 /// Var < UB 3575 /// Var <= UB 3576 /// UB > Var 3577 /// UB >= Var 3578 bool TestIsLessOp = false; 3579 /// \brief This flag is true when condition is strict ( < or > ). 3580 bool TestIsStrictOp = false; 3581 /// \brief This flag is true when step is subtracted on each iteration. 3582 bool SubtractStep = false; 3583 3584 public: 3585 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc) 3586 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {} 3587 /// \brief Check init-expr for canonical loop form and save loop counter 3588 /// variable - #Var and its initialization value - #LB. 3589 bool CheckInit(Stmt *S, bool EmitDiags = true); 3590 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags 3591 /// for less/greater and for strict/non-strict comparison. 3592 bool CheckCond(Expr *S); 3593 /// \brief Check incr-expr for canonical loop form and return true if it 3594 /// does not conform, otherwise save loop step (#Step). 3595 bool CheckInc(Expr *S); 3596 /// \brief Return the loop counter variable. 3597 ValueDecl *GetLoopDecl() const { return LCDecl; } 3598 /// \brief Return the reference expression to loop counter variable. 3599 Expr *GetLoopDeclRefExpr() const { return LCRef; } 3600 /// \brief Source range of the loop init. 3601 SourceRange GetInitSrcRange() const { return InitSrcRange; } 3602 /// \brief Source range of the loop condition. 3603 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; } 3604 /// \brief Source range of the loop increment. 3605 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; } 3606 /// \brief True if the step should be subtracted. 3607 bool ShouldSubtractStep() const { return SubtractStep; } 3608 /// \brief Build the expression to calculate the number of iterations. 3609 Expr * 3610 BuildNumIterations(Scope *S, const bool LimitedType, 3611 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const; 3612 /// \brief Build the precondition expression for the loops. 3613 Expr *BuildPreCond(Scope *S, Expr *Cond, 3614 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const; 3615 /// \brief Build reference expression to the counter be used for codegen. 3616 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures, 3617 DSAStackTy &DSA) const; 3618 /// \brief Build reference expression to the private counter be used for 3619 /// codegen. 3620 Expr *BuildPrivateCounterVar() const; 3621 /// \brief Build initization of the counter be used for codegen. 3622 Expr *BuildCounterInit() const; 3623 /// \brief Build step of the counter be used for codegen. 3624 Expr *BuildCounterStep() const; 3625 /// \brief Return true if any expression is dependent. 3626 bool Dependent() const; 3627 3628 private: 3629 /// \brief Check the right-hand side of an assignment in the increment 3630 /// expression. 3631 bool CheckIncRHS(Expr *RHS); 3632 /// \brief Helper to set loop counter variable and its initializer. 3633 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB); 3634 /// \brief Helper to set upper bound. 3635 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR, 3636 SourceLocation SL); 3637 /// \brief Helper to set loop increment. 3638 bool SetStep(Expr *NewStep, bool Subtract); 3639 }; 3640 3641 bool OpenMPIterationSpaceChecker::Dependent() const { 3642 if (!LCDecl) { 3643 assert(!LB && !UB && !Step); 3644 return false; 3645 } 3646 return LCDecl->getType()->isDependentType() || 3647 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 3648 (Step && Step->isValueDependent()); 3649 } 3650 3651 static Expr *getExprAsWritten(Expr *E) { 3652 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E)) 3653 E = ExprTemp->getSubExpr(); 3654 3655 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 3656 E = MTE->GetTemporaryExpr(); 3657 3658 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 3659 E = Binder->getSubExpr(); 3660 3661 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 3662 E = ICE->getSubExprAsWritten(); 3663 return E->IgnoreParens(); 3664 } 3665 3666 bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl, 3667 Expr *NewLCRefExpr, 3668 Expr *NewLB) { 3669 // State consistency checking to ensure correct usage. 3670 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 3671 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 3672 if (!NewLCDecl || !NewLB) 3673 return true; 3674 LCDecl = getCanonicalDecl(NewLCDecl); 3675 LCRef = NewLCRefExpr; 3676 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 3677 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 3678 if ((Ctor->isCopyOrMoveConstructor() || 3679 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 3680 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 3681 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 3682 LB = NewLB; 3683 return false; 3684 } 3685 3686 bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp, 3687 SourceRange SR, SourceLocation SL) { 3688 // State consistency checking to ensure correct usage. 3689 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 3690 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 3691 if (!NewUB) 3692 return true; 3693 UB = NewUB; 3694 TestIsLessOp = LessOp; 3695 TestIsStrictOp = StrictOp; 3696 ConditionSrcRange = SR; 3697 ConditionLoc = SL; 3698 return false; 3699 } 3700 3701 bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) { 3702 // State consistency checking to ensure correct usage. 3703 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 3704 if (!NewStep) 3705 return true; 3706 if (!NewStep->isValueDependent()) { 3707 // Check that the step is integer expression. 3708 SourceLocation StepLoc = NewStep->getLocStart(); 3709 ExprResult Val = 3710 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep); 3711 if (Val.isInvalid()) 3712 return true; 3713 NewStep = Val.get(); 3714 3715 // OpenMP [2.6, Canonical Loop Form, Restrictions] 3716 // If test-expr is of form var relational-op b and relational-op is < or 3717 // <= then incr-expr must cause var to increase on each iteration of the 3718 // loop. If test-expr is of form var relational-op b and relational-op is 3719 // > or >= then incr-expr must cause var to decrease on each iteration of 3720 // the loop. 3721 // If test-expr is of form b relational-op var and relational-op is < or 3722 // <= then incr-expr must cause var to decrease on each iteration of the 3723 // loop. If test-expr is of form b relational-op var and relational-op is 3724 // > or >= then incr-expr must cause var to increase on each iteration of 3725 // the loop. 3726 llvm::APSInt Result; 3727 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); 3728 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 3729 bool IsConstNeg = 3730 IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); 3731 bool IsConstPos = 3732 IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); 3733 bool IsConstZero = IsConstant && !Result.getBoolValue(); 3734 if (UB && (IsConstZero || 3735 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract)) 3736 : (IsConstPos || (IsUnsigned && !Subtract))))) { 3737 SemaRef.Diag(NewStep->getExprLoc(), 3738 diag::err_omp_loop_incr_not_compatible) 3739 << LCDecl << TestIsLessOp << NewStep->getSourceRange(); 3740 SemaRef.Diag(ConditionLoc, 3741 diag::note_omp_loop_cond_requres_compatible_incr) 3742 << TestIsLessOp << ConditionSrcRange; 3743 return true; 3744 } 3745 if (TestIsLessOp == Subtract) { 3746 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, 3747 NewStep).get(); 3748 Subtract = !Subtract; 3749 } 3750 } 3751 3752 Step = NewStep; 3753 SubtractStep = Subtract; 3754 return false; 3755 } 3756 3757 bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) { 3758 // Check init-expr for canonical loop form and save loop counter 3759 // variable - #Var and its initialization value - #LB. 3760 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 3761 // var = lb 3762 // integer-type var = lb 3763 // random-access-iterator-type var = lb 3764 // pointer-type var = lb 3765 // 3766 if (!S) { 3767 if (EmitDiags) { 3768 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 3769 } 3770 return true; 3771 } 3772 InitSrcRange = S->getSourceRange(); 3773 if (Expr *E = dyn_cast<Expr>(S)) 3774 S = E->IgnoreParens(); 3775 if (auto BO = dyn_cast<BinaryOperator>(S)) { 3776 if (BO->getOpcode() == BO_Assign) { 3777 auto *LHS = BO->getLHS()->IgnoreParens(); 3778 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 3779 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 3780 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 3781 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3782 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS()); 3783 } 3784 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 3785 if (ME->isArrow() && 3786 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 3787 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3788 } 3789 } 3790 } else if (auto DS = dyn_cast<DeclStmt>(S)) { 3791 if (DS->isSingleDecl()) { 3792 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 3793 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 3794 // Accept non-canonical init form here but emit ext. warning. 3795 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 3796 SemaRef.Diag(S->getLocStart(), 3797 diag::ext_omp_loop_not_canonical_init) 3798 << S->getSourceRange(); 3799 return SetLCDeclAndLB(Var, nullptr, Var->getInit()); 3800 } 3801 } 3802 } 3803 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) { 3804 if (CE->getOperator() == OO_Equal) { 3805 auto *LHS = CE->getArg(0); 3806 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) { 3807 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 3808 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 3809 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3810 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1)); 3811 } 3812 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 3813 if (ME->isArrow() && 3814 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 3815 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3816 } 3817 } 3818 } 3819 3820 if (Dependent() || SemaRef.CurContext->isDependentContext()) 3821 return false; 3822 if (EmitDiags) { 3823 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init) 3824 << S->getSourceRange(); 3825 } 3826 return true; 3827 } 3828 3829 /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the 3830 /// variable (which may be the loop variable) if possible. 3831 static const ValueDecl *GetInitLCDecl(Expr *E) { 3832 if (!E) 3833 return nullptr; 3834 E = getExprAsWritten(E); 3835 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 3836 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 3837 if ((Ctor->isCopyOrMoveConstructor() || 3838 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 3839 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 3840 E = CE->getArg(0)->IgnoreParenImpCasts(); 3841 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 3842 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) { 3843 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD)) 3844 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 3845 return getCanonicalDecl(ME->getMemberDecl()); 3846 return getCanonicalDecl(VD); 3847 } 3848 } 3849 if (auto *ME = dyn_cast_or_null<MemberExpr>(E)) 3850 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 3851 return getCanonicalDecl(ME->getMemberDecl()); 3852 return nullptr; 3853 } 3854 3855 bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) { 3856 // Check test-expr for canonical form, save upper-bound UB, flags for 3857 // less/greater and for strict/non-strict comparison. 3858 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 3859 // var relational-op b 3860 // b relational-op var 3861 // 3862 if (!S) { 3863 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl; 3864 return true; 3865 } 3866 S = getExprAsWritten(S); 3867 SourceLocation CondLoc = S->getLocStart(); 3868 if (auto BO = dyn_cast<BinaryOperator>(S)) { 3869 if (BO->isRelationalOp()) { 3870 if (GetInitLCDecl(BO->getLHS()) == LCDecl) 3871 return SetUB(BO->getRHS(), 3872 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 3873 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 3874 BO->getSourceRange(), BO->getOperatorLoc()); 3875 if (GetInitLCDecl(BO->getRHS()) == LCDecl) 3876 return SetUB(BO->getLHS(), 3877 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 3878 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 3879 BO->getSourceRange(), BO->getOperatorLoc()); 3880 } 3881 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) { 3882 if (CE->getNumArgs() == 2) { 3883 auto Op = CE->getOperator(); 3884 switch (Op) { 3885 case OO_Greater: 3886 case OO_GreaterEqual: 3887 case OO_Less: 3888 case OO_LessEqual: 3889 if (GetInitLCDecl(CE->getArg(0)) == LCDecl) 3890 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 3891 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 3892 CE->getOperatorLoc()); 3893 if (GetInitLCDecl(CE->getArg(1)) == LCDecl) 3894 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 3895 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 3896 CE->getOperatorLoc()); 3897 break; 3898 default: 3899 break; 3900 } 3901 } 3902 } 3903 if (Dependent() || SemaRef.CurContext->isDependentContext()) 3904 return false; 3905 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 3906 << S->getSourceRange() << LCDecl; 3907 return true; 3908 } 3909 3910 bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) { 3911 // RHS of canonical loop form increment can be: 3912 // var + incr 3913 // incr + var 3914 // var - incr 3915 // 3916 RHS = RHS->IgnoreParenImpCasts(); 3917 if (auto BO = dyn_cast<BinaryOperator>(RHS)) { 3918 if (BO->isAdditiveOp()) { 3919 bool IsAdd = BO->getOpcode() == BO_Add; 3920 if (GetInitLCDecl(BO->getLHS()) == LCDecl) 3921 return SetStep(BO->getRHS(), !IsAdd); 3922 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl) 3923 return SetStep(BO->getLHS(), false); 3924 } 3925 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 3926 bool IsAdd = CE->getOperator() == OO_Plus; 3927 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 3928 if (GetInitLCDecl(CE->getArg(0)) == LCDecl) 3929 return SetStep(CE->getArg(1), !IsAdd); 3930 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl) 3931 return SetStep(CE->getArg(0), false); 3932 } 3933 } 3934 if (Dependent() || SemaRef.CurContext->isDependentContext()) 3935 return false; 3936 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr) 3937 << RHS->getSourceRange() << LCDecl; 3938 return true; 3939 } 3940 3941 bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) { 3942 // Check incr-expr for canonical loop form and return true if it 3943 // does not conform. 3944 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 3945 // ++var 3946 // var++ 3947 // --var 3948 // var-- 3949 // var += incr 3950 // var -= incr 3951 // var = var + incr 3952 // var = incr + var 3953 // var = var - incr 3954 // 3955 if (!S) { 3956 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 3957 return true; 3958 } 3959 IncrementSrcRange = S->getSourceRange(); 3960 S = S->IgnoreParens(); 3961 if (auto UO = dyn_cast<UnaryOperator>(S)) { 3962 if (UO->isIncrementDecrementOp() && 3963 GetInitLCDecl(UO->getSubExpr()) == LCDecl) 3964 return SetStep( 3965 SemaRef.ActOnIntegerConstant(UO->getLocStart(), 3966 (UO->isDecrementOp() ? -1 : 1)).get(), 3967 false); 3968 } else if (auto BO = dyn_cast<BinaryOperator>(S)) { 3969 switch (BO->getOpcode()) { 3970 case BO_AddAssign: 3971 case BO_SubAssign: 3972 if (GetInitLCDecl(BO->getLHS()) == LCDecl) 3973 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 3974 break; 3975 case BO_Assign: 3976 if (GetInitLCDecl(BO->getLHS()) == LCDecl) 3977 return CheckIncRHS(BO->getRHS()); 3978 break; 3979 default: 3980 break; 3981 } 3982 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) { 3983 switch (CE->getOperator()) { 3984 case OO_PlusPlus: 3985 case OO_MinusMinus: 3986 if (GetInitLCDecl(CE->getArg(0)) == LCDecl) 3987 return SetStep( 3988 SemaRef.ActOnIntegerConstant( 3989 CE->getLocStart(), 3990 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(), 3991 false); 3992 break; 3993 case OO_PlusEqual: 3994 case OO_MinusEqual: 3995 if (GetInitLCDecl(CE->getArg(0)) == LCDecl) 3996 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 3997 break; 3998 case OO_Equal: 3999 if (GetInitLCDecl(CE->getArg(0)) == LCDecl) 4000 return CheckIncRHS(CE->getArg(1)); 4001 break; 4002 default: 4003 break; 4004 } 4005 } 4006 if (Dependent() || SemaRef.CurContext->isDependentContext()) 4007 return false; 4008 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr) 4009 << S->getSourceRange() << LCDecl; 4010 return true; 4011 } 4012 4013 static ExprResult 4014 tryBuildCapture(Sema &SemaRef, Expr *Capture, 4015 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { 4016 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 4017 return SemaRef.PerformImplicitConversion( 4018 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 4019 /*AllowExplicit=*/true); 4020 auto I = Captures.find(Capture); 4021 if (I != Captures.end()) 4022 return buildCapture(SemaRef, Capture, I->second); 4023 DeclRefExpr *Ref = nullptr; 4024 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 4025 Captures[Capture] = Ref; 4026 return Res; 4027 } 4028 4029 /// \brief Build the expression to calculate the number of iterations. 4030 Expr *OpenMPIterationSpaceChecker::BuildNumIterations( 4031 Scope *S, const bool LimitedType, 4032 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const { 4033 ExprResult Diff; 4034 auto VarType = LCDecl->getType().getNonReferenceType(); 4035 if (VarType->isIntegerType() || VarType->isPointerType() || 4036 SemaRef.getLangOpts().CPlusPlus) { 4037 // Upper - Lower 4038 auto *UBExpr = TestIsLessOp ? UB : LB; 4039 auto *LBExpr = TestIsLessOp ? LB : UB; 4040 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 4041 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 4042 if (!Upper || !Lower) 4043 return nullptr; 4044 4045 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 4046 4047 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 4048 // BuildBinOp already emitted error, this one is to point user to upper 4049 // and lower bound, and to tell what is passed to 'operator-'. 4050 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx) 4051 << Upper->getSourceRange() << Lower->getSourceRange(); 4052 return nullptr; 4053 } 4054 } 4055 4056 if (!Diff.isUsable()) 4057 return nullptr; 4058 4059 // Upper - Lower [- 1] 4060 if (TestIsStrictOp) 4061 Diff = SemaRef.BuildBinOp( 4062 S, DefaultLoc, BO_Sub, Diff.get(), 4063 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 4064 if (!Diff.isUsable()) 4065 return nullptr; 4066 4067 // Upper - Lower [- 1] + Step 4068 auto NewStep = tryBuildCapture(SemaRef, Step, Captures); 4069 if (!NewStep.isUsable()) 4070 return nullptr; 4071 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 4072 if (!Diff.isUsable()) 4073 return nullptr; 4074 4075 // Parentheses (for dumping/debugging purposes only). 4076 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 4077 if (!Diff.isUsable()) 4078 return nullptr; 4079 4080 // (Upper - Lower [- 1] + Step) / Step 4081 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 4082 if (!Diff.isUsable()) 4083 return nullptr; 4084 4085 // OpenMP runtime requires 32-bit or 64-bit loop variables. 4086 QualType Type = Diff.get()->getType(); 4087 auto &C = SemaRef.Context; 4088 bool UseVarType = VarType->hasIntegerRepresentation() && 4089 C.getTypeSize(Type) > C.getTypeSize(VarType); 4090 if (!Type->isIntegerType() || UseVarType) { 4091 unsigned NewSize = 4092 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 4093 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 4094 : Type->hasSignedIntegerRepresentation(); 4095 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 4096 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 4097 Diff = SemaRef.PerformImplicitConversion( 4098 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 4099 if (!Diff.isUsable()) 4100 return nullptr; 4101 } 4102 } 4103 if (LimitedType) { 4104 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 4105 if (NewSize != C.getTypeSize(Type)) { 4106 if (NewSize < C.getTypeSize(Type)) { 4107 assert(NewSize == 64 && "incorrect loop var size"); 4108 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 4109 << InitSrcRange << ConditionSrcRange; 4110 } 4111 QualType NewType = C.getIntTypeForBitwidth( 4112 NewSize, Type->hasSignedIntegerRepresentation() || 4113 C.getTypeSize(Type) < NewSize); 4114 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 4115 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 4116 Sema::AA_Converting, true); 4117 if (!Diff.isUsable()) 4118 return nullptr; 4119 } 4120 } 4121 } 4122 4123 return Diff.get(); 4124 } 4125 4126 Expr *OpenMPIterationSpaceChecker::BuildPreCond( 4127 Scope *S, Expr *Cond, 4128 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const { 4129 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 4130 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 4131 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 4132 4133 auto NewLB = tryBuildCapture(SemaRef, LB, Captures); 4134 auto NewUB = tryBuildCapture(SemaRef, UB, Captures); 4135 if (!NewLB.isUsable() || !NewUB.isUsable()) 4136 return nullptr; 4137 4138 auto CondExpr = SemaRef.BuildBinOp( 4139 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE) 4140 : (TestIsStrictOp ? BO_GT : BO_GE), 4141 NewLB.get(), NewUB.get()); 4142 if (CondExpr.isUsable()) { 4143 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 4144 SemaRef.Context.BoolTy)) 4145 CondExpr = SemaRef.PerformImplicitConversion( 4146 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 4147 /*AllowExplicit=*/true); 4148 } 4149 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 4150 // Otherwise use original loop conditon and evaluate it in runtime. 4151 return CondExpr.isUsable() ? CondExpr.get() : Cond; 4152 } 4153 4154 /// \brief Build reference expression to the counter be used for codegen. 4155 DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar( 4156 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const { 4157 auto *VD = dyn_cast<VarDecl>(LCDecl); 4158 if (!VD) { 4159 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl); 4160 auto *Ref = buildDeclRefExpr( 4161 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 4162 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false); 4163 // If the loop control decl is explicitly marked as private, do not mark it 4164 // as captured again. 4165 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 4166 Captures.insert(std::make_pair(LCRef, Ref)); 4167 return Ref; 4168 } 4169 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(), 4170 DefaultLoc); 4171 } 4172 4173 Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const { 4174 if (LCDecl && !LCDecl->isInvalidDecl()) { 4175 auto Type = LCDecl->getType().getNonReferenceType(); 4176 auto *PrivateVar = 4177 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(), 4178 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr); 4179 if (PrivateVar->isInvalidDecl()) 4180 return nullptr; 4181 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 4182 } 4183 return nullptr; 4184 } 4185 4186 /// \brief Build initization of the counter be used for codegen. 4187 Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; } 4188 4189 /// \brief Build step of the counter be used for codegen. 4190 Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; } 4191 4192 /// \brief Iteration space of a single for loop. 4193 struct LoopIterationSpace { 4194 /// \brief Condition of the loop. 4195 Expr *PreCond; 4196 /// \brief This expression calculates the number of iterations in the loop. 4197 /// It is always possible to calculate it before starting the loop. 4198 Expr *NumIterations; 4199 /// \brief The loop counter variable. 4200 Expr *CounterVar; 4201 /// \brief Private loop counter variable. 4202 Expr *PrivateCounterVar; 4203 /// \brief This is initializer for the initial value of #CounterVar. 4204 Expr *CounterInit; 4205 /// \brief This is step for the #CounterVar used to generate its update: 4206 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 4207 Expr *CounterStep; 4208 /// \brief Should step be subtracted? 4209 bool Subtract; 4210 /// \brief Source range of the loop init. 4211 SourceRange InitSrcRange; 4212 /// \brief Source range of the loop condition. 4213 SourceRange CondSrcRange; 4214 /// \brief Source range of the loop increment. 4215 SourceRange IncSrcRange; 4216 }; 4217 4218 } // namespace 4219 4220 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 4221 assert(getLangOpts().OpenMP && "OpenMP is not active."); 4222 assert(Init && "Expected loop in canonical form."); 4223 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 4224 if (AssociatedLoops > 0 && 4225 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 4226 OpenMPIterationSpaceChecker ISC(*this, ForLoc); 4227 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) { 4228 if (auto *D = ISC.GetLoopDecl()) { 4229 auto *VD = dyn_cast<VarDecl>(D); 4230 if (!VD) { 4231 if (auto *Private = IsOpenMPCapturedDecl(D)) 4232 VD = Private; 4233 else { 4234 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(), 4235 /*WithInit=*/false); 4236 VD = cast<VarDecl>(Ref->getDecl()); 4237 } 4238 } 4239 DSAStack->addLoopControlVariable(D, VD); 4240 } 4241 } 4242 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 4243 } 4244 } 4245 4246 /// \brief Called on a for stmt to check and extract its iteration space 4247 /// for further processing (such as collapsing). 4248 static bool CheckOpenMPIterationSpace( 4249 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 4250 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 4251 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr, 4252 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA, 4253 LoopIterationSpace &ResultIterSpace, 4254 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { 4255 // OpenMP [2.6, Canonical Loop Form] 4256 // for (init-expr; test-expr; incr-expr) structured-block 4257 auto For = dyn_cast_or_null<ForStmt>(S); 4258 if (!For) { 4259 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for) 4260 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 4261 << getOpenMPDirectiveName(DKind) << NestedLoopCount 4262 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 4263 if (NestedLoopCount > 1) { 4264 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 4265 SemaRef.Diag(DSA.getConstructLoc(), 4266 diag::note_omp_collapse_ordered_expr) 4267 << 2 << CollapseLoopCountExpr->getSourceRange() 4268 << OrderedLoopCountExpr->getSourceRange(); 4269 else if (CollapseLoopCountExpr) 4270 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 4271 diag::note_omp_collapse_ordered_expr) 4272 << 0 << CollapseLoopCountExpr->getSourceRange(); 4273 else 4274 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 4275 diag::note_omp_collapse_ordered_expr) 4276 << 1 << OrderedLoopCountExpr->getSourceRange(); 4277 } 4278 return true; 4279 } 4280 assert(For->getBody()); 4281 4282 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc()); 4283 4284 // Check init. 4285 auto Init = For->getInit(); 4286 if (ISC.CheckInit(Init)) 4287 return true; 4288 4289 bool HasErrors = false; 4290 4291 // Check loop variable's type. 4292 if (auto *LCDecl = ISC.GetLoopDecl()) { 4293 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr(); 4294 4295 // OpenMP [2.6, Canonical Loop Form] 4296 // Var is one of the following: 4297 // A variable of signed or unsigned integer type. 4298 // For C++, a variable of a random access iterator type. 4299 // For C, a variable of a pointer type. 4300 auto VarType = LCDecl->getType().getNonReferenceType(); 4301 if (!VarType->isDependentType() && !VarType->isIntegerType() && 4302 !VarType->isPointerType() && 4303 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 4304 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type) 4305 << SemaRef.getLangOpts().CPlusPlus; 4306 HasErrors = true; 4307 } 4308 4309 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 4310 // a Construct 4311 // The loop iteration variable(s) in the associated for-loop(s) of a for or 4312 // parallel for construct is (are) private. 4313 // The loop iteration variable in the associated for-loop of a simd 4314 // construct with just one associated for-loop is linear with a 4315 // constant-linear-step that is the increment of the associated for-loop. 4316 // Exclude loop var from the list of variables with implicitly defined data 4317 // sharing attributes. 4318 VarsWithImplicitDSA.erase(LCDecl); 4319 4320 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 4321 // in a Construct, C/C++]. 4322 // The loop iteration variable in the associated for-loop of a simd 4323 // construct with just one associated for-loop may be listed in a linear 4324 // clause with a constant-linear-step that is the increment of the 4325 // associated for-loop. 4326 // The loop iteration variable(s) in the associated for-loop(s) of a for or 4327 // parallel for construct may be listed in a private or lastprivate clause. 4328 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false); 4329 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is 4330 // declared in the loop and it is predetermined as a private. 4331 auto PredeterminedCKind = 4332 isOpenMPSimdDirective(DKind) 4333 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate) 4334 : OMPC_private; 4335 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 4336 DVar.CKind != PredeterminedCKind) || 4337 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 4338 isOpenMPDistributeDirective(DKind)) && 4339 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 4340 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 4341 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 4342 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa) 4343 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind) 4344 << getOpenMPClauseName(PredeterminedCKind); 4345 if (DVar.RefExpr == nullptr) 4346 DVar.CKind = PredeterminedCKind; 4347 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true); 4348 HasErrors = true; 4349 } else if (LoopDeclRefExpr != nullptr) { 4350 // Make the loop iteration variable private (for worksharing constructs), 4351 // linear (for simd directives with the only one associated loop) or 4352 // lastprivate (for simd directives with several collapsed or ordered 4353 // loops). 4354 if (DVar.CKind == OMPC_unknown) 4355 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate, 4356 [](OpenMPDirectiveKind) -> bool { return true; }, 4357 /*FromParent=*/false); 4358 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind); 4359 } 4360 4361 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 4362 4363 // Check test-expr. 4364 HasErrors |= ISC.CheckCond(For->getCond()); 4365 4366 // Check incr-expr. 4367 HasErrors |= ISC.CheckInc(For->getInc()); 4368 } 4369 4370 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 4371 return HasErrors; 4372 4373 // Build the loop's iteration space representation. 4374 ResultIterSpace.PreCond = 4375 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures); 4376 ResultIterSpace.NumIterations = ISC.BuildNumIterations( 4377 DSA.getCurScope(), 4378 (isOpenMPWorksharingDirective(DKind) || 4379 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)), 4380 Captures); 4381 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA); 4382 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar(); 4383 ResultIterSpace.CounterInit = ISC.BuildCounterInit(); 4384 ResultIterSpace.CounterStep = ISC.BuildCounterStep(); 4385 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange(); 4386 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange(); 4387 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange(); 4388 ResultIterSpace.Subtract = ISC.ShouldSubtractStep(); 4389 4390 HasErrors |= (ResultIterSpace.PreCond == nullptr || 4391 ResultIterSpace.NumIterations == nullptr || 4392 ResultIterSpace.CounterVar == nullptr || 4393 ResultIterSpace.PrivateCounterVar == nullptr || 4394 ResultIterSpace.CounterInit == nullptr || 4395 ResultIterSpace.CounterStep == nullptr); 4396 4397 return HasErrors; 4398 } 4399 4400 /// \brief Build 'VarRef = Start. 4401 static ExprResult 4402 BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 4403 ExprResult Start, 4404 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { 4405 // Build 'VarRef = Start. 4406 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures); 4407 if (!NewStart.isUsable()) 4408 return ExprError(); 4409 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 4410 VarRef.get()->getType())) { 4411 NewStart = SemaRef.PerformImplicitConversion( 4412 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 4413 /*AllowExplicit=*/true); 4414 if (!NewStart.isUsable()) 4415 return ExprError(); 4416 } 4417 4418 auto Init = 4419 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 4420 return Init; 4421 } 4422 4423 /// \brief Build 'VarRef = Start + Iter * Step'. 4424 static ExprResult 4425 BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc, 4426 ExprResult VarRef, ExprResult Start, ExprResult Iter, 4427 ExprResult Step, bool Subtract, 4428 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) { 4429 // Add parentheses (for debugging purposes only). 4430 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 4431 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 4432 !Step.isUsable()) 4433 return ExprError(); 4434 4435 ExprResult NewStep = Step; 4436 if (Captures) 4437 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 4438 if (NewStep.isInvalid()) 4439 return ExprError(); 4440 ExprResult Update = 4441 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 4442 if (!Update.isUsable()) 4443 return ExprError(); 4444 4445 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 4446 // 'VarRef = Start (+|-) Iter * Step'. 4447 ExprResult NewStart = Start; 4448 if (Captures) 4449 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 4450 if (NewStart.isInvalid()) 4451 return ExprError(); 4452 4453 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 4454 ExprResult SavedUpdate = Update; 4455 ExprResult UpdateVal; 4456 if (VarRef.get()->getType()->isOverloadableType() || 4457 NewStart.get()->getType()->isOverloadableType() || 4458 Update.get()->getType()->isOverloadableType()) { 4459 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 4460 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 4461 Update = 4462 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 4463 if (Update.isUsable()) { 4464 UpdateVal = 4465 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 4466 VarRef.get(), SavedUpdate.get()); 4467 if (UpdateVal.isUsable()) { 4468 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 4469 UpdateVal.get()); 4470 } 4471 } 4472 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 4473 } 4474 4475 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 4476 if (!Update.isUsable() || !UpdateVal.isUsable()) { 4477 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 4478 NewStart.get(), SavedUpdate.get()); 4479 if (!Update.isUsable()) 4480 return ExprError(); 4481 4482 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 4483 VarRef.get()->getType())) { 4484 Update = SemaRef.PerformImplicitConversion( 4485 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 4486 if (!Update.isUsable()) 4487 return ExprError(); 4488 } 4489 4490 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 4491 } 4492 return Update; 4493 } 4494 4495 /// \brief Convert integer expression \a E to make it have at least \a Bits 4496 /// bits. 4497 static ExprResult WidenIterationCount(unsigned Bits, Expr *E, 4498 Sema &SemaRef) { 4499 if (E == nullptr) 4500 return ExprError(); 4501 auto &C = SemaRef.Context; 4502 QualType OldType = E->getType(); 4503 unsigned HasBits = C.getTypeSize(OldType); 4504 if (HasBits >= Bits) 4505 return ExprResult(E); 4506 // OK to convert to signed, because new type has more bits than old. 4507 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 4508 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 4509 true); 4510 } 4511 4512 /// \brief Check if the given expression \a E is a constant integer that fits 4513 /// into \a Bits bits. 4514 static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) { 4515 if (E == nullptr) 4516 return false; 4517 llvm::APSInt Result; 4518 if (E->isIntegerConstantExpr(Result, SemaRef.Context)) 4519 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); 4520 return false; 4521 } 4522 4523 /// Build preinits statement for the given declarations. 4524 static Stmt *buildPreInits(ASTContext &Context, 4525 SmallVectorImpl<Decl *> &PreInits) { 4526 if (!PreInits.empty()) { 4527 return new (Context) DeclStmt( 4528 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 4529 SourceLocation(), SourceLocation()); 4530 } 4531 return nullptr; 4532 } 4533 4534 /// Build preinits statement for the given declarations. 4535 static Stmt *buildPreInits(ASTContext &Context, 4536 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { 4537 if (!Captures.empty()) { 4538 SmallVector<Decl *, 16> PreInits; 4539 for (auto &Pair : Captures) 4540 PreInits.push_back(Pair.second->getDecl()); 4541 return buildPreInits(Context, PreInits); 4542 } 4543 return nullptr; 4544 } 4545 4546 /// Build postupdate expression for the given list of postupdates expressions. 4547 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 4548 Expr *PostUpdate = nullptr; 4549 if (!PostUpdates.empty()) { 4550 for (auto *E : PostUpdates) { 4551 Expr *ConvE = S.BuildCStyleCastExpr( 4552 E->getExprLoc(), 4553 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 4554 E->getExprLoc(), E) 4555 .get(); 4556 PostUpdate = PostUpdate 4557 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 4558 PostUpdate, ConvE) 4559 .get() 4560 : ConvE; 4561 } 4562 } 4563 return PostUpdate; 4564 } 4565 4566 /// \brief Called on a for stmt to check itself and nested loops (if any). 4567 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 4568 /// number of collapsed loops otherwise. 4569 static unsigned 4570 CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 4571 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 4572 DSAStackTy &DSA, 4573 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA, 4574 OMPLoopDirective::HelperExprs &Built) { 4575 unsigned NestedLoopCount = 1; 4576 if (CollapseLoopCountExpr) { 4577 // Found 'collapse' clause - calculate collapse number. 4578 llvm::APSInt Result; 4579 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) 4580 NestedLoopCount = Result.getLimitedValue(); 4581 } 4582 if (OrderedLoopCountExpr) { 4583 // Found 'ordered' clause - calculate collapse number. 4584 llvm::APSInt Result; 4585 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 4586 if (Result.getLimitedValue() < NestedLoopCount) { 4587 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 4588 diag::err_omp_wrong_ordered_loop_count) 4589 << OrderedLoopCountExpr->getSourceRange(); 4590 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 4591 diag::note_collapse_loop_count) 4592 << CollapseLoopCountExpr->getSourceRange(); 4593 } 4594 NestedLoopCount = Result.getLimitedValue(); 4595 } 4596 } 4597 // This is helper routine for loop directives (e.g., 'for', 'simd', 4598 // 'for simd', etc.). 4599 llvm::MapVector<Expr *, DeclRefExpr *> Captures; 4600 SmallVector<LoopIterationSpace, 4> IterSpaces; 4601 IterSpaces.resize(NestedLoopCount); 4602 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); 4603 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 4604 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt, 4605 NestedLoopCount, CollapseLoopCountExpr, 4606 OrderedLoopCountExpr, VarsWithImplicitDSA, 4607 IterSpaces[Cnt], Captures)) 4608 return 0; 4609 // Move on to the next nested for loop, or to the loop body. 4610 // OpenMP [2.8.1, simd construct, Restrictions] 4611 // All loops associated with the construct must be perfectly nested; that 4612 // is, there must be no intervening code nor any OpenMP directive between 4613 // any two loops. 4614 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); 4615 } 4616 4617 Built.clear(/* size */ NestedLoopCount); 4618 4619 if (SemaRef.CurContext->isDependentContext()) 4620 return NestedLoopCount; 4621 4622 // An example of what is generated for the following code: 4623 // 4624 // #pragma omp simd collapse(2) ordered(2) 4625 // for (i = 0; i < NI; ++i) 4626 // for (k = 0; k < NK; ++k) 4627 // for (j = J0; j < NJ; j+=2) { 4628 // <loop body> 4629 // } 4630 // 4631 // We generate the code below. 4632 // Note: the loop body may be outlined in CodeGen. 4633 // Note: some counters may be C++ classes, operator- is used to find number of 4634 // iterations and operator+= to calculate counter value. 4635 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 4636 // or i64 is currently supported). 4637 // 4638 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 4639 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 4640 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 4641 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 4642 // // similar updates for vars in clauses (e.g. 'linear') 4643 // <loop body (using local i and j)> 4644 // } 4645 // i = NI; // assign final values of counters 4646 // j = NJ; 4647 // 4648 4649 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 4650 // the iteration counts of the collapsed for loops. 4651 // Precondition tests if there is at least one iteration (all conditions are 4652 // true). 4653 auto PreCond = ExprResult(IterSpaces[0].PreCond); 4654 auto N0 = IterSpaces[0].NumIterations; 4655 ExprResult LastIteration32 = WidenIterationCount( 4656 32 /* Bits */, SemaRef.PerformImplicitConversion( 4657 N0->IgnoreImpCasts(), N0->getType(), 4658 Sema::AA_Converting, /*AllowExplicit=*/true) 4659 .get(), 4660 SemaRef); 4661 ExprResult LastIteration64 = WidenIterationCount( 4662 64 /* Bits */, SemaRef.PerformImplicitConversion( 4663 N0->IgnoreImpCasts(), N0->getType(), 4664 Sema::AA_Converting, /*AllowExplicit=*/true) 4665 .get(), 4666 SemaRef); 4667 4668 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 4669 return NestedLoopCount; 4670 4671 auto &C = SemaRef.Context; 4672 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 4673 4674 Scope *CurScope = DSA.getCurScope(); 4675 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 4676 if (PreCond.isUsable()) { 4677 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd, 4678 PreCond.get(), IterSpaces[Cnt].PreCond); 4679 } 4680 auto N = IterSpaces[Cnt].NumIterations; 4681 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 4682 if (LastIteration32.isUsable()) 4683 LastIteration32 = SemaRef.BuildBinOp( 4684 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(), 4685 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 4686 Sema::AA_Converting, 4687 /*AllowExplicit=*/true) 4688 .get()); 4689 if (LastIteration64.isUsable()) 4690 LastIteration64 = SemaRef.BuildBinOp( 4691 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(), 4692 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 4693 Sema::AA_Converting, 4694 /*AllowExplicit=*/true) 4695 .get()); 4696 } 4697 4698 // Choose either the 32-bit or 64-bit version. 4699 ExprResult LastIteration = LastIteration64; 4700 if (LastIteration32.isUsable() && 4701 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 4702 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 4703 FitsInto( 4704 32 /* Bits */, 4705 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 4706 LastIteration64.get(), SemaRef))) 4707 LastIteration = LastIteration32; 4708 QualType VType = LastIteration.get()->getType(); 4709 QualType RealVType = VType; 4710 QualType StrideVType = VType; 4711 if (isOpenMPTaskLoopDirective(DKind)) { 4712 VType = 4713 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 4714 StrideVType = 4715 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 4716 } 4717 4718 if (!LastIteration.isUsable()) 4719 return 0; 4720 4721 // Save the number of iterations. 4722 ExprResult NumIterations = LastIteration; 4723 { 4724 LastIteration = SemaRef.BuildBinOp( 4725 CurScope, SourceLocation(), BO_Sub, LastIteration.get(), 4726 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 4727 if (!LastIteration.isUsable()) 4728 return 0; 4729 } 4730 4731 // Calculate the last iteration number beforehand instead of doing this on 4732 // each iteration. Do not do this if the number of iterations may be kfold-ed. 4733 llvm::APSInt Result; 4734 bool IsConstant = 4735 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); 4736 ExprResult CalcLastIteration; 4737 if (!IsConstant) { 4738 ExprResult SaveRef = 4739 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 4740 LastIteration = SaveRef; 4741 4742 // Prepare SaveRef + 1. 4743 NumIterations = SemaRef.BuildBinOp( 4744 CurScope, SourceLocation(), BO_Add, SaveRef.get(), 4745 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 4746 if (!NumIterations.isUsable()) 4747 return 0; 4748 } 4749 4750 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 4751 4752 // Build variables passed into runtime, nesessary for worksharing directives. 4753 ExprResult LB, UB, IL, ST, EUB; 4754 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 4755 isOpenMPDistributeDirective(DKind)) { 4756 // Lower bound variable, initialized with zero. 4757 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 4758 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 4759 SemaRef.AddInitializerToDecl( 4760 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 4761 /*DirectInit*/ false, /*TypeMayContainAuto*/ false); 4762 4763 // Upper bound variable, initialized with last iteration number. 4764 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 4765 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 4766 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 4767 /*DirectInit*/ false, 4768 /*TypeMayContainAuto*/ false); 4769 4770 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 4771 // This will be used to implement clause 'lastprivate'. 4772 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 4773 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 4774 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 4775 SemaRef.AddInitializerToDecl( 4776 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 4777 /*DirectInit*/ false, /*TypeMayContainAuto*/ false); 4778 4779 // Stride variable returned by runtime (we initialize it to 1 by default). 4780 VarDecl *STDecl = 4781 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 4782 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 4783 SemaRef.AddInitializerToDecl( 4784 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 4785 /*DirectInit*/ false, /*TypeMayContainAuto*/ false); 4786 4787 // Build expression: UB = min(UB, LastIteration) 4788 // It is nesessary for CodeGen of directives with static scheduling. 4789 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 4790 UB.get(), LastIteration.get()); 4791 ExprResult CondOp = SemaRef.ActOnConditionalOp( 4792 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get()); 4793 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 4794 CondOp.get()); 4795 EUB = SemaRef.ActOnFinishFullExpr(EUB.get()); 4796 } 4797 4798 // Build the iteration variable and its initialization before loop. 4799 ExprResult IV; 4800 ExprResult Init; 4801 { 4802 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 4803 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 4804 Expr *RHS = (isOpenMPWorksharingDirective(DKind) || 4805 isOpenMPTaskLoopDirective(DKind) || 4806 isOpenMPDistributeDirective(DKind)) 4807 ? LB.get() 4808 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 4809 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 4810 Init = SemaRef.ActOnFinishFullExpr(Init.get()); 4811 } 4812 4813 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops. 4814 SourceLocation CondLoc; 4815 ExprResult Cond = 4816 (isOpenMPWorksharingDirective(DKind) || 4817 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 4818 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get()) 4819 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 4820 NumIterations.get()); 4821 4822 // Loop increment (IV = IV + 1) 4823 SourceLocation IncLoc; 4824 ExprResult Inc = 4825 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 4826 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 4827 if (!Inc.isUsable()) 4828 return 0; 4829 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 4830 Inc = SemaRef.ActOnFinishFullExpr(Inc.get()); 4831 if (!Inc.isUsable()) 4832 return 0; 4833 4834 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 4835 // Used for directives with static scheduling. 4836 ExprResult NextLB, NextUB; 4837 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 4838 isOpenMPDistributeDirective(DKind)) { 4839 // LB + ST 4840 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 4841 if (!NextLB.isUsable()) 4842 return 0; 4843 // LB = LB + ST 4844 NextLB = 4845 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 4846 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get()); 4847 if (!NextLB.isUsable()) 4848 return 0; 4849 // UB + ST 4850 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 4851 if (!NextUB.isUsable()) 4852 return 0; 4853 // UB = UB + ST 4854 NextUB = 4855 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 4856 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get()); 4857 if (!NextUB.isUsable()) 4858 return 0; 4859 } 4860 4861 // Build updates and final values of the loop counters. 4862 bool HasErrors = false; 4863 Built.Counters.resize(NestedLoopCount); 4864 Built.Inits.resize(NestedLoopCount); 4865 Built.Updates.resize(NestedLoopCount); 4866 Built.Finals.resize(NestedLoopCount); 4867 { 4868 ExprResult Div; 4869 // Go from inner nested loop to outer. 4870 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) { 4871 LoopIterationSpace &IS = IterSpaces[Cnt]; 4872 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 4873 // Build: Iter = (IV / Div) % IS.NumIters 4874 // where Div is product of previous iterations' IS.NumIters. 4875 ExprResult Iter; 4876 if (Div.isUsable()) { 4877 Iter = 4878 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get()); 4879 } else { 4880 Iter = IV; 4881 assert((Cnt == (int)NestedLoopCount - 1) && 4882 "unusable div expected on first iteration only"); 4883 } 4884 4885 if (Cnt != 0 && Iter.isUsable()) 4886 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(), 4887 IS.NumIterations); 4888 if (!Iter.isUsable()) { 4889 HasErrors = true; 4890 break; 4891 } 4892 4893 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 4894 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 4895 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(), 4896 IS.CounterVar->getExprLoc(), 4897 /*RefersToCapture=*/true); 4898 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 4899 IS.CounterInit, Captures); 4900 if (!Init.isUsable()) { 4901 HasErrors = true; 4902 break; 4903 } 4904 ExprResult Update = BuildCounterUpdate( 4905 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 4906 IS.CounterStep, IS.Subtract, &Captures); 4907 if (!Update.isUsable()) { 4908 HasErrors = true; 4909 break; 4910 } 4911 4912 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 4913 ExprResult Final = BuildCounterUpdate( 4914 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, 4915 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures); 4916 if (!Final.isUsable()) { 4917 HasErrors = true; 4918 break; 4919 } 4920 4921 // Build Div for the next iteration: Div <- Div * IS.NumIters 4922 if (Cnt != 0) { 4923 if (Div.isUnset()) 4924 Div = IS.NumIterations; 4925 else 4926 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(), 4927 IS.NumIterations); 4928 4929 // Add parentheses (for debugging purposes only). 4930 if (Div.isUsable()) 4931 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get()); 4932 if (!Div.isUsable()) { 4933 HasErrors = true; 4934 break; 4935 } 4936 } 4937 if (!Update.isUsable() || !Final.isUsable()) { 4938 HasErrors = true; 4939 break; 4940 } 4941 // Save results 4942 Built.Counters[Cnt] = IS.CounterVar; 4943 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 4944 Built.Inits[Cnt] = Init.get(); 4945 Built.Updates[Cnt] = Update.get(); 4946 Built.Finals[Cnt] = Final.get(); 4947 } 4948 } 4949 4950 if (HasErrors) 4951 return 0; 4952 4953 // Save results 4954 Built.IterationVarRef = IV.get(); 4955 Built.LastIteration = LastIteration.get(); 4956 Built.NumIterations = NumIterations.get(); 4957 Built.CalcLastIteration = 4958 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get(); 4959 Built.PreCond = PreCond.get(); 4960 Built.PreInits = buildPreInits(C, Captures); 4961 Built.Cond = Cond.get(); 4962 Built.Init = Init.get(); 4963 Built.Inc = Inc.get(); 4964 Built.LB = LB.get(); 4965 Built.UB = UB.get(); 4966 Built.IL = IL.get(); 4967 Built.ST = ST.get(); 4968 Built.EUB = EUB.get(); 4969 Built.NLB = NextLB.get(); 4970 Built.NUB = NextUB.get(); 4971 4972 return NestedLoopCount; 4973 } 4974 4975 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 4976 auto CollapseClauses = 4977 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 4978 if (CollapseClauses.begin() != CollapseClauses.end()) 4979 return (*CollapseClauses.begin())->getNumForLoops(); 4980 return nullptr; 4981 } 4982 4983 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 4984 auto OrderedClauses = 4985 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 4986 if (OrderedClauses.begin() != OrderedClauses.end()) 4987 return (*OrderedClauses.begin())->getNumForLoops(); 4988 return nullptr; 4989 } 4990 4991 static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen, 4992 const Expr *Safelen) { 4993 llvm::APSInt SimdlenRes, SafelenRes; 4994 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() || 4995 Simdlen->isInstantiationDependent() || 4996 Simdlen->containsUnexpandedParameterPack()) 4997 return false; 4998 if (Safelen->isValueDependent() || Safelen->isTypeDependent() || 4999 Safelen->isInstantiationDependent() || 5000 Safelen->containsUnexpandedParameterPack()) 5001 return false; 5002 Simdlen->EvaluateAsInt(SimdlenRes, S.Context); 5003 Safelen->EvaluateAsInt(SafelenRes, S.Context); 5004 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions] 5005 // If both simdlen and safelen clauses are specified, the value of the simdlen 5006 // parameter must be less than or equal to the value of the safelen parameter. 5007 if (SimdlenRes > SafelenRes) { 5008 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values) 5009 << Simdlen->getSourceRange() << Safelen->getSourceRange(); 5010 return true; 5011 } 5012 return false; 5013 } 5014 5015 StmtResult Sema::ActOnOpenMPSimdDirective( 5016 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5017 SourceLocation EndLoc, 5018 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 5019 if (!AStmt) 5020 return StmtError(); 5021 5022 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5023 OMPLoopDirective::HelperExprs B; 5024 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5025 // define the nested loops number. 5026 unsigned NestedLoopCount = CheckOpenMPLoop( 5027 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 5028 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 5029 if (NestedLoopCount == 0) 5030 return StmtError(); 5031 5032 assert((CurContext->isDependentContext() || B.builtAll()) && 5033 "omp simd loop exprs were not built"); 5034 5035 if (!CurContext->isDependentContext()) { 5036 // Finalize the clauses that need pre-built expressions for CodeGen. 5037 for (auto C : Clauses) { 5038 if (auto LC = dyn_cast<OMPLinearClause>(C)) 5039 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5040 B.NumIterations, *this, CurScope, 5041 DSAStack)) 5042 return StmtError(); 5043 } 5044 } 5045 5046 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions] 5047 // If both simdlen and safelen clauses are specified, the value of the simdlen 5048 // parameter must be less than or equal to the value of the safelen parameter. 5049 OMPSafelenClause *Safelen = nullptr; 5050 OMPSimdlenClause *Simdlen = nullptr; 5051 for (auto *Clause : Clauses) { 5052 if (Clause->getClauseKind() == OMPC_safelen) 5053 Safelen = cast<OMPSafelenClause>(Clause); 5054 else if (Clause->getClauseKind() == OMPC_simdlen) 5055 Simdlen = cast<OMPSimdlenClause>(Clause); 5056 if (Safelen && Simdlen) 5057 break; 5058 } 5059 if (Simdlen && Safelen && 5060 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(), 5061 Safelen->getSafelen())) 5062 return StmtError(); 5063 5064 getCurFunction()->setHasBranchProtectedScope(); 5065 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 5066 Clauses, AStmt, B); 5067 } 5068 5069 StmtResult Sema::ActOnOpenMPForDirective( 5070 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5071 SourceLocation EndLoc, 5072 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 5073 if (!AStmt) 5074 return StmtError(); 5075 5076 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5077 OMPLoopDirective::HelperExprs B; 5078 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5079 // define the nested loops number. 5080 unsigned NestedLoopCount = CheckOpenMPLoop( 5081 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 5082 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 5083 if (NestedLoopCount == 0) 5084 return StmtError(); 5085 5086 assert((CurContext->isDependentContext() || B.builtAll()) && 5087 "omp for loop exprs were not built"); 5088 5089 if (!CurContext->isDependentContext()) { 5090 // Finalize the clauses that need pre-built expressions for CodeGen. 5091 for (auto C : Clauses) { 5092 if (auto LC = dyn_cast<OMPLinearClause>(C)) 5093 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5094 B.NumIterations, *this, CurScope, 5095 DSAStack)) 5096 return StmtError(); 5097 } 5098 } 5099 5100 getCurFunction()->setHasBranchProtectedScope(); 5101 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 5102 Clauses, AStmt, B, DSAStack->isCancelRegion()); 5103 } 5104 5105 StmtResult Sema::ActOnOpenMPForSimdDirective( 5106 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5107 SourceLocation EndLoc, 5108 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 5109 if (!AStmt) 5110 return StmtError(); 5111 5112 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5113 OMPLoopDirective::HelperExprs B; 5114 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5115 // define the nested loops number. 5116 unsigned NestedLoopCount = 5117 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 5118 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 5119 VarsWithImplicitDSA, B); 5120 if (NestedLoopCount == 0) 5121 return StmtError(); 5122 5123 assert((CurContext->isDependentContext() || B.builtAll()) && 5124 "omp for simd loop exprs were not built"); 5125 5126 if (!CurContext->isDependentContext()) { 5127 // Finalize the clauses that need pre-built expressions for CodeGen. 5128 for (auto C : Clauses) { 5129 if (auto LC = dyn_cast<OMPLinearClause>(C)) 5130 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5131 B.NumIterations, *this, CurScope, 5132 DSAStack)) 5133 return StmtError(); 5134 } 5135 } 5136 5137 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions] 5138 // If both simdlen and safelen clauses are specified, the value of the simdlen 5139 // parameter must be less than or equal to the value of the safelen parameter. 5140 OMPSafelenClause *Safelen = nullptr; 5141 OMPSimdlenClause *Simdlen = nullptr; 5142 for (auto *Clause : Clauses) { 5143 if (Clause->getClauseKind() == OMPC_safelen) 5144 Safelen = cast<OMPSafelenClause>(Clause); 5145 else if (Clause->getClauseKind() == OMPC_simdlen) 5146 Simdlen = cast<OMPSimdlenClause>(Clause); 5147 if (Safelen && Simdlen) 5148 break; 5149 } 5150 if (Simdlen && Safelen && 5151 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(), 5152 Safelen->getSafelen())) 5153 return StmtError(); 5154 5155 getCurFunction()->setHasBranchProtectedScope(); 5156 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 5157 Clauses, AStmt, B); 5158 } 5159 5160 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 5161 Stmt *AStmt, 5162 SourceLocation StartLoc, 5163 SourceLocation EndLoc) { 5164 if (!AStmt) 5165 return StmtError(); 5166 5167 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5168 auto BaseStmt = AStmt; 5169 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 5170 BaseStmt = CS->getCapturedStmt(); 5171 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 5172 auto S = C->children(); 5173 if (S.begin() == S.end()) 5174 return StmtError(); 5175 // All associated statements must be '#pragma omp section' except for 5176 // the first one. 5177 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 5178 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 5179 if (SectionStmt) 5180 Diag(SectionStmt->getLocStart(), 5181 diag::err_omp_sections_substmt_not_section); 5182 return StmtError(); 5183 } 5184 cast<OMPSectionDirective>(SectionStmt) 5185 ->setHasCancel(DSAStack->isCancelRegion()); 5186 } 5187 } else { 5188 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt); 5189 return StmtError(); 5190 } 5191 5192 getCurFunction()->setHasBranchProtectedScope(); 5193 5194 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 5195 DSAStack->isCancelRegion()); 5196 } 5197 5198 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 5199 SourceLocation StartLoc, 5200 SourceLocation EndLoc) { 5201 if (!AStmt) 5202 return StmtError(); 5203 5204 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5205 5206 getCurFunction()->setHasBranchProtectedScope(); 5207 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 5208 5209 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 5210 DSAStack->isCancelRegion()); 5211 } 5212 5213 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 5214 Stmt *AStmt, 5215 SourceLocation StartLoc, 5216 SourceLocation EndLoc) { 5217 if (!AStmt) 5218 return StmtError(); 5219 5220 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5221 5222 getCurFunction()->setHasBranchProtectedScope(); 5223 5224 // OpenMP [2.7.3, single Construct, Restrictions] 5225 // The copyprivate clause must not be used with the nowait clause. 5226 OMPClause *Nowait = nullptr; 5227 OMPClause *Copyprivate = nullptr; 5228 for (auto *Clause : Clauses) { 5229 if (Clause->getClauseKind() == OMPC_nowait) 5230 Nowait = Clause; 5231 else if (Clause->getClauseKind() == OMPC_copyprivate) 5232 Copyprivate = Clause; 5233 if (Copyprivate && Nowait) { 5234 Diag(Copyprivate->getLocStart(), 5235 diag::err_omp_single_copyprivate_with_nowait); 5236 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here); 5237 return StmtError(); 5238 } 5239 } 5240 5241 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 5242 } 5243 5244 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 5245 SourceLocation StartLoc, 5246 SourceLocation EndLoc) { 5247 if (!AStmt) 5248 return StmtError(); 5249 5250 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5251 5252 getCurFunction()->setHasBranchProtectedScope(); 5253 5254 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 5255 } 5256 5257 StmtResult Sema::ActOnOpenMPCriticalDirective( 5258 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 5259 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 5260 if (!AStmt) 5261 return StmtError(); 5262 5263 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5264 5265 bool ErrorFound = false; 5266 llvm::APSInt Hint; 5267 SourceLocation HintLoc; 5268 bool DependentHint = false; 5269 for (auto *C : Clauses) { 5270 if (C->getClauseKind() == OMPC_hint) { 5271 if (!DirName.getName()) { 5272 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name); 5273 ErrorFound = true; 5274 } 5275 Expr *E = cast<OMPHintClause>(C)->getHint(); 5276 if (E->isTypeDependent() || E->isValueDependent() || 5277 E->isInstantiationDependent()) 5278 DependentHint = true; 5279 else { 5280 Hint = E->EvaluateKnownConstInt(Context); 5281 HintLoc = C->getLocStart(); 5282 } 5283 } 5284 } 5285 if (ErrorFound) 5286 return StmtError(); 5287 auto Pair = DSAStack->getCriticalWithHint(DirName); 5288 if (Pair.first && DirName.getName() && !DependentHint) { 5289 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 5290 Diag(StartLoc, diag::err_omp_critical_with_hint); 5291 if (HintLoc.isValid()) { 5292 Diag(HintLoc, diag::note_omp_critical_hint_here) 5293 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); 5294 } else 5295 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 5296 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 5297 Diag(C->getLocStart(), diag::note_omp_critical_hint_here) 5298 << 1 5299 << C->getHint()->EvaluateKnownConstInt(Context).toString( 5300 /*Radix=*/10, /*Signed=*/false); 5301 } else 5302 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1; 5303 } 5304 } 5305 5306 getCurFunction()->setHasBranchProtectedScope(); 5307 5308 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 5309 Clauses, AStmt); 5310 if (!Pair.first && DirName.getName() && !DependentHint) 5311 DSAStack->addCriticalWithHint(Dir, Hint); 5312 return Dir; 5313 } 5314 5315 StmtResult Sema::ActOnOpenMPParallelForDirective( 5316 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5317 SourceLocation EndLoc, 5318 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 5319 if (!AStmt) 5320 return StmtError(); 5321 5322 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 5323 // 1.2.2 OpenMP Language Terminology 5324 // Structured block - An executable statement with a single entry at the 5325 // top and a single exit at the bottom. 5326 // The point of exit cannot be a branch out of the structured block. 5327 // longjmp() and throw() must not violate the entry/exit criteria. 5328 CS->getCapturedDecl()->setNothrow(); 5329 5330 OMPLoopDirective::HelperExprs B; 5331 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5332 // define the nested loops number. 5333 unsigned NestedLoopCount = 5334 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 5335 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 5336 VarsWithImplicitDSA, B); 5337 if (NestedLoopCount == 0) 5338 return StmtError(); 5339 5340 assert((CurContext->isDependentContext() || B.builtAll()) && 5341 "omp parallel for loop exprs were not built"); 5342 5343 if (!CurContext->isDependentContext()) { 5344 // Finalize the clauses that need pre-built expressions for CodeGen. 5345 for (auto C : Clauses) { 5346 if (auto LC = dyn_cast<OMPLinearClause>(C)) 5347 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5348 B.NumIterations, *this, CurScope, 5349 DSAStack)) 5350 return StmtError(); 5351 } 5352 } 5353 5354 getCurFunction()->setHasBranchProtectedScope(); 5355 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc, 5356 NestedLoopCount, Clauses, AStmt, B, 5357 DSAStack->isCancelRegion()); 5358 } 5359 5360 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 5361 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5362 SourceLocation EndLoc, 5363 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 5364 if (!AStmt) 5365 return StmtError(); 5366 5367 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 5368 // 1.2.2 OpenMP Language Terminology 5369 // Structured block - An executable statement with a single entry at the 5370 // top and a single exit at the bottom. 5371 // The point of exit cannot be a branch out of the structured block. 5372 // longjmp() and throw() must not violate the entry/exit criteria. 5373 CS->getCapturedDecl()->setNothrow(); 5374 5375 OMPLoopDirective::HelperExprs B; 5376 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5377 // define the nested loops number. 5378 unsigned NestedLoopCount = 5379 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 5380 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 5381 VarsWithImplicitDSA, B); 5382 if (NestedLoopCount == 0) 5383 return StmtError(); 5384 5385 if (!CurContext->isDependentContext()) { 5386 // Finalize the clauses that need pre-built expressions for CodeGen. 5387 for (auto C : Clauses) { 5388 if (auto LC = dyn_cast<OMPLinearClause>(C)) 5389 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5390 B.NumIterations, *this, CurScope, 5391 DSAStack)) 5392 return StmtError(); 5393 } 5394 } 5395 5396 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions] 5397 // If both simdlen and safelen clauses are specified, the value of the simdlen 5398 // parameter must be less than or equal to the value of the safelen parameter. 5399 OMPSafelenClause *Safelen = nullptr; 5400 OMPSimdlenClause *Simdlen = nullptr; 5401 for (auto *Clause : Clauses) { 5402 if (Clause->getClauseKind() == OMPC_safelen) 5403 Safelen = cast<OMPSafelenClause>(Clause); 5404 else if (Clause->getClauseKind() == OMPC_simdlen) 5405 Simdlen = cast<OMPSimdlenClause>(Clause); 5406 if (Safelen && Simdlen) 5407 break; 5408 } 5409 if (Simdlen && Safelen && 5410 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(), 5411 Safelen->getSafelen())) 5412 return StmtError(); 5413 5414 getCurFunction()->setHasBranchProtectedScope(); 5415 return OMPParallelForSimdDirective::Create( 5416 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 5417 } 5418 5419 StmtResult 5420 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 5421 Stmt *AStmt, SourceLocation StartLoc, 5422 SourceLocation EndLoc) { 5423 if (!AStmt) 5424 return StmtError(); 5425 5426 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5427 auto BaseStmt = AStmt; 5428 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 5429 BaseStmt = CS->getCapturedStmt(); 5430 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 5431 auto S = C->children(); 5432 if (S.begin() == S.end()) 5433 return StmtError(); 5434 // All associated statements must be '#pragma omp section' except for 5435 // the first one. 5436 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 5437 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 5438 if (SectionStmt) 5439 Diag(SectionStmt->getLocStart(), 5440 diag::err_omp_parallel_sections_substmt_not_section); 5441 return StmtError(); 5442 } 5443 cast<OMPSectionDirective>(SectionStmt) 5444 ->setHasCancel(DSAStack->isCancelRegion()); 5445 } 5446 } else { 5447 Diag(AStmt->getLocStart(), 5448 diag::err_omp_parallel_sections_not_compound_stmt); 5449 return StmtError(); 5450 } 5451 5452 getCurFunction()->setHasBranchProtectedScope(); 5453 5454 return OMPParallelSectionsDirective::Create( 5455 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion()); 5456 } 5457 5458 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 5459 Stmt *AStmt, SourceLocation StartLoc, 5460 SourceLocation EndLoc) { 5461 if (!AStmt) 5462 return StmtError(); 5463 5464 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 5465 // 1.2.2 OpenMP Language Terminology 5466 // Structured block - An executable statement with a single entry at the 5467 // top and a single exit at the bottom. 5468 // The point of exit cannot be a branch out of the structured block. 5469 // longjmp() and throw() must not violate the entry/exit criteria. 5470 CS->getCapturedDecl()->setNothrow(); 5471 5472 getCurFunction()->setHasBranchProtectedScope(); 5473 5474 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 5475 DSAStack->isCancelRegion()); 5476 } 5477 5478 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 5479 SourceLocation EndLoc) { 5480 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 5481 } 5482 5483 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 5484 SourceLocation EndLoc) { 5485 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 5486 } 5487 5488 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 5489 SourceLocation EndLoc) { 5490 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 5491 } 5492 5493 StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt, 5494 SourceLocation StartLoc, 5495 SourceLocation EndLoc) { 5496 if (!AStmt) 5497 return StmtError(); 5498 5499 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5500 5501 getCurFunction()->setHasBranchProtectedScope(); 5502 5503 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt); 5504 } 5505 5506 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 5507 SourceLocation StartLoc, 5508 SourceLocation EndLoc) { 5509 assert(Clauses.size() <= 1 && "Extra clauses in flush directive"); 5510 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 5511 } 5512 5513 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 5514 Stmt *AStmt, 5515 SourceLocation StartLoc, 5516 SourceLocation EndLoc) { 5517 OMPClause *DependFound = nullptr; 5518 OMPClause *DependSourceClause = nullptr; 5519 OMPClause *DependSinkClause = nullptr; 5520 bool ErrorFound = false; 5521 OMPThreadsClause *TC = nullptr; 5522 OMPSIMDClause *SC = nullptr; 5523 for (auto *C : Clauses) { 5524 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 5525 DependFound = C; 5526 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 5527 if (DependSourceClause) { 5528 Diag(C->getLocStart(), diag::err_omp_more_one_clause) 5529 << getOpenMPDirectiveName(OMPD_ordered) 5530 << getOpenMPClauseName(OMPC_depend) << 2; 5531 ErrorFound = true; 5532 } else 5533 DependSourceClause = C; 5534 if (DependSinkClause) { 5535 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed) 5536 << 0; 5537 ErrorFound = true; 5538 } 5539 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 5540 if (DependSourceClause) { 5541 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed) 5542 << 1; 5543 ErrorFound = true; 5544 } 5545 DependSinkClause = C; 5546 } 5547 } else if (C->getClauseKind() == OMPC_threads) 5548 TC = cast<OMPThreadsClause>(C); 5549 else if (C->getClauseKind() == OMPC_simd) 5550 SC = cast<OMPSIMDClause>(C); 5551 } 5552 if (!ErrorFound && !SC && 5553 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 5554 // OpenMP [2.8.1,simd Construct, Restrictions] 5555 // An ordered construct with the simd clause is the only OpenMP construct 5556 // that can appear in the simd region. 5557 Diag(StartLoc, diag::err_omp_prohibited_region_simd); 5558 ErrorFound = true; 5559 } else if (DependFound && (TC || SC)) { 5560 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd) 5561 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 5562 ErrorFound = true; 5563 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) { 5564 Diag(DependFound->getLocStart(), 5565 diag::err_omp_ordered_directive_without_param); 5566 ErrorFound = true; 5567 } else if (TC || Clauses.empty()) { 5568 if (auto *Param = DSAStack->getParentOrderedRegionParam()) { 5569 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc; 5570 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 5571 << (TC != nullptr); 5572 Diag(Param->getLocStart(), diag::note_omp_ordered_param); 5573 ErrorFound = true; 5574 } 5575 } 5576 if ((!AStmt && !DependFound) || ErrorFound) 5577 return StmtError(); 5578 5579 if (AStmt) { 5580 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5581 5582 getCurFunction()->setHasBranchProtectedScope(); 5583 } 5584 5585 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 5586 } 5587 5588 namespace { 5589 /// \brief Helper class for checking expression in 'omp atomic [update]' 5590 /// construct. 5591 class OpenMPAtomicUpdateChecker { 5592 /// \brief Error results for atomic update expressions. 5593 enum ExprAnalysisErrorCode { 5594 /// \brief A statement is not an expression statement. 5595 NotAnExpression, 5596 /// \brief Expression is not builtin binary or unary operation. 5597 NotABinaryOrUnaryExpression, 5598 /// \brief Unary operation is not post-/pre- increment/decrement operation. 5599 NotAnUnaryIncDecExpression, 5600 /// \brief An expression is not of scalar type. 5601 NotAScalarType, 5602 /// \brief A binary operation is not an assignment operation. 5603 NotAnAssignmentOp, 5604 /// \brief RHS part of the binary operation is not a binary expression. 5605 NotABinaryExpression, 5606 /// \brief RHS part is not additive/multiplicative/shift/biwise binary 5607 /// expression. 5608 NotABinaryOperator, 5609 /// \brief RHS binary operation does not have reference to the updated LHS 5610 /// part. 5611 NotAnUpdateExpression, 5612 /// \brief No errors is found. 5613 NoError 5614 }; 5615 /// \brief Reference to Sema. 5616 Sema &SemaRef; 5617 /// \brief A location for note diagnostics (when error is found). 5618 SourceLocation NoteLoc; 5619 /// \brief 'x' lvalue part of the source atomic expression. 5620 Expr *X; 5621 /// \brief 'expr' rvalue part of the source atomic expression. 5622 Expr *E; 5623 /// \brief Helper expression of the form 5624 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 5625 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 5626 Expr *UpdateExpr; 5627 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is 5628 /// important for non-associative operations. 5629 bool IsXLHSInRHSPart; 5630 BinaryOperatorKind Op; 5631 SourceLocation OpLoc; 5632 /// \brief true if the source expression is a postfix unary operation, false 5633 /// if it is a prefix unary operation. 5634 bool IsPostfixUpdate; 5635 5636 public: 5637 OpenMPAtomicUpdateChecker(Sema &SemaRef) 5638 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 5639 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 5640 /// \brief Check specified statement that it is suitable for 'atomic update' 5641 /// constructs and extract 'x', 'expr' and Operation from the original 5642 /// expression. If DiagId and NoteId == 0, then only check is performed 5643 /// without error notification. 5644 /// \param DiagId Diagnostic which should be emitted if error is found. 5645 /// \param NoteId Diagnostic note for the main error message. 5646 /// \return true if statement is not an update expression, false otherwise. 5647 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 5648 /// \brief Return the 'x' lvalue part of the source atomic expression. 5649 Expr *getX() const { return X; } 5650 /// \brief Return the 'expr' rvalue part of the source atomic expression. 5651 Expr *getExpr() const { return E; } 5652 /// \brief Return the update expression used in calculation of the updated 5653 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 5654 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 5655 Expr *getUpdateExpr() const { return UpdateExpr; } 5656 /// \brief Return true if 'x' is LHS in RHS part of full update expression, 5657 /// false otherwise. 5658 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 5659 5660 /// \brief true if the source expression is a postfix unary operation, false 5661 /// if it is a prefix unary operation. 5662 bool isPostfixUpdate() const { return IsPostfixUpdate; } 5663 5664 private: 5665 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 5666 unsigned NoteId = 0); 5667 }; 5668 } // namespace 5669 5670 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 5671 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 5672 ExprAnalysisErrorCode ErrorFound = NoError; 5673 SourceLocation ErrorLoc, NoteLoc; 5674 SourceRange ErrorRange, NoteRange; 5675 // Allowed constructs are: 5676 // x = x binop expr; 5677 // x = expr binop x; 5678 if (AtomicBinOp->getOpcode() == BO_Assign) { 5679 X = AtomicBinOp->getLHS(); 5680 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 5681 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 5682 if (AtomicInnerBinOp->isMultiplicativeOp() || 5683 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 5684 AtomicInnerBinOp->isBitwiseOp()) { 5685 Op = AtomicInnerBinOp->getOpcode(); 5686 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 5687 auto *LHS = AtomicInnerBinOp->getLHS(); 5688 auto *RHS = AtomicInnerBinOp->getRHS(); 5689 llvm::FoldingSetNodeID XId, LHSId, RHSId; 5690 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 5691 /*Canonical=*/true); 5692 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 5693 /*Canonical=*/true); 5694 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 5695 /*Canonical=*/true); 5696 if (XId == LHSId) { 5697 E = RHS; 5698 IsXLHSInRHSPart = true; 5699 } else if (XId == RHSId) { 5700 E = LHS; 5701 IsXLHSInRHSPart = false; 5702 } else { 5703 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 5704 ErrorRange = AtomicInnerBinOp->getSourceRange(); 5705 NoteLoc = X->getExprLoc(); 5706 NoteRange = X->getSourceRange(); 5707 ErrorFound = NotAnUpdateExpression; 5708 } 5709 } else { 5710 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 5711 ErrorRange = AtomicInnerBinOp->getSourceRange(); 5712 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 5713 NoteRange = SourceRange(NoteLoc, NoteLoc); 5714 ErrorFound = NotABinaryOperator; 5715 } 5716 } else { 5717 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 5718 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 5719 ErrorFound = NotABinaryExpression; 5720 } 5721 } else { 5722 ErrorLoc = AtomicBinOp->getExprLoc(); 5723 ErrorRange = AtomicBinOp->getSourceRange(); 5724 NoteLoc = AtomicBinOp->getOperatorLoc(); 5725 NoteRange = SourceRange(NoteLoc, NoteLoc); 5726 ErrorFound = NotAnAssignmentOp; 5727 } 5728 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 5729 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 5730 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 5731 return true; 5732 } else if (SemaRef.CurContext->isDependentContext()) 5733 E = X = UpdateExpr = nullptr; 5734 return ErrorFound != NoError; 5735 } 5736 5737 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 5738 unsigned NoteId) { 5739 ExprAnalysisErrorCode ErrorFound = NoError; 5740 SourceLocation ErrorLoc, NoteLoc; 5741 SourceRange ErrorRange, NoteRange; 5742 // Allowed constructs are: 5743 // x++; 5744 // x--; 5745 // ++x; 5746 // --x; 5747 // x binop= expr; 5748 // x = x binop expr; 5749 // x = expr binop x; 5750 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 5751 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 5752 if (AtomicBody->getType()->isScalarType() || 5753 AtomicBody->isInstantiationDependent()) { 5754 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 5755 AtomicBody->IgnoreParenImpCasts())) { 5756 // Check for Compound Assignment Operation 5757 Op = BinaryOperator::getOpForCompoundAssignment( 5758 AtomicCompAssignOp->getOpcode()); 5759 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 5760 E = AtomicCompAssignOp->getRHS(); 5761 X = AtomicCompAssignOp->getLHS(); 5762 IsXLHSInRHSPart = true; 5763 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 5764 AtomicBody->IgnoreParenImpCasts())) { 5765 // Check for Binary Operation 5766 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 5767 return true; 5768 } else if (auto *AtomicUnaryOp = 5769 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) { 5770 // Check for Unary Operation 5771 if (AtomicUnaryOp->isIncrementDecrementOp()) { 5772 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 5773 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 5774 OpLoc = AtomicUnaryOp->getOperatorLoc(); 5775 X = AtomicUnaryOp->getSubExpr(); 5776 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 5777 IsXLHSInRHSPart = true; 5778 } else { 5779 ErrorFound = NotAnUnaryIncDecExpression; 5780 ErrorLoc = AtomicUnaryOp->getExprLoc(); 5781 ErrorRange = AtomicUnaryOp->getSourceRange(); 5782 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 5783 NoteRange = SourceRange(NoteLoc, NoteLoc); 5784 } 5785 } else if (!AtomicBody->isInstantiationDependent()) { 5786 ErrorFound = NotABinaryOrUnaryExpression; 5787 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 5788 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 5789 } 5790 } else { 5791 ErrorFound = NotAScalarType; 5792 NoteLoc = ErrorLoc = AtomicBody->getLocStart(); 5793 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 5794 } 5795 } else { 5796 ErrorFound = NotAnExpression; 5797 NoteLoc = ErrorLoc = S->getLocStart(); 5798 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 5799 } 5800 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 5801 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 5802 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 5803 return true; 5804 } else if (SemaRef.CurContext->isDependentContext()) 5805 E = X = UpdateExpr = nullptr; 5806 if (ErrorFound == NoError && E && X) { 5807 // Build an update expression of form 'OpaqueValueExpr(x) binop 5808 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 5809 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 5810 auto *OVEX = new (SemaRef.getASTContext()) 5811 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); 5812 auto *OVEExpr = new (SemaRef.getASTContext()) 5813 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); 5814 auto Update = 5815 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 5816 IsXLHSInRHSPart ? OVEExpr : OVEX); 5817 if (Update.isInvalid()) 5818 return true; 5819 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 5820 Sema::AA_Casting); 5821 if (Update.isInvalid()) 5822 return true; 5823 UpdateExpr = Update.get(); 5824 } 5825 return ErrorFound != NoError; 5826 } 5827 5828 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 5829 Stmt *AStmt, 5830 SourceLocation StartLoc, 5831 SourceLocation EndLoc) { 5832 if (!AStmt) 5833 return StmtError(); 5834 5835 auto CS = cast<CapturedStmt>(AStmt); 5836 // 1.2.2 OpenMP Language Terminology 5837 // Structured block - An executable statement with a single entry at the 5838 // top and a single exit at the bottom. 5839 // The point of exit cannot be a branch out of the structured block. 5840 // longjmp() and throw() must not violate the entry/exit criteria. 5841 OpenMPClauseKind AtomicKind = OMPC_unknown; 5842 SourceLocation AtomicKindLoc; 5843 for (auto *C : Clauses) { 5844 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 5845 C->getClauseKind() == OMPC_update || 5846 C->getClauseKind() == OMPC_capture) { 5847 if (AtomicKind != OMPC_unknown) { 5848 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses) 5849 << SourceRange(C->getLocStart(), C->getLocEnd()); 5850 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause) 5851 << getOpenMPClauseName(AtomicKind); 5852 } else { 5853 AtomicKind = C->getClauseKind(); 5854 AtomicKindLoc = C->getLocStart(); 5855 } 5856 } 5857 } 5858 5859 auto Body = CS->getCapturedStmt(); 5860 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 5861 Body = EWC->getSubExpr(); 5862 5863 Expr *X = nullptr; 5864 Expr *V = nullptr; 5865 Expr *E = nullptr; 5866 Expr *UE = nullptr; 5867 bool IsXLHSInRHSPart = false; 5868 bool IsPostfixUpdate = false; 5869 // OpenMP [2.12.6, atomic Construct] 5870 // In the next expressions: 5871 // * x and v (as applicable) are both l-value expressions with scalar type. 5872 // * During the execution of an atomic region, multiple syntactic 5873 // occurrences of x must designate the same storage location. 5874 // * Neither of v and expr (as applicable) may access the storage location 5875 // designated by x. 5876 // * Neither of x and expr (as applicable) may access the storage location 5877 // designated by v. 5878 // * expr is an expression with scalar type. 5879 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 5880 // * binop, binop=, ++, and -- are not overloaded operators. 5881 // * The expression x binop expr must be numerically equivalent to x binop 5882 // (expr). This requirement is satisfied if the operators in expr have 5883 // precedence greater than binop, or by using parentheses around expr or 5884 // subexpressions of expr. 5885 // * The expression expr binop x must be numerically equivalent to (expr) 5886 // binop x. This requirement is satisfied if the operators in expr have 5887 // precedence equal to or greater than binop, or by using parentheses around 5888 // expr or subexpressions of expr. 5889 // * For forms that allow multiple occurrences of x, the number of times 5890 // that x is evaluated is unspecified. 5891 if (AtomicKind == OMPC_read) { 5892 enum { 5893 NotAnExpression, 5894 NotAnAssignmentOp, 5895 NotAScalarType, 5896 NotAnLValue, 5897 NoError 5898 } ErrorFound = NoError; 5899 SourceLocation ErrorLoc, NoteLoc; 5900 SourceRange ErrorRange, NoteRange; 5901 // If clause is read: 5902 // v = x; 5903 if (auto AtomicBody = dyn_cast<Expr>(Body)) { 5904 auto AtomicBinOp = 5905 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 5906 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 5907 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 5908 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 5909 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 5910 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 5911 if (!X->isLValue() || !V->isLValue()) { 5912 auto NotLValueExpr = X->isLValue() ? V : X; 5913 ErrorFound = NotAnLValue; 5914 ErrorLoc = AtomicBinOp->getExprLoc(); 5915 ErrorRange = AtomicBinOp->getSourceRange(); 5916 NoteLoc = NotLValueExpr->getExprLoc(); 5917 NoteRange = NotLValueExpr->getSourceRange(); 5918 } 5919 } else if (!X->isInstantiationDependent() || 5920 !V->isInstantiationDependent()) { 5921 auto NotScalarExpr = 5922 (X->isInstantiationDependent() || X->getType()->isScalarType()) 5923 ? V 5924 : X; 5925 ErrorFound = NotAScalarType; 5926 ErrorLoc = AtomicBinOp->getExprLoc(); 5927 ErrorRange = AtomicBinOp->getSourceRange(); 5928 NoteLoc = NotScalarExpr->getExprLoc(); 5929 NoteRange = NotScalarExpr->getSourceRange(); 5930 } 5931 } else if (!AtomicBody->isInstantiationDependent()) { 5932 ErrorFound = NotAnAssignmentOp; 5933 ErrorLoc = AtomicBody->getExprLoc(); 5934 ErrorRange = AtomicBody->getSourceRange(); 5935 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 5936 : AtomicBody->getExprLoc(); 5937 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 5938 : AtomicBody->getSourceRange(); 5939 } 5940 } else { 5941 ErrorFound = NotAnExpression; 5942 NoteLoc = ErrorLoc = Body->getLocStart(); 5943 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 5944 } 5945 if (ErrorFound != NoError) { 5946 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 5947 << ErrorRange; 5948 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 5949 << NoteRange; 5950 return StmtError(); 5951 } else if (CurContext->isDependentContext()) 5952 V = X = nullptr; 5953 } else if (AtomicKind == OMPC_write) { 5954 enum { 5955 NotAnExpression, 5956 NotAnAssignmentOp, 5957 NotAScalarType, 5958 NotAnLValue, 5959 NoError 5960 } ErrorFound = NoError; 5961 SourceLocation ErrorLoc, NoteLoc; 5962 SourceRange ErrorRange, NoteRange; 5963 // If clause is write: 5964 // x = expr; 5965 if (auto AtomicBody = dyn_cast<Expr>(Body)) { 5966 auto AtomicBinOp = 5967 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 5968 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 5969 X = AtomicBinOp->getLHS(); 5970 E = AtomicBinOp->getRHS(); 5971 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 5972 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 5973 if (!X->isLValue()) { 5974 ErrorFound = NotAnLValue; 5975 ErrorLoc = AtomicBinOp->getExprLoc(); 5976 ErrorRange = AtomicBinOp->getSourceRange(); 5977 NoteLoc = X->getExprLoc(); 5978 NoteRange = X->getSourceRange(); 5979 } 5980 } else if (!X->isInstantiationDependent() || 5981 !E->isInstantiationDependent()) { 5982 auto NotScalarExpr = 5983 (X->isInstantiationDependent() || X->getType()->isScalarType()) 5984 ? E 5985 : X; 5986 ErrorFound = NotAScalarType; 5987 ErrorLoc = AtomicBinOp->getExprLoc(); 5988 ErrorRange = AtomicBinOp->getSourceRange(); 5989 NoteLoc = NotScalarExpr->getExprLoc(); 5990 NoteRange = NotScalarExpr->getSourceRange(); 5991 } 5992 } else if (!AtomicBody->isInstantiationDependent()) { 5993 ErrorFound = NotAnAssignmentOp; 5994 ErrorLoc = AtomicBody->getExprLoc(); 5995 ErrorRange = AtomicBody->getSourceRange(); 5996 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 5997 : AtomicBody->getExprLoc(); 5998 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 5999 : AtomicBody->getSourceRange(); 6000 } 6001 } else { 6002 ErrorFound = NotAnExpression; 6003 NoteLoc = ErrorLoc = Body->getLocStart(); 6004 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6005 } 6006 if (ErrorFound != NoError) { 6007 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 6008 << ErrorRange; 6009 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 6010 << NoteRange; 6011 return StmtError(); 6012 } else if (CurContext->isDependentContext()) 6013 E = X = nullptr; 6014 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 6015 // If clause is update: 6016 // x++; 6017 // x--; 6018 // ++x; 6019 // --x; 6020 // x binop= expr; 6021 // x = x binop expr; 6022 // x = expr binop x; 6023 OpenMPAtomicUpdateChecker Checker(*this); 6024 if (Checker.checkStatement( 6025 Body, (AtomicKind == OMPC_update) 6026 ? diag::err_omp_atomic_update_not_expression_statement 6027 : diag::err_omp_atomic_not_expression_statement, 6028 diag::note_omp_atomic_update)) 6029 return StmtError(); 6030 if (!CurContext->isDependentContext()) { 6031 E = Checker.getExpr(); 6032 X = Checker.getX(); 6033 UE = Checker.getUpdateExpr(); 6034 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6035 } 6036 } else if (AtomicKind == OMPC_capture) { 6037 enum { 6038 NotAnAssignmentOp, 6039 NotACompoundStatement, 6040 NotTwoSubstatements, 6041 NotASpecificExpression, 6042 NoError 6043 } ErrorFound = NoError; 6044 SourceLocation ErrorLoc, NoteLoc; 6045 SourceRange ErrorRange, NoteRange; 6046 if (auto *AtomicBody = dyn_cast<Expr>(Body)) { 6047 // If clause is a capture: 6048 // v = x++; 6049 // v = x--; 6050 // v = ++x; 6051 // v = --x; 6052 // v = x binop= expr; 6053 // v = x = x binop expr; 6054 // v = x = expr binop x; 6055 auto *AtomicBinOp = 6056 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 6057 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 6058 V = AtomicBinOp->getLHS(); 6059 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 6060 OpenMPAtomicUpdateChecker Checker(*this); 6061 if (Checker.checkStatement( 6062 Body, diag::err_omp_atomic_capture_not_expression_statement, 6063 diag::note_omp_atomic_update)) 6064 return StmtError(); 6065 E = Checker.getExpr(); 6066 X = Checker.getX(); 6067 UE = Checker.getUpdateExpr(); 6068 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6069 IsPostfixUpdate = Checker.isPostfixUpdate(); 6070 } else if (!AtomicBody->isInstantiationDependent()) { 6071 ErrorLoc = AtomicBody->getExprLoc(); 6072 ErrorRange = AtomicBody->getSourceRange(); 6073 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 6074 : AtomicBody->getExprLoc(); 6075 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 6076 : AtomicBody->getSourceRange(); 6077 ErrorFound = NotAnAssignmentOp; 6078 } 6079 if (ErrorFound != NoError) { 6080 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 6081 << ErrorRange; 6082 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 6083 return StmtError(); 6084 } else if (CurContext->isDependentContext()) { 6085 UE = V = E = X = nullptr; 6086 } 6087 } else { 6088 // If clause is a capture: 6089 // { v = x; x = expr; } 6090 // { v = x; x++; } 6091 // { v = x; x--; } 6092 // { v = x; ++x; } 6093 // { v = x; --x; } 6094 // { v = x; x binop= expr; } 6095 // { v = x; x = x binop expr; } 6096 // { v = x; x = expr binop x; } 6097 // { x++; v = x; } 6098 // { x--; v = x; } 6099 // { ++x; v = x; } 6100 // { --x; v = x; } 6101 // { x binop= expr; v = x; } 6102 // { x = x binop expr; v = x; } 6103 // { x = expr binop x; v = x; } 6104 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 6105 // Check that this is { expr1; expr2; } 6106 if (CS->size() == 2) { 6107 auto *First = CS->body_front(); 6108 auto *Second = CS->body_back(); 6109 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 6110 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 6111 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 6112 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 6113 // Need to find what subexpression is 'v' and what is 'x'. 6114 OpenMPAtomicUpdateChecker Checker(*this); 6115 bool IsUpdateExprFound = !Checker.checkStatement(Second); 6116 BinaryOperator *BinOp = nullptr; 6117 if (IsUpdateExprFound) { 6118 BinOp = dyn_cast<BinaryOperator>(First); 6119 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 6120 } 6121 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 6122 // { v = x; x++; } 6123 // { v = x; x--; } 6124 // { v = x; ++x; } 6125 // { v = x; --x; } 6126 // { v = x; x binop= expr; } 6127 // { v = x; x = x binop expr; } 6128 // { v = x; x = expr binop x; } 6129 // Check that the first expression has form v = x. 6130 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 6131 llvm::FoldingSetNodeID XId, PossibleXId; 6132 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 6133 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 6134 IsUpdateExprFound = XId == PossibleXId; 6135 if (IsUpdateExprFound) { 6136 V = BinOp->getLHS(); 6137 X = Checker.getX(); 6138 E = Checker.getExpr(); 6139 UE = Checker.getUpdateExpr(); 6140 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6141 IsPostfixUpdate = true; 6142 } 6143 } 6144 if (!IsUpdateExprFound) { 6145 IsUpdateExprFound = !Checker.checkStatement(First); 6146 BinOp = nullptr; 6147 if (IsUpdateExprFound) { 6148 BinOp = dyn_cast<BinaryOperator>(Second); 6149 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 6150 } 6151 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 6152 // { x++; v = x; } 6153 // { x--; v = x; } 6154 // { ++x; v = x; } 6155 // { --x; v = x; } 6156 // { x binop= expr; v = x; } 6157 // { x = x binop expr; v = x; } 6158 // { x = expr binop x; v = x; } 6159 // Check that the second expression has form v = x. 6160 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 6161 llvm::FoldingSetNodeID XId, PossibleXId; 6162 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 6163 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 6164 IsUpdateExprFound = XId == PossibleXId; 6165 if (IsUpdateExprFound) { 6166 V = BinOp->getLHS(); 6167 X = Checker.getX(); 6168 E = Checker.getExpr(); 6169 UE = Checker.getUpdateExpr(); 6170 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6171 IsPostfixUpdate = false; 6172 } 6173 } 6174 } 6175 if (!IsUpdateExprFound) { 6176 // { v = x; x = expr; } 6177 auto *FirstExpr = dyn_cast<Expr>(First); 6178 auto *SecondExpr = dyn_cast<Expr>(Second); 6179 if (!FirstExpr || !SecondExpr || 6180 !(FirstExpr->isInstantiationDependent() || 6181 SecondExpr->isInstantiationDependent())) { 6182 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 6183 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 6184 ErrorFound = NotAnAssignmentOp; 6185 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 6186 : First->getLocStart(); 6187 NoteRange = ErrorRange = FirstBinOp 6188 ? FirstBinOp->getSourceRange() 6189 : SourceRange(ErrorLoc, ErrorLoc); 6190 } else { 6191 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 6192 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 6193 ErrorFound = NotAnAssignmentOp; 6194 NoteLoc = ErrorLoc = SecondBinOp 6195 ? SecondBinOp->getOperatorLoc() 6196 : Second->getLocStart(); 6197 NoteRange = ErrorRange = 6198 SecondBinOp ? SecondBinOp->getSourceRange() 6199 : SourceRange(ErrorLoc, ErrorLoc); 6200 } else { 6201 auto *PossibleXRHSInFirst = 6202 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 6203 auto *PossibleXLHSInSecond = 6204 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 6205 llvm::FoldingSetNodeID X1Id, X2Id; 6206 PossibleXRHSInFirst->Profile(X1Id, Context, 6207 /*Canonical=*/true); 6208 PossibleXLHSInSecond->Profile(X2Id, Context, 6209 /*Canonical=*/true); 6210 IsUpdateExprFound = X1Id == X2Id; 6211 if (IsUpdateExprFound) { 6212 V = FirstBinOp->getLHS(); 6213 X = SecondBinOp->getLHS(); 6214 E = SecondBinOp->getRHS(); 6215 UE = nullptr; 6216 IsXLHSInRHSPart = false; 6217 IsPostfixUpdate = true; 6218 } else { 6219 ErrorFound = NotASpecificExpression; 6220 ErrorLoc = FirstBinOp->getExprLoc(); 6221 ErrorRange = FirstBinOp->getSourceRange(); 6222 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 6223 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 6224 } 6225 } 6226 } 6227 } 6228 } 6229 } else { 6230 NoteLoc = ErrorLoc = Body->getLocStart(); 6231 NoteRange = ErrorRange = 6232 SourceRange(Body->getLocStart(), Body->getLocStart()); 6233 ErrorFound = NotTwoSubstatements; 6234 } 6235 } else { 6236 NoteLoc = ErrorLoc = Body->getLocStart(); 6237 NoteRange = ErrorRange = 6238 SourceRange(Body->getLocStart(), Body->getLocStart()); 6239 ErrorFound = NotACompoundStatement; 6240 } 6241 if (ErrorFound != NoError) { 6242 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 6243 << ErrorRange; 6244 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 6245 return StmtError(); 6246 } else if (CurContext->isDependentContext()) { 6247 UE = V = E = X = nullptr; 6248 } 6249 } 6250 } 6251 6252 getCurFunction()->setHasBranchProtectedScope(); 6253 6254 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 6255 X, V, E, UE, IsXLHSInRHSPart, 6256 IsPostfixUpdate); 6257 } 6258 6259 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 6260 Stmt *AStmt, 6261 SourceLocation StartLoc, 6262 SourceLocation EndLoc) { 6263 if (!AStmt) 6264 return StmtError(); 6265 6266 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6267 // 1.2.2 OpenMP Language Terminology 6268 // Structured block - An executable statement with a single entry at the 6269 // top and a single exit at the bottom. 6270 // The point of exit cannot be a branch out of the structured block. 6271 // longjmp() and throw() must not violate the entry/exit criteria. 6272 CS->getCapturedDecl()->setNothrow(); 6273 6274 // OpenMP [2.16, Nesting of Regions] 6275 // If specified, a teams construct must be contained within a target 6276 // construct. That target construct must contain no statements or directives 6277 // outside of the teams construct. 6278 if (DSAStack->hasInnerTeamsRegion()) { 6279 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true); 6280 bool OMPTeamsFound = true; 6281 if (auto *CS = dyn_cast<CompoundStmt>(S)) { 6282 auto I = CS->body_begin(); 6283 while (I != CS->body_end()) { 6284 auto OED = dyn_cast<OMPExecutableDirective>(*I); 6285 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) { 6286 OMPTeamsFound = false; 6287 break; 6288 } 6289 ++I; 6290 } 6291 assert(I != CS->body_end() && "Not found statement"); 6292 S = *I; 6293 } 6294 if (!OMPTeamsFound) { 6295 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 6296 Diag(DSAStack->getInnerTeamsRegionLoc(), 6297 diag::note_omp_nested_teams_construct_here); 6298 Diag(S->getLocStart(), diag::note_omp_nested_statement_here) 6299 << isa<OMPExecutableDirective>(S); 6300 return StmtError(); 6301 } 6302 } 6303 6304 getCurFunction()->setHasBranchProtectedScope(); 6305 6306 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 6307 } 6308 6309 StmtResult 6310 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 6311 Stmt *AStmt, SourceLocation StartLoc, 6312 SourceLocation EndLoc) { 6313 if (!AStmt) 6314 return StmtError(); 6315 6316 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6317 // 1.2.2 OpenMP Language Terminology 6318 // Structured block - An executable statement with a single entry at the 6319 // top and a single exit at the bottom. 6320 // The point of exit cannot be a branch out of the structured block. 6321 // longjmp() and throw() must not violate the entry/exit criteria. 6322 CS->getCapturedDecl()->setNothrow(); 6323 6324 getCurFunction()->setHasBranchProtectedScope(); 6325 6326 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, 6327 AStmt); 6328 } 6329 6330 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 6331 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6332 SourceLocation EndLoc, 6333 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6334 if (!AStmt) 6335 return StmtError(); 6336 6337 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6338 // 1.2.2 OpenMP Language Terminology 6339 // Structured block - An executable statement with a single entry at the 6340 // top and a single exit at the bottom. 6341 // The point of exit cannot be a branch out of the structured block. 6342 // longjmp() and throw() must not violate the entry/exit criteria. 6343 CS->getCapturedDecl()->setNothrow(); 6344 6345 OMPLoopDirective::HelperExprs B; 6346 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6347 // define the nested loops number. 6348 unsigned NestedLoopCount = 6349 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 6350 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 6351 VarsWithImplicitDSA, B); 6352 if (NestedLoopCount == 0) 6353 return StmtError(); 6354 6355 assert((CurContext->isDependentContext() || B.builtAll()) && 6356 "omp target parallel for loop exprs were not built"); 6357 6358 if (!CurContext->isDependentContext()) { 6359 // Finalize the clauses that need pre-built expressions for CodeGen. 6360 for (auto C : Clauses) { 6361 if (auto LC = dyn_cast<OMPLinearClause>(C)) 6362 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6363 B.NumIterations, *this, CurScope, 6364 DSAStack)) 6365 return StmtError(); 6366 } 6367 } 6368 6369 getCurFunction()->setHasBranchProtectedScope(); 6370 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc, 6371 NestedLoopCount, Clauses, AStmt, 6372 B, DSAStack->isCancelRegion()); 6373 } 6374 6375 /// \brief Check for existence of a map clause in the list of clauses. 6376 static bool HasMapClause(ArrayRef<OMPClause *> Clauses) { 6377 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end(); 6378 I != E; ++I) { 6379 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) { 6380 return true; 6381 } 6382 } 6383 6384 return false; 6385 } 6386 6387 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 6388 Stmt *AStmt, 6389 SourceLocation StartLoc, 6390 SourceLocation EndLoc) { 6391 if (!AStmt) 6392 return StmtError(); 6393 6394 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6395 6396 // OpenMP [2.10.1, Restrictions, p. 97] 6397 // At least one map clause must appear on the directive. 6398 if (!HasMapClause(Clauses)) { 6399 Diag(StartLoc, diag::err_omp_no_map_for_directive) << 6400 getOpenMPDirectiveName(OMPD_target_data); 6401 return StmtError(); 6402 } 6403 6404 getCurFunction()->setHasBranchProtectedScope(); 6405 6406 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 6407 AStmt); 6408 } 6409 6410 StmtResult 6411 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 6412 SourceLocation StartLoc, 6413 SourceLocation EndLoc) { 6414 // OpenMP [2.10.2, Restrictions, p. 99] 6415 // At least one map clause must appear on the directive. 6416 if (!HasMapClause(Clauses)) { 6417 Diag(StartLoc, diag::err_omp_no_map_for_directive) 6418 << getOpenMPDirectiveName(OMPD_target_enter_data); 6419 return StmtError(); 6420 } 6421 6422 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, 6423 Clauses); 6424 } 6425 6426 StmtResult 6427 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 6428 SourceLocation StartLoc, 6429 SourceLocation EndLoc) { 6430 // OpenMP [2.10.3, Restrictions, p. 102] 6431 // At least one map clause must appear on the directive. 6432 if (!HasMapClause(Clauses)) { 6433 Diag(StartLoc, diag::err_omp_no_map_for_directive) 6434 << getOpenMPDirectiveName(OMPD_target_exit_data); 6435 return StmtError(); 6436 } 6437 6438 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses); 6439 } 6440 6441 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 6442 Stmt *AStmt, SourceLocation StartLoc, 6443 SourceLocation EndLoc) { 6444 if (!AStmt) 6445 return StmtError(); 6446 6447 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6448 // 1.2.2 OpenMP Language Terminology 6449 // Structured block - An executable statement with a single entry at the 6450 // top and a single exit at the bottom. 6451 // The point of exit cannot be a branch out of the structured block. 6452 // longjmp() and throw() must not violate the entry/exit criteria. 6453 CS->getCapturedDecl()->setNothrow(); 6454 6455 getCurFunction()->setHasBranchProtectedScope(); 6456 6457 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 6458 } 6459 6460 StmtResult 6461 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 6462 SourceLocation EndLoc, 6463 OpenMPDirectiveKind CancelRegion) { 6464 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for && 6465 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) { 6466 Diag(StartLoc, diag::err_omp_wrong_cancel_region) 6467 << getOpenMPDirectiveName(CancelRegion); 6468 return StmtError(); 6469 } 6470 if (DSAStack->isParentNowaitRegion()) { 6471 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 6472 return StmtError(); 6473 } 6474 if (DSAStack->isParentOrderedRegion()) { 6475 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 6476 return StmtError(); 6477 } 6478 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 6479 CancelRegion); 6480 } 6481 6482 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 6483 SourceLocation StartLoc, 6484 SourceLocation EndLoc, 6485 OpenMPDirectiveKind CancelRegion) { 6486 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for && 6487 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) { 6488 Diag(StartLoc, diag::err_omp_wrong_cancel_region) 6489 << getOpenMPDirectiveName(CancelRegion); 6490 return StmtError(); 6491 } 6492 if (DSAStack->isParentNowaitRegion()) { 6493 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 6494 return StmtError(); 6495 } 6496 if (DSAStack->isParentOrderedRegion()) { 6497 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 6498 return StmtError(); 6499 } 6500 DSAStack->setParentCancelRegion(/*Cancel=*/true); 6501 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 6502 CancelRegion); 6503 } 6504 6505 static bool checkGrainsizeNumTasksClauses(Sema &S, 6506 ArrayRef<OMPClause *> Clauses) { 6507 OMPClause *PrevClause = nullptr; 6508 bool ErrorFound = false; 6509 for (auto *C : Clauses) { 6510 if (C->getClauseKind() == OMPC_grainsize || 6511 C->getClauseKind() == OMPC_num_tasks) { 6512 if (!PrevClause) 6513 PrevClause = C; 6514 else if (PrevClause->getClauseKind() != C->getClauseKind()) { 6515 S.Diag(C->getLocStart(), 6516 diag::err_omp_grainsize_num_tasks_mutually_exclusive) 6517 << getOpenMPClauseName(C->getClauseKind()) 6518 << getOpenMPClauseName(PrevClause->getClauseKind()); 6519 S.Diag(PrevClause->getLocStart(), 6520 diag::note_omp_previous_grainsize_num_tasks) 6521 << getOpenMPClauseName(PrevClause->getClauseKind()); 6522 ErrorFound = true; 6523 } 6524 } 6525 } 6526 return ErrorFound; 6527 } 6528 6529 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 6530 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6531 SourceLocation EndLoc, 6532 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6533 if (!AStmt) 6534 return StmtError(); 6535 6536 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6537 OMPLoopDirective::HelperExprs B; 6538 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6539 // define the nested loops number. 6540 unsigned NestedLoopCount = 6541 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 6542 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 6543 VarsWithImplicitDSA, B); 6544 if (NestedLoopCount == 0) 6545 return StmtError(); 6546 6547 assert((CurContext->isDependentContext() || B.builtAll()) && 6548 "omp for loop exprs were not built"); 6549 6550 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 6551 // The grainsize clause and num_tasks clause are mutually exclusive and may 6552 // not appear on the same taskloop directive. 6553 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 6554 return StmtError(); 6555 6556 getCurFunction()->setHasBranchProtectedScope(); 6557 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 6558 NestedLoopCount, Clauses, AStmt, B); 6559 } 6560 6561 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 6562 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6563 SourceLocation EndLoc, 6564 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6565 if (!AStmt) 6566 return StmtError(); 6567 6568 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6569 OMPLoopDirective::HelperExprs B; 6570 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6571 // define the nested loops number. 6572 unsigned NestedLoopCount = 6573 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 6574 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 6575 VarsWithImplicitDSA, B); 6576 if (NestedLoopCount == 0) 6577 return StmtError(); 6578 6579 assert((CurContext->isDependentContext() || B.builtAll()) && 6580 "omp for loop exprs were not built"); 6581 6582 if (!CurContext->isDependentContext()) { 6583 // Finalize the clauses that need pre-built expressions for CodeGen. 6584 for (auto C : Clauses) { 6585 if (auto LC = dyn_cast<OMPLinearClause>(C)) 6586 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6587 B.NumIterations, *this, CurScope, 6588 DSAStack)) 6589 return StmtError(); 6590 } 6591 } 6592 6593 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 6594 // The grainsize clause and num_tasks clause are mutually exclusive and may 6595 // not appear on the same taskloop directive. 6596 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 6597 return StmtError(); 6598 6599 getCurFunction()->setHasBranchProtectedScope(); 6600 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 6601 NestedLoopCount, Clauses, AStmt, B); 6602 } 6603 6604 StmtResult Sema::ActOnOpenMPDistributeDirective( 6605 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6606 SourceLocation EndLoc, 6607 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6608 if (!AStmt) 6609 return StmtError(); 6610 6611 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6612 OMPLoopDirective::HelperExprs B; 6613 // In presence of clause 'collapse' with number of loops, it will 6614 // define the nested loops number. 6615 unsigned NestedLoopCount = 6616 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 6617 nullptr /*ordered not a clause on distribute*/, AStmt, 6618 *this, *DSAStack, VarsWithImplicitDSA, B); 6619 if (NestedLoopCount == 0) 6620 return StmtError(); 6621 6622 assert((CurContext->isDependentContext() || B.builtAll()) && 6623 "omp for loop exprs were not built"); 6624 6625 getCurFunction()->setHasBranchProtectedScope(); 6626 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 6627 NestedLoopCount, Clauses, AStmt, B); 6628 } 6629 6630 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 6631 SourceLocation StartLoc, 6632 SourceLocation LParenLoc, 6633 SourceLocation EndLoc) { 6634 OMPClause *Res = nullptr; 6635 switch (Kind) { 6636 case OMPC_final: 6637 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 6638 break; 6639 case OMPC_num_threads: 6640 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 6641 break; 6642 case OMPC_safelen: 6643 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 6644 break; 6645 case OMPC_simdlen: 6646 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 6647 break; 6648 case OMPC_collapse: 6649 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 6650 break; 6651 case OMPC_ordered: 6652 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 6653 break; 6654 case OMPC_device: 6655 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc); 6656 break; 6657 case OMPC_num_teams: 6658 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 6659 break; 6660 case OMPC_thread_limit: 6661 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 6662 break; 6663 case OMPC_priority: 6664 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 6665 break; 6666 case OMPC_grainsize: 6667 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 6668 break; 6669 case OMPC_num_tasks: 6670 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 6671 break; 6672 case OMPC_hint: 6673 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 6674 break; 6675 case OMPC_if: 6676 case OMPC_default: 6677 case OMPC_proc_bind: 6678 case OMPC_schedule: 6679 case OMPC_private: 6680 case OMPC_firstprivate: 6681 case OMPC_lastprivate: 6682 case OMPC_shared: 6683 case OMPC_reduction: 6684 case OMPC_linear: 6685 case OMPC_aligned: 6686 case OMPC_copyin: 6687 case OMPC_copyprivate: 6688 case OMPC_nowait: 6689 case OMPC_untied: 6690 case OMPC_mergeable: 6691 case OMPC_threadprivate: 6692 case OMPC_flush: 6693 case OMPC_read: 6694 case OMPC_write: 6695 case OMPC_update: 6696 case OMPC_capture: 6697 case OMPC_seq_cst: 6698 case OMPC_depend: 6699 case OMPC_threads: 6700 case OMPC_simd: 6701 case OMPC_map: 6702 case OMPC_nogroup: 6703 case OMPC_dist_schedule: 6704 case OMPC_defaultmap: 6705 case OMPC_unknown: 6706 case OMPC_uniform: 6707 llvm_unreachable("Clause is not allowed."); 6708 } 6709 return Res; 6710 } 6711 6712 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 6713 Expr *Condition, SourceLocation StartLoc, 6714 SourceLocation LParenLoc, 6715 SourceLocation NameModifierLoc, 6716 SourceLocation ColonLoc, 6717 SourceLocation EndLoc) { 6718 Expr *ValExpr = Condition; 6719 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 6720 !Condition->isInstantiationDependent() && 6721 !Condition->containsUnexpandedParameterPack()) { 6722 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(), 6723 Condition->getExprLoc(), Condition); 6724 if (Val.isInvalid()) 6725 return nullptr; 6726 6727 ValExpr = Val.get(); 6728 } 6729 6730 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc, 6731 NameModifierLoc, ColonLoc, EndLoc); 6732 } 6733 6734 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 6735 SourceLocation StartLoc, 6736 SourceLocation LParenLoc, 6737 SourceLocation EndLoc) { 6738 Expr *ValExpr = Condition; 6739 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 6740 !Condition->isInstantiationDependent() && 6741 !Condition->containsUnexpandedParameterPack()) { 6742 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(), 6743 Condition->getExprLoc(), Condition); 6744 if (Val.isInvalid()) 6745 return nullptr; 6746 6747 ValExpr = Val.get(); 6748 } 6749 6750 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc); 6751 } 6752 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 6753 Expr *Op) { 6754 if (!Op) 6755 return ExprError(); 6756 6757 class IntConvertDiagnoser : public ICEConvertDiagnoser { 6758 public: 6759 IntConvertDiagnoser() 6760 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 6761 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 6762 QualType T) override { 6763 return S.Diag(Loc, diag::err_omp_not_integral) << T; 6764 } 6765 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 6766 QualType T) override { 6767 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 6768 } 6769 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 6770 QualType T, 6771 QualType ConvTy) override { 6772 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 6773 } 6774 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 6775 QualType ConvTy) override { 6776 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 6777 << ConvTy->isEnumeralType() << ConvTy; 6778 } 6779 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 6780 QualType T) override { 6781 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 6782 } 6783 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 6784 QualType ConvTy) override { 6785 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 6786 << ConvTy->isEnumeralType() << ConvTy; 6787 } 6788 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 6789 QualType) override { 6790 llvm_unreachable("conversion functions are permitted"); 6791 } 6792 } ConvertDiagnoser; 6793 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 6794 } 6795 6796 static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, 6797 OpenMPClauseKind CKind, 6798 bool StrictlyPositive) { 6799 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 6800 !ValExpr->isInstantiationDependent()) { 6801 SourceLocation Loc = ValExpr->getExprLoc(); 6802 ExprResult Value = 6803 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 6804 if (Value.isInvalid()) 6805 return false; 6806 6807 ValExpr = Value.get(); 6808 // The expression must evaluate to a non-negative integer value. 6809 llvm::APSInt Result; 6810 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) && 6811 Result.isSigned() && 6812 !((!StrictlyPositive && Result.isNonNegative()) || 6813 (StrictlyPositive && Result.isStrictlyPositive()))) { 6814 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 6815 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 6816 << ValExpr->getSourceRange(); 6817 return false; 6818 } 6819 } 6820 return true; 6821 } 6822 6823 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 6824 SourceLocation StartLoc, 6825 SourceLocation LParenLoc, 6826 SourceLocation EndLoc) { 6827 Expr *ValExpr = NumThreads; 6828 6829 // OpenMP [2.5, Restrictions] 6830 // The num_threads expression must evaluate to a positive integer value. 6831 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 6832 /*StrictlyPositive=*/true)) 6833 return nullptr; 6834 6835 return new (Context) 6836 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc); 6837 } 6838 6839 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 6840 OpenMPClauseKind CKind, 6841 bool StrictlyPositive) { 6842 if (!E) 6843 return ExprError(); 6844 if (E->isValueDependent() || E->isTypeDependent() || 6845 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 6846 return E; 6847 llvm::APSInt Result; 6848 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); 6849 if (ICE.isInvalid()) 6850 return ExprError(); 6851 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 6852 (!StrictlyPositive && !Result.isNonNegative())) { 6853 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 6854 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 6855 << E->getSourceRange(); 6856 return ExprError(); 6857 } 6858 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 6859 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 6860 << E->getSourceRange(); 6861 return ExprError(); 6862 } 6863 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 6864 DSAStack->setAssociatedLoops(Result.getExtValue()); 6865 else if (CKind == OMPC_ordered) 6866 DSAStack->setAssociatedLoops(Result.getExtValue()); 6867 return ICE; 6868 } 6869 6870 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 6871 SourceLocation LParenLoc, 6872 SourceLocation EndLoc) { 6873 // OpenMP [2.8.1, simd construct, Description] 6874 // The parameter of the safelen clause must be a constant 6875 // positive integer expression. 6876 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 6877 if (Safelen.isInvalid()) 6878 return nullptr; 6879 return new (Context) 6880 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 6881 } 6882 6883 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 6884 SourceLocation LParenLoc, 6885 SourceLocation EndLoc) { 6886 // OpenMP [2.8.1, simd construct, Description] 6887 // The parameter of the simdlen clause must be a constant 6888 // positive integer expression. 6889 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 6890 if (Simdlen.isInvalid()) 6891 return nullptr; 6892 return new (Context) 6893 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 6894 } 6895 6896 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 6897 SourceLocation StartLoc, 6898 SourceLocation LParenLoc, 6899 SourceLocation EndLoc) { 6900 // OpenMP [2.7.1, loop construct, Description] 6901 // OpenMP [2.8.1, simd construct, Description] 6902 // OpenMP [2.9.6, distribute construct, Description] 6903 // The parameter of the collapse clause must be a constant 6904 // positive integer expression. 6905 ExprResult NumForLoopsResult = 6906 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 6907 if (NumForLoopsResult.isInvalid()) 6908 return nullptr; 6909 return new (Context) 6910 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 6911 } 6912 6913 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 6914 SourceLocation EndLoc, 6915 SourceLocation LParenLoc, 6916 Expr *NumForLoops) { 6917 // OpenMP [2.7.1, loop construct, Description] 6918 // OpenMP [2.8.1, simd construct, Description] 6919 // OpenMP [2.9.6, distribute construct, Description] 6920 // The parameter of the ordered clause must be a constant 6921 // positive integer expression if any. 6922 if (NumForLoops && LParenLoc.isValid()) { 6923 ExprResult NumForLoopsResult = 6924 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 6925 if (NumForLoopsResult.isInvalid()) 6926 return nullptr; 6927 NumForLoops = NumForLoopsResult.get(); 6928 } else 6929 NumForLoops = nullptr; 6930 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops); 6931 return new (Context) 6932 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc); 6933 } 6934 6935 OMPClause *Sema::ActOnOpenMPSimpleClause( 6936 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 6937 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 6938 OMPClause *Res = nullptr; 6939 switch (Kind) { 6940 case OMPC_default: 6941 Res = 6942 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), 6943 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 6944 break; 6945 case OMPC_proc_bind: 6946 Res = ActOnOpenMPProcBindClause( 6947 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, 6948 LParenLoc, EndLoc); 6949 break; 6950 case OMPC_if: 6951 case OMPC_final: 6952 case OMPC_num_threads: 6953 case OMPC_safelen: 6954 case OMPC_simdlen: 6955 case OMPC_collapse: 6956 case OMPC_schedule: 6957 case OMPC_private: 6958 case OMPC_firstprivate: 6959 case OMPC_lastprivate: 6960 case OMPC_shared: 6961 case OMPC_reduction: 6962 case OMPC_linear: 6963 case OMPC_aligned: 6964 case OMPC_copyin: 6965 case OMPC_copyprivate: 6966 case OMPC_ordered: 6967 case OMPC_nowait: 6968 case OMPC_untied: 6969 case OMPC_mergeable: 6970 case OMPC_threadprivate: 6971 case OMPC_flush: 6972 case OMPC_read: 6973 case OMPC_write: 6974 case OMPC_update: 6975 case OMPC_capture: 6976 case OMPC_seq_cst: 6977 case OMPC_depend: 6978 case OMPC_device: 6979 case OMPC_threads: 6980 case OMPC_simd: 6981 case OMPC_map: 6982 case OMPC_num_teams: 6983 case OMPC_thread_limit: 6984 case OMPC_priority: 6985 case OMPC_grainsize: 6986 case OMPC_nogroup: 6987 case OMPC_num_tasks: 6988 case OMPC_hint: 6989 case OMPC_dist_schedule: 6990 case OMPC_defaultmap: 6991 case OMPC_unknown: 6992 case OMPC_uniform: 6993 llvm_unreachable("Clause is not allowed."); 6994 } 6995 return Res; 6996 } 6997 6998 static std::string 6999 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 7000 ArrayRef<unsigned> Exclude = llvm::None) { 7001 std::string Values; 7002 unsigned Bound = Last >= 2 ? Last - 2 : 0; 7003 unsigned Skipped = Exclude.size(); 7004 auto S = Exclude.begin(), E = Exclude.end(); 7005 for (unsigned i = First; i < Last; ++i) { 7006 if (std::find(S, E, i) != E) { 7007 --Skipped; 7008 continue; 7009 } 7010 Values += "'"; 7011 Values += getOpenMPSimpleClauseTypeName(K, i); 7012 Values += "'"; 7013 if (i == Bound - Skipped) 7014 Values += " or "; 7015 else if (i != Bound + 1 - Skipped) 7016 Values += ", "; 7017 } 7018 return Values; 7019 } 7020 7021 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, 7022 SourceLocation KindKwLoc, 7023 SourceLocation StartLoc, 7024 SourceLocation LParenLoc, 7025 SourceLocation EndLoc) { 7026 if (Kind == OMPC_DEFAULT_unknown) { 7027 static_assert(OMPC_DEFAULT_unknown > 0, 7028 "OMPC_DEFAULT_unknown not greater than 0"); 7029 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 7030 << getListOfPossibleValues(OMPC_default, /*First=*/0, 7031 /*Last=*/OMPC_DEFAULT_unknown) 7032 << getOpenMPClauseName(OMPC_default); 7033 return nullptr; 7034 } 7035 switch (Kind) { 7036 case OMPC_DEFAULT_none: 7037 DSAStack->setDefaultDSANone(KindKwLoc); 7038 break; 7039 case OMPC_DEFAULT_shared: 7040 DSAStack->setDefaultDSAShared(KindKwLoc); 7041 break; 7042 case OMPC_DEFAULT_unknown: 7043 llvm_unreachable("Clause kind is not allowed."); 7044 break; 7045 } 7046 return new (Context) 7047 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 7048 } 7049 7050 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, 7051 SourceLocation KindKwLoc, 7052 SourceLocation StartLoc, 7053 SourceLocation LParenLoc, 7054 SourceLocation EndLoc) { 7055 if (Kind == OMPC_PROC_BIND_unknown) { 7056 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 7057 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0, 7058 /*Last=*/OMPC_PROC_BIND_unknown) 7059 << getOpenMPClauseName(OMPC_proc_bind); 7060 return nullptr; 7061 } 7062 return new (Context) 7063 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 7064 } 7065 7066 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 7067 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 7068 SourceLocation StartLoc, SourceLocation LParenLoc, 7069 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 7070 SourceLocation EndLoc) { 7071 OMPClause *Res = nullptr; 7072 switch (Kind) { 7073 case OMPC_schedule: 7074 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 7075 assert(Argument.size() == NumberOfElements && 7076 ArgumentLoc.size() == NumberOfElements); 7077 Res = ActOnOpenMPScheduleClause( 7078 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 7079 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 7080 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 7081 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 7082 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 7083 break; 7084 case OMPC_if: 7085 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 7086 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 7087 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 7088 DelimLoc, EndLoc); 7089 break; 7090 case OMPC_dist_schedule: 7091 Res = ActOnOpenMPDistScheduleClause( 7092 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 7093 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 7094 break; 7095 case OMPC_defaultmap: 7096 enum { Modifier, DefaultmapKind }; 7097 Res = ActOnOpenMPDefaultmapClause( 7098 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 7099 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 7100 StartLoc, LParenLoc, ArgumentLoc[Modifier], 7101 ArgumentLoc[DefaultmapKind], EndLoc); 7102 break; 7103 case OMPC_final: 7104 case OMPC_num_threads: 7105 case OMPC_safelen: 7106 case OMPC_simdlen: 7107 case OMPC_collapse: 7108 case OMPC_default: 7109 case OMPC_proc_bind: 7110 case OMPC_private: 7111 case OMPC_firstprivate: 7112 case OMPC_lastprivate: 7113 case OMPC_shared: 7114 case OMPC_reduction: 7115 case OMPC_linear: 7116 case OMPC_aligned: 7117 case OMPC_copyin: 7118 case OMPC_copyprivate: 7119 case OMPC_ordered: 7120 case OMPC_nowait: 7121 case OMPC_untied: 7122 case OMPC_mergeable: 7123 case OMPC_threadprivate: 7124 case OMPC_flush: 7125 case OMPC_read: 7126 case OMPC_write: 7127 case OMPC_update: 7128 case OMPC_capture: 7129 case OMPC_seq_cst: 7130 case OMPC_depend: 7131 case OMPC_device: 7132 case OMPC_threads: 7133 case OMPC_simd: 7134 case OMPC_map: 7135 case OMPC_num_teams: 7136 case OMPC_thread_limit: 7137 case OMPC_priority: 7138 case OMPC_grainsize: 7139 case OMPC_nogroup: 7140 case OMPC_num_tasks: 7141 case OMPC_hint: 7142 case OMPC_unknown: 7143 case OMPC_uniform: 7144 llvm_unreachable("Clause is not allowed."); 7145 } 7146 return Res; 7147 } 7148 7149 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 7150 OpenMPScheduleClauseModifier M2, 7151 SourceLocation M1Loc, SourceLocation M2Loc) { 7152 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 7153 SmallVector<unsigned, 2> Excluded; 7154 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 7155 Excluded.push_back(M2); 7156 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 7157 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 7158 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 7159 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 7160 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 7161 << getListOfPossibleValues(OMPC_schedule, 7162 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 7163 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 7164 Excluded) 7165 << getOpenMPClauseName(OMPC_schedule); 7166 return true; 7167 } 7168 return false; 7169 } 7170 7171 OMPClause *Sema::ActOnOpenMPScheduleClause( 7172 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 7173 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 7174 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 7175 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 7176 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 7177 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 7178 return nullptr; 7179 // OpenMP, 2.7.1, Loop Construct, Restrictions 7180 // Either the monotonic modifier or the nonmonotonic modifier can be specified 7181 // but not both. 7182 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 7183 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 7184 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 7185 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 7186 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 7187 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 7188 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 7189 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 7190 return nullptr; 7191 } 7192 if (Kind == OMPC_SCHEDULE_unknown) { 7193 std::string Values; 7194 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 7195 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 7196 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 7197 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 7198 Exclude); 7199 } else { 7200 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 7201 /*Last=*/OMPC_SCHEDULE_unknown); 7202 } 7203 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 7204 << Values << getOpenMPClauseName(OMPC_schedule); 7205 return nullptr; 7206 } 7207 // OpenMP, 2.7.1, Loop Construct, Restrictions 7208 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 7209 // schedule(guided). 7210 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 7211 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 7212 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 7213 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 7214 diag::err_omp_schedule_nonmonotonic_static); 7215 return nullptr; 7216 } 7217 Expr *ValExpr = ChunkSize; 7218 Stmt *HelperValStmt = nullptr; 7219 if (ChunkSize) { 7220 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 7221 !ChunkSize->isInstantiationDependent() && 7222 !ChunkSize->containsUnexpandedParameterPack()) { 7223 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); 7224 ExprResult Val = 7225 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 7226 if (Val.isInvalid()) 7227 return nullptr; 7228 7229 ValExpr = Val.get(); 7230 7231 // OpenMP [2.7.1, Restrictions] 7232 // chunk_size must be a loop invariant integer expression with a positive 7233 // value. 7234 llvm::APSInt Result; 7235 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 7236 if (Result.isSigned() && !Result.isStrictlyPositive()) { 7237 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 7238 << "schedule" << 1 << ChunkSize->getSourceRange(); 7239 return nullptr; 7240 } 7241 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) { 7242 llvm::MapVector<Expr *, DeclRefExpr *> Captures; 7243 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 7244 HelperValStmt = buildPreInits(Context, Captures); 7245 } 7246 } 7247 } 7248 7249 return new (Context) 7250 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 7251 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 7252 } 7253 7254 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 7255 SourceLocation StartLoc, 7256 SourceLocation EndLoc) { 7257 OMPClause *Res = nullptr; 7258 switch (Kind) { 7259 case OMPC_ordered: 7260 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 7261 break; 7262 case OMPC_nowait: 7263 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 7264 break; 7265 case OMPC_untied: 7266 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 7267 break; 7268 case OMPC_mergeable: 7269 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 7270 break; 7271 case OMPC_read: 7272 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 7273 break; 7274 case OMPC_write: 7275 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 7276 break; 7277 case OMPC_update: 7278 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 7279 break; 7280 case OMPC_capture: 7281 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 7282 break; 7283 case OMPC_seq_cst: 7284 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 7285 break; 7286 case OMPC_threads: 7287 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 7288 break; 7289 case OMPC_simd: 7290 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 7291 break; 7292 case OMPC_nogroup: 7293 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 7294 break; 7295 case OMPC_if: 7296 case OMPC_final: 7297 case OMPC_num_threads: 7298 case OMPC_safelen: 7299 case OMPC_simdlen: 7300 case OMPC_collapse: 7301 case OMPC_schedule: 7302 case OMPC_private: 7303 case OMPC_firstprivate: 7304 case OMPC_lastprivate: 7305 case OMPC_shared: 7306 case OMPC_reduction: 7307 case OMPC_linear: 7308 case OMPC_aligned: 7309 case OMPC_copyin: 7310 case OMPC_copyprivate: 7311 case OMPC_default: 7312 case OMPC_proc_bind: 7313 case OMPC_threadprivate: 7314 case OMPC_flush: 7315 case OMPC_depend: 7316 case OMPC_device: 7317 case OMPC_map: 7318 case OMPC_num_teams: 7319 case OMPC_thread_limit: 7320 case OMPC_priority: 7321 case OMPC_grainsize: 7322 case OMPC_num_tasks: 7323 case OMPC_hint: 7324 case OMPC_dist_schedule: 7325 case OMPC_defaultmap: 7326 case OMPC_unknown: 7327 case OMPC_uniform: 7328 llvm_unreachable("Clause is not allowed."); 7329 } 7330 return Res; 7331 } 7332 7333 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 7334 SourceLocation EndLoc) { 7335 DSAStack->setNowaitRegion(); 7336 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 7337 } 7338 7339 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 7340 SourceLocation EndLoc) { 7341 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 7342 } 7343 7344 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 7345 SourceLocation EndLoc) { 7346 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 7347 } 7348 7349 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 7350 SourceLocation EndLoc) { 7351 return new (Context) OMPReadClause(StartLoc, EndLoc); 7352 } 7353 7354 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 7355 SourceLocation EndLoc) { 7356 return new (Context) OMPWriteClause(StartLoc, EndLoc); 7357 } 7358 7359 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 7360 SourceLocation EndLoc) { 7361 return new (Context) OMPUpdateClause(StartLoc, EndLoc); 7362 } 7363 7364 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 7365 SourceLocation EndLoc) { 7366 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 7367 } 7368 7369 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 7370 SourceLocation EndLoc) { 7371 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 7372 } 7373 7374 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 7375 SourceLocation EndLoc) { 7376 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 7377 } 7378 7379 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 7380 SourceLocation EndLoc) { 7381 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 7382 } 7383 7384 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 7385 SourceLocation EndLoc) { 7386 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 7387 } 7388 7389 OMPClause *Sema::ActOnOpenMPVarListClause( 7390 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, 7391 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 7392 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec, 7393 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind, 7394 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier, 7395 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, 7396 SourceLocation DepLinMapLoc) { 7397 OMPClause *Res = nullptr; 7398 switch (Kind) { 7399 case OMPC_private: 7400 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 7401 break; 7402 case OMPC_firstprivate: 7403 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 7404 break; 7405 case OMPC_lastprivate: 7406 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 7407 break; 7408 case OMPC_shared: 7409 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 7410 break; 7411 case OMPC_reduction: 7412 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 7413 EndLoc, ReductionIdScopeSpec, ReductionId); 7414 break; 7415 case OMPC_linear: 7416 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, 7417 LinKind, DepLinMapLoc, ColonLoc, EndLoc); 7418 break; 7419 case OMPC_aligned: 7420 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, 7421 ColonLoc, EndLoc); 7422 break; 7423 case OMPC_copyin: 7424 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 7425 break; 7426 case OMPC_copyprivate: 7427 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 7428 break; 7429 case OMPC_flush: 7430 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 7431 break; 7432 case OMPC_depend: 7433 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList, 7434 StartLoc, LParenLoc, EndLoc); 7435 break; 7436 case OMPC_map: 7437 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit, 7438 DepLinMapLoc, ColonLoc, VarList, StartLoc, 7439 LParenLoc, EndLoc); 7440 break; 7441 case OMPC_if: 7442 case OMPC_final: 7443 case OMPC_num_threads: 7444 case OMPC_safelen: 7445 case OMPC_simdlen: 7446 case OMPC_collapse: 7447 case OMPC_default: 7448 case OMPC_proc_bind: 7449 case OMPC_schedule: 7450 case OMPC_ordered: 7451 case OMPC_nowait: 7452 case OMPC_untied: 7453 case OMPC_mergeable: 7454 case OMPC_threadprivate: 7455 case OMPC_read: 7456 case OMPC_write: 7457 case OMPC_update: 7458 case OMPC_capture: 7459 case OMPC_seq_cst: 7460 case OMPC_device: 7461 case OMPC_threads: 7462 case OMPC_simd: 7463 case OMPC_num_teams: 7464 case OMPC_thread_limit: 7465 case OMPC_priority: 7466 case OMPC_grainsize: 7467 case OMPC_nogroup: 7468 case OMPC_num_tasks: 7469 case OMPC_hint: 7470 case OMPC_dist_schedule: 7471 case OMPC_defaultmap: 7472 case OMPC_unknown: 7473 case OMPC_uniform: 7474 llvm_unreachable("Clause is not allowed."); 7475 } 7476 return Res; 7477 } 7478 7479 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 7480 ExprObjectKind OK, SourceLocation Loc) { 7481 ExprResult Res = BuildDeclRefExpr( 7482 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 7483 if (!Res.isUsable()) 7484 return ExprError(); 7485 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 7486 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 7487 if (!Res.isUsable()) 7488 return ExprError(); 7489 } 7490 if (VK != VK_LValue && Res.get()->isGLValue()) { 7491 Res = DefaultLvalueConversion(Res.get()); 7492 if (!Res.isUsable()) 7493 return ExprError(); 7494 } 7495 return Res; 7496 } 7497 7498 static std::pair<ValueDecl *, bool> 7499 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 7500 SourceRange &ERange, bool AllowArraySection = false) { 7501 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 7502 RefExpr->containsUnexpandedParameterPack()) 7503 return std::make_pair(nullptr, true); 7504 7505 // OpenMP [3.1, C/C++] 7506 // A list item is a variable name. 7507 // OpenMP [2.9.3.3, Restrictions, p.1] 7508 // A variable that is part of another variable (as an array or 7509 // structure element) cannot appear in a private clause. 7510 RefExpr = RefExpr->IgnoreParens(); 7511 enum { 7512 NoArrayExpr = -1, 7513 ArraySubscript = 0, 7514 OMPArraySection = 1 7515 } IsArrayExpr = NoArrayExpr; 7516 if (AllowArraySection) { 7517 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 7518 auto *Base = ASE->getBase()->IgnoreParenImpCasts(); 7519 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 7520 Base = TempASE->getBase()->IgnoreParenImpCasts(); 7521 RefExpr = Base; 7522 IsArrayExpr = ArraySubscript; 7523 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 7524 auto *Base = OASE->getBase()->IgnoreParenImpCasts(); 7525 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 7526 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 7527 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 7528 Base = TempASE->getBase()->IgnoreParenImpCasts(); 7529 RefExpr = Base; 7530 IsArrayExpr = OMPArraySection; 7531 } 7532 } 7533 ELoc = RefExpr->getExprLoc(); 7534 ERange = RefExpr->getSourceRange(); 7535 RefExpr = RefExpr->IgnoreParenImpCasts(); 7536 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 7537 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 7538 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 7539 (S.getCurrentThisType().isNull() || !ME || 7540 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 7541 !isa<FieldDecl>(ME->getMemberDecl()))) { 7542 if (IsArrayExpr != NoArrayExpr) 7543 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 7544 << ERange; 7545 else { 7546 S.Diag(ELoc, 7547 AllowArraySection 7548 ? diag::err_omp_expected_var_name_member_expr_or_array_item 7549 : diag::err_omp_expected_var_name_member_expr) 7550 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 7551 } 7552 return std::make_pair(nullptr, false); 7553 } 7554 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false); 7555 } 7556 7557 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 7558 SourceLocation StartLoc, 7559 SourceLocation LParenLoc, 7560 SourceLocation EndLoc) { 7561 SmallVector<Expr *, 8> Vars; 7562 SmallVector<Expr *, 8> PrivateCopies; 7563 for (auto &RefExpr : VarList) { 7564 assert(RefExpr && "NULL expr in OpenMP private clause."); 7565 SourceLocation ELoc; 7566 SourceRange ERange; 7567 Expr *SimpleRefExpr = RefExpr; 7568 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 7569 if (Res.second) { 7570 // It will be analyzed later. 7571 Vars.push_back(RefExpr); 7572 PrivateCopies.push_back(nullptr); 7573 } 7574 ValueDecl *D = Res.first; 7575 if (!D) 7576 continue; 7577 7578 QualType Type = D->getType(); 7579 auto *VD = dyn_cast<VarDecl>(D); 7580 7581 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 7582 // A variable that appears in a private clause must not have an incomplete 7583 // type or a reference type. 7584 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 7585 continue; 7586 Type = Type.getNonReferenceType(); 7587 7588 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 7589 // in a Construct] 7590 // Variables with the predetermined data-sharing attributes may not be 7591 // listed in data-sharing attributes clauses, except for the cases 7592 // listed below. For these exceptions only, listing a predetermined 7593 // variable in a data-sharing attribute clause is allowed and overrides 7594 // the variable's predetermined data-sharing attributes. 7595 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); 7596 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 7597 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 7598 << getOpenMPClauseName(OMPC_private); 7599 ReportOriginalDSA(*this, DSAStack, D, DVar); 7600 continue; 7601 } 7602 7603 // Variably modified types are not supported for tasks. 7604 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 7605 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 7606 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 7607 << getOpenMPClauseName(OMPC_private) << Type 7608 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 7609 bool IsDecl = 7610 !VD || 7611 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 7612 Diag(D->getLocation(), 7613 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 7614 << D; 7615 continue; 7616 } 7617 7618 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 7619 // A list item cannot appear in both a map clause and a data-sharing 7620 // attribute clause on the same construct 7621 if (DSAStack->getCurrentDirective() == OMPD_target) { 7622 if (DSAStack->checkMappableExprComponentListsForDecl( 7623 VD, /* CurrentRegionOnly = */ true, 7624 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef) 7625 -> bool { return true; })) { 7626 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa) 7627 << getOpenMPClauseName(OMPC_private) 7628 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 7629 ReportOriginalDSA(*this, DSAStack, D, DVar); 7630 continue; 7631 } 7632 } 7633 7634 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 7635 // A variable of class type (or array thereof) that appears in a private 7636 // clause requires an accessible, unambiguous default constructor for the 7637 // class type. 7638 // Generate helper private variable and initialize it with the default 7639 // value. The address of the original variable is replaced by the address of 7640 // the new private variable in CodeGen. This new variable is not added to 7641 // IdResolver, so the code in the OpenMP region uses original variable for 7642 // proper diagnostics. 7643 Type = Type.getUnqualifiedType(); 7644 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(), 7645 D->hasAttrs() ? &D->getAttrs() : nullptr); 7646 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false); 7647 if (VDPrivate->isInvalidDecl()) 7648 continue; 7649 auto VDPrivateRefExpr = buildDeclRefExpr( 7650 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 7651 7652 DeclRefExpr *Ref = nullptr; 7653 if (!VD) 7654 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 7655 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 7656 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref); 7657 PrivateCopies.push_back(VDPrivateRefExpr); 7658 } 7659 7660 if (Vars.empty()) 7661 return nullptr; 7662 7663 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 7664 PrivateCopies); 7665 } 7666 7667 namespace { 7668 class DiagsUninitializedSeveretyRAII { 7669 private: 7670 DiagnosticsEngine &Diags; 7671 SourceLocation SavedLoc; 7672 bool IsIgnored; 7673 7674 public: 7675 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 7676 bool IsIgnored) 7677 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 7678 if (!IsIgnored) { 7679 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 7680 /*Map*/ diag::Severity::Ignored, Loc); 7681 } 7682 } 7683 ~DiagsUninitializedSeveretyRAII() { 7684 if (!IsIgnored) 7685 Diags.popMappings(SavedLoc); 7686 } 7687 }; 7688 } 7689 7690 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 7691 SourceLocation StartLoc, 7692 SourceLocation LParenLoc, 7693 SourceLocation EndLoc) { 7694 SmallVector<Expr *, 8> Vars; 7695 SmallVector<Expr *, 8> PrivateCopies; 7696 SmallVector<Expr *, 8> Inits; 7697 SmallVector<Decl *, 4> ExprCaptures; 7698 bool IsImplicitClause = 7699 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 7700 auto ImplicitClauseLoc = DSAStack->getConstructLoc(); 7701 7702 for (auto &RefExpr : VarList) { 7703 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 7704 SourceLocation ELoc; 7705 SourceRange ERange; 7706 Expr *SimpleRefExpr = RefExpr; 7707 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 7708 if (Res.second) { 7709 // It will be analyzed later. 7710 Vars.push_back(RefExpr); 7711 PrivateCopies.push_back(nullptr); 7712 Inits.push_back(nullptr); 7713 } 7714 ValueDecl *D = Res.first; 7715 if (!D) 7716 continue; 7717 7718 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 7719 QualType Type = D->getType(); 7720 auto *VD = dyn_cast<VarDecl>(D); 7721 7722 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 7723 // A variable that appears in a private clause must not have an incomplete 7724 // type or a reference type. 7725 if (RequireCompleteType(ELoc, Type, 7726 diag::err_omp_firstprivate_incomplete_type)) 7727 continue; 7728 Type = Type.getNonReferenceType(); 7729 7730 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 7731 // A variable of class type (or array thereof) that appears in a private 7732 // clause requires an accessible, unambiguous copy constructor for the 7733 // class type. 7734 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 7735 7736 // If an implicit firstprivate variable found it was checked already. 7737 DSAStackTy::DSAVarData TopDVar; 7738 if (!IsImplicitClause) { 7739 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); 7740 TopDVar = DVar; 7741 bool IsConstant = ElemType.isConstant(Context); 7742 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 7743 // A list item that specifies a given variable may not appear in more 7744 // than one clause on the same directive, except that a variable may be 7745 // specified in both firstprivate and lastprivate clauses. 7746 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 7747 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) { 7748 Diag(ELoc, diag::err_omp_wrong_dsa) 7749 << getOpenMPClauseName(DVar.CKind) 7750 << getOpenMPClauseName(OMPC_firstprivate); 7751 ReportOriginalDSA(*this, DSAStack, D, DVar); 7752 continue; 7753 } 7754 7755 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 7756 // in a Construct] 7757 // Variables with the predetermined data-sharing attributes may not be 7758 // listed in data-sharing attributes clauses, except for the cases 7759 // listed below. For these exceptions only, listing a predetermined 7760 // variable in a data-sharing attribute clause is allowed and overrides 7761 // the variable's predetermined data-sharing attributes. 7762 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 7763 // in a Construct, C/C++, p.2] 7764 // Variables with const-qualified type having no mutable member may be 7765 // listed in a firstprivate clause, even if they are static data members. 7766 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 7767 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 7768 Diag(ELoc, diag::err_omp_wrong_dsa) 7769 << getOpenMPClauseName(DVar.CKind) 7770 << getOpenMPClauseName(OMPC_firstprivate); 7771 ReportOriginalDSA(*this, DSAStack, D, DVar); 7772 continue; 7773 } 7774 7775 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 7776 // OpenMP [2.9.3.4, Restrictions, p.2] 7777 // A list item that is private within a parallel region must not appear 7778 // in a firstprivate clause on a worksharing construct if any of the 7779 // worksharing regions arising from the worksharing construct ever bind 7780 // to any of the parallel regions arising from the parallel construct. 7781 if (isOpenMPWorksharingDirective(CurrDir) && 7782 !isOpenMPParallelDirective(CurrDir)) { 7783 DVar = DSAStack->getImplicitDSA(D, true); 7784 if (DVar.CKind != OMPC_shared && 7785 (isOpenMPParallelDirective(DVar.DKind) || 7786 DVar.DKind == OMPD_unknown)) { 7787 Diag(ELoc, diag::err_omp_required_access) 7788 << getOpenMPClauseName(OMPC_firstprivate) 7789 << getOpenMPClauseName(OMPC_shared); 7790 ReportOriginalDSA(*this, DSAStack, D, DVar); 7791 continue; 7792 } 7793 } 7794 // OpenMP [2.9.3.4, Restrictions, p.3] 7795 // A list item that appears in a reduction clause of a parallel construct 7796 // must not appear in a firstprivate clause on a worksharing or task 7797 // construct if any of the worksharing or task regions arising from the 7798 // worksharing or task construct ever bind to any of the parallel regions 7799 // arising from the parallel construct. 7800 // OpenMP [2.9.3.4, Restrictions, p.4] 7801 // A list item that appears in a reduction clause in worksharing 7802 // construct must not appear in a firstprivate clause in a task construct 7803 // encountered during execution of any of the worksharing regions arising 7804 // from the worksharing construct. 7805 if (isOpenMPTaskingDirective(CurrDir)) { 7806 DVar = DSAStack->hasInnermostDSA( 7807 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; }, 7808 [](OpenMPDirectiveKind K) -> bool { 7809 return isOpenMPParallelDirective(K) || 7810 isOpenMPWorksharingDirective(K); 7811 }, 7812 false); 7813 if (DVar.CKind == OMPC_reduction && 7814 (isOpenMPParallelDirective(DVar.DKind) || 7815 isOpenMPWorksharingDirective(DVar.DKind))) { 7816 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 7817 << getOpenMPDirectiveName(DVar.DKind); 7818 ReportOriginalDSA(*this, DSAStack, D, DVar); 7819 continue; 7820 } 7821 } 7822 7823 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 7824 // A list item that is private within a teams region must not appear in a 7825 // firstprivate clause on a distribute construct if any of the distribute 7826 // regions arising from the distribute construct ever bind to any of the 7827 // teams regions arising from the teams construct. 7828 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 7829 // A list item that appears in a reduction clause of a teams construct 7830 // must not appear in a firstprivate clause on a distribute construct if 7831 // any of the distribute regions arising from the distribute construct 7832 // ever bind to any of the teams regions arising from the teams construct. 7833 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 7834 // A list item may appear in a firstprivate or lastprivate clause but not 7835 // both. 7836 if (CurrDir == OMPD_distribute) { 7837 DVar = DSAStack->hasInnermostDSA( 7838 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; }, 7839 [](OpenMPDirectiveKind K) -> bool { 7840 return isOpenMPTeamsDirective(K); 7841 }, 7842 false); 7843 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) { 7844 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams); 7845 ReportOriginalDSA(*this, DSAStack, D, DVar); 7846 continue; 7847 } 7848 DVar = DSAStack->hasInnermostDSA( 7849 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; }, 7850 [](OpenMPDirectiveKind K) -> bool { 7851 return isOpenMPTeamsDirective(K); 7852 }, 7853 false); 7854 if (DVar.CKind == OMPC_reduction && 7855 isOpenMPTeamsDirective(DVar.DKind)) { 7856 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction); 7857 ReportOriginalDSA(*this, DSAStack, D, DVar); 7858 continue; 7859 } 7860 DVar = DSAStack->getTopDSA(D, false); 7861 if (DVar.CKind == OMPC_lastprivate) { 7862 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute); 7863 ReportOriginalDSA(*this, DSAStack, D, DVar); 7864 continue; 7865 } 7866 } 7867 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 7868 // A list item cannot appear in both a map clause and a data-sharing 7869 // attribute clause on the same construct 7870 if (CurrDir == OMPD_target) { 7871 if (DSAStack->checkMappableExprComponentListsForDecl( 7872 VD, /* CurrentRegionOnly = */ true, 7873 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef) 7874 -> bool { return true; })) { 7875 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa) 7876 << getOpenMPClauseName(OMPC_firstprivate) 7877 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 7878 ReportOriginalDSA(*this, DSAStack, D, DVar); 7879 continue; 7880 } 7881 } 7882 } 7883 7884 // Variably modified types are not supported for tasks. 7885 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 7886 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 7887 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 7888 << getOpenMPClauseName(OMPC_firstprivate) << Type 7889 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 7890 bool IsDecl = 7891 !VD || 7892 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 7893 Diag(D->getLocation(), 7894 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 7895 << D; 7896 continue; 7897 } 7898 7899 Type = Type.getUnqualifiedType(); 7900 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(), 7901 D->hasAttrs() ? &D->getAttrs() : nullptr); 7902 // Generate helper private variable and initialize it with the value of the 7903 // original variable. The address of the original variable is replaced by 7904 // the address of the new private variable in the CodeGen. This new variable 7905 // is not added to IdResolver, so the code in the OpenMP region uses 7906 // original variable for proper diagnostics and variable capturing. 7907 Expr *VDInitRefExpr = nullptr; 7908 // For arrays generate initializer for single element and replace it by the 7909 // original array element in CodeGen. 7910 if (Type->isArrayType()) { 7911 auto VDInit = 7912 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 7913 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 7914 auto Init = DefaultLvalueConversion(VDInitRefExpr).get(); 7915 ElemType = ElemType.getUnqualifiedType(); 7916 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 7917 ".firstprivate.temp"); 7918 InitializedEntity Entity = 7919 InitializedEntity::InitializeVariable(VDInitTemp); 7920 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 7921 7922 InitializationSequence InitSeq(*this, Entity, Kind, Init); 7923 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 7924 if (Result.isInvalid()) 7925 VDPrivate->setInvalidDecl(); 7926 else 7927 VDPrivate->setInit(Result.getAs<Expr>()); 7928 // Remove temp variable declaration. 7929 Context.Deallocate(VDInitTemp); 7930 } else { 7931 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 7932 ".firstprivate.temp"); 7933 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 7934 RefExpr->getExprLoc()); 7935 AddInitializerToDecl(VDPrivate, 7936 DefaultLvalueConversion(VDInitRefExpr).get(), 7937 /*DirectInit=*/false, /*TypeMayContainAuto=*/false); 7938 } 7939 if (VDPrivate->isInvalidDecl()) { 7940 if (IsImplicitClause) { 7941 Diag(RefExpr->getExprLoc(), 7942 diag::note_omp_task_predetermined_firstprivate_here); 7943 } 7944 continue; 7945 } 7946 CurContext->addDecl(VDPrivate); 7947 auto VDPrivateRefExpr = buildDeclRefExpr( 7948 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 7949 RefExpr->getExprLoc()); 7950 DeclRefExpr *Ref = nullptr; 7951 if (!VD) { 7952 if (TopDVar.CKind == OMPC_lastprivate) 7953 Ref = TopDVar.PrivateCopy; 7954 else { 7955 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 7956 if (!IsOpenMPCapturedDecl(D)) 7957 ExprCaptures.push_back(Ref->getDecl()); 7958 } 7959 } 7960 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 7961 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref); 7962 PrivateCopies.push_back(VDPrivateRefExpr); 7963 Inits.push_back(VDInitRefExpr); 7964 } 7965 7966 if (Vars.empty()) 7967 return nullptr; 7968 7969 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 7970 Vars, PrivateCopies, Inits, 7971 buildPreInits(Context, ExprCaptures)); 7972 } 7973 7974 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList, 7975 SourceLocation StartLoc, 7976 SourceLocation LParenLoc, 7977 SourceLocation EndLoc) { 7978 SmallVector<Expr *, 8> Vars; 7979 SmallVector<Expr *, 8> SrcExprs; 7980 SmallVector<Expr *, 8> DstExprs; 7981 SmallVector<Expr *, 8> AssignmentOps; 7982 SmallVector<Decl *, 4> ExprCaptures; 7983 SmallVector<Expr *, 4> ExprPostUpdates; 7984 for (auto &RefExpr : VarList) { 7985 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 7986 SourceLocation ELoc; 7987 SourceRange ERange; 7988 Expr *SimpleRefExpr = RefExpr; 7989 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 7990 if (Res.second) { 7991 // It will be analyzed later. 7992 Vars.push_back(RefExpr); 7993 SrcExprs.push_back(nullptr); 7994 DstExprs.push_back(nullptr); 7995 AssignmentOps.push_back(nullptr); 7996 } 7997 ValueDecl *D = Res.first; 7998 if (!D) 7999 continue; 8000 8001 QualType Type = D->getType(); 8002 auto *VD = dyn_cast<VarDecl>(D); 8003 8004 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 8005 // A variable that appears in a lastprivate clause must not have an 8006 // incomplete type or a reference type. 8007 if (RequireCompleteType(ELoc, Type, 8008 diag::err_omp_lastprivate_incomplete_type)) 8009 continue; 8010 Type = Type.getNonReferenceType(); 8011 8012 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 8013 // in a Construct] 8014 // Variables with the predetermined data-sharing attributes may not be 8015 // listed in data-sharing attributes clauses, except for the cases 8016 // listed below. 8017 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); 8018 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 8019 DVar.CKind != OMPC_firstprivate && 8020 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 8021 Diag(ELoc, diag::err_omp_wrong_dsa) 8022 << getOpenMPClauseName(DVar.CKind) 8023 << getOpenMPClauseName(OMPC_lastprivate); 8024 ReportOriginalDSA(*this, DSAStack, D, DVar); 8025 continue; 8026 } 8027 8028 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 8029 // OpenMP [2.14.3.5, Restrictions, p.2] 8030 // A list item that is private within a parallel region, or that appears in 8031 // the reduction clause of a parallel construct, must not appear in a 8032 // lastprivate clause on a worksharing construct if any of the corresponding 8033 // worksharing regions ever binds to any of the corresponding parallel 8034 // regions. 8035 DSAStackTy::DSAVarData TopDVar = DVar; 8036 if (isOpenMPWorksharingDirective(CurrDir) && 8037 !isOpenMPParallelDirective(CurrDir)) { 8038 DVar = DSAStack->getImplicitDSA(D, true); 8039 if (DVar.CKind != OMPC_shared) { 8040 Diag(ELoc, diag::err_omp_required_access) 8041 << getOpenMPClauseName(OMPC_lastprivate) 8042 << getOpenMPClauseName(OMPC_shared); 8043 ReportOriginalDSA(*this, DSAStack, D, DVar); 8044 continue; 8045 } 8046 } 8047 8048 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 8049 // A list item may appear in a firstprivate or lastprivate clause but not 8050 // both. 8051 if (CurrDir == OMPD_distribute) { 8052 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); 8053 if (DVar.CKind == OMPC_firstprivate) { 8054 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute); 8055 ReportOriginalDSA(*this, DSAStack, D, DVar); 8056 continue; 8057 } 8058 } 8059 8060 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 8061 // A variable of class type (or array thereof) that appears in a 8062 // lastprivate clause requires an accessible, unambiguous default 8063 // constructor for the class type, unless the list item is also specified 8064 // in a firstprivate clause. 8065 // A variable of class type (or array thereof) that appears in a 8066 // lastprivate clause requires an accessible, unambiguous copy assignment 8067 // operator for the class type. 8068 Type = Context.getBaseElementType(Type).getNonReferenceType(); 8069 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(), 8070 Type.getUnqualifiedType(), ".lastprivate.src", 8071 D->hasAttrs() ? &D->getAttrs() : nullptr); 8072 auto *PseudoSrcExpr = 8073 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 8074 auto *DstVD = 8075 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 8076 D->hasAttrs() ? &D->getAttrs() : nullptr); 8077 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 8078 // For arrays generate assignment operation for single element and replace 8079 // it by the original array element in CodeGen. 8080 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 8081 PseudoDstExpr, PseudoSrcExpr); 8082 if (AssignmentOp.isInvalid()) 8083 continue; 8084 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc, 8085 /*DiscardedValue=*/true); 8086 if (AssignmentOp.isInvalid()) 8087 continue; 8088 8089 DeclRefExpr *Ref = nullptr; 8090 if (!VD) { 8091 if (TopDVar.CKind == OMPC_firstprivate) 8092 Ref = TopDVar.PrivateCopy; 8093 else { 8094 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 8095 if (!IsOpenMPCapturedDecl(D)) 8096 ExprCaptures.push_back(Ref->getDecl()); 8097 } 8098 if (TopDVar.CKind == OMPC_firstprivate || 8099 (!IsOpenMPCapturedDecl(D) && 8100 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 8101 ExprResult RefRes = DefaultLvalueConversion(Ref); 8102 if (!RefRes.isUsable()) 8103 continue; 8104 ExprResult PostUpdateRes = 8105 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 8106 RefRes.get()); 8107 if (!PostUpdateRes.isUsable()) 8108 continue; 8109 ExprPostUpdates.push_back( 8110 IgnoredValueConversions(PostUpdateRes.get()).get()); 8111 } 8112 } 8113 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 8114 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref); 8115 SrcExprs.push_back(PseudoSrcExpr); 8116 DstExprs.push_back(PseudoDstExpr); 8117 AssignmentOps.push_back(AssignmentOp.get()); 8118 } 8119 8120 if (Vars.empty()) 8121 return nullptr; 8122 8123 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 8124 Vars, SrcExprs, DstExprs, AssignmentOps, 8125 buildPreInits(Context, ExprCaptures), 8126 buildPostUpdate(*this, ExprPostUpdates)); 8127 } 8128 8129 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 8130 SourceLocation StartLoc, 8131 SourceLocation LParenLoc, 8132 SourceLocation EndLoc) { 8133 SmallVector<Expr *, 8> Vars; 8134 for (auto &RefExpr : VarList) { 8135 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 8136 SourceLocation ELoc; 8137 SourceRange ERange; 8138 Expr *SimpleRefExpr = RefExpr; 8139 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 8140 if (Res.second) { 8141 // It will be analyzed later. 8142 Vars.push_back(RefExpr); 8143 } 8144 ValueDecl *D = Res.first; 8145 if (!D) 8146 continue; 8147 8148 auto *VD = dyn_cast<VarDecl>(D); 8149 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 8150 // in a Construct] 8151 // Variables with the predetermined data-sharing attributes may not be 8152 // listed in data-sharing attributes clauses, except for the cases 8153 // listed below. For these exceptions only, listing a predetermined 8154 // variable in a data-sharing attribute clause is allowed and overrides 8155 // the variable's predetermined data-sharing attributes. 8156 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); 8157 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 8158 DVar.RefExpr) { 8159 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 8160 << getOpenMPClauseName(OMPC_shared); 8161 ReportOriginalDSA(*this, DSAStack, D, DVar); 8162 continue; 8163 } 8164 8165 DeclRefExpr *Ref = nullptr; 8166 if (!VD && IsOpenMPCapturedDecl(D)) 8167 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 8168 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 8169 Vars.push_back((VD || !Ref) ? RefExpr->IgnoreParens() : Ref); 8170 } 8171 8172 if (Vars.empty()) 8173 return nullptr; 8174 8175 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 8176 } 8177 8178 namespace { 8179 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 8180 DSAStackTy *Stack; 8181 8182 public: 8183 bool VisitDeclRefExpr(DeclRefExpr *E) { 8184 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) { 8185 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false); 8186 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 8187 return false; 8188 if (DVar.CKind != OMPC_unknown) 8189 return true; 8190 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 8191 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; }, 8192 false); 8193 if (DVarPrivate.CKind != OMPC_unknown) 8194 return true; 8195 return false; 8196 } 8197 return false; 8198 } 8199 bool VisitStmt(Stmt *S) { 8200 for (auto Child : S->children()) { 8201 if (Child && Visit(Child)) 8202 return true; 8203 } 8204 return false; 8205 } 8206 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 8207 }; 8208 } // namespace 8209 8210 namespace { 8211 // Transform MemberExpression for specified FieldDecl of current class to 8212 // DeclRefExpr to specified OMPCapturedExprDecl. 8213 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 8214 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 8215 ValueDecl *Field; 8216 DeclRefExpr *CapturedExpr; 8217 8218 public: 8219 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 8220 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 8221 8222 ExprResult TransformMemberExpr(MemberExpr *E) { 8223 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 8224 E->getMemberDecl() == Field) { 8225 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 8226 return CapturedExpr; 8227 } 8228 return BaseTransform::TransformMemberExpr(E); 8229 } 8230 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 8231 }; 8232 } // namespace 8233 8234 template <typename T> 8235 static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups, 8236 const llvm::function_ref<T(ValueDecl *)> &Gen) { 8237 for (auto &Set : Lookups) { 8238 for (auto *D : Set) { 8239 if (auto Res = Gen(cast<ValueDecl>(D))) 8240 return Res; 8241 } 8242 } 8243 return T(); 8244 } 8245 8246 static ExprResult 8247 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 8248 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 8249 const DeclarationNameInfo &ReductionId, QualType Ty, 8250 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 8251 if (ReductionIdScopeSpec.isInvalid()) 8252 return ExprError(); 8253 SmallVector<UnresolvedSet<8>, 4> Lookups; 8254 if (S) { 8255 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 8256 Lookup.suppressDiagnostics(); 8257 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 8258 auto *D = Lookup.getRepresentativeDecl(); 8259 do { 8260 S = S->getParent(); 8261 } while (S && !S->isDeclScope(D)); 8262 if (S) 8263 S = S->getParent(); 8264 Lookups.push_back(UnresolvedSet<8>()); 8265 Lookups.back().append(Lookup.begin(), Lookup.end()); 8266 Lookup.clear(); 8267 } 8268 } else if (auto *ULE = 8269 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 8270 Lookups.push_back(UnresolvedSet<8>()); 8271 Decl *PrevD = nullptr; 8272 for(auto *D : ULE->decls()) { 8273 if (D == PrevD) 8274 Lookups.push_back(UnresolvedSet<8>()); 8275 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D)) 8276 Lookups.back().addDecl(DRD); 8277 PrevD = D; 8278 } 8279 } 8280 if (Ty->isDependentType() || Ty->isInstantiationDependentType() || 8281 Ty->containsUnexpandedParameterPack() || 8282 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool { 8283 return !D->isInvalidDecl() && 8284 (D->getType()->isDependentType() || 8285 D->getType()->isInstantiationDependentType() || 8286 D->getType()->containsUnexpandedParameterPack()); 8287 })) { 8288 UnresolvedSet<8> ResSet; 8289 for (auto &Set : Lookups) { 8290 ResSet.append(Set.begin(), Set.end()); 8291 // The last item marks the end of all declarations at the specified scope. 8292 ResSet.addDecl(Set[Set.size() - 1]); 8293 } 8294 return UnresolvedLookupExpr::Create( 8295 SemaRef.Context, /*NamingClass=*/nullptr, 8296 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 8297 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 8298 } 8299 if (auto *VD = filterLookupForUDR<ValueDecl *>( 8300 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 8301 if (!D->isInvalidDecl() && 8302 SemaRef.Context.hasSameType(D->getType(), Ty)) 8303 return D; 8304 return nullptr; 8305 })) 8306 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc); 8307 if (auto *VD = filterLookupForUDR<ValueDecl *>( 8308 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 8309 if (!D->isInvalidDecl() && 8310 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 8311 !Ty.isMoreQualifiedThan(D->getType())) 8312 return D; 8313 return nullptr; 8314 })) { 8315 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 8316 /*DetectVirtual=*/false); 8317 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 8318 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 8319 VD->getType().getUnqualifiedType()))) { 8320 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(), 8321 /*DiagID=*/0) != 8322 Sema::AR_inaccessible) { 8323 SemaRef.BuildBasePathArray(Paths, BasePath); 8324 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc); 8325 } 8326 } 8327 } 8328 } 8329 if (ReductionIdScopeSpec.isSet()) { 8330 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range; 8331 return ExprError(); 8332 } 8333 return ExprEmpty(); 8334 } 8335 8336 OMPClause *Sema::ActOnOpenMPReductionClause( 8337 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 8338 SourceLocation ColonLoc, SourceLocation EndLoc, 8339 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 8340 ArrayRef<Expr *> UnresolvedReductions) { 8341 auto DN = ReductionId.getName(); 8342 auto OOK = DN.getCXXOverloadedOperator(); 8343 BinaryOperatorKind BOK = BO_Comma; 8344 8345 // OpenMP [2.14.3.6, reduction clause] 8346 // C 8347 // reduction-identifier is either an identifier or one of the following 8348 // operators: +, -, *, &, |, ^, && and || 8349 // C++ 8350 // reduction-identifier is either an id-expression or one of the following 8351 // operators: +, -, *, &, |, ^, && and || 8352 // FIXME: Only 'min' and 'max' identifiers are supported for now. 8353 switch (OOK) { 8354 case OO_Plus: 8355 case OO_Minus: 8356 BOK = BO_Add; 8357 break; 8358 case OO_Star: 8359 BOK = BO_Mul; 8360 break; 8361 case OO_Amp: 8362 BOK = BO_And; 8363 break; 8364 case OO_Pipe: 8365 BOK = BO_Or; 8366 break; 8367 case OO_Caret: 8368 BOK = BO_Xor; 8369 break; 8370 case OO_AmpAmp: 8371 BOK = BO_LAnd; 8372 break; 8373 case OO_PipePipe: 8374 BOK = BO_LOr; 8375 break; 8376 case OO_New: 8377 case OO_Delete: 8378 case OO_Array_New: 8379 case OO_Array_Delete: 8380 case OO_Slash: 8381 case OO_Percent: 8382 case OO_Tilde: 8383 case OO_Exclaim: 8384 case OO_Equal: 8385 case OO_Less: 8386 case OO_Greater: 8387 case OO_LessEqual: 8388 case OO_GreaterEqual: 8389 case OO_PlusEqual: 8390 case OO_MinusEqual: 8391 case OO_StarEqual: 8392 case OO_SlashEqual: 8393 case OO_PercentEqual: 8394 case OO_CaretEqual: 8395 case OO_AmpEqual: 8396 case OO_PipeEqual: 8397 case OO_LessLess: 8398 case OO_GreaterGreater: 8399 case OO_LessLessEqual: 8400 case OO_GreaterGreaterEqual: 8401 case OO_EqualEqual: 8402 case OO_ExclaimEqual: 8403 case OO_PlusPlus: 8404 case OO_MinusMinus: 8405 case OO_Comma: 8406 case OO_ArrowStar: 8407 case OO_Arrow: 8408 case OO_Call: 8409 case OO_Subscript: 8410 case OO_Conditional: 8411 case OO_Coawait: 8412 case NUM_OVERLOADED_OPERATORS: 8413 llvm_unreachable("Unexpected reduction identifier"); 8414 case OO_None: 8415 if (auto II = DN.getAsIdentifierInfo()) { 8416 if (II->isStr("max")) 8417 BOK = BO_GT; 8418 else if (II->isStr("min")) 8419 BOK = BO_LT; 8420 } 8421 break; 8422 } 8423 SourceRange ReductionIdRange; 8424 if (ReductionIdScopeSpec.isValid()) 8425 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 8426 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 8427 8428 SmallVector<Expr *, 8> Vars; 8429 SmallVector<Expr *, 8> Privates; 8430 SmallVector<Expr *, 8> LHSs; 8431 SmallVector<Expr *, 8> RHSs; 8432 SmallVector<Expr *, 8> ReductionOps; 8433 SmallVector<Decl *, 4> ExprCaptures; 8434 SmallVector<Expr *, 4> ExprPostUpdates; 8435 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 8436 bool FirstIter = true; 8437 for (auto RefExpr : VarList) { 8438 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 8439 // OpenMP [2.1, C/C++] 8440 // A list item is a variable or array section, subject to the restrictions 8441 // specified in Section 2.4 on page 42 and in each of the sections 8442 // describing clauses and directives for which a list appears. 8443 // OpenMP [2.14.3.3, Restrictions, p.1] 8444 // A variable that is part of another variable (as an array or 8445 // structure element) cannot appear in a private clause. 8446 if (!FirstIter && IR != ER) 8447 ++IR; 8448 FirstIter = false; 8449 SourceLocation ELoc; 8450 SourceRange ERange; 8451 Expr *SimpleRefExpr = RefExpr; 8452 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 8453 /*AllowArraySection=*/true); 8454 if (Res.second) { 8455 // It will be analyzed later. 8456 Vars.push_back(RefExpr); 8457 Privates.push_back(nullptr); 8458 LHSs.push_back(nullptr); 8459 RHSs.push_back(nullptr); 8460 // Try to find 'declare reduction' corresponding construct before using 8461 // builtin/overloaded operators. 8462 QualType Type = Context.DependentTy; 8463 CXXCastPath BasePath; 8464 ExprResult DeclareReductionRef = buildDeclareReductionRef( 8465 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec, 8466 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 8467 if (CurContext->isDependentContext() && 8468 (DeclareReductionRef.isUnset() || 8469 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 8470 ReductionOps.push_back(DeclareReductionRef.get()); 8471 else 8472 ReductionOps.push_back(nullptr); 8473 } 8474 ValueDecl *D = Res.first; 8475 if (!D) 8476 continue; 8477 8478 QualType Type; 8479 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 8480 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 8481 if (ASE) 8482 Type = ASE->getType().getNonReferenceType(); 8483 else if (OASE) { 8484 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 8485 if (auto *ATy = BaseType->getAsArrayTypeUnsafe()) 8486 Type = ATy->getElementType(); 8487 else 8488 Type = BaseType->getPointeeType(); 8489 Type = Type.getNonReferenceType(); 8490 } else 8491 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 8492 auto *VD = dyn_cast<VarDecl>(D); 8493 8494 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 8495 // A variable that appears in a private clause must not have an incomplete 8496 // type or a reference type. 8497 if (RequireCompleteType(ELoc, Type, 8498 diag::err_omp_reduction_incomplete_type)) 8499 continue; 8500 // OpenMP [2.14.3.6, reduction clause, Restrictions] 8501 // A list item that appears in a reduction clause must not be 8502 // const-qualified. 8503 if (Type.getNonReferenceType().isConstant(Context)) { 8504 Diag(ELoc, diag::err_omp_const_reduction_list_item) 8505 << getOpenMPClauseName(OMPC_reduction) << Type << ERange; 8506 if (!ASE && !OASE) { 8507 bool IsDecl = !VD || 8508 VD->isThisDeclarationADefinition(Context) == 8509 VarDecl::DeclarationOnly; 8510 Diag(D->getLocation(), 8511 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 8512 << D; 8513 } 8514 continue; 8515 } 8516 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 8517 // If a list-item is a reference type then it must bind to the same object 8518 // for all threads of the team. 8519 if (!ASE && !OASE && VD) { 8520 VarDecl *VDDef = VD->getDefinition(); 8521 if (VD->getType()->isReferenceType() && VDDef) { 8522 DSARefChecker Check(DSAStack); 8523 if (Check.Visit(VDDef->getInit())) { 8524 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange; 8525 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 8526 continue; 8527 } 8528 } 8529 } 8530 8531 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 8532 // in a Construct] 8533 // Variables with the predetermined data-sharing attributes may not be 8534 // listed in data-sharing attributes clauses, except for the cases 8535 // listed below. For these exceptions only, listing a predetermined 8536 // variable in a data-sharing attribute clause is allowed and overrides 8537 // the variable's predetermined data-sharing attributes. 8538 // OpenMP [2.14.3.6, Restrictions, p.3] 8539 // Any number of reduction clauses can be specified on the directive, 8540 // but a list item can appear only once in the reduction clauses for that 8541 // directive. 8542 DSAStackTy::DSAVarData DVar; 8543 DVar = DSAStack->getTopDSA(D, false); 8544 if (DVar.CKind == OMPC_reduction) { 8545 Diag(ELoc, diag::err_omp_once_referenced) 8546 << getOpenMPClauseName(OMPC_reduction); 8547 if (DVar.RefExpr) 8548 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 8549 } else if (DVar.CKind != OMPC_unknown) { 8550 Diag(ELoc, diag::err_omp_wrong_dsa) 8551 << getOpenMPClauseName(DVar.CKind) 8552 << getOpenMPClauseName(OMPC_reduction); 8553 ReportOriginalDSA(*this, DSAStack, D, DVar); 8554 continue; 8555 } 8556 8557 // OpenMP [2.14.3.6, Restrictions, p.1] 8558 // A list item that appears in a reduction clause of a worksharing 8559 // construct must be shared in the parallel regions to which any of the 8560 // worksharing regions arising from the worksharing construct bind. 8561 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 8562 if (isOpenMPWorksharingDirective(CurrDir) && 8563 !isOpenMPParallelDirective(CurrDir)) { 8564 DVar = DSAStack->getImplicitDSA(D, true); 8565 if (DVar.CKind != OMPC_shared) { 8566 Diag(ELoc, diag::err_omp_required_access) 8567 << getOpenMPClauseName(OMPC_reduction) 8568 << getOpenMPClauseName(OMPC_shared); 8569 ReportOriginalDSA(*this, DSAStack, D, DVar); 8570 continue; 8571 } 8572 } 8573 8574 // Try to find 'declare reduction' corresponding construct before using 8575 // builtin/overloaded operators. 8576 CXXCastPath BasePath; 8577 ExprResult DeclareReductionRef = buildDeclareReductionRef( 8578 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec, 8579 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 8580 if (DeclareReductionRef.isInvalid()) 8581 continue; 8582 if (CurContext->isDependentContext() && 8583 (DeclareReductionRef.isUnset() || 8584 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 8585 Vars.push_back(RefExpr); 8586 Privates.push_back(nullptr); 8587 LHSs.push_back(nullptr); 8588 RHSs.push_back(nullptr); 8589 ReductionOps.push_back(DeclareReductionRef.get()); 8590 continue; 8591 } 8592 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 8593 // Not allowed reduction identifier is found. 8594 Diag(ReductionId.getLocStart(), 8595 diag::err_omp_unknown_reduction_identifier) 8596 << Type << ReductionIdRange; 8597 continue; 8598 } 8599 8600 // OpenMP [2.14.3.6, reduction clause, Restrictions] 8601 // The type of a list item that appears in a reduction clause must be valid 8602 // for the reduction-identifier. For a max or min reduction in C, the type 8603 // of the list item must be an allowed arithmetic data type: char, int, 8604 // float, double, or _Bool, possibly modified with long, short, signed, or 8605 // unsigned. For a max or min reduction in C++, the type of the list item 8606 // must be an allowed arithmetic data type: char, wchar_t, int, float, 8607 // double, or bool, possibly modified with long, short, signed, or unsigned. 8608 if (DeclareReductionRef.isUnset()) { 8609 if ((BOK == BO_GT || BOK == BO_LT) && 8610 !(Type->isScalarType() || 8611 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 8612 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 8613 << getLangOpts().CPlusPlus; 8614 if (!ASE && !OASE) { 8615 bool IsDecl = !VD || 8616 VD->isThisDeclarationADefinition(Context) == 8617 VarDecl::DeclarationOnly; 8618 Diag(D->getLocation(), 8619 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 8620 << D; 8621 } 8622 continue; 8623 } 8624 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 8625 !getLangOpts().CPlusPlus && Type->isFloatingType()) { 8626 Diag(ELoc, diag::err_omp_clause_floating_type_arg); 8627 if (!ASE && !OASE) { 8628 bool IsDecl = !VD || 8629 VD->isThisDeclarationADefinition(Context) == 8630 VarDecl::DeclarationOnly; 8631 Diag(D->getLocation(), 8632 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 8633 << D; 8634 } 8635 continue; 8636 } 8637 } 8638 8639 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 8640 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs", 8641 D->hasAttrs() ? &D->getAttrs() : nullptr); 8642 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(), 8643 D->hasAttrs() ? &D->getAttrs() : nullptr); 8644 auto PrivateTy = Type; 8645 if (OASE || 8646 (!ASE && 8647 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 8648 // For arays/array sections only: 8649 // Create pseudo array type for private copy. The size for this array will 8650 // be generated during codegen. 8651 // For array subscripts or single variables Private Ty is the same as Type 8652 // (type of the variable or single array element). 8653 PrivateTy = Context.getVariableArrayType( 8654 Type, new (Context) OpaqueValueExpr(SourceLocation(), 8655 Context.getSizeType(), VK_RValue), 8656 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 8657 } else if (!ASE && !OASE && 8658 Context.getAsArrayType(D->getType().getNonReferenceType())) 8659 PrivateTy = D->getType().getNonReferenceType(); 8660 // Private copy. 8661 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(), 8662 D->hasAttrs() ? &D->getAttrs() : nullptr); 8663 // Add initializer for private variable. 8664 Expr *Init = nullptr; 8665 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc); 8666 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc); 8667 if (DeclareReductionRef.isUsable()) { 8668 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 8669 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 8670 if (DRD->getInitializer()) { 8671 Init = DRDRef; 8672 RHSVD->setInit(DRDRef); 8673 RHSVD->setInitStyle(VarDecl::CallInit); 8674 } 8675 } else { 8676 switch (BOK) { 8677 case BO_Add: 8678 case BO_Xor: 8679 case BO_Or: 8680 case BO_LOr: 8681 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 8682 if (Type->isScalarType() || Type->isAnyComplexType()) 8683 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 8684 break; 8685 case BO_Mul: 8686 case BO_LAnd: 8687 if (Type->isScalarType() || Type->isAnyComplexType()) { 8688 // '*' and '&&' reduction ops - initializer is '1'. 8689 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 8690 } 8691 break; 8692 case BO_And: { 8693 // '&' reduction op - initializer is '~0'. 8694 QualType OrigType = Type; 8695 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 8696 Type = ComplexTy->getElementType(); 8697 if (Type->isRealFloatingType()) { 8698 llvm::APFloat InitValue = 8699 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type), 8700 /*isIEEE=*/true); 8701 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 8702 Type, ELoc); 8703 } else if (Type->isScalarType()) { 8704 auto Size = Context.getTypeSize(Type); 8705 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 8706 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 8707 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 8708 } 8709 if (Init && OrigType->isAnyComplexType()) { 8710 // Init = 0xFFFF + 0xFFFFi; 8711 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 8712 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 8713 } 8714 Type = OrigType; 8715 break; 8716 } 8717 case BO_LT: 8718 case BO_GT: { 8719 // 'min' reduction op - initializer is 'Largest representable number in 8720 // the reduction list item type'. 8721 // 'max' reduction op - initializer is 'Least representable number in 8722 // the reduction list item type'. 8723 if (Type->isIntegerType() || Type->isPointerType()) { 8724 bool IsSigned = Type->hasSignedIntegerRepresentation(); 8725 auto Size = Context.getTypeSize(Type); 8726 QualType IntTy = 8727 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 8728 llvm::APInt InitValue = 8729 (BOK != BO_LT) 8730 ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 8731 : llvm::APInt::getMinValue(Size) 8732 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 8733 : llvm::APInt::getMaxValue(Size); 8734 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 8735 if (Type->isPointerType()) { 8736 // Cast to pointer type. 8737 auto CastExpr = BuildCStyleCastExpr( 8738 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc), 8739 SourceLocation(), Init); 8740 if (CastExpr.isInvalid()) 8741 continue; 8742 Init = CastExpr.get(); 8743 } 8744 } else if (Type->isRealFloatingType()) { 8745 llvm::APFloat InitValue = llvm::APFloat::getLargest( 8746 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 8747 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 8748 Type, ELoc); 8749 } 8750 break; 8751 } 8752 case BO_PtrMemD: 8753 case BO_PtrMemI: 8754 case BO_MulAssign: 8755 case BO_Div: 8756 case BO_Rem: 8757 case BO_Sub: 8758 case BO_Shl: 8759 case BO_Shr: 8760 case BO_LE: 8761 case BO_GE: 8762 case BO_EQ: 8763 case BO_NE: 8764 case BO_AndAssign: 8765 case BO_XorAssign: 8766 case BO_OrAssign: 8767 case BO_Assign: 8768 case BO_AddAssign: 8769 case BO_SubAssign: 8770 case BO_DivAssign: 8771 case BO_RemAssign: 8772 case BO_ShlAssign: 8773 case BO_ShrAssign: 8774 case BO_Comma: 8775 llvm_unreachable("Unexpected reduction operation"); 8776 } 8777 } 8778 if (Init && DeclareReductionRef.isUnset()) { 8779 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false, 8780 /*TypeMayContainAuto=*/false); 8781 } else if (!Init) 8782 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false); 8783 if (RHSVD->isInvalidDecl()) 8784 continue; 8785 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) { 8786 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type 8787 << ReductionIdRange; 8788 bool IsDecl = 8789 !VD || 8790 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 8791 Diag(D->getLocation(), 8792 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 8793 << D; 8794 continue; 8795 } 8796 // Store initializer for single element in private copy. Will be used during 8797 // codegen. 8798 PrivateVD->setInit(RHSVD->getInit()); 8799 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 8800 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc); 8801 ExprResult ReductionOp; 8802 if (DeclareReductionRef.isUsable()) { 8803 QualType RedTy = DeclareReductionRef.get()->getType(); 8804 QualType PtrRedTy = Context.getPointerType(RedTy); 8805 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 8806 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 8807 if (!BasePath.empty()) { 8808 LHS = DefaultLvalueConversion(LHS.get()); 8809 RHS = DefaultLvalueConversion(RHS.get()); 8810 LHS = ImplicitCastExpr::Create(Context, PtrRedTy, 8811 CK_UncheckedDerivedToBase, LHS.get(), 8812 &BasePath, LHS.get()->getValueKind()); 8813 RHS = ImplicitCastExpr::Create(Context, PtrRedTy, 8814 CK_UncheckedDerivedToBase, RHS.get(), 8815 &BasePath, RHS.get()->getValueKind()); 8816 } 8817 FunctionProtoType::ExtProtoInfo EPI; 8818 QualType Params[] = {PtrRedTy, PtrRedTy}; 8819 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 8820 auto *OVE = new (Context) OpaqueValueExpr( 8821 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, 8822 DefaultLvalueConversion(DeclareReductionRef.get()).get()); 8823 Expr *Args[] = {LHS.get(), RHS.get()}; 8824 ReductionOp = new (Context) 8825 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc); 8826 } else { 8827 ReductionOp = BuildBinOp(DSAStack->getCurScope(), 8828 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE); 8829 if (ReductionOp.isUsable()) { 8830 if (BOK != BO_LT && BOK != BO_GT) { 8831 ReductionOp = 8832 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), 8833 BO_Assign, LHSDRE, ReductionOp.get()); 8834 } else { 8835 auto *ConditionalOp = new (Context) ConditionalOperator( 8836 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(), 8837 RHSDRE, Type, VK_LValue, OK_Ordinary); 8838 ReductionOp = 8839 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), 8840 BO_Assign, LHSDRE, ConditionalOp); 8841 } 8842 ReductionOp = ActOnFinishFullExpr(ReductionOp.get()); 8843 } 8844 if (ReductionOp.isInvalid()) 8845 continue; 8846 } 8847 8848 DeclRefExpr *Ref = nullptr; 8849 Expr *VarsExpr = RefExpr->IgnoreParens(); 8850 if (!VD) { 8851 if (ASE || OASE) { 8852 TransformExprToCaptures RebuildToCapture(*this, D); 8853 VarsExpr = 8854 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 8855 Ref = RebuildToCapture.getCapturedExpr(); 8856 } else { 8857 VarsExpr = Ref = 8858 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 8859 } 8860 if (!IsOpenMPCapturedDecl(D)) { 8861 ExprCaptures.push_back(Ref->getDecl()); 8862 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 8863 ExprResult RefRes = DefaultLvalueConversion(Ref); 8864 if (!RefRes.isUsable()) 8865 continue; 8866 ExprResult PostUpdateRes = 8867 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 8868 SimpleRefExpr, RefRes.get()); 8869 if (!PostUpdateRes.isUsable()) 8870 continue; 8871 ExprPostUpdates.push_back( 8872 IgnoredValueConversions(PostUpdateRes.get()).get()); 8873 } 8874 } 8875 } 8876 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref); 8877 Vars.push_back(VarsExpr); 8878 Privates.push_back(PrivateDRE); 8879 LHSs.push_back(LHSDRE); 8880 RHSs.push_back(RHSDRE); 8881 ReductionOps.push_back(ReductionOp.get()); 8882 } 8883 8884 if (Vars.empty()) 8885 return nullptr; 8886 8887 return OMPReductionClause::Create( 8888 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars, 8889 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates, 8890 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures), 8891 buildPostUpdate(*this, ExprPostUpdates)); 8892 } 8893 8894 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 8895 SourceLocation LinLoc) { 8896 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 8897 LinKind == OMPC_LINEAR_unknown) { 8898 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 8899 return true; 8900 } 8901 return false; 8902 } 8903 8904 bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc, 8905 OpenMPLinearClauseKind LinKind, 8906 QualType Type) { 8907 auto *VD = dyn_cast_or_null<VarDecl>(D); 8908 // A variable must not have an incomplete type or a reference type. 8909 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 8910 return true; 8911 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 8912 !Type->isReferenceType()) { 8913 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 8914 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 8915 return true; 8916 } 8917 Type = Type.getNonReferenceType(); 8918 8919 // A list item must not be const-qualified. 8920 if (Type.isConstant(Context)) { 8921 Diag(ELoc, diag::err_omp_const_variable) 8922 << getOpenMPClauseName(OMPC_linear); 8923 if (D) { 8924 bool IsDecl = 8925 !VD || 8926 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 8927 Diag(D->getLocation(), 8928 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 8929 << D; 8930 } 8931 return true; 8932 } 8933 8934 // A list item must be of integral or pointer type. 8935 Type = Type.getUnqualifiedType().getCanonicalType(); 8936 const auto *Ty = Type.getTypePtrOrNull(); 8937 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && 8938 !Ty->isPointerType())) { 8939 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 8940 if (D) { 8941 bool IsDecl = 8942 !VD || 8943 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 8944 Diag(D->getLocation(), 8945 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 8946 << D; 8947 } 8948 return true; 8949 } 8950 return false; 8951 } 8952 8953 OMPClause *Sema::ActOnOpenMPLinearClause( 8954 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 8955 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 8956 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 8957 SmallVector<Expr *, 8> Vars; 8958 SmallVector<Expr *, 8> Privates; 8959 SmallVector<Expr *, 8> Inits; 8960 SmallVector<Decl *, 4> ExprCaptures; 8961 SmallVector<Expr *, 4> ExprPostUpdates; 8962 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 8963 LinKind = OMPC_LINEAR_val; 8964 for (auto &RefExpr : VarList) { 8965 assert(RefExpr && "NULL expr in OpenMP linear clause."); 8966 SourceLocation ELoc; 8967 SourceRange ERange; 8968 Expr *SimpleRefExpr = RefExpr; 8969 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 8970 /*AllowArraySection=*/false); 8971 if (Res.second) { 8972 // It will be analyzed later. 8973 Vars.push_back(RefExpr); 8974 Privates.push_back(nullptr); 8975 Inits.push_back(nullptr); 8976 } 8977 ValueDecl *D = Res.first; 8978 if (!D) 8979 continue; 8980 8981 QualType Type = D->getType(); 8982 auto *VD = dyn_cast<VarDecl>(D); 8983 8984 // OpenMP [2.14.3.7, linear clause] 8985 // A list-item cannot appear in more than one linear clause. 8986 // A list-item that appears in a linear clause cannot appear in any 8987 // other data-sharing attribute clause. 8988 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); 8989 if (DVar.RefExpr) { 8990 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 8991 << getOpenMPClauseName(OMPC_linear); 8992 ReportOriginalDSA(*this, DSAStack, D, DVar); 8993 continue; 8994 } 8995 8996 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 8997 continue; 8998 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 8999 9000 // Build private copy of original var. 9001 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(), 9002 D->hasAttrs() ? &D->getAttrs() : nullptr); 9003 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 9004 // Build var to save initial value. 9005 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 9006 Expr *InitExpr; 9007 DeclRefExpr *Ref = nullptr; 9008 if (!VD) { 9009 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 9010 if (!IsOpenMPCapturedDecl(D)) { 9011 ExprCaptures.push_back(Ref->getDecl()); 9012 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 9013 ExprResult RefRes = DefaultLvalueConversion(Ref); 9014 if (!RefRes.isUsable()) 9015 continue; 9016 ExprResult PostUpdateRes = 9017 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 9018 SimpleRefExpr, RefRes.get()); 9019 if (!PostUpdateRes.isUsable()) 9020 continue; 9021 ExprPostUpdates.push_back( 9022 IgnoredValueConversions(PostUpdateRes.get()).get()); 9023 } 9024 } 9025 } 9026 if (LinKind == OMPC_LINEAR_uval) 9027 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 9028 else 9029 InitExpr = VD ? SimpleRefExpr : Ref; 9030 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 9031 /*DirectInit=*/false, /*TypeMayContainAuto=*/false); 9032 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 9033 9034 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 9035 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref); 9036 Privates.push_back(PrivateRef); 9037 Inits.push_back(InitRef); 9038 } 9039 9040 if (Vars.empty()) 9041 return nullptr; 9042 9043 Expr *StepExpr = Step; 9044 Expr *CalcStepExpr = nullptr; 9045 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 9046 !Step->isInstantiationDependent() && 9047 !Step->containsUnexpandedParameterPack()) { 9048 SourceLocation StepLoc = Step->getLocStart(); 9049 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 9050 if (Val.isInvalid()) 9051 return nullptr; 9052 StepExpr = Val.get(); 9053 9054 // Build var to save the step value. 9055 VarDecl *SaveVar = 9056 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 9057 ExprResult SaveRef = 9058 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 9059 ExprResult CalcStep = 9060 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 9061 CalcStep = ActOnFinishFullExpr(CalcStep.get()); 9062 9063 // Warn about zero linear step (it would be probably better specified as 9064 // making corresponding variables 'const'). 9065 llvm::APSInt Result; 9066 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); 9067 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) 9068 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] 9069 << (Vars.size() > 1); 9070 if (!IsConstant && CalcStep.isUsable()) { 9071 // Calculate the step beforehand instead of doing this on each iteration. 9072 // (This is not used if the number of iterations may be kfold-ed). 9073 CalcStepExpr = CalcStep.get(); 9074 } 9075 } 9076 9077 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 9078 ColonLoc, EndLoc, Vars, Privates, Inits, 9079 StepExpr, CalcStepExpr, 9080 buildPreInits(Context, ExprCaptures), 9081 buildPostUpdate(*this, ExprPostUpdates)); 9082 } 9083 9084 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 9085 Expr *NumIterations, Sema &SemaRef, 9086 Scope *S, DSAStackTy *Stack) { 9087 // Walk the vars and build update/final expressions for the CodeGen. 9088 SmallVector<Expr *, 8> Updates; 9089 SmallVector<Expr *, 8> Finals; 9090 Expr *Step = Clause.getStep(); 9091 Expr *CalcStep = Clause.getCalcStep(); 9092 // OpenMP [2.14.3.7, linear clause] 9093 // If linear-step is not specified it is assumed to be 1. 9094 if (Step == nullptr) 9095 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 9096 else if (CalcStep) { 9097 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 9098 } 9099 bool HasErrors = false; 9100 auto CurInit = Clause.inits().begin(); 9101 auto CurPrivate = Clause.privates().begin(); 9102 auto LinKind = Clause.getModifier(); 9103 for (auto &RefExpr : Clause.varlists()) { 9104 SourceLocation ELoc; 9105 SourceRange ERange; 9106 Expr *SimpleRefExpr = RefExpr; 9107 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange, 9108 /*AllowArraySection=*/false); 9109 ValueDecl *D = Res.first; 9110 if (Res.second || !D) { 9111 Updates.push_back(nullptr); 9112 Finals.push_back(nullptr); 9113 HasErrors = true; 9114 continue; 9115 } 9116 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) { 9117 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts()) 9118 ->getMemberDecl(); 9119 } 9120 auto &&Info = Stack->isLoopControlVariable(D); 9121 Expr *InitExpr = *CurInit; 9122 9123 // Build privatized reference to the current linear var. 9124 auto DE = cast<DeclRefExpr>(SimpleRefExpr); 9125 Expr *CapturedRef; 9126 if (LinKind == OMPC_LINEAR_uval) 9127 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 9128 else 9129 CapturedRef = 9130 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 9131 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 9132 /*RefersToCapture=*/true); 9133 9134 // Build update: Var = InitExpr + IV * Step 9135 ExprResult Update; 9136 if (!Info.first) { 9137 Update = 9138 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, 9139 InitExpr, IV, Step, /* Subtract */ false); 9140 } else 9141 Update = *CurPrivate; 9142 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(), 9143 /*DiscardedValue=*/true); 9144 9145 // Build final: Var = InitExpr + NumIterations * Step 9146 ExprResult Final; 9147 if (!Info.first) { 9148 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 9149 InitExpr, NumIterations, Step, 9150 /* Subtract */ false); 9151 } else 9152 Final = *CurPrivate; 9153 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(), 9154 /*DiscardedValue=*/true); 9155 9156 if (!Update.isUsable() || !Final.isUsable()) { 9157 Updates.push_back(nullptr); 9158 Finals.push_back(nullptr); 9159 HasErrors = true; 9160 } else { 9161 Updates.push_back(Update.get()); 9162 Finals.push_back(Final.get()); 9163 } 9164 ++CurInit; 9165 ++CurPrivate; 9166 } 9167 Clause.setUpdates(Updates); 9168 Clause.setFinals(Finals); 9169 return HasErrors; 9170 } 9171 9172 OMPClause *Sema::ActOnOpenMPAlignedClause( 9173 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 9174 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 9175 9176 SmallVector<Expr *, 8> Vars; 9177 for (auto &RefExpr : VarList) { 9178 assert(RefExpr && "NULL expr in OpenMP linear clause."); 9179 SourceLocation ELoc; 9180 SourceRange ERange; 9181 Expr *SimpleRefExpr = RefExpr; 9182 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 9183 /*AllowArraySection=*/false); 9184 if (Res.second) { 9185 // It will be analyzed later. 9186 Vars.push_back(RefExpr); 9187 } 9188 ValueDecl *D = Res.first; 9189 if (!D) 9190 continue; 9191 9192 QualType QType = D->getType(); 9193 auto *VD = dyn_cast<VarDecl>(D); 9194 9195 // OpenMP [2.8.1, simd construct, Restrictions] 9196 // The type of list items appearing in the aligned clause must be 9197 // array, pointer, reference to array, or reference to pointer. 9198 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 9199 const Type *Ty = QType.getTypePtrOrNull(); 9200 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 9201 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 9202 << QType << getLangOpts().CPlusPlus << ERange; 9203 bool IsDecl = 9204 !VD || 9205 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 9206 Diag(D->getLocation(), 9207 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 9208 << D; 9209 continue; 9210 } 9211 9212 // OpenMP [2.8.1, simd construct, Restrictions] 9213 // A list-item cannot appear in more than one aligned clause. 9214 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 9215 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange; 9216 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 9217 << getOpenMPClauseName(OMPC_aligned); 9218 continue; 9219 } 9220 9221 DeclRefExpr *Ref = nullptr; 9222 if (!VD && IsOpenMPCapturedDecl(D)) 9223 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 9224 Vars.push_back(DefaultFunctionArrayConversion( 9225 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 9226 .get()); 9227 } 9228 9229 // OpenMP [2.8.1, simd construct, Description] 9230 // The parameter of the aligned clause, alignment, must be a constant 9231 // positive integer expression. 9232 // If no optional parameter is specified, implementation-defined default 9233 // alignments for SIMD instructions on the target platforms are assumed. 9234 if (Alignment != nullptr) { 9235 ExprResult AlignResult = 9236 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 9237 if (AlignResult.isInvalid()) 9238 return nullptr; 9239 Alignment = AlignResult.get(); 9240 } 9241 if (Vars.empty()) 9242 return nullptr; 9243 9244 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 9245 EndLoc, Vars, Alignment); 9246 } 9247 9248 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 9249 SourceLocation StartLoc, 9250 SourceLocation LParenLoc, 9251 SourceLocation EndLoc) { 9252 SmallVector<Expr *, 8> Vars; 9253 SmallVector<Expr *, 8> SrcExprs; 9254 SmallVector<Expr *, 8> DstExprs; 9255 SmallVector<Expr *, 8> AssignmentOps; 9256 for (auto &RefExpr : VarList) { 9257 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 9258 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 9259 // It will be analyzed later. 9260 Vars.push_back(RefExpr); 9261 SrcExprs.push_back(nullptr); 9262 DstExprs.push_back(nullptr); 9263 AssignmentOps.push_back(nullptr); 9264 continue; 9265 } 9266 9267 SourceLocation ELoc = RefExpr->getExprLoc(); 9268 // OpenMP [2.1, C/C++] 9269 // A list item is a variable name. 9270 // OpenMP [2.14.4.1, Restrictions, p.1] 9271 // A list item that appears in a copyin clause must be threadprivate. 9272 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 9273 if (!DE || !isa<VarDecl>(DE->getDecl())) { 9274 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 9275 << 0 << RefExpr->getSourceRange(); 9276 continue; 9277 } 9278 9279 Decl *D = DE->getDecl(); 9280 VarDecl *VD = cast<VarDecl>(D); 9281 9282 QualType Type = VD->getType(); 9283 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 9284 // It will be analyzed later. 9285 Vars.push_back(DE); 9286 SrcExprs.push_back(nullptr); 9287 DstExprs.push_back(nullptr); 9288 AssignmentOps.push_back(nullptr); 9289 continue; 9290 } 9291 9292 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 9293 // A list item that appears in a copyin clause must be threadprivate. 9294 if (!DSAStack->isThreadPrivate(VD)) { 9295 Diag(ELoc, diag::err_omp_required_access) 9296 << getOpenMPClauseName(OMPC_copyin) 9297 << getOpenMPDirectiveName(OMPD_threadprivate); 9298 continue; 9299 } 9300 9301 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 9302 // A variable of class type (or array thereof) that appears in a 9303 // copyin clause requires an accessible, unambiguous copy assignment 9304 // operator for the class type. 9305 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 9306 auto *SrcVD = 9307 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(), 9308 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 9309 auto *PseudoSrcExpr = buildDeclRefExpr( 9310 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 9311 auto *DstVD = 9312 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst", 9313 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 9314 auto *PseudoDstExpr = 9315 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 9316 // For arrays generate assignment operation for single element and replace 9317 // it by the original array element in CodeGen. 9318 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, 9319 PseudoDstExpr, PseudoSrcExpr); 9320 if (AssignmentOp.isInvalid()) 9321 continue; 9322 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 9323 /*DiscardedValue=*/true); 9324 if (AssignmentOp.isInvalid()) 9325 continue; 9326 9327 DSAStack->addDSA(VD, DE, OMPC_copyin); 9328 Vars.push_back(DE); 9329 SrcExprs.push_back(PseudoSrcExpr); 9330 DstExprs.push_back(PseudoDstExpr); 9331 AssignmentOps.push_back(AssignmentOp.get()); 9332 } 9333 9334 if (Vars.empty()) 9335 return nullptr; 9336 9337 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 9338 SrcExprs, DstExprs, AssignmentOps); 9339 } 9340 9341 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 9342 SourceLocation StartLoc, 9343 SourceLocation LParenLoc, 9344 SourceLocation EndLoc) { 9345 SmallVector<Expr *, 8> Vars; 9346 SmallVector<Expr *, 8> SrcExprs; 9347 SmallVector<Expr *, 8> DstExprs; 9348 SmallVector<Expr *, 8> AssignmentOps; 9349 for (auto &RefExpr : VarList) { 9350 assert(RefExpr && "NULL expr in OpenMP linear clause."); 9351 SourceLocation ELoc; 9352 SourceRange ERange; 9353 Expr *SimpleRefExpr = RefExpr; 9354 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 9355 /*AllowArraySection=*/false); 9356 if (Res.second) { 9357 // It will be analyzed later. 9358 Vars.push_back(RefExpr); 9359 SrcExprs.push_back(nullptr); 9360 DstExprs.push_back(nullptr); 9361 AssignmentOps.push_back(nullptr); 9362 } 9363 ValueDecl *D = Res.first; 9364 if (!D) 9365 continue; 9366 9367 QualType Type = D->getType(); 9368 auto *VD = dyn_cast<VarDecl>(D); 9369 9370 // OpenMP [2.14.4.2, Restrictions, p.2] 9371 // A list item that appears in a copyprivate clause may not appear in a 9372 // private or firstprivate clause on the single construct. 9373 if (!VD || !DSAStack->isThreadPrivate(VD)) { 9374 auto DVar = DSAStack->getTopDSA(D, false); 9375 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 9376 DVar.RefExpr) { 9377 Diag(ELoc, diag::err_omp_wrong_dsa) 9378 << getOpenMPClauseName(DVar.CKind) 9379 << getOpenMPClauseName(OMPC_copyprivate); 9380 ReportOriginalDSA(*this, DSAStack, D, DVar); 9381 continue; 9382 } 9383 9384 // OpenMP [2.11.4.2, Restrictions, p.1] 9385 // All list items that appear in a copyprivate clause must be either 9386 // threadprivate or private in the enclosing context. 9387 if (DVar.CKind == OMPC_unknown) { 9388 DVar = DSAStack->getImplicitDSA(D, false); 9389 if (DVar.CKind == OMPC_shared) { 9390 Diag(ELoc, diag::err_omp_required_access) 9391 << getOpenMPClauseName(OMPC_copyprivate) 9392 << "threadprivate or private in the enclosing context"; 9393 ReportOriginalDSA(*this, DSAStack, D, DVar); 9394 continue; 9395 } 9396 } 9397 } 9398 9399 // Variably modified types are not supported. 9400 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 9401 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 9402 << getOpenMPClauseName(OMPC_copyprivate) << Type 9403 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 9404 bool IsDecl = 9405 !VD || 9406 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 9407 Diag(D->getLocation(), 9408 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 9409 << D; 9410 continue; 9411 } 9412 9413 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 9414 // A variable of class type (or array thereof) that appears in a 9415 // copyin clause requires an accessible, unambiguous copy assignment 9416 // operator for the class type. 9417 Type = Context.getBaseElementType(Type.getNonReferenceType()) 9418 .getUnqualifiedType(); 9419 auto *SrcVD = 9420 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src", 9421 D->hasAttrs() ? &D->getAttrs() : nullptr); 9422 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 9423 auto *DstVD = 9424 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst", 9425 D->hasAttrs() ? &D->getAttrs() : nullptr); 9426 auto *PseudoDstExpr = 9427 buildDeclRefExpr(*this, DstVD, Type, ELoc); 9428 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 9429 PseudoDstExpr, PseudoSrcExpr); 9430 if (AssignmentOp.isInvalid()) 9431 continue; 9432 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc, 9433 /*DiscardedValue=*/true); 9434 if (AssignmentOp.isInvalid()) 9435 continue; 9436 9437 // No need to mark vars as copyprivate, they are already threadprivate or 9438 // implicitly private. 9439 assert(VD || IsOpenMPCapturedDecl(D)); 9440 Vars.push_back( 9441 VD ? RefExpr->IgnoreParens() 9442 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 9443 SrcExprs.push_back(PseudoSrcExpr); 9444 DstExprs.push_back(PseudoDstExpr); 9445 AssignmentOps.push_back(AssignmentOp.get()); 9446 } 9447 9448 if (Vars.empty()) 9449 return nullptr; 9450 9451 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 9452 Vars, SrcExprs, DstExprs, AssignmentOps); 9453 } 9454 9455 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 9456 SourceLocation StartLoc, 9457 SourceLocation LParenLoc, 9458 SourceLocation EndLoc) { 9459 if (VarList.empty()) 9460 return nullptr; 9461 9462 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 9463 } 9464 9465 OMPClause * 9466 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind, 9467 SourceLocation DepLoc, SourceLocation ColonLoc, 9468 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 9469 SourceLocation LParenLoc, SourceLocation EndLoc) { 9470 if (DSAStack->getCurrentDirective() == OMPD_ordered && 9471 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 9472 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 9473 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 9474 return nullptr; 9475 } 9476 if (DSAStack->getCurrentDirective() != OMPD_ordered && 9477 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 9478 DepKind == OMPC_DEPEND_sink)) { 9479 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink}; 9480 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 9481 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 9482 /*Last=*/OMPC_DEPEND_unknown, Except) 9483 << getOpenMPClauseName(OMPC_depend); 9484 return nullptr; 9485 } 9486 SmallVector<Expr *, 8> Vars; 9487 llvm::APSInt DepCounter(/*BitWidth=*/32); 9488 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 9489 if (DepKind == OMPC_DEPEND_sink) { 9490 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) { 9491 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 9492 TotalDepCount.setIsUnsigned(/*Val=*/true); 9493 } 9494 } 9495 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) || 9496 DSAStack->getParentOrderedRegionParam()) { 9497 for (auto &RefExpr : VarList) { 9498 assert(RefExpr && "NULL expr in OpenMP shared clause."); 9499 if (isa<DependentScopeDeclRefExpr>(RefExpr) || 9500 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) { 9501 // It will be analyzed later. 9502 Vars.push_back(RefExpr); 9503 continue; 9504 } 9505 9506 SourceLocation ELoc = RefExpr->getExprLoc(); 9507 auto *SimpleExpr = RefExpr->IgnoreParenCasts(); 9508 if (DepKind == OMPC_DEPEND_sink) { 9509 if (DepCounter >= TotalDepCount) { 9510 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 9511 continue; 9512 } 9513 ++DepCounter; 9514 // OpenMP [2.13.9, Summary] 9515 // depend(dependence-type : vec), where dependence-type is: 9516 // 'sink' and where vec is the iteration vector, which has the form: 9517 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 9518 // where n is the value specified by the ordered clause in the loop 9519 // directive, xi denotes the loop iteration variable of the i-th nested 9520 // loop associated with the loop directive, and di is a constant 9521 // non-negative integer. 9522 SimpleExpr = SimpleExpr->IgnoreImplicit(); 9523 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr); 9524 if (!DE) { 9525 OverloadedOperatorKind OOK = OO_None; 9526 SourceLocation OOLoc; 9527 Expr *LHS, *RHS; 9528 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 9529 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 9530 OOLoc = BO->getOperatorLoc(); 9531 LHS = BO->getLHS()->IgnoreParenImpCasts(); 9532 RHS = BO->getRHS()->IgnoreParenImpCasts(); 9533 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 9534 OOK = OCE->getOperator(); 9535 OOLoc = OCE->getOperatorLoc(); 9536 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 9537 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 9538 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 9539 OOK = MCE->getMethodDecl() 9540 ->getNameInfo() 9541 .getName() 9542 .getCXXOverloadedOperator(); 9543 OOLoc = MCE->getCallee()->getExprLoc(); 9544 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 9545 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 9546 } else { 9547 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr); 9548 continue; 9549 } 9550 DE = dyn_cast<DeclRefExpr>(LHS); 9551 if (!DE) { 9552 Diag(LHS->getExprLoc(), 9553 diag::err_omp_depend_sink_expected_loop_iteration) 9554 << DSAStack->getParentLoopControlVariable( 9555 DepCounter.getZExtValue()); 9556 continue; 9557 } 9558 if (OOK != OO_Plus && OOK != OO_Minus) { 9559 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 9560 continue; 9561 } 9562 ExprResult Res = VerifyPositiveIntegerConstantInClause( 9563 RHS, OMPC_depend, /*StrictlyPositive=*/false); 9564 if (Res.isInvalid()) 9565 continue; 9566 } 9567 auto *VD = dyn_cast<VarDecl>(DE->getDecl()); 9568 if (!CurContext->isDependentContext() && 9569 DSAStack->getParentOrderedRegionParam() && 9570 (!VD || 9571 DepCounter != DSAStack->isParentLoopControlVariable(VD).first)) { 9572 Diag(DE->getExprLoc(), 9573 diag::err_omp_depend_sink_expected_loop_iteration) 9574 << DSAStack->getParentLoopControlVariable( 9575 DepCounter.getZExtValue()); 9576 continue; 9577 } 9578 } else { 9579 // OpenMP [2.11.1.1, Restrictions, p.3] 9580 // A variable that is part of another variable (such as a field of a 9581 // structure) but is not an array element or an array section cannot 9582 // appear in a depend clause. 9583 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr); 9584 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 9585 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); 9586 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 9587 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) || 9588 (ASE && 9589 !ASE->getBase() 9590 ->getType() 9591 .getNonReferenceType() 9592 ->isPointerType() && 9593 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 9594 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item) 9595 << 0 << RefExpr->getSourceRange(); 9596 continue; 9597 } 9598 } 9599 9600 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 9601 } 9602 9603 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 9604 TotalDepCount > VarList.size() && 9605 DSAStack->getParentOrderedRegionParam()) { 9606 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 9607 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 9608 } 9609 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 9610 Vars.empty()) 9611 return nullptr; 9612 } 9613 9614 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind, 9615 DepLoc, ColonLoc, Vars); 9616 } 9617 9618 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc, 9619 SourceLocation LParenLoc, 9620 SourceLocation EndLoc) { 9621 Expr *ValExpr = Device; 9622 9623 // OpenMP [2.9.1, Restrictions] 9624 // The device expression must evaluate to a non-negative integer value. 9625 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 9626 /*StrictlyPositive=*/false)) 9627 return nullptr; 9628 9629 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc); 9630 } 9631 9632 static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc, 9633 DSAStackTy *Stack, CXXRecordDecl *RD) { 9634 if (!RD || RD->isInvalidDecl()) 9635 return true; 9636 9637 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD)) 9638 if (auto *CTD = CTSD->getSpecializedTemplate()) 9639 RD = CTD->getTemplatedDecl(); 9640 auto QTy = SemaRef.Context.getRecordType(RD); 9641 if (RD->isDynamicClass()) { 9642 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; 9643 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target); 9644 return false; 9645 } 9646 auto *DC = RD; 9647 bool IsCorrect = true; 9648 for (auto *I : DC->decls()) { 9649 if (I) { 9650 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) { 9651 if (MD->isStatic()) { 9652 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; 9653 SemaRef.Diag(MD->getLocation(), 9654 diag::note_omp_static_member_in_target); 9655 IsCorrect = false; 9656 } 9657 } else if (auto *VD = dyn_cast<VarDecl>(I)) { 9658 if (VD->isStaticDataMember()) { 9659 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; 9660 SemaRef.Diag(VD->getLocation(), 9661 diag::note_omp_static_member_in_target); 9662 IsCorrect = false; 9663 } 9664 } 9665 } 9666 } 9667 9668 for (auto &I : RD->bases()) { 9669 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack, 9670 I.getType()->getAsCXXRecordDecl())) 9671 IsCorrect = false; 9672 } 9673 return IsCorrect; 9674 } 9675 9676 static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 9677 DSAStackTy *Stack, QualType QTy) { 9678 NamedDecl *ND; 9679 if (QTy->isIncompleteType(&ND)) { 9680 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 9681 return false; 9682 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) { 9683 if (!RD->isInvalidDecl() && 9684 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD)) 9685 return false; 9686 } 9687 return true; 9688 } 9689 9690 /// \brief Return true if it can be proven that the provided array expression 9691 /// (array section or array subscript) does NOT specify the whole size of the 9692 /// array whose base type is \a BaseQTy. 9693 static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 9694 const Expr *E, 9695 QualType BaseQTy) { 9696 auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 9697 9698 // If this is an array subscript, it refers to the whole size if the size of 9699 // the dimension is constant and equals 1. Also, an array section assumes the 9700 // format of an array subscript if no colon is used. 9701 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) { 9702 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 9703 return ATy->getSize().getSExtValue() != 1; 9704 // Size can't be evaluated statically. 9705 return false; 9706 } 9707 9708 assert(OASE && "Expecting array section if not an array subscript."); 9709 auto *LowerBound = OASE->getLowerBound(); 9710 auto *Length = OASE->getLength(); 9711 9712 // If there is a lower bound that does not evaluates to zero, we are not 9713 // convering the whole dimension. 9714 if (LowerBound) { 9715 llvm::APSInt ConstLowerBound; 9716 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext())) 9717 return false; // Can't get the integer value as a constant. 9718 if (ConstLowerBound.getSExtValue()) 9719 return true; 9720 } 9721 9722 // If we don't have a length we covering the whole dimension. 9723 if (!Length) 9724 return false; 9725 9726 // If the base is a pointer, we don't have a way to get the size of the 9727 // pointee. 9728 if (BaseQTy->isPointerType()) 9729 return false; 9730 9731 // We can only check if the length is the same as the size of the dimension 9732 // if we have a constant array. 9733 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 9734 if (!CATy) 9735 return false; 9736 9737 llvm::APSInt ConstLength; 9738 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext())) 9739 return false; // Can't get the integer value as a constant. 9740 9741 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 9742 } 9743 9744 // Return true if it can be proven that the provided array expression (array 9745 // section or array subscript) does NOT specify a single element of the array 9746 // whose base type is \a BaseQTy. 9747 static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 9748 const Expr *E, 9749 QualType BaseQTy) { 9750 auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 9751 9752 // An array subscript always refer to a single element. Also, an array section 9753 // assumes the format of an array subscript if no colon is used. 9754 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) 9755 return false; 9756 9757 assert(OASE && "Expecting array section if not an array subscript."); 9758 auto *Length = OASE->getLength(); 9759 9760 // If we don't have a length we have to check if the array has unitary size 9761 // for this dimension. Also, we should always expect a length if the base type 9762 // is pointer. 9763 if (!Length) { 9764 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 9765 return ATy->getSize().getSExtValue() != 1; 9766 // We cannot assume anything. 9767 return false; 9768 } 9769 9770 // Check if the length evaluates to 1. 9771 llvm::APSInt ConstLength; 9772 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext())) 9773 return false; // Can't get the integer value as a constant. 9774 9775 return ConstLength.getSExtValue() != 1; 9776 } 9777 9778 // Return the expression of the base of the map clause or null if it cannot 9779 // be determined and do all the necessary checks to see if the expression is 9780 // valid as a standalone map clause expression. In the process, record all the 9781 // components of the expression. 9782 static Expr *CheckMapClauseExpressionBase( 9783 Sema &SemaRef, Expr *E, 9784 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents) { 9785 SourceLocation ELoc = E->getExprLoc(); 9786 SourceRange ERange = E->getSourceRange(); 9787 9788 // The base of elements of list in a map clause have to be either: 9789 // - a reference to variable or field. 9790 // - a member expression. 9791 // - an array expression. 9792 // 9793 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 9794 // reference to 'r'. 9795 // 9796 // If we have: 9797 // 9798 // struct SS { 9799 // Bla S; 9800 // foo() { 9801 // #pragma omp target map (S.Arr[:12]); 9802 // } 9803 // } 9804 // 9805 // We want to retrieve the member expression 'this->S'; 9806 9807 Expr *RelevantExpr = nullptr; 9808 9809 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2] 9810 // If a list item is an array section, it must specify contiguous storage. 9811 // 9812 // For this restriction it is sufficient that we make sure only references 9813 // to variables or fields and array expressions, and that no array sections 9814 // exist except in the rightmost expression (unless they cover the whole 9815 // dimension of the array). E.g. these would be invalid: 9816 // 9817 // r.ArrS[3:5].Arr[6:7] 9818 // 9819 // r.ArrS[3:5].x 9820 // 9821 // but these would be valid: 9822 // r.ArrS[3].Arr[6:7] 9823 // 9824 // r.ArrS[3].x 9825 9826 bool AllowUnitySizeArraySection = true; 9827 bool AllowWholeSizeArraySection = true; 9828 9829 while (!RelevantExpr) { 9830 E = E->IgnoreParenImpCasts(); 9831 9832 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) { 9833 if (!isa<VarDecl>(CurE->getDecl())) 9834 break; 9835 9836 RelevantExpr = CurE; 9837 9838 // If we got a reference to a declaration, we should not expect any array 9839 // section before that. 9840 AllowUnitySizeArraySection = false; 9841 AllowWholeSizeArraySection = false; 9842 9843 // Record the component. 9844 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent( 9845 CurE, CurE->getDecl())); 9846 continue; 9847 } 9848 9849 if (auto *CurE = dyn_cast<MemberExpr>(E)) { 9850 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts(); 9851 9852 if (isa<CXXThisExpr>(BaseE)) 9853 // We found a base expression: this->Val. 9854 RelevantExpr = CurE; 9855 else 9856 E = BaseE; 9857 9858 if (!isa<FieldDecl>(CurE->getMemberDecl())) { 9859 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 9860 << CurE->getSourceRange(); 9861 break; 9862 } 9863 9864 auto *FD = cast<FieldDecl>(CurE->getMemberDecl()); 9865 9866 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 9867 // A bit-field cannot appear in a map clause. 9868 // 9869 if (FD->isBitField()) { 9870 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause) 9871 << CurE->getSourceRange(); 9872 break; 9873 } 9874 9875 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 9876 // If the type of a list item is a reference to a type T then the type 9877 // will be considered to be T for all purposes of this clause. 9878 QualType CurType = BaseE->getType().getNonReferenceType(); 9879 9880 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 9881 // A list item cannot be a variable that is a member of a structure with 9882 // a union type. 9883 // 9884 if (auto *RT = CurType->getAs<RecordType>()) 9885 if (RT->isUnionType()) { 9886 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 9887 << CurE->getSourceRange(); 9888 break; 9889 } 9890 9891 // If we got a member expression, we should not expect any array section 9892 // before that: 9893 // 9894 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 9895 // If a list item is an element of a structure, only the rightmost symbol 9896 // of the variable reference can be an array section. 9897 // 9898 AllowUnitySizeArraySection = false; 9899 AllowWholeSizeArraySection = false; 9900 9901 // Record the component. 9902 CurComponents.push_back( 9903 OMPClauseMappableExprCommon::MappableComponent(CurE, FD)); 9904 continue; 9905 } 9906 9907 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) { 9908 E = CurE->getBase()->IgnoreParenImpCasts(); 9909 9910 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 9911 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 9912 << 0 << CurE->getSourceRange(); 9913 break; 9914 } 9915 9916 // If we got an array subscript that express the whole dimension we 9917 // can have any array expressions before. If it only expressing part of 9918 // the dimension, we can only have unitary-size array expressions. 9919 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, 9920 E->getType())) 9921 AllowWholeSizeArraySection = false; 9922 9923 // Record the component - we don't have any declaration associated. 9924 CurComponents.push_back( 9925 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr)); 9926 continue; 9927 } 9928 9929 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) { 9930 E = CurE->getBase()->IgnoreParenImpCasts(); 9931 9932 auto CurType = 9933 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 9934 9935 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 9936 // If the type of a list item is a reference to a type T then the type 9937 // will be considered to be T for all purposes of this clause. 9938 if (CurType->isReferenceType()) 9939 CurType = CurType->getPointeeType(); 9940 9941 bool IsPointer = CurType->isAnyPointerType(); 9942 9943 if (!IsPointer && !CurType->isArrayType()) { 9944 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 9945 << 0 << CurE->getSourceRange(); 9946 break; 9947 } 9948 9949 bool NotWhole = 9950 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType); 9951 bool NotUnity = 9952 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType); 9953 9954 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) { 9955 // Any array section is currently allowed. 9956 // 9957 // If this array section refers to the whole dimension we can still 9958 // accept other array sections before this one, except if the base is a 9959 // pointer. Otherwise, only unitary sections are accepted. 9960 if (NotWhole || IsPointer) 9961 AllowWholeSizeArraySection = false; 9962 } else if ((AllowUnitySizeArraySection && NotUnity) || 9963 (AllowWholeSizeArraySection && NotWhole)) { 9964 // A unity or whole array section is not allowed and that is not 9965 // compatible with the properties of the current array section. 9966 SemaRef.Diag( 9967 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 9968 << CurE->getSourceRange(); 9969 break; 9970 } 9971 9972 // Record the component - we don't have any declaration associated. 9973 CurComponents.push_back( 9974 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr)); 9975 continue; 9976 } 9977 9978 // If nothing else worked, this is not a valid map clause expression. 9979 SemaRef.Diag(ELoc, 9980 diag::err_omp_expected_named_var_member_or_array_expression) 9981 << ERange; 9982 break; 9983 } 9984 9985 return RelevantExpr; 9986 } 9987 9988 // Return true if expression E associated with value VD has conflicts with other 9989 // map information. 9990 static bool CheckMapConflicts( 9991 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E, 9992 bool CurrentRegionOnly, 9993 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents) { 9994 assert(VD && E); 9995 SourceLocation ELoc = E->getExprLoc(); 9996 SourceRange ERange = E->getSourceRange(); 9997 9998 // In order to easily check the conflicts we need to match each component of 9999 // the expression under test with the components of the expressions that are 10000 // already in the stack. 10001 10002 assert(!CurComponents.empty() && "Map clause expression with no components!"); 10003 assert(CurComponents.back().getAssociatedDeclaration() == VD && 10004 "Map clause expression with unexpected base!"); 10005 10006 // Variables to help detecting enclosing problems in data environment nests. 10007 bool IsEnclosedByDataEnvironmentExpr = false; 10008 const Expr *EnclosingExpr = nullptr; 10009 10010 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 10011 VD, CurrentRegionOnly, 10012 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef 10013 StackComponents) -> bool { 10014 10015 assert(!StackComponents.empty() && 10016 "Map clause expression with no components!"); 10017 assert(StackComponents.back().getAssociatedDeclaration() == VD && 10018 "Map clause expression with unexpected base!"); 10019 10020 // The whole expression in the stack. 10021 auto *RE = StackComponents.front().getAssociatedExpression(); 10022 10023 // Expressions must start from the same base. Here we detect at which 10024 // point both expressions diverge from each other and see if we can 10025 // detect if the memory referred to both expressions is contiguous and 10026 // do not overlap. 10027 auto CI = CurComponents.rbegin(); 10028 auto CE = CurComponents.rend(); 10029 auto SI = StackComponents.rbegin(); 10030 auto SE = StackComponents.rend(); 10031 for (; CI != CE && SI != SE; ++CI, ++SI) { 10032 10033 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 10034 // At most one list item can be an array item derived from a given 10035 // variable in map clauses of the same construct. 10036 if (CurrentRegionOnly && 10037 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 10038 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) && 10039 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 10040 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) { 10041 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 10042 diag::err_omp_multiple_array_items_in_map_clause) 10043 << CI->getAssociatedExpression()->getSourceRange(); 10044 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 10045 diag::note_used_here) 10046 << SI->getAssociatedExpression()->getSourceRange(); 10047 return true; 10048 } 10049 10050 // Do both expressions have the same kind? 10051 if (CI->getAssociatedExpression()->getStmtClass() != 10052 SI->getAssociatedExpression()->getStmtClass()) 10053 break; 10054 10055 // Are we dealing with different variables/fields? 10056 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 10057 break; 10058 } 10059 10060 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 10061 // List items of map clauses in the same construct must not share 10062 // original storage. 10063 // 10064 // If the expressions are exactly the same or one is a subset of the 10065 // other, it means they are sharing storage. 10066 if (CI == CE && SI == SE) { 10067 if (CurrentRegionOnly) { 10068 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 10069 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 10070 << RE->getSourceRange(); 10071 return true; 10072 } else { 10073 // If we find the same expression in the enclosing data environment, 10074 // that is legal. 10075 IsEnclosedByDataEnvironmentExpr = true; 10076 return false; 10077 } 10078 } 10079 10080 QualType DerivedType = 10081 std::prev(CI)->getAssociatedDeclaration()->getType(); 10082 SourceLocation DerivedLoc = 10083 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 10084 10085 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 10086 // If the type of a list item is a reference to a type T then the type 10087 // will be considered to be T for all purposes of this clause. 10088 DerivedType = DerivedType.getNonReferenceType(); 10089 10090 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 10091 // A variable for which the type is pointer and an array section 10092 // derived from that variable must not appear as list items of map 10093 // clauses of the same construct. 10094 // 10095 // Also, cover one of the cases in: 10096 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 10097 // If any part of the original storage of a list item has corresponding 10098 // storage in the device data environment, all of the original storage 10099 // must have corresponding storage in the device data environment. 10100 // 10101 if (DerivedType->isAnyPointerType()) { 10102 if (CI == CE || SI == SE) { 10103 SemaRef.Diag( 10104 DerivedLoc, 10105 diag::err_omp_pointer_mapped_along_with_derived_section) 10106 << DerivedLoc; 10107 } else { 10108 assert(CI != CE && SI != SE); 10109 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced) 10110 << DerivedLoc; 10111 } 10112 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 10113 << RE->getSourceRange(); 10114 return true; 10115 } 10116 10117 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 10118 // List items of map clauses in the same construct must not share 10119 // original storage. 10120 // 10121 // An expression is a subset of the other. 10122 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 10123 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 10124 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 10125 << RE->getSourceRange(); 10126 return true; 10127 } 10128 10129 // The current expression uses the same base as other expression in the 10130 // data environment but does not contain it completely. 10131 if (!CurrentRegionOnly && SI != SE) 10132 EnclosingExpr = RE; 10133 10134 // The current expression is a subset of the expression in the data 10135 // environment. 10136 IsEnclosedByDataEnvironmentExpr |= 10137 (!CurrentRegionOnly && CI != CE && SI == SE); 10138 10139 return false; 10140 }); 10141 10142 if (CurrentRegionOnly) 10143 return FoundError; 10144 10145 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 10146 // If any part of the original storage of a list item has corresponding 10147 // storage in the device data environment, all of the original storage must 10148 // have corresponding storage in the device data environment. 10149 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 10150 // If a list item is an element of a structure, and a different element of 10151 // the structure has a corresponding list item in the device data environment 10152 // prior to a task encountering the construct associated with the map clause, 10153 // then the list item must also have a corresponding list item in the device 10154 // data environment prior to the task encountering the construct. 10155 // 10156 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 10157 SemaRef.Diag(ELoc, 10158 diag::err_omp_original_storage_is_shared_and_does_not_contain) 10159 << ERange; 10160 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 10161 << EnclosingExpr->getSourceRange(); 10162 return true; 10163 } 10164 10165 return FoundError; 10166 } 10167 10168 OMPClause * 10169 Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier, 10170 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, 10171 SourceLocation MapLoc, SourceLocation ColonLoc, 10172 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 10173 SourceLocation LParenLoc, SourceLocation EndLoc) { 10174 SmallVector<Expr *, 4> Vars; 10175 10176 // Keep track of the mappable components and base declarations in this clause. 10177 // Each entry in the list is going to have a list of components associated. We 10178 // record each set of the components so that we can build the clause later on. 10179 // In the end we should have the same amount of declarations and component 10180 // lists. 10181 OMPClauseMappableExprCommon::MappableExprComponentLists ClauseComponents; 10182 SmallVector<ValueDecl *, 16> ClauseBaseDeclarations; 10183 10184 ClauseComponents.reserve(VarList.size()); 10185 ClauseBaseDeclarations.reserve(VarList.size()); 10186 10187 for (auto &RE : VarList) { 10188 assert(RE && "Null expr in omp map"); 10189 if (isa<DependentScopeDeclRefExpr>(RE)) { 10190 // It will be analyzed later. 10191 Vars.push_back(RE); 10192 continue; 10193 } 10194 SourceLocation ELoc = RE->getExprLoc(); 10195 10196 auto *VE = RE->IgnoreParenLValueCasts(); 10197 10198 if (VE->isValueDependent() || VE->isTypeDependent() || 10199 VE->isInstantiationDependent() || 10200 VE->containsUnexpandedParameterPack()) { 10201 // We can only analyze this information once the missing information is 10202 // resolved. 10203 Vars.push_back(RE); 10204 continue; 10205 } 10206 10207 auto *SimpleExpr = RE->IgnoreParenCasts(); 10208 10209 if (!RE->IgnoreParenImpCasts()->isLValue()) { 10210 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 10211 << RE->getSourceRange(); 10212 continue; 10213 } 10214 10215 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 10216 ValueDecl *CurDeclaration = nullptr; 10217 10218 // Obtain the array or member expression bases if required. Also, fill the 10219 // components array with all the components identified in the process. 10220 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr, CurComponents); 10221 if (!BE) 10222 continue; 10223 10224 assert(!CurComponents.empty() && 10225 "Invalid mappable expression information."); 10226 10227 // For the following checks, we rely on the base declaration which is 10228 // expected to be associated with the last component. The declaration is 10229 // expected to be a variable or a field (if 'this' is being mapped). 10230 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 10231 assert(CurDeclaration && "Null decl on map clause."); 10232 assert( 10233 CurDeclaration->isCanonicalDecl() && 10234 "Expecting components to have associated only canonical declarations."); 10235 10236 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 10237 auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 10238 10239 assert((VD || FD) && "Only variables or fields are expected here!"); 10240 (void)FD; 10241 10242 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 10243 // threadprivate variables cannot appear in a map clause. 10244 if (VD && DSAStack->isThreadPrivate(VD)) { 10245 auto DVar = DSAStack->getTopDSA(VD, false); 10246 Diag(ELoc, diag::err_omp_threadprivate_in_map); 10247 ReportOriginalDSA(*this, DSAStack, VD, DVar); 10248 continue; 10249 } 10250 10251 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 10252 // A list item cannot appear in both a map clause and a data-sharing 10253 // attribute clause on the same construct. 10254 // 10255 // TODO: Implement this check - it cannot currently be tested because of 10256 // missing implementation of the other data sharing clauses in target 10257 // directives. 10258 10259 // Check conflicts with other map clause expressions. We check the conflicts 10260 // with the current construct separately from the enclosing data 10261 // environment, because the restrictions are different. 10262 if (CheckMapConflicts(*this, DSAStack, CurDeclaration, SimpleExpr, 10263 /*CurrentRegionOnly=*/true, CurComponents)) 10264 break; 10265 if (CheckMapConflicts(*this, DSAStack, CurDeclaration, SimpleExpr, 10266 /*CurrentRegionOnly=*/false, CurComponents)) 10267 break; 10268 10269 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 10270 // If the type of a list item is a reference to a type T then the type will 10271 // be considered to be T for all purposes of this clause. 10272 QualType Type = CurDeclaration->getType().getNonReferenceType(); 10273 10274 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 10275 // A list item must have a mappable type. 10276 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this, 10277 DSAStack, Type)) 10278 continue; 10279 10280 // target enter data 10281 // OpenMP [2.10.2, Restrictions, p. 99] 10282 // A map-type must be specified in all map clauses and must be either 10283 // to or alloc. 10284 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 10285 if (DKind == OMPD_target_enter_data && 10286 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 10287 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 10288 << (IsMapTypeImplicit ? 1 : 0) 10289 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 10290 << getOpenMPDirectiveName(DKind); 10291 continue; 10292 } 10293 10294 // target exit_data 10295 // OpenMP [2.10.3, Restrictions, p. 102] 10296 // A map-type must be specified in all map clauses and must be either 10297 // from, release, or delete. 10298 DKind = DSAStack->getCurrentDirective(); 10299 if (DKind == OMPD_target_exit_data && 10300 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 10301 MapType == OMPC_MAP_delete)) { 10302 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 10303 << (IsMapTypeImplicit ? 1 : 0) 10304 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 10305 << getOpenMPDirectiveName(DKind); 10306 continue; 10307 } 10308 10309 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 10310 // A list item cannot appear in both a map clause and a data-sharing 10311 // attribute clause on the same construct 10312 if (DKind == OMPD_target && VD) { 10313 auto DVar = DSAStack->getTopDSA(VD, false); 10314 if (isOpenMPPrivate(DVar.CKind)) { 10315 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa) 10316 << getOpenMPClauseName(DVar.CKind) 10317 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 10318 ReportOriginalDSA(*this, DSAStack, CurDeclaration, DVar); 10319 continue; 10320 } 10321 } 10322 10323 // Save the current expression. 10324 Vars.push_back(RE); 10325 10326 // Store the components in the stack so that they can be used to check 10327 // against other clauses later on. 10328 DSAStack->addMappableExpressionComponents(CurDeclaration, CurComponents); 10329 10330 // Save the components and declaration to create the clause. For purposes of 10331 // the clause creation, any component list that has has base 'this' uses 10332 // null has 10333 ClauseComponents.resize(ClauseComponents.size() + 1); 10334 ClauseComponents.back().append(CurComponents.begin(), CurComponents.end()); 10335 ClauseBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 10336 : CurDeclaration); 10337 } 10338 10339 // We need to produce a map clause even if we don't have variables so that 10340 // other diagnostics related with non-existing map clauses are accurate. 10341 return OMPMapClause::Create( 10342 Context, StartLoc, LParenLoc, EndLoc, Vars, ClauseBaseDeclarations, 10343 ClauseComponents, MapTypeModifier, MapType, IsMapTypeImplicit, MapLoc); 10344 } 10345 10346 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 10347 TypeResult ParsedType) { 10348 assert(ParsedType.isUsable()); 10349 10350 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 10351 if (ReductionType.isNull()) 10352 return QualType(); 10353 10354 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 10355 // A type name in a declare reduction directive cannot be a function type, an 10356 // array type, a reference type, or a type qualified with const, volatile or 10357 // restrict. 10358 if (ReductionType.hasQualifiers()) { 10359 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 10360 return QualType(); 10361 } 10362 10363 if (ReductionType->isFunctionType()) { 10364 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 10365 return QualType(); 10366 } 10367 if (ReductionType->isReferenceType()) { 10368 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 10369 return QualType(); 10370 } 10371 if (ReductionType->isArrayType()) { 10372 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 10373 return QualType(); 10374 } 10375 return ReductionType; 10376 } 10377 10378 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 10379 Scope *S, DeclContext *DC, DeclarationName Name, 10380 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 10381 AccessSpecifier AS, Decl *PrevDeclInScope) { 10382 SmallVector<Decl *, 8> Decls; 10383 Decls.reserve(ReductionTypes.size()); 10384 10385 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 10386 ForRedeclaration); 10387 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 10388 // A reduction-identifier may not be re-declared in the current scope for the 10389 // same type or for a type that is compatible according to the base language 10390 // rules. 10391 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 10392 OMPDeclareReductionDecl *PrevDRD = nullptr; 10393 bool InCompoundScope = true; 10394 if (S != nullptr) { 10395 // Find previous declaration with the same name not referenced in other 10396 // declarations. 10397 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 10398 InCompoundScope = 10399 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 10400 LookupName(Lookup, S); 10401 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 10402 /*AllowInlineNamespace=*/false); 10403 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 10404 auto Filter = Lookup.makeFilter(); 10405 while (Filter.hasNext()) { 10406 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 10407 if (InCompoundScope) { 10408 auto I = UsedAsPrevious.find(PrevDecl); 10409 if (I == UsedAsPrevious.end()) 10410 UsedAsPrevious[PrevDecl] = false; 10411 if (auto *D = PrevDecl->getPrevDeclInScope()) 10412 UsedAsPrevious[D] = true; 10413 } 10414 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 10415 PrevDecl->getLocation(); 10416 } 10417 Filter.done(); 10418 if (InCompoundScope) { 10419 for (auto &PrevData : UsedAsPrevious) { 10420 if (!PrevData.second) { 10421 PrevDRD = PrevData.first; 10422 break; 10423 } 10424 } 10425 } 10426 } else if (PrevDeclInScope != nullptr) { 10427 auto *PrevDRDInScope = PrevDRD = 10428 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 10429 do { 10430 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 10431 PrevDRDInScope->getLocation(); 10432 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 10433 } while (PrevDRDInScope != nullptr); 10434 } 10435 for (auto &TyData : ReductionTypes) { 10436 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 10437 bool Invalid = false; 10438 if (I != PreviousRedeclTypes.end()) { 10439 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 10440 << TyData.first; 10441 Diag(I->second, diag::note_previous_definition); 10442 Invalid = true; 10443 } 10444 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 10445 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 10446 Name, TyData.first, PrevDRD); 10447 DC->addDecl(DRD); 10448 DRD->setAccess(AS); 10449 Decls.push_back(DRD); 10450 if (Invalid) 10451 DRD->setInvalidDecl(); 10452 else 10453 PrevDRD = DRD; 10454 } 10455 10456 return DeclGroupPtrTy::make( 10457 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 10458 } 10459 10460 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 10461 auto *DRD = cast<OMPDeclareReductionDecl>(D); 10462 10463 // Enter new function scope. 10464 PushFunctionScope(); 10465 getCurFunction()->setHasBranchProtectedScope(); 10466 getCurFunction()->setHasOMPDeclareReductionCombiner(); 10467 10468 if (S != nullptr) 10469 PushDeclContext(S, DRD); 10470 else 10471 CurContext = DRD; 10472 10473 PushExpressionEvaluationContext(PotentiallyEvaluated); 10474 10475 QualType ReductionType = DRD->getType(); 10476 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 10477 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 10478 // uses semantics of argument handles by value, but it should be passed by 10479 // reference. C lang does not support references, so pass all parameters as 10480 // pointers. 10481 // Create 'T omp_in;' variable. 10482 auto *OmpInParm = 10483 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 10484 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 10485 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 10486 // uses semantics of argument handles by value, but it should be passed by 10487 // reference. C lang does not support references, so pass all parameters as 10488 // pointers. 10489 // Create 'T omp_out;' variable. 10490 auto *OmpOutParm = 10491 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 10492 if (S != nullptr) { 10493 PushOnScopeChains(OmpInParm, S); 10494 PushOnScopeChains(OmpOutParm, S); 10495 } else { 10496 DRD->addDecl(OmpInParm); 10497 DRD->addDecl(OmpOutParm); 10498 } 10499 } 10500 10501 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 10502 auto *DRD = cast<OMPDeclareReductionDecl>(D); 10503 DiscardCleanupsInEvaluationContext(); 10504 PopExpressionEvaluationContext(); 10505 10506 PopDeclContext(); 10507 PopFunctionScopeInfo(); 10508 10509 if (Combiner != nullptr) 10510 DRD->setCombiner(Combiner); 10511 else 10512 DRD->setInvalidDecl(); 10513 } 10514 10515 void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 10516 auto *DRD = cast<OMPDeclareReductionDecl>(D); 10517 10518 // Enter new function scope. 10519 PushFunctionScope(); 10520 getCurFunction()->setHasBranchProtectedScope(); 10521 10522 if (S != nullptr) 10523 PushDeclContext(S, DRD); 10524 else 10525 CurContext = DRD; 10526 10527 PushExpressionEvaluationContext(PotentiallyEvaluated); 10528 10529 QualType ReductionType = DRD->getType(); 10530 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 10531 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 10532 // uses semantics of argument handles by value, but it should be passed by 10533 // reference. C lang does not support references, so pass all parameters as 10534 // pointers. 10535 // Create 'T omp_priv;' variable. 10536 auto *OmpPrivParm = 10537 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 10538 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 10539 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 10540 // uses semantics of argument handles by value, but it should be passed by 10541 // reference. C lang does not support references, so pass all parameters as 10542 // pointers. 10543 // Create 'T omp_orig;' variable. 10544 auto *OmpOrigParm = 10545 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 10546 if (S != nullptr) { 10547 PushOnScopeChains(OmpPrivParm, S); 10548 PushOnScopeChains(OmpOrigParm, S); 10549 } else { 10550 DRD->addDecl(OmpPrivParm); 10551 DRD->addDecl(OmpOrigParm); 10552 } 10553 } 10554 10555 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, 10556 Expr *Initializer) { 10557 auto *DRD = cast<OMPDeclareReductionDecl>(D); 10558 DiscardCleanupsInEvaluationContext(); 10559 PopExpressionEvaluationContext(); 10560 10561 PopDeclContext(); 10562 PopFunctionScopeInfo(); 10563 10564 if (Initializer != nullptr) 10565 DRD->setInitializer(Initializer); 10566 else 10567 DRD->setInvalidDecl(); 10568 } 10569 10570 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 10571 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 10572 for (auto *D : DeclReductions.get()) { 10573 if (IsValid) { 10574 auto *DRD = cast<OMPDeclareReductionDecl>(D); 10575 if (S != nullptr) 10576 PushOnScopeChains(DRD, S, /*AddToContext=*/false); 10577 } else 10578 D->setInvalidDecl(); 10579 } 10580 return DeclReductions; 10581 } 10582 10583 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 10584 SourceLocation StartLoc, 10585 SourceLocation LParenLoc, 10586 SourceLocation EndLoc) { 10587 Expr *ValExpr = NumTeams; 10588 10589 // OpenMP [teams Constrcut, Restrictions] 10590 // The num_teams expression must evaluate to a positive integer value. 10591 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 10592 /*StrictlyPositive=*/true)) 10593 return nullptr; 10594 10595 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc); 10596 } 10597 10598 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 10599 SourceLocation StartLoc, 10600 SourceLocation LParenLoc, 10601 SourceLocation EndLoc) { 10602 Expr *ValExpr = ThreadLimit; 10603 10604 // OpenMP [teams Constrcut, Restrictions] 10605 // The thread_limit expression must evaluate to a positive integer value. 10606 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 10607 /*StrictlyPositive=*/true)) 10608 return nullptr; 10609 10610 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc, 10611 EndLoc); 10612 } 10613 10614 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 10615 SourceLocation StartLoc, 10616 SourceLocation LParenLoc, 10617 SourceLocation EndLoc) { 10618 Expr *ValExpr = Priority; 10619 10620 // OpenMP [2.9.1, task Constrcut] 10621 // The priority-value is a non-negative numerical scalar expression. 10622 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority, 10623 /*StrictlyPositive=*/false)) 10624 return nullptr; 10625 10626 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc); 10627 } 10628 10629 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 10630 SourceLocation StartLoc, 10631 SourceLocation LParenLoc, 10632 SourceLocation EndLoc) { 10633 Expr *ValExpr = Grainsize; 10634 10635 // OpenMP [2.9.2, taskloop Constrcut] 10636 // The parameter of the grainsize clause must be a positive integer 10637 // expression. 10638 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize, 10639 /*StrictlyPositive=*/true)) 10640 return nullptr; 10641 10642 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc); 10643 } 10644 10645 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 10646 SourceLocation StartLoc, 10647 SourceLocation LParenLoc, 10648 SourceLocation EndLoc) { 10649 Expr *ValExpr = NumTasks; 10650 10651 // OpenMP [2.9.2, taskloop Constrcut] 10652 // The parameter of the num_tasks clause must be a positive integer 10653 // expression. 10654 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks, 10655 /*StrictlyPositive=*/true)) 10656 return nullptr; 10657 10658 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc); 10659 } 10660 10661 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 10662 SourceLocation LParenLoc, 10663 SourceLocation EndLoc) { 10664 // OpenMP [2.13.2, critical construct, Description] 10665 // ... where hint-expression is an integer constant expression that evaluates 10666 // to a valid lock hint. 10667 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 10668 if (HintExpr.isInvalid()) 10669 return nullptr; 10670 return new (Context) 10671 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 10672 } 10673 10674 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 10675 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 10676 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 10677 SourceLocation EndLoc) { 10678 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 10679 std::string Values; 10680 Values += "'"; 10681 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 10682 Values += "'"; 10683 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 10684 << Values << getOpenMPClauseName(OMPC_dist_schedule); 10685 return nullptr; 10686 } 10687 Expr *ValExpr = ChunkSize; 10688 Stmt *HelperValStmt = nullptr; 10689 if (ChunkSize) { 10690 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 10691 !ChunkSize->isInstantiationDependent() && 10692 !ChunkSize->containsUnexpandedParameterPack()) { 10693 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); 10694 ExprResult Val = 10695 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 10696 if (Val.isInvalid()) 10697 return nullptr; 10698 10699 ValExpr = Val.get(); 10700 10701 // OpenMP [2.7.1, Restrictions] 10702 // chunk_size must be a loop invariant integer expression with a positive 10703 // value. 10704 llvm::APSInt Result; 10705 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 10706 if (Result.isSigned() && !Result.isStrictlyPositive()) { 10707 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 10708 << "dist_schedule" << ChunkSize->getSourceRange(); 10709 return nullptr; 10710 } 10711 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) { 10712 llvm::MapVector<Expr *, DeclRefExpr *> Captures; 10713 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 10714 HelperValStmt = buildPreInits(Context, Captures); 10715 } 10716 } 10717 } 10718 10719 return new (Context) 10720 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 10721 Kind, ValExpr, HelperValStmt); 10722 } 10723 10724 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 10725 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 10726 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 10727 SourceLocation KindLoc, SourceLocation EndLoc) { 10728 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)' 10729 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || 10730 Kind != OMPC_DEFAULTMAP_scalar) { 10731 std::string Value; 10732 SourceLocation Loc; 10733 Value += "'"; 10734 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 10735 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 10736 OMPC_DEFAULTMAP_MODIFIER_tofrom); 10737 Loc = MLoc; 10738 } else { 10739 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 10740 OMPC_DEFAULTMAP_scalar); 10741 Loc = KindLoc; 10742 } 10743 Value += "'"; 10744 Diag(Loc, diag::err_omp_unexpected_clause_value) 10745 << Value << getOpenMPClauseName(OMPC_defaultmap); 10746 return nullptr; 10747 } 10748 10749 return new (Context) 10750 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 10751 } 10752 10753 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { 10754 DeclContext *CurLexicalContext = getCurLexicalContext(); 10755 if (!CurLexicalContext->isFileContext() && 10756 !CurLexicalContext->isExternCContext() && 10757 !CurLexicalContext->isExternCXXContext()) { 10758 Diag(Loc, diag::err_omp_region_not_file_context); 10759 return false; 10760 } 10761 if (IsInOpenMPDeclareTargetContext) { 10762 Diag(Loc, diag::err_omp_enclosed_declare_target); 10763 return false; 10764 } 10765 10766 IsInOpenMPDeclareTargetContext = true; 10767 return true; 10768 } 10769 10770 void Sema::ActOnFinishOpenMPDeclareTargetDirective() { 10771 assert(IsInOpenMPDeclareTargetContext && 10772 "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); 10773 10774 IsInOpenMPDeclareTargetContext = false; 10775 } 10776 10777 void 10778 Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec, 10779 const DeclarationNameInfo &Id, 10780 OMPDeclareTargetDeclAttr::MapTypeTy MT, 10781 NamedDeclSetType &SameDirectiveDecls) { 10782 LookupResult Lookup(*this, Id, LookupOrdinaryName); 10783 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 10784 10785 if (Lookup.isAmbiguous()) 10786 return; 10787 Lookup.suppressDiagnostics(); 10788 10789 if (!Lookup.isSingleResult()) { 10790 if (TypoCorrection Corrected = 10791 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, 10792 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this), 10793 CTK_ErrorRecovery)) { 10794 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 10795 << Id.getName()); 10796 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 10797 return; 10798 } 10799 10800 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 10801 return; 10802 } 10803 10804 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 10805 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) { 10806 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) 10807 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); 10808 10809 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) { 10810 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT); 10811 ND->addAttr(A); 10812 if (ASTMutationListener *ML = Context.getASTMutationListener()) 10813 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 10814 checkDeclIsAllowedInOpenMPTarget(nullptr, ND); 10815 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) { 10816 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link) 10817 << Id.getName(); 10818 } 10819 } else 10820 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 10821 } 10822 10823 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 10824 Sema &SemaRef, Decl *D) { 10825 if (!D) 10826 return; 10827 Decl *LD = nullptr; 10828 if (isa<TagDecl>(D)) { 10829 LD = cast<TagDecl>(D)->getDefinition(); 10830 } else if (isa<VarDecl>(D)) { 10831 LD = cast<VarDecl>(D)->getDefinition(); 10832 10833 // If this is an implicit variable that is legal and we do not need to do 10834 // anything. 10835 if (cast<VarDecl>(D)->isImplicit()) { 10836 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( 10837 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); 10838 D->addAttr(A); 10839 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) 10840 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 10841 return; 10842 } 10843 10844 } else if (isa<FunctionDecl>(D)) { 10845 const FunctionDecl *FD = nullptr; 10846 if (cast<FunctionDecl>(D)->hasBody(FD)) 10847 LD = const_cast<FunctionDecl *>(FD); 10848 10849 // If the definition is associated with the current declaration in the 10850 // target region (it can be e.g. a lambda) that is legal and we do not need 10851 // to do anything else. 10852 if (LD == D) { 10853 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( 10854 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); 10855 D->addAttr(A); 10856 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) 10857 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 10858 return; 10859 } 10860 } 10861 if (!LD) 10862 LD = D; 10863 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() && 10864 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) { 10865 // Outlined declaration is not declared target. 10866 if (LD->isOutOfLine()) { 10867 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context); 10868 SemaRef.Diag(SL, diag::note_used_here) << SR; 10869 } else { 10870 DeclContext *DC = LD->getDeclContext(); 10871 while (DC) { 10872 if (isa<FunctionDecl>(DC) && 10873 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>()) 10874 break; 10875 DC = DC->getParent(); 10876 } 10877 if (DC) 10878 return; 10879 10880 // Is not declared in target context. 10881 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context); 10882 SemaRef.Diag(SL, diag::note_used_here) << SR; 10883 } 10884 // Mark decl as declared target to prevent further diagnostic. 10885 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( 10886 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); 10887 D->addAttr(A); 10888 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) 10889 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 10890 } 10891 } 10892 10893 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 10894 Sema &SemaRef, DSAStackTy *Stack, 10895 ValueDecl *VD) { 10896 if (VD->hasAttr<OMPDeclareTargetDeclAttr>()) 10897 return true; 10898 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType())) 10899 return false; 10900 return true; 10901 } 10902 10903 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) { 10904 if (!D || D->isInvalidDecl()) 10905 return; 10906 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 10907 SourceLocation SL = E ? E->getLocStart() : D->getLocation(); 10908 // 2.10.6: threadprivate variable cannot appear in a declare target directive. 10909 if (VarDecl *VD = dyn_cast<VarDecl>(D)) { 10910 if (DSAStack->isThreadPrivate(VD)) { 10911 Diag(SL, diag::err_omp_threadprivate_in_target); 10912 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 10913 return; 10914 } 10915 } 10916 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { 10917 // Problem if any with var declared with incomplete type will be reported 10918 // as normal, so no need to check it here. 10919 if ((E || !VD->getType()->isIncompleteType()) && 10920 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) { 10921 // Mark decl as declared target to prevent further diagnostic. 10922 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) { 10923 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( 10924 Context, OMPDeclareTargetDeclAttr::MT_To); 10925 VD->addAttr(A); 10926 if (ASTMutationListener *ML = Context.getASTMutationListener()) 10927 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A); 10928 } 10929 return; 10930 } 10931 } 10932 if (!E) { 10933 // Checking declaration inside declare target region. 10934 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() && 10935 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) { 10936 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( 10937 Context, OMPDeclareTargetDeclAttr::MT_To); 10938 D->addAttr(A); 10939 if (ASTMutationListener *ML = Context.getASTMutationListener()) 10940 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 10941 } 10942 return; 10943 } 10944 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 10945 } 10946