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 /// 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/Basic/OpenMPKinds.h" 26 #include "clang/Sema/Initialization.h" 27 #include "clang/Sema/Lookup.h" 28 #include "clang/Sema/Scope.h" 29 #include "clang/Sema/ScopeInfo.h" 30 #include "clang/Sema/SemaInternal.h" 31 #include "llvm/ADT/PointerEmbeddedInt.h" 32 using namespace clang; 33 34 //===----------------------------------------------------------------------===// 35 // Stack of data-sharing attributes for variables 36 //===----------------------------------------------------------------------===// 37 38 static const Expr *checkMapClauseExpressionBase( 39 Sema &SemaRef, Expr *E, 40 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 41 OpenMPClauseKind CKind, bool NoDiagnose); 42 43 namespace { 44 /// Default data sharing attributes, which can be applied to directive. 45 enum DefaultDataSharingAttributes { 46 DSA_unspecified = 0, /// Data sharing attribute not specified. 47 DSA_none = 1 << 0, /// Default data sharing attribute 'none'. 48 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'. 49 }; 50 51 /// Attributes of the defaultmap clause. 52 enum DefaultMapAttributes { 53 DMA_unspecified, /// Default mapping is not specified. 54 DMA_tofrom_scalar, /// Default mapping is 'tofrom:scalar'. 55 }; 56 57 /// Stack for tracking declarations used in OpenMP directives and 58 /// clauses and their data-sharing attributes. 59 class DSAStackTy { 60 public: 61 struct DSAVarData { 62 OpenMPDirectiveKind DKind = OMPD_unknown; 63 OpenMPClauseKind CKind = OMPC_unknown; 64 const Expr *RefExpr = nullptr; 65 DeclRefExpr *PrivateCopy = nullptr; 66 SourceLocation ImplicitDSALoc; 67 DSAVarData() = default; 68 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 69 const Expr *RefExpr, DeclRefExpr *PrivateCopy, 70 SourceLocation ImplicitDSALoc) 71 : DKind(DKind), CKind(CKind), RefExpr(RefExpr), 72 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {} 73 }; 74 using OperatorOffsetTy = 75 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>; 76 using DoacrossDependMapTy = 77 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>; 78 79 private: 80 struct DSAInfo { 81 OpenMPClauseKind Attributes = OMPC_unknown; 82 /// Pointer to a reference expression and a flag which shows that the 83 /// variable is marked as lastprivate(true) or not (false). 84 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr; 85 DeclRefExpr *PrivateCopy = nullptr; 86 }; 87 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>; 88 using AlignedMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>; 89 using LCDeclInfo = std::pair<unsigned, VarDecl *>; 90 using LoopControlVariablesMapTy = 91 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>; 92 /// Struct that associates a component with the clause kind where they are 93 /// found. 94 struct MappedExprComponentTy { 95 OMPClauseMappableExprCommon::MappableExprComponentLists Components; 96 OpenMPClauseKind Kind = OMPC_unknown; 97 }; 98 using MappedExprComponentsTy = 99 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>; 100 using CriticalsWithHintsTy = 101 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>; 102 struct ReductionData { 103 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>; 104 SourceRange ReductionRange; 105 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp; 106 ReductionData() = default; 107 void set(BinaryOperatorKind BO, SourceRange RR) { 108 ReductionRange = RR; 109 ReductionOp = BO; 110 } 111 void set(const Expr *RefExpr, SourceRange RR) { 112 ReductionRange = RR; 113 ReductionOp = RefExpr; 114 } 115 }; 116 using DeclReductionMapTy = 117 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>; 118 119 struct SharingMapTy { 120 DeclSAMapTy SharingMap; 121 DeclReductionMapTy ReductionMap; 122 AlignedMapTy AlignedMap; 123 MappedExprComponentsTy MappedExprComponents; 124 LoopControlVariablesMapTy LCVMap; 125 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified; 126 SourceLocation DefaultAttrLoc; 127 DefaultMapAttributes DefaultMapAttr = DMA_unspecified; 128 SourceLocation DefaultMapAttrLoc; 129 OpenMPDirectiveKind Directive = OMPD_unknown; 130 DeclarationNameInfo DirectiveName; 131 Scope *CurScope = nullptr; 132 SourceLocation ConstructLoc; 133 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to 134 /// get the data (loop counters etc.) about enclosing loop-based construct. 135 /// This data is required during codegen. 136 DoacrossDependMapTy DoacrossDepends; 137 /// first argument (Expr *) contains optional argument of the 138 /// 'ordered' clause, the second one is true if the regions has 'ordered' 139 /// clause, false otherwise. 140 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion; 141 bool NowaitRegion = false; 142 bool CancelRegion = false; 143 unsigned AssociatedLoops = 1; 144 SourceLocation InnerTeamsRegionLoc; 145 /// Reference to the taskgroup task_reduction reference expression. 146 Expr *TaskgroupReductionRef = nullptr; 147 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 148 Scope *CurScope, SourceLocation Loc) 149 : Directive(DKind), DirectiveName(Name), CurScope(CurScope), 150 ConstructLoc(Loc) {} 151 SharingMapTy() = default; 152 }; 153 154 using StackTy = SmallVector<SharingMapTy, 4>; 155 156 /// Stack of used declaration and their data-sharing attributes. 157 DeclSAMapTy Threadprivates; 158 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr; 159 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack; 160 /// true, if check for DSA must be from parent directive, false, if 161 /// from current directive. 162 OpenMPClauseKind ClauseKindMode = OMPC_unknown; 163 Sema &SemaRef; 164 bool ForceCapturing = false; 165 CriticalsWithHintsTy Criticals; 166 167 using iterator = StackTy::const_reverse_iterator; 168 169 DSAVarData getDSA(iterator &Iter, ValueDecl *D) const; 170 171 /// Checks if the variable is a local for OpenMP region. 172 bool isOpenMPLocal(VarDecl *D, iterator Iter) const; 173 174 bool isStackEmpty() const { 175 return Stack.empty() || 176 Stack.back().second != CurrentNonCapturingFunctionScope || 177 Stack.back().first.empty(); 178 } 179 180 /// Vector of previously declared requires directives 181 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls; 182 183 public: 184 explicit DSAStackTy(Sema &S) : SemaRef(S) {} 185 186 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 187 OpenMPClauseKind getClauseParsingMode() const { 188 assert(isClauseParsingMode() && "Must be in clause parsing mode."); 189 return ClauseKindMode; 190 } 191 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 192 193 bool isForceVarCapturing() const { return ForceCapturing; } 194 void setForceVarCapturing(bool V) { ForceCapturing = V; } 195 196 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 197 Scope *CurScope, SourceLocation Loc) { 198 if (Stack.empty() || 199 Stack.back().second != CurrentNonCapturingFunctionScope) 200 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope); 201 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc); 202 Stack.back().first.back().DefaultAttrLoc = Loc; 203 } 204 205 void pop() { 206 assert(!Stack.back().first.empty() && 207 "Data-sharing attributes stack is empty!"); 208 Stack.back().first.pop_back(); 209 } 210 211 /// Start new OpenMP region stack in new non-capturing function. 212 void pushFunction() { 213 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction(); 214 assert(!isa<CapturingScopeInfo>(CurFnScope)); 215 CurrentNonCapturingFunctionScope = CurFnScope; 216 } 217 /// Pop region stack for non-capturing function. 218 void popFunction(const FunctionScopeInfo *OldFSI) { 219 if (!Stack.empty() && Stack.back().second == OldFSI) { 220 assert(Stack.back().first.empty()); 221 Stack.pop_back(); 222 } 223 CurrentNonCapturingFunctionScope = nullptr; 224 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) { 225 if (!isa<CapturingScopeInfo>(FSI)) { 226 CurrentNonCapturingFunctionScope = FSI; 227 break; 228 } 229 } 230 } 231 232 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) { 233 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint); 234 } 235 const std::pair<const OMPCriticalDirective *, llvm::APSInt> 236 getCriticalWithHint(const DeclarationNameInfo &Name) const { 237 auto I = Criticals.find(Name.getAsString()); 238 if (I != Criticals.end()) 239 return I->second; 240 return std::make_pair(nullptr, llvm::APSInt()); 241 } 242 /// If 'aligned' declaration for given variable \a D was not seen yet, 243 /// add it and return NULL; otherwise return previous occurrence's expression 244 /// for diagnostics. 245 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE); 246 247 /// Register specified variable as loop control variable. 248 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture); 249 /// Check if the specified variable is a loop control variable for 250 /// current region. 251 /// \return The index of the loop control variable in the list of associated 252 /// for-loops (from outer to inner). 253 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const; 254 /// Check if the specified variable is a loop control variable for 255 /// parent region. 256 /// \return The index of the loop control variable in the list of associated 257 /// for-loops (from outer to inner). 258 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const; 259 /// Get the loop control variable for the I-th loop (or nullptr) in 260 /// parent directive. 261 const ValueDecl *getParentLoopControlVariable(unsigned I) const; 262 263 /// Adds explicit data sharing attribute to the specified declaration. 264 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 265 DeclRefExpr *PrivateCopy = nullptr); 266 267 /// Adds additional information for the reduction items with the reduction id 268 /// represented as an operator. 269 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 270 BinaryOperatorKind BOK); 271 /// Adds additional information for the reduction items with the reduction id 272 /// represented as reduction identifier. 273 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 274 const Expr *ReductionRef); 275 /// Returns the location and reduction operation from the innermost parent 276 /// region for the given \p D. 277 const DSAVarData 278 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 279 BinaryOperatorKind &BOK, 280 Expr *&TaskgroupDescriptor) const; 281 /// Returns the location and reduction operation from the innermost parent 282 /// region for the given \p D. 283 const DSAVarData 284 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 285 const Expr *&ReductionRef, 286 Expr *&TaskgroupDescriptor) const; 287 /// Return reduction reference expression for the current taskgroup. 288 Expr *getTaskgroupReductionRef() const { 289 assert(Stack.back().first.back().Directive == OMPD_taskgroup && 290 "taskgroup reference expression requested for non taskgroup " 291 "directive."); 292 return Stack.back().first.back().TaskgroupReductionRef; 293 } 294 /// Checks if the given \p VD declaration is actually a taskgroup reduction 295 /// descriptor variable at the \p Level of OpenMP regions. 296 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const { 297 return Stack.back().first[Level].TaskgroupReductionRef && 298 cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef) 299 ->getDecl() == VD; 300 } 301 302 /// Returns data sharing attributes from top of the stack for the 303 /// specified declaration. 304 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 305 /// Returns data-sharing attributes for the specified declaration. 306 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const; 307 /// Checks if the specified variables has data-sharing attributes which 308 /// match specified \a CPred predicate in any directive which matches \a DPred 309 /// predicate. 310 const DSAVarData 311 hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 312 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 313 bool FromParent) const; 314 /// Checks if the specified variables has data-sharing attributes which 315 /// match specified \a CPred predicate in any innermost directive which 316 /// matches \a DPred predicate. 317 const DSAVarData 318 hasInnermostDSA(ValueDecl *D, 319 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 320 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 321 bool FromParent) const; 322 /// Checks if the specified variables has explicit data-sharing 323 /// attributes which match specified \a CPred predicate at the specified 324 /// OpenMP region. 325 bool hasExplicitDSA(const ValueDecl *D, 326 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 327 unsigned Level, bool NotLastprivate = false) const; 328 329 /// Returns true if the directive at level \Level matches in the 330 /// specified \a DPred predicate. 331 bool hasExplicitDirective( 332 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 333 unsigned Level) const; 334 335 /// Finds a directive which matches specified \a DPred predicate. 336 bool hasDirective( 337 const llvm::function_ref<bool( 338 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)> 339 DPred, 340 bool FromParent) const; 341 342 /// Returns currently analyzed directive. 343 OpenMPDirectiveKind getCurrentDirective() const { 344 return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive; 345 } 346 /// Returns directive kind at specified level. 347 OpenMPDirectiveKind getDirective(unsigned Level) const { 348 assert(!isStackEmpty() && "No directive at specified level."); 349 return Stack.back().first[Level].Directive; 350 } 351 /// Returns parent directive. 352 OpenMPDirectiveKind getParentDirective() const { 353 if (isStackEmpty() || Stack.back().first.size() == 1) 354 return OMPD_unknown; 355 return std::next(Stack.back().first.rbegin())->Directive; 356 } 357 358 /// Add requires decl to internal vector 359 void addRequiresDecl(OMPRequiresDecl *RD) { 360 RequiresDecls.push_back(RD); 361 } 362 363 /// Checks for a duplicate clause amongst previously declared requires 364 /// directives 365 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const { 366 bool IsDuplicate = false; 367 for (OMPClause *CNew : ClauseList) { 368 for (const OMPRequiresDecl *D : RequiresDecls) { 369 for (const OMPClause *CPrev : D->clauselists()) { 370 if (CNew->getClauseKind() == CPrev->getClauseKind()) { 371 SemaRef.Diag(CNew->getBeginLoc(), 372 diag::err_omp_requires_clause_redeclaration) 373 << getOpenMPClauseName(CNew->getClauseKind()); 374 SemaRef.Diag(CPrev->getBeginLoc(), 375 diag::note_omp_requires_previous_clause) 376 << getOpenMPClauseName(CPrev->getClauseKind()); 377 IsDuplicate = true; 378 } 379 } 380 } 381 } 382 return IsDuplicate; 383 } 384 385 /// Set default data sharing attribute to none. 386 void setDefaultDSANone(SourceLocation Loc) { 387 assert(!isStackEmpty()); 388 Stack.back().first.back().DefaultAttr = DSA_none; 389 Stack.back().first.back().DefaultAttrLoc = Loc; 390 } 391 /// Set default data sharing attribute to shared. 392 void setDefaultDSAShared(SourceLocation Loc) { 393 assert(!isStackEmpty()); 394 Stack.back().first.back().DefaultAttr = DSA_shared; 395 Stack.back().first.back().DefaultAttrLoc = Loc; 396 } 397 /// Set default data mapping attribute to 'tofrom:scalar'. 398 void setDefaultDMAToFromScalar(SourceLocation Loc) { 399 assert(!isStackEmpty()); 400 Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar; 401 Stack.back().first.back().DefaultMapAttrLoc = Loc; 402 } 403 404 DefaultDataSharingAttributes getDefaultDSA() const { 405 return isStackEmpty() ? DSA_unspecified 406 : Stack.back().first.back().DefaultAttr; 407 } 408 SourceLocation getDefaultDSALocation() const { 409 return isStackEmpty() ? SourceLocation() 410 : Stack.back().first.back().DefaultAttrLoc; 411 } 412 DefaultMapAttributes getDefaultDMA() const { 413 return isStackEmpty() ? DMA_unspecified 414 : Stack.back().first.back().DefaultMapAttr; 415 } 416 DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const { 417 return Stack.back().first[Level].DefaultMapAttr; 418 } 419 SourceLocation getDefaultDMALocation() const { 420 return isStackEmpty() ? SourceLocation() 421 : Stack.back().first.back().DefaultMapAttrLoc; 422 } 423 424 /// Checks if the specified variable is a threadprivate. 425 bool isThreadPrivate(VarDecl *D) { 426 const DSAVarData DVar = getTopDSA(D, false); 427 return isOpenMPThreadPrivate(DVar.CKind); 428 } 429 430 /// Marks current region as ordered (it has an 'ordered' clause). 431 void setOrderedRegion(bool IsOrdered, const Expr *Param, 432 OMPOrderedClause *Clause) { 433 assert(!isStackEmpty()); 434 if (IsOrdered) 435 Stack.back().first.back().OrderedRegion.emplace(Param, Clause); 436 else 437 Stack.back().first.back().OrderedRegion.reset(); 438 } 439 /// Returns true, if region is ordered (has associated 'ordered' clause), 440 /// false - otherwise. 441 bool isOrderedRegion() const { 442 if (isStackEmpty()) 443 return false; 444 return Stack.back().first.rbegin()->OrderedRegion.hasValue(); 445 } 446 /// Returns optional parameter for the ordered region. 447 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const { 448 if (isStackEmpty() || 449 !Stack.back().first.rbegin()->OrderedRegion.hasValue()) 450 return std::make_pair(nullptr, nullptr); 451 return Stack.back().first.rbegin()->OrderedRegion.getValue(); 452 } 453 /// Returns true, if parent region is ordered (has associated 454 /// 'ordered' clause), false - otherwise. 455 bool isParentOrderedRegion() const { 456 if (isStackEmpty() || Stack.back().first.size() == 1) 457 return false; 458 return std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue(); 459 } 460 /// Returns optional parameter for the ordered region. 461 std::pair<const Expr *, OMPOrderedClause *> 462 getParentOrderedRegionParam() const { 463 if (isStackEmpty() || Stack.back().first.size() == 1 || 464 !std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue()) 465 return std::make_pair(nullptr, nullptr); 466 return std::next(Stack.back().first.rbegin())->OrderedRegion.getValue(); 467 } 468 /// Marks current region as nowait (it has a 'nowait' clause). 469 void setNowaitRegion(bool IsNowait = true) { 470 assert(!isStackEmpty()); 471 Stack.back().first.back().NowaitRegion = IsNowait; 472 } 473 /// Returns true, if parent region is nowait (has associated 474 /// 'nowait' clause), false - otherwise. 475 bool isParentNowaitRegion() const { 476 if (isStackEmpty() || Stack.back().first.size() == 1) 477 return false; 478 return std::next(Stack.back().first.rbegin())->NowaitRegion; 479 } 480 /// Marks parent region as cancel region. 481 void setParentCancelRegion(bool Cancel = true) { 482 if (!isStackEmpty() && Stack.back().first.size() > 1) { 483 auto &StackElemRef = *std::next(Stack.back().first.rbegin()); 484 StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel; 485 } 486 } 487 /// Return true if current region has inner cancel construct. 488 bool isCancelRegion() const { 489 return isStackEmpty() ? false : Stack.back().first.back().CancelRegion; 490 } 491 492 /// Set collapse value for the region. 493 void setAssociatedLoops(unsigned Val) { 494 assert(!isStackEmpty()); 495 Stack.back().first.back().AssociatedLoops = Val; 496 } 497 /// Return collapse value for region. 498 unsigned getAssociatedLoops() const { 499 return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops; 500 } 501 502 /// Marks current target region as one with closely nested teams 503 /// region. 504 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 505 if (!isStackEmpty() && Stack.back().first.size() > 1) { 506 std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc = 507 TeamsRegionLoc; 508 } 509 } 510 /// Returns true, if current region has closely nested teams region. 511 bool hasInnerTeamsRegion() const { 512 return getInnerTeamsRegionLoc().isValid(); 513 } 514 /// Returns location of the nested teams region (if any). 515 SourceLocation getInnerTeamsRegionLoc() const { 516 return isStackEmpty() ? SourceLocation() 517 : Stack.back().first.back().InnerTeamsRegionLoc; 518 } 519 520 Scope *getCurScope() const { 521 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope; 522 } 523 SourceLocation getConstructLoc() const { 524 return isStackEmpty() ? SourceLocation() 525 : Stack.back().first.back().ConstructLoc; 526 } 527 528 /// Do the check specified in \a Check to all component lists and return true 529 /// if any issue is found. 530 bool checkMappableExprComponentListsForDecl( 531 const ValueDecl *VD, bool CurrentRegionOnly, 532 const llvm::function_ref< 533 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 534 OpenMPClauseKind)> 535 Check) const { 536 if (isStackEmpty()) 537 return false; 538 auto SI = Stack.back().first.rbegin(); 539 auto SE = Stack.back().first.rend(); 540 541 if (SI == SE) 542 return false; 543 544 if (CurrentRegionOnly) 545 SE = std::next(SI); 546 else 547 std::advance(SI, 1); 548 549 for (; SI != SE; ++SI) { 550 auto MI = SI->MappedExprComponents.find(VD); 551 if (MI != SI->MappedExprComponents.end()) 552 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 553 MI->second.Components) 554 if (Check(L, MI->second.Kind)) 555 return true; 556 } 557 return false; 558 } 559 560 /// Do the check specified in \a Check to all component lists at a given level 561 /// and return true if any issue is found. 562 bool checkMappableExprComponentListsForDeclAtLevel( 563 const ValueDecl *VD, unsigned Level, 564 const llvm::function_ref< 565 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 566 OpenMPClauseKind)> 567 Check) const { 568 if (isStackEmpty()) 569 return false; 570 571 auto StartI = Stack.back().first.begin(); 572 auto EndI = Stack.back().first.end(); 573 if (std::distance(StartI, EndI) <= (int)Level) 574 return false; 575 std::advance(StartI, Level); 576 577 auto MI = StartI->MappedExprComponents.find(VD); 578 if (MI != StartI->MappedExprComponents.end()) 579 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 580 MI->second.Components) 581 if (Check(L, MI->second.Kind)) 582 return true; 583 return false; 584 } 585 586 /// Create a new mappable expression component list associated with a given 587 /// declaration and initialize it with the provided list of components. 588 void addMappableExpressionComponents( 589 const ValueDecl *VD, 590 OMPClauseMappableExprCommon::MappableExprComponentListRef Components, 591 OpenMPClauseKind WhereFoundClauseKind) { 592 assert(!isStackEmpty() && 593 "Not expecting to retrieve components from a empty stack!"); 594 MappedExprComponentTy &MEC = 595 Stack.back().first.back().MappedExprComponents[VD]; 596 // Create new entry and append the new components there. 597 MEC.Components.resize(MEC.Components.size() + 1); 598 MEC.Components.back().append(Components.begin(), Components.end()); 599 MEC.Kind = WhereFoundClauseKind; 600 } 601 602 unsigned getNestingLevel() const { 603 assert(!isStackEmpty()); 604 return Stack.back().first.size() - 1; 605 } 606 void addDoacrossDependClause(OMPDependClause *C, 607 const OperatorOffsetTy &OpsOffs) { 608 assert(!isStackEmpty() && Stack.back().first.size() > 1); 609 SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin()); 610 assert(isOpenMPWorksharingDirective(StackElem.Directive)); 611 StackElem.DoacrossDepends.try_emplace(C, OpsOffs); 612 } 613 llvm::iterator_range<DoacrossDependMapTy::const_iterator> 614 getDoacrossDependClauses() const { 615 assert(!isStackEmpty()); 616 const SharingMapTy &StackElem = Stack.back().first.back(); 617 if (isOpenMPWorksharingDirective(StackElem.Directive)) { 618 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends; 619 return llvm::make_range(Ref.begin(), Ref.end()); 620 } 621 return llvm::make_range(StackElem.DoacrossDepends.end(), 622 StackElem.DoacrossDepends.end()); 623 } 624 }; 625 bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) { 626 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) || 627 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown; 628 } 629 630 } // namespace 631 632 static const Expr *getExprAsWritten(const Expr *E) { 633 if (const auto *ExprTemp = dyn_cast<ExprWithCleanups>(E)) 634 E = ExprTemp->getSubExpr(); 635 636 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 637 E = MTE->GetTemporaryExpr(); 638 639 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 640 E = Binder->getSubExpr(); 641 642 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 643 E = ICE->getSubExprAsWritten(); 644 return E->IgnoreParens(); 645 } 646 647 static Expr *getExprAsWritten(Expr *E) { 648 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E))); 649 } 650 651 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) { 652 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) 653 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 654 D = ME->getMemberDecl(); 655 const auto *VD = dyn_cast<VarDecl>(D); 656 const auto *FD = dyn_cast<FieldDecl>(D); 657 if (VD != nullptr) { 658 VD = VD->getCanonicalDecl(); 659 D = VD; 660 } else { 661 assert(FD); 662 FD = FD->getCanonicalDecl(); 663 D = FD; 664 } 665 return D; 666 } 667 668 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 669 return const_cast<ValueDecl *>( 670 getCanonicalDecl(const_cast<const ValueDecl *>(D))); 671 } 672 673 DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter, 674 ValueDecl *D) const { 675 D = getCanonicalDecl(D); 676 auto *VD = dyn_cast<VarDecl>(D); 677 const auto *FD = dyn_cast<FieldDecl>(D); 678 DSAVarData DVar; 679 if (isStackEmpty() || Iter == Stack.back().first.rend()) { 680 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 681 // in a region but not in construct] 682 // File-scope or namespace-scope variables referenced in called routines 683 // in the region are shared unless they appear in a threadprivate 684 // directive. 685 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD)) 686 DVar.CKind = OMPC_shared; 687 688 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 689 // in a region but not in construct] 690 // Variables with static storage duration that are declared in called 691 // routines in the region are shared. 692 if (VD && VD->hasGlobalStorage()) 693 DVar.CKind = OMPC_shared; 694 695 // Non-static data members are shared by default. 696 if (FD) 697 DVar.CKind = OMPC_shared; 698 699 return DVar; 700 } 701 702 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 703 // in a Construct, C/C++, predetermined, p.1] 704 // Variables with automatic storage duration that are declared in a scope 705 // inside the construct are private. 706 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 707 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 708 DVar.CKind = OMPC_private; 709 return DVar; 710 } 711 712 DVar.DKind = Iter->Directive; 713 // Explicitly specified attributes and local variables with predetermined 714 // attributes. 715 if (Iter->SharingMap.count(D)) { 716 const DSAInfo &Data = Iter->SharingMap.lookup(D); 717 DVar.RefExpr = Data.RefExpr.getPointer(); 718 DVar.PrivateCopy = Data.PrivateCopy; 719 DVar.CKind = Data.Attributes; 720 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 721 return DVar; 722 } 723 724 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 725 // in a Construct, C/C++, implicitly determined, p.1] 726 // In a parallel or task construct, the data-sharing attributes of these 727 // variables are determined by the default clause, if present. 728 switch (Iter->DefaultAttr) { 729 case DSA_shared: 730 DVar.CKind = OMPC_shared; 731 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 732 return DVar; 733 case DSA_none: 734 return DVar; 735 case DSA_unspecified: 736 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 737 // in a Construct, implicitly determined, p.2] 738 // In a parallel construct, if no default clause is present, these 739 // variables are shared. 740 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 741 if (isOpenMPParallelDirective(DVar.DKind) || 742 isOpenMPTeamsDirective(DVar.DKind)) { 743 DVar.CKind = OMPC_shared; 744 return DVar; 745 } 746 747 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 748 // in a Construct, implicitly determined, p.4] 749 // In a task construct, if no default clause is present, a variable that in 750 // the enclosing context is determined to be shared by all implicit tasks 751 // bound to the current team is shared. 752 if (isOpenMPTaskingDirective(DVar.DKind)) { 753 DSAVarData DVarTemp; 754 iterator I = Iter, E = Stack.back().first.rend(); 755 do { 756 ++I; 757 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 758 // Referenced in a Construct, implicitly determined, p.6] 759 // In a task construct, if no default clause is present, a variable 760 // whose data-sharing attribute is not determined by the rules above is 761 // firstprivate. 762 DVarTemp = getDSA(I, D); 763 if (DVarTemp.CKind != OMPC_shared) { 764 DVar.RefExpr = nullptr; 765 DVar.CKind = OMPC_firstprivate; 766 return DVar; 767 } 768 } while (I != E && !isParallelOrTaskRegion(I->Directive)); 769 DVar.CKind = 770 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 771 return DVar; 772 } 773 } 774 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 775 // in a Construct, implicitly determined, p.3] 776 // For constructs other than task, if no default clause is present, these 777 // variables inherit their data-sharing attributes from the enclosing 778 // context. 779 return getDSA(++Iter, D); 780 } 781 782 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D, 783 const Expr *NewDE) { 784 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 785 D = getCanonicalDecl(D); 786 SharingMapTy &StackElem = Stack.back().first.back(); 787 auto It = StackElem.AlignedMap.find(D); 788 if (It == StackElem.AlignedMap.end()) { 789 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 790 StackElem.AlignedMap[D] = NewDE; 791 return nullptr; 792 } 793 assert(It->second && "Unexpected nullptr expr in the aligned map"); 794 return It->second; 795 } 796 797 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) { 798 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 799 D = getCanonicalDecl(D); 800 SharingMapTy &StackElem = Stack.back().first.back(); 801 StackElem.LCVMap.try_emplace( 802 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)); 803 } 804 805 const DSAStackTy::LCDeclInfo 806 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const { 807 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 808 D = getCanonicalDecl(D); 809 const SharingMapTy &StackElem = Stack.back().first.back(); 810 auto It = StackElem.LCVMap.find(D); 811 if (It != StackElem.LCVMap.end()) 812 return It->second; 813 return {0, nullptr}; 814 } 815 816 const DSAStackTy::LCDeclInfo 817 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const { 818 assert(!isStackEmpty() && Stack.back().first.size() > 1 && 819 "Data-sharing attributes stack is empty"); 820 D = getCanonicalDecl(D); 821 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin()); 822 auto It = StackElem.LCVMap.find(D); 823 if (It != StackElem.LCVMap.end()) 824 return It->second; 825 return {0, nullptr}; 826 } 827 828 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const { 829 assert(!isStackEmpty() && Stack.back().first.size() > 1 && 830 "Data-sharing attributes stack is empty"); 831 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin()); 832 if (StackElem.LCVMap.size() < I) 833 return nullptr; 834 for (const auto &Pair : StackElem.LCVMap) 835 if (Pair.second.first == I) 836 return Pair.first; 837 return nullptr; 838 } 839 840 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 841 DeclRefExpr *PrivateCopy) { 842 D = getCanonicalDecl(D); 843 if (A == OMPC_threadprivate) { 844 DSAInfo &Data = Threadprivates[D]; 845 Data.Attributes = A; 846 Data.RefExpr.setPointer(E); 847 Data.PrivateCopy = nullptr; 848 } else { 849 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 850 DSAInfo &Data = Stack.back().first.back().SharingMap[D]; 851 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 852 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 853 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 854 (isLoopControlVariable(D).first && A == OMPC_private)); 855 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 856 Data.RefExpr.setInt(/*IntVal=*/true); 857 return; 858 } 859 const bool IsLastprivate = 860 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 861 Data.Attributes = A; 862 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 863 Data.PrivateCopy = PrivateCopy; 864 if (PrivateCopy) { 865 DSAInfo &Data = 866 Stack.back().first.back().SharingMap[PrivateCopy->getDecl()]; 867 Data.Attributes = A; 868 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 869 Data.PrivateCopy = nullptr; 870 } 871 } 872 } 873 874 /// Build a variable declaration for OpenMP loop iteration variable. 875 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 876 StringRef Name, const AttrVec *Attrs = nullptr, 877 DeclRefExpr *OrigRef = nullptr) { 878 DeclContext *DC = SemaRef.CurContext; 879 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 880 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 881 auto *Decl = 882 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 883 if (Attrs) { 884 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 885 I != E; ++I) 886 Decl->addAttr(*I); 887 } 888 Decl->setImplicit(); 889 if (OrigRef) { 890 Decl->addAttr( 891 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef)); 892 } 893 return Decl; 894 } 895 896 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 897 SourceLocation Loc, 898 bool RefersToCapture = false) { 899 D->setReferenced(); 900 D->markUsed(S.Context); 901 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 902 SourceLocation(), D, RefersToCapture, Loc, Ty, 903 VK_LValue); 904 } 905 906 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 907 BinaryOperatorKind BOK) { 908 D = getCanonicalDecl(D); 909 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 910 assert( 911 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction && 912 "Additional reduction info may be specified only for reduction items."); 913 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D]; 914 assert(ReductionData.ReductionRange.isInvalid() && 915 Stack.back().first.back().Directive == OMPD_taskgroup && 916 "Additional reduction info may be specified only once for reduction " 917 "items."); 918 ReductionData.set(BOK, SR); 919 Expr *&TaskgroupReductionRef = 920 Stack.back().first.back().TaskgroupReductionRef; 921 if (!TaskgroupReductionRef) { 922 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 923 SemaRef.Context.VoidPtrTy, ".task_red."); 924 TaskgroupReductionRef = 925 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 926 } 927 } 928 929 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 930 const Expr *ReductionRef) { 931 D = getCanonicalDecl(D); 932 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 933 assert( 934 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction && 935 "Additional reduction info may be specified only for reduction items."); 936 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D]; 937 assert(ReductionData.ReductionRange.isInvalid() && 938 Stack.back().first.back().Directive == OMPD_taskgroup && 939 "Additional reduction info may be specified only once for reduction " 940 "items."); 941 ReductionData.set(ReductionRef, SR); 942 Expr *&TaskgroupReductionRef = 943 Stack.back().first.back().TaskgroupReductionRef; 944 if (!TaskgroupReductionRef) { 945 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 946 SemaRef.Context.VoidPtrTy, ".task_red."); 947 TaskgroupReductionRef = 948 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 949 } 950 } 951 952 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 953 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK, 954 Expr *&TaskgroupDescriptor) const { 955 D = getCanonicalDecl(D); 956 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 957 if (Stack.back().first.empty()) 958 return DSAVarData(); 959 for (iterator I = std::next(Stack.back().first.rbegin(), 1), 960 E = Stack.back().first.rend(); 961 I != E; std::advance(I, 1)) { 962 const DSAInfo &Data = I->SharingMap.lookup(D); 963 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup) 964 continue; 965 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 966 if (!ReductionData.ReductionOp || 967 ReductionData.ReductionOp.is<const Expr *>()) 968 return DSAVarData(); 969 SR = ReductionData.ReductionRange; 970 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>(); 971 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 972 "expression for the descriptor is not " 973 "set."); 974 TaskgroupDescriptor = I->TaskgroupReductionRef; 975 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(), 976 Data.PrivateCopy, I->DefaultAttrLoc); 977 } 978 return DSAVarData(); 979 } 980 981 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 982 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef, 983 Expr *&TaskgroupDescriptor) const { 984 D = getCanonicalDecl(D); 985 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 986 if (Stack.back().first.empty()) 987 return DSAVarData(); 988 for (iterator I = std::next(Stack.back().first.rbegin(), 1), 989 E = Stack.back().first.rend(); 990 I != E; std::advance(I, 1)) { 991 const DSAInfo &Data = I->SharingMap.lookup(D); 992 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup) 993 continue; 994 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 995 if (!ReductionData.ReductionOp || 996 !ReductionData.ReductionOp.is<const Expr *>()) 997 return DSAVarData(); 998 SR = ReductionData.ReductionRange; 999 ReductionRef = ReductionData.ReductionOp.get<const Expr *>(); 1000 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1001 "expression for the descriptor is not " 1002 "set."); 1003 TaskgroupDescriptor = I->TaskgroupReductionRef; 1004 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(), 1005 Data.PrivateCopy, I->DefaultAttrLoc); 1006 } 1007 return DSAVarData(); 1008 } 1009 1010 bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const { 1011 D = D->getCanonicalDecl(); 1012 if (!isStackEmpty()) { 1013 iterator I = Iter, E = Stack.back().first.rend(); 1014 Scope *TopScope = nullptr; 1015 while (I != E && !isParallelOrTaskRegion(I->Directive) && 1016 !isOpenMPTargetExecutionDirective(I->Directive)) 1017 ++I; 1018 if (I == E) 1019 return false; 1020 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; 1021 Scope *CurScope = getCurScope(); 1022 while (CurScope != TopScope && !CurScope->isDeclScope(D)) 1023 CurScope = CurScope->getParent(); 1024 return CurScope != TopScope; 1025 } 1026 return false; 1027 } 1028 1029 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, 1030 bool FromParent) { 1031 D = getCanonicalDecl(D); 1032 DSAVarData DVar; 1033 1034 auto *VD = dyn_cast<VarDecl>(D); 1035 auto TI = Threadprivates.find(D); 1036 if (TI != Threadprivates.end()) { 1037 DVar.RefExpr = TI->getSecond().RefExpr.getPointer(); 1038 DVar.CKind = OMPC_threadprivate; 1039 return DVar; 1040 } 1041 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) { 1042 DVar.RefExpr = buildDeclRefExpr( 1043 SemaRef, VD, D->getType().getNonReferenceType(), 1044 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation()); 1045 DVar.CKind = OMPC_threadprivate; 1046 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1047 return DVar; 1048 } 1049 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1050 // in a Construct, C/C++, predetermined, p.1] 1051 // Variables appearing in threadprivate directives are threadprivate. 1052 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 1053 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1054 SemaRef.getLangOpts().OpenMPUseTLS && 1055 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 1056 (VD && VD->getStorageClass() == SC_Register && 1057 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 1058 DVar.RefExpr = buildDeclRefExpr( 1059 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation()); 1060 DVar.CKind = OMPC_threadprivate; 1061 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1062 return DVar; 1063 } 1064 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD && 1065 VD->isLocalVarDeclOrParm() && !isStackEmpty() && 1066 !isLoopControlVariable(D).first) { 1067 iterator IterTarget = 1068 std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(), 1069 [](const SharingMapTy &Data) { 1070 return isOpenMPTargetExecutionDirective(Data.Directive); 1071 }); 1072 if (IterTarget != Stack.back().first.rend()) { 1073 iterator ParentIterTarget = std::next(IterTarget, 1); 1074 for (iterator Iter = Stack.back().first.rbegin(); 1075 Iter != ParentIterTarget; std::advance(Iter, 1)) { 1076 if (isOpenMPLocal(VD, Iter)) { 1077 DVar.RefExpr = 1078 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1079 D->getLocation()); 1080 DVar.CKind = OMPC_threadprivate; 1081 return DVar; 1082 } 1083 } 1084 if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) { 1085 auto DSAIter = IterTarget->SharingMap.find(D); 1086 if (DSAIter != IterTarget->SharingMap.end() && 1087 isOpenMPPrivate(DSAIter->getSecond().Attributes)) { 1088 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer(); 1089 DVar.CKind = OMPC_threadprivate; 1090 return DVar; 1091 } 1092 iterator End = Stack.back().first.rend(); 1093 if (!SemaRef.isOpenMPCapturedByRef( 1094 D, std::distance(ParentIterTarget, End))) { 1095 DVar.RefExpr = 1096 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1097 IterTarget->ConstructLoc); 1098 DVar.CKind = OMPC_threadprivate; 1099 return DVar; 1100 } 1101 } 1102 } 1103 } 1104 1105 if (isStackEmpty()) 1106 // Not in OpenMP execution region and top scope was already checked. 1107 return DVar; 1108 1109 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1110 // in a Construct, C/C++, predetermined, p.4] 1111 // Static data members are shared. 1112 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1113 // in a Construct, C/C++, predetermined, p.7] 1114 // Variables with static storage duration that are declared in a scope 1115 // inside the construct are shared. 1116 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; }; 1117 if (VD && VD->isStaticDataMember()) { 1118 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent); 1119 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1120 return DVar; 1121 1122 DVar.CKind = OMPC_shared; 1123 return DVar; 1124 } 1125 1126 QualType Type = D->getType().getNonReferenceType().getCanonicalType(); 1127 bool IsConstant = Type.isConstant(SemaRef.getASTContext()); 1128 Type = SemaRef.getASTContext().getBaseElementType(Type); 1129 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1130 // in a Construct, C/C++, predetermined, p.6] 1131 // Variables with const qualified type having no mutable member are 1132 // shared. 1133 const CXXRecordDecl *RD = 1134 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; 1135 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 1136 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate()) 1137 RD = CTD->getTemplatedDecl(); 1138 if (IsConstant && 1139 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() && 1140 RD->hasMutableFields())) { 1141 // Variables with const-qualified type having no mutable member may be 1142 // listed in a firstprivate clause, even if they are static data members. 1143 DSAVarData DVarTemp = 1144 hasDSA(D, [](OpenMPClauseKind C) { return C == OMPC_firstprivate; }, 1145 MatchesAlways, FromParent); 1146 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr) 1147 return DVarTemp; 1148 1149 DVar.CKind = OMPC_shared; 1150 return DVar; 1151 } 1152 1153 // Explicitly specified attributes and local variables with predetermined 1154 // attributes. 1155 iterator I = Stack.back().first.rbegin(); 1156 iterator EndI = Stack.back().first.rend(); 1157 if (FromParent && I != EndI) 1158 std::advance(I, 1); 1159 auto It = I->SharingMap.find(D); 1160 if (It != I->SharingMap.end()) { 1161 const DSAInfo &Data = It->getSecond(); 1162 DVar.RefExpr = Data.RefExpr.getPointer(); 1163 DVar.PrivateCopy = Data.PrivateCopy; 1164 DVar.CKind = Data.Attributes; 1165 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1166 DVar.DKind = I->Directive; 1167 } 1168 1169 return DVar; 1170 } 1171 1172 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1173 bool FromParent) const { 1174 if (isStackEmpty()) { 1175 iterator I; 1176 return getDSA(I, D); 1177 } 1178 D = getCanonicalDecl(D); 1179 iterator StartI = Stack.back().first.rbegin(); 1180 iterator EndI = Stack.back().first.rend(); 1181 if (FromParent && StartI != EndI) 1182 std::advance(StartI, 1); 1183 return getDSA(StartI, D); 1184 } 1185 1186 const DSAStackTy::DSAVarData 1187 DSAStackTy::hasDSA(ValueDecl *D, 1188 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1189 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1190 bool FromParent) const { 1191 if (isStackEmpty()) 1192 return {}; 1193 D = getCanonicalDecl(D); 1194 iterator I = Stack.back().first.rbegin(); 1195 iterator EndI = Stack.back().first.rend(); 1196 if (FromParent && I != EndI) 1197 std::advance(I, 1); 1198 for (; I != EndI; std::advance(I, 1)) { 1199 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive)) 1200 continue; 1201 iterator NewI = I; 1202 DSAVarData DVar = getDSA(NewI, D); 1203 if (I == NewI && CPred(DVar.CKind)) 1204 return DVar; 1205 } 1206 return {}; 1207 } 1208 1209 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 1210 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1211 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1212 bool FromParent) const { 1213 if (isStackEmpty()) 1214 return {}; 1215 D = getCanonicalDecl(D); 1216 iterator StartI = Stack.back().first.rbegin(); 1217 iterator EndI = Stack.back().first.rend(); 1218 if (FromParent && StartI != EndI) 1219 std::advance(StartI, 1); 1220 if (StartI == EndI || !DPred(StartI->Directive)) 1221 return {}; 1222 iterator NewI = StartI; 1223 DSAVarData DVar = getDSA(NewI, D); 1224 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData(); 1225 } 1226 1227 bool DSAStackTy::hasExplicitDSA( 1228 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1229 unsigned Level, bool NotLastprivate) const { 1230 if (isStackEmpty()) 1231 return false; 1232 D = getCanonicalDecl(D); 1233 auto StartI = Stack.back().first.begin(); 1234 auto EndI = Stack.back().first.end(); 1235 if (std::distance(StartI, EndI) <= (int)Level) 1236 return false; 1237 std::advance(StartI, Level); 1238 auto I = StartI->SharingMap.find(D); 1239 return (I != StartI->SharingMap.end()) && 1240 I->getSecond().RefExpr.getPointer() && 1241 CPred(I->getSecond().Attributes) && 1242 (!NotLastprivate || !I->getSecond().RefExpr.getInt()); 1243 } 1244 1245 bool DSAStackTy::hasExplicitDirective( 1246 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1247 unsigned Level) const { 1248 if (isStackEmpty()) 1249 return false; 1250 auto StartI = Stack.back().first.begin(); 1251 auto EndI = Stack.back().first.end(); 1252 if (std::distance(StartI, EndI) <= (int)Level) 1253 return false; 1254 std::advance(StartI, Level); 1255 return DPred(StartI->Directive); 1256 } 1257 1258 bool DSAStackTy::hasDirective( 1259 const llvm::function_ref<bool(OpenMPDirectiveKind, 1260 const DeclarationNameInfo &, SourceLocation)> 1261 DPred, 1262 bool FromParent) const { 1263 // We look only in the enclosing region. 1264 if (isStackEmpty()) 1265 return false; 1266 auto StartI = std::next(Stack.back().first.rbegin()); 1267 auto EndI = Stack.back().first.rend(); 1268 if (FromParent && StartI != EndI) 1269 StartI = std::next(StartI); 1270 for (auto I = StartI, EE = EndI; I != EE; ++I) { 1271 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 1272 return true; 1273 } 1274 return false; 1275 } 1276 1277 void Sema::InitDataSharingAttributesStack() { 1278 VarDataSharingAttributesStack = new DSAStackTy(*this); 1279 } 1280 1281 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 1282 1283 void Sema::pushOpenMPFunctionRegion() { 1284 DSAStack->pushFunction(); 1285 } 1286 1287 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) { 1288 DSAStack->popFunction(OldFSI); 1289 } 1290 1291 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const { 1292 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1293 1294 ASTContext &Ctx = getASTContext(); 1295 bool IsByRef = true; 1296 1297 // Find the directive that is associated with the provided scope. 1298 D = cast<ValueDecl>(D->getCanonicalDecl()); 1299 QualType Ty = D->getType(); 1300 1301 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 1302 // This table summarizes how a given variable should be passed to the device 1303 // given its type and the clauses where it appears. This table is based on 1304 // the description in OpenMP 4.5 [2.10.4, target Construct] and 1305 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 1306 // 1307 // ========================================================================= 1308 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 1309 // | |(tofrom:scalar)| | pvt | | | | 1310 // ========================================================================= 1311 // | scl | | | | - | | bycopy| 1312 // | scl | | - | x | - | - | bycopy| 1313 // | scl | | x | - | - | - | null | 1314 // | scl | x | | | - | | byref | 1315 // | scl | x | - | x | - | - | bycopy| 1316 // | scl | x | x | - | - | - | null | 1317 // | scl | | - | - | - | x | byref | 1318 // | scl | x | - | - | - | x | byref | 1319 // 1320 // | agg | n.a. | | | - | | byref | 1321 // | agg | n.a. | - | x | - | - | byref | 1322 // | agg | n.a. | x | - | - | - | null | 1323 // | agg | n.a. | - | - | - | x | byref | 1324 // | agg | n.a. | - | - | - | x[] | byref | 1325 // 1326 // | ptr | n.a. | | | - | | bycopy| 1327 // | ptr | n.a. | - | x | - | - | bycopy| 1328 // | ptr | n.a. | x | - | - | - | null | 1329 // | ptr | n.a. | - | - | - | x | byref | 1330 // | ptr | n.a. | - | - | - | x[] | bycopy| 1331 // | ptr | n.a. | - | - | x | | bycopy| 1332 // | ptr | n.a. | - | - | x | x | bycopy| 1333 // | ptr | n.a. | - | - | x | x[] | bycopy| 1334 // ========================================================================= 1335 // Legend: 1336 // scl - scalar 1337 // ptr - pointer 1338 // agg - aggregate 1339 // x - applies 1340 // - - invalid in this combination 1341 // [] - mapped with an array section 1342 // byref - should be mapped by reference 1343 // byval - should be mapped by value 1344 // null - initialize a local variable to null on the device 1345 // 1346 // Observations: 1347 // - All scalar declarations that show up in a map clause have to be passed 1348 // by reference, because they may have been mapped in the enclosing data 1349 // environment. 1350 // - If the scalar value does not fit the size of uintptr, it has to be 1351 // passed by reference, regardless the result in the table above. 1352 // - For pointers mapped by value that have either an implicit map or an 1353 // array section, the runtime library may pass the NULL value to the 1354 // device instead of the value passed to it by the compiler. 1355 1356 if (Ty->isReferenceType()) 1357 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 1358 1359 // Locate map clauses and see if the variable being captured is referred to 1360 // in any of those clauses. Here we only care about variables, not fields, 1361 // because fields are part of aggregates. 1362 bool IsVariableUsedInMapClause = false; 1363 bool IsVariableAssociatedWithSection = false; 1364 1365 DSAStack->checkMappableExprComponentListsForDeclAtLevel( 1366 D, Level, 1367 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D]( 1368 OMPClauseMappableExprCommon::MappableExprComponentListRef 1369 MapExprComponents, 1370 OpenMPClauseKind WhereFoundClauseKind) { 1371 // Only the map clause information influences how a variable is 1372 // captured. E.g. is_device_ptr does not require changing the default 1373 // behavior. 1374 if (WhereFoundClauseKind != OMPC_map) 1375 return false; 1376 1377 auto EI = MapExprComponents.rbegin(); 1378 auto EE = MapExprComponents.rend(); 1379 1380 assert(EI != EE && "Invalid map expression!"); 1381 1382 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 1383 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 1384 1385 ++EI; 1386 if (EI == EE) 1387 return false; 1388 1389 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 1390 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 1391 isa<MemberExpr>(EI->getAssociatedExpression())) { 1392 IsVariableAssociatedWithSection = true; 1393 // There is nothing more we need to know about this variable. 1394 return true; 1395 } 1396 1397 // Keep looking for more map info. 1398 return false; 1399 }); 1400 1401 if (IsVariableUsedInMapClause) { 1402 // If variable is identified in a map clause it is always captured by 1403 // reference except if it is a pointer that is dereferenced somehow. 1404 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 1405 } else { 1406 // By default, all the data that has a scalar type is mapped by copy 1407 // (except for reduction variables). 1408 IsByRef = 1409 !Ty->isScalarType() || 1410 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar || 1411 DSAStack->hasExplicitDSA( 1412 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level); 1413 } 1414 } 1415 1416 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 1417 IsByRef = 1418 !DSAStack->hasExplicitDSA( 1419 D, 1420 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; }, 1421 Level, /*NotLastprivate=*/true) && 1422 // If the variable is artificial and must be captured by value - try to 1423 // capture by value. 1424 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() && 1425 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()); 1426 } 1427 1428 // When passing data by copy, we need to make sure it fits the uintptr size 1429 // and alignment, because the runtime library only deals with uintptr types. 1430 // If it does not fit the uintptr size, we need to pass the data by reference 1431 // instead. 1432 if (!IsByRef && 1433 (Ctx.getTypeSizeInChars(Ty) > 1434 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 1435 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 1436 IsByRef = true; 1437 } 1438 1439 return IsByRef; 1440 } 1441 1442 unsigned Sema::getOpenMPNestingLevel() const { 1443 assert(getLangOpts().OpenMP); 1444 return DSAStack->getNestingLevel(); 1445 } 1446 1447 bool Sema::isInOpenMPTargetExecutionDirective() const { 1448 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) && 1449 !DSAStack->isClauseParsingMode()) || 1450 DSAStack->hasDirective( 1451 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 1452 SourceLocation) -> bool { 1453 return isOpenMPTargetExecutionDirective(K); 1454 }, 1455 false); 1456 } 1457 1458 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) { 1459 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1460 D = getCanonicalDecl(D); 1461 1462 // If we are attempting to capture a global variable in a directive with 1463 // 'target' we return true so that this global is also mapped to the device. 1464 // 1465 auto *VD = dyn_cast<VarDecl>(D); 1466 if (VD && !VD->hasLocalStorage()) { 1467 if (isInOpenMPDeclareTargetContext() && 1468 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) { 1469 // Try to mark variable as declare target if it is used in capturing 1470 // regions. 1471 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 1472 checkDeclIsAllowedInOpenMPTarget(nullptr, VD); 1473 return nullptr; 1474 } else if (isInOpenMPTargetExecutionDirective()) { 1475 // If the declaration is enclosed in a 'declare target' directive, 1476 // then it should not be captured. 1477 // 1478 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 1479 return nullptr; 1480 return VD; 1481 } 1482 } 1483 1484 if (DSAStack->getCurrentDirective() != OMPD_unknown && 1485 (!DSAStack->isClauseParsingMode() || 1486 DSAStack->getParentDirective() != OMPD_unknown)) { 1487 auto &&Info = DSAStack->isLoopControlVariable(D); 1488 if (Info.first || 1489 (VD && VD->hasLocalStorage() && 1490 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) || 1491 (VD && DSAStack->isForceVarCapturing())) 1492 return VD ? VD : Info.second; 1493 DSAStackTy::DSAVarData DVarPrivate = 1494 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 1495 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind)) 1496 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1497 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, 1498 [](OpenMPDirectiveKind) { return true; }, 1499 DSAStack->isClauseParsingMode()); 1500 if (DVarPrivate.CKind != OMPC_unknown) 1501 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1502 } 1503 return nullptr; 1504 } 1505 1506 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex, 1507 unsigned Level) const { 1508 SmallVector<OpenMPDirectiveKind, 4> Regions; 1509 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level)); 1510 FunctionScopesIndex -= Regions.size(); 1511 } 1512 1513 bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const { 1514 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1515 return DSAStack->hasExplicitDSA( 1516 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) || 1517 (DSAStack->isClauseParsingMode() && 1518 DSAStack->getClauseParsingMode() == OMPC_private) || 1519 // Consider taskgroup reduction descriptor variable a private to avoid 1520 // possible capture in the region. 1521 (DSAStack->hasExplicitDirective( 1522 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; }, 1523 Level) && 1524 DSAStack->isTaskgroupReductionRef(D, Level)); 1525 } 1526 1527 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D, 1528 unsigned Level) { 1529 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1530 D = getCanonicalDecl(D); 1531 OpenMPClauseKind OMPC = OMPC_unknown; 1532 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) { 1533 const unsigned NewLevel = I - 1; 1534 if (DSAStack->hasExplicitDSA(D, 1535 [&OMPC](const OpenMPClauseKind K) { 1536 if (isOpenMPPrivate(K)) { 1537 OMPC = K; 1538 return true; 1539 } 1540 return false; 1541 }, 1542 NewLevel)) 1543 break; 1544 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel( 1545 D, NewLevel, 1546 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 1547 OpenMPClauseKind) { return true; })) { 1548 OMPC = OMPC_map; 1549 break; 1550 } 1551 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 1552 NewLevel)) { 1553 OMPC = OMPC_map; 1554 if (D->getType()->isScalarType() && 1555 DSAStack->getDefaultDMAAtLevel(NewLevel) != 1556 DefaultMapAttributes::DMA_tofrom_scalar) 1557 OMPC = OMPC_firstprivate; 1558 break; 1559 } 1560 } 1561 if (OMPC != OMPC_unknown) 1562 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC)); 1563 } 1564 1565 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, 1566 unsigned Level) const { 1567 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1568 // Return true if the current level is no longer enclosed in a target region. 1569 1570 const auto *VD = dyn_cast<VarDecl>(D); 1571 return VD && !VD->hasLocalStorage() && 1572 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 1573 Level); 1574 } 1575 1576 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 1577 1578 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 1579 const DeclarationNameInfo &DirName, 1580 Scope *CurScope, SourceLocation Loc) { 1581 DSAStack->push(DKind, DirName, CurScope, Loc); 1582 PushExpressionEvaluationContext( 1583 ExpressionEvaluationContext::PotentiallyEvaluated); 1584 } 1585 1586 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 1587 DSAStack->setClauseParsingMode(K); 1588 } 1589 1590 void Sema::EndOpenMPClause() { 1591 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 1592 } 1593 1594 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 1595 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 1596 // A variable of class type (or array thereof) that appears in a lastprivate 1597 // clause requires an accessible, unambiguous default constructor for the 1598 // class type, unless the list item is also specified in a firstprivate 1599 // clause. 1600 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 1601 for (OMPClause *C : D->clauses()) { 1602 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 1603 SmallVector<Expr *, 8> PrivateCopies; 1604 for (Expr *DE : Clause->varlists()) { 1605 if (DE->isValueDependent() || DE->isTypeDependent()) { 1606 PrivateCopies.push_back(nullptr); 1607 continue; 1608 } 1609 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 1610 auto *VD = cast<VarDecl>(DRE->getDecl()); 1611 QualType Type = VD->getType().getNonReferenceType(); 1612 const DSAStackTy::DSAVarData DVar = 1613 DSAStack->getTopDSA(VD, /*FromParent=*/false); 1614 if (DVar.CKind == OMPC_lastprivate) { 1615 // Generate helper private variable and initialize it with the 1616 // default value. The address of the original variable is replaced 1617 // by the address of the new private variable in CodeGen. This new 1618 // variable is not added to IdResolver, so the code in the OpenMP 1619 // region uses original variable for proper diagnostics. 1620 VarDecl *VDPrivate = buildVarDecl( 1621 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 1622 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE); 1623 ActOnUninitializedDecl(VDPrivate); 1624 if (VDPrivate->isInvalidDecl()) 1625 continue; 1626 PrivateCopies.push_back(buildDeclRefExpr( 1627 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 1628 } else { 1629 // The variable is also a firstprivate, so initialization sequence 1630 // for private copy is generated already. 1631 PrivateCopies.push_back(nullptr); 1632 } 1633 } 1634 // Set initializers to private copies if no errors were found. 1635 if (PrivateCopies.size() == Clause->varlist_size()) 1636 Clause->setPrivateCopies(PrivateCopies); 1637 } 1638 } 1639 } 1640 1641 DSAStack->pop(); 1642 DiscardCleanupsInEvaluationContext(); 1643 PopExpressionEvaluationContext(); 1644 } 1645 1646 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 1647 Expr *NumIterations, Sema &SemaRef, 1648 Scope *S, DSAStackTy *Stack); 1649 1650 namespace { 1651 1652 class VarDeclFilterCCC final : public CorrectionCandidateCallback { 1653 private: 1654 Sema &SemaRef; 1655 1656 public: 1657 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 1658 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1659 NamedDecl *ND = Candidate.getCorrectionDecl(); 1660 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 1661 return VD->hasGlobalStorage() && 1662 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1663 SemaRef.getCurScope()); 1664 } 1665 return false; 1666 } 1667 }; 1668 1669 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback { 1670 private: 1671 Sema &SemaRef; 1672 1673 public: 1674 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 1675 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1676 NamedDecl *ND = Candidate.getCorrectionDecl(); 1677 if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) { 1678 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1679 SemaRef.getCurScope()); 1680 } 1681 return false; 1682 } 1683 }; 1684 1685 } // namespace 1686 1687 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 1688 CXXScopeSpec &ScopeSpec, 1689 const DeclarationNameInfo &Id) { 1690 LookupResult Lookup(*this, Id, LookupOrdinaryName); 1691 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 1692 1693 if (Lookup.isAmbiguous()) 1694 return ExprError(); 1695 1696 VarDecl *VD; 1697 if (!Lookup.isSingleResult()) { 1698 if (TypoCorrection Corrected = CorrectTypo( 1699 Id, LookupOrdinaryName, CurScope, nullptr, 1700 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) { 1701 diagnoseTypo(Corrected, 1702 PDiag(Lookup.empty() 1703 ? diag::err_undeclared_var_use_suggest 1704 : diag::err_omp_expected_var_arg_suggest) 1705 << Id.getName()); 1706 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 1707 } else { 1708 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 1709 : diag::err_omp_expected_var_arg) 1710 << Id.getName(); 1711 return ExprError(); 1712 } 1713 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) { 1714 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 1715 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 1716 return ExprError(); 1717 } 1718 Lookup.suppressDiagnostics(); 1719 1720 // OpenMP [2.9.2, Syntax, C/C++] 1721 // Variables must be file-scope, namespace-scope, or static block-scope. 1722 if (!VD->hasGlobalStorage()) { 1723 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 1724 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal(); 1725 bool IsDecl = 1726 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1727 Diag(VD->getLocation(), 1728 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1729 << VD; 1730 return ExprError(); 1731 } 1732 1733 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 1734 NamedDecl *ND = CanonicalVD; 1735 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 1736 // A threadprivate directive for file-scope variables must appear outside 1737 // any definition or declaration. 1738 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 1739 !getCurLexicalContext()->isTranslationUnit()) { 1740 Diag(Id.getLoc(), diag::err_omp_var_scope) 1741 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1742 bool IsDecl = 1743 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1744 Diag(VD->getLocation(), 1745 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1746 << VD; 1747 return ExprError(); 1748 } 1749 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 1750 // A threadprivate directive for static class member variables must appear 1751 // in the class definition, in the same scope in which the member 1752 // variables are declared. 1753 if (CanonicalVD->isStaticDataMember() && 1754 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 1755 Diag(Id.getLoc(), diag::err_omp_var_scope) 1756 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1757 bool IsDecl = 1758 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1759 Diag(VD->getLocation(), 1760 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1761 << VD; 1762 return ExprError(); 1763 } 1764 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 1765 // A threadprivate directive for namespace-scope variables must appear 1766 // outside any definition or declaration other than the namespace 1767 // definition itself. 1768 if (CanonicalVD->getDeclContext()->isNamespace() && 1769 (!getCurLexicalContext()->isFileContext() || 1770 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 1771 Diag(Id.getLoc(), diag::err_omp_var_scope) 1772 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1773 bool IsDecl = 1774 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1775 Diag(VD->getLocation(), 1776 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1777 << VD; 1778 return ExprError(); 1779 } 1780 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 1781 // A threadprivate directive for static block-scope variables must appear 1782 // in the scope of the variable and not in a nested scope. 1783 if (CanonicalVD->isStaticLocal() && CurScope && 1784 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 1785 Diag(Id.getLoc(), diag::err_omp_var_scope) 1786 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1787 bool IsDecl = 1788 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1789 Diag(VD->getLocation(), 1790 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1791 << VD; 1792 return ExprError(); 1793 } 1794 1795 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 1796 // A threadprivate directive must lexically precede all references to any 1797 // of the variables in its list. 1798 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) { 1799 Diag(Id.getLoc(), diag::err_omp_var_used) 1800 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1801 return ExprError(); 1802 } 1803 1804 QualType ExprType = VD->getType().getNonReferenceType(); 1805 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 1806 SourceLocation(), VD, 1807 /*RefersToEnclosingVariableOrCapture=*/false, 1808 Id.getLoc(), ExprType, VK_LValue); 1809 } 1810 1811 Sema::DeclGroupPtrTy 1812 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 1813 ArrayRef<Expr *> VarList) { 1814 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 1815 CurContext->addDecl(D); 1816 return DeclGroupPtrTy::make(DeclGroupRef(D)); 1817 } 1818 return nullptr; 1819 } 1820 1821 namespace { 1822 class LocalVarRefChecker final 1823 : public ConstStmtVisitor<LocalVarRefChecker, bool> { 1824 Sema &SemaRef; 1825 1826 public: 1827 bool VisitDeclRefExpr(const DeclRefExpr *E) { 1828 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 1829 if (VD->hasLocalStorage()) { 1830 SemaRef.Diag(E->getBeginLoc(), 1831 diag::err_omp_local_var_in_threadprivate_init) 1832 << E->getSourceRange(); 1833 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 1834 << VD << VD->getSourceRange(); 1835 return true; 1836 } 1837 } 1838 return false; 1839 } 1840 bool VisitStmt(const Stmt *S) { 1841 for (const Stmt *Child : S->children()) { 1842 if (Child && Visit(Child)) 1843 return true; 1844 } 1845 return false; 1846 } 1847 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 1848 }; 1849 } // namespace 1850 1851 OMPThreadPrivateDecl * 1852 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 1853 SmallVector<Expr *, 8> Vars; 1854 for (Expr *RefExpr : VarList) { 1855 auto *DE = cast<DeclRefExpr>(RefExpr); 1856 auto *VD = cast<VarDecl>(DE->getDecl()); 1857 SourceLocation ILoc = DE->getExprLoc(); 1858 1859 // Mark variable as used. 1860 VD->setReferenced(); 1861 VD->markUsed(Context); 1862 1863 QualType QType = VD->getType(); 1864 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 1865 // It will be analyzed later. 1866 Vars.push_back(DE); 1867 continue; 1868 } 1869 1870 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 1871 // A threadprivate variable must not have an incomplete type. 1872 if (RequireCompleteType(ILoc, VD->getType(), 1873 diag::err_omp_threadprivate_incomplete_type)) { 1874 continue; 1875 } 1876 1877 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 1878 // A threadprivate variable must not have a reference type. 1879 if (VD->getType()->isReferenceType()) { 1880 Diag(ILoc, diag::err_omp_ref_type_arg) 1881 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 1882 bool IsDecl = 1883 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1884 Diag(VD->getLocation(), 1885 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1886 << VD; 1887 continue; 1888 } 1889 1890 // Check if this is a TLS variable. If TLS is not being supported, produce 1891 // the corresponding diagnostic. 1892 if ((VD->getTLSKind() != VarDecl::TLS_None && 1893 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1894 getLangOpts().OpenMPUseTLS && 1895 getASTContext().getTargetInfo().isTLSSupported())) || 1896 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 1897 !VD->isLocalVarDecl())) { 1898 Diag(ILoc, diag::err_omp_var_thread_local) 1899 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 1900 bool IsDecl = 1901 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1902 Diag(VD->getLocation(), 1903 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1904 << VD; 1905 continue; 1906 } 1907 1908 // Check if initial value of threadprivate variable reference variable with 1909 // local storage (it is not supported by runtime). 1910 if (const Expr *Init = VD->getAnyInitializer()) { 1911 LocalVarRefChecker Checker(*this); 1912 if (Checker.Visit(Init)) 1913 continue; 1914 } 1915 1916 Vars.push_back(RefExpr); 1917 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 1918 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 1919 Context, SourceRange(Loc, Loc))); 1920 if (ASTMutationListener *ML = Context.getASTMutationListener()) 1921 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 1922 } 1923 OMPThreadPrivateDecl *D = nullptr; 1924 if (!Vars.empty()) { 1925 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 1926 Vars); 1927 D->setAccess(AS_public); 1928 } 1929 return D; 1930 } 1931 1932 Sema::DeclGroupPtrTy 1933 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc, 1934 ArrayRef<OMPClause *> ClauseList) { 1935 OMPRequiresDecl *D = nullptr; 1936 if (!CurContext->isFileContext()) { 1937 Diag(Loc, diag::err_omp_invalid_scope) << "requires"; 1938 } else { 1939 D = CheckOMPRequiresDecl(Loc, ClauseList); 1940 if (D) { 1941 CurContext->addDecl(D); 1942 DSAStack->addRequiresDecl(D); 1943 } 1944 } 1945 return DeclGroupPtrTy::make(DeclGroupRef(D)); 1946 } 1947 1948 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc, 1949 ArrayRef<OMPClause *> ClauseList) { 1950 if (!DSAStack->hasDuplicateRequiresClause(ClauseList)) 1951 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc, 1952 ClauseList); 1953 return nullptr; 1954 } 1955 1956 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 1957 const ValueDecl *D, 1958 const DSAStackTy::DSAVarData &DVar, 1959 bool IsLoopIterVar = false) { 1960 if (DVar.RefExpr) { 1961 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 1962 << getOpenMPClauseName(DVar.CKind); 1963 return; 1964 } 1965 enum { 1966 PDSA_StaticMemberShared, 1967 PDSA_StaticLocalVarShared, 1968 PDSA_LoopIterVarPrivate, 1969 PDSA_LoopIterVarLinear, 1970 PDSA_LoopIterVarLastprivate, 1971 PDSA_ConstVarShared, 1972 PDSA_GlobalVarShared, 1973 PDSA_TaskVarFirstprivate, 1974 PDSA_LocalVarPrivate, 1975 PDSA_Implicit 1976 } Reason = PDSA_Implicit; 1977 bool ReportHint = false; 1978 auto ReportLoc = D->getLocation(); 1979 auto *VD = dyn_cast<VarDecl>(D); 1980 if (IsLoopIterVar) { 1981 if (DVar.CKind == OMPC_private) 1982 Reason = PDSA_LoopIterVarPrivate; 1983 else if (DVar.CKind == OMPC_lastprivate) 1984 Reason = PDSA_LoopIterVarLastprivate; 1985 else 1986 Reason = PDSA_LoopIterVarLinear; 1987 } else if (isOpenMPTaskingDirective(DVar.DKind) && 1988 DVar.CKind == OMPC_firstprivate) { 1989 Reason = PDSA_TaskVarFirstprivate; 1990 ReportLoc = DVar.ImplicitDSALoc; 1991 } else if (VD && VD->isStaticLocal()) 1992 Reason = PDSA_StaticLocalVarShared; 1993 else if (VD && VD->isStaticDataMember()) 1994 Reason = PDSA_StaticMemberShared; 1995 else if (VD && VD->isFileVarDecl()) 1996 Reason = PDSA_GlobalVarShared; 1997 else if (D->getType().isConstant(SemaRef.getASTContext())) 1998 Reason = PDSA_ConstVarShared; 1999 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 2000 ReportHint = true; 2001 Reason = PDSA_LocalVarPrivate; 2002 } 2003 if (Reason != PDSA_Implicit) { 2004 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 2005 << Reason << ReportHint 2006 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 2007 } else if (DVar.ImplicitDSALoc.isValid()) { 2008 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 2009 << getOpenMPClauseName(DVar.CKind); 2010 } 2011 } 2012 2013 namespace { 2014 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> { 2015 DSAStackTy *Stack; 2016 Sema &SemaRef; 2017 bool ErrorFound = false; 2018 CapturedStmt *CS = nullptr; 2019 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate; 2020 llvm::SmallVector<Expr *, 4> ImplicitMap; 2021 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA; 2022 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations; 2023 2024 public: 2025 void VisitDeclRefExpr(DeclRefExpr *E) { 2026 if (E->isTypeDependent() || E->isValueDependent() || 2027 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 2028 return; 2029 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2030 VD = VD->getCanonicalDecl(); 2031 // Skip internally declared variables. 2032 if (VD->hasLocalStorage() && !CS->capturesVariable(VD)) 2033 return; 2034 2035 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 2036 // Check if the variable has explicit DSA set and stop analysis if it so. 2037 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second) 2038 return; 2039 2040 // Skip internally declared static variables. 2041 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 2042 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 2043 if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) && 2044 (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link)) 2045 return; 2046 2047 SourceLocation ELoc = E->getExprLoc(); 2048 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 2049 // The default(none) clause requires that each variable that is referenced 2050 // in the construct, and does not have a predetermined data-sharing 2051 // attribute, must have its data-sharing attribute explicitly determined 2052 // by being listed in a data-sharing attribute clause. 2053 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && 2054 isParallelOrTaskRegion(DKind) && 2055 VarsWithInheritedDSA.count(VD) == 0) { 2056 VarsWithInheritedDSA[VD] = E; 2057 return; 2058 } 2059 2060 if (isOpenMPTargetExecutionDirective(DKind) && 2061 !Stack->isLoopControlVariable(VD).first) { 2062 if (!Stack->checkMappableExprComponentListsForDecl( 2063 VD, /*CurrentRegionOnly=*/true, 2064 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 2065 StackComponents, 2066 OpenMPClauseKind) { 2067 // Variable is used if it has been marked as an array, array 2068 // section or the variable iself. 2069 return StackComponents.size() == 1 || 2070 std::all_of( 2071 std::next(StackComponents.rbegin()), 2072 StackComponents.rend(), 2073 [](const OMPClauseMappableExprCommon:: 2074 MappableComponent &MC) { 2075 return MC.getAssociatedDeclaration() == 2076 nullptr && 2077 (isa<OMPArraySectionExpr>( 2078 MC.getAssociatedExpression()) || 2079 isa<ArraySubscriptExpr>( 2080 MC.getAssociatedExpression())); 2081 }); 2082 })) { 2083 bool IsFirstprivate = false; 2084 // By default lambdas are captured as firstprivates. 2085 if (const auto *RD = 2086 VD->getType().getNonReferenceType()->getAsCXXRecordDecl()) 2087 IsFirstprivate = RD->isLambda(); 2088 IsFirstprivate = 2089 IsFirstprivate || 2090 (VD->getType().getNonReferenceType()->isScalarType() && 2091 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res); 2092 if (IsFirstprivate) 2093 ImplicitFirstprivate.emplace_back(E); 2094 else 2095 ImplicitMap.emplace_back(E); 2096 return; 2097 } 2098 } 2099 2100 // OpenMP [2.9.3.6, Restrictions, p.2] 2101 // A list item that appears in a reduction clause of the innermost 2102 // enclosing worksharing or parallel construct may not be accessed in an 2103 // explicit task. 2104 DVar = Stack->hasInnermostDSA( 2105 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 2106 [](OpenMPDirectiveKind K) { 2107 return isOpenMPParallelDirective(K) || 2108 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 2109 }, 2110 /*FromParent=*/true); 2111 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 2112 ErrorFound = true; 2113 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 2114 reportOriginalDsa(SemaRef, Stack, VD, DVar); 2115 return; 2116 } 2117 2118 // Define implicit data-sharing attributes for task. 2119 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false); 2120 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 2121 !Stack->isLoopControlVariable(VD).first) 2122 ImplicitFirstprivate.push_back(E); 2123 } 2124 } 2125 void VisitMemberExpr(MemberExpr *E) { 2126 if (E->isTypeDependent() || E->isValueDependent() || 2127 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 2128 return; 2129 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 2130 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 2131 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 2132 if (!FD) 2133 return; 2134 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false); 2135 // Check if the variable has explicit DSA set and stop analysis if it 2136 // so. 2137 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second) 2138 return; 2139 2140 if (isOpenMPTargetExecutionDirective(DKind) && 2141 !Stack->isLoopControlVariable(FD).first && 2142 !Stack->checkMappableExprComponentListsForDecl( 2143 FD, /*CurrentRegionOnly=*/true, 2144 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 2145 StackComponents, 2146 OpenMPClauseKind) { 2147 return isa<CXXThisExpr>( 2148 cast<MemberExpr>( 2149 StackComponents.back().getAssociatedExpression()) 2150 ->getBase() 2151 ->IgnoreParens()); 2152 })) { 2153 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 2154 // A bit-field cannot appear in a map clause. 2155 // 2156 if (FD->isBitField()) 2157 return; 2158 ImplicitMap.emplace_back(E); 2159 return; 2160 } 2161 2162 SourceLocation ELoc = E->getExprLoc(); 2163 // OpenMP [2.9.3.6, Restrictions, p.2] 2164 // A list item that appears in a reduction clause of the innermost 2165 // enclosing worksharing or parallel construct may not be accessed in 2166 // an explicit task. 2167 DVar = Stack->hasInnermostDSA( 2168 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 2169 [](OpenMPDirectiveKind K) { 2170 return isOpenMPParallelDirective(K) || 2171 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 2172 }, 2173 /*FromParent=*/true); 2174 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 2175 ErrorFound = true; 2176 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 2177 reportOriginalDsa(SemaRef, Stack, FD, DVar); 2178 return; 2179 } 2180 2181 // Define implicit data-sharing attributes for task. 2182 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false); 2183 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 2184 !Stack->isLoopControlVariable(FD).first) 2185 ImplicitFirstprivate.push_back(E); 2186 return; 2187 } 2188 if (isOpenMPTargetExecutionDirective(DKind)) { 2189 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 2190 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map, 2191 /*NoDiagnose=*/true)) 2192 return; 2193 const auto *VD = cast<ValueDecl>( 2194 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl()); 2195 if (!Stack->checkMappableExprComponentListsForDecl( 2196 VD, /*CurrentRegionOnly=*/true, 2197 [&CurComponents]( 2198 OMPClauseMappableExprCommon::MappableExprComponentListRef 2199 StackComponents, 2200 OpenMPClauseKind) { 2201 auto CCI = CurComponents.rbegin(); 2202 auto CCE = CurComponents.rend(); 2203 for (const auto &SC : llvm::reverse(StackComponents)) { 2204 // Do both expressions have the same kind? 2205 if (CCI->getAssociatedExpression()->getStmtClass() != 2206 SC.getAssociatedExpression()->getStmtClass()) 2207 if (!(isa<OMPArraySectionExpr>( 2208 SC.getAssociatedExpression()) && 2209 isa<ArraySubscriptExpr>( 2210 CCI->getAssociatedExpression()))) 2211 return false; 2212 2213 const Decl *CCD = CCI->getAssociatedDeclaration(); 2214 const Decl *SCD = SC.getAssociatedDeclaration(); 2215 CCD = CCD ? CCD->getCanonicalDecl() : nullptr; 2216 SCD = SCD ? SCD->getCanonicalDecl() : nullptr; 2217 if (SCD != CCD) 2218 return false; 2219 std::advance(CCI, 1); 2220 if (CCI == CCE) 2221 break; 2222 } 2223 return true; 2224 })) { 2225 Visit(E->getBase()); 2226 } 2227 } else { 2228 Visit(E->getBase()); 2229 } 2230 } 2231 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 2232 for (OMPClause *C : S->clauses()) { 2233 // Skip analysis of arguments of implicitly defined firstprivate clause 2234 // for task|target directives. 2235 // Skip analysis of arguments of implicitly defined map clause for target 2236 // directives. 2237 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) && 2238 C->isImplicit())) { 2239 for (Stmt *CC : C->children()) { 2240 if (CC) 2241 Visit(CC); 2242 } 2243 } 2244 } 2245 } 2246 void VisitStmt(Stmt *S) { 2247 for (Stmt *C : S->children()) { 2248 if (C && !isa<OMPExecutableDirective>(C)) 2249 Visit(C); 2250 } 2251 } 2252 2253 bool isErrorFound() const { return ErrorFound; } 2254 ArrayRef<Expr *> getImplicitFirstprivate() const { 2255 return ImplicitFirstprivate; 2256 } 2257 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; } 2258 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const { 2259 return VarsWithInheritedDSA; 2260 } 2261 2262 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 2263 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {} 2264 }; 2265 } // namespace 2266 2267 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 2268 switch (DKind) { 2269 case OMPD_parallel: 2270 case OMPD_parallel_for: 2271 case OMPD_parallel_for_simd: 2272 case OMPD_parallel_sections: 2273 case OMPD_teams: 2274 case OMPD_teams_distribute: 2275 case OMPD_teams_distribute_simd: { 2276 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2277 QualType KmpInt32PtrTy = 2278 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2279 Sema::CapturedParamNameType Params[] = { 2280 std::make_pair(".global_tid.", KmpInt32PtrTy), 2281 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2282 std::make_pair(StringRef(), QualType()) // __context with shared vars 2283 }; 2284 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2285 Params); 2286 break; 2287 } 2288 case OMPD_target_teams: 2289 case OMPD_target_parallel: 2290 case OMPD_target_parallel_for: 2291 case OMPD_target_parallel_for_simd: 2292 case OMPD_target_teams_distribute: 2293 case OMPD_target_teams_distribute_simd: { 2294 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2295 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2296 QualType KmpInt32PtrTy = 2297 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2298 QualType Args[] = {VoidPtrTy}; 2299 FunctionProtoType::ExtProtoInfo EPI; 2300 EPI.Variadic = true; 2301 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2302 Sema::CapturedParamNameType Params[] = { 2303 std::make_pair(".global_tid.", KmpInt32Ty), 2304 std::make_pair(".part_id.", KmpInt32PtrTy), 2305 std::make_pair(".privates.", VoidPtrTy), 2306 std::make_pair( 2307 ".copy_fn.", 2308 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2309 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2310 std::make_pair(StringRef(), QualType()) // __context with shared vars 2311 }; 2312 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2313 Params); 2314 // Mark this captured region as inlined, because we don't use outlined 2315 // function directly. 2316 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2317 AlwaysInlineAttr::CreateImplicit( 2318 Context, AlwaysInlineAttr::Keyword_forceinline)); 2319 Sema::CapturedParamNameType ParamsTarget[] = { 2320 std::make_pair(StringRef(), QualType()) // __context with shared vars 2321 }; 2322 // Start a captured region for 'target' with no implicit parameters. 2323 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2324 ParamsTarget); 2325 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 2326 std::make_pair(".global_tid.", KmpInt32PtrTy), 2327 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2328 std::make_pair(StringRef(), QualType()) // __context with shared vars 2329 }; 2330 // Start a captured region for 'teams' or 'parallel'. Both regions have 2331 // the same implicit parameters. 2332 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2333 ParamsTeamsOrParallel); 2334 break; 2335 } 2336 case OMPD_target: 2337 case OMPD_target_simd: { 2338 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2339 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2340 QualType KmpInt32PtrTy = 2341 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2342 QualType Args[] = {VoidPtrTy}; 2343 FunctionProtoType::ExtProtoInfo EPI; 2344 EPI.Variadic = true; 2345 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2346 Sema::CapturedParamNameType Params[] = { 2347 std::make_pair(".global_tid.", KmpInt32Ty), 2348 std::make_pair(".part_id.", KmpInt32PtrTy), 2349 std::make_pair(".privates.", VoidPtrTy), 2350 std::make_pair( 2351 ".copy_fn.", 2352 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2353 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2354 std::make_pair(StringRef(), QualType()) // __context with shared vars 2355 }; 2356 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2357 Params); 2358 // Mark this captured region as inlined, because we don't use outlined 2359 // function directly. 2360 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2361 AlwaysInlineAttr::CreateImplicit( 2362 Context, AlwaysInlineAttr::Keyword_forceinline)); 2363 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2364 std::make_pair(StringRef(), QualType())); 2365 break; 2366 } 2367 case OMPD_simd: 2368 case OMPD_for: 2369 case OMPD_for_simd: 2370 case OMPD_sections: 2371 case OMPD_section: 2372 case OMPD_single: 2373 case OMPD_master: 2374 case OMPD_critical: 2375 case OMPD_taskgroup: 2376 case OMPD_distribute: 2377 case OMPD_distribute_simd: 2378 case OMPD_ordered: 2379 case OMPD_atomic: 2380 case OMPD_target_data: { 2381 Sema::CapturedParamNameType Params[] = { 2382 std::make_pair(StringRef(), QualType()) // __context with shared vars 2383 }; 2384 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2385 Params); 2386 break; 2387 } 2388 case OMPD_task: { 2389 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2390 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2391 QualType KmpInt32PtrTy = 2392 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2393 QualType Args[] = {VoidPtrTy}; 2394 FunctionProtoType::ExtProtoInfo EPI; 2395 EPI.Variadic = true; 2396 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2397 Sema::CapturedParamNameType Params[] = { 2398 std::make_pair(".global_tid.", KmpInt32Ty), 2399 std::make_pair(".part_id.", KmpInt32PtrTy), 2400 std::make_pair(".privates.", VoidPtrTy), 2401 std::make_pair( 2402 ".copy_fn.", 2403 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2404 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2405 std::make_pair(StringRef(), QualType()) // __context with shared vars 2406 }; 2407 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2408 Params); 2409 // Mark this captured region as inlined, because we don't use outlined 2410 // function directly. 2411 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2412 AlwaysInlineAttr::CreateImplicit( 2413 Context, AlwaysInlineAttr::Keyword_forceinline)); 2414 break; 2415 } 2416 case OMPD_taskloop: 2417 case OMPD_taskloop_simd: { 2418 QualType KmpInt32Ty = 2419 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 2420 .withConst(); 2421 QualType KmpUInt64Ty = 2422 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 2423 .withConst(); 2424 QualType KmpInt64Ty = 2425 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 2426 .withConst(); 2427 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2428 QualType KmpInt32PtrTy = 2429 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2430 QualType Args[] = {VoidPtrTy}; 2431 FunctionProtoType::ExtProtoInfo EPI; 2432 EPI.Variadic = true; 2433 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2434 Sema::CapturedParamNameType Params[] = { 2435 std::make_pair(".global_tid.", KmpInt32Ty), 2436 std::make_pair(".part_id.", KmpInt32PtrTy), 2437 std::make_pair(".privates.", VoidPtrTy), 2438 std::make_pair( 2439 ".copy_fn.", 2440 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2441 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2442 std::make_pair(".lb.", KmpUInt64Ty), 2443 std::make_pair(".ub.", KmpUInt64Ty), 2444 std::make_pair(".st.", KmpInt64Ty), 2445 std::make_pair(".liter.", KmpInt32Ty), 2446 std::make_pair(".reductions.", VoidPtrTy), 2447 std::make_pair(StringRef(), QualType()) // __context with shared vars 2448 }; 2449 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2450 Params); 2451 // Mark this captured region as inlined, because we don't use outlined 2452 // function directly. 2453 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2454 AlwaysInlineAttr::CreateImplicit( 2455 Context, AlwaysInlineAttr::Keyword_forceinline)); 2456 break; 2457 } 2458 case OMPD_distribute_parallel_for_simd: 2459 case OMPD_distribute_parallel_for: { 2460 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2461 QualType KmpInt32PtrTy = 2462 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2463 Sema::CapturedParamNameType Params[] = { 2464 std::make_pair(".global_tid.", KmpInt32PtrTy), 2465 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2466 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 2467 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 2468 std::make_pair(StringRef(), QualType()) // __context with shared vars 2469 }; 2470 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2471 Params); 2472 break; 2473 } 2474 case OMPD_target_teams_distribute_parallel_for: 2475 case OMPD_target_teams_distribute_parallel_for_simd: { 2476 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2477 QualType KmpInt32PtrTy = 2478 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2479 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2480 2481 QualType Args[] = {VoidPtrTy}; 2482 FunctionProtoType::ExtProtoInfo EPI; 2483 EPI.Variadic = true; 2484 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2485 Sema::CapturedParamNameType Params[] = { 2486 std::make_pair(".global_tid.", KmpInt32Ty), 2487 std::make_pair(".part_id.", KmpInt32PtrTy), 2488 std::make_pair(".privates.", VoidPtrTy), 2489 std::make_pair( 2490 ".copy_fn.", 2491 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2492 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2493 std::make_pair(StringRef(), QualType()) // __context with shared vars 2494 }; 2495 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2496 Params); 2497 // Mark this captured region as inlined, because we don't use outlined 2498 // function directly. 2499 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2500 AlwaysInlineAttr::CreateImplicit( 2501 Context, AlwaysInlineAttr::Keyword_forceinline)); 2502 Sema::CapturedParamNameType ParamsTarget[] = { 2503 std::make_pair(StringRef(), QualType()) // __context with shared vars 2504 }; 2505 // Start a captured region for 'target' with no implicit parameters. 2506 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2507 ParamsTarget); 2508 2509 Sema::CapturedParamNameType ParamsTeams[] = { 2510 std::make_pair(".global_tid.", KmpInt32PtrTy), 2511 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2512 std::make_pair(StringRef(), QualType()) // __context with shared vars 2513 }; 2514 // Start a captured region for 'target' with no implicit parameters. 2515 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2516 ParamsTeams); 2517 2518 Sema::CapturedParamNameType ParamsParallel[] = { 2519 std::make_pair(".global_tid.", KmpInt32PtrTy), 2520 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2521 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 2522 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 2523 std::make_pair(StringRef(), QualType()) // __context with shared vars 2524 }; 2525 // Start a captured region for 'teams' or 'parallel'. Both regions have 2526 // the same implicit parameters. 2527 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2528 ParamsParallel); 2529 break; 2530 } 2531 2532 case OMPD_teams_distribute_parallel_for: 2533 case OMPD_teams_distribute_parallel_for_simd: { 2534 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2535 QualType KmpInt32PtrTy = 2536 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2537 2538 Sema::CapturedParamNameType ParamsTeams[] = { 2539 std::make_pair(".global_tid.", KmpInt32PtrTy), 2540 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2541 std::make_pair(StringRef(), QualType()) // __context with shared vars 2542 }; 2543 // Start a captured region for 'target' with no implicit parameters. 2544 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2545 ParamsTeams); 2546 2547 Sema::CapturedParamNameType ParamsParallel[] = { 2548 std::make_pair(".global_tid.", KmpInt32PtrTy), 2549 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2550 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 2551 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 2552 std::make_pair(StringRef(), QualType()) // __context with shared vars 2553 }; 2554 // Start a captured region for 'teams' or 'parallel'. Both regions have 2555 // the same implicit parameters. 2556 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2557 ParamsParallel); 2558 break; 2559 } 2560 case OMPD_target_update: 2561 case OMPD_target_enter_data: 2562 case OMPD_target_exit_data: { 2563 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2564 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2565 QualType KmpInt32PtrTy = 2566 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2567 QualType Args[] = {VoidPtrTy}; 2568 FunctionProtoType::ExtProtoInfo EPI; 2569 EPI.Variadic = true; 2570 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2571 Sema::CapturedParamNameType Params[] = { 2572 std::make_pair(".global_tid.", KmpInt32Ty), 2573 std::make_pair(".part_id.", KmpInt32PtrTy), 2574 std::make_pair(".privates.", VoidPtrTy), 2575 std::make_pair( 2576 ".copy_fn.", 2577 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2578 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2579 std::make_pair(StringRef(), QualType()) // __context with shared vars 2580 }; 2581 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2582 Params); 2583 // Mark this captured region as inlined, because we don't use outlined 2584 // function directly. 2585 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2586 AlwaysInlineAttr::CreateImplicit( 2587 Context, AlwaysInlineAttr::Keyword_forceinline)); 2588 break; 2589 } 2590 case OMPD_threadprivate: 2591 case OMPD_taskyield: 2592 case OMPD_barrier: 2593 case OMPD_taskwait: 2594 case OMPD_cancellation_point: 2595 case OMPD_cancel: 2596 case OMPD_flush: 2597 case OMPD_declare_reduction: 2598 case OMPD_declare_simd: 2599 case OMPD_declare_target: 2600 case OMPD_end_declare_target: 2601 case OMPD_requires: 2602 llvm_unreachable("OpenMP Directive is not allowed"); 2603 case OMPD_unknown: 2604 llvm_unreachable("Unknown OpenMP directive"); 2605 } 2606 } 2607 2608 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 2609 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2610 getOpenMPCaptureRegions(CaptureRegions, DKind); 2611 return CaptureRegions.size(); 2612 } 2613 2614 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 2615 Expr *CaptureExpr, bool WithInit, 2616 bool AsExpression) { 2617 assert(CaptureExpr); 2618 ASTContext &C = S.getASTContext(); 2619 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 2620 QualType Ty = Init->getType(); 2621 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 2622 if (S.getLangOpts().CPlusPlus) { 2623 Ty = C.getLValueReferenceType(Ty); 2624 } else { 2625 Ty = C.getPointerType(Ty); 2626 ExprResult Res = 2627 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 2628 if (!Res.isUsable()) 2629 return nullptr; 2630 Init = Res.get(); 2631 } 2632 WithInit = true; 2633 } 2634 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 2635 CaptureExpr->getBeginLoc()); 2636 if (!WithInit) 2637 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C)); 2638 S.CurContext->addHiddenDecl(CED); 2639 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 2640 return CED; 2641 } 2642 2643 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 2644 bool WithInit) { 2645 OMPCapturedExprDecl *CD; 2646 if (VarDecl *VD = S.isOpenMPCapturedDecl(D)) 2647 CD = cast<OMPCapturedExprDecl>(VD); 2648 else 2649 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 2650 /*AsExpression=*/false); 2651 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 2652 CaptureExpr->getExprLoc()); 2653 } 2654 2655 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 2656 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get(); 2657 if (!Ref) { 2658 OMPCapturedExprDecl *CD = buildCaptureDecl( 2659 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr, 2660 /*WithInit=*/true, /*AsExpression=*/true); 2661 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 2662 CaptureExpr->getExprLoc()); 2663 } 2664 ExprResult Res = Ref; 2665 if (!S.getLangOpts().CPlusPlus && 2666 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 2667 Ref->getType()->isPointerType()) { 2668 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 2669 if (!Res.isUsable()) 2670 return ExprError(); 2671 } 2672 return S.DefaultLvalueConversion(Res.get()); 2673 } 2674 2675 namespace { 2676 // OpenMP directives parsed in this section are represented as a 2677 // CapturedStatement with an associated statement. If a syntax error 2678 // is detected during the parsing of the associated statement, the 2679 // compiler must abort processing and close the CapturedStatement. 2680 // 2681 // Combined directives such as 'target parallel' have more than one 2682 // nested CapturedStatements. This RAII ensures that we unwind out 2683 // of all the nested CapturedStatements when an error is found. 2684 class CaptureRegionUnwinderRAII { 2685 private: 2686 Sema &S; 2687 bool &ErrorFound; 2688 OpenMPDirectiveKind DKind = OMPD_unknown; 2689 2690 public: 2691 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 2692 OpenMPDirectiveKind DKind) 2693 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 2694 ~CaptureRegionUnwinderRAII() { 2695 if (ErrorFound) { 2696 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 2697 while (--ThisCaptureLevel >= 0) 2698 S.ActOnCapturedRegionError(); 2699 } 2700 } 2701 }; 2702 } // namespace 2703 2704 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 2705 ArrayRef<OMPClause *> Clauses) { 2706 bool ErrorFound = false; 2707 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 2708 *this, ErrorFound, DSAStack->getCurrentDirective()); 2709 if (!S.isUsable()) { 2710 ErrorFound = true; 2711 return StmtError(); 2712 } 2713 2714 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2715 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 2716 OMPOrderedClause *OC = nullptr; 2717 OMPScheduleClause *SC = nullptr; 2718 SmallVector<const OMPLinearClause *, 4> LCs; 2719 SmallVector<const OMPClauseWithPreInit *, 4> PICs; 2720 // This is required for proper codegen. 2721 for (OMPClause *Clause : Clauses) { 2722 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) && 2723 Clause->getClauseKind() == OMPC_in_reduction) { 2724 // Capture taskgroup task_reduction descriptors inside the tasking regions 2725 // with the corresponding in_reduction items. 2726 auto *IRC = cast<OMPInReductionClause>(Clause); 2727 for (Expr *E : IRC->taskgroup_descriptors()) 2728 if (E) 2729 MarkDeclarationsReferencedInExpr(E); 2730 } 2731 if (isOpenMPPrivate(Clause->getClauseKind()) || 2732 Clause->getClauseKind() == OMPC_copyprivate || 2733 (getLangOpts().OpenMPUseTLS && 2734 getASTContext().getTargetInfo().isTLSSupported() && 2735 Clause->getClauseKind() == OMPC_copyin)) { 2736 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 2737 // Mark all variables in private list clauses as used in inner region. 2738 for (Stmt *VarRef : Clause->children()) { 2739 if (auto *E = cast_or_null<Expr>(VarRef)) { 2740 MarkDeclarationsReferencedInExpr(E); 2741 } 2742 } 2743 DSAStack->setForceVarCapturing(/*V=*/false); 2744 } else if (CaptureRegions.size() > 1 || 2745 CaptureRegions.back() != OMPD_unknown) { 2746 if (auto *C = OMPClauseWithPreInit::get(Clause)) 2747 PICs.push_back(C); 2748 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 2749 if (Expr *E = C->getPostUpdateExpr()) 2750 MarkDeclarationsReferencedInExpr(E); 2751 } 2752 } 2753 if (Clause->getClauseKind() == OMPC_schedule) 2754 SC = cast<OMPScheduleClause>(Clause); 2755 else if (Clause->getClauseKind() == OMPC_ordered) 2756 OC = cast<OMPOrderedClause>(Clause); 2757 else if (Clause->getClauseKind() == OMPC_linear) 2758 LCs.push_back(cast<OMPLinearClause>(Clause)); 2759 } 2760 // OpenMP, 2.7.1 Loop Construct, Restrictions 2761 // The nonmonotonic modifier cannot be specified if an ordered clause is 2762 // specified. 2763 if (SC && 2764 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 2765 SC->getSecondScheduleModifier() == 2766 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 2767 OC) { 2768 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 2769 ? SC->getFirstScheduleModifierLoc() 2770 : SC->getSecondScheduleModifierLoc(), 2771 diag::err_omp_schedule_nonmonotonic_ordered) 2772 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 2773 ErrorFound = true; 2774 } 2775 if (!LCs.empty() && OC && OC->getNumForLoops()) { 2776 for (const OMPLinearClause *C : LCs) { 2777 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered) 2778 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 2779 } 2780 ErrorFound = true; 2781 } 2782 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 2783 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 2784 OC->getNumForLoops()) { 2785 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd) 2786 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 2787 ErrorFound = true; 2788 } 2789 if (ErrorFound) { 2790 return StmtError(); 2791 } 2792 StmtResult SR = S; 2793 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 2794 // Mark all variables in private list clauses as used in inner region. 2795 // Required for proper codegen of combined directives. 2796 // TODO: add processing for other clauses. 2797 if (ThisCaptureRegion != OMPD_unknown) { 2798 for (const clang::OMPClauseWithPreInit *C : PICs) { 2799 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 2800 // Find the particular capture region for the clause if the 2801 // directive is a combined one with multiple capture regions. 2802 // If the directive is not a combined one, the capture region 2803 // associated with the clause is OMPD_unknown and is generated 2804 // only once. 2805 if (CaptureRegion == ThisCaptureRegion || 2806 CaptureRegion == OMPD_unknown) { 2807 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 2808 for (Decl *D : DS->decls()) 2809 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 2810 } 2811 } 2812 } 2813 } 2814 SR = ActOnCapturedRegionEnd(SR.get()); 2815 } 2816 return SR; 2817 } 2818 2819 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 2820 OpenMPDirectiveKind CancelRegion, 2821 SourceLocation StartLoc) { 2822 // CancelRegion is only needed for cancel and cancellation_point. 2823 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 2824 return false; 2825 2826 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 2827 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 2828 return false; 2829 2830 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 2831 << getOpenMPDirectiveName(CancelRegion); 2832 return true; 2833 } 2834 2835 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack, 2836 OpenMPDirectiveKind CurrentRegion, 2837 const DeclarationNameInfo &CurrentName, 2838 OpenMPDirectiveKind CancelRegion, 2839 SourceLocation StartLoc) { 2840 if (Stack->getCurScope()) { 2841 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective(); 2842 OpenMPDirectiveKind OffendingRegion = ParentRegion; 2843 bool NestingProhibited = false; 2844 bool CloseNesting = true; 2845 bool OrphanSeen = false; 2846 enum { 2847 NoRecommend, 2848 ShouldBeInParallelRegion, 2849 ShouldBeInOrderedRegion, 2850 ShouldBeInTargetRegion, 2851 ShouldBeInTeamsRegion 2852 } Recommend = NoRecommend; 2853 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) { 2854 // OpenMP [2.16, Nesting of Regions] 2855 // OpenMP constructs may not be nested inside a simd region. 2856 // OpenMP [2.8.1,simd Construct, Restrictions] 2857 // An ordered construct with the simd clause is the only OpenMP 2858 // construct that can appear in the simd region. 2859 // Allowing a SIMD construct nested in another SIMD construct is an 2860 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 2861 // message. 2862 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 2863 ? diag::err_omp_prohibited_region_simd 2864 : diag::warn_omp_nesting_simd); 2865 return CurrentRegion != OMPD_simd; 2866 } 2867 if (ParentRegion == OMPD_atomic) { 2868 // OpenMP [2.16, Nesting of Regions] 2869 // OpenMP constructs may not be nested inside an atomic region. 2870 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 2871 return true; 2872 } 2873 if (CurrentRegion == OMPD_section) { 2874 // OpenMP [2.7.2, sections Construct, Restrictions] 2875 // Orphaned section directives are prohibited. That is, the section 2876 // directives must appear within the sections construct and must not be 2877 // encountered elsewhere in the sections region. 2878 if (ParentRegion != OMPD_sections && 2879 ParentRegion != OMPD_parallel_sections) { 2880 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 2881 << (ParentRegion != OMPD_unknown) 2882 << getOpenMPDirectiveName(ParentRegion); 2883 return true; 2884 } 2885 return false; 2886 } 2887 // Allow some constructs (except teams) to be orphaned (they could be 2888 // used in functions, called from OpenMP regions with the required 2889 // preconditions). 2890 if (ParentRegion == OMPD_unknown && 2891 !isOpenMPNestingTeamsDirective(CurrentRegion)) 2892 return false; 2893 if (CurrentRegion == OMPD_cancellation_point || 2894 CurrentRegion == OMPD_cancel) { 2895 // OpenMP [2.16, Nesting of Regions] 2896 // A cancellation point construct for which construct-type-clause is 2897 // taskgroup must be nested inside a task construct. A cancellation 2898 // point construct for which construct-type-clause is not taskgroup must 2899 // be closely nested inside an OpenMP construct that matches the type 2900 // specified in construct-type-clause. 2901 // A cancel construct for which construct-type-clause is taskgroup must be 2902 // nested inside a task construct. A cancel construct for which 2903 // construct-type-clause is not taskgroup must be closely nested inside an 2904 // OpenMP construct that matches the type specified in 2905 // construct-type-clause. 2906 NestingProhibited = 2907 !((CancelRegion == OMPD_parallel && 2908 (ParentRegion == OMPD_parallel || 2909 ParentRegion == OMPD_target_parallel)) || 2910 (CancelRegion == OMPD_for && 2911 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 2912 ParentRegion == OMPD_target_parallel_for || 2913 ParentRegion == OMPD_distribute_parallel_for || 2914 ParentRegion == OMPD_teams_distribute_parallel_for || 2915 ParentRegion == OMPD_target_teams_distribute_parallel_for)) || 2916 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) || 2917 (CancelRegion == OMPD_sections && 2918 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 2919 ParentRegion == OMPD_parallel_sections))); 2920 } else if (CurrentRegion == OMPD_master) { 2921 // OpenMP [2.16, Nesting of Regions] 2922 // A master region may not be closely nested inside a worksharing, 2923 // atomic, or explicit task region. 2924 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 2925 isOpenMPTaskingDirective(ParentRegion); 2926 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 2927 // OpenMP [2.16, Nesting of Regions] 2928 // A critical region may not be nested (closely or otherwise) inside a 2929 // critical region with the same name. Note that this restriction is not 2930 // sufficient to prevent deadlock. 2931 SourceLocation PreviousCriticalLoc; 2932 bool DeadLock = Stack->hasDirective( 2933 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 2934 const DeclarationNameInfo &DNI, 2935 SourceLocation Loc) { 2936 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 2937 PreviousCriticalLoc = Loc; 2938 return true; 2939 } 2940 return false; 2941 }, 2942 false /* skip top directive */); 2943 if (DeadLock) { 2944 SemaRef.Diag(StartLoc, 2945 diag::err_omp_prohibited_region_critical_same_name) 2946 << CurrentName.getName(); 2947 if (PreviousCriticalLoc.isValid()) 2948 SemaRef.Diag(PreviousCriticalLoc, 2949 diag::note_omp_previous_critical_region); 2950 return true; 2951 } 2952 } else if (CurrentRegion == OMPD_barrier) { 2953 // OpenMP [2.16, Nesting of Regions] 2954 // A barrier region may not be closely nested inside a worksharing, 2955 // explicit task, critical, ordered, atomic, or master region. 2956 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 2957 isOpenMPTaskingDirective(ParentRegion) || 2958 ParentRegion == OMPD_master || 2959 ParentRegion == OMPD_critical || 2960 ParentRegion == OMPD_ordered; 2961 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 2962 !isOpenMPParallelDirective(CurrentRegion) && 2963 !isOpenMPTeamsDirective(CurrentRegion)) { 2964 // OpenMP [2.16, Nesting of Regions] 2965 // A worksharing region may not be closely nested inside a worksharing, 2966 // explicit task, critical, ordered, atomic, or master region. 2967 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 2968 isOpenMPTaskingDirective(ParentRegion) || 2969 ParentRegion == OMPD_master || 2970 ParentRegion == OMPD_critical || 2971 ParentRegion == OMPD_ordered; 2972 Recommend = ShouldBeInParallelRegion; 2973 } else if (CurrentRegion == OMPD_ordered) { 2974 // OpenMP [2.16, Nesting of Regions] 2975 // An ordered region may not be closely nested inside a critical, 2976 // atomic, or explicit task region. 2977 // An ordered region must be closely nested inside a loop region (or 2978 // parallel loop region) with an ordered clause. 2979 // OpenMP [2.8.1,simd Construct, Restrictions] 2980 // An ordered construct with the simd clause is the only OpenMP construct 2981 // that can appear in the simd region. 2982 NestingProhibited = ParentRegion == OMPD_critical || 2983 isOpenMPTaskingDirective(ParentRegion) || 2984 !(isOpenMPSimdDirective(ParentRegion) || 2985 Stack->isParentOrderedRegion()); 2986 Recommend = ShouldBeInOrderedRegion; 2987 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 2988 // OpenMP [2.16, Nesting of Regions] 2989 // If specified, a teams construct must be contained within a target 2990 // construct. 2991 NestingProhibited = ParentRegion != OMPD_target; 2992 OrphanSeen = ParentRegion == OMPD_unknown; 2993 Recommend = ShouldBeInTargetRegion; 2994 } 2995 if (!NestingProhibited && 2996 !isOpenMPTargetExecutionDirective(CurrentRegion) && 2997 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 2998 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 2999 // OpenMP [2.16, Nesting of Regions] 3000 // distribute, parallel, parallel sections, parallel workshare, and the 3001 // parallel loop and parallel loop SIMD constructs are the only OpenMP 3002 // constructs that can be closely nested in the teams region. 3003 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 3004 !isOpenMPDistributeDirective(CurrentRegion); 3005 Recommend = ShouldBeInParallelRegion; 3006 } 3007 if (!NestingProhibited && 3008 isOpenMPNestingDistributeDirective(CurrentRegion)) { 3009 // OpenMP 4.5 [2.17 Nesting of Regions] 3010 // The region associated with the distribute construct must be strictly 3011 // nested inside a teams region 3012 NestingProhibited = 3013 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 3014 Recommend = ShouldBeInTeamsRegion; 3015 } 3016 if (!NestingProhibited && 3017 (isOpenMPTargetExecutionDirective(CurrentRegion) || 3018 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 3019 // OpenMP 4.5 [2.17 Nesting of Regions] 3020 // If a target, target update, target data, target enter data, or 3021 // target exit data construct is encountered during execution of a 3022 // target region, the behavior is unspecified. 3023 NestingProhibited = Stack->hasDirective( 3024 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 3025 SourceLocation) { 3026 if (isOpenMPTargetExecutionDirective(K)) { 3027 OffendingRegion = K; 3028 return true; 3029 } 3030 return false; 3031 }, 3032 false /* don't skip top directive */); 3033 CloseNesting = false; 3034 } 3035 if (NestingProhibited) { 3036 if (OrphanSeen) { 3037 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 3038 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 3039 } else { 3040 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 3041 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 3042 << Recommend << getOpenMPDirectiveName(CurrentRegion); 3043 } 3044 return true; 3045 } 3046 } 3047 return false; 3048 } 3049 3050 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 3051 ArrayRef<OMPClause *> Clauses, 3052 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 3053 bool ErrorFound = false; 3054 unsigned NamedModifiersNumber = 0; 3055 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers( 3056 OMPD_unknown + 1); 3057 SmallVector<SourceLocation, 4> NameModifierLoc; 3058 for (const OMPClause *C : Clauses) { 3059 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 3060 // At most one if clause without a directive-name-modifier can appear on 3061 // the directive. 3062 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 3063 if (FoundNameModifiers[CurNM]) { 3064 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 3065 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 3066 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 3067 ErrorFound = true; 3068 } else if (CurNM != OMPD_unknown) { 3069 NameModifierLoc.push_back(IC->getNameModifierLoc()); 3070 ++NamedModifiersNumber; 3071 } 3072 FoundNameModifiers[CurNM] = IC; 3073 if (CurNM == OMPD_unknown) 3074 continue; 3075 // Check if the specified name modifier is allowed for the current 3076 // directive. 3077 // At most one if clause with the particular directive-name-modifier can 3078 // appear on the directive. 3079 bool MatchFound = false; 3080 for (auto NM : AllowedNameModifiers) { 3081 if (CurNM == NM) { 3082 MatchFound = true; 3083 break; 3084 } 3085 } 3086 if (!MatchFound) { 3087 S.Diag(IC->getNameModifierLoc(), 3088 diag::err_omp_wrong_if_directive_name_modifier) 3089 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 3090 ErrorFound = true; 3091 } 3092 } 3093 } 3094 // If any if clause on the directive includes a directive-name-modifier then 3095 // all if clauses on the directive must include a directive-name-modifier. 3096 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 3097 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 3098 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(), 3099 diag::err_omp_no_more_if_clause); 3100 } else { 3101 std::string Values; 3102 std::string Sep(", "); 3103 unsigned AllowedCnt = 0; 3104 unsigned TotalAllowedNum = 3105 AllowedNameModifiers.size() - NamedModifiersNumber; 3106 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 3107 ++Cnt) { 3108 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 3109 if (!FoundNameModifiers[NM]) { 3110 Values += "'"; 3111 Values += getOpenMPDirectiveName(NM); 3112 Values += "'"; 3113 if (AllowedCnt + 2 == TotalAllowedNum) 3114 Values += " or "; 3115 else if (AllowedCnt + 1 != TotalAllowedNum) 3116 Values += Sep; 3117 ++AllowedCnt; 3118 } 3119 } 3120 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(), 3121 diag::err_omp_unnamed_if_clause) 3122 << (TotalAllowedNum > 1) << Values; 3123 } 3124 for (SourceLocation Loc : NameModifierLoc) { 3125 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 3126 } 3127 ErrorFound = true; 3128 } 3129 return ErrorFound; 3130 } 3131 3132 StmtResult Sema::ActOnOpenMPExecutableDirective( 3133 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 3134 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 3135 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 3136 StmtResult Res = StmtError(); 3137 // First check CancelRegion which is then used in checkNestingOfRegions. 3138 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 3139 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 3140 StartLoc)) 3141 return StmtError(); 3142 3143 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 3144 VarsWithInheritedDSAType VarsWithInheritedDSA; 3145 bool ErrorFound = false; 3146 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 3147 if (AStmt && !CurContext->isDependentContext()) { 3148 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3149 3150 // Check default data sharing attributes for referenced variables. 3151 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 3152 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 3153 Stmt *S = AStmt; 3154 while (--ThisCaptureLevel >= 0) 3155 S = cast<CapturedStmt>(S)->getCapturedStmt(); 3156 DSAChecker.Visit(S); 3157 if (DSAChecker.isErrorFound()) 3158 return StmtError(); 3159 // Generate list of implicitly defined firstprivate variables. 3160 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 3161 3162 SmallVector<Expr *, 4> ImplicitFirstprivates( 3163 DSAChecker.getImplicitFirstprivate().begin(), 3164 DSAChecker.getImplicitFirstprivate().end()); 3165 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(), 3166 DSAChecker.getImplicitMap().end()); 3167 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 3168 for (OMPClause *C : Clauses) { 3169 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 3170 for (Expr *E : IRC->taskgroup_descriptors()) 3171 if (E) 3172 ImplicitFirstprivates.emplace_back(E); 3173 } 3174 } 3175 if (!ImplicitFirstprivates.empty()) { 3176 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 3177 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 3178 SourceLocation())) { 3179 ClausesWithImplicit.push_back(Implicit); 3180 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 3181 ImplicitFirstprivates.size(); 3182 } else { 3183 ErrorFound = true; 3184 } 3185 } 3186 if (!ImplicitMaps.empty()) { 3187 if (OMPClause *Implicit = ActOnOpenMPMapClause( 3188 OMPC_MAP_unknown, OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true, 3189 SourceLocation(), SourceLocation(), ImplicitMaps, 3190 SourceLocation(), SourceLocation(), SourceLocation())) { 3191 ClausesWithImplicit.emplace_back(Implicit); 3192 ErrorFound |= 3193 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size(); 3194 } else { 3195 ErrorFound = true; 3196 } 3197 } 3198 } 3199 3200 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 3201 switch (Kind) { 3202 case OMPD_parallel: 3203 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 3204 EndLoc); 3205 AllowedNameModifiers.push_back(OMPD_parallel); 3206 break; 3207 case OMPD_simd: 3208 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 3209 VarsWithInheritedDSA); 3210 break; 3211 case OMPD_for: 3212 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 3213 VarsWithInheritedDSA); 3214 break; 3215 case OMPD_for_simd: 3216 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 3217 EndLoc, VarsWithInheritedDSA); 3218 break; 3219 case OMPD_sections: 3220 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 3221 EndLoc); 3222 break; 3223 case OMPD_section: 3224 assert(ClausesWithImplicit.empty() && 3225 "No clauses are allowed for 'omp section' directive"); 3226 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 3227 break; 3228 case OMPD_single: 3229 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 3230 EndLoc); 3231 break; 3232 case OMPD_master: 3233 assert(ClausesWithImplicit.empty() && 3234 "No clauses are allowed for 'omp master' directive"); 3235 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 3236 break; 3237 case OMPD_critical: 3238 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 3239 StartLoc, EndLoc); 3240 break; 3241 case OMPD_parallel_for: 3242 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 3243 EndLoc, VarsWithInheritedDSA); 3244 AllowedNameModifiers.push_back(OMPD_parallel); 3245 break; 3246 case OMPD_parallel_for_simd: 3247 Res = ActOnOpenMPParallelForSimdDirective( 3248 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3249 AllowedNameModifiers.push_back(OMPD_parallel); 3250 break; 3251 case OMPD_parallel_sections: 3252 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 3253 StartLoc, EndLoc); 3254 AllowedNameModifiers.push_back(OMPD_parallel); 3255 break; 3256 case OMPD_task: 3257 Res = 3258 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 3259 AllowedNameModifiers.push_back(OMPD_task); 3260 break; 3261 case OMPD_taskyield: 3262 assert(ClausesWithImplicit.empty() && 3263 "No clauses are allowed for 'omp taskyield' directive"); 3264 assert(AStmt == nullptr && 3265 "No associated statement allowed for 'omp taskyield' directive"); 3266 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 3267 break; 3268 case OMPD_barrier: 3269 assert(ClausesWithImplicit.empty() && 3270 "No clauses are allowed for 'omp barrier' directive"); 3271 assert(AStmt == nullptr && 3272 "No associated statement allowed for 'omp barrier' directive"); 3273 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 3274 break; 3275 case OMPD_taskwait: 3276 assert(ClausesWithImplicit.empty() && 3277 "No clauses are allowed for 'omp taskwait' directive"); 3278 assert(AStmt == nullptr && 3279 "No associated statement allowed for 'omp taskwait' directive"); 3280 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 3281 break; 3282 case OMPD_taskgroup: 3283 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 3284 EndLoc); 3285 break; 3286 case OMPD_flush: 3287 assert(AStmt == nullptr && 3288 "No associated statement allowed for 'omp flush' directive"); 3289 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 3290 break; 3291 case OMPD_ordered: 3292 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 3293 EndLoc); 3294 break; 3295 case OMPD_atomic: 3296 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 3297 EndLoc); 3298 break; 3299 case OMPD_teams: 3300 Res = 3301 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 3302 break; 3303 case OMPD_target: 3304 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 3305 EndLoc); 3306 AllowedNameModifiers.push_back(OMPD_target); 3307 break; 3308 case OMPD_target_parallel: 3309 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 3310 StartLoc, EndLoc); 3311 AllowedNameModifiers.push_back(OMPD_target); 3312 AllowedNameModifiers.push_back(OMPD_parallel); 3313 break; 3314 case OMPD_target_parallel_for: 3315 Res = ActOnOpenMPTargetParallelForDirective( 3316 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3317 AllowedNameModifiers.push_back(OMPD_target); 3318 AllowedNameModifiers.push_back(OMPD_parallel); 3319 break; 3320 case OMPD_cancellation_point: 3321 assert(ClausesWithImplicit.empty() && 3322 "No clauses are allowed for 'omp cancellation point' directive"); 3323 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 3324 "cancellation point' directive"); 3325 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 3326 break; 3327 case OMPD_cancel: 3328 assert(AStmt == nullptr && 3329 "No associated statement allowed for 'omp cancel' directive"); 3330 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 3331 CancelRegion); 3332 AllowedNameModifiers.push_back(OMPD_cancel); 3333 break; 3334 case OMPD_target_data: 3335 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 3336 EndLoc); 3337 AllowedNameModifiers.push_back(OMPD_target_data); 3338 break; 3339 case OMPD_target_enter_data: 3340 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 3341 EndLoc, AStmt); 3342 AllowedNameModifiers.push_back(OMPD_target_enter_data); 3343 break; 3344 case OMPD_target_exit_data: 3345 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 3346 EndLoc, AStmt); 3347 AllowedNameModifiers.push_back(OMPD_target_exit_data); 3348 break; 3349 case OMPD_taskloop: 3350 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 3351 EndLoc, VarsWithInheritedDSA); 3352 AllowedNameModifiers.push_back(OMPD_taskloop); 3353 break; 3354 case OMPD_taskloop_simd: 3355 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 3356 EndLoc, VarsWithInheritedDSA); 3357 AllowedNameModifiers.push_back(OMPD_taskloop); 3358 break; 3359 case OMPD_distribute: 3360 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 3361 EndLoc, VarsWithInheritedDSA); 3362 break; 3363 case OMPD_target_update: 3364 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 3365 EndLoc, AStmt); 3366 AllowedNameModifiers.push_back(OMPD_target_update); 3367 break; 3368 case OMPD_distribute_parallel_for: 3369 Res = ActOnOpenMPDistributeParallelForDirective( 3370 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3371 AllowedNameModifiers.push_back(OMPD_parallel); 3372 break; 3373 case OMPD_distribute_parallel_for_simd: 3374 Res = ActOnOpenMPDistributeParallelForSimdDirective( 3375 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3376 AllowedNameModifiers.push_back(OMPD_parallel); 3377 break; 3378 case OMPD_distribute_simd: 3379 Res = ActOnOpenMPDistributeSimdDirective( 3380 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3381 break; 3382 case OMPD_target_parallel_for_simd: 3383 Res = ActOnOpenMPTargetParallelForSimdDirective( 3384 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3385 AllowedNameModifiers.push_back(OMPD_target); 3386 AllowedNameModifiers.push_back(OMPD_parallel); 3387 break; 3388 case OMPD_target_simd: 3389 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 3390 EndLoc, VarsWithInheritedDSA); 3391 AllowedNameModifiers.push_back(OMPD_target); 3392 break; 3393 case OMPD_teams_distribute: 3394 Res = ActOnOpenMPTeamsDistributeDirective( 3395 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3396 break; 3397 case OMPD_teams_distribute_simd: 3398 Res = ActOnOpenMPTeamsDistributeSimdDirective( 3399 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3400 break; 3401 case OMPD_teams_distribute_parallel_for_simd: 3402 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 3403 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3404 AllowedNameModifiers.push_back(OMPD_parallel); 3405 break; 3406 case OMPD_teams_distribute_parallel_for: 3407 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 3408 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3409 AllowedNameModifiers.push_back(OMPD_parallel); 3410 break; 3411 case OMPD_target_teams: 3412 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 3413 EndLoc); 3414 AllowedNameModifiers.push_back(OMPD_target); 3415 break; 3416 case OMPD_target_teams_distribute: 3417 Res = ActOnOpenMPTargetTeamsDistributeDirective( 3418 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3419 AllowedNameModifiers.push_back(OMPD_target); 3420 break; 3421 case OMPD_target_teams_distribute_parallel_for: 3422 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 3423 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3424 AllowedNameModifiers.push_back(OMPD_target); 3425 AllowedNameModifiers.push_back(OMPD_parallel); 3426 break; 3427 case OMPD_target_teams_distribute_parallel_for_simd: 3428 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 3429 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3430 AllowedNameModifiers.push_back(OMPD_target); 3431 AllowedNameModifiers.push_back(OMPD_parallel); 3432 break; 3433 case OMPD_target_teams_distribute_simd: 3434 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 3435 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3436 AllowedNameModifiers.push_back(OMPD_target); 3437 break; 3438 case OMPD_declare_target: 3439 case OMPD_end_declare_target: 3440 case OMPD_threadprivate: 3441 case OMPD_declare_reduction: 3442 case OMPD_declare_simd: 3443 case OMPD_requires: 3444 llvm_unreachable("OpenMP Directive is not allowed"); 3445 case OMPD_unknown: 3446 llvm_unreachable("Unknown OpenMP directive"); 3447 } 3448 3449 for (const auto &P : VarsWithInheritedDSA) { 3450 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 3451 << P.first << P.second->getSourceRange(); 3452 } 3453 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound; 3454 3455 if (!AllowedNameModifiers.empty()) 3456 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 3457 ErrorFound; 3458 3459 if (ErrorFound) 3460 return StmtError(); 3461 return Res; 3462 } 3463 3464 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 3465 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 3466 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 3467 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 3468 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 3469 assert(Aligneds.size() == Alignments.size()); 3470 assert(Linears.size() == LinModifiers.size()); 3471 assert(Linears.size() == Steps.size()); 3472 if (!DG || DG.get().isNull()) 3473 return DeclGroupPtrTy(); 3474 3475 if (!DG.get().isSingleDecl()) { 3476 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd); 3477 return DG; 3478 } 3479 Decl *ADecl = DG.get().getSingleDecl(); 3480 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 3481 ADecl = FTD->getTemplatedDecl(); 3482 3483 auto *FD = dyn_cast<FunctionDecl>(ADecl); 3484 if (!FD) { 3485 Diag(ADecl->getLocation(), diag::err_omp_function_expected); 3486 return DeclGroupPtrTy(); 3487 } 3488 3489 // OpenMP [2.8.2, declare simd construct, Description] 3490 // The parameter of the simdlen clause must be a constant positive integer 3491 // expression. 3492 ExprResult SL; 3493 if (Simdlen) 3494 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 3495 // OpenMP [2.8.2, declare simd construct, Description] 3496 // The special this pointer can be used as if was one of the arguments to the 3497 // function in any of the linear, aligned, or uniform clauses. 3498 // The uniform clause declares one or more arguments to have an invariant 3499 // value for all concurrent invocations of the function in the execution of a 3500 // single SIMD loop. 3501 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 3502 const Expr *UniformedLinearThis = nullptr; 3503 for (const Expr *E : Uniforms) { 3504 E = E->IgnoreParenImpCasts(); 3505 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 3506 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 3507 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 3508 FD->getParamDecl(PVD->getFunctionScopeIndex()) 3509 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 3510 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 3511 continue; 3512 } 3513 if (isa<CXXThisExpr>(E)) { 3514 UniformedLinearThis = E; 3515 continue; 3516 } 3517 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 3518 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 3519 } 3520 // OpenMP [2.8.2, declare simd construct, Description] 3521 // The aligned clause declares that the object to which each list item points 3522 // is aligned to the number of bytes expressed in the optional parameter of 3523 // the aligned clause. 3524 // The special this pointer can be used as if was one of the arguments to the 3525 // function in any of the linear, aligned, or uniform clauses. 3526 // The type of list items appearing in the aligned clause must be array, 3527 // pointer, reference to array, or reference to pointer. 3528 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 3529 const Expr *AlignedThis = nullptr; 3530 for (const Expr *E : Aligneds) { 3531 E = E->IgnoreParenImpCasts(); 3532 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 3533 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 3534 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 3535 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 3536 FD->getParamDecl(PVD->getFunctionScopeIndex()) 3537 ->getCanonicalDecl() == CanonPVD) { 3538 // OpenMP [2.8.1, simd construct, Restrictions] 3539 // A list-item cannot appear in more than one aligned clause. 3540 if (AlignedArgs.count(CanonPVD) > 0) { 3541 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 3542 << 1 << E->getSourceRange(); 3543 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 3544 diag::note_omp_explicit_dsa) 3545 << getOpenMPClauseName(OMPC_aligned); 3546 continue; 3547 } 3548 AlignedArgs[CanonPVD] = E; 3549 QualType QTy = PVD->getType() 3550 .getNonReferenceType() 3551 .getUnqualifiedType() 3552 .getCanonicalType(); 3553 const Type *Ty = QTy.getTypePtrOrNull(); 3554 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 3555 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 3556 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 3557 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 3558 } 3559 continue; 3560 } 3561 } 3562 if (isa<CXXThisExpr>(E)) { 3563 if (AlignedThis) { 3564 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 3565 << 2 << E->getSourceRange(); 3566 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 3567 << getOpenMPClauseName(OMPC_aligned); 3568 } 3569 AlignedThis = E; 3570 continue; 3571 } 3572 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 3573 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 3574 } 3575 // The optional parameter of the aligned clause, alignment, must be a constant 3576 // positive integer expression. If no optional parameter is specified, 3577 // implementation-defined default alignments for SIMD instructions on the 3578 // target platforms are assumed. 3579 SmallVector<const Expr *, 4> NewAligns; 3580 for (Expr *E : Alignments) { 3581 ExprResult Align; 3582 if (E) 3583 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 3584 NewAligns.push_back(Align.get()); 3585 } 3586 // OpenMP [2.8.2, declare simd construct, Description] 3587 // The linear clause declares one or more list items to be private to a SIMD 3588 // lane and to have a linear relationship with respect to the iteration space 3589 // of a loop. 3590 // The special this pointer can be used as if was one of the arguments to the 3591 // function in any of the linear, aligned, or uniform clauses. 3592 // When a linear-step expression is specified in a linear clause it must be 3593 // either a constant integer expression or an integer-typed parameter that is 3594 // specified in a uniform clause on the directive. 3595 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 3596 const bool IsUniformedThis = UniformedLinearThis != nullptr; 3597 auto MI = LinModifiers.begin(); 3598 for (const Expr *E : Linears) { 3599 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 3600 ++MI; 3601 E = E->IgnoreParenImpCasts(); 3602 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 3603 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 3604 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 3605 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 3606 FD->getParamDecl(PVD->getFunctionScopeIndex()) 3607 ->getCanonicalDecl() == CanonPVD) { 3608 // OpenMP [2.15.3.7, linear Clause, Restrictions] 3609 // A list-item cannot appear in more than one linear clause. 3610 if (LinearArgs.count(CanonPVD) > 0) { 3611 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3612 << getOpenMPClauseName(OMPC_linear) 3613 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 3614 Diag(LinearArgs[CanonPVD]->getExprLoc(), 3615 diag::note_omp_explicit_dsa) 3616 << getOpenMPClauseName(OMPC_linear); 3617 continue; 3618 } 3619 // Each argument can appear in at most one uniform or linear clause. 3620 if (UniformedArgs.count(CanonPVD) > 0) { 3621 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3622 << getOpenMPClauseName(OMPC_linear) 3623 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 3624 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 3625 diag::note_omp_explicit_dsa) 3626 << getOpenMPClauseName(OMPC_uniform); 3627 continue; 3628 } 3629 LinearArgs[CanonPVD] = E; 3630 if (E->isValueDependent() || E->isTypeDependent() || 3631 E->isInstantiationDependent() || 3632 E->containsUnexpandedParameterPack()) 3633 continue; 3634 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 3635 PVD->getOriginalType()); 3636 continue; 3637 } 3638 } 3639 if (isa<CXXThisExpr>(E)) { 3640 if (UniformedLinearThis) { 3641 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3642 << getOpenMPClauseName(OMPC_linear) 3643 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 3644 << E->getSourceRange(); 3645 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 3646 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 3647 : OMPC_linear); 3648 continue; 3649 } 3650 UniformedLinearThis = E; 3651 if (E->isValueDependent() || E->isTypeDependent() || 3652 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 3653 continue; 3654 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 3655 E->getType()); 3656 continue; 3657 } 3658 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 3659 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 3660 } 3661 Expr *Step = nullptr; 3662 Expr *NewStep = nullptr; 3663 SmallVector<Expr *, 4> NewSteps; 3664 for (Expr *E : Steps) { 3665 // Skip the same step expression, it was checked already. 3666 if (Step == E || !E) { 3667 NewSteps.push_back(E ? NewStep : nullptr); 3668 continue; 3669 } 3670 Step = E; 3671 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 3672 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 3673 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 3674 if (UniformedArgs.count(CanonPVD) == 0) { 3675 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 3676 << Step->getSourceRange(); 3677 } else if (E->isValueDependent() || E->isTypeDependent() || 3678 E->isInstantiationDependent() || 3679 E->containsUnexpandedParameterPack() || 3680 CanonPVD->getType()->hasIntegerRepresentation()) { 3681 NewSteps.push_back(Step); 3682 } else { 3683 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 3684 << Step->getSourceRange(); 3685 } 3686 continue; 3687 } 3688 NewStep = Step; 3689 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 3690 !Step->isInstantiationDependent() && 3691 !Step->containsUnexpandedParameterPack()) { 3692 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 3693 .get(); 3694 if (NewStep) 3695 NewStep = VerifyIntegerConstantExpression(NewStep).get(); 3696 } 3697 NewSteps.push_back(NewStep); 3698 } 3699 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 3700 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 3701 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 3702 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 3703 const_cast<Expr **>(Linears.data()), Linears.size(), 3704 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 3705 NewSteps.data(), NewSteps.size(), SR); 3706 ADecl->addAttr(NewAttr); 3707 return ConvertDeclToDeclGroup(ADecl); 3708 } 3709 3710 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 3711 Stmt *AStmt, 3712 SourceLocation StartLoc, 3713 SourceLocation EndLoc) { 3714 if (!AStmt) 3715 return StmtError(); 3716 3717 auto *CS = cast<CapturedStmt>(AStmt); 3718 // 1.2.2 OpenMP Language Terminology 3719 // Structured block - An executable statement with a single entry at the 3720 // top and a single exit at the bottom. 3721 // The point of exit cannot be a branch out of the structured block. 3722 // longjmp() and throw() must not violate the entry/exit criteria. 3723 CS->getCapturedDecl()->setNothrow(); 3724 3725 setFunctionHasBranchProtectedScope(); 3726 3727 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 3728 DSAStack->isCancelRegion()); 3729 } 3730 3731 namespace { 3732 /// Helper class for checking canonical form of the OpenMP loops and 3733 /// extracting iteration space of each loop in the loop nest, that will be used 3734 /// for IR generation. 3735 class OpenMPIterationSpaceChecker { 3736 /// Reference to Sema. 3737 Sema &SemaRef; 3738 /// A location for diagnostics (when there is no some better location). 3739 SourceLocation DefaultLoc; 3740 /// A location for diagnostics (when increment is not compatible). 3741 SourceLocation ConditionLoc; 3742 /// A source location for referring to loop init later. 3743 SourceRange InitSrcRange; 3744 /// A source location for referring to condition later. 3745 SourceRange ConditionSrcRange; 3746 /// A source location for referring to increment later. 3747 SourceRange IncrementSrcRange; 3748 /// Loop variable. 3749 ValueDecl *LCDecl = nullptr; 3750 /// Reference to loop variable. 3751 Expr *LCRef = nullptr; 3752 /// Lower bound (initializer for the var). 3753 Expr *LB = nullptr; 3754 /// Upper bound. 3755 Expr *UB = nullptr; 3756 /// Loop step (increment). 3757 Expr *Step = nullptr; 3758 /// This flag is true when condition is one of: 3759 /// Var < UB 3760 /// Var <= UB 3761 /// UB > Var 3762 /// UB >= Var 3763 bool TestIsLessOp = false; 3764 /// This flag is true when condition is strict ( < or > ). 3765 bool TestIsStrictOp = false; 3766 /// This flag is true when step is subtracted on each iteration. 3767 bool SubtractStep = false; 3768 3769 public: 3770 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc) 3771 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {} 3772 /// Check init-expr for canonical loop form and save loop counter 3773 /// variable - #Var and its initialization value - #LB. 3774 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 3775 /// Check test-expr for canonical form, save upper-bound (#UB), flags 3776 /// for less/greater and for strict/non-strict comparison. 3777 bool checkAndSetCond(Expr *S); 3778 /// Check incr-expr for canonical loop form and return true if it 3779 /// does not conform, otherwise save loop step (#Step). 3780 bool checkAndSetInc(Expr *S); 3781 /// Return the loop counter variable. 3782 ValueDecl *getLoopDecl() const { return LCDecl; } 3783 /// Return the reference expression to loop counter variable. 3784 Expr *getLoopDeclRefExpr() const { return LCRef; } 3785 /// Source range of the loop init. 3786 SourceRange getInitSrcRange() const { return InitSrcRange; } 3787 /// Source range of the loop condition. 3788 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 3789 /// Source range of the loop increment. 3790 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 3791 /// True if the step should be subtracted. 3792 bool shouldSubtractStep() const { return SubtractStep; } 3793 /// Build the expression to calculate the number of iterations. 3794 Expr *buildNumIterations( 3795 Scope *S, const bool LimitedType, 3796 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 3797 /// Build the precondition expression for the loops. 3798 Expr * 3799 buildPreCond(Scope *S, Expr *Cond, 3800 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 3801 /// Build reference expression to the counter be used for codegen. 3802 DeclRefExpr * 3803 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 3804 DSAStackTy &DSA) const; 3805 /// Build reference expression to the private counter be used for 3806 /// codegen. 3807 Expr *buildPrivateCounterVar() const; 3808 /// Build initialization of the counter be used for codegen. 3809 Expr *buildCounterInit() const; 3810 /// Build step of the counter be used for codegen. 3811 Expr *buildCounterStep() const; 3812 /// Build loop data with counter value for depend clauses in ordered 3813 /// directives. 3814 Expr * 3815 buildOrderedLoopData(Scope *S, Expr *Counter, 3816 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 3817 SourceLocation Loc, Expr *Inc = nullptr, 3818 OverloadedOperatorKind OOK = OO_Amp); 3819 /// Return true if any expression is dependent. 3820 bool dependent() const; 3821 3822 private: 3823 /// Check the right-hand side of an assignment in the increment 3824 /// expression. 3825 bool checkAndSetIncRHS(Expr *RHS); 3826 /// Helper to set loop counter variable and its initializer. 3827 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB); 3828 /// Helper to set upper bound. 3829 bool setUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR, 3830 SourceLocation SL); 3831 /// Helper to set loop increment. 3832 bool setStep(Expr *NewStep, bool Subtract); 3833 }; 3834 3835 bool OpenMPIterationSpaceChecker::dependent() const { 3836 if (!LCDecl) { 3837 assert(!LB && !UB && !Step); 3838 return false; 3839 } 3840 return LCDecl->getType()->isDependentType() || 3841 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 3842 (Step && Step->isValueDependent()); 3843 } 3844 3845 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 3846 Expr *NewLCRefExpr, 3847 Expr *NewLB) { 3848 // State consistency checking to ensure correct usage. 3849 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 3850 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 3851 if (!NewLCDecl || !NewLB) 3852 return true; 3853 LCDecl = getCanonicalDecl(NewLCDecl); 3854 LCRef = NewLCRefExpr; 3855 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 3856 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 3857 if ((Ctor->isCopyOrMoveConstructor() || 3858 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 3859 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 3860 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 3861 LB = NewLB; 3862 return false; 3863 } 3864 3865 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, bool LessOp, bool StrictOp, 3866 SourceRange SR, SourceLocation SL) { 3867 // State consistency checking to ensure correct usage. 3868 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 3869 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 3870 if (!NewUB) 3871 return true; 3872 UB = NewUB; 3873 TestIsLessOp = LessOp; 3874 TestIsStrictOp = StrictOp; 3875 ConditionSrcRange = SR; 3876 ConditionLoc = SL; 3877 return false; 3878 } 3879 3880 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 3881 // State consistency checking to ensure correct usage. 3882 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 3883 if (!NewStep) 3884 return true; 3885 if (!NewStep->isValueDependent()) { 3886 // Check that the step is integer expression. 3887 SourceLocation StepLoc = NewStep->getBeginLoc(); 3888 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 3889 StepLoc, getExprAsWritten(NewStep)); 3890 if (Val.isInvalid()) 3891 return true; 3892 NewStep = Val.get(); 3893 3894 // OpenMP [2.6, Canonical Loop Form, Restrictions] 3895 // If test-expr is of form var relational-op b and relational-op is < or 3896 // <= then incr-expr must cause var to increase on each iteration of the 3897 // loop. If test-expr is of form var relational-op b and relational-op is 3898 // > or >= then incr-expr must cause var to decrease on each iteration of 3899 // the loop. 3900 // If test-expr is of form b relational-op var and relational-op is < or 3901 // <= then incr-expr must cause var to decrease on each iteration of the 3902 // loop. If test-expr is of form b relational-op var and relational-op is 3903 // > or >= then incr-expr must cause var to increase on each iteration of 3904 // the loop. 3905 llvm::APSInt Result; 3906 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); 3907 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 3908 bool IsConstNeg = 3909 IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); 3910 bool IsConstPos = 3911 IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); 3912 bool IsConstZero = IsConstant && !Result.getBoolValue(); 3913 if (UB && (IsConstZero || 3914 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract)) 3915 : (IsConstPos || (IsUnsigned && !Subtract))))) { 3916 SemaRef.Diag(NewStep->getExprLoc(), 3917 diag::err_omp_loop_incr_not_compatible) 3918 << LCDecl << TestIsLessOp << NewStep->getSourceRange(); 3919 SemaRef.Diag(ConditionLoc, 3920 diag::note_omp_loop_cond_requres_compatible_incr) 3921 << TestIsLessOp << ConditionSrcRange; 3922 return true; 3923 } 3924 if (TestIsLessOp == Subtract) { 3925 NewStep = 3926 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 3927 .get(); 3928 Subtract = !Subtract; 3929 } 3930 } 3931 3932 Step = NewStep; 3933 SubtractStep = Subtract; 3934 return false; 3935 } 3936 3937 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 3938 // Check init-expr for canonical loop form and save loop counter 3939 // variable - #Var and its initialization value - #LB. 3940 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 3941 // var = lb 3942 // integer-type var = lb 3943 // random-access-iterator-type var = lb 3944 // pointer-type var = lb 3945 // 3946 if (!S) { 3947 if (EmitDiags) { 3948 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 3949 } 3950 return true; 3951 } 3952 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 3953 if (!ExprTemp->cleanupsHaveSideEffects()) 3954 S = ExprTemp->getSubExpr(); 3955 3956 InitSrcRange = S->getSourceRange(); 3957 if (Expr *E = dyn_cast<Expr>(S)) 3958 S = E->IgnoreParens(); 3959 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 3960 if (BO->getOpcode() == BO_Assign) { 3961 Expr *LHS = BO->getLHS()->IgnoreParens(); 3962 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 3963 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 3964 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 3965 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3966 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS()); 3967 } 3968 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 3969 if (ME->isArrow() && 3970 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 3971 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3972 } 3973 } 3974 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 3975 if (DS->isSingleDecl()) { 3976 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 3977 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 3978 // Accept non-canonical init form here but emit ext. warning. 3979 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 3980 SemaRef.Diag(S->getBeginLoc(), 3981 diag::ext_omp_loop_not_canonical_init) 3982 << S->getSourceRange(); 3983 return setLCDeclAndLB( 3984 Var, 3985 buildDeclRefExpr(SemaRef, Var, 3986 Var->getType().getNonReferenceType(), 3987 DS->getBeginLoc()), 3988 Var->getInit()); 3989 } 3990 } 3991 } 3992 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 3993 if (CE->getOperator() == OO_Equal) { 3994 Expr *LHS = CE->getArg(0); 3995 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 3996 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 3997 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 3998 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3999 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1)); 4000 } 4001 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 4002 if (ME->isArrow() && 4003 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 4004 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 4005 } 4006 } 4007 } 4008 4009 if (dependent() || SemaRef.CurContext->isDependentContext()) 4010 return false; 4011 if (EmitDiags) { 4012 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init) 4013 << S->getSourceRange(); 4014 } 4015 return true; 4016 } 4017 4018 /// Ignore parenthesizes, implicit casts, copy constructor and return the 4019 /// variable (which may be the loop variable) if possible. 4020 static const ValueDecl *getInitLCDecl(const Expr *E) { 4021 if (!E) 4022 return nullptr; 4023 E = getExprAsWritten(E); 4024 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 4025 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 4026 if ((Ctor->isCopyOrMoveConstructor() || 4027 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 4028 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 4029 E = CE->getArg(0)->IgnoreParenImpCasts(); 4030 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 4031 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 4032 return getCanonicalDecl(VD); 4033 } 4034 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 4035 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 4036 return getCanonicalDecl(ME->getMemberDecl()); 4037 return nullptr; 4038 } 4039 4040 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 4041 // Check test-expr for canonical form, save upper-bound UB, flags for 4042 // less/greater and for strict/non-strict comparison. 4043 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 4044 // var relational-op b 4045 // b relational-op var 4046 // 4047 if (!S) { 4048 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl; 4049 return true; 4050 } 4051 S = getExprAsWritten(S); 4052 SourceLocation CondLoc = S->getBeginLoc(); 4053 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 4054 if (BO->isRelationalOp()) { 4055 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4056 return setUB(BO->getRHS(), 4057 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 4058 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 4059 BO->getSourceRange(), BO->getOperatorLoc()); 4060 if (getInitLCDecl(BO->getRHS()) == LCDecl) 4061 return setUB(BO->getLHS(), 4062 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 4063 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 4064 BO->getSourceRange(), BO->getOperatorLoc()); 4065 } 4066 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 4067 if (CE->getNumArgs() == 2) { 4068 auto Op = CE->getOperator(); 4069 switch (Op) { 4070 case OO_Greater: 4071 case OO_GreaterEqual: 4072 case OO_Less: 4073 case OO_LessEqual: 4074 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4075 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 4076 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 4077 CE->getOperatorLoc()); 4078 if (getInitLCDecl(CE->getArg(1)) == LCDecl) 4079 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 4080 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 4081 CE->getOperatorLoc()); 4082 break; 4083 default: 4084 break; 4085 } 4086 } 4087 } 4088 if (dependent() || SemaRef.CurContext->isDependentContext()) 4089 return false; 4090 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 4091 << S->getSourceRange() << LCDecl; 4092 return true; 4093 } 4094 4095 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 4096 // RHS of canonical loop form increment can be: 4097 // var + incr 4098 // incr + var 4099 // var - incr 4100 // 4101 RHS = RHS->IgnoreParenImpCasts(); 4102 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 4103 if (BO->isAdditiveOp()) { 4104 bool IsAdd = BO->getOpcode() == BO_Add; 4105 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4106 return setStep(BO->getRHS(), !IsAdd); 4107 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 4108 return setStep(BO->getLHS(), /*Subtract=*/false); 4109 } 4110 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 4111 bool IsAdd = CE->getOperator() == OO_Plus; 4112 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 4113 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4114 return setStep(CE->getArg(1), !IsAdd); 4115 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 4116 return setStep(CE->getArg(0), /*Subtract=*/false); 4117 } 4118 } 4119 if (dependent() || SemaRef.CurContext->isDependentContext()) 4120 return false; 4121 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 4122 << RHS->getSourceRange() << LCDecl; 4123 return true; 4124 } 4125 4126 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 4127 // Check incr-expr for canonical loop form and return true if it 4128 // does not conform. 4129 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 4130 // ++var 4131 // var++ 4132 // --var 4133 // var-- 4134 // var += incr 4135 // var -= incr 4136 // var = var + incr 4137 // var = incr + var 4138 // var = var - incr 4139 // 4140 if (!S) { 4141 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 4142 return true; 4143 } 4144 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 4145 if (!ExprTemp->cleanupsHaveSideEffects()) 4146 S = ExprTemp->getSubExpr(); 4147 4148 IncrementSrcRange = S->getSourceRange(); 4149 S = S->IgnoreParens(); 4150 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 4151 if (UO->isIncrementDecrementOp() && 4152 getInitLCDecl(UO->getSubExpr()) == LCDecl) 4153 return setStep(SemaRef 4154 .ActOnIntegerConstant(UO->getBeginLoc(), 4155 (UO->isDecrementOp() ? -1 : 1)) 4156 .get(), 4157 /*Subtract=*/false); 4158 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 4159 switch (BO->getOpcode()) { 4160 case BO_AddAssign: 4161 case BO_SubAssign: 4162 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4163 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 4164 break; 4165 case BO_Assign: 4166 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4167 return checkAndSetIncRHS(BO->getRHS()); 4168 break; 4169 default: 4170 break; 4171 } 4172 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 4173 switch (CE->getOperator()) { 4174 case OO_PlusPlus: 4175 case OO_MinusMinus: 4176 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4177 return setStep(SemaRef 4178 .ActOnIntegerConstant( 4179 CE->getBeginLoc(), 4180 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 4181 .get(), 4182 /*Subtract=*/false); 4183 break; 4184 case OO_PlusEqual: 4185 case OO_MinusEqual: 4186 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4187 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 4188 break; 4189 case OO_Equal: 4190 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4191 return checkAndSetIncRHS(CE->getArg(1)); 4192 break; 4193 default: 4194 break; 4195 } 4196 } 4197 if (dependent() || SemaRef.CurContext->isDependentContext()) 4198 return false; 4199 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 4200 << S->getSourceRange() << LCDecl; 4201 return true; 4202 } 4203 4204 static ExprResult 4205 tryBuildCapture(Sema &SemaRef, Expr *Capture, 4206 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 4207 if (SemaRef.CurContext->isDependentContext()) 4208 return ExprResult(Capture); 4209 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 4210 return SemaRef.PerformImplicitConversion( 4211 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 4212 /*AllowExplicit=*/true); 4213 auto I = Captures.find(Capture); 4214 if (I != Captures.end()) 4215 return buildCapture(SemaRef, Capture, I->second); 4216 DeclRefExpr *Ref = nullptr; 4217 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 4218 Captures[Capture] = Ref; 4219 return Res; 4220 } 4221 4222 /// Build the expression to calculate the number of iterations. 4223 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 4224 Scope *S, const bool LimitedType, 4225 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 4226 ExprResult Diff; 4227 QualType VarType = LCDecl->getType().getNonReferenceType(); 4228 if (VarType->isIntegerType() || VarType->isPointerType() || 4229 SemaRef.getLangOpts().CPlusPlus) { 4230 // Upper - Lower 4231 Expr *UBExpr = TestIsLessOp ? UB : LB; 4232 Expr *LBExpr = TestIsLessOp ? LB : UB; 4233 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 4234 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 4235 if (!Upper || !Lower) 4236 return nullptr; 4237 4238 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 4239 4240 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 4241 // BuildBinOp already emitted error, this one is to point user to upper 4242 // and lower bound, and to tell what is passed to 'operator-'. 4243 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 4244 << Upper->getSourceRange() << Lower->getSourceRange(); 4245 return nullptr; 4246 } 4247 } 4248 4249 if (!Diff.isUsable()) 4250 return nullptr; 4251 4252 // Upper - Lower [- 1] 4253 if (TestIsStrictOp) 4254 Diff = SemaRef.BuildBinOp( 4255 S, DefaultLoc, BO_Sub, Diff.get(), 4256 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 4257 if (!Diff.isUsable()) 4258 return nullptr; 4259 4260 // Upper - Lower [- 1] + Step 4261 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 4262 if (!NewStep.isUsable()) 4263 return nullptr; 4264 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 4265 if (!Diff.isUsable()) 4266 return nullptr; 4267 4268 // Parentheses (for dumping/debugging purposes only). 4269 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 4270 if (!Diff.isUsable()) 4271 return nullptr; 4272 4273 // (Upper - Lower [- 1] + Step) / Step 4274 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 4275 if (!Diff.isUsable()) 4276 return nullptr; 4277 4278 // OpenMP runtime requires 32-bit or 64-bit loop variables. 4279 QualType Type = Diff.get()->getType(); 4280 ASTContext &C = SemaRef.Context; 4281 bool UseVarType = VarType->hasIntegerRepresentation() && 4282 C.getTypeSize(Type) > C.getTypeSize(VarType); 4283 if (!Type->isIntegerType() || UseVarType) { 4284 unsigned NewSize = 4285 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 4286 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 4287 : Type->hasSignedIntegerRepresentation(); 4288 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 4289 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 4290 Diff = SemaRef.PerformImplicitConversion( 4291 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 4292 if (!Diff.isUsable()) 4293 return nullptr; 4294 } 4295 } 4296 if (LimitedType) { 4297 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 4298 if (NewSize != C.getTypeSize(Type)) { 4299 if (NewSize < C.getTypeSize(Type)) { 4300 assert(NewSize == 64 && "incorrect loop var size"); 4301 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 4302 << InitSrcRange << ConditionSrcRange; 4303 } 4304 QualType NewType = C.getIntTypeForBitwidth( 4305 NewSize, Type->hasSignedIntegerRepresentation() || 4306 C.getTypeSize(Type) < NewSize); 4307 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 4308 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 4309 Sema::AA_Converting, true); 4310 if (!Diff.isUsable()) 4311 return nullptr; 4312 } 4313 } 4314 } 4315 4316 return Diff.get(); 4317 } 4318 4319 Expr *OpenMPIterationSpaceChecker::buildPreCond( 4320 Scope *S, Expr *Cond, 4321 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 4322 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 4323 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 4324 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 4325 4326 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 4327 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 4328 if (!NewLB.isUsable() || !NewUB.isUsable()) 4329 return nullptr; 4330 4331 ExprResult CondExpr = 4332 SemaRef.BuildBinOp(S, DefaultLoc, 4333 TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE) 4334 : (TestIsStrictOp ? BO_GT : BO_GE), 4335 NewLB.get(), NewUB.get()); 4336 if (CondExpr.isUsable()) { 4337 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 4338 SemaRef.Context.BoolTy)) 4339 CondExpr = SemaRef.PerformImplicitConversion( 4340 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 4341 /*AllowExplicit=*/true); 4342 } 4343 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 4344 // Otherwise use original loop conditon and evaluate it in runtime. 4345 return CondExpr.isUsable() ? CondExpr.get() : Cond; 4346 } 4347 4348 /// Build reference expression to the counter be used for codegen. 4349 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 4350 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 4351 DSAStackTy &DSA) const { 4352 auto *VD = dyn_cast<VarDecl>(LCDecl); 4353 if (!VD) { 4354 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 4355 DeclRefExpr *Ref = buildDeclRefExpr( 4356 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 4357 const DSAStackTy::DSAVarData Data = 4358 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 4359 // If the loop control decl is explicitly marked as private, do not mark it 4360 // as captured again. 4361 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 4362 Captures.insert(std::make_pair(LCRef, Ref)); 4363 return Ref; 4364 } 4365 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(), 4366 DefaultLoc); 4367 } 4368 4369 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 4370 if (LCDecl && !LCDecl->isInvalidDecl()) { 4371 QualType Type = LCDecl->getType().getNonReferenceType(); 4372 VarDecl *PrivateVar = buildVarDecl( 4373 SemaRef, DefaultLoc, Type, LCDecl->getName(), 4374 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 4375 isa<VarDecl>(LCDecl) 4376 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 4377 : nullptr); 4378 if (PrivateVar->isInvalidDecl()) 4379 return nullptr; 4380 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 4381 } 4382 return nullptr; 4383 } 4384 4385 /// Build initialization of the counter to be used for codegen. 4386 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 4387 4388 /// Build step of the counter be used for codegen. 4389 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 4390 4391 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData( 4392 Scope *S, Expr *Counter, 4393 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc, 4394 Expr *Inc, OverloadedOperatorKind OOK) { 4395 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get(); 4396 if (!Cnt) 4397 return nullptr; 4398 if (Inc) { 4399 assert((OOK == OO_Plus || OOK == OO_Minus) && 4400 "Expected only + or - operations for depend clauses."); 4401 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub; 4402 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get(); 4403 if (!Cnt) 4404 return nullptr; 4405 } 4406 ExprResult Diff; 4407 QualType VarType = LCDecl->getType().getNonReferenceType(); 4408 if (VarType->isIntegerType() || VarType->isPointerType() || 4409 SemaRef.getLangOpts().CPlusPlus) { 4410 // Upper - Lower 4411 Expr *Upper = 4412 TestIsLessOp ? Cnt : tryBuildCapture(SemaRef, UB, Captures).get(); 4413 Expr *Lower = 4414 TestIsLessOp ? tryBuildCapture(SemaRef, LB, Captures).get() : Cnt; 4415 if (!Upper || !Lower) 4416 return nullptr; 4417 4418 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 4419 4420 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 4421 // BuildBinOp already emitted error, this one is to point user to upper 4422 // and lower bound, and to tell what is passed to 'operator-'. 4423 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 4424 << Upper->getSourceRange() << Lower->getSourceRange(); 4425 return nullptr; 4426 } 4427 } 4428 4429 if (!Diff.isUsable()) 4430 return nullptr; 4431 4432 // Parentheses (for dumping/debugging purposes only). 4433 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 4434 if (!Diff.isUsable()) 4435 return nullptr; 4436 4437 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 4438 if (!NewStep.isUsable()) 4439 return nullptr; 4440 // (Upper - Lower) / Step 4441 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 4442 if (!Diff.isUsable()) 4443 return nullptr; 4444 4445 return Diff.get(); 4446 } 4447 4448 /// Iteration space of a single for loop. 4449 struct LoopIterationSpace final { 4450 /// Condition of the loop. 4451 Expr *PreCond = nullptr; 4452 /// This expression calculates the number of iterations in the loop. 4453 /// It is always possible to calculate it before starting the loop. 4454 Expr *NumIterations = nullptr; 4455 /// The loop counter variable. 4456 Expr *CounterVar = nullptr; 4457 /// Private loop counter variable. 4458 Expr *PrivateCounterVar = nullptr; 4459 /// This is initializer for the initial value of #CounterVar. 4460 Expr *CounterInit = nullptr; 4461 /// This is step for the #CounterVar used to generate its update: 4462 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 4463 Expr *CounterStep = nullptr; 4464 /// Should step be subtracted? 4465 bool Subtract = false; 4466 /// Source range of the loop init. 4467 SourceRange InitSrcRange; 4468 /// Source range of the loop condition. 4469 SourceRange CondSrcRange; 4470 /// Source range of the loop increment. 4471 SourceRange IncSrcRange; 4472 }; 4473 4474 } // namespace 4475 4476 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 4477 assert(getLangOpts().OpenMP && "OpenMP is not active."); 4478 assert(Init && "Expected loop in canonical form."); 4479 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 4480 if (AssociatedLoops > 0 && 4481 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 4482 OpenMPIterationSpaceChecker ISC(*this, ForLoc); 4483 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 4484 if (ValueDecl *D = ISC.getLoopDecl()) { 4485 auto *VD = dyn_cast<VarDecl>(D); 4486 if (!VD) { 4487 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 4488 VD = Private; 4489 } else { 4490 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 4491 /*WithInit=*/false); 4492 VD = cast<VarDecl>(Ref->getDecl()); 4493 } 4494 } 4495 DSAStack->addLoopControlVariable(D, VD); 4496 } 4497 } 4498 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 4499 } 4500 } 4501 4502 /// Called on a for stmt to check and extract its iteration space 4503 /// for further processing (such as collapsing). 4504 static bool checkOpenMPIterationSpace( 4505 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 4506 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 4507 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr, 4508 Expr *OrderedLoopCountExpr, 4509 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 4510 LoopIterationSpace &ResultIterSpace, 4511 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 4512 // OpenMP [2.6, Canonical Loop Form] 4513 // for (init-expr; test-expr; incr-expr) structured-block 4514 auto *For = dyn_cast_or_null<ForStmt>(S); 4515 if (!For) { 4516 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for) 4517 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 4518 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount 4519 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 4520 if (TotalNestedLoopCount > 1) { 4521 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 4522 SemaRef.Diag(DSA.getConstructLoc(), 4523 diag::note_omp_collapse_ordered_expr) 4524 << 2 << CollapseLoopCountExpr->getSourceRange() 4525 << OrderedLoopCountExpr->getSourceRange(); 4526 else if (CollapseLoopCountExpr) 4527 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 4528 diag::note_omp_collapse_ordered_expr) 4529 << 0 << CollapseLoopCountExpr->getSourceRange(); 4530 else 4531 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 4532 diag::note_omp_collapse_ordered_expr) 4533 << 1 << OrderedLoopCountExpr->getSourceRange(); 4534 } 4535 return true; 4536 } 4537 assert(For->getBody()); 4538 4539 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc()); 4540 4541 // Check init. 4542 Stmt *Init = For->getInit(); 4543 if (ISC.checkAndSetInit(Init)) 4544 return true; 4545 4546 bool HasErrors = false; 4547 4548 // Check loop variable's type. 4549 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 4550 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 4551 4552 // OpenMP [2.6, Canonical Loop Form] 4553 // Var is one of the following: 4554 // A variable of signed or unsigned integer type. 4555 // For C++, a variable of a random access iterator type. 4556 // For C, a variable of a pointer type. 4557 QualType VarType = LCDecl->getType().getNonReferenceType(); 4558 if (!VarType->isDependentType() && !VarType->isIntegerType() && 4559 !VarType->isPointerType() && 4560 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 4561 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type) 4562 << SemaRef.getLangOpts().CPlusPlus; 4563 HasErrors = true; 4564 } 4565 4566 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 4567 // a Construct 4568 // The loop iteration variable(s) in the associated for-loop(s) of a for or 4569 // parallel for construct is (are) private. 4570 // The loop iteration variable in the associated for-loop of a simd 4571 // construct with just one associated for-loop is linear with a 4572 // constant-linear-step that is the increment of the associated for-loop. 4573 // Exclude loop var from the list of variables with implicitly defined data 4574 // sharing attributes. 4575 VarsWithImplicitDSA.erase(LCDecl); 4576 4577 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 4578 // in a Construct, C/C++]. 4579 // The loop iteration variable in the associated for-loop of a simd 4580 // construct with just one associated for-loop may be listed in a linear 4581 // clause with a constant-linear-step that is the increment of the 4582 // associated for-loop. 4583 // The loop iteration variable(s) in the associated for-loop(s) of a for or 4584 // parallel for construct may be listed in a private or lastprivate clause. 4585 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false); 4586 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is 4587 // declared in the loop and it is predetermined as a private. 4588 OpenMPClauseKind PredeterminedCKind = 4589 isOpenMPSimdDirective(DKind) 4590 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate) 4591 : OMPC_private; 4592 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 4593 DVar.CKind != PredeterminedCKind) || 4594 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 4595 isOpenMPDistributeDirective(DKind)) && 4596 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 4597 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 4598 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 4599 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa) 4600 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind) 4601 << getOpenMPClauseName(PredeterminedCKind); 4602 if (DVar.RefExpr == nullptr) 4603 DVar.CKind = PredeterminedCKind; 4604 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true); 4605 HasErrors = true; 4606 } else if (LoopDeclRefExpr != nullptr) { 4607 // Make the loop iteration variable private (for worksharing constructs), 4608 // linear (for simd directives with the only one associated loop) or 4609 // lastprivate (for simd directives with several collapsed or ordered 4610 // loops). 4611 if (DVar.CKind == OMPC_unknown) 4612 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate, 4613 [](OpenMPDirectiveKind) -> bool { return true; }, 4614 /*FromParent=*/false); 4615 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind); 4616 } 4617 4618 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 4619 4620 // Check test-expr. 4621 HasErrors |= ISC.checkAndSetCond(For->getCond()); 4622 4623 // Check incr-expr. 4624 HasErrors |= ISC.checkAndSetInc(For->getInc()); 4625 } 4626 4627 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 4628 return HasErrors; 4629 4630 // Build the loop's iteration space representation. 4631 ResultIterSpace.PreCond = 4632 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures); 4633 ResultIterSpace.NumIterations = ISC.buildNumIterations( 4634 DSA.getCurScope(), 4635 (isOpenMPWorksharingDirective(DKind) || 4636 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)), 4637 Captures); 4638 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA); 4639 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar(); 4640 ResultIterSpace.CounterInit = ISC.buildCounterInit(); 4641 ResultIterSpace.CounterStep = ISC.buildCounterStep(); 4642 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange(); 4643 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange(); 4644 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange(); 4645 ResultIterSpace.Subtract = ISC.shouldSubtractStep(); 4646 4647 HasErrors |= (ResultIterSpace.PreCond == nullptr || 4648 ResultIterSpace.NumIterations == nullptr || 4649 ResultIterSpace.CounterVar == nullptr || 4650 ResultIterSpace.PrivateCounterVar == nullptr || 4651 ResultIterSpace.CounterInit == nullptr || 4652 ResultIterSpace.CounterStep == nullptr); 4653 if (!HasErrors && DSA.isOrderedRegion()) { 4654 if (DSA.getOrderedRegionParam().second->getNumForLoops()) { 4655 if (CurrentNestedLoopCount < 4656 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) { 4657 DSA.getOrderedRegionParam().second->setLoopNumIterations( 4658 CurrentNestedLoopCount, ResultIterSpace.NumIterations); 4659 DSA.getOrderedRegionParam().second->setLoopCounter( 4660 CurrentNestedLoopCount, ResultIterSpace.CounterVar); 4661 } 4662 } 4663 for (auto &Pair : DSA.getDoacrossDependClauses()) { 4664 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) { 4665 // Erroneous case - clause has some problems. 4666 continue; 4667 } 4668 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink && 4669 Pair.second.size() <= CurrentNestedLoopCount) { 4670 // Erroneous case - clause has some problems. 4671 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr); 4672 continue; 4673 } 4674 Expr *CntValue; 4675 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 4676 CntValue = ISC.buildOrderedLoopData( 4677 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures, 4678 Pair.first->getDependencyLoc()); 4679 else 4680 CntValue = ISC.buildOrderedLoopData( 4681 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures, 4682 Pair.first->getDependencyLoc(), 4683 Pair.second[CurrentNestedLoopCount].first, 4684 Pair.second[CurrentNestedLoopCount].second); 4685 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue); 4686 } 4687 } 4688 4689 return HasErrors; 4690 } 4691 4692 /// Build 'VarRef = Start. 4693 static ExprResult 4694 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 4695 ExprResult Start, 4696 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 4697 // Build 'VarRef = Start. 4698 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures); 4699 if (!NewStart.isUsable()) 4700 return ExprError(); 4701 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 4702 VarRef.get()->getType())) { 4703 NewStart = SemaRef.PerformImplicitConversion( 4704 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 4705 /*AllowExplicit=*/true); 4706 if (!NewStart.isUsable()) 4707 return ExprError(); 4708 } 4709 4710 ExprResult Init = 4711 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 4712 return Init; 4713 } 4714 4715 /// Build 'VarRef = Start + Iter * Step'. 4716 static ExprResult buildCounterUpdate( 4717 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 4718 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 4719 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 4720 // Add parentheses (for debugging purposes only). 4721 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 4722 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 4723 !Step.isUsable()) 4724 return ExprError(); 4725 4726 ExprResult NewStep = Step; 4727 if (Captures) 4728 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 4729 if (NewStep.isInvalid()) 4730 return ExprError(); 4731 ExprResult Update = 4732 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 4733 if (!Update.isUsable()) 4734 return ExprError(); 4735 4736 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 4737 // 'VarRef = Start (+|-) Iter * Step'. 4738 ExprResult NewStart = Start; 4739 if (Captures) 4740 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 4741 if (NewStart.isInvalid()) 4742 return ExprError(); 4743 4744 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 4745 ExprResult SavedUpdate = Update; 4746 ExprResult UpdateVal; 4747 if (VarRef.get()->getType()->isOverloadableType() || 4748 NewStart.get()->getType()->isOverloadableType() || 4749 Update.get()->getType()->isOverloadableType()) { 4750 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 4751 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 4752 Update = 4753 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 4754 if (Update.isUsable()) { 4755 UpdateVal = 4756 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 4757 VarRef.get(), SavedUpdate.get()); 4758 if (UpdateVal.isUsable()) { 4759 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 4760 UpdateVal.get()); 4761 } 4762 } 4763 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 4764 } 4765 4766 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 4767 if (!Update.isUsable() || !UpdateVal.isUsable()) { 4768 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 4769 NewStart.get(), SavedUpdate.get()); 4770 if (!Update.isUsable()) 4771 return ExprError(); 4772 4773 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 4774 VarRef.get()->getType())) { 4775 Update = SemaRef.PerformImplicitConversion( 4776 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 4777 if (!Update.isUsable()) 4778 return ExprError(); 4779 } 4780 4781 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 4782 } 4783 return Update; 4784 } 4785 4786 /// Convert integer expression \a E to make it have at least \a Bits 4787 /// bits. 4788 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 4789 if (E == nullptr) 4790 return ExprError(); 4791 ASTContext &C = SemaRef.Context; 4792 QualType OldType = E->getType(); 4793 unsigned HasBits = C.getTypeSize(OldType); 4794 if (HasBits >= Bits) 4795 return ExprResult(E); 4796 // OK to convert to signed, because new type has more bits than old. 4797 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 4798 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 4799 true); 4800 } 4801 4802 /// Check if the given expression \a E is a constant integer that fits 4803 /// into \a Bits bits. 4804 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 4805 if (E == nullptr) 4806 return false; 4807 llvm::APSInt Result; 4808 if (E->isIntegerConstantExpr(Result, SemaRef.Context)) 4809 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); 4810 return false; 4811 } 4812 4813 /// Build preinits statement for the given declarations. 4814 static Stmt *buildPreInits(ASTContext &Context, 4815 MutableArrayRef<Decl *> PreInits) { 4816 if (!PreInits.empty()) { 4817 return new (Context) DeclStmt( 4818 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 4819 SourceLocation(), SourceLocation()); 4820 } 4821 return nullptr; 4822 } 4823 4824 /// Build preinits statement for the given declarations. 4825 static Stmt * 4826 buildPreInits(ASTContext &Context, 4827 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 4828 if (!Captures.empty()) { 4829 SmallVector<Decl *, 16> PreInits; 4830 for (const auto &Pair : Captures) 4831 PreInits.push_back(Pair.second->getDecl()); 4832 return buildPreInits(Context, PreInits); 4833 } 4834 return nullptr; 4835 } 4836 4837 /// Build postupdate expression for the given list of postupdates expressions. 4838 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 4839 Expr *PostUpdate = nullptr; 4840 if (!PostUpdates.empty()) { 4841 for (Expr *E : PostUpdates) { 4842 Expr *ConvE = S.BuildCStyleCastExpr( 4843 E->getExprLoc(), 4844 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 4845 E->getExprLoc(), E) 4846 .get(); 4847 PostUpdate = PostUpdate 4848 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 4849 PostUpdate, ConvE) 4850 .get() 4851 : ConvE; 4852 } 4853 } 4854 return PostUpdate; 4855 } 4856 4857 /// Called on a for stmt to check itself and nested loops (if any). 4858 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 4859 /// number of collapsed loops otherwise. 4860 static unsigned 4861 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 4862 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 4863 DSAStackTy &DSA, 4864 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 4865 OMPLoopDirective::HelperExprs &Built) { 4866 unsigned NestedLoopCount = 1; 4867 if (CollapseLoopCountExpr) { 4868 // Found 'collapse' clause - calculate collapse number. 4869 llvm::APSInt Result; 4870 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) 4871 NestedLoopCount = Result.getLimitedValue(); 4872 } 4873 unsigned OrderedLoopCount = 1; 4874 if (OrderedLoopCountExpr) { 4875 // Found 'ordered' clause - calculate collapse number. 4876 llvm::APSInt Result; 4877 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 4878 if (Result.getLimitedValue() < NestedLoopCount) { 4879 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 4880 diag::err_omp_wrong_ordered_loop_count) 4881 << OrderedLoopCountExpr->getSourceRange(); 4882 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 4883 diag::note_collapse_loop_count) 4884 << CollapseLoopCountExpr->getSourceRange(); 4885 } 4886 OrderedLoopCount = Result.getLimitedValue(); 4887 } 4888 } 4889 // This is helper routine for loop directives (e.g., 'for', 'simd', 4890 // 'for simd', etc.). 4891 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 4892 SmallVector<LoopIterationSpace, 4> IterSpaces; 4893 IterSpaces.resize(std::max(OrderedLoopCount, NestedLoopCount)); 4894 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); 4895 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 4896 if (checkOpenMPIterationSpace( 4897 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 4898 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 4899 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt], 4900 Captures)) 4901 return 0; 4902 // Move on to the next nested for loop, or to the loop body. 4903 // OpenMP [2.8.1, simd construct, Restrictions] 4904 // All loops associated with the construct must be perfectly nested; that 4905 // is, there must be no intervening code nor any OpenMP directive between 4906 // any two loops. 4907 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); 4908 } 4909 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) { 4910 if (checkOpenMPIterationSpace( 4911 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 4912 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 4913 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt], 4914 Captures)) 4915 return 0; 4916 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) { 4917 // Handle initialization of captured loop iterator variables. 4918 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar); 4919 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) { 4920 Captures[DRE] = DRE; 4921 } 4922 } 4923 // Move on to the next nested for loop, or to the loop body. 4924 // OpenMP [2.8.1, simd construct, Restrictions] 4925 // All loops associated with the construct must be perfectly nested; that 4926 // is, there must be no intervening code nor any OpenMP directive between 4927 // any two loops. 4928 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); 4929 } 4930 4931 Built.clear(/* size */ NestedLoopCount); 4932 4933 if (SemaRef.CurContext->isDependentContext()) 4934 return NestedLoopCount; 4935 4936 // An example of what is generated for the following code: 4937 // 4938 // #pragma omp simd collapse(2) ordered(2) 4939 // for (i = 0; i < NI; ++i) 4940 // for (k = 0; k < NK; ++k) 4941 // for (j = J0; j < NJ; j+=2) { 4942 // <loop body> 4943 // } 4944 // 4945 // We generate the code below. 4946 // Note: the loop body may be outlined in CodeGen. 4947 // Note: some counters may be C++ classes, operator- is used to find number of 4948 // iterations and operator+= to calculate counter value. 4949 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 4950 // or i64 is currently supported). 4951 // 4952 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 4953 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 4954 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 4955 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 4956 // // similar updates for vars in clauses (e.g. 'linear') 4957 // <loop body (using local i and j)> 4958 // } 4959 // i = NI; // assign final values of counters 4960 // j = NJ; 4961 // 4962 4963 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 4964 // the iteration counts of the collapsed for loops. 4965 // Precondition tests if there is at least one iteration (all conditions are 4966 // true). 4967 auto PreCond = ExprResult(IterSpaces[0].PreCond); 4968 Expr *N0 = IterSpaces[0].NumIterations; 4969 ExprResult LastIteration32 = 4970 widenIterationCount(/*Bits=*/32, 4971 SemaRef 4972 .PerformImplicitConversion( 4973 N0->IgnoreImpCasts(), N0->getType(), 4974 Sema::AA_Converting, /*AllowExplicit=*/true) 4975 .get(), 4976 SemaRef); 4977 ExprResult LastIteration64 = widenIterationCount( 4978 /*Bits=*/64, 4979 SemaRef 4980 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 4981 Sema::AA_Converting, 4982 /*AllowExplicit=*/true) 4983 .get(), 4984 SemaRef); 4985 4986 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 4987 return NestedLoopCount; 4988 4989 ASTContext &C = SemaRef.Context; 4990 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 4991 4992 Scope *CurScope = DSA.getCurScope(); 4993 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 4994 if (PreCond.isUsable()) { 4995 PreCond = 4996 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 4997 PreCond.get(), IterSpaces[Cnt].PreCond); 4998 } 4999 Expr *N = IterSpaces[Cnt].NumIterations; 5000 SourceLocation Loc = N->getExprLoc(); 5001 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 5002 if (LastIteration32.isUsable()) 5003 LastIteration32 = SemaRef.BuildBinOp( 5004 CurScope, Loc, BO_Mul, LastIteration32.get(), 5005 SemaRef 5006 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 5007 Sema::AA_Converting, 5008 /*AllowExplicit=*/true) 5009 .get()); 5010 if (LastIteration64.isUsable()) 5011 LastIteration64 = SemaRef.BuildBinOp( 5012 CurScope, Loc, BO_Mul, LastIteration64.get(), 5013 SemaRef 5014 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 5015 Sema::AA_Converting, 5016 /*AllowExplicit=*/true) 5017 .get()); 5018 } 5019 5020 // Choose either the 32-bit or 64-bit version. 5021 ExprResult LastIteration = LastIteration64; 5022 if (LastIteration32.isUsable() && 5023 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 5024 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 5025 fitsInto( 5026 /*Bits=*/32, 5027 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 5028 LastIteration64.get(), SemaRef))) 5029 LastIteration = LastIteration32; 5030 QualType VType = LastIteration.get()->getType(); 5031 QualType RealVType = VType; 5032 QualType StrideVType = VType; 5033 if (isOpenMPTaskLoopDirective(DKind)) { 5034 VType = 5035 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 5036 StrideVType = 5037 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 5038 } 5039 5040 if (!LastIteration.isUsable()) 5041 return 0; 5042 5043 // Save the number of iterations. 5044 ExprResult NumIterations = LastIteration; 5045 { 5046 LastIteration = SemaRef.BuildBinOp( 5047 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 5048 LastIteration.get(), 5049 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 5050 if (!LastIteration.isUsable()) 5051 return 0; 5052 } 5053 5054 // Calculate the last iteration number beforehand instead of doing this on 5055 // each iteration. Do not do this if the number of iterations may be kfold-ed. 5056 llvm::APSInt Result; 5057 bool IsConstant = 5058 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); 5059 ExprResult CalcLastIteration; 5060 if (!IsConstant) { 5061 ExprResult SaveRef = 5062 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 5063 LastIteration = SaveRef; 5064 5065 // Prepare SaveRef + 1. 5066 NumIterations = SemaRef.BuildBinOp( 5067 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 5068 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 5069 if (!NumIterations.isUsable()) 5070 return 0; 5071 } 5072 5073 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 5074 5075 // Build variables passed into runtime, necessary for worksharing directives. 5076 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 5077 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 5078 isOpenMPDistributeDirective(DKind)) { 5079 // Lower bound variable, initialized with zero. 5080 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 5081 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 5082 SemaRef.AddInitializerToDecl(LBDecl, 5083 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 5084 /*DirectInit*/ false); 5085 5086 // Upper bound variable, initialized with last iteration number. 5087 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 5088 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 5089 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 5090 /*DirectInit*/ false); 5091 5092 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 5093 // This will be used to implement clause 'lastprivate'. 5094 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 5095 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 5096 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 5097 SemaRef.AddInitializerToDecl(ILDecl, 5098 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 5099 /*DirectInit*/ false); 5100 5101 // Stride variable returned by runtime (we initialize it to 1 by default). 5102 VarDecl *STDecl = 5103 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 5104 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 5105 SemaRef.AddInitializerToDecl(STDecl, 5106 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 5107 /*DirectInit*/ false); 5108 5109 // Build expression: UB = min(UB, LastIteration) 5110 // It is necessary for CodeGen of directives with static scheduling. 5111 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 5112 UB.get(), LastIteration.get()); 5113 ExprResult CondOp = SemaRef.ActOnConditionalOp( 5114 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 5115 LastIteration.get(), UB.get()); 5116 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 5117 CondOp.get()); 5118 EUB = SemaRef.ActOnFinishFullExpr(EUB.get()); 5119 5120 // If we have a combined directive that combines 'distribute', 'for' or 5121 // 'simd' we need to be able to access the bounds of the schedule of the 5122 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 5123 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 5124 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5125 // Lower bound variable, initialized with zero. 5126 VarDecl *CombLBDecl = 5127 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 5128 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 5129 SemaRef.AddInitializerToDecl( 5130 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 5131 /*DirectInit*/ false); 5132 5133 // Upper bound variable, initialized with last iteration number. 5134 VarDecl *CombUBDecl = 5135 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 5136 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 5137 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 5138 /*DirectInit*/ false); 5139 5140 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 5141 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 5142 ExprResult CombCondOp = 5143 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 5144 LastIteration.get(), CombUB.get()); 5145 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 5146 CombCondOp.get()); 5147 CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get()); 5148 5149 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 5150 // We expect to have at least 2 more parameters than the 'parallel' 5151 // directive does - the lower and upper bounds of the previous schedule. 5152 assert(CD->getNumParams() >= 4 && 5153 "Unexpected number of parameters in loop combined directive"); 5154 5155 // Set the proper type for the bounds given what we learned from the 5156 // enclosed loops. 5157 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 5158 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 5159 5160 // Previous lower and upper bounds are obtained from the region 5161 // parameters. 5162 PrevLB = 5163 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 5164 PrevUB = 5165 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 5166 } 5167 } 5168 5169 // Build the iteration variable and its initialization before loop. 5170 ExprResult IV; 5171 ExprResult Init, CombInit; 5172 { 5173 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 5174 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 5175 Expr *RHS = 5176 (isOpenMPWorksharingDirective(DKind) || 5177 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 5178 ? LB.get() 5179 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 5180 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 5181 Init = SemaRef.ActOnFinishFullExpr(Init.get()); 5182 5183 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5184 Expr *CombRHS = 5185 (isOpenMPWorksharingDirective(DKind) || 5186 isOpenMPTaskLoopDirective(DKind) || 5187 isOpenMPDistributeDirective(DKind)) 5188 ? CombLB.get() 5189 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 5190 CombInit = 5191 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 5192 CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get()); 5193 } 5194 } 5195 5196 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops. 5197 SourceLocation CondLoc = AStmt->getBeginLoc(); 5198 ExprResult Cond = 5199 (isOpenMPWorksharingDirective(DKind) || 5200 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 5201 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get()) 5202 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 5203 NumIterations.get()); 5204 ExprResult CombCond; 5205 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5206 CombCond = 5207 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get()); 5208 } 5209 // Loop increment (IV = IV + 1) 5210 SourceLocation IncLoc = AStmt->getBeginLoc(); 5211 ExprResult Inc = 5212 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 5213 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 5214 if (!Inc.isUsable()) 5215 return 0; 5216 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 5217 Inc = SemaRef.ActOnFinishFullExpr(Inc.get()); 5218 if (!Inc.isUsable()) 5219 return 0; 5220 5221 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 5222 // Used for directives with static scheduling. 5223 // In combined construct, add combined version that use CombLB and CombUB 5224 // base variables for the update 5225 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 5226 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 5227 isOpenMPDistributeDirective(DKind)) { 5228 // LB + ST 5229 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 5230 if (!NextLB.isUsable()) 5231 return 0; 5232 // LB = LB + ST 5233 NextLB = 5234 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 5235 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get()); 5236 if (!NextLB.isUsable()) 5237 return 0; 5238 // UB + ST 5239 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 5240 if (!NextUB.isUsable()) 5241 return 0; 5242 // UB = UB + ST 5243 NextUB = 5244 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 5245 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get()); 5246 if (!NextUB.isUsable()) 5247 return 0; 5248 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5249 CombNextLB = 5250 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 5251 if (!NextLB.isUsable()) 5252 return 0; 5253 // LB = LB + ST 5254 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 5255 CombNextLB.get()); 5256 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get()); 5257 if (!CombNextLB.isUsable()) 5258 return 0; 5259 // UB + ST 5260 CombNextUB = 5261 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 5262 if (!CombNextUB.isUsable()) 5263 return 0; 5264 // UB = UB + ST 5265 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 5266 CombNextUB.get()); 5267 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get()); 5268 if (!CombNextUB.isUsable()) 5269 return 0; 5270 } 5271 } 5272 5273 // Create increment expression for distribute loop when combined in a same 5274 // directive with for as IV = IV + ST; ensure upper bound expression based 5275 // on PrevUB instead of NumIterations - used to implement 'for' when found 5276 // in combination with 'distribute', like in 'distribute parallel for' 5277 SourceLocation DistIncLoc = AStmt->getBeginLoc(); 5278 ExprResult DistCond, DistInc, PrevEUB; 5279 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5280 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get()); 5281 assert(DistCond.isUsable() && "distribute cond expr was not built"); 5282 5283 DistInc = 5284 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 5285 assert(DistInc.isUsable() && "distribute inc expr was not built"); 5286 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 5287 DistInc.get()); 5288 DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get()); 5289 assert(DistInc.isUsable() && "distribute inc expr was not built"); 5290 5291 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 5292 // construct 5293 SourceLocation DistEUBLoc = AStmt->getBeginLoc(); 5294 ExprResult IsUBGreater = 5295 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get()); 5296 ExprResult CondOp = SemaRef.ActOnConditionalOp( 5297 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get()); 5298 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 5299 CondOp.get()); 5300 PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get()); 5301 } 5302 5303 // Build updates and final values of the loop counters. 5304 bool HasErrors = false; 5305 Built.Counters.resize(NestedLoopCount); 5306 Built.Inits.resize(NestedLoopCount); 5307 Built.Updates.resize(NestedLoopCount); 5308 Built.Finals.resize(NestedLoopCount); 5309 { 5310 ExprResult Div; 5311 // Go from inner nested loop to outer. 5312 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) { 5313 LoopIterationSpace &IS = IterSpaces[Cnt]; 5314 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 5315 // Build: Iter = (IV / Div) % IS.NumIters 5316 // where Div is product of previous iterations' IS.NumIters. 5317 ExprResult Iter; 5318 if (Div.isUsable()) { 5319 Iter = 5320 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get()); 5321 } else { 5322 Iter = IV; 5323 assert((Cnt == (int)NestedLoopCount - 1) && 5324 "unusable div expected on first iteration only"); 5325 } 5326 5327 if (Cnt != 0 && Iter.isUsable()) 5328 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(), 5329 IS.NumIterations); 5330 if (!Iter.isUsable()) { 5331 HasErrors = true; 5332 break; 5333 } 5334 5335 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 5336 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 5337 DeclRefExpr *CounterVar = buildDeclRefExpr( 5338 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 5339 /*RefersToCapture=*/true); 5340 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 5341 IS.CounterInit, Captures); 5342 if (!Init.isUsable()) { 5343 HasErrors = true; 5344 break; 5345 } 5346 ExprResult Update = buildCounterUpdate( 5347 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 5348 IS.CounterStep, IS.Subtract, &Captures); 5349 if (!Update.isUsable()) { 5350 HasErrors = true; 5351 break; 5352 } 5353 5354 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 5355 ExprResult Final = buildCounterUpdate( 5356 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, 5357 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures); 5358 if (!Final.isUsable()) { 5359 HasErrors = true; 5360 break; 5361 } 5362 5363 // Build Div for the next iteration: Div <- Div * IS.NumIters 5364 if (Cnt != 0) { 5365 if (Div.isUnset()) 5366 Div = IS.NumIterations; 5367 else 5368 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(), 5369 IS.NumIterations); 5370 5371 // Add parentheses (for debugging purposes only). 5372 if (Div.isUsable()) 5373 Div = tryBuildCapture(SemaRef, Div.get(), Captures); 5374 if (!Div.isUsable()) { 5375 HasErrors = true; 5376 break; 5377 } 5378 } 5379 if (!Update.isUsable() || !Final.isUsable()) { 5380 HasErrors = true; 5381 break; 5382 } 5383 // Save results 5384 Built.Counters[Cnt] = IS.CounterVar; 5385 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 5386 Built.Inits[Cnt] = Init.get(); 5387 Built.Updates[Cnt] = Update.get(); 5388 Built.Finals[Cnt] = Final.get(); 5389 } 5390 } 5391 5392 if (HasErrors) 5393 return 0; 5394 5395 // Save results 5396 Built.IterationVarRef = IV.get(); 5397 Built.LastIteration = LastIteration.get(); 5398 Built.NumIterations = NumIterations.get(); 5399 Built.CalcLastIteration = 5400 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get(); 5401 Built.PreCond = PreCond.get(); 5402 Built.PreInits = buildPreInits(C, Captures); 5403 Built.Cond = Cond.get(); 5404 Built.Init = Init.get(); 5405 Built.Inc = Inc.get(); 5406 Built.LB = LB.get(); 5407 Built.UB = UB.get(); 5408 Built.IL = IL.get(); 5409 Built.ST = ST.get(); 5410 Built.EUB = EUB.get(); 5411 Built.NLB = NextLB.get(); 5412 Built.NUB = NextUB.get(); 5413 Built.PrevLB = PrevLB.get(); 5414 Built.PrevUB = PrevUB.get(); 5415 Built.DistInc = DistInc.get(); 5416 Built.PrevEUB = PrevEUB.get(); 5417 Built.DistCombinedFields.LB = CombLB.get(); 5418 Built.DistCombinedFields.UB = CombUB.get(); 5419 Built.DistCombinedFields.EUB = CombEUB.get(); 5420 Built.DistCombinedFields.Init = CombInit.get(); 5421 Built.DistCombinedFields.Cond = CombCond.get(); 5422 Built.DistCombinedFields.NLB = CombNextLB.get(); 5423 Built.DistCombinedFields.NUB = CombNextUB.get(); 5424 5425 return NestedLoopCount; 5426 } 5427 5428 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 5429 auto CollapseClauses = 5430 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 5431 if (CollapseClauses.begin() != CollapseClauses.end()) 5432 return (*CollapseClauses.begin())->getNumForLoops(); 5433 return nullptr; 5434 } 5435 5436 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 5437 auto OrderedClauses = 5438 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 5439 if (OrderedClauses.begin() != OrderedClauses.end()) 5440 return (*OrderedClauses.begin())->getNumForLoops(); 5441 return nullptr; 5442 } 5443 5444 static bool checkSimdlenSafelenSpecified(Sema &S, 5445 const ArrayRef<OMPClause *> Clauses) { 5446 const OMPSafelenClause *Safelen = nullptr; 5447 const OMPSimdlenClause *Simdlen = nullptr; 5448 5449 for (const OMPClause *Clause : Clauses) { 5450 if (Clause->getClauseKind() == OMPC_safelen) 5451 Safelen = cast<OMPSafelenClause>(Clause); 5452 else if (Clause->getClauseKind() == OMPC_simdlen) 5453 Simdlen = cast<OMPSimdlenClause>(Clause); 5454 if (Safelen && Simdlen) 5455 break; 5456 } 5457 5458 if (Simdlen && Safelen) { 5459 llvm::APSInt SimdlenRes, SafelenRes; 5460 const Expr *SimdlenLength = Simdlen->getSimdlen(); 5461 const Expr *SafelenLength = Safelen->getSafelen(); 5462 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 5463 SimdlenLength->isInstantiationDependent() || 5464 SimdlenLength->containsUnexpandedParameterPack()) 5465 return false; 5466 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 5467 SafelenLength->isInstantiationDependent() || 5468 SafelenLength->containsUnexpandedParameterPack()) 5469 return false; 5470 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context); 5471 SafelenLength->EvaluateAsInt(SafelenRes, S.Context); 5472 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 5473 // If both simdlen and safelen clauses are specified, the value of the 5474 // simdlen parameter must be less than or equal to the value of the safelen 5475 // parameter. 5476 if (SimdlenRes > SafelenRes) { 5477 S.Diag(SimdlenLength->getExprLoc(), 5478 diag::err_omp_wrong_simdlen_safelen_values) 5479 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 5480 return true; 5481 } 5482 } 5483 return false; 5484 } 5485 5486 StmtResult 5487 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 5488 SourceLocation StartLoc, SourceLocation EndLoc, 5489 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5490 if (!AStmt) 5491 return StmtError(); 5492 5493 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5494 OMPLoopDirective::HelperExprs B; 5495 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5496 // define the nested loops number. 5497 unsigned NestedLoopCount = checkOpenMPLoop( 5498 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 5499 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 5500 if (NestedLoopCount == 0) 5501 return StmtError(); 5502 5503 assert((CurContext->isDependentContext() || B.builtAll()) && 5504 "omp simd loop exprs were not built"); 5505 5506 if (!CurContext->isDependentContext()) { 5507 // Finalize the clauses that need pre-built expressions for CodeGen. 5508 for (OMPClause *C : Clauses) { 5509 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5510 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5511 B.NumIterations, *this, CurScope, 5512 DSAStack)) 5513 return StmtError(); 5514 } 5515 } 5516 5517 if (checkSimdlenSafelenSpecified(*this, Clauses)) 5518 return StmtError(); 5519 5520 setFunctionHasBranchProtectedScope(); 5521 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 5522 Clauses, AStmt, B); 5523 } 5524 5525 StmtResult 5526 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 5527 SourceLocation StartLoc, SourceLocation EndLoc, 5528 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5529 if (!AStmt) 5530 return StmtError(); 5531 5532 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5533 OMPLoopDirective::HelperExprs B; 5534 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5535 // define the nested loops number. 5536 unsigned NestedLoopCount = checkOpenMPLoop( 5537 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 5538 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 5539 if (NestedLoopCount == 0) 5540 return StmtError(); 5541 5542 assert((CurContext->isDependentContext() || B.builtAll()) && 5543 "omp for loop exprs were not built"); 5544 5545 if (!CurContext->isDependentContext()) { 5546 // Finalize the clauses that need pre-built expressions for CodeGen. 5547 for (OMPClause *C : Clauses) { 5548 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5549 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5550 B.NumIterations, *this, CurScope, 5551 DSAStack)) 5552 return StmtError(); 5553 } 5554 } 5555 5556 setFunctionHasBranchProtectedScope(); 5557 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 5558 Clauses, AStmt, B, DSAStack->isCancelRegion()); 5559 } 5560 5561 StmtResult Sema::ActOnOpenMPForSimdDirective( 5562 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5563 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5564 if (!AStmt) 5565 return StmtError(); 5566 5567 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5568 OMPLoopDirective::HelperExprs B; 5569 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5570 // define the nested loops number. 5571 unsigned NestedLoopCount = 5572 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 5573 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 5574 VarsWithImplicitDSA, B); 5575 if (NestedLoopCount == 0) 5576 return StmtError(); 5577 5578 assert((CurContext->isDependentContext() || B.builtAll()) && 5579 "omp for simd loop exprs were not built"); 5580 5581 if (!CurContext->isDependentContext()) { 5582 // Finalize the clauses that need pre-built expressions for CodeGen. 5583 for (OMPClause *C : Clauses) { 5584 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5585 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5586 B.NumIterations, *this, CurScope, 5587 DSAStack)) 5588 return StmtError(); 5589 } 5590 } 5591 5592 if (checkSimdlenSafelenSpecified(*this, Clauses)) 5593 return StmtError(); 5594 5595 setFunctionHasBranchProtectedScope(); 5596 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 5597 Clauses, AStmt, B); 5598 } 5599 5600 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 5601 Stmt *AStmt, 5602 SourceLocation StartLoc, 5603 SourceLocation EndLoc) { 5604 if (!AStmt) 5605 return StmtError(); 5606 5607 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5608 auto BaseStmt = AStmt; 5609 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 5610 BaseStmt = CS->getCapturedStmt(); 5611 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 5612 auto S = C->children(); 5613 if (S.begin() == S.end()) 5614 return StmtError(); 5615 // All associated statements must be '#pragma omp section' except for 5616 // the first one. 5617 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 5618 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 5619 if (SectionStmt) 5620 Diag(SectionStmt->getBeginLoc(), 5621 diag::err_omp_sections_substmt_not_section); 5622 return StmtError(); 5623 } 5624 cast<OMPSectionDirective>(SectionStmt) 5625 ->setHasCancel(DSAStack->isCancelRegion()); 5626 } 5627 } else { 5628 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt); 5629 return StmtError(); 5630 } 5631 5632 setFunctionHasBranchProtectedScope(); 5633 5634 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 5635 DSAStack->isCancelRegion()); 5636 } 5637 5638 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 5639 SourceLocation StartLoc, 5640 SourceLocation EndLoc) { 5641 if (!AStmt) 5642 return StmtError(); 5643 5644 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5645 5646 setFunctionHasBranchProtectedScope(); 5647 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 5648 5649 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 5650 DSAStack->isCancelRegion()); 5651 } 5652 5653 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 5654 Stmt *AStmt, 5655 SourceLocation StartLoc, 5656 SourceLocation EndLoc) { 5657 if (!AStmt) 5658 return StmtError(); 5659 5660 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5661 5662 setFunctionHasBranchProtectedScope(); 5663 5664 // OpenMP [2.7.3, single Construct, Restrictions] 5665 // The copyprivate clause must not be used with the nowait clause. 5666 const OMPClause *Nowait = nullptr; 5667 const OMPClause *Copyprivate = nullptr; 5668 for (const OMPClause *Clause : Clauses) { 5669 if (Clause->getClauseKind() == OMPC_nowait) 5670 Nowait = Clause; 5671 else if (Clause->getClauseKind() == OMPC_copyprivate) 5672 Copyprivate = Clause; 5673 if (Copyprivate && Nowait) { 5674 Diag(Copyprivate->getBeginLoc(), 5675 diag::err_omp_single_copyprivate_with_nowait); 5676 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here); 5677 return StmtError(); 5678 } 5679 } 5680 5681 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 5682 } 5683 5684 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 5685 SourceLocation StartLoc, 5686 SourceLocation EndLoc) { 5687 if (!AStmt) 5688 return StmtError(); 5689 5690 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5691 5692 setFunctionHasBranchProtectedScope(); 5693 5694 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 5695 } 5696 5697 StmtResult Sema::ActOnOpenMPCriticalDirective( 5698 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 5699 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 5700 if (!AStmt) 5701 return StmtError(); 5702 5703 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5704 5705 bool ErrorFound = false; 5706 llvm::APSInt Hint; 5707 SourceLocation HintLoc; 5708 bool DependentHint = false; 5709 for (const OMPClause *C : Clauses) { 5710 if (C->getClauseKind() == OMPC_hint) { 5711 if (!DirName.getName()) { 5712 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name); 5713 ErrorFound = true; 5714 } 5715 Expr *E = cast<OMPHintClause>(C)->getHint(); 5716 if (E->isTypeDependent() || E->isValueDependent() || 5717 E->isInstantiationDependent()) { 5718 DependentHint = true; 5719 } else { 5720 Hint = E->EvaluateKnownConstInt(Context); 5721 HintLoc = C->getBeginLoc(); 5722 } 5723 } 5724 } 5725 if (ErrorFound) 5726 return StmtError(); 5727 const auto Pair = DSAStack->getCriticalWithHint(DirName); 5728 if (Pair.first && DirName.getName() && !DependentHint) { 5729 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 5730 Diag(StartLoc, diag::err_omp_critical_with_hint); 5731 if (HintLoc.isValid()) 5732 Diag(HintLoc, diag::note_omp_critical_hint_here) 5733 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); 5734 else 5735 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 5736 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 5737 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here) 5738 << 1 5739 << C->getHint()->EvaluateKnownConstInt(Context).toString( 5740 /*Radix=*/10, /*Signed=*/false); 5741 } else { 5742 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1; 5743 } 5744 } 5745 } 5746 5747 setFunctionHasBranchProtectedScope(); 5748 5749 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 5750 Clauses, AStmt); 5751 if (!Pair.first && DirName.getName() && !DependentHint) 5752 DSAStack->addCriticalWithHint(Dir, Hint); 5753 return Dir; 5754 } 5755 5756 StmtResult Sema::ActOnOpenMPParallelForDirective( 5757 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5758 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5759 if (!AStmt) 5760 return StmtError(); 5761 5762 auto *CS = cast<CapturedStmt>(AStmt); 5763 // 1.2.2 OpenMP Language Terminology 5764 // Structured block - An executable statement with a single entry at the 5765 // top and a single exit at the bottom. 5766 // The point of exit cannot be a branch out of the structured block. 5767 // longjmp() and throw() must not violate the entry/exit criteria. 5768 CS->getCapturedDecl()->setNothrow(); 5769 5770 OMPLoopDirective::HelperExprs B; 5771 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5772 // define the nested loops number. 5773 unsigned NestedLoopCount = 5774 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 5775 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 5776 VarsWithImplicitDSA, B); 5777 if (NestedLoopCount == 0) 5778 return StmtError(); 5779 5780 assert((CurContext->isDependentContext() || B.builtAll()) && 5781 "omp parallel for loop exprs were not built"); 5782 5783 if (!CurContext->isDependentContext()) { 5784 // Finalize the clauses that need pre-built expressions for CodeGen. 5785 for (OMPClause *C : Clauses) { 5786 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5787 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5788 B.NumIterations, *this, CurScope, 5789 DSAStack)) 5790 return StmtError(); 5791 } 5792 } 5793 5794 setFunctionHasBranchProtectedScope(); 5795 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc, 5796 NestedLoopCount, Clauses, AStmt, B, 5797 DSAStack->isCancelRegion()); 5798 } 5799 5800 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 5801 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5802 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5803 if (!AStmt) 5804 return StmtError(); 5805 5806 auto *CS = cast<CapturedStmt>(AStmt); 5807 // 1.2.2 OpenMP Language Terminology 5808 // Structured block - An executable statement with a single entry at the 5809 // top and a single exit at the bottom. 5810 // The point of exit cannot be a branch out of the structured block. 5811 // longjmp() and throw() must not violate the entry/exit criteria. 5812 CS->getCapturedDecl()->setNothrow(); 5813 5814 OMPLoopDirective::HelperExprs B; 5815 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5816 // define the nested loops number. 5817 unsigned NestedLoopCount = 5818 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 5819 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 5820 VarsWithImplicitDSA, B); 5821 if (NestedLoopCount == 0) 5822 return StmtError(); 5823 5824 if (!CurContext->isDependentContext()) { 5825 // Finalize the clauses that need pre-built expressions for CodeGen. 5826 for (OMPClause *C : Clauses) { 5827 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5828 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5829 B.NumIterations, *this, CurScope, 5830 DSAStack)) 5831 return StmtError(); 5832 } 5833 } 5834 5835 if (checkSimdlenSafelenSpecified(*this, Clauses)) 5836 return StmtError(); 5837 5838 setFunctionHasBranchProtectedScope(); 5839 return OMPParallelForSimdDirective::Create( 5840 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 5841 } 5842 5843 StmtResult 5844 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 5845 Stmt *AStmt, SourceLocation StartLoc, 5846 SourceLocation EndLoc) { 5847 if (!AStmt) 5848 return StmtError(); 5849 5850 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5851 auto BaseStmt = AStmt; 5852 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 5853 BaseStmt = CS->getCapturedStmt(); 5854 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 5855 auto S = C->children(); 5856 if (S.begin() == S.end()) 5857 return StmtError(); 5858 // All associated statements must be '#pragma omp section' except for 5859 // the first one. 5860 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 5861 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 5862 if (SectionStmt) 5863 Diag(SectionStmt->getBeginLoc(), 5864 diag::err_omp_parallel_sections_substmt_not_section); 5865 return StmtError(); 5866 } 5867 cast<OMPSectionDirective>(SectionStmt) 5868 ->setHasCancel(DSAStack->isCancelRegion()); 5869 } 5870 } else { 5871 Diag(AStmt->getBeginLoc(), 5872 diag::err_omp_parallel_sections_not_compound_stmt); 5873 return StmtError(); 5874 } 5875 5876 setFunctionHasBranchProtectedScope(); 5877 5878 return OMPParallelSectionsDirective::Create( 5879 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion()); 5880 } 5881 5882 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 5883 Stmt *AStmt, SourceLocation StartLoc, 5884 SourceLocation EndLoc) { 5885 if (!AStmt) 5886 return StmtError(); 5887 5888 auto *CS = cast<CapturedStmt>(AStmt); 5889 // 1.2.2 OpenMP Language Terminology 5890 // Structured block - An executable statement with a single entry at the 5891 // top and a single exit at the bottom. 5892 // The point of exit cannot be a branch out of the structured block. 5893 // longjmp() and throw() must not violate the entry/exit criteria. 5894 CS->getCapturedDecl()->setNothrow(); 5895 5896 setFunctionHasBranchProtectedScope(); 5897 5898 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 5899 DSAStack->isCancelRegion()); 5900 } 5901 5902 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 5903 SourceLocation EndLoc) { 5904 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 5905 } 5906 5907 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 5908 SourceLocation EndLoc) { 5909 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 5910 } 5911 5912 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 5913 SourceLocation EndLoc) { 5914 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 5915 } 5916 5917 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 5918 Stmt *AStmt, 5919 SourceLocation StartLoc, 5920 SourceLocation EndLoc) { 5921 if (!AStmt) 5922 return StmtError(); 5923 5924 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5925 5926 setFunctionHasBranchProtectedScope(); 5927 5928 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 5929 AStmt, 5930 DSAStack->getTaskgroupReductionRef()); 5931 } 5932 5933 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 5934 SourceLocation StartLoc, 5935 SourceLocation EndLoc) { 5936 assert(Clauses.size() <= 1 && "Extra clauses in flush directive"); 5937 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 5938 } 5939 5940 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 5941 Stmt *AStmt, 5942 SourceLocation StartLoc, 5943 SourceLocation EndLoc) { 5944 const OMPClause *DependFound = nullptr; 5945 const OMPClause *DependSourceClause = nullptr; 5946 const OMPClause *DependSinkClause = nullptr; 5947 bool ErrorFound = false; 5948 const OMPThreadsClause *TC = nullptr; 5949 const OMPSIMDClause *SC = nullptr; 5950 for (const OMPClause *C : Clauses) { 5951 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 5952 DependFound = C; 5953 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 5954 if (DependSourceClause) { 5955 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 5956 << getOpenMPDirectiveName(OMPD_ordered) 5957 << getOpenMPClauseName(OMPC_depend) << 2; 5958 ErrorFound = true; 5959 } else { 5960 DependSourceClause = C; 5961 } 5962 if (DependSinkClause) { 5963 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 5964 << 0; 5965 ErrorFound = true; 5966 } 5967 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 5968 if (DependSourceClause) { 5969 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 5970 << 1; 5971 ErrorFound = true; 5972 } 5973 DependSinkClause = C; 5974 } 5975 } else if (C->getClauseKind() == OMPC_threads) { 5976 TC = cast<OMPThreadsClause>(C); 5977 } else if (C->getClauseKind() == OMPC_simd) { 5978 SC = cast<OMPSIMDClause>(C); 5979 } 5980 } 5981 if (!ErrorFound && !SC && 5982 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 5983 // OpenMP [2.8.1,simd Construct, Restrictions] 5984 // An ordered construct with the simd clause is the only OpenMP construct 5985 // that can appear in the simd region. 5986 Diag(StartLoc, diag::err_omp_prohibited_region_simd); 5987 ErrorFound = true; 5988 } else if (DependFound && (TC || SC)) { 5989 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd) 5990 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 5991 ErrorFound = true; 5992 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) { 5993 Diag(DependFound->getBeginLoc(), 5994 diag::err_omp_ordered_directive_without_param); 5995 ErrorFound = true; 5996 } else if (TC || Clauses.empty()) { 5997 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) { 5998 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc; 5999 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 6000 << (TC != nullptr); 6001 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param); 6002 ErrorFound = true; 6003 } 6004 } 6005 if ((!AStmt && !DependFound) || ErrorFound) 6006 return StmtError(); 6007 6008 if (AStmt) { 6009 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6010 6011 setFunctionHasBranchProtectedScope(); 6012 } 6013 6014 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 6015 } 6016 6017 namespace { 6018 /// Helper class for checking expression in 'omp atomic [update]' 6019 /// construct. 6020 class OpenMPAtomicUpdateChecker { 6021 /// Error results for atomic update expressions. 6022 enum ExprAnalysisErrorCode { 6023 /// A statement is not an expression statement. 6024 NotAnExpression, 6025 /// Expression is not builtin binary or unary operation. 6026 NotABinaryOrUnaryExpression, 6027 /// Unary operation is not post-/pre- increment/decrement operation. 6028 NotAnUnaryIncDecExpression, 6029 /// An expression is not of scalar type. 6030 NotAScalarType, 6031 /// A binary operation is not an assignment operation. 6032 NotAnAssignmentOp, 6033 /// RHS part of the binary operation is not a binary expression. 6034 NotABinaryExpression, 6035 /// RHS part is not additive/multiplicative/shift/biwise binary 6036 /// expression. 6037 NotABinaryOperator, 6038 /// RHS binary operation does not have reference to the updated LHS 6039 /// part. 6040 NotAnUpdateExpression, 6041 /// No errors is found. 6042 NoError 6043 }; 6044 /// Reference to Sema. 6045 Sema &SemaRef; 6046 /// A location for note diagnostics (when error is found). 6047 SourceLocation NoteLoc; 6048 /// 'x' lvalue part of the source atomic expression. 6049 Expr *X; 6050 /// 'expr' rvalue part of the source atomic expression. 6051 Expr *E; 6052 /// Helper expression of the form 6053 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 6054 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 6055 Expr *UpdateExpr; 6056 /// Is 'x' a LHS in a RHS part of full update expression. It is 6057 /// important for non-associative operations. 6058 bool IsXLHSInRHSPart; 6059 BinaryOperatorKind Op; 6060 SourceLocation OpLoc; 6061 /// true if the source expression is a postfix unary operation, false 6062 /// if it is a prefix unary operation. 6063 bool IsPostfixUpdate; 6064 6065 public: 6066 OpenMPAtomicUpdateChecker(Sema &SemaRef) 6067 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 6068 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 6069 /// Check specified statement that it is suitable for 'atomic update' 6070 /// constructs and extract 'x', 'expr' and Operation from the original 6071 /// expression. If DiagId and NoteId == 0, then only check is performed 6072 /// without error notification. 6073 /// \param DiagId Diagnostic which should be emitted if error is found. 6074 /// \param NoteId Diagnostic note for the main error message. 6075 /// \return true if statement is not an update expression, false otherwise. 6076 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 6077 /// Return the 'x' lvalue part of the source atomic expression. 6078 Expr *getX() const { return X; } 6079 /// Return the 'expr' rvalue part of the source atomic expression. 6080 Expr *getExpr() const { return E; } 6081 /// Return the update expression used in calculation of the updated 6082 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 6083 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 6084 Expr *getUpdateExpr() const { return UpdateExpr; } 6085 /// Return true if 'x' is LHS in RHS part of full update expression, 6086 /// false otherwise. 6087 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 6088 6089 /// true if the source expression is a postfix unary operation, false 6090 /// if it is a prefix unary operation. 6091 bool isPostfixUpdate() const { return IsPostfixUpdate; } 6092 6093 private: 6094 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 6095 unsigned NoteId = 0); 6096 }; 6097 } // namespace 6098 6099 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 6100 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 6101 ExprAnalysisErrorCode ErrorFound = NoError; 6102 SourceLocation ErrorLoc, NoteLoc; 6103 SourceRange ErrorRange, NoteRange; 6104 // Allowed constructs are: 6105 // x = x binop expr; 6106 // x = expr binop x; 6107 if (AtomicBinOp->getOpcode() == BO_Assign) { 6108 X = AtomicBinOp->getLHS(); 6109 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 6110 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 6111 if (AtomicInnerBinOp->isMultiplicativeOp() || 6112 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 6113 AtomicInnerBinOp->isBitwiseOp()) { 6114 Op = AtomicInnerBinOp->getOpcode(); 6115 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 6116 Expr *LHS = AtomicInnerBinOp->getLHS(); 6117 Expr *RHS = AtomicInnerBinOp->getRHS(); 6118 llvm::FoldingSetNodeID XId, LHSId, RHSId; 6119 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 6120 /*Canonical=*/true); 6121 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 6122 /*Canonical=*/true); 6123 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 6124 /*Canonical=*/true); 6125 if (XId == LHSId) { 6126 E = RHS; 6127 IsXLHSInRHSPart = true; 6128 } else if (XId == RHSId) { 6129 E = LHS; 6130 IsXLHSInRHSPart = false; 6131 } else { 6132 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 6133 ErrorRange = AtomicInnerBinOp->getSourceRange(); 6134 NoteLoc = X->getExprLoc(); 6135 NoteRange = X->getSourceRange(); 6136 ErrorFound = NotAnUpdateExpression; 6137 } 6138 } else { 6139 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 6140 ErrorRange = AtomicInnerBinOp->getSourceRange(); 6141 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 6142 NoteRange = SourceRange(NoteLoc, NoteLoc); 6143 ErrorFound = NotABinaryOperator; 6144 } 6145 } else { 6146 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 6147 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 6148 ErrorFound = NotABinaryExpression; 6149 } 6150 } else { 6151 ErrorLoc = AtomicBinOp->getExprLoc(); 6152 ErrorRange = AtomicBinOp->getSourceRange(); 6153 NoteLoc = AtomicBinOp->getOperatorLoc(); 6154 NoteRange = SourceRange(NoteLoc, NoteLoc); 6155 ErrorFound = NotAnAssignmentOp; 6156 } 6157 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 6158 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 6159 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 6160 return true; 6161 } 6162 if (SemaRef.CurContext->isDependentContext()) 6163 E = X = UpdateExpr = nullptr; 6164 return ErrorFound != NoError; 6165 } 6166 6167 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 6168 unsigned NoteId) { 6169 ExprAnalysisErrorCode ErrorFound = NoError; 6170 SourceLocation ErrorLoc, NoteLoc; 6171 SourceRange ErrorRange, NoteRange; 6172 // Allowed constructs are: 6173 // x++; 6174 // x--; 6175 // ++x; 6176 // --x; 6177 // x binop= expr; 6178 // x = x binop expr; 6179 // x = expr binop x; 6180 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 6181 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 6182 if (AtomicBody->getType()->isScalarType() || 6183 AtomicBody->isInstantiationDependent()) { 6184 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 6185 AtomicBody->IgnoreParenImpCasts())) { 6186 // Check for Compound Assignment Operation 6187 Op = BinaryOperator::getOpForCompoundAssignment( 6188 AtomicCompAssignOp->getOpcode()); 6189 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 6190 E = AtomicCompAssignOp->getRHS(); 6191 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 6192 IsXLHSInRHSPart = true; 6193 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 6194 AtomicBody->IgnoreParenImpCasts())) { 6195 // Check for Binary Operation 6196 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 6197 return true; 6198 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 6199 AtomicBody->IgnoreParenImpCasts())) { 6200 // Check for Unary Operation 6201 if (AtomicUnaryOp->isIncrementDecrementOp()) { 6202 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 6203 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 6204 OpLoc = AtomicUnaryOp->getOperatorLoc(); 6205 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 6206 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 6207 IsXLHSInRHSPart = true; 6208 } else { 6209 ErrorFound = NotAnUnaryIncDecExpression; 6210 ErrorLoc = AtomicUnaryOp->getExprLoc(); 6211 ErrorRange = AtomicUnaryOp->getSourceRange(); 6212 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 6213 NoteRange = SourceRange(NoteLoc, NoteLoc); 6214 } 6215 } else if (!AtomicBody->isInstantiationDependent()) { 6216 ErrorFound = NotABinaryOrUnaryExpression; 6217 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 6218 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 6219 } 6220 } else { 6221 ErrorFound = NotAScalarType; 6222 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc(); 6223 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6224 } 6225 } else { 6226 ErrorFound = NotAnExpression; 6227 NoteLoc = ErrorLoc = S->getBeginLoc(); 6228 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6229 } 6230 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 6231 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 6232 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 6233 return true; 6234 } 6235 if (SemaRef.CurContext->isDependentContext()) 6236 E = X = UpdateExpr = nullptr; 6237 if (ErrorFound == NoError && E && X) { 6238 // Build an update expression of form 'OpaqueValueExpr(x) binop 6239 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 6240 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 6241 auto *OVEX = new (SemaRef.getASTContext()) 6242 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); 6243 auto *OVEExpr = new (SemaRef.getASTContext()) 6244 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); 6245 ExprResult Update = 6246 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 6247 IsXLHSInRHSPart ? OVEExpr : OVEX); 6248 if (Update.isInvalid()) 6249 return true; 6250 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 6251 Sema::AA_Casting); 6252 if (Update.isInvalid()) 6253 return true; 6254 UpdateExpr = Update.get(); 6255 } 6256 return ErrorFound != NoError; 6257 } 6258 6259 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 6260 Stmt *AStmt, 6261 SourceLocation StartLoc, 6262 SourceLocation EndLoc) { 6263 if (!AStmt) 6264 return StmtError(); 6265 6266 auto *CS = cast<CapturedStmt>(AStmt); 6267 // 1.2.2 OpenMP Language Terminology 6268 // Structured block - An executable statement with a single entry at the 6269 // top and a single exit at the bottom. 6270 // The point of exit cannot be a branch out of the structured block. 6271 // longjmp() and throw() must not violate the entry/exit criteria. 6272 OpenMPClauseKind AtomicKind = OMPC_unknown; 6273 SourceLocation AtomicKindLoc; 6274 for (const OMPClause *C : Clauses) { 6275 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 6276 C->getClauseKind() == OMPC_update || 6277 C->getClauseKind() == OMPC_capture) { 6278 if (AtomicKind != OMPC_unknown) { 6279 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses) 6280 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 6281 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause) 6282 << getOpenMPClauseName(AtomicKind); 6283 } else { 6284 AtomicKind = C->getClauseKind(); 6285 AtomicKindLoc = C->getBeginLoc(); 6286 } 6287 } 6288 } 6289 6290 Stmt *Body = CS->getCapturedStmt(); 6291 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 6292 Body = EWC->getSubExpr(); 6293 6294 Expr *X = nullptr; 6295 Expr *V = nullptr; 6296 Expr *E = nullptr; 6297 Expr *UE = nullptr; 6298 bool IsXLHSInRHSPart = false; 6299 bool IsPostfixUpdate = false; 6300 // OpenMP [2.12.6, atomic Construct] 6301 // In the next expressions: 6302 // * x and v (as applicable) are both l-value expressions with scalar type. 6303 // * During the execution of an atomic region, multiple syntactic 6304 // occurrences of x must designate the same storage location. 6305 // * Neither of v and expr (as applicable) may access the storage location 6306 // designated by x. 6307 // * Neither of x and expr (as applicable) may access the storage location 6308 // designated by v. 6309 // * expr is an expression with scalar type. 6310 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 6311 // * binop, binop=, ++, and -- are not overloaded operators. 6312 // * The expression x binop expr must be numerically equivalent to x binop 6313 // (expr). This requirement is satisfied if the operators in expr have 6314 // precedence greater than binop, or by using parentheses around expr or 6315 // subexpressions of expr. 6316 // * The expression expr binop x must be numerically equivalent to (expr) 6317 // binop x. This requirement is satisfied if the operators in expr have 6318 // precedence equal to or greater than binop, or by using parentheses around 6319 // expr or subexpressions of expr. 6320 // * For forms that allow multiple occurrences of x, the number of times 6321 // that x is evaluated is unspecified. 6322 if (AtomicKind == OMPC_read) { 6323 enum { 6324 NotAnExpression, 6325 NotAnAssignmentOp, 6326 NotAScalarType, 6327 NotAnLValue, 6328 NoError 6329 } ErrorFound = NoError; 6330 SourceLocation ErrorLoc, NoteLoc; 6331 SourceRange ErrorRange, NoteRange; 6332 // If clause is read: 6333 // v = x; 6334 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 6335 const auto *AtomicBinOp = 6336 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 6337 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 6338 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 6339 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 6340 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 6341 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 6342 if (!X->isLValue() || !V->isLValue()) { 6343 const Expr *NotLValueExpr = X->isLValue() ? V : X; 6344 ErrorFound = NotAnLValue; 6345 ErrorLoc = AtomicBinOp->getExprLoc(); 6346 ErrorRange = AtomicBinOp->getSourceRange(); 6347 NoteLoc = NotLValueExpr->getExprLoc(); 6348 NoteRange = NotLValueExpr->getSourceRange(); 6349 } 6350 } else if (!X->isInstantiationDependent() || 6351 !V->isInstantiationDependent()) { 6352 const Expr *NotScalarExpr = 6353 (X->isInstantiationDependent() || X->getType()->isScalarType()) 6354 ? V 6355 : X; 6356 ErrorFound = NotAScalarType; 6357 ErrorLoc = AtomicBinOp->getExprLoc(); 6358 ErrorRange = AtomicBinOp->getSourceRange(); 6359 NoteLoc = NotScalarExpr->getExprLoc(); 6360 NoteRange = NotScalarExpr->getSourceRange(); 6361 } 6362 } else if (!AtomicBody->isInstantiationDependent()) { 6363 ErrorFound = NotAnAssignmentOp; 6364 ErrorLoc = AtomicBody->getExprLoc(); 6365 ErrorRange = AtomicBody->getSourceRange(); 6366 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 6367 : AtomicBody->getExprLoc(); 6368 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 6369 : AtomicBody->getSourceRange(); 6370 } 6371 } else { 6372 ErrorFound = NotAnExpression; 6373 NoteLoc = ErrorLoc = Body->getBeginLoc(); 6374 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6375 } 6376 if (ErrorFound != NoError) { 6377 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 6378 << ErrorRange; 6379 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 6380 << NoteRange; 6381 return StmtError(); 6382 } 6383 if (CurContext->isDependentContext()) 6384 V = X = nullptr; 6385 } else if (AtomicKind == OMPC_write) { 6386 enum { 6387 NotAnExpression, 6388 NotAnAssignmentOp, 6389 NotAScalarType, 6390 NotAnLValue, 6391 NoError 6392 } ErrorFound = NoError; 6393 SourceLocation ErrorLoc, NoteLoc; 6394 SourceRange ErrorRange, NoteRange; 6395 // If clause is write: 6396 // x = expr; 6397 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 6398 const auto *AtomicBinOp = 6399 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 6400 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 6401 X = AtomicBinOp->getLHS(); 6402 E = AtomicBinOp->getRHS(); 6403 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 6404 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 6405 if (!X->isLValue()) { 6406 ErrorFound = NotAnLValue; 6407 ErrorLoc = AtomicBinOp->getExprLoc(); 6408 ErrorRange = AtomicBinOp->getSourceRange(); 6409 NoteLoc = X->getExprLoc(); 6410 NoteRange = X->getSourceRange(); 6411 } 6412 } else if (!X->isInstantiationDependent() || 6413 !E->isInstantiationDependent()) { 6414 const Expr *NotScalarExpr = 6415 (X->isInstantiationDependent() || X->getType()->isScalarType()) 6416 ? E 6417 : X; 6418 ErrorFound = NotAScalarType; 6419 ErrorLoc = AtomicBinOp->getExprLoc(); 6420 ErrorRange = AtomicBinOp->getSourceRange(); 6421 NoteLoc = NotScalarExpr->getExprLoc(); 6422 NoteRange = NotScalarExpr->getSourceRange(); 6423 } 6424 } else if (!AtomicBody->isInstantiationDependent()) { 6425 ErrorFound = NotAnAssignmentOp; 6426 ErrorLoc = AtomicBody->getExprLoc(); 6427 ErrorRange = AtomicBody->getSourceRange(); 6428 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 6429 : AtomicBody->getExprLoc(); 6430 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 6431 : AtomicBody->getSourceRange(); 6432 } 6433 } else { 6434 ErrorFound = NotAnExpression; 6435 NoteLoc = ErrorLoc = Body->getBeginLoc(); 6436 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6437 } 6438 if (ErrorFound != NoError) { 6439 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 6440 << ErrorRange; 6441 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 6442 << NoteRange; 6443 return StmtError(); 6444 } 6445 if (CurContext->isDependentContext()) 6446 E = X = nullptr; 6447 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 6448 // If clause is update: 6449 // x++; 6450 // x--; 6451 // ++x; 6452 // --x; 6453 // x binop= expr; 6454 // x = x binop expr; 6455 // x = expr binop x; 6456 OpenMPAtomicUpdateChecker Checker(*this); 6457 if (Checker.checkStatement( 6458 Body, (AtomicKind == OMPC_update) 6459 ? diag::err_omp_atomic_update_not_expression_statement 6460 : diag::err_omp_atomic_not_expression_statement, 6461 diag::note_omp_atomic_update)) 6462 return StmtError(); 6463 if (!CurContext->isDependentContext()) { 6464 E = Checker.getExpr(); 6465 X = Checker.getX(); 6466 UE = Checker.getUpdateExpr(); 6467 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6468 } 6469 } else if (AtomicKind == OMPC_capture) { 6470 enum { 6471 NotAnAssignmentOp, 6472 NotACompoundStatement, 6473 NotTwoSubstatements, 6474 NotASpecificExpression, 6475 NoError 6476 } ErrorFound = NoError; 6477 SourceLocation ErrorLoc, NoteLoc; 6478 SourceRange ErrorRange, NoteRange; 6479 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 6480 // If clause is a capture: 6481 // v = x++; 6482 // v = x--; 6483 // v = ++x; 6484 // v = --x; 6485 // v = x binop= expr; 6486 // v = x = x binop expr; 6487 // v = x = expr binop x; 6488 const auto *AtomicBinOp = 6489 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 6490 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 6491 V = AtomicBinOp->getLHS(); 6492 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 6493 OpenMPAtomicUpdateChecker Checker(*this); 6494 if (Checker.checkStatement( 6495 Body, diag::err_omp_atomic_capture_not_expression_statement, 6496 diag::note_omp_atomic_update)) 6497 return StmtError(); 6498 E = Checker.getExpr(); 6499 X = Checker.getX(); 6500 UE = Checker.getUpdateExpr(); 6501 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6502 IsPostfixUpdate = Checker.isPostfixUpdate(); 6503 } else if (!AtomicBody->isInstantiationDependent()) { 6504 ErrorLoc = AtomicBody->getExprLoc(); 6505 ErrorRange = AtomicBody->getSourceRange(); 6506 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 6507 : AtomicBody->getExprLoc(); 6508 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 6509 : AtomicBody->getSourceRange(); 6510 ErrorFound = NotAnAssignmentOp; 6511 } 6512 if (ErrorFound != NoError) { 6513 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 6514 << ErrorRange; 6515 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 6516 return StmtError(); 6517 } 6518 if (CurContext->isDependentContext()) 6519 UE = V = E = X = nullptr; 6520 } else { 6521 // If clause is a capture: 6522 // { v = x; x = expr; } 6523 // { v = x; x++; } 6524 // { v = x; x--; } 6525 // { v = x; ++x; } 6526 // { v = x; --x; } 6527 // { v = x; x binop= expr; } 6528 // { v = x; x = x binop expr; } 6529 // { v = x; x = expr binop x; } 6530 // { x++; v = x; } 6531 // { x--; v = x; } 6532 // { ++x; v = x; } 6533 // { --x; v = x; } 6534 // { x binop= expr; v = x; } 6535 // { x = x binop expr; v = x; } 6536 // { x = expr binop x; v = x; } 6537 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 6538 // Check that this is { expr1; expr2; } 6539 if (CS->size() == 2) { 6540 Stmt *First = CS->body_front(); 6541 Stmt *Second = CS->body_back(); 6542 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 6543 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 6544 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 6545 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 6546 // Need to find what subexpression is 'v' and what is 'x'. 6547 OpenMPAtomicUpdateChecker Checker(*this); 6548 bool IsUpdateExprFound = !Checker.checkStatement(Second); 6549 BinaryOperator *BinOp = nullptr; 6550 if (IsUpdateExprFound) { 6551 BinOp = dyn_cast<BinaryOperator>(First); 6552 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 6553 } 6554 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 6555 // { v = x; x++; } 6556 // { v = x; x--; } 6557 // { v = x; ++x; } 6558 // { v = x; --x; } 6559 // { v = x; x binop= expr; } 6560 // { v = x; x = x binop expr; } 6561 // { v = x; x = expr binop x; } 6562 // Check that the first expression has form v = x. 6563 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 6564 llvm::FoldingSetNodeID XId, PossibleXId; 6565 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 6566 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 6567 IsUpdateExprFound = XId == PossibleXId; 6568 if (IsUpdateExprFound) { 6569 V = BinOp->getLHS(); 6570 X = Checker.getX(); 6571 E = Checker.getExpr(); 6572 UE = Checker.getUpdateExpr(); 6573 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6574 IsPostfixUpdate = true; 6575 } 6576 } 6577 if (!IsUpdateExprFound) { 6578 IsUpdateExprFound = !Checker.checkStatement(First); 6579 BinOp = nullptr; 6580 if (IsUpdateExprFound) { 6581 BinOp = dyn_cast<BinaryOperator>(Second); 6582 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 6583 } 6584 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 6585 // { x++; v = x; } 6586 // { x--; v = x; } 6587 // { ++x; v = x; } 6588 // { --x; v = x; } 6589 // { x binop= expr; v = x; } 6590 // { x = x binop expr; v = x; } 6591 // { x = expr binop x; v = x; } 6592 // Check that the second expression has form v = x. 6593 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 6594 llvm::FoldingSetNodeID XId, PossibleXId; 6595 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 6596 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 6597 IsUpdateExprFound = XId == PossibleXId; 6598 if (IsUpdateExprFound) { 6599 V = BinOp->getLHS(); 6600 X = Checker.getX(); 6601 E = Checker.getExpr(); 6602 UE = Checker.getUpdateExpr(); 6603 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6604 IsPostfixUpdate = false; 6605 } 6606 } 6607 } 6608 if (!IsUpdateExprFound) { 6609 // { v = x; x = expr; } 6610 auto *FirstExpr = dyn_cast<Expr>(First); 6611 auto *SecondExpr = dyn_cast<Expr>(Second); 6612 if (!FirstExpr || !SecondExpr || 6613 !(FirstExpr->isInstantiationDependent() || 6614 SecondExpr->isInstantiationDependent())) { 6615 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 6616 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 6617 ErrorFound = NotAnAssignmentOp; 6618 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 6619 : First->getBeginLoc(); 6620 NoteRange = ErrorRange = FirstBinOp 6621 ? FirstBinOp->getSourceRange() 6622 : SourceRange(ErrorLoc, ErrorLoc); 6623 } else { 6624 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 6625 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 6626 ErrorFound = NotAnAssignmentOp; 6627 NoteLoc = ErrorLoc = SecondBinOp 6628 ? SecondBinOp->getOperatorLoc() 6629 : Second->getBeginLoc(); 6630 NoteRange = ErrorRange = 6631 SecondBinOp ? SecondBinOp->getSourceRange() 6632 : SourceRange(ErrorLoc, ErrorLoc); 6633 } else { 6634 Expr *PossibleXRHSInFirst = 6635 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 6636 Expr *PossibleXLHSInSecond = 6637 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 6638 llvm::FoldingSetNodeID X1Id, X2Id; 6639 PossibleXRHSInFirst->Profile(X1Id, Context, 6640 /*Canonical=*/true); 6641 PossibleXLHSInSecond->Profile(X2Id, Context, 6642 /*Canonical=*/true); 6643 IsUpdateExprFound = X1Id == X2Id; 6644 if (IsUpdateExprFound) { 6645 V = FirstBinOp->getLHS(); 6646 X = SecondBinOp->getLHS(); 6647 E = SecondBinOp->getRHS(); 6648 UE = nullptr; 6649 IsXLHSInRHSPart = false; 6650 IsPostfixUpdate = true; 6651 } else { 6652 ErrorFound = NotASpecificExpression; 6653 ErrorLoc = FirstBinOp->getExprLoc(); 6654 ErrorRange = FirstBinOp->getSourceRange(); 6655 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 6656 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 6657 } 6658 } 6659 } 6660 } 6661 } 6662 } else { 6663 NoteLoc = ErrorLoc = Body->getBeginLoc(); 6664 NoteRange = ErrorRange = 6665 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 6666 ErrorFound = NotTwoSubstatements; 6667 } 6668 } else { 6669 NoteLoc = ErrorLoc = Body->getBeginLoc(); 6670 NoteRange = ErrorRange = 6671 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 6672 ErrorFound = NotACompoundStatement; 6673 } 6674 if (ErrorFound != NoError) { 6675 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 6676 << ErrorRange; 6677 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 6678 return StmtError(); 6679 } 6680 if (CurContext->isDependentContext()) 6681 UE = V = E = X = nullptr; 6682 } 6683 } 6684 6685 setFunctionHasBranchProtectedScope(); 6686 6687 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 6688 X, V, E, UE, IsXLHSInRHSPart, 6689 IsPostfixUpdate); 6690 } 6691 6692 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 6693 Stmt *AStmt, 6694 SourceLocation StartLoc, 6695 SourceLocation EndLoc) { 6696 if (!AStmt) 6697 return StmtError(); 6698 6699 auto *CS = cast<CapturedStmt>(AStmt); 6700 // 1.2.2 OpenMP Language Terminology 6701 // Structured block - An executable statement with a single entry at the 6702 // top and a single exit at the bottom. 6703 // The point of exit cannot be a branch out of the structured block. 6704 // longjmp() and throw() must not violate the entry/exit criteria. 6705 CS->getCapturedDecl()->setNothrow(); 6706 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 6707 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6708 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6709 // 1.2.2 OpenMP Language Terminology 6710 // Structured block - An executable statement with a single entry at the 6711 // top and a single exit at the bottom. 6712 // The point of exit cannot be a branch out of the structured block. 6713 // longjmp() and throw() must not violate the entry/exit criteria. 6714 CS->getCapturedDecl()->setNothrow(); 6715 } 6716 6717 // OpenMP [2.16, Nesting of Regions] 6718 // If specified, a teams construct must be contained within a target 6719 // construct. That target construct must contain no statements or directives 6720 // outside of the teams construct. 6721 if (DSAStack->hasInnerTeamsRegion()) { 6722 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 6723 bool OMPTeamsFound = true; 6724 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 6725 auto I = CS->body_begin(); 6726 while (I != CS->body_end()) { 6727 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 6728 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) { 6729 OMPTeamsFound = false; 6730 break; 6731 } 6732 ++I; 6733 } 6734 assert(I != CS->body_end() && "Not found statement"); 6735 S = *I; 6736 } else { 6737 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 6738 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 6739 } 6740 if (!OMPTeamsFound) { 6741 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 6742 Diag(DSAStack->getInnerTeamsRegionLoc(), 6743 diag::note_omp_nested_teams_construct_here); 6744 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here) 6745 << isa<OMPExecutableDirective>(S); 6746 return StmtError(); 6747 } 6748 } 6749 6750 setFunctionHasBranchProtectedScope(); 6751 6752 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 6753 } 6754 6755 StmtResult 6756 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 6757 Stmt *AStmt, SourceLocation StartLoc, 6758 SourceLocation EndLoc) { 6759 if (!AStmt) 6760 return StmtError(); 6761 6762 auto *CS = cast<CapturedStmt>(AStmt); 6763 // 1.2.2 OpenMP Language Terminology 6764 // Structured block - An executable statement with a single entry at the 6765 // top and a single exit at the bottom. 6766 // The point of exit cannot be a branch out of the structured block. 6767 // longjmp() and throw() must not violate the entry/exit criteria. 6768 CS->getCapturedDecl()->setNothrow(); 6769 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 6770 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6771 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6772 // 1.2.2 OpenMP Language Terminology 6773 // Structured block - An executable statement with a single entry at the 6774 // top and a single exit at the bottom. 6775 // The point of exit cannot be a branch out of the structured block. 6776 // longjmp() and throw() must not violate the entry/exit criteria. 6777 CS->getCapturedDecl()->setNothrow(); 6778 } 6779 6780 setFunctionHasBranchProtectedScope(); 6781 6782 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, 6783 AStmt); 6784 } 6785 6786 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 6787 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6788 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6789 if (!AStmt) 6790 return StmtError(); 6791 6792 auto *CS = cast<CapturedStmt>(AStmt); 6793 // 1.2.2 OpenMP Language Terminology 6794 // Structured block - An executable statement with a single entry at the 6795 // top and a single exit at the bottom. 6796 // The point of exit cannot be a branch out of the structured block. 6797 // longjmp() and throw() must not violate the entry/exit criteria. 6798 CS->getCapturedDecl()->setNothrow(); 6799 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 6800 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6801 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6802 // 1.2.2 OpenMP Language Terminology 6803 // Structured block - An executable statement with a single entry at the 6804 // top and a single exit at the bottom. 6805 // The point of exit cannot be a branch out of the structured block. 6806 // longjmp() and throw() must not violate the entry/exit criteria. 6807 CS->getCapturedDecl()->setNothrow(); 6808 } 6809 6810 OMPLoopDirective::HelperExprs B; 6811 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6812 // define the nested loops number. 6813 unsigned NestedLoopCount = 6814 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 6815 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 6816 VarsWithImplicitDSA, B); 6817 if (NestedLoopCount == 0) 6818 return StmtError(); 6819 6820 assert((CurContext->isDependentContext() || B.builtAll()) && 6821 "omp target parallel for loop exprs were not built"); 6822 6823 if (!CurContext->isDependentContext()) { 6824 // Finalize the clauses that need pre-built expressions for CodeGen. 6825 for (OMPClause *C : Clauses) { 6826 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6827 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6828 B.NumIterations, *this, CurScope, 6829 DSAStack)) 6830 return StmtError(); 6831 } 6832 } 6833 6834 setFunctionHasBranchProtectedScope(); 6835 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc, 6836 NestedLoopCount, Clauses, AStmt, 6837 B, DSAStack->isCancelRegion()); 6838 } 6839 6840 /// Check for existence of a map clause in the list of clauses. 6841 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 6842 const OpenMPClauseKind K) { 6843 return llvm::any_of( 6844 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 6845 } 6846 6847 template <typename... Params> 6848 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 6849 const Params... ClauseTypes) { 6850 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 6851 } 6852 6853 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 6854 Stmt *AStmt, 6855 SourceLocation StartLoc, 6856 SourceLocation EndLoc) { 6857 if (!AStmt) 6858 return StmtError(); 6859 6860 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6861 6862 // OpenMP [2.10.1, Restrictions, p. 97] 6863 // At least one map clause must appear on the directive. 6864 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) { 6865 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 6866 << "'map' or 'use_device_ptr'" 6867 << getOpenMPDirectiveName(OMPD_target_data); 6868 return StmtError(); 6869 } 6870 6871 setFunctionHasBranchProtectedScope(); 6872 6873 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 6874 AStmt); 6875 } 6876 6877 StmtResult 6878 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 6879 SourceLocation StartLoc, 6880 SourceLocation EndLoc, Stmt *AStmt) { 6881 if (!AStmt) 6882 return StmtError(); 6883 6884 auto *CS = cast<CapturedStmt>(AStmt); 6885 // 1.2.2 OpenMP Language Terminology 6886 // Structured block - An executable statement with a single entry at the 6887 // top and a single exit at the bottom. 6888 // The point of exit cannot be a branch out of the structured block. 6889 // longjmp() and throw() must not violate the entry/exit criteria. 6890 CS->getCapturedDecl()->setNothrow(); 6891 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 6892 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6893 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6894 // 1.2.2 OpenMP Language Terminology 6895 // Structured block - An executable statement with a single entry at the 6896 // top and a single exit at the bottom. 6897 // The point of exit cannot be a branch out of the structured block. 6898 // longjmp() and throw() must not violate the entry/exit criteria. 6899 CS->getCapturedDecl()->setNothrow(); 6900 } 6901 6902 // OpenMP [2.10.2, Restrictions, p. 99] 6903 // At least one map clause must appear on the directive. 6904 if (!hasClauses(Clauses, OMPC_map)) { 6905 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 6906 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 6907 return StmtError(); 6908 } 6909 6910 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 6911 AStmt); 6912 } 6913 6914 StmtResult 6915 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 6916 SourceLocation StartLoc, 6917 SourceLocation EndLoc, Stmt *AStmt) { 6918 if (!AStmt) 6919 return StmtError(); 6920 6921 auto *CS = cast<CapturedStmt>(AStmt); 6922 // 1.2.2 OpenMP Language Terminology 6923 // Structured block - An executable statement with a single entry at the 6924 // top and a single exit at the bottom. 6925 // The point of exit cannot be a branch out of the structured block. 6926 // longjmp() and throw() must not violate the entry/exit criteria. 6927 CS->getCapturedDecl()->setNothrow(); 6928 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 6929 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6930 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6931 // 1.2.2 OpenMP Language Terminology 6932 // Structured block - An executable statement with a single entry at the 6933 // top and a single exit at the bottom. 6934 // The point of exit cannot be a branch out of the structured block. 6935 // longjmp() and throw() must not violate the entry/exit criteria. 6936 CS->getCapturedDecl()->setNothrow(); 6937 } 6938 6939 // OpenMP [2.10.3, Restrictions, p. 102] 6940 // At least one map clause must appear on the directive. 6941 if (!hasClauses(Clauses, OMPC_map)) { 6942 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 6943 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 6944 return StmtError(); 6945 } 6946 6947 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 6948 AStmt); 6949 } 6950 6951 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 6952 SourceLocation StartLoc, 6953 SourceLocation EndLoc, 6954 Stmt *AStmt) { 6955 if (!AStmt) 6956 return StmtError(); 6957 6958 auto *CS = cast<CapturedStmt>(AStmt); 6959 // 1.2.2 OpenMP Language Terminology 6960 // Structured block - An executable statement with a single entry at the 6961 // top and a single exit at the bottom. 6962 // The point of exit cannot be a branch out of the structured block. 6963 // longjmp() and throw() must not violate the entry/exit criteria. 6964 CS->getCapturedDecl()->setNothrow(); 6965 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 6966 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6967 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6968 // 1.2.2 OpenMP Language Terminology 6969 // Structured block - An executable statement with a single entry at the 6970 // top and a single exit at the bottom. 6971 // The point of exit cannot be a branch out of the structured block. 6972 // longjmp() and throw() must not violate the entry/exit criteria. 6973 CS->getCapturedDecl()->setNothrow(); 6974 } 6975 6976 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 6977 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 6978 return StmtError(); 6979 } 6980 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 6981 AStmt); 6982 } 6983 6984 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 6985 Stmt *AStmt, SourceLocation StartLoc, 6986 SourceLocation EndLoc) { 6987 if (!AStmt) 6988 return StmtError(); 6989 6990 auto *CS = cast<CapturedStmt>(AStmt); 6991 // 1.2.2 OpenMP Language Terminology 6992 // Structured block - An executable statement with a single entry at the 6993 // top and a single exit at the bottom. 6994 // The point of exit cannot be a branch out of the structured block. 6995 // longjmp() and throw() must not violate the entry/exit criteria. 6996 CS->getCapturedDecl()->setNothrow(); 6997 6998 setFunctionHasBranchProtectedScope(); 6999 7000 DSAStack->setParentTeamsRegionLoc(StartLoc); 7001 7002 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 7003 } 7004 7005 StmtResult 7006 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 7007 SourceLocation EndLoc, 7008 OpenMPDirectiveKind CancelRegion) { 7009 if (DSAStack->isParentNowaitRegion()) { 7010 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 7011 return StmtError(); 7012 } 7013 if (DSAStack->isParentOrderedRegion()) { 7014 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 7015 return StmtError(); 7016 } 7017 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 7018 CancelRegion); 7019 } 7020 7021 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 7022 SourceLocation StartLoc, 7023 SourceLocation EndLoc, 7024 OpenMPDirectiveKind CancelRegion) { 7025 if (DSAStack->isParentNowaitRegion()) { 7026 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 7027 return StmtError(); 7028 } 7029 if (DSAStack->isParentOrderedRegion()) { 7030 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 7031 return StmtError(); 7032 } 7033 DSAStack->setParentCancelRegion(/*Cancel=*/true); 7034 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 7035 CancelRegion); 7036 } 7037 7038 static bool checkGrainsizeNumTasksClauses(Sema &S, 7039 ArrayRef<OMPClause *> Clauses) { 7040 const OMPClause *PrevClause = nullptr; 7041 bool ErrorFound = false; 7042 for (const OMPClause *C : Clauses) { 7043 if (C->getClauseKind() == OMPC_grainsize || 7044 C->getClauseKind() == OMPC_num_tasks) { 7045 if (!PrevClause) 7046 PrevClause = C; 7047 else if (PrevClause->getClauseKind() != C->getClauseKind()) { 7048 S.Diag(C->getBeginLoc(), 7049 diag::err_omp_grainsize_num_tasks_mutually_exclusive) 7050 << getOpenMPClauseName(C->getClauseKind()) 7051 << getOpenMPClauseName(PrevClause->getClauseKind()); 7052 S.Diag(PrevClause->getBeginLoc(), 7053 diag::note_omp_previous_grainsize_num_tasks) 7054 << getOpenMPClauseName(PrevClause->getClauseKind()); 7055 ErrorFound = true; 7056 } 7057 } 7058 } 7059 return ErrorFound; 7060 } 7061 7062 static bool checkReductionClauseWithNogroup(Sema &S, 7063 ArrayRef<OMPClause *> Clauses) { 7064 const OMPClause *ReductionClause = nullptr; 7065 const OMPClause *NogroupClause = nullptr; 7066 for (const OMPClause *C : Clauses) { 7067 if (C->getClauseKind() == OMPC_reduction) { 7068 ReductionClause = C; 7069 if (NogroupClause) 7070 break; 7071 continue; 7072 } 7073 if (C->getClauseKind() == OMPC_nogroup) { 7074 NogroupClause = C; 7075 if (ReductionClause) 7076 break; 7077 continue; 7078 } 7079 } 7080 if (ReductionClause && NogroupClause) { 7081 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup) 7082 << SourceRange(NogroupClause->getBeginLoc(), 7083 NogroupClause->getEndLoc()); 7084 return true; 7085 } 7086 return false; 7087 } 7088 7089 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 7090 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7091 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7092 if (!AStmt) 7093 return StmtError(); 7094 7095 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 7096 OMPLoopDirective::HelperExprs B; 7097 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 7098 // define the nested loops number. 7099 unsigned NestedLoopCount = 7100 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 7101 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 7102 VarsWithImplicitDSA, B); 7103 if (NestedLoopCount == 0) 7104 return StmtError(); 7105 7106 assert((CurContext->isDependentContext() || B.builtAll()) && 7107 "omp for loop exprs were not built"); 7108 7109 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 7110 // The grainsize clause and num_tasks clause are mutually exclusive and may 7111 // not appear on the same taskloop directive. 7112 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 7113 return StmtError(); 7114 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 7115 // If a reduction clause is present on the taskloop directive, the nogroup 7116 // clause must not be specified. 7117 if (checkReductionClauseWithNogroup(*this, Clauses)) 7118 return StmtError(); 7119 7120 setFunctionHasBranchProtectedScope(); 7121 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 7122 NestedLoopCount, Clauses, AStmt, B); 7123 } 7124 7125 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 7126 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7127 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7128 if (!AStmt) 7129 return StmtError(); 7130 7131 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 7132 OMPLoopDirective::HelperExprs B; 7133 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 7134 // define the nested loops number. 7135 unsigned NestedLoopCount = 7136 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 7137 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 7138 VarsWithImplicitDSA, B); 7139 if (NestedLoopCount == 0) 7140 return StmtError(); 7141 7142 assert((CurContext->isDependentContext() || B.builtAll()) && 7143 "omp for loop exprs were not built"); 7144 7145 if (!CurContext->isDependentContext()) { 7146 // Finalize the clauses that need pre-built expressions for CodeGen. 7147 for (OMPClause *C : Clauses) { 7148 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7149 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7150 B.NumIterations, *this, CurScope, 7151 DSAStack)) 7152 return StmtError(); 7153 } 7154 } 7155 7156 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 7157 // The grainsize clause and num_tasks clause are mutually exclusive and may 7158 // not appear on the same taskloop directive. 7159 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 7160 return StmtError(); 7161 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 7162 // If a reduction clause is present on the taskloop directive, the nogroup 7163 // clause must not be specified. 7164 if (checkReductionClauseWithNogroup(*this, Clauses)) 7165 return StmtError(); 7166 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7167 return StmtError(); 7168 7169 setFunctionHasBranchProtectedScope(); 7170 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 7171 NestedLoopCount, Clauses, AStmt, B); 7172 } 7173 7174 StmtResult Sema::ActOnOpenMPDistributeDirective( 7175 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7176 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7177 if (!AStmt) 7178 return StmtError(); 7179 7180 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 7181 OMPLoopDirective::HelperExprs B; 7182 // In presence of clause 'collapse' with number of loops, it will 7183 // define the nested loops number. 7184 unsigned NestedLoopCount = 7185 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 7186 nullptr /*ordered not a clause on distribute*/, AStmt, 7187 *this, *DSAStack, VarsWithImplicitDSA, B); 7188 if (NestedLoopCount == 0) 7189 return StmtError(); 7190 7191 assert((CurContext->isDependentContext() || B.builtAll()) && 7192 "omp for loop exprs were not built"); 7193 7194 setFunctionHasBranchProtectedScope(); 7195 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 7196 NestedLoopCount, Clauses, AStmt, B); 7197 } 7198 7199 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 7200 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7201 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7202 if (!AStmt) 7203 return StmtError(); 7204 7205 auto *CS = cast<CapturedStmt>(AStmt); 7206 // 1.2.2 OpenMP Language Terminology 7207 // Structured block - An executable statement with a single entry at the 7208 // top and a single exit at the bottom. 7209 // The point of exit cannot be a branch out of the structured block. 7210 // longjmp() and throw() must not violate the entry/exit criteria. 7211 CS->getCapturedDecl()->setNothrow(); 7212 for (int ThisCaptureLevel = 7213 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 7214 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7215 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7216 // 1.2.2 OpenMP Language Terminology 7217 // Structured block - An executable statement with a single entry at the 7218 // top and a single exit at the bottom. 7219 // The point of exit cannot be a branch out of the structured block. 7220 // longjmp() and throw() must not violate the entry/exit criteria. 7221 CS->getCapturedDecl()->setNothrow(); 7222 } 7223 7224 OMPLoopDirective::HelperExprs B; 7225 // In presence of clause 'collapse' with number of loops, it will 7226 // define the nested loops number. 7227 unsigned NestedLoopCount = checkOpenMPLoop( 7228 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 7229 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7230 VarsWithImplicitDSA, B); 7231 if (NestedLoopCount == 0) 7232 return StmtError(); 7233 7234 assert((CurContext->isDependentContext() || B.builtAll()) && 7235 "omp for loop exprs were not built"); 7236 7237 setFunctionHasBranchProtectedScope(); 7238 return OMPDistributeParallelForDirective::Create( 7239 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 7240 DSAStack->isCancelRegion()); 7241 } 7242 7243 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 7244 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7245 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7246 if (!AStmt) 7247 return StmtError(); 7248 7249 auto *CS = cast<CapturedStmt>(AStmt); 7250 // 1.2.2 OpenMP Language Terminology 7251 // Structured block - An executable statement with a single entry at the 7252 // top and a single exit at the bottom. 7253 // The point of exit cannot be a branch out of the structured block. 7254 // longjmp() and throw() must not violate the entry/exit criteria. 7255 CS->getCapturedDecl()->setNothrow(); 7256 for (int ThisCaptureLevel = 7257 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 7258 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7259 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7260 // 1.2.2 OpenMP Language Terminology 7261 // Structured block - An executable statement with a single entry at the 7262 // top and a single exit at the bottom. 7263 // The point of exit cannot be a branch out of the structured block. 7264 // longjmp() and throw() must not violate the entry/exit criteria. 7265 CS->getCapturedDecl()->setNothrow(); 7266 } 7267 7268 OMPLoopDirective::HelperExprs B; 7269 // In presence of clause 'collapse' with number of loops, it will 7270 // define the nested loops number. 7271 unsigned NestedLoopCount = checkOpenMPLoop( 7272 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 7273 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7274 VarsWithImplicitDSA, B); 7275 if (NestedLoopCount == 0) 7276 return StmtError(); 7277 7278 assert((CurContext->isDependentContext() || B.builtAll()) && 7279 "omp for loop exprs were not built"); 7280 7281 if (!CurContext->isDependentContext()) { 7282 // Finalize the clauses that need pre-built expressions for CodeGen. 7283 for (OMPClause *C : Clauses) { 7284 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7285 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7286 B.NumIterations, *this, CurScope, 7287 DSAStack)) 7288 return StmtError(); 7289 } 7290 } 7291 7292 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7293 return StmtError(); 7294 7295 setFunctionHasBranchProtectedScope(); 7296 return OMPDistributeParallelForSimdDirective::Create( 7297 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7298 } 7299 7300 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 7301 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7302 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7303 if (!AStmt) 7304 return StmtError(); 7305 7306 auto *CS = cast<CapturedStmt>(AStmt); 7307 // 1.2.2 OpenMP Language Terminology 7308 // Structured block - An executable statement with a single entry at the 7309 // top and a single exit at the bottom. 7310 // The point of exit cannot be a branch out of the structured block. 7311 // longjmp() and throw() must not violate the entry/exit criteria. 7312 CS->getCapturedDecl()->setNothrow(); 7313 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 7314 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7315 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7316 // 1.2.2 OpenMP Language Terminology 7317 // Structured block - An executable statement with a single entry at the 7318 // top and a single exit at the bottom. 7319 // The point of exit cannot be a branch out of the structured block. 7320 // longjmp() and throw() must not violate the entry/exit criteria. 7321 CS->getCapturedDecl()->setNothrow(); 7322 } 7323 7324 OMPLoopDirective::HelperExprs B; 7325 // In presence of clause 'collapse' with number of loops, it will 7326 // define the nested loops number. 7327 unsigned NestedLoopCount = 7328 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 7329 nullptr /*ordered not a clause on distribute*/, CS, *this, 7330 *DSAStack, VarsWithImplicitDSA, B); 7331 if (NestedLoopCount == 0) 7332 return StmtError(); 7333 7334 assert((CurContext->isDependentContext() || B.builtAll()) && 7335 "omp for loop exprs were not built"); 7336 7337 if (!CurContext->isDependentContext()) { 7338 // Finalize the clauses that need pre-built expressions for CodeGen. 7339 for (OMPClause *C : Clauses) { 7340 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7341 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7342 B.NumIterations, *this, CurScope, 7343 DSAStack)) 7344 return StmtError(); 7345 } 7346 } 7347 7348 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7349 return StmtError(); 7350 7351 setFunctionHasBranchProtectedScope(); 7352 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 7353 NestedLoopCount, Clauses, AStmt, B); 7354 } 7355 7356 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 7357 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7358 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7359 if (!AStmt) 7360 return StmtError(); 7361 7362 auto *CS = cast<CapturedStmt>(AStmt); 7363 // 1.2.2 OpenMP Language Terminology 7364 // Structured block - An executable statement with a single entry at the 7365 // top and a single exit at the bottom. 7366 // The point of exit cannot be a branch out of the structured block. 7367 // longjmp() and throw() must not violate the entry/exit criteria. 7368 CS->getCapturedDecl()->setNothrow(); 7369 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 7370 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7371 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7372 // 1.2.2 OpenMP Language Terminology 7373 // Structured block - An executable statement with a single entry at the 7374 // top and a single exit at the bottom. 7375 // The point of exit cannot be a branch out of the structured block. 7376 // longjmp() and throw() must not violate the entry/exit criteria. 7377 CS->getCapturedDecl()->setNothrow(); 7378 } 7379 7380 OMPLoopDirective::HelperExprs B; 7381 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 7382 // define the nested loops number. 7383 unsigned NestedLoopCount = checkOpenMPLoop( 7384 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 7385 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 7386 VarsWithImplicitDSA, B); 7387 if (NestedLoopCount == 0) 7388 return StmtError(); 7389 7390 assert((CurContext->isDependentContext() || B.builtAll()) && 7391 "omp target parallel for simd loop exprs were not built"); 7392 7393 if (!CurContext->isDependentContext()) { 7394 // Finalize the clauses that need pre-built expressions for CodeGen. 7395 for (OMPClause *C : Clauses) { 7396 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7397 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7398 B.NumIterations, *this, CurScope, 7399 DSAStack)) 7400 return StmtError(); 7401 } 7402 } 7403 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7404 return StmtError(); 7405 7406 setFunctionHasBranchProtectedScope(); 7407 return OMPTargetParallelForSimdDirective::Create( 7408 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7409 } 7410 7411 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 7412 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7413 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7414 if (!AStmt) 7415 return StmtError(); 7416 7417 auto *CS = cast<CapturedStmt>(AStmt); 7418 // 1.2.2 OpenMP Language Terminology 7419 // Structured block - An executable statement with a single entry at the 7420 // top and a single exit at the bottom. 7421 // The point of exit cannot be a branch out of the structured block. 7422 // longjmp() and throw() must not violate the entry/exit criteria. 7423 CS->getCapturedDecl()->setNothrow(); 7424 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 7425 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7426 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7427 // 1.2.2 OpenMP Language Terminology 7428 // Structured block - An executable statement with a single entry at the 7429 // top and a single exit at the bottom. 7430 // The point of exit cannot be a branch out of the structured block. 7431 // longjmp() and throw() must not violate the entry/exit criteria. 7432 CS->getCapturedDecl()->setNothrow(); 7433 } 7434 7435 OMPLoopDirective::HelperExprs B; 7436 // In presence of clause 'collapse' with number of loops, it will define the 7437 // nested loops number. 7438 unsigned NestedLoopCount = 7439 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 7440 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 7441 VarsWithImplicitDSA, B); 7442 if (NestedLoopCount == 0) 7443 return StmtError(); 7444 7445 assert((CurContext->isDependentContext() || B.builtAll()) && 7446 "omp target simd loop exprs were not built"); 7447 7448 if (!CurContext->isDependentContext()) { 7449 // Finalize the clauses that need pre-built expressions for CodeGen. 7450 for (OMPClause *C : Clauses) { 7451 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7452 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7453 B.NumIterations, *this, CurScope, 7454 DSAStack)) 7455 return StmtError(); 7456 } 7457 } 7458 7459 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7460 return StmtError(); 7461 7462 setFunctionHasBranchProtectedScope(); 7463 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 7464 NestedLoopCount, Clauses, AStmt, B); 7465 } 7466 7467 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 7468 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7469 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7470 if (!AStmt) 7471 return StmtError(); 7472 7473 auto *CS = cast<CapturedStmt>(AStmt); 7474 // 1.2.2 OpenMP Language Terminology 7475 // Structured block - An executable statement with a single entry at the 7476 // top and a single exit at the bottom. 7477 // The point of exit cannot be a branch out of the structured block. 7478 // longjmp() and throw() must not violate the entry/exit criteria. 7479 CS->getCapturedDecl()->setNothrow(); 7480 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 7481 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7482 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7483 // 1.2.2 OpenMP Language Terminology 7484 // Structured block - An executable statement with a single entry at the 7485 // top and a single exit at the bottom. 7486 // The point of exit cannot be a branch out of the structured block. 7487 // longjmp() and throw() must not violate the entry/exit criteria. 7488 CS->getCapturedDecl()->setNothrow(); 7489 } 7490 7491 OMPLoopDirective::HelperExprs B; 7492 // In presence of clause 'collapse' with number of loops, it will 7493 // define the nested loops number. 7494 unsigned NestedLoopCount = 7495 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 7496 nullptr /*ordered not a clause on distribute*/, CS, *this, 7497 *DSAStack, VarsWithImplicitDSA, B); 7498 if (NestedLoopCount == 0) 7499 return StmtError(); 7500 7501 assert((CurContext->isDependentContext() || B.builtAll()) && 7502 "omp teams distribute loop exprs were not built"); 7503 7504 setFunctionHasBranchProtectedScope(); 7505 7506 DSAStack->setParentTeamsRegionLoc(StartLoc); 7507 7508 return OMPTeamsDistributeDirective::Create( 7509 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7510 } 7511 7512 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 7513 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7514 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7515 if (!AStmt) 7516 return StmtError(); 7517 7518 auto *CS = cast<CapturedStmt>(AStmt); 7519 // 1.2.2 OpenMP Language Terminology 7520 // Structured block - An executable statement with a single entry at the 7521 // top and a single exit at the bottom. 7522 // The point of exit cannot be a branch out of the structured block. 7523 // longjmp() and throw() must not violate the entry/exit criteria. 7524 CS->getCapturedDecl()->setNothrow(); 7525 for (int ThisCaptureLevel = 7526 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 7527 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7528 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7529 // 1.2.2 OpenMP Language Terminology 7530 // Structured block - An executable statement with a single entry at the 7531 // top and a single exit at the bottom. 7532 // The point of exit cannot be a branch out of the structured block. 7533 // longjmp() and throw() must not violate the entry/exit criteria. 7534 CS->getCapturedDecl()->setNothrow(); 7535 } 7536 7537 7538 OMPLoopDirective::HelperExprs B; 7539 // In presence of clause 'collapse' with number of loops, it will 7540 // define the nested loops number. 7541 unsigned NestedLoopCount = checkOpenMPLoop( 7542 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 7543 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7544 VarsWithImplicitDSA, B); 7545 7546 if (NestedLoopCount == 0) 7547 return StmtError(); 7548 7549 assert((CurContext->isDependentContext() || B.builtAll()) && 7550 "omp teams distribute simd loop exprs were not built"); 7551 7552 if (!CurContext->isDependentContext()) { 7553 // Finalize the clauses that need pre-built expressions for CodeGen. 7554 for (OMPClause *C : Clauses) { 7555 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7556 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7557 B.NumIterations, *this, CurScope, 7558 DSAStack)) 7559 return StmtError(); 7560 } 7561 } 7562 7563 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7564 return StmtError(); 7565 7566 setFunctionHasBranchProtectedScope(); 7567 7568 DSAStack->setParentTeamsRegionLoc(StartLoc); 7569 7570 return OMPTeamsDistributeSimdDirective::Create( 7571 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7572 } 7573 7574 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 7575 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7576 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7577 if (!AStmt) 7578 return StmtError(); 7579 7580 auto *CS = cast<CapturedStmt>(AStmt); 7581 // 1.2.2 OpenMP Language Terminology 7582 // Structured block - An executable statement with a single entry at the 7583 // top and a single exit at the bottom. 7584 // The point of exit cannot be a branch out of the structured block. 7585 // longjmp() and throw() must not violate the entry/exit criteria. 7586 CS->getCapturedDecl()->setNothrow(); 7587 7588 for (int ThisCaptureLevel = 7589 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 7590 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7591 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7592 // 1.2.2 OpenMP Language Terminology 7593 // Structured block - An executable statement with a single entry at the 7594 // top and a single exit at the bottom. 7595 // The point of exit cannot be a branch out of the structured block. 7596 // longjmp() and throw() must not violate the entry/exit criteria. 7597 CS->getCapturedDecl()->setNothrow(); 7598 } 7599 7600 OMPLoopDirective::HelperExprs B; 7601 // In presence of clause 'collapse' with number of loops, it will 7602 // define the nested loops number. 7603 unsigned NestedLoopCount = checkOpenMPLoop( 7604 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 7605 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7606 VarsWithImplicitDSA, B); 7607 7608 if (NestedLoopCount == 0) 7609 return StmtError(); 7610 7611 assert((CurContext->isDependentContext() || B.builtAll()) && 7612 "omp for loop exprs were not built"); 7613 7614 if (!CurContext->isDependentContext()) { 7615 // Finalize the clauses that need pre-built expressions for CodeGen. 7616 for (OMPClause *C : Clauses) { 7617 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7618 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7619 B.NumIterations, *this, CurScope, 7620 DSAStack)) 7621 return StmtError(); 7622 } 7623 } 7624 7625 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7626 return StmtError(); 7627 7628 setFunctionHasBranchProtectedScope(); 7629 7630 DSAStack->setParentTeamsRegionLoc(StartLoc); 7631 7632 return OMPTeamsDistributeParallelForSimdDirective::Create( 7633 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7634 } 7635 7636 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 7637 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7638 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7639 if (!AStmt) 7640 return StmtError(); 7641 7642 auto *CS = cast<CapturedStmt>(AStmt); 7643 // 1.2.2 OpenMP Language Terminology 7644 // Structured block - An executable statement with a single entry at the 7645 // top and a single exit at the bottom. 7646 // The point of exit cannot be a branch out of the structured block. 7647 // longjmp() and throw() must not violate the entry/exit criteria. 7648 CS->getCapturedDecl()->setNothrow(); 7649 7650 for (int ThisCaptureLevel = 7651 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 7652 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7653 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7654 // 1.2.2 OpenMP Language Terminology 7655 // Structured block - An executable statement with a single entry at the 7656 // top and a single exit at the bottom. 7657 // The point of exit cannot be a branch out of the structured block. 7658 // longjmp() and throw() must not violate the entry/exit criteria. 7659 CS->getCapturedDecl()->setNothrow(); 7660 } 7661 7662 OMPLoopDirective::HelperExprs B; 7663 // In presence of clause 'collapse' with number of loops, it will 7664 // define the nested loops number. 7665 unsigned NestedLoopCount = checkOpenMPLoop( 7666 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 7667 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7668 VarsWithImplicitDSA, B); 7669 7670 if (NestedLoopCount == 0) 7671 return StmtError(); 7672 7673 assert((CurContext->isDependentContext() || B.builtAll()) && 7674 "omp for loop exprs were not built"); 7675 7676 setFunctionHasBranchProtectedScope(); 7677 7678 DSAStack->setParentTeamsRegionLoc(StartLoc); 7679 7680 return OMPTeamsDistributeParallelForDirective::Create( 7681 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 7682 DSAStack->isCancelRegion()); 7683 } 7684 7685 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 7686 Stmt *AStmt, 7687 SourceLocation StartLoc, 7688 SourceLocation EndLoc) { 7689 if (!AStmt) 7690 return StmtError(); 7691 7692 auto *CS = cast<CapturedStmt>(AStmt); 7693 // 1.2.2 OpenMP Language Terminology 7694 // Structured block - An executable statement with a single entry at the 7695 // top and a single exit at the bottom. 7696 // The point of exit cannot be a branch out of the structured block. 7697 // longjmp() and throw() must not violate the entry/exit criteria. 7698 CS->getCapturedDecl()->setNothrow(); 7699 7700 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 7701 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7702 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7703 // 1.2.2 OpenMP Language Terminology 7704 // Structured block - An executable statement with a single entry at the 7705 // top and a single exit at the bottom. 7706 // The point of exit cannot be a branch out of the structured block. 7707 // longjmp() and throw() must not violate the entry/exit criteria. 7708 CS->getCapturedDecl()->setNothrow(); 7709 } 7710 setFunctionHasBranchProtectedScope(); 7711 7712 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 7713 AStmt); 7714 } 7715 7716 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 7717 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7718 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7719 if (!AStmt) 7720 return StmtError(); 7721 7722 auto *CS = cast<CapturedStmt>(AStmt); 7723 // 1.2.2 OpenMP Language Terminology 7724 // Structured block - An executable statement with a single entry at the 7725 // top and a single exit at the bottom. 7726 // The point of exit cannot be a branch out of the structured block. 7727 // longjmp() and throw() must not violate the entry/exit criteria. 7728 CS->getCapturedDecl()->setNothrow(); 7729 for (int ThisCaptureLevel = 7730 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 7731 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7732 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7733 // 1.2.2 OpenMP Language Terminology 7734 // Structured block - An executable statement with a single entry at the 7735 // top and a single exit at the bottom. 7736 // The point of exit cannot be a branch out of the structured block. 7737 // longjmp() and throw() must not violate the entry/exit criteria. 7738 CS->getCapturedDecl()->setNothrow(); 7739 } 7740 7741 OMPLoopDirective::HelperExprs B; 7742 // In presence of clause 'collapse' with number of loops, it will 7743 // define the nested loops number. 7744 unsigned NestedLoopCount = checkOpenMPLoop( 7745 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 7746 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7747 VarsWithImplicitDSA, B); 7748 if (NestedLoopCount == 0) 7749 return StmtError(); 7750 7751 assert((CurContext->isDependentContext() || B.builtAll()) && 7752 "omp target teams distribute loop exprs were not built"); 7753 7754 setFunctionHasBranchProtectedScope(); 7755 return OMPTargetTeamsDistributeDirective::Create( 7756 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7757 } 7758 7759 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 7760 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7761 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7762 if (!AStmt) 7763 return StmtError(); 7764 7765 auto *CS = cast<CapturedStmt>(AStmt); 7766 // 1.2.2 OpenMP Language Terminology 7767 // Structured block - An executable statement with a single entry at the 7768 // top and a single exit at the bottom. 7769 // The point of exit cannot be a branch out of the structured block. 7770 // longjmp() and throw() must not violate the entry/exit criteria. 7771 CS->getCapturedDecl()->setNothrow(); 7772 for (int ThisCaptureLevel = 7773 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 7774 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7775 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7776 // 1.2.2 OpenMP Language Terminology 7777 // Structured block - An executable statement with a single entry at the 7778 // top and a single exit at the bottom. 7779 // The point of exit cannot be a branch out of the structured block. 7780 // longjmp() and throw() must not violate the entry/exit criteria. 7781 CS->getCapturedDecl()->setNothrow(); 7782 } 7783 7784 OMPLoopDirective::HelperExprs B; 7785 // In presence of clause 'collapse' with number of loops, it will 7786 // define the nested loops number. 7787 unsigned NestedLoopCount = checkOpenMPLoop( 7788 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 7789 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7790 VarsWithImplicitDSA, B); 7791 if (NestedLoopCount == 0) 7792 return StmtError(); 7793 7794 assert((CurContext->isDependentContext() || B.builtAll()) && 7795 "omp target teams distribute parallel for loop exprs were not built"); 7796 7797 if (!CurContext->isDependentContext()) { 7798 // Finalize the clauses that need pre-built expressions for CodeGen. 7799 for (OMPClause *C : Clauses) { 7800 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7801 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7802 B.NumIterations, *this, CurScope, 7803 DSAStack)) 7804 return StmtError(); 7805 } 7806 } 7807 7808 setFunctionHasBranchProtectedScope(); 7809 return OMPTargetTeamsDistributeParallelForDirective::Create( 7810 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 7811 DSAStack->isCancelRegion()); 7812 } 7813 7814 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 7815 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7816 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7817 if (!AStmt) 7818 return StmtError(); 7819 7820 auto *CS = cast<CapturedStmt>(AStmt); 7821 // 1.2.2 OpenMP Language Terminology 7822 // Structured block - An executable statement with a single entry at the 7823 // top and a single exit at the bottom. 7824 // The point of exit cannot be a branch out of the structured block. 7825 // longjmp() and throw() must not violate the entry/exit criteria. 7826 CS->getCapturedDecl()->setNothrow(); 7827 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 7828 OMPD_target_teams_distribute_parallel_for_simd); 7829 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7830 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7831 // 1.2.2 OpenMP Language Terminology 7832 // Structured block - An executable statement with a single entry at the 7833 // top and a single exit at the bottom. 7834 // The point of exit cannot be a branch out of the structured block. 7835 // longjmp() and throw() must not violate the entry/exit criteria. 7836 CS->getCapturedDecl()->setNothrow(); 7837 } 7838 7839 OMPLoopDirective::HelperExprs B; 7840 // In presence of clause 'collapse' with number of loops, it will 7841 // define the nested loops number. 7842 unsigned NestedLoopCount = 7843 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 7844 getCollapseNumberExpr(Clauses), 7845 nullptr /*ordered not a clause on distribute*/, CS, *this, 7846 *DSAStack, VarsWithImplicitDSA, B); 7847 if (NestedLoopCount == 0) 7848 return StmtError(); 7849 7850 assert((CurContext->isDependentContext() || B.builtAll()) && 7851 "omp target teams distribute parallel for simd loop exprs were not " 7852 "built"); 7853 7854 if (!CurContext->isDependentContext()) { 7855 // Finalize the clauses that need pre-built expressions for CodeGen. 7856 for (OMPClause *C : Clauses) { 7857 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7858 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7859 B.NumIterations, *this, CurScope, 7860 DSAStack)) 7861 return StmtError(); 7862 } 7863 } 7864 7865 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7866 return StmtError(); 7867 7868 setFunctionHasBranchProtectedScope(); 7869 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 7870 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7871 } 7872 7873 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 7874 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7875 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7876 if (!AStmt) 7877 return StmtError(); 7878 7879 auto *CS = cast<CapturedStmt>(AStmt); 7880 // 1.2.2 OpenMP Language Terminology 7881 // Structured block - An executable statement with a single entry at the 7882 // top and a single exit at the bottom. 7883 // The point of exit cannot be a branch out of the structured block. 7884 // longjmp() and throw() must not violate the entry/exit criteria. 7885 CS->getCapturedDecl()->setNothrow(); 7886 for (int ThisCaptureLevel = 7887 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 7888 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7889 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7890 // 1.2.2 OpenMP Language Terminology 7891 // Structured block - An executable statement with a single entry at the 7892 // top and a single exit at the bottom. 7893 // The point of exit cannot be a branch out of the structured block. 7894 // longjmp() and throw() must not violate the entry/exit criteria. 7895 CS->getCapturedDecl()->setNothrow(); 7896 } 7897 7898 OMPLoopDirective::HelperExprs B; 7899 // In presence of clause 'collapse' with number of loops, it will 7900 // define the nested loops number. 7901 unsigned NestedLoopCount = checkOpenMPLoop( 7902 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 7903 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7904 VarsWithImplicitDSA, B); 7905 if (NestedLoopCount == 0) 7906 return StmtError(); 7907 7908 assert((CurContext->isDependentContext() || B.builtAll()) && 7909 "omp target teams distribute simd loop exprs were not built"); 7910 7911 if (!CurContext->isDependentContext()) { 7912 // Finalize the clauses that need pre-built expressions for CodeGen. 7913 for (OMPClause *C : Clauses) { 7914 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7915 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7916 B.NumIterations, *this, CurScope, 7917 DSAStack)) 7918 return StmtError(); 7919 } 7920 } 7921 7922 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7923 return StmtError(); 7924 7925 setFunctionHasBranchProtectedScope(); 7926 return OMPTargetTeamsDistributeSimdDirective::Create( 7927 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7928 } 7929 7930 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 7931 SourceLocation StartLoc, 7932 SourceLocation LParenLoc, 7933 SourceLocation EndLoc) { 7934 OMPClause *Res = nullptr; 7935 switch (Kind) { 7936 case OMPC_final: 7937 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 7938 break; 7939 case OMPC_num_threads: 7940 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 7941 break; 7942 case OMPC_safelen: 7943 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 7944 break; 7945 case OMPC_simdlen: 7946 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 7947 break; 7948 case OMPC_collapse: 7949 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 7950 break; 7951 case OMPC_ordered: 7952 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 7953 break; 7954 case OMPC_device: 7955 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc); 7956 break; 7957 case OMPC_num_teams: 7958 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 7959 break; 7960 case OMPC_thread_limit: 7961 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 7962 break; 7963 case OMPC_priority: 7964 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 7965 break; 7966 case OMPC_grainsize: 7967 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 7968 break; 7969 case OMPC_num_tasks: 7970 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 7971 break; 7972 case OMPC_hint: 7973 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 7974 break; 7975 case OMPC_if: 7976 case OMPC_default: 7977 case OMPC_proc_bind: 7978 case OMPC_schedule: 7979 case OMPC_private: 7980 case OMPC_firstprivate: 7981 case OMPC_lastprivate: 7982 case OMPC_shared: 7983 case OMPC_reduction: 7984 case OMPC_task_reduction: 7985 case OMPC_in_reduction: 7986 case OMPC_linear: 7987 case OMPC_aligned: 7988 case OMPC_copyin: 7989 case OMPC_copyprivate: 7990 case OMPC_nowait: 7991 case OMPC_untied: 7992 case OMPC_mergeable: 7993 case OMPC_threadprivate: 7994 case OMPC_flush: 7995 case OMPC_read: 7996 case OMPC_write: 7997 case OMPC_update: 7998 case OMPC_capture: 7999 case OMPC_seq_cst: 8000 case OMPC_depend: 8001 case OMPC_threads: 8002 case OMPC_simd: 8003 case OMPC_map: 8004 case OMPC_nogroup: 8005 case OMPC_dist_schedule: 8006 case OMPC_defaultmap: 8007 case OMPC_unknown: 8008 case OMPC_uniform: 8009 case OMPC_to: 8010 case OMPC_from: 8011 case OMPC_use_device_ptr: 8012 case OMPC_is_device_ptr: 8013 case OMPC_unified_address: 8014 case OMPC_unified_shared_memory: 8015 case OMPC_reverse_offload: 8016 case OMPC_dynamic_allocators: 8017 llvm_unreachable("Clause is not allowed."); 8018 } 8019 return Res; 8020 } 8021 8022 // An OpenMP directive such as 'target parallel' has two captured regions: 8023 // for the 'target' and 'parallel' respectively. This function returns 8024 // the region in which to capture expressions associated with a clause. 8025 // A return value of OMPD_unknown signifies that the expression should not 8026 // be captured. 8027 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 8028 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 8029 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 8030 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 8031 switch (CKind) { 8032 case OMPC_if: 8033 switch (DKind) { 8034 case OMPD_target_parallel: 8035 case OMPD_target_parallel_for: 8036 case OMPD_target_parallel_for_simd: 8037 // If this clause applies to the nested 'parallel' region, capture within 8038 // the 'target' region, otherwise do not capture. 8039 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 8040 CaptureRegion = OMPD_target; 8041 break; 8042 case OMPD_target_teams_distribute_parallel_for: 8043 case OMPD_target_teams_distribute_parallel_for_simd: 8044 // If this clause applies to the nested 'parallel' region, capture within 8045 // the 'teams' region, otherwise do not capture. 8046 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 8047 CaptureRegion = OMPD_teams; 8048 break; 8049 case OMPD_teams_distribute_parallel_for: 8050 case OMPD_teams_distribute_parallel_for_simd: 8051 CaptureRegion = OMPD_teams; 8052 break; 8053 case OMPD_target_update: 8054 case OMPD_target_enter_data: 8055 case OMPD_target_exit_data: 8056 CaptureRegion = OMPD_task; 8057 break; 8058 case OMPD_cancel: 8059 case OMPD_parallel: 8060 case OMPD_parallel_sections: 8061 case OMPD_parallel_for: 8062 case OMPD_parallel_for_simd: 8063 case OMPD_target: 8064 case OMPD_target_simd: 8065 case OMPD_target_teams: 8066 case OMPD_target_teams_distribute: 8067 case OMPD_target_teams_distribute_simd: 8068 case OMPD_distribute_parallel_for: 8069 case OMPD_distribute_parallel_for_simd: 8070 case OMPD_task: 8071 case OMPD_taskloop: 8072 case OMPD_taskloop_simd: 8073 case OMPD_target_data: 8074 // Do not capture if-clause expressions. 8075 break; 8076 case OMPD_threadprivate: 8077 case OMPD_taskyield: 8078 case OMPD_barrier: 8079 case OMPD_taskwait: 8080 case OMPD_cancellation_point: 8081 case OMPD_flush: 8082 case OMPD_declare_reduction: 8083 case OMPD_declare_simd: 8084 case OMPD_declare_target: 8085 case OMPD_end_declare_target: 8086 case OMPD_teams: 8087 case OMPD_simd: 8088 case OMPD_for: 8089 case OMPD_for_simd: 8090 case OMPD_sections: 8091 case OMPD_section: 8092 case OMPD_single: 8093 case OMPD_master: 8094 case OMPD_critical: 8095 case OMPD_taskgroup: 8096 case OMPD_distribute: 8097 case OMPD_ordered: 8098 case OMPD_atomic: 8099 case OMPD_distribute_simd: 8100 case OMPD_teams_distribute: 8101 case OMPD_teams_distribute_simd: 8102 case OMPD_requires: 8103 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 8104 case OMPD_unknown: 8105 llvm_unreachable("Unknown OpenMP directive"); 8106 } 8107 break; 8108 case OMPC_num_threads: 8109 switch (DKind) { 8110 case OMPD_target_parallel: 8111 case OMPD_target_parallel_for: 8112 case OMPD_target_parallel_for_simd: 8113 CaptureRegion = OMPD_target; 8114 break; 8115 case OMPD_teams_distribute_parallel_for: 8116 case OMPD_teams_distribute_parallel_for_simd: 8117 case OMPD_target_teams_distribute_parallel_for: 8118 case OMPD_target_teams_distribute_parallel_for_simd: 8119 CaptureRegion = OMPD_teams; 8120 break; 8121 case OMPD_parallel: 8122 case OMPD_parallel_sections: 8123 case OMPD_parallel_for: 8124 case OMPD_parallel_for_simd: 8125 case OMPD_distribute_parallel_for: 8126 case OMPD_distribute_parallel_for_simd: 8127 // Do not capture num_threads-clause expressions. 8128 break; 8129 case OMPD_target_data: 8130 case OMPD_target_enter_data: 8131 case OMPD_target_exit_data: 8132 case OMPD_target_update: 8133 case OMPD_target: 8134 case OMPD_target_simd: 8135 case OMPD_target_teams: 8136 case OMPD_target_teams_distribute: 8137 case OMPD_target_teams_distribute_simd: 8138 case OMPD_cancel: 8139 case OMPD_task: 8140 case OMPD_taskloop: 8141 case OMPD_taskloop_simd: 8142 case OMPD_threadprivate: 8143 case OMPD_taskyield: 8144 case OMPD_barrier: 8145 case OMPD_taskwait: 8146 case OMPD_cancellation_point: 8147 case OMPD_flush: 8148 case OMPD_declare_reduction: 8149 case OMPD_declare_simd: 8150 case OMPD_declare_target: 8151 case OMPD_end_declare_target: 8152 case OMPD_teams: 8153 case OMPD_simd: 8154 case OMPD_for: 8155 case OMPD_for_simd: 8156 case OMPD_sections: 8157 case OMPD_section: 8158 case OMPD_single: 8159 case OMPD_master: 8160 case OMPD_critical: 8161 case OMPD_taskgroup: 8162 case OMPD_distribute: 8163 case OMPD_ordered: 8164 case OMPD_atomic: 8165 case OMPD_distribute_simd: 8166 case OMPD_teams_distribute: 8167 case OMPD_teams_distribute_simd: 8168 case OMPD_requires: 8169 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 8170 case OMPD_unknown: 8171 llvm_unreachable("Unknown OpenMP directive"); 8172 } 8173 break; 8174 case OMPC_num_teams: 8175 switch (DKind) { 8176 case OMPD_target_teams: 8177 case OMPD_target_teams_distribute: 8178 case OMPD_target_teams_distribute_simd: 8179 case OMPD_target_teams_distribute_parallel_for: 8180 case OMPD_target_teams_distribute_parallel_for_simd: 8181 CaptureRegion = OMPD_target; 8182 break; 8183 case OMPD_teams_distribute_parallel_for: 8184 case OMPD_teams_distribute_parallel_for_simd: 8185 case OMPD_teams: 8186 case OMPD_teams_distribute: 8187 case OMPD_teams_distribute_simd: 8188 // Do not capture num_teams-clause expressions. 8189 break; 8190 case OMPD_distribute_parallel_for: 8191 case OMPD_distribute_parallel_for_simd: 8192 case OMPD_task: 8193 case OMPD_taskloop: 8194 case OMPD_taskloop_simd: 8195 case OMPD_target_data: 8196 case OMPD_target_enter_data: 8197 case OMPD_target_exit_data: 8198 case OMPD_target_update: 8199 case OMPD_cancel: 8200 case OMPD_parallel: 8201 case OMPD_parallel_sections: 8202 case OMPD_parallel_for: 8203 case OMPD_parallel_for_simd: 8204 case OMPD_target: 8205 case OMPD_target_simd: 8206 case OMPD_target_parallel: 8207 case OMPD_target_parallel_for: 8208 case OMPD_target_parallel_for_simd: 8209 case OMPD_threadprivate: 8210 case OMPD_taskyield: 8211 case OMPD_barrier: 8212 case OMPD_taskwait: 8213 case OMPD_cancellation_point: 8214 case OMPD_flush: 8215 case OMPD_declare_reduction: 8216 case OMPD_declare_simd: 8217 case OMPD_declare_target: 8218 case OMPD_end_declare_target: 8219 case OMPD_simd: 8220 case OMPD_for: 8221 case OMPD_for_simd: 8222 case OMPD_sections: 8223 case OMPD_section: 8224 case OMPD_single: 8225 case OMPD_master: 8226 case OMPD_critical: 8227 case OMPD_taskgroup: 8228 case OMPD_distribute: 8229 case OMPD_ordered: 8230 case OMPD_atomic: 8231 case OMPD_distribute_simd: 8232 case OMPD_requires: 8233 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 8234 case OMPD_unknown: 8235 llvm_unreachable("Unknown OpenMP directive"); 8236 } 8237 break; 8238 case OMPC_thread_limit: 8239 switch (DKind) { 8240 case OMPD_target_teams: 8241 case OMPD_target_teams_distribute: 8242 case OMPD_target_teams_distribute_simd: 8243 case OMPD_target_teams_distribute_parallel_for: 8244 case OMPD_target_teams_distribute_parallel_for_simd: 8245 CaptureRegion = OMPD_target; 8246 break; 8247 case OMPD_teams_distribute_parallel_for: 8248 case OMPD_teams_distribute_parallel_for_simd: 8249 case OMPD_teams: 8250 case OMPD_teams_distribute: 8251 case OMPD_teams_distribute_simd: 8252 // Do not capture thread_limit-clause expressions. 8253 break; 8254 case OMPD_distribute_parallel_for: 8255 case OMPD_distribute_parallel_for_simd: 8256 case OMPD_task: 8257 case OMPD_taskloop: 8258 case OMPD_taskloop_simd: 8259 case OMPD_target_data: 8260 case OMPD_target_enter_data: 8261 case OMPD_target_exit_data: 8262 case OMPD_target_update: 8263 case OMPD_cancel: 8264 case OMPD_parallel: 8265 case OMPD_parallel_sections: 8266 case OMPD_parallel_for: 8267 case OMPD_parallel_for_simd: 8268 case OMPD_target: 8269 case OMPD_target_simd: 8270 case OMPD_target_parallel: 8271 case OMPD_target_parallel_for: 8272 case OMPD_target_parallel_for_simd: 8273 case OMPD_threadprivate: 8274 case OMPD_taskyield: 8275 case OMPD_barrier: 8276 case OMPD_taskwait: 8277 case OMPD_cancellation_point: 8278 case OMPD_flush: 8279 case OMPD_declare_reduction: 8280 case OMPD_declare_simd: 8281 case OMPD_declare_target: 8282 case OMPD_end_declare_target: 8283 case OMPD_simd: 8284 case OMPD_for: 8285 case OMPD_for_simd: 8286 case OMPD_sections: 8287 case OMPD_section: 8288 case OMPD_single: 8289 case OMPD_master: 8290 case OMPD_critical: 8291 case OMPD_taskgroup: 8292 case OMPD_distribute: 8293 case OMPD_ordered: 8294 case OMPD_atomic: 8295 case OMPD_distribute_simd: 8296 case OMPD_requires: 8297 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 8298 case OMPD_unknown: 8299 llvm_unreachable("Unknown OpenMP directive"); 8300 } 8301 break; 8302 case OMPC_schedule: 8303 switch (DKind) { 8304 case OMPD_parallel_for: 8305 case OMPD_parallel_for_simd: 8306 case OMPD_distribute_parallel_for: 8307 case OMPD_distribute_parallel_for_simd: 8308 case OMPD_teams_distribute_parallel_for: 8309 case OMPD_teams_distribute_parallel_for_simd: 8310 case OMPD_target_parallel_for: 8311 case OMPD_target_parallel_for_simd: 8312 case OMPD_target_teams_distribute_parallel_for: 8313 case OMPD_target_teams_distribute_parallel_for_simd: 8314 CaptureRegion = OMPD_parallel; 8315 break; 8316 case OMPD_for: 8317 case OMPD_for_simd: 8318 // Do not capture schedule-clause expressions. 8319 break; 8320 case OMPD_task: 8321 case OMPD_taskloop: 8322 case OMPD_taskloop_simd: 8323 case OMPD_target_data: 8324 case OMPD_target_enter_data: 8325 case OMPD_target_exit_data: 8326 case OMPD_target_update: 8327 case OMPD_teams: 8328 case OMPD_teams_distribute: 8329 case OMPD_teams_distribute_simd: 8330 case OMPD_target_teams_distribute: 8331 case OMPD_target_teams_distribute_simd: 8332 case OMPD_target: 8333 case OMPD_target_simd: 8334 case OMPD_target_parallel: 8335 case OMPD_cancel: 8336 case OMPD_parallel: 8337 case OMPD_parallel_sections: 8338 case OMPD_threadprivate: 8339 case OMPD_taskyield: 8340 case OMPD_barrier: 8341 case OMPD_taskwait: 8342 case OMPD_cancellation_point: 8343 case OMPD_flush: 8344 case OMPD_declare_reduction: 8345 case OMPD_declare_simd: 8346 case OMPD_declare_target: 8347 case OMPD_end_declare_target: 8348 case OMPD_simd: 8349 case OMPD_sections: 8350 case OMPD_section: 8351 case OMPD_single: 8352 case OMPD_master: 8353 case OMPD_critical: 8354 case OMPD_taskgroup: 8355 case OMPD_distribute: 8356 case OMPD_ordered: 8357 case OMPD_atomic: 8358 case OMPD_distribute_simd: 8359 case OMPD_target_teams: 8360 case OMPD_requires: 8361 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 8362 case OMPD_unknown: 8363 llvm_unreachable("Unknown OpenMP directive"); 8364 } 8365 break; 8366 case OMPC_dist_schedule: 8367 switch (DKind) { 8368 case OMPD_teams_distribute_parallel_for: 8369 case OMPD_teams_distribute_parallel_for_simd: 8370 case OMPD_teams_distribute: 8371 case OMPD_teams_distribute_simd: 8372 case OMPD_target_teams_distribute_parallel_for: 8373 case OMPD_target_teams_distribute_parallel_for_simd: 8374 case OMPD_target_teams_distribute: 8375 case OMPD_target_teams_distribute_simd: 8376 CaptureRegion = OMPD_teams; 8377 break; 8378 case OMPD_distribute_parallel_for: 8379 case OMPD_distribute_parallel_for_simd: 8380 case OMPD_distribute: 8381 case OMPD_distribute_simd: 8382 // Do not capture thread_limit-clause expressions. 8383 break; 8384 case OMPD_parallel_for: 8385 case OMPD_parallel_for_simd: 8386 case OMPD_target_parallel_for_simd: 8387 case OMPD_target_parallel_for: 8388 case OMPD_task: 8389 case OMPD_taskloop: 8390 case OMPD_taskloop_simd: 8391 case OMPD_target_data: 8392 case OMPD_target_enter_data: 8393 case OMPD_target_exit_data: 8394 case OMPD_target_update: 8395 case OMPD_teams: 8396 case OMPD_target: 8397 case OMPD_target_simd: 8398 case OMPD_target_parallel: 8399 case OMPD_cancel: 8400 case OMPD_parallel: 8401 case OMPD_parallel_sections: 8402 case OMPD_threadprivate: 8403 case OMPD_taskyield: 8404 case OMPD_barrier: 8405 case OMPD_taskwait: 8406 case OMPD_cancellation_point: 8407 case OMPD_flush: 8408 case OMPD_declare_reduction: 8409 case OMPD_declare_simd: 8410 case OMPD_declare_target: 8411 case OMPD_end_declare_target: 8412 case OMPD_simd: 8413 case OMPD_for: 8414 case OMPD_for_simd: 8415 case OMPD_sections: 8416 case OMPD_section: 8417 case OMPD_single: 8418 case OMPD_master: 8419 case OMPD_critical: 8420 case OMPD_taskgroup: 8421 case OMPD_ordered: 8422 case OMPD_atomic: 8423 case OMPD_target_teams: 8424 case OMPD_requires: 8425 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 8426 case OMPD_unknown: 8427 llvm_unreachable("Unknown OpenMP directive"); 8428 } 8429 break; 8430 case OMPC_device: 8431 switch (DKind) { 8432 case OMPD_target_update: 8433 case OMPD_target_enter_data: 8434 case OMPD_target_exit_data: 8435 case OMPD_target: 8436 case OMPD_target_simd: 8437 case OMPD_target_teams: 8438 case OMPD_target_parallel: 8439 case OMPD_target_teams_distribute: 8440 case OMPD_target_teams_distribute_simd: 8441 case OMPD_target_parallel_for: 8442 case OMPD_target_parallel_for_simd: 8443 case OMPD_target_teams_distribute_parallel_for: 8444 case OMPD_target_teams_distribute_parallel_for_simd: 8445 CaptureRegion = OMPD_task; 8446 break; 8447 case OMPD_target_data: 8448 // Do not capture device-clause expressions. 8449 break; 8450 case OMPD_teams_distribute_parallel_for: 8451 case OMPD_teams_distribute_parallel_for_simd: 8452 case OMPD_teams: 8453 case OMPD_teams_distribute: 8454 case OMPD_teams_distribute_simd: 8455 case OMPD_distribute_parallel_for: 8456 case OMPD_distribute_parallel_for_simd: 8457 case OMPD_task: 8458 case OMPD_taskloop: 8459 case OMPD_taskloop_simd: 8460 case OMPD_cancel: 8461 case OMPD_parallel: 8462 case OMPD_parallel_sections: 8463 case OMPD_parallel_for: 8464 case OMPD_parallel_for_simd: 8465 case OMPD_threadprivate: 8466 case OMPD_taskyield: 8467 case OMPD_barrier: 8468 case OMPD_taskwait: 8469 case OMPD_cancellation_point: 8470 case OMPD_flush: 8471 case OMPD_declare_reduction: 8472 case OMPD_declare_simd: 8473 case OMPD_declare_target: 8474 case OMPD_end_declare_target: 8475 case OMPD_simd: 8476 case OMPD_for: 8477 case OMPD_for_simd: 8478 case OMPD_sections: 8479 case OMPD_section: 8480 case OMPD_single: 8481 case OMPD_master: 8482 case OMPD_critical: 8483 case OMPD_taskgroup: 8484 case OMPD_distribute: 8485 case OMPD_ordered: 8486 case OMPD_atomic: 8487 case OMPD_distribute_simd: 8488 case OMPD_requires: 8489 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 8490 case OMPD_unknown: 8491 llvm_unreachable("Unknown OpenMP directive"); 8492 } 8493 break; 8494 case OMPC_firstprivate: 8495 case OMPC_lastprivate: 8496 case OMPC_reduction: 8497 case OMPC_task_reduction: 8498 case OMPC_in_reduction: 8499 case OMPC_linear: 8500 case OMPC_default: 8501 case OMPC_proc_bind: 8502 case OMPC_final: 8503 case OMPC_safelen: 8504 case OMPC_simdlen: 8505 case OMPC_collapse: 8506 case OMPC_private: 8507 case OMPC_shared: 8508 case OMPC_aligned: 8509 case OMPC_copyin: 8510 case OMPC_copyprivate: 8511 case OMPC_ordered: 8512 case OMPC_nowait: 8513 case OMPC_untied: 8514 case OMPC_mergeable: 8515 case OMPC_threadprivate: 8516 case OMPC_flush: 8517 case OMPC_read: 8518 case OMPC_write: 8519 case OMPC_update: 8520 case OMPC_capture: 8521 case OMPC_seq_cst: 8522 case OMPC_depend: 8523 case OMPC_threads: 8524 case OMPC_simd: 8525 case OMPC_map: 8526 case OMPC_priority: 8527 case OMPC_grainsize: 8528 case OMPC_nogroup: 8529 case OMPC_num_tasks: 8530 case OMPC_hint: 8531 case OMPC_defaultmap: 8532 case OMPC_unknown: 8533 case OMPC_uniform: 8534 case OMPC_to: 8535 case OMPC_from: 8536 case OMPC_use_device_ptr: 8537 case OMPC_is_device_ptr: 8538 case OMPC_unified_address: 8539 case OMPC_unified_shared_memory: 8540 case OMPC_reverse_offload: 8541 case OMPC_dynamic_allocators: 8542 llvm_unreachable("Unexpected OpenMP clause."); 8543 } 8544 return CaptureRegion; 8545 } 8546 8547 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 8548 Expr *Condition, SourceLocation StartLoc, 8549 SourceLocation LParenLoc, 8550 SourceLocation NameModifierLoc, 8551 SourceLocation ColonLoc, 8552 SourceLocation EndLoc) { 8553 Expr *ValExpr = Condition; 8554 Stmt *HelperValStmt = nullptr; 8555 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 8556 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 8557 !Condition->isInstantiationDependent() && 8558 !Condition->containsUnexpandedParameterPack()) { 8559 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 8560 if (Val.isInvalid()) 8561 return nullptr; 8562 8563 ValExpr = Val.get(); 8564 8565 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 8566 CaptureRegion = 8567 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier); 8568 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 8569 ValExpr = MakeFullExpr(ValExpr).get(); 8570 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 8571 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 8572 HelperValStmt = buildPreInits(Context, Captures); 8573 } 8574 } 8575 8576 return new (Context) 8577 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 8578 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 8579 } 8580 8581 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 8582 SourceLocation StartLoc, 8583 SourceLocation LParenLoc, 8584 SourceLocation EndLoc) { 8585 Expr *ValExpr = Condition; 8586 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 8587 !Condition->isInstantiationDependent() && 8588 !Condition->containsUnexpandedParameterPack()) { 8589 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 8590 if (Val.isInvalid()) 8591 return nullptr; 8592 8593 ValExpr = MakeFullExpr(Val.get()).get(); 8594 } 8595 8596 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc); 8597 } 8598 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 8599 Expr *Op) { 8600 if (!Op) 8601 return ExprError(); 8602 8603 class IntConvertDiagnoser : public ICEConvertDiagnoser { 8604 public: 8605 IntConvertDiagnoser() 8606 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 8607 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 8608 QualType T) override { 8609 return S.Diag(Loc, diag::err_omp_not_integral) << T; 8610 } 8611 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 8612 QualType T) override { 8613 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 8614 } 8615 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 8616 QualType T, 8617 QualType ConvTy) override { 8618 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 8619 } 8620 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 8621 QualType ConvTy) override { 8622 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 8623 << ConvTy->isEnumeralType() << ConvTy; 8624 } 8625 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 8626 QualType T) override { 8627 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 8628 } 8629 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 8630 QualType ConvTy) override { 8631 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 8632 << ConvTy->isEnumeralType() << ConvTy; 8633 } 8634 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 8635 QualType) override { 8636 llvm_unreachable("conversion functions are permitted"); 8637 } 8638 } ConvertDiagnoser; 8639 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 8640 } 8641 8642 static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, 8643 OpenMPClauseKind CKind, 8644 bool StrictlyPositive) { 8645 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 8646 !ValExpr->isInstantiationDependent()) { 8647 SourceLocation Loc = ValExpr->getExprLoc(); 8648 ExprResult Value = 8649 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 8650 if (Value.isInvalid()) 8651 return false; 8652 8653 ValExpr = Value.get(); 8654 // The expression must evaluate to a non-negative integer value. 8655 llvm::APSInt Result; 8656 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) && 8657 Result.isSigned() && 8658 !((!StrictlyPositive && Result.isNonNegative()) || 8659 (StrictlyPositive && Result.isStrictlyPositive()))) { 8660 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 8661 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 8662 << ValExpr->getSourceRange(); 8663 return false; 8664 } 8665 } 8666 return true; 8667 } 8668 8669 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 8670 SourceLocation StartLoc, 8671 SourceLocation LParenLoc, 8672 SourceLocation EndLoc) { 8673 Expr *ValExpr = NumThreads; 8674 Stmt *HelperValStmt = nullptr; 8675 8676 // OpenMP [2.5, Restrictions] 8677 // The num_threads expression must evaluate to a positive integer value. 8678 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 8679 /*StrictlyPositive=*/true)) 8680 return nullptr; 8681 8682 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 8683 OpenMPDirectiveKind CaptureRegion = 8684 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads); 8685 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 8686 ValExpr = MakeFullExpr(ValExpr).get(); 8687 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 8688 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 8689 HelperValStmt = buildPreInits(Context, Captures); 8690 } 8691 8692 return new (Context) OMPNumThreadsClause( 8693 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 8694 } 8695 8696 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 8697 OpenMPClauseKind CKind, 8698 bool StrictlyPositive) { 8699 if (!E) 8700 return ExprError(); 8701 if (E->isValueDependent() || E->isTypeDependent() || 8702 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 8703 return E; 8704 llvm::APSInt Result; 8705 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); 8706 if (ICE.isInvalid()) 8707 return ExprError(); 8708 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 8709 (!StrictlyPositive && !Result.isNonNegative())) { 8710 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 8711 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 8712 << E->getSourceRange(); 8713 return ExprError(); 8714 } 8715 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 8716 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 8717 << E->getSourceRange(); 8718 return ExprError(); 8719 } 8720 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 8721 DSAStack->setAssociatedLoops(Result.getExtValue()); 8722 else if (CKind == OMPC_ordered) 8723 DSAStack->setAssociatedLoops(Result.getExtValue()); 8724 return ICE; 8725 } 8726 8727 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 8728 SourceLocation LParenLoc, 8729 SourceLocation EndLoc) { 8730 // OpenMP [2.8.1, simd construct, Description] 8731 // The parameter of the safelen clause must be a constant 8732 // positive integer expression. 8733 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 8734 if (Safelen.isInvalid()) 8735 return nullptr; 8736 return new (Context) 8737 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 8738 } 8739 8740 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 8741 SourceLocation LParenLoc, 8742 SourceLocation EndLoc) { 8743 // OpenMP [2.8.1, simd construct, Description] 8744 // The parameter of the simdlen clause must be a constant 8745 // positive integer expression. 8746 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 8747 if (Simdlen.isInvalid()) 8748 return nullptr; 8749 return new (Context) 8750 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 8751 } 8752 8753 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 8754 SourceLocation StartLoc, 8755 SourceLocation LParenLoc, 8756 SourceLocation EndLoc) { 8757 // OpenMP [2.7.1, loop construct, Description] 8758 // OpenMP [2.8.1, simd construct, Description] 8759 // OpenMP [2.9.6, distribute construct, Description] 8760 // The parameter of the collapse clause must be a constant 8761 // positive integer expression. 8762 ExprResult NumForLoopsResult = 8763 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 8764 if (NumForLoopsResult.isInvalid()) 8765 return nullptr; 8766 return new (Context) 8767 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 8768 } 8769 8770 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 8771 SourceLocation EndLoc, 8772 SourceLocation LParenLoc, 8773 Expr *NumForLoops) { 8774 // OpenMP [2.7.1, loop construct, Description] 8775 // OpenMP [2.8.1, simd construct, Description] 8776 // OpenMP [2.9.6, distribute construct, Description] 8777 // The parameter of the ordered clause must be a constant 8778 // positive integer expression if any. 8779 if (NumForLoops && LParenLoc.isValid()) { 8780 ExprResult NumForLoopsResult = 8781 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 8782 if (NumForLoopsResult.isInvalid()) 8783 return nullptr; 8784 NumForLoops = NumForLoopsResult.get(); 8785 } else { 8786 NumForLoops = nullptr; 8787 } 8788 auto *Clause = OMPOrderedClause::Create( 8789 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0, 8790 StartLoc, LParenLoc, EndLoc); 8791 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause); 8792 return Clause; 8793 } 8794 8795 OMPClause *Sema::ActOnOpenMPSimpleClause( 8796 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 8797 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 8798 OMPClause *Res = nullptr; 8799 switch (Kind) { 8800 case OMPC_default: 8801 Res = 8802 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), 8803 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 8804 break; 8805 case OMPC_proc_bind: 8806 Res = ActOnOpenMPProcBindClause( 8807 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, 8808 LParenLoc, EndLoc); 8809 break; 8810 case OMPC_if: 8811 case OMPC_final: 8812 case OMPC_num_threads: 8813 case OMPC_safelen: 8814 case OMPC_simdlen: 8815 case OMPC_collapse: 8816 case OMPC_schedule: 8817 case OMPC_private: 8818 case OMPC_firstprivate: 8819 case OMPC_lastprivate: 8820 case OMPC_shared: 8821 case OMPC_reduction: 8822 case OMPC_task_reduction: 8823 case OMPC_in_reduction: 8824 case OMPC_linear: 8825 case OMPC_aligned: 8826 case OMPC_copyin: 8827 case OMPC_copyprivate: 8828 case OMPC_ordered: 8829 case OMPC_nowait: 8830 case OMPC_untied: 8831 case OMPC_mergeable: 8832 case OMPC_threadprivate: 8833 case OMPC_flush: 8834 case OMPC_read: 8835 case OMPC_write: 8836 case OMPC_update: 8837 case OMPC_capture: 8838 case OMPC_seq_cst: 8839 case OMPC_depend: 8840 case OMPC_device: 8841 case OMPC_threads: 8842 case OMPC_simd: 8843 case OMPC_map: 8844 case OMPC_num_teams: 8845 case OMPC_thread_limit: 8846 case OMPC_priority: 8847 case OMPC_grainsize: 8848 case OMPC_nogroup: 8849 case OMPC_num_tasks: 8850 case OMPC_hint: 8851 case OMPC_dist_schedule: 8852 case OMPC_defaultmap: 8853 case OMPC_unknown: 8854 case OMPC_uniform: 8855 case OMPC_to: 8856 case OMPC_from: 8857 case OMPC_use_device_ptr: 8858 case OMPC_is_device_ptr: 8859 case OMPC_unified_address: 8860 case OMPC_unified_shared_memory: 8861 case OMPC_reverse_offload: 8862 case OMPC_dynamic_allocators: 8863 llvm_unreachable("Clause is not allowed."); 8864 } 8865 return Res; 8866 } 8867 8868 static std::string 8869 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 8870 ArrayRef<unsigned> Exclude = llvm::None) { 8871 SmallString<256> Buffer; 8872 llvm::raw_svector_ostream Out(Buffer); 8873 unsigned Bound = Last >= 2 ? Last - 2 : 0; 8874 unsigned Skipped = Exclude.size(); 8875 auto S = Exclude.begin(), E = Exclude.end(); 8876 for (unsigned I = First; I < Last; ++I) { 8877 if (std::find(S, E, I) != E) { 8878 --Skipped; 8879 continue; 8880 } 8881 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 8882 if (I == Bound - Skipped) 8883 Out << " or "; 8884 else if (I != Bound + 1 - Skipped) 8885 Out << ", "; 8886 } 8887 return Out.str(); 8888 } 8889 8890 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, 8891 SourceLocation KindKwLoc, 8892 SourceLocation StartLoc, 8893 SourceLocation LParenLoc, 8894 SourceLocation EndLoc) { 8895 if (Kind == OMPC_DEFAULT_unknown) { 8896 static_assert(OMPC_DEFAULT_unknown > 0, 8897 "OMPC_DEFAULT_unknown not greater than 0"); 8898 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 8899 << getListOfPossibleValues(OMPC_default, /*First=*/0, 8900 /*Last=*/OMPC_DEFAULT_unknown) 8901 << getOpenMPClauseName(OMPC_default); 8902 return nullptr; 8903 } 8904 switch (Kind) { 8905 case OMPC_DEFAULT_none: 8906 DSAStack->setDefaultDSANone(KindKwLoc); 8907 break; 8908 case OMPC_DEFAULT_shared: 8909 DSAStack->setDefaultDSAShared(KindKwLoc); 8910 break; 8911 case OMPC_DEFAULT_unknown: 8912 llvm_unreachable("Clause kind is not allowed."); 8913 break; 8914 } 8915 return new (Context) 8916 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 8917 } 8918 8919 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, 8920 SourceLocation KindKwLoc, 8921 SourceLocation StartLoc, 8922 SourceLocation LParenLoc, 8923 SourceLocation EndLoc) { 8924 if (Kind == OMPC_PROC_BIND_unknown) { 8925 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 8926 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0, 8927 /*Last=*/OMPC_PROC_BIND_unknown) 8928 << getOpenMPClauseName(OMPC_proc_bind); 8929 return nullptr; 8930 } 8931 return new (Context) 8932 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 8933 } 8934 8935 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 8936 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 8937 SourceLocation StartLoc, SourceLocation LParenLoc, 8938 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 8939 SourceLocation EndLoc) { 8940 OMPClause *Res = nullptr; 8941 switch (Kind) { 8942 case OMPC_schedule: 8943 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 8944 assert(Argument.size() == NumberOfElements && 8945 ArgumentLoc.size() == NumberOfElements); 8946 Res = ActOnOpenMPScheduleClause( 8947 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 8948 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 8949 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 8950 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 8951 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 8952 break; 8953 case OMPC_if: 8954 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 8955 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 8956 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 8957 DelimLoc, EndLoc); 8958 break; 8959 case OMPC_dist_schedule: 8960 Res = ActOnOpenMPDistScheduleClause( 8961 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 8962 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 8963 break; 8964 case OMPC_defaultmap: 8965 enum { Modifier, DefaultmapKind }; 8966 Res = ActOnOpenMPDefaultmapClause( 8967 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 8968 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 8969 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 8970 EndLoc); 8971 break; 8972 case OMPC_final: 8973 case OMPC_num_threads: 8974 case OMPC_safelen: 8975 case OMPC_simdlen: 8976 case OMPC_collapse: 8977 case OMPC_default: 8978 case OMPC_proc_bind: 8979 case OMPC_private: 8980 case OMPC_firstprivate: 8981 case OMPC_lastprivate: 8982 case OMPC_shared: 8983 case OMPC_reduction: 8984 case OMPC_task_reduction: 8985 case OMPC_in_reduction: 8986 case OMPC_linear: 8987 case OMPC_aligned: 8988 case OMPC_copyin: 8989 case OMPC_copyprivate: 8990 case OMPC_ordered: 8991 case OMPC_nowait: 8992 case OMPC_untied: 8993 case OMPC_mergeable: 8994 case OMPC_threadprivate: 8995 case OMPC_flush: 8996 case OMPC_read: 8997 case OMPC_write: 8998 case OMPC_update: 8999 case OMPC_capture: 9000 case OMPC_seq_cst: 9001 case OMPC_depend: 9002 case OMPC_device: 9003 case OMPC_threads: 9004 case OMPC_simd: 9005 case OMPC_map: 9006 case OMPC_num_teams: 9007 case OMPC_thread_limit: 9008 case OMPC_priority: 9009 case OMPC_grainsize: 9010 case OMPC_nogroup: 9011 case OMPC_num_tasks: 9012 case OMPC_hint: 9013 case OMPC_unknown: 9014 case OMPC_uniform: 9015 case OMPC_to: 9016 case OMPC_from: 9017 case OMPC_use_device_ptr: 9018 case OMPC_is_device_ptr: 9019 case OMPC_unified_address: 9020 case OMPC_unified_shared_memory: 9021 case OMPC_reverse_offload: 9022 case OMPC_dynamic_allocators: 9023 llvm_unreachable("Clause is not allowed."); 9024 } 9025 return Res; 9026 } 9027 9028 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 9029 OpenMPScheduleClauseModifier M2, 9030 SourceLocation M1Loc, SourceLocation M2Loc) { 9031 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 9032 SmallVector<unsigned, 2> Excluded; 9033 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 9034 Excluded.push_back(M2); 9035 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 9036 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 9037 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 9038 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 9039 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 9040 << getListOfPossibleValues(OMPC_schedule, 9041 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 9042 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 9043 Excluded) 9044 << getOpenMPClauseName(OMPC_schedule); 9045 return true; 9046 } 9047 return false; 9048 } 9049 9050 OMPClause *Sema::ActOnOpenMPScheduleClause( 9051 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 9052 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 9053 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 9054 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 9055 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 9056 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 9057 return nullptr; 9058 // OpenMP, 2.7.1, Loop Construct, Restrictions 9059 // Either the monotonic modifier or the nonmonotonic modifier can be specified 9060 // but not both. 9061 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 9062 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 9063 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 9064 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 9065 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 9066 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 9067 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 9068 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 9069 return nullptr; 9070 } 9071 if (Kind == OMPC_SCHEDULE_unknown) { 9072 std::string Values; 9073 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 9074 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 9075 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 9076 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 9077 Exclude); 9078 } else { 9079 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 9080 /*Last=*/OMPC_SCHEDULE_unknown); 9081 } 9082 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 9083 << Values << getOpenMPClauseName(OMPC_schedule); 9084 return nullptr; 9085 } 9086 // OpenMP, 2.7.1, Loop Construct, Restrictions 9087 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 9088 // schedule(guided). 9089 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 9090 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 9091 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 9092 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 9093 diag::err_omp_schedule_nonmonotonic_static); 9094 return nullptr; 9095 } 9096 Expr *ValExpr = ChunkSize; 9097 Stmt *HelperValStmt = nullptr; 9098 if (ChunkSize) { 9099 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 9100 !ChunkSize->isInstantiationDependent() && 9101 !ChunkSize->containsUnexpandedParameterPack()) { 9102 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 9103 ExprResult Val = 9104 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 9105 if (Val.isInvalid()) 9106 return nullptr; 9107 9108 ValExpr = Val.get(); 9109 9110 // OpenMP [2.7.1, Restrictions] 9111 // chunk_size must be a loop invariant integer expression with a positive 9112 // value. 9113 llvm::APSInt Result; 9114 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 9115 if (Result.isSigned() && !Result.isStrictlyPositive()) { 9116 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 9117 << "schedule" << 1 << ChunkSize->getSourceRange(); 9118 return nullptr; 9119 } 9120 } else if (getOpenMPCaptureRegionForClause( 9121 DSAStack->getCurrentDirective(), OMPC_schedule) != 9122 OMPD_unknown && 9123 !CurContext->isDependentContext()) { 9124 ValExpr = MakeFullExpr(ValExpr).get(); 9125 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 9126 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 9127 HelperValStmt = buildPreInits(Context, Captures); 9128 } 9129 } 9130 } 9131 9132 return new (Context) 9133 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 9134 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 9135 } 9136 9137 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 9138 SourceLocation StartLoc, 9139 SourceLocation EndLoc) { 9140 OMPClause *Res = nullptr; 9141 switch (Kind) { 9142 case OMPC_ordered: 9143 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 9144 break; 9145 case OMPC_nowait: 9146 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 9147 break; 9148 case OMPC_untied: 9149 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 9150 break; 9151 case OMPC_mergeable: 9152 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 9153 break; 9154 case OMPC_read: 9155 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 9156 break; 9157 case OMPC_write: 9158 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 9159 break; 9160 case OMPC_update: 9161 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 9162 break; 9163 case OMPC_capture: 9164 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 9165 break; 9166 case OMPC_seq_cst: 9167 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 9168 break; 9169 case OMPC_threads: 9170 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 9171 break; 9172 case OMPC_simd: 9173 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 9174 break; 9175 case OMPC_nogroup: 9176 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 9177 break; 9178 case OMPC_unified_address: 9179 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc); 9180 break; 9181 case OMPC_unified_shared_memory: 9182 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 9183 break; 9184 case OMPC_reverse_offload: 9185 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc); 9186 break; 9187 case OMPC_dynamic_allocators: 9188 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc); 9189 break; 9190 case OMPC_if: 9191 case OMPC_final: 9192 case OMPC_num_threads: 9193 case OMPC_safelen: 9194 case OMPC_simdlen: 9195 case OMPC_collapse: 9196 case OMPC_schedule: 9197 case OMPC_private: 9198 case OMPC_firstprivate: 9199 case OMPC_lastprivate: 9200 case OMPC_shared: 9201 case OMPC_reduction: 9202 case OMPC_task_reduction: 9203 case OMPC_in_reduction: 9204 case OMPC_linear: 9205 case OMPC_aligned: 9206 case OMPC_copyin: 9207 case OMPC_copyprivate: 9208 case OMPC_default: 9209 case OMPC_proc_bind: 9210 case OMPC_threadprivate: 9211 case OMPC_flush: 9212 case OMPC_depend: 9213 case OMPC_device: 9214 case OMPC_map: 9215 case OMPC_num_teams: 9216 case OMPC_thread_limit: 9217 case OMPC_priority: 9218 case OMPC_grainsize: 9219 case OMPC_num_tasks: 9220 case OMPC_hint: 9221 case OMPC_dist_schedule: 9222 case OMPC_defaultmap: 9223 case OMPC_unknown: 9224 case OMPC_uniform: 9225 case OMPC_to: 9226 case OMPC_from: 9227 case OMPC_use_device_ptr: 9228 case OMPC_is_device_ptr: 9229 llvm_unreachable("Clause is not allowed."); 9230 } 9231 return Res; 9232 } 9233 9234 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 9235 SourceLocation EndLoc) { 9236 DSAStack->setNowaitRegion(); 9237 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 9238 } 9239 9240 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 9241 SourceLocation EndLoc) { 9242 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 9243 } 9244 9245 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 9246 SourceLocation EndLoc) { 9247 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 9248 } 9249 9250 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 9251 SourceLocation EndLoc) { 9252 return new (Context) OMPReadClause(StartLoc, EndLoc); 9253 } 9254 9255 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 9256 SourceLocation EndLoc) { 9257 return new (Context) OMPWriteClause(StartLoc, EndLoc); 9258 } 9259 9260 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 9261 SourceLocation EndLoc) { 9262 return new (Context) OMPUpdateClause(StartLoc, EndLoc); 9263 } 9264 9265 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 9266 SourceLocation EndLoc) { 9267 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 9268 } 9269 9270 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 9271 SourceLocation EndLoc) { 9272 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 9273 } 9274 9275 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 9276 SourceLocation EndLoc) { 9277 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 9278 } 9279 9280 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 9281 SourceLocation EndLoc) { 9282 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 9283 } 9284 9285 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 9286 SourceLocation EndLoc) { 9287 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 9288 } 9289 9290 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc, 9291 SourceLocation EndLoc) { 9292 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc); 9293 } 9294 9295 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc, 9296 SourceLocation EndLoc) { 9297 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 9298 } 9299 9300 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc, 9301 SourceLocation EndLoc) { 9302 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc); 9303 } 9304 9305 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc, 9306 SourceLocation EndLoc) { 9307 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc); 9308 } 9309 9310 OMPClause *Sema::ActOnOpenMPVarListClause( 9311 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, 9312 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 9313 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec, 9314 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind, 9315 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier, 9316 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, 9317 SourceLocation DepLinMapLoc) { 9318 OMPClause *Res = nullptr; 9319 switch (Kind) { 9320 case OMPC_private: 9321 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9322 break; 9323 case OMPC_firstprivate: 9324 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9325 break; 9326 case OMPC_lastprivate: 9327 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9328 break; 9329 case OMPC_shared: 9330 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 9331 break; 9332 case OMPC_reduction: 9333 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 9334 EndLoc, ReductionIdScopeSpec, ReductionId); 9335 break; 9336 case OMPC_task_reduction: 9337 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 9338 EndLoc, ReductionIdScopeSpec, 9339 ReductionId); 9340 break; 9341 case OMPC_in_reduction: 9342 Res = 9343 ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 9344 EndLoc, ReductionIdScopeSpec, ReductionId); 9345 break; 9346 case OMPC_linear: 9347 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, 9348 LinKind, DepLinMapLoc, ColonLoc, EndLoc); 9349 break; 9350 case OMPC_aligned: 9351 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, 9352 ColonLoc, EndLoc); 9353 break; 9354 case OMPC_copyin: 9355 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 9356 break; 9357 case OMPC_copyprivate: 9358 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9359 break; 9360 case OMPC_flush: 9361 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 9362 break; 9363 case OMPC_depend: 9364 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList, 9365 StartLoc, LParenLoc, EndLoc); 9366 break; 9367 case OMPC_map: 9368 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit, 9369 DepLinMapLoc, ColonLoc, VarList, StartLoc, 9370 LParenLoc, EndLoc); 9371 break; 9372 case OMPC_to: 9373 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc); 9374 break; 9375 case OMPC_from: 9376 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc); 9377 break; 9378 case OMPC_use_device_ptr: 9379 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc); 9380 break; 9381 case OMPC_is_device_ptr: 9382 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc); 9383 break; 9384 case OMPC_if: 9385 case OMPC_final: 9386 case OMPC_num_threads: 9387 case OMPC_safelen: 9388 case OMPC_simdlen: 9389 case OMPC_collapse: 9390 case OMPC_default: 9391 case OMPC_proc_bind: 9392 case OMPC_schedule: 9393 case OMPC_ordered: 9394 case OMPC_nowait: 9395 case OMPC_untied: 9396 case OMPC_mergeable: 9397 case OMPC_threadprivate: 9398 case OMPC_read: 9399 case OMPC_write: 9400 case OMPC_update: 9401 case OMPC_capture: 9402 case OMPC_seq_cst: 9403 case OMPC_device: 9404 case OMPC_threads: 9405 case OMPC_simd: 9406 case OMPC_num_teams: 9407 case OMPC_thread_limit: 9408 case OMPC_priority: 9409 case OMPC_grainsize: 9410 case OMPC_nogroup: 9411 case OMPC_num_tasks: 9412 case OMPC_hint: 9413 case OMPC_dist_schedule: 9414 case OMPC_defaultmap: 9415 case OMPC_unknown: 9416 case OMPC_uniform: 9417 case OMPC_unified_address: 9418 case OMPC_unified_shared_memory: 9419 case OMPC_reverse_offload: 9420 case OMPC_dynamic_allocators: 9421 llvm_unreachable("Clause is not allowed."); 9422 } 9423 return Res; 9424 } 9425 9426 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 9427 ExprObjectKind OK, SourceLocation Loc) { 9428 ExprResult Res = BuildDeclRefExpr( 9429 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 9430 if (!Res.isUsable()) 9431 return ExprError(); 9432 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 9433 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 9434 if (!Res.isUsable()) 9435 return ExprError(); 9436 } 9437 if (VK != VK_LValue && Res.get()->isGLValue()) { 9438 Res = DefaultLvalueConversion(Res.get()); 9439 if (!Res.isUsable()) 9440 return ExprError(); 9441 } 9442 return Res; 9443 } 9444 9445 static std::pair<ValueDecl *, bool> 9446 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 9447 SourceRange &ERange, bool AllowArraySection = false) { 9448 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 9449 RefExpr->containsUnexpandedParameterPack()) 9450 return std::make_pair(nullptr, true); 9451 9452 // OpenMP [3.1, C/C++] 9453 // A list item is a variable name. 9454 // OpenMP [2.9.3.3, Restrictions, p.1] 9455 // A variable that is part of another variable (as an array or 9456 // structure element) cannot appear in a private clause. 9457 RefExpr = RefExpr->IgnoreParens(); 9458 enum { 9459 NoArrayExpr = -1, 9460 ArraySubscript = 0, 9461 OMPArraySection = 1 9462 } IsArrayExpr = NoArrayExpr; 9463 if (AllowArraySection) { 9464 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 9465 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 9466 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 9467 Base = TempASE->getBase()->IgnoreParenImpCasts(); 9468 RefExpr = Base; 9469 IsArrayExpr = ArraySubscript; 9470 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 9471 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 9472 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 9473 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 9474 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 9475 Base = TempASE->getBase()->IgnoreParenImpCasts(); 9476 RefExpr = Base; 9477 IsArrayExpr = OMPArraySection; 9478 } 9479 } 9480 ELoc = RefExpr->getExprLoc(); 9481 ERange = RefExpr->getSourceRange(); 9482 RefExpr = RefExpr->IgnoreParenImpCasts(); 9483 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 9484 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 9485 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 9486 (S.getCurrentThisType().isNull() || !ME || 9487 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 9488 !isa<FieldDecl>(ME->getMemberDecl()))) { 9489 if (IsArrayExpr != NoArrayExpr) { 9490 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 9491 << ERange; 9492 } else { 9493 S.Diag(ELoc, 9494 AllowArraySection 9495 ? diag::err_omp_expected_var_name_member_expr_or_array_item 9496 : diag::err_omp_expected_var_name_member_expr) 9497 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 9498 } 9499 return std::make_pair(nullptr, false); 9500 } 9501 return std::make_pair( 9502 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 9503 } 9504 9505 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 9506 SourceLocation StartLoc, 9507 SourceLocation LParenLoc, 9508 SourceLocation EndLoc) { 9509 SmallVector<Expr *, 8> Vars; 9510 SmallVector<Expr *, 8> PrivateCopies; 9511 for (Expr *RefExpr : VarList) { 9512 assert(RefExpr && "NULL expr in OpenMP private clause."); 9513 SourceLocation ELoc; 9514 SourceRange ERange; 9515 Expr *SimpleRefExpr = RefExpr; 9516 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 9517 if (Res.second) { 9518 // It will be analyzed later. 9519 Vars.push_back(RefExpr); 9520 PrivateCopies.push_back(nullptr); 9521 } 9522 ValueDecl *D = Res.first; 9523 if (!D) 9524 continue; 9525 9526 QualType Type = D->getType(); 9527 auto *VD = dyn_cast<VarDecl>(D); 9528 9529 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 9530 // A variable that appears in a private clause must not have an incomplete 9531 // type or a reference type. 9532 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 9533 continue; 9534 Type = Type.getNonReferenceType(); 9535 9536 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 9537 // in a Construct] 9538 // Variables with the predetermined data-sharing attributes may not be 9539 // listed in data-sharing attributes clauses, except for the cases 9540 // listed below. For these exceptions only, listing a predetermined 9541 // variable in a data-sharing attribute clause is allowed and overrides 9542 // the variable's predetermined data-sharing attributes. 9543 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 9544 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 9545 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 9546 << getOpenMPClauseName(OMPC_private); 9547 reportOriginalDsa(*this, DSAStack, D, DVar); 9548 continue; 9549 } 9550 9551 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 9552 // Variably modified types are not supported for tasks. 9553 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 9554 isOpenMPTaskingDirective(CurrDir)) { 9555 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 9556 << getOpenMPClauseName(OMPC_private) << Type 9557 << getOpenMPDirectiveName(CurrDir); 9558 bool IsDecl = 9559 !VD || 9560 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 9561 Diag(D->getLocation(), 9562 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 9563 << D; 9564 continue; 9565 } 9566 9567 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 9568 // A list item cannot appear in both a map clause and a data-sharing 9569 // attribute clause on the same construct 9570 if (isOpenMPTargetExecutionDirective(CurrDir)) { 9571 OpenMPClauseKind ConflictKind; 9572 if (DSAStack->checkMappableExprComponentListsForDecl( 9573 VD, /*CurrentRegionOnly=*/true, 9574 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 9575 OpenMPClauseKind WhereFoundClauseKind) -> bool { 9576 ConflictKind = WhereFoundClauseKind; 9577 return true; 9578 })) { 9579 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 9580 << getOpenMPClauseName(OMPC_private) 9581 << getOpenMPClauseName(ConflictKind) 9582 << getOpenMPDirectiveName(CurrDir); 9583 reportOriginalDsa(*this, DSAStack, D, DVar); 9584 continue; 9585 } 9586 } 9587 9588 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 9589 // A variable of class type (or array thereof) that appears in a private 9590 // clause requires an accessible, unambiguous default constructor for the 9591 // class type. 9592 // Generate helper private variable and initialize it with the default 9593 // value. The address of the original variable is replaced by the address of 9594 // the new private variable in CodeGen. This new variable is not added to 9595 // IdResolver, so the code in the OpenMP region uses original variable for 9596 // proper diagnostics. 9597 Type = Type.getUnqualifiedType(); 9598 VarDecl *VDPrivate = 9599 buildVarDecl(*this, ELoc, Type, D->getName(), 9600 D->hasAttrs() ? &D->getAttrs() : nullptr, 9601 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 9602 ActOnUninitializedDecl(VDPrivate); 9603 if (VDPrivate->isInvalidDecl()) 9604 continue; 9605 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 9606 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 9607 9608 DeclRefExpr *Ref = nullptr; 9609 if (!VD && !CurContext->isDependentContext()) 9610 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 9611 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 9612 Vars.push_back((VD || CurContext->isDependentContext()) 9613 ? RefExpr->IgnoreParens() 9614 : Ref); 9615 PrivateCopies.push_back(VDPrivateRefExpr); 9616 } 9617 9618 if (Vars.empty()) 9619 return nullptr; 9620 9621 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 9622 PrivateCopies); 9623 } 9624 9625 namespace { 9626 class DiagsUninitializedSeveretyRAII { 9627 private: 9628 DiagnosticsEngine &Diags; 9629 SourceLocation SavedLoc; 9630 bool IsIgnored = false; 9631 9632 public: 9633 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 9634 bool IsIgnored) 9635 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 9636 if (!IsIgnored) { 9637 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 9638 /*Map*/ diag::Severity::Ignored, Loc); 9639 } 9640 } 9641 ~DiagsUninitializedSeveretyRAII() { 9642 if (!IsIgnored) 9643 Diags.popMappings(SavedLoc); 9644 } 9645 }; 9646 } 9647 9648 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 9649 SourceLocation StartLoc, 9650 SourceLocation LParenLoc, 9651 SourceLocation EndLoc) { 9652 SmallVector<Expr *, 8> Vars; 9653 SmallVector<Expr *, 8> PrivateCopies; 9654 SmallVector<Expr *, 8> Inits; 9655 SmallVector<Decl *, 4> ExprCaptures; 9656 bool IsImplicitClause = 9657 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 9658 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 9659 9660 for (Expr *RefExpr : VarList) { 9661 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 9662 SourceLocation ELoc; 9663 SourceRange ERange; 9664 Expr *SimpleRefExpr = RefExpr; 9665 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 9666 if (Res.second) { 9667 // It will be analyzed later. 9668 Vars.push_back(RefExpr); 9669 PrivateCopies.push_back(nullptr); 9670 Inits.push_back(nullptr); 9671 } 9672 ValueDecl *D = Res.first; 9673 if (!D) 9674 continue; 9675 9676 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 9677 QualType Type = D->getType(); 9678 auto *VD = dyn_cast<VarDecl>(D); 9679 9680 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 9681 // A variable that appears in a private clause must not have an incomplete 9682 // type or a reference type. 9683 if (RequireCompleteType(ELoc, Type, 9684 diag::err_omp_firstprivate_incomplete_type)) 9685 continue; 9686 Type = Type.getNonReferenceType(); 9687 9688 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 9689 // A variable of class type (or array thereof) that appears in a private 9690 // clause requires an accessible, unambiguous copy constructor for the 9691 // class type. 9692 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 9693 9694 // If an implicit firstprivate variable found it was checked already. 9695 DSAStackTy::DSAVarData TopDVar; 9696 if (!IsImplicitClause) { 9697 DSAStackTy::DSAVarData DVar = 9698 DSAStack->getTopDSA(D, /*FromParent=*/false); 9699 TopDVar = DVar; 9700 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 9701 bool IsConstant = ElemType.isConstant(Context); 9702 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 9703 // A list item that specifies a given variable may not appear in more 9704 // than one clause on the same directive, except that a variable may be 9705 // specified in both firstprivate and lastprivate clauses. 9706 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 9707 // A list item may appear in a firstprivate or lastprivate clause but not 9708 // both. 9709 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 9710 (isOpenMPDistributeDirective(CurrDir) || 9711 DVar.CKind != OMPC_lastprivate) && 9712 DVar.RefExpr) { 9713 Diag(ELoc, diag::err_omp_wrong_dsa) 9714 << getOpenMPClauseName(DVar.CKind) 9715 << getOpenMPClauseName(OMPC_firstprivate); 9716 reportOriginalDsa(*this, DSAStack, D, DVar); 9717 continue; 9718 } 9719 9720 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 9721 // in a Construct] 9722 // Variables with the predetermined data-sharing attributes may not be 9723 // listed in data-sharing attributes clauses, except for the cases 9724 // listed below. For these exceptions only, listing a predetermined 9725 // variable in a data-sharing attribute clause is allowed and overrides 9726 // the variable's predetermined data-sharing attributes. 9727 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 9728 // in a Construct, C/C++, p.2] 9729 // Variables with const-qualified type having no mutable member may be 9730 // listed in a firstprivate clause, even if they are static data members. 9731 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 9732 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 9733 Diag(ELoc, diag::err_omp_wrong_dsa) 9734 << getOpenMPClauseName(DVar.CKind) 9735 << getOpenMPClauseName(OMPC_firstprivate); 9736 reportOriginalDsa(*this, DSAStack, D, DVar); 9737 continue; 9738 } 9739 9740 // OpenMP [2.9.3.4, Restrictions, p.2] 9741 // A list item that is private within a parallel region must not appear 9742 // in a firstprivate clause on a worksharing construct if any of the 9743 // worksharing regions arising from the worksharing construct ever bind 9744 // to any of the parallel regions arising from the parallel construct. 9745 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 9746 // A list item that is private within a teams region must not appear in a 9747 // firstprivate clause on a distribute construct if any of the distribute 9748 // regions arising from the distribute construct ever bind to any of the 9749 // teams regions arising from the teams construct. 9750 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 9751 // A list item that appears in a reduction clause of a teams construct 9752 // must not appear in a firstprivate clause on a distribute construct if 9753 // any of the distribute regions arising from the distribute construct 9754 // ever bind to any of the teams regions arising from the teams construct. 9755 if ((isOpenMPWorksharingDirective(CurrDir) || 9756 isOpenMPDistributeDirective(CurrDir)) && 9757 !isOpenMPParallelDirective(CurrDir) && 9758 !isOpenMPTeamsDirective(CurrDir)) { 9759 DVar = DSAStack->getImplicitDSA(D, true); 9760 if (DVar.CKind != OMPC_shared && 9761 (isOpenMPParallelDirective(DVar.DKind) || 9762 isOpenMPTeamsDirective(DVar.DKind) || 9763 DVar.DKind == OMPD_unknown)) { 9764 Diag(ELoc, diag::err_omp_required_access) 9765 << getOpenMPClauseName(OMPC_firstprivate) 9766 << getOpenMPClauseName(OMPC_shared); 9767 reportOriginalDsa(*this, DSAStack, D, DVar); 9768 continue; 9769 } 9770 } 9771 // OpenMP [2.9.3.4, Restrictions, p.3] 9772 // A list item that appears in a reduction clause of a parallel construct 9773 // must not appear in a firstprivate clause on a worksharing or task 9774 // construct if any of the worksharing or task regions arising from the 9775 // worksharing or task construct ever bind to any of the parallel regions 9776 // arising from the parallel construct. 9777 // OpenMP [2.9.3.4, Restrictions, p.4] 9778 // A list item that appears in a reduction clause in worksharing 9779 // construct must not appear in a firstprivate clause in a task construct 9780 // encountered during execution of any of the worksharing regions arising 9781 // from the worksharing construct. 9782 if (isOpenMPTaskingDirective(CurrDir)) { 9783 DVar = DSAStack->hasInnermostDSA( 9784 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 9785 [](OpenMPDirectiveKind K) { 9786 return isOpenMPParallelDirective(K) || 9787 isOpenMPWorksharingDirective(K) || 9788 isOpenMPTeamsDirective(K); 9789 }, 9790 /*FromParent=*/true); 9791 if (DVar.CKind == OMPC_reduction && 9792 (isOpenMPParallelDirective(DVar.DKind) || 9793 isOpenMPWorksharingDirective(DVar.DKind) || 9794 isOpenMPTeamsDirective(DVar.DKind))) { 9795 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 9796 << getOpenMPDirectiveName(DVar.DKind); 9797 reportOriginalDsa(*this, DSAStack, D, DVar); 9798 continue; 9799 } 9800 } 9801 9802 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 9803 // A list item cannot appear in both a map clause and a data-sharing 9804 // attribute clause on the same construct 9805 if (isOpenMPTargetExecutionDirective(CurrDir)) { 9806 OpenMPClauseKind ConflictKind; 9807 if (DSAStack->checkMappableExprComponentListsForDecl( 9808 VD, /*CurrentRegionOnly=*/true, 9809 [&ConflictKind]( 9810 OMPClauseMappableExprCommon::MappableExprComponentListRef, 9811 OpenMPClauseKind WhereFoundClauseKind) { 9812 ConflictKind = WhereFoundClauseKind; 9813 return true; 9814 })) { 9815 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 9816 << getOpenMPClauseName(OMPC_firstprivate) 9817 << getOpenMPClauseName(ConflictKind) 9818 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 9819 reportOriginalDsa(*this, DSAStack, D, DVar); 9820 continue; 9821 } 9822 } 9823 } 9824 9825 // Variably modified types are not supported for tasks. 9826 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 9827 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 9828 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 9829 << getOpenMPClauseName(OMPC_firstprivate) << Type 9830 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 9831 bool IsDecl = 9832 !VD || 9833 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 9834 Diag(D->getLocation(), 9835 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 9836 << D; 9837 continue; 9838 } 9839 9840 Type = Type.getUnqualifiedType(); 9841 VarDecl *VDPrivate = 9842 buildVarDecl(*this, ELoc, Type, D->getName(), 9843 D->hasAttrs() ? &D->getAttrs() : nullptr, 9844 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 9845 // Generate helper private variable and initialize it with the value of the 9846 // original variable. The address of the original variable is replaced by 9847 // the address of the new private variable in the CodeGen. This new variable 9848 // is not added to IdResolver, so the code in the OpenMP region uses 9849 // original variable for proper diagnostics and variable capturing. 9850 Expr *VDInitRefExpr = nullptr; 9851 // For arrays generate initializer for single element and replace it by the 9852 // original array element in CodeGen. 9853 if (Type->isArrayType()) { 9854 VarDecl *VDInit = 9855 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 9856 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 9857 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 9858 ElemType = ElemType.getUnqualifiedType(); 9859 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 9860 ".firstprivate.temp"); 9861 InitializedEntity Entity = 9862 InitializedEntity::InitializeVariable(VDInitTemp); 9863 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 9864 9865 InitializationSequence InitSeq(*this, Entity, Kind, Init); 9866 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 9867 if (Result.isInvalid()) 9868 VDPrivate->setInvalidDecl(); 9869 else 9870 VDPrivate->setInit(Result.getAs<Expr>()); 9871 // Remove temp variable declaration. 9872 Context.Deallocate(VDInitTemp); 9873 } else { 9874 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 9875 ".firstprivate.temp"); 9876 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 9877 RefExpr->getExprLoc()); 9878 AddInitializerToDecl(VDPrivate, 9879 DefaultLvalueConversion(VDInitRefExpr).get(), 9880 /*DirectInit=*/false); 9881 } 9882 if (VDPrivate->isInvalidDecl()) { 9883 if (IsImplicitClause) { 9884 Diag(RefExpr->getExprLoc(), 9885 diag::note_omp_task_predetermined_firstprivate_here); 9886 } 9887 continue; 9888 } 9889 CurContext->addDecl(VDPrivate); 9890 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 9891 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 9892 RefExpr->getExprLoc()); 9893 DeclRefExpr *Ref = nullptr; 9894 if (!VD && !CurContext->isDependentContext()) { 9895 if (TopDVar.CKind == OMPC_lastprivate) { 9896 Ref = TopDVar.PrivateCopy; 9897 } else { 9898 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 9899 if (!isOpenMPCapturedDecl(D)) 9900 ExprCaptures.push_back(Ref->getDecl()); 9901 } 9902 } 9903 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 9904 Vars.push_back((VD || CurContext->isDependentContext()) 9905 ? RefExpr->IgnoreParens() 9906 : Ref); 9907 PrivateCopies.push_back(VDPrivateRefExpr); 9908 Inits.push_back(VDInitRefExpr); 9909 } 9910 9911 if (Vars.empty()) 9912 return nullptr; 9913 9914 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 9915 Vars, PrivateCopies, Inits, 9916 buildPreInits(Context, ExprCaptures)); 9917 } 9918 9919 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList, 9920 SourceLocation StartLoc, 9921 SourceLocation LParenLoc, 9922 SourceLocation EndLoc) { 9923 SmallVector<Expr *, 8> Vars; 9924 SmallVector<Expr *, 8> SrcExprs; 9925 SmallVector<Expr *, 8> DstExprs; 9926 SmallVector<Expr *, 8> AssignmentOps; 9927 SmallVector<Decl *, 4> ExprCaptures; 9928 SmallVector<Expr *, 4> ExprPostUpdates; 9929 for (Expr *RefExpr : VarList) { 9930 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 9931 SourceLocation ELoc; 9932 SourceRange ERange; 9933 Expr *SimpleRefExpr = RefExpr; 9934 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 9935 if (Res.second) { 9936 // It will be analyzed later. 9937 Vars.push_back(RefExpr); 9938 SrcExprs.push_back(nullptr); 9939 DstExprs.push_back(nullptr); 9940 AssignmentOps.push_back(nullptr); 9941 } 9942 ValueDecl *D = Res.first; 9943 if (!D) 9944 continue; 9945 9946 QualType Type = D->getType(); 9947 auto *VD = dyn_cast<VarDecl>(D); 9948 9949 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 9950 // A variable that appears in a lastprivate clause must not have an 9951 // incomplete type or a reference type. 9952 if (RequireCompleteType(ELoc, Type, 9953 diag::err_omp_lastprivate_incomplete_type)) 9954 continue; 9955 Type = Type.getNonReferenceType(); 9956 9957 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 9958 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 9959 // in a Construct] 9960 // Variables with the predetermined data-sharing attributes may not be 9961 // listed in data-sharing attributes clauses, except for the cases 9962 // listed below. 9963 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 9964 // A list item may appear in a firstprivate or lastprivate clause but not 9965 // both. 9966 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 9967 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 9968 (isOpenMPDistributeDirective(CurrDir) || 9969 DVar.CKind != OMPC_firstprivate) && 9970 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 9971 Diag(ELoc, diag::err_omp_wrong_dsa) 9972 << getOpenMPClauseName(DVar.CKind) 9973 << getOpenMPClauseName(OMPC_lastprivate); 9974 reportOriginalDsa(*this, DSAStack, D, DVar); 9975 continue; 9976 } 9977 9978 // OpenMP [2.14.3.5, Restrictions, p.2] 9979 // A list item that is private within a parallel region, or that appears in 9980 // the reduction clause of a parallel construct, must not appear in a 9981 // lastprivate clause on a worksharing construct if any of the corresponding 9982 // worksharing regions ever binds to any of the corresponding parallel 9983 // regions. 9984 DSAStackTy::DSAVarData TopDVar = DVar; 9985 if (isOpenMPWorksharingDirective(CurrDir) && 9986 !isOpenMPParallelDirective(CurrDir) && 9987 !isOpenMPTeamsDirective(CurrDir)) { 9988 DVar = DSAStack->getImplicitDSA(D, true); 9989 if (DVar.CKind != OMPC_shared) { 9990 Diag(ELoc, diag::err_omp_required_access) 9991 << getOpenMPClauseName(OMPC_lastprivate) 9992 << getOpenMPClauseName(OMPC_shared); 9993 reportOriginalDsa(*this, DSAStack, D, DVar); 9994 continue; 9995 } 9996 } 9997 9998 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 9999 // A variable of class type (or array thereof) that appears in a 10000 // lastprivate clause requires an accessible, unambiguous default 10001 // constructor for the class type, unless the list item is also specified 10002 // in a firstprivate clause. 10003 // A variable of class type (or array thereof) that appears in a 10004 // lastprivate clause requires an accessible, unambiguous copy assignment 10005 // operator for the class type. 10006 Type = Context.getBaseElementType(Type).getNonReferenceType(); 10007 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 10008 Type.getUnqualifiedType(), ".lastprivate.src", 10009 D->hasAttrs() ? &D->getAttrs() : nullptr); 10010 DeclRefExpr *PseudoSrcExpr = 10011 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 10012 VarDecl *DstVD = 10013 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 10014 D->hasAttrs() ? &D->getAttrs() : nullptr); 10015 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 10016 // For arrays generate assignment operation for single element and replace 10017 // it by the original array element in CodeGen. 10018 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 10019 PseudoDstExpr, PseudoSrcExpr); 10020 if (AssignmentOp.isInvalid()) 10021 continue; 10022 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc, 10023 /*DiscardedValue=*/true); 10024 if (AssignmentOp.isInvalid()) 10025 continue; 10026 10027 DeclRefExpr *Ref = nullptr; 10028 if (!VD && !CurContext->isDependentContext()) { 10029 if (TopDVar.CKind == OMPC_firstprivate) { 10030 Ref = TopDVar.PrivateCopy; 10031 } else { 10032 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 10033 if (!isOpenMPCapturedDecl(D)) 10034 ExprCaptures.push_back(Ref->getDecl()); 10035 } 10036 if (TopDVar.CKind == OMPC_firstprivate || 10037 (!isOpenMPCapturedDecl(D) && 10038 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 10039 ExprResult RefRes = DefaultLvalueConversion(Ref); 10040 if (!RefRes.isUsable()) 10041 continue; 10042 ExprResult PostUpdateRes = 10043 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 10044 RefRes.get()); 10045 if (!PostUpdateRes.isUsable()) 10046 continue; 10047 ExprPostUpdates.push_back( 10048 IgnoredValueConversions(PostUpdateRes.get()).get()); 10049 } 10050 } 10051 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 10052 Vars.push_back((VD || CurContext->isDependentContext()) 10053 ? RefExpr->IgnoreParens() 10054 : Ref); 10055 SrcExprs.push_back(PseudoSrcExpr); 10056 DstExprs.push_back(PseudoDstExpr); 10057 AssignmentOps.push_back(AssignmentOp.get()); 10058 } 10059 10060 if (Vars.empty()) 10061 return nullptr; 10062 10063 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 10064 Vars, SrcExprs, DstExprs, AssignmentOps, 10065 buildPreInits(Context, ExprCaptures), 10066 buildPostUpdate(*this, ExprPostUpdates)); 10067 } 10068 10069 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 10070 SourceLocation StartLoc, 10071 SourceLocation LParenLoc, 10072 SourceLocation EndLoc) { 10073 SmallVector<Expr *, 8> Vars; 10074 for (Expr *RefExpr : VarList) { 10075 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 10076 SourceLocation ELoc; 10077 SourceRange ERange; 10078 Expr *SimpleRefExpr = RefExpr; 10079 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 10080 if (Res.second) { 10081 // It will be analyzed later. 10082 Vars.push_back(RefExpr); 10083 } 10084 ValueDecl *D = Res.first; 10085 if (!D) 10086 continue; 10087 10088 auto *VD = dyn_cast<VarDecl>(D); 10089 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 10090 // in a Construct] 10091 // Variables with the predetermined data-sharing attributes may not be 10092 // listed in data-sharing attributes clauses, except for the cases 10093 // listed below. For these exceptions only, listing a predetermined 10094 // variable in a data-sharing attribute clause is allowed and overrides 10095 // the variable's predetermined data-sharing attributes. 10096 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 10097 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 10098 DVar.RefExpr) { 10099 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 10100 << getOpenMPClauseName(OMPC_shared); 10101 reportOriginalDsa(*this, DSAStack, D, DVar); 10102 continue; 10103 } 10104 10105 DeclRefExpr *Ref = nullptr; 10106 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 10107 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 10108 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 10109 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 10110 ? RefExpr->IgnoreParens() 10111 : Ref); 10112 } 10113 10114 if (Vars.empty()) 10115 return nullptr; 10116 10117 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 10118 } 10119 10120 namespace { 10121 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 10122 DSAStackTy *Stack; 10123 10124 public: 10125 bool VisitDeclRefExpr(DeclRefExpr *E) { 10126 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 10127 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 10128 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 10129 return false; 10130 if (DVar.CKind != OMPC_unknown) 10131 return true; 10132 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 10133 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; }, 10134 /*FromParent=*/true); 10135 return DVarPrivate.CKind != OMPC_unknown; 10136 } 10137 return false; 10138 } 10139 bool VisitStmt(Stmt *S) { 10140 for (Stmt *Child : S->children()) { 10141 if (Child && Visit(Child)) 10142 return true; 10143 } 10144 return false; 10145 } 10146 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 10147 }; 10148 } // namespace 10149 10150 namespace { 10151 // Transform MemberExpression for specified FieldDecl of current class to 10152 // DeclRefExpr to specified OMPCapturedExprDecl. 10153 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 10154 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 10155 ValueDecl *Field = nullptr; 10156 DeclRefExpr *CapturedExpr = nullptr; 10157 10158 public: 10159 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 10160 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 10161 10162 ExprResult TransformMemberExpr(MemberExpr *E) { 10163 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 10164 E->getMemberDecl() == Field) { 10165 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 10166 return CapturedExpr; 10167 } 10168 return BaseTransform::TransformMemberExpr(E); 10169 } 10170 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 10171 }; 10172 } // namespace 10173 10174 template <typename T, typename U> 10175 static T filterLookupForUDR(SmallVectorImpl<U> &Lookups, 10176 const llvm::function_ref<T(ValueDecl *)> Gen) { 10177 for (U &Set : Lookups) { 10178 for (auto *D : Set) { 10179 if (T Res = Gen(cast<ValueDecl>(D))) 10180 return Res; 10181 } 10182 } 10183 return T(); 10184 } 10185 10186 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 10187 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 10188 10189 for (auto RD : D->redecls()) { 10190 // Don't bother with extra checks if we already know this one isn't visible. 10191 if (RD == D) 10192 continue; 10193 10194 auto ND = cast<NamedDecl>(RD); 10195 if (LookupResult::isVisible(SemaRef, ND)) 10196 return ND; 10197 } 10198 10199 return nullptr; 10200 } 10201 10202 static void 10203 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &ReductionId, 10204 SourceLocation Loc, QualType Ty, 10205 SmallVectorImpl<UnresolvedSet<8>> &Lookups) { 10206 // Find all of the associated namespaces and classes based on the 10207 // arguments we have. 10208 Sema::AssociatedNamespaceSet AssociatedNamespaces; 10209 Sema::AssociatedClassSet AssociatedClasses; 10210 OpaqueValueExpr OVE(Loc, Ty, VK_LValue); 10211 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces, 10212 AssociatedClasses); 10213 10214 // C++ [basic.lookup.argdep]p3: 10215 // Let X be the lookup set produced by unqualified lookup (3.4.1) 10216 // and let Y be the lookup set produced by argument dependent 10217 // lookup (defined as follows). If X contains [...] then Y is 10218 // empty. Otherwise Y is the set of declarations found in the 10219 // namespaces associated with the argument types as described 10220 // below. The set of declarations found by the lookup of the name 10221 // is the union of X and Y. 10222 // 10223 // Here, we compute Y and add its members to the overloaded 10224 // candidate set. 10225 for (auto *NS : AssociatedNamespaces) { 10226 // When considering an associated namespace, the lookup is the 10227 // same as the lookup performed when the associated namespace is 10228 // used as a qualifier (3.4.3.2) except that: 10229 // 10230 // -- Any using-directives in the associated namespace are 10231 // ignored. 10232 // 10233 // -- Any namespace-scope friend functions declared in 10234 // associated classes are visible within their respective 10235 // namespaces even if they are not visible during an ordinary 10236 // lookup (11.4). 10237 DeclContext::lookup_result R = NS->lookup(ReductionId.getName()); 10238 for (auto *D : R) { 10239 auto *Underlying = D; 10240 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 10241 Underlying = USD->getTargetDecl(); 10242 10243 if (!isa<OMPDeclareReductionDecl>(Underlying)) 10244 continue; 10245 10246 if (!SemaRef.isVisible(D)) { 10247 D = findAcceptableDecl(SemaRef, D); 10248 if (!D) 10249 continue; 10250 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 10251 Underlying = USD->getTargetDecl(); 10252 } 10253 Lookups.emplace_back(); 10254 Lookups.back().addDecl(Underlying); 10255 } 10256 } 10257 } 10258 10259 static ExprResult 10260 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 10261 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 10262 const DeclarationNameInfo &ReductionId, QualType Ty, 10263 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 10264 if (ReductionIdScopeSpec.isInvalid()) 10265 return ExprError(); 10266 SmallVector<UnresolvedSet<8>, 4> Lookups; 10267 if (S) { 10268 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 10269 Lookup.suppressDiagnostics(); 10270 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 10271 NamedDecl *D = Lookup.getRepresentativeDecl(); 10272 do { 10273 S = S->getParent(); 10274 } while (S && !S->isDeclScope(D)); 10275 if (S) 10276 S = S->getParent(); 10277 Lookups.emplace_back(); 10278 Lookups.back().append(Lookup.begin(), Lookup.end()); 10279 Lookup.clear(); 10280 } 10281 } else if (auto *ULE = 10282 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 10283 Lookups.push_back(UnresolvedSet<8>()); 10284 Decl *PrevD = nullptr; 10285 for (NamedDecl *D : ULE->decls()) { 10286 if (D == PrevD) 10287 Lookups.push_back(UnresolvedSet<8>()); 10288 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D)) 10289 Lookups.back().addDecl(DRD); 10290 PrevD = D; 10291 } 10292 } 10293 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 10294 Ty->isInstantiationDependentType() || 10295 Ty->containsUnexpandedParameterPack() || 10296 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) { 10297 return !D->isInvalidDecl() && 10298 (D->getType()->isDependentType() || 10299 D->getType()->isInstantiationDependentType() || 10300 D->getType()->containsUnexpandedParameterPack()); 10301 })) { 10302 UnresolvedSet<8> ResSet; 10303 for (const UnresolvedSet<8> &Set : Lookups) { 10304 if (Set.empty()) 10305 continue; 10306 ResSet.append(Set.begin(), Set.end()); 10307 // The last item marks the end of all declarations at the specified scope. 10308 ResSet.addDecl(Set[Set.size() - 1]); 10309 } 10310 return UnresolvedLookupExpr::Create( 10311 SemaRef.Context, /*NamingClass=*/nullptr, 10312 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 10313 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 10314 } 10315 // Lookup inside the classes. 10316 // C++ [over.match.oper]p3: 10317 // For a unary operator @ with an operand of a type whose 10318 // cv-unqualified version is T1, and for a binary operator @ with 10319 // a left operand of a type whose cv-unqualified version is T1 and 10320 // a right operand of a type whose cv-unqualified version is T2, 10321 // three sets of candidate functions, designated member 10322 // candidates, non-member candidates and built-in candidates, are 10323 // constructed as follows: 10324 // -- If T1 is a complete class type or a class currently being 10325 // defined, the set of member candidates is the result of the 10326 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, 10327 // the set of member candidates is empty. 10328 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 10329 Lookup.suppressDiagnostics(); 10330 if (const auto *TyRec = Ty->getAs<RecordType>()) { 10331 // Complete the type if it can be completed. 10332 // If the type is neither complete nor being defined, bail out now. 10333 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() || 10334 TyRec->getDecl()->getDefinition()) { 10335 Lookup.clear(); 10336 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl()); 10337 if (Lookup.empty()) { 10338 Lookups.emplace_back(); 10339 Lookups.back().append(Lookup.begin(), Lookup.end()); 10340 } 10341 } 10342 } 10343 // Perform ADL. 10344 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups); 10345 if (auto *VD = filterLookupForUDR<ValueDecl *>( 10346 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 10347 if (!D->isInvalidDecl() && 10348 SemaRef.Context.hasSameType(D->getType(), Ty)) 10349 return D; 10350 return nullptr; 10351 })) 10352 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc); 10353 if (auto *VD = filterLookupForUDR<ValueDecl *>( 10354 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 10355 if (!D->isInvalidDecl() && 10356 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 10357 !Ty.isMoreQualifiedThan(D->getType())) 10358 return D; 10359 return nullptr; 10360 })) { 10361 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 10362 /*DetectVirtual=*/false); 10363 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 10364 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 10365 VD->getType().getUnqualifiedType()))) { 10366 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(), 10367 /*DiagID=*/0) != 10368 Sema::AR_inaccessible) { 10369 SemaRef.BuildBasePathArray(Paths, BasePath); 10370 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc); 10371 } 10372 } 10373 } 10374 } 10375 if (ReductionIdScopeSpec.isSet()) { 10376 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range; 10377 return ExprError(); 10378 } 10379 return ExprEmpty(); 10380 } 10381 10382 namespace { 10383 /// Data for the reduction-based clauses. 10384 struct ReductionData { 10385 /// List of original reduction items. 10386 SmallVector<Expr *, 8> Vars; 10387 /// List of private copies of the reduction items. 10388 SmallVector<Expr *, 8> Privates; 10389 /// LHS expressions for the reduction_op expressions. 10390 SmallVector<Expr *, 8> LHSs; 10391 /// RHS expressions for the reduction_op expressions. 10392 SmallVector<Expr *, 8> RHSs; 10393 /// Reduction operation expression. 10394 SmallVector<Expr *, 8> ReductionOps; 10395 /// Taskgroup descriptors for the corresponding reduction items in 10396 /// in_reduction clauses. 10397 SmallVector<Expr *, 8> TaskgroupDescriptors; 10398 /// List of captures for clause. 10399 SmallVector<Decl *, 4> ExprCaptures; 10400 /// List of postupdate expressions. 10401 SmallVector<Expr *, 4> ExprPostUpdates; 10402 ReductionData() = delete; 10403 /// Reserves required memory for the reduction data. 10404 ReductionData(unsigned Size) { 10405 Vars.reserve(Size); 10406 Privates.reserve(Size); 10407 LHSs.reserve(Size); 10408 RHSs.reserve(Size); 10409 ReductionOps.reserve(Size); 10410 TaskgroupDescriptors.reserve(Size); 10411 ExprCaptures.reserve(Size); 10412 ExprPostUpdates.reserve(Size); 10413 } 10414 /// Stores reduction item and reduction operation only (required for dependent 10415 /// reduction item). 10416 void push(Expr *Item, Expr *ReductionOp) { 10417 Vars.emplace_back(Item); 10418 Privates.emplace_back(nullptr); 10419 LHSs.emplace_back(nullptr); 10420 RHSs.emplace_back(nullptr); 10421 ReductionOps.emplace_back(ReductionOp); 10422 TaskgroupDescriptors.emplace_back(nullptr); 10423 } 10424 /// Stores reduction data. 10425 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 10426 Expr *TaskgroupDescriptor) { 10427 Vars.emplace_back(Item); 10428 Privates.emplace_back(Private); 10429 LHSs.emplace_back(LHS); 10430 RHSs.emplace_back(RHS); 10431 ReductionOps.emplace_back(ReductionOp); 10432 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 10433 } 10434 }; 10435 } // namespace 10436 10437 static bool checkOMPArraySectionConstantForReduction( 10438 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 10439 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 10440 const Expr *Length = OASE->getLength(); 10441 if (Length == nullptr) { 10442 // For array sections of the form [1:] or [:], we would need to analyze 10443 // the lower bound... 10444 if (OASE->getColonLoc().isValid()) 10445 return false; 10446 10447 // This is an array subscript which has implicit length 1! 10448 SingleElement = true; 10449 ArraySizes.push_back(llvm::APSInt::get(1)); 10450 } else { 10451 llvm::APSInt ConstantLengthValue; 10452 if (!Length->EvaluateAsInt(ConstantLengthValue, Context)) 10453 return false; 10454 10455 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 10456 ArraySizes.push_back(ConstantLengthValue); 10457 } 10458 10459 // Get the base of this array section and walk up from there. 10460 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 10461 10462 // We require length = 1 for all array sections except the right-most to 10463 // guarantee that the memory region is contiguous and has no holes in it. 10464 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 10465 Length = TempOASE->getLength(); 10466 if (Length == nullptr) { 10467 // For array sections of the form [1:] or [:], we would need to analyze 10468 // the lower bound... 10469 if (OASE->getColonLoc().isValid()) 10470 return false; 10471 10472 // This is an array subscript which has implicit length 1! 10473 ArraySizes.push_back(llvm::APSInt::get(1)); 10474 } else { 10475 llvm::APSInt ConstantLengthValue; 10476 if (!Length->EvaluateAsInt(ConstantLengthValue, Context) || 10477 ConstantLengthValue.getSExtValue() != 1) 10478 return false; 10479 10480 ArraySizes.push_back(ConstantLengthValue); 10481 } 10482 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 10483 } 10484 10485 // If we have a single element, we don't need to add the implicit lengths. 10486 if (!SingleElement) { 10487 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 10488 // Has implicit length 1! 10489 ArraySizes.push_back(llvm::APSInt::get(1)); 10490 Base = TempASE->getBase()->IgnoreParenImpCasts(); 10491 } 10492 } 10493 10494 // This array section can be privatized as a single value or as a constant 10495 // sized array. 10496 return true; 10497 } 10498 10499 static bool actOnOMPReductionKindClause( 10500 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 10501 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 10502 SourceLocation ColonLoc, SourceLocation EndLoc, 10503 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 10504 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 10505 DeclarationName DN = ReductionId.getName(); 10506 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 10507 BinaryOperatorKind BOK = BO_Comma; 10508 10509 ASTContext &Context = S.Context; 10510 // OpenMP [2.14.3.6, reduction clause] 10511 // C 10512 // reduction-identifier is either an identifier or one of the following 10513 // operators: +, -, *, &, |, ^, && and || 10514 // C++ 10515 // reduction-identifier is either an id-expression or one of the following 10516 // operators: +, -, *, &, |, ^, && and || 10517 switch (OOK) { 10518 case OO_Plus: 10519 case OO_Minus: 10520 BOK = BO_Add; 10521 break; 10522 case OO_Star: 10523 BOK = BO_Mul; 10524 break; 10525 case OO_Amp: 10526 BOK = BO_And; 10527 break; 10528 case OO_Pipe: 10529 BOK = BO_Or; 10530 break; 10531 case OO_Caret: 10532 BOK = BO_Xor; 10533 break; 10534 case OO_AmpAmp: 10535 BOK = BO_LAnd; 10536 break; 10537 case OO_PipePipe: 10538 BOK = BO_LOr; 10539 break; 10540 case OO_New: 10541 case OO_Delete: 10542 case OO_Array_New: 10543 case OO_Array_Delete: 10544 case OO_Slash: 10545 case OO_Percent: 10546 case OO_Tilde: 10547 case OO_Exclaim: 10548 case OO_Equal: 10549 case OO_Less: 10550 case OO_Greater: 10551 case OO_LessEqual: 10552 case OO_GreaterEqual: 10553 case OO_PlusEqual: 10554 case OO_MinusEqual: 10555 case OO_StarEqual: 10556 case OO_SlashEqual: 10557 case OO_PercentEqual: 10558 case OO_CaretEqual: 10559 case OO_AmpEqual: 10560 case OO_PipeEqual: 10561 case OO_LessLess: 10562 case OO_GreaterGreater: 10563 case OO_LessLessEqual: 10564 case OO_GreaterGreaterEqual: 10565 case OO_EqualEqual: 10566 case OO_ExclaimEqual: 10567 case OO_Spaceship: 10568 case OO_PlusPlus: 10569 case OO_MinusMinus: 10570 case OO_Comma: 10571 case OO_ArrowStar: 10572 case OO_Arrow: 10573 case OO_Call: 10574 case OO_Subscript: 10575 case OO_Conditional: 10576 case OO_Coawait: 10577 case NUM_OVERLOADED_OPERATORS: 10578 llvm_unreachable("Unexpected reduction identifier"); 10579 case OO_None: 10580 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 10581 if (II->isStr("max")) 10582 BOK = BO_GT; 10583 else if (II->isStr("min")) 10584 BOK = BO_LT; 10585 } 10586 break; 10587 } 10588 SourceRange ReductionIdRange; 10589 if (ReductionIdScopeSpec.isValid()) 10590 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 10591 else 10592 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 10593 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 10594 10595 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 10596 bool FirstIter = true; 10597 for (Expr *RefExpr : VarList) { 10598 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 10599 // OpenMP [2.1, C/C++] 10600 // A list item is a variable or array section, subject to the restrictions 10601 // specified in Section 2.4 on page 42 and in each of the sections 10602 // describing clauses and directives for which a list appears. 10603 // OpenMP [2.14.3.3, Restrictions, p.1] 10604 // A variable that is part of another variable (as an array or 10605 // structure element) cannot appear in a private clause. 10606 if (!FirstIter && IR != ER) 10607 ++IR; 10608 FirstIter = false; 10609 SourceLocation ELoc; 10610 SourceRange ERange; 10611 Expr *SimpleRefExpr = RefExpr; 10612 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 10613 /*AllowArraySection=*/true); 10614 if (Res.second) { 10615 // Try to find 'declare reduction' corresponding construct before using 10616 // builtin/overloaded operators. 10617 QualType Type = Context.DependentTy; 10618 CXXCastPath BasePath; 10619 ExprResult DeclareReductionRef = buildDeclareReductionRef( 10620 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 10621 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 10622 Expr *ReductionOp = nullptr; 10623 if (S.CurContext->isDependentContext() && 10624 (DeclareReductionRef.isUnset() || 10625 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 10626 ReductionOp = DeclareReductionRef.get(); 10627 // It will be analyzed later. 10628 RD.push(RefExpr, ReductionOp); 10629 } 10630 ValueDecl *D = Res.first; 10631 if (!D) 10632 continue; 10633 10634 Expr *TaskgroupDescriptor = nullptr; 10635 QualType Type; 10636 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 10637 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 10638 if (ASE) { 10639 Type = ASE->getType().getNonReferenceType(); 10640 } else if (OASE) { 10641 QualType BaseType = 10642 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 10643 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 10644 Type = ATy->getElementType(); 10645 else 10646 Type = BaseType->getPointeeType(); 10647 Type = Type.getNonReferenceType(); 10648 } else { 10649 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 10650 } 10651 auto *VD = dyn_cast<VarDecl>(D); 10652 10653 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 10654 // A variable that appears in a private clause must not have an incomplete 10655 // type or a reference type. 10656 if (S.RequireCompleteType(ELoc, D->getType(), 10657 diag::err_omp_reduction_incomplete_type)) 10658 continue; 10659 // OpenMP [2.14.3.6, reduction clause, Restrictions] 10660 // A list item that appears in a reduction clause must not be 10661 // const-qualified. 10662 if (Type.getNonReferenceType().isConstant(Context)) { 10663 S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange; 10664 if (!ASE && !OASE) { 10665 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 10666 VarDecl::DeclarationOnly; 10667 S.Diag(D->getLocation(), 10668 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10669 << D; 10670 } 10671 continue; 10672 } 10673 10674 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 10675 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 10676 // If a list-item is a reference type then it must bind to the same object 10677 // for all threads of the team. 10678 if (!ASE && !OASE) { 10679 if (VD) { 10680 VarDecl *VDDef = VD->getDefinition(); 10681 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 10682 DSARefChecker Check(Stack); 10683 if (Check.Visit(VDDef->getInit())) { 10684 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 10685 << getOpenMPClauseName(ClauseKind) << ERange; 10686 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 10687 continue; 10688 } 10689 } 10690 } 10691 10692 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 10693 // in a Construct] 10694 // Variables with the predetermined data-sharing attributes may not be 10695 // listed in data-sharing attributes clauses, except for the cases 10696 // listed below. For these exceptions only, listing a predetermined 10697 // variable in a data-sharing attribute clause is allowed and overrides 10698 // the variable's predetermined data-sharing attributes. 10699 // OpenMP [2.14.3.6, Restrictions, p.3] 10700 // Any number of reduction clauses can be specified on the directive, 10701 // but a list item can appear only once in the reduction clauses for that 10702 // directive. 10703 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 10704 if (DVar.CKind == OMPC_reduction) { 10705 S.Diag(ELoc, diag::err_omp_once_referenced) 10706 << getOpenMPClauseName(ClauseKind); 10707 if (DVar.RefExpr) 10708 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 10709 continue; 10710 } 10711 if (DVar.CKind != OMPC_unknown) { 10712 S.Diag(ELoc, diag::err_omp_wrong_dsa) 10713 << getOpenMPClauseName(DVar.CKind) 10714 << getOpenMPClauseName(OMPC_reduction); 10715 reportOriginalDsa(S, Stack, D, DVar); 10716 continue; 10717 } 10718 10719 // OpenMP [2.14.3.6, Restrictions, p.1] 10720 // A list item that appears in a reduction clause of a worksharing 10721 // construct must be shared in the parallel regions to which any of the 10722 // worksharing regions arising from the worksharing construct bind. 10723 if (isOpenMPWorksharingDirective(CurrDir) && 10724 !isOpenMPParallelDirective(CurrDir) && 10725 !isOpenMPTeamsDirective(CurrDir)) { 10726 DVar = Stack->getImplicitDSA(D, true); 10727 if (DVar.CKind != OMPC_shared) { 10728 S.Diag(ELoc, diag::err_omp_required_access) 10729 << getOpenMPClauseName(OMPC_reduction) 10730 << getOpenMPClauseName(OMPC_shared); 10731 reportOriginalDsa(S, Stack, D, DVar); 10732 continue; 10733 } 10734 } 10735 } 10736 10737 // Try to find 'declare reduction' corresponding construct before using 10738 // builtin/overloaded operators. 10739 CXXCastPath BasePath; 10740 ExprResult DeclareReductionRef = buildDeclareReductionRef( 10741 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 10742 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 10743 if (DeclareReductionRef.isInvalid()) 10744 continue; 10745 if (S.CurContext->isDependentContext() && 10746 (DeclareReductionRef.isUnset() || 10747 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 10748 RD.push(RefExpr, DeclareReductionRef.get()); 10749 continue; 10750 } 10751 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 10752 // Not allowed reduction identifier is found. 10753 S.Diag(ReductionId.getBeginLoc(), 10754 diag::err_omp_unknown_reduction_identifier) 10755 << Type << ReductionIdRange; 10756 continue; 10757 } 10758 10759 // OpenMP [2.14.3.6, reduction clause, Restrictions] 10760 // The type of a list item that appears in a reduction clause must be valid 10761 // for the reduction-identifier. For a max or min reduction in C, the type 10762 // of the list item must be an allowed arithmetic data type: char, int, 10763 // float, double, or _Bool, possibly modified with long, short, signed, or 10764 // unsigned. For a max or min reduction in C++, the type of the list item 10765 // must be an allowed arithmetic data type: char, wchar_t, int, float, 10766 // double, or bool, possibly modified with long, short, signed, or unsigned. 10767 if (DeclareReductionRef.isUnset()) { 10768 if ((BOK == BO_GT || BOK == BO_LT) && 10769 !(Type->isScalarType() || 10770 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 10771 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 10772 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 10773 if (!ASE && !OASE) { 10774 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 10775 VarDecl::DeclarationOnly; 10776 S.Diag(D->getLocation(), 10777 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10778 << D; 10779 } 10780 continue; 10781 } 10782 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 10783 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 10784 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 10785 << getOpenMPClauseName(ClauseKind); 10786 if (!ASE && !OASE) { 10787 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 10788 VarDecl::DeclarationOnly; 10789 S.Diag(D->getLocation(), 10790 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10791 << D; 10792 } 10793 continue; 10794 } 10795 } 10796 10797 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 10798 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 10799 D->hasAttrs() ? &D->getAttrs() : nullptr); 10800 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 10801 D->hasAttrs() ? &D->getAttrs() : nullptr); 10802 QualType PrivateTy = Type; 10803 10804 // Try if we can determine constant lengths for all array sections and avoid 10805 // the VLA. 10806 bool ConstantLengthOASE = false; 10807 if (OASE) { 10808 bool SingleElement; 10809 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 10810 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 10811 Context, OASE, SingleElement, ArraySizes); 10812 10813 // If we don't have a single element, we must emit a constant array type. 10814 if (ConstantLengthOASE && !SingleElement) { 10815 for (llvm::APSInt &Size : ArraySizes) 10816 PrivateTy = Context.getConstantArrayType( 10817 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0); 10818 } 10819 } 10820 10821 if ((OASE && !ConstantLengthOASE) || 10822 (!OASE && !ASE && 10823 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 10824 if (!Context.getTargetInfo().isVLASupported() && 10825 S.shouldDiagnoseTargetSupportFromOpenMP()) { 10826 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 10827 S.Diag(ELoc, diag::note_vla_unsupported); 10828 continue; 10829 } 10830 // For arrays/array sections only: 10831 // Create pseudo array type for private copy. The size for this array will 10832 // be generated during codegen. 10833 // For array subscripts or single variables Private Ty is the same as Type 10834 // (type of the variable or single array element). 10835 PrivateTy = Context.getVariableArrayType( 10836 Type, 10837 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue), 10838 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 10839 } else if (!ASE && !OASE && 10840 Context.getAsArrayType(D->getType().getNonReferenceType())) { 10841 PrivateTy = D->getType().getNonReferenceType(); 10842 } 10843 // Private copy. 10844 VarDecl *PrivateVD = 10845 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 10846 D->hasAttrs() ? &D->getAttrs() : nullptr, 10847 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 10848 // Add initializer for private variable. 10849 Expr *Init = nullptr; 10850 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 10851 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 10852 if (DeclareReductionRef.isUsable()) { 10853 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 10854 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 10855 if (DRD->getInitializer()) { 10856 Init = DRDRef; 10857 RHSVD->setInit(DRDRef); 10858 RHSVD->setInitStyle(VarDecl::CallInit); 10859 } 10860 } else { 10861 switch (BOK) { 10862 case BO_Add: 10863 case BO_Xor: 10864 case BO_Or: 10865 case BO_LOr: 10866 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 10867 if (Type->isScalarType() || Type->isAnyComplexType()) 10868 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 10869 break; 10870 case BO_Mul: 10871 case BO_LAnd: 10872 if (Type->isScalarType() || Type->isAnyComplexType()) { 10873 // '*' and '&&' reduction ops - initializer is '1'. 10874 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 10875 } 10876 break; 10877 case BO_And: { 10878 // '&' reduction op - initializer is '~0'. 10879 QualType OrigType = Type; 10880 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 10881 Type = ComplexTy->getElementType(); 10882 if (Type->isRealFloatingType()) { 10883 llvm::APFloat InitValue = 10884 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type), 10885 /*isIEEE=*/true); 10886 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 10887 Type, ELoc); 10888 } else if (Type->isScalarType()) { 10889 uint64_t Size = Context.getTypeSize(Type); 10890 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 10891 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 10892 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 10893 } 10894 if (Init && OrigType->isAnyComplexType()) { 10895 // Init = 0xFFFF + 0xFFFFi; 10896 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 10897 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 10898 } 10899 Type = OrigType; 10900 break; 10901 } 10902 case BO_LT: 10903 case BO_GT: { 10904 // 'min' reduction op - initializer is 'Largest representable number in 10905 // the reduction list item type'. 10906 // 'max' reduction op - initializer is 'Least representable number in 10907 // the reduction list item type'. 10908 if (Type->isIntegerType() || Type->isPointerType()) { 10909 bool IsSigned = Type->hasSignedIntegerRepresentation(); 10910 uint64_t Size = Context.getTypeSize(Type); 10911 QualType IntTy = 10912 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 10913 llvm::APInt InitValue = 10914 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 10915 : llvm::APInt::getMinValue(Size) 10916 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 10917 : llvm::APInt::getMaxValue(Size); 10918 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 10919 if (Type->isPointerType()) { 10920 // Cast to pointer type. 10921 ExprResult CastExpr = S.BuildCStyleCastExpr( 10922 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 10923 if (CastExpr.isInvalid()) 10924 continue; 10925 Init = CastExpr.get(); 10926 } 10927 } else if (Type->isRealFloatingType()) { 10928 llvm::APFloat InitValue = llvm::APFloat::getLargest( 10929 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 10930 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 10931 Type, ELoc); 10932 } 10933 break; 10934 } 10935 case BO_PtrMemD: 10936 case BO_PtrMemI: 10937 case BO_MulAssign: 10938 case BO_Div: 10939 case BO_Rem: 10940 case BO_Sub: 10941 case BO_Shl: 10942 case BO_Shr: 10943 case BO_LE: 10944 case BO_GE: 10945 case BO_EQ: 10946 case BO_NE: 10947 case BO_Cmp: 10948 case BO_AndAssign: 10949 case BO_XorAssign: 10950 case BO_OrAssign: 10951 case BO_Assign: 10952 case BO_AddAssign: 10953 case BO_SubAssign: 10954 case BO_DivAssign: 10955 case BO_RemAssign: 10956 case BO_ShlAssign: 10957 case BO_ShrAssign: 10958 case BO_Comma: 10959 llvm_unreachable("Unexpected reduction operation"); 10960 } 10961 } 10962 if (Init && DeclareReductionRef.isUnset()) 10963 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 10964 else if (!Init) 10965 S.ActOnUninitializedDecl(RHSVD); 10966 if (RHSVD->isInvalidDecl()) 10967 continue; 10968 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) { 10969 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 10970 << Type << ReductionIdRange; 10971 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 10972 VarDecl::DeclarationOnly; 10973 S.Diag(D->getLocation(), 10974 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10975 << D; 10976 continue; 10977 } 10978 // Store initializer for single element in private copy. Will be used during 10979 // codegen. 10980 PrivateVD->setInit(RHSVD->getInit()); 10981 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 10982 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 10983 ExprResult ReductionOp; 10984 if (DeclareReductionRef.isUsable()) { 10985 QualType RedTy = DeclareReductionRef.get()->getType(); 10986 QualType PtrRedTy = Context.getPointerType(RedTy); 10987 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 10988 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 10989 if (!BasePath.empty()) { 10990 LHS = S.DefaultLvalueConversion(LHS.get()); 10991 RHS = S.DefaultLvalueConversion(RHS.get()); 10992 LHS = ImplicitCastExpr::Create(Context, PtrRedTy, 10993 CK_UncheckedDerivedToBase, LHS.get(), 10994 &BasePath, LHS.get()->getValueKind()); 10995 RHS = ImplicitCastExpr::Create(Context, PtrRedTy, 10996 CK_UncheckedDerivedToBase, RHS.get(), 10997 &BasePath, RHS.get()->getValueKind()); 10998 } 10999 FunctionProtoType::ExtProtoInfo EPI; 11000 QualType Params[] = {PtrRedTy, PtrRedTy}; 11001 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 11002 auto *OVE = new (Context) OpaqueValueExpr( 11003 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, 11004 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 11005 Expr *Args[] = {LHS.get(), RHS.get()}; 11006 ReductionOp = new (Context) 11007 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc); 11008 } else { 11009 ReductionOp = S.BuildBinOp( 11010 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE); 11011 if (ReductionOp.isUsable()) { 11012 if (BOK != BO_LT && BOK != BO_GT) { 11013 ReductionOp = 11014 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 11015 BO_Assign, LHSDRE, ReductionOp.get()); 11016 } else { 11017 auto *ConditionalOp = new (Context) 11018 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE, 11019 Type, VK_LValue, OK_Ordinary); 11020 ReductionOp = 11021 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 11022 BO_Assign, LHSDRE, ConditionalOp); 11023 } 11024 if (ReductionOp.isUsable()) 11025 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get()); 11026 } 11027 if (!ReductionOp.isUsable()) 11028 continue; 11029 } 11030 11031 // OpenMP [2.15.4.6, Restrictions, p.2] 11032 // A list item that appears in an in_reduction clause of a task construct 11033 // must appear in a task_reduction clause of a construct associated with a 11034 // taskgroup region that includes the participating task in its taskgroup 11035 // set. The construct associated with the innermost region that meets this 11036 // condition must specify the same reduction-identifier as the in_reduction 11037 // clause. 11038 if (ClauseKind == OMPC_in_reduction) { 11039 SourceRange ParentSR; 11040 BinaryOperatorKind ParentBOK; 11041 const Expr *ParentReductionOp; 11042 Expr *ParentBOKTD, *ParentReductionOpTD; 11043 DSAStackTy::DSAVarData ParentBOKDSA = 11044 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 11045 ParentBOKTD); 11046 DSAStackTy::DSAVarData ParentReductionOpDSA = 11047 Stack->getTopMostTaskgroupReductionData( 11048 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 11049 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 11050 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 11051 if (!IsParentBOK && !IsParentReductionOp) { 11052 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction); 11053 continue; 11054 } 11055 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 11056 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK || 11057 IsParentReductionOp) { 11058 bool EmitError = true; 11059 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 11060 llvm::FoldingSetNodeID RedId, ParentRedId; 11061 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 11062 DeclareReductionRef.get()->Profile(RedId, Context, 11063 /*Canonical=*/true); 11064 EmitError = RedId != ParentRedId; 11065 } 11066 if (EmitError) { 11067 S.Diag(ReductionId.getBeginLoc(), 11068 diag::err_omp_reduction_identifier_mismatch) 11069 << ReductionIdRange << RefExpr->getSourceRange(); 11070 S.Diag(ParentSR.getBegin(), 11071 diag::note_omp_previous_reduction_identifier) 11072 << ParentSR 11073 << (IsParentBOK ? ParentBOKDSA.RefExpr 11074 : ParentReductionOpDSA.RefExpr) 11075 ->getSourceRange(); 11076 continue; 11077 } 11078 } 11079 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 11080 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined."); 11081 } 11082 11083 DeclRefExpr *Ref = nullptr; 11084 Expr *VarsExpr = RefExpr->IgnoreParens(); 11085 if (!VD && !S.CurContext->isDependentContext()) { 11086 if (ASE || OASE) { 11087 TransformExprToCaptures RebuildToCapture(S, D); 11088 VarsExpr = 11089 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 11090 Ref = RebuildToCapture.getCapturedExpr(); 11091 } else { 11092 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 11093 } 11094 if (!S.isOpenMPCapturedDecl(D)) { 11095 RD.ExprCaptures.emplace_back(Ref->getDecl()); 11096 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 11097 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 11098 if (!RefRes.isUsable()) 11099 continue; 11100 ExprResult PostUpdateRes = 11101 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 11102 RefRes.get()); 11103 if (!PostUpdateRes.isUsable()) 11104 continue; 11105 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 11106 Stack->getCurrentDirective() == OMPD_taskgroup) { 11107 S.Diag(RefExpr->getExprLoc(), 11108 diag::err_omp_reduction_non_addressable_expression) 11109 << RefExpr->getSourceRange(); 11110 continue; 11111 } 11112 RD.ExprPostUpdates.emplace_back( 11113 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 11114 } 11115 } 11116 } 11117 // All reduction items are still marked as reduction (to do not increase 11118 // code base size). 11119 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref); 11120 if (CurrDir == OMPD_taskgroup) { 11121 if (DeclareReductionRef.isUsable()) 11122 Stack->addTaskgroupReductionData(D, ReductionIdRange, 11123 DeclareReductionRef.get()); 11124 else 11125 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 11126 } 11127 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 11128 TaskgroupDescriptor); 11129 } 11130 return RD.Vars.empty(); 11131 } 11132 11133 OMPClause *Sema::ActOnOpenMPReductionClause( 11134 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 11135 SourceLocation ColonLoc, SourceLocation EndLoc, 11136 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 11137 ArrayRef<Expr *> UnresolvedReductions) { 11138 ReductionData RD(VarList.size()); 11139 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 11140 StartLoc, LParenLoc, ColonLoc, EndLoc, 11141 ReductionIdScopeSpec, ReductionId, 11142 UnresolvedReductions, RD)) 11143 return nullptr; 11144 11145 return OMPReductionClause::Create( 11146 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 11147 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 11148 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 11149 buildPreInits(Context, RD.ExprCaptures), 11150 buildPostUpdate(*this, RD.ExprPostUpdates)); 11151 } 11152 11153 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 11154 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 11155 SourceLocation ColonLoc, SourceLocation EndLoc, 11156 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 11157 ArrayRef<Expr *> UnresolvedReductions) { 11158 ReductionData RD(VarList.size()); 11159 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 11160 StartLoc, LParenLoc, ColonLoc, EndLoc, 11161 ReductionIdScopeSpec, ReductionId, 11162 UnresolvedReductions, RD)) 11163 return nullptr; 11164 11165 return OMPTaskReductionClause::Create( 11166 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 11167 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 11168 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 11169 buildPreInits(Context, RD.ExprCaptures), 11170 buildPostUpdate(*this, RD.ExprPostUpdates)); 11171 } 11172 11173 OMPClause *Sema::ActOnOpenMPInReductionClause( 11174 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 11175 SourceLocation ColonLoc, SourceLocation EndLoc, 11176 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 11177 ArrayRef<Expr *> UnresolvedReductions) { 11178 ReductionData RD(VarList.size()); 11179 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 11180 StartLoc, LParenLoc, ColonLoc, EndLoc, 11181 ReductionIdScopeSpec, ReductionId, 11182 UnresolvedReductions, RD)) 11183 return nullptr; 11184 11185 return OMPInReductionClause::Create( 11186 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 11187 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 11188 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 11189 buildPreInits(Context, RD.ExprCaptures), 11190 buildPostUpdate(*this, RD.ExprPostUpdates)); 11191 } 11192 11193 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 11194 SourceLocation LinLoc) { 11195 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 11196 LinKind == OMPC_LINEAR_unknown) { 11197 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 11198 return true; 11199 } 11200 return false; 11201 } 11202 11203 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 11204 OpenMPLinearClauseKind LinKind, 11205 QualType Type) { 11206 const auto *VD = dyn_cast_or_null<VarDecl>(D); 11207 // A variable must not have an incomplete type or a reference type. 11208 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 11209 return true; 11210 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 11211 !Type->isReferenceType()) { 11212 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 11213 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 11214 return true; 11215 } 11216 Type = Type.getNonReferenceType(); 11217 11218 // A list item must not be const-qualified. 11219 if (Type.isConstant(Context)) { 11220 Diag(ELoc, diag::err_omp_const_variable) 11221 << getOpenMPClauseName(OMPC_linear); 11222 if (D) { 11223 bool IsDecl = 11224 !VD || 11225 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 11226 Diag(D->getLocation(), 11227 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11228 << D; 11229 } 11230 return true; 11231 } 11232 11233 // A list item must be of integral or pointer type. 11234 Type = Type.getUnqualifiedType().getCanonicalType(); 11235 const auto *Ty = Type.getTypePtrOrNull(); 11236 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && 11237 !Ty->isPointerType())) { 11238 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 11239 if (D) { 11240 bool IsDecl = 11241 !VD || 11242 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 11243 Diag(D->getLocation(), 11244 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11245 << D; 11246 } 11247 return true; 11248 } 11249 return false; 11250 } 11251 11252 OMPClause *Sema::ActOnOpenMPLinearClause( 11253 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 11254 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 11255 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 11256 SmallVector<Expr *, 8> Vars; 11257 SmallVector<Expr *, 8> Privates; 11258 SmallVector<Expr *, 8> Inits; 11259 SmallVector<Decl *, 4> ExprCaptures; 11260 SmallVector<Expr *, 4> ExprPostUpdates; 11261 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 11262 LinKind = OMPC_LINEAR_val; 11263 for (Expr *RefExpr : VarList) { 11264 assert(RefExpr && "NULL expr in OpenMP linear clause."); 11265 SourceLocation ELoc; 11266 SourceRange ERange; 11267 Expr *SimpleRefExpr = RefExpr; 11268 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 11269 if (Res.second) { 11270 // It will be analyzed later. 11271 Vars.push_back(RefExpr); 11272 Privates.push_back(nullptr); 11273 Inits.push_back(nullptr); 11274 } 11275 ValueDecl *D = Res.first; 11276 if (!D) 11277 continue; 11278 11279 QualType Type = D->getType(); 11280 auto *VD = dyn_cast<VarDecl>(D); 11281 11282 // OpenMP [2.14.3.7, linear clause] 11283 // A list-item cannot appear in more than one linear clause. 11284 // A list-item that appears in a linear clause cannot appear in any 11285 // other data-sharing attribute clause. 11286 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 11287 if (DVar.RefExpr) { 11288 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 11289 << getOpenMPClauseName(OMPC_linear); 11290 reportOriginalDsa(*this, DSAStack, D, DVar); 11291 continue; 11292 } 11293 11294 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 11295 continue; 11296 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 11297 11298 // Build private copy of original var. 11299 VarDecl *Private = 11300 buildVarDecl(*this, ELoc, Type, D->getName(), 11301 D->hasAttrs() ? &D->getAttrs() : nullptr, 11302 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 11303 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 11304 // Build var to save initial value. 11305 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 11306 Expr *InitExpr; 11307 DeclRefExpr *Ref = nullptr; 11308 if (!VD && !CurContext->isDependentContext()) { 11309 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 11310 if (!isOpenMPCapturedDecl(D)) { 11311 ExprCaptures.push_back(Ref->getDecl()); 11312 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 11313 ExprResult RefRes = DefaultLvalueConversion(Ref); 11314 if (!RefRes.isUsable()) 11315 continue; 11316 ExprResult PostUpdateRes = 11317 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 11318 SimpleRefExpr, RefRes.get()); 11319 if (!PostUpdateRes.isUsable()) 11320 continue; 11321 ExprPostUpdates.push_back( 11322 IgnoredValueConversions(PostUpdateRes.get()).get()); 11323 } 11324 } 11325 } 11326 if (LinKind == OMPC_LINEAR_uval) 11327 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 11328 else 11329 InitExpr = VD ? SimpleRefExpr : Ref; 11330 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 11331 /*DirectInit=*/false); 11332 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 11333 11334 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 11335 Vars.push_back((VD || CurContext->isDependentContext()) 11336 ? RefExpr->IgnoreParens() 11337 : Ref); 11338 Privates.push_back(PrivateRef); 11339 Inits.push_back(InitRef); 11340 } 11341 11342 if (Vars.empty()) 11343 return nullptr; 11344 11345 Expr *StepExpr = Step; 11346 Expr *CalcStepExpr = nullptr; 11347 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 11348 !Step->isInstantiationDependent() && 11349 !Step->containsUnexpandedParameterPack()) { 11350 SourceLocation StepLoc = Step->getBeginLoc(); 11351 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 11352 if (Val.isInvalid()) 11353 return nullptr; 11354 StepExpr = Val.get(); 11355 11356 // Build var to save the step value. 11357 VarDecl *SaveVar = 11358 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 11359 ExprResult SaveRef = 11360 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 11361 ExprResult CalcStep = 11362 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 11363 CalcStep = ActOnFinishFullExpr(CalcStep.get()); 11364 11365 // Warn about zero linear step (it would be probably better specified as 11366 // making corresponding variables 'const'). 11367 llvm::APSInt Result; 11368 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); 11369 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) 11370 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] 11371 << (Vars.size() > 1); 11372 if (!IsConstant && CalcStep.isUsable()) { 11373 // Calculate the step beforehand instead of doing this on each iteration. 11374 // (This is not used if the number of iterations may be kfold-ed). 11375 CalcStepExpr = CalcStep.get(); 11376 } 11377 } 11378 11379 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 11380 ColonLoc, EndLoc, Vars, Privates, Inits, 11381 StepExpr, CalcStepExpr, 11382 buildPreInits(Context, ExprCaptures), 11383 buildPostUpdate(*this, ExprPostUpdates)); 11384 } 11385 11386 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 11387 Expr *NumIterations, Sema &SemaRef, 11388 Scope *S, DSAStackTy *Stack) { 11389 // Walk the vars and build update/final expressions for the CodeGen. 11390 SmallVector<Expr *, 8> Updates; 11391 SmallVector<Expr *, 8> Finals; 11392 Expr *Step = Clause.getStep(); 11393 Expr *CalcStep = Clause.getCalcStep(); 11394 // OpenMP [2.14.3.7, linear clause] 11395 // If linear-step is not specified it is assumed to be 1. 11396 if (!Step) 11397 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 11398 else if (CalcStep) 11399 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 11400 bool HasErrors = false; 11401 auto CurInit = Clause.inits().begin(); 11402 auto CurPrivate = Clause.privates().begin(); 11403 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 11404 for (Expr *RefExpr : Clause.varlists()) { 11405 SourceLocation ELoc; 11406 SourceRange ERange; 11407 Expr *SimpleRefExpr = RefExpr; 11408 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange); 11409 ValueDecl *D = Res.first; 11410 if (Res.second || !D) { 11411 Updates.push_back(nullptr); 11412 Finals.push_back(nullptr); 11413 HasErrors = true; 11414 continue; 11415 } 11416 auto &&Info = Stack->isLoopControlVariable(D); 11417 // OpenMP [2.15.11, distribute simd Construct] 11418 // A list item may not appear in a linear clause, unless it is the loop 11419 // iteration variable. 11420 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 11421 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 11422 SemaRef.Diag(ELoc, 11423 diag::err_omp_linear_distribute_var_non_loop_iteration); 11424 Updates.push_back(nullptr); 11425 Finals.push_back(nullptr); 11426 HasErrors = true; 11427 continue; 11428 } 11429 Expr *InitExpr = *CurInit; 11430 11431 // Build privatized reference to the current linear var. 11432 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 11433 Expr *CapturedRef; 11434 if (LinKind == OMPC_LINEAR_uval) 11435 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 11436 else 11437 CapturedRef = 11438 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 11439 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 11440 /*RefersToCapture=*/true); 11441 11442 // Build update: Var = InitExpr + IV * Step 11443 ExprResult Update; 11444 if (!Info.first) 11445 Update = 11446 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, 11447 InitExpr, IV, Step, /* Subtract */ false); 11448 else 11449 Update = *CurPrivate; 11450 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(), 11451 /*DiscardedValue=*/true); 11452 11453 // Build final: Var = InitExpr + NumIterations * Step 11454 ExprResult Final; 11455 if (!Info.first) 11456 Final = 11457 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 11458 InitExpr, NumIterations, Step, /*Subtract=*/false); 11459 else 11460 Final = *CurPrivate; 11461 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(), 11462 /*DiscardedValue=*/true); 11463 11464 if (!Update.isUsable() || !Final.isUsable()) { 11465 Updates.push_back(nullptr); 11466 Finals.push_back(nullptr); 11467 HasErrors = true; 11468 } else { 11469 Updates.push_back(Update.get()); 11470 Finals.push_back(Final.get()); 11471 } 11472 ++CurInit; 11473 ++CurPrivate; 11474 } 11475 Clause.setUpdates(Updates); 11476 Clause.setFinals(Finals); 11477 return HasErrors; 11478 } 11479 11480 OMPClause *Sema::ActOnOpenMPAlignedClause( 11481 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 11482 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 11483 SmallVector<Expr *, 8> Vars; 11484 for (Expr *RefExpr : VarList) { 11485 assert(RefExpr && "NULL expr in OpenMP linear clause."); 11486 SourceLocation ELoc; 11487 SourceRange ERange; 11488 Expr *SimpleRefExpr = RefExpr; 11489 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 11490 if (Res.second) { 11491 // It will be analyzed later. 11492 Vars.push_back(RefExpr); 11493 } 11494 ValueDecl *D = Res.first; 11495 if (!D) 11496 continue; 11497 11498 QualType QType = D->getType(); 11499 auto *VD = dyn_cast<VarDecl>(D); 11500 11501 // OpenMP [2.8.1, simd construct, Restrictions] 11502 // The type of list items appearing in the aligned clause must be 11503 // array, pointer, reference to array, or reference to pointer. 11504 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 11505 const Type *Ty = QType.getTypePtrOrNull(); 11506 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 11507 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 11508 << QType << getLangOpts().CPlusPlus << ERange; 11509 bool IsDecl = 11510 !VD || 11511 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 11512 Diag(D->getLocation(), 11513 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11514 << D; 11515 continue; 11516 } 11517 11518 // OpenMP [2.8.1, simd construct, Restrictions] 11519 // A list-item cannot appear in more than one aligned clause. 11520 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 11521 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange; 11522 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 11523 << getOpenMPClauseName(OMPC_aligned); 11524 continue; 11525 } 11526 11527 DeclRefExpr *Ref = nullptr; 11528 if (!VD && isOpenMPCapturedDecl(D)) 11529 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 11530 Vars.push_back(DefaultFunctionArrayConversion( 11531 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 11532 .get()); 11533 } 11534 11535 // OpenMP [2.8.1, simd construct, Description] 11536 // The parameter of the aligned clause, alignment, must be a constant 11537 // positive integer expression. 11538 // If no optional parameter is specified, implementation-defined default 11539 // alignments for SIMD instructions on the target platforms are assumed. 11540 if (Alignment != nullptr) { 11541 ExprResult AlignResult = 11542 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 11543 if (AlignResult.isInvalid()) 11544 return nullptr; 11545 Alignment = AlignResult.get(); 11546 } 11547 if (Vars.empty()) 11548 return nullptr; 11549 11550 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 11551 EndLoc, Vars, Alignment); 11552 } 11553 11554 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 11555 SourceLocation StartLoc, 11556 SourceLocation LParenLoc, 11557 SourceLocation EndLoc) { 11558 SmallVector<Expr *, 8> Vars; 11559 SmallVector<Expr *, 8> SrcExprs; 11560 SmallVector<Expr *, 8> DstExprs; 11561 SmallVector<Expr *, 8> AssignmentOps; 11562 for (Expr *RefExpr : VarList) { 11563 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 11564 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 11565 // It will be analyzed later. 11566 Vars.push_back(RefExpr); 11567 SrcExprs.push_back(nullptr); 11568 DstExprs.push_back(nullptr); 11569 AssignmentOps.push_back(nullptr); 11570 continue; 11571 } 11572 11573 SourceLocation ELoc = RefExpr->getExprLoc(); 11574 // OpenMP [2.1, C/C++] 11575 // A list item is a variable name. 11576 // OpenMP [2.14.4.1, Restrictions, p.1] 11577 // A list item that appears in a copyin clause must be threadprivate. 11578 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 11579 if (!DE || !isa<VarDecl>(DE->getDecl())) { 11580 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 11581 << 0 << RefExpr->getSourceRange(); 11582 continue; 11583 } 11584 11585 Decl *D = DE->getDecl(); 11586 auto *VD = cast<VarDecl>(D); 11587 11588 QualType Type = VD->getType(); 11589 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 11590 // It will be analyzed later. 11591 Vars.push_back(DE); 11592 SrcExprs.push_back(nullptr); 11593 DstExprs.push_back(nullptr); 11594 AssignmentOps.push_back(nullptr); 11595 continue; 11596 } 11597 11598 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 11599 // A list item that appears in a copyin clause must be threadprivate. 11600 if (!DSAStack->isThreadPrivate(VD)) { 11601 Diag(ELoc, diag::err_omp_required_access) 11602 << getOpenMPClauseName(OMPC_copyin) 11603 << getOpenMPDirectiveName(OMPD_threadprivate); 11604 continue; 11605 } 11606 11607 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 11608 // A variable of class type (or array thereof) that appears in a 11609 // copyin clause requires an accessible, unambiguous copy assignment 11610 // operator for the class type. 11611 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 11612 VarDecl *SrcVD = 11613 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(), 11614 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 11615 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 11616 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 11617 VarDecl *DstVD = 11618 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst", 11619 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 11620 DeclRefExpr *PseudoDstExpr = 11621 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 11622 // For arrays generate assignment operation for single element and replace 11623 // it by the original array element in CodeGen. 11624 ExprResult AssignmentOp = 11625 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 11626 PseudoSrcExpr); 11627 if (AssignmentOp.isInvalid()) 11628 continue; 11629 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 11630 /*DiscardedValue=*/true); 11631 if (AssignmentOp.isInvalid()) 11632 continue; 11633 11634 DSAStack->addDSA(VD, DE, OMPC_copyin); 11635 Vars.push_back(DE); 11636 SrcExprs.push_back(PseudoSrcExpr); 11637 DstExprs.push_back(PseudoDstExpr); 11638 AssignmentOps.push_back(AssignmentOp.get()); 11639 } 11640 11641 if (Vars.empty()) 11642 return nullptr; 11643 11644 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 11645 SrcExprs, DstExprs, AssignmentOps); 11646 } 11647 11648 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 11649 SourceLocation StartLoc, 11650 SourceLocation LParenLoc, 11651 SourceLocation EndLoc) { 11652 SmallVector<Expr *, 8> Vars; 11653 SmallVector<Expr *, 8> SrcExprs; 11654 SmallVector<Expr *, 8> DstExprs; 11655 SmallVector<Expr *, 8> AssignmentOps; 11656 for (Expr *RefExpr : VarList) { 11657 assert(RefExpr && "NULL expr in OpenMP linear clause."); 11658 SourceLocation ELoc; 11659 SourceRange ERange; 11660 Expr *SimpleRefExpr = RefExpr; 11661 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 11662 if (Res.second) { 11663 // It will be analyzed later. 11664 Vars.push_back(RefExpr); 11665 SrcExprs.push_back(nullptr); 11666 DstExprs.push_back(nullptr); 11667 AssignmentOps.push_back(nullptr); 11668 } 11669 ValueDecl *D = Res.first; 11670 if (!D) 11671 continue; 11672 11673 QualType Type = D->getType(); 11674 auto *VD = dyn_cast<VarDecl>(D); 11675 11676 // OpenMP [2.14.4.2, Restrictions, p.2] 11677 // A list item that appears in a copyprivate clause may not appear in a 11678 // private or firstprivate clause on the single construct. 11679 if (!VD || !DSAStack->isThreadPrivate(VD)) { 11680 DSAStackTy::DSAVarData DVar = 11681 DSAStack->getTopDSA(D, /*FromParent=*/false); 11682 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 11683 DVar.RefExpr) { 11684 Diag(ELoc, diag::err_omp_wrong_dsa) 11685 << getOpenMPClauseName(DVar.CKind) 11686 << getOpenMPClauseName(OMPC_copyprivate); 11687 reportOriginalDsa(*this, DSAStack, D, DVar); 11688 continue; 11689 } 11690 11691 // OpenMP [2.11.4.2, Restrictions, p.1] 11692 // All list items that appear in a copyprivate clause must be either 11693 // threadprivate or private in the enclosing context. 11694 if (DVar.CKind == OMPC_unknown) { 11695 DVar = DSAStack->getImplicitDSA(D, false); 11696 if (DVar.CKind == OMPC_shared) { 11697 Diag(ELoc, diag::err_omp_required_access) 11698 << getOpenMPClauseName(OMPC_copyprivate) 11699 << "threadprivate or private in the enclosing context"; 11700 reportOriginalDsa(*this, DSAStack, D, DVar); 11701 continue; 11702 } 11703 } 11704 } 11705 11706 // Variably modified types are not supported. 11707 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 11708 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 11709 << getOpenMPClauseName(OMPC_copyprivate) << Type 11710 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 11711 bool IsDecl = 11712 !VD || 11713 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 11714 Diag(D->getLocation(), 11715 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11716 << D; 11717 continue; 11718 } 11719 11720 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 11721 // A variable of class type (or array thereof) that appears in a 11722 // copyin clause requires an accessible, unambiguous copy assignment 11723 // operator for the class type. 11724 Type = Context.getBaseElementType(Type.getNonReferenceType()) 11725 .getUnqualifiedType(); 11726 VarDecl *SrcVD = 11727 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src", 11728 D->hasAttrs() ? &D->getAttrs() : nullptr); 11729 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 11730 VarDecl *DstVD = 11731 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst", 11732 D->hasAttrs() ? &D->getAttrs() : nullptr); 11733 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 11734 ExprResult AssignmentOp = BuildBinOp( 11735 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 11736 if (AssignmentOp.isInvalid()) 11737 continue; 11738 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc, 11739 /*DiscardedValue=*/true); 11740 if (AssignmentOp.isInvalid()) 11741 continue; 11742 11743 // No need to mark vars as copyprivate, they are already threadprivate or 11744 // implicitly private. 11745 assert(VD || isOpenMPCapturedDecl(D)); 11746 Vars.push_back( 11747 VD ? RefExpr->IgnoreParens() 11748 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 11749 SrcExprs.push_back(PseudoSrcExpr); 11750 DstExprs.push_back(PseudoDstExpr); 11751 AssignmentOps.push_back(AssignmentOp.get()); 11752 } 11753 11754 if (Vars.empty()) 11755 return nullptr; 11756 11757 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 11758 Vars, SrcExprs, DstExprs, AssignmentOps); 11759 } 11760 11761 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 11762 SourceLocation StartLoc, 11763 SourceLocation LParenLoc, 11764 SourceLocation EndLoc) { 11765 if (VarList.empty()) 11766 return nullptr; 11767 11768 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 11769 } 11770 11771 OMPClause * 11772 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind, 11773 SourceLocation DepLoc, SourceLocation ColonLoc, 11774 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 11775 SourceLocation LParenLoc, SourceLocation EndLoc) { 11776 if (DSAStack->getCurrentDirective() == OMPD_ordered && 11777 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 11778 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 11779 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 11780 return nullptr; 11781 } 11782 if (DSAStack->getCurrentDirective() != OMPD_ordered && 11783 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 11784 DepKind == OMPC_DEPEND_sink)) { 11785 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink}; 11786 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 11787 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 11788 /*Last=*/OMPC_DEPEND_unknown, Except) 11789 << getOpenMPClauseName(OMPC_depend); 11790 return nullptr; 11791 } 11792 SmallVector<Expr *, 8> Vars; 11793 DSAStackTy::OperatorOffsetTy OpsOffs; 11794 llvm::APSInt DepCounter(/*BitWidth=*/32); 11795 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 11796 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) { 11797 if (const Expr *OrderedCountExpr = 11798 DSAStack->getParentOrderedRegionParam().first) { 11799 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 11800 TotalDepCount.setIsUnsigned(/*Val=*/true); 11801 } 11802 } 11803 for (Expr *RefExpr : VarList) { 11804 assert(RefExpr && "NULL expr in OpenMP shared clause."); 11805 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 11806 // It will be analyzed later. 11807 Vars.push_back(RefExpr); 11808 continue; 11809 } 11810 11811 SourceLocation ELoc = RefExpr->getExprLoc(); 11812 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 11813 if (DepKind == OMPC_DEPEND_sink) { 11814 if (DSAStack->getParentOrderedRegionParam().first && 11815 DepCounter >= TotalDepCount) { 11816 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 11817 continue; 11818 } 11819 ++DepCounter; 11820 // OpenMP [2.13.9, Summary] 11821 // depend(dependence-type : vec), where dependence-type is: 11822 // 'sink' and where vec is the iteration vector, which has the form: 11823 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 11824 // where n is the value specified by the ordered clause in the loop 11825 // directive, xi denotes the loop iteration variable of the i-th nested 11826 // loop associated with the loop directive, and di is a constant 11827 // non-negative integer. 11828 if (CurContext->isDependentContext()) { 11829 // It will be analyzed later. 11830 Vars.push_back(RefExpr); 11831 continue; 11832 } 11833 SimpleExpr = SimpleExpr->IgnoreImplicit(); 11834 OverloadedOperatorKind OOK = OO_None; 11835 SourceLocation OOLoc; 11836 Expr *LHS = SimpleExpr; 11837 Expr *RHS = nullptr; 11838 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 11839 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 11840 OOLoc = BO->getOperatorLoc(); 11841 LHS = BO->getLHS()->IgnoreParenImpCasts(); 11842 RHS = BO->getRHS()->IgnoreParenImpCasts(); 11843 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 11844 OOK = OCE->getOperator(); 11845 OOLoc = OCE->getOperatorLoc(); 11846 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 11847 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 11848 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 11849 OOK = MCE->getMethodDecl() 11850 ->getNameInfo() 11851 .getName() 11852 .getCXXOverloadedOperator(); 11853 OOLoc = MCE->getCallee()->getExprLoc(); 11854 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 11855 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 11856 } 11857 SourceLocation ELoc; 11858 SourceRange ERange; 11859 auto Res = getPrivateItem(*this, LHS, ELoc, ERange); 11860 if (Res.second) { 11861 // It will be analyzed later. 11862 Vars.push_back(RefExpr); 11863 } 11864 ValueDecl *D = Res.first; 11865 if (!D) 11866 continue; 11867 11868 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 11869 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 11870 continue; 11871 } 11872 if (RHS) { 11873 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 11874 RHS, OMPC_depend, /*StrictlyPositive=*/false); 11875 if (RHSRes.isInvalid()) 11876 continue; 11877 } 11878 if (!CurContext->isDependentContext() && 11879 DSAStack->getParentOrderedRegionParam().first && 11880 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 11881 const ValueDecl *VD = 11882 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 11883 if (VD) 11884 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 11885 << 1 << VD; 11886 else 11887 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 11888 continue; 11889 } 11890 OpsOffs.emplace_back(RHS, OOK); 11891 } else { 11892 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 11893 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 11894 (ASE && 11895 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() && 11896 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 11897 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 11898 << RefExpr->getSourceRange(); 11899 continue; 11900 } 11901 bool Suppress = getDiagnostics().getSuppressAllDiagnostics(); 11902 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 11903 ExprResult Res = 11904 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts()); 11905 getDiagnostics().setSuppressAllDiagnostics(Suppress); 11906 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) { 11907 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 11908 << RefExpr->getSourceRange(); 11909 continue; 11910 } 11911 } 11912 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 11913 } 11914 11915 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 11916 TotalDepCount > VarList.size() && 11917 DSAStack->getParentOrderedRegionParam().first && 11918 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 11919 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 11920 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 11921 } 11922 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 11923 Vars.empty()) 11924 return nullptr; 11925 11926 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 11927 DepKind, DepLoc, ColonLoc, Vars, 11928 TotalDepCount.getZExtValue()); 11929 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 11930 DSAStack->isParentOrderedRegion()) 11931 DSAStack->addDoacrossDependClause(C, OpsOffs); 11932 return C; 11933 } 11934 11935 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc, 11936 SourceLocation LParenLoc, 11937 SourceLocation EndLoc) { 11938 Expr *ValExpr = Device; 11939 Stmt *HelperValStmt = nullptr; 11940 11941 // OpenMP [2.9.1, Restrictions] 11942 // The device expression must evaluate to a non-negative integer value. 11943 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 11944 /*StrictlyPositive=*/false)) 11945 return nullptr; 11946 11947 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 11948 OpenMPDirectiveKind CaptureRegion = 11949 getOpenMPCaptureRegionForClause(DKind, OMPC_device); 11950 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 11951 ValExpr = MakeFullExpr(ValExpr).get(); 11952 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 11953 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 11954 HelperValStmt = buildPreInits(Context, Captures); 11955 } 11956 11957 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion, 11958 StartLoc, LParenLoc, EndLoc); 11959 } 11960 11961 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 11962 DSAStackTy *Stack, QualType QTy, 11963 bool FullCheck = true) { 11964 NamedDecl *ND; 11965 if (QTy->isIncompleteType(&ND)) { 11966 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 11967 return false; 11968 } 11969 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 11970 !QTy.isTrivialType(SemaRef.Context)) 11971 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 11972 return true; 11973 } 11974 11975 /// Return true if it can be proven that the provided array expression 11976 /// (array section or array subscript) does NOT specify the whole size of the 11977 /// array whose base type is \a BaseQTy. 11978 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 11979 const Expr *E, 11980 QualType BaseQTy) { 11981 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 11982 11983 // If this is an array subscript, it refers to the whole size if the size of 11984 // the dimension is constant and equals 1. Also, an array section assumes the 11985 // format of an array subscript if no colon is used. 11986 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) { 11987 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 11988 return ATy->getSize().getSExtValue() != 1; 11989 // Size can't be evaluated statically. 11990 return false; 11991 } 11992 11993 assert(OASE && "Expecting array section if not an array subscript."); 11994 const Expr *LowerBound = OASE->getLowerBound(); 11995 const Expr *Length = OASE->getLength(); 11996 11997 // If there is a lower bound that does not evaluates to zero, we are not 11998 // covering the whole dimension. 11999 if (LowerBound) { 12000 llvm::APSInt ConstLowerBound; 12001 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext())) 12002 return false; // Can't get the integer value as a constant. 12003 if (ConstLowerBound.getSExtValue()) 12004 return true; 12005 } 12006 12007 // If we don't have a length we covering the whole dimension. 12008 if (!Length) 12009 return false; 12010 12011 // If the base is a pointer, we don't have a way to get the size of the 12012 // pointee. 12013 if (BaseQTy->isPointerType()) 12014 return false; 12015 12016 // We can only check if the length is the same as the size of the dimension 12017 // if we have a constant array. 12018 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 12019 if (!CATy) 12020 return false; 12021 12022 llvm::APSInt ConstLength; 12023 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext())) 12024 return false; // Can't get the integer value as a constant. 12025 12026 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 12027 } 12028 12029 // Return true if it can be proven that the provided array expression (array 12030 // section or array subscript) does NOT specify a single element of the array 12031 // whose base type is \a BaseQTy. 12032 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 12033 const Expr *E, 12034 QualType BaseQTy) { 12035 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 12036 12037 // An array subscript always refer to a single element. Also, an array section 12038 // assumes the format of an array subscript if no colon is used. 12039 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) 12040 return false; 12041 12042 assert(OASE && "Expecting array section if not an array subscript."); 12043 const Expr *Length = OASE->getLength(); 12044 12045 // If we don't have a length we have to check if the array has unitary size 12046 // for this dimension. Also, we should always expect a length if the base type 12047 // is pointer. 12048 if (!Length) { 12049 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 12050 return ATy->getSize().getSExtValue() != 1; 12051 // We cannot assume anything. 12052 return false; 12053 } 12054 12055 // Check if the length evaluates to 1. 12056 llvm::APSInt ConstLength; 12057 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext())) 12058 return false; // Can't get the integer value as a constant. 12059 12060 return ConstLength.getSExtValue() != 1; 12061 } 12062 12063 // Return the expression of the base of the mappable expression or null if it 12064 // cannot be determined and do all the necessary checks to see if the expression 12065 // is valid as a standalone mappable expression. In the process, record all the 12066 // components of the expression. 12067 static const Expr *checkMapClauseExpressionBase( 12068 Sema &SemaRef, Expr *E, 12069 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 12070 OpenMPClauseKind CKind, bool NoDiagnose) { 12071 SourceLocation ELoc = E->getExprLoc(); 12072 SourceRange ERange = E->getSourceRange(); 12073 12074 // The base of elements of list in a map clause have to be either: 12075 // - a reference to variable or field. 12076 // - a member expression. 12077 // - an array expression. 12078 // 12079 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 12080 // reference to 'r'. 12081 // 12082 // If we have: 12083 // 12084 // struct SS { 12085 // Bla S; 12086 // foo() { 12087 // #pragma omp target map (S.Arr[:12]); 12088 // } 12089 // } 12090 // 12091 // We want to retrieve the member expression 'this->S'; 12092 12093 const Expr *RelevantExpr = nullptr; 12094 12095 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2] 12096 // If a list item is an array section, it must specify contiguous storage. 12097 // 12098 // For this restriction it is sufficient that we make sure only references 12099 // to variables or fields and array expressions, and that no array sections 12100 // exist except in the rightmost expression (unless they cover the whole 12101 // dimension of the array). E.g. these would be invalid: 12102 // 12103 // r.ArrS[3:5].Arr[6:7] 12104 // 12105 // r.ArrS[3:5].x 12106 // 12107 // but these would be valid: 12108 // r.ArrS[3].Arr[6:7] 12109 // 12110 // r.ArrS[3].x 12111 12112 bool AllowUnitySizeArraySection = true; 12113 bool AllowWholeSizeArraySection = true; 12114 12115 while (!RelevantExpr) { 12116 E = E->IgnoreParenImpCasts(); 12117 12118 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) { 12119 if (!isa<VarDecl>(CurE->getDecl())) 12120 return nullptr; 12121 12122 RelevantExpr = CurE; 12123 12124 // If we got a reference to a declaration, we should not expect any array 12125 // section before that. 12126 AllowUnitySizeArraySection = false; 12127 AllowWholeSizeArraySection = false; 12128 12129 // Record the component. 12130 CurComponents.emplace_back(CurE, CurE->getDecl()); 12131 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) { 12132 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts(); 12133 12134 if (isa<CXXThisExpr>(BaseE)) 12135 // We found a base expression: this->Val. 12136 RelevantExpr = CurE; 12137 else 12138 E = BaseE; 12139 12140 if (!isa<FieldDecl>(CurE->getMemberDecl())) { 12141 if (!NoDiagnose) { 12142 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 12143 << CurE->getSourceRange(); 12144 return nullptr; 12145 } 12146 if (RelevantExpr) 12147 return nullptr; 12148 continue; 12149 } 12150 12151 auto *FD = cast<FieldDecl>(CurE->getMemberDecl()); 12152 12153 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 12154 // A bit-field cannot appear in a map clause. 12155 // 12156 if (FD->isBitField()) { 12157 if (!NoDiagnose) { 12158 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 12159 << CurE->getSourceRange() << getOpenMPClauseName(CKind); 12160 return nullptr; 12161 } 12162 if (RelevantExpr) 12163 return nullptr; 12164 continue; 12165 } 12166 12167 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 12168 // If the type of a list item is a reference to a type T then the type 12169 // will be considered to be T for all purposes of this clause. 12170 QualType CurType = BaseE->getType().getNonReferenceType(); 12171 12172 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 12173 // A list item cannot be a variable that is a member of a structure with 12174 // a union type. 12175 // 12176 if (CurType->isUnionType()) { 12177 if (!NoDiagnose) { 12178 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 12179 << CurE->getSourceRange(); 12180 return nullptr; 12181 } 12182 continue; 12183 } 12184 12185 // If we got a member expression, we should not expect any array section 12186 // before that: 12187 // 12188 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 12189 // If a list item is an element of a structure, only the rightmost symbol 12190 // of the variable reference can be an array section. 12191 // 12192 AllowUnitySizeArraySection = false; 12193 AllowWholeSizeArraySection = false; 12194 12195 // Record the component. 12196 CurComponents.emplace_back(CurE, FD); 12197 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) { 12198 E = CurE->getBase()->IgnoreParenImpCasts(); 12199 12200 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 12201 if (!NoDiagnose) { 12202 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 12203 << 0 << CurE->getSourceRange(); 12204 return nullptr; 12205 } 12206 continue; 12207 } 12208 12209 // If we got an array subscript that express the whole dimension we 12210 // can have any array expressions before. If it only expressing part of 12211 // the dimension, we can only have unitary-size array expressions. 12212 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, 12213 E->getType())) 12214 AllowWholeSizeArraySection = false; 12215 12216 // Record the component - we don't have any declaration associated. 12217 CurComponents.emplace_back(CurE, nullptr); 12218 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) { 12219 assert(!NoDiagnose && "Array sections cannot be implicitly mapped."); 12220 E = CurE->getBase()->IgnoreParenImpCasts(); 12221 12222 QualType CurType = 12223 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 12224 12225 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 12226 // If the type of a list item is a reference to a type T then the type 12227 // will be considered to be T for all purposes of this clause. 12228 if (CurType->isReferenceType()) 12229 CurType = CurType->getPointeeType(); 12230 12231 bool IsPointer = CurType->isAnyPointerType(); 12232 12233 if (!IsPointer && !CurType->isArrayType()) { 12234 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 12235 << 0 << CurE->getSourceRange(); 12236 return nullptr; 12237 } 12238 12239 bool NotWhole = 12240 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType); 12241 bool NotUnity = 12242 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType); 12243 12244 if (AllowWholeSizeArraySection) { 12245 // Any array section is currently allowed. Allowing a whole size array 12246 // section implies allowing a unity array section as well. 12247 // 12248 // If this array section refers to the whole dimension we can still 12249 // accept other array sections before this one, except if the base is a 12250 // pointer. Otherwise, only unitary sections are accepted. 12251 if (NotWhole || IsPointer) 12252 AllowWholeSizeArraySection = false; 12253 } else if (AllowUnitySizeArraySection && NotUnity) { 12254 // A unity or whole array section is not allowed and that is not 12255 // compatible with the properties of the current array section. 12256 SemaRef.Diag( 12257 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 12258 << CurE->getSourceRange(); 12259 return nullptr; 12260 } 12261 12262 // Record the component - we don't have any declaration associated. 12263 CurComponents.emplace_back(CurE, nullptr); 12264 } else { 12265 if (!NoDiagnose) { 12266 // If nothing else worked, this is not a valid map clause expression. 12267 SemaRef.Diag( 12268 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 12269 << ERange; 12270 } 12271 return nullptr; 12272 } 12273 } 12274 12275 return RelevantExpr; 12276 } 12277 12278 // Return true if expression E associated with value VD has conflicts with other 12279 // map information. 12280 static bool checkMapConflicts( 12281 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 12282 bool CurrentRegionOnly, 12283 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 12284 OpenMPClauseKind CKind) { 12285 assert(VD && E); 12286 SourceLocation ELoc = E->getExprLoc(); 12287 SourceRange ERange = E->getSourceRange(); 12288 12289 // In order to easily check the conflicts we need to match each component of 12290 // the expression under test with the components of the expressions that are 12291 // already in the stack. 12292 12293 assert(!CurComponents.empty() && "Map clause expression with no components!"); 12294 assert(CurComponents.back().getAssociatedDeclaration() == VD && 12295 "Map clause expression with unexpected base!"); 12296 12297 // Variables to help detecting enclosing problems in data environment nests. 12298 bool IsEnclosedByDataEnvironmentExpr = false; 12299 const Expr *EnclosingExpr = nullptr; 12300 12301 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 12302 VD, CurrentRegionOnly, 12303 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 12304 ERange, CKind, &EnclosingExpr, 12305 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 12306 StackComponents, 12307 OpenMPClauseKind) { 12308 assert(!StackComponents.empty() && 12309 "Map clause expression with no components!"); 12310 assert(StackComponents.back().getAssociatedDeclaration() == VD && 12311 "Map clause expression with unexpected base!"); 12312 (void)VD; 12313 12314 // The whole expression in the stack. 12315 const Expr *RE = StackComponents.front().getAssociatedExpression(); 12316 12317 // Expressions must start from the same base. Here we detect at which 12318 // point both expressions diverge from each other and see if we can 12319 // detect if the memory referred to both expressions is contiguous and 12320 // do not overlap. 12321 auto CI = CurComponents.rbegin(); 12322 auto CE = CurComponents.rend(); 12323 auto SI = StackComponents.rbegin(); 12324 auto SE = StackComponents.rend(); 12325 for (; CI != CE && SI != SE; ++CI, ++SI) { 12326 12327 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 12328 // At most one list item can be an array item derived from a given 12329 // variable in map clauses of the same construct. 12330 if (CurrentRegionOnly && 12331 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 12332 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) && 12333 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 12334 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) { 12335 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 12336 diag::err_omp_multiple_array_items_in_map_clause) 12337 << CI->getAssociatedExpression()->getSourceRange(); 12338 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 12339 diag::note_used_here) 12340 << SI->getAssociatedExpression()->getSourceRange(); 12341 return true; 12342 } 12343 12344 // Do both expressions have the same kind? 12345 if (CI->getAssociatedExpression()->getStmtClass() != 12346 SI->getAssociatedExpression()->getStmtClass()) 12347 break; 12348 12349 // Are we dealing with different variables/fields? 12350 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 12351 break; 12352 } 12353 // Check if the extra components of the expressions in the enclosing 12354 // data environment are redundant for the current base declaration. 12355 // If they are, the maps completely overlap, which is legal. 12356 for (; SI != SE; ++SI) { 12357 QualType Type; 12358 if (const auto *ASE = 12359 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 12360 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 12361 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 12362 SI->getAssociatedExpression())) { 12363 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 12364 Type = 12365 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 12366 } 12367 if (Type.isNull() || Type->isAnyPointerType() || 12368 checkArrayExpressionDoesNotReferToWholeSize( 12369 SemaRef, SI->getAssociatedExpression(), Type)) 12370 break; 12371 } 12372 12373 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 12374 // List items of map clauses in the same construct must not share 12375 // original storage. 12376 // 12377 // If the expressions are exactly the same or one is a subset of the 12378 // other, it means they are sharing storage. 12379 if (CI == CE && SI == SE) { 12380 if (CurrentRegionOnly) { 12381 if (CKind == OMPC_map) { 12382 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 12383 } else { 12384 assert(CKind == OMPC_to || CKind == OMPC_from); 12385 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 12386 << ERange; 12387 } 12388 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12389 << RE->getSourceRange(); 12390 return true; 12391 } 12392 // If we find the same expression in the enclosing data environment, 12393 // that is legal. 12394 IsEnclosedByDataEnvironmentExpr = true; 12395 return false; 12396 } 12397 12398 QualType DerivedType = 12399 std::prev(CI)->getAssociatedDeclaration()->getType(); 12400 SourceLocation DerivedLoc = 12401 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 12402 12403 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 12404 // If the type of a list item is a reference to a type T then the type 12405 // will be considered to be T for all purposes of this clause. 12406 DerivedType = DerivedType.getNonReferenceType(); 12407 12408 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 12409 // A variable for which the type is pointer and an array section 12410 // derived from that variable must not appear as list items of map 12411 // clauses of the same construct. 12412 // 12413 // Also, cover one of the cases in: 12414 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 12415 // If any part of the original storage of a list item has corresponding 12416 // storage in the device data environment, all of the original storage 12417 // must have corresponding storage in the device data environment. 12418 // 12419 if (DerivedType->isAnyPointerType()) { 12420 if (CI == CE || SI == SE) { 12421 SemaRef.Diag( 12422 DerivedLoc, 12423 diag::err_omp_pointer_mapped_along_with_derived_section) 12424 << DerivedLoc; 12425 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12426 << RE->getSourceRange(); 12427 return true; 12428 } 12429 if (CI->getAssociatedExpression()->getStmtClass() != 12430 SI->getAssociatedExpression()->getStmtClass() || 12431 CI->getAssociatedDeclaration()->getCanonicalDecl() == 12432 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 12433 assert(CI != CE && SI != SE); 12434 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 12435 << DerivedLoc; 12436 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12437 << RE->getSourceRange(); 12438 return true; 12439 } 12440 } 12441 12442 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 12443 // List items of map clauses in the same construct must not share 12444 // original storage. 12445 // 12446 // An expression is a subset of the other. 12447 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 12448 if (CKind == OMPC_map) { 12449 if (CI != CE || SI != SE) { 12450 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is 12451 // a pointer. 12452 auto Begin = 12453 CI != CE ? CurComponents.begin() : StackComponents.begin(); 12454 auto End = CI != CE ? CurComponents.end() : StackComponents.end(); 12455 auto It = Begin; 12456 while (It != End && !It->getAssociatedDeclaration()) 12457 std::advance(It, 1); 12458 assert(It != End && 12459 "Expected at least one component with the declaration."); 12460 if (It != Begin && It->getAssociatedDeclaration() 12461 ->getType() 12462 .getCanonicalType() 12463 ->isAnyPointerType()) { 12464 IsEnclosedByDataEnvironmentExpr = false; 12465 EnclosingExpr = nullptr; 12466 return false; 12467 } 12468 } 12469 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 12470 } else { 12471 assert(CKind == OMPC_to || CKind == OMPC_from); 12472 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 12473 << ERange; 12474 } 12475 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12476 << RE->getSourceRange(); 12477 return true; 12478 } 12479 12480 // The current expression uses the same base as other expression in the 12481 // data environment but does not contain it completely. 12482 if (!CurrentRegionOnly && SI != SE) 12483 EnclosingExpr = RE; 12484 12485 // The current expression is a subset of the expression in the data 12486 // environment. 12487 IsEnclosedByDataEnvironmentExpr |= 12488 (!CurrentRegionOnly && CI != CE && SI == SE); 12489 12490 return false; 12491 }); 12492 12493 if (CurrentRegionOnly) 12494 return FoundError; 12495 12496 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 12497 // If any part of the original storage of a list item has corresponding 12498 // storage in the device data environment, all of the original storage must 12499 // have corresponding storage in the device data environment. 12500 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 12501 // If a list item is an element of a structure, and a different element of 12502 // the structure has a corresponding list item in the device data environment 12503 // prior to a task encountering the construct associated with the map clause, 12504 // then the list item must also have a corresponding list item in the device 12505 // data environment prior to the task encountering the construct. 12506 // 12507 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 12508 SemaRef.Diag(ELoc, 12509 diag::err_omp_original_storage_is_shared_and_does_not_contain) 12510 << ERange; 12511 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 12512 << EnclosingExpr->getSourceRange(); 12513 return true; 12514 } 12515 12516 return FoundError; 12517 } 12518 12519 namespace { 12520 // Utility struct that gathers all the related lists associated with a mappable 12521 // expression. 12522 struct MappableVarListInfo { 12523 // The list of expressions. 12524 ArrayRef<Expr *> VarList; 12525 // The list of processed expressions. 12526 SmallVector<Expr *, 16> ProcessedVarList; 12527 // The mappble components for each expression. 12528 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 12529 // The base declaration of the variable. 12530 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 12531 12532 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 12533 // We have a list of components and base declarations for each entry in the 12534 // variable list. 12535 VarComponents.reserve(VarList.size()); 12536 VarBaseDeclarations.reserve(VarList.size()); 12537 } 12538 }; 12539 } 12540 12541 // Check the validity of the provided variable list for the provided clause kind 12542 // \a CKind. In the check process the valid expressions, and mappable expression 12543 // components and variables are extracted and used to fill \a Vars, 12544 // \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and 12545 // \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'. 12546 static void 12547 checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS, 12548 OpenMPClauseKind CKind, MappableVarListInfo &MVLI, 12549 SourceLocation StartLoc, 12550 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 12551 bool IsMapTypeImplicit = false) { 12552 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 12553 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 12554 "Unexpected clause kind with mappable expressions!"); 12555 12556 // Keep track of the mappable components and base declarations in this clause. 12557 // Each entry in the list is going to have a list of components associated. We 12558 // record each set of the components so that we can build the clause later on. 12559 // In the end we should have the same amount of declarations and component 12560 // lists. 12561 12562 for (Expr *RE : MVLI.VarList) { 12563 assert(RE && "Null expr in omp to/from/map clause"); 12564 SourceLocation ELoc = RE->getExprLoc(); 12565 12566 const Expr *VE = RE->IgnoreParenLValueCasts(); 12567 12568 if (VE->isValueDependent() || VE->isTypeDependent() || 12569 VE->isInstantiationDependent() || 12570 VE->containsUnexpandedParameterPack()) { 12571 // We can only analyze this information once the missing information is 12572 // resolved. 12573 MVLI.ProcessedVarList.push_back(RE); 12574 continue; 12575 } 12576 12577 Expr *SimpleExpr = RE->IgnoreParenCasts(); 12578 12579 if (!RE->IgnoreParenImpCasts()->isLValue()) { 12580 SemaRef.Diag(ELoc, 12581 diag::err_omp_expected_named_var_member_or_array_expression) 12582 << RE->getSourceRange(); 12583 continue; 12584 } 12585 12586 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 12587 ValueDecl *CurDeclaration = nullptr; 12588 12589 // Obtain the array or member expression bases if required. Also, fill the 12590 // components array with all the components identified in the process. 12591 const Expr *BE = checkMapClauseExpressionBase( 12592 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false); 12593 if (!BE) 12594 continue; 12595 12596 assert(!CurComponents.empty() && 12597 "Invalid mappable expression information."); 12598 12599 // For the following checks, we rely on the base declaration which is 12600 // expected to be associated with the last component. The declaration is 12601 // expected to be a variable or a field (if 'this' is being mapped). 12602 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 12603 assert(CurDeclaration && "Null decl on map clause."); 12604 assert( 12605 CurDeclaration->isCanonicalDecl() && 12606 "Expecting components to have associated only canonical declarations."); 12607 12608 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 12609 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 12610 12611 assert((VD || FD) && "Only variables or fields are expected here!"); 12612 (void)FD; 12613 12614 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 12615 // threadprivate variables cannot appear in a map clause. 12616 // OpenMP 4.5 [2.10.5, target update Construct] 12617 // threadprivate variables cannot appear in a from clause. 12618 if (VD && DSAS->isThreadPrivate(VD)) { 12619 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 12620 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 12621 << getOpenMPClauseName(CKind); 12622 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 12623 continue; 12624 } 12625 12626 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 12627 // A list item cannot appear in both a map clause and a data-sharing 12628 // attribute clause on the same construct. 12629 12630 // Check conflicts with other map clause expressions. We check the conflicts 12631 // with the current construct separately from the enclosing data 12632 // environment, because the restrictions are different. We only have to 12633 // check conflicts across regions for the map clauses. 12634 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 12635 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 12636 break; 12637 if (CKind == OMPC_map && 12638 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 12639 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 12640 break; 12641 12642 // OpenMP 4.5 [2.10.5, target update Construct] 12643 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 12644 // If the type of a list item is a reference to a type T then the type will 12645 // be considered to be T for all purposes of this clause. 12646 auto I = llvm::find_if( 12647 CurComponents, 12648 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 12649 return MC.getAssociatedDeclaration(); 12650 }); 12651 assert(I != CurComponents.end() && "Null decl on map clause."); 12652 QualType Type = 12653 I->getAssociatedDeclaration()->getType().getNonReferenceType(); 12654 12655 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 12656 // A list item in a to or from clause must have a mappable type. 12657 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 12658 // A list item must have a mappable type. 12659 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 12660 DSAS, Type)) 12661 continue; 12662 12663 if (CKind == OMPC_map) { 12664 // target enter data 12665 // OpenMP [2.10.2, Restrictions, p. 99] 12666 // A map-type must be specified in all map clauses and must be either 12667 // to or alloc. 12668 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 12669 if (DKind == OMPD_target_enter_data && 12670 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 12671 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 12672 << (IsMapTypeImplicit ? 1 : 0) 12673 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 12674 << getOpenMPDirectiveName(DKind); 12675 continue; 12676 } 12677 12678 // target exit_data 12679 // OpenMP [2.10.3, Restrictions, p. 102] 12680 // A map-type must be specified in all map clauses and must be either 12681 // from, release, or delete. 12682 if (DKind == OMPD_target_exit_data && 12683 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 12684 MapType == OMPC_MAP_delete)) { 12685 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 12686 << (IsMapTypeImplicit ? 1 : 0) 12687 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 12688 << getOpenMPDirectiveName(DKind); 12689 continue; 12690 } 12691 12692 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 12693 // A list item cannot appear in both a map clause and a data-sharing 12694 // attribute clause on the same construct 12695 if (VD && isOpenMPTargetExecutionDirective(DKind)) { 12696 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 12697 if (isOpenMPPrivate(DVar.CKind)) { 12698 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 12699 << getOpenMPClauseName(DVar.CKind) 12700 << getOpenMPClauseName(OMPC_map) 12701 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 12702 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 12703 continue; 12704 } 12705 } 12706 } 12707 12708 // Save the current expression. 12709 MVLI.ProcessedVarList.push_back(RE); 12710 12711 // Store the components in the stack so that they can be used to check 12712 // against other clauses later on. 12713 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 12714 /*WhereFoundClauseKind=*/OMPC_map); 12715 12716 // Save the components and declaration to create the clause. For purposes of 12717 // the clause creation, any component list that has has base 'this' uses 12718 // null as base declaration. 12719 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 12720 MVLI.VarComponents.back().append(CurComponents.begin(), 12721 CurComponents.end()); 12722 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 12723 : CurDeclaration); 12724 } 12725 } 12726 12727 OMPClause * 12728 Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier, 12729 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, 12730 SourceLocation MapLoc, SourceLocation ColonLoc, 12731 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 12732 SourceLocation LParenLoc, SourceLocation EndLoc) { 12733 MappableVarListInfo MVLI(VarList); 12734 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc, 12735 MapType, IsMapTypeImplicit); 12736 12737 // We need to produce a map clause even if we don't have variables so that 12738 // other diagnostics related with non-existing map clauses are accurate. 12739 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, 12740 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 12741 MVLI.VarComponents, MapTypeModifier, MapType, 12742 IsMapTypeImplicit, MapLoc); 12743 } 12744 12745 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 12746 TypeResult ParsedType) { 12747 assert(ParsedType.isUsable()); 12748 12749 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 12750 if (ReductionType.isNull()) 12751 return QualType(); 12752 12753 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 12754 // A type name in a declare reduction directive cannot be a function type, an 12755 // array type, a reference type, or a type qualified with const, volatile or 12756 // restrict. 12757 if (ReductionType.hasQualifiers()) { 12758 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 12759 return QualType(); 12760 } 12761 12762 if (ReductionType->isFunctionType()) { 12763 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 12764 return QualType(); 12765 } 12766 if (ReductionType->isReferenceType()) { 12767 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 12768 return QualType(); 12769 } 12770 if (ReductionType->isArrayType()) { 12771 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 12772 return QualType(); 12773 } 12774 return ReductionType; 12775 } 12776 12777 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 12778 Scope *S, DeclContext *DC, DeclarationName Name, 12779 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 12780 AccessSpecifier AS, Decl *PrevDeclInScope) { 12781 SmallVector<Decl *, 8> Decls; 12782 Decls.reserve(ReductionTypes.size()); 12783 12784 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 12785 forRedeclarationInCurContext()); 12786 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 12787 // A reduction-identifier may not be re-declared in the current scope for the 12788 // same type or for a type that is compatible according to the base language 12789 // rules. 12790 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 12791 OMPDeclareReductionDecl *PrevDRD = nullptr; 12792 bool InCompoundScope = true; 12793 if (S != nullptr) { 12794 // Find previous declaration with the same name not referenced in other 12795 // declarations. 12796 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 12797 InCompoundScope = 12798 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 12799 LookupName(Lookup, S); 12800 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 12801 /*AllowInlineNamespace=*/false); 12802 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 12803 LookupResult::Filter Filter = Lookup.makeFilter(); 12804 while (Filter.hasNext()) { 12805 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 12806 if (InCompoundScope) { 12807 auto I = UsedAsPrevious.find(PrevDecl); 12808 if (I == UsedAsPrevious.end()) 12809 UsedAsPrevious[PrevDecl] = false; 12810 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 12811 UsedAsPrevious[D] = true; 12812 } 12813 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 12814 PrevDecl->getLocation(); 12815 } 12816 Filter.done(); 12817 if (InCompoundScope) { 12818 for (const auto &PrevData : UsedAsPrevious) { 12819 if (!PrevData.second) { 12820 PrevDRD = PrevData.first; 12821 break; 12822 } 12823 } 12824 } 12825 } else if (PrevDeclInScope != nullptr) { 12826 auto *PrevDRDInScope = PrevDRD = 12827 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 12828 do { 12829 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 12830 PrevDRDInScope->getLocation(); 12831 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 12832 } while (PrevDRDInScope != nullptr); 12833 } 12834 for (const auto &TyData : ReductionTypes) { 12835 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 12836 bool Invalid = false; 12837 if (I != PreviousRedeclTypes.end()) { 12838 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 12839 << TyData.first; 12840 Diag(I->second, diag::note_previous_definition); 12841 Invalid = true; 12842 } 12843 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 12844 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 12845 Name, TyData.first, PrevDRD); 12846 DC->addDecl(DRD); 12847 DRD->setAccess(AS); 12848 Decls.push_back(DRD); 12849 if (Invalid) 12850 DRD->setInvalidDecl(); 12851 else 12852 PrevDRD = DRD; 12853 } 12854 12855 return DeclGroupPtrTy::make( 12856 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 12857 } 12858 12859 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 12860 auto *DRD = cast<OMPDeclareReductionDecl>(D); 12861 12862 // Enter new function scope. 12863 PushFunctionScope(); 12864 setFunctionHasBranchProtectedScope(); 12865 getCurFunction()->setHasOMPDeclareReductionCombiner(); 12866 12867 if (S != nullptr) 12868 PushDeclContext(S, DRD); 12869 else 12870 CurContext = DRD; 12871 12872 PushExpressionEvaluationContext( 12873 ExpressionEvaluationContext::PotentiallyEvaluated); 12874 12875 QualType ReductionType = DRD->getType(); 12876 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 12877 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 12878 // uses semantics of argument handles by value, but it should be passed by 12879 // reference. C lang does not support references, so pass all parameters as 12880 // pointers. 12881 // Create 'T omp_in;' variable. 12882 VarDecl *OmpInParm = 12883 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 12884 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 12885 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 12886 // uses semantics of argument handles by value, but it should be passed by 12887 // reference. C lang does not support references, so pass all parameters as 12888 // pointers. 12889 // Create 'T omp_out;' variable. 12890 VarDecl *OmpOutParm = 12891 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 12892 if (S != nullptr) { 12893 PushOnScopeChains(OmpInParm, S); 12894 PushOnScopeChains(OmpOutParm, S); 12895 } else { 12896 DRD->addDecl(OmpInParm); 12897 DRD->addDecl(OmpOutParm); 12898 } 12899 Expr *InE = 12900 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation()); 12901 Expr *OutE = 12902 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation()); 12903 DRD->setCombinerData(InE, OutE); 12904 } 12905 12906 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 12907 auto *DRD = cast<OMPDeclareReductionDecl>(D); 12908 DiscardCleanupsInEvaluationContext(); 12909 PopExpressionEvaluationContext(); 12910 12911 PopDeclContext(); 12912 PopFunctionScopeInfo(); 12913 12914 if (Combiner != nullptr) 12915 DRD->setCombiner(Combiner); 12916 else 12917 DRD->setInvalidDecl(); 12918 } 12919 12920 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 12921 auto *DRD = cast<OMPDeclareReductionDecl>(D); 12922 12923 // Enter new function scope. 12924 PushFunctionScope(); 12925 setFunctionHasBranchProtectedScope(); 12926 12927 if (S != nullptr) 12928 PushDeclContext(S, DRD); 12929 else 12930 CurContext = DRD; 12931 12932 PushExpressionEvaluationContext( 12933 ExpressionEvaluationContext::PotentiallyEvaluated); 12934 12935 QualType ReductionType = DRD->getType(); 12936 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 12937 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 12938 // uses semantics of argument handles by value, but it should be passed by 12939 // reference. C lang does not support references, so pass all parameters as 12940 // pointers. 12941 // Create 'T omp_priv;' variable. 12942 VarDecl *OmpPrivParm = 12943 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 12944 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 12945 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 12946 // uses semantics of argument handles by value, but it should be passed by 12947 // reference. C lang does not support references, so pass all parameters as 12948 // pointers. 12949 // Create 'T omp_orig;' variable. 12950 VarDecl *OmpOrigParm = 12951 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 12952 if (S != nullptr) { 12953 PushOnScopeChains(OmpPrivParm, S); 12954 PushOnScopeChains(OmpOrigParm, S); 12955 } else { 12956 DRD->addDecl(OmpPrivParm); 12957 DRD->addDecl(OmpOrigParm); 12958 } 12959 Expr *OrigE = 12960 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation()); 12961 Expr *PrivE = 12962 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation()); 12963 DRD->setInitializerData(OrigE, PrivE); 12964 return OmpPrivParm; 12965 } 12966 12967 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 12968 VarDecl *OmpPrivParm) { 12969 auto *DRD = cast<OMPDeclareReductionDecl>(D); 12970 DiscardCleanupsInEvaluationContext(); 12971 PopExpressionEvaluationContext(); 12972 12973 PopDeclContext(); 12974 PopFunctionScopeInfo(); 12975 12976 if (Initializer != nullptr) { 12977 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 12978 } else if (OmpPrivParm->hasInit()) { 12979 DRD->setInitializer(OmpPrivParm->getInit(), 12980 OmpPrivParm->isDirectInit() 12981 ? OMPDeclareReductionDecl::DirectInit 12982 : OMPDeclareReductionDecl::CopyInit); 12983 } else { 12984 DRD->setInvalidDecl(); 12985 } 12986 } 12987 12988 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 12989 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 12990 for (Decl *D : DeclReductions.get()) { 12991 if (IsValid) { 12992 if (S) 12993 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 12994 /*AddToContext=*/false); 12995 } else { 12996 D->setInvalidDecl(); 12997 } 12998 } 12999 return DeclReductions; 13000 } 13001 13002 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 13003 SourceLocation StartLoc, 13004 SourceLocation LParenLoc, 13005 SourceLocation EndLoc) { 13006 Expr *ValExpr = NumTeams; 13007 Stmt *HelperValStmt = nullptr; 13008 13009 // OpenMP [teams Constrcut, Restrictions] 13010 // The num_teams expression must evaluate to a positive integer value. 13011 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 13012 /*StrictlyPositive=*/true)) 13013 return nullptr; 13014 13015 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 13016 OpenMPDirectiveKind CaptureRegion = 13017 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams); 13018 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 13019 ValExpr = MakeFullExpr(ValExpr).get(); 13020 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13021 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13022 HelperValStmt = buildPreInits(Context, Captures); 13023 } 13024 13025 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 13026 StartLoc, LParenLoc, EndLoc); 13027 } 13028 13029 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 13030 SourceLocation StartLoc, 13031 SourceLocation LParenLoc, 13032 SourceLocation EndLoc) { 13033 Expr *ValExpr = ThreadLimit; 13034 Stmt *HelperValStmt = nullptr; 13035 13036 // OpenMP [teams Constrcut, Restrictions] 13037 // The thread_limit expression must evaluate to a positive integer value. 13038 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 13039 /*StrictlyPositive=*/true)) 13040 return nullptr; 13041 13042 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 13043 OpenMPDirectiveKind CaptureRegion = 13044 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit); 13045 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 13046 ValExpr = MakeFullExpr(ValExpr).get(); 13047 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13048 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13049 HelperValStmt = buildPreInits(Context, Captures); 13050 } 13051 13052 return new (Context) OMPThreadLimitClause( 13053 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 13054 } 13055 13056 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 13057 SourceLocation StartLoc, 13058 SourceLocation LParenLoc, 13059 SourceLocation EndLoc) { 13060 Expr *ValExpr = Priority; 13061 13062 // OpenMP [2.9.1, task Constrcut] 13063 // The priority-value is a non-negative numerical scalar expression. 13064 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority, 13065 /*StrictlyPositive=*/false)) 13066 return nullptr; 13067 13068 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc); 13069 } 13070 13071 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 13072 SourceLocation StartLoc, 13073 SourceLocation LParenLoc, 13074 SourceLocation EndLoc) { 13075 Expr *ValExpr = Grainsize; 13076 13077 // OpenMP [2.9.2, taskloop Constrcut] 13078 // The parameter of the grainsize clause must be a positive integer 13079 // expression. 13080 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize, 13081 /*StrictlyPositive=*/true)) 13082 return nullptr; 13083 13084 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc); 13085 } 13086 13087 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 13088 SourceLocation StartLoc, 13089 SourceLocation LParenLoc, 13090 SourceLocation EndLoc) { 13091 Expr *ValExpr = NumTasks; 13092 13093 // OpenMP [2.9.2, taskloop Constrcut] 13094 // The parameter of the num_tasks clause must be a positive integer 13095 // expression. 13096 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks, 13097 /*StrictlyPositive=*/true)) 13098 return nullptr; 13099 13100 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc); 13101 } 13102 13103 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 13104 SourceLocation LParenLoc, 13105 SourceLocation EndLoc) { 13106 // OpenMP [2.13.2, critical construct, Description] 13107 // ... where hint-expression is an integer constant expression that evaluates 13108 // to a valid lock hint. 13109 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 13110 if (HintExpr.isInvalid()) 13111 return nullptr; 13112 return new (Context) 13113 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 13114 } 13115 13116 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 13117 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 13118 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 13119 SourceLocation EndLoc) { 13120 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 13121 std::string Values; 13122 Values += "'"; 13123 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 13124 Values += "'"; 13125 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 13126 << Values << getOpenMPClauseName(OMPC_dist_schedule); 13127 return nullptr; 13128 } 13129 Expr *ValExpr = ChunkSize; 13130 Stmt *HelperValStmt = nullptr; 13131 if (ChunkSize) { 13132 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 13133 !ChunkSize->isInstantiationDependent() && 13134 !ChunkSize->containsUnexpandedParameterPack()) { 13135 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 13136 ExprResult Val = 13137 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 13138 if (Val.isInvalid()) 13139 return nullptr; 13140 13141 ValExpr = Val.get(); 13142 13143 // OpenMP [2.7.1, Restrictions] 13144 // chunk_size must be a loop invariant integer expression with a positive 13145 // value. 13146 llvm::APSInt Result; 13147 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 13148 if (Result.isSigned() && !Result.isStrictlyPositive()) { 13149 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 13150 << "dist_schedule" << ChunkSize->getSourceRange(); 13151 return nullptr; 13152 } 13153 } else if (getOpenMPCaptureRegionForClause( 13154 DSAStack->getCurrentDirective(), OMPC_dist_schedule) != 13155 OMPD_unknown && 13156 !CurContext->isDependentContext()) { 13157 ValExpr = MakeFullExpr(ValExpr).get(); 13158 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13159 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13160 HelperValStmt = buildPreInits(Context, Captures); 13161 } 13162 } 13163 } 13164 13165 return new (Context) 13166 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 13167 Kind, ValExpr, HelperValStmt); 13168 } 13169 13170 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 13171 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 13172 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 13173 SourceLocation KindLoc, SourceLocation EndLoc) { 13174 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)' 13175 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) { 13176 std::string Value; 13177 SourceLocation Loc; 13178 Value += "'"; 13179 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 13180 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 13181 OMPC_DEFAULTMAP_MODIFIER_tofrom); 13182 Loc = MLoc; 13183 } else { 13184 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 13185 OMPC_DEFAULTMAP_scalar); 13186 Loc = KindLoc; 13187 } 13188 Value += "'"; 13189 Diag(Loc, diag::err_omp_unexpected_clause_value) 13190 << Value << getOpenMPClauseName(OMPC_defaultmap); 13191 return nullptr; 13192 } 13193 DSAStack->setDefaultDMAToFromScalar(StartLoc); 13194 13195 return new (Context) 13196 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 13197 } 13198 13199 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { 13200 DeclContext *CurLexicalContext = getCurLexicalContext(); 13201 if (!CurLexicalContext->isFileContext() && 13202 !CurLexicalContext->isExternCContext() && 13203 !CurLexicalContext->isExternCXXContext() && 13204 !isa<CXXRecordDecl>(CurLexicalContext) && 13205 !isa<ClassTemplateDecl>(CurLexicalContext) && 13206 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 13207 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 13208 Diag(Loc, diag::err_omp_region_not_file_context); 13209 return false; 13210 } 13211 ++DeclareTargetNestingLevel; 13212 return true; 13213 } 13214 13215 void Sema::ActOnFinishOpenMPDeclareTargetDirective() { 13216 assert(DeclareTargetNestingLevel > 0 && 13217 "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); 13218 --DeclareTargetNestingLevel; 13219 } 13220 13221 void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, 13222 CXXScopeSpec &ScopeSpec, 13223 const DeclarationNameInfo &Id, 13224 OMPDeclareTargetDeclAttr::MapTypeTy MT, 13225 NamedDeclSetType &SameDirectiveDecls) { 13226 LookupResult Lookup(*this, Id, LookupOrdinaryName); 13227 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 13228 13229 if (Lookup.isAmbiguous()) 13230 return; 13231 Lookup.suppressDiagnostics(); 13232 13233 if (!Lookup.isSingleResult()) { 13234 if (TypoCorrection Corrected = 13235 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, 13236 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this), 13237 CTK_ErrorRecovery)) { 13238 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 13239 << Id.getName()); 13240 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 13241 return; 13242 } 13243 13244 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 13245 return; 13246 } 13247 13248 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 13249 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) || 13250 isa<FunctionTemplateDecl>(ND)) { 13251 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) 13252 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); 13253 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 13254 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 13255 cast<ValueDecl>(ND)); 13256 if (!Res) { 13257 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT); 13258 ND->addAttr(A); 13259 if (ASTMutationListener *ML = Context.getASTMutationListener()) 13260 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 13261 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc()); 13262 } else if (*Res != MT) { 13263 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link) 13264 << Id.getName(); 13265 } 13266 } else { 13267 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 13268 } 13269 } 13270 13271 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 13272 Sema &SemaRef, Decl *D) { 13273 if (!D || !isa<VarDecl>(D)) 13274 return; 13275 auto *VD = cast<VarDecl>(D); 13276 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 13277 return; 13278 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context); 13279 SemaRef.Diag(SL, diag::note_used_here) << SR; 13280 } 13281 13282 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 13283 Sema &SemaRef, DSAStackTy *Stack, 13284 ValueDecl *VD) { 13285 return VD->hasAttr<OMPDeclareTargetDeclAttr>() || 13286 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 13287 /*FullCheck=*/false); 13288 } 13289 13290 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 13291 SourceLocation IdLoc) { 13292 if (!D || D->isInvalidDecl()) 13293 return; 13294 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 13295 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation(); 13296 if (auto *VD = dyn_cast<VarDecl>(D)) { 13297 // Only global variables can be marked as declare target. 13298 if (!VD->isFileVarDecl() && !VD->isStaticLocal() && 13299 !VD->isStaticDataMember()) 13300 return; 13301 // 2.10.6: threadprivate variable cannot appear in a declare target 13302 // directive. 13303 if (DSAStack->isThreadPrivate(VD)) { 13304 Diag(SL, diag::err_omp_threadprivate_in_target); 13305 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 13306 return; 13307 } 13308 } 13309 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) 13310 D = FTD->getTemplatedDecl(); 13311 if (const auto *FD = dyn_cast<FunctionDecl>(D)) { 13312 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 13313 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD); 13314 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { 13315 assert(IdLoc.isValid() && "Source location is expected"); 13316 Diag(IdLoc, diag::err_omp_function_in_link_clause); 13317 Diag(FD->getLocation(), diag::note_defined_here) << FD; 13318 return; 13319 } 13320 } 13321 if (auto *VD = dyn_cast<ValueDecl>(D)) { 13322 // Problem if any with var declared with incomplete type will be reported 13323 // as normal, so no need to check it here. 13324 if ((E || !VD->getType()->isIncompleteType()) && 13325 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) 13326 return; 13327 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { 13328 // Checking declaration inside declare target region. 13329 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 13330 isa<FunctionTemplateDecl>(D)) { 13331 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 13332 Context, OMPDeclareTargetDeclAttr::MT_To); 13333 D->addAttr(A); 13334 if (ASTMutationListener *ML = Context.getASTMutationListener()) 13335 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 13336 } 13337 return; 13338 } 13339 } 13340 if (!E) 13341 return; 13342 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 13343 } 13344 13345 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList, 13346 SourceLocation StartLoc, 13347 SourceLocation LParenLoc, 13348 SourceLocation EndLoc) { 13349 MappableVarListInfo MVLI(VarList); 13350 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc); 13351 if (MVLI.ProcessedVarList.empty()) 13352 return nullptr; 13353 13354 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc, 13355 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 13356 MVLI.VarComponents); 13357 } 13358 13359 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList, 13360 SourceLocation StartLoc, 13361 SourceLocation LParenLoc, 13362 SourceLocation EndLoc) { 13363 MappableVarListInfo MVLI(VarList); 13364 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc); 13365 if (MVLI.ProcessedVarList.empty()) 13366 return nullptr; 13367 13368 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc, 13369 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 13370 MVLI.VarComponents); 13371 } 13372 13373 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 13374 SourceLocation StartLoc, 13375 SourceLocation LParenLoc, 13376 SourceLocation EndLoc) { 13377 MappableVarListInfo MVLI(VarList); 13378 SmallVector<Expr *, 8> PrivateCopies; 13379 SmallVector<Expr *, 8> Inits; 13380 13381 for (Expr *RefExpr : VarList) { 13382 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 13383 SourceLocation ELoc; 13384 SourceRange ERange; 13385 Expr *SimpleRefExpr = RefExpr; 13386 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 13387 if (Res.second) { 13388 // It will be analyzed later. 13389 MVLI.ProcessedVarList.push_back(RefExpr); 13390 PrivateCopies.push_back(nullptr); 13391 Inits.push_back(nullptr); 13392 } 13393 ValueDecl *D = Res.first; 13394 if (!D) 13395 continue; 13396 13397 QualType Type = D->getType(); 13398 Type = Type.getNonReferenceType().getUnqualifiedType(); 13399 13400 auto *VD = dyn_cast<VarDecl>(D); 13401 13402 // Item should be a pointer or reference to pointer. 13403 if (!Type->isPointerType()) { 13404 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 13405 << 0 << RefExpr->getSourceRange(); 13406 continue; 13407 } 13408 13409 // Build the private variable and the expression that refers to it. 13410 auto VDPrivate = 13411 buildVarDecl(*this, ELoc, Type, D->getName(), 13412 D->hasAttrs() ? &D->getAttrs() : nullptr, 13413 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 13414 if (VDPrivate->isInvalidDecl()) 13415 continue; 13416 13417 CurContext->addDecl(VDPrivate); 13418 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 13419 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 13420 13421 // Add temporary variable to initialize the private copy of the pointer. 13422 VarDecl *VDInit = 13423 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 13424 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 13425 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 13426 AddInitializerToDecl(VDPrivate, 13427 DefaultLvalueConversion(VDInitRefExpr).get(), 13428 /*DirectInit=*/false); 13429 13430 // If required, build a capture to implement the privatization initialized 13431 // with the current list item value. 13432 DeclRefExpr *Ref = nullptr; 13433 if (!VD) 13434 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 13435 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 13436 PrivateCopies.push_back(VDPrivateRefExpr); 13437 Inits.push_back(VDInitRefExpr); 13438 13439 // We need to add a data sharing attribute for this variable to make sure it 13440 // is correctly captured. A variable that shows up in a use_device_ptr has 13441 // similar properties of a first private variable. 13442 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 13443 13444 // Create a mappable component for the list item. List items in this clause 13445 // only need a component. 13446 MVLI.VarBaseDeclarations.push_back(D); 13447 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 13448 MVLI.VarComponents.back().push_back( 13449 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D)); 13450 } 13451 13452 if (MVLI.ProcessedVarList.empty()) 13453 return nullptr; 13454 13455 return OMPUseDevicePtrClause::Create( 13456 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList, 13457 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents); 13458 } 13459 13460 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 13461 SourceLocation StartLoc, 13462 SourceLocation LParenLoc, 13463 SourceLocation EndLoc) { 13464 MappableVarListInfo MVLI(VarList); 13465 for (Expr *RefExpr : VarList) { 13466 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 13467 SourceLocation ELoc; 13468 SourceRange ERange; 13469 Expr *SimpleRefExpr = RefExpr; 13470 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 13471 if (Res.second) { 13472 // It will be analyzed later. 13473 MVLI.ProcessedVarList.push_back(RefExpr); 13474 } 13475 ValueDecl *D = Res.first; 13476 if (!D) 13477 continue; 13478 13479 QualType Type = D->getType(); 13480 // item should be a pointer or array or reference to pointer or array 13481 if (!Type.getNonReferenceType()->isPointerType() && 13482 !Type.getNonReferenceType()->isArrayType()) { 13483 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 13484 << 0 << RefExpr->getSourceRange(); 13485 continue; 13486 } 13487 13488 // Check if the declaration in the clause does not show up in any data 13489 // sharing attribute. 13490 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 13491 if (isOpenMPPrivate(DVar.CKind)) { 13492 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 13493 << getOpenMPClauseName(DVar.CKind) 13494 << getOpenMPClauseName(OMPC_is_device_ptr) 13495 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 13496 reportOriginalDsa(*this, DSAStack, D, DVar); 13497 continue; 13498 } 13499 13500 const Expr *ConflictExpr; 13501 if (DSAStack->checkMappableExprComponentListsForDecl( 13502 D, /*CurrentRegionOnly=*/true, 13503 [&ConflictExpr]( 13504 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 13505 OpenMPClauseKind) -> bool { 13506 ConflictExpr = R.front().getAssociatedExpression(); 13507 return true; 13508 })) { 13509 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 13510 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 13511 << ConflictExpr->getSourceRange(); 13512 continue; 13513 } 13514 13515 // Store the components in the stack so that they can be used to check 13516 // against other clauses later on. 13517 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D); 13518 DSAStack->addMappableExpressionComponents( 13519 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 13520 13521 // Record the expression we've just processed. 13522 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 13523 13524 // Create a mappable component for the list item. List items in this clause 13525 // only need a component. We use a null declaration to signal fields in 13526 // 'this'. 13527 assert((isa<DeclRefExpr>(SimpleRefExpr) || 13528 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 13529 "Unexpected device pointer expression!"); 13530 MVLI.VarBaseDeclarations.push_back( 13531 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 13532 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 13533 MVLI.VarComponents.back().push_back(MC); 13534 } 13535 13536 if (MVLI.ProcessedVarList.empty()) 13537 return nullptr; 13538 13539 return OMPIsDevicePtrClause::Create( 13540 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList, 13541 MVLI.VarBaseDeclarations, MVLI.VarComponents); 13542 } 13543