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 typedef llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4> 61 OperatorOffsetTy; 62 63 private: 64 struct DSAInfo final { 65 OpenMPClauseKind Attributes = OMPC_unknown; 66 /// Pointer to a reference expression and a flag which shows that the 67 /// variable is marked as lastprivate(true) or not (false). 68 llvm::PointerIntPair<Expr *, 1, bool> RefExpr; 69 DeclRefExpr *PrivateCopy = nullptr; 70 }; 71 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy; 72 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy; 73 typedef std::pair<unsigned, VarDecl *> LCDeclInfo; 74 typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy; 75 /// Struct that associates a component with the clause kind where they are 76 /// found. 77 struct MappedExprComponentTy { 78 OMPClauseMappableExprCommon::MappableExprComponentLists Components; 79 OpenMPClauseKind Kind = OMPC_unknown; 80 }; 81 typedef llvm::DenseMap<ValueDecl *, MappedExprComponentTy> 82 MappedExprComponentsTy; 83 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>> 84 CriticalsWithHintsTy; 85 typedef llvm::DenseMap<OMPDependClause *, OperatorOffsetTy> 86 DoacrossDependMapTy; 87 88 struct SharingMapTy final { 89 DeclSAMapTy SharingMap; 90 AlignedMapTy AlignedMap; 91 MappedExprComponentsTy MappedExprComponents; 92 LoopControlVariablesMapTy LCVMap; 93 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified; 94 SourceLocation DefaultAttrLoc; 95 OpenMPDirectiveKind Directive = OMPD_unknown; 96 DeclarationNameInfo DirectiveName; 97 Scope *CurScope = nullptr; 98 SourceLocation ConstructLoc; 99 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to 100 /// get the data (loop counters etc.) about enclosing loop-based construct. 101 /// This data is required during codegen. 102 DoacrossDependMapTy DoacrossDepends; 103 /// \brief first argument (Expr *) contains optional argument of the 104 /// 'ordered' clause, the second one is true if the regions has 'ordered' 105 /// clause, false otherwise. 106 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion; 107 bool NowaitRegion = false; 108 bool CancelRegion = false; 109 unsigned AssociatedLoops = 1; 110 SourceLocation InnerTeamsRegionLoc; 111 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 112 Scope *CurScope, SourceLocation Loc) 113 : Directive(DKind), DirectiveName(Name), CurScope(CurScope), 114 ConstructLoc(Loc) {} 115 SharingMapTy() {} 116 }; 117 118 typedef SmallVector<SharingMapTy, 4> StackTy; 119 120 /// \brief Stack of used declaration and their data-sharing attributes. 121 StackTy Stack; 122 /// \brief true, if check for DSA must be from parent directive, false, if 123 /// from current directive. 124 OpenMPClauseKind ClauseKindMode = OMPC_unknown; 125 Sema &SemaRef; 126 bool ForceCapturing = false; 127 CriticalsWithHintsTy Criticals; 128 129 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator; 130 131 DSAVarData getDSA(StackTy::reverse_iterator &Iter, ValueDecl *D); 132 133 /// \brief Checks if the variable is a local for OpenMP region. 134 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter); 135 136 public: 137 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {} 138 139 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 140 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 141 142 bool isForceVarCapturing() const { return ForceCapturing; } 143 void setForceVarCapturing(bool V) { ForceCapturing = V; } 144 145 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 146 Scope *CurScope, SourceLocation Loc) { 147 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc)); 148 Stack.back().DefaultAttrLoc = Loc; 149 } 150 151 void pop() { 152 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!"); 153 Stack.pop_back(); 154 } 155 156 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) { 157 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint); 158 } 159 const std::pair<OMPCriticalDirective *, llvm::APSInt> 160 getCriticalWithHint(const DeclarationNameInfo &Name) const { 161 auto I = Criticals.find(Name.getAsString()); 162 if (I != Criticals.end()) 163 return I->second; 164 return std::make_pair(nullptr, llvm::APSInt()); 165 } 166 /// \brief If 'aligned' declaration for given variable \a D was not seen yet, 167 /// add it and return NULL; otherwise return previous occurrence's expression 168 /// for diagnostics. 169 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE); 170 171 /// \brief Register specified variable as loop control variable. 172 void addLoopControlVariable(ValueDecl *D, VarDecl *Capture); 173 /// \brief Check if the specified variable is a loop control variable for 174 /// current region. 175 /// \return The index of the loop control variable in the list of associated 176 /// for-loops (from outer to inner). 177 LCDeclInfo isLoopControlVariable(ValueDecl *D); 178 /// \brief Check if the specified variable is a loop control variable for 179 /// parent region. 180 /// \return The index of the loop control variable in the list of associated 181 /// for-loops (from outer to inner). 182 LCDeclInfo isParentLoopControlVariable(ValueDecl *D); 183 /// \brief Get the loop control variable for the I-th loop (or nullptr) in 184 /// parent directive. 185 ValueDecl *getParentLoopControlVariable(unsigned I); 186 187 /// \brief Adds explicit data sharing attribute to the specified declaration. 188 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A, 189 DeclRefExpr *PrivateCopy = nullptr); 190 191 /// \brief Returns data sharing attributes from top of the stack for the 192 /// specified declaration. 193 DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 194 /// \brief Returns data-sharing attributes for the specified declaration. 195 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent); 196 /// \brief Checks if the specified variables has data-sharing attributes which 197 /// match specified \a CPred predicate in any directive which matches \a DPred 198 /// predicate. 199 DSAVarData hasDSA(ValueDecl *D, 200 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 201 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 202 bool FromParent); 203 /// \brief Checks if the specified variables has data-sharing attributes which 204 /// match specified \a CPred predicate in any innermost directive which 205 /// matches \a DPred predicate. 206 DSAVarData 207 hasInnermostDSA(ValueDecl *D, 208 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 209 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 210 bool FromParent); 211 /// \brief Checks if the specified variables has explicit data-sharing 212 /// attributes which match specified \a CPred predicate at the specified 213 /// OpenMP region. 214 bool hasExplicitDSA(ValueDecl *D, 215 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 216 unsigned Level, bool NotLastprivate = false); 217 218 /// \brief Returns true if the directive at level \Level matches in the 219 /// specified \a DPred predicate. 220 bool hasExplicitDirective( 221 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 222 unsigned Level); 223 224 /// \brief Finds a directive which matches specified \a DPred predicate. 225 bool hasDirective(const llvm::function_ref<bool(OpenMPDirectiveKind, 226 const DeclarationNameInfo &, 227 SourceLocation)> &DPred, 228 bool FromParent); 229 230 /// \brief Returns currently analyzed directive. 231 OpenMPDirectiveKind getCurrentDirective() const { 232 return Stack.back().Directive; 233 } 234 /// \brief Returns parent directive. 235 OpenMPDirectiveKind getParentDirective() const { 236 if (Stack.size() > 2) 237 return Stack[Stack.size() - 2].Directive; 238 return OMPD_unknown; 239 } 240 241 /// \brief Set default data sharing attribute to none. 242 void setDefaultDSANone(SourceLocation Loc) { 243 Stack.back().DefaultAttr = DSA_none; 244 Stack.back().DefaultAttrLoc = Loc; 245 } 246 /// \brief Set default data sharing attribute to shared. 247 void setDefaultDSAShared(SourceLocation Loc) { 248 Stack.back().DefaultAttr = DSA_shared; 249 Stack.back().DefaultAttrLoc = Loc; 250 } 251 252 DefaultDataSharingAttributes getDefaultDSA() const { 253 return Stack.back().DefaultAttr; 254 } 255 SourceLocation getDefaultDSALocation() const { 256 return Stack.back().DefaultAttrLoc; 257 } 258 259 /// \brief Checks if the specified variable is a threadprivate. 260 bool isThreadPrivate(VarDecl *D) { 261 DSAVarData DVar = getTopDSA(D, false); 262 return isOpenMPThreadPrivate(DVar.CKind); 263 } 264 265 /// \brief Marks current region as ordered (it has an 'ordered' clause). 266 void setOrderedRegion(bool IsOrdered, Expr *Param) { 267 Stack.back().OrderedRegion.setInt(IsOrdered); 268 Stack.back().OrderedRegion.setPointer(Param); 269 } 270 /// \brief Returns true, if parent region is ordered (has associated 271 /// 'ordered' clause), false - otherwise. 272 bool isParentOrderedRegion() const { 273 if (Stack.size() > 2) 274 return Stack[Stack.size() - 2].OrderedRegion.getInt(); 275 return false; 276 } 277 /// \brief Returns optional parameter for the ordered region. 278 Expr *getParentOrderedRegionParam() const { 279 if (Stack.size() > 2) 280 return Stack[Stack.size() - 2].OrderedRegion.getPointer(); 281 return nullptr; 282 } 283 /// \brief Marks current region as nowait (it has a 'nowait' clause). 284 void setNowaitRegion(bool IsNowait = true) { 285 Stack.back().NowaitRegion = IsNowait; 286 } 287 /// \brief Returns true, if parent region is nowait (has associated 288 /// 'nowait' clause), false - otherwise. 289 bool isParentNowaitRegion() const { 290 if (Stack.size() > 2) 291 return Stack[Stack.size() - 2].NowaitRegion; 292 return false; 293 } 294 /// \brief Marks parent region as cancel region. 295 void setParentCancelRegion(bool Cancel = true) { 296 if (Stack.size() > 2) 297 Stack[Stack.size() - 2].CancelRegion = 298 Stack[Stack.size() - 2].CancelRegion || Cancel; 299 } 300 /// \brief Return true if current region has inner cancel construct. 301 bool isCancelRegion() const { return Stack.back().CancelRegion; } 302 303 /// \brief Set collapse value for the region. 304 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; } 305 /// \brief Return collapse value for region. 306 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; } 307 308 /// \brief Marks current target region as one with closely nested teams 309 /// region. 310 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 311 if (Stack.size() > 2) 312 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc; 313 } 314 /// \brief Returns true, if current region has closely nested teams region. 315 bool hasInnerTeamsRegion() const { 316 return getInnerTeamsRegionLoc().isValid(); 317 } 318 /// \brief Returns location of the nested teams region (if any). 319 SourceLocation getInnerTeamsRegionLoc() const { 320 if (Stack.size() > 1) 321 return Stack.back().InnerTeamsRegionLoc; 322 return SourceLocation(); 323 } 324 325 Scope *getCurScope() const { return Stack.back().CurScope; } 326 Scope *getCurScope() { return Stack.back().CurScope; } 327 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; } 328 329 /// Do the check specified in \a Check to all component lists and return true 330 /// if any issue is found. 331 bool checkMappableExprComponentListsForDecl( 332 ValueDecl *VD, bool CurrentRegionOnly, 333 const llvm::function_ref< 334 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 335 OpenMPClauseKind)> &Check) { 336 auto SI = Stack.rbegin(); 337 auto SE = Stack.rend(); 338 339 if (SI == SE) 340 return false; 341 342 if (CurrentRegionOnly) { 343 SE = std::next(SI); 344 } else { 345 ++SI; 346 } 347 348 for (; SI != SE; ++SI) { 349 auto MI = SI->MappedExprComponents.find(VD); 350 if (MI != SI->MappedExprComponents.end()) 351 for (auto &L : MI->second.Components) 352 if (Check(L, MI->second.Kind)) 353 return true; 354 } 355 return false; 356 } 357 358 /// Create a new mappable expression component list associated with a given 359 /// declaration and initialize it with the provided list of components. 360 void addMappableExpressionComponents( 361 ValueDecl *VD, 362 OMPClauseMappableExprCommon::MappableExprComponentListRef Components, 363 OpenMPClauseKind WhereFoundClauseKind) { 364 assert(Stack.size() > 1 && 365 "Not expecting to retrieve components from a empty stack!"); 366 auto &MEC = Stack.back().MappedExprComponents[VD]; 367 // Create new entry and append the new components there. 368 MEC.Components.resize(MEC.Components.size() + 1); 369 MEC.Components.back().append(Components.begin(), Components.end()); 370 MEC.Kind = WhereFoundClauseKind; 371 } 372 373 unsigned getNestingLevel() const { 374 assert(Stack.size() > 1); 375 return Stack.size() - 2; 376 } 377 void addDoacrossDependClause(OMPDependClause *C, OperatorOffsetTy &OpsOffs) { 378 assert(Stack.size() > 2); 379 assert(isOpenMPWorksharingDirective(Stack[Stack.size() - 2].Directive)); 380 Stack[Stack.size() - 2].DoacrossDepends.insert({C, OpsOffs}); 381 } 382 llvm::iterator_range<DoacrossDependMapTy::const_iterator> 383 getDoacrossDependClauses() const { 384 assert(Stack.size() > 1); 385 if (isOpenMPWorksharingDirective(Stack[Stack.size() - 1].Directive)) { 386 auto &Ref = Stack[Stack.size() - 1].DoacrossDepends; 387 return llvm::make_range(Ref.begin(), Ref.end()); 388 } 389 return llvm::make_range(Stack[0].DoacrossDepends.end(), 390 Stack[0].DoacrossDepends.end()); 391 } 392 }; 393 bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) { 394 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) || 395 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown; 396 } 397 } // namespace 398 399 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 400 auto *VD = dyn_cast<VarDecl>(D); 401 auto *FD = dyn_cast<FieldDecl>(D); 402 if (VD != nullptr) { 403 VD = VD->getCanonicalDecl(); 404 D = VD; 405 } else { 406 assert(FD); 407 FD = FD->getCanonicalDecl(); 408 D = FD; 409 } 410 return D; 411 } 412 413 DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator &Iter, 414 ValueDecl *D) { 415 D = getCanonicalDecl(D); 416 auto *VD = dyn_cast<VarDecl>(D); 417 auto *FD = dyn_cast<FieldDecl>(D); 418 DSAVarData DVar; 419 if (Iter == std::prev(Stack.rend())) { 420 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 421 // in a region but not in construct] 422 // File-scope or namespace-scope variables referenced in called routines 423 // in the region are shared unless they appear in a threadprivate 424 // directive. 425 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D)) 426 DVar.CKind = OMPC_shared; 427 428 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 429 // in a region but not in construct] 430 // Variables with static storage duration that are declared in called 431 // routines in the region are shared. 432 if (VD && VD->hasGlobalStorage()) 433 DVar.CKind = OMPC_shared; 434 435 // Non-static data members are shared by default. 436 if (FD) 437 DVar.CKind = OMPC_shared; 438 439 return DVar; 440 } 441 442 DVar.DKind = Iter->Directive; 443 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 444 // in a Construct, C/C++, predetermined, p.1] 445 // Variables with automatic storage duration that are declared in a scope 446 // inside the construct are private. 447 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 448 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 449 DVar.CKind = OMPC_private; 450 return DVar; 451 } 452 453 // Explicitly specified attributes and local variables with predetermined 454 // attributes. 455 if (Iter->SharingMap.count(D)) { 456 DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer(); 457 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy; 458 DVar.CKind = Iter->SharingMap[D].Attributes; 459 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 460 return DVar; 461 } 462 463 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 464 // in a Construct, C/C++, implicitly determined, p.1] 465 // In a parallel or task construct, the data-sharing attributes of these 466 // variables are determined by the default clause, if present. 467 switch (Iter->DefaultAttr) { 468 case DSA_shared: 469 DVar.CKind = OMPC_shared; 470 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 471 return DVar; 472 case DSA_none: 473 return DVar; 474 case DSA_unspecified: 475 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 476 // in a Construct, implicitly determined, p.2] 477 // In a parallel construct, if no default clause is present, these 478 // variables are shared. 479 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 480 if (isOpenMPParallelDirective(DVar.DKind) || 481 isOpenMPTeamsDirective(DVar.DKind)) { 482 DVar.CKind = OMPC_shared; 483 return DVar; 484 } 485 486 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 487 // in a Construct, implicitly determined, p.4] 488 // In a task construct, if no default clause is present, a variable that in 489 // the enclosing context is determined to be shared by all implicit tasks 490 // bound to the current team is shared. 491 if (isOpenMPTaskingDirective(DVar.DKind)) { 492 DSAVarData DVarTemp; 493 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend(); 494 I != EE; ++I) { 495 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 496 // Referenced in a Construct, implicitly determined, p.6] 497 // In a task construct, if no default clause is present, a variable 498 // whose data-sharing attribute is not determined by the rules above is 499 // firstprivate. 500 DVarTemp = getDSA(I, D); 501 if (DVarTemp.CKind != OMPC_shared) { 502 DVar.RefExpr = nullptr; 503 DVar.CKind = OMPC_firstprivate; 504 return DVar; 505 } 506 if (isParallelOrTaskRegion(I->Directive)) 507 break; 508 } 509 DVar.CKind = 510 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 511 return DVar; 512 } 513 } 514 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 515 // in a Construct, implicitly determined, p.3] 516 // For constructs other than task, if no default clause is present, these 517 // variables inherit their data-sharing attributes from the enclosing 518 // context. 519 return getDSA(++Iter, D); 520 } 521 522 Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) { 523 assert(Stack.size() > 1 && "Data sharing attributes stack is empty"); 524 D = getCanonicalDecl(D); 525 auto It = Stack.back().AlignedMap.find(D); 526 if (It == Stack.back().AlignedMap.end()) { 527 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 528 Stack.back().AlignedMap[D] = NewDE; 529 return nullptr; 530 } else { 531 assert(It->second && "Unexpected nullptr expr in the aligned map"); 532 return It->second; 533 } 534 return nullptr; 535 } 536 537 void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) { 538 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); 539 D = getCanonicalDecl(D); 540 Stack.back().LCVMap.insert( 541 std::make_pair(D, LCDeclInfo(Stack.back().LCVMap.size() + 1, Capture))); 542 } 543 544 DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) { 545 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); 546 D = getCanonicalDecl(D); 547 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D] 548 : LCDeclInfo(0, nullptr); 549 } 550 551 DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) { 552 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty"); 553 D = getCanonicalDecl(D); 554 return Stack[Stack.size() - 2].LCVMap.count(D) > 0 555 ? Stack[Stack.size() - 2].LCVMap[D] 556 : LCDeclInfo(0, nullptr); 557 } 558 559 ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) { 560 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty"); 561 if (Stack[Stack.size() - 2].LCVMap.size() < I) 562 return nullptr; 563 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) { 564 if (Pair.second.first == I) 565 return Pair.first; 566 } 567 return nullptr; 568 } 569 570 void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A, 571 DeclRefExpr *PrivateCopy) { 572 D = getCanonicalDecl(D); 573 if (A == OMPC_threadprivate) { 574 auto &Data = Stack[0].SharingMap[D]; 575 Data.Attributes = A; 576 Data.RefExpr.setPointer(E); 577 Data.PrivateCopy = nullptr; 578 } else { 579 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); 580 auto &Data = Stack.back().SharingMap[D]; 581 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 582 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 583 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 584 (isLoopControlVariable(D).first && A == OMPC_private)); 585 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 586 Data.RefExpr.setInt(/*IntVal=*/true); 587 return; 588 } 589 const bool IsLastprivate = 590 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 591 Data.Attributes = A; 592 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 593 Data.PrivateCopy = PrivateCopy; 594 if (PrivateCopy) { 595 auto &Data = Stack.back().SharingMap[PrivateCopy->getDecl()]; 596 Data.Attributes = A; 597 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 598 Data.PrivateCopy = nullptr; 599 } 600 } 601 } 602 603 bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) { 604 D = D->getCanonicalDecl(); 605 if (Stack.size() > 2) { 606 reverse_iterator I = Iter, E = std::prev(Stack.rend()); 607 Scope *TopScope = nullptr; 608 while (I != E && !isParallelOrTaskRegion(I->Directive)) { 609 ++I; 610 } 611 if (I == E) 612 return false; 613 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; 614 Scope *CurScope = getCurScope(); 615 while (CurScope != TopScope && !CurScope->isDeclScope(D)) { 616 CurScope = CurScope->getParent(); 617 } 618 return CurScope != TopScope; 619 } 620 return false; 621 } 622 623 /// \brief Build a variable declaration for OpenMP loop iteration variable. 624 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 625 StringRef Name, const AttrVec *Attrs = nullptr) { 626 DeclContext *DC = SemaRef.CurContext; 627 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 628 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 629 VarDecl *Decl = 630 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 631 if (Attrs) { 632 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 633 I != E; ++I) 634 Decl->addAttr(*I); 635 } 636 Decl->setImplicit(); 637 return Decl; 638 } 639 640 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 641 SourceLocation Loc, 642 bool RefersToCapture = false) { 643 D->setReferenced(); 644 D->markUsed(S.Context); 645 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 646 SourceLocation(), D, RefersToCapture, Loc, Ty, 647 VK_LValue); 648 } 649 650 DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) { 651 D = getCanonicalDecl(D); 652 DSAVarData DVar; 653 654 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 655 // in a Construct, C/C++, predetermined, p.1] 656 // Variables appearing in threadprivate directives are threadprivate. 657 auto *VD = dyn_cast<VarDecl>(D); 658 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 659 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 660 SemaRef.getLangOpts().OpenMPUseTLS && 661 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 662 (VD && VD->getStorageClass() == SC_Register && 663 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 664 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 665 D->getLocation()), 666 OMPC_threadprivate); 667 } 668 if (Stack[0].SharingMap.count(D)) { 669 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr.getPointer(); 670 DVar.CKind = OMPC_threadprivate; 671 return DVar; 672 } 673 674 if (Stack.size() == 1) { 675 // Not in OpenMP execution region and top scope was already checked. 676 return DVar; 677 } 678 679 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 680 // in a Construct, C/C++, predetermined, p.4] 681 // Static data members are shared. 682 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 683 // in a Construct, C/C++, predetermined, p.7] 684 // Variables with static storage duration that are declared in a scope 685 // inside the construct are shared. 686 auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; }; 687 if (VD && VD->isStaticDataMember()) { 688 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent); 689 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 690 return DVar; 691 692 DVar.CKind = OMPC_shared; 693 return DVar; 694 } 695 696 QualType Type = D->getType().getNonReferenceType().getCanonicalType(); 697 bool IsConstant = Type.isConstant(SemaRef.getASTContext()); 698 Type = SemaRef.getASTContext().getBaseElementType(Type); 699 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 700 // in a Construct, C/C++, predetermined, p.6] 701 // Variables with const qualified type having no mutable member are 702 // shared. 703 CXXRecordDecl *RD = 704 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; 705 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 706 if (auto *CTD = CTSD->getSpecializedTemplate()) 707 RD = CTD->getTemplatedDecl(); 708 if (IsConstant && 709 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() && 710 RD->hasMutableFields())) { 711 // Variables with const-qualified type having no mutable member may be 712 // listed in a firstprivate clause, even if they are static data members. 713 DSAVarData DVarTemp = hasDSA( 714 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; }, 715 MatchesAlways, FromParent); 716 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr) 717 return DVar; 718 719 DVar.CKind = OMPC_shared; 720 return DVar; 721 } 722 723 // Explicitly specified attributes and local variables with predetermined 724 // attributes. 725 auto StartI = std::next(Stack.rbegin()); 726 auto EndI = std::prev(Stack.rend()); 727 if (FromParent && StartI != EndI) { 728 StartI = std::next(StartI); 729 } 730 auto I = std::prev(StartI); 731 if (I->SharingMap.count(D)) { 732 DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer(); 733 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy; 734 DVar.CKind = I->SharingMap[D].Attributes; 735 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 736 } 737 738 return DVar; 739 } 740 741 DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 742 bool FromParent) { 743 D = getCanonicalDecl(D); 744 auto StartI = Stack.rbegin(); 745 auto EndI = std::prev(Stack.rend()); 746 if (FromParent && StartI != EndI) { 747 StartI = std::next(StartI); 748 } 749 return getDSA(StartI, D); 750 } 751 752 DSAStackTy::DSAVarData 753 DSAStackTy::hasDSA(ValueDecl *D, 754 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 755 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 756 bool FromParent) { 757 D = getCanonicalDecl(D); 758 auto StartI = std::next(Stack.rbegin()); 759 auto EndI = Stack.rend(); 760 if (FromParent && StartI != EndI) { 761 StartI = std::next(StartI); 762 } 763 for (auto I = StartI, EE = EndI; I != EE; ++I) { 764 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive)) 765 continue; 766 DSAVarData DVar = getDSA(I, D); 767 if (CPred(DVar.CKind)) 768 return DVar; 769 } 770 return DSAVarData(); 771 } 772 773 DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 774 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 775 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 776 bool FromParent) { 777 D = getCanonicalDecl(D); 778 auto StartI = std::next(Stack.rbegin()); 779 auto EndI = Stack.rend(); 780 if (FromParent && StartI != EndI) 781 StartI = std::next(StartI); 782 if (StartI == EndI || !DPred(StartI->Directive)) 783 return DSAVarData(); 784 DSAVarData DVar = getDSA(StartI, D); 785 return CPred(DVar.CKind) ? DVar : DSAVarData(); 786 } 787 788 bool DSAStackTy::hasExplicitDSA( 789 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred, 790 unsigned Level, bool NotLastprivate) { 791 if (CPred(ClauseKindMode)) 792 return true; 793 D = getCanonicalDecl(D); 794 auto StartI = std::next(Stack.begin()); 795 auto EndI = Stack.end(); 796 if (std::distance(StartI, EndI) <= (int)Level) 797 return false; 798 std::advance(StartI, Level); 799 return (StartI->SharingMap.count(D) > 0) && 800 StartI->SharingMap[D].RefExpr.getPointer() && 801 CPred(StartI->SharingMap[D].Attributes) && 802 (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt()); 803 } 804 805 bool DSAStackTy::hasExplicitDirective( 806 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred, 807 unsigned Level) { 808 auto StartI = std::next(Stack.begin()); 809 auto EndI = Stack.end(); 810 if (std::distance(StartI, EndI) <= (int)Level) 811 return false; 812 std::advance(StartI, Level); 813 return DPred(StartI->Directive); 814 } 815 816 bool DSAStackTy::hasDirective( 817 const llvm::function_ref<bool(OpenMPDirectiveKind, 818 const DeclarationNameInfo &, SourceLocation)> 819 &DPred, 820 bool FromParent) { 821 // We look only in the enclosing region. 822 if (Stack.size() < 2) 823 return false; 824 auto StartI = std::next(Stack.rbegin()); 825 auto EndI = std::prev(Stack.rend()); 826 if (FromParent && StartI != EndI) { 827 StartI = std::next(StartI); 828 } 829 for (auto I = StartI, EE = EndI; I != EE; ++I) { 830 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 831 return true; 832 } 833 return false; 834 } 835 836 void Sema::InitDataSharingAttributesStack() { 837 VarDataSharingAttributesStack = new DSAStackTy(*this); 838 } 839 840 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 841 842 bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) { 843 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 844 845 auto &Ctx = getASTContext(); 846 bool IsByRef = true; 847 848 // Find the directive that is associated with the provided scope. 849 auto Ty = D->getType(); 850 851 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 852 // This table summarizes how a given variable should be passed to the device 853 // given its type and the clauses where it appears. This table is based on 854 // the description in OpenMP 4.5 [2.10.4, target Construct] and 855 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 856 // 857 // ========================================================================= 858 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 859 // | |(tofrom:scalar)| | pvt | | | | 860 // ========================================================================= 861 // | scl | | | | - | | bycopy| 862 // | scl | | - | x | - | - | bycopy| 863 // | scl | | x | - | - | - | null | 864 // | scl | x | | | - | | byref | 865 // | scl | x | - | x | - | - | bycopy| 866 // | scl | x | x | - | - | - | null | 867 // | scl | | - | - | - | x | byref | 868 // | scl | x | - | - | - | x | byref | 869 // 870 // | agg | n.a. | | | - | | byref | 871 // | agg | n.a. | - | x | - | - | byref | 872 // | agg | n.a. | x | - | - | - | null | 873 // | agg | n.a. | - | - | - | x | byref | 874 // | agg | n.a. | - | - | - | x[] | byref | 875 // 876 // | ptr | n.a. | | | - | | bycopy| 877 // | ptr | n.a. | - | x | - | - | bycopy| 878 // | ptr | n.a. | x | - | - | - | null | 879 // | ptr | n.a. | - | - | - | x | byref | 880 // | ptr | n.a. | - | - | - | x[] | bycopy| 881 // | ptr | n.a. | - | - | x | | bycopy| 882 // | ptr | n.a. | - | - | x | x | bycopy| 883 // | ptr | n.a. | - | - | x | x[] | bycopy| 884 // ========================================================================= 885 // Legend: 886 // scl - scalar 887 // ptr - pointer 888 // agg - aggregate 889 // x - applies 890 // - - invalid in this combination 891 // [] - mapped with an array section 892 // byref - should be mapped by reference 893 // byval - should be mapped by value 894 // null - initialize a local variable to null on the device 895 // 896 // Observations: 897 // - All scalar declarations that show up in a map clause have to be passed 898 // by reference, because they may have been mapped in the enclosing data 899 // environment. 900 // - If the scalar value does not fit the size of uintptr, it has to be 901 // passed by reference, regardless the result in the table above. 902 // - For pointers mapped by value that have either an implicit map or an 903 // array section, the runtime library may pass the NULL value to the 904 // device instead of the value passed to it by the compiler. 905 906 if (Ty->isReferenceType()) 907 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 908 909 // Locate map clauses and see if the variable being captured is referred to 910 // in any of those clauses. Here we only care about variables, not fields, 911 // because fields are part of aggregates. 912 bool IsVariableUsedInMapClause = false; 913 bool IsVariableAssociatedWithSection = false; 914 915 DSAStack->checkMappableExprComponentListsForDecl( 916 D, /*CurrentRegionOnly=*/true, 917 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef 918 MapExprComponents, 919 OpenMPClauseKind WhereFoundClauseKind) { 920 // Only the map clause information influences how a variable is 921 // captured. E.g. is_device_ptr does not require changing the default 922 // behavior. 923 if (WhereFoundClauseKind != OMPC_map) 924 return false; 925 926 auto EI = MapExprComponents.rbegin(); 927 auto EE = MapExprComponents.rend(); 928 929 assert(EI != EE && "Invalid map expression!"); 930 931 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 932 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 933 934 ++EI; 935 if (EI == EE) 936 return false; 937 938 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 939 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 940 isa<MemberExpr>(EI->getAssociatedExpression())) { 941 IsVariableAssociatedWithSection = true; 942 // There is nothing more we need to know about this variable. 943 return true; 944 } 945 946 // Keep looking for more map info. 947 return false; 948 }); 949 950 if (IsVariableUsedInMapClause) { 951 // If variable is identified in a map clause it is always captured by 952 // reference except if it is a pointer that is dereferenced somehow. 953 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 954 } else { 955 // By default, all the data that has a scalar type is mapped by copy. 956 IsByRef = !Ty->isScalarType(); 957 } 958 } 959 960 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 961 IsByRef = !DSAStack->hasExplicitDSA( 962 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; }, 963 Level, /*NotLastprivate=*/true); 964 } 965 966 // When passing data by copy, we need to make sure it fits the uintptr size 967 // and alignment, because the runtime library only deals with uintptr types. 968 // If it does not fit the uintptr size, we need to pass the data by reference 969 // instead. 970 if (!IsByRef && 971 (Ctx.getTypeSizeInChars(Ty) > 972 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 973 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 974 IsByRef = true; 975 } 976 977 return IsByRef; 978 } 979 980 unsigned Sema::getOpenMPNestingLevel() const { 981 assert(getLangOpts().OpenMP); 982 return DSAStack->getNestingLevel(); 983 } 984 985 VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) { 986 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 987 D = getCanonicalDecl(D); 988 989 // If we are attempting to capture a global variable in a directive with 990 // 'target' we return true so that this global is also mapped to the device. 991 // 992 // FIXME: If the declaration is enclosed in a 'declare target' directive, 993 // then it should not be captured. Therefore, an extra check has to be 994 // inserted here once support for 'declare target' is added. 995 // 996 auto *VD = dyn_cast<VarDecl>(D); 997 if (VD && !VD->hasLocalStorage()) { 998 if (DSAStack->getCurrentDirective() == OMPD_target && 999 !DSAStack->isClauseParsingMode()) 1000 return VD; 1001 if (DSAStack->hasDirective( 1002 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 1003 SourceLocation) -> bool { 1004 return isOpenMPTargetExecutionDirective(K); 1005 }, 1006 false)) 1007 return VD; 1008 } 1009 1010 if (DSAStack->getCurrentDirective() != OMPD_unknown && 1011 (!DSAStack->isClauseParsingMode() || 1012 DSAStack->getParentDirective() != OMPD_unknown)) { 1013 auto &&Info = DSAStack->isLoopControlVariable(D); 1014 if (Info.first || 1015 (VD && VD->hasLocalStorage() && 1016 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) || 1017 (VD && DSAStack->isForceVarCapturing())) 1018 return VD ? VD : Info.second; 1019 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 1020 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind)) 1021 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1022 DVarPrivate = DSAStack->hasDSA( 1023 D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; }, 1024 DSAStack->isClauseParsingMode()); 1025 if (DVarPrivate.CKind != OMPC_unknown) 1026 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1027 } 1028 return nullptr; 1029 } 1030 1031 bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) { 1032 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1033 return DSAStack->hasExplicitDSA( 1034 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level); 1035 } 1036 1037 bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) { 1038 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1039 // Return true if the current level is no longer enclosed in a target region. 1040 1041 auto *VD = dyn_cast<VarDecl>(D); 1042 return VD && !VD->hasLocalStorage() && 1043 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 1044 Level); 1045 } 1046 1047 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 1048 1049 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 1050 const DeclarationNameInfo &DirName, 1051 Scope *CurScope, SourceLocation Loc) { 1052 DSAStack->push(DKind, DirName, CurScope, Loc); 1053 PushExpressionEvaluationContext(PotentiallyEvaluated); 1054 } 1055 1056 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 1057 DSAStack->setClauseParsingMode(K); 1058 } 1059 1060 void Sema::EndOpenMPClause() { 1061 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 1062 } 1063 1064 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 1065 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 1066 // A variable of class type (or array thereof) that appears in a lastprivate 1067 // clause requires an accessible, unambiguous default constructor for the 1068 // class type, unless the list item is also specified in a firstprivate 1069 // clause. 1070 if (auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 1071 for (auto *C : D->clauses()) { 1072 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 1073 SmallVector<Expr *, 8> PrivateCopies; 1074 for (auto *DE : Clause->varlists()) { 1075 if (DE->isValueDependent() || DE->isTypeDependent()) { 1076 PrivateCopies.push_back(nullptr); 1077 continue; 1078 } 1079 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 1080 VarDecl *VD = cast<VarDecl>(DRE->getDecl()); 1081 QualType Type = VD->getType().getNonReferenceType(); 1082 auto DVar = DSAStack->getTopDSA(VD, false); 1083 if (DVar.CKind == OMPC_lastprivate) { 1084 // Generate helper private variable and initialize it with the 1085 // default value. The address of the original variable is replaced 1086 // by the address of the new private variable in CodeGen. This new 1087 // variable is not added to IdResolver, so the code in the OpenMP 1088 // region uses original variable for proper diagnostics. 1089 auto *VDPrivate = buildVarDecl( 1090 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 1091 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr); 1092 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false); 1093 if (VDPrivate->isInvalidDecl()) 1094 continue; 1095 PrivateCopies.push_back(buildDeclRefExpr( 1096 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 1097 } else { 1098 // The variable is also a firstprivate, so initialization sequence 1099 // for private copy is generated already. 1100 PrivateCopies.push_back(nullptr); 1101 } 1102 } 1103 // Set initializers to private copies if no errors were found. 1104 if (PrivateCopies.size() == Clause->varlist_size()) 1105 Clause->setPrivateCopies(PrivateCopies); 1106 } 1107 } 1108 } 1109 1110 DSAStack->pop(); 1111 DiscardCleanupsInEvaluationContext(); 1112 PopExpressionEvaluationContext(); 1113 } 1114 1115 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 1116 Expr *NumIterations, Sema &SemaRef, 1117 Scope *S, DSAStackTy *Stack); 1118 1119 namespace { 1120 1121 class VarDeclFilterCCC : public CorrectionCandidateCallback { 1122 private: 1123 Sema &SemaRef; 1124 1125 public: 1126 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 1127 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1128 NamedDecl *ND = Candidate.getCorrectionDecl(); 1129 if (auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 1130 return VD->hasGlobalStorage() && 1131 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1132 SemaRef.getCurScope()); 1133 } 1134 return false; 1135 } 1136 }; 1137 1138 class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback { 1139 private: 1140 Sema &SemaRef; 1141 1142 public: 1143 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 1144 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1145 NamedDecl *ND = Candidate.getCorrectionDecl(); 1146 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) { 1147 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1148 SemaRef.getCurScope()); 1149 } 1150 return false; 1151 } 1152 }; 1153 1154 } // namespace 1155 1156 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 1157 CXXScopeSpec &ScopeSpec, 1158 const DeclarationNameInfo &Id) { 1159 LookupResult Lookup(*this, Id, LookupOrdinaryName); 1160 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 1161 1162 if (Lookup.isAmbiguous()) 1163 return ExprError(); 1164 1165 VarDecl *VD; 1166 if (!Lookup.isSingleResult()) { 1167 if (TypoCorrection Corrected = CorrectTypo( 1168 Id, LookupOrdinaryName, CurScope, nullptr, 1169 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) { 1170 diagnoseTypo(Corrected, 1171 PDiag(Lookup.empty() 1172 ? diag::err_undeclared_var_use_suggest 1173 : diag::err_omp_expected_var_arg_suggest) 1174 << Id.getName()); 1175 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 1176 } else { 1177 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 1178 : diag::err_omp_expected_var_arg) 1179 << Id.getName(); 1180 return ExprError(); 1181 } 1182 } else { 1183 if (!(VD = Lookup.getAsSingle<VarDecl>())) { 1184 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 1185 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 1186 return ExprError(); 1187 } 1188 } 1189 Lookup.suppressDiagnostics(); 1190 1191 // OpenMP [2.9.2, Syntax, C/C++] 1192 // Variables must be file-scope, namespace-scope, or static block-scope. 1193 if (!VD->hasGlobalStorage()) { 1194 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 1195 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal(); 1196 bool IsDecl = 1197 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1198 Diag(VD->getLocation(), 1199 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1200 << VD; 1201 return ExprError(); 1202 } 1203 1204 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 1205 NamedDecl *ND = cast<NamedDecl>(CanonicalVD); 1206 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 1207 // A threadprivate directive for file-scope variables must appear outside 1208 // any definition or declaration. 1209 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 1210 !getCurLexicalContext()->isTranslationUnit()) { 1211 Diag(Id.getLoc(), diag::err_omp_var_scope) 1212 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1213 bool IsDecl = 1214 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1215 Diag(VD->getLocation(), 1216 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1217 << VD; 1218 return ExprError(); 1219 } 1220 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 1221 // A threadprivate directive for static class member variables must appear 1222 // in the class definition, in the same scope in which the member 1223 // variables are declared. 1224 if (CanonicalVD->isStaticDataMember() && 1225 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 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 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 1236 // A threadprivate directive for namespace-scope variables must appear 1237 // outside any definition or declaration other than the namespace 1238 // definition itself. 1239 if (CanonicalVD->getDeclContext()->isNamespace() && 1240 (!getCurLexicalContext()->isFileContext() || 1241 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 1242 Diag(Id.getLoc(), diag::err_omp_var_scope) 1243 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1244 bool IsDecl = 1245 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1246 Diag(VD->getLocation(), 1247 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1248 << VD; 1249 return ExprError(); 1250 } 1251 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 1252 // A threadprivate directive for static block-scope variables must appear 1253 // in the scope of the variable and not in a nested scope. 1254 if (CanonicalVD->isStaticLocal() && CurScope && 1255 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 1256 Diag(Id.getLoc(), diag::err_omp_var_scope) 1257 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1258 bool IsDecl = 1259 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1260 Diag(VD->getLocation(), 1261 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1262 << VD; 1263 return ExprError(); 1264 } 1265 1266 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 1267 // A threadprivate directive must lexically precede all references to any 1268 // of the variables in its list. 1269 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) { 1270 Diag(Id.getLoc(), diag::err_omp_var_used) 1271 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1272 return ExprError(); 1273 } 1274 1275 QualType ExprType = VD->getType().getNonReferenceType(); 1276 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 1277 SourceLocation(), VD, 1278 /*RefersToEnclosingVariableOrCapture=*/false, 1279 Id.getLoc(), ExprType, VK_LValue); 1280 } 1281 1282 Sema::DeclGroupPtrTy 1283 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 1284 ArrayRef<Expr *> VarList) { 1285 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 1286 CurContext->addDecl(D); 1287 return DeclGroupPtrTy::make(DeclGroupRef(D)); 1288 } 1289 return nullptr; 1290 } 1291 1292 namespace { 1293 class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> { 1294 Sema &SemaRef; 1295 1296 public: 1297 bool VisitDeclRefExpr(const DeclRefExpr *E) { 1298 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 1299 if (VD->hasLocalStorage()) { 1300 SemaRef.Diag(E->getLocStart(), 1301 diag::err_omp_local_var_in_threadprivate_init) 1302 << E->getSourceRange(); 1303 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 1304 << VD << VD->getSourceRange(); 1305 return true; 1306 } 1307 } 1308 return false; 1309 } 1310 bool VisitStmt(const Stmt *S) { 1311 for (auto Child : S->children()) { 1312 if (Child && Visit(Child)) 1313 return true; 1314 } 1315 return false; 1316 } 1317 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 1318 }; 1319 } // namespace 1320 1321 OMPThreadPrivateDecl * 1322 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 1323 SmallVector<Expr *, 8> Vars; 1324 for (auto &RefExpr : VarList) { 1325 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr); 1326 VarDecl *VD = cast<VarDecl>(DE->getDecl()); 1327 SourceLocation ILoc = DE->getExprLoc(); 1328 1329 // Mark variable as used. 1330 VD->setReferenced(); 1331 VD->markUsed(Context); 1332 1333 QualType QType = VD->getType(); 1334 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 1335 // It will be analyzed later. 1336 Vars.push_back(DE); 1337 continue; 1338 } 1339 1340 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 1341 // A threadprivate variable must not have an incomplete type. 1342 if (RequireCompleteType(ILoc, VD->getType(), 1343 diag::err_omp_threadprivate_incomplete_type)) { 1344 continue; 1345 } 1346 1347 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 1348 // A threadprivate variable must not have a reference type. 1349 if (VD->getType()->isReferenceType()) { 1350 Diag(ILoc, diag::err_omp_ref_type_arg) 1351 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 1352 bool IsDecl = 1353 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1354 Diag(VD->getLocation(), 1355 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1356 << VD; 1357 continue; 1358 } 1359 1360 // Check if this is a TLS variable. If TLS is not being supported, produce 1361 // the corresponding diagnostic. 1362 if ((VD->getTLSKind() != VarDecl::TLS_None && 1363 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1364 getLangOpts().OpenMPUseTLS && 1365 getASTContext().getTargetInfo().isTLSSupported())) || 1366 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 1367 !VD->isLocalVarDecl())) { 1368 Diag(ILoc, diag::err_omp_var_thread_local) 1369 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 1370 bool IsDecl = 1371 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1372 Diag(VD->getLocation(), 1373 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1374 << VD; 1375 continue; 1376 } 1377 1378 // Check if initial value of threadprivate variable reference variable with 1379 // local storage (it is not supported by runtime). 1380 if (auto Init = VD->getAnyInitializer()) { 1381 LocalVarRefChecker Checker(*this); 1382 if (Checker.Visit(Init)) 1383 continue; 1384 } 1385 1386 Vars.push_back(RefExpr); 1387 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 1388 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 1389 Context, SourceRange(Loc, Loc))); 1390 if (auto *ML = Context.getASTMutationListener()) 1391 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 1392 } 1393 OMPThreadPrivateDecl *D = nullptr; 1394 if (!Vars.empty()) { 1395 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 1396 Vars); 1397 D->setAccess(AS_public); 1398 } 1399 return D; 1400 } 1401 1402 static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack, 1403 const ValueDecl *D, DSAStackTy::DSAVarData DVar, 1404 bool IsLoopIterVar = false) { 1405 if (DVar.RefExpr) { 1406 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 1407 << getOpenMPClauseName(DVar.CKind); 1408 return; 1409 } 1410 enum { 1411 PDSA_StaticMemberShared, 1412 PDSA_StaticLocalVarShared, 1413 PDSA_LoopIterVarPrivate, 1414 PDSA_LoopIterVarLinear, 1415 PDSA_LoopIterVarLastprivate, 1416 PDSA_ConstVarShared, 1417 PDSA_GlobalVarShared, 1418 PDSA_TaskVarFirstprivate, 1419 PDSA_LocalVarPrivate, 1420 PDSA_Implicit 1421 } Reason = PDSA_Implicit; 1422 bool ReportHint = false; 1423 auto ReportLoc = D->getLocation(); 1424 auto *VD = dyn_cast<VarDecl>(D); 1425 if (IsLoopIterVar) { 1426 if (DVar.CKind == OMPC_private) 1427 Reason = PDSA_LoopIterVarPrivate; 1428 else if (DVar.CKind == OMPC_lastprivate) 1429 Reason = PDSA_LoopIterVarLastprivate; 1430 else 1431 Reason = PDSA_LoopIterVarLinear; 1432 } else if (isOpenMPTaskingDirective(DVar.DKind) && 1433 DVar.CKind == OMPC_firstprivate) { 1434 Reason = PDSA_TaskVarFirstprivate; 1435 ReportLoc = DVar.ImplicitDSALoc; 1436 } else if (VD && VD->isStaticLocal()) 1437 Reason = PDSA_StaticLocalVarShared; 1438 else if (VD && VD->isStaticDataMember()) 1439 Reason = PDSA_StaticMemberShared; 1440 else if (VD && VD->isFileVarDecl()) 1441 Reason = PDSA_GlobalVarShared; 1442 else if (D->getType().isConstant(SemaRef.getASTContext())) 1443 Reason = PDSA_ConstVarShared; 1444 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 1445 ReportHint = true; 1446 Reason = PDSA_LocalVarPrivate; 1447 } 1448 if (Reason != PDSA_Implicit) { 1449 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 1450 << Reason << ReportHint 1451 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 1452 } else if (DVar.ImplicitDSALoc.isValid()) { 1453 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 1454 << getOpenMPClauseName(DVar.CKind); 1455 } 1456 } 1457 1458 namespace { 1459 class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> { 1460 DSAStackTy *Stack; 1461 Sema &SemaRef; 1462 bool ErrorFound; 1463 CapturedStmt *CS; 1464 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate; 1465 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA; 1466 1467 public: 1468 void VisitDeclRefExpr(DeclRefExpr *E) { 1469 if (E->isTypeDependent() || E->isValueDependent() || 1470 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 1471 return; 1472 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 1473 // Skip internally declared variables. 1474 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD)) 1475 return; 1476 1477 auto DVar = Stack->getTopDSA(VD, false); 1478 // Check if the variable has explicit DSA set and stop analysis if it so. 1479 if (DVar.RefExpr) 1480 return; 1481 1482 auto ELoc = E->getExprLoc(); 1483 auto DKind = Stack->getCurrentDirective(); 1484 // The default(none) clause requires that each variable that is referenced 1485 // in the construct, and does not have a predetermined data-sharing 1486 // attribute, must have its data-sharing attribute explicitly determined 1487 // by being listed in a data-sharing attribute clause. 1488 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && 1489 isParallelOrTaskRegion(DKind) && 1490 VarsWithInheritedDSA.count(VD) == 0) { 1491 VarsWithInheritedDSA[VD] = E; 1492 return; 1493 } 1494 1495 // OpenMP [2.9.3.6, Restrictions, p.2] 1496 // A list item that appears in a reduction clause of the innermost 1497 // enclosing worksharing or parallel construct may not be accessed in an 1498 // explicit task. 1499 DVar = Stack->hasInnermostDSA( 1500 VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; }, 1501 [](OpenMPDirectiveKind K) -> bool { 1502 return isOpenMPParallelDirective(K) || 1503 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 1504 }, 1505 false); 1506 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 1507 ErrorFound = true; 1508 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 1509 ReportOriginalDSA(SemaRef, Stack, VD, DVar); 1510 return; 1511 } 1512 1513 // Define implicit data-sharing attributes for task. 1514 DVar = Stack->getImplicitDSA(VD, false); 1515 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 1516 !Stack->isLoopControlVariable(VD).first) 1517 ImplicitFirstprivate.push_back(E); 1518 } 1519 } 1520 void VisitMemberExpr(MemberExpr *E) { 1521 if (E->isTypeDependent() || E->isValueDependent() || 1522 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 1523 return; 1524 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 1525 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { 1526 auto DVar = Stack->getTopDSA(FD, false); 1527 // Check if the variable has explicit DSA set and stop analysis if it 1528 // so. 1529 if (DVar.RefExpr) 1530 return; 1531 1532 auto ELoc = E->getExprLoc(); 1533 auto DKind = Stack->getCurrentDirective(); 1534 // OpenMP [2.9.3.6, Restrictions, p.2] 1535 // A list item that appears in a reduction clause of the innermost 1536 // enclosing worksharing or parallel construct may not be accessed in 1537 // an explicit task. 1538 DVar = Stack->hasInnermostDSA( 1539 FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; }, 1540 [](OpenMPDirectiveKind K) -> bool { 1541 return isOpenMPParallelDirective(K) || 1542 isOpenMPWorksharingDirective(K) || 1543 isOpenMPTeamsDirective(K); 1544 }, 1545 false); 1546 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 1547 ErrorFound = true; 1548 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 1549 ReportOriginalDSA(SemaRef, Stack, FD, DVar); 1550 return; 1551 } 1552 1553 // Define implicit data-sharing attributes for task. 1554 DVar = Stack->getImplicitDSA(FD, false); 1555 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 1556 !Stack->isLoopControlVariable(FD).first) 1557 ImplicitFirstprivate.push_back(E); 1558 } 1559 } else 1560 Visit(E->getBase()); 1561 } 1562 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 1563 for (auto *C : S->clauses()) { 1564 // Skip analysis of arguments of implicitly defined firstprivate clause 1565 // for task directives. 1566 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid())) 1567 for (auto *CC : C->children()) { 1568 if (CC) 1569 Visit(CC); 1570 } 1571 } 1572 } 1573 void VisitStmt(Stmt *S) { 1574 for (auto *C : S->children()) { 1575 if (C && !isa<OMPExecutableDirective>(C)) 1576 Visit(C); 1577 } 1578 } 1579 1580 bool isErrorFound() { return ErrorFound; } 1581 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; } 1582 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() { 1583 return VarsWithInheritedDSA; 1584 } 1585 1586 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 1587 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {} 1588 }; 1589 } // namespace 1590 1591 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 1592 switch (DKind) { 1593 case OMPD_parallel: 1594 case OMPD_parallel_for: 1595 case OMPD_parallel_for_simd: 1596 case OMPD_parallel_sections: 1597 case OMPD_teams: 1598 case OMPD_target_teams: { 1599 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1600 QualType KmpInt32PtrTy = 1601 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 1602 Sema::CapturedParamNameType Params[] = { 1603 std::make_pair(".global_tid.", KmpInt32PtrTy), 1604 std::make_pair(".bound_tid.", KmpInt32PtrTy), 1605 std::make_pair(StringRef(), QualType()) // __context with shared vars 1606 }; 1607 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1608 Params); 1609 break; 1610 } 1611 case OMPD_simd: 1612 case OMPD_for: 1613 case OMPD_for_simd: 1614 case OMPD_sections: 1615 case OMPD_section: 1616 case OMPD_single: 1617 case OMPD_master: 1618 case OMPD_critical: 1619 case OMPD_taskgroup: 1620 case OMPD_distribute: 1621 case OMPD_ordered: 1622 case OMPD_atomic: 1623 case OMPD_target_data: 1624 case OMPD_target: 1625 case OMPD_target_parallel: 1626 case OMPD_target_parallel_for: 1627 case OMPD_target_parallel_for_simd: 1628 case OMPD_target_simd: { 1629 Sema::CapturedParamNameType Params[] = { 1630 std::make_pair(StringRef(), QualType()) // __context with shared vars 1631 }; 1632 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1633 Params); 1634 break; 1635 } 1636 case OMPD_task: { 1637 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1638 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()}; 1639 FunctionProtoType::ExtProtoInfo EPI; 1640 EPI.Variadic = true; 1641 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 1642 Sema::CapturedParamNameType Params[] = { 1643 std::make_pair(".global_tid.", KmpInt32Ty), 1644 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)), 1645 std::make_pair(".privates.", Context.VoidPtrTy.withConst()), 1646 std::make_pair(".copy_fn.", 1647 Context.getPointerType(CopyFnType).withConst()), 1648 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 1649 std::make_pair(StringRef(), QualType()) // __context with shared vars 1650 }; 1651 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1652 Params); 1653 // Mark this captured region as inlined, because we don't use outlined 1654 // function directly. 1655 getCurCapturedRegion()->TheCapturedDecl->addAttr( 1656 AlwaysInlineAttr::CreateImplicit( 1657 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange())); 1658 break; 1659 } 1660 case OMPD_taskloop: 1661 case OMPD_taskloop_simd: { 1662 QualType KmpInt32Ty = 1663 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); 1664 QualType KmpUInt64Ty = 1665 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 1666 QualType KmpInt64Ty = 1667 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 1668 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()}; 1669 FunctionProtoType::ExtProtoInfo EPI; 1670 EPI.Variadic = true; 1671 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 1672 Sema::CapturedParamNameType Params[] = { 1673 std::make_pair(".global_tid.", KmpInt32Ty), 1674 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)), 1675 std::make_pair(".privates.", 1676 Context.VoidPtrTy.withConst().withRestrict()), 1677 std::make_pair( 1678 ".copy_fn.", 1679 Context.getPointerType(CopyFnType).withConst().withRestrict()), 1680 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 1681 std::make_pair(".lb.", KmpUInt64Ty), 1682 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty), 1683 std::make_pair(".liter.", KmpInt32Ty), 1684 std::make_pair(StringRef(), QualType()) // __context with shared vars 1685 }; 1686 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1687 Params); 1688 // Mark this captured region as inlined, because we don't use outlined 1689 // function directly. 1690 getCurCapturedRegion()->TheCapturedDecl->addAttr( 1691 AlwaysInlineAttr::CreateImplicit( 1692 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange())); 1693 break; 1694 } 1695 case OMPD_distribute_parallel_for_simd: 1696 case OMPD_distribute_simd: 1697 case OMPD_distribute_parallel_for: 1698 case OMPD_teams_distribute: 1699 case OMPD_teams_distribute_simd: 1700 case OMPD_teams_distribute_parallel_for_simd: 1701 case OMPD_teams_distribute_parallel_for: 1702 case OMPD_target_teams_distribute: 1703 case OMPD_target_teams_distribute_parallel_for: 1704 case OMPD_target_teams_distribute_parallel_for_simd: 1705 case OMPD_target_teams_distribute_simd: { 1706 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 1707 QualType KmpInt32PtrTy = 1708 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 1709 Sema::CapturedParamNameType Params[] = { 1710 std::make_pair(".global_tid.", KmpInt32PtrTy), 1711 std::make_pair(".bound_tid.", KmpInt32PtrTy), 1712 std::make_pair(".previous.lb.", Context.getSizeType()), 1713 std::make_pair(".previous.ub.", Context.getSizeType()), 1714 std::make_pair(StringRef(), QualType()) // __context with shared vars 1715 }; 1716 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 1717 Params); 1718 break; 1719 } 1720 case OMPD_threadprivate: 1721 case OMPD_taskyield: 1722 case OMPD_barrier: 1723 case OMPD_taskwait: 1724 case OMPD_cancellation_point: 1725 case OMPD_cancel: 1726 case OMPD_flush: 1727 case OMPD_target_enter_data: 1728 case OMPD_target_exit_data: 1729 case OMPD_declare_reduction: 1730 case OMPD_declare_simd: 1731 case OMPD_declare_target: 1732 case OMPD_end_declare_target: 1733 case OMPD_target_update: 1734 llvm_unreachable("OpenMP Directive is not allowed"); 1735 case OMPD_unknown: 1736 llvm_unreachable("Unknown OpenMP directive"); 1737 } 1738 } 1739 1740 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 1741 Expr *CaptureExpr, bool WithInit, 1742 bool AsExpression) { 1743 assert(CaptureExpr); 1744 ASTContext &C = S.getASTContext(); 1745 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 1746 QualType Ty = Init->getType(); 1747 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 1748 if (S.getLangOpts().CPlusPlus) 1749 Ty = C.getLValueReferenceType(Ty); 1750 else { 1751 Ty = C.getPointerType(Ty); 1752 ExprResult Res = 1753 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 1754 if (!Res.isUsable()) 1755 return nullptr; 1756 Init = Res.get(); 1757 } 1758 WithInit = true; 1759 } 1760 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 1761 CaptureExpr->getLocStart()); 1762 if (!WithInit) 1763 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange())); 1764 S.CurContext->addHiddenDecl(CED); 1765 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false, 1766 /*TypeMayContainAuto=*/true); 1767 return CED; 1768 } 1769 1770 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 1771 bool WithInit) { 1772 OMPCapturedExprDecl *CD; 1773 if (auto *VD = S.IsOpenMPCapturedDecl(D)) 1774 CD = cast<OMPCapturedExprDecl>(VD); 1775 else 1776 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 1777 /*AsExpression=*/false); 1778 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 1779 CaptureExpr->getExprLoc()); 1780 } 1781 1782 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 1783 if (!Ref) { 1784 auto *CD = 1785 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."), 1786 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true); 1787 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 1788 CaptureExpr->getExprLoc()); 1789 } 1790 ExprResult Res = Ref; 1791 if (!S.getLangOpts().CPlusPlus && 1792 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 1793 Ref->getType()->isPointerType()) 1794 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 1795 if (!Res.isUsable()) 1796 return ExprError(); 1797 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get()); 1798 } 1799 1800 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 1801 ArrayRef<OMPClause *> Clauses) { 1802 if (!S.isUsable()) { 1803 ActOnCapturedRegionError(); 1804 return StmtError(); 1805 } 1806 1807 OMPOrderedClause *OC = nullptr; 1808 OMPScheduleClause *SC = nullptr; 1809 SmallVector<OMPLinearClause *, 4> LCs; 1810 // This is required for proper codegen. 1811 for (auto *Clause : Clauses) { 1812 if (isOpenMPPrivate(Clause->getClauseKind()) || 1813 Clause->getClauseKind() == OMPC_copyprivate || 1814 (getLangOpts().OpenMPUseTLS && 1815 getASTContext().getTargetInfo().isTLSSupported() && 1816 Clause->getClauseKind() == OMPC_copyin)) { 1817 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 1818 // Mark all variables in private list clauses as used in inner region. 1819 for (auto *VarRef : Clause->children()) { 1820 if (auto *E = cast_or_null<Expr>(VarRef)) { 1821 MarkDeclarationsReferencedInExpr(E); 1822 } 1823 } 1824 DSAStack->setForceVarCapturing(/*V=*/false); 1825 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) { 1826 // Mark all variables in private list clauses as used in inner region. 1827 // Required for proper codegen of combined directives. 1828 // TODO: add processing for other clauses. 1829 if (auto *C = OMPClauseWithPreInit::get(Clause)) { 1830 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 1831 for (auto *D : DS->decls()) 1832 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 1833 } 1834 } 1835 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 1836 if (auto *E = C->getPostUpdateExpr()) 1837 MarkDeclarationsReferencedInExpr(E); 1838 } 1839 } 1840 if (Clause->getClauseKind() == OMPC_schedule) 1841 SC = cast<OMPScheduleClause>(Clause); 1842 else if (Clause->getClauseKind() == OMPC_ordered) 1843 OC = cast<OMPOrderedClause>(Clause); 1844 else if (Clause->getClauseKind() == OMPC_linear) 1845 LCs.push_back(cast<OMPLinearClause>(Clause)); 1846 } 1847 bool ErrorFound = false; 1848 // OpenMP, 2.7.1 Loop Construct, Restrictions 1849 // The nonmonotonic modifier cannot be specified if an ordered clause is 1850 // specified. 1851 if (SC && 1852 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 1853 SC->getSecondScheduleModifier() == 1854 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 1855 OC) { 1856 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 1857 ? SC->getFirstScheduleModifierLoc() 1858 : SC->getSecondScheduleModifierLoc(), 1859 diag::err_omp_schedule_nonmonotonic_ordered) 1860 << SourceRange(OC->getLocStart(), OC->getLocEnd()); 1861 ErrorFound = true; 1862 } 1863 if (!LCs.empty() && OC && OC->getNumForLoops()) { 1864 for (auto *C : LCs) { 1865 Diag(C->getLocStart(), diag::err_omp_linear_ordered) 1866 << SourceRange(OC->getLocStart(), OC->getLocEnd()); 1867 } 1868 ErrorFound = true; 1869 } 1870 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 1871 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 1872 OC->getNumForLoops()) { 1873 Diag(OC->getLocStart(), diag::err_omp_ordered_simd) 1874 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 1875 ErrorFound = true; 1876 } 1877 if (ErrorFound) { 1878 ActOnCapturedRegionError(); 1879 return StmtError(); 1880 } 1881 return ActOnCapturedRegionEnd(S.get()); 1882 } 1883 1884 static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, 1885 OpenMPDirectiveKind CurrentRegion, 1886 const DeclarationNameInfo &CurrentName, 1887 OpenMPDirectiveKind CancelRegion, 1888 SourceLocation StartLoc) { 1889 if (Stack->getCurScope()) { 1890 auto ParentRegion = Stack->getParentDirective(); 1891 auto OffendingRegion = ParentRegion; 1892 bool NestingProhibited = false; 1893 bool CloseNesting = true; 1894 bool OrphanSeen = false; 1895 enum { 1896 NoRecommend, 1897 ShouldBeInParallelRegion, 1898 ShouldBeInOrderedRegion, 1899 ShouldBeInTargetRegion, 1900 ShouldBeInTeamsRegion 1901 } Recommend = NoRecommend; 1902 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) { 1903 // OpenMP [2.16, Nesting of Regions] 1904 // OpenMP constructs may not be nested inside a simd region. 1905 // OpenMP [2.8.1,simd Construct, Restrictions] 1906 // An ordered construct with the simd clause is the only OpenMP 1907 // construct that can appear in the simd region. 1908 // Allowing a SIMD construct nested in another SIMD construct is an 1909 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 1910 // message. 1911 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 1912 ? diag::err_omp_prohibited_region_simd 1913 : diag::warn_omp_nesting_simd); 1914 return CurrentRegion != OMPD_simd; 1915 } 1916 if (ParentRegion == OMPD_atomic) { 1917 // OpenMP [2.16, Nesting of Regions] 1918 // OpenMP constructs may not be nested inside an atomic region. 1919 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 1920 return true; 1921 } 1922 if (CurrentRegion == OMPD_section) { 1923 // OpenMP [2.7.2, sections Construct, Restrictions] 1924 // Orphaned section directives are prohibited. That is, the section 1925 // directives must appear within the sections construct and must not be 1926 // encountered elsewhere in the sections region. 1927 if (ParentRegion != OMPD_sections && 1928 ParentRegion != OMPD_parallel_sections) { 1929 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 1930 << (ParentRegion != OMPD_unknown) 1931 << getOpenMPDirectiveName(ParentRegion); 1932 return true; 1933 } 1934 return false; 1935 } 1936 // Allow some constructs (except teams) to be orphaned (they could be 1937 // used in functions, called from OpenMP regions with the required 1938 // preconditions). 1939 if (ParentRegion == OMPD_unknown && 1940 !isOpenMPNestingTeamsDirective(CurrentRegion)) 1941 return false; 1942 if (CurrentRegion == OMPD_cancellation_point || 1943 CurrentRegion == OMPD_cancel) { 1944 // OpenMP [2.16, Nesting of Regions] 1945 // A cancellation point construct for which construct-type-clause is 1946 // taskgroup must be nested inside a task construct. A cancellation 1947 // point construct for which construct-type-clause is not taskgroup must 1948 // be closely nested inside an OpenMP construct that matches the type 1949 // specified in construct-type-clause. 1950 // A cancel construct for which construct-type-clause is taskgroup must be 1951 // nested inside a task construct. A cancel construct for which 1952 // construct-type-clause is not taskgroup must be closely nested inside an 1953 // OpenMP construct that matches the type specified in 1954 // construct-type-clause. 1955 NestingProhibited = 1956 !((CancelRegion == OMPD_parallel && 1957 (ParentRegion == OMPD_parallel || 1958 ParentRegion == OMPD_target_parallel)) || 1959 (CancelRegion == OMPD_for && 1960 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 1961 ParentRegion == OMPD_target_parallel_for)) || 1962 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) || 1963 (CancelRegion == OMPD_sections && 1964 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 1965 ParentRegion == OMPD_parallel_sections))); 1966 } else if (CurrentRegion == OMPD_master) { 1967 // OpenMP [2.16, Nesting of Regions] 1968 // A master region may not be closely nested inside a worksharing, 1969 // atomic, or explicit task region. 1970 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 1971 isOpenMPTaskingDirective(ParentRegion); 1972 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 1973 // OpenMP [2.16, Nesting of Regions] 1974 // A critical region may not be nested (closely or otherwise) inside a 1975 // critical region with the same name. Note that this restriction is not 1976 // sufficient to prevent deadlock. 1977 SourceLocation PreviousCriticalLoc; 1978 bool DeadLock = Stack->hasDirective( 1979 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 1980 const DeclarationNameInfo &DNI, 1981 SourceLocation Loc) -> bool { 1982 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 1983 PreviousCriticalLoc = Loc; 1984 return true; 1985 } else 1986 return false; 1987 }, 1988 false /* skip top directive */); 1989 if (DeadLock) { 1990 SemaRef.Diag(StartLoc, 1991 diag::err_omp_prohibited_region_critical_same_name) 1992 << CurrentName.getName(); 1993 if (PreviousCriticalLoc.isValid()) 1994 SemaRef.Diag(PreviousCriticalLoc, 1995 diag::note_omp_previous_critical_region); 1996 return true; 1997 } 1998 } else if (CurrentRegion == OMPD_barrier) { 1999 // OpenMP [2.16, Nesting of Regions] 2000 // A barrier region may not be closely nested inside a worksharing, 2001 // explicit task, critical, ordered, atomic, or master region. 2002 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 2003 isOpenMPTaskingDirective(ParentRegion) || 2004 ParentRegion == OMPD_master || 2005 ParentRegion == OMPD_critical || 2006 ParentRegion == OMPD_ordered; 2007 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 2008 !isOpenMPParallelDirective(CurrentRegion) && 2009 !isOpenMPTeamsDirective(CurrentRegion)) { 2010 // OpenMP [2.16, Nesting of Regions] 2011 // A worksharing region may not be closely nested inside a worksharing, 2012 // explicit task, critical, ordered, atomic, or master region. 2013 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 2014 isOpenMPTaskingDirective(ParentRegion) || 2015 ParentRegion == OMPD_master || 2016 ParentRegion == OMPD_critical || 2017 ParentRegion == OMPD_ordered; 2018 Recommend = ShouldBeInParallelRegion; 2019 } else if (CurrentRegion == OMPD_ordered) { 2020 // OpenMP [2.16, Nesting of Regions] 2021 // An ordered region may not be closely nested inside a critical, 2022 // atomic, or explicit task region. 2023 // An ordered region must be closely nested inside a loop region (or 2024 // parallel loop region) with an ordered clause. 2025 // OpenMP [2.8.1,simd Construct, Restrictions] 2026 // An ordered construct with the simd clause is the only OpenMP construct 2027 // that can appear in the simd region. 2028 NestingProhibited = ParentRegion == OMPD_critical || 2029 isOpenMPTaskingDirective(ParentRegion) || 2030 !(isOpenMPSimdDirective(ParentRegion) || 2031 Stack->isParentOrderedRegion()); 2032 Recommend = ShouldBeInOrderedRegion; 2033 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 2034 // OpenMP [2.16, Nesting of Regions] 2035 // If specified, a teams construct must be contained within a target 2036 // construct. 2037 NestingProhibited = ParentRegion != OMPD_target; 2038 OrphanSeen = ParentRegion == OMPD_unknown; 2039 Recommend = ShouldBeInTargetRegion; 2040 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc()); 2041 } 2042 if (!NestingProhibited && 2043 !isOpenMPTargetExecutionDirective(CurrentRegion) && 2044 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 2045 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 2046 // OpenMP [2.16, Nesting of Regions] 2047 // distribute, parallel, parallel sections, parallel workshare, and the 2048 // parallel loop and parallel loop SIMD constructs are the only OpenMP 2049 // constructs that can be closely nested in the teams region. 2050 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 2051 !isOpenMPDistributeDirective(CurrentRegion); 2052 Recommend = ShouldBeInParallelRegion; 2053 } 2054 if (!NestingProhibited && 2055 isOpenMPNestingDistributeDirective(CurrentRegion)) { 2056 // OpenMP 4.5 [2.17 Nesting of Regions] 2057 // The region associated with the distribute construct must be strictly 2058 // nested inside a teams region 2059 NestingProhibited = 2060 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 2061 Recommend = ShouldBeInTeamsRegion; 2062 } 2063 if (!NestingProhibited && 2064 (isOpenMPTargetExecutionDirective(CurrentRegion) || 2065 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 2066 // OpenMP 4.5 [2.17 Nesting of Regions] 2067 // If a target, target update, target data, target enter data, or 2068 // target exit data construct is encountered during execution of a 2069 // target region, the behavior is unspecified. 2070 NestingProhibited = Stack->hasDirective( 2071 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 2072 SourceLocation) -> bool { 2073 if (isOpenMPTargetExecutionDirective(K)) { 2074 OffendingRegion = K; 2075 return true; 2076 } else 2077 return false; 2078 }, 2079 false /* don't skip top directive */); 2080 CloseNesting = false; 2081 } 2082 if (NestingProhibited) { 2083 if (OrphanSeen) { 2084 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 2085 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 2086 } else { 2087 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 2088 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 2089 << Recommend << getOpenMPDirectiveName(CurrentRegion); 2090 } 2091 return true; 2092 } 2093 } 2094 return false; 2095 } 2096 2097 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 2098 ArrayRef<OMPClause *> Clauses, 2099 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 2100 bool ErrorFound = false; 2101 unsigned NamedModifiersNumber = 0; 2102 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers( 2103 OMPD_unknown + 1); 2104 SmallVector<SourceLocation, 4> NameModifierLoc; 2105 for (const auto *C : Clauses) { 2106 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 2107 // At most one if clause without a directive-name-modifier can appear on 2108 // the directive. 2109 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 2110 if (FoundNameModifiers[CurNM]) { 2111 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause) 2112 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 2113 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 2114 ErrorFound = true; 2115 } else if (CurNM != OMPD_unknown) { 2116 NameModifierLoc.push_back(IC->getNameModifierLoc()); 2117 ++NamedModifiersNumber; 2118 } 2119 FoundNameModifiers[CurNM] = IC; 2120 if (CurNM == OMPD_unknown) 2121 continue; 2122 // Check if the specified name modifier is allowed for the current 2123 // directive. 2124 // At most one if clause with the particular directive-name-modifier can 2125 // appear on the directive. 2126 bool MatchFound = false; 2127 for (auto NM : AllowedNameModifiers) { 2128 if (CurNM == NM) { 2129 MatchFound = true; 2130 break; 2131 } 2132 } 2133 if (!MatchFound) { 2134 S.Diag(IC->getNameModifierLoc(), 2135 diag::err_omp_wrong_if_directive_name_modifier) 2136 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 2137 ErrorFound = true; 2138 } 2139 } 2140 } 2141 // If any if clause on the directive includes a directive-name-modifier then 2142 // all if clauses on the directive must include a directive-name-modifier. 2143 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 2144 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 2145 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(), 2146 diag::err_omp_no_more_if_clause); 2147 } else { 2148 std::string Values; 2149 std::string Sep(", "); 2150 unsigned AllowedCnt = 0; 2151 unsigned TotalAllowedNum = 2152 AllowedNameModifiers.size() - NamedModifiersNumber; 2153 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 2154 ++Cnt) { 2155 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 2156 if (!FoundNameModifiers[NM]) { 2157 Values += "'"; 2158 Values += getOpenMPDirectiveName(NM); 2159 Values += "'"; 2160 if (AllowedCnt + 2 == TotalAllowedNum) 2161 Values += " or "; 2162 else if (AllowedCnt + 1 != TotalAllowedNum) 2163 Values += Sep; 2164 ++AllowedCnt; 2165 } 2166 } 2167 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(), 2168 diag::err_omp_unnamed_if_clause) 2169 << (TotalAllowedNum > 1) << Values; 2170 } 2171 for (auto Loc : NameModifierLoc) { 2172 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 2173 } 2174 ErrorFound = true; 2175 } 2176 return ErrorFound; 2177 } 2178 2179 StmtResult Sema::ActOnOpenMPExecutableDirective( 2180 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 2181 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 2182 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 2183 StmtResult Res = StmtError(); 2184 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 2185 StartLoc)) 2186 return StmtError(); 2187 2188 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 2189 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA; 2190 bool ErrorFound = false; 2191 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 2192 if (AStmt) { 2193 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 2194 2195 // Check default data sharing attributes for referenced variables. 2196 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 2197 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt()); 2198 if (DSAChecker.isErrorFound()) 2199 return StmtError(); 2200 // Generate list of implicitly defined firstprivate variables. 2201 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 2202 2203 if (!DSAChecker.getImplicitFirstprivate().empty()) { 2204 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 2205 DSAChecker.getImplicitFirstprivate(), SourceLocation(), 2206 SourceLocation(), SourceLocation())) { 2207 ClausesWithImplicit.push_back(Implicit); 2208 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 2209 DSAChecker.getImplicitFirstprivate().size(); 2210 } else 2211 ErrorFound = true; 2212 } 2213 } 2214 2215 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 2216 switch (Kind) { 2217 case OMPD_parallel: 2218 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 2219 EndLoc); 2220 AllowedNameModifiers.push_back(OMPD_parallel); 2221 break; 2222 case OMPD_simd: 2223 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 2224 VarsWithInheritedDSA); 2225 break; 2226 case OMPD_for: 2227 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 2228 VarsWithInheritedDSA); 2229 break; 2230 case OMPD_for_simd: 2231 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 2232 EndLoc, VarsWithInheritedDSA); 2233 break; 2234 case OMPD_sections: 2235 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 2236 EndLoc); 2237 break; 2238 case OMPD_section: 2239 assert(ClausesWithImplicit.empty() && 2240 "No clauses are allowed for 'omp section' directive"); 2241 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 2242 break; 2243 case OMPD_single: 2244 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 2245 EndLoc); 2246 break; 2247 case OMPD_master: 2248 assert(ClausesWithImplicit.empty() && 2249 "No clauses are allowed for 'omp master' directive"); 2250 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 2251 break; 2252 case OMPD_critical: 2253 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 2254 StartLoc, EndLoc); 2255 break; 2256 case OMPD_parallel_for: 2257 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 2258 EndLoc, VarsWithInheritedDSA); 2259 AllowedNameModifiers.push_back(OMPD_parallel); 2260 break; 2261 case OMPD_parallel_for_simd: 2262 Res = ActOnOpenMPParallelForSimdDirective( 2263 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2264 AllowedNameModifiers.push_back(OMPD_parallel); 2265 break; 2266 case OMPD_parallel_sections: 2267 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 2268 StartLoc, EndLoc); 2269 AllowedNameModifiers.push_back(OMPD_parallel); 2270 break; 2271 case OMPD_task: 2272 Res = 2273 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 2274 AllowedNameModifiers.push_back(OMPD_task); 2275 break; 2276 case OMPD_taskyield: 2277 assert(ClausesWithImplicit.empty() && 2278 "No clauses are allowed for 'omp taskyield' directive"); 2279 assert(AStmt == nullptr && 2280 "No associated statement allowed for 'omp taskyield' directive"); 2281 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 2282 break; 2283 case OMPD_barrier: 2284 assert(ClausesWithImplicit.empty() && 2285 "No clauses are allowed for 'omp barrier' directive"); 2286 assert(AStmt == nullptr && 2287 "No associated statement allowed for 'omp barrier' directive"); 2288 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 2289 break; 2290 case OMPD_taskwait: 2291 assert(ClausesWithImplicit.empty() && 2292 "No clauses are allowed for 'omp taskwait' directive"); 2293 assert(AStmt == nullptr && 2294 "No associated statement allowed for 'omp taskwait' directive"); 2295 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 2296 break; 2297 case OMPD_taskgroup: 2298 assert(ClausesWithImplicit.empty() && 2299 "No clauses are allowed for 'omp taskgroup' directive"); 2300 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc); 2301 break; 2302 case OMPD_flush: 2303 assert(AStmt == nullptr && 2304 "No associated statement allowed for 'omp flush' directive"); 2305 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 2306 break; 2307 case OMPD_ordered: 2308 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 2309 EndLoc); 2310 break; 2311 case OMPD_atomic: 2312 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 2313 EndLoc); 2314 break; 2315 case OMPD_teams: 2316 Res = 2317 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 2318 break; 2319 case OMPD_target: 2320 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 2321 EndLoc); 2322 AllowedNameModifiers.push_back(OMPD_target); 2323 break; 2324 case OMPD_target_parallel: 2325 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 2326 StartLoc, EndLoc); 2327 AllowedNameModifiers.push_back(OMPD_target); 2328 AllowedNameModifiers.push_back(OMPD_parallel); 2329 break; 2330 case OMPD_target_parallel_for: 2331 Res = ActOnOpenMPTargetParallelForDirective( 2332 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2333 AllowedNameModifiers.push_back(OMPD_target); 2334 AllowedNameModifiers.push_back(OMPD_parallel); 2335 break; 2336 case OMPD_cancellation_point: 2337 assert(ClausesWithImplicit.empty() && 2338 "No clauses are allowed for 'omp cancellation point' directive"); 2339 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 2340 "cancellation point' directive"); 2341 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 2342 break; 2343 case OMPD_cancel: 2344 assert(AStmt == nullptr && 2345 "No associated statement allowed for 'omp cancel' directive"); 2346 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 2347 CancelRegion); 2348 AllowedNameModifiers.push_back(OMPD_cancel); 2349 break; 2350 case OMPD_target_data: 2351 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 2352 EndLoc); 2353 AllowedNameModifiers.push_back(OMPD_target_data); 2354 break; 2355 case OMPD_target_enter_data: 2356 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 2357 EndLoc); 2358 AllowedNameModifiers.push_back(OMPD_target_enter_data); 2359 break; 2360 case OMPD_target_exit_data: 2361 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 2362 EndLoc); 2363 AllowedNameModifiers.push_back(OMPD_target_exit_data); 2364 break; 2365 case OMPD_taskloop: 2366 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 2367 EndLoc, VarsWithInheritedDSA); 2368 AllowedNameModifiers.push_back(OMPD_taskloop); 2369 break; 2370 case OMPD_taskloop_simd: 2371 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 2372 EndLoc, VarsWithInheritedDSA); 2373 AllowedNameModifiers.push_back(OMPD_taskloop); 2374 break; 2375 case OMPD_distribute: 2376 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 2377 EndLoc, VarsWithInheritedDSA); 2378 break; 2379 case OMPD_target_update: 2380 assert(!AStmt && "Statement is not allowed for target update"); 2381 Res = 2382 ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc); 2383 AllowedNameModifiers.push_back(OMPD_target_update); 2384 break; 2385 case OMPD_distribute_parallel_for: 2386 Res = ActOnOpenMPDistributeParallelForDirective( 2387 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2388 AllowedNameModifiers.push_back(OMPD_parallel); 2389 break; 2390 case OMPD_distribute_parallel_for_simd: 2391 Res = ActOnOpenMPDistributeParallelForSimdDirective( 2392 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2393 AllowedNameModifiers.push_back(OMPD_parallel); 2394 break; 2395 case OMPD_distribute_simd: 2396 Res = ActOnOpenMPDistributeSimdDirective( 2397 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2398 break; 2399 case OMPD_target_parallel_for_simd: 2400 Res = ActOnOpenMPTargetParallelForSimdDirective( 2401 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2402 AllowedNameModifiers.push_back(OMPD_target); 2403 AllowedNameModifiers.push_back(OMPD_parallel); 2404 break; 2405 case OMPD_target_simd: 2406 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 2407 EndLoc, VarsWithInheritedDSA); 2408 AllowedNameModifiers.push_back(OMPD_target); 2409 break; 2410 case OMPD_teams_distribute: 2411 Res = ActOnOpenMPTeamsDistributeDirective( 2412 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2413 break; 2414 case OMPD_teams_distribute_simd: 2415 Res = ActOnOpenMPTeamsDistributeSimdDirective( 2416 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2417 break; 2418 case OMPD_teams_distribute_parallel_for_simd: 2419 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 2420 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2421 AllowedNameModifiers.push_back(OMPD_parallel); 2422 break; 2423 case OMPD_teams_distribute_parallel_for: 2424 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 2425 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2426 AllowedNameModifiers.push_back(OMPD_parallel); 2427 break; 2428 case OMPD_target_teams: 2429 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 2430 EndLoc); 2431 AllowedNameModifiers.push_back(OMPD_target); 2432 break; 2433 case OMPD_target_teams_distribute: 2434 Res = ActOnOpenMPTargetTeamsDistributeDirective( 2435 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2436 AllowedNameModifiers.push_back(OMPD_target); 2437 break; 2438 case OMPD_target_teams_distribute_parallel_for: 2439 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 2440 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2441 AllowedNameModifiers.push_back(OMPD_target); 2442 AllowedNameModifiers.push_back(OMPD_parallel); 2443 break; 2444 case OMPD_target_teams_distribute_parallel_for_simd: 2445 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 2446 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2447 AllowedNameModifiers.push_back(OMPD_target); 2448 AllowedNameModifiers.push_back(OMPD_parallel); 2449 break; 2450 case OMPD_target_teams_distribute_simd: 2451 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 2452 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 2453 AllowedNameModifiers.push_back(OMPD_target); 2454 break; 2455 case OMPD_declare_target: 2456 case OMPD_end_declare_target: 2457 case OMPD_threadprivate: 2458 case OMPD_declare_reduction: 2459 case OMPD_declare_simd: 2460 llvm_unreachable("OpenMP Directive is not allowed"); 2461 case OMPD_unknown: 2462 llvm_unreachable("Unknown OpenMP directive"); 2463 } 2464 2465 for (auto P : VarsWithInheritedDSA) { 2466 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 2467 << P.first << P.second->getSourceRange(); 2468 } 2469 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound; 2470 2471 if (!AllowedNameModifiers.empty()) 2472 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 2473 ErrorFound; 2474 2475 if (ErrorFound) 2476 return StmtError(); 2477 return Res; 2478 } 2479 2480 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 2481 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 2482 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 2483 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 2484 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 2485 assert(Aligneds.size() == Alignments.size()); 2486 assert(Linears.size() == LinModifiers.size()); 2487 assert(Linears.size() == Steps.size()); 2488 if (!DG || DG.get().isNull()) 2489 return DeclGroupPtrTy(); 2490 2491 if (!DG.get().isSingleDecl()) { 2492 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd); 2493 return DG; 2494 } 2495 auto *ADecl = DG.get().getSingleDecl(); 2496 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 2497 ADecl = FTD->getTemplatedDecl(); 2498 2499 auto *FD = dyn_cast<FunctionDecl>(ADecl); 2500 if (!FD) { 2501 Diag(ADecl->getLocation(), diag::err_omp_function_expected); 2502 return DeclGroupPtrTy(); 2503 } 2504 2505 // OpenMP [2.8.2, declare simd construct, Description] 2506 // The parameter of the simdlen clause must be a constant positive integer 2507 // expression. 2508 ExprResult SL; 2509 if (Simdlen) 2510 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 2511 // OpenMP [2.8.2, declare simd construct, Description] 2512 // The special this pointer can be used as if was one of the arguments to the 2513 // function in any of the linear, aligned, or uniform clauses. 2514 // The uniform clause declares one or more arguments to have an invariant 2515 // value for all concurrent invocations of the function in the execution of a 2516 // single SIMD loop. 2517 llvm::DenseMap<Decl *, Expr *> UniformedArgs; 2518 Expr *UniformedLinearThis = nullptr; 2519 for (auto *E : Uniforms) { 2520 E = E->IgnoreParenImpCasts(); 2521 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) 2522 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 2523 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 2524 FD->getParamDecl(PVD->getFunctionScopeIndex()) 2525 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 2526 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E)); 2527 continue; 2528 } 2529 if (isa<CXXThisExpr>(E)) { 2530 UniformedLinearThis = E; 2531 continue; 2532 } 2533 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 2534 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 2535 } 2536 // OpenMP [2.8.2, declare simd construct, Description] 2537 // The aligned clause declares that the object to which each list item points 2538 // is aligned to the number of bytes expressed in the optional parameter of 2539 // the aligned clause. 2540 // The special this pointer can be used as if was one of the arguments to the 2541 // function in any of the linear, aligned, or uniform clauses. 2542 // The type of list items appearing in the aligned clause must be array, 2543 // pointer, reference to array, or reference to pointer. 2544 llvm::DenseMap<Decl *, Expr *> AlignedArgs; 2545 Expr *AlignedThis = nullptr; 2546 for (auto *E : Aligneds) { 2547 E = E->IgnoreParenImpCasts(); 2548 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) 2549 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 2550 auto *CanonPVD = PVD->getCanonicalDecl(); 2551 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 2552 FD->getParamDecl(PVD->getFunctionScopeIndex()) 2553 ->getCanonicalDecl() == CanonPVD) { 2554 // OpenMP [2.8.1, simd construct, Restrictions] 2555 // A list-item cannot appear in more than one aligned clause. 2556 if (AlignedArgs.count(CanonPVD) > 0) { 2557 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 2558 << 1 << E->getSourceRange(); 2559 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 2560 diag::note_omp_explicit_dsa) 2561 << getOpenMPClauseName(OMPC_aligned); 2562 continue; 2563 } 2564 AlignedArgs[CanonPVD] = E; 2565 QualType QTy = PVD->getType() 2566 .getNonReferenceType() 2567 .getUnqualifiedType() 2568 .getCanonicalType(); 2569 const Type *Ty = QTy.getTypePtrOrNull(); 2570 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 2571 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 2572 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 2573 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 2574 } 2575 continue; 2576 } 2577 } 2578 if (isa<CXXThisExpr>(E)) { 2579 if (AlignedThis) { 2580 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 2581 << 2 << E->getSourceRange(); 2582 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 2583 << getOpenMPClauseName(OMPC_aligned); 2584 } 2585 AlignedThis = E; 2586 continue; 2587 } 2588 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 2589 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 2590 } 2591 // The optional parameter of the aligned clause, alignment, must be a constant 2592 // positive integer expression. If no optional parameter is specified, 2593 // implementation-defined default alignments for SIMD instructions on the 2594 // target platforms are assumed. 2595 SmallVector<Expr *, 4> NewAligns; 2596 for (auto *E : Alignments) { 2597 ExprResult Align; 2598 if (E) 2599 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 2600 NewAligns.push_back(Align.get()); 2601 } 2602 // OpenMP [2.8.2, declare simd construct, Description] 2603 // The linear clause declares one or more list items to be private to a SIMD 2604 // lane and to have a linear relationship with respect to the iteration space 2605 // of a loop. 2606 // The special this pointer can be used as if was one of the arguments to the 2607 // function in any of the linear, aligned, or uniform clauses. 2608 // When a linear-step expression is specified in a linear clause it must be 2609 // either a constant integer expression or an integer-typed parameter that is 2610 // specified in a uniform clause on the directive. 2611 llvm::DenseMap<Decl *, Expr *> LinearArgs; 2612 const bool IsUniformedThis = UniformedLinearThis != nullptr; 2613 auto MI = LinModifiers.begin(); 2614 for (auto *E : Linears) { 2615 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 2616 ++MI; 2617 E = E->IgnoreParenImpCasts(); 2618 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) 2619 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 2620 auto *CanonPVD = PVD->getCanonicalDecl(); 2621 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 2622 FD->getParamDecl(PVD->getFunctionScopeIndex()) 2623 ->getCanonicalDecl() == CanonPVD) { 2624 // OpenMP [2.15.3.7, linear Clause, Restrictions] 2625 // A list-item cannot appear in more than one linear clause. 2626 if (LinearArgs.count(CanonPVD) > 0) { 2627 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 2628 << getOpenMPClauseName(OMPC_linear) 2629 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 2630 Diag(LinearArgs[CanonPVD]->getExprLoc(), 2631 diag::note_omp_explicit_dsa) 2632 << getOpenMPClauseName(OMPC_linear); 2633 continue; 2634 } 2635 // Each argument can appear in at most one uniform or linear clause. 2636 if (UniformedArgs.count(CanonPVD) > 0) { 2637 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 2638 << getOpenMPClauseName(OMPC_linear) 2639 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 2640 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 2641 diag::note_omp_explicit_dsa) 2642 << getOpenMPClauseName(OMPC_uniform); 2643 continue; 2644 } 2645 LinearArgs[CanonPVD] = E; 2646 if (E->isValueDependent() || E->isTypeDependent() || 2647 E->isInstantiationDependent() || 2648 E->containsUnexpandedParameterPack()) 2649 continue; 2650 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 2651 PVD->getOriginalType()); 2652 continue; 2653 } 2654 } 2655 if (isa<CXXThisExpr>(E)) { 2656 if (UniformedLinearThis) { 2657 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 2658 << getOpenMPClauseName(OMPC_linear) 2659 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 2660 << E->getSourceRange(); 2661 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 2662 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 2663 : OMPC_linear); 2664 continue; 2665 } 2666 UniformedLinearThis = E; 2667 if (E->isValueDependent() || E->isTypeDependent() || 2668 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 2669 continue; 2670 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 2671 E->getType()); 2672 continue; 2673 } 2674 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 2675 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 2676 } 2677 Expr *Step = nullptr; 2678 Expr *NewStep = nullptr; 2679 SmallVector<Expr *, 4> NewSteps; 2680 for (auto *E : Steps) { 2681 // Skip the same step expression, it was checked already. 2682 if (Step == E || !E) { 2683 NewSteps.push_back(E ? NewStep : nullptr); 2684 continue; 2685 } 2686 Step = E; 2687 if (auto *DRE = dyn_cast<DeclRefExpr>(Step)) 2688 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 2689 auto *CanonPVD = PVD->getCanonicalDecl(); 2690 if (UniformedArgs.count(CanonPVD) == 0) { 2691 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 2692 << Step->getSourceRange(); 2693 } else if (E->isValueDependent() || E->isTypeDependent() || 2694 E->isInstantiationDependent() || 2695 E->containsUnexpandedParameterPack() || 2696 CanonPVD->getType()->hasIntegerRepresentation()) 2697 NewSteps.push_back(Step); 2698 else { 2699 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 2700 << Step->getSourceRange(); 2701 } 2702 continue; 2703 } 2704 NewStep = Step; 2705 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 2706 !Step->isInstantiationDependent() && 2707 !Step->containsUnexpandedParameterPack()) { 2708 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 2709 .get(); 2710 if (NewStep) 2711 NewStep = VerifyIntegerConstantExpression(NewStep).get(); 2712 } 2713 NewSteps.push_back(NewStep); 2714 } 2715 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 2716 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 2717 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 2718 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 2719 const_cast<Expr **>(Linears.data()), Linears.size(), 2720 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 2721 NewSteps.data(), NewSteps.size(), SR); 2722 ADecl->addAttr(NewAttr); 2723 return ConvertDeclToDeclGroup(ADecl); 2724 } 2725 2726 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 2727 Stmt *AStmt, 2728 SourceLocation StartLoc, 2729 SourceLocation EndLoc) { 2730 if (!AStmt) 2731 return StmtError(); 2732 2733 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 2734 // 1.2.2 OpenMP Language Terminology 2735 // Structured block - An executable statement with a single entry at the 2736 // top and a single exit at the bottom. 2737 // The point of exit cannot be a branch out of the structured block. 2738 // longjmp() and throw() must not violate the entry/exit criteria. 2739 CS->getCapturedDecl()->setNothrow(); 2740 2741 getCurFunction()->setHasBranchProtectedScope(); 2742 2743 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 2744 DSAStack->isCancelRegion()); 2745 } 2746 2747 namespace { 2748 /// \brief Helper class for checking canonical form of the OpenMP loops and 2749 /// extracting iteration space of each loop in the loop nest, that will be used 2750 /// for IR generation. 2751 class OpenMPIterationSpaceChecker { 2752 /// \brief Reference to Sema. 2753 Sema &SemaRef; 2754 /// \brief A location for diagnostics (when there is no some better location). 2755 SourceLocation DefaultLoc; 2756 /// \brief A location for diagnostics (when increment is not compatible). 2757 SourceLocation ConditionLoc; 2758 /// \brief A source location for referring to loop init later. 2759 SourceRange InitSrcRange; 2760 /// \brief A source location for referring to condition later. 2761 SourceRange ConditionSrcRange; 2762 /// \brief A source location for referring to increment later. 2763 SourceRange IncrementSrcRange; 2764 /// \brief Loop variable. 2765 ValueDecl *LCDecl = nullptr; 2766 /// \brief Reference to loop variable. 2767 Expr *LCRef = nullptr; 2768 /// \brief Lower bound (initializer for the var). 2769 Expr *LB = nullptr; 2770 /// \brief Upper bound. 2771 Expr *UB = nullptr; 2772 /// \brief Loop step (increment). 2773 Expr *Step = nullptr; 2774 /// \brief This flag is true when condition is one of: 2775 /// Var < UB 2776 /// Var <= UB 2777 /// UB > Var 2778 /// UB >= Var 2779 bool TestIsLessOp = false; 2780 /// \brief This flag is true when condition is strict ( < or > ). 2781 bool TestIsStrictOp = false; 2782 /// \brief This flag is true when step is subtracted on each iteration. 2783 bool SubtractStep = false; 2784 2785 public: 2786 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc) 2787 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {} 2788 /// \brief Check init-expr for canonical loop form and save loop counter 2789 /// variable - #Var and its initialization value - #LB. 2790 bool CheckInit(Stmt *S, bool EmitDiags = true); 2791 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags 2792 /// for less/greater and for strict/non-strict comparison. 2793 bool CheckCond(Expr *S); 2794 /// \brief Check incr-expr for canonical loop form and return true if it 2795 /// does not conform, otherwise save loop step (#Step). 2796 bool CheckInc(Expr *S); 2797 /// \brief Return the loop counter variable. 2798 ValueDecl *GetLoopDecl() const { return LCDecl; } 2799 /// \brief Return the reference expression to loop counter variable. 2800 Expr *GetLoopDeclRefExpr() const { return LCRef; } 2801 /// \brief Source range of the loop init. 2802 SourceRange GetInitSrcRange() const { return InitSrcRange; } 2803 /// \brief Source range of the loop condition. 2804 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; } 2805 /// \brief Source range of the loop increment. 2806 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; } 2807 /// \brief True if the step should be subtracted. 2808 bool ShouldSubtractStep() const { return SubtractStep; } 2809 /// \brief Build the expression to calculate the number of iterations. 2810 Expr * 2811 BuildNumIterations(Scope *S, const bool LimitedType, 2812 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const; 2813 /// \brief Build the precondition expression for the loops. 2814 Expr *BuildPreCond(Scope *S, Expr *Cond, 2815 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const; 2816 /// \brief Build reference expression to the counter be used for codegen. 2817 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures, 2818 DSAStackTy &DSA) const; 2819 /// \brief Build reference expression to the private counter be used for 2820 /// codegen. 2821 Expr *BuildPrivateCounterVar() const; 2822 /// \brief Build initialization of the counter be used for codegen. 2823 Expr *BuildCounterInit() const; 2824 /// \brief Build step of the counter be used for codegen. 2825 Expr *BuildCounterStep() const; 2826 /// \brief Return true if any expression is dependent. 2827 bool Dependent() const; 2828 2829 private: 2830 /// \brief Check the right-hand side of an assignment in the increment 2831 /// expression. 2832 bool CheckIncRHS(Expr *RHS); 2833 /// \brief Helper to set loop counter variable and its initializer. 2834 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB); 2835 /// \brief Helper to set upper bound. 2836 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR, 2837 SourceLocation SL); 2838 /// \brief Helper to set loop increment. 2839 bool SetStep(Expr *NewStep, bool Subtract); 2840 }; 2841 2842 bool OpenMPIterationSpaceChecker::Dependent() const { 2843 if (!LCDecl) { 2844 assert(!LB && !UB && !Step); 2845 return false; 2846 } 2847 return LCDecl->getType()->isDependentType() || 2848 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 2849 (Step && Step->isValueDependent()); 2850 } 2851 2852 static Expr *getExprAsWritten(Expr *E) { 2853 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E)) 2854 E = ExprTemp->getSubExpr(); 2855 2856 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 2857 E = MTE->GetTemporaryExpr(); 2858 2859 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 2860 E = Binder->getSubExpr(); 2861 2862 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 2863 E = ICE->getSubExprAsWritten(); 2864 return E->IgnoreParens(); 2865 } 2866 2867 bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl, 2868 Expr *NewLCRefExpr, 2869 Expr *NewLB) { 2870 // State consistency checking to ensure correct usage. 2871 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 2872 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 2873 if (!NewLCDecl || !NewLB) 2874 return true; 2875 LCDecl = getCanonicalDecl(NewLCDecl); 2876 LCRef = NewLCRefExpr; 2877 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 2878 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 2879 if ((Ctor->isCopyOrMoveConstructor() || 2880 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 2881 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 2882 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 2883 LB = NewLB; 2884 return false; 2885 } 2886 2887 bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp, 2888 SourceRange SR, SourceLocation SL) { 2889 // State consistency checking to ensure correct usage. 2890 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 2891 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 2892 if (!NewUB) 2893 return true; 2894 UB = NewUB; 2895 TestIsLessOp = LessOp; 2896 TestIsStrictOp = StrictOp; 2897 ConditionSrcRange = SR; 2898 ConditionLoc = SL; 2899 return false; 2900 } 2901 2902 bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) { 2903 // State consistency checking to ensure correct usage. 2904 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 2905 if (!NewStep) 2906 return true; 2907 if (!NewStep->isValueDependent()) { 2908 // Check that the step is integer expression. 2909 SourceLocation StepLoc = NewStep->getLocStart(); 2910 ExprResult Val = 2911 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep); 2912 if (Val.isInvalid()) 2913 return true; 2914 NewStep = Val.get(); 2915 2916 // OpenMP [2.6, Canonical Loop Form, Restrictions] 2917 // If test-expr is of form var relational-op b and relational-op is < or 2918 // <= then incr-expr must cause var to increase on each iteration of the 2919 // loop. If test-expr is of form var relational-op b and relational-op is 2920 // > or >= then incr-expr must cause var to decrease on each iteration of 2921 // the loop. 2922 // If test-expr is of form b relational-op var and relational-op is < or 2923 // <= then incr-expr must cause var to decrease on each iteration of the 2924 // loop. If test-expr is of form b relational-op var and relational-op is 2925 // > or >= then incr-expr must cause var to increase on each iteration of 2926 // the loop. 2927 llvm::APSInt Result; 2928 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); 2929 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 2930 bool IsConstNeg = 2931 IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); 2932 bool IsConstPos = 2933 IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); 2934 bool IsConstZero = IsConstant && !Result.getBoolValue(); 2935 if (UB && (IsConstZero || 2936 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract)) 2937 : (IsConstPos || (IsUnsigned && !Subtract))))) { 2938 SemaRef.Diag(NewStep->getExprLoc(), 2939 diag::err_omp_loop_incr_not_compatible) 2940 << LCDecl << TestIsLessOp << NewStep->getSourceRange(); 2941 SemaRef.Diag(ConditionLoc, 2942 diag::note_omp_loop_cond_requres_compatible_incr) 2943 << TestIsLessOp << ConditionSrcRange; 2944 return true; 2945 } 2946 if (TestIsLessOp == Subtract) { 2947 NewStep = 2948 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 2949 .get(); 2950 Subtract = !Subtract; 2951 } 2952 } 2953 2954 Step = NewStep; 2955 SubtractStep = Subtract; 2956 return false; 2957 } 2958 2959 bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) { 2960 // Check init-expr for canonical loop form and save loop counter 2961 // variable - #Var and its initialization value - #LB. 2962 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 2963 // var = lb 2964 // integer-type var = lb 2965 // random-access-iterator-type var = lb 2966 // pointer-type var = lb 2967 // 2968 if (!S) { 2969 if (EmitDiags) { 2970 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 2971 } 2972 return true; 2973 } 2974 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 2975 if (!ExprTemp->cleanupsHaveSideEffects()) 2976 S = ExprTemp->getSubExpr(); 2977 2978 InitSrcRange = S->getSourceRange(); 2979 if (Expr *E = dyn_cast<Expr>(S)) 2980 S = E->IgnoreParens(); 2981 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 2982 if (BO->getOpcode() == BO_Assign) { 2983 auto *LHS = BO->getLHS()->IgnoreParens(); 2984 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 2985 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 2986 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 2987 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 2988 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS()); 2989 } 2990 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 2991 if (ME->isArrow() && 2992 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 2993 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 2994 } 2995 } 2996 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 2997 if (DS->isSingleDecl()) { 2998 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 2999 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 3000 // Accept non-canonical init form here but emit ext. warning. 3001 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 3002 SemaRef.Diag(S->getLocStart(), 3003 diag::ext_omp_loop_not_canonical_init) 3004 << S->getSourceRange(); 3005 return SetLCDeclAndLB(Var, nullptr, Var->getInit()); 3006 } 3007 } 3008 } 3009 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 3010 if (CE->getOperator() == OO_Equal) { 3011 auto *LHS = CE->getArg(0); 3012 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 3013 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 3014 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 3015 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3016 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1)); 3017 } 3018 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 3019 if (ME->isArrow() && 3020 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 3021 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3022 } 3023 } 3024 } 3025 3026 if (Dependent() || SemaRef.CurContext->isDependentContext()) 3027 return false; 3028 if (EmitDiags) { 3029 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init) 3030 << S->getSourceRange(); 3031 } 3032 return true; 3033 } 3034 3035 /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the 3036 /// variable (which may be the loop variable) if possible. 3037 static const ValueDecl *GetInitLCDecl(Expr *E) { 3038 if (!E) 3039 return nullptr; 3040 E = getExprAsWritten(E); 3041 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 3042 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 3043 if ((Ctor->isCopyOrMoveConstructor() || 3044 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 3045 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 3046 E = CE->getArg(0)->IgnoreParenImpCasts(); 3047 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 3048 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) { 3049 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD)) 3050 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 3051 return getCanonicalDecl(ME->getMemberDecl()); 3052 return getCanonicalDecl(VD); 3053 } 3054 } 3055 if (auto *ME = dyn_cast_or_null<MemberExpr>(E)) 3056 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 3057 return getCanonicalDecl(ME->getMemberDecl()); 3058 return nullptr; 3059 } 3060 3061 bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) { 3062 // Check test-expr for canonical form, save upper-bound UB, flags for 3063 // less/greater and for strict/non-strict comparison. 3064 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 3065 // var relational-op b 3066 // b relational-op var 3067 // 3068 if (!S) { 3069 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl; 3070 return true; 3071 } 3072 S = getExprAsWritten(S); 3073 SourceLocation CondLoc = S->getLocStart(); 3074 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 3075 if (BO->isRelationalOp()) { 3076 if (GetInitLCDecl(BO->getLHS()) == LCDecl) 3077 return SetUB(BO->getRHS(), 3078 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 3079 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 3080 BO->getSourceRange(), BO->getOperatorLoc()); 3081 if (GetInitLCDecl(BO->getRHS()) == LCDecl) 3082 return SetUB(BO->getLHS(), 3083 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 3084 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 3085 BO->getSourceRange(), BO->getOperatorLoc()); 3086 } 3087 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 3088 if (CE->getNumArgs() == 2) { 3089 auto Op = CE->getOperator(); 3090 switch (Op) { 3091 case OO_Greater: 3092 case OO_GreaterEqual: 3093 case OO_Less: 3094 case OO_LessEqual: 3095 if (GetInitLCDecl(CE->getArg(0)) == LCDecl) 3096 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 3097 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 3098 CE->getOperatorLoc()); 3099 if (GetInitLCDecl(CE->getArg(1)) == LCDecl) 3100 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 3101 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 3102 CE->getOperatorLoc()); 3103 break; 3104 default: 3105 break; 3106 } 3107 } 3108 } 3109 if (Dependent() || SemaRef.CurContext->isDependentContext()) 3110 return false; 3111 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 3112 << S->getSourceRange() << LCDecl; 3113 return true; 3114 } 3115 3116 bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) { 3117 // RHS of canonical loop form increment can be: 3118 // var + incr 3119 // incr + var 3120 // var - incr 3121 // 3122 RHS = RHS->IgnoreParenImpCasts(); 3123 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 3124 if (BO->isAdditiveOp()) { 3125 bool IsAdd = BO->getOpcode() == BO_Add; 3126 if (GetInitLCDecl(BO->getLHS()) == LCDecl) 3127 return SetStep(BO->getRHS(), !IsAdd); 3128 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl) 3129 return SetStep(BO->getLHS(), false); 3130 } 3131 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 3132 bool IsAdd = CE->getOperator() == OO_Plus; 3133 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 3134 if (GetInitLCDecl(CE->getArg(0)) == LCDecl) 3135 return SetStep(CE->getArg(1), !IsAdd); 3136 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl) 3137 return SetStep(CE->getArg(0), false); 3138 } 3139 } 3140 if (Dependent() || SemaRef.CurContext->isDependentContext()) 3141 return false; 3142 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr) 3143 << RHS->getSourceRange() << LCDecl; 3144 return true; 3145 } 3146 3147 bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) { 3148 // Check incr-expr for canonical loop form and return true if it 3149 // does not conform. 3150 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 3151 // ++var 3152 // var++ 3153 // --var 3154 // var-- 3155 // var += incr 3156 // var -= incr 3157 // var = var + incr 3158 // var = incr + var 3159 // var = var - incr 3160 // 3161 if (!S) { 3162 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 3163 return true; 3164 } 3165 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 3166 if (!ExprTemp->cleanupsHaveSideEffects()) 3167 S = ExprTemp->getSubExpr(); 3168 3169 IncrementSrcRange = S->getSourceRange(); 3170 S = S->IgnoreParens(); 3171 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 3172 if (UO->isIncrementDecrementOp() && 3173 GetInitLCDecl(UO->getSubExpr()) == LCDecl) 3174 return SetStep(SemaRef 3175 .ActOnIntegerConstant(UO->getLocStart(), 3176 (UO->isDecrementOp() ? -1 : 1)) 3177 .get(), 3178 false); 3179 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 3180 switch (BO->getOpcode()) { 3181 case BO_AddAssign: 3182 case BO_SubAssign: 3183 if (GetInitLCDecl(BO->getLHS()) == LCDecl) 3184 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 3185 break; 3186 case BO_Assign: 3187 if (GetInitLCDecl(BO->getLHS()) == LCDecl) 3188 return CheckIncRHS(BO->getRHS()); 3189 break; 3190 default: 3191 break; 3192 } 3193 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 3194 switch (CE->getOperator()) { 3195 case OO_PlusPlus: 3196 case OO_MinusMinus: 3197 if (GetInitLCDecl(CE->getArg(0)) == LCDecl) 3198 return SetStep(SemaRef 3199 .ActOnIntegerConstant( 3200 CE->getLocStart(), 3201 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 3202 .get(), 3203 false); 3204 break; 3205 case OO_PlusEqual: 3206 case OO_MinusEqual: 3207 if (GetInitLCDecl(CE->getArg(0)) == LCDecl) 3208 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 3209 break; 3210 case OO_Equal: 3211 if (GetInitLCDecl(CE->getArg(0)) == LCDecl) 3212 return CheckIncRHS(CE->getArg(1)); 3213 break; 3214 default: 3215 break; 3216 } 3217 } 3218 if (Dependent() || SemaRef.CurContext->isDependentContext()) 3219 return false; 3220 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr) 3221 << S->getSourceRange() << LCDecl; 3222 return true; 3223 } 3224 3225 static ExprResult 3226 tryBuildCapture(Sema &SemaRef, Expr *Capture, 3227 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { 3228 if (SemaRef.CurContext->isDependentContext()) 3229 return ExprResult(Capture); 3230 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 3231 return SemaRef.PerformImplicitConversion( 3232 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 3233 /*AllowExplicit=*/true); 3234 auto I = Captures.find(Capture); 3235 if (I != Captures.end()) 3236 return buildCapture(SemaRef, Capture, I->second); 3237 DeclRefExpr *Ref = nullptr; 3238 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 3239 Captures[Capture] = Ref; 3240 return Res; 3241 } 3242 3243 /// \brief Build the expression to calculate the number of iterations. 3244 Expr *OpenMPIterationSpaceChecker::BuildNumIterations( 3245 Scope *S, const bool LimitedType, 3246 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const { 3247 ExprResult Diff; 3248 auto VarType = LCDecl->getType().getNonReferenceType(); 3249 if (VarType->isIntegerType() || VarType->isPointerType() || 3250 SemaRef.getLangOpts().CPlusPlus) { 3251 // Upper - Lower 3252 auto *UBExpr = TestIsLessOp ? UB : LB; 3253 auto *LBExpr = TestIsLessOp ? LB : UB; 3254 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 3255 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 3256 if (!Upper || !Lower) 3257 return nullptr; 3258 3259 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 3260 3261 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 3262 // BuildBinOp already emitted error, this one is to point user to upper 3263 // and lower bound, and to tell what is passed to 'operator-'. 3264 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx) 3265 << Upper->getSourceRange() << Lower->getSourceRange(); 3266 return nullptr; 3267 } 3268 } 3269 3270 if (!Diff.isUsable()) 3271 return nullptr; 3272 3273 // Upper - Lower [- 1] 3274 if (TestIsStrictOp) 3275 Diff = SemaRef.BuildBinOp( 3276 S, DefaultLoc, BO_Sub, Diff.get(), 3277 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 3278 if (!Diff.isUsable()) 3279 return nullptr; 3280 3281 // Upper - Lower [- 1] + Step 3282 auto NewStep = tryBuildCapture(SemaRef, Step, Captures); 3283 if (!NewStep.isUsable()) 3284 return nullptr; 3285 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 3286 if (!Diff.isUsable()) 3287 return nullptr; 3288 3289 // Parentheses (for dumping/debugging purposes only). 3290 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 3291 if (!Diff.isUsable()) 3292 return nullptr; 3293 3294 // (Upper - Lower [- 1] + Step) / Step 3295 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 3296 if (!Diff.isUsable()) 3297 return nullptr; 3298 3299 // OpenMP runtime requires 32-bit or 64-bit loop variables. 3300 QualType Type = Diff.get()->getType(); 3301 auto &C = SemaRef.Context; 3302 bool UseVarType = VarType->hasIntegerRepresentation() && 3303 C.getTypeSize(Type) > C.getTypeSize(VarType); 3304 if (!Type->isIntegerType() || UseVarType) { 3305 unsigned NewSize = 3306 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 3307 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 3308 : Type->hasSignedIntegerRepresentation(); 3309 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 3310 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 3311 Diff = SemaRef.PerformImplicitConversion( 3312 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 3313 if (!Diff.isUsable()) 3314 return nullptr; 3315 } 3316 } 3317 if (LimitedType) { 3318 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 3319 if (NewSize != C.getTypeSize(Type)) { 3320 if (NewSize < C.getTypeSize(Type)) { 3321 assert(NewSize == 64 && "incorrect loop var size"); 3322 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 3323 << InitSrcRange << ConditionSrcRange; 3324 } 3325 QualType NewType = C.getIntTypeForBitwidth( 3326 NewSize, Type->hasSignedIntegerRepresentation() || 3327 C.getTypeSize(Type) < NewSize); 3328 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 3329 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 3330 Sema::AA_Converting, true); 3331 if (!Diff.isUsable()) 3332 return nullptr; 3333 } 3334 } 3335 } 3336 3337 return Diff.get(); 3338 } 3339 3340 Expr *OpenMPIterationSpaceChecker::BuildPreCond( 3341 Scope *S, Expr *Cond, 3342 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const { 3343 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 3344 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 3345 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 3346 3347 auto NewLB = tryBuildCapture(SemaRef, LB, Captures); 3348 auto NewUB = tryBuildCapture(SemaRef, UB, Captures); 3349 if (!NewLB.isUsable() || !NewUB.isUsable()) 3350 return nullptr; 3351 3352 auto CondExpr = SemaRef.BuildBinOp( 3353 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE) 3354 : (TestIsStrictOp ? BO_GT : BO_GE), 3355 NewLB.get(), NewUB.get()); 3356 if (CondExpr.isUsable()) { 3357 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 3358 SemaRef.Context.BoolTy)) 3359 CondExpr = SemaRef.PerformImplicitConversion( 3360 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 3361 /*AllowExplicit=*/true); 3362 } 3363 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 3364 // Otherwise use original loop conditon and evaluate it in runtime. 3365 return CondExpr.isUsable() ? CondExpr.get() : Cond; 3366 } 3367 3368 /// \brief Build reference expression to the counter be used for codegen. 3369 DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar( 3370 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const { 3371 auto *VD = dyn_cast<VarDecl>(LCDecl); 3372 if (!VD) { 3373 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl); 3374 auto *Ref = buildDeclRefExpr( 3375 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 3376 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false); 3377 // If the loop control decl is explicitly marked as private, do not mark it 3378 // as captured again. 3379 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 3380 Captures.insert(std::make_pair(LCRef, Ref)); 3381 return Ref; 3382 } 3383 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(), 3384 DefaultLoc); 3385 } 3386 3387 Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const { 3388 if (LCDecl && !LCDecl->isInvalidDecl()) { 3389 auto Type = LCDecl->getType().getNonReferenceType(); 3390 auto *PrivateVar = 3391 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(), 3392 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr); 3393 if (PrivateVar->isInvalidDecl()) 3394 return nullptr; 3395 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 3396 } 3397 return nullptr; 3398 } 3399 3400 /// \brief Build initialization of the counter to be used for codegen. 3401 Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; } 3402 3403 /// \brief Build step of the counter be used for codegen. 3404 Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; } 3405 3406 /// \brief Iteration space of a single for loop. 3407 struct LoopIterationSpace final { 3408 /// \brief Condition of the loop. 3409 Expr *PreCond = nullptr; 3410 /// \brief This expression calculates the number of iterations in the loop. 3411 /// It is always possible to calculate it before starting the loop. 3412 Expr *NumIterations = nullptr; 3413 /// \brief The loop counter variable. 3414 Expr *CounterVar = nullptr; 3415 /// \brief Private loop counter variable. 3416 Expr *PrivateCounterVar = nullptr; 3417 /// \brief This is initializer for the initial value of #CounterVar. 3418 Expr *CounterInit = nullptr; 3419 /// \brief This is step for the #CounterVar used to generate its update: 3420 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 3421 Expr *CounterStep = nullptr; 3422 /// \brief Should step be subtracted? 3423 bool Subtract = false; 3424 /// \brief Source range of the loop init. 3425 SourceRange InitSrcRange; 3426 /// \brief Source range of the loop condition. 3427 SourceRange CondSrcRange; 3428 /// \brief Source range of the loop increment. 3429 SourceRange IncSrcRange; 3430 }; 3431 3432 } // namespace 3433 3434 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 3435 assert(getLangOpts().OpenMP && "OpenMP is not active."); 3436 assert(Init && "Expected loop in canonical form."); 3437 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 3438 if (AssociatedLoops > 0 && 3439 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 3440 OpenMPIterationSpaceChecker ISC(*this, ForLoc); 3441 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) { 3442 if (auto *D = ISC.GetLoopDecl()) { 3443 auto *VD = dyn_cast<VarDecl>(D); 3444 if (!VD) { 3445 if (auto *Private = IsOpenMPCapturedDecl(D)) 3446 VD = Private; 3447 else { 3448 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(), 3449 /*WithInit=*/false); 3450 VD = cast<VarDecl>(Ref->getDecl()); 3451 } 3452 } 3453 DSAStack->addLoopControlVariable(D, VD); 3454 } 3455 } 3456 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 3457 } 3458 } 3459 3460 /// \brief Called on a for stmt to check and extract its iteration space 3461 /// for further processing (such as collapsing). 3462 static bool CheckOpenMPIterationSpace( 3463 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 3464 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 3465 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr, 3466 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA, 3467 LoopIterationSpace &ResultIterSpace, 3468 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { 3469 // OpenMP [2.6, Canonical Loop Form] 3470 // for (init-expr; test-expr; incr-expr) structured-block 3471 auto *For = dyn_cast_or_null<ForStmt>(S); 3472 if (!For) { 3473 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for) 3474 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 3475 << getOpenMPDirectiveName(DKind) << NestedLoopCount 3476 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 3477 if (NestedLoopCount > 1) { 3478 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 3479 SemaRef.Diag(DSA.getConstructLoc(), 3480 diag::note_omp_collapse_ordered_expr) 3481 << 2 << CollapseLoopCountExpr->getSourceRange() 3482 << OrderedLoopCountExpr->getSourceRange(); 3483 else if (CollapseLoopCountExpr) 3484 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 3485 diag::note_omp_collapse_ordered_expr) 3486 << 0 << CollapseLoopCountExpr->getSourceRange(); 3487 else 3488 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 3489 diag::note_omp_collapse_ordered_expr) 3490 << 1 << OrderedLoopCountExpr->getSourceRange(); 3491 } 3492 return true; 3493 } 3494 assert(For->getBody()); 3495 3496 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc()); 3497 3498 // Check init. 3499 auto Init = For->getInit(); 3500 if (ISC.CheckInit(Init)) 3501 return true; 3502 3503 bool HasErrors = false; 3504 3505 // Check loop variable's type. 3506 if (auto *LCDecl = ISC.GetLoopDecl()) { 3507 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr(); 3508 3509 // OpenMP [2.6, Canonical Loop Form] 3510 // Var is one of the following: 3511 // A variable of signed or unsigned integer type. 3512 // For C++, a variable of a random access iterator type. 3513 // For C, a variable of a pointer type. 3514 auto VarType = LCDecl->getType().getNonReferenceType(); 3515 if (!VarType->isDependentType() && !VarType->isIntegerType() && 3516 !VarType->isPointerType() && 3517 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 3518 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type) 3519 << SemaRef.getLangOpts().CPlusPlus; 3520 HasErrors = true; 3521 } 3522 3523 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 3524 // a Construct 3525 // The loop iteration variable(s) in the associated for-loop(s) of a for or 3526 // parallel for construct is (are) private. 3527 // The loop iteration variable in the associated for-loop of a simd 3528 // construct with just one associated for-loop is linear with a 3529 // constant-linear-step that is the increment of the associated for-loop. 3530 // Exclude loop var from the list of variables with implicitly defined data 3531 // sharing attributes. 3532 VarsWithImplicitDSA.erase(LCDecl); 3533 3534 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 3535 // in a Construct, C/C++]. 3536 // The loop iteration variable in the associated for-loop of a simd 3537 // construct with just one associated for-loop may be listed in a linear 3538 // clause with a constant-linear-step that is the increment of the 3539 // associated for-loop. 3540 // The loop iteration variable(s) in the associated for-loop(s) of a for or 3541 // parallel for construct may be listed in a private or lastprivate clause. 3542 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false); 3543 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is 3544 // declared in the loop and it is predetermined as a private. 3545 auto PredeterminedCKind = 3546 isOpenMPSimdDirective(DKind) 3547 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate) 3548 : OMPC_private; 3549 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 3550 DVar.CKind != PredeterminedCKind) || 3551 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 3552 isOpenMPDistributeDirective(DKind)) && 3553 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 3554 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 3555 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 3556 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa) 3557 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind) 3558 << getOpenMPClauseName(PredeterminedCKind); 3559 if (DVar.RefExpr == nullptr) 3560 DVar.CKind = PredeterminedCKind; 3561 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true); 3562 HasErrors = true; 3563 } else if (LoopDeclRefExpr != nullptr) { 3564 // Make the loop iteration variable private (for worksharing constructs), 3565 // linear (for simd directives with the only one associated loop) or 3566 // lastprivate (for simd directives with several collapsed or ordered 3567 // loops). 3568 if (DVar.CKind == OMPC_unknown) 3569 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate, 3570 [](OpenMPDirectiveKind) -> bool { return true; }, 3571 /*FromParent=*/false); 3572 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind); 3573 } 3574 3575 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 3576 3577 // Check test-expr. 3578 HasErrors |= ISC.CheckCond(For->getCond()); 3579 3580 // Check incr-expr. 3581 HasErrors |= ISC.CheckInc(For->getInc()); 3582 } 3583 3584 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 3585 return HasErrors; 3586 3587 // Build the loop's iteration space representation. 3588 ResultIterSpace.PreCond = 3589 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures); 3590 ResultIterSpace.NumIterations = ISC.BuildNumIterations( 3591 DSA.getCurScope(), 3592 (isOpenMPWorksharingDirective(DKind) || 3593 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)), 3594 Captures); 3595 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA); 3596 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar(); 3597 ResultIterSpace.CounterInit = ISC.BuildCounterInit(); 3598 ResultIterSpace.CounterStep = ISC.BuildCounterStep(); 3599 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange(); 3600 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange(); 3601 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange(); 3602 ResultIterSpace.Subtract = ISC.ShouldSubtractStep(); 3603 3604 HasErrors |= (ResultIterSpace.PreCond == nullptr || 3605 ResultIterSpace.NumIterations == nullptr || 3606 ResultIterSpace.CounterVar == nullptr || 3607 ResultIterSpace.PrivateCounterVar == nullptr || 3608 ResultIterSpace.CounterInit == nullptr || 3609 ResultIterSpace.CounterStep == nullptr); 3610 3611 return HasErrors; 3612 } 3613 3614 /// \brief Build 'VarRef = Start. 3615 static ExprResult 3616 BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 3617 ExprResult Start, 3618 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { 3619 // Build 'VarRef = Start. 3620 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures); 3621 if (!NewStart.isUsable()) 3622 return ExprError(); 3623 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 3624 VarRef.get()->getType())) { 3625 NewStart = SemaRef.PerformImplicitConversion( 3626 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 3627 /*AllowExplicit=*/true); 3628 if (!NewStart.isUsable()) 3629 return ExprError(); 3630 } 3631 3632 auto Init = 3633 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 3634 return Init; 3635 } 3636 3637 /// \brief Build 'VarRef = Start + Iter * Step'. 3638 static ExprResult 3639 BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc, 3640 ExprResult VarRef, ExprResult Start, ExprResult Iter, 3641 ExprResult Step, bool Subtract, 3642 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) { 3643 // Add parentheses (for debugging purposes only). 3644 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 3645 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 3646 !Step.isUsable()) 3647 return ExprError(); 3648 3649 ExprResult NewStep = Step; 3650 if (Captures) 3651 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 3652 if (NewStep.isInvalid()) 3653 return ExprError(); 3654 ExprResult Update = 3655 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 3656 if (!Update.isUsable()) 3657 return ExprError(); 3658 3659 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 3660 // 'VarRef = Start (+|-) Iter * Step'. 3661 ExprResult NewStart = Start; 3662 if (Captures) 3663 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 3664 if (NewStart.isInvalid()) 3665 return ExprError(); 3666 3667 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 3668 ExprResult SavedUpdate = Update; 3669 ExprResult UpdateVal; 3670 if (VarRef.get()->getType()->isOverloadableType() || 3671 NewStart.get()->getType()->isOverloadableType() || 3672 Update.get()->getType()->isOverloadableType()) { 3673 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 3674 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 3675 Update = 3676 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 3677 if (Update.isUsable()) { 3678 UpdateVal = 3679 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 3680 VarRef.get(), SavedUpdate.get()); 3681 if (UpdateVal.isUsable()) { 3682 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 3683 UpdateVal.get()); 3684 } 3685 } 3686 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 3687 } 3688 3689 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 3690 if (!Update.isUsable() || !UpdateVal.isUsable()) { 3691 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 3692 NewStart.get(), SavedUpdate.get()); 3693 if (!Update.isUsable()) 3694 return ExprError(); 3695 3696 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 3697 VarRef.get()->getType())) { 3698 Update = SemaRef.PerformImplicitConversion( 3699 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 3700 if (!Update.isUsable()) 3701 return ExprError(); 3702 } 3703 3704 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 3705 } 3706 return Update; 3707 } 3708 3709 /// \brief Convert integer expression \a E to make it have at least \a Bits 3710 /// bits. 3711 static ExprResult WidenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 3712 if (E == nullptr) 3713 return ExprError(); 3714 auto &C = SemaRef.Context; 3715 QualType OldType = E->getType(); 3716 unsigned HasBits = C.getTypeSize(OldType); 3717 if (HasBits >= Bits) 3718 return ExprResult(E); 3719 // OK to convert to signed, because new type has more bits than old. 3720 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 3721 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 3722 true); 3723 } 3724 3725 /// \brief Check if the given expression \a E is a constant integer that fits 3726 /// into \a Bits bits. 3727 static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) { 3728 if (E == nullptr) 3729 return false; 3730 llvm::APSInt Result; 3731 if (E->isIntegerConstantExpr(Result, SemaRef.Context)) 3732 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); 3733 return false; 3734 } 3735 3736 /// Build preinits statement for the given declarations. 3737 static Stmt *buildPreInits(ASTContext &Context, 3738 SmallVectorImpl<Decl *> &PreInits) { 3739 if (!PreInits.empty()) { 3740 return new (Context) DeclStmt( 3741 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 3742 SourceLocation(), SourceLocation()); 3743 } 3744 return nullptr; 3745 } 3746 3747 /// Build preinits statement for the given declarations. 3748 static Stmt *buildPreInits(ASTContext &Context, 3749 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) { 3750 if (!Captures.empty()) { 3751 SmallVector<Decl *, 16> PreInits; 3752 for (auto &Pair : Captures) 3753 PreInits.push_back(Pair.second->getDecl()); 3754 return buildPreInits(Context, PreInits); 3755 } 3756 return nullptr; 3757 } 3758 3759 /// Build postupdate expression for the given list of postupdates expressions. 3760 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 3761 Expr *PostUpdate = nullptr; 3762 if (!PostUpdates.empty()) { 3763 for (auto *E : PostUpdates) { 3764 Expr *ConvE = S.BuildCStyleCastExpr( 3765 E->getExprLoc(), 3766 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 3767 E->getExprLoc(), E) 3768 .get(); 3769 PostUpdate = PostUpdate 3770 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 3771 PostUpdate, ConvE) 3772 .get() 3773 : ConvE; 3774 } 3775 } 3776 return PostUpdate; 3777 } 3778 3779 /// \brief Called on a for stmt to check itself and nested loops (if any). 3780 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 3781 /// number of collapsed loops otherwise. 3782 static unsigned 3783 CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 3784 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 3785 DSAStackTy &DSA, 3786 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA, 3787 OMPLoopDirective::HelperExprs &Built) { 3788 unsigned NestedLoopCount = 1; 3789 if (CollapseLoopCountExpr) { 3790 // Found 'collapse' clause - calculate collapse number. 3791 llvm::APSInt Result; 3792 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) 3793 NestedLoopCount = Result.getLimitedValue(); 3794 } 3795 if (OrderedLoopCountExpr) { 3796 // Found 'ordered' clause - calculate collapse number. 3797 llvm::APSInt Result; 3798 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 3799 if (Result.getLimitedValue() < NestedLoopCount) { 3800 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 3801 diag::err_omp_wrong_ordered_loop_count) 3802 << OrderedLoopCountExpr->getSourceRange(); 3803 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 3804 diag::note_collapse_loop_count) 3805 << CollapseLoopCountExpr->getSourceRange(); 3806 } 3807 NestedLoopCount = Result.getLimitedValue(); 3808 } 3809 } 3810 // This is helper routine for loop directives (e.g., 'for', 'simd', 3811 // 'for simd', etc.). 3812 llvm::MapVector<Expr *, DeclRefExpr *> Captures; 3813 SmallVector<LoopIterationSpace, 4> IterSpaces; 3814 IterSpaces.resize(NestedLoopCount); 3815 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); 3816 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 3817 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt, 3818 NestedLoopCount, CollapseLoopCountExpr, 3819 OrderedLoopCountExpr, VarsWithImplicitDSA, 3820 IterSpaces[Cnt], Captures)) 3821 return 0; 3822 // Move on to the next nested for loop, or to the loop body. 3823 // OpenMP [2.8.1, simd construct, Restrictions] 3824 // All loops associated with the construct must be perfectly nested; that 3825 // is, there must be no intervening code nor any OpenMP directive between 3826 // any two loops. 3827 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); 3828 } 3829 3830 Built.clear(/* size */ NestedLoopCount); 3831 3832 if (SemaRef.CurContext->isDependentContext()) 3833 return NestedLoopCount; 3834 3835 // An example of what is generated for the following code: 3836 // 3837 // #pragma omp simd collapse(2) ordered(2) 3838 // for (i = 0; i < NI; ++i) 3839 // for (k = 0; k < NK; ++k) 3840 // for (j = J0; j < NJ; j+=2) { 3841 // <loop body> 3842 // } 3843 // 3844 // We generate the code below. 3845 // Note: the loop body may be outlined in CodeGen. 3846 // Note: some counters may be C++ classes, operator- is used to find number of 3847 // iterations and operator+= to calculate counter value. 3848 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 3849 // or i64 is currently supported). 3850 // 3851 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 3852 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 3853 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 3854 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 3855 // // similar updates for vars in clauses (e.g. 'linear') 3856 // <loop body (using local i and j)> 3857 // } 3858 // i = NI; // assign final values of counters 3859 // j = NJ; 3860 // 3861 3862 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 3863 // the iteration counts of the collapsed for loops. 3864 // Precondition tests if there is at least one iteration (all conditions are 3865 // true). 3866 auto PreCond = ExprResult(IterSpaces[0].PreCond); 3867 auto N0 = IterSpaces[0].NumIterations; 3868 ExprResult LastIteration32 = WidenIterationCount( 3869 32 /* Bits */, SemaRef 3870 .PerformImplicitConversion( 3871 N0->IgnoreImpCasts(), N0->getType(), 3872 Sema::AA_Converting, /*AllowExplicit=*/true) 3873 .get(), 3874 SemaRef); 3875 ExprResult LastIteration64 = WidenIterationCount( 3876 64 /* Bits */, SemaRef 3877 .PerformImplicitConversion( 3878 N0->IgnoreImpCasts(), N0->getType(), 3879 Sema::AA_Converting, /*AllowExplicit=*/true) 3880 .get(), 3881 SemaRef); 3882 3883 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 3884 return NestedLoopCount; 3885 3886 auto &C = SemaRef.Context; 3887 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 3888 3889 Scope *CurScope = DSA.getCurScope(); 3890 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 3891 if (PreCond.isUsable()) { 3892 PreCond = 3893 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 3894 PreCond.get(), IterSpaces[Cnt].PreCond); 3895 } 3896 auto N = IterSpaces[Cnt].NumIterations; 3897 SourceLocation Loc = N->getExprLoc(); 3898 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 3899 if (LastIteration32.isUsable()) 3900 LastIteration32 = SemaRef.BuildBinOp( 3901 CurScope, Loc, BO_Mul, LastIteration32.get(), 3902 SemaRef 3903 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 3904 Sema::AA_Converting, 3905 /*AllowExplicit=*/true) 3906 .get()); 3907 if (LastIteration64.isUsable()) 3908 LastIteration64 = SemaRef.BuildBinOp( 3909 CurScope, Loc, BO_Mul, LastIteration64.get(), 3910 SemaRef 3911 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 3912 Sema::AA_Converting, 3913 /*AllowExplicit=*/true) 3914 .get()); 3915 } 3916 3917 // Choose either the 32-bit or 64-bit version. 3918 ExprResult LastIteration = LastIteration64; 3919 if (LastIteration32.isUsable() && 3920 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 3921 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 3922 FitsInto( 3923 32 /* Bits */, 3924 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 3925 LastIteration64.get(), SemaRef))) 3926 LastIteration = LastIteration32; 3927 QualType VType = LastIteration.get()->getType(); 3928 QualType RealVType = VType; 3929 QualType StrideVType = VType; 3930 if (isOpenMPTaskLoopDirective(DKind)) { 3931 VType = 3932 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 3933 StrideVType = 3934 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 3935 } 3936 3937 if (!LastIteration.isUsable()) 3938 return 0; 3939 3940 // Save the number of iterations. 3941 ExprResult NumIterations = LastIteration; 3942 { 3943 LastIteration = SemaRef.BuildBinOp( 3944 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 3945 LastIteration.get(), 3946 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 3947 if (!LastIteration.isUsable()) 3948 return 0; 3949 } 3950 3951 // Calculate the last iteration number beforehand instead of doing this on 3952 // each iteration. Do not do this if the number of iterations may be kfold-ed. 3953 llvm::APSInt Result; 3954 bool IsConstant = 3955 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); 3956 ExprResult CalcLastIteration; 3957 if (!IsConstant) { 3958 ExprResult SaveRef = 3959 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 3960 LastIteration = SaveRef; 3961 3962 // Prepare SaveRef + 1. 3963 NumIterations = SemaRef.BuildBinOp( 3964 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 3965 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 3966 if (!NumIterations.isUsable()) 3967 return 0; 3968 } 3969 3970 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 3971 3972 // Build variables passed into runtime, necessary for worksharing directives. 3973 ExprResult LB, UB, IL, ST, EUB, PrevLB, PrevUB; 3974 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 3975 isOpenMPDistributeDirective(DKind)) { 3976 // Lower bound variable, initialized with zero. 3977 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 3978 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 3979 SemaRef.AddInitializerToDecl( 3980 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 3981 /*DirectInit*/ false, /*TypeMayContainAuto*/ false); 3982 3983 // Upper bound variable, initialized with last iteration number. 3984 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 3985 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 3986 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 3987 /*DirectInit*/ false, 3988 /*TypeMayContainAuto*/ false); 3989 3990 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 3991 // This will be used to implement clause 'lastprivate'. 3992 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 3993 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 3994 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 3995 SemaRef.AddInitializerToDecl( 3996 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 3997 /*DirectInit*/ false, /*TypeMayContainAuto*/ false); 3998 3999 // Stride variable returned by runtime (we initialize it to 1 by default). 4000 VarDecl *STDecl = 4001 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 4002 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 4003 SemaRef.AddInitializerToDecl( 4004 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 4005 /*DirectInit*/ false, /*TypeMayContainAuto*/ false); 4006 4007 // Build expression: UB = min(UB, LastIteration) 4008 // It is necessary for CodeGen of directives with static scheduling. 4009 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 4010 UB.get(), LastIteration.get()); 4011 ExprResult CondOp = SemaRef.ActOnConditionalOp( 4012 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get()); 4013 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 4014 CondOp.get()); 4015 EUB = SemaRef.ActOnFinishFullExpr(EUB.get()); 4016 4017 // If we have a combined directive that combines 'distribute', 'for' or 4018 // 'simd' we need to be able to access the bounds of the schedule of the 4019 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 4020 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 4021 if (isOpenMPLoopBoundSharingDirective(DKind)) { 4022 auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 4023 4024 // We expect to have at least 2 more parameters than the 'parallel' 4025 // directive does - the lower and upper bounds of the previous schedule. 4026 assert(CD->getNumParams() >= 4 && 4027 "Unexpected number of parameters in loop combined directive"); 4028 4029 // Set the proper type for the bounds given what we learned from the 4030 // enclosed loops. 4031 auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 4032 auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 4033 4034 // Previous lower and upper bounds are obtained from the region 4035 // parameters. 4036 PrevLB = 4037 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 4038 PrevUB = 4039 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 4040 } 4041 } 4042 4043 // Build the iteration variable and its initialization before loop. 4044 ExprResult IV; 4045 ExprResult Init; 4046 { 4047 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 4048 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 4049 Expr *RHS = 4050 (isOpenMPWorksharingDirective(DKind) || 4051 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 4052 ? LB.get() 4053 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 4054 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 4055 Init = SemaRef.ActOnFinishFullExpr(Init.get()); 4056 } 4057 4058 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops. 4059 SourceLocation CondLoc; 4060 ExprResult Cond = 4061 (isOpenMPWorksharingDirective(DKind) || 4062 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 4063 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get()) 4064 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 4065 NumIterations.get()); 4066 4067 // Loop increment (IV = IV + 1) 4068 SourceLocation IncLoc; 4069 ExprResult Inc = 4070 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 4071 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 4072 if (!Inc.isUsable()) 4073 return 0; 4074 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 4075 Inc = SemaRef.ActOnFinishFullExpr(Inc.get()); 4076 if (!Inc.isUsable()) 4077 return 0; 4078 4079 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 4080 // Used for directives with static scheduling. 4081 ExprResult NextLB, NextUB; 4082 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 4083 isOpenMPDistributeDirective(DKind)) { 4084 // LB + ST 4085 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 4086 if (!NextLB.isUsable()) 4087 return 0; 4088 // LB = LB + ST 4089 NextLB = 4090 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 4091 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get()); 4092 if (!NextLB.isUsable()) 4093 return 0; 4094 // UB + ST 4095 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 4096 if (!NextUB.isUsable()) 4097 return 0; 4098 // UB = UB + ST 4099 NextUB = 4100 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 4101 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get()); 4102 if (!NextUB.isUsable()) 4103 return 0; 4104 } 4105 4106 // Build updates and final values of the loop counters. 4107 bool HasErrors = false; 4108 Built.Counters.resize(NestedLoopCount); 4109 Built.Inits.resize(NestedLoopCount); 4110 Built.Updates.resize(NestedLoopCount); 4111 Built.Finals.resize(NestedLoopCount); 4112 SmallVector<Expr *, 4> LoopMultipliers; 4113 { 4114 ExprResult Div; 4115 // Go from inner nested loop to outer. 4116 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) { 4117 LoopIterationSpace &IS = IterSpaces[Cnt]; 4118 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 4119 // Build: Iter = (IV / Div) % IS.NumIters 4120 // where Div is product of previous iterations' IS.NumIters. 4121 ExprResult Iter; 4122 if (Div.isUsable()) { 4123 Iter = 4124 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get()); 4125 } else { 4126 Iter = IV; 4127 assert((Cnt == (int)NestedLoopCount - 1) && 4128 "unusable div expected on first iteration only"); 4129 } 4130 4131 if (Cnt != 0 && Iter.isUsable()) 4132 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(), 4133 IS.NumIterations); 4134 if (!Iter.isUsable()) { 4135 HasErrors = true; 4136 break; 4137 } 4138 4139 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 4140 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 4141 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(), 4142 IS.CounterVar->getExprLoc(), 4143 /*RefersToCapture=*/true); 4144 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 4145 IS.CounterInit, Captures); 4146 if (!Init.isUsable()) { 4147 HasErrors = true; 4148 break; 4149 } 4150 ExprResult Update = BuildCounterUpdate( 4151 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 4152 IS.CounterStep, IS.Subtract, &Captures); 4153 if (!Update.isUsable()) { 4154 HasErrors = true; 4155 break; 4156 } 4157 4158 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 4159 ExprResult Final = BuildCounterUpdate( 4160 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, 4161 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures); 4162 if (!Final.isUsable()) { 4163 HasErrors = true; 4164 break; 4165 } 4166 4167 // Build Div for the next iteration: Div <- Div * IS.NumIters 4168 if (Cnt != 0) { 4169 if (Div.isUnset()) 4170 Div = IS.NumIterations; 4171 else 4172 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(), 4173 IS.NumIterations); 4174 4175 // Add parentheses (for debugging purposes only). 4176 if (Div.isUsable()) 4177 Div = tryBuildCapture(SemaRef, Div.get(), Captures); 4178 if (!Div.isUsable()) { 4179 HasErrors = true; 4180 break; 4181 } 4182 LoopMultipliers.push_back(Div.get()); 4183 } 4184 if (!Update.isUsable() || !Final.isUsable()) { 4185 HasErrors = true; 4186 break; 4187 } 4188 // Save results 4189 Built.Counters[Cnt] = IS.CounterVar; 4190 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 4191 Built.Inits[Cnt] = Init.get(); 4192 Built.Updates[Cnt] = Update.get(); 4193 Built.Finals[Cnt] = Final.get(); 4194 } 4195 } 4196 4197 if (HasErrors) 4198 return 0; 4199 4200 // Save results 4201 Built.IterationVarRef = IV.get(); 4202 Built.LastIteration = LastIteration.get(); 4203 Built.NumIterations = NumIterations.get(); 4204 Built.CalcLastIteration = 4205 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get(); 4206 Built.PreCond = PreCond.get(); 4207 Built.PreInits = buildPreInits(C, Captures); 4208 Built.Cond = Cond.get(); 4209 Built.Init = Init.get(); 4210 Built.Inc = Inc.get(); 4211 Built.LB = LB.get(); 4212 Built.UB = UB.get(); 4213 Built.IL = IL.get(); 4214 Built.ST = ST.get(); 4215 Built.EUB = EUB.get(); 4216 Built.NLB = NextLB.get(); 4217 Built.NUB = NextUB.get(); 4218 Built.PrevLB = PrevLB.get(); 4219 Built.PrevUB = PrevUB.get(); 4220 4221 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get(); 4222 // Fill data for doacross depend clauses. 4223 for (auto Pair : DSA.getDoacrossDependClauses()) { 4224 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 4225 Pair.first->setCounterValue(CounterVal); 4226 else { 4227 if (NestedLoopCount != Pair.second.size() || 4228 NestedLoopCount != LoopMultipliers.size() + 1) { 4229 // Erroneous case - clause has some problems. 4230 Pair.first->setCounterValue(CounterVal); 4231 continue; 4232 } 4233 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink); 4234 auto I = Pair.second.rbegin(); 4235 auto IS = IterSpaces.rbegin(); 4236 auto ILM = LoopMultipliers.rbegin(); 4237 Expr *UpCounterVal = CounterVal; 4238 Expr *Multiplier = nullptr; 4239 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) { 4240 if (I->first) { 4241 assert(IS->CounterStep); 4242 Expr *NormalizedOffset = 4243 SemaRef 4244 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div, 4245 I->first, IS->CounterStep) 4246 .get(); 4247 if (Multiplier) { 4248 NormalizedOffset = 4249 SemaRef 4250 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul, 4251 NormalizedOffset, Multiplier) 4252 .get(); 4253 } 4254 assert(I->second == OO_Plus || I->second == OO_Minus); 4255 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub; 4256 UpCounterVal = SemaRef 4257 .BuildBinOp(CurScope, I->first->getExprLoc(), BOK, 4258 UpCounterVal, NormalizedOffset) 4259 .get(); 4260 } 4261 Multiplier = *ILM; 4262 ++I; 4263 ++IS; 4264 ++ILM; 4265 } 4266 Pair.first->setCounterValue(UpCounterVal); 4267 } 4268 } 4269 4270 return NestedLoopCount; 4271 } 4272 4273 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 4274 auto CollapseClauses = 4275 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 4276 if (CollapseClauses.begin() != CollapseClauses.end()) 4277 return (*CollapseClauses.begin())->getNumForLoops(); 4278 return nullptr; 4279 } 4280 4281 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 4282 auto OrderedClauses = 4283 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 4284 if (OrderedClauses.begin() != OrderedClauses.end()) 4285 return (*OrderedClauses.begin())->getNumForLoops(); 4286 return nullptr; 4287 } 4288 4289 static bool checkSimdlenSafelenSpecified(Sema &S, 4290 const ArrayRef<OMPClause *> Clauses) { 4291 OMPSafelenClause *Safelen = nullptr; 4292 OMPSimdlenClause *Simdlen = nullptr; 4293 4294 for (auto *Clause : Clauses) { 4295 if (Clause->getClauseKind() == OMPC_safelen) 4296 Safelen = cast<OMPSafelenClause>(Clause); 4297 else if (Clause->getClauseKind() == OMPC_simdlen) 4298 Simdlen = cast<OMPSimdlenClause>(Clause); 4299 if (Safelen && Simdlen) 4300 break; 4301 } 4302 4303 if (Simdlen && Safelen) { 4304 llvm::APSInt SimdlenRes, SafelenRes; 4305 auto SimdlenLength = Simdlen->getSimdlen(); 4306 auto SafelenLength = Safelen->getSafelen(); 4307 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 4308 SimdlenLength->isInstantiationDependent() || 4309 SimdlenLength->containsUnexpandedParameterPack()) 4310 return false; 4311 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 4312 SafelenLength->isInstantiationDependent() || 4313 SafelenLength->containsUnexpandedParameterPack()) 4314 return false; 4315 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context); 4316 SafelenLength->EvaluateAsInt(SafelenRes, S.Context); 4317 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 4318 // If both simdlen and safelen clauses are specified, the value of the 4319 // simdlen parameter must be less than or equal to the value of the safelen 4320 // parameter. 4321 if (SimdlenRes > SafelenRes) { 4322 S.Diag(SimdlenLength->getExprLoc(), 4323 diag::err_omp_wrong_simdlen_safelen_values) 4324 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 4325 return true; 4326 } 4327 } 4328 return false; 4329 } 4330 4331 StmtResult Sema::ActOnOpenMPSimdDirective( 4332 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 4333 SourceLocation EndLoc, 4334 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 4335 if (!AStmt) 4336 return StmtError(); 4337 4338 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4339 OMPLoopDirective::HelperExprs B; 4340 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 4341 // define the nested loops number. 4342 unsigned NestedLoopCount = CheckOpenMPLoop( 4343 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 4344 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 4345 if (NestedLoopCount == 0) 4346 return StmtError(); 4347 4348 assert((CurContext->isDependentContext() || B.builtAll()) && 4349 "omp simd loop exprs were not built"); 4350 4351 if (!CurContext->isDependentContext()) { 4352 // Finalize the clauses that need pre-built expressions for CodeGen. 4353 for (auto C : Clauses) { 4354 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 4355 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 4356 B.NumIterations, *this, CurScope, 4357 DSAStack)) 4358 return StmtError(); 4359 } 4360 } 4361 4362 if (checkSimdlenSafelenSpecified(*this, Clauses)) 4363 return StmtError(); 4364 4365 getCurFunction()->setHasBranchProtectedScope(); 4366 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 4367 Clauses, AStmt, B); 4368 } 4369 4370 StmtResult Sema::ActOnOpenMPForDirective( 4371 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 4372 SourceLocation EndLoc, 4373 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 4374 if (!AStmt) 4375 return StmtError(); 4376 4377 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4378 OMPLoopDirective::HelperExprs B; 4379 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 4380 // define the nested loops number. 4381 unsigned NestedLoopCount = CheckOpenMPLoop( 4382 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 4383 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 4384 if (NestedLoopCount == 0) 4385 return StmtError(); 4386 4387 assert((CurContext->isDependentContext() || B.builtAll()) && 4388 "omp for loop exprs were not built"); 4389 4390 if (!CurContext->isDependentContext()) { 4391 // Finalize the clauses that need pre-built expressions for CodeGen. 4392 for (auto C : Clauses) { 4393 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 4394 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 4395 B.NumIterations, *this, CurScope, 4396 DSAStack)) 4397 return StmtError(); 4398 } 4399 } 4400 4401 getCurFunction()->setHasBranchProtectedScope(); 4402 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 4403 Clauses, AStmt, B, DSAStack->isCancelRegion()); 4404 } 4405 4406 StmtResult Sema::ActOnOpenMPForSimdDirective( 4407 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 4408 SourceLocation EndLoc, 4409 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 4410 if (!AStmt) 4411 return StmtError(); 4412 4413 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4414 OMPLoopDirective::HelperExprs B; 4415 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 4416 // define the nested loops number. 4417 unsigned NestedLoopCount = 4418 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 4419 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 4420 VarsWithImplicitDSA, B); 4421 if (NestedLoopCount == 0) 4422 return StmtError(); 4423 4424 assert((CurContext->isDependentContext() || B.builtAll()) && 4425 "omp for simd loop exprs were not built"); 4426 4427 if (!CurContext->isDependentContext()) { 4428 // Finalize the clauses that need pre-built expressions for CodeGen. 4429 for (auto C : Clauses) { 4430 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 4431 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 4432 B.NumIterations, *this, CurScope, 4433 DSAStack)) 4434 return StmtError(); 4435 } 4436 } 4437 4438 if (checkSimdlenSafelenSpecified(*this, Clauses)) 4439 return StmtError(); 4440 4441 getCurFunction()->setHasBranchProtectedScope(); 4442 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 4443 Clauses, AStmt, B); 4444 } 4445 4446 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 4447 Stmt *AStmt, 4448 SourceLocation StartLoc, 4449 SourceLocation EndLoc) { 4450 if (!AStmt) 4451 return StmtError(); 4452 4453 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4454 auto BaseStmt = AStmt; 4455 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 4456 BaseStmt = CS->getCapturedStmt(); 4457 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 4458 auto S = C->children(); 4459 if (S.begin() == S.end()) 4460 return StmtError(); 4461 // All associated statements must be '#pragma omp section' except for 4462 // the first one. 4463 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 4464 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 4465 if (SectionStmt) 4466 Diag(SectionStmt->getLocStart(), 4467 diag::err_omp_sections_substmt_not_section); 4468 return StmtError(); 4469 } 4470 cast<OMPSectionDirective>(SectionStmt) 4471 ->setHasCancel(DSAStack->isCancelRegion()); 4472 } 4473 } else { 4474 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt); 4475 return StmtError(); 4476 } 4477 4478 getCurFunction()->setHasBranchProtectedScope(); 4479 4480 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 4481 DSAStack->isCancelRegion()); 4482 } 4483 4484 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 4485 SourceLocation StartLoc, 4486 SourceLocation EndLoc) { 4487 if (!AStmt) 4488 return StmtError(); 4489 4490 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4491 4492 getCurFunction()->setHasBranchProtectedScope(); 4493 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 4494 4495 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 4496 DSAStack->isCancelRegion()); 4497 } 4498 4499 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 4500 Stmt *AStmt, 4501 SourceLocation StartLoc, 4502 SourceLocation EndLoc) { 4503 if (!AStmt) 4504 return StmtError(); 4505 4506 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4507 4508 getCurFunction()->setHasBranchProtectedScope(); 4509 4510 // OpenMP [2.7.3, single Construct, Restrictions] 4511 // The copyprivate clause must not be used with the nowait clause. 4512 OMPClause *Nowait = nullptr; 4513 OMPClause *Copyprivate = nullptr; 4514 for (auto *Clause : Clauses) { 4515 if (Clause->getClauseKind() == OMPC_nowait) 4516 Nowait = Clause; 4517 else if (Clause->getClauseKind() == OMPC_copyprivate) 4518 Copyprivate = Clause; 4519 if (Copyprivate && Nowait) { 4520 Diag(Copyprivate->getLocStart(), 4521 diag::err_omp_single_copyprivate_with_nowait); 4522 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here); 4523 return StmtError(); 4524 } 4525 } 4526 4527 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 4528 } 4529 4530 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 4531 SourceLocation StartLoc, 4532 SourceLocation EndLoc) { 4533 if (!AStmt) 4534 return StmtError(); 4535 4536 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4537 4538 getCurFunction()->setHasBranchProtectedScope(); 4539 4540 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 4541 } 4542 4543 StmtResult Sema::ActOnOpenMPCriticalDirective( 4544 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 4545 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 4546 if (!AStmt) 4547 return StmtError(); 4548 4549 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4550 4551 bool ErrorFound = false; 4552 llvm::APSInt Hint; 4553 SourceLocation HintLoc; 4554 bool DependentHint = false; 4555 for (auto *C : Clauses) { 4556 if (C->getClauseKind() == OMPC_hint) { 4557 if (!DirName.getName()) { 4558 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name); 4559 ErrorFound = true; 4560 } 4561 Expr *E = cast<OMPHintClause>(C)->getHint(); 4562 if (E->isTypeDependent() || E->isValueDependent() || 4563 E->isInstantiationDependent()) 4564 DependentHint = true; 4565 else { 4566 Hint = E->EvaluateKnownConstInt(Context); 4567 HintLoc = C->getLocStart(); 4568 } 4569 } 4570 } 4571 if (ErrorFound) 4572 return StmtError(); 4573 auto Pair = DSAStack->getCriticalWithHint(DirName); 4574 if (Pair.first && DirName.getName() && !DependentHint) { 4575 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 4576 Diag(StartLoc, diag::err_omp_critical_with_hint); 4577 if (HintLoc.isValid()) { 4578 Diag(HintLoc, diag::note_omp_critical_hint_here) 4579 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); 4580 } else 4581 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 4582 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 4583 Diag(C->getLocStart(), diag::note_omp_critical_hint_here) 4584 << 1 4585 << C->getHint()->EvaluateKnownConstInt(Context).toString( 4586 /*Radix=*/10, /*Signed=*/false); 4587 } else 4588 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1; 4589 } 4590 } 4591 4592 getCurFunction()->setHasBranchProtectedScope(); 4593 4594 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 4595 Clauses, AStmt); 4596 if (!Pair.first && DirName.getName() && !DependentHint) 4597 DSAStack->addCriticalWithHint(Dir, Hint); 4598 return Dir; 4599 } 4600 4601 StmtResult Sema::ActOnOpenMPParallelForDirective( 4602 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 4603 SourceLocation EndLoc, 4604 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 4605 if (!AStmt) 4606 return StmtError(); 4607 4608 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 4609 // 1.2.2 OpenMP Language Terminology 4610 // Structured block - An executable statement with a single entry at the 4611 // top and a single exit at the bottom. 4612 // The point of exit cannot be a branch out of the structured block. 4613 // longjmp() and throw() must not violate the entry/exit criteria. 4614 CS->getCapturedDecl()->setNothrow(); 4615 4616 OMPLoopDirective::HelperExprs B; 4617 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 4618 // define the nested loops number. 4619 unsigned NestedLoopCount = 4620 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 4621 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 4622 VarsWithImplicitDSA, B); 4623 if (NestedLoopCount == 0) 4624 return StmtError(); 4625 4626 assert((CurContext->isDependentContext() || B.builtAll()) && 4627 "omp parallel for loop exprs were not built"); 4628 4629 if (!CurContext->isDependentContext()) { 4630 // Finalize the clauses that need pre-built expressions for CodeGen. 4631 for (auto C : Clauses) { 4632 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 4633 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 4634 B.NumIterations, *this, CurScope, 4635 DSAStack)) 4636 return StmtError(); 4637 } 4638 } 4639 4640 getCurFunction()->setHasBranchProtectedScope(); 4641 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc, 4642 NestedLoopCount, Clauses, AStmt, B, 4643 DSAStack->isCancelRegion()); 4644 } 4645 4646 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 4647 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 4648 SourceLocation EndLoc, 4649 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 4650 if (!AStmt) 4651 return StmtError(); 4652 4653 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 4654 // 1.2.2 OpenMP Language Terminology 4655 // Structured block - An executable statement with a single entry at the 4656 // top and a single exit at the bottom. 4657 // The point of exit cannot be a branch out of the structured block. 4658 // longjmp() and throw() must not violate the entry/exit criteria. 4659 CS->getCapturedDecl()->setNothrow(); 4660 4661 OMPLoopDirective::HelperExprs B; 4662 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 4663 // define the nested loops number. 4664 unsigned NestedLoopCount = 4665 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 4666 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 4667 VarsWithImplicitDSA, B); 4668 if (NestedLoopCount == 0) 4669 return StmtError(); 4670 4671 if (!CurContext->isDependentContext()) { 4672 // Finalize the clauses that need pre-built expressions for CodeGen. 4673 for (auto C : Clauses) { 4674 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 4675 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 4676 B.NumIterations, *this, CurScope, 4677 DSAStack)) 4678 return StmtError(); 4679 } 4680 } 4681 4682 if (checkSimdlenSafelenSpecified(*this, Clauses)) 4683 return StmtError(); 4684 4685 getCurFunction()->setHasBranchProtectedScope(); 4686 return OMPParallelForSimdDirective::Create( 4687 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 4688 } 4689 4690 StmtResult 4691 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 4692 Stmt *AStmt, SourceLocation StartLoc, 4693 SourceLocation EndLoc) { 4694 if (!AStmt) 4695 return StmtError(); 4696 4697 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4698 auto BaseStmt = AStmt; 4699 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 4700 BaseStmt = CS->getCapturedStmt(); 4701 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 4702 auto S = C->children(); 4703 if (S.begin() == S.end()) 4704 return StmtError(); 4705 // All associated statements must be '#pragma omp section' except for 4706 // the first one. 4707 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 4708 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 4709 if (SectionStmt) 4710 Diag(SectionStmt->getLocStart(), 4711 diag::err_omp_parallel_sections_substmt_not_section); 4712 return StmtError(); 4713 } 4714 cast<OMPSectionDirective>(SectionStmt) 4715 ->setHasCancel(DSAStack->isCancelRegion()); 4716 } 4717 } else { 4718 Diag(AStmt->getLocStart(), 4719 diag::err_omp_parallel_sections_not_compound_stmt); 4720 return StmtError(); 4721 } 4722 4723 getCurFunction()->setHasBranchProtectedScope(); 4724 4725 return OMPParallelSectionsDirective::Create( 4726 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion()); 4727 } 4728 4729 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 4730 Stmt *AStmt, SourceLocation StartLoc, 4731 SourceLocation EndLoc) { 4732 if (!AStmt) 4733 return StmtError(); 4734 4735 auto *CS = cast<CapturedStmt>(AStmt); 4736 // 1.2.2 OpenMP Language Terminology 4737 // Structured block - An executable statement with a single entry at the 4738 // top and a single exit at the bottom. 4739 // The point of exit cannot be a branch out of the structured block. 4740 // longjmp() and throw() must not violate the entry/exit criteria. 4741 CS->getCapturedDecl()->setNothrow(); 4742 4743 getCurFunction()->setHasBranchProtectedScope(); 4744 4745 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 4746 DSAStack->isCancelRegion()); 4747 } 4748 4749 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 4750 SourceLocation EndLoc) { 4751 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 4752 } 4753 4754 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 4755 SourceLocation EndLoc) { 4756 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 4757 } 4758 4759 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 4760 SourceLocation EndLoc) { 4761 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 4762 } 4763 4764 StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt, 4765 SourceLocation StartLoc, 4766 SourceLocation EndLoc) { 4767 if (!AStmt) 4768 return StmtError(); 4769 4770 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4771 4772 getCurFunction()->setHasBranchProtectedScope(); 4773 4774 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt); 4775 } 4776 4777 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 4778 SourceLocation StartLoc, 4779 SourceLocation EndLoc) { 4780 assert(Clauses.size() <= 1 && "Extra clauses in flush directive"); 4781 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 4782 } 4783 4784 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 4785 Stmt *AStmt, 4786 SourceLocation StartLoc, 4787 SourceLocation EndLoc) { 4788 OMPClause *DependFound = nullptr; 4789 OMPClause *DependSourceClause = nullptr; 4790 OMPClause *DependSinkClause = nullptr; 4791 bool ErrorFound = false; 4792 OMPThreadsClause *TC = nullptr; 4793 OMPSIMDClause *SC = nullptr; 4794 for (auto *C : Clauses) { 4795 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 4796 DependFound = C; 4797 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 4798 if (DependSourceClause) { 4799 Diag(C->getLocStart(), diag::err_omp_more_one_clause) 4800 << getOpenMPDirectiveName(OMPD_ordered) 4801 << getOpenMPClauseName(OMPC_depend) << 2; 4802 ErrorFound = true; 4803 } else 4804 DependSourceClause = C; 4805 if (DependSinkClause) { 4806 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed) 4807 << 0; 4808 ErrorFound = true; 4809 } 4810 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 4811 if (DependSourceClause) { 4812 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed) 4813 << 1; 4814 ErrorFound = true; 4815 } 4816 DependSinkClause = C; 4817 } 4818 } else if (C->getClauseKind() == OMPC_threads) 4819 TC = cast<OMPThreadsClause>(C); 4820 else if (C->getClauseKind() == OMPC_simd) 4821 SC = cast<OMPSIMDClause>(C); 4822 } 4823 if (!ErrorFound && !SC && 4824 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 4825 // OpenMP [2.8.1,simd Construct, Restrictions] 4826 // An ordered construct with the simd clause is the only OpenMP construct 4827 // that can appear in the simd region. 4828 Diag(StartLoc, diag::err_omp_prohibited_region_simd); 4829 ErrorFound = true; 4830 } else if (DependFound && (TC || SC)) { 4831 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd) 4832 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 4833 ErrorFound = true; 4834 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) { 4835 Diag(DependFound->getLocStart(), 4836 diag::err_omp_ordered_directive_without_param); 4837 ErrorFound = true; 4838 } else if (TC || Clauses.empty()) { 4839 if (auto *Param = DSAStack->getParentOrderedRegionParam()) { 4840 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc; 4841 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 4842 << (TC != nullptr); 4843 Diag(Param->getLocStart(), diag::note_omp_ordered_param); 4844 ErrorFound = true; 4845 } 4846 } 4847 if ((!AStmt && !DependFound) || ErrorFound) 4848 return StmtError(); 4849 4850 if (AStmt) { 4851 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4852 4853 getCurFunction()->setHasBranchProtectedScope(); 4854 } 4855 4856 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 4857 } 4858 4859 namespace { 4860 /// \brief Helper class for checking expression in 'omp atomic [update]' 4861 /// construct. 4862 class OpenMPAtomicUpdateChecker { 4863 /// \brief Error results for atomic update expressions. 4864 enum ExprAnalysisErrorCode { 4865 /// \brief A statement is not an expression statement. 4866 NotAnExpression, 4867 /// \brief Expression is not builtin binary or unary operation. 4868 NotABinaryOrUnaryExpression, 4869 /// \brief Unary operation is not post-/pre- increment/decrement operation. 4870 NotAnUnaryIncDecExpression, 4871 /// \brief An expression is not of scalar type. 4872 NotAScalarType, 4873 /// \brief A binary operation is not an assignment operation. 4874 NotAnAssignmentOp, 4875 /// \brief RHS part of the binary operation is not a binary expression. 4876 NotABinaryExpression, 4877 /// \brief RHS part is not additive/multiplicative/shift/biwise binary 4878 /// expression. 4879 NotABinaryOperator, 4880 /// \brief RHS binary operation does not have reference to the updated LHS 4881 /// part. 4882 NotAnUpdateExpression, 4883 /// \brief No errors is found. 4884 NoError 4885 }; 4886 /// \brief Reference to Sema. 4887 Sema &SemaRef; 4888 /// \brief A location for note diagnostics (when error is found). 4889 SourceLocation NoteLoc; 4890 /// \brief 'x' lvalue part of the source atomic expression. 4891 Expr *X; 4892 /// \brief 'expr' rvalue part of the source atomic expression. 4893 Expr *E; 4894 /// \brief Helper expression of the form 4895 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 4896 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 4897 Expr *UpdateExpr; 4898 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is 4899 /// important for non-associative operations. 4900 bool IsXLHSInRHSPart; 4901 BinaryOperatorKind Op; 4902 SourceLocation OpLoc; 4903 /// \brief true if the source expression is a postfix unary operation, false 4904 /// if it is a prefix unary operation. 4905 bool IsPostfixUpdate; 4906 4907 public: 4908 OpenMPAtomicUpdateChecker(Sema &SemaRef) 4909 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 4910 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 4911 /// \brief Check specified statement that it is suitable for 'atomic update' 4912 /// constructs and extract 'x', 'expr' and Operation from the original 4913 /// expression. If DiagId and NoteId == 0, then only check is performed 4914 /// without error notification. 4915 /// \param DiagId Diagnostic which should be emitted if error is found. 4916 /// \param NoteId Diagnostic note for the main error message. 4917 /// \return true if statement is not an update expression, false otherwise. 4918 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 4919 /// \brief Return the 'x' lvalue part of the source atomic expression. 4920 Expr *getX() const { return X; } 4921 /// \brief Return the 'expr' rvalue part of the source atomic expression. 4922 Expr *getExpr() const { return E; } 4923 /// \brief Return the update expression used in calculation of the updated 4924 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 4925 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 4926 Expr *getUpdateExpr() const { return UpdateExpr; } 4927 /// \brief Return true if 'x' is LHS in RHS part of full update expression, 4928 /// false otherwise. 4929 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 4930 4931 /// \brief true if the source expression is a postfix unary operation, false 4932 /// if it is a prefix unary operation. 4933 bool isPostfixUpdate() const { return IsPostfixUpdate; } 4934 4935 private: 4936 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 4937 unsigned NoteId = 0); 4938 }; 4939 } // namespace 4940 4941 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 4942 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 4943 ExprAnalysisErrorCode ErrorFound = NoError; 4944 SourceLocation ErrorLoc, NoteLoc; 4945 SourceRange ErrorRange, NoteRange; 4946 // Allowed constructs are: 4947 // x = x binop expr; 4948 // x = expr binop x; 4949 if (AtomicBinOp->getOpcode() == BO_Assign) { 4950 X = AtomicBinOp->getLHS(); 4951 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 4952 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 4953 if (AtomicInnerBinOp->isMultiplicativeOp() || 4954 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 4955 AtomicInnerBinOp->isBitwiseOp()) { 4956 Op = AtomicInnerBinOp->getOpcode(); 4957 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 4958 auto *LHS = AtomicInnerBinOp->getLHS(); 4959 auto *RHS = AtomicInnerBinOp->getRHS(); 4960 llvm::FoldingSetNodeID XId, LHSId, RHSId; 4961 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 4962 /*Canonical=*/true); 4963 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 4964 /*Canonical=*/true); 4965 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 4966 /*Canonical=*/true); 4967 if (XId == LHSId) { 4968 E = RHS; 4969 IsXLHSInRHSPart = true; 4970 } else if (XId == RHSId) { 4971 E = LHS; 4972 IsXLHSInRHSPart = false; 4973 } else { 4974 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 4975 ErrorRange = AtomicInnerBinOp->getSourceRange(); 4976 NoteLoc = X->getExprLoc(); 4977 NoteRange = X->getSourceRange(); 4978 ErrorFound = NotAnUpdateExpression; 4979 } 4980 } else { 4981 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 4982 ErrorRange = AtomicInnerBinOp->getSourceRange(); 4983 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 4984 NoteRange = SourceRange(NoteLoc, NoteLoc); 4985 ErrorFound = NotABinaryOperator; 4986 } 4987 } else { 4988 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 4989 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 4990 ErrorFound = NotABinaryExpression; 4991 } 4992 } else { 4993 ErrorLoc = AtomicBinOp->getExprLoc(); 4994 ErrorRange = AtomicBinOp->getSourceRange(); 4995 NoteLoc = AtomicBinOp->getOperatorLoc(); 4996 NoteRange = SourceRange(NoteLoc, NoteLoc); 4997 ErrorFound = NotAnAssignmentOp; 4998 } 4999 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 5000 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 5001 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 5002 return true; 5003 } else if (SemaRef.CurContext->isDependentContext()) 5004 E = X = UpdateExpr = nullptr; 5005 return ErrorFound != NoError; 5006 } 5007 5008 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 5009 unsigned NoteId) { 5010 ExprAnalysisErrorCode ErrorFound = NoError; 5011 SourceLocation ErrorLoc, NoteLoc; 5012 SourceRange ErrorRange, NoteRange; 5013 // Allowed constructs are: 5014 // x++; 5015 // x--; 5016 // ++x; 5017 // --x; 5018 // x binop= expr; 5019 // x = x binop expr; 5020 // x = expr binop x; 5021 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 5022 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 5023 if (AtomicBody->getType()->isScalarType() || 5024 AtomicBody->isInstantiationDependent()) { 5025 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 5026 AtomicBody->IgnoreParenImpCasts())) { 5027 // Check for Compound Assignment Operation 5028 Op = BinaryOperator::getOpForCompoundAssignment( 5029 AtomicCompAssignOp->getOpcode()); 5030 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 5031 E = AtomicCompAssignOp->getRHS(); 5032 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 5033 IsXLHSInRHSPart = true; 5034 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 5035 AtomicBody->IgnoreParenImpCasts())) { 5036 // Check for Binary Operation 5037 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 5038 return true; 5039 } else if (auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 5040 AtomicBody->IgnoreParenImpCasts())) { 5041 // Check for Unary Operation 5042 if (AtomicUnaryOp->isIncrementDecrementOp()) { 5043 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 5044 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 5045 OpLoc = AtomicUnaryOp->getOperatorLoc(); 5046 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 5047 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 5048 IsXLHSInRHSPart = true; 5049 } else { 5050 ErrorFound = NotAnUnaryIncDecExpression; 5051 ErrorLoc = AtomicUnaryOp->getExprLoc(); 5052 ErrorRange = AtomicUnaryOp->getSourceRange(); 5053 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 5054 NoteRange = SourceRange(NoteLoc, NoteLoc); 5055 } 5056 } else if (!AtomicBody->isInstantiationDependent()) { 5057 ErrorFound = NotABinaryOrUnaryExpression; 5058 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 5059 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 5060 } 5061 } else { 5062 ErrorFound = NotAScalarType; 5063 NoteLoc = ErrorLoc = AtomicBody->getLocStart(); 5064 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 5065 } 5066 } else { 5067 ErrorFound = NotAnExpression; 5068 NoteLoc = ErrorLoc = S->getLocStart(); 5069 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 5070 } 5071 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 5072 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 5073 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 5074 return true; 5075 } else if (SemaRef.CurContext->isDependentContext()) 5076 E = X = UpdateExpr = nullptr; 5077 if (ErrorFound == NoError && E && X) { 5078 // Build an update expression of form 'OpaqueValueExpr(x) binop 5079 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 5080 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 5081 auto *OVEX = new (SemaRef.getASTContext()) 5082 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); 5083 auto *OVEExpr = new (SemaRef.getASTContext()) 5084 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); 5085 auto Update = 5086 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 5087 IsXLHSInRHSPart ? OVEExpr : OVEX); 5088 if (Update.isInvalid()) 5089 return true; 5090 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 5091 Sema::AA_Casting); 5092 if (Update.isInvalid()) 5093 return true; 5094 UpdateExpr = Update.get(); 5095 } 5096 return ErrorFound != NoError; 5097 } 5098 5099 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 5100 Stmt *AStmt, 5101 SourceLocation StartLoc, 5102 SourceLocation EndLoc) { 5103 if (!AStmt) 5104 return StmtError(); 5105 5106 auto *CS = cast<CapturedStmt>(AStmt); 5107 // 1.2.2 OpenMP Language Terminology 5108 // Structured block - An executable statement with a single entry at the 5109 // top and a single exit at the bottom. 5110 // The point of exit cannot be a branch out of the structured block. 5111 // longjmp() and throw() must not violate the entry/exit criteria. 5112 OpenMPClauseKind AtomicKind = OMPC_unknown; 5113 SourceLocation AtomicKindLoc; 5114 for (auto *C : Clauses) { 5115 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 5116 C->getClauseKind() == OMPC_update || 5117 C->getClauseKind() == OMPC_capture) { 5118 if (AtomicKind != OMPC_unknown) { 5119 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses) 5120 << SourceRange(C->getLocStart(), C->getLocEnd()); 5121 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause) 5122 << getOpenMPClauseName(AtomicKind); 5123 } else { 5124 AtomicKind = C->getClauseKind(); 5125 AtomicKindLoc = C->getLocStart(); 5126 } 5127 } 5128 } 5129 5130 auto Body = CS->getCapturedStmt(); 5131 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 5132 Body = EWC->getSubExpr(); 5133 5134 Expr *X = nullptr; 5135 Expr *V = nullptr; 5136 Expr *E = nullptr; 5137 Expr *UE = nullptr; 5138 bool IsXLHSInRHSPart = false; 5139 bool IsPostfixUpdate = false; 5140 // OpenMP [2.12.6, atomic Construct] 5141 // In the next expressions: 5142 // * x and v (as applicable) are both l-value expressions with scalar type. 5143 // * During the execution of an atomic region, multiple syntactic 5144 // occurrences of x must designate the same storage location. 5145 // * Neither of v and expr (as applicable) may access the storage location 5146 // designated by x. 5147 // * Neither of x and expr (as applicable) may access the storage location 5148 // designated by v. 5149 // * expr is an expression with scalar type. 5150 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 5151 // * binop, binop=, ++, and -- are not overloaded operators. 5152 // * The expression x binop expr must be numerically equivalent to x binop 5153 // (expr). This requirement is satisfied if the operators in expr have 5154 // precedence greater than binop, or by using parentheses around expr or 5155 // subexpressions of expr. 5156 // * The expression expr binop x must be numerically equivalent to (expr) 5157 // binop x. This requirement is satisfied if the operators in expr have 5158 // precedence equal to or greater than binop, or by using parentheses around 5159 // expr or subexpressions of expr. 5160 // * For forms that allow multiple occurrences of x, the number of times 5161 // that x is evaluated is unspecified. 5162 if (AtomicKind == OMPC_read) { 5163 enum { 5164 NotAnExpression, 5165 NotAnAssignmentOp, 5166 NotAScalarType, 5167 NotAnLValue, 5168 NoError 5169 } ErrorFound = NoError; 5170 SourceLocation ErrorLoc, NoteLoc; 5171 SourceRange ErrorRange, NoteRange; 5172 // If clause is read: 5173 // v = x; 5174 if (auto *AtomicBody = dyn_cast<Expr>(Body)) { 5175 auto *AtomicBinOp = 5176 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 5177 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 5178 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 5179 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 5180 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 5181 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 5182 if (!X->isLValue() || !V->isLValue()) { 5183 auto NotLValueExpr = X->isLValue() ? V : X; 5184 ErrorFound = NotAnLValue; 5185 ErrorLoc = AtomicBinOp->getExprLoc(); 5186 ErrorRange = AtomicBinOp->getSourceRange(); 5187 NoteLoc = NotLValueExpr->getExprLoc(); 5188 NoteRange = NotLValueExpr->getSourceRange(); 5189 } 5190 } else if (!X->isInstantiationDependent() || 5191 !V->isInstantiationDependent()) { 5192 auto NotScalarExpr = 5193 (X->isInstantiationDependent() || X->getType()->isScalarType()) 5194 ? V 5195 : X; 5196 ErrorFound = NotAScalarType; 5197 ErrorLoc = AtomicBinOp->getExprLoc(); 5198 ErrorRange = AtomicBinOp->getSourceRange(); 5199 NoteLoc = NotScalarExpr->getExprLoc(); 5200 NoteRange = NotScalarExpr->getSourceRange(); 5201 } 5202 } else if (!AtomicBody->isInstantiationDependent()) { 5203 ErrorFound = NotAnAssignmentOp; 5204 ErrorLoc = AtomicBody->getExprLoc(); 5205 ErrorRange = AtomicBody->getSourceRange(); 5206 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 5207 : AtomicBody->getExprLoc(); 5208 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 5209 : AtomicBody->getSourceRange(); 5210 } 5211 } else { 5212 ErrorFound = NotAnExpression; 5213 NoteLoc = ErrorLoc = Body->getLocStart(); 5214 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 5215 } 5216 if (ErrorFound != NoError) { 5217 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 5218 << ErrorRange; 5219 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 5220 << NoteRange; 5221 return StmtError(); 5222 } else if (CurContext->isDependentContext()) 5223 V = X = nullptr; 5224 } else if (AtomicKind == OMPC_write) { 5225 enum { 5226 NotAnExpression, 5227 NotAnAssignmentOp, 5228 NotAScalarType, 5229 NotAnLValue, 5230 NoError 5231 } ErrorFound = NoError; 5232 SourceLocation ErrorLoc, NoteLoc; 5233 SourceRange ErrorRange, NoteRange; 5234 // If clause is write: 5235 // x = expr; 5236 if (auto *AtomicBody = dyn_cast<Expr>(Body)) { 5237 auto *AtomicBinOp = 5238 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 5239 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 5240 X = AtomicBinOp->getLHS(); 5241 E = AtomicBinOp->getRHS(); 5242 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 5243 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 5244 if (!X->isLValue()) { 5245 ErrorFound = NotAnLValue; 5246 ErrorLoc = AtomicBinOp->getExprLoc(); 5247 ErrorRange = AtomicBinOp->getSourceRange(); 5248 NoteLoc = X->getExprLoc(); 5249 NoteRange = X->getSourceRange(); 5250 } 5251 } else if (!X->isInstantiationDependent() || 5252 !E->isInstantiationDependent()) { 5253 auto NotScalarExpr = 5254 (X->isInstantiationDependent() || X->getType()->isScalarType()) 5255 ? E 5256 : X; 5257 ErrorFound = NotAScalarType; 5258 ErrorLoc = AtomicBinOp->getExprLoc(); 5259 ErrorRange = AtomicBinOp->getSourceRange(); 5260 NoteLoc = NotScalarExpr->getExprLoc(); 5261 NoteRange = NotScalarExpr->getSourceRange(); 5262 } 5263 } else if (!AtomicBody->isInstantiationDependent()) { 5264 ErrorFound = NotAnAssignmentOp; 5265 ErrorLoc = AtomicBody->getExprLoc(); 5266 ErrorRange = AtomicBody->getSourceRange(); 5267 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 5268 : AtomicBody->getExprLoc(); 5269 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 5270 : AtomicBody->getSourceRange(); 5271 } 5272 } else { 5273 ErrorFound = NotAnExpression; 5274 NoteLoc = ErrorLoc = Body->getLocStart(); 5275 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 5276 } 5277 if (ErrorFound != NoError) { 5278 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 5279 << ErrorRange; 5280 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 5281 << NoteRange; 5282 return StmtError(); 5283 } else if (CurContext->isDependentContext()) 5284 E = X = nullptr; 5285 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 5286 // If clause is update: 5287 // x++; 5288 // x--; 5289 // ++x; 5290 // --x; 5291 // x binop= expr; 5292 // x = x binop expr; 5293 // x = expr binop x; 5294 OpenMPAtomicUpdateChecker Checker(*this); 5295 if (Checker.checkStatement( 5296 Body, (AtomicKind == OMPC_update) 5297 ? diag::err_omp_atomic_update_not_expression_statement 5298 : diag::err_omp_atomic_not_expression_statement, 5299 diag::note_omp_atomic_update)) 5300 return StmtError(); 5301 if (!CurContext->isDependentContext()) { 5302 E = Checker.getExpr(); 5303 X = Checker.getX(); 5304 UE = Checker.getUpdateExpr(); 5305 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 5306 } 5307 } else if (AtomicKind == OMPC_capture) { 5308 enum { 5309 NotAnAssignmentOp, 5310 NotACompoundStatement, 5311 NotTwoSubstatements, 5312 NotASpecificExpression, 5313 NoError 5314 } ErrorFound = NoError; 5315 SourceLocation ErrorLoc, NoteLoc; 5316 SourceRange ErrorRange, NoteRange; 5317 if (auto *AtomicBody = dyn_cast<Expr>(Body)) { 5318 // If clause is a capture: 5319 // v = x++; 5320 // v = x--; 5321 // v = ++x; 5322 // v = --x; 5323 // v = x binop= expr; 5324 // v = x = x binop expr; 5325 // v = x = expr binop x; 5326 auto *AtomicBinOp = 5327 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 5328 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 5329 V = AtomicBinOp->getLHS(); 5330 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 5331 OpenMPAtomicUpdateChecker Checker(*this); 5332 if (Checker.checkStatement( 5333 Body, diag::err_omp_atomic_capture_not_expression_statement, 5334 diag::note_omp_atomic_update)) 5335 return StmtError(); 5336 E = Checker.getExpr(); 5337 X = Checker.getX(); 5338 UE = Checker.getUpdateExpr(); 5339 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 5340 IsPostfixUpdate = Checker.isPostfixUpdate(); 5341 } else if (!AtomicBody->isInstantiationDependent()) { 5342 ErrorLoc = AtomicBody->getExprLoc(); 5343 ErrorRange = AtomicBody->getSourceRange(); 5344 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 5345 : AtomicBody->getExprLoc(); 5346 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 5347 : AtomicBody->getSourceRange(); 5348 ErrorFound = NotAnAssignmentOp; 5349 } 5350 if (ErrorFound != NoError) { 5351 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 5352 << ErrorRange; 5353 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 5354 return StmtError(); 5355 } else if (CurContext->isDependentContext()) { 5356 UE = V = E = X = nullptr; 5357 } 5358 } else { 5359 // If clause is a capture: 5360 // { v = x; x = expr; } 5361 // { v = x; x++; } 5362 // { v = x; x--; } 5363 // { v = x; ++x; } 5364 // { v = x; --x; } 5365 // { v = x; x binop= expr; } 5366 // { v = x; x = x binop expr; } 5367 // { v = x; x = expr binop x; } 5368 // { x++; v = x; } 5369 // { x--; v = x; } 5370 // { ++x; v = x; } 5371 // { --x; v = x; } 5372 // { x binop= expr; v = x; } 5373 // { x = x binop expr; v = x; } 5374 // { x = expr binop x; v = x; } 5375 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 5376 // Check that this is { expr1; expr2; } 5377 if (CS->size() == 2) { 5378 auto *First = CS->body_front(); 5379 auto *Second = CS->body_back(); 5380 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 5381 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 5382 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 5383 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 5384 // Need to find what subexpression is 'v' and what is 'x'. 5385 OpenMPAtomicUpdateChecker Checker(*this); 5386 bool IsUpdateExprFound = !Checker.checkStatement(Second); 5387 BinaryOperator *BinOp = nullptr; 5388 if (IsUpdateExprFound) { 5389 BinOp = dyn_cast<BinaryOperator>(First); 5390 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 5391 } 5392 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 5393 // { v = x; x++; } 5394 // { v = x; x--; } 5395 // { v = x; ++x; } 5396 // { v = x; --x; } 5397 // { v = x; x binop= expr; } 5398 // { v = x; x = x binop expr; } 5399 // { v = x; x = expr binop x; } 5400 // Check that the first expression has form v = x. 5401 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 5402 llvm::FoldingSetNodeID XId, PossibleXId; 5403 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 5404 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 5405 IsUpdateExprFound = XId == PossibleXId; 5406 if (IsUpdateExprFound) { 5407 V = BinOp->getLHS(); 5408 X = Checker.getX(); 5409 E = Checker.getExpr(); 5410 UE = Checker.getUpdateExpr(); 5411 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 5412 IsPostfixUpdate = true; 5413 } 5414 } 5415 if (!IsUpdateExprFound) { 5416 IsUpdateExprFound = !Checker.checkStatement(First); 5417 BinOp = nullptr; 5418 if (IsUpdateExprFound) { 5419 BinOp = dyn_cast<BinaryOperator>(Second); 5420 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 5421 } 5422 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 5423 // { x++; v = x; } 5424 // { x--; v = x; } 5425 // { ++x; v = x; } 5426 // { --x; v = x; } 5427 // { x binop= expr; v = x; } 5428 // { x = x binop expr; v = x; } 5429 // { x = expr binop x; v = x; } 5430 // Check that the second expression has form v = x. 5431 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 5432 llvm::FoldingSetNodeID XId, PossibleXId; 5433 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 5434 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 5435 IsUpdateExprFound = XId == PossibleXId; 5436 if (IsUpdateExprFound) { 5437 V = BinOp->getLHS(); 5438 X = Checker.getX(); 5439 E = Checker.getExpr(); 5440 UE = Checker.getUpdateExpr(); 5441 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 5442 IsPostfixUpdate = false; 5443 } 5444 } 5445 } 5446 if (!IsUpdateExprFound) { 5447 // { v = x; x = expr; } 5448 auto *FirstExpr = dyn_cast<Expr>(First); 5449 auto *SecondExpr = dyn_cast<Expr>(Second); 5450 if (!FirstExpr || !SecondExpr || 5451 !(FirstExpr->isInstantiationDependent() || 5452 SecondExpr->isInstantiationDependent())) { 5453 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 5454 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 5455 ErrorFound = NotAnAssignmentOp; 5456 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 5457 : First->getLocStart(); 5458 NoteRange = ErrorRange = FirstBinOp 5459 ? FirstBinOp->getSourceRange() 5460 : SourceRange(ErrorLoc, ErrorLoc); 5461 } else { 5462 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 5463 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 5464 ErrorFound = NotAnAssignmentOp; 5465 NoteLoc = ErrorLoc = SecondBinOp 5466 ? SecondBinOp->getOperatorLoc() 5467 : Second->getLocStart(); 5468 NoteRange = ErrorRange = 5469 SecondBinOp ? SecondBinOp->getSourceRange() 5470 : SourceRange(ErrorLoc, ErrorLoc); 5471 } else { 5472 auto *PossibleXRHSInFirst = 5473 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 5474 auto *PossibleXLHSInSecond = 5475 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 5476 llvm::FoldingSetNodeID X1Id, X2Id; 5477 PossibleXRHSInFirst->Profile(X1Id, Context, 5478 /*Canonical=*/true); 5479 PossibleXLHSInSecond->Profile(X2Id, Context, 5480 /*Canonical=*/true); 5481 IsUpdateExprFound = X1Id == X2Id; 5482 if (IsUpdateExprFound) { 5483 V = FirstBinOp->getLHS(); 5484 X = SecondBinOp->getLHS(); 5485 E = SecondBinOp->getRHS(); 5486 UE = nullptr; 5487 IsXLHSInRHSPart = false; 5488 IsPostfixUpdate = true; 5489 } else { 5490 ErrorFound = NotASpecificExpression; 5491 ErrorLoc = FirstBinOp->getExprLoc(); 5492 ErrorRange = FirstBinOp->getSourceRange(); 5493 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 5494 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 5495 } 5496 } 5497 } 5498 } 5499 } 5500 } else { 5501 NoteLoc = ErrorLoc = Body->getLocStart(); 5502 NoteRange = ErrorRange = 5503 SourceRange(Body->getLocStart(), Body->getLocStart()); 5504 ErrorFound = NotTwoSubstatements; 5505 } 5506 } else { 5507 NoteLoc = ErrorLoc = Body->getLocStart(); 5508 NoteRange = ErrorRange = 5509 SourceRange(Body->getLocStart(), Body->getLocStart()); 5510 ErrorFound = NotACompoundStatement; 5511 } 5512 if (ErrorFound != NoError) { 5513 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 5514 << ErrorRange; 5515 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 5516 return StmtError(); 5517 } else if (CurContext->isDependentContext()) { 5518 UE = V = E = X = nullptr; 5519 } 5520 } 5521 } 5522 5523 getCurFunction()->setHasBranchProtectedScope(); 5524 5525 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 5526 X, V, E, UE, IsXLHSInRHSPart, 5527 IsPostfixUpdate); 5528 } 5529 5530 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 5531 Stmt *AStmt, 5532 SourceLocation StartLoc, 5533 SourceLocation EndLoc) { 5534 if (!AStmt) 5535 return StmtError(); 5536 5537 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 5538 // 1.2.2 OpenMP Language Terminology 5539 // Structured block - An executable statement with a single entry at the 5540 // top and a single exit at the bottom. 5541 // The point of exit cannot be a branch out of the structured block. 5542 // longjmp() and throw() must not violate the entry/exit criteria. 5543 CS->getCapturedDecl()->setNothrow(); 5544 5545 // OpenMP [2.16, Nesting of Regions] 5546 // If specified, a teams construct must be contained within a target 5547 // construct. That target construct must contain no statements or directives 5548 // outside of the teams construct. 5549 if (DSAStack->hasInnerTeamsRegion()) { 5550 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true); 5551 bool OMPTeamsFound = true; 5552 if (auto *CS = dyn_cast<CompoundStmt>(S)) { 5553 auto I = CS->body_begin(); 5554 while (I != CS->body_end()) { 5555 auto *OED = dyn_cast<OMPExecutableDirective>(*I); 5556 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) { 5557 OMPTeamsFound = false; 5558 break; 5559 } 5560 ++I; 5561 } 5562 assert(I != CS->body_end() && "Not found statement"); 5563 S = *I; 5564 } else { 5565 auto *OED = dyn_cast<OMPExecutableDirective>(S); 5566 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 5567 } 5568 if (!OMPTeamsFound) { 5569 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 5570 Diag(DSAStack->getInnerTeamsRegionLoc(), 5571 diag::note_omp_nested_teams_construct_here); 5572 Diag(S->getLocStart(), diag::note_omp_nested_statement_here) 5573 << isa<OMPExecutableDirective>(S); 5574 return StmtError(); 5575 } 5576 } 5577 5578 getCurFunction()->setHasBranchProtectedScope(); 5579 5580 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 5581 } 5582 5583 StmtResult 5584 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 5585 Stmt *AStmt, SourceLocation StartLoc, 5586 SourceLocation EndLoc) { 5587 if (!AStmt) 5588 return StmtError(); 5589 5590 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 5591 // 1.2.2 OpenMP Language Terminology 5592 // Structured block - An executable statement with a single entry at the 5593 // top and a single exit at the bottom. 5594 // The point of exit cannot be a branch out of the structured block. 5595 // longjmp() and throw() must not violate the entry/exit criteria. 5596 CS->getCapturedDecl()->setNothrow(); 5597 5598 getCurFunction()->setHasBranchProtectedScope(); 5599 5600 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, 5601 AStmt); 5602 } 5603 5604 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 5605 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5606 SourceLocation EndLoc, 5607 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 5608 if (!AStmt) 5609 return StmtError(); 5610 5611 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 5612 // 1.2.2 OpenMP Language Terminology 5613 // Structured block - An executable statement with a single entry at the 5614 // top and a single exit at the bottom. 5615 // The point of exit cannot be a branch out of the structured block. 5616 // longjmp() and throw() must not violate the entry/exit criteria. 5617 CS->getCapturedDecl()->setNothrow(); 5618 5619 OMPLoopDirective::HelperExprs B; 5620 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5621 // define the nested loops number. 5622 unsigned NestedLoopCount = 5623 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 5624 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 5625 VarsWithImplicitDSA, B); 5626 if (NestedLoopCount == 0) 5627 return StmtError(); 5628 5629 assert((CurContext->isDependentContext() || B.builtAll()) && 5630 "omp target parallel for loop exprs were not built"); 5631 5632 if (!CurContext->isDependentContext()) { 5633 // Finalize the clauses that need pre-built expressions for CodeGen. 5634 for (auto C : Clauses) { 5635 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5636 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5637 B.NumIterations, *this, CurScope, 5638 DSAStack)) 5639 return StmtError(); 5640 } 5641 } 5642 5643 getCurFunction()->setHasBranchProtectedScope(); 5644 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc, 5645 NestedLoopCount, Clauses, AStmt, 5646 B, DSAStack->isCancelRegion()); 5647 } 5648 5649 /// \brief Check for existence of a map clause in the list of clauses. 5650 static bool HasMapClause(ArrayRef<OMPClause *> Clauses) { 5651 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end(); 5652 I != E; ++I) { 5653 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) { 5654 return true; 5655 } 5656 } 5657 5658 return false; 5659 } 5660 5661 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 5662 Stmt *AStmt, 5663 SourceLocation StartLoc, 5664 SourceLocation EndLoc) { 5665 if (!AStmt) 5666 return StmtError(); 5667 5668 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5669 5670 // OpenMP [2.10.1, Restrictions, p. 97] 5671 // At least one map clause must appear on the directive. 5672 if (!HasMapClause(Clauses)) { 5673 Diag(StartLoc, diag::err_omp_no_map_for_directive) 5674 << getOpenMPDirectiveName(OMPD_target_data); 5675 return StmtError(); 5676 } 5677 5678 getCurFunction()->setHasBranchProtectedScope(); 5679 5680 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 5681 AStmt); 5682 } 5683 5684 StmtResult 5685 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 5686 SourceLocation StartLoc, 5687 SourceLocation EndLoc) { 5688 // OpenMP [2.10.2, Restrictions, p. 99] 5689 // At least one map clause must appear on the directive. 5690 if (!HasMapClause(Clauses)) { 5691 Diag(StartLoc, diag::err_omp_no_map_for_directive) 5692 << getOpenMPDirectiveName(OMPD_target_enter_data); 5693 return StmtError(); 5694 } 5695 5696 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, 5697 Clauses); 5698 } 5699 5700 StmtResult 5701 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 5702 SourceLocation StartLoc, 5703 SourceLocation EndLoc) { 5704 // OpenMP [2.10.3, Restrictions, p. 102] 5705 // At least one map clause must appear on the directive. 5706 if (!HasMapClause(Clauses)) { 5707 Diag(StartLoc, diag::err_omp_no_map_for_directive) 5708 << getOpenMPDirectiveName(OMPD_target_exit_data); 5709 return StmtError(); 5710 } 5711 5712 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses); 5713 } 5714 5715 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 5716 SourceLocation StartLoc, 5717 SourceLocation EndLoc) { 5718 bool seenMotionClause = false; 5719 for (auto *C : Clauses) { 5720 if (C->getClauseKind() == OMPC_to || C->getClauseKind() == OMPC_from) 5721 seenMotionClause = true; 5722 } 5723 if (!seenMotionClause) { 5724 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 5725 return StmtError(); 5726 } 5727 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses); 5728 } 5729 5730 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 5731 Stmt *AStmt, SourceLocation StartLoc, 5732 SourceLocation EndLoc) { 5733 if (!AStmt) 5734 return StmtError(); 5735 5736 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 5737 // 1.2.2 OpenMP Language Terminology 5738 // Structured block - An executable statement with a single entry at the 5739 // top and a single exit at the bottom. 5740 // The point of exit cannot be a branch out of the structured block. 5741 // longjmp() and throw() must not violate the entry/exit criteria. 5742 CS->getCapturedDecl()->setNothrow(); 5743 5744 getCurFunction()->setHasBranchProtectedScope(); 5745 5746 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 5747 } 5748 5749 StmtResult 5750 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 5751 SourceLocation EndLoc, 5752 OpenMPDirectiveKind CancelRegion) { 5753 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for && 5754 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) { 5755 Diag(StartLoc, diag::err_omp_wrong_cancel_region) 5756 << getOpenMPDirectiveName(CancelRegion); 5757 return StmtError(); 5758 } 5759 if (DSAStack->isParentNowaitRegion()) { 5760 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 5761 return StmtError(); 5762 } 5763 if (DSAStack->isParentOrderedRegion()) { 5764 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 5765 return StmtError(); 5766 } 5767 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 5768 CancelRegion); 5769 } 5770 5771 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 5772 SourceLocation StartLoc, 5773 SourceLocation EndLoc, 5774 OpenMPDirectiveKind CancelRegion) { 5775 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for && 5776 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) { 5777 Diag(StartLoc, diag::err_omp_wrong_cancel_region) 5778 << getOpenMPDirectiveName(CancelRegion); 5779 return StmtError(); 5780 } 5781 if (DSAStack->isParentNowaitRegion()) { 5782 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 5783 return StmtError(); 5784 } 5785 if (DSAStack->isParentOrderedRegion()) { 5786 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 5787 return StmtError(); 5788 } 5789 DSAStack->setParentCancelRegion(/*Cancel=*/true); 5790 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 5791 CancelRegion); 5792 } 5793 5794 static bool checkGrainsizeNumTasksClauses(Sema &S, 5795 ArrayRef<OMPClause *> Clauses) { 5796 OMPClause *PrevClause = nullptr; 5797 bool ErrorFound = false; 5798 for (auto *C : Clauses) { 5799 if (C->getClauseKind() == OMPC_grainsize || 5800 C->getClauseKind() == OMPC_num_tasks) { 5801 if (!PrevClause) 5802 PrevClause = C; 5803 else if (PrevClause->getClauseKind() != C->getClauseKind()) { 5804 S.Diag(C->getLocStart(), 5805 diag::err_omp_grainsize_num_tasks_mutually_exclusive) 5806 << getOpenMPClauseName(C->getClauseKind()) 5807 << getOpenMPClauseName(PrevClause->getClauseKind()); 5808 S.Diag(PrevClause->getLocStart(), 5809 diag::note_omp_previous_grainsize_num_tasks) 5810 << getOpenMPClauseName(PrevClause->getClauseKind()); 5811 ErrorFound = true; 5812 } 5813 } 5814 } 5815 return ErrorFound; 5816 } 5817 5818 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 5819 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5820 SourceLocation EndLoc, 5821 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 5822 if (!AStmt) 5823 return StmtError(); 5824 5825 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5826 OMPLoopDirective::HelperExprs B; 5827 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5828 // define the nested loops number. 5829 unsigned NestedLoopCount = 5830 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 5831 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 5832 VarsWithImplicitDSA, B); 5833 if (NestedLoopCount == 0) 5834 return StmtError(); 5835 5836 assert((CurContext->isDependentContext() || B.builtAll()) && 5837 "omp for loop exprs were not built"); 5838 5839 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 5840 // The grainsize clause and num_tasks clause are mutually exclusive and may 5841 // not appear on the same taskloop directive. 5842 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 5843 return StmtError(); 5844 5845 getCurFunction()->setHasBranchProtectedScope(); 5846 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 5847 NestedLoopCount, Clauses, AStmt, B); 5848 } 5849 5850 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 5851 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5852 SourceLocation EndLoc, 5853 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 5854 if (!AStmt) 5855 return StmtError(); 5856 5857 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5858 OMPLoopDirective::HelperExprs B; 5859 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5860 // define the nested loops number. 5861 unsigned NestedLoopCount = 5862 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 5863 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 5864 VarsWithImplicitDSA, B); 5865 if (NestedLoopCount == 0) 5866 return StmtError(); 5867 5868 assert((CurContext->isDependentContext() || B.builtAll()) && 5869 "omp for loop exprs were not built"); 5870 5871 if (!CurContext->isDependentContext()) { 5872 // Finalize the clauses that need pre-built expressions for CodeGen. 5873 for (auto C : Clauses) { 5874 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5875 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5876 B.NumIterations, *this, CurScope, 5877 DSAStack)) 5878 return StmtError(); 5879 } 5880 } 5881 5882 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 5883 // The grainsize clause and num_tasks clause are mutually exclusive and may 5884 // not appear on the same taskloop directive. 5885 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 5886 return StmtError(); 5887 5888 getCurFunction()->setHasBranchProtectedScope(); 5889 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 5890 NestedLoopCount, Clauses, AStmt, B); 5891 } 5892 5893 StmtResult Sema::ActOnOpenMPDistributeDirective( 5894 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5895 SourceLocation EndLoc, 5896 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 5897 if (!AStmt) 5898 return StmtError(); 5899 5900 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5901 OMPLoopDirective::HelperExprs B; 5902 // In presence of clause 'collapse' with number of loops, it will 5903 // define the nested loops number. 5904 unsigned NestedLoopCount = 5905 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 5906 nullptr /*ordered not a clause on distribute*/, AStmt, 5907 *this, *DSAStack, VarsWithImplicitDSA, B); 5908 if (NestedLoopCount == 0) 5909 return StmtError(); 5910 5911 assert((CurContext->isDependentContext() || B.builtAll()) && 5912 "omp for loop exprs were not built"); 5913 5914 getCurFunction()->setHasBranchProtectedScope(); 5915 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 5916 NestedLoopCount, Clauses, AStmt, B); 5917 } 5918 5919 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 5920 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5921 SourceLocation EndLoc, 5922 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 5923 if (!AStmt) 5924 return StmtError(); 5925 5926 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 5927 // 1.2.2 OpenMP Language Terminology 5928 // Structured block - An executable statement with a single entry at the 5929 // top and a single exit at the bottom. 5930 // The point of exit cannot be a branch out of the structured block. 5931 // longjmp() and throw() must not violate the entry/exit criteria. 5932 CS->getCapturedDecl()->setNothrow(); 5933 5934 OMPLoopDirective::HelperExprs B; 5935 // In presence of clause 'collapse' with number of loops, it will 5936 // define the nested loops number. 5937 unsigned NestedLoopCount = CheckOpenMPLoop( 5938 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 5939 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, 5940 VarsWithImplicitDSA, B); 5941 if (NestedLoopCount == 0) 5942 return StmtError(); 5943 5944 assert((CurContext->isDependentContext() || B.builtAll()) && 5945 "omp for loop exprs were not built"); 5946 5947 getCurFunction()->setHasBranchProtectedScope(); 5948 return OMPDistributeParallelForDirective::Create( 5949 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 5950 } 5951 5952 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 5953 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5954 SourceLocation EndLoc, 5955 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 5956 if (!AStmt) 5957 return StmtError(); 5958 5959 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 5960 // 1.2.2 OpenMP Language Terminology 5961 // Structured block - An executable statement with a single entry at the 5962 // top and a single exit at the bottom. 5963 // The point of exit cannot be a branch out of the structured block. 5964 // longjmp() and throw() must not violate the entry/exit criteria. 5965 CS->getCapturedDecl()->setNothrow(); 5966 5967 OMPLoopDirective::HelperExprs B; 5968 // In presence of clause 'collapse' with number of loops, it will 5969 // define the nested loops number. 5970 unsigned NestedLoopCount = CheckOpenMPLoop( 5971 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 5972 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, 5973 VarsWithImplicitDSA, B); 5974 if (NestedLoopCount == 0) 5975 return StmtError(); 5976 5977 assert((CurContext->isDependentContext() || B.builtAll()) && 5978 "omp for loop exprs were not built"); 5979 5980 if (checkSimdlenSafelenSpecified(*this, Clauses)) 5981 return StmtError(); 5982 5983 getCurFunction()->setHasBranchProtectedScope(); 5984 return OMPDistributeParallelForSimdDirective::Create( 5985 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 5986 } 5987 5988 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 5989 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5990 SourceLocation EndLoc, 5991 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 5992 if (!AStmt) 5993 return StmtError(); 5994 5995 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 5996 // 1.2.2 OpenMP Language Terminology 5997 // Structured block - An executable statement with a single entry at the 5998 // top and a single exit at the bottom. 5999 // The point of exit cannot be a branch out of the structured block. 6000 // longjmp() and throw() must not violate the entry/exit criteria. 6001 CS->getCapturedDecl()->setNothrow(); 6002 6003 OMPLoopDirective::HelperExprs B; 6004 // In presence of clause 'collapse' with number of loops, it will 6005 // define the nested loops number. 6006 unsigned NestedLoopCount = 6007 CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 6008 nullptr /*ordered not a clause on distribute*/, AStmt, 6009 *this, *DSAStack, VarsWithImplicitDSA, B); 6010 if (NestedLoopCount == 0) 6011 return StmtError(); 6012 6013 assert((CurContext->isDependentContext() || B.builtAll()) && 6014 "omp for loop exprs were not built"); 6015 6016 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6017 return StmtError(); 6018 6019 getCurFunction()->setHasBranchProtectedScope(); 6020 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 6021 NestedLoopCount, Clauses, AStmt, B); 6022 } 6023 6024 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 6025 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6026 SourceLocation EndLoc, 6027 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6028 if (!AStmt) 6029 return StmtError(); 6030 6031 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6032 // 1.2.2 OpenMP Language Terminology 6033 // Structured block - An executable statement with a single entry at the 6034 // top and a single exit at the bottom. 6035 // The point of exit cannot be a branch out of the structured block. 6036 // longjmp() and throw() must not violate the entry/exit criteria. 6037 CS->getCapturedDecl()->setNothrow(); 6038 6039 OMPLoopDirective::HelperExprs B; 6040 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6041 // define the nested loops number. 6042 unsigned NestedLoopCount = CheckOpenMPLoop( 6043 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 6044 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 6045 VarsWithImplicitDSA, B); 6046 if (NestedLoopCount == 0) 6047 return StmtError(); 6048 6049 assert((CurContext->isDependentContext() || B.builtAll()) && 6050 "omp target parallel for simd loop exprs were not built"); 6051 6052 if (!CurContext->isDependentContext()) { 6053 // Finalize the clauses that need pre-built expressions for CodeGen. 6054 for (auto C : Clauses) { 6055 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6056 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6057 B.NumIterations, *this, CurScope, 6058 DSAStack)) 6059 return StmtError(); 6060 } 6061 } 6062 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6063 return StmtError(); 6064 6065 getCurFunction()->setHasBranchProtectedScope(); 6066 return OMPTargetParallelForSimdDirective::Create( 6067 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6068 } 6069 6070 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 6071 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6072 SourceLocation EndLoc, 6073 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6074 if (!AStmt) 6075 return StmtError(); 6076 6077 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6078 // 1.2.2 OpenMP Language Terminology 6079 // Structured block - An executable statement with a single entry at the 6080 // top and a single exit at the bottom. 6081 // The point of exit cannot be a branch out of the structured block. 6082 // longjmp() and throw() must not violate the entry/exit criteria. 6083 CS->getCapturedDecl()->setNothrow(); 6084 6085 OMPLoopDirective::HelperExprs B; 6086 // In presence of clause 'collapse' with number of loops, it will define the 6087 // nested loops number. 6088 unsigned NestedLoopCount = 6089 CheckOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 6090 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 6091 VarsWithImplicitDSA, B); 6092 if (NestedLoopCount == 0) 6093 return StmtError(); 6094 6095 assert((CurContext->isDependentContext() || B.builtAll()) && 6096 "omp target simd loop exprs were not built"); 6097 6098 if (!CurContext->isDependentContext()) { 6099 // Finalize the clauses that need pre-built expressions for CodeGen. 6100 for (auto C : Clauses) { 6101 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6102 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6103 B.NumIterations, *this, CurScope, 6104 DSAStack)) 6105 return StmtError(); 6106 } 6107 } 6108 6109 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6110 return StmtError(); 6111 6112 getCurFunction()->setHasBranchProtectedScope(); 6113 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 6114 NestedLoopCount, Clauses, AStmt, B); 6115 } 6116 6117 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 6118 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6119 SourceLocation EndLoc, 6120 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6121 if (!AStmt) 6122 return StmtError(); 6123 6124 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6125 // 1.2.2 OpenMP Language Terminology 6126 // Structured block - An executable statement with a single entry at the 6127 // top and a single exit at the bottom. 6128 // The point of exit cannot be a branch out of the structured block. 6129 // longjmp() and throw() must not violate the entry/exit criteria. 6130 CS->getCapturedDecl()->setNothrow(); 6131 6132 OMPLoopDirective::HelperExprs B; 6133 // In presence of clause 'collapse' with number of loops, it will 6134 // define the nested loops number. 6135 unsigned NestedLoopCount = 6136 CheckOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 6137 nullptr /*ordered not a clause on distribute*/, AStmt, 6138 *this, *DSAStack, VarsWithImplicitDSA, B); 6139 if (NestedLoopCount == 0) 6140 return StmtError(); 6141 6142 assert((CurContext->isDependentContext() || B.builtAll()) && 6143 "omp teams distribute loop exprs were not built"); 6144 6145 getCurFunction()->setHasBranchProtectedScope(); 6146 return OMPTeamsDistributeDirective::Create( 6147 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6148 } 6149 6150 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 6151 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6152 SourceLocation EndLoc, 6153 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6154 if (!AStmt) 6155 return StmtError(); 6156 6157 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6158 // 1.2.2 OpenMP Language Terminology 6159 // Structured block - An executable statement with a single entry at the 6160 // top and a single exit at the bottom. 6161 // The point of exit cannot be a branch out of the structured block. 6162 // longjmp() and throw() must not violate the entry/exit criteria. 6163 CS->getCapturedDecl()->setNothrow(); 6164 6165 OMPLoopDirective::HelperExprs B; 6166 // In presence of clause 'collapse' with number of loops, it will 6167 // define the nested loops number. 6168 unsigned NestedLoopCount = CheckOpenMPLoop( 6169 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 6170 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, 6171 VarsWithImplicitDSA, B); 6172 6173 if (NestedLoopCount == 0) 6174 return StmtError(); 6175 6176 assert((CurContext->isDependentContext() || B.builtAll()) && 6177 "omp teams distribute simd loop exprs were not built"); 6178 6179 if (!CurContext->isDependentContext()) { 6180 // Finalize the clauses that need pre-built expressions for CodeGen. 6181 for (auto C : Clauses) { 6182 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6183 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6184 B.NumIterations, *this, CurScope, 6185 DSAStack)) 6186 return StmtError(); 6187 } 6188 } 6189 6190 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6191 return StmtError(); 6192 6193 getCurFunction()->setHasBranchProtectedScope(); 6194 return OMPTeamsDistributeSimdDirective::Create( 6195 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6196 } 6197 6198 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 6199 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6200 SourceLocation EndLoc, 6201 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6202 if (!AStmt) 6203 return StmtError(); 6204 6205 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6206 // 1.2.2 OpenMP Language Terminology 6207 // Structured block - An executable statement with a single entry at the 6208 // top and a single exit at the bottom. 6209 // The point of exit cannot be a branch out of the structured block. 6210 // longjmp() and throw() must not violate the entry/exit criteria. 6211 CS->getCapturedDecl()->setNothrow(); 6212 6213 OMPLoopDirective::HelperExprs B; 6214 // In presence of clause 'collapse' with number of loops, it will 6215 // define the nested loops number. 6216 auto NestedLoopCount = CheckOpenMPLoop( 6217 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 6218 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, 6219 VarsWithImplicitDSA, B); 6220 6221 if (NestedLoopCount == 0) 6222 return StmtError(); 6223 6224 assert((CurContext->isDependentContext() || B.builtAll()) && 6225 "omp for loop exprs were not built"); 6226 6227 if (!CurContext->isDependentContext()) { 6228 // Finalize the clauses that need pre-built expressions for CodeGen. 6229 for (auto C : Clauses) { 6230 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6231 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6232 B.NumIterations, *this, CurScope, 6233 DSAStack)) 6234 return StmtError(); 6235 } 6236 } 6237 6238 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6239 return StmtError(); 6240 6241 getCurFunction()->setHasBranchProtectedScope(); 6242 return OMPTeamsDistributeParallelForSimdDirective::Create( 6243 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6244 } 6245 6246 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 6247 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6248 SourceLocation EndLoc, 6249 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6250 if (!AStmt) 6251 return StmtError(); 6252 6253 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6254 // 1.2.2 OpenMP Language Terminology 6255 // Structured block - An executable statement with a single entry at the 6256 // top and a single exit at the bottom. 6257 // The point of exit cannot be a branch out of the structured block. 6258 // longjmp() and throw() must not violate the entry/exit criteria. 6259 CS->getCapturedDecl()->setNothrow(); 6260 6261 OMPLoopDirective::HelperExprs B; 6262 // In presence of clause 'collapse' with number of loops, it will 6263 // define the nested loops number. 6264 unsigned NestedLoopCount = CheckOpenMPLoop( 6265 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 6266 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, 6267 VarsWithImplicitDSA, B); 6268 6269 if (NestedLoopCount == 0) 6270 return StmtError(); 6271 6272 assert((CurContext->isDependentContext() || B.builtAll()) && 6273 "omp for loop exprs were not built"); 6274 6275 if (!CurContext->isDependentContext()) { 6276 // Finalize the clauses that need pre-built expressions for CodeGen. 6277 for (auto C : Clauses) { 6278 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6279 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6280 B.NumIterations, *this, CurScope, 6281 DSAStack)) 6282 return StmtError(); 6283 } 6284 } 6285 6286 getCurFunction()->setHasBranchProtectedScope(); 6287 return OMPTeamsDistributeParallelForDirective::Create( 6288 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6289 } 6290 6291 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 6292 Stmt *AStmt, 6293 SourceLocation StartLoc, 6294 SourceLocation EndLoc) { 6295 if (!AStmt) 6296 return StmtError(); 6297 6298 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6299 // 1.2.2 OpenMP Language Terminology 6300 // Structured block - An executable statement with a single entry at the 6301 // top and a single exit at the bottom. 6302 // The point of exit cannot be a branch out of the structured block. 6303 // longjmp() and throw() must not violate the entry/exit criteria. 6304 CS->getCapturedDecl()->setNothrow(); 6305 6306 getCurFunction()->setHasBranchProtectedScope(); 6307 6308 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 6309 AStmt); 6310 } 6311 6312 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 6313 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6314 SourceLocation EndLoc, 6315 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6316 if (!AStmt) 6317 return StmtError(); 6318 6319 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6320 // 1.2.2 OpenMP Language Terminology 6321 // Structured block - An executable statement with a single entry at the 6322 // top and a single exit at the bottom. 6323 // The point of exit cannot be a branch out of the structured block. 6324 // longjmp() and throw() must not violate the entry/exit criteria. 6325 CS->getCapturedDecl()->setNothrow(); 6326 6327 OMPLoopDirective::HelperExprs B; 6328 // In presence of clause 'collapse' with number of loops, it will 6329 // define the nested loops number. 6330 auto NestedLoopCount = CheckOpenMPLoop( 6331 OMPD_target_teams_distribute, 6332 getCollapseNumberExpr(Clauses), 6333 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, 6334 VarsWithImplicitDSA, B); 6335 if (NestedLoopCount == 0) 6336 return StmtError(); 6337 6338 assert((CurContext->isDependentContext() || B.builtAll()) && 6339 "omp target teams distribute loop exprs were not built"); 6340 6341 getCurFunction()->setHasBranchProtectedScope(); 6342 return OMPTargetTeamsDistributeDirective::Create( 6343 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6344 } 6345 6346 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 6347 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6348 SourceLocation EndLoc, 6349 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6350 if (!AStmt) 6351 return StmtError(); 6352 6353 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6354 // 1.2.2 OpenMP Language Terminology 6355 // Structured block - An executable statement with a single entry at the 6356 // top and a single exit at the bottom. 6357 // The point of exit cannot be a branch out of the structured block. 6358 // longjmp() and throw() must not violate the entry/exit criteria. 6359 CS->getCapturedDecl()->setNothrow(); 6360 6361 OMPLoopDirective::HelperExprs B; 6362 // In presence of clause 'collapse' with number of loops, it will 6363 // define the nested loops number. 6364 auto NestedLoopCount = CheckOpenMPLoop( 6365 OMPD_target_teams_distribute_parallel_for, 6366 getCollapseNumberExpr(Clauses), 6367 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, 6368 VarsWithImplicitDSA, B); 6369 if (NestedLoopCount == 0) 6370 return StmtError(); 6371 6372 assert((CurContext->isDependentContext() || B.builtAll()) && 6373 "omp target teams distribute parallel for loop exprs were not built"); 6374 6375 if (!CurContext->isDependentContext()) { 6376 // Finalize the clauses that need pre-built expressions for CodeGen. 6377 for (auto C : Clauses) { 6378 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6379 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6380 B.NumIterations, *this, CurScope, 6381 DSAStack)) 6382 return StmtError(); 6383 } 6384 } 6385 6386 getCurFunction()->setHasBranchProtectedScope(); 6387 return OMPTargetTeamsDistributeParallelForDirective::Create( 6388 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6389 } 6390 6391 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 6392 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6393 SourceLocation EndLoc, 6394 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6395 if (!AStmt) 6396 return StmtError(); 6397 6398 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 6399 // 1.2.2 OpenMP Language Terminology 6400 // Structured block - An executable statement with a single entry at the 6401 // top and a single exit at the bottom. 6402 // The point of exit cannot be a branch out of the structured block. 6403 // longjmp() and throw() must not violate the entry/exit criteria. 6404 CS->getCapturedDecl()->setNothrow(); 6405 6406 OMPLoopDirective::HelperExprs B; 6407 // In presence of clause 'collapse' with number of loops, it will 6408 // define the nested loops number. 6409 auto NestedLoopCount = CheckOpenMPLoop( 6410 OMPD_target_teams_distribute_parallel_for_simd, 6411 getCollapseNumberExpr(Clauses), 6412 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, 6413 VarsWithImplicitDSA, B); 6414 if (NestedLoopCount == 0) 6415 return StmtError(); 6416 6417 assert((CurContext->isDependentContext() || B.builtAll()) && 6418 "omp target teams distribute parallel for simd loop exprs were not " 6419 "built"); 6420 6421 if (!CurContext->isDependentContext()) { 6422 // Finalize the clauses that need pre-built expressions for CodeGen. 6423 for (auto C : Clauses) { 6424 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6425 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6426 B.NumIterations, *this, CurScope, 6427 DSAStack)) 6428 return StmtError(); 6429 } 6430 } 6431 6432 getCurFunction()->setHasBranchProtectedScope(); 6433 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 6434 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6435 } 6436 6437 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 6438 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6439 SourceLocation EndLoc, 6440 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) { 6441 if (!AStmt) 6442 return StmtError(); 6443 6444 auto *CS = cast<CapturedStmt>(AStmt); 6445 // 1.2.2 OpenMP Language Terminology 6446 // Structured block - An executable statement with a single entry at the 6447 // top and a single exit at the bottom. 6448 // The point of exit cannot be a branch out of the structured block. 6449 // longjmp() and throw() must not violate the entry/exit criteria. 6450 CS->getCapturedDecl()->setNothrow(); 6451 6452 OMPLoopDirective::HelperExprs B; 6453 // In presence of clause 'collapse' with number of loops, it will 6454 // define the nested loops number. 6455 auto NestedLoopCount = CheckOpenMPLoop( 6456 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 6457 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack, 6458 VarsWithImplicitDSA, B); 6459 if (NestedLoopCount == 0) 6460 return StmtError(); 6461 6462 assert((CurContext->isDependentContext() || B.builtAll()) && 6463 "omp target teams distribute simd loop exprs were not built"); 6464 6465 getCurFunction()->setHasBranchProtectedScope(); 6466 return OMPTargetTeamsDistributeSimdDirective::Create( 6467 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6468 } 6469 6470 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 6471 SourceLocation StartLoc, 6472 SourceLocation LParenLoc, 6473 SourceLocation EndLoc) { 6474 OMPClause *Res = nullptr; 6475 switch (Kind) { 6476 case OMPC_final: 6477 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 6478 break; 6479 case OMPC_num_threads: 6480 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 6481 break; 6482 case OMPC_safelen: 6483 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 6484 break; 6485 case OMPC_simdlen: 6486 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 6487 break; 6488 case OMPC_collapse: 6489 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 6490 break; 6491 case OMPC_ordered: 6492 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 6493 break; 6494 case OMPC_device: 6495 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc); 6496 break; 6497 case OMPC_num_teams: 6498 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 6499 break; 6500 case OMPC_thread_limit: 6501 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 6502 break; 6503 case OMPC_priority: 6504 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 6505 break; 6506 case OMPC_grainsize: 6507 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 6508 break; 6509 case OMPC_num_tasks: 6510 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 6511 break; 6512 case OMPC_hint: 6513 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 6514 break; 6515 case OMPC_if: 6516 case OMPC_default: 6517 case OMPC_proc_bind: 6518 case OMPC_schedule: 6519 case OMPC_private: 6520 case OMPC_firstprivate: 6521 case OMPC_lastprivate: 6522 case OMPC_shared: 6523 case OMPC_reduction: 6524 case OMPC_linear: 6525 case OMPC_aligned: 6526 case OMPC_copyin: 6527 case OMPC_copyprivate: 6528 case OMPC_nowait: 6529 case OMPC_untied: 6530 case OMPC_mergeable: 6531 case OMPC_threadprivate: 6532 case OMPC_flush: 6533 case OMPC_read: 6534 case OMPC_write: 6535 case OMPC_update: 6536 case OMPC_capture: 6537 case OMPC_seq_cst: 6538 case OMPC_depend: 6539 case OMPC_threads: 6540 case OMPC_simd: 6541 case OMPC_map: 6542 case OMPC_nogroup: 6543 case OMPC_dist_schedule: 6544 case OMPC_defaultmap: 6545 case OMPC_unknown: 6546 case OMPC_uniform: 6547 case OMPC_to: 6548 case OMPC_from: 6549 case OMPC_use_device_ptr: 6550 case OMPC_is_device_ptr: 6551 llvm_unreachable("Clause is not allowed."); 6552 } 6553 return Res; 6554 } 6555 6556 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 6557 Expr *Condition, SourceLocation StartLoc, 6558 SourceLocation LParenLoc, 6559 SourceLocation NameModifierLoc, 6560 SourceLocation ColonLoc, 6561 SourceLocation EndLoc) { 6562 Expr *ValExpr = Condition; 6563 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 6564 !Condition->isInstantiationDependent() && 6565 !Condition->containsUnexpandedParameterPack()) { 6566 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 6567 if (Val.isInvalid()) 6568 return nullptr; 6569 6570 ValExpr = MakeFullExpr(Val.get()).get(); 6571 } 6572 6573 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc, 6574 NameModifierLoc, ColonLoc, EndLoc); 6575 } 6576 6577 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 6578 SourceLocation StartLoc, 6579 SourceLocation LParenLoc, 6580 SourceLocation EndLoc) { 6581 Expr *ValExpr = Condition; 6582 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 6583 !Condition->isInstantiationDependent() && 6584 !Condition->containsUnexpandedParameterPack()) { 6585 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 6586 if (Val.isInvalid()) 6587 return nullptr; 6588 6589 ValExpr = MakeFullExpr(Val.get()).get(); 6590 } 6591 6592 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc); 6593 } 6594 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 6595 Expr *Op) { 6596 if (!Op) 6597 return ExprError(); 6598 6599 class IntConvertDiagnoser : public ICEConvertDiagnoser { 6600 public: 6601 IntConvertDiagnoser() 6602 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 6603 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 6604 QualType T) override { 6605 return S.Diag(Loc, diag::err_omp_not_integral) << T; 6606 } 6607 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 6608 QualType T) override { 6609 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 6610 } 6611 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 6612 QualType T, 6613 QualType ConvTy) override { 6614 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 6615 } 6616 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 6617 QualType ConvTy) override { 6618 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 6619 << ConvTy->isEnumeralType() << ConvTy; 6620 } 6621 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 6622 QualType T) override { 6623 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 6624 } 6625 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 6626 QualType ConvTy) override { 6627 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 6628 << ConvTy->isEnumeralType() << ConvTy; 6629 } 6630 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 6631 QualType) override { 6632 llvm_unreachable("conversion functions are permitted"); 6633 } 6634 } ConvertDiagnoser; 6635 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 6636 } 6637 6638 static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, 6639 OpenMPClauseKind CKind, 6640 bool StrictlyPositive) { 6641 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 6642 !ValExpr->isInstantiationDependent()) { 6643 SourceLocation Loc = ValExpr->getExprLoc(); 6644 ExprResult Value = 6645 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 6646 if (Value.isInvalid()) 6647 return false; 6648 6649 ValExpr = Value.get(); 6650 // The expression must evaluate to a non-negative integer value. 6651 llvm::APSInt Result; 6652 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) && 6653 Result.isSigned() && 6654 !((!StrictlyPositive && Result.isNonNegative()) || 6655 (StrictlyPositive && Result.isStrictlyPositive()))) { 6656 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 6657 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 6658 << ValExpr->getSourceRange(); 6659 return false; 6660 } 6661 } 6662 return true; 6663 } 6664 6665 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 6666 SourceLocation StartLoc, 6667 SourceLocation LParenLoc, 6668 SourceLocation EndLoc) { 6669 Expr *ValExpr = NumThreads; 6670 6671 // OpenMP [2.5, Restrictions] 6672 // The num_threads expression must evaluate to a positive integer value. 6673 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 6674 /*StrictlyPositive=*/true)) 6675 return nullptr; 6676 6677 return new (Context) 6678 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc); 6679 } 6680 6681 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 6682 OpenMPClauseKind CKind, 6683 bool StrictlyPositive) { 6684 if (!E) 6685 return ExprError(); 6686 if (E->isValueDependent() || E->isTypeDependent() || 6687 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 6688 return E; 6689 llvm::APSInt Result; 6690 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); 6691 if (ICE.isInvalid()) 6692 return ExprError(); 6693 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 6694 (!StrictlyPositive && !Result.isNonNegative())) { 6695 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 6696 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 6697 << E->getSourceRange(); 6698 return ExprError(); 6699 } 6700 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 6701 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 6702 << E->getSourceRange(); 6703 return ExprError(); 6704 } 6705 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 6706 DSAStack->setAssociatedLoops(Result.getExtValue()); 6707 else if (CKind == OMPC_ordered) 6708 DSAStack->setAssociatedLoops(Result.getExtValue()); 6709 return ICE; 6710 } 6711 6712 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 6713 SourceLocation LParenLoc, 6714 SourceLocation EndLoc) { 6715 // OpenMP [2.8.1, simd construct, Description] 6716 // The parameter of the safelen clause must be a constant 6717 // positive integer expression. 6718 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 6719 if (Safelen.isInvalid()) 6720 return nullptr; 6721 return new (Context) 6722 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 6723 } 6724 6725 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 6726 SourceLocation LParenLoc, 6727 SourceLocation EndLoc) { 6728 // OpenMP [2.8.1, simd construct, Description] 6729 // The parameter of the simdlen clause must be a constant 6730 // positive integer expression. 6731 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 6732 if (Simdlen.isInvalid()) 6733 return nullptr; 6734 return new (Context) 6735 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 6736 } 6737 6738 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 6739 SourceLocation StartLoc, 6740 SourceLocation LParenLoc, 6741 SourceLocation EndLoc) { 6742 // OpenMP [2.7.1, loop construct, Description] 6743 // OpenMP [2.8.1, simd construct, Description] 6744 // OpenMP [2.9.6, distribute construct, Description] 6745 // The parameter of the collapse clause must be a constant 6746 // positive integer expression. 6747 ExprResult NumForLoopsResult = 6748 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 6749 if (NumForLoopsResult.isInvalid()) 6750 return nullptr; 6751 return new (Context) 6752 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 6753 } 6754 6755 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 6756 SourceLocation EndLoc, 6757 SourceLocation LParenLoc, 6758 Expr *NumForLoops) { 6759 // OpenMP [2.7.1, loop construct, Description] 6760 // OpenMP [2.8.1, simd construct, Description] 6761 // OpenMP [2.9.6, distribute construct, Description] 6762 // The parameter of the ordered clause must be a constant 6763 // positive integer expression if any. 6764 if (NumForLoops && LParenLoc.isValid()) { 6765 ExprResult NumForLoopsResult = 6766 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 6767 if (NumForLoopsResult.isInvalid()) 6768 return nullptr; 6769 NumForLoops = NumForLoopsResult.get(); 6770 } else 6771 NumForLoops = nullptr; 6772 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops); 6773 return new (Context) 6774 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc); 6775 } 6776 6777 OMPClause *Sema::ActOnOpenMPSimpleClause( 6778 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 6779 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 6780 OMPClause *Res = nullptr; 6781 switch (Kind) { 6782 case OMPC_default: 6783 Res = 6784 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), 6785 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 6786 break; 6787 case OMPC_proc_bind: 6788 Res = ActOnOpenMPProcBindClause( 6789 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, 6790 LParenLoc, EndLoc); 6791 break; 6792 case OMPC_if: 6793 case OMPC_final: 6794 case OMPC_num_threads: 6795 case OMPC_safelen: 6796 case OMPC_simdlen: 6797 case OMPC_collapse: 6798 case OMPC_schedule: 6799 case OMPC_private: 6800 case OMPC_firstprivate: 6801 case OMPC_lastprivate: 6802 case OMPC_shared: 6803 case OMPC_reduction: 6804 case OMPC_linear: 6805 case OMPC_aligned: 6806 case OMPC_copyin: 6807 case OMPC_copyprivate: 6808 case OMPC_ordered: 6809 case OMPC_nowait: 6810 case OMPC_untied: 6811 case OMPC_mergeable: 6812 case OMPC_threadprivate: 6813 case OMPC_flush: 6814 case OMPC_read: 6815 case OMPC_write: 6816 case OMPC_update: 6817 case OMPC_capture: 6818 case OMPC_seq_cst: 6819 case OMPC_depend: 6820 case OMPC_device: 6821 case OMPC_threads: 6822 case OMPC_simd: 6823 case OMPC_map: 6824 case OMPC_num_teams: 6825 case OMPC_thread_limit: 6826 case OMPC_priority: 6827 case OMPC_grainsize: 6828 case OMPC_nogroup: 6829 case OMPC_num_tasks: 6830 case OMPC_hint: 6831 case OMPC_dist_schedule: 6832 case OMPC_defaultmap: 6833 case OMPC_unknown: 6834 case OMPC_uniform: 6835 case OMPC_to: 6836 case OMPC_from: 6837 case OMPC_use_device_ptr: 6838 case OMPC_is_device_ptr: 6839 llvm_unreachable("Clause is not allowed."); 6840 } 6841 return Res; 6842 } 6843 6844 static std::string 6845 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 6846 ArrayRef<unsigned> Exclude = llvm::None) { 6847 std::string Values; 6848 unsigned Bound = Last >= 2 ? Last - 2 : 0; 6849 unsigned Skipped = Exclude.size(); 6850 auto S = Exclude.begin(), E = Exclude.end(); 6851 for (unsigned i = First; i < Last; ++i) { 6852 if (std::find(S, E, i) != E) { 6853 --Skipped; 6854 continue; 6855 } 6856 Values += "'"; 6857 Values += getOpenMPSimpleClauseTypeName(K, i); 6858 Values += "'"; 6859 if (i == Bound - Skipped) 6860 Values += " or "; 6861 else if (i != Bound + 1 - Skipped) 6862 Values += ", "; 6863 } 6864 return Values; 6865 } 6866 6867 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, 6868 SourceLocation KindKwLoc, 6869 SourceLocation StartLoc, 6870 SourceLocation LParenLoc, 6871 SourceLocation EndLoc) { 6872 if (Kind == OMPC_DEFAULT_unknown) { 6873 static_assert(OMPC_DEFAULT_unknown > 0, 6874 "OMPC_DEFAULT_unknown not greater than 0"); 6875 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 6876 << getListOfPossibleValues(OMPC_default, /*First=*/0, 6877 /*Last=*/OMPC_DEFAULT_unknown) 6878 << getOpenMPClauseName(OMPC_default); 6879 return nullptr; 6880 } 6881 switch (Kind) { 6882 case OMPC_DEFAULT_none: 6883 DSAStack->setDefaultDSANone(KindKwLoc); 6884 break; 6885 case OMPC_DEFAULT_shared: 6886 DSAStack->setDefaultDSAShared(KindKwLoc); 6887 break; 6888 case OMPC_DEFAULT_unknown: 6889 llvm_unreachable("Clause kind is not allowed."); 6890 break; 6891 } 6892 return new (Context) 6893 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 6894 } 6895 6896 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, 6897 SourceLocation KindKwLoc, 6898 SourceLocation StartLoc, 6899 SourceLocation LParenLoc, 6900 SourceLocation EndLoc) { 6901 if (Kind == OMPC_PROC_BIND_unknown) { 6902 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 6903 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0, 6904 /*Last=*/OMPC_PROC_BIND_unknown) 6905 << getOpenMPClauseName(OMPC_proc_bind); 6906 return nullptr; 6907 } 6908 return new (Context) 6909 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 6910 } 6911 6912 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 6913 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 6914 SourceLocation StartLoc, SourceLocation LParenLoc, 6915 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 6916 SourceLocation EndLoc) { 6917 OMPClause *Res = nullptr; 6918 switch (Kind) { 6919 case OMPC_schedule: 6920 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 6921 assert(Argument.size() == NumberOfElements && 6922 ArgumentLoc.size() == NumberOfElements); 6923 Res = ActOnOpenMPScheduleClause( 6924 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 6925 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 6926 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 6927 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 6928 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 6929 break; 6930 case OMPC_if: 6931 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 6932 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 6933 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 6934 DelimLoc, EndLoc); 6935 break; 6936 case OMPC_dist_schedule: 6937 Res = ActOnOpenMPDistScheduleClause( 6938 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 6939 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 6940 break; 6941 case OMPC_defaultmap: 6942 enum { Modifier, DefaultmapKind }; 6943 Res = ActOnOpenMPDefaultmapClause( 6944 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 6945 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 6946 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 6947 EndLoc); 6948 break; 6949 case OMPC_final: 6950 case OMPC_num_threads: 6951 case OMPC_safelen: 6952 case OMPC_simdlen: 6953 case OMPC_collapse: 6954 case OMPC_default: 6955 case OMPC_proc_bind: 6956 case OMPC_private: 6957 case OMPC_firstprivate: 6958 case OMPC_lastprivate: 6959 case OMPC_shared: 6960 case OMPC_reduction: 6961 case OMPC_linear: 6962 case OMPC_aligned: 6963 case OMPC_copyin: 6964 case OMPC_copyprivate: 6965 case OMPC_ordered: 6966 case OMPC_nowait: 6967 case OMPC_untied: 6968 case OMPC_mergeable: 6969 case OMPC_threadprivate: 6970 case OMPC_flush: 6971 case OMPC_read: 6972 case OMPC_write: 6973 case OMPC_update: 6974 case OMPC_capture: 6975 case OMPC_seq_cst: 6976 case OMPC_depend: 6977 case OMPC_device: 6978 case OMPC_threads: 6979 case OMPC_simd: 6980 case OMPC_map: 6981 case OMPC_num_teams: 6982 case OMPC_thread_limit: 6983 case OMPC_priority: 6984 case OMPC_grainsize: 6985 case OMPC_nogroup: 6986 case OMPC_num_tasks: 6987 case OMPC_hint: 6988 case OMPC_unknown: 6989 case OMPC_uniform: 6990 case OMPC_to: 6991 case OMPC_from: 6992 case OMPC_use_device_ptr: 6993 case OMPC_is_device_ptr: 6994 llvm_unreachable("Clause is not allowed."); 6995 } 6996 return Res; 6997 } 6998 6999 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 7000 OpenMPScheduleClauseModifier M2, 7001 SourceLocation M1Loc, SourceLocation M2Loc) { 7002 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 7003 SmallVector<unsigned, 2> Excluded; 7004 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 7005 Excluded.push_back(M2); 7006 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 7007 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 7008 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 7009 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 7010 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 7011 << getListOfPossibleValues(OMPC_schedule, 7012 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 7013 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 7014 Excluded) 7015 << getOpenMPClauseName(OMPC_schedule); 7016 return true; 7017 } 7018 return false; 7019 } 7020 7021 OMPClause *Sema::ActOnOpenMPScheduleClause( 7022 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 7023 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 7024 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 7025 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 7026 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 7027 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 7028 return nullptr; 7029 // OpenMP, 2.7.1, Loop Construct, Restrictions 7030 // Either the monotonic modifier or the nonmonotonic modifier can be specified 7031 // but not both. 7032 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 7033 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 7034 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 7035 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 7036 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 7037 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 7038 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 7039 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 7040 return nullptr; 7041 } 7042 if (Kind == OMPC_SCHEDULE_unknown) { 7043 std::string Values; 7044 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 7045 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 7046 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 7047 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 7048 Exclude); 7049 } else { 7050 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 7051 /*Last=*/OMPC_SCHEDULE_unknown); 7052 } 7053 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 7054 << Values << getOpenMPClauseName(OMPC_schedule); 7055 return nullptr; 7056 } 7057 // OpenMP, 2.7.1, Loop Construct, Restrictions 7058 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 7059 // schedule(guided). 7060 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 7061 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 7062 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 7063 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 7064 diag::err_omp_schedule_nonmonotonic_static); 7065 return nullptr; 7066 } 7067 Expr *ValExpr = ChunkSize; 7068 Stmt *HelperValStmt = nullptr; 7069 if (ChunkSize) { 7070 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 7071 !ChunkSize->isInstantiationDependent() && 7072 !ChunkSize->containsUnexpandedParameterPack()) { 7073 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); 7074 ExprResult Val = 7075 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 7076 if (Val.isInvalid()) 7077 return nullptr; 7078 7079 ValExpr = Val.get(); 7080 7081 // OpenMP [2.7.1, Restrictions] 7082 // chunk_size must be a loop invariant integer expression with a positive 7083 // value. 7084 llvm::APSInt Result; 7085 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 7086 if (Result.isSigned() && !Result.isStrictlyPositive()) { 7087 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 7088 << "schedule" << 1 << ChunkSize->getSourceRange(); 7089 return nullptr; 7090 } 7091 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) && 7092 !CurContext->isDependentContext()) { 7093 llvm::MapVector<Expr *, DeclRefExpr *> Captures; 7094 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 7095 HelperValStmt = buildPreInits(Context, Captures); 7096 } 7097 } 7098 } 7099 7100 return new (Context) 7101 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 7102 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 7103 } 7104 7105 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 7106 SourceLocation StartLoc, 7107 SourceLocation EndLoc) { 7108 OMPClause *Res = nullptr; 7109 switch (Kind) { 7110 case OMPC_ordered: 7111 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 7112 break; 7113 case OMPC_nowait: 7114 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 7115 break; 7116 case OMPC_untied: 7117 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 7118 break; 7119 case OMPC_mergeable: 7120 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 7121 break; 7122 case OMPC_read: 7123 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 7124 break; 7125 case OMPC_write: 7126 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 7127 break; 7128 case OMPC_update: 7129 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 7130 break; 7131 case OMPC_capture: 7132 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 7133 break; 7134 case OMPC_seq_cst: 7135 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 7136 break; 7137 case OMPC_threads: 7138 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 7139 break; 7140 case OMPC_simd: 7141 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 7142 break; 7143 case OMPC_nogroup: 7144 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 7145 break; 7146 case OMPC_if: 7147 case OMPC_final: 7148 case OMPC_num_threads: 7149 case OMPC_safelen: 7150 case OMPC_simdlen: 7151 case OMPC_collapse: 7152 case OMPC_schedule: 7153 case OMPC_private: 7154 case OMPC_firstprivate: 7155 case OMPC_lastprivate: 7156 case OMPC_shared: 7157 case OMPC_reduction: 7158 case OMPC_linear: 7159 case OMPC_aligned: 7160 case OMPC_copyin: 7161 case OMPC_copyprivate: 7162 case OMPC_default: 7163 case OMPC_proc_bind: 7164 case OMPC_threadprivate: 7165 case OMPC_flush: 7166 case OMPC_depend: 7167 case OMPC_device: 7168 case OMPC_map: 7169 case OMPC_num_teams: 7170 case OMPC_thread_limit: 7171 case OMPC_priority: 7172 case OMPC_grainsize: 7173 case OMPC_num_tasks: 7174 case OMPC_hint: 7175 case OMPC_dist_schedule: 7176 case OMPC_defaultmap: 7177 case OMPC_unknown: 7178 case OMPC_uniform: 7179 case OMPC_to: 7180 case OMPC_from: 7181 case OMPC_use_device_ptr: 7182 case OMPC_is_device_ptr: 7183 llvm_unreachable("Clause is not allowed."); 7184 } 7185 return Res; 7186 } 7187 7188 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 7189 SourceLocation EndLoc) { 7190 DSAStack->setNowaitRegion(); 7191 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 7192 } 7193 7194 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 7195 SourceLocation EndLoc) { 7196 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 7197 } 7198 7199 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 7200 SourceLocation EndLoc) { 7201 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 7202 } 7203 7204 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 7205 SourceLocation EndLoc) { 7206 return new (Context) OMPReadClause(StartLoc, EndLoc); 7207 } 7208 7209 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 7210 SourceLocation EndLoc) { 7211 return new (Context) OMPWriteClause(StartLoc, EndLoc); 7212 } 7213 7214 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 7215 SourceLocation EndLoc) { 7216 return new (Context) OMPUpdateClause(StartLoc, EndLoc); 7217 } 7218 7219 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 7220 SourceLocation EndLoc) { 7221 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 7222 } 7223 7224 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 7225 SourceLocation EndLoc) { 7226 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 7227 } 7228 7229 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 7230 SourceLocation EndLoc) { 7231 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 7232 } 7233 7234 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 7235 SourceLocation EndLoc) { 7236 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 7237 } 7238 7239 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 7240 SourceLocation EndLoc) { 7241 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 7242 } 7243 7244 OMPClause *Sema::ActOnOpenMPVarListClause( 7245 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, 7246 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 7247 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec, 7248 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind, 7249 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier, 7250 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, 7251 SourceLocation DepLinMapLoc) { 7252 OMPClause *Res = nullptr; 7253 switch (Kind) { 7254 case OMPC_private: 7255 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 7256 break; 7257 case OMPC_firstprivate: 7258 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 7259 break; 7260 case OMPC_lastprivate: 7261 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 7262 break; 7263 case OMPC_shared: 7264 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 7265 break; 7266 case OMPC_reduction: 7267 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 7268 EndLoc, ReductionIdScopeSpec, ReductionId); 7269 break; 7270 case OMPC_linear: 7271 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, 7272 LinKind, DepLinMapLoc, ColonLoc, EndLoc); 7273 break; 7274 case OMPC_aligned: 7275 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, 7276 ColonLoc, EndLoc); 7277 break; 7278 case OMPC_copyin: 7279 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 7280 break; 7281 case OMPC_copyprivate: 7282 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 7283 break; 7284 case OMPC_flush: 7285 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 7286 break; 7287 case OMPC_depend: 7288 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList, 7289 StartLoc, LParenLoc, EndLoc); 7290 break; 7291 case OMPC_map: 7292 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit, 7293 DepLinMapLoc, ColonLoc, VarList, StartLoc, 7294 LParenLoc, EndLoc); 7295 break; 7296 case OMPC_to: 7297 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc); 7298 break; 7299 case OMPC_from: 7300 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc); 7301 break; 7302 case OMPC_use_device_ptr: 7303 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc); 7304 break; 7305 case OMPC_is_device_ptr: 7306 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc); 7307 break; 7308 case OMPC_if: 7309 case OMPC_final: 7310 case OMPC_num_threads: 7311 case OMPC_safelen: 7312 case OMPC_simdlen: 7313 case OMPC_collapse: 7314 case OMPC_default: 7315 case OMPC_proc_bind: 7316 case OMPC_schedule: 7317 case OMPC_ordered: 7318 case OMPC_nowait: 7319 case OMPC_untied: 7320 case OMPC_mergeable: 7321 case OMPC_threadprivate: 7322 case OMPC_read: 7323 case OMPC_write: 7324 case OMPC_update: 7325 case OMPC_capture: 7326 case OMPC_seq_cst: 7327 case OMPC_device: 7328 case OMPC_threads: 7329 case OMPC_simd: 7330 case OMPC_num_teams: 7331 case OMPC_thread_limit: 7332 case OMPC_priority: 7333 case OMPC_grainsize: 7334 case OMPC_nogroup: 7335 case OMPC_num_tasks: 7336 case OMPC_hint: 7337 case OMPC_dist_schedule: 7338 case OMPC_defaultmap: 7339 case OMPC_unknown: 7340 case OMPC_uniform: 7341 llvm_unreachable("Clause is not allowed."); 7342 } 7343 return Res; 7344 } 7345 7346 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 7347 ExprObjectKind OK, SourceLocation Loc) { 7348 ExprResult Res = BuildDeclRefExpr( 7349 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 7350 if (!Res.isUsable()) 7351 return ExprError(); 7352 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 7353 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 7354 if (!Res.isUsable()) 7355 return ExprError(); 7356 } 7357 if (VK != VK_LValue && Res.get()->isGLValue()) { 7358 Res = DefaultLvalueConversion(Res.get()); 7359 if (!Res.isUsable()) 7360 return ExprError(); 7361 } 7362 return Res; 7363 } 7364 7365 static std::pair<ValueDecl *, bool> 7366 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 7367 SourceRange &ERange, bool AllowArraySection = false) { 7368 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 7369 RefExpr->containsUnexpandedParameterPack()) 7370 return std::make_pair(nullptr, true); 7371 7372 // OpenMP [3.1, C/C++] 7373 // A list item is a variable name. 7374 // OpenMP [2.9.3.3, Restrictions, p.1] 7375 // A variable that is part of another variable (as an array or 7376 // structure element) cannot appear in a private clause. 7377 RefExpr = RefExpr->IgnoreParens(); 7378 enum { 7379 NoArrayExpr = -1, 7380 ArraySubscript = 0, 7381 OMPArraySection = 1 7382 } IsArrayExpr = NoArrayExpr; 7383 if (AllowArraySection) { 7384 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 7385 auto *Base = ASE->getBase()->IgnoreParenImpCasts(); 7386 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 7387 Base = TempASE->getBase()->IgnoreParenImpCasts(); 7388 RefExpr = Base; 7389 IsArrayExpr = ArraySubscript; 7390 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 7391 auto *Base = OASE->getBase()->IgnoreParenImpCasts(); 7392 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 7393 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 7394 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 7395 Base = TempASE->getBase()->IgnoreParenImpCasts(); 7396 RefExpr = Base; 7397 IsArrayExpr = OMPArraySection; 7398 } 7399 } 7400 ELoc = RefExpr->getExprLoc(); 7401 ERange = RefExpr->getSourceRange(); 7402 RefExpr = RefExpr->IgnoreParenImpCasts(); 7403 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 7404 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 7405 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 7406 (S.getCurrentThisType().isNull() || !ME || 7407 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 7408 !isa<FieldDecl>(ME->getMemberDecl()))) { 7409 if (IsArrayExpr != NoArrayExpr) 7410 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 7411 << ERange; 7412 else { 7413 S.Diag(ELoc, 7414 AllowArraySection 7415 ? diag::err_omp_expected_var_name_member_expr_or_array_item 7416 : diag::err_omp_expected_var_name_member_expr) 7417 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 7418 } 7419 return std::make_pair(nullptr, false); 7420 } 7421 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false); 7422 } 7423 7424 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 7425 SourceLocation StartLoc, 7426 SourceLocation LParenLoc, 7427 SourceLocation EndLoc) { 7428 SmallVector<Expr *, 8> Vars; 7429 SmallVector<Expr *, 8> PrivateCopies; 7430 for (auto &RefExpr : VarList) { 7431 assert(RefExpr && "NULL expr in OpenMP private clause."); 7432 SourceLocation ELoc; 7433 SourceRange ERange; 7434 Expr *SimpleRefExpr = RefExpr; 7435 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 7436 if (Res.second) { 7437 // It will be analyzed later. 7438 Vars.push_back(RefExpr); 7439 PrivateCopies.push_back(nullptr); 7440 } 7441 ValueDecl *D = Res.first; 7442 if (!D) 7443 continue; 7444 7445 QualType Type = D->getType(); 7446 auto *VD = dyn_cast<VarDecl>(D); 7447 7448 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 7449 // A variable that appears in a private clause must not have an incomplete 7450 // type or a reference type. 7451 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 7452 continue; 7453 Type = Type.getNonReferenceType(); 7454 7455 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 7456 // in a Construct] 7457 // Variables with the predetermined data-sharing attributes may not be 7458 // listed in data-sharing attributes clauses, except for the cases 7459 // listed below. For these exceptions only, listing a predetermined 7460 // variable in a data-sharing attribute clause is allowed and overrides 7461 // the variable's predetermined data-sharing attributes. 7462 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); 7463 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 7464 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 7465 << getOpenMPClauseName(OMPC_private); 7466 ReportOriginalDSA(*this, DSAStack, D, DVar); 7467 continue; 7468 } 7469 7470 auto CurrDir = DSAStack->getCurrentDirective(); 7471 // Variably modified types are not supported for tasks. 7472 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 7473 isOpenMPTaskingDirective(CurrDir)) { 7474 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 7475 << getOpenMPClauseName(OMPC_private) << Type 7476 << getOpenMPDirectiveName(CurrDir); 7477 bool IsDecl = 7478 !VD || 7479 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 7480 Diag(D->getLocation(), 7481 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 7482 << D; 7483 continue; 7484 } 7485 7486 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 7487 // A list item cannot appear in both a map clause and a data-sharing 7488 // attribute clause on the same construct 7489 if (CurrDir == OMPD_target || CurrDir == OMPD_target_parallel || 7490 CurrDir == OMPD_target_teams || 7491 CurrDir == OMPD_target_teams_distribute || 7492 CurrDir == OMPD_target_teams_distribute_parallel_for || 7493 CurrDir == OMPD_target_teams_distribute_parallel_for_simd || 7494 CurrDir == OMPD_target_teams_distribute_simd || 7495 CurrDir == OMPD_target_parallel_for_simd || 7496 CurrDir == OMPD_target_parallel_for) { 7497 OpenMPClauseKind ConflictKind; 7498 if (DSAStack->checkMappableExprComponentListsForDecl( 7499 VD, /*CurrentRegionOnly=*/true, 7500 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 7501 OpenMPClauseKind WhereFoundClauseKind) -> bool { 7502 ConflictKind = WhereFoundClauseKind; 7503 return true; 7504 })) { 7505 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 7506 << getOpenMPClauseName(OMPC_private) 7507 << getOpenMPClauseName(ConflictKind) 7508 << getOpenMPDirectiveName(CurrDir); 7509 ReportOriginalDSA(*this, DSAStack, D, DVar); 7510 continue; 7511 } 7512 } 7513 7514 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 7515 // A variable of class type (or array thereof) that appears in a private 7516 // clause requires an accessible, unambiguous default constructor for the 7517 // class type. 7518 // Generate helper private variable and initialize it with the default 7519 // value. The address of the original variable is replaced by the address of 7520 // the new private variable in CodeGen. This new variable is not added to 7521 // IdResolver, so the code in the OpenMP region uses original variable for 7522 // proper diagnostics. 7523 Type = Type.getUnqualifiedType(); 7524 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(), 7525 D->hasAttrs() ? &D->getAttrs() : nullptr); 7526 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false); 7527 if (VDPrivate->isInvalidDecl()) 7528 continue; 7529 auto VDPrivateRefExpr = buildDeclRefExpr( 7530 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 7531 7532 DeclRefExpr *Ref = nullptr; 7533 if (!VD && !CurContext->isDependentContext()) 7534 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 7535 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 7536 Vars.push_back((VD || CurContext->isDependentContext()) 7537 ? RefExpr->IgnoreParens() 7538 : Ref); 7539 PrivateCopies.push_back(VDPrivateRefExpr); 7540 } 7541 7542 if (Vars.empty()) 7543 return nullptr; 7544 7545 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 7546 PrivateCopies); 7547 } 7548 7549 namespace { 7550 class DiagsUninitializedSeveretyRAII { 7551 private: 7552 DiagnosticsEngine &Diags; 7553 SourceLocation SavedLoc; 7554 bool IsIgnored; 7555 7556 public: 7557 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 7558 bool IsIgnored) 7559 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 7560 if (!IsIgnored) { 7561 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 7562 /*Map*/ diag::Severity::Ignored, Loc); 7563 } 7564 } 7565 ~DiagsUninitializedSeveretyRAII() { 7566 if (!IsIgnored) 7567 Diags.popMappings(SavedLoc); 7568 } 7569 }; 7570 } 7571 7572 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 7573 SourceLocation StartLoc, 7574 SourceLocation LParenLoc, 7575 SourceLocation EndLoc) { 7576 SmallVector<Expr *, 8> Vars; 7577 SmallVector<Expr *, 8> PrivateCopies; 7578 SmallVector<Expr *, 8> Inits; 7579 SmallVector<Decl *, 4> ExprCaptures; 7580 bool IsImplicitClause = 7581 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 7582 auto ImplicitClauseLoc = DSAStack->getConstructLoc(); 7583 7584 for (auto &RefExpr : VarList) { 7585 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 7586 SourceLocation ELoc; 7587 SourceRange ERange; 7588 Expr *SimpleRefExpr = RefExpr; 7589 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 7590 if (Res.second) { 7591 // It will be analyzed later. 7592 Vars.push_back(RefExpr); 7593 PrivateCopies.push_back(nullptr); 7594 Inits.push_back(nullptr); 7595 } 7596 ValueDecl *D = Res.first; 7597 if (!D) 7598 continue; 7599 7600 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 7601 QualType Type = D->getType(); 7602 auto *VD = dyn_cast<VarDecl>(D); 7603 7604 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 7605 // A variable that appears in a private clause must not have an incomplete 7606 // type or a reference type. 7607 if (RequireCompleteType(ELoc, Type, 7608 diag::err_omp_firstprivate_incomplete_type)) 7609 continue; 7610 Type = Type.getNonReferenceType(); 7611 7612 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 7613 // A variable of class type (or array thereof) that appears in a private 7614 // clause requires an accessible, unambiguous copy constructor for the 7615 // class type. 7616 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 7617 7618 // If an implicit firstprivate variable found it was checked already. 7619 DSAStackTy::DSAVarData TopDVar; 7620 if (!IsImplicitClause) { 7621 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); 7622 TopDVar = DVar; 7623 bool IsConstant = ElemType.isConstant(Context); 7624 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 7625 // A list item that specifies a given variable may not appear in more 7626 // than one clause on the same directive, except that a variable may be 7627 // specified in both firstprivate and lastprivate clauses. 7628 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 7629 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) { 7630 Diag(ELoc, diag::err_omp_wrong_dsa) 7631 << getOpenMPClauseName(DVar.CKind) 7632 << getOpenMPClauseName(OMPC_firstprivate); 7633 ReportOriginalDSA(*this, DSAStack, D, DVar); 7634 continue; 7635 } 7636 7637 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 7638 // in a Construct] 7639 // Variables with the predetermined data-sharing attributes may not be 7640 // listed in data-sharing attributes clauses, except for the cases 7641 // listed below. For these exceptions only, listing a predetermined 7642 // variable in a data-sharing attribute clause is allowed and overrides 7643 // the variable's predetermined data-sharing attributes. 7644 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 7645 // in a Construct, C/C++, p.2] 7646 // Variables with const-qualified type having no mutable member may be 7647 // listed in a firstprivate clause, even if they are static data members. 7648 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 7649 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 7650 Diag(ELoc, diag::err_omp_wrong_dsa) 7651 << getOpenMPClauseName(DVar.CKind) 7652 << getOpenMPClauseName(OMPC_firstprivate); 7653 ReportOriginalDSA(*this, DSAStack, D, DVar); 7654 continue; 7655 } 7656 7657 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 7658 // OpenMP [2.9.3.4, Restrictions, p.2] 7659 // A list item that is private within a parallel region must not appear 7660 // in a firstprivate clause on a worksharing construct if any of the 7661 // worksharing regions arising from the worksharing construct ever bind 7662 // to any of the parallel regions arising from the parallel construct. 7663 if (isOpenMPWorksharingDirective(CurrDir) && 7664 !isOpenMPParallelDirective(CurrDir) && 7665 !isOpenMPTeamsDirective(CurrDir)) { 7666 DVar = DSAStack->getImplicitDSA(D, true); 7667 if (DVar.CKind != OMPC_shared && 7668 (isOpenMPParallelDirective(DVar.DKind) || 7669 DVar.DKind == OMPD_unknown)) { 7670 Diag(ELoc, diag::err_omp_required_access) 7671 << getOpenMPClauseName(OMPC_firstprivate) 7672 << getOpenMPClauseName(OMPC_shared); 7673 ReportOriginalDSA(*this, DSAStack, D, DVar); 7674 continue; 7675 } 7676 } 7677 // OpenMP [2.9.3.4, Restrictions, p.3] 7678 // A list item that appears in a reduction clause of a parallel construct 7679 // must not appear in a firstprivate clause on a worksharing or task 7680 // construct if any of the worksharing or task regions arising from the 7681 // worksharing or task construct ever bind to any of the parallel regions 7682 // arising from the parallel construct. 7683 // OpenMP [2.9.3.4, Restrictions, p.4] 7684 // A list item that appears in a reduction clause in worksharing 7685 // construct must not appear in a firstprivate clause in a task construct 7686 // encountered during execution of any of the worksharing regions arising 7687 // from the worksharing construct. 7688 if (isOpenMPTaskingDirective(CurrDir)) { 7689 DVar = DSAStack->hasInnermostDSA( 7690 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; }, 7691 [](OpenMPDirectiveKind K) -> bool { 7692 return isOpenMPParallelDirective(K) || 7693 isOpenMPWorksharingDirective(K); 7694 }, 7695 false); 7696 if (DVar.CKind == OMPC_reduction && 7697 (isOpenMPParallelDirective(DVar.DKind) || 7698 isOpenMPWorksharingDirective(DVar.DKind))) { 7699 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 7700 << getOpenMPDirectiveName(DVar.DKind); 7701 ReportOriginalDSA(*this, DSAStack, D, DVar); 7702 continue; 7703 } 7704 } 7705 7706 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 7707 // A list item that is private within a teams region must not appear in a 7708 // firstprivate clause on a distribute construct if any of the distribute 7709 // regions arising from the distribute construct ever bind to any of the 7710 // teams regions arising from the teams construct. 7711 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 7712 // A list item that appears in a reduction clause of a teams construct 7713 // must not appear in a firstprivate clause on a distribute construct if 7714 // any of the distribute regions arising from the distribute construct 7715 // ever bind to any of the teams regions arising from the teams construct. 7716 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 7717 // A list item may appear in a firstprivate or lastprivate clause but not 7718 // both. 7719 if (CurrDir == OMPD_distribute) { 7720 DVar = DSAStack->hasInnermostDSA( 7721 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; }, 7722 [](OpenMPDirectiveKind K) -> bool { 7723 return isOpenMPTeamsDirective(K); 7724 }, 7725 false); 7726 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) { 7727 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams); 7728 ReportOriginalDSA(*this, DSAStack, D, DVar); 7729 continue; 7730 } 7731 DVar = DSAStack->hasInnermostDSA( 7732 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; }, 7733 [](OpenMPDirectiveKind K) -> bool { 7734 return isOpenMPTeamsDirective(K); 7735 }, 7736 false); 7737 if (DVar.CKind == OMPC_reduction && 7738 isOpenMPTeamsDirective(DVar.DKind)) { 7739 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction); 7740 ReportOriginalDSA(*this, DSAStack, D, DVar); 7741 continue; 7742 } 7743 DVar = DSAStack->getTopDSA(D, false); 7744 if (DVar.CKind == OMPC_lastprivate) { 7745 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute); 7746 ReportOriginalDSA(*this, DSAStack, D, DVar); 7747 continue; 7748 } 7749 } 7750 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 7751 // A list item cannot appear in both a map clause and a data-sharing 7752 // attribute clause on the same construct 7753 if (CurrDir == OMPD_target || CurrDir == OMPD_target_parallel || 7754 CurrDir == OMPD_target_teams || 7755 CurrDir == OMPD_target_teams_distribute || 7756 CurrDir == OMPD_target_teams_distribute_parallel_for || 7757 CurrDir == OMPD_target_teams_distribute_parallel_for_simd || 7758 CurrDir == OMPD_target_teams_distribute_simd || 7759 CurrDir == OMPD_target_parallel_for_simd || 7760 CurrDir == OMPD_target_parallel_for) { 7761 OpenMPClauseKind ConflictKind; 7762 if (DSAStack->checkMappableExprComponentListsForDecl( 7763 VD, /*CurrentRegionOnly=*/true, 7764 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 7765 OpenMPClauseKind WhereFoundClauseKind) -> bool { 7766 ConflictKind = WhereFoundClauseKind; 7767 return true; 7768 })) { 7769 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 7770 << getOpenMPClauseName(OMPC_firstprivate) 7771 << getOpenMPClauseName(ConflictKind) 7772 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 7773 ReportOriginalDSA(*this, DSAStack, D, DVar); 7774 continue; 7775 } 7776 } 7777 } 7778 7779 // Variably modified types are not supported for tasks. 7780 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 7781 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 7782 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 7783 << getOpenMPClauseName(OMPC_firstprivate) << Type 7784 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 7785 bool IsDecl = 7786 !VD || 7787 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 7788 Diag(D->getLocation(), 7789 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 7790 << D; 7791 continue; 7792 } 7793 7794 Type = Type.getUnqualifiedType(); 7795 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(), 7796 D->hasAttrs() ? &D->getAttrs() : nullptr); 7797 // Generate helper private variable and initialize it with the value of the 7798 // original variable. The address of the original variable is replaced by 7799 // the address of the new private variable in the CodeGen. This new variable 7800 // is not added to IdResolver, so the code in the OpenMP region uses 7801 // original variable for proper diagnostics and variable capturing. 7802 Expr *VDInitRefExpr = nullptr; 7803 // For arrays generate initializer for single element and replace it by the 7804 // original array element in CodeGen. 7805 if (Type->isArrayType()) { 7806 auto VDInit = 7807 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 7808 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 7809 auto Init = DefaultLvalueConversion(VDInitRefExpr).get(); 7810 ElemType = ElemType.getUnqualifiedType(); 7811 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 7812 ".firstprivate.temp"); 7813 InitializedEntity Entity = 7814 InitializedEntity::InitializeVariable(VDInitTemp); 7815 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 7816 7817 InitializationSequence InitSeq(*this, Entity, Kind, Init); 7818 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 7819 if (Result.isInvalid()) 7820 VDPrivate->setInvalidDecl(); 7821 else 7822 VDPrivate->setInit(Result.getAs<Expr>()); 7823 // Remove temp variable declaration. 7824 Context.Deallocate(VDInitTemp); 7825 } else { 7826 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 7827 ".firstprivate.temp"); 7828 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 7829 RefExpr->getExprLoc()); 7830 AddInitializerToDecl(VDPrivate, 7831 DefaultLvalueConversion(VDInitRefExpr).get(), 7832 /*DirectInit=*/false, /*TypeMayContainAuto=*/false); 7833 } 7834 if (VDPrivate->isInvalidDecl()) { 7835 if (IsImplicitClause) { 7836 Diag(RefExpr->getExprLoc(), 7837 diag::note_omp_task_predetermined_firstprivate_here); 7838 } 7839 continue; 7840 } 7841 CurContext->addDecl(VDPrivate); 7842 auto VDPrivateRefExpr = buildDeclRefExpr( 7843 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 7844 RefExpr->getExprLoc()); 7845 DeclRefExpr *Ref = nullptr; 7846 if (!VD && !CurContext->isDependentContext()) { 7847 if (TopDVar.CKind == OMPC_lastprivate) 7848 Ref = TopDVar.PrivateCopy; 7849 else { 7850 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 7851 if (!IsOpenMPCapturedDecl(D)) 7852 ExprCaptures.push_back(Ref->getDecl()); 7853 } 7854 } 7855 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 7856 Vars.push_back((VD || CurContext->isDependentContext()) 7857 ? RefExpr->IgnoreParens() 7858 : Ref); 7859 PrivateCopies.push_back(VDPrivateRefExpr); 7860 Inits.push_back(VDInitRefExpr); 7861 } 7862 7863 if (Vars.empty()) 7864 return nullptr; 7865 7866 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 7867 Vars, PrivateCopies, Inits, 7868 buildPreInits(Context, ExprCaptures)); 7869 } 7870 7871 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList, 7872 SourceLocation StartLoc, 7873 SourceLocation LParenLoc, 7874 SourceLocation EndLoc) { 7875 SmallVector<Expr *, 8> Vars; 7876 SmallVector<Expr *, 8> SrcExprs; 7877 SmallVector<Expr *, 8> DstExprs; 7878 SmallVector<Expr *, 8> AssignmentOps; 7879 SmallVector<Decl *, 4> ExprCaptures; 7880 SmallVector<Expr *, 4> ExprPostUpdates; 7881 for (auto &RefExpr : VarList) { 7882 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 7883 SourceLocation ELoc; 7884 SourceRange ERange; 7885 Expr *SimpleRefExpr = RefExpr; 7886 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 7887 if (Res.second) { 7888 // It will be analyzed later. 7889 Vars.push_back(RefExpr); 7890 SrcExprs.push_back(nullptr); 7891 DstExprs.push_back(nullptr); 7892 AssignmentOps.push_back(nullptr); 7893 } 7894 ValueDecl *D = Res.first; 7895 if (!D) 7896 continue; 7897 7898 QualType Type = D->getType(); 7899 auto *VD = dyn_cast<VarDecl>(D); 7900 7901 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 7902 // A variable that appears in a lastprivate clause must not have an 7903 // incomplete type or a reference type. 7904 if (RequireCompleteType(ELoc, Type, 7905 diag::err_omp_lastprivate_incomplete_type)) 7906 continue; 7907 Type = Type.getNonReferenceType(); 7908 7909 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 7910 // in a Construct] 7911 // Variables with the predetermined data-sharing attributes may not be 7912 // listed in data-sharing attributes clauses, except for the cases 7913 // listed below. 7914 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); 7915 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 7916 DVar.CKind != OMPC_firstprivate && 7917 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 7918 Diag(ELoc, diag::err_omp_wrong_dsa) 7919 << getOpenMPClauseName(DVar.CKind) 7920 << getOpenMPClauseName(OMPC_lastprivate); 7921 ReportOriginalDSA(*this, DSAStack, D, DVar); 7922 continue; 7923 } 7924 7925 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 7926 // OpenMP [2.14.3.5, Restrictions, p.2] 7927 // A list item that is private within a parallel region, or that appears in 7928 // the reduction clause of a parallel construct, must not appear in a 7929 // lastprivate clause on a worksharing construct if any of the corresponding 7930 // worksharing regions ever binds to any of the corresponding parallel 7931 // regions. 7932 DSAStackTy::DSAVarData TopDVar = DVar; 7933 if (isOpenMPWorksharingDirective(CurrDir) && 7934 !isOpenMPParallelDirective(CurrDir) && 7935 !isOpenMPTeamsDirective(CurrDir)) { 7936 DVar = DSAStack->getImplicitDSA(D, true); 7937 if (DVar.CKind != OMPC_shared) { 7938 Diag(ELoc, diag::err_omp_required_access) 7939 << getOpenMPClauseName(OMPC_lastprivate) 7940 << getOpenMPClauseName(OMPC_shared); 7941 ReportOriginalDSA(*this, DSAStack, D, DVar); 7942 continue; 7943 } 7944 } 7945 7946 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 7947 // A list item may appear in a firstprivate or lastprivate clause but not 7948 // both. 7949 if (CurrDir == OMPD_distribute) { 7950 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); 7951 if (DVar.CKind == OMPC_firstprivate) { 7952 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute); 7953 ReportOriginalDSA(*this, DSAStack, D, DVar); 7954 continue; 7955 } 7956 } 7957 7958 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 7959 // A variable of class type (or array thereof) that appears in a 7960 // lastprivate clause requires an accessible, unambiguous default 7961 // constructor for the class type, unless the list item is also specified 7962 // in a firstprivate clause. 7963 // A variable of class type (or array thereof) that appears in a 7964 // lastprivate clause requires an accessible, unambiguous copy assignment 7965 // operator for the class type. 7966 Type = Context.getBaseElementType(Type).getNonReferenceType(); 7967 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(), 7968 Type.getUnqualifiedType(), ".lastprivate.src", 7969 D->hasAttrs() ? &D->getAttrs() : nullptr); 7970 auto *PseudoSrcExpr = 7971 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 7972 auto *DstVD = 7973 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 7974 D->hasAttrs() ? &D->getAttrs() : nullptr); 7975 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 7976 // For arrays generate assignment operation for single element and replace 7977 // it by the original array element in CodeGen. 7978 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 7979 PseudoDstExpr, PseudoSrcExpr); 7980 if (AssignmentOp.isInvalid()) 7981 continue; 7982 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc, 7983 /*DiscardedValue=*/true); 7984 if (AssignmentOp.isInvalid()) 7985 continue; 7986 7987 DeclRefExpr *Ref = nullptr; 7988 if (!VD && !CurContext->isDependentContext()) { 7989 if (TopDVar.CKind == OMPC_firstprivate) 7990 Ref = TopDVar.PrivateCopy; 7991 else { 7992 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 7993 if (!IsOpenMPCapturedDecl(D)) 7994 ExprCaptures.push_back(Ref->getDecl()); 7995 } 7996 if (TopDVar.CKind == OMPC_firstprivate || 7997 (!IsOpenMPCapturedDecl(D) && 7998 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 7999 ExprResult RefRes = DefaultLvalueConversion(Ref); 8000 if (!RefRes.isUsable()) 8001 continue; 8002 ExprResult PostUpdateRes = 8003 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 8004 RefRes.get()); 8005 if (!PostUpdateRes.isUsable()) 8006 continue; 8007 ExprPostUpdates.push_back( 8008 IgnoredValueConversions(PostUpdateRes.get()).get()); 8009 } 8010 } 8011 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 8012 Vars.push_back((VD || CurContext->isDependentContext()) 8013 ? RefExpr->IgnoreParens() 8014 : Ref); 8015 SrcExprs.push_back(PseudoSrcExpr); 8016 DstExprs.push_back(PseudoDstExpr); 8017 AssignmentOps.push_back(AssignmentOp.get()); 8018 } 8019 8020 if (Vars.empty()) 8021 return nullptr; 8022 8023 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 8024 Vars, SrcExprs, DstExprs, AssignmentOps, 8025 buildPreInits(Context, ExprCaptures), 8026 buildPostUpdate(*this, ExprPostUpdates)); 8027 } 8028 8029 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 8030 SourceLocation StartLoc, 8031 SourceLocation LParenLoc, 8032 SourceLocation EndLoc) { 8033 SmallVector<Expr *, 8> Vars; 8034 for (auto &RefExpr : VarList) { 8035 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 8036 SourceLocation ELoc; 8037 SourceRange ERange; 8038 Expr *SimpleRefExpr = RefExpr; 8039 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 8040 if (Res.second) { 8041 // It will be analyzed later. 8042 Vars.push_back(RefExpr); 8043 } 8044 ValueDecl *D = Res.first; 8045 if (!D) 8046 continue; 8047 8048 auto *VD = dyn_cast<VarDecl>(D); 8049 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 8050 // in a Construct] 8051 // Variables with the predetermined data-sharing attributes may not be 8052 // listed in data-sharing attributes clauses, except for the cases 8053 // listed below. For these exceptions only, listing a predetermined 8054 // variable in a data-sharing attribute clause is allowed and overrides 8055 // the variable's predetermined data-sharing attributes. 8056 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); 8057 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 8058 DVar.RefExpr) { 8059 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 8060 << getOpenMPClauseName(OMPC_shared); 8061 ReportOriginalDSA(*this, DSAStack, D, DVar); 8062 continue; 8063 } 8064 8065 DeclRefExpr *Ref = nullptr; 8066 if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 8067 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 8068 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 8069 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 8070 ? RefExpr->IgnoreParens() 8071 : Ref); 8072 } 8073 8074 if (Vars.empty()) 8075 return nullptr; 8076 8077 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 8078 } 8079 8080 namespace { 8081 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 8082 DSAStackTy *Stack; 8083 8084 public: 8085 bool VisitDeclRefExpr(DeclRefExpr *E) { 8086 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) { 8087 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false); 8088 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 8089 return false; 8090 if (DVar.CKind != OMPC_unknown) 8091 return true; 8092 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 8093 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; }, 8094 false); 8095 if (DVarPrivate.CKind != OMPC_unknown) 8096 return true; 8097 return false; 8098 } 8099 return false; 8100 } 8101 bool VisitStmt(Stmt *S) { 8102 for (auto Child : S->children()) { 8103 if (Child && Visit(Child)) 8104 return true; 8105 } 8106 return false; 8107 } 8108 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 8109 }; 8110 } // namespace 8111 8112 namespace { 8113 // Transform MemberExpression for specified FieldDecl of current class to 8114 // DeclRefExpr to specified OMPCapturedExprDecl. 8115 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 8116 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 8117 ValueDecl *Field; 8118 DeclRefExpr *CapturedExpr; 8119 8120 public: 8121 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 8122 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 8123 8124 ExprResult TransformMemberExpr(MemberExpr *E) { 8125 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 8126 E->getMemberDecl() == Field) { 8127 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 8128 return CapturedExpr; 8129 } 8130 return BaseTransform::TransformMemberExpr(E); 8131 } 8132 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 8133 }; 8134 } // namespace 8135 8136 template <typename T> 8137 static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups, 8138 const llvm::function_ref<T(ValueDecl *)> &Gen) { 8139 for (auto &Set : Lookups) { 8140 for (auto *D : Set) { 8141 if (auto Res = Gen(cast<ValueDecl>(D))) 8142 return Res; 8143 } 8144 } 8145 return T(); 8146 } 8147 8148 static ExprResult 8149 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 8150 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 8151 const DeclarationNameInfo &ReductionId, QualType Ty, 8152 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 8153 if (ReductionIdScopeSpec.isInvalid()) 8154 return ExprError(); 8155 SmallVector<UnresolvedSet<8>, 4> Lookups; 8156 if (S) { 8157 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 8158 Lookup.suppressDiagnostics(); 8159 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 8160 auto *D = Lookup.getRepresentativeDecl(); 8161 do { 8162 S = S->getParent(); 8163 } while (S && !S->isDeclScope(D)); 8164 if (S) 8165 S = S->getParent(); 8166 Lookups.push_back(UnresolvedSet<8>()); 8167 Lookups.back().append(Lookup.begin(), Lookup.end()); 8168 Lookup.clear(); 8169 } 8170 } else if (auto *ULE = 8171 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 8172 Lookups.push_back(UnresolvedSet<8>()); 8173 Decl *PrevD = nullptr; 8174 for (auto *D : ULE->decls()) { 8175 if (D == PrevD) 8176 Lookups.push_back(UnresolvedSet<8>()); 8177 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D)) 8178 Lookups.back().addDecl(DRD); 8179 PrevD = D; 8180 } 8181 } 8182 if (Ty->isDependentType() || Ty->isInstantiationDependentType() || 8183 Ty->containsUnexpandedParameterPack() || 8184 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool { 8185 return !D->isInvalidDecl() && 8186 (D->getType()->isDependentType() || 8187 D->getType()->isInstantiationDependentType() || 8188 D->getType()->containsUnexpandedParameterPack()); 8189 })) { 8190 UnresolvedSet<8> ResSet; 8191 for (auto &Set : Lookups) { 8192 ResSet.append(Set.begin(), Set.end()); 8193 // The last item marks the end of all declarations at the specified scope. 8194 ResSet.addDecl(Set[Set.size() - 1]); 8195 } 8196 return UnresolvedLookupExpr::Create( 8197 SemaRef.Context, /*NamingClass=*/nullptr, 8198 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 8199 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 8200 } 8201 if (auto *VD = filterLookupForUDR<ValueDecl *>( 8202 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 8203 if (!D->isInvalidDecl() && 8204 SemaRef.Context.hasSameType(D->getType(), Ty)) 8205 return D; 8206 return nullptr; 8207 })) 8208 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc); 8209 if (auto *VD = filterLookupForUDR<ValueDecl *>( 8210 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 8211 if (!D->isInvalidDecl() && 8212 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 8213 !Ty.isMoreQualifiedThan(D->getType())) 8214 return D; 8215 return nullptr; 8216 })) { 8217 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 8218 /*DetectVirtual=*/false); 8219 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 8220 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 8221 VD->getType().getUnqualifiedType()))) { 8222 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(), 8223 /*DiagID=*/0) != 8224 Sema::AR_inaccessible) { 8225 SemaRef.BuildBasePathArray(Paths, BasePath); 8226 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc); 8227 } 8228 } 8229 } 8230 } 8231 if (ReductionIdScopeSpec.isSet()) { 8232 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range; 8233 return ExprError(); 8234 } 8235 return ExprEmpty(); 8236 } 8237 8238 OMPClause *Sema::ActOnOpenMPReductionClause( 8239 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 8240 SourceLocation ColonLoc, SourceLocation EndLoc, 8241 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 8242 ArrayRef<Expr *> UnresolvedReductions) { 8243 auto DN = ReductionId.getName(); 8244 auto OOK = DN.getCXXOverloadedOperator(); 8245 BinaryOperatorKind BOK = BO_Comma; 8246 8247 // OpenMP [2.14.3.6, reduction clause] 8248 // C 8249 // reduction-identifier is either an identifier or one of the following 8250 // operators: +, -, *, &, |, ^, && and || 8251 // C++ 8252 // reduction-identifier is either an id-expression or one of the following 8253 // operators: +, -, *, &, |, ^, && and || 8254 // FIXME: Only 'min' and 'max' identifiers are supported for now. 8255 switch (OOK) { 8256 case OO_Plus: 8257 case OO_Minus: 8258 BOK = BO_Add; 8259 break; 8260 case OO_Star: 8261 BOK = BO_Mul; 8262 break; 8263 case OO_Amp: 8264 BOK = BO_And; 8265 break; 8266 case OO_Pipe: 8267 BOK = BO_Or; 8268 break; 8269 case OO_Caret: 8270 BOK = BO_Xor; 8271 break; 8272 case OO_AmpAmp: 8273 BOK = BO_LAnd; 8274 break; 8275 case OO_PipePipe: 8276 BOK = BO_LOr; 8277 break; 8278 case OO_New: 8279 case OO_Delete: 8280 case OO_Array_New: 8281 case OO_Array_Delete: 8282 case OO_Slash: 8283 case OO_Percent: 8284 case OO_Tilde: 8285 case OO_Exclaim: 8286 case OO_Equal: 8287 case OO_Less: 8288 case OO_Greater: 8289 case OO_LessEqual: 8290 case OO_GreaterEqual: 8291 case OO_PlusEqual: 8292 case OO_MinusEqual: 8293 case OO_StarEqual: 8294 case OO_SlashEqual: 8295 case OO_PercentEqual: 8296 case OO_CaretEqual: 8297 case OO_AmpEqual: 8298 case OO_PipeEqual: 8299 case OO_LessLess: 8300 case OO_GreaterGreater: 8301 case OO_LessLessEqual: 8302 case OO_GreaterGreaterEqual: 8303 case OO_EqualEqual: 8304 case OO_ExclaimEqual: 8305 case OO_PlusPlus: 8306 case OO_MinusMinus: 8307 case OO_Comma: 8308 case OO_ArrowStar: 8309 case OO_Arrow: 8310 case OO_Call: 8311 case OO_Subscript: 8312 case OO_Conditional: 8313 case OO_Coawait: 8314 case NUM_OVERLOADED_OPERATORS: 8315 llvm_unreachable("Unexpected reduction identifier"); 8316 case OO_None: 8317 if (auto II = DN.getAsIdentifierInfo()) { 8318 if (II->isStr("max")) 8319 BOK = BO_GT; 8320 else if (II->isStr("min")) 8321 BOK = BO_LT; 8322 } 8323 break; 8324 } 8325 SourceRange ReductionIdRange; 8326 if (ReductionIdScopeSpec.isValid()) 8327 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 8328 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 8329 8330 SmallVector<Expr *, 8> Vars; 8331 SmallVector<Expr *, 8> Privates; 8332 SmallVector<Expr *, 8> LHSs; 8333 SmallVector<Expr *, 8> RHSs; 8334 SmallVector<Expr *, 8> ReductionOps; 8335 SmallVector<Decl *, 4> ExprCaptures; 8336 SmallVector<Expr *, 4> ExprPostUpdates; 8337 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 8338 bool FirstIter = true; 8339 for (auto RefExpr : VarList) { 8340 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 8341 // OpenMP [2.1, C/C++] 8342 // A list item is a variable or array section, subject to the restrictions 8343 // specified in Section 2.4 on page 42 and in each of the sections 8344 // describing clauses and directives for which a list appears. 8345 // OpenMP [2.14.3.3, Restrictions, p.1] 8346 // A variable that is part of another variable (as an array or 8347 // structure element) cannot appear in a private clause. 8348 if (!FirstIter && IR != ER) 8349 ++IR; 8350 FirstIter = false; 8351 SourceLocation ELoc; 8352 SourceRange ERange; 8353 Expr *SimpleRefExpr = RefExpr; 8354 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 8355 /*AllowArraySection=*/true); 8356 if (Res.second) { 8357 // It will be analyzed later. 8358 Vars.push_back(RefExpr); 8359 Privates.push_back(nullptr); 8360 LHSs.push_back(nullptr); 8361 RHSs.push_back(nullptr); 8362 // Try to find 'declare reduction' corresponding construct before using 8363 // builtin/overloaded operators. 8364 QualType Type = Context.DependentTy; 8365 CXXCastPath BasePath; 8366 ExprResult DeclareReductionRef = buildDeclareReductionRef( 8367 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec, 8368 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 8369 if (CurContext->isDependentContext() && 8370 (DeclareReductionRef.isUnset() || 8371 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 8372 ReductionOps.push_back(DeclareReductionRef.get()); 8373 else 8374 ReductionOps.push_back(nullptr); 8375 } 8376 ValueDecl *D = Res.first; 8377 if (!D) 8378 continue; 8379 8380 QualType Type; 8381 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 8382 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 8383 if (ASE) 8384 Type = ASE->getType().getNonReferenceType(); 8385 else if (OASE) { 8386 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 8387 if (auto *ATy = BaseType->getAsArrayTypeUnsafe()) 8388 Type = ATy->getElementType(); 8389 else 8390 Type = BaseType->getPointeeType(); 8391 Type = Type.getNonReferenceType(); 8392 } else 8393 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 8394 auto *VD = dyn_cast<VarDecl>(D); 8395 8396 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 8397 // A variable that appears in a private clause must not have an incomplete 8398 // type or a reference type. 8399 if (RequireCompleteType(ELoc, Type, 8400 diag::err_omp_reduction_incomplete_type)) 8401 continue; 8402 // OpenMP [2.14.3.6, reduction clause, Restrictions] 8403 // A list item that appears in a reduction clause must not be 8404 // const-qualified. 8405 if (Type.getNonReferenceType().isConstant(Context)) { 8406 Diag(ELoc, diag::err_omp_const_reduction_list_item) 8407 << getOpenMPClauseName(OMPC_reduction) << Type << ERange; 8408 if (!ASE && !OASE) { 8409 bool IsDecl = !VD || 8410 VD->isThisDeclarationADefinition(Context) == 8411 VarDecl::DeclarationOnly; 8412 Diag(D->getLocation(), 8413 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 8414 << D; 8415 } 8416 continue; 8417 } 8418 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 8419 // If a list-item is a reference type then it must bind to the same object 8420 // for all threads of the team. 8421 if (!ASE && !OASE && VD) { 8422 VarDecl *VDDef = VD->getDefinition(); 8423 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 8424 DSARefChecker Check(DSAStack); 8425 if (Check.Visit(VDDef->getInit())) { 8426 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange; 8427 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 8428 continue; 8429 } 8430 } 8431 } 8432 8433 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 8434 // in a Construct] 8435 // Variables with the predetermined data-sharing attributes may not be 8436 // listed in data-sharing attributes clauses, except for the cases 8437 // listed below. For these exceptions only, listing a predetermined 8438 // variable in a data-sharing attribute clause is allowed and overrides 8439 // the variable's predetermined data-sharing attributes. 8440 // OpenMP [2.14.3.6, Restrictions, p.3] 8441 // Any number of reduction clauses can be specified on the directive, 8442 // but a list item can appear only once in the reduction clauses for that 8443 // directive. 8444 DSAStackTy::DSAVarData DVar; 8445 DVar = DSAStack->getTopDSA(D, false); 8446 if (DVar.CKind == OMPC_reduction) { 8447 Diag(ELoc, diag::err_omp_once_referenced) 8448 << getOpenMPClauseName(OMPC_reduction); 8449 if (DVar.RefExpr) 8450 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 8451 } else if (DVar.CKind != OMPC_unknown) { 8452 Diag(ELoc, diag::err_omp_wrong_dsa) 8453 << getOpenMPClauseName(DVar.CKind) 8454 << getOpenMPClauseName(OMPC_reduction); 8455 ReportOriginalDSA(*this, DSAStack, D, DVar); 8456 continue; 8457 } 8458 8459 // OpenMP [2.14.3.6, Restrictions, p.1] 8460 // A list item that appears in a reduction clause of a worksharing 8461 // construct must be shared in the parallel regions to which any of the 8462 // worksharing regions arising from the worksharing construct bind. 8463 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 8464 if (isOpenMPWorksharingDirective(CurrDir) && 8465 !isOpenMPParallelDirective(CurrDir) && 8466 !isOpenMPTeamsDirective(CurrDir)) { 8467 DVar = DSAStack->getImplicitDSA(D, true); 8468 if (DVar.CKind != OMPC_shared) { 8469 Diag(ELoc, diag::err_omp_required_access) 8470 << getOpenMPClauseName(OMPC_reduction) 8471 << getOpenMPClauseName(OMPC_shared); 8472 ReportOriginalDSA(*this, DSAStack, D, DVar); 8473 continue; 8474 } 8475 } 8476 8477 // Try to find 'declare reduction' corresponding construct before using 8478 // builtin/overloaded operators. 8479 CXXCastPath BasePath; 8480 ExprResult DeclareReductionRef = buildDeclareReductionRef( 8481 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec, 8482 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 8483 if (DeclareReductionRef.isInvalid()) 8484 continue; 8485 if (CurContext->isDependentContext() && 8486 (DeclareReductionRef.isUnset() || 8487 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 8488 Vars.push_back(RefExpr); 8489 Privates.push_back(nullptr); 8490 LHSs.push_back(nullptr); 8491 RHSs.push_back(nullptr); 8492 ReductionOps.push_back(DeclareReductionRef.get()); 8493 continue; 8494 } 8495 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 8496 // Not allowed reduction identifier is found. 8497 Diag(ReductionId.getLocStart(), 8498 diag::err_omp_unknown_reduction_identifier) 8499 << Type << ReductionIdRange; 8500 continue; 8501 } 8502 8503 // OpenMP [2.14.3.6, reduction clause, Restrictions] 8504 // The type of a list item that appears in a reduction clause must be valid 8505 // for the reduction-identifier. For a max or min reduction in C, the type 8506 // of the list item must be an allowed arithmetic data type: char, int, 8507 // float, double, or _Bool, possibly modified with long, short, signed, or 8508 // unsigned. For a max or min reduction in C++, the type of the list item 8509 // must be an allowed arithmetic data type: char, wchar_t, int, float, 8510 // double, or bool, possibly modified with long, short, signed, or unsigned. 8511 if (DeclareReductionRef.isUnset()) { 8512 if ((BOK == BO_GT || BOK == BO_LT) && 8513 !(Type->isScalarType() || 8514 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 8515 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 8516 << getLangOpts().CPlusPlus; 8517 if (!ASE && !OASE) { 8518 bool IsDecl = !VD || 8519 VD->isThisDeclarationADefinition(Context) == 8520 VarDecl::DeclarationOnly; 8521 Diag(D->getLocation(), 8522 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 8523 << D; 8524 } 8525 continue; 8526 } 8527 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 8528 !getLangOpts().CPlusPlus && Type->isFloatingType()) { 8529 Diag(ELoc, diag::err_omp_clause_floating_type_arg); 8530 if (!ASE && !OASE) { 8531 bool IsDecl = !VD || 8532 VD->isThisDeclarationADefinition(Context) == 8533 VarDecl::DeclarationOnly; 8534 Diag(D->getLocation(), 8535 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 8536 << D; 8537 } 8538 continue; 8539 } 8540 } 8541 8542 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 8543 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs", 8544 D->hasAttrs() ? &D->getAttrs() : nullptr); 8545 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(), 8546 D->hasAttrs() ? &D->getAttrs() : nullptr); 8547 auto PrivateTy = Type; 8548 if (OASE || 8549 (!ASE && 8550 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 8551 // For arrays/array sections only: 8552 // Create pseudo array type for private copy. The size for this array will 8553 // be generated during codegen. 8554 // For array subscripts or single variables Private Ty is the same as Type 8555 // (type of the variable or single array element). 8556 PrivateTy = Context.getVariableArrayType( 8557 Type, new (Context) OpaqueValueExpr(SourceLocation(), 8558 Context.getSizeType(), VK_RValue), 8559 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 8560 } else if (!ASE && !OASE && 8561 Context.getAsArrayType(D->getType().getNonReferenceType())) 8562 PrivateTy = D->getType().getNonReferenceType(); 8563 // Private copy. 8564 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(), 8565 D->hasAttrs() ? &D->getAttrs() : nullptr); 8566 // Add initializer for private variable. 8567 Expr *Init = nullptr; 8568 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc); 8569 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc); 8570 if (DeclareReductionRef.isUsable()) { 8571 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 8572 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 8573 if (DRD->getInitializer()) { 8574 Init = DRDRef; 8575 RHSVD->setInit(DRDRef); 8576 RHSVD->setInitStyle(VarDecl::CallInit); 8577 } 8578 } else { 8579 switch (BOK) { 8580 case BO_Add: 8581 case BO_Xor: 8582 case BO_Or: 8583 case BO_LOr: 8584 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 8585 if (Type->isScalarType() || Type->isAnyComplexType()) 8586 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 8587 break; 8588 case BO_Mul: 8589 case BO_LAnd: 8590 if (Type->isScalarType() || Type->isAnyComplexType()) { 8591 // '*' and '&&' reduction ops - initializer is '1'. 8592 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 8593 } 8594 break; 8595 case BO_And: { 8596 // '&' reduction op - initializer is '~0'. 8597 QualType OrigType = Type; 8598 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 8599 Type = ComplexTy->getElementType(); 8600 if (Type->isRealFloatingType()) { 8601 llvm::APFloat InitValue = 8602 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type), 8603 /*isIEEE=*/true); 8604 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 8605 Type, ELoc); 8606 } else if (Type->isScalarType()) { 8607 auto Size = Context.getTypeSize(Type); 8608 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 8609 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 8610 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 8611 } 8612 if (Init && OrigType->isAnyComplexType()) { 8613 // Init = 0xFFFF + 0xFFFFi; 8614 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 8615 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 8616 } 8617 Type = OrigType; 8618 break; 8619 } 8620 case BO_LT: 8621 case BO_GT: { 8622 // 'min' reduction op - initializer is 'Largest representable number in 8623 // the reduction list item type'. 8624 // 'max' reduction op - initializer is 'Least representable number in 8625 // the reduction list item type'. 8626 if (Type->isIntegerType() || Type->isPointerType()) { 8627 bool IsSigned = Type->hasSignedIntegerRepresentation(); 8628 auto Size = Context.getTypeSize(Type); 8629 QualType IntTy = 8630 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 8631 llvm::APInt InitValue = 8632 (BOK != BO_LT) 8633 ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 8634 : llvm::APInt::getMinValue(Size) 8635 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 8636 : llvm::APInt::getMaxValue(Size); 8637 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 8638 if (Type->isPointerType()) { 8639 // Cast to pointer type. 8640 auto CastExpr = BuildCStyleCastExpr( 8641 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc), 8642 SourceLocation(), Init); 8643 if (CastExpr.isInvalid()) 8644 continue; 8645 Init = CastExpr.get(); 8646 } 8647 } else if (Type->isRealFloatingType()) { 8648 llvm::APFloat InitValue = llvm::APFloat::getLargest( 8649 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 8650 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 8651 Type, ELoc); 8652 } 8653 break; 8654 } 8655 case BO_PtrMemD: 8656 case BO_PtrMemI: 8657 case BO_MulAssign: 8658 case BO_Div: 8659 case BO_Rem: 8660 case BO_Sub: 8661 case BO_Shl: 8662 case BO_Shr: 8663 case BO_LE: 8664 case BO_GE: 8665 case BO_EQ: 8666 case BO_NE: 8667 case BO_AndAssign: 8668 case BO_XorAssign: 8669 case BO_OrAssign: 8670 case BO_Assign: 8671 case BO_AddAssign: 8672 case BO_SubAssign: 8673 case BO_DivAssign: 8674 case BO_RemAssign: 8675 case BO_ShlAssign: 8676 case BO_ShrAssign: 8677 case BO_Comma: 8678 llvm_unreachable("Unexpected reduction operation"); 8679 } 8680 } 8681 if (Init && DeclareReductionRef.isUnset()) { 8682 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false, 8683 /*TypeMayContainAuto=*/false); 8684 } else if (!Init) 8685 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false); 8686 if (RHSVD->isInvalidDecl()) 8687 continue; 8688 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) { 8689 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type 8690 << ReductionIdRange; 8691 bool IsDecl = 8692 !VD || 8693 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 8694 Diag(D->getLocation(), 8695 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 8696 << D; 8697 continue; 8698 } 8699 // Store initializer for single element in private copy. Will be used during 8700 // codegen. 8701 PrivateVD->setInit(RHSVD->getInit()); 8702 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 8703 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc); 8704 ExprResult ReductionOp; 8705 if (DeclareReductionRef.isUsable()) { 8706 QualType RedTy = DeclareReductionRef.get()->getType(); 8707 QualType PtrRedTy = Context.getPointerType(RedTy); 8708 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 8709 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 8710 if (!BasePath.empty()) { 8711 LHS = DefaultLvalueConversion(LHS.get()); 8712 RHS = DefaultLvalueConversion(RHS.get()); 8713 LHS = ImplicitCastExpr::Create(Context, PtrRedTy, 8714 CK_UncheckedDerivedToBase, LHS.get(), 8715 &BasePath, LHS.get()->getValueKind()); 8716 RHS = ImplicitCastExpr::Create(Context, PtrRedTy, 8717 CK_UncheckedDerivedToBase, RHS.get(), 8718 &BasePath, RHS.get()->getValueKind()); 8719 } 8720 FunctionProtoType::ExtProtoInfo EPI; 8721 QualType Params[] = {PtrRedTy, PtrRedTy}; 8722 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 8723 auto *OVE = new (Context) OpaqueValueExpr( 8724 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, 8725 DefaultLvalueConversion(DeclareReductionRef.get()).get()); 8726 Expr *Args[] = {LHS.get(), RHS.get()}; 8727 ReductionOp = new (Context) 8728 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc); 8729 } else { 8730 ReductionOp = BuildBinOp(DSAStack->getCurScope(), 8731 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE); 8732 if (ReductionOp.isUsable()) { 8733 if (BOK != BO_LT && BOK != BO_GT) { 8734 ReductionOp = 8735 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), 8736 BO_Assign, LHSDRE, ReductionOp.get()); 8737 } else { 8738 auto *ConditionalOp = new (Context) ConditionalOperator( 8739 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(), 8740 RHSDRE, Type, VK_LValue, OK_Ordinary); 8741 ReductionOp = 8742 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), 8743 BO_Assign, LHSDRE, ConditionalOp); 8744 } 8745 ReductionOp = ActOnFinishFullExpr(ReductionOp.get()); 8746 } 8747 if (ReductionOp.isInvalid()) 8748 continue; 8749 } 8750 8751 DeclRefExpr *Ref = nullptr; 8752 Expr *VarsExpr = RefExpr->IgnoreParens(); 8753 if (!VD && !CurContext->isDependentContext()) { 8754 if (ASE || OASE) { 8755 TransformExprToCaptures RebuildToCapture(*this, D); 8756 VarsExpr = 8757 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 8758 Ref = RebuildToCapture.getCapturedExpr(); 8759 } else { 8760 VarsExpr = Ref = 8761 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 8762 } 8763 if (!IsOpenMPCapturedDecl(D)) { 8764 ExprCaptures.push_back(Ref->getDecl()); 8765 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 8766 ExprResult RefRes = DefaultLvalueConversion(Ref); 8767 if (!RefRes.isUsable()) 8768 continue; 8769 ExprResult PostUpdateRes = 8770 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 8771 SimpleRefExpr, RefRes.get()); 8772 if (!PostUpdateRes.isUsable()) 8773 continue; 8774 ExprPostUpdates.push_back( 8775 IgnoredValueConversions(PostUpdateRes.get()).get()); 8776 } 8777 } 8778 } 8779 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref); 8780 Vars.push_back(VarsExpr); 8781 Privates.push_back(PrivateDRE); 8782 LHSs.push_back(LHSDRE); 8783 RHSs.push_back(RHSDRE); 8784 ReductionOps.push_back(ReductionOp.get()); 8785 } 8786 8787 if (Vars.empty()) 8788 return nullptr; 8789 8790 return OMPReductionClause::Create( 8791 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars, 8792 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates, 8793 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures), 8794 buildPostUpdate(*this, ExprPostUpdates)); 8795 } 8796 8797 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 8798 SourceLocation LinLoc) { 8799 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 8800 LinKind == OMPC_LINEAR_unknown) { 8801 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 8802 return true; 8803 } 8804 return false; 8805 } 8806 8807 bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc, 8808 OpenMPLinearClauseKind LinKind, 8809 QualType Type) { 8810 auto *VD = dyn_cast_or_null<VarDecl>(D); 8811 // A variable must not have an incomplete type or a reference type. 8812 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 8813 return true; 8814 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 8815 !Type->isReferenceType()) { 8816 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 8817 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 8818 return true; 8819 } 8820 Type = Type.getNonReferenceType(); 8821 8822 // A list item must not be const-qualified. 8823 if (Type.isConstant(Context)) { 8824 Diag(ELoc, diag::err_omp_const_variable) 8825 << getOpenMPClauseName(OMPC_linear); 8826 if (D) { 8827 bool IsDecl = 8828 !VD || 8829 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 8830 Diag(D->getLocation(), 8831 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 8832 << D; 8833 } 8834 return true; 8835 } 8836 8837 // A list item must be of integral or pointer type. 8838 Type = Type.getUnqualifiedType().getCanonicalType(); 8839 const auto *Ty = Type.getTypePtrOrNull(); 8840 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && 8841 !Ty->isPointerType())) { 8842 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 8843 if (D) { 8844 bool IsDecl = 8845 !VD || 8846 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 8847 Diag(D->getLocation(), 8848 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 8849 << D; 8850 } 8851 return true; 8852 } 8853 return false; 8854 } 8855 8856 OMPClause *Sema::ActOnOpenMPLinearClause( 8857 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 8858 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 8859 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 8860 SmallVector<Expr *, 8> Vars; 8861 SmallVector<Expr *, 8> Privates; 8862 SmallVector<Expr *, 8> Inits; 8863 SmallVector<Decl *, 4> ExprCaptures; 8864 SmallVector<Expr *, 4> ExprPostUpdates; 8865 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 8866 LinKind = OMPC_LINEAR_val; 8867 for (auto &RefExpr : VarList) { 8868 assert(RefExpr && "NULL expr in OpenMP linear clause."); 8869 SourceLocation ELoc; 8870 SourceRange ERange; 8871 Expr *SimpleRefExpr = RefExpr; 8872 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 8873 /*AllowArraySection=*/false); 8874 if (Res.second) { 8875 // It will be analyzed later. 8876 Vars.push_back(RefExpr); 8877 Privates.push_back(nullptr); 8878 Inits.push_back(nullptr); 8879 } 8880 ValueDecl *D = Res.first; 8881 if (!D) 8882 continue; 8883 8884 QualType Type = D->getType(); 8885 auto *VD = dyn_cast<VarDecl>(D); 8886 8887 // OpenMP [2.14.3.7, linear clause] 8888 // A list-item cannot appear in more than one linear clause. 8889 // A list-item that appears in a linear clause cannot appear in any 8890 // other data-sharing attribute clause. 8891 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false); 8892 if (DVar.RefExpr) { 8893 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 8894 << getOpenMPClauseName(OMPC_linear); 8895 ReportOriginalDSA(*this, DSAStack, D, DVar); 8896 continue; 8897 } 8898 8899 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 8900 continue; 8901 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 8902 8903 // Build private copy of original var. 8904 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(), 8905 D->hasAttrs() ? &D->getAttrs() : nullptr); 8906 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 8907 // Build var to save initial value. 8908 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 8909 Expr *InitExpr; 8910 DeclRefExpr *Ref = nullptr; 8911 if (!VD && !CurContext->isDependentContext()) { 8912 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 8913 if (!IsOpenMPCapturedDecl(D)) { 8914 ExprCaptures.push_back(Ref->getDecl()); 8915 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 8916 ExprResult RefRes = DefaultLvalueConversion(Ref); 8917 if (!RefRes.isUsable()) 8918 continue; 8919 ExprResult PostUpdateRes = 8920 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 8921 SimpleRefExpr, RefRes.get()); 8922 if (!PostUpdateRes.isUsable()) 8923 continue; 8924 ExprPostUpdates.push_back( 8925 IgnoredValueConversions(PostUpdateRes.get()).get()); 8926 } 8927 } 8928 } 8929 if (LinKind == OMPC_LINEAR_uval) 8930 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 8931 else 8932 InitExpr = VD ? SimpleRefExpr : Ref; 8933 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 8934 /*DirectInit=*/false, /*TypeMayContainAuto=*/false); 8935 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 8936 8937 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 8938 Vars.push_back((VD || CurContext->isDependentContext()) 8939 ? RefExpr->IgnoreParens() 8940 : Ref); 8941 Privates.push_back(PrivateRef); 8942 Inits.push_back(InitRef); 8943 } 8944 8945 if (Vars.empty()) 8946 return nullptr; 8947 8948 Expr *StepExpr = Step; 8949 Expr *CalcStepExpr = nullptr; 8950 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 8951 !Step->isInstantiationDependent() && 8952 !Step->containsUnexpandedParameterPack()) { 8953 SourceLocation StepLoc = Step->getLocStart(); 8954 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 8955 if (Val.isInvalid()) 8956 return nullptr; 8957 StepExpr = Val.get(); 8958 8959 // Build var to save the step value. 8960 VarDecl *SaveVar = 8961 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 8962 ExprResult SaveRef = 8963 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 8964 ExprResult CalcStep = 8965 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 8966 CalcStep = ActOnFinishFullExpr(CalcStep.get()); 8967 8968 // Warn about zero linear step (it would be probably better specified as 8969 // making corresponding variables 'const'). 8970 llvm::APSInt Result; 8971 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); 8972 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) 8973 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] 8974 << (Vars.size() > 1); 8975 if (!IsConstant && CalcStep.isUsable()) { 8976 // Calculate the step beforehand instead of doing this on each iteration. 8977 // (This is not used if the number of iterations may be kfold-ed). 8978 CalcStepExpr = CalcStep.get(); 8979 } 8980 } 8981 8982 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 8983 ColonLoc, EndLoc, Vars, Privates, Inits, 8984 StepExpr, CalcStepExpr, 8985 buildPreInits(Context, ExprCaptures), 8986 buildPostUpdate(*this, ExprPostUpdates)); 8987 } 8988 8989 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 8990 Expr *NumIterations, Sema &SemaRef, 8991 Scope *S, DSAStackTy *Stack) { 8992 // Walk the vars and build update/final expressions for the CodeGen. 8993 SmallVector<Expr *, 8> Updates; 8994 SmallVector<Expr *, 8> Finals; 8995 Expr *Step = Clause.getStep(); 8996 Expr *CalcStep = Clause.getCalcStep(); 8997 // OpenMP [2.14.3.7, linear clause] 8998 // If linear-step is not specified it is assumed to be 1. 8999 if (Step == nullptr) 9000 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 9001 else if (CalcStep) { 9002 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 9003 } 9004 bool HasErrors = false; 9005 auto CurInit = Clause.inits().begin(); 9006 auto CurPrivate = Clause.privates().begin(); 9007 auto LinKind = Clause.getModifier(); 9008 for (auto &RefExpr : Clause.varlists()) { 9009 SourceLocation ELoc; 9010 SourceRange ERange; 9011 Expr *SimpleRefExpr = RefExpr; 9012 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange, 9013 /*AllowArraySection=*/false); 9014 ValueDecl *D = Res.first; 9015 if (Res.second || !D) { 9016 Updates.push_back(nullptr); 9017 Finals.push_back(nullptr); 9018 HasErrors = true; 9019 continue; 9020 } 9021 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) { 9022 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts()) 9023 ->getMemberDecl(); 9024 } 9025 auto &&Info = Stack->isLoopControlVariable(D); 9026 Expr *InitExpr = *CurInit; 9027 9028 // Build privatized reference to the current linear var. 9029 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 9030 Expr *CapturedRef; 9031 if (LinKind == OMPC_LINEAR_uval) 9032 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 9033 else 9034 CapturedRef = 9035 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 9036 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 9037 /*RefersToCapture=*/true); 9038 9039 // Build update: Var = InitExpr + IV * Step 9040 ExprResult Update; 9041 if (!Info.first) { 9042 Update = 9043 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, 9044 InitExpr, IV, Step, /* Subtract */ false); 9045 } else 9046 Update = *CurPrivate; 9047 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(), 9048 /*DiscardedValue=*/true); 9049 9050 // Build final: Var = InitExpr + NumIterations * Step 9051 ExprResult Final; 9052 if (!Info.first) { 9053 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 9054 InitExpr, NumIterations, Step, 9055 /* Subtract */ false); 9056 } else 9057 Final = *CurPrivate; 9058 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(), 9059 /*DiscardedValue=*/true); 9060 9061 if (!Update.isUsable() || !Final.isUsable()) { 9062 Updates.push_back(nullptr); 9063 Finals.push_back(nullptr); 9064 HasErrors = true; 9065 } else { 9066 Updates.push_back(Update.get()); 9067 Finals.push_back(Final.get()); 9068 } 9069 ++CurInit; 9070 ++CurPrivate; 9071 } 9072 Clause.setUpdates(Updates); 9073 Clause.setFinals(Finals); 9074 return HasErrors; 9075 } 9076 9077 OMPClause *Sema::ActOnOpenMPAlignedClause( 9078 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 9079 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 9080 9081 SmallVector<Expr *, 8> Vars; 9082 for (auto &RefExpr : VarList) { 9083 assert(RefExpr && "NULL expr in OpenMP linear clause."); 9084 SourceLocation ELoc; 9085 SourceRange ERange; 9086 Expr *SimpleRefExpr = RefExpr; 9087 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 9088 /*AllowArraySection=*/false); 9089 if (Res.second) { 9090 // It will be analyzed later. 9091 Vars.push_back(RefExpr); 9092 } 9093 ValueDecl *D = Res.first; 9094 if (!D) 9095 continue; 9096 9097 QualType QType = D->getType(); 9098 auto *VD = dyn_cast<VarDecl>(D); 9099 9100 // OpenMP [2.8.1, simd construct, Restrictions] 9101 // The type of list items appearing in the aligned clause must be 9102 // array, pointer, reference to array, or reference to pointer. 9103 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 9104 const Type *Ty = QType.getTypePtrOrNull(); 9105 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 9106 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 9107 << QType << getLangOpts().CPlusPlus << ERange; 9108 bool IsDecl = 9109 !VD || 9110 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 9111 Diag(D->getLocation(), 9112 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 9113 << D; 9114 continue; 9115 } 9116 9117 // OpenMP [2.8.1, simd construct, Restrictions] 9118 // A list-item cannot appear in more than one aligned clause. 9119 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 9120 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange; 9121 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 9122 << getOpenMPClauseName(OMPC_aligned); 9123 continue; 9124 } 9125 9126 DeclRefExpr *Ref = nullptr; 9127 if (!VD && IsOpenMPCapturedDecl(D)) 9128 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 9129 Vars.push_back(DefaultFunctionArrayConversion( 9130 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 9131 .get()); 9132 } 9133 9134 // OpenMP [2.8.1, simd construct, Description] 9135 // The parameter of the aligned clause, alignment, must be a constant 9136 // positive integer expression. 9137 // If no optional parameter is specified, implementation-defined default 9138 // alignments for SIMD instructions on the target platforms are assumed. 9139 if (Alignment != nullptr) { 9140 ExprResult AlignResult = 9141 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 9142 if (AlignResult.isInvalid()) 9143 return nullptr; 9144 Alignment = AlignResult.get(); 9145 } 9146 if (Vars.empty()) 9147 return nullptr; 9148 9149 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 9150 EndLoc, Vars, Alignment); 9151 } 9152 9153 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 9154 SourceLocation StartLoc, 9155 SourceLocation LParenLoc, 9156 SourceLocation EndLoc) { 9157 SmallVector<Expr *, 8> Vars; 9158 SmallVector<Expr *, 8> SrcExprs; 9159 SmallVector<Expr *, 8> DstExprs; 9160 SmallVector<Expr *, 8> AssignmentOps; 9161 for (auto &RefExpr : VarList) { 9162 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 9163 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 9164 // It will be analyzed later. 9165 Vars.push_back(RefExpr); 9166 SrcExprs.push_back(nullptr); 9167 DstExprs.push_back(nullptr); 9168 AssignmentOps.push_back(nullptr); 9169 continue; 9170 } 9171 9172 SourceLocation ELoc = RefExpr->getExprLoc(); 9173 // OpenMP [2.1, C/C++] 9174 // A list item is a variable name. 9175 // OpenMP [2.14.4.1, Restrictions, p.1] 9176 // A list item that appears in a copyin clause must be threadprivate. 9177 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 9178 if (!DE || !isa<VarDecl>(DE->getDecl())) { 9179 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 9180 << 0 << RefExpr->getSourceRange(); 9181 continue; 9182 } 9183 9184 Decl *D = DE->getDecl(); 9185 VarDecl *VD = cast<VarDecl>(D); 9186 9187 QualType Type = VD->getType(); 9188 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 9189 // It will be analyzed later. 9190 Vars.push_back(DE); 9191 SrcExprs.push_back(nullptr); 9192 DstExprs.push_back(nullptr); 9193 AssignmentOps.push_back(nullptr); 9194 continue; 9195 } 9196 9197 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 9198 // A list item that appears in a copyin clause must be threadprivate. 9199 if (!DSAStack->isThreadPrivate(VD)) { 9200 Diag(ELoc, diag::err_omp_required_access) 9201 << getOpenMPClauseName(OMPC_copyin) 9202 << getOpenMPDirectiveName(OMPD_threadprivate); 9203 continue; 9204 } 9205 9206 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 9207 // A variable of class type (or array thereof) that appears in a 9208 // copyin clause requires an accessible, unambiguous copy assignment 9209 // operator for the class type. 9210 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 9211 auto *SrcVD = 9212 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(), 9213 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 9214 auto *PseudoSrcExpr = buildDeclRefExpr( 9215 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 9216 auto *DstVD = 9217 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst", 9218 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 9219 auto *PseudoDstExpr = 9220 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 9221 // For arrays generate assignment operation for single element and replace 9222 // it by the original array element in CodeGen. 9223 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, 9224 PseudoDstExpr, PseudoSrcExpr); 9225 if (AssignmentOp.isInvalid()) 9226 continue; 9227 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 9228 /*DiscardedValue=*/true); 9229 if (AssignmentOp.isInvalid()) 9230 continue; 9231 9232 DSAStack->addDSA(VD, DE, OMPC_copyin); 9233 Vars.push_back(DE); 9234 SrcExprs.push_back(PseudoSrcExpr); 9235 DstExprs.push_back(PseudoDstExpr); 9236 AssignmentOps.push_back(AssignmentOp.get()); 9237 } 9238 9239 if (Vars.empty()) 9240 return nullptr; 9241 9242 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 9243 SrcExprs, DstExprs, AssignmentOps); 9244 } 9245 9246 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 9247 SourceLocation StartLoc, 9248 SourceLocation LParenLoc, 9249 SourceLocation EndLoc) { 9250 SmallVector<Expr *, 8> Vars; 9251 SmallVector<Expr *, 8> SrcExprs; 9252 SmallVector<Expr *, 8> DstExprs; 9253 SmallVector<Expr *, 8> AssignmentOps; 9254 for (auto &RefExpr : VarList) { 9255 assert(RefExpr && "NULL expr in OpenMP linear clause."); 9256 SourceLocation ELoc; 9257 SourceRange ERange; 9258 Expr *SimpleRefExpr = RefExpr; 9259 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 9260 /*AllowArraySection=*/false); 9261 if (Res.second) { 9262 // It will be analyzed later. 9263 Vars.push_back(RefExpr); 9264 SrcExprs.push_back(nullptr); 9265 DstExprs.push_back(nullptr); 9266 AssignmentOps.push_back(nullptr); 9267 } 9268 ValueDecl *D = Res.first; 9269 if (!D) 9270 continue; 9271 9272 QualType Type = D->getType(); 9273 auto *VD = dyn_cast<VarDecl>(D); 9274 9275 // OpenMP [2.14.4.2, Restrictions, p.2] 9276 // A list item that appears in a copyprivate clause may not appear in a 9277 // private or firstprivate clause on the single construct. 9278 if (!VD || !DSAStack->isThreadPrivate(VD)) { 9279 auto DVar = DSAStack->getTopDSA(D, false); 9280 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 9281 DVar.RefExpr) { 9282 Diag(ELoc, diag::err_omp_wrong_dsa) 9283 << getOpenMPClauseName(DVar.CKind) 9284 << getOpenMPClauseName(OMPC_copyprivate); 9285 ReportOriginalDSA(*this, DSAStack, D, DVar); 9286 continue; 9287 } 9288 9289 // OpenMP [2.11.4.2, Restrictions, p.1] 9290 // All list items that appear in a copyprivate clause must be either 9291 // threadprivate or private in the enclosing context. 9292 if (DVar.CKind == OMPC_unknown) { 9293 DVar = DSAStack->getImplicitDSA(D, false); 9294 if (DVar.CKind == OMPC_shared) { 9295 Diag(ELoc, diag::err_omp_required_access) 9296 << getOpenMPClauseName(OMPC_copyprivate) 9297 << "threadprivate or private in the enclosing context"; 9298 ReportOriginalDSA(*this, DSAStack, D, DVar); 9299 continue; 9300 } 9301 } 9302 } 9303 9304 // Variably modified types are not supported. 9305 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 9306 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 9307 << getOpenMPClauseName(OMPC_copyprivate) << Type 9308 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 9309 bool IsDecl = 9310 !VD || 9311 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 9312 Diag(D->getLocation(), 9313 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 9314 << D; 9315 continue; 9316 } 9317 9318 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 9319 // A variable of class type (or array thereof) that appears in a 9320 // copyin clause requires an accessible, unambiguous copy assignment 9321 // operator for the class type. 9322 Type = Context.getBaseElementType(Type.getNonReferenceType()) 9323 .getUnqualifiedType(); 9324 auto *SrcVD = 9325 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src", 9326 D->hasAttrs() ? &D->getAttrs() : nullptr); 9327 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 9328 auto *DstVD = 9329 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst", 9330 D->hasAttrs() ? &D->getAttrs() : nullptr); 9331 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 9332 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 9333 PseudoDstExpr, PseudoSrcExpr); 9334 if (AssignmentOp.isInvalid()) 9335 continue; 9336 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc, 9337 /*DiscardedValue=*/true); 9338 if (AssignmentOp.isInvalid()) 9339 continue; 9340 9341 // No need to mark vars as copyprivate, they are already threadprivate or 9342 // implicitly private. 9343 assert(VD || IsOpenMPCapturedDecl(D)); 9344 Vars.push_back( 9345 VD ? RefExpr->IgnoreParens() 9346 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 9347 SrcExprs.push_back(PseudoSrcExpr); 9348 DstExprs.push_back(PseudoDstExpr); 9349 AssignmentOps.push_back(AssignmentOp.get()); 9350 } 9351 9352 if (Vars.empty()) 9353 return nullptr; 9354 9355 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 9356 Vars, SrcExprs, DstExprs, AssignmentOps); 9357 } 9358 9359 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 9360 SourceLocation StartLoc, 9361 SourceLocation LParenLoc, 9362 SourceLocation EndLoc) { 9363 if (VarList.empty()) 9364 return nullptr; 9365 9366 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 9367 } 9368 9369 OMPClause * 9370 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind, 9371 SourceLocation DepLoc, SourceLocation ColonLoc, 9372 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 9373 SourceLocation LParenLoc, SourceLocation EndLoc) { 9374 if (DSAStack->getCurrentDirective() == OMPD_ordered && 9375 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 9376 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 9377 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 9378 return nullptr; 9379 } 9380 if (DSAStack->getCurrentDirective() != OMPD_ordered && 9381 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 9382 DepKind == OMPC_DEPEND_sink)) { 9383 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink}; 9384 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 9385 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 9386 /*Last=*/OMPC_DEPEND_unknown, Except) 9387 << getOpenMPClauseName(OMPC_depend); 9388 return nullptr; 9389 } 9390 SmallVector<Expr *, 8> Vars; 9391 DSAStackTy::OperatorOffsetTy OpsOffs; 9392 llvm::APSInt DepCounter(/*BitWidth=*/32); 9393 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 9394 if (DepKind == OMPC_DEPEND_sink) { 9395 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) { 9396 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 9397 TotalDepCount.setIsUnsigned(/*Val=*/true); 9398 } 9399 } 9400 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) || 9401 DSAStack->getParentOrderedRegionParam()) { 9402 for (auto &RefExpr : VarList) { 9403 assert(RefExpr && "NULL expr in OpenMP shared clause."); 9404 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 9405 // It will be analyzed later. 9406 Vars.push_back(RefExpr); 9407 continue; 9408 } 9409 9410 SourceLocation ELoc = RefExpr->getExprLoc(); 9411 auto *SimpleExpr = RefExpr->IgnoreParenCasts(); 9412 if (DepKind == OMPC_DEPEND_sink) { 9413 if (DepCounter >= TotalDepCount) { 9414 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 9415 continue; 9416 } 9417 ++DepCounter; 9418 // OpenMP [2.13.9, Summary] 9419 // depend(dependence-type : vec), where dependence-type is: 9420 // 'sink' and where vec is the iteration vector, which has the form: 9421 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 9422 // where n is the value specified by the ordered clause in the loop 9423 // directive, xi denotes the loop iteration variable of the i-th nested 9424 // loop associated with the loop directive, and di is a constant 9425 // non-negative integer. 9426 if (CurContext->isDependentContext()) { 9427 // It will be analyzed later. 9428 Vars.push_back(RefExpr); 9429 continue; 9430 } 9431 SimpleExpr = SimpleExpr->IgnoreImplicit(); 9432 OverloadedOperatorKind OOK = OO_None; 9433 SourceLocation OOLoc; 9434 Expr *LHS = SimpleExpr; 9435 Expr *RHS = nullptr; 9436 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 9437 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 9438 OOLoc = BO->getOperatorLoc(); 9439 LHS = BO->getLHS()->IgnoreParenImpCasts(); 9440 RHS = BO->getRHS()->IgnoreParenImpCasts(); 9441 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 9442 OOK = OCE->getOperator(); 9443 OOLoc = OCE->getOperatorLoc(); 9444 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 9445 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 9446 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 9447 OOK = MCE->getMethodDecl() 9448 ->getNameInfo() 9449 .getName() 9450 .getCXXOverloadedOperator(); 9451 OOLoc = MCE->getCallee()->getExprLoc(); 9452 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 9453 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 9454 } 9455 SourceLocation ELoc; 9456 SourceRange ERange; 9457 auto Res = getPrivateItem(*this, LHS, ELoc, ERange, 9458 /*AllowArraySection=*/false); 9459 if (Res.second) { 9460 // It will be analyzed later. 9461 Vars.push_back(RefExpr); 9462 } 9463 ValueDecl *D = Res.first; 9464 if (!D) 9465 continue; 9466 9467 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 9468 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 9469 continue; 9470 } 9471 if (RHS) { 9472 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 9473 RHS, OMPC_depend, /*StrictlyPositive=*/false); 9474 if (RHSRes.isInvalid()) 9475 continue; 9476 } 9477 if (!CurContext->isDependentContext() && 9478 DSAStack->getParentOrderedRegionParam() && 9479 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 9480 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 9481 << DSAStack->getParentLoopControlVariable( 9482 DepCounter.getZExtValue()); 9483 continue; 9484 } 9485 OpsOffs.push_back({RHS, OOK}); 9486 } else { 9487 // OpenMP [2.11.1.1, Restrictions, p.3] 9488 // A variable that is part of another variable (such as a field of a 9489 // structure) but is not an array element or an array section cannot 9490 // appear in a depend clause. 9491 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr); 9492 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 9493 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); 9494 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 9495 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) || 9496 (ASE && 9497 !ASE->getBase() 9498 ->getType() 9499 .getNonReferenceType() 9500 ->isPointerType() && 9501 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 9502 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item) 9503 << 0 << RefExpr->getSourceRange(); 9504 continue; 9505 } 9506 } 9507 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 9508 } 9509 9510 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 9511 TotalDepCount > VarList.size() && 9512 DSAStack->getParentOrderedRegionParam()) { 9513 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 9514 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 9515 } 9516 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 9517 Vars.empty()) 9518 return nullptr; 9519 } 9520 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 9521 DepKind, DepLoc, ColonLoc, Vars); 9522 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) 9523 DSAStack->addDoacrossDependClause(C, OpsOffs); 9524 return C; 9525 } 9526 9527 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc, 9528 SourceLocation LParenLoc, 9529 SourceLocation EndLoc) { 9530 Expr *ValExpr = Device; 9531 9532 // OpenMP [2.9.1, Restrictions] 9533 // The device expression must evaluate to a non-negative integer value. 9534 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 9535 /*StrictlyPositive=*/false)) 9536 return nullptr; 9537 9538 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc); 9539 } 9540 9541 static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc, 9542 DSAStackTy *Stack, CXXRecordDecl *RD) { 9543 if (!RD || RD->isInvalidDecl()) 9544 return true; 9545 9546 auto QTy = SemaRef.Context.getRecordType(RD); 9547 if (RD->isDynamicClass()) { 9548 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; 9549 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target); 9550 return false; 9551 } 9552 auto *DC = RD; 9553 bool IsCorrect = true; 9554 for (auto *I : DC->decls()) { 9555 if (I) { 9556 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) { 9557 if (MD->isStatic()) { 9558 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; 9559 SemaRef.Diag(MD->getLocation(), 9560 diag::note_omp_static_member_in_target); 9561 IsCorrect = false; 9562 } 9563 } else if (auto *VD = dyn_cast<VarDecl>(I)) { 9564 if (VD->isStaticDataMember()) { 9565 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy; 9566 SemaRef.Diag(VD->getLocation(), 9567 diag::note_omp_static_member_in_target); 9568 IsCorrect = false; 9569 } 9570 } 9571 } 9572 } 9573 9574 for (auto &I : RD->bases()) { 9575 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack, 9576 I.getType()->getAsCXXRecordDecl())) 9577 IsCorrect = false; 9578 } 9579 return IsCorrect; 9580 } 9581 9582 static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 9583 DSAStackTy *Stack, QualType QTy) { 9584 NamedDecl *ND; 9585 if (QTy->isIncompleteType(&ND)) { 9586 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 9587 return false; 9588 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) { 9589 if (!RD->isInvalidDecl() && !IsCXXRecordForMappable(SemaRef, SL, Stack, RD)) 9590 return false; 9591 } 9592 return true; 9593 } 9594 9595 /// \brief Return true if it can be proven that the provided array expression 9596 /// (array section or array subscript) does NOT specify the whole size of the 9597 /// array whose base type is \a BaseQTy. 9598 static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 9599 const Expr *E, 9600 QualType BaseQTy) { 9601 auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 9602 9603 // If this is an array subscript, it refers to the whole size if the size of 9604 // the dimension is constant and equals 1. Also, an array section assumes the 9605 // format of an array subscript if no colon is used. 9606 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) { 9607 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 9608 return ATy->getSize().getSExtValue() != 1; 9609 // Size can't be evaluated statically. 9610 return false; 9611 } 9612 9613 assert(OASE && "Expecting array section if not an array subscript."); 9614 auto *LowerBound = OASE->getLowerBound(); 9615 auto *Length = OASE->getLength(); 9616 9617 // If there is a lower bound that does not evaluates to zero, we are not 9618 // covering the whole dimension. 9619 if (LowerBound) { 9620 llvm::APSInt ConstLowerBound; 9621 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext())) 9622 return false; // Can't get the integer value as a constant. 9623 if (ConstLowerBound.getSExtValue()) 9624 return true; 9625 } 9626 9627 // If we don't have a length we covering the whole dimension. 9628 if (!Length) 9629 return false; 9630 9631 // If the base is a pointer, we don't have a way to get the size of the 9632 // pointee. 9633 if (BaseQTy->isPointerType()) 9634 return false; 9635 9636 // We can only check if the length is the same as the size of the dimension 9637 // if we have a constant array. 9638 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 9639 if (!CATy) 9640 return false; 9641 9642 llvm::APSInt ConstLength; 9643 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext())) 9644 return false; // Can't get the integer value as a constant. 9645 9646 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 9647 } 9648 9649 // Return true if it can be proven that the provided array expression (array 9650 // section or array subscript) does NOT specify a single element of the array 9651 // whose base type is \a BaseQTy. 9652 static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 9653 const Expr *E, 9654 QualType BaseQTy) { 9655 auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 9656 9657 // An array subscript always refer to a single element. Also, an array section 9658 // assumes the format of an array subscript if no colon is used. 9659 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) 9660 return false; 9661 9662 assert(OASE && "Expecting array section if not an array subscript."); 9663 auto *Length = OASE->getLength(); 9664 9665 // If we don't have a length we have to check if the array has unitary size 9666 // for this dimension. Also, we should always expect a length if the base type 9667 // is pointer. 9668 if (!Length) { 9669 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 9670 return ATy->getSize().getSExtValue() != 1; 9671 // We cannot assume anything. 9672 return false; 9673 } 9674 9675 // Check if the length evaluates to 1. 9676 llvm::APSInt ConstLength; 9677 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext())) 9678 return false; // Can't get the integer value as a constant. 9679 9680 return ConstLength.getSExtValue() != 1; 9681 } 9682 9683 // Return the expression of the base of the mappable expression or null if it 9684 // cannot be determined and do all the necessary checks to see if the expression 9685 // is valid as a standalone mappable expression. In the process, record all the 9686 // components of the expression. 9687 static Expr *CheckMapClauseExpressionBase( 9688 Sema &SemaRef, Expr *E, 9689 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 9690 OpenMPClauseKind CKind) { 9691 SourceLocation ELoc = E->getExprLoc(); 9692 SourceRange ERange = E->getSourceRange(); 9693 9694 // The base of elements of list in a map clause have to be either: 9695 // - a reference to variable or field. 9696 // - a member expression. 9697 // - an array expression. 9698 // 9699 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 9700 // reference to 'r'. 9701 // 9702 // If we have: 9703 // 9704 // struct SS { 9705 // Bla S; 9706 // foo() { 9707 // #pragma omp target map (S.Arr[:12]); 9708 // } 9709 // } 9710 // 9711 // We want to retrieve the member expression 'this->S'; 9712 9713 Expr *RelevantExpr = nullptr; 9714 9715 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2] 9716 // If a list item is an array section, it must specify contiguous storage. 9717 // 9718 // For this restriction it is sufficient that we make sure only references 9719 // to variables or fields and array expressions, and that no array sections 9720 // exist except in the rightmost expression (unless they cover the whole 9721 // dimension of the array). E.g. these would be invalid: 9722 // 9723 // r.ArrS[3:5].Arr[6:7] 9724 // 9725 // r.ArrS[3:5].x 9726 // 9727 // but these would be valid: 9728 // r.ArrS[3].Arr[6:7] 9729 // 9730 // r.ArrS[3].x 9731 9732 bool AllowUnitySizeArraySection = true; 9733 bool AllowWholeSizeArraySection = true; 9734 9735 while (!RelevantExpr) { 9736 E = E->IgnoreParenImpCasts(); 9737 9738 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) { 9739 if (!isa<VarDecl>(CurE->getDecl())) 9740 break; 9741 9742 RelevantExpr = CurE; 9743 9744 // If we got a reference to a declaration, we should not expect any array 9745 // section before that. 9746 AllowUnitySizeArraySection = false; 9747 AllowWholeSizeArraySection = false; 9748 9749 // Record the component. 9750 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent( 9751 CurE, CurE->getDecl())); 9752 continue; 9753 } 9754 9755 if (auto *CurE = dyn_cast<MemberExpr>(E)) { 9756 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts(); 9757 9758 if (isa<CXXThisExpr>(BaseE)) 9759 // We found a base expression: this->Val. 9760 RelevantExpr = CurE; 9761 else 9762 E = BaseE; 9763 9764 if (!isa<FieldDecl>(CurE->getMemberDecl())) { 9765 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 9766 << CurE->getSourceRange(); 9767 break; 9768 } 9769 9770 auto *FD = cast<FieldDecl>(CurE->getMemberDecl()); 9771 9772 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 9773 // A bit-field cannot appear in a map clause. 9774 // 9775 if (FD->isBitField()) { 9776 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 9777 << CurE->getSourceRange() << getOpenMPClauseName(CKind); 9778 break; 9779 } 9780 9781 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 9782 // If the type of a list item is a reference to a type T then the type 9783 // will be considered to be T for all purposes of this clause. 9784 QualType CurType = BaseE->getType().getNonReferenceType(); 9785 9786 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 9787 // A list item cannot be a variable that is a member of a structure with 9788 // a union type. 9789 // 9790 if (auto *RT = CurType->getAs<RecordType>()) 9791 if (RT->isUnionType()) { 9792 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 9793 << CurE->getSourceRange(); 9794 break; 9795 } 9796 9797 // If we got a member expression, we should not expect any array section 9798 // before that: 9799 // 9800 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 9801 // If a list item is an element of a structure, only the rightmost symbol 9802 // of the variable reference can be an array section. 9803 // 9804 AllowUnitySizeArraySection = false; 9805 AllowWholeSizeArraySection = false; 9806 9807 // Record the component. 9808 CurComponents.push_back( 9809 OMPClauseMappableExprCommon::MappableComponent(CurE, FD)); 9810 continue; 9811 } 9812 9813 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) { 9814 E = CurE->getBase()->IgnoreParenImpCasts(); 9815 9816 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 9817 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 9818 << 0 << CurE->getSourceRange(); 9819 break; 9820 } 9821 9822 // If we got an array subscript that express the whole dimension we 9823 // can have any array expressions before. If it only expressing part of 9824 // the dimension, we can only have unitary-size array expressions. 9825 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, 9826 E->getType())) 9827 AllowWholeSizeArraySection = false; 9828 9829 // Record the component - we don't have any declaration associated. 9830 CurComponents.push_back( 9831 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr)); 9832 continue; 9833 } 9834 9835 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) { 9836 E = CurE->getBase()->IgnoreParenImpCasts(); 9837 9838 auto CurType = 9839 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 9840 9841 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 9842 // If the type of a list item is a reference to a type T then the type 9843 // will be considered to be T for all purposes of this clause. 9844 if (CurType->isReferenceType()) 9845 CurType = CurType->getPointeeType(); 9846 9847 bool IsPointer = CurType->isAnyPointerType(); 9848 9849 if (!IsPointer && !CurType->isArrayType()) { 9850 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 9851 << 0 << CurE->getSourceRange(); 9852 break; 9853 } 9854 9855 bool NotWhole = 9856 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType); 9857 bool NotUnity = 9858 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType); 9859 9860 if (AllowWholeSizeArraySection) { 9861 // Any array section is currently allowed. Allowing a whole size array 9862 // section implies allowing a unity array section as well. 9863 // 9864 // If this array section refers to the whole dimension we can still 9865 // accept other array sections before this one, except if the base is a 9866 // pointer. Otherwise, only unitary sections are accepted. 9867 if (NotWhole || IsPointer) 9868 AllowWholeSizeArraySection = false; 9869 } else if (AllowUnitySizeArraySection && NotUnity) { 9870 // A unity or whole array section is not allowed and that is not 9871 // compatible with the properties of the current array section. 9872 SemaRef.Diag( 9873 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 9874 << CurE->getSourceRange(); 9875 break; 9876 } 9877 9878 // Record the component - we don't have any declaration associated. 9879 CurComponents.push_back( 9880 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr)); 9881 continue; 9882 } 9883 9884 // If nothing else worked, this is not a valid map clause expression. 9885 SemaRef.Diag(ELoc, 9886 diag::err_omp_expected_named_var_member_or_array_expression) 9887 << ERange; 9888 break; 9889 } 9890 9891 return RelevantExpr; 9892 } 9893 9894 // Return true if expression E associated with value VD has conflicts with other 9895 // map information. 9896 static bool CheckMapConflicts( 9897 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E, 9898 bool CurrentRegionOnly, 9899 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 9900 OpenMPClauseKind CKind) { 9901 assert(VD && E); 9902 SourceLocation ELoc = E->getExprLoc(); 9903 SourceRange ERange = E->getSourceRange(); 9904 9905 // In order to easily check the conflicts we need to match each component of 9906 // the expression under test with the components of the expressions that are 9907 // already in the stack. 9908 9909 assert(!CurComponents.empty() && "Map clause expression with no components!"); 9910 assert(CurComponents.back().getAssociatedDeclaration() == VD && 9911 "Map clause expression with unexpected base!"); 9912 9913 // Variables to help detecting enclosing problems in data environment nests. 9914 bool IsEnclosedByDataEnvironmentExpr = false; 9915 const Expr *EnclosingExpr = nullptr; 9916 9917 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 9918 VD, CurrentRegionOnly, 9919 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef 9920 StackComponents, 9921 OpenMPClauseKind) -> bool { 9922 9923 assert(!StackComponents.empty() && 9924 "Map clause expression with no components!"); 9925 assert(StackComponents.back().getAssociatedDeclaration() == VD && 9926 "Map clause expression with unexpected base!"); 9927 9928 // The whole expression in the stack. 9929 auto *RE = StackComponents.front().getAssociatedExpression(); 9930 9931 // Expressions must start from the same base. Here we detect at which 9932 // point both expressions diverge from each other and see if we can 9933 // detect if the memory referred to both expressions is contiguous and 9934 // do not overlap. 9935 auto CI = CurComponents.rbegin(); 9936 auto CE = CurComponents.rend(); 9937 auto SI = StackComponents.rbegin(); 9938 auto SE = StackComponents.rend(); 9939 for (; CI != CE && SI != SE; ++CI, ++SI) { 9940 9941 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 9942 // At most one list item can be an array item derived from a given 9943 // variable in map clauses of the same construct. 9944 if (CurrentRegionOnly && 9945 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 9946 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) && 9947 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 9948 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) { 9949 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 9950 diag::err_omp_multiple_array_items_in_map_clause) 9951 << CI->getAssociatedExpression()->getSourceRange(); 9952 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 9953 diag::note_used_here) 9954 << SI->getAssociatedExpression()->getSourceRange(); 9955 return true; 9956 } 9957 9958 // Do both expressions have the same kind? 9959 if (CI->getAssociatedExpression()->getStmtClass() != 9960 SI->getAssociatedExpression()->getStmtClass()) 9961 break; 9962 9963 // Are we dealing with different variables/fields? 9964 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 9965 break; 9966 } 9967 // Check if the extra components of the expressions in the enclosing 9968 // data environment are redundant for the current base declaration. 9969 // If they are, the maps completely overlap, which is legal. 9970 for (; SI != SE; ++SI) { 9971 QualType Type; 9972 if (auto *ASE = 9973 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 9974 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 9975 } else if (auto *OASE = dyn_cast<OMPArraySectionExpr>( 9976 SI->getAssociatedExpression())) { 9977 auto *E = OASE->getBase()->IgnoreParenImpCasts(); 9978 Type = 9979 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 9980 } 9981 if (Type.isNull() || Type->isAnyPointerType() || 9982 CheckArrayExpressionDoesNotReferToWholeSize( 9983 SemaRef, SI->getAssociatedExpression(), Type)) 9984 break; 9985 } 9986 9987 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 9988 // List items of map clauses in the same construct must not share 9989 // original storage. 9990 // 9991 // If the expressions are exactly the same or one is a subset of the 9992 // other, it means they are sharing storage. 9993 if (CI == CE && SI == SE) { 9994 if (CurrentRegionOnly) { 9995 if (CKind == OMPC_map) 9996 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 9997 else { 9998 assert(CKind == OMPC_to || CKind == OMPC_from); 9999 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 10000 << ERange; 10001 } 10002 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 10003 << RE->getSourceRange(); 10004 return true; 10005 } else { 10006 // If we find the same expression in the enclosing data environment, 10007 // that is legal. 10008 IsEnclosedByDataEnvironmentExpr = true; 10009 return false; 10010 } 10011 } 10012 10013 QualType DerivedType = 10014 std::prev(CI)->getAssociatedDeclaration()->getType(); 10015 SourceLocation DerivedLoc = 10016 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 10017 10018 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 10019 // If the type of a list item is a reference to a type T then the type 10020 // will be considered to be T for all purposes of this clause. 10021 DerivedType = DerivedType.getNonReferenceType(); 10022 10023 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 10024 // A variable for which the type is pointer and an array section 10025 // derived from that variable must not appear as list items of map 10026 // clauses of the same construct. 10027 // 10028 // Also, cover one of the cases in: 10029 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 10030 // If any part of the original storage of a list item has corresponding 10031 // storage in the device data environment, all of the original storage 10032 // must have corresponding storage in the device data environment. 10033 // 10034 if (DerivedType->isAnyPointerType()) { 10035 if (CI == CE || SI == SE) { 10036 SemaRef.Diag( 10037 DerivedLoc, 10038 diag::err_omp_pointer_mapped_along_with_derived_section) 10039 << DerivedLoc; 10040 } else { 10041 assert(CI != CE && SI != SE); 10042 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced) 10043 << DerivedLoc; 10044 } 10045 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 10046 << RE->getSourceRange(); 10047 return true; 10048 } 10049 10050 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 10051 // List items of map clauses in the same construct must not share 10052 // original storage. 10053 // 10054 // An expression is a subset of the other. 10055 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 10056 if (CKind == OMPC_map) 10057 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 10058 else { 10059 assert(CKind == OMPC_to || CKind == OMPC_from); 10060 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 10061 << ERange; 10062 } 10063 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 10064 << RE->getSourceRange(); 10065 return true; 10066 } 10067 10068 // The current expression uses the same base as other expression in the 10069 // data environment but does not contain it completely. 10070 if (!CurrentRegionOnly && SI != SE) 10071 EnclosingExpr = RE; 10072 10073 // The current expression is a subset of the expression in the data 10074 // environment. 10075 IsEnclosedByDataEnvironmentExpr |= 10076 (!CurrentRegionOnly && CI != CE && SI == SE); 10077 10078 return false; 10079 }); 10080 10081 if (CurrentRegionOnly) 10082 return FoundError; 10083 10084 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 10085 // If any part of the original storage of a list item has corresponding 10086 // storage in the device data environment, all of the original storage must 10087 // have corresponding storage in the device data environment. 10088 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 10089 // If a list item is an element of a structure, and a different element of 10090 // the structure has a corresponding list item in the device data environment 10091 // prior to a task encountering the construct associated with the map clause, 10092 // then the list item must also have a corresponding list item in the device 10093 // data environment prior to the task encountering the construct. 10094 // 10095 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 10096 SemaRef.Diag(ELoc, 10097 diag::err_omp_original_storage_is_shared_and_does_not_contain) 10098 << ERange; 10099 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 10100 << EnclosingExpr->getSourceRange(); 10101 return true; 10102 } 10103 10104 return FoundError; 10105 } 10106 10107 namespace { 10108 // Utility struct that gathers all the related lists associated with a mappable 10109 // expression. 10110 struct MappableVarListInfo final { 10111 // The list of expressions. 10112 ArrayRef<Expr *> VarList; 10113 // The list of processed expressions. 10114 SmallVector<Expr *, 16> ProcessedVarList; 10115 // The mappble components for each expression. 10116 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 10117 // The base declaration of the variable. 10118 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 10119 10120 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 10121 // We have a list of components and base declarations for each entry in the 10122 // variable list. 10123 VarComponents.reserve(VarList.size()); 10124 VarBaseDeclarations.reserve(VarList.size()); 10125 } 10126 }; 10127 } 10128 10129 // Check the validity of the provided variable list for the provided clause kind 10130 // \a CKind. In the check process the valid expressions, and mappable expression 10131 // components and variables are extracted and used to fill \a Vars, 10132 // \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and 10133 // \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'. 10134 static void 10135 checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS, 10136 OpenMPClauseKind CKind, MappableVarListInfo &MVLI, 10137 SourceLocation StartLoc, 10138 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 10139 bool IsMapTypeImplicit = false) { 10140 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 10141 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 10142 "Unexpected clause kind with mappable expressions!"); 10143 10144 // Keep track of the mappable components and base declarations in this clause. 10145 // Each entry in the list is going to have a list of components associated. We 10146 // record each set of the components so that we can build the clause later on. 10147 // In the end we should have the same amount of declarations and component 10148 // lists. 10149 10150 for (auto &RE : MVLI.VarList) { 10151 assert(RE && "Null expr in omp to/from/map clause"); 10152 SourceLocation ELoc = RE->getExprLoc(); 10153 10154 auto *VE = RE->IgnoreParenLValueCasts(); 10155 10156 if (VE->isValueDependent() || VE->isTypeDependent() || 10157 VE->isInstantiationDependent() || 10158 VE->containsUnexpandedParameterPack()) { 10159 // We can only analyze this information once the missing information is 10160 // resolved. 10161 MVLI.ProcessedVarList.push_back(RE); 10162 continue; 10163 } 10164 10165 auto *SimpleExpr = RE->IgnoreParenCasts(); 10166 10167 if (!RE->IgnoreParenImpCasts()->isLValue()) { 10168 SemaRef.Diag(ELoc, 10169 diag::err_omp_expected_named_var_member_or_array_expression) 10170 << RE->getSourceRange(); 10171 continue; 10172 } 10173 10174 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 10175 ValueDecl *CurDeclaration = nullptr; 10176 10177 // Obtain the array or member expression bases if required. Also, fill the 10178 // components array with all the components identified in the process. 10179 auto *BE = 10180 CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind); 10181 if (!BE) 10182 continue; 10183 10184 assert(!CurComponents.empty() && 10185 "Invalid mappable expression information."); 10186 10187 // For the following checks, we rely on the base declaration which is 10188 // expected to be associated with the last component. The declaration is 10189 // expected to be a variable or a field (if 'this' is being mapped). 10190 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 10191 assert(CurDeclaration && "Null decl on map clause."); 10192 assert( 10193 CurDeclaration->isCanonicalDecl() && 10194 "Expecting components to have associated only canonical declarations."); 10195 10196 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 10197 auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 10198 10199 assert((VD || FD) && "Only variables or fields are expected here!"); 10200 (void)FD; 10201 10202 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 10203 // threadprivate variables cannot appear in a map clause. 10204 // OpenMP 4.5 [2.10.5, target update Construct] 10205 // threadprivate variables cannot appear in a from clause. 10206 if (VD && DSAS->isThreadPrivate(VD)) { 10207 auto DVar = DSAS->getTopDSA(VD, false); 10208 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 10209 << getOpenMPClauseName(CKind); 10210 ReportOriginalDSA(SemaRef, DSAS, VD, DVar); 10211 continue; 10212 } 10213 10214 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 10215 // A list item cannot appear in both a map clause and a data-sharing 10216 // attribute clause on the same construct. 10217 10218 // Check conflicts with other map clause expressions. We check the conflicts 10219 // with the current construct separately from the enclosing data 10220 // environment, because the restrictions are different. We only have to 10221 // check conflicts across regions for the map clauses. 10222 if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 10223 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 10224 break; 10225 if (CKind == OMPC_map && 10226 CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 10227 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 10228 break; 10229 10230 // OpenMP 4.5 [2.10.5, target update Construct] 10231 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 10232 // If the type of a list item is a reference to a type T then the type will 10233 // be considered to be T for all purposes of this clause. 10234 QualType Type = CurDeclaration->getType().getNonReferenceType(); 10235 10236 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 10237 // A list item in a to or from clause must have a mappable type. 10238 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 10239 // A list item must have a mappable type. 10240 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 10241 DSAS, Type)) 10242 continue; 10243 10244 if (CKind == OMPC_map) { 10245 // target enter data 10246 // OpenMP [2.10.2, Restrictions, p. 99] 10247 // A map-type must be specified in all map clauses and must be either 10248 // to or alloc. 10249 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 10250 if (DKind == OMPD_target_enter_data && 10251 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 10252 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 10253 << (IsMapTypeImplicit ? 1 : 0) 10254 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 10255 << getOpenMPDirectiveName(DKind); 10256 continue; 10257 } 10258 10259 // target exit_data 10260 // OpenMP [2.10.3, Restrictions, p. 102] 10261 // A map-type must be specified in all map clauses and must be either 10262 // from, release, or delete. 10263 if (DKind == OMPD_target_exit_data && 10264 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 10265 MapType == OMPC_MAP_delete)) { 10266 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 10267 << (IsMapTypeImplicit ? 1 : 0) 10268 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 10269 << getOpenMPDirectiveName(DKind); 10270 continue; 10271 } 10272 10273 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 10274 // A list item cannot appear in both a map clause and a data-sharing 10275 // attribute clause on the same construct 10276 if ((DKind == OMPD_target || DKind == OMPD_target_teams || 10277 DKind == OMPD_target_teams_distribute || 10278 DKind == OMPD_target_teams_distribute_parallel_for || 10279 DKind == OMPD_target_teams_distribute_parallel_for_simd || 10280 DKind == OMPD_target_teams_distribute_simd) && VD) { 10281 auto DVar = DSAS->getTopDSA(VD, false); 10282 if (isOpenMPPrivate(DVar.CKind)) { 10283 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 10284 << getOpenMPClauseName(DVar.CKind) 10285 << getOpenMPClauseName(OMPC_map) 10286 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 10287 ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar); 10288 continue; 10289 } 10290 } 10291 } 10292 10293 // Save the current expression. 10294 MVLI.ProcessedVarList.push_back(RE); 10295 10296 // Store the components in the stack so that they can be used to check 10297 // against other clauses later on. 10298 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 10299 /*WhereFoundClauseKind=*/OMPC_map); 10300 10301 // Save the components and declaration to create the clause. For purposes of 10302 // the clause creation, any component list that has has base 'this' uses 10303 // null as base declaration. 10304 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 10305 MVLI.VarComponents.back().append(CurComponents.begin(), 10306 CurComponents.end()); 10307 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 10308 : CurDeclaration); 10309 } 10310 } 10311 10312 OMPClause * 10313 Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier, 10314 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, 10315 SourceLocation MapLoc, SourceLocation ColonLoc, 10316 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 10317 SourceLocation LParenLoc, SourceLocation EndLoc) { 10318 MappableVarListInfo MVLI(VarList); 10319 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc, 10320 MapType, IsMapTypeImplicit); 10321 10322 // We need to produce a map clause even if we don't have variables so that 10323 // other diagnostics related with non-existing map clauses are accurate. 10324 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, 10325 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 10326 MVLI.VarComponents, MapTypeModifier, MapType, 10327 IsMapTypeImplicit, MapLoc); 10328 } 10329 10330 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 10331 TypeResult ParsedType) { 10332 assert(ParsedType.isUsable()); 10333 10334 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 10335 if (ReductionType.isNull()) 10336 return QualType(); 10337 10338 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 10339 // A type name in a declare reduction directive cannot be a function type, an 10340 // array type, a reference type, or a type qualified with const, volatile or 10341 // restrict. 10342 if (ReductionType.hasQualifiers()) { 10343 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 10344 return QualType(); 10345 } 10346 10347 if (ReductionType->isFunctionType()) { 10348 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 10349 return QualType(); 10350 } 10351 if (ReductionType->isReferenceType()) { 10352 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 10353 return QualType(); 10354 } 10355 if (ReductionType->isArrayType()) { 10356 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 10357 return QualType(); 10358 } 10359 return ReductionType; 10360 } 10361 10362 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 10363 Scope *S, DeclContext *DC, DeclarationName Name, 10364 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 10365 AccessSpecifier AS, Decl *PrevDeclInScope) { 10366 SmallVector<Decl *, 8> Decls; 10367 Decls.reserve(ReductionTypes.size()); 10368 10369 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 10370 ForRedeclaration); 10371 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 10372 // A reduction-identifier may not be re-declared in the current scope for the 10373 // same type or for a type that is compatible according to the base language 10374 // rules. 10375 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 10376 OMPDeclareReductionDecl *PrevDRD = nullptr; 10377 bool InCompoundScope = true; 10378 if (S != nullptr) { 10379 // Find previous declaration with the same name not referenced in other 10380 // declarations. 10381 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 10382 InCompoundScope = 10383 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 10384 LookupName(Lookup, S); 10385 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 10386 /*AllowInlineNamespace=*/false); 10387 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 10388 auto Filter = Lookup.makeFilter(); 10389 while (Filter.hasNext()) { 10390 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 10391 if (InCompoundScope) { 10392 auto I = UsedAsPrevious.find(PrevDecl); 10393 if (I == UsedAsPrevious.end()) 10394 UsedAsPrevious[PrevDecl] = false; 10395 if (auto *D = PrevDecl->getPrevDeclInScope()) 10396 UsedAsPrevious[D] = true; 10397 } 10398 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 10399 PrevDecl->getLocation(); 10400 } 10401 Filter.done(); 10402 if (InCompoundScope) { 10403 for (auto &PrevData : UsedAsPrevious) { 10404 if (!PrevData.second) { 10405 PrevDRD = PrevData.first; 10406 break; 10407 } 10408 } 10409 } 10410 } else if (PrevDeclInScope != nullptr) { 10411 auto *PrevDRDInScope = PrevDRD = 10412 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 10413 do { 10414 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 10415 PrevDRDInScope->getLocation(); 10416 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 10417 } while (PrevDRDInScope != nullptr); 10418 } 10419 for (auto &TyData : ReductionTypes) { 10420 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 10421 bool Invalid = false; 10422 if (I != PreviousRedeclTypes.end()) { 10423 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 10424 << TyData.first; 10425 Diag(I->second, diag::note_previous_definition); 10426 Invalid = true; 10427 } 10428 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 10429 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 10430 Name, TyData.first, PrevDRD); 10431 DC->addDecl(DRD); 10432 DRD->setAccess(AS); 10433 Decls.push_back(DRD); 10434 if (Invalid) 10435 DRD->setInvalidDecl(); 10436 else 10437 PrevDRD = DRD; 10438 } 10439 10440 return DeclGroupPtrTy::make( 10441 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 10442 } 10443 10444 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 10445 auto *DRD = cast<OMPDeclareReductionDecl>(D); 10446 10447 // Enter new function scope. 10448 PushFunctionScope(); 10449 getCurFunction()->setHasBranchProtectedScope(); 10450 getCurFunction()->setHasOMPDeclareReductionCombiner(); 10451 10452 if (S != nullptr) 10453 PushDeclContext(S, DRD); 10454 else 10455 CurContext = DRD; 10456 10457 PushExpressionEvaluationContext(PotentiallyEvaluated); 10458 10459 QualType ReductionType = DRD->getType(); 10460 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 10461 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 10462 // uses semantics of argument handles by value, but it should be passed by 10463 // reference. C lang does not support references, so pass all parameters as 10464 // pointers. 10465 // Create 'T omp_in;' variable. 10466 auto *OmpInParm = 10467 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 10468 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 10469 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 10470 // uses semantics of argument handles by value, but it should be passed by 10471 // reference. C lang does not support references, so pass all parameters as 10472 // pointers. 10473 // Create 'T omp_out;' variable. 10474 auto *OmpOutParm = 10475 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 10476 if (S != nullptr) { 10477 PushOnScopeChains(OmpInParm, S); 10478 PushOnScopeChains(OmpOutParm, S); 10479 } else { 10480 DRD->addDecl(OmpInParm); 10481 DRD->addDecl(OmpOutParm); 10482 } 10483 } 10484 10485 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 10486 auto *DRD = cast<OMPDeclareReductionDecl>(D); 10487 DiscardCleanupsInEvaluationContext(); 10488 PopExpressionEvaluationContext(); 10489 10490 PopDeclContext(); 10491 PopFunctionScopeInfo(); 10492 10493 if (Combiner != nullptr) 10494 DRD->setCombiner(Combiner); 10495 else 10496 DRD->setInvalidDecl(); 10497 } 10498 10499 void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 10500 auto *DRD = cast<OMPDeclareReductionDecl>(D); 10501 10502 // Enter new function scope. 10503 PushFunctionScope(); 10504 getCurFunction()->setHasBranchProtectedScope(); 10505 10506 if (S != nullptr) 10507 PushDeclContext(S, DRD); 10508 else 10509 CurContext = DRD; 10510 10511 PushExpressionEvaluationContext(PotentiallyEvaluated); 10512 10513 QualType ReductionType = DRD->getType(); 10514 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 10515 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 10516 // uses semantics of argument handles by value, but it should be passed by 10517 // reference. C lang does not support references, so pass all parameters as 10518 // pointers. 10519 // Create 'T omp_priv;' variable. 10520 auto *OmpPrivParm = 10521 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 10522 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 10523 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 10524 // uses semantics of argument handles by value, but it should be passed by 10525 // reference. C lang does not support references, so pass all parameters as 10526 // pointers. 10527 // Create 'T omp_orig;' variable. 10528 auto *OmpOrigParm = 10529 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 10530 if (S != nullptr) { 10531 PushOnScopeChains(OmpPrivParm, S); 10532 PushOnScopeChains(OmpOrigParm, S); 10533 } else { 10534 DRD->addDecl(OmpPrivParm); 10535 DRD->addDecl(OmpOrigParm); 10536 } 10537 } 10538 10539 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, 10540 Expr *Initializer) { 10541 auto *DRD = cast<OMPDeclareReductionDecl>(D); 10542 DiscardCleanupsInEvaluationContext(); 10543 PopExpressionEvaluationContext(); 10544 10545 PopDeclContext(); 10546 PopFunctionScopeInfo(); 10547 10548 if (Initializer != nullptr) 10549 DRD->setInitializer(Initializer); 10550 else 10551 DRD->setInvalidDecl(); 10552 } 10553 10554 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 10555 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 10556 for (auto *D : DeclReductions.get()) { 10557 if (IsValid) { 10558 auto *DRD = cast<OMPDeclareReductionDecl>(D); 10559 if (S != nullptr) 10560 PushOnScopeChains(DRD, S, /*AddToContext=*/false); 10561 } else 10562 D->setInvalidDecl(); 10563 } 10564 return DeclReductions; 10565 } 10566 10567 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 10568 SourceLocation StartLoc, 10569 SourceLocation LParenLoc, 10570 SourceLocation EndLoc) { 10571 Expr *ValExpr = NumTeams; 10572 10573 // OpenMP [teams Constrcut, Restrictions] 10574 // The num_teams expression must evaluate to a positive integer value. 10575 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 10576 /*StrictlyPositive=*/true)) 10577 return nullptr; 10578 10579 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc); 10580 } 10581 10582 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 10583 SourceLocation StartLoc, 10584 SourceLocation LParenLoc, 10585 SourceLocation EndLoc) { 10586 Expr *ValExpr = ThreadLimit; 10587 10588 // OpenMP [teams Constrcut, Restrictions] 10589 // The thread_limit expression must evaluate to a positive integer value. 10590 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 10591 /*StrictlyPositive=*/true)) 10592 return nullptr; 10593 10594 return new (Context) 10595 OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc, EndLoc); 10596 } 10597 10598 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 10599 SourceLocation StartLoc, 10600 SourceLocation LParenLoc, 10601 SourceLocation EndLoc) { 10602 Expr *ValExpr = Priority; 10603 10604 // OpenMP [2.9.1, task Constrcut] 10605 // The priority-value is a non-negative numerical scalar expression. 10606 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority, 10607 /*StrictlyPositive=*/false)) 10608 return nullptr; 10609 10610 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc); 10611 } 10612 10613 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 10614 SourceLocation StartLoc, 10615 SourceLocation LParenLoc, 10616 SourceLocation EndLoc) { 10617 Expr *ValExpr = Grainsize; 10618 10619 // OpenMP [2.9.2, taskloop Constrcut] 10620 // The parameter of the grainsize clause must be a positive integer 10621 // expression. 10622 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize, 10623 /*StrictlyPositive=*/true)) 10624 return nullptr; 10625 10626 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc); 10627 } 10628 10629 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 10630 SourceLocation StartLoc, 10631 SourceLocation LParenLoc, 10632 SourceLocation EndLoc) { 10633 Expr *ValExpr = NumTasks; 10634 10635 // OpenMP [2.9.2, taskloop Constrcut] 10636 // The parameter of the num_tasks clause must be a positive integer 10637 // expression. 10638 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks, 10639 /*StrictlyPositive=*/true)) 10640 return nullptr; 10641 10642 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc); 10643 } 10644 10645 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 10646 SourceLocation LParenLoc, 10647 SourceLocation EndLoc) { 10648 // OpenMP [2.13.2, critical construct, Description] 10649 // ... where hint-expression is an integer constant expression that evaluates 10650 // to a valid lock hint. 10651 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 10652 if (HintExpr.isInvalid()) 10653 return nullptr; 10654 return new (Context) 10655 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 10656 } 10657 10658 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 10659 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 10660 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 10661 SourceLocation EndLoc) { 10662 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 10663 std::string Values; 10664 Values += "'"; 10665 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 10666 Values += "'"; 10667 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 10668 << Values << getOpenMPClauseName(OMPC_dist_schedule); 10669 return nullptr; 10670 } 10671 Expr *ValExpr = ChunkSize; 10672 Stmt *HelperValStmt = nullptr; 10673 if (ChunkSize) { 10674 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 10675 !ChunkSize->isInstantiationDependent() && 10676 !ChunkSize->containsUnexpandedParameterPack()) { 10677 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); 10678 ExprResult Val = 10679 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 10680 if (Val.isInvalid()) 10681 return nullptr; 10682 10683 ValExpr = Val.get(); 10684 10685 // OpenMP [2.7.1, Restrictions] 10686 // chunk_size must be a loop invariant integer expression with a positive 10687 // value. 10688 llvm::APSInt Result; 10689 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 10690 if (Result.isSigned() && !Result.isStrictlyPositive()) { 10691 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 10692 << "dist_schedule" << ChunkSize->getSourceRange(); 10693 return nullptr; 10694 } 10695 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) && 10696 !CurContext->isDependentContext()) { 10697 llvm::MapVector<Expr *, DeclRefExpr *> Captures; 10698 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 10699 HelperValStmt = buildPreInits(Context, Captures); 10700 } 10701 } 10702 } 10703 10704 return new (Context) 10705 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 10706 Kind, ValExpr, HelperValStmt); 10707 } 10708 10709 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 10710 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 10711 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 10712 SourceLocation KindLoc, SourceLocation EndLoc) { 10713 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)' 10714 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) { 10715 std::string Value; 10716 SourceLocation Loc; 10717 Value += "'"; 10718 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 10719 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 10720 OMPC_DEFAULTMAP_MODIFIER_tofrom); 10721 Loc = MLoc; 10722 } else { 10723 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 10724 OMPC_DEFAULTMAP_scalar); 10725 Loc = KindLoc; 10726 } 10727 Value += "'"; 10728 Diag(Loc, diag::err_omp_unexpected_clause_value) 10729 << Value << getOpenMPClauseName(OMPC_defaultmap); 10730 return nullptr; 10731 } 10732 10733 return new (Context) 10734 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 10735 } 10736 10737 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { 10738 DeclContext *CurLexicalContext = getCurLexicalContext(); 10739 if (!CurLexicalContext->isFileContext() && 10740 !CurLexicalContext->isExternCContext() && 10741 !CurLexicalContext->isExternCXXContext()) { 10742 Diag(Loc, diag::err_omp_region_not_file_context); 10743 return false; 10744 } 10745 if (IsInOpenMPDeclareTargetContext) { 10746 Diag(Loc, diag::err_omp_enclosed_declare_target); 10747 return false; 10748 } 10749 10750 IsInOpenMPDeclareTargetContext = true; 10751 return true; 10752 } 10753 10754 void Sema::ActOnFinishOpenMPDeclareTargetDirective() { 10755 assert(IsInOpenMPDeclareTargetContext && 10756 "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); 10757 10758 IsInOpenMPDeclareTargetContext = false; 10759 } 10760 10761 void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, 10762 CXXScopeSpec &ScopeSpec, 10763 const DeclarationNameInfo &Id, 10764 OMPDeclareTargetDeclAttr::MapTypeTy MT, 10765 NamedDeclSetType &SameDirectiveDecls) { 10766 LookupResult Lookup(*this, Id, LookupOrdinaryName); 10767 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 10768 10769 if (Lookup.isAmbiguous()) 10770 return; 10771 Lookup.suppressDiagnostics(); 10772 10773 if (!Lookup.isSingleResult()) { 10774 if (TypoCorrection Corrected = 10775 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, 10776 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this), 10777 CTK_ErrorRecovery)) { 10778 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 10779 << Id.getName()); 10780 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 10781 return; 10782 } 10783 10784 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 10785 return; 10786 } 10787 10788 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 10789 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) { 10790 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) 10791 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); 10792 10793 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) { 10794 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT); 10795 ND->addAttr(A); 10796 if (ASTMutationListener *ML = Context.getASTMutationListener()) 10797 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 10798 checkDeclIsAllowedInOpenMPTarget(nullptr, ND); 10799 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) { 10800 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link) 10801 << Id.getName(); 10802 } 10803 } else 10804 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 10805 } 10806 10807 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 10808 Sema &SemaRef, Decl *D) { 10809 if (!D) 10810 return; 10811 Decl *LD = nullptr; 10812 if (isa<TagDecl>(D)) { 10813 LD = cast<TagDecl>(D)->getDefinition(); 10814 } else if (isa<VarDecl>(D)) { 10815 LD = cast<VarDecl>(D)->getDefinition(); 10816 10817 // If this is an implicit variable that is legal and we do not need to do 10818 // anything. 10819 if (cast<VarDecl>(D)->isImplicit()) { 10820 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( 10821 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); 10822 D->addAttr(A); 10823 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) 10824 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 10825 return; 10826 } 10827 10828 } else if (isa<FunctionDecl>(D)) { 10829 const FunctionDecl *FD = nullptr; 10830 if (cast<FunctionDecl>(D)->hasBody(FD)) 10831 LD = const_cast<FunctionDecl *>(FD); 10832 10833 // If the definition is associated with the current declaration in the 10834 // target region (it can be e.g. a lambda) that is legal and we do not need 10835 // to do anything else. 10836 if (LD == D) { 10837 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( 10838 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); 10839 D->addAttr(A); 10840 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) 10841 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 10842 return; 10843 } 10844 } 10845 if (!LD) 10846 LD = D; 10847 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() && 10848 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) { 10849 // Outlined declaration is not declared target. 10850 if (LD->isOutOfLine()) { 10851 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context); 10852 SemaRef.Diag(SL, diag::note_used_here) << SR; 10853 } else { 10854 DeclContext *DC = LD->getDeclContext(); 10855 while (DC) { 10856 if (isa<FunctionDecl>(DC) && 10857 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>()) 10858 break; 10859 DC = DC->getParent(); 10860 } 10861 if (DC) 10862 return; 10863 10864 // Is not declared in target context. 10865 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context); 10866 SemaRef.Diag(SL, diag::note_used_here) << SR; 10867 } 10868 // Mark decl as declared target to prevent further diagnostic. 10869 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( 10870 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); 10871 D->addAttr(A); 10872 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) 10873 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 10874 } 10875 } 10876 10877 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 10878 Sema &SemaRef, DSAStackTy *Stack, 10879 ValueDecl *VD) { 10880 if (VD->hasAttr<OMPDeclareTargetDeclAttr>()) 10881 return true; 10882 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType())) 10883 return false; 10884 return true; 10885 } 10886 10887 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) { 10888 if (!D || D->isInvalidDecl()) 10889 return; 10890 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 10891 SourceLocation SL = E ? E->getLocStart() : D->getLocation(); 10892 // 2.10.6: threadprivate variable cannot appear in a declare target directive. 10893 if (VarDecl *VD = dyn_cast<VarDecl>(D)) { 10894 if (DSAStack->isThreadPrivate(VD)) { 10895 Diag(SL, diag::err_omp_threadprivate_in_target); 10896 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 10897 return; 10898 } 10899 } 10900 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { 10901 // Problem if any with var declared with incomplete type will be reported 10902 // as normal, so no need to check it here. 10903 if ((E || !VD->getType()->isIncompleteType()) && 10904 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) { 10905 // Mark decl as declared target to prevent further diagnostic. 10906 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) { 10907 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( 10908 Context, OMPDeclareTargetDeclAttr::MT_To); 10909 VD->addAttr(A); 10910 if (ASTMutationListener *ML = Context.getASTMutationListener()) 10911 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A); 10912 } 10913 return; 10914 } 10915 } 10916 if (!E) { 10917 // Checking declaration inside declare target region. 10918 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() && 10919 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) { 10920 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( 10921 Context, OMPDeclareTargetDeclAttr::MT_To); 10922 D->addAttr(A); 10923 if (ASTMutationListener *ML = Context.getASTMutationListener()) 10924 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 10925 } 10926 return; 10927 } 10928 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 10929 } 10930 10931 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList, 10932 SourceLocation StartLoc, 10933 SourceLocation LParenLoc, 10934 SourceLocation EndLoc) { 10935 MappableVarListInfo MVLI(VarList); 10936 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc); 10937 if (MVLI.ProcessedVarList.empty()) 10938 return nullptr; 10939 10940 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc, 10941 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 10942 MVLI.VarComponents); 10943 } 10944 10945 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList, 10946 SourceLocation StartLoc, 10947 SourceLocation LParenLoc, 10948 SourceLocation EndLoc) { 10949 MappableVarListInfo MVLI(VarList); 10950 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc); 10951 if (MVLI.ProcessedVarList.empty()) 10952 return nullptr; 10953 10954 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc, 10955 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 10956 MVLI.VarComponents); 10957 } 10958 10959 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 10960 SourceLocation StartLoc, 10961 SourceLocation LParenLoc, 10962 SourceLocation EndLoc) { 10963 MappableVarListInfo MVLI(VarList); 10964 SmallVector<Expr *, 8> PrivateCopies; 10965 SmallVector<Expr *, 8> Inits; 10966 10967 for (auto &RefExpr : VarList) { 10968 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 10969 SourceLocation ELoc; 10970 SourceRange ERange; 10971 Expr *SimpleRefExpr = RefExpr; 10972 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 10973 if (Res.second) { 10974 // It will be analyzed later. 10975 MVLI.ProcessedVarList.push_back(RefExpr); 10976 PrivateCopies.push_back(nullptr); 10977 Inits.push_back(nullptr); 10978 } 10979 ValueDecl *D = Res.first; 10980 if (!D) 10981 continue; 10982 10983 QualType Type = D->getType(); 10984 Type = Type.getNonReferenceType().getUnqualifiedType(); 10985 10986 auto *VD = dyn_cast<VarDecl>(D); 10987 10988 // Item should be a pointer or reference to pointer. 10989 if (!Type->isPointerType()) { 10990 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 10991 << 0 << RefExpr->getSourceRange(); 10992 continue; 10993 } 10994 10995 // Build the private variable and the expression that refers to it. 10996 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(), 10997 D->hasAttrs() ? &D->getAttrs() : nullptr); 10998 if (VDPrivate->isInvalidDecl()) 10999 continue; 11000 11001 CurContext->addDecl(VDPrivate); 11002 auto VDPrivateRefExpr = buildDeclRefExpr( 11003 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 11004 11005 // Add temporary variable to initialize the private copy of the pointer. 11006 auto *VDInit = 11007 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 11008 auto *VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 11009 RefExpr->getExprLoc()); 11010 AddInitializerToDecl(VDPrivate, 11011 DefaultLvalueConversion(VDInitRefExpr).get(), 11012 /*DirectInit=*/false, /*TypeMayContainAuto=*/false); 11013 11014 // If required, build a capture to implement the privatization initialized 11015 // with the current list item value. 11016 DeclRefExpr *Ref = nullptr; 11017 if (!VD) 11018 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 11019 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 11020 PrivateCopies.push_back(VDPrivateRefExpr); 11021 Inits.push_back(VDInitRefExpr); 11022 11023 // We need to add a data sharing attribute for this variable to make sure it 11024 // is correctly captured. A variable that shows up in a use_device_ptr has 11025 // similar properties of a first private variable. 11026 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 11027 11028 // Create a mappable component for the list item. List items in this clause 11029 // only need a component. 11030 MVLI.VarBaseDeclarations.push_back(D); 11031 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 11032 MVLI.VarComponents.back().push_back( 11033 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D)); 11034 } 11035 11036 if (MVLI.ProcessedVarList.empty()) 11037 return nullptr; 11038 11039 return OMPUseDevicePtrClause::Create( 11040 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList, 11041 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents); 11042 } 11043 11044 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 11045 SourceLocation StartLoc, 11046 SourceLocation LParenLoc, 11047 SourceLocation EndLoc) { 11048 MappableVarListInfo MVLI(VarList); 11049 for (auto &RefExpr : VarList) { 11050 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 11051 SourceLocation ELoc; 11052 SourceRange ERange; 11053 Expr *SimpleRefExpr = RefExpr; 11054 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 11055 if (Res.second) { 11056 // It will be analyzed later. 11057 MVLI.ProcessedVarList.push_back(RefExpr); 11058 } 11059 ValueDecl *D = Res.first; 11060 if (!D) 11061 continue; 11062 11063 QualType Type = D->getType(); 11064 // item should be a pointer or array or reference to pointer or array 11065 if (!Type.getNonReferenceType()->isPointerType() && 11066 !Type.getNonReferenceType()->isArrayType()) { 11067 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 11068 << 0 << RefExpr->getSourceRange(); 11069 continue; 11070 } 11071 11072 // Check if the declaration in the clause does not show up in any data 11073 // sharing attribute. 11074 auto DVar = DSAStack->getTopDSA(D, false); 11075 if (isOpenMPPrivate(DVar.CKind)) { 11076 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 11077 << getOpenMPClauseName(DVar.CKind) 11078 << getOpenMPClauseName(OMPC_is_device_ptr) 11079 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 11080 ReportOriginalDSA(*this, DSAStack, D, DVar); 11081 continue; 11082 } 11083 11084 Expr *ConflictExpr; 11085 if (DSAStack->checkMappableExprComponentListsForDecl( 11086 D, /*CurrentRegionOnly=*/true, 11087 [&ConflictExpr]( 11088 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 11089 OpenMPClauseKind) -> bool { 11090 ConflictExpr = R.front().getAssociatedExpression(); 11091 return true; 11092 })) { 11093 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 11094 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 11095 << ConflictExpr->getSourceRange(); 11096 continue; 11097 } 11098 11099 // Store the components in the stack so that they can be used to check 11100 // against other clauses later on. 11101 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D); 11102 DSAStack->addMappableExpressionComponents( 11103 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 11104 11105 // Record the expression we've just processed. 11106 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 11107 11108 // Create a mappable component for the list item. List items in this clause 11109 // only need a component. We use a null declaration to signal fields in 11110 // 'this'. 11111 assert((isa<DeclRefExpr>(SimpleRefExpr) || 11112 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 11113 "Unexpected device pointer expression!"); 11114 MVLI.VarBaseDeclarations.push_back( 11115 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 11116 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 11117 MVLI.VarComponents.back().push_back(MC); 11118 } 11119 11120 if (MVLI.ProcessedVarList.empty()) 11121 return nullptr; 11122 11123 return OMPIsDevicePtrClause::Create( 11124 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList, 11125 MVLI.VarBaseDeclarations, MVLI.VarComponents); 11126 } 11127