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 77 private: 78 struct DSAInfo { 79 OpenMPClauseKind Attributes = OMPC_unknown; 80 /// Pointer to a reference expression and a flag which shows that the 81 /// variable is marked as lastprivate(true) or not (false). 82 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr; 83 DeclRefExpr *PrivateCopy = nullptr; 84 }; 85 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>; 86 using AlignedMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>; 87 using LCDeclInfo = std::pair<unsigned, VarDecl *>; 88 using LoopControlVariablesMapTy = 89 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>; 90 /// Struct that associates a component with the clause kind where they are 91 /// found. 92 struct MappedExprComponentTy { 93 OMPClauseMappableExprCommon::MappableExprComponentLists Components; 94 OpenMPClauseKind Kind = OMPC_unknown; 95 }; 96 using MappedExprComponentsTy = 97 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>; 98 using CriticalsWithHintsTy = 99 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>; 100 using DoacrossDependMapTy = 101 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>; 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::PointerIntPair<const Expr *, 1, bool> 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 public: 181 explicit DSAStackTy(Sema &S) : SemaRef(S) {} 182 183 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 184 OpenMPClauseKind getClauseParsingMode() const { 185 assert(isClauseParsingMode() && "Must be in clause parsing mode."); 186 return ClauseKindMode; 187 } 188 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 189 190 bool isForceVarCapturing() const { return ForceCapturing; } 191 void setForceVarCapturing(bool V) { ForceCapturing = V; } 192 193 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 194 Scope *CurScope, SourceLocation Loc) { 195 if (Stack.empty() || 196 Stack.back().second != CurrentNonCapturingFunctionScope) 197 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope); 198 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc); 199 Stack.back().first.back().DefaultAttrLoc = Loc; 200 } 201 202 void pop() { 203 assert(!Stack.back().first.empty() && 204 "Data-sharing attributes stack is empty!"); 205 Stack.back().first.pop_back(); 206 } 207 208 /// Start new OpenMP region stack in new non-capturing function. 209 void pushFunction() { 210 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction(); 211 assert(!isa<CapturingScopeInfo>(CurFnScope)); 212 CurrentNonCapturingFunctionScope = CurFnScope; 213 } 214 /// Pop region stack for non-capturing function. 215 void popFunction(const FunctionScopeInfo *OldFSI) { 216 if (!Stack.empty() && Stack.back().second == OldFSI) { 217 assert(Stack.back().first.empty()); 218 Stack.pop_back(); 219 } 220 CurrentNonCapturingFunctionScope = nullptr; 221 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) { 222 if (!isa<CapturingScopeInfo>(FSI)) { 223 CurrentNonCapturingFunctionScope = FSI; 224 break; 225 } 226 } 227 } 228 229 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) { 230 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint); 231 } 232 const std::pair<const OMPCriticalDirective *, llvm::APSInt> 233 getCriticalWithHint(const DeclarationNameInfo &Name) const { 234 auto I = Criticals.find(Name.getAsString()); 235 if (I != Criticals.end()) 236 return I->second; 237 return std::make_pair(nullptr, llvm::APSInt()); 238 } 239 /// If 'aligned' declaration for given variable \a D was not seen yet, 240 /// add it and return NULL; otherwise return previous occurrence's expression 241 /// for diagnostics. 242 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE); 243 244 /// Register specified variable as loop control variable. 245 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture); 246 /// Check if the specified variable is a loop control variable for 247 /// current region. 248 /// \return The index of the loop control variable in the list of associated 249 /// for-loops (from outer to inner). 250 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const; 251 /// Check if the specified variable is a loop control variable for 252 /// parent region. 253 /// \return The index of the loop control variable in the list of associated 254 /// for-loops (from outer to inner). 255 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const; 256 /// Get the loop control variable for the I-th loop (or nullptr) in 257 /// parent directive. 258 const ValueDecl *getParentLoopControlVariable(unsigned I) const; 259 260 /// Adds explicit data sharing attribute to the specified declaration. 261 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 262 DeclRefExpr *PrivateCopy = nullptr); 263 264 /// Adds additional information for the reduction items with the reduction id 265 /// represented as an operator. 266 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 267 BinaryOperatorKind BOK); 268 /// Adds additional information for the reduction items with the reduction id 269 /// represented as reduction identifier. 270 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 271 const Expr *ReductionRef); 272 /// Returns the location and reduction operation from the innermost parent 273 /// region for the given \p D. 274 const DSAVarData 275 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 276 BinaryOperatorKind &BOK, 277 Expr *&TaskgroupDescriptor) const; 278 /// Returns the location and reduction operation from the innermost parent 279 /// region for the given \p D. 280 const DSAVarData 281 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 282 const Expr *&ReductionRef, 283 Expr *&TaskgroupDescriptor) const; 284 /// Return reduction reference expression for the current taskgroup. 285 Expr *getTaskgroupReductionRef() const { 286 assert(Stack.back().first.back().Directive == OMPD_taskgroup && 287 "taskgroup reference expression requested for non taskgroup " 288 "directive."); 289 return Stack.back().first.back().TaskgroupReductionRef; 290 } 291 /// Checks if the given \p VD declaration is actually a taskgroup reduction 292 /// descriptor variable at the \p Level of OpenMP regions. 293 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const { 294 return Stack.back().first[Level].TaskgroupReductionRef && 295 cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef) 296 ->getDecl() == VD; 297 } 298 299 /// Returns data sharing attributes from top of the stack for the 300 /// specified declaration. 301 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 302 /// Returns data-sharing attributes for the specified declaration. 303 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const; 304 /// Checks if the specified variables has data-sharing attributes which 305 /// match specified \a CPred predicate in any directive which matches \a DPred 306 /// predicate. 307 const DSAVarData 308 hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 309 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 310 bool FromParent) const; 311 /// Checks if the specified variables has data-sharing attributes which 312 /// match specified \a CPred predicate in any innermost directive which 313 /// matches \a DPred predicate. 314 const DSAVarData 315 hasInnermostDSA(ValueDecl *D, 316 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 317 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 318 bool FromParent) const; 319 /// Checks if the specified variables has explicit data-sharing 320 /// attributes which match specified \a CPred predicate at the specified 321 /// OpenMP region. 322 bool hasExplicitDSA(const ValueDecl *D, 323 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 324 unsigned Level, bool NotLastprivate = false) const; 325 326 /// Returns true if the directive at level \Level matches in the 327 /// specified \a DPred predicate. 328 bool hasExplicitDirective( 329 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 330 unsigned Level) const; 331 332 /// Finds a directive which matches specified \a DPred predicate. 333 bool hasDirective( 334 const llvm::function_ref<bool( 335 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)> 336 DPred, 337 bool FromParent) const; 338 339 /// Returns currently analyzed directive. 340 OpenMPDirectiveKind getCurrentDirective() const { 341 return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive; 342 } 343 /// Returns directive kind at specified level. 344 OpenMPDirectiveKind getDirective(unsigned Level) const { 345 assert(!isStackEmpty() && "No directive at specified level."); 346 return Stack.back().first[Level].Directive; 347 } 348 /// Returns parent directive. 349 OpenMPDirectiveKind getParentDirective() const { 350 if (isStackEmpty() || Stack.back().first.size() == 1) 351 return OMPD_unknown; 352 return std::next(Stack.back().first.rbegin())->Directive; 353 } 354 355 /// Set default data sharing attribute to none. 356 void setDefaultDSANone(SourceLocation Loc) { 357 assert(!isStackEmpty()); 358 Stack.back().first.back().DefaultAttr = DSA_none; 359 Stack.back().first.back().DefaultAttrLoc = Loc; 360 } 361 /// Set default data sharing attribute to shared. 362 void setDefaultDSAShared(SourceLocation Loc) { 363 assert(!isStackEmpty()); 364 Stack.back().first.back().DefaultAttr = DSA_shared; 365 Stack.back().first.back().DefaultAttrLoc = Loc; 366 } 367 /// Set default data mapping attribute to 'tofrom:scalar'. 368 void setDefaultDMAToFromScalar(SourceLocation Loc) { 369 assert(!isStackEmpty()); 370 Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar; 371 Stack.back().first.back().DefaultMapAttrLoc = Loc; 372 } 373 374 DefaultDataSharingAttributes getDefaultDSA() const { 375 return isStackEmpty() ? DSA_unspecified 376 : Stack.back().first.back().DefaultAttr; 377 } 378 SourceLocation getDefaultDSALocation() const { 379 return isStackEmpty() ? SourceLocation() 380 : Stack.back().first.back().DefaultAttrLoc; 381 } 382 DefaultMapAttributes getDefaultDMA() const { 383 return isStackEmpty() ? DMA_unspecified 384 : Stack.back().first.back().DefaultMapAttr; 385 } 386 DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const { 387 return Stack.back().first[Level].DefaultMapAttr; 388 } 389 SourceLocation getDefaultDMALocation() const { 390 return isStackEmpty() ? SourceLocation() 391 : Stack.back().first.back().DefaultMapAttrLoc; 392 } 393 394 /// Checks if the specified variable is a threadprivate. 395 bool isThreadPrivate(VarDecl *D) { 396 const DSAVarData DVar = getTopDSA(D, false); 397 return isOpenMPThreadPrivate(DVar.CKind); 398 } 399 400 /// Marks current region as ordered (it has an 'ordered' clause). 401 void setOrderedRegion(bool IsOrdered, const Expr *Param) { 402 assert(!isStackEmpty()); 403 Stack.back().first.back().OrderedRegion.setInt(IsOrdered); 404 Stack.back().first.back().OrderedRegion.setPointer(Param); 405 } 406 /// Returns true, if parent region is ordered (has associated 407 /// 'ordered' clause), false - otherwise. 408 bool isParentOrderedRegion() const { 409 if (isStackEmpty() || Stack.back().first.size() == 1) 410 return false; 411 return std::next(Stack.back().first.rbegin())->OrderedRegion.getInt(); 412 } 413 /// Returns optional parameter for the ordered region. 414 const Expr *getParentOrderedRegionParam() const { 415 if (isStackEmpty() || Stack.back().first.size() == 1) 416 return nullptr; 417 return std::next(Stack.back().first.rbegin())->OrderedRegion.getPointer(); 418 } 419 /// Marks current region as nowait (it has a 'nowait' clause). 420 void setNowaitRegion(bool IsNowait = true) { 421 assert(!isStackEmpty()); 422 Stack.back().first.back().NowaitRegion = IsNowait; 423 } 424 /// Returns true, if parent region is nowait (has associated 425 /// 'nowait' clause), false - otherwise. 426 bool isParentNowaitRegion() const { 427 if (isStackEmpty() || Stack.back().first.size() == 1) 428 return false; 429 return std::next(Stack.back().first.rbegin())->NowaitRegion; 430 } 431 /// Marks parent region as cancel region. 432 void setParentCancelRegion(bool Cancel = true) { 433 if (!isStackEmpty() && Stack.back().first.size() > 1) { 434 auto &StackElemRef = *std::next(Stack.back().first.rbegin()); 435 StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel; 436 } 437 } 438 /// Return true if current region has inner cancel construct. 439 bool isCancelRegion() const { 440 return isStackEmpty() ? false : Stack.back().first.back().CancelRegion; 441 } 442 443 /// Set collapse value for the region. 444 void setAssociatedLoops(unsigned Val) { 445 assert(!isStackEmpty()); 446 Stack.back().first.back().AssociatedLoops = Val; 447 } 448 /// Return collapse value for region. 449 unsigned getAssociatedLoops() const { 450 return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops; 451 } 452 453 /// Marks current target region as one with closely nested teams 454 /// region. 455 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 456 if (!isStackEmpty() && Stack.back().first.size() > 1) { 457 std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc = 458 TeamsRegionLoc; 459 } 460 } 461 /// Returns true, if current region has closely nested teams region. 462 bool hasInnerTeamsRegion() const { 463 return getInnerTeamsRegionLoc().isValid(); 464 } 465 /// Returns location of the nested teams region (if any). 466 SourceLocation getInnerTeamsRegionLoc() const { 467 return isStackEmpty() ? SourceLocation() 468 : Stack.back().first.back().InnerTeamsRegionLoc; 469 } 470 471 Scope *getCurScope() const { 472 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope; 473 } 474 SourceLocation getConstructLoc() const { 475 return isStackEmpty() ? SourceLocation() 476 : Stack.back().first.back().ConstructLoc; 477 } 478 479 /// Do the check specified in \a Check to all component lists and return true 480 /// if any issue is found. 481 bool checkMappableExprComponentListsForDecl( 482 const ValueDecl *VD, bool CurrentRegionOnly, 483 const llvm::function_ref< 484 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 485 OpenMPClauseKind)> 486 Check) const { 487 if (isStackEmpty()) 488 return false; 489 auto SI = Stack.back().first.rbegin(); 490 auto SE = Stack.back().first.rend(); 491 492 if (SI == SE) 493 return false; 494 495 if (CurrentRegionOnly) 496 SE = std::next(SI); 497 else 498 std::advance(SI, 1); 499 500 for (; SI != SE; ++SI) { 501 auto MI = SI->MappedExprComponents.find(VD); 502 if (MI != SI->MappedExprComponents.end()) 503 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 504 MI->second.Components) 505 if (Check(L, MI->second.Kind)) 506 return true; 507 } 508 return false; 509 } 510 511 /// Do the check specified in \a Check to all component lists at a given level 512 /// and return true if any issue is found. 513 bool checkMappableExprComponentListsForDeclAtLevel( 514 const ValueDecl *VD, unsigned Level, 515 const llvm::function_ref< 516 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 517 OpenMPClauseKind)> 518 Check) const { 519 if (isStackEmpty()) 520 return false; 521 522 auto StartI = Stack.back().first.begin(); 523 auto EndI = Stack.back().first.end(); 524 if (std::distance(StartI, EndI) <= (int)Level) 525 return false; 526 std::advance(StartI, Level); 527 528 auto MI = StartI->MappedExprComponents.find(VD); 529 if (MI != StartI->MappedExprComponents.end()) 530 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 531 MI->second.Components) 532 if (Check(L, MI->second.Kind)) 533 return true; 534 return false; 535 } 536 537 /// Create a new mappable expression component list associated with a given 538 /// declaration and initialize it with the provided list of components. 539 void addMappableExpressionComponents( 540 const ValueDecl *VD, 541 OMPClauseMappableExprCommon::MappableExprComponentListRef Components, 542 OpenMPClauseKind WhereFoundClauseKind) { 543 assert(!isStackEmpty() && 544 "Not expecting to retrieve components from a empty stack!"); 545 MappedExprComponentTy &MEC = 546 Stack.back().first.back().MappedExprComponents[VD]; 547 // Create new entry and append the new components there. 548 MEC.Components.resize(MEC.Components.size() + 1); 549 MEC.Components.back().append(Components.begin(), Components.end()); 550 MEC.Kind = WhereFoundClauseKind; 551 } 552 553 unsigned getNestingLevel() const { 554 assert(!isStackEmpty()); 555 return Stack.back().first.size() - 1; 556 } 557 void addDoacrossDependClause(OMPDependClause *C, 558 const OperatorOffsetTy &OpsOffs) { 559 assert(!isStackEmpty() && Stack.back().first.size() > 1); 560 SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin()); 561 assert(isOpenMPWorksharingDirective(StackElem.Directive)); 562 StackElem.DoacrossDepends.try_emplace(C, OpsOffs); 563 } 564 llvm::iterator_range<DoacrossDependMapTy::const_iterator> 565 getDoacrossDependClauses() const { 566 assert(!isStackEmpty()); 567 const SharingMapTy &StackElem = Stack.back().first.back(); 568 if (isOpenMPWorksharingDirective(StackElem.Directive)) { 569 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends; 570 return llvm::make_range(Ref.begin(), Ref.end()); 571 } 572 return llvm::make_range(StackElem.DoacrossDepends.end(), 573 StackElem.DoacrossDepends.end()); 574 } 575 }; 576 bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) { 577 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) || 578 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown; 579 } 580 581 } // namespace 582 583 static const Expr *getExprAsWritten(const Expr *E) { 584 if (const auto *ExprTemp = dyn_cast<ExprWithCleanups>(E)) 585 E = ExprTemp->getSubExpr(); 586 587 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 588 E = MTE->GetTemporaryExpr(); 589 590 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 591 E = Binder->getSubExpr(); 592 593 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 594 E = ICE->getSubExprAsWritten(); 595 return E->IgnoreParens(); 596 } 597 598 static Expr *getExprAsWritten(Expr *E) { 599 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E))); 600 } 601 602 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) { 603 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) 604 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 605 D = ME->getMemberDecl(); 606 const auto *VD = dyn_cast<VarDecl>(D); 607 const auto *FD = dyn_cast<FieldDecl>(D); 608 if (VD != nullptr) { 609 VD = VD->getCanonicalDecl(); 610 D = VD; 611 } else { 612 assert(FD); 613 FD = FD->getCanonicalDecl(); 614 D = FD; 615 } 616 return D; 617 } 618 619 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 620 return const_cast<ValueDecl *>( 621 getCanonicalDecl(const_cast<const ValueDecl *>(D))); 622 } 623 624 DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter, 625 ValueDecl *D) const { 626 D = getCanonicalDecl(D); 627 auto *VD = dyn_cast<VarDecl>(D); 628 const auto *FD = dyn_cast<FieldDecl>(D); 629 DSAVarData DVar; 630 if (isStackEmpty() || Iter == Stack.back().first.rend()) { 631 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 632 // in a region but not in construct] 633 // File-scope or namespace-scope variables referenced in called routines 634 // in the region are shared unless they appear in a threadprivate 635 // directive. 636 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD)) 637 DVar.CKind = OMPC_shared; 638 639 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 640 // in a region but not in construct] 641 // Variables with static storage duration that are declared in called 642 // routines in the region are shared. 643 if (VD && VD->hasGlobalStorage()) 644 DVar.CKind = OMPC_shared; 645 646 // Non-static data members are shared by default. 647 if (FD) 648 DVar.CKind = OMPC_shared; 649 650 return DVar; 651 } 652 653 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 654 // in a Construct, C/C++, predetermined, p.1] 655 // Variables with automatic storage duration that are declared in a scope 656 // inside the construct are private. 657 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 658 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 659 DVar.CKind = OMPC_private; 660 return DVar; 661 } 662 663 DVar.DKind = Iter->Directive; 664 // Explicitly specified attributes and local variables with predetermined 665 // attributes. 666 if (Iter->SharingMap.count(D)) { 667 const DSAInfo &Data = Iter->SharingMap.lookup(D); 668 DVar.RefExpr = Data.RefExpr.getPointer(); 669 DVar.PrivateCopy = Data.PrivateCopy; 670 DVar.CKind = Data.Attributes; 671 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 672 return DVar; 673 } 674 675 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 676 // in a Construct, C/C++, implicitly determined, p.1] 677 // In a parallel or task construct, the data-sharing attributes of these 678 // variables are determined by the default clause, if present. 679 switch (Iter->DefaultAttr) { 680 case DSA_shared: 681 DVar.CKind = OMPC_shared; 682 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 683 return DVar; 684 case DSA_none: 685 return DVar; 686 case DSA_unspecified: 687 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 688 // in a Construct, implicitly determined, p.2] 689 // In a parallel construct, if no default clause is present, these 690 // variables are shared. 691 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 692 if (isOpenMPParallelDirective(DVar.DKind) || 693 isOpenMPTeamsDirective(DVar.DKind)) { 694 DVar.CKind = OMPC_shared; 695 return DVar; 696 } 697 698 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 699 // in a Construct, implicitly determined, p.4] 700 // In a task construct, if no default clause is present, a variable that in 701 // the enclosing context is determined to be shared by all implicit tasks 702 // bound to the current team is shared. 703 if (isOpenMPTaskingDirective(DVar.DKind)) { 704 DSAVarData DVarTemp; 705 iterator I = Iter, E = Stack.back().first.rend(); 706 do { 707 ++I; 708 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 709 // Referenced in a Construct, implicitly determined, p.6] 710 // In a task construct, if no default clause is present, a variable 711 // whose data-sharing attribute is not determined by the rules above is 712 // firstprivate. 713 DVarTemp = getDSA(I, D); 714 if (DVarTemp.CKind != OMPC_shared) { 715 DVar.RefExpr = nullptr; 716 DVar.CKind = OMPC_firstprivate; 717 return DVar; 718 } 719 } while (I != E && !isParallelOrTaskRegion(I->Directive)); 720 DVar.CKind = 721 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 722 return DVar; 723 } 724 } 725 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 726 // in a Construct, implicitly determined, p.3] 727 // For constructs other than task, if no default clause is present, these 728 // variables inherit their data-sharing attributes from the enclosing 729 // context. 730 return getDSA(++Iter, D); 731 } 732 733 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D, 734 const Expr *NewDE) { 735 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 736 D = getCanonicalDecl(D); 737 SharingMapTy &StackElem = Stack.back().first.back(); 738 auto It = StackElem.AlignedMap.find(D); 739 if (It == StackElem.AlignedMap.end()) { 740 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 741 StackElem.AlignedMap[D] = NewDE; 742 return nullptr; 743 } 744 assert(It->second && "Unexpected nullptr expr in the aligned map"); 745 return It->second; 746 } 747 748 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) { 749 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 750 D = getCanonicalDecl(D); 751 SharingMapTy &StackElem = Stack.back().first.back(); 752 StackElem.LCVMap.try_emplace( 753 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)); 754 } 755 756 const DSAStackTy::LCDeclInfo 757 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const { 758 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 759 D = getCanonicalDecl(D); 760 const SharingMapTy &StackElem = Stack.back().first.back(); 761 auto It = StackElem.LCVMap.find(D); 762 if (It != StackElem.LCVMap.end()) 763 return It->second; 764 return {0, nullptr}; 765 } 766 767 const DSAStackTy::LCDeclInfo 768 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const { 769 assert(!isStackEmpty() && Stack.back().first.size() > 1 && 770 "Data-sharing attributes stack is empty"); 771 D = getCanonicalDecl(D); 772 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin()); 773 auto It = StackElem.LCVMap.find(D); 774 if (It != StackElem.LCVMap.end()) 775 return It->second; 776 return {0, nullptr}; 777 } 778 779 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const { 780 assert(!isStackEmpty() && Stack.back().first.size() > 1 && 781 "Data-sharing attributes stack is empty"); 782 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin()); 783 if (StackElem.LCVMap.size() < I) 784 return nullptr; 785 for (const auto &Pair : StackElem.LCVMap) 786 if (Pair.second.first == I) 787 return Pair.first; 788 return nullptr; 789 } 790 791 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 792 DeclRefExpr *PrivateCopy) { 793 D = getCanonicalDecl(D); 794 if (A == OMPC_threadprivate) { 795 DSAInfo &Data = Threadprivates[D]; 796 Data.Attributes = A; 797 Data.RefExpr.setPointer(E); 798 Data.PrivateCopy = nullptr; 799 } else { 800 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 801 DSAInfo &Data = Stack.back().first.back().SharingMap[D]; 802 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 803 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 804 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 805 (isLoopControlVariable(D).first && A == OMPC_private)); 806 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 807 Data.RefExpr.setInt(/*IntVal=*/true); 808 return; 809 } 810 const bool IsLastprivate = 811 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 812 Data.Attributes = A; 813 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 814 Data.PrivateCopy = PrivateCopy; 815 if (PrivateCopy) { 816 DSAInfo &Data = 817 Stack.back().first.back().SharingMap[PrivateCopy->getDecl()]; 818 Data.Attributes = A; 819 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 820 Data.PrivateCopy = nullptr; 821 } 822 } 823 } 824 825 /// Build a variable declaration for OpenMP loop iteration variable. 826 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 827 StringRef Name, const AttrVec *Attrs = nullptr, 828 DeclRefExpr *OrigRef = nullptr) { 829 DeclContext *DC = SemaRef.CurContext; 830 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 831 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 832 auto *Decl = 833 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 834 if (Attrs) { 835 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 836 I != E; ++I) 837 Decl->addAttr(*I); 838 } 839 Decl->setImplicit(); 840 if (OrigRef) { 841 Decl->addAttr( 842 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef)); 843 } 844 return Decl; 845 } 846 847 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 848 SourceLocation Loc, 849 bool RefersToCapture = false) { 850 D->setReferenced(); 851 D->markUsed(S.Context); 852 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 853 SourceLocation(), D, RefersToCapture, Loc, Ty, 854 VK_LValue); 855 } 856 857 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 858 BinaryOperatorKind BOK) { 859 D = getCanonicalDecl(D); 860 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 861 assert( 862 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction && 863 "Additional reduction info may be specified only for reduction items."); 864 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D]; 865 assert(ReductionData.ReductionRange.isInvalid() && 866 Stack.back().first.back().Directive == OMPD_taskgroup && 867 "Additional reduction info may be specified only once for reduction " 868 "items."); 869 ReductionData.set(BOK, SR); 870 Expr *&TaskgroupReductionRef = 871 Stack.back().first.back().TaskgroupReductionRef; 872 if (!TaskgroupReductionRef) { 873 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 874 SemaRef.Context.VoidPtrTy, ".task_red."); 875 TaskgroupReductionRef = 876 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 877 } 878 } 879 880 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 881 const Expr *ReductionRef) { 882 D = getCanonicalDecl(D); 883 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 884 assert( 885 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction && 886 "Additional reduction info may be specified only for reduction items."); 887 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D]; 888 assert(ReductionData.ReductionRange.isInvalid() && 889 Stack.back().first.back().Directive == OMPD_taskgroup && 890 "Additional reduction info may be specified only once for reduction " 891 "items."); 892 ReductionData.set(ReductionRef, SR); 893 Expr *&TaskgroupReductionRef = 894 Stack.back().first.back().TaskgroupReductionRef; 895 if (!TaskgroupReductionRef) { 896 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 897 SemaRef.Context.VoidPtrTy, ".task_red."); 898 TaskgroupReductionRef = 899 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 900 } 901 } 902 903 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 904 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK, 905 Expr *&TaskgroupDescriptor) const { 906 D = getCanonicalDecl(D); 907 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 908 if (Stack.back().first.empty()) 909 return DSAVarData(); 910 for (iterator I = std::next(Stack.back().first.rbegin(), 1), 911 E = Stack.back().first.rend(); 912 I != E; std::advance(I, 1)) { 913 const DSAInfo &Data = I->SharingMap.lookup(D); 914 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup) 915 continue; 916 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 917 if (!ReductionData.ReductionOp || 918 ReductionData.ReductionOp.is<const Expr *>()) 919 return DSAVarData(); 920 SR = ReductionData.ReductionRange; 921 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>(); 922 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 923 "expression for the descriptor is not " 924 "set."); 925 TaskgroupDescriptor = I->TaskgroupReductionRef; 926 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(), 927 Data.PrivateCopy, I->DefaultAttrLoc); 928 } 929 return DSAVarData(); 930 } 931 932 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 933 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef, 934 Expr *&TaskgroupDescriptor) const { 935 D = getCanonicalDecl(D); 936 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 937 if (Stack.back().first.empty()) 938 return DSAVarData(); 939 for (iterator I = std::next(Stack.back().first.rbegin(), 1), 940 E = Stack.back().first.rend(); 941 I != E; std::advance(I, 1)) { 942 const DSAInfo &Data = I->SharingMap.lookup(D); 943 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup) 944 continue; 945 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 946 if (!ReductionData.ReductionOp || 947 !ReductionData.ReductionOp.is<const Expr *>()) 948 return DSAVarData(); 949 SR = ReductionData.ReductionRange; 950 ReductionRef = ReductionData.ReductionOp.get<const Expr *>(); 951 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 952 "expression for the descriptor is not " 953 "set."); 954 TaskgroupDescriptor = I->TaskgroupReductionRef; 955 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(), 956 Data.PrivateCopy, I->DefaultAttrLoc); 957 } 958 return DSAVarData(); 959 } 960 961 bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const { 962 D = D->getCanonicalDecl(); 963 if (!isStackEmpty()) { 964 iterator I = Iter, E = Stack.back().first.rend(); 965 Scope *TopScope = nullptr; 966 while (I != E && !isParallelOrTaskRegion(I->Directive) && 967 !isOpenMPTargetExecutionDirective(I->Directive)) 968 ++I; 969 if (I == E) 970 return false; 971 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; 972 Scope *CurScope = getCurScope(); 973 while (CurScope != TopScope && !CurScope->isDeclScope(D)) 974 CurScope = CurScope->getParent(); 975 return CurScope != TopScope; 976 } 977 return false; 978 } 979 980 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, 981 bool FromParent) { 982 D = getCanonicalDecl(D); 983 DSAVarData DVar; 984 985 auto *VD = dyn_cast<VarDecl>(D); 986 auto TI = Threadprivates.find(D); 987 if (TI != Threadprivates.end()) { 988 DVar.RefExpr = TI->getSecond().RefExpr.getPointer(); 989 DVar.CKind = OMPC_threadprivate; 990 return DVar; 991 } 992 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) { 993 DVar.RefExpr = buildDeclRefExpr( 994 SemaRef, VD, D->getType().getNonReferenceType(), 995 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation()); 996 DVar.CKind = OMPC_threadprivate; 997 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 998 return DVar; 999 } 1000 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1001 // in a Construct, C/C++, predetermined, p.1] 1002 // Variables appearing in threadprivate directives are threadprivate. 1003 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 1004 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1005 SemaRef.getLangOpts().OpenMPUseTLS && 1006 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 1007 (VD && VD->getStorageClass() == SC_Register && 1008 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 1009 DVar.RefExpr = buildDeclRefExpr( 1010 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation()); 1011 DVar.CKind = OMPC_threadprivate; 1012 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1013 return DVar; 1014 } 1015 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD && 1016 VD->isLocalVarDeclOrParm() && !isStackEmpty() && 1017 !isLoopControlVariable(D).first) { 1018 iterator IterTarget = 1019 std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(), 1020 [](const SharingMapTy &Data) { 1021 return isOpenMPTargetExecutionDirective(Data.Directive); 1022 }); 1023 if (IterTarget != Stack.back().first.rend()) { 1024 iterator ParentIterTarget = std::next(IterTarget, 1); 1025 for (iterator Iter = Stack.back().first.rbegin(); 1026 Iter != ParentIterTarget; std::advance(Iter, 1)) { 1027 if (isOpenMPLocal(VD, Iter)) { 1028 DVar.RefExpr = 1029 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1030 D->getLocation()); 1031 DVar.CKind = OMPC_threadprivate; 1032 return DVar; 1033 } 1034 } 1035 if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) { 1036 auto DSAIter = IterTarget->SharingMap.find(D); 1037 if (DSAIter != IterTarget->SharingMap.end() && 1038 isOpenMPPrivate(DSAIter->getSecond().Attributes)) { 1039 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer(); 1040 DVar.CKind = OMPC_threadprivate; 1041 return DVar; 1042 } 1043 iterator End = Stack.back().first.rend(); 1044 if (!SemaRef.isOpenMPCapturedByRef( 1045 D, std::distance(ParentIterTarget, End))) { 1046 DVar.RefExpr = 1047 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1048 IterTarget->ConstructLoc); 1049 DVar.CKind = OMPC_threadprivate; 1050 return DVar; 1051 } 1052 } 1053 } 1054 } 1055 1056 if (isStackEmpty()) 1057 // Not in OpenMP execution region and top scope was already checked. 1058 return DVar; 1059 1060 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1061 // in a Construct, C/C++, predetermined, p.4] 1062 // Static data members are shared. 1063 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1064 // in a Construct, C/C++, predetermined, p.7] 1065 // Variables with static storage duration that are declared in a scope 1066 // inside the construct are shared. 1067 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; }; 1068 if (VD && VD->isStaticDataMember()) { 1069 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent); 1070 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1071 return DVar; 1072 1073 DVar.CKind = OMPC_shared; 1074 return DVar; 1075 } 1076 1077 QualType Type = D->getType().getNonReferenceType().getCanonicalType(); 1078 bool IsConstant = Type.isConstant(SemaRef.getASTContext()); 1079 Type = SemaRef.getASTContext().getBaseElementType(Type); 1080 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1081 // in a Construct, C/C++, predetermined, p.6] 1082 // Variables with const qualified type having no mutable member are 1083 // shared. 1084 const CXXRecordDecl *RD = 1085 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; 1086 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 1087 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate()) 1088 RD = CTD->getTemplatedDecl(); 1089 if (IsConstant && 1090 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() && 1091 RD->hasMutableFields())) { 1092 // Variables with const-qualified type having no mutable member may be 1093 // listed in a firstprivate clause, even if they are static data members. 1094 DSAVarData DVarTemp = 1095 hasDSA(D, [](OpenMPClauseKind C) { return C == OMPC_firstprivate; }, 1096 MatchesAlways, FromParent); 1097 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr) 1098 return DVarTemp; 1099 1100 DVar.CKind = OMPC_shared; 1101 return DVar; 1102 } 1103 1104 // Explicitly specified attributes and local variables with predetermined 1105 // attributes. 1106 iterator I = Stack.back().first.rbegin(); 1107 iterator EndI = Stack.back().first.rend(); 1108 if (FromParent && I != EndI) 1109 std::advance(I, 1); 1110 auto It = I->SharingMap.find(D); 1111 if (It != I->SharingMap.end()) { 1112 const DSAInfo &Data = It->getSecond(); 1113 DVar.RefExpr = Data.RefExpr.getPointer(); 1114 DVar.PrivateCopy = Data.PrivateCopy; 1115 DVar.CKind = Data.Attributes; 1116 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1117 DVar.DKind = I->Directive; 1118 } 1119 1120 return DVar; 1121 } 1122 1123 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1124 bool FromParent) const { 1125 if (isStackEmpty()) { 1126 iterator I; 1127 return getDSA(I, D); 1128 } 1129 D = getCanonicalDecl(D); 1130 iterator StartI = Stack.back().first.rbegin(); 1131 iterator EndI = Stack.back().first.rend(); 1132 if (FromParent && StartI != EndI) 1133 std::advance(StartI, 1); 1134 return getDSA(StartI, D); 1135 } 1136 1137 const DSAStackTy::DSAVarData 1138 DSAStackTy::hasDSA(ValueDecl *D, 1139 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1140 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1141 bool FromParent) const { 1142 if (isStackEmpty()) 1143 return {}; 1144 D = getCanonicalDecl(D); 1145 iterator I = Stack.back().first.rbegin(); 1146 iterator EndI = Stack.back().first.rend(); 1147 if (FromParent && I != EndI) 1148 std::advance(I, 1); 1149 for (; I != EndI; std::advance(I, 1)) { 1150 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive)) 1151 continue; 1152 iterator NewI = I; 1153 DSAVarData DVar = getDSA(NewI, D); 1154 if (I == NewI && CPred(DVar.CKind)) 1155 return DVar; 1156 } 1157 return {}; 1158 } 1159 1160 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 1161 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1162 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1163 bool FromParent) const { 1164 if (isStackEmpty()) 1165 return {}; 1166 D = getCanonicalDecl(D); 1167 iterator StartI = Stack.back().first.rbegin(); 1168 iterator EndI = Stack.back().first.rend(); 1169 if (FromParent && StartI != EndI) 1170 std::advance(StartI, 1); 1171 if (StartI == EndI || !DPred(StartI->Directive)) 1172 return {}; 1173 iterator NewI = StartI; 1174 DSAVarData DVar = getDSA(NewI, D); 1175 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData(); 1176 } 1177 1178 bool DSAStackTy::hasExplicitDSA( 1179 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1180 unsigned Level, bool NotLastprivate) const { 1181 if (isStackEmpty()) 1182 return false; 1183 D = getCanonicalDecl(D); 1184 auto StartI = Stack.back().first.begin(); 1185 auto EndI = Stack.back().first.end(); 1186 if (std::distance(StartI, EndI) <= (int)Level) 1187 return false; 1188 std::advance(StartI, Level); 1189 auto I = StartI->SharingMap.find(D); 1190 return (I != StartI->SharingMap.end()) && 1191 I->getSecond().RefExpr.getPointer() && 1192 CPred(I->getSecond().Attributes) && 1193 (!NotLastprivate || !I->getSecond().RefExpr.getInt()); 1194 } 1195 1196 bool DSAStackTy::hasExplicitDirective( 1197 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1198 unsigned Level) const { 1199 if (isStackEmpty()) 1200 return false; 1201 auto StartI = Stack.back().first.begin(); 1202 auto EndI = Stack.back().first.end(); 1203 if (std::distance(StartI, EndI) <= (int)Level) 1204 return false; 1205 std::advance(StartI, Level); 1206 return DPred(StartI->Directive); 1207 } 1208 1209 bool DSAStackTy::hasDirective( 1210 const llvm::function_ref<bool(OpenMPDirectiveKind, 1211 const DeclarationNameInfo &, SourceLocation)> 1212 DPred, 1213 bool FromParent) const { 1214 // We look only in the enclosing region. 1215 if (isStackEmpty()) 1216 return false; 1217 auto StartI = std::next(Stack.back().first.rbegin()); 1218 auto EndI = Stack.back().first.rend(); 1219 if (FromParent && StartI != EndI) 1220 StartI = std::next(StartI); 1221 for (auto I = StartI, EE = EndI; I != EE; ++I) { 1222 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 1223 return true; 1224 } 1225 return false; 1226 } 1227 1228 void Sema::InitDataSharingAttributesStack() { 1229 VarDataSharingAttributesStack = new DSAStackTy(*this); 1230 } 1231 1232 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 1233 1234 void Sema::pushOpenMPFunctionRegion() { 1235 DSAStack->pushFunction(); 1236 } 1237 1238 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) { 1239 DSAStack->popFunction(OldFSI); 1240 } 1241 1242 static llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> 1243 isDeclareTargetDeclaration(const ValueDecl *VD) { 1244 for (const Decl *D : VD->redecls()) { 1245 if (!D->hasAttrs()) 1246 continue; 1247 if (const auto *Attr = D->getAttr<OMPDeclareTargetDeclAttr>()) 1248 return Attr->getMapType(); 1249 } 1250 return llvm::None; 1251 } 1252 1253 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const { 1254 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1255 1256 ASTContext &Ctx = getASTContext(); 1257 bool IsByRef = true; 1258 1259 // Find the directive that is associated with the provided scope. 1260 D = cast<ValueDecl>(D->getCanonicalDecl()); 1261 QualType Ty = D->getType(); 1262 1263 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 1264 // This table summarizes how a given variable should be passed to the device 1265 // given its type and the clauses where it appears. This table is based on 1266 // the description in OpenMP 4.5 [2.10.4, target Construct] and 1267 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 1268 // 1269 // ========================================================================= 1270 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 1271 // | |(tofrom:scalar)| | pvt | | | | 1272 // ========================================================================= 1273 // | scl | | | | - | | bycopy| 1274 // | scl | | - | x | - | - | bycopy| 1275 // | scl | | x | - | - | - | null | 1276 // | scl | x | | | - | | byref | 1277 // | scl | x | - | x | - | - | bycopy| 1278 // | scl | x | x | - | - | - | null | 1279 // | scl | | - | - | - | x | byref | 1280 // | scl | x | - | - | - | x | byref | 1281 // 1282 // | agg | n.a. | | | - | | byref | 1283 // | agg | n.a. | - | x | - | - | byref | 1284 // | agg | n.a. | x | - | - | - | null | 1285 // | agg | n.a. | - | - | - | x | byref | 1286 // | agg | n.a. | - | - | - | x[] | byref | 1287 // 1288 // | ptr | n.a. | | | - | | bycopy| 1289 // | ptr | n.a. | - | x | - | - | bycopy| 1290 // | ptr | n.a. | x | - | - | - | null | 1291 // | ptr | n.a. | - | - | - | x | byref | 1292 // | ptr | n.a. | - | - | - | x[] | bycopy| 1293 // | ptr | n.a. | - | - | x | | bycopy| 1294 // | ptr | n.a. | - | - | x | x | bycopy| 1295 // | ptr | n.a. | - | - | x | x[] | bycopy| 1296 // ========================================================================= 1297 // Legend: 1298 // scl - scalar 1299 // ptr - pointer 1300 // agg - aggregate 1301 // x - applies 1302 // - - invalid in this combination 1303 // [] - mapped with an array section 1304 // byref - should be mapped by reference 1305 // byval - should be mapped by value 1306 // null - initialize a local variable to null on the device 1307 // 1308 // Observations: 1309 // - All scalar declarations that show up in a map clause have to be passed 1310 // by reference, because they may have been mapped in the enclosing data 1311 // environment. 1312 // - If the scalar value does not fit the size of uintptr, it has to be 1313 // passed by reference, regardless the result in the table above. 1314 // - For pointers mapped by value that have either an implicit map or an 1315 // array section, the runtime library may pass the NULL value to the 1316 // device instead of the value passed to it by the compiler. 1317 1318 if (Ty->isReferenceType()) 1319 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 1320 1321 // Locate map clauses and see if the variable being captured is referred to 1322 // in any of those clauses. Here we only care about variables, not fields, 1323 // because fields are part of aggregates. 1324 bool IsVariableUsedInMapClause = false; 1325 bool IsVariableAssociatedWithSection = false; 1326 1327 DSAStack->checkMappableExprComponentListsForDeclAtLevel( 1328 D, Level, 1329 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D]( 1330 OMPClauseMappableExprCommon::MappableExprComponentListRef 1331 MapExprComponents, 1332 OpenMPClauseKind WhereFoundClauseKind) { 1333 // Only the map clause information influences how a variable is 1334 // captured. E.g. is_device_ptr does not require changing the default 1335 // behavior. 1336 if (WhereFoundClauseKind != OMPC_map) 1337 return false; 1338 1339 auto EI = MapExprComponents.rbegin(); 1340 auto EE = MapExprComponents.rend(); 1341 1342 assert(EI != EE && "Invalid map expression!"); 1343 1344 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 1345 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 1346 1347 ++EI; 1348 if (EI == EE) 1349 return false; 1350 1351 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 1352 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 1353 isa<MemberExpr>(EI->getAssociatedExpression())) { 1354 IsVariableAssociatedWithSection = true; 1355 // There is nothing more we need to know about this variable. 1356 return true; 1357 } 1358 1359 // Keep looking for more map info. 1360 return false; 1361 }); 1362 1363 if (IsVariableUsedInMapClause) { 1364 // If variable is identified in a map clause it is always captured by 1365 // reference except if it is a pointer that is dereferenced somehow. 1366 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 1367 } else { 1368 // By default, all the data that has a scalar type is mapped by copy 1369 // (except for reduction variables). 1370 IsByRef = 1371 !Ty->isScalarType() || 1372 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar || 1373 DSAStack->hasExplicitDSA( 1374 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level); 1375 } 1376 } 1377 1378 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 1379 IsByRef = 1380 !DSAStack->hasExplicitDSA( 1381 D, 1382 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; }, 1383 Level, /*NotLastprivate=*/true) && 1384 // If the variable is artificial and must be captured by value - try to 1385 // capture by value. 1386 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() && 1387 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()); 1388 } 1389 1390 // When passing data by copy, we need to make sure it fits the uintptr size 1391 // and alignment, because the runtime library only deals with uintptr types. 1392 // If it does not fit the uintptr size, we need to pass the data by reference 1393 // instead. 1394 if (!IsByRef && 1395 (Ctx.getTypeSizeInChars(Ty) > 1396 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 1397 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 1398 IsByRef = true; 1399 } 1400 1401 return IsByRef; 1402 } 1403 1404 unsigned Sema::getOpenMPNestingLevel() const { 1405 assert(getLangOpts().OpenMP); 1406 return DSAStack->getNestingLevel(); 1407 } 1408 1409 bool Sema::isInOpenMPTargetExecutionDirective() const { 1410 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) && 1411 !DSAStack->isClauseParsingMode()) || 1412 DSAStack->hasDirective( 1413 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 1414 SourceLocation) -> bool { 1415 return isOpenMPTargetExecutionDirective(K); 1416 }, 1417 false); 1418 } 1419 1420 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) const { 1421 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1422 D = getCanonicalDecl(D); 1423 1424 // If we are attempting to capture a global variable in a directive with 1425 // 'target' we return true so that this global is also mapped to the device. 1426 // 1427 auto *VD = dyn_cast<VarDecl>(D); 1428 if (VD && !VD->hasLocalStorage() && isInOpenMPTargetExecutionDirective()) { 1429 // If the declaration is enclosed in a 'declare target' directive, 1430 // then it should not be captured. 1431 // 1432 if (isDeclareTargetDeclaration(VD)) 1433 return nullptr; 1434 return VD; 1435 } 1436 1437 if (DSAStack->getCurrentDirective() != OMPD_unknown && 1438 (!DSAStack->isClauseParsingMode() || 1439 DSAStack->getParentDirective() != OMPD_unknown)) { 1440 auto &&Info = DSAStack->isLoopControlVariable(D); 1441 if (Info.first || 1442 (VD && VD->hasLocalStorage() && 1443 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) || 1444 (VD && DSAStack->isForceVarCapturing())) 1445 return VD ? VD : Info.second; 1446 DSAStackTy::DSAVarData DVarPrivate = 1447 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 1448 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind)) 1449 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1450 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, 1451 [](OpenMPDirectiveKind) { return true; }, 1452 DSAStack->isClauseParsingMode()); 1453 if (DVarPrivate.CKind != OMPC_unknown) 1454 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1455 } 1456 return nullptr; 1457 } 1458 1459 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex, 1460 unsigned Level) const { 1461 SmallVector<OpenMPDirectiveKind, 4> Regions; 1462 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level)); 1463 FunctionScopesIndex -= Regions.size(); 1464 } 1465 1466 bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const { 1467 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1468 return DSAStack->hasExplicitDSA( 1469 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) || 1470 (DSAStack->isClauseParsingMode() && 1471 DSAStack->getClauseParsingMode() == OMPC_private) || 1472 // Consider taskgroup reduction descriptor variable a private to avoid 1473 // possible capture in the region. 1474 (DSAStack->hasExplicitDirective( 1475 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; }, 1476 Level) && 1477 DSAStack->isTaskgroupReductionRef(D, Level)); 1478 } 1479 1480 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D, 1481 unsigned Level) { 1482 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1483 D = getCanonicalDecl(D); 1484 OpenMPClauseKind OMPC = OMPC_unknown; 1485 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) { 1486 const unsigned NewLevel = I - 1; 1487 if (DSAStack->hasExplicitDSA(D, 1488 [&OMPC](const OpenMPClauseKind K) { 1489 if (isOpenMPPrivate(K)) { 1490 OMPC = K; 1491 return true; 1492 } 1493 return false; 1494 }, 1495 NewLevel)) 1496 break; 1497 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel( 1498 D, NewLevel, 1499 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 1500 OpenMPClauseKind) { return true; })) { 1501 OMPC = OMPC_map; 1502 break; 1503 } 1504 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 1505 NewLevel)) { 1506 OMPC = OMPC_map; 1507 if (D->getType()->isScalarType() && 1508 DSAStack->getDefaultDMAAtLevel(NewLevel) != 1509 DefaultMapAttributes::DMA_tofrom_scalar) 1510 OMPC = OMPC_firstprivate; 1511 break; 1512 } 1513 } 1514 if (OMPC != OMPC_unknown) 1515 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC)); 1516 } 1517 1518 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, 1519 unsigned Level) const { 1520 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1521 // Return true if the current level is no longer enclosed in a target region. 1522 1523 const auto *VD = dyn_cast<VarDecl>(D); 1524 return VD && !VD->hasLocalStorage() && 1525 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 1526 Level); 1527 } 1528 1529 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 1530 1531 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 1532 const DeclarationNameInfo &DirName, 1533 Scope *CurScope, SourceLocation Loc) { 1534 DSAStack->push(DKind, DirName, CurScope, Loc); 1535 PushExpressionEvaluationContext( 1536 ExpressionEvaluationContext::PotentiallyEvaluated); 1537 } 1538 1539 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 1540 DSAStack->setClauseParsingMode(K); 1541 } 1542 1543 void Sema::EndOpenMPClause() { 1544 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 1545 } 1546 1547 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 1548 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 1549 // A variable of class type (or array thereof) that appears in a lastprivate 1550 // clause requires an accessible, unambiguous default constructor for the 1551 // class type, unless the list item is also specified in a firstprivate 1552 // clause. 1553 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 1554 for (OMPClause *C : D->clauses()) { 1555 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 1556 SmallVector<Expr *, 8> PrivateCopies; 1557 for (Expr *DE : Clause->varlists()) { 1558 if (DE->isValueDependent() || DE->isTypeDependent()) { 1559 PrivateCopies.push_back(nullptr); 1560 continue; 1561 } 1562 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 1563 auto *VD = cast<VarDecl>(DRE->getDecl()); 1564 QualType Type = VD->getType().getNonReferenceType(); 1565 const DSAStackTy::DSAVarData DVar = 1566 DSAStack->getTopDSA(VD, /*FromParent=*/false); 1567 if (DVar.CKind == OMPC_lastprivate) { 1568 // Generate helper private variable and initialize it with the 1569 // default value. The address of the original variable is replaced 1570 // by the address of the new private variable in CodeGen. This new 1571 // variable is not added to IdResolver, so the code in the OpenMP 1572 // region uses original variable for proper diagnostics. 1573 VarDecl *VDPrivate = buildVarDecl( 1574 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 1575 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE); 1576 ActOnUninitializedDecl(VDPrivate); 1577 if (VDPrivate->isInvalidDecl()) 1578 continue; 1579 PrivateCopies.push_back(buildDeclRefExpr( 1580 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 1581 } else { 1582 // The variable is also a firstprivate, so initialization sequence 1583 // for private copy is generated already. 1584 PrivateCopies.push_back(nullptr); 1585 } 1586 } 1587 // Set initializers to private copies if no errors were found. 1588 if (PrivateCopies.size() == Clause->varlist_size()) 1589 Clause->setPrivateCopies(PrivateCopies); 1590 } 1591 } 1592 } 1593 1594 DSAStack->pop(); 1595 DiscardCleanupsInEvaluationContext(); 1596 PopExpressionEvaluationContext(); 1597 } 1598 1599 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 1600 Expr *NumIterations, Sema &SemaRef, 1601 Scope *S, DSAStackTy *Stack); 1602 1603 namespace { 1604 1605 class VarDeclFilterCCC final : public CorrectionCandidateCallback { 1606 private: 1607 Sema &SemaRef; 1608 1609 public: 1610 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 1611 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1612 NamedDecl *ND = Candidate.getCorrectionDecl(); 1613 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 1614 return VD->hasGlobalStorage() && 1615 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1616 SemaRef.getCurScope()); 1617 } 1618 return false; 1619 } 1620 }; 1621 1622 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback { 1623 private: 1624 Sema &SemaRef; 1625 1626 public: 1627 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 1628 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1629 NamedDecl *ND = Candidate.getCorrectionDecl(); 1630 if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) { 1631 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1632 SemaRef.getCurScope()); 1633 } 1634 return false; 1635 } 1636 }; 1637 1638 } // namespace 1639 1640 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 1641 CXXScopeSpec &ScopeSpec, 1642 const DeclarationNameInfo &Id) { 1643 LookupResult Lookup(*this, Id, LookupOrdinaryName); 1644 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 1645 1646 if (Lookup.isAmbiguous()) 1647 return ExprError(); 1648 1649 VarDecl *VD; 1650 if (!Lookup.isSingleResult()) { 1651 if (TypoCorrection Corrected = CorrectTypo( 1652 Id, LookupOrdinaryName, CurScope, nullptr, 1653 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) { 1654 diagnoseTypo(Corrected, 1655 PDiag(Lookup.empty() 1656 ? diag::err_undeclared_var_use_suggest 1657 : diag::err_omp_expected_var_arg_suggest) 1658 << Id.getName()); 1659 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 1660 } else { 1661 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 1662 : diag::err_omp_expected_var_arg) 1663 << Id.getName(); 1664 return ExprError(); 1665 } 1666 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) { 1667 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 1668 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 1669 return ExprError(); 1670 } 1671 Lookup.suppressDiagnostics(); 1672 1673 // OpenMP [2.9.2, Syntax, C/C++] 1674 // Variables must be file-scope, namespace-scope, or static block-scope. 1675 if (!VD->hasGlobalStorage()) { 1676 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 1677 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal(); 1678 bool IsDecl = 1679 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1680 Diag(VD->getLocation(), 1681 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1682 << VD; 1683 return ExprError(); 1684 } 1685 1686 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 1687 NamedDecl *ND = CanonicalVD; 1688 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 1689 // A threadprivate directive for file-scope variables must appear outside 1690 // any definition or declaration. 1691 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 1692 !getCurLexicalContext()->isTranslationUnit()) { 1693 Diag(Id.getLoc(), diag::err_omp_var_scope) 1694 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1695 bool IsDecl = 1696 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1697 Diag(VD->getLocation(), 1698 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1699 << VD; 1700 return ExprError(); 1701 } 1702 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 1703 // A threadprivate directive for static class member variables must appear 1704 // in the class definition, in the same scope in which the member 1705 // variables are declared. 1706 if (CanonicalVD->isStaticDataMember() && 1707 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 1708 Diag(Id.getLoc(), diag::err_omp_var_scope) 1709 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1710 bool IsDecl = 1711 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1712 Diag(VD->getLocation(), 1713 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1714 << VD; 1715 return ExprError(); 1716 } 1717 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 1718 // A threadprivate directive for namespace-scope variables must appear 1719 // outside any definition or declaration other than the namespace 1720 // definition itself. 1721 if (CanonicalVD->getDeclContext()->isNamespace() && 1722 (!getCurLexicalContext()->isFileContext() || 1723 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 1724 Diag(Id.getLoc(), diag::err_omp_var_scope) 1725 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1726 bool IsDecl = 1727 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1728 Diag(VD->getLocation(), 1729 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1730 << VD; 1731 return ExprError(); 1732 } 1733 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 1734 // A threadprivate directive for static block-scope variables must appear 1735 // in the scope of the variable and not in a nested scope. 1736 if (CanonicalVD->isStaticLocal() && CurScope && 1737 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 1738 Diag(Id.getLoc(), diag::err_omp_var_scope) 1739 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1740 bool IsDecl = 1741 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1742 Diag(VD->getLocation(), 1743 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1744 << VD; 1745 return ExprError(); 1746 } 1747 1748 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 1749 // A threadprivate directive must lexically precede all references to any 1750 // of the variables in its list. 1751 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) { 1752 Diag(Id.getLoc(), diag::err_omp_var_used) 1753 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1754 return ExprError(); 1755 } 1756 1757 QualType ExprType = VD->getType().getNonReferenceType(); 1758 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 1759 SourceLocation(), VD, 1760 /*RefersToEnclosingVariableOrCapture=*/false, 1761 Id.getLoc(), ExprType, VK_LValue); 1762 } 1763 1764 Sema::DeclGroupPtrTy 1765 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 1766 ArrayRef<Expr *> VarList) { 1767 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 1768 CurContext->addDecl(D); 1769 return DeclGroupPtrTy::make(DeclGroupRef(D)); 1770 } 1771 return nullptr; 1772 } 1773 1774 namespace { 1775 class LocalVarRefChecker final 1776 : public ConstStmtVisitor<LocalVarRefChecker, bool> { 1777 Sema &SemaRef; 1778 1779 public: 1780 bool VisitDeclRefExpr(const DeclRefExpr *E) { 1781 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 1782 if (VD->hasLocalStorage()) { 1783 SemaRef.Diag(E->getLocStart(), 1784 diag::err_omp_local_var_in_threadprivate_init) 1785 << E->getSourceRange(); 1786 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 1787 << VD << VD->getSourceRange(); 1788 return true; 1789 } 1790 } 1791 return false; 1792 } 1793 bool VisitStmt(const Stmt *S) { 1794 for (const Stmt *Child : S->children()) { 1795 if (Child && Visit(Child)) 1796 return true; 1797 } 1798 return false; 1799 } 1800 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 1801 }; 1802 } // namespace 1803 1804 OMPThreadPrivateDecl * 1805 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 1806 SmallVector<Expr *, 8> Vars; 1807 for (Expr *RefExpr : VarList) { 1808 auto *DE = cast<DeclRefExpr>(RefExpr); 1809 auto *VD = cast<VarDecl>(DE->getDecl()); 1810 SourceLocation ILoc = DE->getExprLoc(); 1811 1812 // Mark variable as used. 1813 VD->setReferenced(); 1814 VD->markUsed(Context); 1815 1816 QualType QType = VD->getType(); 1817 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 1818 // It will be analyzed later. 1819 Vars.push_back(DE); 1820 continue; 1821 } 1822 1823 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 1824 // A threadprivate variable must not have an incomplete type. 1825 if (RequireCompleteType(ILoc, VD->getType(), 1826 diag::err_omp_threadprivate_incomplete_type)) { 1827 continue; 1828 } 1829 1830 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 1831 // A threadprivate variable must not have a reference type. 1832 if (VD->getType()->isReferenceType()) { 1833 Diag(ILoc, diag::err_omp_ref_type_arg) 1834 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 1835 bool IsDecl = 1836 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1837 Diag(VD->getLocation(), 1838 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1839 << VD; 1840 continue; 1841 } 1842 1843 // Check if this is a TLS variable. If TLS is not being supported, produce 1844 // the corresponding diagnostic. 1845 if ((VD->getTLSKind() != VarDecl::TLS_None && 1846 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1847 getLangOpts().OpenMPUseTLS && 1848 getASTContext().getTargetInfo().isTLSSupported())) || 1849 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 1850 !VD->isLocalVarDecl())) { 1851 Diag(ILoc, diag::err_omp_var_thread_local) 1852 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 1853 bool IsDecl = 1854 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1855 Diag(VD->getLocation(), 1856 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1857 << VD; 1858 continue; 1859 } 1860 1861 // Check if initial value of threadprivate variable reference variable with 1862 // local storage (it is not supported by runtime). 1863 if (const Expr *Init = VD->getAnyInitializer()) { 1864 LocalVarRefChecker Checker(*this); 1865 if (Checker.Visit(Init)) 1866 continue; 1867 } 1868 1869 Vars.push_back(RefExpr); 1870 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 1871 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 1872 Context, SourceRange(Loc, Loc))); 1873 if (ASTMutationListener *ML = Context.getASTMutationListener()) 1874 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 1875 } 1876 OMPThreadPrivateDecl *D = nullptr; 1877 if (!Vars.empty()) { 1878 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 1879 Vars); 1880 D->setAccess(AS_public); 1881 } 1882 return D; 1883 } 1884 1885 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 1886 const ValueDecl *D, 1887 const DSAStackTy::DSAVarData &DVar, 1888 bool IsLoopIterVar = false) { 1889 if (DVar.RefExpr) { 1890 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 1891 << getOpenMPClauseName(DVar.CKind); 1892 return; 1893 } 1894 enum { 1895 PDSA_StaticMemberShared, 1896 PDSA_StaticLocalVarShared, 1897 PDSA_LoopIterVarPrivate, 1898 PDSA_LoopIterVarLinear, 1899 PDSA_LoopIterVarLastprivate, 1900 PDSA_ConstVarShared, 1901 PDSA_GlobalVarShared, 1902 PDSA_TaskVarFirstprivate, 1903 PDSA_LocalVarPrivate, 1904 PDSA_Implicit 1905 } Reason = PDSA_Implicit; 1906 bool ReportHint = false; 1907 auto ReportLoc = D->getLocation(); 1908 auto *VD = dyn_cast<VarDecl>(D); 1909 if (IsLoopIterVar) { 1910 if (DVar.CKind == OMPC_private) 1911 Reason = PDSA_LoopIterVarPrivate; 1912 else if (DVar.CKind == OMPC_lastprivate) 1913 Reason = PDSA_LoopIterVarLastprivate; 1914 else 1915 Reason = PDSA_LoopIterVarLinear; 1916 } else if (isOpenMPTaskingDirective(DVar.DKind) && 1917 DVar.CKind == OMPC_firstprivate) { 1918 Reason = PDSA_TaskVarFirstprivate; 1919 ReportLoc = DVar.ImplicitDSALoc; 1920 } else if (VD && VD->isStaticLocal()) 1921 Reason = PDSA_StaticLocalVarShared; 1922 else if (VD && VD->isStaticDataMember()) 1923 Reason = PDSA_StaticMemberShared; 1924 else if (VD && VD->isFileVarDecl()) 1925 Reason = PDSA_GlobalVarShared; 1926 else if (D->getType().isConstant(SemaRef.getASTContext())) 1927 Reason = PDSA_ConstVarShared; 1928 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 1929 ReportHint = true; 1930 Reason = PDSA_LocalVarPrivate; 1931 } 1932 if (Reason != PDSA_Implicit) { 1933 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 1934 << Reason << ReportHint 1935 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 1936 } else if (DVar.ImplicitDSALoc.isValid()) { 1937 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 1938 << getOpenMPClauseName(DVar.CKind); 1939 } 1940 } 1941 1942 namespace { 1943 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> { 1944 DSAStackTy *Stack; 1945 Sema &SemaRef; 1946 bool ErrorFound = false; 1947 CapturedStmt *CS = nullptr; 1948 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate; 1949 llvm::SmallVector<Expr *, 4> ImplicitMap; 1950 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA; 1951 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations; 1952 1953 public: 1954 void VisitDeclRefExpr(DeclRefExpr *E) { 1955 if (E->isTypeDependent() || E->isValueDependent() || 1956 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 1957 return; 1958 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 1959 VD = VD->getCanonicalDecl(); 1960 // Skip internally declared variables. 1961 if (VD->hasLocalStorage() && !CS->capturesVariable(VD)) 1962 return; 1963 1964 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 1965 // Check if the variable has explicit DSA set and stop analysis if it so. 1966 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second) 1967 return; 1968 1969 // Skip internally declared static variables. 1970 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 1971 isDeclareTargetDeclaration(VD); 1972 if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) && 1973 (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link)) 1974 return; 1975 1976 SourceLocation ELoc = E->getExprLoc(); 1977 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 1978 // The default(none) clause requires that each variable that is referenced 1979 // in the construct, and does not have a predetermined data-sharing 1980 // attribute, must have its data-sharing attribute explicitly determined 1981 // by being listed in a data-sharing attribute clause. 1982 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && 1983 isParallelOrTaskRegion(DKind) && 1984 VarsWithInheritedDSA.count(VD) == 0) { 1985 VarsWithInheritedDSA[VD] = E; 1986 return; 1987 } 1988 1989 if (isOpenMPTargetExecutionDirective(DKind) && 1990 !Stack->isLoopControlVariable(VD).first) { 1991 if (!Stack->checkMappableExprComponentListsForDecl( 1992 VD, /*CurrentRegionOnly=*/true, 1993 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 1994 StackComponents, 1995 OpenMPClauseKind) { 1996 // Variable is used if it has been marked as an array, array 1997 // section or the variable iself. 1998 return StackComponents.size() == 1 || 1999 std::all_of( 2000 std::next(StackComponents.rbegin()), 2001 StackComponents.rend(), 2002 [](const OMPClauseMappableExprCommon:: 2003 MappableComponent &MC) { 2004 return MC.getAssociatedDeclaration() == 2005 nullptr && 2006 (isa<OMPArraySectionExpr>( 2007 MC.getAssociatedExpression()) || 2008 isa<ArraySubscriptExpr>( 2009 MC.getAssociatedExpression())); 2010 }); 2011 })) { 2012 bool IsFirstprivate = false; 2013 // By default lambdas are captured as firstprivates. 2014 if (const auto *RD = 2015 VD->getType().getNonReferenceType()->getAsCXXRecordDecl()) 2016 IsFirstprivate = RD->isLambda(); 2017 IsFirstprivate = 2018 IsFirstprivate || 2019 (VD->getType().getNonReferenceType()->isScalarType() && 2020 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res); 2021 if (IsFirstprivate) 2022 ImplicitFirstprivate.emplace_back(E); 2023 else 2024 ImplicitMap.emplace_back(E); 2025 return; 2026 } 2027 } 2028 2029 // OpenMP [2.9.3.6, Restrictions, p.2] 2030 // A list item that appears in a reduction clause of the innermost 2031 // enclosing worksharing or parallel construct may not be accessed in an 2032 // explicit task. 2033 DVar = Stack->hasInnermostDSA( 2034 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 2035 [](OpenMPDirectiveKind K) { 2036 return isOpenMPParallelDirective(K) || 2037 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 2038 }, 2039 /*FromParent=*/true); 2040 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 2041 ErrorFound = true; 2042 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 2043 reportOriginalDsa(SemaRef, Stack, VD, DVar); 2044 return; 2045 } 2046 2047 // Define implicit data-sharing attributes for task. 2048 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false); 2049 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 2050 !Stack->isLoopControlVariable(VD).first) 2051 ImplicitFirstprivate.push_back(E); 2052 } 2053 } 2054 void VisitMemberExpr(MemberExpr *E) { 2055 if (E->isTypeDependent() || E->isValueDependent() || 2056 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 2057 return; 2058 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 2059 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 2060 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 2061 if (!FD) 2062 return; 2063 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false); 2064 // Check if the variable has explicit DSA set and stop analysis if it 2065 // so. 2066 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second) 2067 return; 2068 2069 if (isOpenMPTargetExecutionDirective(DKind) && 2070 !Stack->isLoopControlVariable(FD).first && 2071 !Stack->checkMappableExprComponentListsForDecl( 2072 FD, /*CurrentRegionOnly=*/true, 2073 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 2074 StackComponents, 2075 OpenMPClauseKind) { 2076 return isa<CXXThisExpr>( 2077 cast<MemberExpr>( 2078 StackComponents.back().getAssociatedExpression()) 2079 ->getBase() 2080 ->IgnoreParens()); 2081 })) { 2082 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 2083 // A bit-field cannot appear in a map clause. 2084 // 2085 if (FD->isBitField()) 2086 return; 2087 ImplicitMap.emplace_back(E); 2088 return; 2089 } 2090 2091 SourceLocation ELoc = E->getExprLoc(); 2092 // OpenMP [2.9.3.6, Restrictions, p.2] 2093 // A list item that appears in a reduction clause of the innermost 2094 // enclosing worksharing or parallel construct may not be accessed in 2095 // an explicit task. 2096 DVar = Stack->hasInnermostDSA( 2097 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 2098 [](OpenMPDirectiveKind K) { 2099 return isOpenMPParallelDirective(K) || 2100 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 2101 }, 2102 /*FromParent=*/true); 2103 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 2104 ErrorFound = true; 2105 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 2106 reportOriginalDsa(SemaRef, Stack, FD, DVar); 2107 return; 2108 } 2109 2110 // Define implicit data-sharing attributes for task. 2111 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false); 2112 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 2113 !Stack->isLoopControlVariable(FD).first) 2114 ImplicitFirstprivate.push_back(E); 2115 return; 2116 } 2117 if (isOpenMPTargetExecutionDirective(DKind)) { 2118 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 2119 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map, 2120 /*NoDiagnose=*/true)) 2121 return; 2122 const auto *VD = cast<ValueDecl>( 2123 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl()); 2124 if (!Stack->checkMappableExprComponentListsForDecl( 2125 VD, /*CurrentRegionOnly=*/true, 2126 [&CurComponents]( 2127 OMPClauseMappableExprCommon::MappableExprComponentListRef 2128 StackComponents, 2129 OpenMPClauseKind) { 2130 auto CCI = CurComponents.rbegin(); 2131 auto CCE = CurComponents.rend(); 2132 for (const auto &SC : llvm::reverse(StackComponents)) { 2133 // Do both expressions have the same kind? 2134 if (CCI->getAssociatedExpression()->getStmtClass() != 2135 SC.getAssociatedExpression()->getStmtClass()) 2136 if (!(isa<OMPArraySectionExpr>( 2137 SC.getAssociatedExpression()) && 2138 isa<ArraySubscriptExpr>( 2139 CCI->getAssociatedExpression()))) 2140 return false; 2141 2142 const Decl *CCD = CCI->getAssociatedDeclaration(); 2143 const Decl *SCD = SC.getAssociatedDeclaration(); 2144 CCD = CCD ? CCD->getCanonicalDecl() : nullptr; 2145 SCD = SCD ? SCD->getCanonicalDecl() : nullptr; 2146 if (SCD != CCD) 2147 return false; 2148 std::advance(CCI, 1); 2149 if (CCI == CCE) 2150 break; 2151 } 2152 return true; 2153 })) { 2154 Visit(E->getBase()); 2155 } 2156 } else { 2157 Visit(E->getBase()); 2158 } 2159 } 2160 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 2161 for (OMPClause *C : S->clauses()) { 2162 // Skip analysis of arguments of implicitly defined firstprivate clause 2163 // for task|target directives. 2164 // Skip analysis of arguments of implicitly defined map clause for target 2165 // directives. 2166 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) && 2167 C->isImplicit())) { 2168 for (Stmt *CC : C->children()) { 2169 if (CC) 2170 Visit(CC); 2171 } 2172 } 2173 } 2174 } 2175 void VisitStmt(Stmt *S) { 2176 for (Stmt *C : S->children()) { 2177 if (C && !isa<OMPExecutableDirective>(C)) 2178 Visit(C); 2179 } 2180 } 2181 2182 bool isErrorFound() const { return ErrorFound; } 2183 ArrayRef<Expr *> getImplicitFirstprivate() const { 2184 return ImplicitFirstprivate; 2185 } 2186 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; } 2187 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const { 2188 return VarsWithInheritedDSA; 2189 } 2190 2191 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 2192 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {} 2193 }; 2194 } // namespace 2195 2196 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 2197 switch (DKind) { 2198 case OMPD_parallel: 2199 case OMPD_parallel_for: 2200 case OMPD_parallel_for_simd: 2201 case OMPD_parallel_sections: 2202 case OMPD_teams: 2203 case OMPD_teams_distribute: 2204 case OMPD_teams_distribute_simd: { 2205 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2206 QualType KmpInt32PtrTy = 2207 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2208 Sema::CapturedParamNameType Params[] = { 2209 std::make_pair(".global_tid.", KmpInt32PtrTy), 2210 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2211 std::make_pair(StringRef(), QualType()) // __context with shared vars 2212 }; 2213 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2214 Params); 2215 break; 2216 } 2217 case OMPD_target_teams: 2218 case OMPD_target_parallel: 2219 case OMPD_target_parallel_for: 2220 case OMPD_target_parallel_for_simd: 2221 case OMPD_target_teams_distribute: 2222 case OMPD_target_teams_distribute_simd: { 2223 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2224 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2225 QualType KmpInt32PtrTy = 2226 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2227 QualType Args[] = {VoidPtrTy}; 2228 FunctionProtoType::ExtProtoInfo EPI; 2229 EPI.Variadic = true; 2230 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2231 Sema::CapturedParamNameType Params[] = { 2232 std::make_pair(".global_tid.", KmpInt32Ty), 2233 std::make_pair(".part_id.", KmpInt32PtrTy), 2234 std::make_pair(".privates.", VoidPtrTy), 2235 std::make_pair( 2236 ".copy_fn.", 2237 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2238 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2239 std::make_pair(StringRef(), QualType()) // __context with shared vars 2240 }; 2241 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2242 Params); 2243 // Mark this captured region as inlined, because we don't use outlined 2244 // function directly. 2245 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2246 AlwaysInlineAttr::CreateImplicit( 2247 Context, AlwaysInlineAttr::Keyword_forceinline)); 2248 Sema::CapturedParamNameType ParamsTarget[] = { 2249 std::make_pair(StringRef(), QualType()) // __context with shared vars 2250 }; 2251 // Start a captured region for 'target' with no implicit parameters. 2252 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2253 ParamsTarget); 2254 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 2255 std::make_pair(".global_tid.", KmpInt32PtrTy), 2256 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2257 std::make_pair(StringRef(), QualType()) // __context with shared vars 2258 }; 2259 // Start a captured region for 'teams' or 'parallel'. Both regions have 2260 // the same implicit parameters. 2261 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2262 ParamsTeamsOrParallel); 2263 break; 2264 } 2265 case OMPD_target: 2266 case OMPD_target_simd: { 2267 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2268 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2269 QualType KmpInt32PtrTy = 2270 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2271 QualType Args[] = {VoidPtrTy}; 2272 FunctionProtoType::ExtProtoInfo EPI; 2273 EPI.Variadic = true; 2274 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2275 Sema::CapturedParamNameType Params[] = { 2276 std::make_pair(".global_tid.", KmpInt32Ty), 2277 std::make_pair(".part_id.", KmpInt32PtrTy), 2278 std::make_pair(".privates.", VoidPtrTy), 2279 std::make_pair( 2280 ".copy_fn.", 2281 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2282 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2283 std::make_pair(StringRef(), QualType()) // __context with shared vars 2284 }; 2285 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2286 Params); 2287 // Mark this captured region as inlined, because we don't use outlined 2288 // function directly. 2289 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2290 AlwaysInlineAttr::CreateImplicit( 2291 Context, AlwaysInlineAttr::Keyword_forceinline)); 2292 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2293 std::make_pair(StringRef(), QualType())); 2294 break; 2295 } 2296 case OMPD_simd: 2297 case OMPD_for: 2298 case OMPD_for_simd: 2299 case OMPD_sections: 2300 case OMPD_section: 2301 case OMPD_single: 2302 case OMPD_master: 2303 case OMPD_critical: 2304 case OMPD_taskgroup: 2305 case OMPD_distribute: 2306 case OMPD_distribute_simd: 2307 case OMPD_ordered: 2308 case OMPD_atomic: 2309 case OMPD_target_data: { 2310 Sema::CapturedParamNameType Params[] = { 2311 std::make_pair(StringRef(), QualType()) // __context with shared vars 2312 }; 2313 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2314 Params); 2315 break; 2316 } 2317 case OMPD_task: { 2318 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2319 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2320 QualType KmpInt32PtrTy = 2321 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2322 QualType Args[] = {VoidPtrTy}; 2323 FunctionProtoType::ExtProtoInfo EPI; 2324 EPI.Variadic = true; 2325 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2326 Sema::CapturedParamNameType Params[] = { 2327 std::make_pair(".global_tid.", KmpInt32Ty), 2328 std::make_pair(".part_id.", KmpInt32PtrTy), 2329 std::make_pair(".privates.", VoidPtrTy), 2330 std::make_pair( 2331 ".copy_fn.", 2332 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2333 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2334 std::make_pair(StringRef(), QualType()) // __context with shared vars 2335 }; 2336 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2337 Params); 2338 // Mark this captured region as inlined, because we don't use outlined 2339 // function directly. 2340 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2341 AlwaysInlineAttr::CreateImplicit( 2342 Context, AlwaysInlineAttr::Keyword_forceinline)); 2343 break; 2344 } 2345 case OMPD_taskloop: 2346 case OMPD_taskloop_simd: { 2347 QualType KmpInt32Ty = 2348 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 2349 .withConst(); 2350 QualType KmpUInt64Ty = 2351 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 2352 .withConst(); 2353 QualType KmpInt64Ty = 2354 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 2355 .withConst(); 2356 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2357 QualType KmpInt32PtrTy = 2358 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2359 QualType Args[] = {VoidPtrTy}; 2360 FunctionProtoType::ExtProtoInfo EPI; 2361 EPI.Variadic = true; 2362 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2363 Sema::CapturedParamNameType Params[] = { 2364 std::make_pair(".global_tid.", KmpInt32Ty), 2365 std::make_pair(".part_id.", KmpInt32PtrTy), 2366 std::make_pair(".privates.", VoidPtrTy), 2367 std::make_pair( 2368 ".copy_fn.", 2369 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2370 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2371 std::make_pair(".lb.", KmpUInt64Ty), 2372 std::make_pair(".ub.", KmpUInt64Ty), 2373 std::make_pair(".st.", KmpInt64Ty), 2374 std::make_pair(".liter.", KmpInt32Ty), 2375 std::make_pair(".reductions.", VoidPtrTy), 2376 std::make_pair(StringRef(), QualType()) // __context with shared vars 2377 }; 2378 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2379 Params); 2380 // Mark this captured region as inlined, because we don't use outlined 2381 // function directly. 2382 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2383 AlwaysInlineAttr::CreateImplicit( 2384 Context, AlwaysInlineAttr::Keyword_forceinline)); 2385 break; 2386 } 2387 case OMPD_distribute_parallel_for_simd: 2388 case OMPD_distribute_parallel_for: { 2389 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2390 QualType KmpInt32PtrTy = 2391 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2392 Sema::CapturedParamNameType Params[] = { 2393 std::make_pair(".global_tid.", KmpInt32PtrTy), 2394 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2395 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 2396 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 2397 std::make_pair(StringRef(), QualType()) // __context with shared vars 2398 }; 2399 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2400 Params); 2401 break; 2402 } 2403 case OMPD_target_teams_distribute_parallel_for: 2404 case OMPD_target_teams_distribute_parallel_for_simd: { 2405 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2406 QualType KmpInt32PtrTy = 2407 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2408 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2409 2410 QualType Args[] = {VoidPtrTy}; 2411 FunctionProtoType::ExtProtoInfo EPI; 2412 EPI.Variadic = true; 2413 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2414 Sema::CapturedParamNameType Params[] = { 2415 std::make_pair(".global_tid.", KmpInt32Ty), 2416 std::make_pair(".part_id.", KmpInt32PtrTy), 2417 std::make_pair(".privates.", VoidPtrTy), 2418 std::make_pair( 2419 ".copy_fn.", 2420 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2421 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2422 std::make_pair(StringRef(), QualType()) // __context with shared vars 2423 }; 2424 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2425 Params); 2426 // Mark this captured region as inlined, because we don't use outlined 2427 // function directly. 2428 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2429 AlwaysInlineAttr::CreateImplicit( 2430 Context, AlwaysInlineAttr::Keyword_forceinline)); 2431 Sema::CapturedParamNameType ParamsTarget[] = { 2432 std::make_pair(StringRef(), QualType()) // __context with shared vars 2433 }; 2434 // Start a captured region for 'target' with no implicit parameters. 2435 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2436 ParamsTarget); 2437 2438 Sema::CapturedParamNameType ParamsTeams[] = { 2439 std::make_pair(".global_tid.", KmpInt32PtrTy), 2440 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2441 std::make_pair(StringRef(), QualType()) // __context with shared vars 2442 }; 2443 // Start a captured region for 'target' with no implicit parameters. 2444 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2445 ParamsTeams); 2446 2447 Sema::CapturedParamNameType ParamsParallel[] = { 2448 std::make_pair(".global_tid.", KmpInt32PtrTy), 2449 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2450 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 2451 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 2452 std::make_pair(StringRef(), QualType()) // __context with shared vars 2453 }; 2454 // Start a captured region for 'teams' or 'parallel'. Both regions have 2455 // the same implicit parameters. 2456 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2457 ParamsParallel); 2458 break; 2459 } 2460 2461 case OMPD_teams_distribute_parallel_for: 2462 case OMPD_teams_distribute_parallel_for_simd: { 2463 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2464 QualType KmpInt32PtrTy = 2465 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2466 2467 Sema::CapturedParamNameType ParamsTeams[] = { 2468 std::make_pair(".global_tid.", KmpInt32PtrTy), 2469 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2470 std::make_pair(StringRef(), QualType()) // __context with shared vars 2471 }; 2472 // Start a captured region for 'target' with no implicit parameters. 2473 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2474 ParamsTeams); 2475 2476 Sema::CapturedParamNameType ParamsParallel[] = { 2477 std::make_pair(".global_tid.", KmpInt32PtrTy), 2478 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2479 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 2480 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 2481 std::make_pair(StringRef(), QualType()) // __context with shared vars 2482 }; 2483 // Start a captured region for 'teams' or 'parallel'. Both regions have 2484 // the same implicit parameters. 2485 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2486 ParamsParallel); 2487 break; 2488 } 2489 case OMPD_target_update: 2490 case OMPD_target_enter_data: 2491 case OMPD_target_exit_data: { 2492 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2493 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2494 QualType KmpInt32PtrTy = 2495 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2496 QualType Args[] = {VoidPtrTy}; 2497 FunctionProtoType::ExtProtoInfo EPI; 2498 EPI.Variadic = true; 2499 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2500 Sema::CapturedParamNameType Params[] = { 2501 std::make_pair(".global_tid.", KmpInt32Ty), 2502 std::make_pair(".part_id.", KmpInt32PtrTy), 2503 std::make_pair(".privates.", VoidPtrTy), 2504 std::make_pair( 2505 ".copy_fn.", 2506 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2507 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2508 std::make_pair(StringRef(), QualType()) // __context with shared vars 2509 }; 2510 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2511 Params); 2512 // Mark this captured region as inlined, because we don't use outlined 2513 // function directly. 2514 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2515 AlwaysInlineAttr::CreateImplicit( 2516 Context, AlwaysInlineAttr::Keyword_forceinline)); 2517 break; 2518 } 2519 case OMPD_threadprivate: 2520 case OMPD_taskyield: 2521 case OMPD_barrier: 2522 case OMPD_taskwait: 2523 case OMPD_cancellation_point: 2524 case OMPD_cancel: 2525 case OMPD_flush: 2526 case OMPD_declare_reduction: 2527 case OMPD_declare_simd: 2528 case OMPD_declare_target: 2529 case OMPD_end_declare_target: 2530 llvm_unreachable("OpenMP Directive is not allowed"); 2531 case OMPD_unknown: 2532 llvm_unreachable("Unknown OpenMP directive"); 2533 } 2534 } 2535 2536 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 2537 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2538 getOpenMPCaptureRegions(CaptureRegions, DKind); 2539 return CaptureRegions.size(); 2540 } 2541 2542 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 2543 Expr *CaptureExpr, bool WithInit, 2544 bool AsExpression) { 2545 assert(CaptureExpr); 2546 ASTContext &C = S.getASTContext(); 2547 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 2548 QualType Ty = Init->getType(); 2549 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 2550 if (S.getLangOpts().CPlusPlus) { 2551 Ty = C.getLValueReferenceType(Ty); 2552 } else { 2553 Ty = C.getPointerType(Ty); 2554 ExprResult Res = 2555 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 2556 if (!Res.isUsable()) 2557 return nullptr; 2558 Init = Res.get(); 2559 } 2560 WithInit = true; 2561 } 2562 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 2563 CaptureExpr->getLocStart()); 2564 if (!WithInit) 2565 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C)); 2566 S.CurContext->addHiddenDecl(CED); 2567 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 2568 return CED; 2569 } 2570 2571 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 2572 bool WithInit) { 2573 OMPCapturedExprDecl *CD; 2574 if (VarDecl *VD = S.isOpenMPCapturedDecl(D)) 2575 CD = cast<OMPCapturedExprDecl>(VD); 2576 else 2577 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 2578 /*AsExpression=*/false); 2579 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 2580 CaptureExpr->getExprLoc()); 2581 } 2582 2583 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 2584 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get(); 2585 if (!Ref) { 2586 OMPCapturedExprDecl *CD = buildCaptureDecl( 2587 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr, 2588 /*WithInit=*/true, /*AsExpression=*/true); 2589 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 2590 CaptureExpr->getExprLoc()); 2591 } 2592 ExprResult Res = Ref; 2593 if (!S.getLangOpts().CPlusPlus && 2594 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 2595 Ref->getType()->isPointerType()) { 2596 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 2597 if (!Res.isUsable()) 2598 return ExprError(); 2599 } 2600 return S.DefaultLvalueConversion(Res.get()); 2601 } 2602 2603 namespace { 2604 // OpenMP directives parsed in this section are represented as a 2605 // CapturedStatement with an associated statement. If a syntax error 2606 // is detected during the parsing of the associated statement, the 2607 // compiler must abort processing and close the CapturedStatement. 2608 // 2609 // Combined directives such as 'target parallel' have more than one 2610 // nested CapturedStatements. This RAII ensures that we unwind out 2611 // of all the nested CapturedStatements when an error is found. 2612 class CaptureRegionUnwinderRAII { 2613 private: 2614 Sema &S; 2615 bool &ErrorFound; 2616 OpenMPDirectiveKind DKind = OMPD_unknown; 2617 2618 public: 2619 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 2620 OpenMPDirectiveKind DKind) 2621 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 2622 ~CaptureRegionUnwinderRAII() { 2623 if (ErrorFound) { 2624 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 2625 while (--ThisCaptureLevel >= 0) 2626 S.ActOnCapturedRegionError(); 2627 } 2628 } 2629 }; 2630 } // namespace 2631 2632 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 2633 ArrayRef<OMPClause *> Clauses) { 2634 bool ErrorFound = false; 2635 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 2636 *this, ErrorFound, DSAStack->getCurrentDirective()); 2637 if (!S.isUsable()) { 2638 ErrorFound = true; 2639 return StmtError(); 2640 } 2641 2642 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2643 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 2644 OMPOrderedClause *OC = nullptr; 2645 OMPScheduleClause *SC = nullptr; 2646 SmallVector<const OMPLinearClause *, 4> LCs; 2647 SmallVector<const OMPClauseWithPreInit *, 4> PICs; 2648 // This is required for proper codegen. 2649 for (OMPClause *Clause : Clauses) { 2650 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) && 2651 Clause->getClauseKind() == OMPC_in_reduction) { 2652 // Capture taskgroup task_reduction descriptors inside the tasking regions 2653 // with the corresponding in_reduction items. 2654 auto *IRC = cast<OMPInReductionClause>(Clause); 2655 for (Expr *E : IRC->taskgroup_descriptors()) 2656 if (E) 2657 MarkDeclarationsReferencedInExpr(E); 2658 } 2659 if (isOpenMPPrivate(Clause->getClauseKind()) || 2660 Clause->getClauseKind() == OMPC_copyprivate || 2661 (getLangOpts().OpenMPUseTLS && 2662 getASTContext().getTargetInfo().isTLSSupported() && 2663 Clause->getClauseKind() == OMPC_copyin)) { 2664 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 2665 // Mark all variables in private list clauses as used in inner region. 2666 for (Stmt *VarRef : Clause->children()) { 2667 if (auto *E = cast_or_null<Expr>(VarRef)) { 2668 MarkDeclarationsReferencedInExpr(E); 2669 } 2670 } 2671 DSAStack->setForceVarCapturing(/*V=*/false); 2672 } else if (CaptureRegions.size() > 1 || 2673 CaptureRegions.back() != OMPD_unknown) { 2674 if (auto *C = OMPClauseWithPreInit::get(Clause)) 2675 PICs.push_back(C); 2676 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 2677 if (Expr *E = C->getPostUpdateExpr()) 2678 MarkDeclarationsReferencedInExpr(E); 2679 } 2680 } 2681 if (Clause->getClauseKind() == OMPC_schedule) 2682 SC = cast<OMPScheduleClause>(Clause); 2683 else if (Clause->getClauseKind() == OMPC_ordered) 2684 OC = cast<OMPOrderedClause>(Clause); 2685 else if (Clause->getClauseKind() == OMPC_linear) 2686 LCs.push_back(cast<OMPLinearClause>(Clause)); 2687 } 2688 // OpenMP, 2.7.1 Loop Construct, Restrictions 2689 // The nonmonotonic modifier cannot be specified if an ordered clause is 2690 // specified. 2691 if (SC && 2692 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 2693 SC->getSecondScheduleModifier() == 2694 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 2695 OC) { 2696 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 2697 ? SC->getFirstScheduleModifierLoc() 2698 : SC->getSecondScheduleModifierLoc(), 2699 diag::err_omp_schedule_nonmonotonic_ordered) 2700 << SourceRange(OC->getLocStart(), OC->getLocEnd()); 2701 ErrorFound = true; 2702 } 2703 if (!LCs.empty() && OC && OC->getNumForLoops()) { 2704 for (const OMPLinearClause *C : LCs) { 2705 Diag(C->getLocStart(), diag::err_omp_linear_ordered) 2706 << SourceRange(OC->getLocStart(), OC->getLocEnd()); 2707 } 2708 ErrorFound = true; 2709 } 2710 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 2711 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 2712 OC->getNumForLoops()) { 2713 Diag(OC->getLocStart(), diag::err_omp_ordered_simd) 2714 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 2715 ErrorFound = true; 2716 } 2717 if (ErrorFound) { 2718 return StmtError(); 2719 } 2720 StmtResult SR = S; 2721 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 2722 // Mark all variables in private list clauses as used in inner region. 2723 // Required for proper codegen of combined directives. 2724 // TODO: add processing for other clauses. 2725 if (ThisCaptureRegion != OMPD_unknown) { 2726 for (const clang::OMPClauseWithPreInit *C : PICs) { 2727 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 2728 // Find the particular capture region for the clause if the 2729 // directive is a combined one with multiple capture regions. 2730 // If the directive is not a combined one, the capture region 2731 // associated with the clause is OMPD_unknown and is generated 2732 // only once. 2733 if (CaptureRegion == ThisCaptureRegion || 2734 CaptureRegion == OMPD_unknown) { 2735 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 2736 for (Decl *D : DS->decls()) 2737 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 2738 } 2739 } 2740 } 2741 } 2742 SR = ActOnCapturedRegionEnd(SR.get()); 2743 } 2744 return SR; 2745 } 2746 2747 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 2748 OpenMPDirectiveKind CancelRegion, 2749 SourceLocation StartLoc) { 2750 // CancelRegion is only needed for cancel and cancellation_point. 2751 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 2752 return false; 2753 2754 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 2755 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 2756 return false; 2757 2758 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 2759 << getOpenMPDirectiveName(CancelRegion); 2760 return true; 2761 } 2762 2763 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack, 2764 OpenMPDirectiveKind CurrentRegion, 2765 const DeclarationNameInfo &CurrentName, 2766 OpenMPDirectiveKind CancelRegion, 2767 SourceLocation StartLoc) { 2768 if (Stack->getCurScope()) { 2769 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective(); 2770 OpenMPDirectiveKind OffendingRegion = ParentRegion; 2771 bool NestingProhibited = false; 2772 bool CloseNesting = true; 2773 bool OrphanSeen = false; 2774 enum { 2775 NoRecommend, 2776 ShouldBeInParallelRegion, 2777 ShouldBeInOrderedRegion, 2778 ShouldBeInTargetRegion, 2779 ShouldBeInTeamsRegion 2780 } Recommend = NoRecommend; 2781 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) { 2782 // OpenMP [2.16, Nesting of Regions] 2783 // OpenMP constructs may not be nested inside a simd region. 2784 // OpenMP [2.8.1,simd Construct, Restrictions] 2785 // An ordered construct with the simd clause is the only OpenMP 2786 // construct that can appear in the simd region. 2787 // Allowing a SIMD construct nested in another SIMD construct is an 2788 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 2789 // message. 2790 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 2791 ? diag::err_omp_prohibited_region_simd 2792 : diag::warn_omp_nesting_simd); 2793 return CurrentRegion != OMPD_simd; 2794 } 2795 if (ParentRegion == OMPD_atomic) { 2796 // OpenMP [2.16, Nesting of Regions] 2797 // OpenMP constructs may not be nested inside an atomic region. 2798 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 2799 return true; 2800 } 2801 if (CurrentRegion == OMPD_section) { 2802 // OpenMP [2.7.2, sections Construct, Restrictions] 2803 // Orphaned section directives are prohibited. That is, the section 2804 // directives must appear within the sections construct and must not be 2805 // encountered elsewhere in the sections region. 2806 if (ParentRegion != OMPD_sections && 2807 ParentRegion != OMPD_parallel_sections) { 2808 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 2809 << (ParentRegion != OMPD_unknown) 2810 << getOpenMPDirectiveName(ParentRegion); 2811 return true; 2812 } 2813 return false; 2814 } 2815 // Allow some constructs (except teams) to be orphaned (they could be 2816 // used in functions, called from OpenMP regions with the required 2817 // preconditions). 2818 if (ParentRegion == OMPD_unknown && 2819 !isOpenMPNestingTeamsDirective(CurrentRegion)) 2820 return false; 2821 if (CurrentRegion == OMPD_cancellation_point || 2822 CurrentRegion == OMPD_cancel) { 2823 // OpenMP [2.16, Nesting of Regions] 2824 // A cancellation point construct for which construct-type-clause is 2825 // taskgroup must be nested inside a task construct. A cancellation 2826 // point construct for which construct-type-clause is not taskgroup must 2827 // be closely nested inside an OpenMP construct that matches the type 2828 // specified in construct-type-clause. 2829 // A cancel construct for which construct-type-clause is taskgroup must be 2830 // nested inside a task construct. A cancel construct for which 2831 // construct-type-clause is not taskgroup must be closely nested inside an 2832 // OpenMP construct that matches the type specified in 2833 // construct-type-clause. 2834 NestingProhibited = 2835 !((CancelRegion == OMPD_parallel && 2836 (ParentRegion == OMPD_parallel || 2837 ParentRegion == OMPD_target_parallel)) || 2838 (CancelRegion == OMPD_for && 2839 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 2840 ParentRegion == OMPD_target_parallel_for || 2841 ParentRegion == OMPD_distribute_parallel_for || 2842 ParentRegion == OMPD_teams_distribute_parallel_for || 2843 ParentRegion == OMPD_target_teams_distribute_parallel_for)) || 2844 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) || 2845 (CancelRegion == OMPD_sections && 2846 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 2847 ParentRegion == OMPD_parallel_sections))); 2848 } else if (CurrentRegion == OMPD_master) { 2849 // OpenMP [2.16, Nesting of Regions] 2850 // A master region may not be closely nested inside a worksharing, 2851 // atomic, or explicit task region. 2852 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 2853 isOpenMPTaskingDirective(ParentRegion); 2854 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 2855 // OpenMP [2.16, Nesting of Regions] 2856 // A critical region may not be nested (closely or otherwise) inside a 2857 // critical region with the same name. Note that this restriction is not 2858 // sufficient to prevent deadlock. 2859 SourceLocation PreviousCriticalLoc; 2860 bool DeadLock = Stack->hasDirective( 2861 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 2862 const DeclarationNameInfo &DNI, 2863 SourceLocation Loc) { 2864 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 2865 PreviousCriticalLoc = Loc; 2866 return true; 2867 } 2868 return false; 2869 }, 2870 false /* skip top directive */); 2871 if (DeadLock) { 2872 SemaRef.Diag(StartLoc, 2873 diag::err_omp_prohibited_region_critical_same_name) 2874 << CurrentName.getName(); 2875 if (PreviousCriticalLoc.isValid()) 2876 SemaRef.Diag(PreviousCriticalLoc, 2877 diag::note_omp_previous_critical_region); 2878 return true; 2879 } 2880 } else if (CurrentRegion == OMPD_barrier) { 2881 // OpenMP [2.16, Nesting of Regions] 2882 // A barrier region may not be closely nested inside a worksharing, 2883 // explicit task, critical, ordered, atomic, or master region. 2884 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 2885 isOpenMPTaskingDirective(ParentRegion) || 2886 ParentRegion == OMPD_master || 2887 ParentRegion == OMPD_critical || 2888 ParentRegion == OMPD_ordered; 2889 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 2890 !isOpenMPParallelDirective(CurrentRegion) && 2891 !isOpenMPTeamsDirective(CurrentRegion)) { 2892 // OpenMP [2.16, Nesting of Regions] 2893 // A worksharing region may not be closely nested inside a worksharing, 2894 // explicit task, critical, ordered, atomic, or master region. 2895 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 2896 isOpenMPTaskingDirective(ParentRegion) || 2897 ParentRegion == OMPD_master || 2898 ParentRegion == OMPD_critical || 2899 ParentRegion == OMPD_ordered; 2900 Recommend = ShouldBeInParallelRegion; 2901 } else if (CurrentRegion == OMPD_ordered) { 2902 // OpenMP [2.16, Nesting of Regions] 2903 // An ordered region may not be closely nested inside a critical, 2904 // atomic, or explicit task region. 2905 // An ordered region must be closely nested inside a loop region (or 2906 // parallel loop region) with an ordered clause. 2907 // OpenMP [2.8.1,simd Construct, Restrictions] 2908 // An ordered construct with the simd clause is the only OpenMP construct 2909 // that can appear in the simd region. 2910 NestingProhibited = ParentRegion == OMPD_critical || 2911 isOpenMPTaskingDirective(ParentRegion) || 2912 !(isOpenMPSimdDirective(ParentRegion) || 2913 Stack->isParentOrderedRegion()); 2914 Recommend = ShouldBeInOrderedRegion; 2915 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 2916 // OpenMP [2.16, Nesting of Regions] 2917 // If specified, a teams construct must be contained within a target 2918 // construct. 2919 NestingProhibited = ParentRegion != OMPD_target; 2920 OrphanSeen = ParentRegion == OMPD_unknown; 2921 Recommend = ShouldBeInTargetRegion; 2922 } 2923 if (!NestingProhibited && 2924 !isOpenMPTargetExecutionDirective(CurrentRegion) && 2925 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 2926 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 2927 // OpenMP [2.16, Nesting of Regions] 2928 // distribute, parallel, parallel sections, parallel workshare, and the 2929 // parallel loop and parallel loop SIMD constructs are the only OpenMP 2930 // constructs that can be closely nested in the teams region. 2931 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 2932 !isOpenMPDistributeDirective(CurrentRegion); 2933 Recommend = ShouldBeInParallelRegion; 2934 } 2935 if (!NestingProhibited && 2936 isOpenMPNestingDistributeDirective(CurrentRegion)) { 2937 // OpenMP 4.5 [2.17 Nesting of Regions] 2938 // The region associated with the distribute construct must be strictly 2939 // nested inside a teams region 2940 NestingProhibited = 2941 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 2942 Recommend = ShouldBeInTeamsRegion; 2943 } 2944 if (!NestingProhibited && 2945 (isOpenMPTargetExecutionDirective(CurrentRegion) || 2946 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 2947 // OpenMP 4.5 [2.17 Nesting of Regions] 2948 // If a target, target update, target data, target enter data, or 2949 // target exit data construct is encountered during execution of a 2950 // target region, the behavior is unspecified. 2951 NestingProhibited = Stack->hasDirective( 2952 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 2953 SourceLocation) { 2954 if (isOpenMPTargetExecutionDirective(K)) { 2955 OffendingRegion = K; 2956 return true; 2957 } 2958 return false; 2959 }, 2960 false /* don't skip top directive */); 2961 CloseNesting = false; 2962 } 2963 if (NestingProhibited) { 2964 if (OrphanSeen) { 2965 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 2966 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 2967 } else { 2968 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 2969 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 2970 << Recommend << getOpenMPDirectiveName(CurrentRegion); 2971 } 2972 return true; 2973 } 2974 } 2975 return false; 2976 } 2977 2978 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 2979 ArrayRef<OMPClause *> Clauses, 2980 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 2981 bool ErrorFound = false; 2982 unsigned NamedModifiersNumber = 0; 2983 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers( 2984 OMPD_unknown + 1); 2985 SmallVector<SourceLocation, 4> NameModifierLoc; 2986 for (const OMPClause *C : Clauses) { 2987 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 2988 // At most one if clause without a directive-name-modifier can appear on 2989 // the directive. 2990 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 2991 if (FoundNameModifiers[CurNM]) { 2992 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause) 2993 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 2994 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 2995 ErrorFound = true; 2996 } else if (CurNM != OMPD_unknown) { 2997 NameModifierLoc.push_back(IC->getNameModifierLoc()); 2998 ++NamedModifiersNumber; 2999 } 3000 FoundNameModifiers[CurNM] = IC; 3001 if (CurNM == OMPD_unknown) 3002 continue; 3003 // Check if the specified name modifier is allowed for the current 3004 // directive. 3005 // At most one if clause with the particular directive-name-modifier can 3006 // appear on the directive. 3007 bool MatchFound = false; 3008 for (auto NM : AllowedNameModifiers) { 3009 if (CurNM == NM) { 3010 MatchFound = true; 3011 break; 3012 } 3013 } 3014 if (!MatchFound) { 3015 S.Diag(IC->getNameModifierLoc(), 3016 diag::err_omp_wrong_if_directive_name_modifier) 3017 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 3018 ErrorFound = true; 3019 } 3020 } 3021 } 3022 // If any if clause on the directive includes a directive-name-modifier then 3023 // all if clauses on the directive must include a directive-name-modifier. 3024 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 3025 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 3026 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(), 3027 diag::err_omp_no_more_if_clause); 3028 } else { 3029 std::string Values; 3030 std::string Sep(", "); 3031 unsigned AllowedCnt = 0; 3032 unsigned TotalAllowedNum = 3033 AllowedNameModifiers.size() - NamedModifiersNumber; 3034 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 3035 ++Cnt) { 3036 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 3037 if (!FoundNameModifiers[NM]) { 3038 Values += "'"; 3039 Values += getOpenMPDirectiveName(NM); 3040 Values += "'"; 3041 if (AllowedCnt + 2 == TotalAllowedNum) 3042 Values += " or "; 3043 else if (AllowedCnt + 1 != TotalAllowedNum) 3044 Values += Sep; 3045 ++AllowedCnt; 3046 } 3047 } 3048 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(), 3049 diag::err_omp_unnamed_if_clause) 3050 << (TotalAllowedNum > 1) << Values; 3051 } 3052 for (SourceLocation Loc : NameModifierLoc) { 3053 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 3054 } 3055 ErrorFound = true; 3056 } 3057 return ErrorFound; 3058 } 3059 3060 StmtResult Sema::ActOnOpenMPExecutableDirective( 3061 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 3062 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 3063 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 3064 StmtResult Res = StmtError(); 3065 // First check CancelRegion which is then used in checkNestingOfRegions. 3066 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 3067 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 3068 StartLoc)) 3069 return StmtError(); 3070 3071 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 3072 VarsWithInheritedDSAType VarsWithInheritedDSA; 3073 bool ErrorFound = false; 3074 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 3075 if (AStmt && !CurContext->isDependentContext()) { 3076 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3077 3078 // Check default data sharing attributes for referenced variables. 3079 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 3080 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 3081 Stmt *S = AStmt; 3082 while (--ThisCaptureLevel >= 0) 3083 S = cast<CapturedStmt>(S)->getCapturedStmt(); 3084 DSAChecker.Visit(S); 3085 if (DSAChecker.isErrorFound()) 3086 return StmtError(); 3087 // Generate list of implicitly defined firstprivate variables. 3088 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 3089 3090 SmallVector<Expr *, 4> ImplicitFirstprivates( 3091 DSAChecker.getImplicitFirstprivate().begin(), 3092 DSAChecker.getImplicitFirstprivate().end()); 3093 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(), 3094 DSAChecker.getImplicitMap().end()); 3095 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 3096 for (OMPClause *C : Clauses) { 3097 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 3098 for (Expr *E : IRC->taskgroup_descriptors()) 3099 if (E) 3100 ImplicitFirstprivates.emplace_back(E); 3101 } 3102 } 3103 if (!ImplicitFirstprivates.empty()) { 3104 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 3105 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 3106 SourceLocation())) { 3107 ClausesWithImplicit.push_back(Implicit); 3108 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 3109 ImplicitFirstprivates.size(); 3110 } else { 3111 ErrorFound = true; 3112 } 3113 } 3114 if (!ImplicitMaps.empty()) { 3115 if (OMPClause *Implicit = ActOnOpenMPMapClause( 3116 OMPC_MAP_unknown, OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true, 3117 SourceLocation(), SourceLocation(), ImplicitMaps, 3118 SourceLocation(), SourceLocation(), SourceLocation())) { 3119 ClausesWithImplicit.emplace_back(Implicit); 3120 ErrorFound |= 3121 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size(); 3122 } else { 3123 ErrorFound = true; 3124 } 3125 } 3126 } 3127 3128 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 3129 switch (Kind) { 3130 case OMPD_parallel: 3131 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 3132 EndLoc); 3133 AllowedNameModifiers.push_back(OMPD_parallel); 3134 break; 3135 case OMPD_simd: 3136 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 3137 VarsWithInheritedDSA); 3138 break; 3139 case OMPD_for: 3140 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 3141 VarsWithInheritedDSA); 3142 break; 3143 case OMPD_for_simd: 3144 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 3145 EndLoc, VarsWithInheritedDSA); 3146 break; 3147 case OMPD_sections: 3148 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 3149 EndLoc); 3150 break; 3151 case OMPD_section: 3152 assert(ClausesWithImplicit.empty() && 3153 "No clauses are allowed for 'omp section' directive"); 3154 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 3155 break; 3156 case OMPD_single: 3157 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 3158 EndLoc); 3159 break; 3160 case OMPD_master: 3161 assert(ClausesWithImplicit.empty() && 3162 "No clauses are allowed for 'omp master' directive"); 3163 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 3164 break; 3165 case OMPD_critical: 3166 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 3167 StartLoc, EndLoc); 3168 break; 3169 case OMPD_parallel_for: 3170 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 3171 EndLoc, VarsWithInheritedDSA); 3172 AllowedNameModifiers.push_back(OMPD_parallel); 3173 break; 3174 case OMPD_parallel_for_simd: 3175 Res = ActOnOpenMPParallelForSimdDirective( 3176 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3177 AllowedNameModifiers.push_back(OMPD_parallel); 3178 break; 3179 case OMPD_parallel_sections: 3180 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 3181 StartLoc, EndLoc); 3182 AllowedNameModifiers.push_back(OMPD_parallel); 3183 break; 3184 case OMPD_task: 3185 Res = 3186 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 3187 AllowedNameModifiers.push_back(OMPD_task); 3188 break; 3189 case OMPD_taskyield: 3190 assert(ClausesWithImplicit.empty() && 3191 "No clauses are allowed for 'omp taskyield' directive"); 3192 assert(AStmt == nullptr && 3193 "No associated statement allowed for 'omp taskyield' directive"); 3194 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 3195 break; 3196 case OMPD_barrier: 3197 assert(ClausesWithImplicit.empty() && 3198 "No clauses are allowed for 'omp barrier' directive"); 3199 assert(AStmt == nullptr && 3200 "No associated statement allowed for 'omp barrier' directive"); 3201 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 3202 break; 3203 case OMPD_taskwait: 3204 assert(ClausesWithImplicit.empty() && 3205 "No clauses are allowed for 'omp taskwait' directive"); 3206 assert(AStmt == nullptr && 3207 "No associated statement allowed for 'omp taskwait' directive"); 3208 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 3209 break; 3210 case OMPD_taskgroup: 3211 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 3212 EndLoc); 3213 break; 3214 case OMPD_flush: 3215 assert(AStmt == nullptr && 3216 "No associated statement allowed for 'omp flush' directive"); 3217 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 3218 break; 3219 case OMPD_ordered: 3220 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 3221 EndLoc); 3222 break; 3223 case OMPD_atomic: 3224 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 3225 EndLoc); 3226 break; 3227 case OMPD_teams: 3228 Res = 3229 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 3230 break; 3231 case OMPD_target: 3232 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 3233 EndLoc); 3234 AllowedNameModifiers.push_back(OMPD_target); 3235 break; 3236 case OMPD_target_parallel: 3237 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 3238 StartLoc, EndLoc); 3239 AllowedNameModifiers.push_back(OMPD_target); 3240 AllowedNameModifiers.push_back(OMPD_parallel); 3241 break; 3242 case OMPD_target_parallel_for: 3243 Res = ActOnOpenMPTargetParallelForDirective( 3244 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3245 AllowedNameModifiers.push_back(OMPD_target); 3246 AllowedNameModifiers.push_back(OMPD_parallel); 3247 break; 3248 case OMPD_cancellation_point: 3249 assert(ClausesWithImplicit.empty() && 3250 "No clauses are allowed for 'omp cancellation point' directive"); 3251 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 3252 "cancellation point' directive"); 3253 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 3254 break; 3255 case OMPD_cancel: 3256 assert(AStmt == nullptr && 3257 "No associated statement allowed for 'omp cancel' directive"); 3258 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 3259 CancelRegion); 3260 AllowedNameModifiers.push_back(OMPD_cancel); 3261 break; 3262 case OMPD_target_data: 3263 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 3264 EndLoc); 3265 AllowedNameModifiers.push_back(OMPD_target_data); 3266 break; 3267 case OMPD_target_enter_data: 3268 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 3269 EndLoc, AStmt); 3270 AllowedNameModifiers.push_back(OMPD_target_enter_data); 3271 break; 3272 case OMPD_target_exit_data: 3273 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 3274 EndLoc, AStmt); 3275 AllowedNameModifiers.push_back(OMPD_target_exit_data); 3276 break; 3277 case OMPD_taskloop: 3278 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 3279 EndLoc, VarsWithInheritedDSA); 3280 AllowedNameModifiers.push_back(OMPD_taskloop); 3281 break; 3282 case OMPD_taskloop_simd: 3283 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 3284 EndLoc, VarsWithInheritedDSA); 3285 AllowedNameModifiers.push_back(OMPD_taskloop); 3286 break; 3287 case OMPD_distribute: 3288 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 3289 EndLoc, VarsWithInheritedDSA); 3290 break; 3291 case OMPD_target_update: 3292 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 3293 EndLoc, AStmt); 3294 AllowedNameModifiers.push_back(OMPD_target_update); 3295 break; 3296 case OMPD_distribute_parallel_for: 3297 Res = ActOnOpenMPDistributeParallelForDirective( 3298 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3299 AllowedNameModifiers.push_back(OMPD_parallel); 3300 break; 3301 case OMPD_distribute_parallel_for_simd: 3302 Res = ActOnOpenMPDistributeParallelForSimdDirective( 3303 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3304 AllowedNameModifiers.push_back(OMPD_parallel); 3305 break; 3306 case OMPD_distribute_simd: 3307 Res = ActOnOpenMPDistributeSimdDirective( 3308 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3309 break; 3310 case OMPD_target_parallel_for_simd: 3311 Res = ActOnOpenMPTargetParallelForSimdDirective( 3312 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3313 AllowedNameModifiers.push_back(OMPD_target); 3314 AllowedNameModifiers.push_back(OMPD_parallel); 3315 break; 3316 case OMPD_target_simd: 3317 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 3318 EndLoc, VarsWithInheritedDSA); 3319 AllowedNameModifiers.push_back(OMPD_target); 3320 break; 3321 case OMPD_teams_distribute: 3322 Res = ActOnOpenMPTeamsDistributeDirective( 3323 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3324 break; 3325 case OMPD_teams_distribute_simd: 3326 Res = ActOnOpenMPTeamsDistributeSimdDirective( 3327 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3328 break; 3329 case OMPD_teams_distribute_parallel_for_simd: 3330 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 3331 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3332 AllowedNameModifiers.push_back(OMPD_parallel); 3333 break; 3334 case OMPD_teams_distribute_parallel_for: 3335 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 3336 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3337 AllowedNameModifiers.push_back(OMPD_parallel); 3338 break; 3339 case OMPD_target_teams: 3340 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 3341 EndLoc); 3342 AllowedNameModifiers.push_back(OMPD_target); 3343 break; 3344 case OMPD_target_teams_distribute: 3345 Res = ActOnOpenMPTargetTeamsDistributeDirective( 3346 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3347 AllowedNameModifiers.push_back(OMPD_target); 3348 break; 3349 case OMPD_target_teams_distribute_parallel_for: 3350 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 3351 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3352 AllowedNameModifiers.push_back(OMPD_target); 3353 AllowedNameModifiers.push_back(OMPD_parallel); 3354 break; 3355 case OMPD_target_teams_distribute_parallel_for_simd: 3356 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 3357 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3358 AllowedNameModifiers.push_back(OMPD_target); 3359 AllowedNameModifiers.push_back(OMPD_parallel); 3360 break; 3361 case OMPD_target_teams_distribute_simd: 3362 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 3363 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3364 AllowedNameModifiers.push_back(OMPD_target); 3365 break; 3366 case OMPD_declare_target: 3367 case OMPD_end_declare_target: 3368 case OMPD_threadprivate: 3369 case OMPD_declare_reduction: 3370 case OMPD_declare_simd: 3371 llvm_unreachable("OpenMP Directive is not allowed"); 3372 case OMPD_unknown: 3373 llvm_unreachable("Unknown OpenMP directive"); 3374 } 3375 3376 for (const auto &P : VarsWithInheritedDSA) { 3377 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 3378 << P.first << P.second->getSourceRange(); 3379 } 3380 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound; 3381 3382 if (!AllowedNameModifiers.empty()) 3383 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 3384 ErrorFound; 3385 3386 if (ErrorFound) 3387 return StmtError(); 3388 return Res; 3389 } 3390 3391 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 3392 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 3393 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 3394 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 3395 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 3396 assert(Aligneds.size() == Alignments.size()); 3397 assert(Linears.size() == LinModifiers.size()); 3398 assert(Linears.size() == Steps.size()); 3399 if (!DG || DG.get().isNull()) 3400 return DeclGroupPtrTy(); 3401 3402 if (!DG.get().isSingleDecl()) { 3403 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd); 3404 return DG; 3405 } 3406 Decl *ADecl = DG.get().getSingleDecl(); 3407 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 3408 ADecl = FTD->getTemplatedDecl(); 3409 3410 auto *FD = dyn_cast<FunctionDecl>(ADecl); 3411 if (!FD) { 3412 Diag(ADecl->getLocation(), diag::err_omp_function_expected); 3413 return DeclGroupPtrTy(); 3414 } 3415 3416 // OpenMP [2.8.2, declare simd construct, Description] 3417 // The parameter of the simdlen clause must be a constant positive integer 3418 // expression. 3419 ExprResult SL; 3420 if (Simdlen) 3421 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 3422 // OpenMP [2.8.2, declare simd construct, Description] 3423 // The special this pointer can be used as if was one of the arguments to the 3424 // function in any of the linear, aligned, or uniform clauses. 3425 // The uniform clause declares one or more arguments to have an invariant 3426 // value for all concurrent invocations of the function in the execution of a 3427 // single SIMD loop. 3428 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 3429 const Expr *UniformedLinearThis = nullptr; 3430 for (const Expr *E : Uniforms) { 3431 E = E->IgnoreParenImpCasts(); 3432 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 3433 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 3434 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 3435 FD->getParamDecl(PVD->getFunctionScopeIndex()) 3436 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 3437 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 3438 continue; 3439 } 3440 if (isa<CXXThisExpr>(E)) { 3441 UniformedLinearThis = E; 3442 continue; 3443 } 3444 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 3445 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 3446 } 3447 // OpenMP [2.8.2, declare simd construct, Description] 3448 // The aligned clause declares that the object to which each list item points 3449 // is aligned to the number of bytes expressed in the optional parameter of 3450 // the aligned clause. 3451 // The special this pointer can be used as if was one of the arguments to the 3452 // function in any of the linear, aligned, or uniform clauses. 3453 // The type of list items appearing in the aligned clause must be array, 3454 // pointer, reference to array, or reference to pointer. 3455 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 3456 const Expr *AlignedThis = nullptr; 3457 for (const Expr *E : Aligneds) { 3458 E = E->IgnoreParenImpCasts(); 3459 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 3460 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 3461 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 3462 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 3463 FD->getParamDecl(PVD->getFunctionScopeIndex()) 3464 ->getCanonicalDecl() == CanonPVD) { 3465 // OpenMP [2.8.1, simd construct, Restrictions] 3466 // A list-item cannot appear in more than one aligned clause. 3467 if (AlignedArgs.count(CanonPVD) > 0) { 3468 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 3469 << 1 << E->getSourceRange(); 3470 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 3471 diag::note_omp_explicit_dsa) 3472 << getOpenMPClauseName(OMPC_aligned); 3473 continue; 3474 } 3475 AlignedArgs[CanonPVD] = E; 3476 QualType QTy = PVD->getType() 3477 .getNonReferenceType() 3478 .getUnqualifiedType() 3479 .getCanonicalType(); 3480 const Type *Ty = QTy.getTypePtrOrNull(); 3481 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 3482 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 3483 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 3484 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 3485 } 3486 continue; 3487 } 3488 } 3489 if (isa<CXXThisExpr>(E)) { 3490 if (AlignedThis) { 3491 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 3492 << 2 << E->getSourceRange(); 3493 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 3494 << getOpenMPClauseName(OMPC_aligned); 3495 } 3496 AlignedThis = E; 3497 continue; 3498 } 3499 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 3500 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 3501 } 3502 // The optional parameter of the aligned clause, alignment, must be a constant 3503 // positive integer expression. If no optional parameter is specified, 3504 // implementation-defined default alignments for SIMD instructions on the 3505 // target platforms are assumed. 3506 SmallVector<const Expr *, 4> NewAligns; 3507 for (Expr *E : Alignments) { 3508 ExprResult Align; 3509 if (E) 3510 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 3511 NewAligns.push_back(Align.get()); 3512 } 3513 // OpenMP [2.8.2, declare simd construct, Description] 3514 // The linear clause declares one or more list items to be private to a SIMD 3515 // lane and to have a linear relationship with respect to the iteration space 3516 // of a loop. 3517 // The special this pointer can be used as if was one of the arguments to the 3518 // function in any of the linear, aligned, or uniform clauses. 3519 // When a linear-step expression is specified in a linear clause it must be 3520 // either a constant integer expression or an integer-typed parameter that is 3521 // specified in a uniform clause on the directive. 3522 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 3523 const bool IsUniformedThis = UniformedLinearThis != nullptr; 3524 auto MI = LinModifiers.begin(); 3525 for (const Expr *E : Linears) { 3526 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 3527 ++MI; 3528 E = E->IgnoreParenImpCasts(); 3529 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 3530 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 3531 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 3532 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 3533 FD->getParamDecl(PVD->getFunctionScopeIndex()) 3534 ->getCanonicalDecl() == CanonPVD) { 3535 // OpenMP [2.15.3.7, linear Clause, Restrictions] 3536 // A list-item cannot appear in more than one linear clause. 3537 if (LinearArgs.count(CanonPVD) > 0) { 3538 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3539 << getOpenMPClauseName(OMPC_linear) 3540 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 3541 Diag(LinearArgs[CanonPVD]->getExprLoc(), 3542 diag::note_omp_explicit_dsa) 3543 << getOpenMPClauseName(OMPC_linear); 3544 continue; 3545 } 3546 // Each argument can appear in at most one uniform or linear clause. 3547 if (UniformedArgs.count(CanonPVD) > 0) { 3548 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3549 << getOpenMPClauseName(OMPC_linear) 3550 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 3551 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 3552 diag::note_omp_explicit_dsa) 3553 << getOpenMPClauseName(OMPC_uniform); 3554 continue; 3555 } 3556 LinearArgs[CanonPVD] = E; 3557 if (E->isValueDependent() || E->isTypeDependent() || 3558 E->isInstantiationDependent() || 3559 E->containsUnexpandedParameterPack()) 3560 continue; 3561 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 3562 PVD->getOriginalType()); 3563 continue; 3564 } 3565 } 3566 if (isa<CXXThisExpr>(E)) { 3567 if (UniformedLinearThis) { 3568 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3569 << getOpenMPClauseName(OMPC_linear) 3570 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 3571 << E->getSourceRange(); 3572 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 3573 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 3574 : OMPC_linear); 3575 continue; 3576 } 3577 UniformedLinearThis = E; 3578 if (E->isValueDependent() || E->isTypeDependent() || 3579 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 3580 continue; 3581 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 3582 E->getType()); 3583 continue; 3584 } 3585 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 3586 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 3587 } 3588 Expr *Step = nullptr; 3589 Expr *NewStep = nullptr; 3590 SmallVector<Expr *, 4> NewSteps; 3591 for (Expr *E : Steps) { 3592 // Skip the same step expression, it was checked already. 3593 if (Step == E || !E) { 3594 NewSteps.push_back(E ? NewStep : nullptr); 3595 continue; 3596 } 3597 Step = E; 3598 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 3599 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 3600 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 3601 if (UniformedArgs.count(CanonPVD) == 0) { 3602 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 3603 << Step->getSourceRange(); 3604 } else if (E->isValueDependent() || E->isTypeDependent() || 3605 E->isInstantiationDependent() || 3606 E->containsUnexpandedParameterPack() || 3607 CanonPVD->getType()->hasIntegerRepresentation()) { 3608 NewSteps.push_back(Step); 3609 } else { 3610 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 3611 << Step->getSourceRange(); 3612 } 3613 continue; 3614 } 3615 NewStep = Step; 3616 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 3617 !Step->isInstantiationDependent() && 3618 !Step->containsUnexpandedParameterPack()) { 3619 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 3620 .get(); 3621 if (NewStep) 3622 NewStep = VerifyIntegerConstantExpression(NewStep).get(); 3623 } 3624 NewSteps.push_back(NewStep); 3625 } 3626 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 3627 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 3628 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 3629 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 3630 const_cast<Expr **>(Linears.data()), Linears.size(), 3631 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 3632 NewSteps.data(), NewSteps.size(), SR); 3633 ADecl->addAttr(NewAttr); 3634 return ConvertDeclToDeclGroup(ADecl); 3635 } 3636 3637 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 3638 Stmt *AStmt, 3639 SourceLocation StartLoc, 3640 SourceLocation EndLoc) { 3641 if (!AStmt) 3642 return StmtError(); 3643 3644 auto *CS = cast<CapturedStmt>(AStmt); 3645 // 1.2.2 OpenMP Language Terminology 3646 // Structured block - An executable statement with a single entry at the 3647 // top and a single exit at the bottom. 3648 // The point of exit cannot be a branch out of the structured block. 3649 // longjmp() and throw() must not violate the entry/exit criteria. 3650 CS->getCapturedDecl()->setNothrow(); 3651 3652 setFunctionHasBranchProtectedScope(); 3653 3654 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 3655 DSAStack->isCancelRegion()); 3656 } 3657 3658 namespace { 3659 /// Helper class for checking canonical form of the OpenMP loops and 3660 /// extracting iteration space of each loop in the loop nest, that will be used 3661 /// for IR generation. 3662 class OpenMPIterationSpaceChecker { 3663 /// Reference to Sema. 3664 Sema &SemaRef; 3665 /// A location for diagnostics (when there is no some better location). 3666 SourceLocation DefaultLoc; 3667 /// A location for diagnostics (when increment is not compatible). 3668 SourceLocation ConditionLoc; 3669 /// A source location for referring to loop init later. 3670 SourceRange InitSrcRange; 3671 /// A source location for referring to condition later. 3672 SourceRange ConditionSrcRange; 3673 /// A source location for referring to increment later. 3674 SourceRange IncrementSrcRange; 3675 /// Loop variable. 3676 ValueDecl *LCDecl = nullptr; 3677 /// Reference to loop variable. 3678 Expr *LCRef = nullptr; 3679 /// Lower bound (initializer for the var). 3680 Expr *LB = nullptr; 3681 /// Upper bound. 3682 Expr *UB = nullptr; 3683 /// Loop step (increment). 3684 Expr *Step = nullptr; 3685 /// This flag is true when condition is one of: 3686 /// Var < UB 3687 /// Var <= UB 3688 /// UB > Var 3689 /// UB >= Var 3690 bool TestIsLessOp = false; 3691 /// This flag is true when condition is strict ( < or > ). 3692 bool TestIsStrictOp = false; 3693 /// This flag is true when step is subtracted on each iteration. 3694 bool SubtractStep = false; 3695 3696 public: 3697 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc) 3698 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {} 3699 /// Check init-expr for canonical loop form and save loop counter 3700 /// variable - #Var and its initialization value - #LB. 3701 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 3702 /// Check test-expr for canonical form, save upper-bound (#UB), flags 3703 /// for less/greater and for strict/non-strict comparison. 3704 bool checkAndSetCond(Expr *S); 3705 /// Check incr-expr for canonical loop form and return true if it 3706 /// does not conform, otherwise save loop step (#Step). 3707 bool checkAndSetInc(Expr *S); 3708 /// Return the loop counter variable. 3709 ValueDecl *getLoopDecl() const { return LCDecl; } 3710 /// Return the reference expression to loop counter variable. 3711 Expr *getLoopDeclRefExpr() const { return LCRef; } 3712 /// Source range of the loop init. 3713 SourceRange getInitSrcRange() const { return InitSrcRange; } 3714 /// Source range of the loop condition. 3715 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 3716 /// Source range of the loop increment. 3717 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 3718 /// True if the step should be subtracted. 3719 bool shouldSubtractStep() const { return SubtractStep; } 3720 /// Build the expression to calculate the number of iterations. 3721 Expr *buildNumIterations( 3722 Scope *S, const bool LimitedType, 3723 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 3724 /// Build the precondition expression for the loops. 3725 Expr * 3726 buildPreCond(Scope *S, Expr *Cond, 3727 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 3728 /// Build reference expression to the counter be used for codegen. 3729 DeclRefExpr * 3730 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 3731 DSAStackTy &DSA) const; 3732 /// Build reference expression to the private counter be used for 3733 /// codegen. 3734 Expr *buildPrivateCounterVar() const; 3735 /// Build initialization of the counter be used for codegen. 3736 Expr *buildCounterInit() const; 3737 /// Build step of the counter be used for codegen. 3738 Expr *buildCounterStep() const; 3739 /// Return true if any expression is dependent. 3740 bool dependent() const; 3741 3742 private: 3743 /// Check the right-hand side of an assignment in the increment 3744 /// expression. 3745 bool checkAndSetIncRHS(Expr *RHS); 3746 /// Helper to set loop counter variable and its initializer. 3747 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB); 3748 /// Helper to set upper bound. 3749 bool setUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR, 3750 SourceLocation SL); 3751 /// Helper to set loop increment. 3752 bool setStep(Expr *NewStep, bool Subtract); 3753 }; 3754 3755 bool OpenMPIterationSpaceChecker::dependent() const { 3756 if (!LCDecl) { 3757 assert(!LB && !UB && !Step); 3758 return false; 3759 } 3760 return LCDecl->getType()->isDependentType() || 3761 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 3762 (Step && Step->isValueDependent()); 3763 } 3764 3765 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 3766 Expr *NewLCRefExpr, 3767 Expr *NewLB) { 3768 // State consistency checking to ensure correct usage. 3769 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 3770 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 3771 if (!NewLCDecl || !NewLB) 3772 return true; 3773 LCDecl = getCanonicalDecl(NewLCDecl); 3774 LCRef = NewLCRefExpr; 3775 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 3776 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 3777 if ((Ctor->isCopyOrMoveConstructor() || 3778 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 3779 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 3780 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 3781 LB = NewLB; 3782 return false; 3783 } 3784 3785 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, bool LessOp, bool StrictOp, 3786 SourceRange SR, SourceLocation SL) { 3787 // State consistency checking to ensure correct usage. 3788 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 3789 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 3790 if (!NewUB) 3791 return true; 3792 UB = NewUB; 3793 TestIsLessOp = LessOp; 3794 TestIsStrictOp = StrictOp; 3795 ConditionSrcRange = SR; 3796 ConditionLoc = SL; 3797 return false; 3798 } 3799 3800 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 3801 // State consistency checking to ensure correct usage. 3802 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 3803 if (!NewStep) 3804 return true; 3805 if (!NewStep->isValueDependent()) { 3806 // Check that the step is integer expression. 3807 SourceLocation StepLoc = NewStep->getLocStart(); 3808 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 3809 StepLoc, getExprAsWritten(NewStep)); 3810 if (Val.isInvalid()) 3811 return true; 3812 NewStep = Val.get(); 3813 3814 // OpenMP [2.6, Canonical Loop Form, Restrictions] 3815 // If test-expr is of form var relational-op b and relational-op is < or 3816 // <= then incr-expr must cause var to increase on each iteration of the 3817 // loop. If test-expr is of form var relational-op b and relational-op is 3818 // > or >= then incr-expr must cause var to decrease on each iteration of 3819 // the loop. 3820 // If test-expr is of form b relational-op var and relational-op is < or 3821 // <= then incr-expr must cause var to decrease on each iteration of the 3822 // loop. If test-expr is of form b relational-op var and relational-op is 3823 // > or >= then incr-expr must cause var to increase on each iteration of 3824 // the loop. 3825 llvm::APSInt Result; 3826 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); 3827 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 3828 bool IsConstNeg = 3829 IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); 3830 bool IsConstPos = 3831 IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); 3832 bool IsConstZero = IsConstant && !Result.getBoolValue(); 3833 if (UB && (IsConstZero || 3834 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract)) 3835 : (IsConstPos || (IsUnsigned && !Subtract))))) { 3836 SemaRef.Diag(NewStep->getExprLoc(), 3837 diag::err_omp_loop_incr_not_compatible) 3838 << LCDecl << TestIsLessOp << NewStep->getSourceRange(); 3839 SemaRef.Diag(ConditionLoc, 3840 diag::note_omp_loop_cond_requres_compatible_incr) 3841 << TestIsLessOp << ConditionSrcRange; 3842 return true; 3843 } 3844 if (TestIsLessOp == Subtract) { 3845 NewStep = 3846 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 3847 .get(); 3848 Subtract = !Subtract; 3849 } 3850 } 3851 3852 Step = NewStep; 3853 SubtractStep = Subtract; 3854 return false; 3855 } 3856 3857 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 3858 // Check init-expr for canonical loop form and save loop counter 3859 // variable - #Var and its initialization value - #LB. 3860 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 3861 // var = lb 3862 // integer-type var = lb 3863 // random-access-iterator-type var = lb 3864 // pointer-type var = lb 3865 // 3866 if (!S) { 3867 if (EmitDiags) { 3868 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 3869 } 3870 return true; 3871 } 3872 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 3873 if (!ExprTemp->cleanupsHaveSideEffects()) 3874 S = ExprTemp->getSubExpr(); 3875 3876 InitSrcRange = S->getSourceRange(); 3877 if (Expr *E = dyn_cast<Expr>(S)) 3878 S = E->IgnoreParens(); 3879 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 3880 if (BO->getOpcode() == BO_Assign) { 3881 Expr *LHS = BO->getLHS()->IgnoreParens(); 3882 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 3883 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 3884 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 3885 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3886 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS()); 3887 } 3888 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 3889 if (ME->isArrow() && 3890 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 3891 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3892 } 3893 } 3894 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 3895 if (DS->isSingleDecl()) { 3896 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 3897 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 3898 // Accept non-canonical init form here but emit ext. warning. 3899 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 3900 SemaRef.Diag(S->getLocStart(), 3901 diag::ext_omp_loop_not_canonical_init) 3902 << S->getSourceRange(); 3903 return setLCDeclAndLB(Var, nullptr, Var->getInit()); 3904 } 3905 } 3906 } 3907 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 3908 if (CE->getOperator() == OO_Equal) { 3909 Expr *LHS = CE->getArg(0); 3910 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 3911 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 3912 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 3913 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3914 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1)); 3915 } 3916 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 3917 if (ME->isArrow() && 3918 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 3919 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3920 } 3921 } 3922 } 3923 3924 if (dependent() || SemaRef.CurContext->isDependentContext()) 3925 return false; 3926 if (EmitDiags) { 3927 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init) 3928 << S->getSourceRange(); 3929 } 3930 return true; 3931 } 3932 3933 /// Ignore parenthesizes, implicit casts, copy constructor and return the 3934 /// variable (which may be the loop variable) if possible. 3935 static const ValueDecl *getInitLCDecl(const Expr *E) { 3936 if (!E) 3937 return nullptr; 3938 E = getExprAsWritten(E); 3939 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 3940 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 3941 if ((Ctor->isCopyOrMoveConstructor() || 3942 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 3943 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 3944 E = CE->getArg(0)->IgnoreParenImpCasts(); 3945 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 3946 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 3947 return getCanonicalDecl(VD); 3948 } 3949 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 3950 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 3951 return getCanonicalDecl(ME->getMemberDecl()); 3952 return nullptr; 3953 } 3954 3955 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 3956 // Check test-expr for canonical form, save upper-bound UB, flags for 3957 // less/greater and for strict/non-strict comparison. 3958 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 3959 // var relational-op b 3960 // b relational-op var 3961 // 3962 if (!S) { 3963 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl; 3964 return true; 3965 } 3966 S = getExprAsWritten(S); 3967 SourceLocation CondLoc = S->getLocStart(); 3968 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 3969 if (BO->isRelationalOp()) { 3970 if (getInitLCDecl(BO->getLHS()) == LCDecl) 3971 return setUB(BO->getRHS(), 3972 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 3973 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 3974 BO->getSourceRange(), BO->getOperatorLoc()); 3975 if (getInitLCDecl(BO->getRHS()) == LCDecl) 3976 return setUB(BO->getLHS(), 3977 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 3978 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 3979 BO->getSourceRange(), BO->getOperatorLoc()); 3980 } 3981 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 3982 if (CE->getNumArgs() == 2) { 3983 auto Op = CE->getOperator(); 3984 switch (Op) { 3985 case OO_Greater: 3986 case OO_GreaterEqual: 3987 case OO_Less: 3988 case OO_LessEqual: 3989 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 3990 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 3991 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 3992 CE->getOperatorLoc()); 3993 if (getInitLCDecl(CE->getArg(1)) == LCDecl) 3994 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 3995 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 3996 CE->getOperatorLoc()); 3997 break; 3998 default: 3999 break; 4000 } 4001 } 4002 } 4003 if (dependent() || SemaRef.CurContext->isDependentContext()) 4004 return false; 4005 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 4006 << S->getSourceRange() << LCDecl; 4007 return true; 4008 } 4009 4010 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 4011 // RHS of canonical loop form increment can be: 4012 // var + incr 4013 // incr + var 4014 // var - incr 4015 // 4016 RHS = RHS->IgnoreParenImpCasts(); 4017 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 4018 if (BO->isAdditiveOp()) { 4019 bool IsAdd = BO->getOpcode() == BO_Add; 4020 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4021 return setStep(BO->getRHS(), !IsAdd); 4022 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 4023 return setStep(BO->getLHS(), /*Subtract=*/false); 4024 } 4025 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 4026 bool IsAdd = CE->getOperator() == OO_Plus; 4027 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 4028 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4029 return setStep(CE->getArg(1), !IsAdd); 4030 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 4031 return setStep(CE->getArg(0), /*Subtract=*/false); 4032 } 4033 } 4034 if (dependent() || SemaRef.CurContext->isDependentContext()) 4035 return false; 4036 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr) 4037 << RHS->getSourceRange() << LCDecl; 4038 return true; 4039 } 4040 4041 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 4042 // Check incr-expr for canonical loop form and return true if it 4043 // does not conform. 4044 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 4045 // ++var 4046 // var++ 4047 // --var 4048 // var-- 4049 // var += incr 4050 // var -= incr 4051 // var = var + incr 4052 // var = incr + var 4053 // var = var - incr 4054 // 4055 if (!S) { 4056 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 4057 return true; 4058 } 4059 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 4060 if (!ExprTemp->cleanupsHaveSideEffects()) 4061 S = ExprTemp->getSubExpr(); 4062 4063 IncrementSrcRange = S->getSourceRange(); 4064 S = S->IgnoreParens(); 4065 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 4066 if (UO->isIncrementDecrementOp() && 4067 getInitLCDecl(UO->getSubExpr()) == LCDecl) 4068 return setStep(SemaRef 4069 .ActOnIntegerConstant(UO->getLocStart(), 4070 (UO->isDecrementOp() ? -1 : 1)) 4071 .get(), 4072 /*Subtract=*/false); 4073 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 4074 switch (BO->getOpcode()) { 4075 case BO_AddAssign: 4076 case BO_SubAssign: 4077 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4078 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 4079 break; 4080 case BO_Assign: 4081 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4082 return checkAndSetIncRHS(BO->getRHS()); 4083 break; 4084 default: 4085 break; 4086 } 4087 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 4088 switch (CE->getOperator()) { 4089 case OO_PlusPlus: 4090 case OO_MinusMinus: 4091 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4092 return setStep(SemaRef 4093 .ActOnIntegerConstant( 4094 CE->getLocStart(), 4095 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 4096 .get(), 4097 /*Subtract=*/false); 4098 break; 4099 case OO_PlusEqual: 4100 case OO_MinusEqual: 4101 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4102 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 4103 break; 4104 case OO_Equal: 4105 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4106 return checkAndSetIncRHS(CE->getArg(1)); 4107 break; 4108 default: 4109 break; 4110 } 4111 } 4112 if (dependent() || SemaRef.CurContext->isDependentContext()) 4113 return false; 4114 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr) 4115 << S->getSourceRange() << LCDecl; 4116 return true; 4117 } 4118 4119 static ExprResult 4120 tryBuildCapture(Sema &SemaRef, Expr *Capture, 4121 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 4122 if (SemaRef.CurContext->isDependentContext()) 4123 return ExprResult(Capture); 4124 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 4125 return SemaRef.PerformImplicitConversion( 4126 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 4127 /*AllowExplicit=*/true); 4128 auto I = Captures.find(Capture); 4129 if (I != Captures.end()) 4130 return buildCapture(SemaRef, Capture, I->second); 4131 DeclRefExpr *Ref = nullptr; 4132 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 4133 Captures[Capture] = Ref; 4134 return Res; 4135 } 4136 4137 /// Build the expression to calculate the number of iterations. 4138 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 4139 Scope *S, const bool LimitedType, 4140 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 4141 ExprResult Diff; 4142 QualType VarType = LCDecl->getType().getNonReferenceType(); 4143 if (VarType->isIntegerType() || VarType->isPointerType() || 4144 SemaRef.getLangOpts().CPlusPlus) { 4145 // Upper - Lower 4146 Expr *UBExpr = TestIsLessOp ? UB : LB; 4147 Expr *LBExpr = TestIsLessOp ? LB : UB; 4148 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 4149 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 4150 if (!Upper || !Lower) 4151 return nullptr; 4152 4153 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 4154 4155 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 4156 // BuildBinOp already emitted error, this one is to point user to upper 4157 // and lower bound, and to tell what is passed to 'operator-'. 4158 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx) 4159 << Upper->getSourceRange() << Lower->getSourceRange(); 4160 return nullptr; 4161 } 4162 } 4163 4164 if (!Diff.isUsable()) 4165 return nullptr; 4166 4167 // Upper - Lower [- 1] 4168 if (TestIsStrictOp) 4169 Diff = SemaRef.BuildBinOp( 4170 S, DefaultLoc, BO_Sub, Diff.get(), 4171 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 4172 if (!Diff.isUsable()) 4173 return nullptr; 4174 4175 // Upper - Lower [- 1] + Step 4176 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 4177 if (!NewStep.isUsable()) 4178 return nullptr; 4179 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 4180 if (!Diff.isUsable()) 4181 return nullptr; 4182 4183 // Parentheses (for dumping/debugging purposes only). 4184 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 4185 if (!Diff.isUsable()) 4186 return nullptr; 4187 4188 // (Upper - Lower [- 1] + Step) / Step 4189 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 4190 if (!Diff.isUsable()) 4191 return nullptr; 4192 4193 // OpenMP runtime requires 32-bit or 64-bit loop variables. 4194 QualType Type = Diff.get()->getType(); 4195 ASTContext &C = SemaRef.Context; 4196 bool UseVarType = VarType->hasIntegerRepresentation() && 4197 C.getTypeSize(Type) > C.getTypeSize(VarType); 4198 if (!Type->isIntegerType() || UseVarType) { 4199 unsigned NewSize = 4200 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 4201 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 4202 : Type->hasSignedIntegerRepresentation(); 4203 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 4204 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 4205 Diff = SemaRef.PerformImplicitConversion( 4206 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 4207 if (!Diff.isUsable()) 4208 return nullptr; 4209 } 4210 } 4211 if (LimitedType) { 4212 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 4213 if (NewSize != C.getTypeSize(Type)) { 4214 if (NewSize < C.getTypeSize(Type)) { 4215 assert(NewSize == 64 && "incorrect loop var size"); 4216 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 4217 << InitSrcRange << ConditionSrcRange; 4218 } 4219 QualType NewType = C.getIntTypeForBitwidth( 4220 NewSize, Type->hasSignedIntegerRepresentation() || 4221 C.getTypeSize(Type) < NewSize); 4222 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 4223 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 4224 Sema::AA_Converting, true); 4225 if (!Diff.isUsable()) 4226 return nullptr; 4227 } 4228 } 4229 } 4230 4231 return Diff.get(); 4232 } 4233 4234 Expr *OpenMPIterationSpaceChecker::buildPreCond( 4235 Scope *S, Expr *Cond, 4236 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 4237 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 4238 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 4239 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 4240 4241 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 4242 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 4243 if (!NewLB.isUsable() || !NewUB.isUsable()) 4244 return nullptr; 4245 4246 ExprResult CondExpr = 4247 SemaRef.BuildBinOp(S, DefaultLoc, 4248 TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE) 4249 : (TestIsStrictOp ? BO_GT : BO_GE), 4250 NewLB.get(), NewUB.get()); 4251 if (CondExpr.isUsable()) { 4252 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 4253 SemaRef.Context.BoolTy)) 4254 CondExpr = SemaRef.PerformImplicitConversion( 4255 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 4256 /*AllowExplicit=*/true); 4257 } 4258 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 4259 // Otherwise use original loop conditon and evaluate it in runtime. 4260 return CondExpr.isUsable() ? CondExpr.get() : Cond; 4261 } 4262 4263 /// Build reference expression to the counter be used for codegen. 4264 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 4265 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const { 4266 auto *VD = dyn_cast<VarDecl>(LCDecl); 4267 if (!VD) { 4268 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 4269 DeclRefExpr *Ref = buildDeclRefExpr( 4270 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 4271 const DSAStackTy::DSAVarData Data = 4272 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 4273 // If the loop control decl is explicitly marked as private, do not mark it 4274 // as captured again. 4275 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 4276 Captures.insert(std::make_pair(LCRef, Ref)); 4277 return Ref; 4278 } 4279 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(), 4280 DefaultLoc); 4281 } 4282 4283 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 4284 if (LCDecl && !LCDecl->isInvalidDecl()) { 4285 QualType Type = LCDecl->getType().getNonReferenceType(); 4286 VarDecl *PrivateVar = buildVarDecl( 4287 SemaRef, DefaultLoc, Type, LCDecl->getName(), 4288 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 4289 isa<VarDecl>(LCDecl) 4290 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 4291 : nullptr); 4292 if (PrivateVar->isInvalidDecl()) 4293 return nullptr; 4294 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 4295 } 4296 return nullptr; 4297 } 4298 4299 /// Build initialization of the counter to be used for codegen. 4300 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 4301 4302 /// Build step of the counter be used for codegen. 4303 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 4304 4305 /// Iteration space of a single for loop. 4306 struct LoopIterationSpace final { 4307 /// Condition of the loop. 4308 Expr *PreCond = nullptr; 4309 /// This expression calculates the number of iterations in the loop. 4310 /// It is always possible to calculate it before starting the loop. 4311 Expr *NumIterations = nullptr; 4312 /// The loop counter variable. 4313 Expr *CounterVar = nullptr; 4314 /// Private loop counter variable. 4315 Expr *PrivateCounterVar = nullptr; 4316 /// This is initializer for the initial value of #CounterVar. 4317 Expr *CounterInit = nullptr; 4318 /// This is step for the #CounterVar used to generate its update: 4319 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 4320 Expr *CounterStep = nullptr; 4321 /// Should step be subtracted? 4322 bool Subtract = false; 4323 /// Source range of the loop init. 4324 SourceRange InitSrcRange; 4325 /// Source range of the loop condition. 4326 SourceRange CondSrcRange; 4327 /// Source range of the loop increment. 4328 SourceRange IncSrcRange; 4329 }; 4330 4331 } // namespace 4332 4333 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 4334 assert(getLangOpts().OpenMP && "OpenMP is not active."); 4335 assert(Init && "Expected loop in canonical form."); 4336 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 4337 if (AssociatedLoops > 0 && 4338 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 4339 OpenMPIterationSpaceChecker ISC(*this, ForLoc); 4340 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 4341 if (ValueDecl *D = ISC.getLoopDecl()) { 4342 auto *VD = dyn_cast<VarDecl>(D); 4343 if (!VD) { 4344 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 4345 VD = Private; 4346 } else { 4347 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 4348 /*WithInit=*/false); 4349 VD = cast<VarDecl>(Ref->getDecl()); 4350 } 4351 } 4352 DSAStack->addLoopControlVariable(D, VD); 4353 } 4354 } 4355 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 4356 } 4357 } 4358 4359 /// Called on a for stmt to check and extract its iteration space 4360 /// for further processing (such as collapsing). 4361 static bool checkOpenMPIterationSpace( 4362 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 4363 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 4364 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr, 4365 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 4366 LoopIterationSpace &ResultIterSpace, 4367 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 4368 // OpenMP [2.6, Canonical Loop Form] 4369 // for (init-expr; test-expr; incr-expr) structured-block 4370 auto *For = dyn_cast_or_null<ForStmt>(S); 4371 if (!For) { 4372 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for) 4373 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 4374 << getOpenMPDirectiveName(DKind) << NestedLoopCount 4375 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 4376 if (NestedLoopCount > 1) { 4377 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 4378 SemaRef.Diag(DSA.getConstructLoc(), 4379 diag::note_omp_collapse_ordered_expr) 4380 << 2 << CollapseLoopCountExpr->getSourceRange() 4381 << OrderedLoopCountExpr->getSourceRange(); 4382 else if (CollapseLoopCountExpr) 4383 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 4384 diag::note_omp_collapse_ordered_expr) 4385 << 0 << CollapseLoopCountExpr->getSourceRange(); 4386 else 4387 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 4388 diag::note_omp_collapse_ordered_expr) 4389 << 1 << OrderedLoopCountExpr->getSourceRange(); 4390 } 4391 return true; 4392 } 4393 assert(For->getBody()); 4394 4395 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc()); 4396 4397 // Check init. 4398 Stmt *Init = For->getInit(); 4399 if (ISC.checkAndSetInit(Init)) 4400 return true; 4401 4402 bool HasErrors = false; 4403 4404 // Check loop variable's type. 4405 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 4406 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 4407 4408 // OpenMP [2.6, Canonical Loop Form] 4409 // Var is one of the following: 4410 // A variable of signed or unsigned integer type. 4411 // For C++, a variable of a random access iterator type. 4412 // For C, a variable of a pointer type. 4413 QualType VarType = LCDecl->getType().getNonReferenceType(); 4414 if (!VarType->isDependentType() && !VarType->isIntegerType() && 4415 !VarType->isPointerType() && 4416 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 4417 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type) 4418 << SemaRef.getLangOpts().CPlusPlus; 4419 HasErrors = true; 4420 } 4421 4422 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 4423 // a Construct 4424 // The loop iteration variable(s) in the associated for-loop(s) of a for or 4425 // parallel for construct is (are) private. 4426 // The loop iteration variable in the associated for-loop of a simd 4427 // construct with just one associated for-loop is linear with a 4428 // constant-linear-step that is the increment of the associated for-loop. 4429 // Exclude loop var from the list of variables with implicitly defined data 4430 // sharing attributes. 4431 VarsWithImplicitDSA.erase(LCDecl); 4432 4433 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 4434 // in a Construct, C/C++]. 4435 // The loop iteration variable in the associated for-loop of a simd 4436 // construct with just one associated for-loop may be listed in a linear 4437 // clause with a constant-linear-step that is the increment of the 4438 // associated for-loop. 4439 // The loop iteration variable(s) in the associated for-loop(s) of a for or 4440 // parallel for construct may be listed in a private or lastprivate clause. 4441 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false); 4442 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is 4443 // declared in the loop and it is predetermined as a private. 4444 OpenMPClauseKind PredeterminedCKind = 4445 isOpenMPSimdDirective(DKind) 4446 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate) 4447 : OMPC_private; 4448 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 4449 DVar.CKind != PredeterminedCKind) || 4450 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 4451 isOpenMPDistributeDirective(DKind)) && 4452 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 4453 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 4454 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 4455 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa) 4456 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind) 4457 << getOpenMPClauseName(PredeterminedCKind); 4458 if (DVar.RefExpr == nullptr) 4459 DVar.CKind = PredeterminedCKind; 4460 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true); 4461 HasErrors = true; 4462 } else if (LoopDeclRefExpr != nullptr) { 4463 // Make the loop iteration variable private (for worksharing constructs), 4464 // linear (for simd directives with the only one associated loop) or 4465 // lastprivate (for simd directives with several collapsed or ordered 4466 // loops). 4467 if (DVar.CKind == OMPC_unknown) 4468 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate, 4469 [](OpenMPDirectiveKind) -> bool { return true; }, 4470 /*FromParent=*/false); 4471 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind); 4472 } 4473 4474 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 4475 4476 // Check test-expr. 4477 HasErrors |= ISC.checkAndSetCond(For->getCond()); 4478 4479 // Check incr-expr. 4480 HasErrors |= ISC.checkAndSetInc(For->getInc()); 4481 } 4482 4483 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 4484 return HasErrors; 4485 4486 // Build the loop's iteration space representation. 4487 ResultIterSpace.PreCond = 4488 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures); 4489 ResultIterSpace.NumIterations = ISC.buildNumIterations( 4490 DSA.getCurScope(), 4491 (isOpenMPWorksharingDirective(DKind) || 4492 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)), 4493 Captures); 4494 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA); 4495 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar(); 4496 ResultIterSpace.CounterInit = ISC.buildCounterInit(); 4497 ResultIterSpace.CounterStep = ISC.buildCounterStep(); 4498 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange(); 4499 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange(); 4500 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange(); 4501 ResultIterSpace.Subtract = ISC.shouldSubtractStep(); 4502 4503 HasErrors |= (ResultIterSpace.PreCond == nullptr || 4504 ResultIterSpace.NumIterations == nullptr || 4505 ResultIterSpace.CounterVar == nullptr || 4506 ResultIterSpace.PrivateCounterVar == nullptr || 4507 ResultIterSpace.CounterInit == nullptr || 4508 ResultIterSpace.CounterStep == nullptr); 4509 4510 return HasErrors; 4511 } 4512 4513 /// Build 'VarRef = Start. 4514 static ExprResult 4515 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 4516 ExprResult Start, 4517 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 4518 // Build 'VarRef = Start. 4519 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures); 4520 if (!NewStart.isUsable()) 4521 return ExprError(); 4522 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 4523 VarRef.get()->getType())) { 4524 NewStart = SemaRef.PerformImplicitConversion( 4525 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 4526 /*AllowExplicit=*/true); 4527 if (!NewStart.isUsable()) 4528 return ExprError(); 4529 } 4530 4531 ExprResult Init = 4532 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 4533 return Init; 4534 } 4535 4536 /// Build 'VarRef = Start + Iter * Step'. 4537 static ExprResult buildCounterUpdate( 4538 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 4539 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 4540 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 4541 // Add parentheses (for debugging purposes only). 4542 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 4543 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 4544 !Step.isUsable()) 4545 return ExprError(); 4546 4547 ExprResult NewStep = Step; 4548 if (Captures) 4549 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 4550 if (NewStep.isInvalid()) 4551 return ExprError(); 4552 ExprResult Update = 4553 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 4554 if (!Update.isUsable()) 4555 return ExprError(); 4556 4557 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 4558 // 'VarRef = Start (+|-) Iter * Step'. 4559 ExprResult NewStart = Start; 4560 if (Captures) 4561 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 4562 if (NewStart.isInvalid()) 4563 return ExprError(); 4564 4565 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 4566 ExprResult SavedUpdate = Update; 4567 ExprResult UpdateVal; 4568 if (VarRef.get()->getType()->isOverloadableType() || 4569 NewStart.get()->getType()->isOverloadableType() || 4570 Update.get()->getType()->isOverloadableType()) { 4571 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 4572 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 4573 Update = 4574 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 4575 if (Update.isUsable()) { 4576 UpdateVal = 4577 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 4578 VarRef.get(), SavedUpdate.get()); 4579 if (UpdateVal.isUsable()) { 4580 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 4581 UpdateVal.get()); 4582 } 4583 } 4584 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 4585 } 4586 4587 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 4588 if (!Update.isUsable() || !UpdateVal.isUsable()) { 4589 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 4590 NewStart.get(), SavedUpdate.get()); 4591 if (!Update.isUsable()) 4592 return ExprError(); 4593 4594 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 4595 VarRef.get()->getType())) { 4596 Update = SemaRef.PerformImplicitConversion( 4597 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 4598 if (!Update.isUsable()) 4599 return ExprError(); 4600 } 4601 4602 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 4603 } 4604 return Update; 4605 } 4606 4607 /// Convert integer expression \a E to make it have at least \a Bits 4608 /// bits. 4609 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 4610 if (E == nullptr) 4611 return ExprError(); 4612 ASTContext &C = SemaRef.Context; 4613 QualType OldType = E->getType(); 4614 unsigned HasBits = C.getTypeSize(OldType); 4615 if (HasBits >= Bits) 4616 return ExprResult(E); 4617 // OK to convert to signed, because new type has more bits than old. 4618 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 4619 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 4620 true); 4621 } 4622 4623 /// Check if the given expression \a E is a constant integer that fits 4624 /// into \a Bits bits. 4625 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 4626 if (E == nullptr) 4627 return false; 4628 llvm::APSInt Result; 4629 if (E->isIntegerConstantExpr(Result, SemaRef.Context)) 4630 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); 4631 return false; 4632 } 4633 4634 /// Build preinits statement for the given declarations. 4635 static Stmt *buildPreInits(ASTContext &Context, 4636 MutableArrayRef<Decl *> PreInits) { 4637 if (!PreInits.empty()) { 4638 return new (Context) DeclStmt( 4639 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 4640 SourceLocation(), SourceLocation()); 4641 } 4642 return nullptr; 4643 } 4644 4645 /// Build preinits statement for the given declarations. 4646 static Stmt * 4647 buildPreInits(ASTContext &Context, 4648 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 4649 if (!Captures.empty()) { 4650 SmallVector<Decl *, 16> PreInits; 4651 for (const auto &Pair : Captures) 4652 PreInits.push_back(Pair.second->getDecl()); 4653 return buildPreInits(Context, PreInits); 4654 } 4655 return nullptr; 4656 } 4657 4658 /// Build postupdate expression for the given list of postupdates expressions. 4659 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 4660 Expr *PostUpdate = nullptr; 4661 if (!PostUpdates.empty()) { 4662 for (Expr *E : PostUpdates) { 4663 Expr *ConvE = S.BuildCStyleCastExpr( 4664 E->getExprLoc(), 4665 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 4666 E->getExprLoc(), E) 4667 .get(); 4668 PostUpdate = PostUpdate 4669 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 4670 PostUpdate, ConvE) 4671 .get() 4672 : ConvE; 4673 } 4674 } 4675 return PostUpdate; 4676 } 4677 4678 /// Called on a for stmt to check itself and nested loops (if any). 4679 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 4680 /// number of collapsed loops otherwise. 4681 static unsigned 4682 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 4683 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 4684 DSAStackTy &DSA, 4685 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 4686 OMPLoopDirective::HelperExprs &Built) { 4687 unsigned NestedLoopCount = 1; 4688 if (CollapseLoopCountExpr) { 4689 // Found 'collapse' clause - calculate collapse number. 4690 llvm::APSInt Result; 4691 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) 4692 NestedLoopCount = Result.getLimitedValue(); 4693 } 4694 if (OrderedLoopCountExpr) { 4695 // Found 'ordered' clause - calculate collapse number. 4696 llvm::APSInt Result; 4697 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 4698 if (Result.getLimitedValue() < NestedLoopCount) { 4699 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 4700 diag::err_omp_wrong_ordered_loop_count) 4701 << OrderedLoopCountExpr->getSourceRange(); 4702 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 4703 diag::note_collapse_loop_count) 4704 << CollapseLoopCountExpr->getSourceRange(); 4705 } 4706 NestedLoopCount = Result.getLimitedValue(); 4707 } 4708 } 4709 // This is helper routine for loop directives (e.g., 'for', 'simd', 4710 // 'for simd', etc.). 4711 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 4712 SmallVector<LoopIterationSpace, 4> IterSpaces; 4713 IterSpaces.resize(NestedLoopCount); 4714 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); 4715 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 4716 if (checkOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt, 4717 NestedLoopCount, CollapseLoopCountExpr, 4718 OrderedLoopCountExpr, VarsWithImplicitDSA, 4719 IterSpaces[Cnt], Captures)) 4720 return 0; 4721 // Move on to the next nested for loop, or to the loop body. 4722 // OpenMP [2.8.1, simd construct, Restrictions] 4723 // All loops associated with the construct must be perfectly nested; that 4724 // is, there must be no intervening code nor any OpenMP directive between 4725 // any two loops. 4726 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); 4727 } 4728 4729 Built.clear(/* size */ NestedLoopCount); 4730 4731 if (SemaRef.CurContext->isDependentContext()) 4732 return NestedLoopCount; 4733 4734 // An example of what is generated for the following code: 4735 // 4736 // #pragma omp simd collapse(2) ordered(2) 4737 // for (i = 0; i < NI; ++i) 4738 // for (k = 0; k < NK; ++k) 4739 // for (j = J0; j < NJ; j+=2) { 4740 // <loop body> 4741 // } 4742 // 4743 // We generate the code below. 4744 // Note: the loop body may be outlined in CodeGen. 4745 // Note: some counters may be C++ classes, operator- is used to find number of 4746 // iterations and operator+= to calculate counter value. 4747 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 4748 // or i64 is currently supported). 4749 // 4750 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 4751 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 4752 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 4753 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 4754 // // similar updates for vars in clauses (e.g. 'linear') 4755 // <loop body (using local i and j)> 4756 // } 4757 // i = NI; // assign final values of counters 4758 // j = NJ; 4759 // 4760 4761 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 4762 // the iteration counts of the collapsed for loops. 4763 // Precondition tests if there is at least one iteration (all conditions are 4764 // true). 4765 auto PreCond = ExprResult(IterSpaces[0].PreCond); 4766 Expr *N0 = IterSpaces[0].NumIterations; 4767 ExprResult LastIteration32 = 4768 widenIterationCount(/*Bits=*/32, 4769 SemaRef 4770 .PerformImplicitConversion( 4771 N0->IgnoreImpCasts(), N0->getType(), 4772 Sema::AA_Converting, /*AllowExplicit=*/true) 4773 .get(), 4774 SemaRef); 4775 ExprResult LastIteration64 = widenIterationCount( 4776 /*Bits=*/64, 4777 SemaRef 4778 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 4779 Sema::AA_Converting, 4780 /*AllowExplicit=*/true) 4781 .get(), 4782 SemaRef); 4783 4784 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 4785 return NestedLoopCount; 4786 4787 ASTContext &C = SemaRef.Context; 4788 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 4789 4790 Scope *CurScope = DSA.getCurScope(); 4791 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 4792 if (PreCond.isUsable()) { 4793 PreCond = 4794 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 4795 PreCond.get(), IterSpaces[Cnt].PreCond); 4796 } 4797 Expr *N = IterSpaces[Cnt].NumIterations; 4798 SourceLocation Loc = N->getExprLoc(); 4799 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 4800 if (LastIteration32.isUsable()) 4801 LastIteration32 = SemaRef.BuildBinOp( 4802 CurScope, Loc, BO_Mul, LastIteration32.get(), 4803 SemaRef 4804 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 4805 Sema::AA_Converting, 4806 /*AllowExplicit=*/true) 4807 .get()); 4808 if (LastIteration64.isUsable()) 4809 LastIteration64 = SemaRef.BuildBinOp( 4810 CurScope, Loc, BO_Mul, LastIteration64.get(), 4811 SemaRef 4812 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 4813 Sema::AA_Converting, 4814 /*AllowExplicit=*/true) 4815 .get()); 4816 } 4817 4818 // Choose either the 32-bit or 64-bit version. 4819 ExprResult LastIteration = LastIteration64; 4820 if (LastIteration32.isUsable() && 4821 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 4822 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 4823 fitsInto( 4824 /*Bits=*/32, 4825 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 4826 LastIteration64.get(), SemaRef))) 4827 LastIteration = LastIteration32; 4828 QualType VType = LastIteration.get()->getType(); 4829 QualType RealVType = VType; 4830 QualType StrideVType = VType; 4831 if (isOpenMPTaskLoopDirective(DKind)) { 4832 VType = 4833 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 4834 StrideVType = 4835 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 4836 } 4837 4838 if (!LastIteration.isUsable()) 4839 return 0; 4840 4841 // Save the number of iterations. 4842 ExprResult NumIterations = LastIteration; 4843 { 4844 LastIteration = SemaRef.BuildBinOp( 4845 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 4846 LastIteration.get(), 4847 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 4848 if (!LastIteration.isUsable()) 4849 return 0; 4850 } 4851 4852 // Calculate the last iteration number beforehand instead of doing this on 4853 // each iteration. Do not do this if the number of iterations may be kfold-ed. 4854 llvm::APSInt Result; 4855 bool IsConstant = 4856 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); 4857 ExprResult CalcLastIteration; 4858 if (!IsConstant) { 4859 ExprResult SaveRef = 4860 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 4861 LastIteration = SaveRef; 4862 4863 // Prepare SaveRef + 1. 4864 NumIterations = SemaRef.BuildBinOp( 4865 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 4866 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 4867 if (!NumIterations.isUsable()) 4868 return 0; 4869 } 4870 4871 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 4872 4873 // Build variables passed into runtime, necessary for worksharing directives. 4874 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 4875 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 4876 isOpenMPDistributeDirective(DKind)) { 4877 // Lower bound variable, initialized with zero. 4878 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 4879 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 4880 SemaRef.AddInitializerToDecl(LBDecl, 4881 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 4882 /*DirectInit*/ false); 4883 4884 // Upper bound variable, initialized with last iteration number. 4885 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 4886 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 4887 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 4888 /*DirectInit*/ false); 4889 4890 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 4891 // This will be used to implement clause 'lastprivate'. 4892 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 4893 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 4894 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 4895 SemaRef.AddInitializerToDecl(ILDecl, 4896 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 4897 /*DirectInit*/ false); 4898 4899 // Stride variable returned by runtime (we initialize it to 1 by default). 4900 VarDecl *STDecl = 4901 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 4902 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 4903 SemaRef.AddInitializerToDecl(STDecl, 4904 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 4905 /*DirectInit*/ false); 4906 4907 // Build expression: UB = min(UB, LastIteration) 4908 // It is necessary for CodeGen of directives with static scheduling. 4909 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 4910 UB.get(), LastIteration.get()); 4911 ExprResult CondOp = SemaRef.ActOnConditionalOp( 4912 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 4913 LastIteration.get(), UB.get()); 4914 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 4915 CondOp.get()); 4916 EUB = SemaRef.ActOnFinishFullExpr(EUB.get()); 4917 4918 // If we have a combined directive that combines 'distribute', 'for' or 4919 // 'simd' we need to be able to access the bounds of the schedule of the 4920 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 4921 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 4922 if (isOpenMPLoopBoundSharingDirective(DKind)) { 4923 // Lower bound variable, initialized with zero. 4924 VarDecl *CombLBDecl = 4925 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 4926 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 4927 SemaRef.AddInitializerToDecl( 4928 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 4929 /*DirectInit*/ false); 4930 4931 // Upper bound variable, initialized with last iteration number. 4932 VarDecl *CombUBDecl = 4933 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 4934 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 4935 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 4936 /*DirectInit*/ false); 4937 4938 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 4939 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 4940 ExprResult CombCondOp = 4941 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 4942 LastIteration.get(), CombUB.get()); 4943 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 4944 CombCondOp.get()); 4945 CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get()); 4946 4947 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 4948 // We expect to have at least 2 more parameters than the 'parallel' 4949 // directive does - the lower and upper bounds of the previous schedule. 4950 assert(CD->getNumParams() >= 4 && 4951 "Unexpected number of parameters in loop combined directive"); 4952 4953 // Set the proper type for the bounds given what we learned from the 4954 // enclosed loops. 4955 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 4956 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 4957 4958 // Previous lower and upper bounds are obtained from the region 4959 // parameters. 4960 PrevLB = 4961 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 4962 PrevUB = 4963 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 4964 } 4965 } 4966 4967 // Build the iteration variable and its initialization before loop. 4968 ExprResult IV; 4969 ExprResult Init, CombInit; 4970 { 4971 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 4972 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 4973 Expr *RHS = 4974 (isOpenMPWorksharingDirective(DKind) || 4975 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 4976 ? LB.get() 4977 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 4978 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 4979 Init = SemaRef.ActOnFinishFullExpr(Init.get()); 4980 4981 if (isOpenMPLoopBoundSharingDirective(DKind)) { 4982 Expr *CombRHS = 4983 (isOpenMPWorksharingDirective(DKind) || 4984 isOpenMPTaskLoopDirective(DKind) || 4985 isOpenMPDistributeDirective(DKind)) 4986 ? CombLB.get() 4987 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 4988 CombInit = 4989 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 4990 CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get()); 4991 } 4992 } 4993 4994 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops. 4995 SourceLocation CondLoc = AStmt->getLocStart(); 4996 ExprResult Cond = 4997 (isOpenMPWorksharingDirective(DKind) || 4998 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 4999 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get()) 5000 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 5001 NumIterations.get()); 5002 ExprResult CombCond; 5003 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5004 CombCond = 5005 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get()); 5006 } 5007 // Loop increment (IV = IV + 1) 5008 SourceLocation IncLoc = AStmt->getLocStart(); 5009 ExprResult Inc = 5010 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 5011 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 5012 if (!Inc.isUsable()) 5013 return 0; 5014 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 5015 Inc = SemaRef.ActOnFinishFullExpr(Inc.get()); 5016 if (!Inc.isUsable()) 5017 return 0; 5018 5019 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 5020 // Used for directives with static scheduling. 5021 // In combined construct, add combined version that use CombLB and CombUB 5022 // base variables for the update 5023 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 5024 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 5025 isOpenMPDistributeDirective(DKind)) { 5026 // LB + ST 5027 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 5028 if (!NextLB.isUsable()) 5029 return 0; 5030 // LB = LB + ST 5031 NextLB = 5032 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 5033 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get()); 5034 if (!NextLB.isUsable()) 5035 return 0; 5036 // UB + ST 5037 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 5038 if (!NextUB.isUsable()) 5039 return 0; 5040 // UB = UB + ST 5041 NextUB = 5042 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 5043 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get()); 5044 if (!NextUB.isUsable()) 5045 return 0; 5046 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5047 CombNextLB = 5048 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 5049 if (!NextLB.isUsable()) 5050 return 0; 5051 // LB = LB + ST 5052 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 5053 CombNextLB.get()); 5054 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get()); 5055 if (!CombNextLB.isUsable()) 5056 return 0; 5057 // UB + ST 5058 CombNextUB = 5059 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 5060 if (!CombNextUB.isUsable()) 5061 return 0; 5062 // UB = UB + ST 5063 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 5064 CombNextUB.get()); 5065 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get()); 5066 if (!CombNextUB.isUsable()) 5067 return 0; 5068 } 5069 } 5070 5071 // Create increment expression for distribute loop when combined in a same 5072 // directive with for as IV = IV + ST; ensure upper bound expression based 5073 // on PrevUB instead of NumIterations - used to implement 'for' when found 5074 // in combination with 'distribute', like in 'distribute parallel for' 5075 SourceLocation DistIncLoc = AStmt->getLocStart(); 5076 ExprResult DistCond, DistInc, PrevEUB; 5077 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5078 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get()); 5079 assert(DistCond.isUsable() && "distribute cond expr was not built"); 5080 5081 DistInc = 5082 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 5083 assert(DistInc.isUsable() && "distribute inc expr was not built"); 5084 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 5085 DistInc.get()); 5086 DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get()); 5087 assert(DistInc.isUsable() && "distribute inc expr was not built"); 5088 5089 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 5090 // construct 5091 SourceLocation DistEUBLoc = AStmt->getLocStart(); 5092 ExprResult IsUBGreater = 5093 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get()); 5094 ExprResult CondOp = SemaRef.ActOnConditionalOp( 5095 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get()); 5096 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 5097 CondOp.get()); 5098 PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get()); 5099 } 5100 5101 // Build updates and final values of the loop counters. 5102 bool HasErrors = false; 5103 Built.Counters.resize(NestedLoopCount); 5104 Built.Inits.resize(NestedLoopCount); 5105 Built.Updates.resize(NestedLoopCount); 5106 Built.Finals.resize(NestedLoopCount); 5107 SmallVector<Expr *, 4> LoopMultipliers; 5108 { 5109 ExprResult Div; 5110 // Go from inner nested loop to outer. 5111 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) { 5112 LoopIterationSpace &IS = IterSpaces[Cnt]; 5113 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 5114 // Build: Iter = (IV / Div) % IS.NumIters 5115 // where Div is product of previous iterations' IS.NumIters. 5116 ExprResult Iter; 5117 if (Div.isUsable()) { 5118 Iter = 5119 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get()); 5120 } else { 5121 Iter = IV; 5122 assert((Cnt == (int)NestedLoopCount - 1) && 5123 "unusable div expected on first iteration only"); 5124 } 5125 5126 if (Cnt != 0 && Iter.isUsable()) 5127 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(), 5128 IS.NumIterations); 5129 if (!Iter.isUsable()) { 5130 HasErrors = true; 5131 break; 5132 } 5133 5134 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 5135 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 5136 DeclRefExpr *CounterVar = buildDeclRefExpr( 5137 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 5138 /*RefersToCapture=*/true); 5139 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 5140 IS.CounterInit, Captures); 5141 if (!Init.isUsable()) { 5142 HasErrors = true; 5143 break; 5144 } 5145 ExprResult Update = buildCounterUpdate( 5146 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 5147 IS.CounterStep, IS.Subtract, &Captures); 5148 if (!Update.isUsable()) { 5149 HasErrors = true; 5150 break; 5151 } 5152 5153 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 5154 ExprResult Final = buildCounterUpdate( 5155 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, 5156 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures); 5157 if (!Final.isUsable()) { 5158 HasErrors = true; 5159 break; 5160 } 5161 5162 // Build Div for the next iteration: Div <- Div * IS.NumIters 5163 if (Cnt != 0) { 5164 if (Div.isUnset()) 5165 Div = IS.NumIterations; 5166 else 5167 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(), 5168 IS.NumIterations); 5169 5170 // Add parentheses (for debugging purposes only). 5171 if (Div.isUsable()) 5172 Div = tryBuildCapture(SemaRef, Div.get(), Captures); 5173 if (!Div.isUsable()) { 5174 HasErrors = true; 5175 break; 5176 } 5177 LoopMultipliers.push_back(Div.get()); 5178 } 5179 if (!Update.isUsable() || !Final.isUsable()) { 5180 HasErrors = true; 5181 break; 5182 } 5183 // Save results 5184 Built.Counters[Cnt] = IS.CounterVar; 5185 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 5186 Built.Inits[Cnt] = Init.get(); 5187 Built.Updates[Cnt] = Update.get(); 5188 Built.Finals[Cnt] = Final.get(); 5189 } 5190 } 5191 5192 if (HasErrors) 5193 return 0; 5194 5195 // Save results 5196 Built.IterationVarRef = IV.get(); 5197 Built.LastIteration = LastIteration.get(); 5198 Built.NumIterations = NumIterations.get(); 5199 Built.CalcLastIteration = 5200 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get(); 5201 Built.PreCond = PreCond.get(); 5202 Built.PreInits = buildPreInits(C, Captures); 5203 Built.Cond = Cond.get(); 5204 Built.Init = Init.get(); 5205 Built.Inc = Inc.get(); 5206 Built.LB = LB.get(); 5207 Built.UB = UB.get(); 5208 Built.IL = IL.get(); 5209 Built.ST = ST.get(); 5210 Built.EUB = EUB.get(); 5211 Built.NLB = NextLB.get(); 5212 Built.NUB = NextUB.get(); 5213 Built.PrevLB = PrevLB.get(); 5214 Built.PrevUB = PrevUB.get(); 5215 Built.DistInc = DistInc.get(); 5216 Built.PrevEUB = PrevEUB.get(); 5217 Built.DistCombinedFields.LB = CombLB.get(); 5218 Built.DistCombinedFields.UB = CombUB.get(); 5219 Built.DistCombinedFields.EUB = CombEUB.get(); 5220 Built.DistCombinedFields.Init = CombInit.get(); 5221 Built.DistCombinedFields.Cond = CombCond.get(); 5222 Built.DistCombinedFields.NLB = CombNextLB.get(); 5223 Built.DistCombinedFields.NUB = CombNextUB.get(); 5224 5225 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get(); 5226 // Fill data for doacross depend clauses. 5227 for (const auto &Pair : DSA.getDoacrossDependClauses()) { 5228 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) { 5229 Pair.first->setCounterValue(CounterVal); 5230 } else { 5231 if (NestedLoopCount != Pair.second.size() || 5232 NestedLoopCount != LoopMultipliers.size() + 1) { 5233 // Erroneous case - clause has some problems. 5234 Pair.first->setCounterValue(CounterVal); 5235 continue; 5236 } 5237 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink); 5238 auto I = Pair.second.rbegin(); 5239 auto IS = IterSpaces.rbegin(); 5240 auto ILM = LoopMultipliers.rbegin(); 5241 Expr *UpCounterVal = CounterVal; 5242 Expr *Multiplier = nullptr; 5243 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) { 5244 if (I->first) { 5245 assert(IS->CounterStep); 5246 Expr *NormalizedOffset = 5247 SemaRef 5248 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div, 5249 I->first, IS->CounterStep) 5250 .get(); 5251 if (Multiplier) { 5252 NormalizedOffset = 5253 SemaRef 5254 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul, 5255 NormalizedOffset, Multiplier) 5256 .get(); 5257 } 5258 assert(I->second == OO_Plus || I->second == OO_Minus); 5259 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub; 5260 UpCounterVal = SemaRef 5261 .BuildBinOp(CurScope, I->first->getExprLoc(), BOK, 5262 UpCounterVal, NormalizedOffset) 5263 .get(); 5264 } 5265 Multiplier = *ILM; 5266 ++I; 5267 ++IS; 5268 ++ILM; 5269 } 5270 Pair.first->setCounterValue(UpCounterVal); 5271 } 5272 } 5273 5274 return NestedLoopCount; 5275 } 5276 5277 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 5278 auto CollapseClauses = 5279 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 5280 if (CollapseClauses.begin() != CollapseClauses.end()) 5281 return (*CollapseClauses.begin())->getNumForLoops(); 5282 return nullptr; 5283 } 5284 5285 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 5286 auto OrderedClauses = 5287 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 5288 if (OrderedClauses.begin() != OrderedClauses.end()) 5289 return (*OrderedClauses.begin())->getNumForLoops(); 5290 return nullptr; 5291 } 5292 5293 static bool checkSimdlenSafelenSpecified(Sema &S, 5294 const ArrayRef<OMPClause *> Clauses) { 5295 const OMPSafelenClause *Safelen = nullptr; 5296 const OMPSimdlenClause *Simdlen = nullptr; 5297 5298 for (const OMPClause *Clause : Clauses) { 5299 if (Clause->getClauseKind() == OMPC_safelen) 5300 Safelen = cast<OMPSafelenClause>(Clause); 5301 else if (Clause->getClauseKind() == OMPC_simdlen) 5302 Simdlen = cast<OMPSimdlenClause>(Clause); 5303 if (Safelen && Simdlen) 5304 break; 5305 } 5306 5307 if (Simdlen && Safelen) { 5308 llvm::APSInt SimdlenRes, SafelenRes; 5309 const Expr *SimdlenLength = Simdlen->getSimdlen(); 5310 const Expr *SafelenLength = Safelen->getSafelen(); 5311 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 5312 SimdlenLength->isInstantiationDependent() || 5313 SimdlenLength->containsUnexpandedParameterPack()) 5314 return false; 5315 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 5316 SafelenLength->isInstantiationDependent() || 5317 SafelenLength->containsUnexpandedParameterPack()) 5318 return false; 5319 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context); 5320 SafelenLength->EvaluateAsInt(SafelenRes, S.Context); 5321 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 5322 // If both simdlen and safelen clauses are specified, the value of the 5323 // simdlen parameter must be less than or equal to the value of the safelen 5324 // parameter. 5325 if (SimdlenRes > SafelenRes) { 5326 S.Diag(SimdlenLength->getExprLoc(), 5327 diag::err_omp_wrong_simdlen_safelen_values) 5328 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 5329 return true; 5330 } 5331 } 5332 return false; 5333 } 5334 5335 StmtResult 5336 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 5337 SourceLocation StartLoc, SourceLocation EndLoc, 5338 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5339 if (!AStmt) 5340 return StmtError(); 5341 5342 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5343 OMPLoopDirective::HelperExprs B; 5344 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5345 // define the nested loops number. 5346 unsigned NestedLoopCount = checkOpenMPLoop( 5347 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 5348 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 5349 if (NestedLoopCount == 0) 5350 return StmtError(); 5351 5352 assert((CurContext->isDependentContext() || B.builtAll()) && 5353 "omp simd loop exprs were not built"); 5354 5355 if (!CurContext->isDependentContext()) { 5356 // Finalize the clauses that need pre-built expressions for CodeGen. 5357 for (OMPClause *C : Clauses) { 5358 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5359 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5360 B.NumIterations, *this, CurScope, 5361 DSAStack)) 5362 return StmtError(); 5363 } 5364 } 5365 5366 if (checkSimdlenSafelenSpecified(*this, Clauses)) 5367 return StmtError(); 5368 5369 setFunctionHasBranchProtectedScope(); 5370 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 5371 Clauses, AStmt, B); 5372 } 5373 5374 StmtResult 5375 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 5376 SourceLocation StartLoc, SourceLocation EndLoc, 5377 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5378 if (!AStmt) 5379 return StmtError(); 5380 5381 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5382 OMPLoopDirective::HelperExprs B; 5383 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5384 // define the nested loops number. 5385 unsigned NestedLoopCount = checkOpenMPLoop( 5386 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 5387 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 5388 if (NestedLoopCount == 0) 5389 return StmtError(); 5390 5391 assert((CurContext->isDependentContext() || B.builtAll()) && 5392 "omp for loop exprs were not built"); 5393 5394 if (!CurContext->isDependentContext()) { 5395 // Finalize the clauses that need pre-built expressions for CodeGen. 5396 for (OMPClause *C : Clauses) { 5397 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5398 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5399 B.NumIterations, *this, CurScope, 5400 DSAStack)) 5401 return StmtError(); 5402 } 5403 } 5404 5405 setFunctionHasBranchProtectedScope(); 5406 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 5407 Clauses, AStmt, B, DSAStack->isCancelRegion()); 5408 } 5409 5410 StmtResult Sema::ActOnOpenMPForSimdDirective( 5411 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5412 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5413 if (!AStmt) 5414 return StmtError(); 5415 5416 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5417 OMPLoopDirective::HelperExprs B; 5418 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5419 // define the nested loops number. 5420 unsigned NestedLoopCount = 5421 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 5422 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 5423 VarsWithImplicitDSA, B); 5424 if (NestedLoopCount == 0) 5425 return StmtError(); 5426 5427 assert((CurContext->isDependentContext() || B.builtAll()) && 5428 "omp for simd loop exprs were not built"); 5429 5430 if (!CurContext->isDependentContext()) { 5431 // Finalize the clauses that need pre-built expressions for CodeGen. 5432 for (OMPClause *C : Clauses) { 5433 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5434 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5435 B.NumIterations, *this, CurScope, 5436 DSAStack)) 5437 return StmtError(); 5438 } 5439 } 5440 5441 if (checkSimdlenSafelenSpecified(*this, Clauses)) 5442 return StmtError(); 5443 5444 setFunctionHasBranchProtectedScope(); 5445 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 5446 Clauses, AStmt, B); 5447 } 5448 5449 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 5450 Stmt *AStmt, 5451 SourceLocation StartLoc, 5452 SourceLocation EndLoc) { 5453 if (!AStmt) 5454 return StmtError(); 5455 5456 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5457 auto BaseStmt = AStmt; 5458 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 5459 BaseStmt = CS->getCapturedStmt(); 5460 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 5461 auto S = C->children(); 5462 if (S.begin() == S.end()) 5463 return StmtError(); 5464 // All associated statements must be '#pragma omp section' except for 5465 // the first one. 5466 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 5467 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 5468 if (SectionStmt) 5469 Diag(SectionStmt->getLocStart(), 5470 diag::err_omp_sections_substmt_not_section); 5471 return StmtError(); 5472 } 5473 cast<OMPSectionDirective>(SectionStmt) 5474 ->setHasCancel(DSAStack->isCancelRegion()); 5475 } 5476 } else { 5477 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt); 5478 return StmtError(); 5479 } 5480 5481 setFunctionHasBranchProtectedScope(); 5482 5483 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 5484 DSAStack->isCancelRegion()); 5485 } 5486 5487 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 5488 SourceLocation StartLoc, 5489 SourceLocation EndLoc) { 5490 if (!AStmt) 5491 return StmtError(); 5492 5493 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5494 5495 setFunctionHasBranchProtectedScope(); 5496 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 5497 5498 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 5499 DSAStack->isCancelRegion()); 5500 } 5501 5502 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 5503 Stmt *AStmt, 5504 SourceLocation StartLoc, 5505 SourceLocation EndLoc) { 5506 if (!AStmt) 5507 return StmtError(); 5508 5509 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5510 5511 setFunctionHasBranchProtectedScope(); 5512 5513 // OpenMP [2.7.3, single Construct, Restrictions] 5514 // The copyprivate clause must not be used with the nowait clause. 5515 const OMPClause *Nowait = nullptr; 5516 const OMPClause *Copyprivate = nullptr; 5517 for (const OMPClause *Clause : Clauses) { 5518 if (Clause->getClauseKind() == OMPC_nowait) 5519 Nowait = Clause; 5520 else if (Clause->getClauseKind() == OMPC_copyprivate) 5521 Copyprivate = Clause; 5522 if (Copyprivate && Nowait) { 5523 Diag(Copyprivate->getLocStart(), 5524 diag::err_omp_single_copyprivate_with_nowait); 5525 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here); 5526 return StmtError(); 5527 } 5528 } 5529 5530 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 5531 } 5532 5533 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 5534 SourceLocation StartLoc, 5535 SourceLocation EndLoc) { 5536 if (!AStmt) 5537 return StmtError(); 5538 5539 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5540 5541 setFunctionHasBranchProtectedScope(); 5542 5543 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 5544 } 5545 5546 StmtResult Sema::ActOnOpenMPCriticalDirective( 5547 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 5548 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 5549 if (!AStmt) 5550 return StmtError(); 5551 5552 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5553 5554 bool ErrorFound = false; 5555 llvm::APSInt Hint; 5556 SourceLocation HintLoc; 5557 bool DependentHint = false; 5558 for (const OMPClause *C : Clauses) { 5559 if (C->getClauseKind() == OMPC_hint) { 5560 if (!DirName.getName()) { 5561 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name); 5562 ErrorFound = true; 5563 } 5564 Expr *E = cast<OMPHintClause>(C)->getHint(); 5565 if (E->isTypeDependent() || E->isValueDependent() || 5566 E->isInstantiationDependent()) { 5567 DependentHint = true; 5568 } else { 5569 Hint = E->EvaluateKnownConstInt(Context); 5570 HintLoc = C->getLocStart(); 5571 } 5572 } 5573 } 5574 if (ErrorFound) 5575 return StmtError(); 5576 const auto Pair = DSAStack->getCriticalWithHint(DirName); 5577 if (Pair.first && DirName.getName() && !DependentHint) { 5578 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 5579 Diag(StartLoc, diag::err_omp_critical_with_hint); 5580 if (HintLoc.isValid()) 5581 Diag(HintLoc, diag::note_omp_critical_hint_here) 5582 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); 5583 else 5584 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 5585 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 5586 Diag(C->getLocStart(), diag::note_omp_critical_hint_here) 5587 << 1 5588 << C->getHint()->EvaluateKnownConstInt(Context).toString( 5589 /*Radix=*/10, /*Signed=*/false); 5590 } else { 5591 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1; 5592 } 5593 } 5594 } 5595 5596 setFunctionHasBranchProtectedScope(); 5597 5598 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 5599 Clauses, AStmt); 5600 if (!Pair.first && DirName.getName() && !DependentHint) 5601 DSAStack->addCriticalWithHint(Dir, Hint); 5602 return Dir; 5603 } 5604 5605 StmtResult Sema::ActOnOpenMPParallelForDirective( 5606 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5607 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5608 if (!AStmt) 5609 return StmtError(); 5610 5611 auto *CS = cast<CapturedStmt>(AStmt); 5612 // 1.2.2 OpenMP Language Terminology 5613 // Structured block - An executable statement with a single entry at the 5614 // top and a single exit at the bottom. 5615 // The point of exit cannot be a branch out of the structured block. 5616 // longjmp() and throw() must not violate the entry/exit criteria. 5617 CS->getCapturedDecl()->setNothrow(); 5618 5619 OMPLoopDirective::HelperExprs B; 5620 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5621 // define the nested loops number. 5622 unsigned NestedLoopCount = 5623 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 5624 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 5625 VarsWithImplicitDSA, B); 5626 if (NestedLoopCount == 0) 5627 return StmtError(); 5628 5629 assert((CurContext->isDependentContext() || B.builtAll()) && 5630 "omp parallel for loop exprs were not built"); 5631 5632 if (!CurContext->isDependentContext()) { 5633 // Finalize the clauses that need pre-built expressions for CodeGen. 5634 for (OMPClause *C : Clauses) { 5635 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5636 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5637 B.NumIterations, *this, CurScope, 5638 DSAStack)) 5639 return StmtError(); 5640 } 5641 } 5642 5643 setFunctionHasBranchProtectedScope(); 5644 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc, 5645 NestedLoopCount, Clauses, AStmt, B, 5646 DSAStack->isCancelRegion()); 5647 } 5648 5649 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 5650 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5651 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5652 if (!AStmt) 5653 return StmtError(); 5654 5655 auto *CS = cast<CapturedStmt>(AStmt); 5656 // 1.2.2 OpenMP Language Terminology 5657 // Structured block - An executable statement with a single entry at the 5658 // top and a single exit at the bottom. 5659 // The point of exit cannot be a branch out of the structured block. 5660 // longjmp() and throw() must not violate the entry/exit criteria. 5661 CS->getCapturedDecl()->setNothrow(); 5662 5663 OMPLoopDirective::HelperExprs B; 5664 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5665 // define the nested loops number. 5666 unsigned NestedLoopCount = 5667 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 5668 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 5669 VarsWithImplicitDSA, B); 5670 if (NestedLoopCount == 0) 5671 return StmtError(); 5672 5673 if (!CurContext->isDependentContext()) { 5674 // Finalize the clauses that need pre-built expressions for CodeGen. 5675 for (OMPClause *C : Clauses) { 5676 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5677 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5678 B.NumIterations, *this, CurScope, 5679 DSAStack)) 5680 return StmtError(); 5681 } 5682 } 5683 5684 if (checkSimdlenSafelenSpecified(*this, Clauses)) 5685 return StmtError(); 5686 5687 setFunctionHasBranchProtectedScope(); 5688 return OMPParallelForSimdDirective::Create( 5689 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 5690 } 5691 5692 StmtResult 5693 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 5694 Stmt *AStmt, SourceLocation StartLoc, 5695 SourceLocation EndLoc) { 5696 if (!AStmt) 5697 return StmtError(); 5698 5699 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5700 auto BaseStmt = AStmt; 5701 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 5702 BaseStmt = CS->getCapturedStmt(); 5703 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 5704 auto S = C->children(); 5705 if (S.begin() == S.end()) 5706 return StmtError(); 5707 // All associated statements must be '#pragma omp section' except for 5708 // the first one. 5709 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 5710 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 5711 if (SectionStmt) 5712 Diag(SectionStmt->getLocStart(), 5713 diag::err_omp_parallel_sections_substmt_not_section); 5714 return StmtError(); 5715 } 5716 cast<OMPSectionDirective>(SectionStmt) 5717 ->setHasCancel(DSAStack->isCancelRegion()); 5718 } 5719 } else { 5720 Diag(AStmt->getLocStart(), 5721 diag::err_omp_parallel_sections_not_compound_stmt); 5722 return StmtError(); 5723 } 5724 5725 setFunctionHasBranchProtectedScope(); 5726 5727 return OMPParallelSectionsDirective::Create( 5728 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion()); 5729 } 5730 5731 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 5732 Stmt *AStmt, SourceLocation StartLoc, 5733 SourceLocation EndLoc) { 5734 if (!AStmt) 5735 return StmtError(); 5736 5737 auto *CS = cast<CapturedStmt>(AStmt); 5738 // 1.2.2 OpenMP Language Terminology 5739 // Structured block - An executable statement with a single entry at the 5740 // top and a single exit at the bottom. 5741 // The point of exit cannot be a branch out of the structured block. 5742 // longjmp() and throw() must not violate the entry/exit criteria. 5743 CS->getCapturedDecl()->setNothrow(); 5744 5745 setFunctionHasBranchProtectedScope(); 5746 5747 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 5748 DSAStack->isCancelRegion()); 5749 } 5750 5751 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 5752 SourceLocation EndLoc) { 5753 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 5754 } 5755 5756 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 5757 SourceLocation EndLoc) { 5758 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 5759 } 5760 5761 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 5762 SourceLocation EndLoc) { 5763 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 5764 } 5765 5766 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 5767 Stmt *AStmt, 5768 SourceLocation StartLoc, 5769 SourceLocation EndLoc) { 5770 if (!AStmt) 5771 return StmtError(); 5772 5773 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5774 5775 setFunctionHasBranchProtectedScope(); 5776 5777 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 5778 AStmt, 5779 DSAStack->getTaskgroupReductionRef()); 5780 } 5781 5782 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 5783 SourceLocation StartLoc, 5784 SourceLocation EndLoc) { 5785 assert(Clauses.size() <= 1 && "Extra clauses in flush directive"); 5786 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 5787 } 5788 5789 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 5790 Stmt *AStmt, 5791 SourceLocation StartLoc, 5792 SourceLocation EndLoc) { 5793 const OMPClause *DependFound = nullptr; 5794 const OMPClause *DependSourceClause = nullptr; 5795 const OMPClause *DependSinkClause = nullptr; 5796 bool ErrorFound = false; 5797 const OMPThreadsClause *TC = nullptr; 5798 const OMPSIMDClause *SC = nullptr; 5799 for (const OMPClause *C : Clauses) { 5800 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 5801 DependFound = C; 5802 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 5803 if (DependSourceClause) { 5804 Diag(C->getLocStart(), diag::err_omp_more_one_clause) 5805 << getOpenMPDirectiveName(OMPD_ordered) 5806 << getOpenMPClauseName(OMPC_depend) << 2; 5807 ErrorFound = true; 5808 } else { 5809 DependSourceClause = C; 5810 } 5811 if (DependSinkClause) { 5812 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed) 5813 << 0; 5814 ErrorFound = true; 5815 } 5816 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 5817 if (DependSourceClause) { 5818 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed) 5819 << 1; 5820 ErrorFound = true; 5821 } 5822 DependSinkClause = C; 5823 } 5824 } else if (C->getClauseKind() == OMPC_threads) { 5825 TC = cast<OMPThreadsClause>(C); 5826 } else if (C->getClauseKind() == OMPC_simd) { 5827 SC = cast<OMPSIMDClause>(C); 5828 } 5829 } 5830 if (!ErrorFound && !SC && 5831 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 5832 // OpenMP [2.8.1,simd Construct, Restrictions] 5833 // An ordered construct with the simd clause is the only OpenMP construct 5834 // that can appear in the simd region. 5835 Diag(StartLoc, diag::err_omp_prohibited_region_simd); 5836 ErrorFound = true; 5837 } else if (DependFound && (TC || SC)) { 5838 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd) 5839 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 5840 ErrorFound = true; 5841 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) { 5842 Diag(DependFound->getLocStart(), 5843 diag::err_omp_ordered_directive_without_param); 5844 ErrorFound = true; 5845 } else if (TC || Clauses.empty()) { 5846 if (const Expr *Param = DSAStack->getParentOrderedRegionParam()) { 5847 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc; 5848 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 5849 << (TC != nullptr); 5850 Diag(Param->getLocStart(), diag::note_omp_ordered_param); 5851 ErrorFound = true; 5852 } 5853 } 5854 if ((!AStmt && !DependFound) || ErrorFound) 5855 return StmtError(); 5856 5857 if (AStmt) { 5858 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5859 5860 setFunctionHasBranchProtectedScope(); 5861 } 5862 5863 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 5864 } 5865 5866 namespace { 5867 /// Helper class for checking expression in 'omp atomic [update]' 5868 /// construct. 5869 class OpenMPAtomicUpdateChecker { 5870 /// Error results for atomic update expressions. 5871 enum ExprAnalysisErrorCode { 5872 /// A statement is not an expression statement. 5873 NotAnExpression, 5874 /// Expression is not builtin binary or unary operation. 5875 NotABinaryOrUnaryExpression, 5876 /// Unary operation is not post-/pre- increment/decrement operation. 5877 NotAnUnaryIncDecExpression, 5878 /// An expression is not of scalar type. 5879 NotAScalarType, 5880 /// A binary operation is not an assignment operation. 5881 NotAnAssignmentOp, 5882 /// RHS part of the binary operation is not a binary expression. 5883 NotABinaryExpression, 5884 /// RHS part is not additive/multiplicative/shift/biwise binary 5885 /// expression. 5886 NotABinaryOperator, 5887 /// RHS binary operation does not have reference to the updated LHS 5888 /// part. 5889 NotAnUpdateExpression, 5890 /// No errors is found. 5891 NoError 5892 }; 5893 /// Reference to Sema. 5894 Sema &SemaRef; 5895 /// A location for note diagnostics (when error is found). 5896 SourceLocation NoteLoc; 5897 /// 'x' lvalue part of the source atomic expression. 5898 Expr *X; 5899 /// 'expr' rvalue part of the source atomic expression. 5900 Expr *E; 5901 /// Helper expression of the form 5902 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 5903 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 5904 Expr *UpdateExpr; 5905 /// Is 'x' a LHS in a RHS part of full update expression. It is 5906 /// important for non-associative operations. 5907 bool IsXLHSInRHSPart; 5908 BinaryOperatorKind Op; 5909 SourceLocation OpLoc; 5910 /// true if the source expression is a postfix unary operation, false 5911 /// if it is a prefix unary operation. 5912 bool IsPostfixUpdate; 5913 5914 public: 5915 OpenMPAtomicUpdateChecker(Sema &SemaRef) 5916 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 5917 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 5918 /// Check specified statement that it is suitable for 'atomic update' 5919 /// constructs and extract 'x', 'expr' and Operation from the original 5920 /// expression. If DiagId and NoteId == 0, then only check is performed 5921 /// without error notification. 5922 /// \param DiagId Diagnostic which should be emitted if error is found. 5923 /// \param NoteId Diagnostic note for the main error message. 5924 /// \return true if statement is not an update expression, false otherwise. 5925 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 5926 /// Return the 'x' lvalue part of the source atomic expression. 5927 Expr *getX() const { return X; } 5928 /// Return the 'expr' rvalue part of the source atomic expression. 5929 Expr *getExpr() const { return E; } 5930 /// Return the update expression used in calculation of the updated 5931 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 5932 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 5933 Expr *getUpdateExpr() const { return UpdateExpr; } 5934 /// Return true if 'x' is LHS in RHS part of full update expression, 5935 /// false otherwise. 5936 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 5937 5938 /// true if the source expression is a postfix unary operation, false 5939 /// if it is a prefix unary operation. 5940 bool isPostfixUpdate() const { return IsPostfixUpdate; } 5941 5942 private: 5943 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 5944 unsigned NoteId = 0); 5945 }; 5946 } // namespace 5947 5948 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 5949 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 5950 ExprAnalysisErrorCode ErrorFound = NoError; 5951 SourceLocation ErrorLoc, NoteLoc; 5952 SourceRange ErrorRange, NoteRange; 5953 // Allowed constructs are: 5954 // x = x binop expr; 5955 // x = expr binop x; 5956 if (AtomicBinOp->getOpcode() == BO_Assign) { 5957 X = AtomicBinOp->getLHS(); 5958 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 5959 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 5960 if (AtomicInnerBinOp->isMultiplicativeOp() || 5961 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 5962 AtomicInnerBinOp->isBitwiseOp()) { 5963 Op = AtomicInnerBinOp->getOpcode(); 5964 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 5965 Expr *LHS = AtomicInnerBinOp->getLHS(); 5966 Expr *RHS = AtomicInnerBinOp->getRHS(); 5967 llvm::FoldingSetNodeID XId, LHSId, RHSId; 5968 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 5969 /*Canonical=*/true); 5970 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 5971 /*Canonical=*/true); 5972 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 5973 /*Canonical=*/true); 5974 if (XId == LHSId) { 5975 E = RHS; 5976 IsXLHSInRHSPart = true; 5977 } else if (XId == RHSId) { 5978 E = LHS; 5979 IsXLHSInRHSPart = false; 5980 } else { 5981 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 5982 ErrorRange = AtomicInnerBinOp->getSourceRange(); 5983 NoteLoc = X->getExprLoc(); 5984 NoteRange = X->getSourceRange(); 5985 ErrorFound = NotAnUpdateExpression; 5986 } 5987 } else { 5988 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 5989 ErrorRange = AtomicInnerBinOp->getSourceRange(); 5990 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 5991 NoteRange = SourceRange(NoteLoc, NoteLoc); 5992 ErrorFound = NotABinaryOperator; 5993 } 5994 } else { 5995 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 5996 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 5997 ErrorFound = NotABinaryExpression; 5998 } 5999 } else { 6000 ErrorLoc = AtomicBinOp->getExprLoc(); 6001 ErrorRange = AtomicBinOp->getSourceRange(); 6002 NoteLoc = AtomicBinOp->getOperatorLoc(); 6003 NoteRange = SourceRange(NoteLoc, NoteLoc); 6004 ErrorFound = NotAnAssignmentOp; 6005 } 6006 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 6007 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 6008 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 6009 return true; 6010 } 6011 if (SemaRef.CurContext->isDependentContext()) 6012 E = X = UpdateExpr = nullptr; 6013 return ErrorFound != NoError; 6014 } 6015 6016 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 6017 unsigned NoteId) { 6018 ExprAnalysisErrorCode ErrorFound = NoError; 6019 SourceLocation ErrorLoc, NoteLoc; 6020 SourceRange ErrorRange, NoteRange; 6021 // Allowed constructs are: 6022 // x++; 6023 // x--; 6024 // ++x; 6025 // --x; 6026 // x binop= expr; 6027 // x = x binop expr; 6028 // x = expr binop x; 6029 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 6030 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 6031 if (AtomicBody->getType()->isScalarType() || 6032 AtomicBody->isInstantiationDependent()) { 6033 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 6034 AtomicBody->IgnoreParenImpCasts())) { 6035 // Check for Compound Assignment Operation 6036 Op = BinaryOperator::getOpForCompoundAssignment( 6037 AtomicCompAssignOp->getOpcode()); 6038 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 6039 E = AtomicCompAssignOp->getRHS(); 6040 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 6041 IsXLHSInRHSPart = true; 6042 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 6043 AtomicBody->IgnoreParenImpCasts())) { 6044 // Check for Binary Operation 6045 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 6046 return true; 6047 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 6048 AtomicBody->IgnoreParenImpCasts())) { 6049 // Check for Unary Operation 6050 if (AtomicUnaryOp->isIncrementDecrementOp()) { 6051 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 6052 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 6053 OpLoc = AtomicUnaryOp->getOperatorLoc(); 6054 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 6055 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 6056 IsXLHSInRHSPart = true; 6057 } else { 6058 ErrorFound = NotAnUnaryIncDecExpression; 6059 ErrorLoc = AtomicUnaryOp->getExprLoc(); 6060 ErrorRange = AtomicUnaryOp->getSourceRange(); 6061 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 6062 NoteRange = SourceRange(NoteLoc, NoteLoc); 6063 } 6064 } else if (!AtomicBody->isInstantiationDependent()) { 6065 ErrorFound = NotABinaryOrUnaryExpression; 6066 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 6067 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 6068 } 6069 } else { 6070 ErrorFound = NotAScalarType; 6071 NoteLoc = ErrorLoc = AtomicBody->getLocStart(); 6072 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6073 } 6074 } else { 6075 ErrorFound = NotAnExpression; 6076 NoteLoc = ErrorLoc = S->getLocStart(); 6077 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6078 } 6079 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 6080 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 6081 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 6082 return true; 6083 } 6084 if (SemaRef.CurContext->isDependentContext()) 6085 E = X = UpdateExpr = nullptr; 6086 if (ErrorFound == NoError && E && X) { 6087 // Build an update expression of form 'OpaqueValueExpr(x) binop 6088 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 6089 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 6090 auto *OVEX = new (SemaRef.getASTContext()) 6091 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); 6092 auto *OVEExpr = new (SemaRef.getASTContext()) 6093 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); 6094 ExprResult Update = 6095 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 6096 IsXLHSInRHSPart ? OVEExpr : OVEX); 6097 if (Update.isInvalid()) 6098 return true; 6099 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 6100 Sema::AA_Casting); 6101 if (Update.isInvalid()) 6102 return true; 6103 UpdateExpr = Update.get(); 6104 } 6105 return ErrorFound != NoError; 6106 } 6107 6108 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 6109 Stmt *AStmt, 6110 SourceLocation StartLoc, 6111 SourceLocation EndLoc) { 6112 if (!AStmt) 6113 return StmtError(); 6114 6115 auto *CS = cast<CapturedStmt>(AStmt); 6116 // 1.2.2 OpenMP Language Terminology 6117 // Structured block - An executable statement with a single entry at the 6118 // top and a single exit at the bottom. 6119 // The point of exit cannot be a branch out of the structured block. 6120 // longjmp() and throw() must not violate the entry/exit criteria. 6121 OpenMPClauseKind AtomicKind = OMPC_unknown; 6122 SourceLocation AtomicKindLoc; 6123 for (const OMPClause *C : Clauses) { 6124 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 6125 C->getClauseKind() == OMPC_update || 6126 C->getClauseKind() == OMPC_capture) { 6127 if (AtomicKind != OMPC_unknown) { 6128 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses) 6129 << SourceRange(C->getLocStart(), C->getLocEnd()); 6130 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause) 6131 << getOpenMPClauseName(AtomicKind); 6132 } else { 6133 AtomicKind = C->getClauseKind(); 6134 AtomicKindLoc = C->getLocStart(); 6135 } 6136 } 6137 } 6138 6139 Stmt *Body = CS->getCapturedStmt(); 6140 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 6141 Body = EWC->getSubExpr(); 6142 6143 Expr *X = nullptr; 6144 Expr *V = nullptr; 6145 Expr *E = nullptr; 6146 Expr *UE = nullptr; 6147 bool IsXLHSInRHSPart = false; 6148 bool IsPostfixUpdate = false; 6149 // OpenMP [2.12.6, atomic Construct] 6150 // In the next expressions: 6151 // * x and v (as applicable) are both l-value expressions with scalar type. 6152 // * During the execution of an atomic region, multiple syntactic 6153 // occurrences of x must designate the same storage location. 6154 // * Neither of v and expr (as applicable) may access the storage location 6155 // designated by x. 6156 // * Neither of x and expr (as applicable) may access the storage location 6157 // designated by v. 6158 // * expr is an expression with scalar type. 6159 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 6160 // * binop, binop=, ++, and -- are not overloaded operators. 6161 // * The expression x binop expr must be numerically equivalent to x binop 6162 // (expr). This requirement is satisfied if the operators in expr have 6163 // precedence greater than binop, or by using parentheses around expr or 6164 // subexpressions of expr. 6165 // * The expression expr binop x must be numerically equivalent to (expr) 6166 // binop x. This requirement is satisfied if the operators in expr have 6167 // precedence equal to or greater than binop, or by using parentheses around 6168 // expr or subexpressions of expr. 6169 // * For forms that allow multiple occurrences of x, the number of times 6170 // that x is evaluated is unspecified. 6171 if (AtomicKind == OMPC_read) { 6172 enum { 6173 NotAnExpression, 6174 NotAnAssignmentOp, 6175 NotAScalarType, 6176 NotAnLValue, 6177 NoError 6178 } ErrorFound = NoError; 6179 SourceLocation ErrorLoc, NoteLoc; 6180 SourceRange ErrorRange, NoteRange; 6181 // If clause is read: 6182 // v = x; 6183 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 6184 const auto *AtomicBinOp = 6185 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 6186 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 6187 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 6188 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 6189 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 6190 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 6191 if (!X->isLValue() || !V->isLValue()) { 6192 const Expr *NotLValueExpr = X->isLValue() ? V : X; 6193 ErrorFound = NotAnLValue; 6194 ErrorLoc = AtomicBinOp->getExprLoc(); 6195 ErrorRange = AtomicBinOp->getSourceRange(); 6196 NoteLoc = NotLValueExpr->getExprLoc(); 6197 NoteRange = NotLValueExpr->getSourceRange(); 6198 } 6199 } else if (!X->isInstantiationDependent() || 6200 !V->isInstantiationDependent()) { 6201 const Expr *NotScalarExpr = 6202 (X->isInstantiationDependent() || X->getType()->isScalarType()) 6203 ? V 6204 : X; 6205 ErrorFound = NotAScalarType; 6206 ErrorLoc = AtomicBinOp->getExprLoc(); 6207 ErrorRange = AtomicBinOp->getSourceRange(); 6208 NoteLoc = NotScalarExpr->getExprLoc(); 6209 NoteRange = NotScalarExpr->getSourceRange(); 6210 } 6211 } else if (!AtomicBody->isInstantiationDependent()) { 6212 ErrorFound = NotAnAssignmentOp; 6213 ErrorLoc = AtomicBody->getExprLoc(); 6214 ErrorRange = AtomicBody->getSourceRange(); 6215 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 6216 : AtomicBody->getExprLoc(); 6217 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 6218 : AtomicBody->getSourceRange(); 6219 } 6220 } else { 6221 ErrorFound = NotAnExpression; 6222 NoteLoc = ErrorLoc = Body->getLocStart(); 6223 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6224 } 6225 if (ErrorFound != NoError) { 6226 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 6227 << ErrorRange; 6228 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 6229 << NoteRange; 6230 return StmtError(); 6231 } 6232 if (CurContext->isDependentContext()) 6233 V = X = nullptr; 6234 } else if (AtomicKind == OMPC_write) { 6235 enum { 6236 NotAnExpression, 6237 NotAnAssignmentOp, 6238 NotAScalarType, 6239 NotAnLValue, 6240 NoError 6241 } ErrorFound = NoError; 6242 SourceLocation ErrorLoc, NoteLoc; 6243 SourceRange ErrorRange, NoteRange; 6244 // If clause is write: 6245 // x = expr; 6246 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 6247 const auto *AtomicBinOp = 6248 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 6249 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 6250 X = AtomicBinOp->getLHS(); 6251 E = AtomicBinOp->getRHS(); 6252 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 6253 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 6254 if (!X->isLValue()) { 6255 ErrorFound = NotAnLValue; 6256 ErrorLoc = AtomicBinOp->getExprLoc(); 6257 ErrorRange = AtomicBinOp->getSourceRange(); 6258 NoteLoc = X->getExprLoc(); 6259 NoteRange = X->getSourceRange(); 6260 } 6261 } else if (!X->isInstantiationDependent() || 6262 !E->isInstantiationDependent()) { 6263 const Expr *NotScalarExpr = 6264 (X->isInstantiationDependent() || X->getType()->isScalarType()) 6265 ? E 6266 : X; 6267 ErrorFound = NotAScalarType; 6268 ErrorLoc = AtomicBinOp->getExprLoc(); 6269 ErrorRange = AtomicBinOp->getSourceRange(); 6270 NoteLoc = NotScalarExpr->getExprLoc(); 6271 NoteRange = NotScalarExpr->getSourceRange(); 6272 } 6273 } else if (!AtomicBody->isInstantiationDependent()) { 6274 ErrorFound = NotAnAssignmentOp; 6275 ErrorLoc = AtomicBody->getExprLoc(); 6276 ErrorRange = AtomicBody->getSourceRange(); 6277 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 6278 : AtomicBody->getExprLoc(); 6279 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 6280 : AtomicBody->getSourceRange(); 6281 } 6282 } else { 6283 ErrorFound = NotAnExpression; 6284 NoteLoc = ErrorLoc = Body->getLocStart(); 6285 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6286 } 6287 if (ErrorFound != NoError) { 6288 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 6289 << ErrorRange; 6290 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 6291 << NoteRange; 6292 return StmtError(); 6293 } 6294 if (CurContext->isDependentContext()) 6295 E = X = nullptr; 6296 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 6297 // If clause is update: 6298 // x++; 6299 // x--; 6300 // ++x; 6301 // --x; 6302 // x binop= expr; 6303 // x = x binop expr; 6304 // x = expr binop x; 6305 OpenMPAtomicUpdateChecker Checker(*this); 6306 if (Checker.checkStatement( 6307 Body, (AtomicKind == OMPC_update) 6308 ? diag::err_omp_atomic_update_not_expression_statement 6309 : diag::err_omp_atomic_not_expression_statement, 6310 diag::note_omp_atomic_update)) 6311 return StmtError(); 6312 if (!CurContext->isDependentContext()) { 6313 E = Checker.getExpr(); 6314 X = Checker.getX(); 6315 UE = Checker.getUpdateExpr(); 6316 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6317 } 6318 } else if (AtomicKind == OMPC_capture) { 6319 enum { 6320 NotAnAssignmentOp, 6321 NotACompoundStatement, 6322 NotTwoSubstatements, 6323 NotASpecificExpression, 6324 NoError 6325 } ErrorFound = NoError; 6326 SourceLocation ErrorLoc, NoteLoc; 6327 SourceRange ErrorRange, NoteRange; 6328 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 6329 // If clause is a capture: 6330 // v = x++; 6331 // v = x--; 6332 // v = ++x; 6333 // v = --x; 6334 // v = x binop= expr; 6335 // v = x = x binop expr; 6336 // v = x = expr binop x; 6337 const auto *AtomicBinOp = 6338 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 6339 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 6340 V = AtomicBinOp->getLHS(); 6341 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 6342 OpenMPAtomicUpdateChecker Checker(*this); 6343 if (Checker.checkStatement( 6344 Body, diag::err_omp_atomic_capture_not_expression_statement, 6345 diag::note_omp_atomic_update)) 6346 return StmtError(); 6347 E = Checker.getExpr(); 6348 X = Checker.getX(); 6349 UE = Checker.getUpdateExpr(); 6350 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6351 IsPostfixUpdate = Checker.isPostfixUpdate(); 6352 } else if (!AtomicBody->isInstantiationDependent()) { 6353 ErrorLoc = AtomicBody->getExprLoc(); 6354 ErrorRange = AtomicBody->getSourceRange(); 6355 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 6356 : AtomicBody->getExprLoc(); 6357 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 6358 : AtomicBody->getSourceRange(); 6359 ErrorFound = NotAnAssignmentOp; 6360 } 6361 if (ErrorFound != NoError) { 6362 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 6363 << ErrorRange; 6364 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 6365 return StmtError(); 6366 } 6367 if (CurContext->isDependentContext()) 6368 UE = V = E = X = nullptr; 6369 } else { 6370 // If clause is a capture: 6371 // { v = x; x = expr; } 6372 // { v = x; x++; } 6373 // { v = x; x--; } 6374 // { v = x; ++x; } 6375 // { v = x; --x; } 6376 // { v = x; x binop= expr; } 6377 // { v = x; x = x binop expr; } 6378 // { v = x; x = expr binop x; } 6379 // { x++; v = x; } 6380 // { x--; v = x; } 6381 // { ++x; v = x; } 6382 // { --x; v = x; } 6383 // { x binop= expr; v = x; } 6384 // { x = x binop expr; v = x; } 6385 // { x = expr binop x; v = x; } 6386 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 6387 // Check that this is { expr1; expr2; } 6388 if (CS->size() == 2) { 6389 Stmt *First = CS->body_front(); 6390 Stmt *Second = CS->body_back(); 6391 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 6392 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 6393 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 6394 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 6395 // Need to find what subexpression is 'v' and what is 'x'. 6396 OpenMPAtomicUpdateChecker Checker(*this); 6397 bool IsUpdateExprFound = !Checker.checkStatement(Second); 6398 BinaryOperator *BinOp = nullptr; 6399 if (IsUpdateExprFound) { 6400 BinOp = dyn_cast<BinaryOperator>(First); 6401 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 6402 } 6403 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 6404 // { v = x; x++; } 6405 // { v = x; x--; } 6406 // { v = x; ++x; } 6407 // { v = x; --x; } 6408 // { v = x; x binop= expr; } 6409 // { v = x; x = x binop expr; } 6410 // { v = x; x = expr binop x; } 6411 // Check that the first expression has form v = x. 6412 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 6413 llvm::FoldingSetNodeID XId, PossibleXId; 6414 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 6415 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 6416 IsUpdateExprFound = XId == PossibleXId; 6417 if (IsUpdateExprFound) { 6418 V = BinOp->getLHS(); 6419 X = Checker.getX(); 6420 E = Checker.getExpr(); 6421 UE = Checker.getUpdateExpr(); 6422 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6423 IsPostfixUpdate = true; 6424 } 6425 } 6426 if (!IsUpdateExprFound) { 6427 IsUpdateExprFound = !Checker.checkStatement(First); 6428 BinOp = nullptr; 6429 if (IsUpdateExprFound) { 6430 BinOp = dyn_cast<BinaryOperator>(Second); 6431 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 6432 } 6433 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 6434 // { x++; v = x; } 6435 // { x--; v = x; } 6436 // { ++x; v = x; } 6437 // { --x; v = x; } 6438 // { x binop= expr; v = x; } 6439 // { x = x binop expr; v = x; } 6440 // { x = expr binop x; v = x; } 6441 // Check that the second expression has form v = x. 6442 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 6443 llvm::FoldingSetNodeID XId, PossibleXId; 6444 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 6445 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 6446 IsUpdateExprFound = XId == PossibleXId; 6447 if (IsUpdateExprFound) { 6448 V = BinOp->getLHS(); 6449 X = Checker.getX(); 6450 E = Checker.getExpr(); 6451 UE = Checker.getUpdateExpr(); 6452 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6453 IsPostfixUpdate = false; 6454 } 6455 } 6456 } 6457 if (!IsUpdateExprFound) { 6458 // { v = x; x = expr; } 6459 auto *FirstExpr = dyn_cast<Expr>(First); 6460 auto *SecondExpr = dyn_cast<Expr>(Second); 6461 if (!FirstExpr || !SecondExpr || 6462 !(FirstExpr->isInstantiationDependent() || 6463 SecondExpr->isInstantiationDependent())) { 6464 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 6465 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 6466 ErrorFound = NotAnAssignmentOp; 6467 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 6468 : First->getLocStart(); 6469 NoteRange = ErrorRange = FirstBinOp 6470 ? FirstBinOp->getSourceRange() 6471 : SourceRange(ErrorLoc, ErrorLoc); 6472 } else { 6473 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 6474 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 6475 ErrorFound = NotAnAssignmentOp; 6476 NoteLoc = ErrorLoc = SecondBinOp 6477 ? SecondBinOp->getOperatorLoc() 6478 : Second->getLocStart(); 6479 NoteRange = ErrorRange = 6480 SecondBinOp ? SecondBinOp->getSourceRange() 6481 : SourceRange(ErrorLoc, ErrorLoc); 6482 } else { 6483 Expr *PossibleXRHSInFirst = 6484 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 6485 Expr *PossibleXLHSInSecond = 6486 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 6487 llvm::FoldingSetNodeID X1Id, X2Id; 6488 PossibleXRHSInFirst->Profile(X1Id, Context, 6489 /*Canonical=*/true); 6490 PossibleXLHSInSecond->Profile(X2Id, Context, 6491 /*Canonical=*/true); 6492 IsUpdateExprFound = X1Id == X2Id; 6493 if (IsUpdateExprFound) { 6494 V = FirstBinOp->getLHS(); 6495 X = SecondBinOp->getLHS(); 6496 E = SecondBinOp->getRHS(); 6497 UE = nullptr; 6498 IsXLHSInRHSPart = false; 6499 IsPostfixUpdate = true; 6500 } else { 6501 ErrorFound = NotASpecificExpression; 6502 ErrorLoc = FirstBinOp->getExprLoc(); 6503 ErrorRange = FirstBinOp->getSourceRange(); 6504 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 6505 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 6506 } 6507 } 6508 } 6509 } 6510 } 6511 } else { 6512 NoteLoc = ErrorLoc = Body->getLocStart(); 6513 NoteRange = ErrorRange = 6514 SourceRange(Body->getLocStart(), Body->getLocStart()); 6515 ErrorFound = NotTwoSubstatements; 6516 } 6517 } else { 6518 NoteLoc = ErrorLoc = Body->getLocStart(); 6519 NoteRange = ErrorRange = 6520 SourceRange(Body->getLocStart(), Body->getLocStart()); 6521 ErrorFound = NotACompoundStatement; 6522 } 6523 if (ErrorFound != NoError) { 6524 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 6525 << ErrorRange; 6526 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 6527 return StmtError(); 6528 } 6529 if (CurContext->isDependentContext()) 6530 UE = V = E = X = nullptr; 6531 } 6532 } 6533 6534 setFunctionHasBranchProtectedScope(); 6535 6536 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 6537 X, V, E, UE, IsXLHSInRHSPart, 6538 IsPostfixUpdate); 6539 } 6540 6541 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 6542 Stmt *AStmt, 6543 SourceLocation StartLoc, 6544 SourceLocation EndLoc) { 6545 if (!AStmt) 6546 return StmtError(); 6547 6548 auto *CS = cast<CapturedStmt>(AStmt); 6549 // 1.2.2 OpenMP Language Terminology 6550 // Structured block - An executable statement with a single entry at the 6551 // top and a single exit at the bottom. 6552 // The point of exit cannot be a branch out of the structured block. 6553 // longjmp() and throw() must not violate the entry/exit criteria. 6554 CS->getCapturedDecl()->setNothrow(); 6555 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 6556 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6557 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6558 // 1.2.2 OpenMP Language Terminology 6559 // Structured block - An executable statement with a single entry at the 6560 // top and a single exit at the bottom. 6561 // The point of exit cannot be a branch out of the structured block. 6562 // longjmp() and throw() must not violate the entry/exit criteria. 6563 CS->getCapturedDecl()->setNothrow(); 6564 } 6565 6566 // OpenMP [2.16, Nesting of Regions] 6567 // If specified, a teams construct must be contained within a target 6568 // construct. That target construct must contain no statements or directives 6569 // outside of the teams construct. 6570 if (DSAStack->hasInnerTeamsRegion()) { 6571 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 6572 bool OMPTeamsFound = true; 6573 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 6574 auto I = CS->body_begin(); 6575 while (I != CS->body_end()) { 6576 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 6577 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) { 6578 OMPTeamsFound = false; 6579 break; 6580 } 6581 ++I; 6582 } 6583 assert(I != CS->body_end() && "Not found statement"); 6584 S = *I; 6585 } else { 6586 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 6587 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 6588 } 6589 if (!OMPTeamsFound) { 6590 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 6591 Diag(DSAStack->getInnerTeamsRegionLoc(), 6592 diag::note_omp_nested_teams_construct_here); 6593 Diag(S->getLocStart(), diag::note_omp_nested_statement_here) 6594 << isa<OMPExecutableDirective>(S); 6595 return StmtError(); 6596 } 6597 } 6598 6599 setFunctionHasBranchProtectedScope(); 6600 6601 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 6602 } 6603 6604 StmtResult 6605 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 6606 Stmt *AStmt, SourceLocation StartLoc, 6607 SourceLocation EndLoc) { 6608 if (!AStmt) 6609 return StmtError(); 6610 6611 auto *CS = cast<CapturedStmt>(AStmt); 6612 // 1.2.2 OpenMP Language Terminology 6613 // Structured block - An executable statement with a single entry at the 6614 // top and a single exit at the bottom. 6615 // The point of exit cannot be a branch out of the structured block. 6616 // longjmp() and throw() must not violate the entry/exit criteria. 6617 CS->getCapturedDecl()->setNothrow(); 6618 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 6619 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6620 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6621 // 1.2.2 OpenMP Language Terminology 6622 // Structured block - An executable statement with a single entry at the 6623 // top and a single exit at the bottom. 6624 // The point of exit cannot be a branch out of the structured block. 6625 // longjmp() and throw() must not violate the entry/exit criteria. 6626 CS->getCapturedDecl()->setNothrow(); 6627 } 6628 6629 setFunctionHasBranchProtectedScope(); 6630 6631 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, 6632 AStmt); 6633 } 6634 6635 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 6636 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6637 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6638 if (!AStmt) 6639 return StmtError(); 6640 6641 auto *CS = cast<CapturedStmt>(AStmt); 6642 // 1.2.2 OpenMP Language Terminology 6643 // Structured block - An executable statement with a single entry at the 6644 // top and a single exit at the bottom. 6645 // The point of exit cannot be a branch out of the structured block. 6646 // longjmp() and throw() must not violate the entry/exit criteria. 6647 CS->getCapturedDecl()->setNothrow(); 6648 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 6649 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6650 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6651 // 1.2.2 OpenMP Language Terminology 6652 // Structured block - An executable statement with a single entry at the 6653 // top and a single exit at the bottom. 6654 // The point of exit cannot be a branch out of the structured block. 6655 // longjmp() and throw() must not violate the entry/exit criteria. 6656 CS->getCapturedDecl()->setNothrow(); 6657 } 6658 6659 OMPLoopDirective::HelperExprs B; 6660 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6661 // define the nested loops number. 6662 unsigned NestedLoopCount = 6663 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 6664 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 6665 VarsWithImplicitDSA, B); 6666 if (NestedLoopCount == 0) 6667 return StmtError(); 6668 6669 assert((CurContext->isDependentContext() || B.builtAll()) && 6670 "omp target parallel for loop exprs were not built"); 6671 6672 if (!CurContext->isDependentContext()) { 6673 // Finalize the clauses that need pre-built expressions for CodeGen. 6674 for (OMPClause *C : Clauses) { 6675 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6676 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6677 B.NumIterations, *this, CurScope, 6678 DSAStack)) 6679 return StmtError(); 6680 } 6681 } 6682 6683 setFunctionHasBranchProtectedScope(); 6684 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc, 6685 NestedLoopCount, Clauses, AStmt, 6686 B, DSAStack->isCancelRegion()); 6687 } 6688 6689 /// Check for existence of a map clause in the list of clauses. 6690 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 6691 const OpenMPClauseKind K) { 6692 return llvm::any_of( 6693 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 6694 } 6695 6696 template <typename... Params> 6697 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 6698 const Params... ClauseTypes) { 6699 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 6700 } 6701 6702 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 6703 Stmt *AStmt, 6704 SourceLocation StartLoc, 6705 SourceLocation EndLoc) { 6706 if (!AStmt) 6707 return StmtError(); 6708 6709 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6710 6711 // OpenMP [2.10.1, Restrictions, p. 97] 6712 // At least one map clause must appear on the directive. 6713 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) { 6714 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 6715 << "'map' or 'use_device_ptr'" 6716 << getOpenMPDirectiveName(OMPD_target_data); 6717 return StmtError(); 6718 } 6719 6720 setFunctionHasBranchProtectedScope(); 6721 6722 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 6723 AStmt); 6724 } 6725 6726 StmtResult 6727 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 6728 SourceLocation StartLoc, 6729 SourceLocation EndLoc, Stmt *AStmt) { 6730 if (!AStmt) 6731 return StmtError(); 6732 6733 auto *CS = cast<CapturedStmt>(AStmt); 6734 // 1.2.2 OpenMP Language Terminology 6735 // Structured block - An executable statement with a single entry at the 6736 // top and a single exit at the bottom. 6737 // The point of exit cannot be a branch out of the structured block. 6738 // longjmp() and throw() must not violate the entry/exit criteria. 6739 CS->getCapturedDecl()->setNothrow(); 6740 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 6741 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6742 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6743 // 1.2.2 OpenMP Language Terminology 6744 // Structured block - An executable statement with a single entry at the 6745 // top and a single exit at the bottom. 6746 // The point of exit cannot be a branch out of the structured block. 6747 // longjmp() and throw() must not violate the entry/exit criteria. 6748 CS->getCapturedDecl()->setNothrow(); 6749 } 6750 6751 // OpenMP [2.10.2, Restrictions, p. 99] 6752 // At least one map clause must appear on the directive. 6753 if (!hasClauses(Clauses, OMPC_map)) { 6754 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 6755 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 6756 return StmtError(); 6757 } 6758 6759 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 6760 AStmt); 6761 } 6762 6763 StmtResult 6764 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 6765 SourceLocation StartLoc, 6766 SourceLocation EndLoc, Stmt *AStmt) { 6767 if (!AStmt) 6768 return StmtError(); 6769 6770 auto *CS = cast<CapturedStmt>(AStmt); 6771 // 1.2.2 OpenMP Language Terminology 6772 // Structured block - An executable statement with a single entry at the 6773 // top and a single exit at the bottom. 6774 // The point of exit cannot be a branch out of the structured block. 6775 // longjmp() and throw() must not violate the entry/exit criteria. 6776 CS->getCapturedDecl()->setNothrow(); 6777 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 6778 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6779 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6780 // 1.2.2 OpenMP Language Terminology 6781 // Structured block - An executable statement with a single entry at the 6782 // top and a single exit at the bottom. 6783 // The point of exit cannot be a branch out of the structured block. 6784 // longjmp() and throw() must not violate the entry/exit criteria. 6785 CS->getCapturedDecl()->setNothrow(); 6786 } 6787 6788 // OpenMP [2.10.3, Restrictions, p. 102] 6789 // At least one map clause must appear on the directive. 6790 if (!hasClauses(Clauses, OMPC_map)) { 6791 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 6792 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 6793 return StmtError(); 6794 } 6795 6796 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 6797 AStmt); 6798 } 6799 6800 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 6801 SourceLocation StartLoc, 6802 SourceLocation EndLoc, 6803 Stmt *AStmt) { 6804 if (!AStmt) 6805 return StmtError(); 6806 6807 auto *CS = cast<CapturedStmt>(AStmt); 6808 // 1.2.2 OpenMP Language Terminology 6809 // Structured block - An executable statement with a single entry at the 6810 // top and a single exit at the bottom. 6811 // The point of exit cannot be a branch out of the structured block. 6812 // longjmp() and throw() must not violate the entry/exit criteria. 6813 CS->getCapturedDecl()->setNothrow(); 6814 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 6815 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6816 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6817 // 1.2.2 OpenMP Language Terminology 6818 // Structured block - An executable statement with a single entry at the 6819 // top and a single exit at the bottom. 6820 // The point of exit cannot be a branch out of the structured block. 6821 // longjmp() and throw() must not violate the entry/exit criteria. 6822 CS->getCapturedDecl()->setNothrow(); 6823 } 6824 6825 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 6826 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 6827 return StmtError(); 6828 } 6829 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 6830 AStmt); 6831 } 6832 6833 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 6834 Stmt *AStmt, SourceLocation StartLoc, 6835 SourceLocation EndLoc) { 6836 if (!AStmt) 6837 return StmtError(); 6838 6839 auto *CS = cast<CapturedStmt>(AStmt); 6840 // 1.2.2 OpenMP Language Terminology 6841 // Structured block - An executable statement with a single entry at the 6842 // top and a single exit at the bottom. 6843 // The point of exit cannot be a branch out of the structured block. 6844 // longjmp() and throw() must not violate the entry/exit criteria. 6845 CS->getCapturedDecl()->setNothrow(); 6846 6847 setFunctionHasBranchProtectedScope(); 6848 6849 DSAStack->setParentTeamsRegionLoc(StartLoc); 6850 6851 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 6852 } 6853 6854 StmtResult 6855 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 6856 SourceLocation EndLoc, 6857 OpenMPDirectiveKind CancelRegion) { 6858 if (DSAStack->isParentNowaitRegion()) { 6859 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 6860 return StmtError(); 6861 } 6862 if (DSAStack->isParentOrderedRegion()) { 6863 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 6864 return StmtError(); 6865 } 6866 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 6867 CancelRegion); 6868 } 6869 6870 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 6871 SourceLocation StartLoc, 6872 SourceLocation EndLoc, 6873 OpenMPDirectiveKind CancelRegion) { 6874 if (DSAStack->isParentNowaitRegion()) { 6875 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 6876 return StmtError(); 6877 } 6878 if (DSAStack->isParentOrderedRegion()) { 6879 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 6880 return StmtError(); 6881 } 6882 DSAStack->setParentCancelRegion(/*Cancel=*/true); 6883 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 6884 CancelRegion); 6885 } 6886 6887 static bool checkGrainsizeNumTasksClauses(Sema &S, 6888 ArrayRef<OMPClause *> Clauses) { 6889 const OMPClause *PrevClause = nullptr; 6890 bool ErrorFound = false; 6891 for (const OMPClause *C : Clauses) { 6892 if (C->getClauseKind() == OMPC_grainsize || 6893 C->getClauseKind() == OMPC_num_tasks) { 6894 if (!PrevClause) 6895 PrevClause = C; 6896 else if (PrevClause->getClauseKind() != C->getClauseKind()) { 6897 S.Diag(C->getLocStart(), 6898 diag::err_omp_grainsize_num_tasks_mutually_exclusive) 6899 << getOpenMPClauseName(C->getClauseKind()) 6900 << getOpenMPClauseName(PrevClause->getClauseKind()); 6901 S.Diag(PrevClause->getLocStart(), 6902 diag::note_omp_previous_grainsize_num_tasks) 6903 << getOpenMPClauseName(PrevClause->getClauseKind()); 6904 ErrorFound = true; 6905 } 6906 } 6907 } 6908 return ErrorFound; 6909 } 6910 6911 static bool checkReductionClauseWithNogroup(Sema &S, 6912 ArrayRef<OMPClause *> Clauses) { 6913 const OMPClause *ReductionClause = nullptr; 6914 const OMPClause *NogroupClause = nullptr; 6915 for (const OMPClause *C : Clauses) { 6916 if (C->getClauseKind() == OMPC_reduction) { 6917 ReductionClause = C; 6918 if (NogroupClause) 6919 break; 6920 continue; 6921 } 6922 if (C->getClauseKind() == OMPC_nogroup) { 6923 NogroupClause = C; 6924 if (ReductionClause) 6925 break; 6926 continue; 6927 } 6928 } 6929 if (ReductionClause && NogroupClause) { 6930 S.Diag(ReductionClause->getLocStart(), diag::err_omp_reduction_with_nogroup) 6931 << SourceRange(NogroupClause->getLocStart(), 6932 NogroupClause->getLocEnd()); 6933 return true; 6934 } 6935 return false; 6936 } 6937 6938 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 6939 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6940 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6941 if (!AStmt) 6942 return StmtError(); 6943 6944 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6945 OMPLoopDirective::HelperExprs B; 6946 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6947 // define the nested loops number. 6948 unsigned NestedLoopCount = 6949 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 6950 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 6951 VarsWithImplicitDSA, B); 6952 if (NestedLoopCount == 0) 6953 return StmtError(); 6954 6955 assert((CurContext->isDependentContext() || B.builtAll()) && 6956 "omp for loop exprs were not built"); 6957 6958 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 6959 // The grainsize clause and num_tasks clause are mutually exclusive and may 6960 // not appear on the same taskloop directive. 6961 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 6962 return StmtError(); 6963 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 6964 // If a reduction clause is present on the taskloop directive, the nogroup 6965 // clause must not be specified. 6966 if (checkReductionClauseWithNogroup(*this, Clauses)) 6967 return StmtError(); 6968 6969 setFunctionHasBranchProtectedScope(); 6970 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 6971 NestedLoopCount, Clauses, AStmt, B); 6972 } 6973 6974 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 6975 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6976 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6977 if (!AStmt) 6978 return StmtError(); 6979 6980 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6981 OMPLoopDirective::HelperExprs B; 6982 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6983 // define the nested loops number. 6984 unsigned NestedLoopCount = 6985 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 6986 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 6987 VarsWithImplicitDSA, B); 6988 if (NestedLoopCount == 0) 6989 return StmtError(); 6990 6991 assert((CurContext->isDependentContext() || B.builtAll()) && 6992 "omp for loop exprs were not built"); 6993 6994 if (!CurContext->isDependentContext()) { 6995 // Finalize the clauses that need pre-built expressions for CodeGen. 6996 for (OMPClause *C : Clauses) { 6997 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6998 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6999 B.NumIterations, *this, CurScope, 7000 DSAStack)) 7001 return StmtError(); 7002 } 7003 } 7004 7005 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 7006 // The grainsize clause and num_tasks clause are mutually exclusive and may 7007 // not appear on the same taskloop directive. 7008 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 7009 return StmtError(); 7010 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 7011 // If a reduction clause is present on the taskloop directive, the nogroup 7012 // clause must not be specified. 7013 if (checkReductionClauseWithNogroup(*this, Clauses)) 7014 return StmtError(); 7015 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7016 return StmtError(); 7017 7018 setFunctionHasBranchProtectedScope(); 7019 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 7020 NestedLoopCount, Clauses, AStmt, B); 7021 } 7022 7023 StmtResult Sema::ActOnOpenMPDistributeDirective( 7024 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7025 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7026 if (!AStmt) 7027 return StmtError(); 7028 7029 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 7030 OMPLoopDirective::HelperExprs B; 7031 // In presence of clause 'collapse' with number of loops, it will 7032 // define the nested loops number. 7033 unsigned NestedLoopCount = 7034 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 7035 nullptr /*ordered not a clause on distribute*/, AStmt, 7036 *this, *DSAStack, VarsWithImplicitDSA, B); 7037 if (NestedLoopCount == 0) 7038 return StmtError(); 7039 7040 assert((CurContext->isDependentContext() || B.builtAll()) && 7041 "omp for loop exprs were not built"); 7042 7043 setFunctionHasBranchProtectedScope(); 7044 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 7045 NestedLoopCount, Clauses, AStmt, B); 7046 } 7047 7048 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 7049 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7050 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7051 if (!AStmt) 7052 return StmtError(); 7053 7054 auto *CS = cast<CapturedStmt>(AStmt); 7055 // 1.2.2 OpenMP Language Terminology 7056 // Structured block - An executable statement with a single entry at the 7057 // top and a single exit at the bottom. 7058 // The point of exit cannot be a branch out of the structured block. 7059 // longjmp() and throw() must not violate the entry/exit criteria. 7060 CS->getCapturedDecl()->setNothrow(); 7061 for (int ThisCaptureLevel = 7062 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 7063 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7064 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7065 // 1.2.2 OpenMP Language Terminology 7066 // Structured block - An executable statement with a single entry at the 7067 // top and a single exit at the bottom. 7068 // The point of exit cannot be a branch out of the structured block. 7069 // longjmp() and throw() must not violate the entry/exit criteria. 7070 CS->getCapturedDecl()->setNothrow(); 7071 } 7072 7073 OMPLoopDirective::HelperExprs B; 7074 // In presence of clause 'collapse' with number of loops, it will 7075 // define the nested loops number. 7076 unsigned NestedLoopCount = checkOpenMPLoop( 7077 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 7078 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7079 VarsWithImplicitDSA, B); 7080 if (NestedLoopCount == 0) 7081 return StmtError(); 7082 7083 assert((CurContext->isDependentContext() || B.builtAll()) && 7084 "omp for loop exprs were not built"); 7085 7086 setFunctionHasBranchProtectedScope(); 7087 return OMPDistributeParallelForDirective::Create( 7088 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 7089 DSAStack->isCancelRegion()); 7090 } 7091 7092 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 7093 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7094 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7095 if (!AStmt) 7096 return StmtError(); 7097 7098 auto *CS = cast<CapturedStmt>(AStmt); 7099 // 1.2.2 OpenMP Language Terminology 7100 // Structured block - An executable statement with a single entry at the 7101 // top and a single exit at the bottom. 7102 // The point of exit cannot be a branch out of the structured block. 7103 // longjmp() and throw() must not violate the entry/exit criteria. 7104 CS->getCapturedDecl()->setNothrow(); 7105 for (int ThisCaptureLevel = 7106 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 7107 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7108 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7109 // 1.2.2 OpenMP Language Terminology 7110 // Structured block - An executable statement with a single entry at the 7111 // top and a single exit at the bottom. 7112 // The point of exit cannot be a branch out of the structured block. 7113 // longjmp() and throw() must not violate the entry/exit criteria. 7114 CS->getCapturedDecl()->setNothrow(); 7115 } 7116 7117 OMPLoopDirective::HelperExprs B; 7118 // In presence of clause 'collapse' with number of loops, it will 7119 // define the nested loops number. 7120 unsigned NestedLoopCount = checkOpenMPLoop( 7121 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 7122 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7123 VarsWithImplicitDSA, B); 7124 if (NestedLoopCount == 0) 7125 return StmtError(); 7126 7127 assert((CurContext->isDependentContext() || B.builtAll()) && 7128 "omp for loop exprs were not built"); 7129 7130 if (!CurContext->isDependentContext()) { 7131 // Finalize the clauses that need pre-built expressions for CodeGen. 7132 for (OMPClause *C : Clauses) { 7133 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7134 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7135 B.NumIterations, *this, CurScope, 7136 DSAStack)) 7137 return StmtError(); 7138 } 7139 } 7140 7141 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7142 return StmtError(); 7143 7144 setFunctionHasBranchProtectedScope(); 7145 return OMPDistributeParallelForSimdDirective::Create( 7146 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7147 } 7148 7149 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 7150 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7151 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7152 if (!AStmt) 7153 return StmtError(); 7154 7155 auto *CS = cast<CapturedStmt>(AStmt); 7156 // 1.2.2 OpenMP Language Terminology 7157 // Structured block - An executable statement with a single entry at the 7158 // top and a single exit at the bottom. 7159 // The point of exit cannot be a branch out of the structured block. 7160 // longjmp() and throw() must not violate the entry/exit criteria. 7161 CS->getCapturedDecl()->setNothrow(); 7162 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 7163 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7164 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7165 // 1.2.2 OpenMP Language Terminology 7166 // Structured block - An executable statement with a single entry at the 7167 // top and a single exit at the bottom. 7168 // The point of exit cannot be a branch out of the structured block. 7169 // longjmp() and throw() must not violate the entry/exit criteria. 7170 CS->getCapturedDecl()->setNothrow(); 7171 } 7172 7173 OMPLoopDirective::HelperExprs B; 7174 // In presence of clause 'collapse' with number of loops, it will 7175 // define the nested loops number. 7176 unsigned NestedLoopCount = 7177 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 7178 nullptr /*ordered not a clause on distribute*/, CS, *this, 7179 *DSAStack, VarsWithImplicitDSA, B); 7180 if (NestedLoopCount == 0) 7181 return StmtError(); 7182 7183 assert((CurContext->isDependentContext() || B.builtAll()) && 7184 "omp for loop exprs were not built"); 7185 7186 if (!CurContext->isDependentContext()) { 7187 // Finalize the clauses that need pre-built expressions for CodeGen. 7188 for (OMPClause *C : Clauses) { 7189 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7190 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7191 B.NumIterations, *this, CurScope, 7192 DSAStack)) 7193 return StmtError(); 7194 } 7195 } 7196 7197 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7198 return StmtError(); 7199 7200 setFunctionHasBranchProtectedScope(); 7201 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 7202 NestedLoopCount, Clauses, AStmt, B); 7203 } 7204 7205 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 7206 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7207 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7208 if (!AStmt) 7209 return StmtError(); 7210 7211 auto *CS = cast<CapturedStmt>(AStmt); 7212 // 1.2.2 OpenMP Language Terminology 7213 // Structured block - An executable statement with a single entry at the 7214 // top and a single exit at the bottom. 7215 // The point of exit cannot be a branch out of the structured block. 7216 // longjmp() and throw() must not violate the entry/exit criteria. 7217 CS->getCapturedDecl()->setNothrow(); 7218 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 7219 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7220 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7221 // 1.2.2 OpenMP Language Terminology 7222 // Structured block - An executable statement with a single entry at the 7223 // top and a single exit at the bottom. 7224 // The point of exit cannot be a branch out of the structured block. 7225 // longjmp() and throw() must not violate the entry/exit criteria. 7226 CS->getCapturedDecl()->setNothrow(); 7227 } 7228 7229 OMPLoopDirective::HelperExprs B; 7230 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 7231 // define the nested loops number. 7232 unsigned NestedLoopCount = checkOpenMPLoop( 7233 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 7234 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 7235 VarsWithImplicitDSA, B); 7236 if (NestedLoopCount == 0) 7237 return StmtError(); 7238 7239 assert((CurContext->isDependentContext() || B.builtAll()) && 7240 "omp target parallel for simd loop exprs were not built"); 7241 7242 if (!CurContext->isDependentContext()) { 7243 // Finalize the clauses that need pre-built expressions for CodeGen. 7244 for (OMPClause *C : Clauses) { 7245 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7246 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7247 B.NumIterations, *this, CurScope, 7248 DSAStack)) 7249 return StmtError(); 7250 } 7251 } 7252 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7253 return StmtError(); 7254 7255 setFunctionHasBranchProtectedScope(); 7256 return OMPTargetParallelForSimdDirective::Create( 7257 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7258 } 7259 7260 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 7261 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7262 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7263 if (!AStmt) 7264 return StmtError(); 7265 7266 auto *CS = cast<CapturedStmt>(AStmt); 7267 // 1.2.2 OpenMP Language Terminology 7268 // Structured block - An executable statement with a single entry at the 7269 // top and a single exit at the bottom. 7270 // The point of exit cannot be a branch out of the structured block. 7271 // longjmp() and throw() must not violate the entry/exit criteria. 7272 CS->getCapturedDecl()->setNothrow(); 7273 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 7274 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7275 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7276 // 1.2.2 OpenMP Language Terminology 7277 // Structured block - An executable statement with a single entry at the 7278 // top and a single exit at the bottom. 7279 // The point of exit cannot be a branch out of the structured block. 7280 // longjmp() and throw() must not violate the entry/exit criteria. 7281 CS->getCapturedDecl()->setNothrow(); 7282 } 7283 7284 OMPLoopDirective::HelperExprs B; 7285 // In presence of clause 'collapse' with number of loops, it will define the 7286 // nested loops number. 7287 unsigned NestedLoopCount = 7288 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 7289 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 7290 VarsWithImplicitDSA, B); 7291 if (NestedLoopCount == 0) 7292 return StmtError(); 7293 7294 assert((CurContext->isDependentContext() || B.builtAll()) && 7295 "omp target simd loop exprs were not built"); 7296 7297 if (!CurContext->isDependentContext()) { 7298 // Finalize the clauses that need pre-built expressions for CodeGen. 7299 for (OMPClause *C : Clauses) { 7300 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7301 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7302 B.NumIterations, *this, CurScope, 7303 DSAStack)) 7304 return StmtError(); 7305 } 7306 } 7307 7308 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7309 return StmtError(); 7310 7311 setFunctionHasBranchProtectedScope(); 7312 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 7313 NestedLoopCount, Clauses, AStmt, B); 7314 } 7315 7316 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 7317 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7318 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7319 if (!AStmt) 7320 return StmtError(); 7321 7322 auto *CS = cast<CapturedStmt>(AStmt); 7323 // 1.2.2 OpenMP Language Terminology 7324 // Structured block - An executable statement with a single entry at the 7325 // top and a single exit at the bottom. 7326 // The point of exit cannot be a branch out of the structured block. 7327 // longjmp() and throw() must not violate the entry/exit criteria. 7328 CS->getCapturedDecl()->setNothrow(); 7329 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 7330 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7331 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7332 // 1.2.2 OpenMP Language Terminology 7333 // Structured block - An executable statement with a single entry at the 7334 // top and a single exit at the bottom. 7335 // The point of exit cannot be a branch out of the structured block. 7336 // longjmp() and throw() must not violate the entry/exit criteria. 7337 CS->getCapturedDecl()->setNothrow(); 7338 } 7339 7340 OMPLoopDirective::HelperExprs B; 7341 // In presence of clause 'collapse' with number of loops, it will 7342 // define the nested loops number. 7343 unsigned NestedLoopCount = 7344 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 7345 nullptr /*ordered not a clause on distribute*/, CS, *this, 7346 *DSAStack, VarsWithImplicitDSA, B); 7347 if (NestedLoopCount == 0) 7348 return StmtError(); 7349 7350 assert((CurContext->isDependentContext() || B.builtAll()) && 7351 "omp teams distribute loop exprs were not built"); 7352 7353 setFunctionHasBranchProtectedScope(); 7354 7355 DSAStack->setParentTeamsRegionLoc(StartLoc); 7356 7357 return OMPTeamsDistributeDirective::Create( 7358 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7359 } 7360 7361 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 7362 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7363 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7364 if (!AStmt) 7365 return StmtError(); 7366 7367 auto *CS = cast<CapturedStmt>(AStmt); 7368 // 1.2.2 OpenMP Language Terminology 7369 // Structured block - An executable statement with a single entry at the 7370 // top and a single exit at the bottom. 7371 // The point of exit cannot be a branch out of the structured block. 7372 // longjmp() and throw() must not violate the entry/exit criteria. 7373 CS->getCapturedDecl()->setNothrow(); 7374 for (int ThisCaptureLevel = 7375 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 7376 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7377 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7378 // 1.2.2 OpenMP Language Terminology 7379 // Structured block - An executable statement with a single entry at the 7380 // top and a single exit at the bottom. 7381 // The point of exit cannot be a branch out of the structured block. 7382 // longjmp() and throw() must not violate the entry/exit criteria. 7383 CS->getCapturedDecl()->setNothrow(); 7384 } 7385 7386 7387 OMPLoopDirective::HelperExprs B; 7388 // In presence of clause 'collapse' with number of loops, it will 7389 // define the nested loops number. 7390 unsigned NestedLoopCount = checkOpenMPLoop( 7391 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 7392 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7393 VarsWithImplicitDSA, B); 7394 7395 if (NestedLoopCount == 0) 7396 return StmtError(); 7397 7398 assert((CurContext->isDependentContext() || B.builtAll()) && 7399 "omp teams distribute simd loop exprs were not built"); 7400 7401 if (!CurContext->isDependentContext()) { 7402 // Finalize the clauses that need pre-built expressions for CodeGen. 7403 for (OMPClause *C : Clauses) { 7404 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7405 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7406 B.NumIterations, *this, CurScope, 7407 DSAStack)) 7408 return StmtError(); 7409 } 7410 } 7411 7412 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7413 return StmtError(); 7414 7415 setFunctionHasBranchProtectedScope(); 7416 7417 DSAStack->setParentTeamsRegionLoc(StartLoc); 7418 7419 return OMPTeamsDistributeSimdDirective::Create( 7420 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7421 } 7422 7423 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 7424 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7425 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7426 if (!AStmt) 7427 return StmtError(); 7428 7429 auto *CS = cast<CapturedStmt>(AStmt); 7430 // 1.2.2 OpenMP Language Terminology 7431 // Structured block - An executable statement with a single entry at the 7432 // top and a single exit at the bottom. 7433 // The point of exit cannot be a branch out of the structured block. 7434 // longjmp() and throw() must not violate the entry/exit criteria. 7435 CS->getCapturedDecl()->setNothrow(); 7436 7437 for (int ThisCaptureLevel = 7438 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 7439 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7440 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7441 // 1.2.2 OpenMP Language Terminology 7442 // Structured block - An executable statement with a single entry at the 7443 // top and a single exit at the bottom. 7444 // The point of exit cannot be a branch out of the structured block. 7445 // longjmp() and throw() must not violate the entry/exit criteria. 7446 CS->getCapturedDecl()->setNothrow(); 7447 } 7448 7449 OMPLoopDirective::HelperExprs B; 7450 // In presence of clause 'collapse' with number of loops, it will 7451 // define the nested loops number. 7452 unsigned NestedLoopCount = checkOpenMPLoop( 7453 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 7454 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7455 VarsWithImplicitDSA, B); 7456 7457 if (NestedLoopCount == 0) 7458 return StmtError(); 7459 7460 assert((CurContext->isDependentContext() || B.builtAll()) && 7461 "omp for loop exprs were not built"); 7462 7463 if (!CurContext->isDependentContext()) { 7464 // Finalize the clauses that need pre-built expressions for CodeGen. 7465 for (OMPClause *C : Clauses) { 7466 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7467 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7468 B.NumIterations, *this, CurScope, 7469 DSAStack)) 7470 return StmtError(); 7471 } 7472 } 7473 7474 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7475 return StmtError(); 7476 7477 setFunctionHasBranchProtectedScope(); 7478 7479 DSAStack->setParentTeamsRegionLoc(StartLoc); 7480 7481 return OMPTeamsDistributeParallelForSimdDirective::Create( 7482 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7483 } 7484 7485 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 7486 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7487 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7488 if (!AStmt) 7489 return StmtError(); 7490 7491 auto *CS = cast<CapturedStmt>(AStmt); 7492 // 1.2.2 OpenMP Language Terminology 7493 // Structured block - An executable statement with a single entry at the 7494 // top and a single exit at the bottom. 7495 // The point of exit cannot be a branch out of the structured block. 7496 // longjmp() and throw() must not violate the entry/exit criteria. 7497 CS->getCapturedDecl()->setNothrow(); 7498 7499 for (int ThisCaptureLevel = 7500 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 7501 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7502 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7503 // 1.2.2 OpenMP Language Terminology 7504 // Structured block - An executable statement with a single entry at the 7505 // top and a single exit at the bottom. 7506 // The point of exit cannot be a branch out of the structured block. 7507 // longjmp() and throw() must not violate the entry/exit criteria. 7508 CS->getCapturedDecl()->setNothrow(); 7509 } 7510 7511 OMPLoopDirective::HelperExprs B; 7512 // In presence of clause 'collapse' with number of loops, it will 7513 // define the nested loops number. 7514 unsigned NestedLoopCount = checkOpenMPLoop( 7515 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 7516 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7517 VarsWithImplicitDSA, B); 7518 7519 if (NestedLoopCount == 0) 7520 return StmtError(); 7521 7522 assert((CurContext->isDependentContext() || B.builtAll()) && 7523 "omp for loop exprs were not built"); 7524 7525 setFunctionHasBranchProtectedScope(); 7526 7527 DSAStack->setParentTeamsRegionLoc(StartLoc); 7528 7529 return OMPTeamsDistributeParallelForDirective::Create( 7530 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 7531 DSAStack->isCancelRegion()); 7532 } 7533 7534 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 7535 Stmt *AStmt, 7536 SourceLocation StartLoc, 7537 SourceLocation EndLoc) { 7538 if (!AStmt) 7539 return StmtError(); 7540 7541 auto *CS = cast<CapturedStmt>(AStmt); 7542 // 1.2.2 OpenMP Language Terminology 7543 // Structured block - An executable statement with a single entry at the 7544 // top and a single exit at the bottom. 7545 // The point of exit cannot be a branch out of the structured block. 7546 // longjmp() and throw() must not violate the entry/exit criteria. 7547 CS->getCapturedDecl()->setNothrow(); 7548 7549 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 7550 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7551 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7552 // 1.2.2 OpenMP Language Terminology 7553 // Structured block - An executable statement with a single entry at the 7554 // top and a single exit at the bottom. 7555 // The point of exit cannot be a branch out of the structured block. 7556 // longjmp() and throw() must not violate the entry/exit criteria. 7557 CS->getCapturedDecl()->setNothrow(); 7558 } 7559 setFunctionHasBranchProtectedScope(); 7560 7561 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 7562 AStmt); 7563 } 7564 7565 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 7566 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7567 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7568 if (!AStmt) 7569 return StmtError(); 7570 7571 auto *CS = cast<CapturedStmt>(AStmt); 7572 // 1.2.2 OpenMP Language Terminology 7573 // Structured block - An executable statement with a single entry at the 7574 // top and a single exit at the bottom. 7575 // The point of exit cannot be a branch out of the structured block. 7576 // longjmp() and throw() must not violate the entry/exit criteria. 7577 CS->getCapturedDecl()->setNothrow(); 7578 for (int ThisCaptureLevel = 7579 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 7580 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7581 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7582 // 1.2.2 OpenMP Language Terminology 7583 // Structured block - An executable statement with a single entry at the 7584 // top and a single exit at the bottom. 7585 // The point of exit cannot be a branch out of the structured block. 7586 // longjmp() and throw() must not violate the entry/exit criteria. 7587 CS->getCapturedDecl()->setNothrow(); 7588 } 7589 7590 OMPLoopDirective::HelperExprs B; 7591 // In presence of clause 'collapse' with number of loops, it will 7592 // define the nested loops number. 7593 unsigned NestedLoopCount = checkOpenMPLoop( 7594 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 7595 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7596 VarsWithImplicitDSA, B); 7597 if (NestedLoopCount == 0) 7598 return StmtError(); 7599 7600 assert((CurContext->isDependentContext() || B.builtAll()) && 7601 "omp target teams distribute loop exprs were not built"); 7602 7603 setFunctionHasBranchProtectedScope(); 7604 return OMPTargetTeamsDistributeDirective::Create( 7605 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7606 } 7607 7608 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 7609 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7610 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7611 if (!AStmt) 7612 return StmtError(); 7613 7614 auto *CS = cast<CapturedStmt>(AStmt); 7615 // 1.2.2 OpenMP Language Terminology 7616 // Structured block - An executable statement with a single entry at the 7617 // top and a single exit at the bottom. 7618 // The point of exit cannot be a branch out of the structured block. 7619 // longjmp() and throw() must not violate the entry/exit criteria. 7620 CS->getCapturedDecl()->setNothrow(); 7621 for (int ThisCaptureLevel = 7622 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 7623 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7624 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7625 // 1.2.2 OpenMP Language Terminology 7626 // Structured block - An executable statement with a single entry at the 7627 // top and a single exit at the bottom. 7628 // The point of exit cannot be a branch out of the structured block. 7629 // longjmp() and throw() must not violate the entry/exit criteria. 7630 CS->getCapturedDecl()->setNothrow(); 7631 } 7632 7633 OMPLoopDirective::HelperExprs B; 7634 // In presence of clause 'collapse' with number of loops, it will 7635 // define the nested loops number. 7636 unsigned NestedLoopCount = checkOpenMPLoop( 7637 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 7638 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7639 VarsWithImplicitDSA, B); 7640 if (NestedLoopCount == 0) 7641 return StmtError(); 7642 7643 assert((CurContext->isDependentContext() || B.builtAll()) && 7644 "omp target teams distribute parallel for loop exprs were not built"); 7645 7646 if (!CurContext->isDependentContext()) { 7647 // Finalize the clauses that need pre-built expressions for CodeGen. 7648 for (OMPClause *C : Clauses) { 7649 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7650 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7651 B.NumIterations, *this, CurScope, 7652 DSAStack)) 7653 return StmtError(); 7654 } 7655 } 7656 7657 setFunctionHasBranchProtectedScope(); 7658 return OMPTargetTeamsDistributeParallelForDirective::Create( 7659 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 7660 DSAStack->isCancelRegion()); 7661 } 7662 7663 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 7664 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7665 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7666 if (!AStmt) 7667 return StmtError(); 7668 7669 auto *CS = cast<CapturedStmt>(AStmt); 7670 // 1.2.2 OpenMP Language Terminology 7671 // Structured block - An executable statement with a single entry at the 7672 // top and a single exit at the bottom. 7673 // The point of exit cannot be a branch out of the structured block. 7674 // longjmp() and throw() must not violate the entry/exit criteria. 7675 CS->getCapturedDecl()->setNothrow(); 7676 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 7677 OMPD_target_teams_distribute_parallel_for_simd); 7678 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7679 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7680 // 1.2.2 OpenMP Language Terminology 7681 // Structured block - An executable statement with a single entry at the 7682 // top and a single exit at the bottom. 7683 // The point of exit cannot be a branch out of the structured block. 7684 // longjmp() and throw() must not violate the entry/exit criteria. 7685 CS->getCapturedDecl()->setNothrow(); 7686 } 7687 7688 OMPLoopDirective::HelperExprs B; 7689 // In presence of clause 'collapse' with number of loops, it will 7690 // define the nested loops number. 7691 unsigned NestedLoopCount = 7692 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 7693 getCollapseNumberExpr(Clauses), 7694 nullptr /*ordered not a clause on distribute*/, CS, *this, 7695 *DSAStack, VarsWithImplicitDSA, B); 7696 if (NestedLoopCount == 0) 7697 return StmtError(); 7698 7699 assert((CurContext->isDependentContext() || B.builtAll()) && 7700 "omp target teams distribute parallel for simd loop exprs were not " 7701 "built"); 7702 7703 if (!CurContext->isDependentContext()) { 7704 // Finalize the clauses that need pre-built expressions for CodeGen. 7705 for (OMPClause *C : Clauses) { 7706 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7707 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7708 B.NumIterations, *this, CurScope, 7709 DSAStack)) 7710 return StmtError(); 7711 } 7712 } 7713 7714 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7715 return StmtError(); 7716 7717 setFunctionHasBranchProtectedScope(); 7718 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 7719 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7720 } 7721 7722 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 7723 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7724 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7725 if (!AStmt) 7726 return StmtError(); 7727 7728 auto *CS = cast<CapturedStmt>(AStmt); 7729 // 1.2.2 OpenMP Language Terminology 7730 // Structured block - An executable statement with a single entry at the 7731 // top and a single exit at the bottom. 7732 // The point of exit cannot be a branch out of the structured block. 7733 // longjmp() and throw() must not violate the entry/exit criteria. 7734 CS->getCapturedDecl()->setNothrow(); 7735 for (int ThisCaptureLevel = 7736 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 7737 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7738 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7739 // 1.2.2 OpenMP Language Terminology 7740 // Structured block - An executable statement with a single entry at the 7741 // top and a single exit at the bottom. 7742 // The point of exit cannot be a branch out of the structured block. 7743 // longjmp() and throw() must not violate the entry/exit criteria. 7744 CS->getCapturedDecl()->setNothrow(); 7745 } 7746 7747 OMPLoopDirective::HelperExprs B; 7748 // In presence of clause 'collapse' with number of loops, it will 7749 // define the nested loops number. 7750 unsigned NestedLoopCount = checkOpenMPLoop( 7751 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 7752 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7753 VarsWithImplicitDSA, B); 7754 if (NestedLoopCount == 0) 7755 return StmtError(); 7756 7757 assert((CurContext->isDependentContext() || B.builtAll()) && 7758 "omp target teams distribute simd loop exprs were not built"); 7759 7760 if (!CurContext->isDependentContext()) { 7761 // Finalize the clauses that need pre-built expressions for CodeGen. 7762 for (OMPClause *C : Clauses) { 7763 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7764 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7765 B.NumIterations, *this, CurScope, 7766 DSAStack)) 7767 return StmtError(); 7768 } 7769 } 7770 7771 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7772 return StmtError(); 7773 7774 setFunctionHasBranchProtectedScope(); 7775 return OMPTargetTeamsDistributeSimdDirective::Create( 7776 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7777 } 7778 7779 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 7780 SourceLocation StartLoc, 7781 SourceLocation LParenLoc, 7782 SourceLocation EndLoc) { 7783 OMPClause *Res = nullptr; 7784 switch (Kind) { 7785 case OMPC_final: 7786 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 7787 break; 7788 case OMPC_num_threads: 7789 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 7790 break; 7791 case OMPC_safelen: 7792 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 7793 break; 7794 case OMPC_simdlen: 7795 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 7796 break; 7797 case OMPC_collapse: 7798 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 7799 break; 7800 case OMPC_ordered: 7801 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 7802 break; 7803 case OMPC_device: 7804 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc); 7805 break; 7806 case OMPC_num_teams: 7807 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 7808 break; 7809 case OMPC_thread_limit: 7810 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 7811 break; 7812 case OMPC_priority: 7813 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 7814 break; 7815 case OMPC_grainsize: 7816 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 7817 break; 7818 case OMPC_num_tasks: 7819 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 7820 break; 7821 case OMPC_hint: 7822 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 7823 break; 7824 case OMPC_if: 7825 case OMPC_default: 7826 case OMPC_proc_bind: 7827 case OMPC_schedule: 7828 case OMPC_private: 7829 case OMPC_firstprivate: 7830 case OMPC_lastprivate: 7831 case OMPC_shared: 7832 case OMPC_reduction: 7833 case OMPC_task_reduction: 7834 case OMPC_in_reduction: 7835 case OMPC_linear: 7836 case OMPC_aligned: 7837 case OMPC_copyin: 7838 case OMPC_copyprivate: 7839 case OMPC_nowait: 7840 case OMPC_untied: 7841 case OMPC_mergeable: 7842 case OMPC_threadprivate: 7843 case OMPC_flush: 7844 case OMPC_read: 7845 case OMPC_write: 7846 case OMPC_update: 7847 case OMPC_capture: 7848 case OMPC_seq_cst: 7849 case OMPC_depend: 7850 case OMPC_threads: 7851 case OMPC_simd: 7852 case OMPC_map: 7853 case OMPC_nogroup: 7854 case OMPC_dist_schedule: 7855 case OMPC_defaultmap: 7856 case OMPC_unknown: 7857 case OMPC_uniform: 7858 case OMPC_to: 7859 case OMPC_from: 7860 case OMPC_use_device_ptr: 7861 case OMPC_is_device_ptr: 7862 llvm_unreachable("Clause is not allowed."); 7863 } 7864 return Res; 7865 } 7866 7867 // An OpenMP directive such as 'target parallel' has two captured regions: 7868 // for the 'target' and 'parallel' respectively. This function returns 7869 // the region in which to capture expressions associated with a clause. 7870 // A return value of OMPD_unknown signifies that the expression should not 7871 // be captured. 7872 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 7873 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 7874 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 7875 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 7876 switch (CKind) { 7877 case OMPC_if: 7878 switch (DKind) { 7879 case OMPD_target_parallel: 7880 case OMPD_target_parallel_for: 7881 case OMPD_target_parallel_for_simd: 7882 // If this clause applies to the nested 'parallel' region, capture within 7883 // the 'target' region, otherwise do not capture. 7884 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 7885 CaptureRegion = OMPD_target; 7886 break; 7887 case OMPD_target_teams_distribute_parallel_for: 7888 case OMPD_target_teams_distribute_parallel_for_simd: 7889 // If this clause applies to the nested 'parallel' region, capture within 7890 // the 'teams' region, otherwise do not capture. 7891 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 7892 CaptureRegion = OMPD_teams; 7893 break; 7894 case OMPD_teams_distribute_parallel_for: 7895 case OMPD_teams_distribute_parallel_for_simd: 7896 CaptureRegion = OMPD_teams; 7897 break; 7898 case OMPD_target_update: 7899 case OMPD_target_enter_data: 7900 case OMPD_target_exit_data: 7901 CaptureRegion = OMPD_task; 7902 break; 7903 case OMPD_cancel: 7904 case OMPD_parallel: 7905 case OMPD_parallel_sections: 7906 case OMPD_parallel_for: 7907 case OMPD_parallel_for_simd: 7908 case OMPD_target: 7909 case OMPD_target_simd: 7910 case OMPD_target_teams: 7911 case OMPD_target_teams_distribute: 7912 case OMPD_target_teams_distribute_simd: 7913 case OMPD_distribute_parallel_for: 7914 case OMPD_distribute_parallel_for_simd: 7915 case OMPD_task: 7916 case OMPD_taskloop: 7917 case OMPD_taskloop_simd: 7918 case OMPD_target_data: 7919 // Do not capture if-clause expressions. 7920 break; 7921 case OMPD_threadprivate: 7922 case OMPD_taskyield: 7923 case OMPD_barrier: 7924 case OMPD_taskwait: 7925 case OMPD_cancellation_point: 7926 case OMPD_flush: 7927 case OMPD_declare_reduction: 7928 case OMPD_declare_simd: 7929 case OMPD_declare_target: 7930 case OMPD_end_declare_target: 7931 case OMPD_teams: 7932 case OMPD_simd: 7933 case OMPD_for: 7934 case OMPD_for_simd: 7935 case OMPD_sections: 7936 case OMPD_section: 7937 case OMPD_single: 7938 case OMPD_master: 7939 case OMPD_critical: 7940 case OMPD_taskgroup: 7941 case OMPD_distribute: 7942 case OMPD_ordered: 7943 case OMPD_atomic: 7944 case OMPD_distribute_simd: 7945 case OMPD_teams_distribute: 7946 case OMPD_teams_distribute_simd: 7947 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 7948 case OMPD_unknown: 7949 llvm_unreachable("Unknown OpenMP directive"); 7950 } 7951 break; 7952 case OMPC_num_threads: 7953 switch (DKind) { 7954 case OMPD_target_parallel: 7955 case OMPD_target_parallel_for: 7956 case OMPD_target_parallel_for_simd: 7957 CaptureRegion = OMPD_target; 7958 break; 7959 case OMPD_teams_distribute_parallel_for: 7960 case OMPD_teams_distribute_parallel_for_simd: 7961 case OMPD_target_teams_distribute_parallel_for: 7962 case OMPD_target_teams_distribute_parallel_for_simd: 7963 CaptureRegion = OMPD_teams; 7964 break; 7965 case OMPD_parallel: 7966 case OMPD_parallel_sections: 7967 case OMPD_parallel_for: 7968 case OMPD_parallel_for_simd: 7969 case OMPD_distribute_parallel_for: 7970 case OMPD_distribute_parallel_for_simd: 7971 // Do not capture num_threads-clause expressions. 7972 break; 7973 case OMPD_target_data: 7974 case OMPD_target_enter_data: 7975 case OMPD_target_exit_data: 7976 case OMPD_target_update: 7977 case OMPD_target: 7978 case OMPD_target_simd: 7979 case OMPD_target_teams: 7980 case OMPD_target_teams_distribute: 7981 case OMPD_target_teams_distribute_simd: 7982 case OMPD_cancel: 7983 case OMPD_task: 7984 case OMPD_taskloop: 7985 case OMPD_taskloop_simd: 7986 case OMPD_threadprivate: 7987 case OMPD_taskyield: 7988 case OMPD_barrier: 7989 case OMPD_taskwait: 7990 case OMPD_cancellation_point: 7991 case OMPD_flush: 7992 case OMPD_declare_reduction: 7993 case OMPD_declare_simd: 7994 case OMPD_declare_target: 7995 case OMPD_end_declare_target: 7996 case OMPD_teams: 7997 case OMPD_simd: 7998 case OMPD_for: 7999 case OMPD_for_simd: 8000 case OMPD_sections: 8001 case OMPD_section: 8002 case OMPD_single: 8003 case OMPD_master: 8004 case OMPD_critical: 8005 case OMPD_taskgroup: 8006 case OMPD_distribute: 8007 case OMPD_ordered: 8008 case OMPD_atomic: 8009 case OMPD_distribute_simd: 8010 case OMPD_teams_distribute: 8011 case OMPD_teams_distribute_simd: 8012 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 8013 case OMPD_unknown: 8014 llvm_unreachable("Unknown OpenMP directive"); 8015 } 8016 break; 8017 case OMPC_num_teams: 8018 switch (DKind) { 8019 case OMPD_target_teams: 8020 case OMPD_target_teams_distribute: 8021 case OMPD_target_teams_distribute_simd: 8022 case OMPD_target_teams_distribute_parallel_for: 8023 case OMPD_target_teams_distribute_parallel_for_simd: 8024 CaptureRegion = OMPD_target; 8025 break; 8026 case OMPD_teams_distribute_parallel_for: 8027 case OMPD_teams_distribute_parallel_for_simd: 8028 case OMPD_teams: 8029 case OMPD_teams_distribute: 8030 case OMPD_teams_distribute_simd: 8031 // Do not capture num_teams-clause expressions. 8032 break; 8033 case OMPD_distribute_parallel_for: 8034 case OMPD_distribute_parallel_for_simd: 8035 case OMPD_task: 8036 case OMPD_taskloop: 8037 case OMPD_taskloop_simd: 8038 case OMPD_target_data: 8039 case OMPD_target_enter_data: 8040 case OMPD_target_exit_data: 8041 case OMPD_target_update: 8042 case OMPD_cancel: 8043 case OMPD_parallel: 8044 case OMPD_parallel_sections: 8045 case OMPD_parallel_for: 8046 case OMPD_parallel_for_simd: 8047 case OMPD_target: 8048 case OMPD_target_simd: 8049 case OMPD_target_parallel: 8050 case OMPD_target_parallel_for: 8051 case OMPD_target_parallel_for_simd: 8052 case OMPD_threadprivate: 8053 case OMPD_taskyield: 8054 case OMPD_barrier: 8055 case OMPD_taskwait: 8056 case OMPD_cancellation_point: 8057 case OMPD_flush: 8058 case OMPD_declare_reduction: 8059 case OMPD_declare_simd: 8060 case OMPD_declare_target: 8061 case OMPD_end_declare_target: 8062 case OMPD_simd: 8063 case OMPD_for: 8064 case OMPD_for_simd: 8065 case OMPD_sections: 8066 case OMPD_section: 8067 case OMPD_single: 8068 case OMPD_master: 8069 case OMPD_critical: 8070 case OMPD_taskgroup: 8071 case OMPD_distribute: 8072 case OMPD_ordered: 8073 case OMPD_atomic: 8074 case OMPD_distribute_simd: 8075 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 8076 case OMPD_unknown: 8077 llvm_unreachable("Unknown OpenMP directive"); 8078 } 8079 break; 8080 case OMPC_thread_limit: 8081 switch (DKind) { 8082 case OMPD_target_teams: 8083 case OMPD_target_teams_distribute: 8084 case OMPD_target_teams_distribute_simd: 8085 case OMPD_target_teams_distribute_parallel_for: 8086 case OMPD_target_teams_distribute_parallel_for_simd: 8087 CaptureRegion = OMPD_target; 8088 break; 8089 case OMPD_teams_distribute_parallel_for: 8090 case OMPD_teams_distribute_parallel_for_simd: 8091 case OMPD_teams: 8092 case OMPD_teams_distribute: 8093 case OMPD_teams_distribute_simd: 8094 // Do not capture thread_limit-clause expressions. 8095 break; 8096 case OMPD_distribute_parallel_for: 8097 case OMPD_distribute_parallel_for_simd: 8098 case OMPD_task: 8099 case OMPD_taskloop: 8100 case OMPD_taskloop_simd: 8101 case OMPD_target_data: 8102 case OMPD_target_enter_data: 8103 case OMPD_target_exit_data: 8104 case OMPD_target_update: 8105 case OMPD_cancel: 8106 case OMPD_parallel: 8107 case OMPD_parallel_sections: 8108 case OMPD_parallel_for: 8109 case OMPD_parallel_for_simd: 8110 case OMPD_target: 8111 case OMPD_target_simd: 8112 case OMPD_target_parallel: 8113 case OMPD_target_parallel_for: 8114 case OMPD_target_parallel_for_simd: 8115 case OMPD_threadprivate: 8116 case OMPD_taskyield: 8117 case OMPD_barrier: 8118 case OMPD_taskwait: 8119 case OMPD_cancellation_point: 8120 case OMPD_flush: 8121 case OMPD_declare_reduction: 8122 case OMPD_declare_simd: 8123 case OMPD_declare_target: 8124 case OMPD_end_declare_target: 8125 case OMPD_simd: 8126 case OMPD_for: 8127 case OMPD_for_simd: 8128 case OMPD_sections: 8129 case OMPD_section: 8130 case OMPD_single: 8131 case OMPD_master: 8132 case OMPD_critical: 8133 case OMPD_taskgroup: 8134 case OMPD_distribute: 8135 case OMPD_ordered: 8136 case OMPD_atomic: 8137 case OMPD_distribute_simd: 8138 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 8139 case OMPD_unknown: 8140 llvm_unreachable("Unknown OpenMP directive"); 8141 } 8142 break; 8143 case OMPC_schedule: 8144 switch (DKind) { 8145 case OMPD_parallel_for: 8146 case OMPD_parallel_for_simd: 8147 case OMPD_distribute_parallel_for: 8148 case OMPD_distribute_parallel_for_simd: 8149 case OMPD_teams_distribute_parallel_for: 8150 case OMPD_teams_distribute_parallel_for_simd: 8151 case OMPD_target_parallel_for: 8152 case OMPD_target_parallel_for_simd: 8153 case OMPD_target_teams_distribute_parallel_for: 8154 case OMPD_target_teams_distribute_parallel_for_simd: 8155 CaptureRegion = OMPD_parallel; 8156 break; 8157 case OMPD_for: 8158 case OMPD_for_simd: 8159 // Do not capture schedule-clause expressions. 8160 break; 8161 case OMPD_task: 8162 case OMPD_taskloop: 8163 case OMPD_taskloop_simd: 8164 case OMPD_target_data: 8165 case OMPD_target_enter_data: 8166 case OMPD_target_exit_data: 8167 case OMPD_target_update: 8168 case OMPD_teams: 8169 case OMPD_teams_distribute: 8170 case OMPD_teams_distribute_simd: 8171 case OMPD_target_teams_distribute: 8172 case OMPD_target_teams_distribute_simd: 8173 case OMPD_target: 8174 case OMPD_target_simd: 8175 case OMPD_target_parallel: 8176 case OMPD_cancel: 8177 case OMPD_parallel: 8178 case OMPD_parallel_sections: 8179 case OMPD_threadprivate: 8180 case OMPD_taskyield: 8181 case OMPD_barrier: 8182 case OMPD_taskwait: 8183 case OMPD_cancellation_point: 8184 case OMPD_flush: 8185 case OMPD_declare_reduction: 8186 case OMPD_declare_simd: 8187 case OMPD_declare_target: 8188 case OMPD_end_declare_target: 8189 case OMPD_simd: 8190 case OMPD_sections: 8191 case OMPD_section: 8192 case OMPD_single: 8193 case OMPD_master: 8194 case OMPD_critical: 8195 case OMPD_taskgroup: 8196 case OMPD_distribute: 8197 case OMPD_ordered: 8198 case OMPD_atomic: 8199 case OMPD_distribute_simd: 8200 case OMPD_target_teams: 8201 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 8202 case OMPD_unknown: 8203 llvm_unreachable("Unknown OpenMP directive"); 8204 } 8205 break; 8206 case OMPC_dist_schedule: 8207 switch (DKind) { 8208 case OMPD_teams_distribute_parallel_for: 8209 case OMPD_teams_distribute_parallel_for_simd: 8210 case OMPD_teams_distribute: 8211 case OMPD_teams_distribute_simd: 8212 case OMPD_target_teams_distribute_parallel_for: 8213 case OMPD_target_teams_distribute_parallel_for_simd: 8214 case OMPD_target_teams_distribute: 8215 case OMPD_target_teams_distribute_simd: 8216 CaptureRegion = OMPD_teams; 8217 break; 8218 case OMPD_distribute_parallel_for: 8219 case OMPD_distribute_parallel_for_simd: 8220 case OMPD_distribute: 8221 case OMPD_distribute_simd: 8222 // Do not capture thread_limit-clause expressions. 8223 break; 8224 case OMPD_parallel_for: 8225 case OMPD_parallel_for_simd: 8226 case OMPD_target_parallel_for_simd: 8227 case OMPD_target_parallel_for: 8228 case OMPD_task: 8229 case OMPD_taskloop: 8230 case OMPD_taskloop_simd: 8231 case OMPD_target_data: 8232 case OMPD_target_enter_data: 8233 case OMPD_target_exit_data: 8234 case OMPD_target_update: 8235 case OMPD_teams: 8236 case OMPD_target: 8237 case OMPD_target_simd: 8238 case OMPD_target_parallel: 8239 case OMPD_cancel: 8240 case OMPD_parallel: 8241 case OMPD_parallel_sections: 8242 case OMPD_threadprivate: 8243 case OMPD_taskyield: 8244 case OMPD_barrier: 8245 case OMPD_taskwait: 8246 case OMPD_cancellation_point: 8247 case OMPD_flush: 8248 case OMPD_declare_reduction: 8249 case OMPD_declare_simd: 8250 case OMPD_declare_target: 8251 case OMPD_end_declare_target: 8252 case OMPD_simd: 8253 case OMPD_for: 8254 case OMPD_for_simd: 8255 case OMPD_sections: 8256 case OMPD_section: 8257 case OMPD_single: 8258 case OMPD_master: 8259 case OMPD_critical: 8260 case OMPD_taskgroup: 8261 case OMPD_ordered: 8262 case OMPD_atomic: 8263 case OMPD_target_teams: 8264 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 8265 case OMPD_unknown: 8266 llvm_unreachable("Unknown OpenMP directive"); 8267 } 8268 break; 8269 case OMPC_device: 8270 switch (DKind) { 8271 case OMPD_target_update: 8272 case OMPD_target_enter_data: 8273 case OMPD_target_exit_data: 8274 case OMPD_target: 8275 case OMPD_target_simd: 8276 case OMPD_target_teams: 8277 case OMPD_target_parallel: 8278 case OMPD_target_teams_distribute: 8279 case OMPD_target_teams_distribute_simd: 8280 case OMPD_target_parallel_for: 8281 case OMPD_target_parallel_for_simd: 8282 case OMPD_target_teams_distribute_parallel_for: 8283 case OMPD_target_teams_distribute_parallel_for_simd: 8284 CaptureRegion = OMPD_task; 8285 break; 8286 case OMPD_target_data: 8287 // Do not capture device-clause expressions. 8288 break; 8289 case OMPD_teams_distribute_parallel_for: 8290 case OMPD_teams_distribute_parallel_for_simd: 8291 case OMPD_teams: 8292 case OMPD_teams_distribute: 8293 case OMPD_teams_distribute_simd: 8294 case OMPD_distribute_parallel_for: 8295 case OMPD_distribute_parallel_for_simd: 8296 case OMPD_task: 8297 case OMPD_taskloop: 8298 case OMPD_taskloop_simd: 8299 case OMPD_cancel: 8300 case OMPD_parallel: 8301 case OMPD_parallel_sections: 8302 case OMPD_parallel_for: 8303 case OMPD_parallel_for_simd: 8304 case OMPD_threadprivate: 8305 case OMPD_taskyield: 8306 case OMPD_barrier: 8307 case OMPD_taskwait: 8308 case OMPD_cancellation_point: 8309 case OMPD_flush: 8310 case OMPD_declare_reduction: 8311 case OMPD_declare_simd: 8312 case OMPD_declare_target: 8313 case OMPD_end_declare_target: 8314 case OMPD_simd: 8315 case OMPD_for: 8316 case OMPD_for_simd: 8317 case OMPD_sections: 8318 case OMPD_section: 8319 case OMPD_single: 8320 case OMPD_master: 8321 case OMPD_critical: 8322 case OMPD_taskgroup: 8323 case OMPD_distribute: 8324 case OMPD_ordered: 8325 case OMPD_atomic: 8326 case OMPD_distribute_simd: 8327 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 8328 case OMPD_unknown: 8329 llvm_unreachable("Unknown OpenMP directive"); 8330 } 8331 break; 8332 case OMPC_firstprivate: 8333 case OMPC_lastprivate: 8334 case OMPC_reduction: 8335 case OMPC_task_reduction: 8336 case OMPC_in_reduction: 8337 case OMPC_linear: 8338 case OMPC_default: 8339 case OMPC_proc_bind: 8340 case OMPC_final: 8341 case OMPC_safelen: 8342 case OMPC_simdlen: 8343 case OMPC_collapse: 8344 case OMPC_private: 8345 case OMPC_shared: 8346 case OMPC_aligned: 8347 case OMPC_copyin: 8348 case OMPC_copyprivate: 8349 case OMPC_ordered: 8350 case OMPC_nowait: 8351 case OMPC_untied: 8352 case OMPC_mergeable: 8353 case OMPC_threadprivate: 8354 case OMPC_flush: 8355 case OMPC_read: 8356 case OMPC_write: 8357 case OMPC_update: 8358 case OMPC_capture: 8359 case OMPC_seq_cst: 8360 case OMPC_depend: 8361 case OMPC_threads: 8362 case OMPC_simd: 8363 case OMPC_map: 8364 case OMPC_priority: 8365 case OMPC_grainsize: 8366 case OMPC_nogroup: 8367 case OMPC_num_tasks: 8368 case OMPC_hint: 8369 case OMPC_defaultmap: 8370 case OMPC_unknown: 8371 case OMPC_uniform: 8372 case OMPC_to: 8373 case OMPC_from: 8374 case OMPC_use_device_ptr: 8375 case OMPC_is_device_ptr: 8376 llvm_unreachable("Unexpected OpenMP clause."); 8377 } 8378 return CaptureRegion; 8379 } 8380 8381 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 8382 Expr *Condition, SourceLocation StartLoc, 8383 SourceLocation LParenLoc, 8384 SourceLocation NameModifierLoc, 8385 SourceLocation ColonLoc, 8386 SourceLocation EndLoc) { 8387 Expr *ValExpr = Condition; 8388 Stmt *HelperValStmt = nullptr; 8389 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 8390 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 8391 !Condition->isInstantiationDependent() && 8392 !Condition->containsUnexpandedParameterPack()) { 8393 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 8394 if (Val.isInvalid()) 8395 return nullptr; 8396 8397 ValExpr = Val.get(); 8398 8399 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 8400 CaptureRegion = 8401 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier); 8402 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 8403 ValExpr = MakeFullExpr(ValExpr).get(); 8404 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 8405 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 8406 HelperValStmt = buildPreInits(Context, Captures); 8407 } 8408 } 8409 8410 return new (Context) 8411 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 8412 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 8413 } 8414 8415 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 8416 SourceLocation StartLoc, 8417 SourceLocation LParenLoc, 8418 SourceLocation EndLoc) { 8419 Expr *ValExpr = Condition; 8420 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 8421 !Condition->isInstantiationDependent() && 8422 !Condition->containsUnexpandedParameterPack()) { 8423 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 8424 if (Val.isInvalid()) 8425 return nullptr; 8426 8427 ValExpr = MakeFullExpr(Val.get()).get(); 8428 } 8429 8430 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc); 8431 } 8432 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 8433 Expr *Op) { 8434 if (!Op) 8435 return ExprError(); 8436 8437 class IntConvertDiagnoser : public ICEConvertDiagnoser { 8438 public: 8439 IntConvertDiagnoser() 8440 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 8441 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 8442 QualType T) override { 8443 return S.Diag(Loc, diag::err_omp_not_integral) << T; 8444 } 8445 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 8446 QualType T) override { 8447 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 8448 } 8449 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 8450 QualType T, 8451 QualType ConvTy) override { 8452 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 8453 } 8454 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 8455 QualType ConvTy) override { 8456 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 8457 << ConvTy->isEnumeralType() << ConvTy; 8458 } 8459 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 8460 QualType T) override { 8461 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 8462 } 8463 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 8464 QualType ConvTy) override { 8465 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 8466 << ConvTy->isEnumeralType() << ConvTy; 8467 } 8468 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 8469 QualType) override { 8470 llvm_unreachable("conversion functions are permitted"); 8471 } 8472 } ConvertDiagnoser; 8473 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 8474 } 8475 8476 static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, 8477 OpenMPClauseKind CKind, 8478 bool StrictlyPositive) { 8479 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 8480 !ValExpr->isInstantiationDependent()) { 8481 SourceLocation Loc = ValExpr->getExprLoc(); 8482 ExprResult Value = 8483 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 8484 if (Value.isInvalid()) 8485 return false; 8486 8487 ValExpr = Value.get(); 8488 // The expression must evaluate to a non-negative integer value. 8489 llvm::APSInt Result; 8490 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) && 8491 Result.isSigned() && 8492 !((!StrictlyPositive && Result.isNonNegative()) || 8493 (StrictlyPositive && Result.isStrictlyPositive()))) { 8494 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 8495 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 8496 << ValExpr->getSourceRange(); 8497 return false; 8498 } 8499 } 8500 return true; 8501 } 8502 8503 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 8504 SourceLocation StartLoc, 8505 SourceLocation LParenLoc, 8506 SourceLocation EndLoc) { 8507 Expr *ValExpr = NumThreads; 8508 Stmt *HelperValStmt = nullptr; 8509 8510 // OpenMP [2.5, Restrictions] 8511 // The num_threads expression must evaluate to a positive integer value. 8512 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 8513 /*StrictlyPositive=*/true)) 8514 return nullptr; 8515 8516 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 8517 OpenMPDirectiveKind CaptureRegion = 8518 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads); 8519 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 8520 ValExpr = MakeFullExpr(ValExpr).get(); 8521 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 8522 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 8523 HelperValStmt = buildPreInits(Context, Captures); 8524 } 8525 8526 return new (Context) OMPNumThreadsClause( 8527 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 8528 } 8529 8530 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 8531 OpenMPClauseKind CKind, 8532 bool StrictlyPositive) { 8533 if (!E) 8534 return ExprError(); 8535 if (E->isValueDependent() || E->isTypeDependent() || 8536 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 8537 return E; 8538 llvm::APSInt Result; 8539 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); 8540 if (ICE.isInvalid()) 8541 return ExprError(); 8542 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 8543 (!StrictlyPositive && !Result.isNonNegative())) { 8544 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 8545 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 8546 << E->getSourceRange(); 8547 return ExprError(); 8548 } 8549 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 8550 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 8551 << E->getSourceRange(); 8552 return ExprError(); 8553 } 8554 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 8555 DSAStack->setAssociatedLoops(Result.getExtValue()); 8556 else if (CKind == OMPC_ordered) 8557 DSAStack->setAssociatedLoops(Result.getExtValue()); 8558 return ICE; 8559 } 8560 8561 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 8562 SourceLocation LParenLoc, 8563 SourceLocation EndLoc) { 8564 // OpenMP [2.8.1, simd construct, Description] 8565 // The parameter of the safelen clause must be a constant 8566 // positive integer expression. 8567 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 8568 if (Safelen.isInvalid()) 8569 return nullptr; 8570 return new (Context) 8571 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 8572 } 8573 8574 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 8575 SourceLocation LParenLoc, 8576 SourceLocation EndLoc) { 8577 // OpenMP [2.8.1, simd construct, Description] 8578 // The parameter of the simdlen clause must be a constant 8579 // positive integer expression. 8580 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 8581 if (Simdlen.isInvalid()) 8582 return nullptr; 8583 return new (Context) 8584 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 8585 } 8586 8587 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 8588 SourceLocation StartLoc, 8589 SourceLocation LParenLoc, 8590 SourceLocation EndLoc) { 8591 // OpenMP [2.7.1, loop construct, Description] 8592 // OpenMP [2.8.1, simd construct, Description] 8593 // OpenMP [2.9.6, distribute construct, Description] 8594 // The parameter of the collapse clause must be a constant 8595 // positive integer expression. 8596 ExprResult NumForLoopsResult = 8597 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 8598 if (NumForLoopsResult.isInvalid()) 8599 return nullptr; 8600 return new (Context) 8601 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 8602 } 8603 8604 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 8605 SourceLocation EndLoc, 8606 SourceLocation LParenLoc, 8607 Expr *NumForLoops) { 8608 // OpenMP [2.7.1, loop construct, Description] 8609 // OpenMP [2.8.1, simd construct, Description] 8610 // OpenMP [2.9.6, distribute construct, Description] 8611 // The parameter of the ordered clause must be a constant 8612 // positive integer expression if any. 8613 if (NumForLoops && LParenLoc.isValid()) { 8614 ExprResult NumForLoopsResult = 8615 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 8616 if (NumForLoopsResult.isInvalid()) 8617 return nullptr; 8618 NumForLoops = NumForLoopsResult.get(); 8619 } else { 8620 NumForLoops = nullptr; 8621 } 8622 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops); 8623 return new (Context) 8624 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc); 8625 } 8626 8627 OMPClause *Sema::ActOnOpenMPSimpleClause( 8628 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 8629 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 8630 OMPClause *Res = nullptr; 8631 switch (Kind) { 8632 case OMPC_default: 8633 Res = 8634 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), 8635 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 8636 break; 8637 case OMPC_proc_bind: 8638 Res = ActOnOpenMPProcBindClause( 8639 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, 8640 LParenLoc, EndLoc); 8641 break; 8642 case OMPC_if: 8643 case OMPC_final: 8644 case OMPC_num_threads: 8645 case OMPC_safelen: 8646 case OMPC_simdlen: 8647 case OMPC_collapse: 8648 case OMPC_schedule: 8649 case OMPC_private: 8650 case OMPC_firstprivate: 8651 case OMPC_lastprivate: 8652 case OMPC_shared: 8653 case OMPC_reduction: 8654 case OMPC_task_reduction: 8655 case OMPC_in_reduction: 8656 case OMPC_linear: 8657 case OMPC_aligned: 8658 case OMPC_copyin: 8659 case OMPC_copyprivate: 8660 case OMPC_ordered: 8661 case OMPC_nowait: 8662 case OMPC_untied: 8663 case OMPC_mergeable: 8664 case OMPC_threadprivate: 8665 case OMPC_flush: 8666 case OMPC_read: 8667 case OMPC_write: 8668 case OMPC_update: 8669 case OMPC_capture: 8670 case OMPC_seq_cst: 8671 case OMPC_depend: 8672 case OMPC_device: 8673 case OMPC_threads: 8674 case OMPC_simd: 8675 case OMPC_map: 8676 case OMPC_num_teams: 8677 case OMPC_thread_limit: 8678 case OMPC_priority: 8679 case OMPC_grainsize: 8680 case OMPC_nogroup: 8681 case OMPC_num_tasks: 8682 case OMPC_hint: 8683 case OMPC_dist_schedule: 8684 case OMPC_defaultmap: 8685 case OMPC_unknown: 8686 case OMPC_uniform: 8687 case OMPC_to: 8688 case OMPC_from: 8689 case OMPC_use_device_ptr: 8690 case OMPC_is_device_ptr: 8691 llvm_unreachable("Clause is not allowed."); 8692 } 8693 return Res; 8694 } 8695 8696 static std::string 8697 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 8698 ArrayRef<unsigned> Exclude = llvm::None) { 8699 SmallString<256> Buffer; 8700 llvm::raw_svector_ostream Out(Buffer); 8701 unsigned Bound = Last >= 2 ? Last - 2 : 0; 8702 unsigned Skipped = Exclude.size(); 8703 auto S = Exclude.begin(), E = Exclude.end(); 8704 for (unsigned I = First; I < Last; ++I) { 8705 if (std::find(S, E, I) != E) { 8706 --Skipped; 8707 continue; 8708 } 8709 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 8710 if (I == Bound - Skipped) 8711 Out << " or "; 8712 else if (I != Bound + 1 - Skipped) 8713 Out << ", "; 8714 } 8715 return Out.str(); 8716 } 8717 8718 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, 8719 SourceLocation KindKwLoc, 8720 SourceLocation StartLoc, 8721 SourceLocation LParenLoc, 8722 SourceLocation EndLoc) { 8723 if (Kind == OMPC_DEFAULT_unknown) { 8724 static_assert(OMPC_DEFAULT_unknown > 0, 8725 "OMPC_DEFAULT_unknown not greater than 0"); 8726 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 8727 << getListOfPossibleValues(OMPC_default, /*First=*/0, 8728 /*Last=*/OMPC_DEFAULT_unknown) 8729 << getOpenMPClauseName(OMPC_default); 8730 return nullptr; 8731 } 8732 switch (Kind) { 8733 case OMPC_DEFAULT_none: 8734 DSAStack->setDefaultDSANone(KindKwLoc); 8735 break; 8736 case OMPC_DEFAULT_shared: 8737 DSAStack->setDefaultDSAShared(KindKwLoc); 8738 break; 8739 case OMPC_DEFAULT_unknown: 8740 llvm_unreachable("Clause kind is not allowed."); 8741 break; 8742 } 8743 return new (Context) 8744 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 8745 } 8746 8747 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, 8748 SourceLocation KindKwLoc, 8749 SourceLocation StartLoc, 8750 SourceLocation LParenLoc, 8751 SourceLocation EndLoc) { 8752 if (Kind == OMPC_PROC_BIND_unknown) { 8753 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 8754 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0, 8755 /*Last=*/OMPC_PROC_BIND_unknown) 8756 << getOpenMPClauseName(OMPC_proc_bind); 8757 return nullptr; 8758 } 8759 return new (Context) 8760 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 8761 } 8762 8763 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 8764 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 8765 SourceLocation StartLoc, SourceLocation LParenLoc, 8766 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 8767 SourceLocation EndLoc) { 8768 OMPClause *Res = nullptr; 8769 switch (Kind) { 8770 case OMPC_schedule: 8771 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 8772 assert(Argument.size() == NumberOfElements && 8773 ArgumentLoc.size() == NumberOfElements); 8774 Res = ActOnOpenMPScheduleClause( 8775 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 8776 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 8777 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 8778 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 8779 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 8780 break; 8781 case OMPC_if: 8782 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 8783 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 8784 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 8785 DelimLoc, EndLoc); 8786 break; 8787 case OMPC_dist_schedule: 8788 Res = ActOnOpenMPDistScheduleClause( 8789 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 8790 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 8791 break; 8792 case OMPC_defaultmap: 8793 enum { Modifier, DefaultmapKind }; 8794 Res = ActOnOpenMPDefaultmapClause( 8795 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 8796 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 8797 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 8798 EndLoc); 8799 break; 8800 case OMPC_final: 8801 case OMPC_num_threads: 8802 case OMPC_safelen: 8803 case OMPC_simdlen: 8804 case OMPC_collapse: 8805 case OMPC_default: 8806 case OMPC_proc_bind: 8807 case OMPC_private: 8808 case OMPC_firstprivate: 8809 case OMPC_lastprivate: 8810 case OMPC_shared: 8811 case OMPC_reduction: 8812 case OMPC_task_reduction: 8813 case OMPC_in_reduction: 8814 case OMPC_linear: 8815 case OMPC_aligned: 8816 case OMPC_copyin: 8817 case OMPC_copyprivate: 8818 case OMPC_ordered: 8819 case OMPC_nowait: 8820 case OMPC_untied: 8821 case OMPC_mergeable: 8822 case OMPC_threadprivate: 8823 case OMPC_flush: 8824 case OMPC_read: 8825 case OMPC_write: 8826 case OMPC_update: 8827 case OMPC_capture: 8828 case OMPC_seq_cst: 8829 case OMPC_depend: 8830 case OMPC_device: 8831 case OMPC_threads: 8832 case OMPC_simd: 8833 case OMPC_map: 8834 case OMPC_num_teams: 8835 case OMPC_thread_limit: 8836 case OMPC_priority: 8837 case OMPC_grainsize: 8838 case OMPC_nogroup: 8839 case OMPC_num_tasks: 8840 case OMPC_hint: 8841 case OMPC_unknown: 8842 case OMPC_uniform: 8843 case OMPC_to: 8844 case OMPC_from: 8845 case OMPC_use_device_ptr: 8846 case OMPC_is_device_ptr: 8847 llvm_unreachable("Clause is not allowed."); 8848 } 8849 return Res; 8850 } 8851 8852 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 8853 OpenMPScheduleClauseModifier M2, 8854 SourceLocation M1Loc, SourceLocation M2Loc) { 8855 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 8856 SmallVector<unsigned, 2> Excluded; 8857 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 8858 Excluded.push_back(M2); 8859 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 8860 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 8861 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 8862 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 8863 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 8864 << getListOfPossibleValues(OMPC_schedule, 8865 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 8866 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 8867 Excluded) 8868 << getOpenMPClauseName(OMPC_schedule); 8869 return true; 8870 } 8871 return false; 8872 } 8873 8874 OMPClause *Sema::ActOnOpenMPScheduleClause( 8875 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 8876 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 8877 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 8878 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 8879 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 8880 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 8881 return nullptr; 8882 // OpenMP, 2.7.1, Loop Construct, Restrictions 8883 // Either the monotonic modifier or the nonmonotonic modifier can be specified 8884 // but not both. 8885 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 8886 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 8887 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 8888 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 8889 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 8890 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 8891 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 8892 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 8893 return nullptr; 8894 } 8895 if (Kind == OMPC_SCHEDULE_unknown) { 8896 std::string Values; 8897 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 8898 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 8899 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 8900 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 8901 Exclude); 8902 } else { 8903 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 8904 /*Last=*/OMPC_SCHEDULE_unknown); 8905 } 8906 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 8907 << Values << getOpenMPClauseName(OMPC_schedule); 8908 return nullptr; 8909 } 8910 // OpenMP, 2.7.1, Loop Construct, Restrictions 8911 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 8912 // schedule(guided). 8913 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 8914 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 8915 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 8916 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 8917 diag::err_omp_schedule_nonmonotonic_static); 8918 return nullptr; 8919 } 8920 Expr *ValExpr = ChunkSize; 8921 Stmt *HelperValStmt = nullptr; 8922 if (ChunkSize) { 8923 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 8924 !ChunkSize->isInstantiationDependent() && 8925 !ChunkSize->containsUnexpandedParameterPack()) { 8926 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); 8927 ExprResult Val = 8928 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 8929 if (Val.isInvalid()) 8930 return nullptr; 8931 8932 ValExpr = Val.get(); 8933 8934 // OpenMP [2.7.1, Restrictions] 8935 // chunk_size must be a loop invariant integer expression with a positive 8936 // value. 8937 llvm::APSInt Result; 8938 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 8939 if (Result.isSigned() && !Result.isStrictlyPositive()) { 8940 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 8941 << "schedule" << 1 << ChunkSize->getSourceRange(); 8942 return nullptr; 8943 } 8944 } else if (getOpenMPCaptureRegionForClause( 8945 DSAStack->getCurrentDirective(), OMPC_schedule) != 8946 OMPD_unknown && 8947 !CurContext->isDependentContext()) { 8948 ValExpr = MakeFullExpr(ValExpr).get(); 8949 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 8950 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 8951 HelperValStmt = buildPreInits(Context, Captures); 8952 } 8953 } 8954 } 8955 8956 return new (Context) 8957 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 8958 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 8959 } 8960 8961 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 8962 SourceLocation StartLoc, 8963 SourceLocation EndLoc) { 8964 OMPClause *Res = nullptr; 8965 switch (Kind) { 8966 case OMPC_ordered: 8967 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 8968 break; 8969 case OMPC_nowait: 8970 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 8971 break; 8972 case OMPC_untied: 8973 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 8974 break; 8975 case OMPC_mergeable: 8976 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 8977 break; 8978 case OMPC_read: 8979 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 8980 break; 8981 case OMPC_write: 8982 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 8983 break; 8984 case OMPC_update: 8985 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 8986 break; 8987 case OMPC_capture: 8988 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 8989 break; 8990 case OMPC_seq_cst: 8991 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 8992 break; 8993 case OMPC_threads: 8994 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 8995 break; 8996 case OMPC_simd: 8997 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 8998 break; 8999 case OMPC_nogroup: 9000 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 9001 break; 9002 case OMPC_if: 9003 case OMPC_final: 9004 case OMPC_num_threads: 9005 case OMPC_safelen: 9006 case OMPC_simdlen: 9007 case OMPC_collapse: 9008 case OMPC_schedule: 9009 case OMPC_private: 9010 case OMPC_firstprivate: 9011 case OMPC_lastprivate: 9012 case OMPC_shared: 9013 case OMPC_reduction: 9014 case OMPC_task_reduction: 9015 case OMPC_in_reduction: 9016 case OMPC_linear: 9017 case OMPC_aligned: 9018 case OMPC_copyin: 9019 case OMPC_copyprivate: 9020 case OMPC_default: 9021 case OMPC_proc_bind: 9022 case OMPC_threadprivate: 9023 case OMPC_flush: 9024 case OMPC_depend: 9025 case OMPC_device: 9026 case OMPC_map: 9027 case OMPC_num_teams: 9028 case OMPC_thread_limit: 9029 case OMPC_priority: 9030 case OMPC_grainsize: 9031 case OMPC_num_tasks: 9032 case OMPC_hint: 9033 case OMPC_dist_schedule: 9034 case OMPC_defaultmap: 9035 case OMPC_unknown: 9036 case OMPC_uniform: 9037 case OMPC_to: 9038 case OMPC_from: 9039 case OMPC_use_device_ptr: 9040 case OMPC_is_device_ptr: 9041 llvm_unreachable("Clause is not allowed."); 9042 } 9043 return Res; 9044 } 9045 9046 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 9047 SourceLocation EndLoc) { 9048 DSAStack->setNowaitRegion(); 9049 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 9050 } 9051 9052 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 9053 SourceLocation EndLoc) { 9054 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 9055 } 9056 9057 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 9058 SourceLocation EndLoc) { 9059 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 9060 } 9061 9062 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 9063 SourceLocation EndLoc) { 9064 return new (Context) OMPReadClause(StartLoc, EndLoc); 9065 } 9066 9067 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 9068 SourceLocation EndLoc) { 9069 return new (Context) OMPWriteClause(StartLoc, EndLoc); 9070 } 9071 9072 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 9073 SourceLocation EndLoc) { 9074 return new (Context) OMPUpdateClause(StartLoc, EndLoc); 9075 } 9076 9077 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 9078 SourceLocation EndLoc) { 9079 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 9080 } 9081 9082 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 9083 SourceLocation EndLoc) { 9084 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 9085 } 9086 9087 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 9088 SourceLocation EndLoc) { 9089 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 9090 } 9091 9092 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 9093 SourceLocation EndLoc) { 9094 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 9095 } 9096 9097 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 9098 SourceLocation EndLoc) { 9099 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 9100 } 9101 9102 OMPClause *Sema::ActOnOpenMPVarListClause( 9103 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, 9104 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 9105 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec, 9106 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind, 9107 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier, 9108 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, 9109 SourceLocation DepLinMapLoc) { 9110 OMPClause *Res = nullptr; 9111 switch (Kind) { 9112 case OMPC_private: 9113 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9114 break; 9115 case OMPC_firstprivate: 9116 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9117 break; 9118 case OMPC_lastprivate: 9119 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9120 break; 9121 case OMPC_shared: 9122 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 9123 break; 9124 case OMPC_reduction: 9125 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 9126 EndLoc, ReductionIdScopeSpec, ReductionId); 9127 break; 9128 case OMPC_task_reduction: 9129 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 9130 EndLoc, ReductionIdScopeSpec, 9131 ReductionId); 9132 break; 9133 case OMPC_in_reduction: 9134 Res = 9135 ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 9136 EndLoc, ReductionIdScopeSpec, ReductionId); 9137 break; 9138 case OMPC_linear: 9139 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, 9140 LinKind, DepLinMapLoc, ColonLoc, EndLoc); 9141 break; 9142 case OMPC_aligned: 9143 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, 9144 ColonLoc, EndLoc); 9145 break; 9146 case OMPC_copyin: 9147 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 9148 break; 9149 case OMPC_copyprivate: 9150 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9151 break; 9152 case OMPC_flush: 9153 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 9154 break; 9155 case OMPC_depend: 9156 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList, 9157 StartLoc, LParenLoc, EndLoc); 9158 break; 9159 case OMPC_map: 9160 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit, 9161 DepLinMapLoc, ColonLoc, VarList, StartLoc, 9162 LParenLoc, EndLoc); 9163 break; 9164 case OMPC_to: 9165 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc); 9166 break; 9167 case OMPC_from: 9168 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc); 9169 break; 9170 case OMPC_use_device_ptr: 9171 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc); 9172 break; 9173 case OMPC_is_device_ptr: 9174 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc); 9175 break; 9176 case OMPC_if: 9177 case OMPC_final: 9178 case OMPC_num_threads: 9179 case OMPC_safelen: 9180 case OMPC_simdlen: 9181 case OMPC_collapse: 9182 case OMPC_default: 9183 case OMPC_proc_bind: 9184 case OMPC_schedule: 9185 case OMPC_ordered: 9186 case OMPC_nowait: 9187 case OMPC_untied: 9188 case OMPC_mergeable: 9189 case OMPC_threadprivate: 9190 case OMPC_read: 9191 case OMPC_write: 9192 case OMPC_update: 9193 case OMPC_capture: 9194 case OMPC_seq_cst: 9195 case OMPC_device: 9196 case OMPC_threads: 9197 case OMPC_simd: 9198 case OMPC_num_teams: 9199 case OMPC_thread_limit: 9200 case OMPC_priority: 9201 case OMPC_grainsize: 9202 case OMPC_nogroup: 9203 case OMPC_num_tasks: 9204 case OMPC_hint: 9205 case OMPC_dist_schedule: 9206 case OMPC_defaultmap: 9207 case OMPC_unknown: 9208 case OMPC_uniform: 9209 llvm_unreachable("Clause is not allowed."); 9210 } 9211 return Res; 9212 } 9213 9214 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 9215 ExprObjectKind OK, SourceLocation Loc) { 9216 ExprResult Res = BuildDeclRefExpr( 9217 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 9218 if (!Res.isUsable()) 9219 return ExprError(); 9220 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 9221 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 9222 if (!Res.isUsable()) 9223 return ExprError(); 9224 } 9225 if (VK != VK_LValue && Res.get()->isGLValue()) { 9226 Res = DefaultLvalueConversion(Res.get()); 9227 if (!Res.isUsable()) 9228 return ExprError(); 9229 } 9230 return Res; 9231 } 9232 9233 static std::pair<ValueDecl *, bool> 9234 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 9235 SourceRange &ERange, bool AllowArraySection = false) { 9236 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 9237 RefExpr->containsUnexpandedParameterPack()) 9238 return std::make_pair(nullptr, true); 9239 9240 // OpenMP [3.1, C/C++] 9241 // A list item is a variable name. 9242 // OpenMP [2.9.3.3, Restrictions, p.1] 9243 // A variable that is part of another variable (as an array or 9244 // structure element) cannot appear in a private clause. 9245 RefExpr = RefExpr->IgnoreParens(); 9246 enum { 9247 NoArrayExpr = -1, 9248 ArraySubscript = 0, 9249 OMPArraySection = 1 9250 } IsArrayExpr = NoArrayExpr; 9251 if (AllowArraySection) { 9252 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 9253 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 9254 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 9255 Base = TempASE->getBase()->IgnoreParenImpCasts(); 9256 RefExpr = Base; 9257 IsArrayExpr = ArraySubscript; 9258 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 9259 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 9260 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 9261 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 9262 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 9263 Base = TempASE->getBase()->IgnoreParenImpCasts(); 9264 RefExpr = Base; 9265 IsArrayExpr = OMPArraySection; 9266 } 9267 } 9268 ELoc = RefExpr->getExprLoc(); 9269 ERange = RefExpr->getSourceRange(); 9270 RefExpr = RefExpr->IgnoreParenImpCasts(); 9271 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 9272 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 9273 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 9274 (S.getCurrentThisType().isNull() || !ME || 9275 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 9276 !isa<FieldDecl>(ME->getMemberDecl()))) { 9277 if (IsArrayExpr != NoArrayExpr) { 9278 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 9279 << ERange; 9280 } else { 9281 S.Diag(ELoc, 9282 AllowArraySection 9283 ? diag::err_omp_expected_var_name_member_expr_or_array_item 9284 : diag::err_omp_expected_var_name_member_expr) 9285 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 9286 } 9287 return std::make_pair(nullptr, false); 9288 } 9289 return std::make_pair( 9290 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 9291 } 9292 9293 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 9294 SourceLocation StartLoc, 9295 SourceLocation LParenLoc, 9296 SourceLocation EndLoc) { 9297 SmallVector<Expr *, 8> Vars; 9298 SmallVector<Expr *, 8> PrivateCopies; 9299 for (Expr *RefExpr : VarList) { 9300 assert(RefExpr && "NULL expr in OpenMP private clause."); 9301 SourceLocation ELoc; 9302 SourceRange ERange; 9303 Expr *SimpleRefExpr = RefExpr; 9304 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 9305 if (Res.second) { 9306 // It will be analyzed later. 9307 Vars.push_back(RefExpr); 9308 PrivateCopies.push_back(nullptr); 9309 } 9310 ValueDecl *D = Res.first; 9311 if (!D) 9312 continue; 9313 9314 QualType Type = D->getType(); 9315 auto *VD = dyn_cast<VarDecl>(D); 9316 9317 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 9318 // A variable that appears in a private clause must not have an incomplete 9319 // type or a reference type. 9320 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 9321 continue; 9322 Type = Type.getNonReferenceType(); 9323 9324 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 9325 // in a Construct] 9326 // Variables with the predetermined data-sharing attributes may not be 9327 // listed in data-sharing attributes clauses, except for the cases 9328 // listed below. For these exceptions only, listing a predetermined 9329 // variable in a data-sharing attribute clause is allowed and overrides 9330 // the variable's predetermined data-sharing attributes. 9331 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 9332 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 9333 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 9334 << getOpenMPClauseName(OMPC_private); 9335 reportOriginalDsa(*this, DSAStack, D, DVar); 9336 continue; 9337 } 9338 9339 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 9340 // Variably modified types are not supported for tasks. 9341 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 9342 isOpenMPTaskingDirective(CurrDir)) { 9343 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 9344 << getOpenMPClauseName(OMPC_private) << Type 9345 << getOpenMPDirectiveName(CurrDir); 9346 bool IsDecl = 9347 !VD || 9348 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 9349 Diag(D->getLocation(), 9350 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 9351 << D; 9352 continue; 9353 } 9354 9355 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 9356 // A list item cannot appear in both a map clause and a data-sharing 9357 // attribute clause on the same construct 9358 if (isOpenMPTargetExecutionDirective(CurrDir)) { 9359 OpenMPClauseKind ConflictKind; 9360 if (DSAStack->checkMappableExprComponentListsForDecl( 9361 VD, /*CurrentRegionOnly=*/true, 9362 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 9363 OpenMPClauseKind WhereFoundClauseKind) -> bool { 9364 ConflictKind = WhereFoundClauseKind; 9365 return true; 9366 })) { 9367 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 9368 << getOpenMPClauseName(OMPC_private) 9369 << getOpenMPClauseName(ConflictKind) 9370 << getOpenMPDirectiveName(CurrDir); 9371 reportOriginalDsa(*this, DSAStack, D, DVar); 9372 continue; 9373 } 9374 } 9375 9376 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 9377 // A variable of class type (or array thereof) that appears in a private 9378 // clause requires an accessible, unambiguous default constructor for the 9379 // class type. 9380 // Generate helper private variable and initialize it with the default 9381 // value. The address of the original variable is replaced by the address of 9382 // the new private variable in CodeGen. This new variable is not added to 9383 // IdResolver, so the code in the OpenMP region uses original variable for 9384 // proper diagnostics. 9385 Type = Type.getUnqualifiedType(); 9386 VarDecl *VDPrivate = 9387 buildVarDecl(*this, ELoc, Type, D->getName(), 9388 D->hasAttrs() ? &D->getAttrs() : nullptr, 9389 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 9390 ActOnUninitializedDecl(VDPrivate); 9391 if (VDPrivate->isInvalidDecl()) 9392 continue; 9393 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 9394 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 9395 9396 DeclRefExpr *Ref = nullptr; 9397 if (!VD && !CurContext->isDependentContext()) 9398 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 9399 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 9400 Vars.push_back((VD || CurContext->isDependentContext()) 9401 ? RefExpr->IgnoreParens() 9402 : Ref); 9403 PrivateCopies.push_back(VDPrivateRefExpr); 9404 } 9405 9406 if (Vars.empty()) 9407 return nullptr; 9408 9409 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 9410 PrivateCopies); 9411 } 9412 9413 namespace { 9414 class DiagsUninitializedSeveretyRAII { 9415 private: 9416 DiagnosticsEngine &Diags; 9417 SourceLocation SavedLoc; 9418 bool IsIgnored = false; 9419 9420 public: 9421 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 9422 bool IsIgnored) 9423 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 9424 if (!IsIgnored) { 9425 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 9426 /*Map*/ diag::Severity::Ignored, Loc); 9427 } 9428 } 9429 ~DiagsUninitializedSeveretyRAII() { 9430 if (!IsIgnored) 9431 Diags.popMappings(SavedLoc); 9432 } 9433 }; 9434 } 9435 9436 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 9437 SourceLocation StartLoc, 9438 SourceLocation LParenLoc, 9439 SourceLocation EndLoc) { 9440 SmallVector<Expr *, 8> Vars; 9441 SmallVector<Expr *, 8> PrivateCopies; 9442 SmallVector<Expr *, 8> Inits; 9443 SmallVector<Decl *, 4> ExprCaptures; 9444 bool IsImplicitClause = 9445 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 9446 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 9447 9448 for (Expr *RefExpr : VarList) { 9449 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 9450 SourceLocation ELoc; 9451 SourceRange ERange; 9452 Expr *SimpleRefExpr = RefExpr; 9453 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 9454 if (Res.second) { 9455 // It will be analyzed later. 9456 Vars.push_back(RefExpr); 9457 PrivateCopies.push_back(nullptr); 9458 Inits.push_back(nullptr); 9459 } 9460 ValueDecl *D = Res.first; 9461 if (!D) 9462 continue; 9463 9464 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 9465 QualType Type = D->getType(); 9466 auto *VD = dyn_cast<VarDecl>(D); 9467 9468 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 9469 // A variable that appears in a private clause must not have an incomplete 9470 // type or a reference type. 9471 if (RequireCompleteType(ELoc, Type, 9472 diag::err_omp_firstprivate_incomplete_type)) 9473 continue; 9474 Type = Type.getNonReferenceType(); 9475 9476 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 9477 // A variable of class type (or array thereof) that appears in a private 9478 // clause requires an accessible, unambiguous copy constructor for the 9479 // class type. 9480 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 9481 9482 // If an implicit firstprivate variable found it was checked already. 9483 DSAStackTy::DSAVarData TopDVar; 9484 if (!IsImplicitClause) { 9485 DSAStackTy::DSAVarData DVar = 9486 DSAStack->getTopDSA(D, /*FromParent=*/false); 9487 TopDVar = DVar; 9488 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 9489 bool IsConstant = ElemType.isConstant(Context); 9490 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 9491 // A list item that specifies a given variable may not appear in more 9492 // than one clause on the same directive, except that a variable may be 9493 // specified in both firstprivate and lastprivate clauses. 9494 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 9495 // A list item may appear in a firstprivate or lastprivate clause but not 9496 // both. 9497 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 9498 (isOpenMPDistributeDirective(CurrDir) || 9499 DVar.CKind != OMPC_lastprivate) && 9500 DVar.RefExpr) { 9501 Diag(ELoc, diag::err_omp_wrong_dsa) 9502 << getOpenMPClauseName(DVar.CKind) 9503 << getOpenMPClauseName(OMPC_firstprivate); 9504 reportOriginalDsa(*this, DSAStack, D, DVar); 9505 continue; 9506 } 9507 9508 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 9509 // in a Construct] 9510 // Variables with the predetermined data-sharing attributes may not be 9511 // listed in data-sharing attributes clauses, except for the cases 9512 // listed below. For these exceptions only, listing a predetermined 9513 // variable in a data-sharing attribute clause is allowed and overrides 9514 // the variable's predetermined data-sharing attributes. 9515 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 9516 // in a Construct, C/C++, p.2] 9517 // Variables with const-qualified type having no mutable member may be 9518 // listed in a firstprivate clause, even if they are static data members. 9519 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 9520 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 9521 Diag(ELoc, diag::err_omp_wrong_dsa) 9522 << getOpenMPClauseName(DVar.CKind) 9523 << getOpenMPClauseName(OMPC_firstprivate); 9524 reportOriginalDsa(*this, DSAStack, D, DVar); 9525 continue; 9526 } 9527 9528 // OpenMP [2.9.3.4, Restrictions, p.2] 9529 // A list item that is private within a parallel region must not appear 9530 // in a firstprivate clause on a worksharing construct if any of the 9531 // worksharing regions arising from the worksharing construct ever bind 9532 // to any of the parallel regions arising from the parallel construct. 9533 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 9534 // A list item that is private within a teams region must not appear in a 9535 // firstprivate clause on a distribute construct if any of the distribute 9536 // regions arising from the distribute construct ever bind to any of the 9537 // teams regions arising from the teams construct. 9538 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 9539 // A list item that appears in a reduction clause of a teams construct 9540 // must not appear in a firstprivate clause on a distribute construct if 9541 // any of the distribute regions arising from the distribute construct 9542 // ever bind to any of the teams regions arising from the teams construct. 9543 if ((isOpenMPWorksharingDirective(CurrDir) || 9544 isOpenMPDistributeDirective(CurrDir)) && 9545 !isOpenMPParallelDirective(CurrDir) && 9546 !isOpenMPTeamsDirective(CurrDir)) { 9547 DVar = DSAStack->getImplicitDSA(D, true); 9548 if (DVar.CKind != OMPC_shared && 9549 (isOpenMPParallelDirective(DVar.DKind) || 9550 isOpenMPTeamsDirective(DVar.DKind) || 9551 DVar.DKind == OMPD_unknown)) { 9552 Diag(ELoc, diag::err_omp_required_access) 9553 << getOpenMPClauseName(OMPC_firstprivate) 9554 << getOpenMPClauseName(OMPC_shared); 9555 reportOriginalDsa(*this, DSAStack, D, DVar); 9556 continue; 9557 } 9558 } 9559 // OpenMP [2.9.3.4, Restrictions, p.3] 9560 // A list item that appears in a reduction clause of a parallel construct 9561 // must not appear in a firstprivate clause on a worksharing or task 9562 // construct if any of the worksharing or task regions arising from the 9563 // worksharing or task construct ever bind to any of the parallel regions 9564 // arising from the parallel construct. 9565 // OpenMP [2.9.3.4, Restrictions, p.4] 9566 // A list item that appears in a reduction clause in worksharing 9567 // construct must not appear in a firstprivate clause in a task construct 9568 // encountered during execution of any of the worksharing regions arising 9569 // from the worksharing construct. 9570 if (isOpenMPTaskingDirective(CurrDir)) { 9571 DVar = DSAStack->hasInnermostDSA( 9572 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 9573 [](OpenMPDirectiveKind K) { 9574 return isOpenMPParallelDirective(K) || 9575 isOpenMPWorksharingDirective(K) || 9576 isOpenMPTeamsDirective(K); 9577 }, 9578 /*FromParent=*/true); 9579 if (DVar.CKind == OMPC_reduction && 9580 (isOpenMPParallelDirective(DVar.DKind) || 9581 isOpenMPWorksharingDirective(DVar.DKind) || 9582 isOpenMPTeamsDirective(DVar.DKind))) { 9583 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 9584 << getOpenMPDirectiveName(DVar.DKind); 9585 reportOriginalDsa(*this, DSAStack, D, DVar); 9586 continue; 9587 } 9588 } 9589 9590 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 9591 // A list item cannot appear in both a map clause and a data-sharing 9592 // attribute clause on the same construct 9593 if (isOpenMPTargetExecutionDirective(CurrDir)) { 9594 OpenMPClauseKind ConflictKind; 9595 if (DSAStack->checkMappableExprComponentListsForDecl( 9596 VD, /*CurrentRegionOnly=*/true, 9597 [&ConflictKind]( 9598 OMPClauseMappableExprCommon::MappableExprComponentListRef, 9599 OpenMPClauseKind WhereFoundClauseKind) { 9600 ConflictKind = WhereFoundClauseKind; 9601 return true; 9602 })) { 9603 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 9604 << getOpenMPClauseName(OMPC_firstprivate) 9605 << getOpenMPClauseName(ConflictKind) 9606 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 9607 reportOriginalDsa(*this, DSAStack, D, DVar); 9608 continue; 9609 } 9610 } 9611 } 9612 9613 // Variably modified types are not supported for tasks. 9614 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 9615 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 9616 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 9617 << getOpenMPClauseName(OMPC_firstprivate) << Type 9618 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 9619 bool IsDecl = 9620 !VD || 9621 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 9622 Diag(D->getLocation(), 9623 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 9624 << D; 9625 continue; 9626 } 9627 9628 Type = Type.getUnqualifiedType(); 9629 VarDecl *VDPrivate = 9630 buildVarDecl(*this, ELoc, Type, D->getName(), 9631 D->hasAttrs() ? &D->getAttrs() : nullptr, 9632 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 9633 // Generate helper private variable and initialize it with the value of the 9634 // original variable. The address of the original variable is replaced by 9635 // the address of the new private variable in the CodeGen. This new variable 9636 // is not added to IdResolver, so the code in the OpenMP region uses 9637 // original variable for proper diagnostics and variable capturing. 9638 Expr *VDInitRefExpr = nullptr; 9639 // For arrays generate initializer for single element and replace it by the 9640 // original array element in CodeGen. 9641 if (Type->isArrayType()) { 9642 VarDecl *VDInit = 9643 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 9644 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 9645 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 9646 ElemType = ElemType.getUnqualifiedType(); 9647 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 9648 ".firstprivate.temp"); 9649 InitializedEntity Entity = 9650 InitializedEntity::InitializeVariable(VDInitTemp); 9651 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 9652 9653 InitializationSequence InitSeq(*this, Entity, Kind, Init); 9654 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 9655 if (Result.isInvalid()) 9656 VDPrivate->setInvalidDecl(); 9657 else 9658 VDPrivate->setInit(Result.getAs<Expr>()); 9659 // Remove temp variable declaration. 9660 Context.Deallocate(VDInitTemp); 9661 } else { 9662 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 9663 ".firstprivate.temp"); 9664 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 9665 RefExpr->getExprLoc()); 9666 AddInitializerToDecl(VDPrivate, 9667 DefaultLvalueConversion(VDInitRefExpr).get(), 9668 /*DirectInit=*/false); 9669 } 9670 if (VDPrivate->isInvalidDecl()) { 9671 if (IsImplicitClause) { 9672 Diag(RefExpr->getExprLoc(), 9673 diag::note_omp_task_predetermined_firstprivate_here); 9674 } 9675 continue; 9676 } 9677 CurContext->addDecl(VDPrivate); 9678 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 9679 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 9680 RefExpr->getExprLoc()); 9681 DeclRefExpr *Ref = nullptr; 9682 if (!VD && !CurContext->isDependentContext()) { 9683 if (TopDVar.CKind == OMPC_lastprivate) { 9684 Ref = TopDVar.PrivateCopy; 9685 } else { 9686 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 9687 if (!isOpenMPCapturedDecl(D)) 9688 ExprCaptures.push_back(Ref->getDecl()); 9689 } 9690 } 9691 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 9692 Vars.push_back((VD || CurContext->isDependentContext()) 9693 ? RefExpr->IgnoreParens() 9694 : Ref); 9695 PrivateCopies.push_back(VDPrivateRefExpr); 9696 Inits.push_back(VDInitRefExpr); 9697 } 9698 9699 if (Vars.empty()) 9700 return nullptr; 9701 9702 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 9703 Vars, PrivateCopies, Inits, 9704 buildPreInits(Context, ExprCaptures)); 9705 } 9706 9707 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList, 9708 SourceLocation StartLoc, 9709 SourceLocation LParenLoc, 9710 SourceLocation EndLoc) { 9711 SmallVector<Expr *, 8> Vars; 9712 SmallVector<Expr *, 8> SrcExprs; 9713 SmallVector<Expr *, 8> DstExprs; 9714 SmallVector<Expr *, 8> AssignmentOps; 9715 SmallVector<Decl *, 4> ExprCaptures; 9716 SmallVector<Expr *, 4> ExprPostUpdates; 9717 for (Expr *RefExpr : VarList) { 9718 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 9719 SourceLocation ELoc; 9720 SourceRange ERange; 9721 Expr *SimpleRefExpr = RefExpr; 9722 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 9723 if (Res.second) { 9724 // It will be analyzed later. 9725 Vars.push_back(RefExpr); 9726 SrcExprs.push_back(nullptr); 9727 DstExprs.push_back(nullptr); 9728 AssignmentOps.push_back(nullptr); 9729 } 9730 ValueDecl *D = Res.first; 9731 if (!D) 9732 continue; 9733 9734 QualType Type = D->getType(); 9735 auto *VD = dyn_cast<VarDecl>(D); 9736 9737 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 9738 // A variable that appears in a lastprivate clause must not have an 9739 // incomplete type or a reference type. 9740 if (RequireCompleteType(ELoc, Type, 9741 diag::err_omp_lastprivate_incomplete_type)) 9742 continue; 9743 Type = Type.getNonReferenceType(); 9744 9745 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 9746 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 9747 // in a Construct] 9748 // Variables with the predetermined data-sharing attributes may not be 9749 // listed in data-sharing attributes clauses, except for the cases 9750 // listed below. 9751 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 9752 // A list item may appear in a firstprivate or lastprivate clause but not 9753 // both. 9754 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 9755 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 9756 (isOpenMPDistributeDirective(CurrDir) || 9757 DVar.CKind != OMPC_firstprivate) && 9758 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 9759 Diag(ELoc, diag::err_omp_wrong_dsa) 9760 << getOpenMPClauseName(DVar.CKind) 9761 << getOpenMPClauseName(OMPC_lastprivate); 9762 reportOriginalDsa(*this, DSAStack, D, DVar); 9763 continue; 9764 } 9765 9766 // OpenMP [2.14.3.5, Restrictions, p.2] 9767 // A list item that is private within a parallel region, or that appears in 9768 // the reduction clause of a parallel construct, must not appear in a 9769 // lastprivate clause on a worksharing construct if any of the corresponding 9770 // worksharing regions ever binds to any of the corresponding parallel 9771 // regions. 9772 DSAStackTy::DSAVarData TopDVar = DVar; 9773 if (isOpenMPWorksharingDirective(CurrDir) && 9774 !isOpenMPParallelDirective(CurrDir) && 9775 !isOpenMPTeamsDirective(CurrDir)) { 9776 DVar = DSAStack->getImplicitDSA(D, true); 9777 if (DVar.CKind != OMPC_shared) { 9778 Diag(ELoc, diag::err_omp_required_access) 9779 << getOpenMPClauseName(OMPC_lastprivate) 9780 << getOpenMPClauseName(OMPC_shared); 9781 reportOriginalDsa(*this, DSAStack, D, DVar); 9782 continue; 9783 } 9784 } 9785 9786 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 9787 // A variable of class type (or array thereof) that appears in a 9788 // lastprivate clause requires an accessible, unambiguous default 9789 // constructor for the class type, unless the list item is also specified 9790 // in a firstprivate clause. 9791 // A variable of class type (or array thereof) that appears in a 9792 // lastprivate clause requires an accessible, unambiguous copy assignment 9793 // operator for the class type. 9794 Type = Context.getBaseElementType(Type).getNonReferenceType(); 9795 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 9796 Type.getUnqualifiedType(), ".lastprivate.src", 9797 D->hasAttrs() ? &D->getAttrs() : nullptr); 9798 DeclRefExpr *PseudoSrcExpr = 9799 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 9800 VarDecl *DstVD = 9801 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 9802 D->hasAttrs() ? &D->getAttrs() : nullptr); 9803 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 9804 // For arrays generate assignment operation for single element and replace 9805 // it by the original array element in CodeGen. 9806 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 9807 PseudoDstExpr, PseudoSrcExpr); 9808 if (AssignmentOp.isInvalid()) 9809 continue; 9810 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc, 9811 /*DiscardedValue=*/true); 9812 if (AssignmentOp.isInvalid()) 9813 continue; 9814 9815 DeclRefExpr *Ref = nullptr; 9816 if (!VD && !CurContext->isDependentContext()) { 9817 if (TopDVar.CKind == OMPC_firstprivate) { 9818 Ref = TopDVar.PrivateCopy; 9819 } else { 9820 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 9821 if (!isOpenMPCapturedDecl(D)) 9822 ExprCaptures.push_back(Ref->getDecl()); 9823 } 9824 if (TopDVar.CKind == OMPC_firstprivate || 9825 (!isOpenMPCapturedDecl(D) && 9826 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 9827 ExprResult RefRes = DefaultLvalueConversion(Ref); 9828 if (!RefRes.isUsable()) 9829 continue; 9830 ExprResult PostUpdateRes = 9831 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 9832 RefRes.get()); 9833 if (!PostUpdateRes.isUsable()) 9834 continue; 9835 ExprPostUpdates.push_back( 9836 IgnoredValueConversions(PostUpdateRes.get()).get()); 9837 } 9838 } 9839 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 9840 Vars.push_back((VD || CurContext->isDependentContext()) 9841 ? RefExpr->IgnoreParens() 9842 : Ref); 9843 SrcExprs.push_back(PseudoSrcExpr); 9844 DstExprs.push_back(PseudoDstExpr); 9845 AssignmentOps.push_back(AssignmentOp.get()); 9846 } 9847 9848 if (Vars.empty()) 9849 return nullptr; 9850 9851 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 9852 Vars, SrcExprs, DstExprs, AssignmentOps, 9853 buildPreInits(Context, ExprCaptures), 9854 buildPostUpdate(*this, ExprPostUpdates)); 9855 } 9856 9857 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 9858 SourceLocation StartLoc, 9859 SourceLocation LParenLoc, 9860 SourceLocation EndLoc) { 9861 SmallVector<Expr *, 8> Vars; 9862 for (Expr *RefExpr : VarList) { 9863 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 9864 SourceLocation ELoc; 9865 SourceRange ERange; 9866 Expr *SimpleRefExpr = RefExpr; 9867 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 9868 if (Res.second) { 9869 // It will be analyzed later. 9870 Vars.push_back(RefExpr); 9871 } 9872 ValueDecl *D = Res.first; 9873 if (!D) 9874 continue; 9875 9876 auto *VD = dyn_cast<VarDecl>(D); 9877 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 9878 // in a Construct] 9879 // Variables with the predetermined data-sharing attributes may not be 9880 // listed in data-sharing attributes clauses, except for the cases 9881 // listed below. For these exceptions only, listing a predetermined 9882 // variable in a data-sharing attribute clause is allowed and overrides 9883 // the variable's predetermined data-sharing attributes. 9884 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 9885 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 9886 DVar.RefExpr) { 9887 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 9888 << getOpenMPClauseName(OMPC_shared); 9889 reportOriginalDsa(*this, DSAStack, D, DVar); 9890 continue; 9891 } 9892 9893 DeclRefExpr *Ref = nullptr; 9894 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 9895 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 9896 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 9897 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 9898 ? RefExpr->IgnoreParens() 9899 : Ref); 9900 } 9901 9902 if (Vars.empty()) 9903 return nullptr; 9904 9905 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 9906 } 9907 9908 namespace { 9909 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 9910 DSAStackTy *Stack; 9911 9912 public: 9913 bool VisitDeclRefExpr(DeclRefExpr *E) { 9914 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 9915 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 9916 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 9917 return false; 9918 if (DVar.CKind != OMPC_unknown) 9919 return true; 9920 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 9921 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; }, 9922 /*FromParent=*/true); 9923 return DVarPrivate.CKind != OMPC_unknown; 9924 } 9925 return false; 9926 } 9927 bool VisitStmt(Stmt *S) { 9928 for (Stmt *Child : S->children()) { 9929 if (Child && Visit(Child)) 9930 return true; 9931 } 9932 return false; 9933 } 9934 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 9935 }; 9936 } // namespace 9937 9938 namespace { 9939 // Transform MemberExpression for specified FieldDecl of current class to 9940 // DeclRefExpr to specified OMPCapturedExprDecl. 9941 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 9942 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 9943 ValueDecl *Field = nullptr; 9944 DeclRefExpr *CapturedExpr = nullptr; 9945 9946 public: 9947 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 9948 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 9949 9950 ExprResult TransformMemberExpr(MemberExpr *E) { 9951 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 9952 E->getMemberDecl() == Field) { 9953 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 9954 return CapturedExpr; 9955 } 9956 return BaseTransform::TransformMemberExpr(E); 9957 } 9958 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 9959 }; 9960 } // namespace 9961 9962 template <typename T, typename U> 9963 static T filterLookupForUDR(SmallVectorImpl<U> &Lookups, 9964 const llvm::function_ref<T(ValueDecl *)> Gen) { 9965 for (U &Set : Lookups) { 9966 for (auto *D : Set) { 9967 if (T Res = Gen(cast<ValueDecl>(D))) 9968 return Res; 9969 } 9970 } 9971 return T(); 9972 } 9973 9974 static ExprResult 9975 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 9976 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 9977 const DeclarationNameInfo &ReductionId, QualType Ty, 9978 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 9979 if (ReductionIdScopeSpec.isInvalid()) 9980 return ExprError(); 9981 SmallVector<UnresolvedSet<8>, 4> Lookups; 9982 if (S) { 9983 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 9984 Lookup.suppressDiagnostics(); 9985 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 9986 NamedDecl *D = Lookup.getRepresentativeDecl(); 9987 do { 9988 S = S->getParent(); 9989 } while (S && !S->isDeclScope(D)); 9990 if (S) 9991 S = S->getParent(); 9992 Lookups.push_back(UnresolvedSet<8>()); 9993 Lookups.back().append(Lookup.begin(), Lookup.end()); 9994 Lookup.clear(); 9995 } 9996 } else if (auto *ULE = 9997 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 9998 Lookups.push_back(UnresolvedSet<8>()); 9999 Decl *PrevD = nullptr; 10000 for (NamedDecl *D : ULE->decls()) { 10001 if (D == PrevD) 10002 Lookups.push_back(UnresolvedSet<8>()); 10003 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D)) 10004 Lookups.back().addDecl(DRD); 10005 PrevD = D; 10006 } 10007 } 10008 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 10009 Ty->isInstantiationDependentType() || 10010 Ty->containsUnexpandedParameterPack() || 10011 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) { 10012 return !D->isInvalidDecl() && 10013 (D->getType()->isDependentType() || 10014 D->getType()->isInstantiationDependentType() || 10015 D->getType()->containsUnexpandedParameterPack()); 10016 })) { 10017 UnresolvedSet<8> ResSet; 10018 for (const UnresolvedSet<8> &Set : Lookups) { 10019 ResSet.append(Set.begin(), Set.end()); 10020 // The last item marks the end of all declarations at the specified scope. 10021 ResSet.addDecl(Set[Set.size() - 1]); 10022 } 10023 return UnresolvedLookupExpr::Create( 10024 SemaRef.Context, /*NamingClass=*/nullptr, 10025 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 10026 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 10027 } 10028 if (auto *VD = filterLookupForUDR<ValueDecl *>( 10029 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 10030 if (!D->isInvalidDecl() && 10031 SemaRef.Context.hasSameType(D->getType(), Ty)) 10032 return D; 10033 return nullptr; 10034 })) 10035 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc); 10036 if (auto *VD = filterLookupForUDR<ValueDecl *>( 10037 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 10038 if (!D->isInvalidDecl() && 10039 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 10040 !Ty.isMoreQualifiedThan(D->getType())) 10041 return D; 10042 return nullptr; 10043 })) { 10044 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 10045 /*DetectVirtual=*/false); 10046 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 10047 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 10048 VD->getType().getUnqualifiedType()))) { 10049 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(), 10050 /*DiagID=*/0) != 10051 Sema::AR_inaccessible) { 10052 SemaRef.BuildBasePathArray(Paths, BasePath); 10053 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc); 10054 } 10055 } 10056 } 10057 } 10058 if (ReductionIdScopeSpec.isSet()) { 10059 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range; 10060 return ExprError(); 10061 } 10062 return ExprEmpty(); 10063 } 10064 10065 namespace { 10066 /// Data for the reduction-based clauses. 10067 struct ReductionData { 10068 /// List of original reduction items. 10069 SmallVector<Expr *, 8> Vars; 10070 /// List of private copies of the reduction items. 10071 SmallVector<Expr *, 8> Privates; 10072 /// LHS expressions for the reduction_op expressions. 10073 SmallVector<Expr *, 8> LHSs; 10074 /// RHS expressions for the reduction_op expressions. 10075 SmallVector<Expr *, 8> RHSs; 10076 /// Reduction operation expression. 10077 SmallVector<Expr *, 8> ReductionOps; 10078 /// Taskgroup descriptors for the corresponding reduction items in 10079 /// in_reduction clauses. 10080 SmallVector<Expr *, 8> TaskgroupDescriptors; 10081 /// List of captures for clause. 10082 SmallVector<Decl *, 4> ExprCaptures; 10083 /// List of postupdate expressions. 10084 SmallVector<Expr *, 4> ExprPostUpdates; 10085 ReductionData() = delete; 10086 /// Reserves required memory for the reduction data. 10087 ReductionData(unsigned Size) { 10088 Vars.reserve(Size); 10089 Privates.reserve(Size); 10090 LHSs.reserve(Size); 10091 RHSs.reserve(Size); 10092 ReductionOps.reserve(Size); 10093 TaskgroupDescriptors.reserve(Size); 10094 ExprCaptures.reserve(Size); 10095 ExprPostUpdates.reserve(Size); 10096 } 10097 /// Stores reduction item and reduction operation only (required for dependent 10098 /// reduction item). 10099 void push(Expr *Item, Expr *ReductionOp) { 10100 Vars.emplace_back(Item); 10101 Privates.emplace_back(nullptr); 10102 LHSs.emplace_back(nullptr); 10103 RHSs.emplace_back(nullptr); 10104 ReductionOps.emplace_back(ReductionOp); 10105 TaskgroupDescriptors.emplace_back(nullptr); 10106 } 10107 /// Stores reduction data. 10108 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 10109 Expr *TaskgroupDescriptor) { 10110 Vars.emplace_back(Item); 10111 Privates.emplace_back(Private); 10112 LHSs.emplace_back(LHS); 10113 RHSs.emplace_back(RHS); 10114 ReductionOps.emplace_back(ReductionOp); 10115 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 10116 } 10117 }; 10118 } // namespace 10119 10120 static bool checkOMPArraySectionConstantForReduction( 10121 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 10122 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 10123 const Expr *Length = OASE->getLength(); 10124 if (Length == nullptr) { 10125 // For array sections of the form [1:] or [:], we would need to analyze 10126 // the lower bound... 10127 if (OASE->getColonLoc().isValid()) 10128 return false; 10129 10130 // This is an array subscript which has implicit length 1! 10131 SingleElement = true; 10132 ArraySizes.push_back(llvm::APSInt::get(1)); 10133 } else { 10134 llvm::APSInt ConstantLengthValue; 10135 if (!Length->EvaluateAsInt(ConstantLengthValue, Context)) 10136 return false; 10137 10138 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 10139 ArraySizes.push_back(ConstantLengthValue); 10140 } 10141 10142 // Get the base of this array section and walk up from there. 10143 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 10144 10145 // We require length = 1 for all array sections except the right-most to 10146 // guarantee that the memory region is contiguous and has no holes in it. 10147 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 10148 Length = TempOASE->getLength(); 10149 if (Length == nullptr) { 10150 // For array sections of the form [1:] or [:], we would need to analyze 10151 // the lower bound... 10152 if (OASE->getColonLoc().isValid()) 10153 return false; 10154 10155 // This is an array subscript which has implicit length 1! 10156 ArraySizes.push_back(llvm::APSInt::get(1)); 10157 } else { 10158 llvm::APSInt ConstantLengthValue; 10159 if (!Length->EvaluateAsInt(ConstantLengthValue, Context) || 10160 ConstantLengthValue.getSExtValue() != 1) 10161 return false; 10162 10163 ArraySizes.push_back(ConstantLengthValue); 10164 } 10165 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 10166 } 10167 10168 // If we have a single element, we don't need to add the implicit lengths. 10169 if (!SingleElement) { 10170 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 10171 // Has implicit length 1! 10172 ArraySizes.push_back(llvm::APSInt::get(1)); 10173 Base = TempASE->getBase()->IgnoreParenImpCasts(); 10174 } 10175 } 10176 10177 // This array section can be privatized as a single value or as a constant 10178 // sized array. 10179 return true; 10180 } 10181 10182 static bool actOnOMPReductionKindClause( 10183 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 10184 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 10185 SourceLocation ColonLoc, SourceLocation EndLoc, 10186 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 10187 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 10188 DeclarationName DN = ReductionId.getName(); 10189 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 10190 BinaryOperatorKind BOK = BO_Comma; 10191 10192 ASTContext &Context = S.Context; 10193 // OpenMP [2.14.3.6, reduction clause] 10194 // C 10195 // reduction-identifier is either an identifier or one of the following 10196 // operators: +, -, *, &, |, ^, && and || 10197 // C++ 10198 // reduction-identifier is either an id-expression or one of the following 10199 // operators: +, -, *, &, |, ^, && and || 10200 switch (OOK) { 10201 case OO_Plus: 10202 case OO_Minus: 10203 BOK = BO_Add; 10204 break; 10205 case OO_Star: 10206 BOK = BO_Mul; 10207 break; 10208 case OO_Amp: 10209 BOK = BO_And; 10210 break; 10211 case OO_Pipe: 10212 BOK = BO_Or; 10213 break; 10214 case OO_Caret: 10215 BOK = BO_Xor; 10216 break; 10217 case OO_AmpAmp: 10218 BOK = BO_LAnd; 10219 break; 10220 case OO_PipePipe: 10221 BOK = BO_LOr; 10222 break; 10223 case OO_New: 10224 case OO_Delete: 10225 case OO_Array_New: 10226 case OO_Array_Delete: 10227 case OO_Slash: 10228 case OO_Percent: 10229 case OO_Tilde: 10230 case OO_Exclaim: 10231 case OO_Equal: 10232 case OO_Less: 10233 case OO_Greater: 10234 case OO_LessEqual: 10235 case OO_GreaterEqual: 10236 case OO_PlusEqual: 10237 case OO_MinusEqual: 10238 case OO_StarEqual: 10239 case OO_SlashEqual: 10240 case OO_PercentEqual: 10241 case OO_CaretEqual: 10242 case OO_AmpEqual: 10243 case OO_PipeEqual: 10244 case OO_LessLess: 10245 case OO_GreaterGreater: 10246 case OO_LessLessEqual: 10247 case OO_GreaterGreaterEqual: 10248 case OO_EqualEqual: 10249 case OO_ExclaimEqual: 10250 case OO_Spaceship: 10251 case OO_PlusPlus: 10252 case OO_MinusMinus: 10253 case OO_Comma: 10254 case OO_ArrowStar: 10255 case OO_Arrow: 10256 case OO_Call: 10257 case OO_Subscript: 10258 case OO_Conditional: 10259 case OO_Coawait: 10260 case NUM_OVERLOADED_OPERATORS: 10261 llvm_unreachable("Unexpected reduction identifier"); 10262 case OO_None: 10263 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 10264 if (II->isStr("max")) 10265 BOK = BO_GT; 10266 else if (II->isStr("min")) 10267 BOK = BO_LT; 10268 } 10269 break; 10270 } 10271 SourceRange ReductionIdRange; 10272 if (ReductionIdScopeSpec.isValid()) 10273 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 10274 else 10275 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 10276 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 10277 10278 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 10279 bool FirstIter = true; 10280 for (Expr *RefExpr : VarList) { 10281 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 10282 // OpenMP [2.1, C/C++] 10283 // A list item is a variable or array section, subject to the restrictions 10284 // specified in Section 2.4 on page 42 and in each of the sections 10285 // describing clauses and directives for which a list appears. 10286 // OpenMP [2.14.3.3, Restrictions, p.1] 10287 // A variable that is part of another variable (as an array or 10288 // structure element) cannot appear in a private clause. 10289 if (!FirstIter && IR != ER) 10290 ++IR; 10291 FirstIter = false; 10292 SourceLocation ELoc; 10293 SourceRange ERange; 10294 Expr *SimpleRefExpr = RefExpr; 10295 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 10296 /*AllowArraySection=*/true); 10297 if (Res.second) { 10298 // Try to find 'declare reduction' corresponding construct before using 10299 // builtin/overloaded operators. 10300 QualType Type = Context.DependentTy; 10301 CXXCastPath BasePath; 10302 ExprResult DeclareReductionRef = buildDeclareReductionRef( 10303 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 10304 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 10305 Expr *ReductionOp = nullptr; 10306 if (S.CurContext->isDependentContext() && 10307 (DeclareReductionRef.isUnset() || 10308 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 10309 ReductionOp = DeclareReductionRef.get(); 10310 // It will be analyzed later. 10311 RD.push(RefExpr, ReductionOp); 10312 } 10313 ValueDecl *D = Res.first; 10314 if (!D) 10315 continue; 10316 10317 Expr *TaskgroupDescriptor = nullptr; 10318 QualType Type; 10319 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 10320 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 10321 if (ASE) { 10322 Type = ASE->getType().getNonReferenceType(); 10323 } else if (OASE) { 10324 QualType BaseType = 10325 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 10326 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 10327 Type = ATy->getElementType(); 10328 else 10329 Type = BaseType->getPointeeType(); 10330 Type = Type.getNonReferenceType(); 10331 } else { 10332 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 10333 } 10334 auto *VD = dyn_cast<VarDecl>(D); 10335 10336 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 10337 // A variable that appears in a private clause must not have an incomplete 10338 // type or a reference type. 10339 if (S.RequireCompleteType(ELoc, D->getType(), 10340 diag::err_omp_reduction_incomplete_type)) 10341 continue; 10342 // OpenMP [2.14.3.6, reduction clause, Restrictions] 10343 // A list item that appears in a reduction clause must not be 10344 // const-qualified. 10345 if (Type.getNonReferenceType().isConstant(Context)) { 10346 S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange; 10347 if (!ASE && !OASE) { 10348 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 10349 VarDecl::DeclarationOnly; 10350 S.Diag(D->getLocation(), 10351 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10352 << D; 10353 } 10354 continue; 10355 } 10356 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 10357 // If a list-item is a reference type then it must bind to the same object 10358 // for all threads of the team. 10359 if (!ASE && !OASE && VD) { 10360 VarDecl *VDDef = VD->getDefinition(); 10361 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 10362 DSARefChecker Check(Stack); 10363 if (Check.Visit(VDDef->getInit())) { 10364 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 10365 << getOpenMPClauseName(ClauseKind) << ERange; 10366 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 10367 continue; 10368 } 10369 } 10370 } 10371 10372 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 10373 // in a Construct] 10374 // Variables with the predetermined data-sharing attributes may not be 10375 // listed in data-sharing attributes clauses, except for the cases 10376 // listed below. For these exceptions only, listing a predetermined 10377 // variable in a data-sharing attribute clause is allowed and overrides 10378 // the variable's predetermined data-sharing attributes. 10379 // OpenMP [2.14.3.6, Restrictions, p.3] 10380 // Any number of reduction clauses can be specified on the directive, 10381 // but a list item can appear only once in the reduction clauses for that 10382 // directive. 10383 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 10384 if (DVar.CKind == OMPC_reduction) { 10385 S.Diag(ELoc, diag::err_omp_once_referenced) 10386 << getOpenMPClauseName(ClauseKind); 10387 if (DVar.RefExpr) 10388 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 10389 continue; 10390 } 10391 if (DVar.CKind != OMPC_unknown) { 10392 S.Diag(ELoc, diag::err_omp_wrong_dsa) 10393 << getOpenMPClauseName(DVar.CKind) 10394 << getOpenMPClauseName(OMPC_reduction); 10395 reportOriginalDsa(S, Stack, D, DVar); 10396 continue; 10397 } 10398 10399 // OpenMP [2.14.3.6, Restrictions, p.1] 10400 // A list item that appears in a reduction clause of a worksharing 10401 // construct must be shared in the parallel regions to which any of the 10402 // worksharing regions arising from the worksharing construct bind. 10403 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 10404 if (isOpenMPWorksharingDirective(CurrDir) && 10405 !isOpenMPParallelDirective(CurrDir) && 10406 !isOpenMPTeamsDirective(CurrDir)) { 10407 DVar = Stack->getImplicitDSA(D, true); 10408 if (DVar.CKind != OMPC_shared) { 10409 S.Diag(ELoc, diag::err_omp_required_access) 10410 << getOpenMPClauseName(OMPC_reduction) 10411 << getOpenMPClauseName(OMPC_shared); 10412 reportOriginalDsa(S, Stack, D, DVar); 10413 continue; 10414 } 10415 } 10416 10417 // Try to find 'declare reduction' corresponding construct before using 10418 // builtin/overloaded operators. 10419 CXXCastPath BasePath; 10420 ExprResult DeclareReductionRef = buildDeclareReductionRef( 10421 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 10422 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 10423 if (DeclareReductionRef.isInvalid()) 10424 continue; 10425 if (S.CurContext->isDependentContext() && 10426 (DeclareReductionRef.isUnset() || 10427 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 10428 RD.push(RefExpr, DeclareReductionRef.get()); 10429 continue; 10430 } 10431 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 10432 // Not allowed reduction identifier is found. 10433 S.Diag(ReductionId.getLocStart(), 10434 diag::err_omp_unknown_reduction_identifier) 10435 << Type << ReductionIdRange; 10436 continue; 10437 } 10438 10439 // OpenMP [2.14.3.6, reduction clause, Restrictions] 10440 // The type of a list item that appears in a reduction clause must be valid 10441 // for the reduction-identifier. For a max or min reduction in C, the type 10442 // of the list item must be an allowed arithmetic data type: char, int, 10443 // float, double, or _Bool, possibly modified with long, short, signed, or 10444 // unsigned. For a max or min reduction in C++, the type of the list item 10445 // must be an allowed arithmetic data type: char, wchar_t, int, float, 10446 // double, or bool, possibly modified with long, short, signed, or unsigned. 10447 if (DeclareReductionRef.isUnset()) { 10448 if ((BOK == BO_GT || BOK == BO_LT) && 10449 !(Type->isScalarType() || 10450 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 10451 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 10452 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 10453 if (!ASE && !OASE) { 10454 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 10455 VarDecl::DeclarationOnly; 10456 S.Diag(D->getLocation(), 10457 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10458 << D; 10459 } 10460 continue; 10461 } 10462 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 10463 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 10464 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 10465 << getOpenMPClauseName(ClauseKind); 10466 if (!ASE && !OASE) { 10467 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 10468 VarDecl::DeclarationOnly; 10469 S.Diag(D->getLocation(), 10470 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10471 << D; 10472 } 10473 continue; 10474 } 10475 } 10476 10477 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 10478 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 10479 D->hasAttrs() ? &D->getAttrs() : nullptr); 10480 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 10481 D->hasAttrs() ? &D->getAttrs() : nullptr); 10482 QualType PrivateTy = Type; 10483 10484 // Try if we can determine constant lengths for all array sections and avoid 10485 // the VLA. 10486 bool ConstantLengthOASE = false; 10487 if (OASE) { 10488 bool SingleElement; 10489 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 10490 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 10491 Context, OASE, SingleElement, ArraySizes); 10492 10493 // If we don't have a single element, we must emit a constant array type. 10494 if (ConstantLengthOASE && !SingleElement) { 10495 for (llvm::APSInt &Size : ArraySizes) 10496 PrivateTy = Context.getConstantArrayType( 10497 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0); 10498 } 10499 } 10500 10501 if ((OASE && !ConstantLengthOASE) || 10502 (!OASE && !ASE && 10503 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 10504 if (!Context.getTargetInfo().isVLASupported() && 10505 S.shouldDiagnoseTargetSupportFromOpenMP()) { 10506 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 10507 S.Diag(ELoc, diag::note_vla_unsupported); 10508 continue; 10509 } 10510 // For arrays/array sections only: 10511 // Create pseudo array type for private copy. The size for this array will 10512 // be generated during codegen. 10513 // For array subscripts or single variables Private Ty is the same as Type 10514 // (type of the variable or single array element). 10515 PrivateTy = Context.getVariableArrayType( 10516 Type, 10517 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue), 10518 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 10519 } else if (!ASE && !OASE && 10520 Context.getAsArrayType(D->getType().getNonReferenceType())) { 10521 PrivateTy = D->getType().getNonReferenceType(); 10522 } 10523 // Private copy. 10524 VarDecl *PrivateVD = 10525 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 10526 D->hasAttrs() ? &D->getAttrs() : nullptr, 10527 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 10528 // Add initializer for private variable. 10529 Expr *Init = nullptr; 10530 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 10531 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 10532 if (DeclareReductionRef.isUsable()) { 10533 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 10534 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 10535 if (DRD->getInitializer()) { 10536 Init = DRDRef; 10537 RHSVD->setInit(DRDRef); 10538 RHSVD->setInitStyle(VarDecl::CallInit); 10539 } 10540 } else { 10541 switch (BOK) { 10542 case BO_Add: 10543 case BO_Xor: 10544 case BO_Or: 10545 case BO_LOr: 10546 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 10547 if (Type->isScalarType() || Type->isAnyComplexType()) 10548 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 10549 break; 10550 case BO_Mul: 10551 case BO_LAnd: 10552 if (Type->isScalarType() || Type->isAnyComplexType()) { 10553 // '*' and '&&' reduction ops - initializer is '1'. 10554 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 10555 } 10556 break; 10557 case BO_And: { 10558 // '&' reduction op - initializer is '~0'. 10559 QualType OrigType = Type; 10560 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 10561 Type = ComplexTy->getElementType(); 10562 if (Type->isRealFloatingType()) { 10563 llvm::APFloat InitValue = 10564 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type), 10565 /*isIEEE=*/true); 10566 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 10567 Type, ELoc); 10568 } else if (Type->isScalarType()) { 10569 uint64_t Size = Context.getTypeSize(Type); 10570 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 10571 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 10572 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 10573 } 10574 if (Init && OrigType->isAnyComplexType()) { 10575 // Init = 0xFFFF + 0xFFFFi; 10576 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 10577 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 10578 } 10579 Type = OrigType; 10580 break; 10581 } 10582 case BO_LT: 10583 case BO_GT: { 10584 // 'min' reduction op - initializer is 'Largest representable number in 10585 // the reduction list item type'. 10586 // 'max' reduction op - initializer is 'Least representable number in 10587 // the reduction list item type'. 10588 if (Type->isIntegerType() || Type->isPointerType()) { 10589 bool IsSigned = Type->hasSignedIntegerRepresentation(); 10590 uint64_t Size = Context.getTypeSize(Type); 10591 QualType IntTy = 10592 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 10593 llvm::APInt InitValue = 10594 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 10595 : llvm::APInt::getMinValue(Size) 10596 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 10597 : llvm::APInt::getMaxValue(Size); 10598 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 10599 if (Type->isPointerType()) { 10600 // Cast to pointer type. 10601 ExprResult CastExpr = S.BuildCStyleCastExpr( 10602 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 10603 if (CastExpr.isInvalid()) 10604 continue; 10605 Init = CastExpr.get(); 10606 } 10607 } else if (Type->isRealFloatingType()) { 10608 llvm::APFloat InitValue = llvm::APFloat::getLargest( 10609 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 10610 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 10611 Type, ELoc); 10612 } 10613 break; 10614 } 10615 case BO_PtrMemD: 10616 case BO_PtrMemI: 10617 case BO_MulAssign: 10618 case BO_Div: 10619 case BO_Rem: 10620 case BO_Sub: 10621 case BO_Shl: 10622 case BO_Shr: 10623 case BO_LE: 10624 case BO_GE: 10625 case BO_EQ: 10626 case BO_NE: 10627 case BO_Cmp: 10628 case BO_AndAssign: 10629 case BO_XorAssign: 10630 case BO_OrAssign: 10631 case BO_Assign: 10632 case BO_AddAssign: 10633 case BO_SubAssign: 10634 case BO_DivAssign: 10635 case BO_RemAssign: 10636 case BO_ShlAssign: 10637 case BO_ShrAssign: 10638 case BO_Comma: 10639 llvm_unreachable("Unexpected reduction operation"); 10640 } 10641 } 10642 if (Init && DeclareReductionRef.isUnset()) 10643 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 10644 else if (!Init) 10645 S.ActOnUninitializedDecl(RHSVD); 10646 if (RHSVD->isInvalidDecl()) 10647 continue; 10648 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) { 10649 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 10650 << Type << ReductionIdRange; 10651 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 10652 VarDecl::DeclarationOnly; 10653 S.Diag(D->getLocation(), 10654 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10655 << D; 10656 continue; 10657 } 10658 // Store initializer for single element in private copy. Will be used during 10659 // codegen. 10660 PrivateVD->setInit(RHSVD->getInit()); 10661 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 10662 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 10663 ExprResult ReductionOp; 10664 if (DeclareReductionRef.isUsable()) { 10665 QualType RedTy = DeclareReductionRef.get()->getType(); 10666 QualType PtrRedTy = Context.getPointerType(RedTy); 10667 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 10668 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 10669 if (!BasePath.empty()) { 10670 LHS = S.DefaultLvalueConversion(LHS.get()); 10671 RHS = S.DefaultLvalueConversion(RHS.get()); 10672 LHS = ImplicitCastExpr::Create(Context, PtrRedTy, 10673 CK_UncheckedDerivedToBase, LHS.get(), 10674 &BasePath, LHS.get()->getValueKind()); 10675 RHS = ImplicitCastExpr::Create(Context, PtrRedTy, 10676 CK_UncheckedDerivedToBase, RHS.get(), 10677 &BasePath, RHS.get()->getValueKind()); 10678 } 10679 FunctionProtoType::ExtProtoInfo EPI; 10680 QualType Params[] = {PtrRedTy, PtrRedTy}; 10681 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 10682 auto *OVE = new (Context) OpaqueValueExpr( 10683 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, 10684 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 10685 Expr *Args[] = {LHS.get(), RHS.get()}; 10686 ReductionOp = new (Context) 10687 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc); 10688 } else { 10689 ReductionOp = S.BuildBinOp( 10690 Stack->getCurScope(), ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE); 10691 if (ReductionOp.isUsable()) { 10692 if (BOK != BO_LT && BOK != BO_GT) { 10693 ReductionOp = 10694 S.BuildBinOp(Stack->getCurScope(), ReductionId.getLocStart(), 10695 BO_Assign, LHSDRE, ReductionOp.get()); 10696 } else { 10697 auto *ConditionalOp = new (Context) 10698 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE, 10699 Type, VK_LValue, OK_Ordinary); 10700 ReductionOp = 10701 S.BuildBinOp(Stack->getCurScope(), ReductionId.getLocStart(), 10702 BO_Assign, LHSDRE, ConditionalOp); 10703 } 10704 if (ReductionOp.isUsable()) 10705 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get()); 10706 } 10707 if (!ReductionOp.isUsable()) 10708 continue; 10709 } 10710 10711 // OpenMP [2.15.4.6, Restrictions, p.2] 10712 // A list item that appears in an in_reduction clause of a task construct 10713 // must appear in a task_reduction clause of a construct associated with a 10714 // taskgroup region that includes the participating task in its taskgroup 10715 // set. The construct associated with the innermost region that meets this 10716 // condition must specify the same reduction-identifier as the in_reduction 10717 // clause. 10718 if (ClauseKind == OMPC_in_reduction) { 10719 SourceRange ParentSR; 10720 BinaryOperatorKind ParentBOK; 10721 const Expr *ParentReductionOp; 10722 Expr *ParentBOKTD, *ParentReductionOpTD; 10723 DSAStackTy::DSAVarData ParentBOKDSA = 10724 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 10725 ParentBOKTD); 10726 DSAStackTy::DSAVarData ParentReductionOpDSA = 10727 Stack->getTopMostTaskgroupReductionData( 10728 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 10729 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 10730 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 10731 if (!IsParentBOK && !IsParentReductionOp) { 10732 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction); 10733 continue; 10734 } 10735 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 10736 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK || 10737 IsParentReductionOp) { 10738 bool EmitError = true; 10739 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 10740 llvm::FoldingSetNodeID RedId, ParentRedId; 10741 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 10742 DeclareReductionRef.get()->Profile(RedId, Context, 10743 /*Canonical=*/true); 10744 EmitError = RedId != ParentRedId; 10745 } 10746 if (EmitError) { 10747 S.Diag(ReductionId.getLocStart(), 10748 diag::err_omp_reduction_identifier_mismatch) 10749 << ReductionIdRange << RefExpr->getSourceRange(); 10750 S.Diag(ParentSR.getBegin(), 10751 diag::note_omp_previous_reduction_identifier) 10752 << ParentSR 10753 << (IsParentBOK ? ParentBOKDSA.RefExpr 10754 : ParentReductionOpDSA.RefExpr) 10755 ->getSourceRange(); 10756 continue; 10757 } 10758 } 10759 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 10760 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined."); 10761 } 10762 10763 DeclRefExpr *Ref = nullptr; 10764 Expr *VarsExpr = RefExpr->IgnoreParens(); 10765 if (!VD && !S.CurContext->isDependentContext()) { 10766 if (ASE || OASE) { 10767 TransformExprToCaptures RebuildToCapture(S, D); 10768 VarsExpr = 10769 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 10770 Ref = RebuildToCapture.getCapturedExpr(); 10771 } else { 10772 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 10773 } 10774 if (!S.isOpenMPCapturedDecl(D)) { 10775 RD.ExprCaptures.emplace_back(Ref->getDecl()); 10776 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 10777 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 10778 if (!RefRes.isUsable()) 10779 continue; 10780 ExprResult PostUpdateRes = 10781 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 10782 RefRes.get()); 10783 if (!PostUpdateRes.isUsable()) 10784 continue; 10785 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 10786 Stack->getCurrentDirective() == OMPD_taskgroup) { 10787 S.Diag(RefExpr->getExprLoc(), 10788 diag::err_omp_reduction_non_addressable_expression) 10789 << RefExpr->getSourceRange(); 10790 continue; 10791 } 10792 RD.ExprPostUpdates.emplace_back( 10793 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 10794 } 10795 } 10796 } 10797 // All reduction items are still marked as reduction (to do not increase 10798 // code base size). 10799 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref); 10800 if (CurrDir == OMPD_taskgroup) { 10801 if (DeclareReductionRef.isUsable()) 10802 Stack->addTaskgroupReductionData(D, ReductionIdRange, 10803 DeclareReductionRef.get()); 10804 else 10805 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 10806 } 10807 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 10808 TaskgroupDescriptor); 10809 } 10810 return RD.Vars.empty(); 10811 } 10812 10813 OMPClause *Sema::ActOnOpenMPReductionClause( 10814 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 10815 SourceLocation ColonLoc, SourceLocation EndLoc, 10816 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 10817 ArrayRef<Expr *> UnresolvedReductions) { 10818 ReductionData RD(VarList.size()); 10819 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 10820 StartLoc, LParenLoc, ColonLoc, EndLoc, 10821 ReductionIdScopeSpec, ReductionId, 10822 UnresolvedReductions, RD)) 10823 return nullptr; 10824 10825 return OMPReductionClause::Create( 10826 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 10827 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 10828 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 10829 buildPreInits(Context, RD.ExprCaptures), 10830 buildPostUpdate(*this, RD.ExprPostUpdates)); 10831 } 10832 10833 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 10834 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 10835 SourceLocation ColonLoc, SourceLocation EndLoc, 10836 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 10837 ArrayRef<Expr *> UnresolvedReductions) { 10838 ReductionData RD(VarList.size()); 10839 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 10840 StartLoc, LParenLoc, ColonLoc, EndLoc, 10841 ReductionIdScopeSpec, ReductionId, 10842 UnresolvedReductions, RD)) 10843 return nullptr; 10844 10845 return OMPTaskReductionClause::Create( 10846 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 10847 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 10848 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 10849 buildPreInits(Context, RD.ExprCaptures), 10850 buildPostUpdate(*this, RD.ExprPostUpdates)); 10851 } 10852 10853 OMPClause *Sema::ActOnOpenMPInReductionClause( 10854 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 10855 SourceLocation ColonLoc, SourceLocation EndLoc, 10856 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 10857 ArrayRef<Expr *> UnresolvedReductions) { 10858 ReductionData RD(VarList.size()); 10859 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 10860 StartLoc, LParenLoc, ColonLoc, EndLoc, 10861 ReductionIdScopeSpec, ReductionId, 10862 UnresolvedReductions, RD)) 10863 return nullptr; 10864 10865 return OMPInReductionClause::Create( 10866 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 10867 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 10868 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 10869 buildPreInits(Context, RD.ExprCaptures), 10870 buildPostUpdate(*this, RD.ExprPostUpdates)); 10871 } 10872 10873 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 10874 SourceLocation LinLoc) { 10875 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 10876 LinKind == OMPC_LINEAR_unknown) { 10877 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 10878 return true; 10879 } 10880 return false; 10881 } 10882 10883 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 10884 OpenMPLinearClauseKind LinKind, 10885 QualType Type) { 10886 const auto *VD = dyn_cast_or_null<VarDecl>(D); 10887 // A variable must not have an incomplete type or a reference type. 10888 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 10889 return true; 10890 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 10891 !Type->isReferenceType()) { 10892 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 10893 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 10894 return true; 10895 } 10896 Type = Type.getNonReferenceType(); 10897 10898 // A list item must not be const-qualified. 10899 if (Type.isConstant(Context)) { 10900 Diag(ELoc, diag::err_omp_const_variable) 10901 << getOpenMPClauseName(OMPC_linear); 10902 if (D) { 10903 bool IsDecl = 10904 !VD || 10905 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 10906 Diag(D->getLocation(), 10907 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10908 << D; 10909 } 10910 return true; 10911 } 10912 10913 // A list item must be of integral or pointer type. 10914 Type = Type.getUnqualifiedType().getCanonicalType(); 10915 const auto *Ty = Type.getTypePtrOrNull(); 10916 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && 10917 !Ty->isPointerType())) { 10918 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 10919 if (D) { 10920 bool IsDecl = 10921 !VD || 10922 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 10923 Diag(D->getLocation(), 10924 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10925 << D; 10926 } 10927 return true; 10928 } 10929 return false; 10930 } 10931 10932 OMPClause *Sema::ActOnOpenMPLinearClause( 10933 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 10934 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 10935 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 10936 SmallVector<Expr *, 8> Vars; 10937 SmallVector<Expr *, 8> Privates; 10938 SmallVector<Expr *, 8> Inits; 10939 SmallVector<Decl *, 4> ExprCaptures; 10940 SmallVector<Expr *, 4> ExprPostUpdates; 10941 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 10942 LinKind = OMPC_LINEAR_val; 10943 for (Expr *RefExpr : VarList) { 10944 assert(RefExpr && "NULL expr in OpenMP linear clause."); 10945 SourceLocation ELoc; 10946 SourceRange ERange; 10947 Expr *SimpleRefExpr = RefExpr; 10948 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 10949 /*AllowArraySection=*/false); 10950 if (Res.second) { 10951 // It will be analyzed later. 10952 Vars.push_back(RefExpr); 10953 Privates.push_back(nullptr); 10954 Inits.push_back(nullptr); 10955 } 10956 ValueDecl *D = Res.first; 10957 if (!D) 10958 continue; 10959 10960 QualType Type = D->getType(); 10961 auto *VD = dyn_cast<VarDecl>(D); 10962 10963 // OpenMP [2.14.3.7, linear clause] 10964 // A list-item cannot appear in more than one linear clause. 10965 // A list-item that appears in a linear clause cannot appear in any 10966 // other data-sharing attribute clause. 10967 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 10968 if (DVar.RefExpr) { 10969 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 10970 << getOpenMPClauseName(OMPC_linear); 10971 reportOriginalDsa(*this, DSAStack, D, DVar); 10972 continue; 10973 } 10974 10975 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 10976 continue; 10977 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 10978 10979 // Build private copy of original var. 10980 VarDecl *Private = 10981 buildVarDecl(*this, ELoc, Type, D->getName(), 10982 D->hasAttrs() ? &D->getAttrs() : nullptr, 10983 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 10984 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 10985 // Build var to save initial value. 10986 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 10987 Expr *InitExpr; 10988 DeclRefExpr *Ref = nullptr; 10989 if (!VD && !CurContext->isDependentContext()) { 10990 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 10991 if (!isOpenMPCapturedDecl(D)) { 10992 ExprCaptures.push_back(Ref->getDecl()); 10993 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 10994 ExprResult RefRes = DefaultLvalueConversion(Ref); 10995 if (!RefRes.isUsable()) 10996 continue; 10997 ExprResult PostUpdateRes = 10998 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 10999 SimpleRefExpr, RefRes.get()); 11000 if (!PostUpdateRes.isUsable()) 11001 continue; 11002 ExprPostUpdates.push_back( 11003 IgnoredValueConversions(PostUpdateRes.get()).get()); 11004 } 11005 } 11006 } 11007 if (LinKind == OMPC_LINEAR_uval) 11008 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 11009 else 11010 InitExpr = VD ? SimpleRefExpr : Ref; 11011 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 11012 /*DirectInit=*/false); 11013 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 11014 11015 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 11016 Vars.push_back((VD || CurContext->isDependentContext()) 11017 ? RefExpr->IgnoreParens() 11018 : Ref); 11019 Privates.push_back(PrivateRef); 11020 Inits.push_back(InitRef); 11021 } 11022 11023 if (Vars.empty()) 11024 return nullptr; 11025 11026 Expr *StepExpr = Step; 11027 Expr *CalcStepExpr = nullptr; 11028 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 11029 !Step->isInstantiationDependent() && 11030 !Step->containsUnexpandedParameterPack()) { 11031 SourceLocation StepLoc = Step->getLocStart(); 11032 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 11033 if (Val.isInvalid()) 11034 return nullptr; 11035 StepExpr = Val.get(); 11036 11037 // Build var to save the step value. 11038 VarDecl *SaveVar = 11039 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 11040 ExprResult SaveRef = 11041 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 11042 ExprResult CalcStep = 11043 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 11044 CalcStep = ActOnFinishFullExpr(CalcStep.get()); 11045 11046 // Warn about zero linear step (it would be probably better specified as 11047 // making corresponding variables 'const'). 11048 llvm::APSInt Result; 11049 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); 11050 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) 11051 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] 11052 << (Vars.size() > 1); 11053 if (!IsConstant && CalcStep.isUsable()) { 11054 // Calculate the step beforehand instead of doing this on each iteration. 11055 // (This is not used if the number of iterations may be kfold-ed). 11056 CalcStepExpr = CalcStep.get(); 11057 } 11058 } 11059 11060 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 11061 ColonLoc, EndLoc, Vars, Privates, Inits, 11062 StepExpr, CalcStepExpr, 11063 buildPreInits(Context, ExprCaptures), 11064 buildPostUpdate(*this, ExprPostUpdates)); 11065 } 11066 11067 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 11068 Expr *NumIterations, Sema &SemaRef, 11069 Scope *S, DSAStackTy *Stack) { 11070 // Walk the vars and build update/final expressions for the CodeGen. 11071 SmallVector<Expr *, 8> Updates; 11072 SmallVector<Expr *, 8> Finals; 11073 Expr *Step = Clause.getStep(); 11074 Expr *CalcStep = Clause.getCalcStep(); 11075 // OpenMP [2.14.3.7, linear clause] 11076 // If linear-step is not specified it is assumed to be 1. 11077 if (!Step) 11078 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 11079 else if (CalcStep) 11080 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 11081 bool HasErrors = false; 11082 auto CurInit = Clause.inits().begin(); 11083 auto CurPrivate = Clause.privates().begin(); 11084 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 11085 for (Expr *RefExpr : Clause.varlists()) { 11086 SourceLocation ELoc; 11087 SourceRange ERange; 11088 Expr *SimpleRefExpr = RefExpr; 11089 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange, 11090 /*AllowArraySection=*/false); 11091 ValueDecl *D = Res.first; 11092 if (Res.second || !D) { 11093 Updates.push_back(nullptr); 11094 Finals.push_back(nullptr); 11095 HasErrors = true; 11096 continue; 11097 } 11098 auto &&Info = Stack->isLoopControlVariable(D); 11099 // OpenMP [2.15.11, distribute simd Construct] 11100 // A list item may not appear in a linear clause, unless it is the loop 11101 // iteration variable. 11102 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 11103 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 11104 SemaRef.Diag(ELoc, 11105 diag::err_omp_linear_distribute_var_non_loop_iteration); 11106 Updates.push_back(nullptr); 11107 Finals.push_back(nullptr); 11108 HasErrors = true; 11109 continue; 11110 } 11111 Expr *InitExpr = *CurInit; 11112 11113 // Build privatized reference to the current linear var. 11114 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 11115 Expr *CapturedRef; 11116 if (LinKind == OMPC_LINEAR_uval) 11117 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 11118 else 11119 CapturedRef = 11120 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 11121 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 11122 /*RefersToCapture=*/true); 11123 11124 // Build update: Var = InitExpr + IV * Step 11125 ExprResult Update; 11126 if (!Info.first) 11127 Update = 11128 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, 11129 InitExpr, IV, Step, /* Subtract */ false); 11130 else 11131 Update = *CurPrivate; 11132 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(), 11133 /*DiscardedValue=*/true); 11134 11135 // Build final: Var = InitExpr + NumIterations * Step 11136 ExprResult Final; 11137 if (!Info.first) 11138 Final = 11139 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 11140 InitExpr, NumIterations, Step, /*Subtract=*/false); 11141 else 11142 Final = *CurPrivate; 11143 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(), 11144 /*DiscardedValue=*/true); 11145 11146 if (!Update.isUsable() || !Final.isUsable()) { 11147 Updates.push_back(nullptr); 11148 Finals.push_back(nullptr); 11149 HasErrors = true; 11150 } else { 11151 Updates.push_back(Update.get()); 11152 Finals.push_back(Final.get()); 11153 } 11154 ++CurInit; 11155 ++CurPrivate; 11156 } 11157 Clause.setUpdates(Updates); 11158 Clause.setFinals(Finals); 11159 return HasErrors; 11160 } 11161 11162 OMPClause *Sema::ActOnOpenMPAlignedClause( 11163 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 11164 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 11165 SmallVector<Expr *, 8> Vars; 11166 for (Expr *RefExpr : VarList) { 11167 assert(RefExpr && "NULL expr in OpenMP linear clause."); 11168 SourceLocation ELoc; 11169 SourceRange ERange; 11170 Expr *SimpleRefExpr = RefExpr; 11171 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 11172 /*AllowArraySection=*/false); 11173 if (Res.second) { 11174 // It will be analyzed later. 11175 Vars.push_back(RefExpr); 11176 } 11177 ValueDecl *D = Res.first; 11178 if (!D) 11179 continue; 11180 11181 QualType QType = D->getType(); 11182 auto *VD = dyn_cast<VarDecl>(D); 11183 11184 // OpenMP [2.8.1, simd construct, Restrictions] 11185 // The type of list items appearing in the aligned clause must be 11186 // array, pointer, reference to array, or reference to pointer. 11187 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 11188 const Type *Ty = QType.getTypePtrOrNull(); 11189 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 11190 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 11191 << QType << getLangOpts().CPlusPlus << ERange; 11192 bool IsDecl = 11193 !VD || 11194 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 11195 Diag(D->getLocation(), 11196 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11197 << D; 11198 continue; 11199 } 11200 11201 // OpenMP [2.8.1, simd construct, Restrictions] 11202 // A list-item cannot appear in more than one aligned clause. 11203 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 11204 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange; 11205 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 11206 << getOpenMPClauseName(OMPC_aligned); 11207 continue; 11208 } 11209 11210 DeclRefExpr *Ref = nullptr; 11211 if (!VD && isOpenMPCapturedDecl(D)) 11212 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 11213 Vars.push_back(DefaultFunctionArrayConversion( 11214 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 11215 .get()); 11216 } 11217 11218 // OpenMP [2.8.1, simd construct, Description] 11219 // The parameter of the aligned clause, alignment, must be a constant 11220 // positive integer expression. 11221 // If no optional parameter is specified, implementation-defined default 11222 // alignments for SIMD instructions on the target platforms are assumed. 11223 if (Alignment != nullptr) { 11224 ExprResult AlignResult = 11225 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 11226 if (AlignResult.isInvalid()) 11227 return nullptr; 11228 Alignment = AlignResult.get(); 11229 } 11230 if (Vars.empty()) 11231 return nullptr; 11232 11233 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 11234 EndLoc, Vars, Alignment); 11235 } 11236 11237 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 11238 SourceLocation StartLoc, 11239 SourceLocation LParenLoc, 11240 SourceLocation EndLoc) { 11241 SmallVector<Expr *, 8> Vars; 11242 SmallVector<Expr *, 8> SrcExprs; 11243 SmallVector<Expr *, 8> DstExprs; 11244 SmallVector<Expr *, 8> AssignmentOps; 11245 for (Expr *RefExpr : VarList) { 11246 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 11247 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 11248 // It will be analyzed later. 11249 Vars.push_back(RefExpr); 11250 SrcExprs.push_back(nullptr); 11251 DstExprs.push_back(nullptr); 11252 AssignmentOps.push_back(nullptr); 11253 continue; 11254 } 11255 11256 SourceLocation ELoc = RefExpr->getExprLoc(); 11257 // OpenMP [2.1, C/C++] 11258 // A list item is a variable name. 11259 // OpenMP [2.14.4.1, Restrictions, p.1] 11260 // A list item that appears in a copyin clause must be threadprivate. 11261 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 11262 if (!DE || !isa<VarDecl>(DE->getDecl())) { 11263 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 11264 << 0 << RefExpr->getSourceRange(); 11265 continue; 11266 } 11267 11268 Decl *D = DE->getDecl(); 11269 auto *VD = cast<VarDecl>(D); 11270 11271 QualType Type = VD->getType(); 11272 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 11273 // It will be analyzed later. 11274 Vars.push_back(DE); 11275 SrcExprs.push_back(nullptr); 11276 DstExprs.push_back(nullptr); 11277 AssignmentOps.push_back(nullptr); 11278 continue; 11279 } 11280 11281 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 11282 // A list item that appears in a copyin clause must be threadprivate. 11283 if (!DSAStack->isThreadPrivate(VD)) { 11284 Diag(ELoc, diag::err_omp_required_access) 11285 << getOpenMPClauseName(OMPC_copyin) 11286 << getOpenMPDirectiveName(OMPD_threadprivate); 11287 continue; 11288 } 11289 11290 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 11291 // A variable of class type (or array thereof) that appears in a 11292 // copyin clause requires an accessible, unambiguous copy assignment 11293 // operator for the class type. 11294 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 11295 VarDecl *SrcVD = 11296 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(), 11297 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 11298 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 11299 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 11300 VarDecl *DstVD = 11301 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst", 11302 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 11303 DeclRefExpr *PseudoDstExpr = 11304 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 11305 // For arrays generate assignment operation for single element and replace 11306 // it by the original array element in CodeGen. 11307 ExprResult AssignmentOp = 11308 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 11309 PseudoSrcExpr); 11310 if (AssignmentOp.isInvalid()) 11311 continue; 11312 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 11313 /*DiscardedValue=*/true); 11314 if (AssignmentOp.isInvalid()) 11315 continue; 11316 11317 DSAStack->addDSA(VD, DE, OMPC_copyin); 11318 Vars.push_back(DE); 11319 SrcExprs.push_back(PseudoSrcExpr); 11320 DstExprs.push_back(PseudoDstExpr); 11321 AssignmentOps.push_back(AssignmentOp.get()); 11322 } 11323 11324 if (Vars.empty()) 11325 return nullptr; 11326 11327 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 11328 SrcExprs, DstExprs, AssignmentOps); 11329 } 11330 11331 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 11332 SourceLocation StartLoc, 11333 SourceLocation LParenLoc, 11334 SourceLocation EndLoc) { 11335 SmallVector<Expr *, 8> Vars; 11336 SmallVector<Expr *, 8> SrcExprs; 11337 SmallVector<Expr *, 8> DstExprs; 11338 SmallVector<Expr *, 8> AssignmentOps; 11339 for (Expr *RefExpr : VarList) { 11340 assert(RefExpr && "NULL expr in OpenMP linear clause."); 11341 SourceLocation ELoc; 11342 SourceRange ERange; 11343 Expr *SimpleRefExpr = RefExpr; 11344 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 11345 /*AllowArraySection=*/false); 11346 if (Res.second) { 11347 // It will be analyzed later. 11348 Vars.push_back(RefExpr); 11349 SrcExprs.push_back(nullptr); 11350 DstExprs.push_back(nullptr); 11351 AssignmentOps.push_back(nullptr); 11352 } 11353 ValueDecl *D = Res.first; 11354 if (!D) 11355 continue; 11356 11357 QualType Type = D->getType(); 11358 auto *VD = dyn_cast<VarDecl>(D); 11359 11360 // OpenMP [2.14.4.2, Restrictions, p.2] 11361 // A list item that appears in a copyprivate clause may not appear in a 11362 // private or firstprivate clause on the single construct. 11363 if (!VD || !DSAStack->isThreadPrivate(VD)) { 11364 DSAStackTy::DSAVarData DVar = 11365 DSAStack->getTopDSA(D, /*FromParent=*/false); 11366 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 11367 DVar.RefExpr) { 11368 Diag(ELoc, diag::err_omp_wrong_dsa) 11369 << getOpenMPClauseName(DVar.CKind) 11370 << getOpenMPClauseName(OMPC_copyprivate); 11371 reportOriginalDsa(*this, DSAStack, D, DVar); 11372 continue; 11373 } 11374 11375 // OpenMP [2.11.4.2, Restrictions, p.1] 11376 // All list items that appear in a copyprivate clause must be either 11377 // threadprivate or private in the enclosing context. 11378 if (DVar.CKind == OMPC_unknown) { 11379 DVar = DSAStack->getImplicitDSA(D, false); 11380 if (DVar.CKind == OMPC_shared) { 11381 Diag(ELoc, diag::err_omp_required_access) 11382 << getOpenMPClauseName(OMPC_copyprivate) 11383 << "threadprivate or private in the enclosing context"; 11384 reportOriginalDsa(*this, DSAStack, D, DVar); 11385 continue; 11386 } 11387 } 11388 } 11389 11390 // Variably modified types are not supported. 11391 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 11392 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 11393 << getOpenMPClauseName(OMPC_copyprivate) << Type 11394 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 11395 bool IsDecl = 11396 !VD || 11397 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 11398 Diag(D->getLocation(), 11399 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11400 << D; 11401 continue; 11402 } 11403 11404 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 11405 // A variable of class type (or array thereof) that appears in a 11406 // copyin clause requires an accessible, unambiguous copy assignment 11407 // operator for the class type. 11408 Type = Context.getBaseElementType(Type.getNonReferenceType()) 11409 .getUnqualifiedType(); 11410 VarDecl *SrcVD = 11411 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src", 11412 D->hasAttrs() ? &D->getAttrs() : nullptr); 11413 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 11414 VarDecl *DstVD = 11415 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst", 11416 D->hasAttrs() ? &D->getAttrs() : nullptr); 11417 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 11418 ExprResult AssignmentOp = BuildBinOp( 11419 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 11420 if (AssignmentOp.isInvalid()) 11421 continue; 11422 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc, 11423 /*DiscardedValue=*/true); 11424 if (AssignmentOp.isInvalid()) 11425 continue; 11426 11427 // No need to mark vars as copyprivate, they are already threadprivate or 11428 // implicitly private. 11429 assert(VD || isOpenMPCapturedDecl(D)); 11430 Vars.push_back( 11431 VD ? RefExpr->IgnoreParens() 11432 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 11433 SrcExprs.push_back(PseudoSrcExpr); 11434 DstExprs.push_back(PseudoDstExpr); 11435 AssignmentOps.push_back(AssignmentOp.get()); 11436 } 11437 11438 if (Vars.empty()) 11439 return nullptr; 11440 11441 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 11442 Vars, SrcExprs, DstExprs, AssignmentOps); 11443 } 11444 11445 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 11446 SourceLocation StartLoc, 11447 SourceLocation LParenLoc, 11448 SourceLocation EndLoc) { 11449 if (VarList.empty()) 11450 return nullptr; 11451 11452 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 11453 } 11454 11455 OMPClause * 11456 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind, 11457 SourceLocation DepLoc, SourceLocation ColonLoc, 11458 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 11459 SourceLocation LParenLoc, SourceLocation EndLoc) { 11460 if (DSAStack->getCurrentDirective() == OMPD_ordered && 11461 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 11462 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 11463 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 11464 return nullptr; 11465 } 11466 if (DSAStack->getCurrentDirective() != OMPD_ordered && 11467 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 11468 DepKind == OMPC_DEPEND_sink)) { 11469 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink}; 11470 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 11471 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 11472 /*Last=*/OMPC_DEPEND_unknown, Except) 11473 << getOpenMPClauseName(OMPC_depend); 11474 return nullptr; 11475 } 11476 SmallVector<Expr *, 8> Vars; 11477 DSAStackTy::OperatorOffsetTy OpsOffs; 11478 llvm::APSInt DepCounter(/*BitWidth=*/32); 11479 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 11480 if (DepKind == OMPC_DEPEND_sink) { 11481 if (const Expr *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) { 11482 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 11483 TotalDepCount.setIsUnsigned(/*Val=*/true); 11484 } 11485 } 11486 for (Expr *RefExpr : VarList) { 11487 assert(RefExpr && "NULL expr in OpenMP shared clause."); 11488 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 11489 // It will be analyzed later. 11490 Vars.push_back(RefExpr); 11491 continue; 11492 } 11493 11494 SourceLocation ELoc = RefExpr->getExprLoc(); 11495 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 11496 if (DepKind == OMPC_DEPEND_sink) { 11497 if (DSAStack->getParentOrderedRegionParam() && 11498 DepCounter >= TotalDepCount) { 11499 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 11500 continue; 11501 } 11502 ++DepCounter; 11503 // OpenMP [2.13.9, Summary] 11504 // depend(dependence-type : vec), where dependence-type is: 11505 // 'sink' and where vec is the iteration vector, which has the form: 11506 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 11507 // where n is the value specified by the ordered clause in the loop 11508 // directive, xi denotes the loop iteration variable of the i-th nested 11509 // loop associated with the loop directive, and di is a constant 11510 // non-negative integer. 11511 if (CurContext->isDependentContext()) { 11512 // It will be analyzed later. 11513 Vars.push_back(RefExpr); 11514 continue; 11515 } 11516 SimpleExpr = SimpleExpr->IgnoreImplicit(); 11517 OverloadedOperatorKind OOK = OO_None; 11518 SourceLocation OOLoc; 11519 Expr *LHS = SimpleExpr; 11520 Expr *RHS = nullptr; 11521 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 11522 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 11523 OOLoc = BO->getOperatorLoc(); 11524 LHS = BO->getLHS()->IgnoreParenImpCasts(); 11525 RHS = BO->getRHS()->IgnoreParenImpCasts(); 11526 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 11527 OOK = OCE->getOperator(); 11528 OOLoc = OCE->getOperatorLoc(); 11529 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 11530 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 11531 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 11532 OOK = MCE->getMethodDecl() 11533 ->getNameInfo() 11534 .getName() 11535 .getCXXOverloadedOperator(); 11536 OOLoc = MCE->getCallee()->getExprLoc(); 11537 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 11538 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 11539 } 11540 SourceLocation ELoc; 11541 SourceRange ERange; 11542 auto Res = getPrivateItem(*this, LHS, ELoc, ERange, 11543 /*AllowArraySection=*/false); 11544 if (Res.second) { 11545 // It will be analyzed later. 11546 Vars.push_back(RefExpr); 11547 } 11548 ValueDecl *D = Res.first; 11549 if (!D) 11550 continue; 11551 11552 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 11553 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 11554 continue; 11555 } 11556 if (RHS) { 11557 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 11558 RHS, OMPC_depend, /*StrictlyPositive=*/false); 11559 if (RHSRes.isInvalid()) 11560 continue; 11561 } 11562 if (!CurContext->isDependentContext() && 11563 DSAStack->getParentOrderedRegionParam() && 11564 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 11565 const ValueDecl *VD = 11566 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 11567 if (VD) 11568 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 11569 << 1 << VD; 11570 else 11571 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 11572 continue; 11573 } 11574 OpsOffs.emplace_back(RHS, OOK); 11575 } else { 11576 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 11577 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 11578 (ASE && 11579 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() && 11580 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 11581 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 11582 << RefExpr->getSourceRange(); 11583 continue; 11584 } 11585 bool Suppress = getDiagnostics().getSuppressAllDiagnostics(); 11586 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 11587 ExprResult Res = 11588 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts()); 11589 getDiagnostics().setSuppressAllDiagnostics(Suppress); 11590 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) { 11591 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 11592 << RefExpr->getSourceRange(); 11593 continue; 11594 } 11595 } 11596 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 11597 } 11598 11599 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 11600 TotalDepCount > VarList.size() && 11601 DSAStack->getParentOrderedRegionParam() && 11602 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 11603 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 11604 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 11605 } 11606 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 11607 Vars.empty()) 11608 return nullptr; 11609 11610 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 11611 DepKind, DepLoc, ColonLoc, Vars); 11612 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 11613 DSAStack->isParentOrderedRegion()) 11614 DSAStack->addDoacrossDependClause(C, OpsOffs); 11615 return C; 11616 } 11617 11618 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc, 11619 SourceLocation LParenLoc, 11620 SourceLocation EndLoc) { 11621 Expr *ValExpr = Device; 11622 Stmt *HelperValStmt = nullptr; 11623 11624 // OpenMP [2.9.1, Restrictions] 11625 // The device expression must evaluate to a non-negative integer value. 11626 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 11627 /*StrictlyPositive=*/false)) 11628 return nullptr; 11629 11630 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 11631 OpenMPDirectiveKind CaptureRegion = 11632 getOpenMPCaptureRegionForClause(DKind, OMPC_device); 11633 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 11634 ValExpr = MakeFullExpr(ValExpr).get(); 11635 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 11636 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 11637 HelperValStmt = buildPreInits(Context, Captures); 11638 } 11639 11640 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion, 11641 StartLoc, LParenLoc, EndLoc); 11642 } 11643 11644 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 11645 DSAStackTy *Stack, QualType QTy, 11646 bool FullCheck = true) { 11647 NamedDecl *ND; 11648 if (QTy->isIncompleteType(&ND)) { 11649 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 11650 return false; 11651 } 11652 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 11653 !QTy.isTrivialType(SemaRef.Context)) 11654 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 11655 return true; 11656 } 11657 11658 /// Return true if it can be proven that the provided array expression 11659 /// (array section or array subscript) does NOT specify the whole size of the 11660 /// array whose base type is \a BaseQTy. 11661 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 11662 const Expr *E, 11663 QualType BaseQTy) { 11664 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 11665 11666 // If this is an array subscript, it refers to the whole size if the size of 11667 // the dimension is constant and equals 1. Also, an array section assumes the 11668 // format of an array subscript if no colon is used. 11669 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) { 11670 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 11671 return ATy->getSize().getSExtValue() != 1; 11672 // Size can't be evaluated statically. 11673 return false; 11674 } 11675 11676 assert(OASE && "Expecting array section if not an array subscript."); 11677 const Expr *LowerBound = OASE->getLowerBound(); 11678 const Expr *Length = OASE->getLength(); 11679 11680 // If there is a lower bound that does not evaluates to zero, we are not 11681 // covering the whole dimension. 11682 if (LowerBound) { 11683 llvm::APSInt ConstLowerBound; 11684 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext())) 11685 return false; // Can't get the integer value as a constant. 11686 if (ConstLowerBound.getSExtValue()) 11687 return true; 11688 } 11689 11690 // If we don't have a length we covering the whole dimension. 11691 if (!Length) 11692 return false; 11693 11694 // If the base is a pointer, we don't have a way to get the size of the 11695 // pointee. 11696 if (BaseQTy->isPointerType()) 11697 return false; 11698 11699 // We can only check if the length is the same as the size of the dimension 11700 // if we have a constant array. 11701 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 11702 if (!CATy) 11703 return false; 11704 11705 llvm::APSInt ConstLength; 11706 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext())) 11707 return false; // Can't get the integer value as a constant. 11708 11709 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 11710 } 11711 11712 // Return true if it can be proven that the provided array expression (array 11713 // section or array subscript) does NOT specify a single element of the array 11714 // whose base type is \a BaseQTy. 11715 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 11716 const Expr *E, 11717 QualType BaseQTy) { 11718 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 11719 11720 // An array subscript always refer to a single element. Also, an array section 11721 // assumes the format of an array subscript if no colon is used. 11722 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) 11723 return false; 11724 11725 assert(OASE && "Expecting array section if not an array subscript."); 11726 const Expr *Length = OASE->getLength(); 11727 11728 // If we don't have a length we have to check if the array has unitary size 11729 // for this dimension. Also, we should always expect a length if the base type 11730 // is pointer. 11731 if (!Length) { 11732 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 11733 return ATy->getSize().getSExtValue() != 1; 11734 // We cannot assume anything. 11735 return false; 11736 } 11737 11738 // Check if the length evaluates to 1. 11739 llvm::APSInt ConstLength; 11740 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext())) 11741 return false; // Can't get the integer value as a constant. 11742 11743 return ConstLength.getSExtValue() != 1; 11744 } 11745 11746 // Return the expression of the base of the mappable expression or null if it 11747 // cannot be determined and do all the necessary checks to see if the expression 11748 // is valid as a standalone mappable expression. In the process, record all the 11749 // components of the expression. 11750 static const Expr *checkMapClauseExpressionBase( 11751 Sema &SemaRef, Expr *E, 11752 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 11753 OpenMPClauseKind CKind, bool NoDiagnose) { 11754 SourceLocation ELoc = E->getExprLoc(); 11755 SourceRange ERange = E->getSourceRange(); 11756 11757 // The base of elements of list in a map clause have to be either: 11758 // - a reference to variable or field. 11759 // - a member expression. 11760 // - an array expression. 11761 // 11762 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 11763 // reference to 'r'. 11764 // 11765 // If we have: 11766 // 11767 // struct SS { 11768 // Bla S; 11769 // foo() { 11770 // #pragma omp target map (S.Arr[:12]); 11771 // } 11772 // } 11773 // 11774 // We want to retrieve the member expression 'this->S'; 11775 11776 const Expr *RelevantExpr = nullptr; 11777 11778 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2] 11779 // If a list item is an array section, it must specify contiguous storage. 11780 // 11781 // For this restriction it is sufficient that we make sure only references 11782 // to variables or fields and array expressions, and that no array sections 11783 // exist except in the rightmost expression (unless they cover the whole 11784 // dimension of the array). E.g. these would be invalid: 11785 // 11786 // r.ArrS[3:5].Arr[6:7] 11787 // 11788 // r.ArrS[3:5].x 11789 // 11790 // but these would be valid: 11791 // r.ArrS[3].Arr[6:7] 11792 // 11793 // r.ArrS[3].x 11794 11795 bool AllowUnitySizeArraySection = true; 11796 bool AllowWholeSizeArraySection = true; 11797 11798 while (!RelevantExpr) { 11799 E = E->IgnoreParenImpCasts(); 11800 11801 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) { 11802 if (!isa<VarDecl>(CurE->getDecl())) 11803 return nullptr; 11804 11805 RelevantExpr = CurE; 11806 11807 // If we got a reference to a declaration, we should not expect any array 11808 // section before that. 11809 AllowUnitySizeArraySection = false; 11810 AllowWholeSizeArraySection = false; 11811 11812 // Record the component. 11813 CurComponents.emplace_back(CurE, CurE->getDecl()); 11814 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) { 11815 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts(); 11816 11817 if (isa<CXXThisExpr>(BaseE)) 11818 // We found a base expression: this->Val. 11819 RelevantExpr = CurE; 11820 else 11821 E = BaseE; 11822 11823 if (!isa<FieldDecl>(CurE->getMemberDecl())) { 11824 if (!NoDiagnose) { 11825 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 11826 << CurE->getSourceRange(); 11827 return nullptr; 11828 } 11829 if (RelevantExpr) 11830 return nullptr; 11831 continue; 11832 } 11833 11834 auto *FD = cast<FieldDecl>(CurE->getMemberDecl()); 11835 11836 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 11837 // A bit-field cannot appear in a map clause. 11838 // 11839 if (FD->isBitField()) { 11840 if (!NoDiagnose) { 11841 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 11842 << CurE->getSourceRange() << getOpenMPClauseName(CKind); 11843 return nullptr; 11844 } 11845 if (RelevantExpr) 11846 return nullptr; 11847 continue; 11848 } 11849 11850 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 11851 // If the type of a list item is a reference to a type T then the type 11852 // will be considered to be T for all purposes of this clause. 11853 QualType CurType = BaseE->getType().getNonReferenceType(); 11854 11855 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 11856 // A list item cannot be a variable that is a member of a structure with 11857 // a union type. 11858 // 11859 if (CurType->isUnionType()) { 11860 if (!NoDiagnose) { 11861 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 11862 << CurE->getSourceRange(); 11863 return nullptr; 11864 } 11865 continue; 11866 } 11867 11868 // If we got a member expression, we should not expect any array section 11869 // before that: 11870 // 11871 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 11872 // If a list item is an element of a structure, only the rightmost symbol 11873 // of the variable reference can be an array section. 11874 // 11875 AllowUnitySizeArraySection = false; 11876 AllowWholeSizeArraySection = false; 11877 11878 // Record the component. 11879 CurComponents.emplace_back(CurE, FD); 11880 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) { 11881 E = CurE->getBase()->IgnoreParenImpCasts(); 11882 11883 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 11884 if (!NoDiagnose) { 11885 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 11886 << 0 << CurE->getSourceRange(); 11887 return nullptr; 11888 } 11889 continue; 11890 } 11891 11892 // If we got an array subscript that express the whole dimension we 11893 // can have any array expressions before. If it only expressing part of 11894 // the dimension, we can only have unitary-size array expressions. 11895 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, 11896 E->getType())) 11897 AllowWholeSizeArraySection = false; 11898 11899 // Record the component - we don't have any declaration associated. 11900 CurComponents.emplace_back(CurE, nullptr); 11901 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) { 11902 assert(!NoDiagnose && "Array sections cannot be implicitly mapped."); 11903 E = CurE->getBase()->IgnoreParenImpCasts(); 11904 11905 QualType CurType = 11906 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 11907 11908 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 11909 // If the type of a list item is a reference to a type T then the type 11910 // will be considered to be T for all purposes of this clause. 11911 if (CurType->isReferenceType()) 11912 CurType = CurType->getPointeeType(); 11913 11914 bool IsPointer = CurType->isAnyPointerType(); 11915 11916 if (!IsPointer && !CurType->isArrayType()) { 11917 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 11918 << 0 << CurE->getSourceRange(); 11919 return nullptr; 11920 } 11921 11922 bool NotWhole = 11923 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType); 11924 bool NotUnity = 11925 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType); 11926 11927 if (AllowWholeSizeArraySection) { 11928 // Any array section is currently allowed. Allowing a whole size array 11929 // section implies allowing a unity array section as well. 11930 // 11931 // If this array section refers to the whole dimension we can still 11932 // accept other array sections before this one, except if the base is a 11933 // pointer. Otherwise, only unitary sections are accepted. 11934 if (NotWhole || IsPointer) 11935 AllowWholeSizeArraySection = false; 11936 } else if (AllowUnitySizeArraySection && NotUnity) { 11937 // A unity or whole array section is not allowed and that is not 11938 // compatible with the properties of the current array section. 11939 SemaRef.Diag( 11940 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 11941 << CurE->getSourceRange(); 11942 return nullptr; 11943 } 11944 11945 // Record the component - we don't have any declaration associated. 11946 CurComponents.emplace_back(CurE, nullptr); 11947 } else { 11948 if (!NoDiagnose) { 11949 // If nothing else worked, this is not a valid map clause expression. 11950 SemaRef.Diag( 11951 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 11952 << ERange; 11953 } 11954 return nullptr; 11955 } 11956 } 11957 11958 return RelevantExpr; 11959 } 11960 11961 // Return true if expression E associated with value VD has conflicts with other 11962 // map information. 11963 static bool checkMapConflicts( 11964 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 11965 bool CurrentRegionOnly, 11966 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 11967 OpenMPClauseKind CKind) { 11968 assert(VD && E); 11969 SourceLocation ELoc = E->getExprLoc(); 11970 SourceRange ERange = E->getSourceRange(); 11971 11972 // In order to easily check the conflicts we need to match each component of 11973 // the expression under test with the components of the expressions that are 11974 // already in the stack. 11975 11976 assert(!CurComponents.empty() && "Map clause expression with no components!"); 11977 assert(CurComponents.back().getAssociatedDeclaration() == VD && 11978 "Map clause expression with unexpected base!"); 11979 11980 // Variables to help detecting enclosing problems in data environment nests. 11981 bool IsEnclosedByDataEnvironmentExpr = false; 11982 const Expr *EnclosingExpr = nullptr; 11983 11984 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 11985 VD, CurrentRegionOnly, 11986 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 11987 ERange, CKind, &EnclosingExpr, 11988 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 11989 StackComponents, 11990 OpenMPClauseKind) { 11991 assert(!StackComponents.empty() && 11992 "Map clause expression with no components!"); 11993 assert(StackComponents.back().getAssociatedDeclaration() == VD && 11994 "Map clause expression with unexpected base!"); 11995 (void)VD; 11996 11997 // The whole expression in the stack. 11998 const Expr *RE = StackComponents.front().getAssociatedExpression(); 11999 12000 // Expressions must start from the same base. Here we detect at which 12001 // point both expressions diverge from each other and see if we can 12002 // detect if the memory referred to both expressions is contiguous and 12003 // do not overlap. 12004 auto CI = CurComponents.rbegin(); 12005 auto CE = CurComponents.rend(); 12006 auto SI = StackComponents.rbegin(); 12007 auto SE = StackComponents.rend(); 12008 for (; CI != CE && SI != SE; ++CI, ++SI) { 12009 12010 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 12011 // At most one list item can be an array item derived from a given 12012 // variable in map clauses of the same construct. 12013 if (CurrentRegionOnly && 12014 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 12015 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) && 12016 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 12017 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) { 12018 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 12019 diag::err_omp_multiple_array_items_in_map_clause) 12020 << CI->getAssociatedExpression()->getSourceRange(); 12021 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 12022 diag::note_used_here) 12023 << SI->getAssociatedExpression()->getSourceRange(); 12024 return true; 12025 } 12026 12027 // Do both expressions have the same kind? 12028 if (CI->getAssociatedExpression()->getStmtClass() != 12029 SI->getAssociatedExpression()->getStmtClass()) 12030 break; 12031 12032 // Are we dealing with different variables/fields? 12033 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 12034 break; 12035 } 12036 // Check if the extra components of the expressions in the enclosing 12037 // data environment are redundant for the current base declaration. 12038 // If they are, the maps completely overlap, which is legal. 12039 for (; SI != SE; ++SI) { 12040 QualType Type; 12041 if (const auto *ASE = 12042 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 12043 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 12044 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 12045 SI->getAssociatedExpression())) { 12046 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 12047 Type = 12048 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 12049 } 12050 if (Type.isNull() || Type->isAnyPointerType() || 12051 checkArrayExpressionDoesNotReferToWholeSize( 12052 SemaRef, SI->getAssociatedExpression(), Type)) 12053 break; 12054 } 12055 12056 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 12057 // List items of map clauses in the same construct must not share 12058 // original storage. 12059 // 12060 // If the expressions are exactly the same or one is a subset of the 12061 // other, it means they are sharing storage. 12062 if (CI == CE && SI == SE) { 12063 if (CurrentRegionOnly) { 12064 if (CKind == OMPC_map) { 12065 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 12066 } else { 12067 assert(CKind == OMPC_to || CKind == OMPC_from); 12068 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 12069 << ERange; 12070 } 12071 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12072 << RE->getSourceRange(); 12073 return true; 12074 } 12075 // If we find the same expression in the enclosing data environment, 12076 // that is legal. 12077 IsEnclosedByDataEnvironmentExpr = true; 12078 return false; 12079 } 12080 12081 QualType DerivedType = 12082 std::prev(CI)->getAssociatedDeclaration()->getType(); 12083 SourceLocation DerivedLoc = 12084 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 12085 12086 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 12087 // If the type of a list item is a reference to a type T then the type 12088 // will be considered to be T for all purposes of this clause. 12089 DerivedType = DerivedType.getNonReferenceType(); 12090 12091 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 12092 // A variable for which the type is pointer and an array section 12093 // derived from that variable must not appear as list items of map 12094 // clauses of the same construct. 12095 // 12096 // Also, cover one of the cases in: 12097 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 12098 // If any part of the original storage of a list item has corresponding 12099 // storage in the device data environment, all of the original storage 12100 // must have corresponding storage in the device data environment. 12101 // 12102 if (DerivedType->isAnyPointerType()) { 12103 if (CI == CE || SI == SE) { 12104 SemaRef.Diag( 12105 DerivedLoc, 12106 diag::err_omp_pointer_mapped_along_with_derived_section) 12107 << DerivedLoc; 12108 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12109 << RE->getSourceRange(); 12110 return true; 12111 } 12112 if (CI->getAssociatedExpression()->getStmtClass() != 12113 SI->getAssociatedExpression()->getStmtClass() || 12114 CI->getAssociatedDeclaration()->getCanonicalDecl() == 12115 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 12116 assert(CI != CE && SI != SE); 12117 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 12118 << DerivedLoc; 12119 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12120 << RE->getSourceRange(); 12121 return true; 12122 } 12123 } 12124 12125 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 12126 // List items of map clauses in the same construct must not share 12127 // original storage. 12128 // 12129 // An expression is a subset of the other. 12130 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 12131 if (CKind == OMPC_map) { 12132 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 12133 } else { 12134 assert(CKind == OMPC_to || CKind == OMPC_from); 12135 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 12136 << ERange; 12137 } 12138 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12139 << RE->getSourceRange(); 12140 return true; 12141 } 12142 12143 // The current expression uses the same base as other expression in the 12144 // data environment but does not contain it completely. 12145 if (!CurrentRegionOnly && SI != SE) 12146 EnclosingExpr = RE; 12147 12148 // The current expression is a subset of the expression in the data 12149 // environment. 12150 IsEnclosedByDataEnvironmentExpr |= 12151 (!CurrentRegionOnly && CI != CE && SI == SE); 12152 12153 return false; 12154 }); 12155 12156 if (CurrentRegionOnly) 12157 return FoundError; 12158 12159 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 12160 // If any part of the original storage of a list item has corresponding 12161 // storage in the device data environment, all of the original storage must 12162 // have corresponding storage in the device data environment. 12163 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 12164 // If a list item is an element of a structure, and a different element of 12165 // the structure has a corresponding list item in the device data environment 12166 // prior to a task encountering the construct associated with the map clause, 12167 // then the list item must also have a corresponding list item in the device 12168 // data environment prior to the task encountering the construct. 12169 // 12170 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 12171 SemaRef.Diag(ELoc, 12172 diag::err_omp_original_storage_is_shared_and_does_not_contain) 12173 << ERange; 12174 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 12175 << EnclosingExpr->getSourceRange(); 12176 return true; 12177 } 12178 12179 return FoundError; 12180 } 12181 12182 namespace { 12183 // Utility struct that gathers all the related lists associated with a mappable 12184 // expression. 12185 struct MappableVarListInfo { 12186 // The list of expressions. 12187 ArrayRef<Expr *> VarList; 12188 // The list of processed expressions. 12189 SmallVector<Expr *, 16> ProcessedVarList; 12190 // The mappble components for each expression. 12191 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 12192 // The base declaration of the variable. 12193 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 12194 12195 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 12196 // We have a list of components and base declarations for each entry in the 12197 // variable list. 12198 VarComponents.reserve(VarList.size()); 12199 VarBaseDeclarations.reserve(VarList.size()); 12200 } 12201 }; 12202 } 12203 12204 // Check the validity of the provided variable list for the provided clause kind 12205 // \a CKind. In the check process the valid expressions, and mappable expression 12206 // components and variables are extracted and used to fill \a Vars, 12207 // \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and 12208 // \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'. 12209 static void 12210 checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS, 12211 OpenMPClauseKind CKind, MappableVarListInfo &MVLI, 12212 SourceLocation StartLoc, 12213 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 12214 bool IsMapTypeImplicit = false) { 12215 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 12216 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 12217 "Unexpected clause kind with mappable expressions!"); 12218 12219 // Keep track of the mappable components and base declarations in this clause. 12220 // Each entry in the list is going to have a list of components associated. We 12221 // record each set of the components so that we can build the clause later on. 12222 // In the end we should have the same amount of declarations and component 12223 // lists. 12224 12225 for (Expr *RE : MVLI.VarList) { 12226 assert(RE && "Null expr in omp to/from/map clause"); 12227 SourceLocation ELoc = RE->getExprLoc(); 12228 12229 const Expr *VE = RE->IgnoreParenLValueCasts(); 12230 12231 if (VE->isValueDependent() || VE->isTypeDependent() || 12232 VE->isInstantiationDependent() || 12233 VE->containsUnexpandedParameterPack()) { 12234 // We can only analyze this information once the missing information is 12235 // resolved. 12236 MVLI.ProcessedVarList.push_back(RE); 12237 continue; 12238 } 12239 12240 Expr *SimpleExpr = RE->IgnoreParenCasts(); 12241 12242 if (!RE->IgnoreParenImpCasts()->isLValue()) { 12243 SemaRef.Diag(ELoc, 12244 diag::err_omp_expected_named_var_member_or_array_expression) 12245 << RE->getSourceRange(); 12246 continue; 12247 } 12248 12249 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 12250 ValueDecl *CurDeclaration = nullptr; 12251 12252 // Obtain the array or member expression bases if required. Also, fill the 12253 // components array with all the components identified in the process. 12254 const Expr *BE = checkMapClauseExpressionBase( 12255 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false); 12256 if (!BE) 12257 continue; 12258 12259 assert(!CurComponents.empty() && 12260 "Invalid mappable expression information."); 12261 12262 // For the following checks, we rely on the base declaration which is 12263 // expected to be associated with the last component. The declaration is 12264 // expected to be a variable or a field (if 'this' is being mapped). 12265 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 12266 assert(CurDeclaration && "Null decl on map clause."); 12267 assert( 12268 CurDeclaration->isCanonicalDecl() && 12269 "Expecting components to have associated only canonical declarations."); 12270 12271 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 12272 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 12273 12274 assert((VD || FD) && "Only variables or fields are expected here!"); 12275 (void)FD; 12276 12277 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 12278 // threadprivate variables cannot appear in a map clause. 12279 // OpenMP 4.5 [2.10.5, target update Construct] 12280 // threadprivate variables cannot appear in a from clause. 12281 if (VD && DSAS->isThreadPrivate(VD)) { 12282 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 12283 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 12284 << getOpenMPClauseName(CKind); 12285 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 12286 continue; 12287 } 12288 12289 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 12290 // A list item cannot appear in both a map clause and a data-sharing 12291 // attribute clause on the same construct. 12292 12293 // Check conflicts with other map clause expressions. We check the conflicts 12294 // with the current construct separately from the enclosing data 12295 // environment, because the restrictions are different. We only have to 12296 // check conflicts across regions for the map clauses. 12297 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 12298 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 12299 break; 12300 if (CKind == OMPC_map && 12301 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 12302 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 12303 break; 12304 12305 // OpenMP 4.5 [2.10.5, target update Construct] 12306 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 12307 // If the type of a list item is a reference to a type T then the type will 12308 // be considered to be T for all purposes of this clause. 12309 auto I = llvm::find_if( 12310 CurComponents, 12311 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 12312 return MC.getAssociatedDeclaration(); 12313 }); 12314 assert(I != CurComponents.end() && "Null decl on map clause."); 12315 QualType Type = 12316 I->getAssociatedDeclaration()->getType().getNonReferenceType(); 12317 12318 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 12319 // A list item in a to or from clause must have a mappable type. 12320 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 12321 // A list item must have a mappable type. 12322 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 12323 DSAS, Type)) 12324 continue; 12325 12326 if (CKind == OMPC_map) { 12327 // target enter data 12328 // OpenMP [2.10.2, Restrictions, p. 99] 12329 // A map-type must be specified in all map clauses and must be either 12330 // to or alloc. 12331 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 12332 if (DKind == OMPD_target_enter_data && 12333 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 12334 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 12335 << (IsMapTypeImplicit ? 1 : 0) 12336 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 12337 << getOpenMPDirectiveName(DKind); 12338 continue; 12339 } 12340 12341 // target exit_data 12342 // OpenMP [2.10.3, Restrictions, p. 102] 12343 // A map-type must be specified in all map clauses and must be either 12344 // from, release, or delete. 12345 if (DKind == OMPD_target_exit_data && 12346 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 12347 MapType == OMPC_MAP_delete)) { 12348 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 12349 << (IsMapTypeImplicit ? 1 : 0) 12350 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 12351 << getOpenMPDirectiveName(DKind); 12352 continue; 12353 } 12354 12355 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 12356 // A list item cannot appear in both a map clause and a data-sharing 12357 // attribute clause on the same construct 12358 if (VD && isOpenMPTargetExecutionDirective(DKind)) { 12359 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 12360 if (isOpenMPPrivate(DVar.CKind)) { 12361 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 12362 << getOpenMPClauseName(DVar.CKind) 12363 << getOpenMPClauseName(OMPC_map) 12364 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 12365 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 12366 continue; 12367 } 12368 } 12369 } 12370 12371 // Save the current expression. 12372 MVLI.ProcessedVarList.push_back(RE); 12373 12374 // Store the components in the stack so that they can be used to check 12375 // against other clauses later on. 12376 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 12377 /*WhereFoundClauseKind=*/OMPC_map); 12378 12379 // Save the components and declaration to create the clause. For purposes of 12380 // the clause creation, any component list that has has base 'this' uses 12381 // null as base declaration. 12382 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 12383 MVLI.VarComponents.back().append(CurComponents.begin(), 12384 CurComponents.end()); 12385 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 12386 : CurDeclaration); 12387 } 12388 } 12389 12390 OMPClause * 12391 Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier, 12392 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, 12393 SourceLocation MapLoc, SourceLocation ColonLoc, 12394 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 12395 SourceLocation LParenLoc, SourceLocation EndLoc) { 12396 MappableVarListInfo MVLI(VarList); 12397 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc, 12398 MapType, IsMapTypeImplicit); 12399 12400 // We need to produce a map clause even if we don't have variables so that 12401 // other diagnostics related with non-existing map clauses are accurate. 12402 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, 12403 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 12404 MVLI.VarComponents, MapTypeModifier, MapType, 12405 IsMapTypeImplicit, MapLoc); 12406 } 12407 12408 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 12409 TypeResult ParsedType) { 12410 assert(ParsedType.isUsable()); 12411 12412 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 12413 if (ReductionType.isNull()) 12414 return QualType(); 12415 12416 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 12417 // A type name in a declare reduction directive cannot be a function type, an 12418 // array type, a reference type, or a type qualified with const, volatile or 12419 // restrict. 12420 if (ReductionType.hasQualifiers()) { 12421 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 12422 return QualType(); 12423 } 12424 12425 if (ReductionType->isFunctionType()) { 12426 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 12427 return QualType(); 12428 } 12429 if (ReductionType->isReferenceType()) { 12430 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 12431 return QualType(); 12432 } 12433 if (ReductionType->isArrayType()) { 12434 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 12435 return QualType(); 12436 } 12437 return ReductionType; 12438 } 12439 12440 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 12441 Scope *S, DeclContext *DC, DeclarationName Name, 12442 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 12443 AccessSpecifier AS, Decl *PrevDeclInScope) { 12444 SmallVector<Decl *, 8> Decls; 12445 Decls.reserve(ReductionTypes.size()); 12446 12447 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 12448 forRedeclarationInCurContext()); 12449 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 12450 // A reduction-identifier may not be re-declared in the current scope for the 12451 // same type or for a type that is compatible according to the base language 12452 // rules. 12453 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 12454 OMPDeclareReductionDecl *PrevDRD = nullptr; 12455 bool InCompoundScope = true; 12456 if (S != nullptr) { 12457 // Find previous declaration with the same name not referenced in other 12458 // declarations. 12459 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 12460 InCompoundScope = 12461 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 12462 LookupName(Lookup, S); 12463 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 12464 /*AllowInlineNamespace=*/false); 12465 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 12466 LookupResult::Filter Filter = Lookup.makeFilter(); 12467 while (Filter.hasNext()) { 12468 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 12469 if (InCompoundScope) { 12470 auto I = UsedAsPrevious.find(PrevDecl); 12471 if (I == UsedAsPrevious.end()) 12472 UsedAsPrevious[PrevDecl] = false; 12473 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 12474 UsedAsPrevious[D] = true; 12475 } 12476 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 12477 PrevDecl->getLocation(); 12478 } 12479 Filter.done(); 12480 if (InCompoundScope) { 12481 for (const auto &PrevData : UsedAsPrevious) { 12482 if (!PrevData.second) { 12483 PrevDRD = PrevData.first; 12484 break; 12485 } 12486 } 12487 } 12488 } else if (PrevDeclInScope != nullptr) { 12489 auto *PrevDRDInScope = PrevDRD = 12490 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 12491 do { 12492 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 12493 PrevDRDInScope->getLocation(); 12494 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 12495 } while (PrevDRDInScope != nullptr); 12496 } 12497 for (const auto &TyData : ReductionTypes) { 12498 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 12499 bool Invalid = false; 12500 if (I != PreviousRedeclTypes.end()) { 12501 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 12502 << TyData.first; 12503 Diag(I->second, diag::note_previous_definition); 12504 Invalid = true; 12505 } 12506 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 12507 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 12508 Name, TyData.first, PrevDRD); 12509 DC->addDecl(DRD); 12510 DRD->setAccess(AS); 12511 Decls.push_back(DRD); 12512 if (Invalid) 12513 DRD->setInvalidDecl(); 12514 else 12515 PrevDRD = DRD; 12516 } 12517 12518 return DeclGroupPtrTy::make( 12519 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 12520 } 12521 12522 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 12523 auto *DRD = cast<OMPDeclareReductionDecl>(D); 12524 12525 // Enter new function scope. 12526 PushFunctionScope(); 12527 setFunctionHasBranchProtectedScope(); 12528 getCurFunction()->setHasOMPDeclareReductionCombiner(); 12529 12530 if (S != nullptr) 12531 PushDeclContext(S, DRD); 12532 else 12533 CurContext = DRD; 12534 12535 PushExpressionEvaluationContext( 12536 ExpressionEvaluationContext::PotentiallyEvaluated); 12537 12538 QualType ReductionType = DRD->getType(); 12539 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 12540 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 12541 // uses semantics of argument handles by value, but it should be passed by 12542 // reference. C lang does not support references, so pass all parameters as 12543 // pointers. 12544 // Create 'T omp_in;' variable. 12545 VarDecl *OmpInParm = 12546 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 12547 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 12548 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 12549 // uses semantics of argument handles by value, but it should be passed by 12550 // reference. C lang does not support references, so pass all parameters as 12551 // pointers. 12552 // Create 'T omp_out;' variable. 12553 VarDecl *OmpOutParm = 12554 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 12555 if (S != nullptr) { 12556 PushOnScopeChains(OmpInParm, S); 12557 PushOnScopeChains(OmpOutParm, S); 12558 } else { 12559 DRD->addDecl(OmpInParm); 12560 DRD->addDecl(OmpOutParm); 12561 } 12562 } 12563 12564 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 12565 auto *DRD = cast<OMPDeclareReductionDecl>(D); 12566 DiscardCleanupsInEvaluationContext(); 12567 PopExpressionEvaluationContext(); 12568 12569 PopDeclContext(); 12570 PopFunctionScopeInfo(); 12571 12572 if (Combiner != nullptr) 12573 DRD->setCombiner(Combiner); 12574 else 12575 DRD->setInvalidDecl(); 12576 } 12577 12578 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 12579 auto *DRD = cast<OMPDeclareReductionDecl>(D); 12580 12581 // Enter new function scope. 12582 PushFunctionScope(); 12583 setFunctionHasBranchProtectedScope(); 12584 12585 if (S != nullptr) 12586 PushDeclContext(S, DRD); 12587 else 12588 CurContext = DRD; 12589 12590 PushExpressionEvaluationContext( 12591 ExpressionEvaluationContext::PotentiallyEvaluated); 12592 12593 QualType ReductionType = DRD->getType(); 12594 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 12595 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 12596 // uses semantics of argument handles by value, but it should be passed by 12597 // reference. C lang does not support references, so pass all parameters as 12598 // pointers. 12599 // Create 'T omp_priv;' variable. 12600 VarDecl *OmpPrivParm = 12601 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 12602 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 12603 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 12604 // uses semantics of argument handles by value, but it should be passed by 12605 // reference. C lang does not support references, so pass all parameters as 12606 // pointers. 12607 // Create 'T omp_orig;' variable. 12608 VarDecl *OmpOrigParm = 12609 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 12610 if (S != nullptr) { 12611 PushOnScopeChains(OmpPrivParm, S); 12612 PushOnScopeChains(OmpOrigParm, S); 12613 } else { 12614 DRD->addDecl(OmpPrivParm); 12615 DRD->addDecl(OmpOrigParm); 12616 } 12617 return OmpPrivParm; 12618 } 12619 12620 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 12621 VarDecl *OmpPrivParm) { 12622 auto *DRD = cast<OMPDeclareReductionDecl>(D); 12623 DiscardCleanupsInEvaluationContext(); 12624 PopExpressionEvaluationContext(); 12625 12626 PopDeclContext(); 12627 PopFunctionScopeInfo(); 12628 12629 if (Initializer != nullptr) { 12630 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 12631 } else if (OmpPrivParm->hasInit()) { 12632 DRD->setInitializer(OmpPrivParm->getInit(), 12633 OmpPrivParm->isDirectInit() 12634 ? OMPDeclareReductionDecl::DirectInit 12635 : OMPDeclareReductionDecl::CopyInit); 12636 } else { 12637 DRD->setInvalidDecl(); 12638 } 12639 } 12640 12641 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 12642 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 12643 for (Decl *D : DeclReductions.get()) { 12644 if (IsValid) { 12645 if (S) 12646 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 12647 /*AddToContext=*/false); 12648 } else { 12649 D->setInvalidDecl(); 12650 } 12651 } 12652 return DeclReductions; 12653 } 12654 12655 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 12656 SourceLocation StartLoc, 12657 SourceLocation LParenLoc, 12658 SourceLocation EndLoc) { 12659 Expr *ValExpr = NumTeams; 12660 Stmt *HelperValStmt = nullptr; 12661 12662 // OpenMP [teams Constrcut, Restrictions] 12663 // The num_teams expression must evaluate to a positive integer value. 12664 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 12665 /*StrictlyPositive=*/true)) 12666 return nullptr; 12667 12668 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12669 OpenMPDirectiveKind CaptureRegion = 12670 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams); 12671 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12672 ValExpr = MakeFullExpr(ValExpr).get(); 12673 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12674 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12675 HelperValStmt = buildPreInits(Context, Captures); 12676 } 12677 12678 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 12679 StartLoc, LParenLoc, EndLoc); 12680 } 12681 12682 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 12683 SourceLocation StartLoc, 12684 SourceLocation LParenLoc, 12685 SourceLocation EndLoc) { 12686 Expr *ValExpr = ThreadLimit; 12687 Stmt *HelperValStmt = nullptr; 12688 12689 // OpenMP [teams Constrcut, Restrictions] 12690 // The thread_limit expression must evaluate to a positive integer value. 12691 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 12692 /*StrictlyPositive=*/true)) 12693 return nullptr; 12694 12695 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12696 OpenMPDirectiveKind CaptureRegion = 12697 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit); 12698 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12699 ValExpr = MakeFullExpr(ValExpr).get(); 12700 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12701 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12702 HelperValStmt = buildPreInits(Context, Captures); 12703 } 12704 12705 return new (Context) OMPThreadLimitClause( 12706 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 12707 } 12708 12709 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 12710 SourceLocation StartLoc, 12711 SourceLocation LParenLoc, 12712 SourceLocation EndLoc) { 12713 Expr *ValExpr = Priority; 12714 12715 // OpenMP [2.9.1, task Constrcut] 12716 // The priority-value is a non-negative numerical scalar expression. 12717 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority, 12718 /*StrictlyPositive=*/false)) 12719 return nullptr; 12720 12721 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc); 12722 } 12723 12724 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 12725 SourceLocation StartLoc, 12726 SourceLocation LParenLoc, 12727 SourceLocation EndLoc) { 12728 Expr *ValExpr = Grainsize; 12729 12730 // OpenMP [2.9.2, taskloop Constrcut] 12731 // The parameter of the grainsize clause must be a positive integer 12732 // expression. 12733 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize, 12734 /*StrictlyPositive=*/true)) 12735 return nullptr; 12736 12737 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc); 12738 } 12739 12740 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 12741 SourceLocation StartLoc, 12742 SourceLocation LParenLoc, 12743 SourceLocation EndLoc) { 12744 Expr *ValExpr = NumTasks; 12745 12746 // OpenMP [2.9.2, taskloop Constrcut] 12747 // The parameter of the num_tasks clause must be a positive integer 12748 // expression. 12749 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks, 12750 /*StrictlyPositive=*/true)) 12751 return nullptr; 12752 12753 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc); 12754 } 12755 12756 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 12757 SourceLocation LParenLoc, 12758 SourceLocation EndLoc) { 12759 // OpenMP [2.13.2, critical construct, Description] 12760 // ... where hint-expression is an integer constant expression that evaluates 12761 // to a valid lock hint. 12762 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 12763 if (HintExpr.isInvalid()) 12764 return nullptr; 12765 return new (Context) 12766 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 12767 } 12768 12769 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 12770 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 12771 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 12772 SourceLocation EndLoc) { 12773 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 12774 std::string Values; 12775 Values += "'"; 12776 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 12777 Values += "'"; 12778 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 12779 << Values << getOpenMPClauseName(OMPC_dist_schedule); 12780 return nullptr; 12781 } 12782 Expr *ValExpr = ChunkSize; 12783 Stmt *HelperValStmt = nullptr; 12784 if (ChunkSize) { 12785 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 12786 !ChunkSize->isInstantiationDependent() && 12787 !ChunkSize->containsUnexpandedParameterPack()) { 12788 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); 12789 ExprResult Val = 12790 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 12791 if (Val.isInvalid()) 12792 return nullptr; 12793 12794 ValExpr = Val.get(); 12795 12796 // OpenMP [2.7.1, Restrictions] 12797 // chunk_size must be a loop invariant integer expression with a positive 12798 // value. 12799 llvm::APSInt Result; 12800 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 12801 if (Result.isSigned() && !Result.isStrictlyPositive()) { 12802 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 12803 << "dist_schedule" << ChunkSize->getSourceRange(); 12804 return nullptr; 12805 } 12806 } else if (getOpenMPCaptureRegionForClause( 12807 DSAStack->getCurrentDirective(), OMPC_dist_schedule) != 12808 OMPD_unknown && 12809 !CurContext->isDependentContext()) { 12810 ValExpr = MakeFullExpr(ValExpr).get(); 12811 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12812 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12813 HelperValStmt = buildPreInits(Context, Captures); 12814 } 12815 } 12816 } 12817 12818 return new (Context) 12819 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 12820 Kind, ValExpr, HelperValStmt); 12821 } 12822 12823 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 12824 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 12825 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 12826 SourceLocation KindLoc, SourceLocation EndLoc) { 12827 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)' 12828 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) { 12829 std::string Value; 12830 SourceLocation Loc; 12831 Value += "'"; 12832 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 12833 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 12834 OMPC_DEFAULTMAP_MODIFIER_tofrom); 12835 Loc = MLoc; 12836 } else { 12837 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 12838 OMPC_DEFAULTMAP_scalar); 12839 Loc = KindLoc; 12840 } 12841 Value += "'"; 12842 Diag(Loc, diag::err_omp_unexpected_clause_value) 12843 << Value << getOpenMPClauseName(OMPC_defaultmap); 12844 return nullptr; 12845 } 12846 DSAStack->setDefaultDMAToFromScalar(StartLoc); 12847 12848 return new (Context) 12849 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 12850 } 12851 12852 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { 12853 DeclContext *CurLexicalContext = getCurLexicalContext(); 12854 if (!CurLexicalContext->isFileContext() && 12855 !CurLexicalContext->isExternCContext() && 12856 !CurLexicalContext->isExternCXXContext() && 12857 !isa<CXXRecordDecl>(CurLexicalContext) && 12858 !isa<ClassTemplateDecl>(CurLexicalContext) && 12859 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 12860 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 12861 Diag(Loc, diag::err_omp_region_not_file_context); 12862 return false; 12863 } 12864 if (IsInOpenMPDeclareTargetContext) { 12865 Diag(Loc, diag::err_omp_enclosed_declare_target); 12866 return false; 12867 } 12868 12869 IsInOpenMPDeclareTargetContext = true; 12870 return true; 12871 } 12872 12873 void Sema::ActOnFinishOpenMPDeclareTargetDirective() { 12874 assert(IsInOpenMPDeclareTargetContext && 12875 "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); 12876 IsInOpenMPDeclareTargetContext = false; 12877 } 12878 12879 void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, 12880 CXXScopeSpec &ScopeSpec, 12881 const DeclarationNameInfo &Id, 12882 OMPDeclareTargetDeclAttr::MapTypeTy MT, 12883 NamedDeclSetType &SameDirectiveDecls) { 12884 LookupResult Lookup(*this, Id, LookupOrdinaryName); 12885 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 12886 12887 if (Lookup.isAmbiguous()) 12888 return; 12889 Lookup.suppressDiagnostics(); 12890 12891 if (!Lookup.isSingleResult()) { 12892 if (TypoCorrection Corrected = 12893 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, 12894 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this), 12895 CTK_ErrorRecovery)) { 12896 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 12897 << Id.getName()); 12898 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 12899 return; 12900 } 12901 12902 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 12903 return; 12904 } 12905 12906 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 12907 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) { 12908 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) 12909 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); 12910 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) { 12911 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT); 12912 ND->addAttr(A); 12913 if (ASTMutationListener *ML = Context.getASTMutationListener()) 12914 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 12915 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc()); 12916 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) { 12917 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link) 12918 << Id.getName(); 12919 } 12920 } else { 12921 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 12922 } 12923 } 12924 12925 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 12926 Sema &SemaRef, Decl *D) { 12927 if (!D) 12928 return; 12929 const Decl *LD = nullptr; 12930 if (isa<TagDecl>(D)) { 12931 LD = cast<TagDecl>(D)->getDefinition(); 12932 } else if (isa<VarDecl>(D)) { 12933 LD = cast<VarDecl>(D)->getDefinition(); 12934 12935 // If this is an implicit variable that is legal and we do not need to do 12936 // anything. 12937 if (cast<VarDecl>(D)->isImplicit()) { 12938 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 12939 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); 12940 D->addAttr(A); 12941 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) 12942 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 12943 return; 12944 } 12945 } else if (const auto *F = dyn_cast<FunctionDecl>(D)) { 12946 const FunctionDecl *FD = nullptr; 12947 if (cast<FunctionDecl>(D)->hasBody(FD)) { 12948 LD = FD; 12949 // If the definition is associated with the current declaration in the 12950 // target region (it can be e.g. a lambda) that is legal and we do not 12951 // need to do anything else. 12952 if (LD == D) { 12953 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 12954 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); 12955 D->addAttr(A); 12956 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) 12957 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 12958 return; 12959 } 12960 } else if (F->isFunctionTemplateSpecialization() && 12961 F->getTemplateSpecializationKind() == 12962 TSK_ImplicitInstantiation) { 12963 // Check if the function is implicitly instantiated from the template 12964 // defined in the declare target region. 12965 const FunctionTemplateDecl *FTD = F->getPrimaryTemplate(); 12966 if (FTD && FTD->hasAttr<OMPDeclareTargetDeclAttr>()) 12967 return; 12968 } 12969 } 12970 if (!LD) 12971 LD = D; 12972 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() && 12973 ((isa<VarDecl>(LD) && !isa<ParmVarDecl>(LD)) || isa<FunctionDecl>(LD))) { 12974 // Outlined declaration is not declared target. 12975 if (!isa<FunctionDecl>(LD)) { 12976 if (LD->isOutOfLine()) { 12977 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context); 12978 SemaRef.Diag(SL, diag::note_used_here) << SR; 12979 } else { 12980 const DeclContext *DC = LD->getDeclContext(); 12981 while (DC && 12982 (!isa<FunctionDecl>(DC) || 12983 !cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())) 12984 DC = DC->getParent(); 12985 if (DC) 12986 return; 12987 12988 // Is not declared in target context. 12989 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context); 12990 SemaRef.Diag(SL, diag::note_used_here) << SR; 12991 } 12992 } 12993 // Mark decl as declared target to prevent further diagnostic. 12994 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 12995 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); 12996 D->addAttr(A); 12997 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) 12998 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 12999 } 13000 } 13001 13002 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 13003 Sema &SemaRef, DSAStackTy *Stack, 13004 ValueDecl *VD) { 13005 return VD->hasAttr<OMPDeclareTargetDeclAttr>() || 13006 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 13007 /*FullCheck=*/false); 13008 } 13009 13010 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 13011 SourceLocation IdLoc) { 13012 if (!D || D->isInvalidDecl()) 13013 return; 13014 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 13015 SourceLocation SL = E ? E->getLocStart() : D->getLocation(); 13016 if (auto *VD = dyn_cast<VarDecl>(D)) { 13017 // Only global variables can be marked as declare target. 13018 if (VD->isLocalVarDeclOrParm()) 13019 return; 13020 // 2.10.6: threadprivate variable cannot appear in a declare target 13021 // directive. 13022 if (DSAStack->isThreadPrivate(VD)) { 13023 Diag(SL, diag::err_omp_threadprivate_in_target); 13024 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 13025 return; 13026 } 13027 } 13028 if (auto *VD = dyn_cast<ValueDecl>(D)) { 13029 // Problem if any with var declared with incomplete type will be reported 13030 // as normal, so no need to check it here. 13031 if ((E || !VD->getType()->isIncompleteType()) && 13032 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) { 13033 // Mark decl as declared target to prevent further diagnostic. 13034 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD) || 13035 isa<FunctionTemplateDecl>(VD)) { 13036 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 13037 Context, OMPDeclareTargetDeclAttr::MT_To); 13038 VD->addAttr(A); 13039 if (ASTMutationListener *ML = Context.getASTMutationListener()) 13040 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A); 13041 } 13042 return; 13043 } 13044 } 13045 if (const auto *FD = dyn_cast<FunctionDecl>(D)) { 13046 if (FD->hasAttr<OMPDeclareTargetDeclAttr>() && 13047 (FD->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() == 13048 OMPDeclareTargetDeclAttr::MT_Link)) { 13049 assert(IdLoc.isValid() && "Source location is expected"); 13050 Diag(IdLoc, diag::err_omp_function_in_link_clause); 13051 Diag(FD->getLocation(), diag::note_defined_here) << FD; 13052 return; 13053 } 13054 } 13055 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) { 13056 if (FTD->hasAttr<OMPDeclareTargetDeclAttr>() && 13057 (FTD->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() == 13058 OMPDeclareTargetDeclAttr::MT_Link)) { 13059 assert(IdLoc.isValid() && "Source location is expected"); 13060 Diag(IdLoc, diag::err_omp_function_in_link_clause); 13061 Diag(FTD->getLocation(), diag::note_defined_here) << FTD; 13062 return; 13063 } 13064 } 13065 if (!E) { 13066 // Checking declaration inside declare target region. 13067 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() && 13068 (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 13069 isa<FunctionTemplateDecl>(D))) { 13070 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 13071 Context, OMPDeclareTargetDeclAttr::MT_To); 13072 D->addAttr(A); 13073 if (ASTMutationListener *ML = Context.getASTMutationListener()) 13074 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 13075 } 13076 return; 13077 } 13078 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 13079 } 13080 13081 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList, 13082 SourceLocation StartLoc, 13083 SourceLocation LParenLoc, 13084 SourceLocation EndLoc) { 13085 MappableVarListInfo MVLI(VarList); 13086 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc); 13087 if (MVLI.ProcessedVarList.empty()) 13088 return nullptr; 13089 13090 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc, 13091 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 13092 MVLI.VarComponents); 13093 } 13094 13095 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList, 13096 SourceLocation StartLoc, 13097 SourceLocation LParenLoc, 13098 SourceLocation EndLoc) { 13099 MappableVarListInfo MVLI(VarList); 13100 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc); 13101 if (MVLI.ProcessedVarList.empty()) 13102 return nullptr; 13103 13104 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc, 13105 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 13106 MVLI.VarComponents); 13107 } 13108 13109 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 13110 SourceLocation StartLoc, 13111 SourceLocation LParenLoc, 13112 SourceLocation EndLoc) { 13113 MappableVarListInfo MVLI(VarList); 13114 SmallVector<Expr *, 8> PrivateCopies; 13115 SmallVector<Expr *, 8> Inits; 13116 13117 for (Expr *RefExpr : VarList) { 13118 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 13119 SourceLocation ELoc; 13120 SourceRange ERange; 13121 Expr *SimpleRefExpr = RefExpr; 13122 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 13123 if (Res.second) { 13124 // It will be analyzed later. 13125 MVLI.ProcessedVarList.push_back(RefExpr); 13126 PrivateCopies.push_back(nullptr); 13127 Inits.push_back(nullptr); 13128 } 13129 ValueDecl *D = Res.first; 13130 if (!D) 13131 continue; 13132 13133 QualType Type = D->getType(); 13134 Type = Type.getNonReferenceType().getUnqualifiedType(); 13135 13136 auto *VD = dyn_cast<VarDecl>(D); 13137 13138 // Item should be a pointer or reference to pointer. 13139 if (!Type->isPointerType()) { 13140 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 13141 << 0 << RefExpr->getSourceRange(); 13142 continue; 13143 } 13144 13145 // Build the private variable and the expression that refers to it. 13146 auto VDPrivate = 13147 buildVarDecl(*this, ELoc, Type, D->getName(), 13148 D->hasAttrs() ? &D->getAttrs() : nullptr, 13149 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 13150 if (VDPrivate->isInvalidDecl()) 13151 continue; 13152 13153 CurContext->addDecl(VDPrivate); 13154 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 13155 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 13156 13157 // Add temporary variable to initialize the private copy of the pointer. 13158 VarDecl *VDInit = 13159 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 13160 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 13161 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 13162 AddInitializerToDecl(VDPrivate, 13163 DefaultLvalueConversion(VDInitRefExpr).get(), 13164 /*DirectInit=*/false); 13165 13166 // If required, build a capture to implement the privatization initialized 13167 // with the current list item value. 13168 DeclRefExpr *Ref = nullptr; 13169 if (!VD) 13170 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 13171 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 13172 PrivateCopies.push_back(VDPrivateRefExpr); 13173 Inits.push_back(VDInitRefExpr); 13174 13175 // We need to add a data sharing attribute for this variable to make sure it 13176 // is correctly captured. A variable that shows up in a use_device_ptr has 13177 // similar properties of a first private variable. 13178 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 13179 13180 // Create a mappable component for the list item. List items in this clause 13181 // only need a component. 13182 MVLI.VarBaseDeclarations.push_back(D); 13183 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 13184 MVLI.VarComponents.back().push_back( 13185 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D)); 13186 } 13187 13188 if (MVLI.ProcessedVarList.empty()) 13189 return nullptr; 13190 13191 return OMPUseDevicePtrClause::Create( 13192 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList, 13193 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents); 13194 } 13195 13196 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 13197 SourceLocation StartLoc, 13198 SourceLocation LParenLoc, 13199 SourceLocation EndLoc) { 13200 MappableVarListInfo MVLI(VarList); 13201 for (Expr *RefExpr : VarList) { 13202 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 13203 SourceLocation ELoc; 13204 SourceRange ERange; 13205 Expr *SimpleRefExpr = RefExpr; 13206 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 13207 if (Res.second) { 13208 // It will be analyzed later. 13209 MVLI.ProcessedVarList.push_back(RefExpr); 13210 } 13211 ValueDecl *D = Res.first; 13212 if (!D) 13213 continue; 13214 13215 QualType Type = D->getType(); 13216 // item should be a pointer or array or reference to pointer or array 13217 if (!Type.getNonReferenceType()->isPointerType() && 13218 !Type.getNonReferenceType()->isArrayType()) { 13219 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 13220 << 0 << RefExpr->getSourceRange(); 13221 continue; 13222 } 13223 13224 // Check if the declaration in the clause does not show up in any data 13225 // sharing attribute. 13226 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 13227 if (isOpenMPPrivate(DVar.CKind)) { 13228 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 13229 << getOpenMPClauseName(DVar.CKind) 13230 << getOpenMPClauseName(OMPC_is_device_ptr) 13231 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 13232 reportOriginalDsa(*this, DSAStack, D, DVar); 13233 continue; 13234 } 13235 13236 const Expr *ConflictExpr; 13237 if (DSAStack->checkMappableExprComponentListsForDecl( 13238 D, /*CurrentRegionOnly=*/true, 13239 [&ConflictExpr]( 13240 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 13241 OpenMPClauseKind) -> bool { 13242 ConflictExpr = R.front().getAssociatedExpression(); 13243 return true; 13244 })) { 13245 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 13246 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 13247 << ConflictExpr->getSourceRange(); 13248 continue; 13249 } 13250 13251 // Store the components in the stack so that they can be used to check 13252 // against other clauses later on. 13253 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D); 13254 DSAStack->addMappableExpressionComponents( 13255 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 13256 13257 // Record the expression we've just processed. 13258 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 13259 13260 // Create a mappable component for the list item. List items in this clause 13261 // only need a component. We use a null declaration to signal fields in 13262 // 'this'. 13263 assert((isa<DeclRefExpr>(SimpleRefExpr) || 13264 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 13265 "Unexpected device pointer expression!"); 13266 MVLI.VarBaseDeclarations.push_back( 13267 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 13268 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 13269 MVLI.VarComponents.back().push_back(MC); 13270 } 13271 13272 if (MVLI.ProcessedVarList.empty()) 13273 return nullptr; 13274 13275 return OMPIsDevicePtrClause::Create( 13276 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList, 13277 MVLI.VarBaseDeclarations, MVLI.VarComponents); 13278 } 13279