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) { 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()) { 1429 if (isInOpenMPDeclareTargetContext() && 1430 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) { 1431 // Try to mark variable as declare target if it is used in capturing 1432 // regions. 1433 if (!isDeclareTargetDeclaration(VD)) 1434 checkDeclIsAllowedInOpenMPTarget(nullptr, VD); 1435 return nullptr; 1436 } else if (isInOpenMPTargetExecutionDirective()) { 1437 // If the declaration is enclosed in a 'declare target' directive, 1438 // then it should not be captured. 1439 // 1440 if (isDeclareTargetDeclaration(VD)) 1441 return nullptr; 1442 return VD; 1443 } 1444 } 1445 1446 if (DSAStack->getCurrentDirective() != OMPD_unknown && 1447 (!DSAStack->isClauseParsingMode() || 1448 DSAStack->getParentDirective() != OMPD_unknown)) { 1449 auto &&Info = DSAStack->isLoopControlVariable(D); 1450 if (Info.first || 1451 (VD && VD->hasLocalStorage() && 1452 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) || 1453 (VD && DSAStack->isForceVarCapturing())) 1454 return VD ? VD : Info.second; 1455 DSAStackTy::DSAVarData DVarPrivate = 1456 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 1457 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind)) 1458 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1459 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, 1460 [](OpenMPDirectiveKind) { return true; }, 1461 DSAStack->isClauseParsingMode()); 1462 if (DVarPrivate.CKind != OMPC_unknown) 1463 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1464 } 1465 return nullptr; 1466 } 1467 1468 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex, 1469 unsigned Level) const { 1470 SmallVector<OpenMPDirectiveKind, 4> Regions; 1471 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level)); 1472 FunctionScopesIndex -= Regions.size(); 1473 } 1474 1475 bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const { 1476 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1477 return DSAStack->hasExplicitDSA( 1478 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) || 1479 (DSAStack->isClauseParsingMode() && 1480 DSAStack->getClauseParsingMode() == OMPC_private) || 1481 // Consider taskgroup reduction descriptor variable a private to avoid 1482 // possible capture in the region. 1483 (DSAStack->hasExplicitDirective( 1484 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; }, 1485 Level) && 1486 DSAStack->isTaskgroupReductionRef(D, Level)); 1487 } 1488 1489 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D, 1490 unsigned Level) { 1491 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1492 D = getCanonicalDecl(D); 1493 OpenMPClauseKind OMPC = OMPC_unknown; 1494 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) { 1495 const unsigned NewLevel = I - 1; 1496 if (DSAStack->hasExplicitDSA(D, 1497 [&OMPC](const OpenMPClauseKind K) { 1498 if (isOpenMPPrivate(K)) { 1499 OMPC = K; 1500 return true; 1501 } 1502 return false; 1503 }, 1504 NewLevel)) 1505 break; 1506 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel( 1507 D, NewLevel, 1508 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 1509 OpenMPClauseKind) { return true; })) { 1510 OMPC = OMPC_map; 1511 break; 1512 } 1513 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 1514 NewLevel)) { 1515 OMPC = OMPC_map; 1516 if (D->getType()->isScalarType() && 1517 DSAStack->getDefaultDMAAtLevel(NewLevel) != 1518 DefaultMapAttributes::DMA_tofrom_scalar) 1519 OMPC = OMPC_firstprivate; 1520 break; 1521 } 1522 } 1523 if (OMPC != OMPC_unknown) 1524 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC)); 1525 } 1526 1527 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, 1528 unsigned Level) const { 1529 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1530 // Return true if the current level is no longer enclosed in a target region. 1531 1532 const auto *VD = dyn_cast<VarDecl>(D); 1533 return VD && !VD->hasLocalStorage() && 1534 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 1535 Level); 1536 } 1537 1538 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 1539 1540 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 1541 const DeclarationNameInfo &DirName, 1542 Scope *CurScope, SourceLocation Loc) { 1543 DSAStack->push(DKind, DirName, CurScope, Loc); 1544 PushExpressionEvaluationContext( 1545 ExpressionEvaluationContext::PotentiallyEvaluated); 1546 } 1547 1548 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 1549 DSAStack->setClauseParsingMode(K); 1550 } 1551 1552 void Sema::EndOpenMPClause() { 1553 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 1554 } 1555 1556 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 1557 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 1558 // A variable of class type (or array thereof) that appears in a lastprivate 1559 // clause requires an accessible, unambiguous default constructor for the 1560 // class type, unless the list item is also specified in a firstprivate 1561 // clause. 1562 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 1563 for (OMPClause *C : D->clauses()) { 1564 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 1565 SmallVector<Expr *, 8> PrivateCopies; 1566 for (Expr *DE : Clause->varlists()) { 1567 if (DE->isValueDependent() || DE->isTypeDependent()) { 1568 PrivateCopies.push_back(nullptr); 1569 continue; 1570 } 1571 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 1572 auto *VD = cast<VarDecl>(DRE->getDecl()); 1573 QualType Type = VD->getType().getNonReferenceType(); 1574 const DSAStackTy::DSAVarData DVar = 1575 DSAStack->getTopDSA(VD, /*FromParent=*/false); 1576 if (DVar.CKind == OMPC_lastprivate) { 1577 // Generate helper private variable and initialize it with the 1578 // default value. The address of the original variable is replaced 1579 // by the address of the new private variable in CodeGen. This new 1580 // variable is not added to IdResolver, so the code in the OpenMP 1581 // region uses original variable for proper diagnostics. 1582 VarDecl *VDPrivate = buildVarDecl( 1583 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 1584 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE); 1585 ActOnUninitializedDecl(VDPrivate); 1586 if (VDPrivate->isInvalidDecl()) 1587 continue; 1588 PrivateCopies.push_back(buildDeclRefExpr( 1589 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 1590 } else { 1591 // The variable is also a firstprivate, so initialization sequence 1592 // for private copy is generated already. 1593 PrivateCopies.push_back(nullptr); 1594 } 1595 } 1596 // Set initializers to private copies if no errors were found. 1597 if (PrivateCopies.size() == Clause->varlist_size()) 1598 Clause->setPrivateCopies(PrivateCopies); 1599 } 1600 } 1601 } 1602 1603 DSAStack->pop(); 1604 DiscardCleanupsInEvaluationContext(); 1605 PopExpressionEvaluationContext(); 1606 } 1607 1608 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 1609 Expr *NumIterations, Sema &SemaRef, 1610 Scope *S, DSAStackTy *Stack); 1611 1612 namespace { 1613 1614 class VarDeclFilterCCC final : public CorrectionCandidateCallback { 1615 private: 1616 Sema &SemaRef; 1617 1618 public: 1619 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 1620 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1621 NamedDecl *ND = Candidate.getCorrectionDecl(); 1622 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 1623 return VD->hasGlobalStorage() && 1624 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1625 SemaRef.getCurScope()); 1626 } 1627 return false; 1628 } 1629 }; 1630 1631 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback { 1632 private: 1633 Sema &SemaRef; 1634 1635 public: 1636 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 1637 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1638 NamedDecl *ND = Candidate.getCorrectionDecl(); 1639 if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) { 1640 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1641 SemaRef.getCurScope()); 1642 } 1643 return false; 1644 } 1645 }; 1646 1647 } // namespace 1648 1649 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 1650 CXXScopeSpec &ScopeSpec, 1651 const DeclarationNameInfo &Id) { 1652 LookupResult Lookup(*this, Id, LookupOrdinaryName); 1653 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 1654 1655 if (Lookup.isAmbiguous()) 1656 return ExprError(); 1657 1658 VarDecl *VD; 1659 if (!Lookup.isSingleResult()) { 1660 if (TypoCorrection Corrected = CorrectTypo( 1661 Id, LookupOrdinaryName, CurScope, nullptr, 1662 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) { 1663 diagnoseTypo(Corrected, 1664 PDiag(Lookup.empty() 1665 ? diag::err_undeclared_var_use_suggest 1666 : diag::err_omp_expected_var_arg_suggest) 1667 << Id.getName()); 1668 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 1669 } else { 1670 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 1671 : diag::err_omp_expected_var_arg) 1672 << Id.getName(); 1673 return ExprError(); 1674 } 1675 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) { 1676 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 1677 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 1678 return ExprError(); 1679 } 1680 Lookup.suppressDiagnostics(); 1681 1682 // OpenMP [2.9.2, Syntax, C/C++] 1683 // Variables must be file-scope, namespace-scope, or static block-scope. 1684 if (!VD->hasGlobalStorage()) { 1685 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 1686 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal(); 1687 bool IsDecl = 1688 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1689 Diag(VD->getLocation(), 1690 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1691 << VD; 1692 return ExprError(); 1693 } 1694 1695 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 1696 NamedDecl *ND = CanonicalVD; 1697 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 1698 // A threadprivate directive for file-scope variables must appear outside 1699 // any definition or declaration. 1700 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 1701 !getCurLexicalContext()->isTranslationUnit()) { 1702 Diag(Id.getLoc(), diag::err_omp_var_scope) 1703 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1704 bool IsDecl = 1705 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1706 Diag(VD->getLocation(), 1707 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1708 << VD; 1709 return ExprError(); 1710 } 1711 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 1712 // A threadprivate directive for static class member variables must appear 1713 // in the class definition, in the same scope in which the member 1714 // variables are declared. 1715 if (CanonicalVD->isStaticDataMember() && 1716 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 1717 Diag(Id.getLoc(), diag::err_omp_var_scope) 1718 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1719 bool IsDecl = 1720 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1721 Diag(VD->getLocation(), 1722 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1723 << VD; 1724 return ExprError(); 1725 } 1726 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 1727 // A threadprivate directive for namespace-scope variables must appear 1728 // outside any definition or declaration other than the namespace 1729 // definition itself. 1730 if (CanonicalVD->getDeclContext()->isNamespace() && 1731 (!getCurLexicalContext()->isFileContext() || 1732 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 1733 Diag(Id.getLoc(), diag::err_omp_var_scope) 1734 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1735 bool IsDecl = 1736 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1737 Diag(VD->getLocation(), 1738 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1739 << VD; 1740 return ExprError(); 1741 } 1742 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 1743 // A threadprivate directive for static block-scope variables must appear 1744 // in the scope of the variable and not in a nested scope. 1745 if (CanonicalVD->isStaticLocal() && CurScope && 1746 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 1747 Diag(Id.getLoc(), diag::err_omp_var_scope) 1748 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1749 bool IsDecl = 1750 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1751 Diag(VD->getLocation(), 1752 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1753 << VD; 1754 return ExprError(); 1755 } 1756 1757 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 1758 // A threadprivate directive must lexically precede all references to any 1759 // of the variables in its list. 1760 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) { 1761 Diag(Id.getLoc(), diag::err_omp_var_used) 1762 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1763 return ExprError(); 1764 } 1765 1766 QualType ExprType = VD->getType().getNonReferenceType(); 1767 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 1768 SourceLocation(), VD, 1769 /*RefersToEnclosingVariableOrCapture=*/false, 1770 Id.getLoc(), ExprType, VK_LValue); 1771 } 1772 1773 Sema::DeclGroupPtrTy 1774 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 1775 ArrayRef<Expr *> VarList) { 1776 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 1777 CurContext->addDecl(D); 1778 return DeclGroupPtrTy::make(DeclGroupRef(D)); 1779 } 1780 return nullptr; 1781 } 1782 1783 namespace { 1784 class LocalVarRefChecker final 1785 : public ConstStmtVisitor<LocalVarRefChecker, bool> { 1786 Sema &SemaRef; 1787 1788 public: 1789 bool VisitDeclRefExpr(const DeclRefExpr *E) { 1790 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 1791 if (VD->hasLocalStorage()) { 1792 SemaRef.Diag(E->getLocStart(), 1793 diag::err_omp_local_var_in_threadprivate_init) 1794 << E->getSourceRange(); 1795 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 1796 << VD << VD->getSourceRange(); 1797 return true; 1798 } 1799 } 1800 return false; 1801 } 1802 bool VisitStmt(const Stmt *S) { 1803 for (const Stmt *Child : S->children()) { 1804 if (Child && Visit(Child)) 1805 return true; 1806 } 1807 return false; 1808 } 1809 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 1810 }; 1811 } // namespace 1812 1813 OMPThreadPrivateDecl * 1814 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 1815 SmallVector<Expr *, 8> Vars; 1816 for (Expr *RefExpr : VarList) { 1817 auto *DE = cast<DeclRefExpr>(RefExpr); 1818 auto *VD = cast<VarDecl>(DE->getDecl()); 1819 SourceLocation ILoc = DE->getExprLoc(); 1820 1821 // Mark variable as used. 1822 VD->setReferenced(); 1823 VD->markUsed(Context); 1824 1825 QualType QType = VD->getType(); 1826 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 1827 // It will be analyzed later. 1828 Vars.push_back(DE); 1829 continue; 1830 } 1831 1832 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 1833 // A threadprivate variable must not have an incomplete type. 1834 if (RequireCompleteType(ILoc, VD->getType(), 1835 diag::err_omp_threadprivate_incomplete_type)) { 1836 continue; 1837 } 1838 1839 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 1840 // A threadprivate variable must not have a reference type. 1841 if (VD->getType()->isReferenceType()) { 1842 Diag(ILoc, diag::err_omp_ref_type_arg) 1843 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 1844 bool IsDecl = 1845 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1846 Diag(VD->getLocation(), 1847 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1848 << VD; 1849 continue; 1850 } 1851 1852 // Check if this is a TLS variable. If TLS is not being supported, produce 1853 // the corresponding diagnostic. 1854 if ((VD->getTLSKind() != VarDecl::TLS_None && 1855 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1856 getLangOpts().OpenMPUseTLS && 1857 getASTContext().getTargetInfo().isTLSSupported())) || 1858 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 1859 !VD->isLocalVarDecl())) { 1860 Diag(ILoc, diag::err_omp_var_thread_local) 1861 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 1862 bool IsDecl = 1863 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1864 Diag(VD->getLocation(), 1865 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1866 << VD; 1867 continue; 1868 } 1869 1870 // Check if initial value of threadprivate variable reference variable with 1871 // local storage (it is not supported by runtime). 1872 if (const Expr *Init = VD->getAnyInitializer()) { 1873 LocalVarRefChecker Checker(*this); 1874 if (Checker.Visit(Init)) 1875 continue; 1876 } 1877 1878 Vars.push_back(RefExpr); 1879 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 1880 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 1881 Context, SourceRange(Loc, Loc))); 1882 if (ASTMutationListener *ML = Context.getASTMutationListener()) 1883 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 1884 } 1885 OMPThreadPrivateDecl *D = nullptr; 1886 if (!Vars.empty()) { 1887 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 1888 Vars); 1889 D->setAccess(AS_public); 1890 } 1891 return D; 1892 } 1893 1894 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 1895 const ValueDecl *D, 1896 const DSAStackTy::DSAVarData &DVar, 1897 bool IsLoopIterVar = false) { 1898 if (DVar.RefExpr) { 1899 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 1900 << getOpenMPClauseName(DVar.CKind); 1901 return; 1902 } 1903 enum { 1904 PDSA_StaticMemberShared, 1905 PDSA_StaticLocalVarShared, 1906 PDSA_LoopIterVarPrivate, 1907 PDSA_LoopIterVarLinear, 1908 PDSA_LoopIterVarLastprivate, 1909 PDSA_ConstVarShared, 1910 PDSA_GlobalVarShared, 1911 PDSA_TaskVarFirstprivate, 1912 PDSA_LocalVarPrivate, 1913 PDSA_Implicit 1914 } Reason = PDSA_Implicit; 1915 bool ReportHint = false; 1916 auto ReportLoc = D->getLocation(); 1917 auto *VD = dyn_cast<VarDecl>(D); 1918 if (IsLoopIterVar) { 1919 if (DVar.CKind == OMPC_private) 1920 Reason = PDSA_LoopIterVarPrivate; 1921 else if (DVar.CKind == OMPC_lastprivate) 1922 Reason = PDSA_LoopIterVarLastprivate; 1923 else 1924 Reason = PDSA_LoopIterVarLinear; 1925 } else if (isOpenMPTaskingDirective(DVar.DKind) && 1926 DVar.CKind == OMPC_firstprivate) { 1927 Reason = PDSA_TaskVarFirstprivate; 1928 ReportLoc = DVar.ImplicitDSALoc; 1929 } else if (VD && VD->isStaticLocal()) 1930 Reason = PDSA_StaticLocalVarShared; 1931 else if (VD && VD->isStaticDataMember()) 1932 Reason = PDSA_StaticMemberShared; 1933 else if (VD && VD->isFileVarDecl()) 1934 Reason = PDSA_GlobalVarShared; 1935 else if (D->getType().isConstant(SemaRef.getASTContext())) 1936 Reason = PDSA_ConstVarShared; 1937 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 1938 ReportHint = true; 1939 Reason = PDSA_LocalVarPrivate; 1940 } 1941 if (Reason != PDSA_Implicit) { 1942 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 1943 << Reason << ReportHint 1944 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 1945 } else if (DVar.ImplicitDSALoc.isValid()) { 1946 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 1947 << getOpenMPClauseName(DVar.CKind); 1948 } 1949 } 1950 1951 namespace { 1952 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> { 1953 DSAStackTy *Stack; 1954 Sema &SemaRef; 1955 bool ErrorFound = false; 1956 CapturedStmt *CS = nullptr; 1957 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate; 1958 llvm::SmallVector<Expr *, 4> ImplicitMap; 1959 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA; 1960 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations; 1961 1962 public: 1963 void VisitDeclRefExpr(DeclRefExpr *E) { 1964 if (E->isTypeDependent() || E->isValueDependent() || 1965 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 1966 return; 1967 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 1968 VD = VD->getCanonicalDecl(); 1969 // Skip internally declared variables. 1970 if (VD->hasLocalStorage() && !CS->capturesVariable(VD)) 1971 return; 1972 1973 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 1974 // Check if the variable has explicit DSA set and stop analysis if it so. 1975 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second) 1976 return; 1977 1978 // Skip internally declared static variables. 1979 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 1980 isDeclareTargetDeclaration(VD); 1981 if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) && 1982 (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link)) 1983 return; 1984 1985 SourceLocation ELoc = E->getExprLoc(); 1986 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 1987 // The default(none) clause requires that each variable that is referenced 1988 // in the construct, and does not have a predetermined data-sharing 1989 // attribute, must have its data-sharing attribute explicitly determined 1990 // by being listed in a data-sharing attribute clause. 1991 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && 1992 isParallelOrTaskRegion(DKind) && 1993 VarsWithInheritedDSA.count(VD) == 0) { 1994 VarsWithInheritedDSA[VD] = E; 1995 return; 1996 } 1997 1998 if (isOpenMPTargetExecutionDirective(DKind) && 1999 !Stack->isLoopControlVariable(VD).first) { 2000 if (!Stack->checkMappableExprComponentListsForDecl( 2001 VD, /*CurrentRegionOnly=*/true, 2002 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 2003 StackComponents, 2004 OpenMPClauseKind) { 2005 // Variable is used if it has been marked as an array, array 2006 // section or the variable iself. 2007 return StackComponents.size() == 1 || 2008 std::all_of( 2009 std::next(StackComponents.rbegin()), 2010 StackComponents.rend(), 2011 [](const OMPClauseMappableExprCommon:: 2012 MappableComponent &MC) { 2013 return MC.getAssociatedDeclaration() == 2014 nullptr && 2015 (isa<OMPArraySectionExpr>( 2016 MC.getAssociatedExpression()) || 2017 isa<ArraySubscriptExpr>( 2018 MC.getAssociatedExpression())); 2019 }); 2020 })) { 2021 bool IsFirstprivate = false; 2022 // By default lambdas are captured as firstprivates. 2023 if (const auto *RD = 2024 VD->getType().getNonReferenceType()->getAsCXXRecordDecl()) 2025 IsFirstprivate = RD->isLambda(); 2026 IsFirstprivate = 2027 IsFirstprivate || 2028 (VD->getType().getNonReferenceType()->isScalarType() && 2029 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res); 2030 if (IsFirstprivate) 2031 ImplicitFirstprivate.emplace_back(E); 2032 else 2033 ImplicitMap.emplace_back(E); 2034 return; 2035 } 2036 } 2037 2038 // OpenMP [2.9.3.6, Restrictions, p.2] 2039 // A list item that appears in a reduction clause of the innermost 2040 // enclosing worksharing or parallel construct may not be accessed in an 2041 // explicit task. 2042 DVar = Stack->hasInnermostDSA( 2043 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 2044 [](OpenMPDirectiveKind K) { 2045 return isOpenMPParallelDirective(K) || 2046 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 2047 }, 2048 /*FromParent=*/true); 2049 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 2050 ErrorFound = true; 2051 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 2052 reportOriginalDsa(SemaRef, Stack, VD, DVar); 2053 return; 2054 } 2055 2056 // Define implicit data-sharing attributes for task. 2057 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false); 2058 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 2059 !Stack->isLoopControlVariable(VD).first) 2060 ImplicitFirstprivate.push_back(E); 2061 } 2062 } 2063 void VisitMemberExpr(MemberExpr *E) { 2064 if (E->isTypeDependent() || E->isValueDependent() || 2065 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 2066 return; 2067 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 2068 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 2069 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 2070 if (!FD) 2071 return; 2072 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false); 2073 // Check if the variable has explicit DSA set and stop analysis if it 2074 // so. 2075 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second) 2076 return; 2077 2078 if (isOpenMPTargetExecutionDirective(DKind) && 2079 !Stack->isLoopControlVariable(FD).first && 2080 !Stack->checkMappableExprComponentListsForDecl( 2081 FD, /*CurrentRegionOnly=*/true, 2082 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 2083 StackComponents, 2084 OpenMPClauseKind) { 2085 return isa<CXXThisExpr>( 2086 cast<MemberExpr>( 2087 StackComponents.back().getAssociatedExpression()) 2088 ->getBase() 2089 ->IgnoreParens()); 2090 })) { 2091 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 2092 // A bit-field cannot appear in a map clause. 2093 // 2094 if (FD->isBitField()) 2095 return; 2096 ImplicitMap.emplace_back(E); 2097 return; 2098 } 2099 2100 SourceLocation ELoc = E->getExprLoc(); 2101 // OpenMP [2.9.3.6, Restrictions, p.2] 2102 // A list item that appears in a reduction clause of the innermost 2103 // enclosing worksharing or parallel construct may not be accessed in 2104 // an explicit task. 2105 DVar = Stack->hasInnermostDSA( 2106 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 2107 [](OpenMPDirectiveKind K) { 2108 return isOpenMPParallelDirective(K) || 2109 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 2110 }, 2111 /*FromParent=*/true); 2112 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 2113 ErrorFound = true; 2114 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 2115 reportOriginalDsa(SemaRef, Stack, FD, DVar); 2116 return; 2117 } 2118 2119 // Define implicit data-sharing attributes for task. 2120 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false); 2121 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 2122 !Stack->isLoopControlVariable(FD).first) 2123 ImplicitFirstprivate.push_back(E); 2124 return; 2125 } 2126 if (isOpenMPTargetExecutionDirective(DKind)) { 2127 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 2128 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map, 2129 /*NoDiagnose=*/true)) 2130 return; 2131 const auto *VD = cast<ValueDecl>( 2132 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl()); 2133 if (!Stack->checkMappableExprComponentListsForDecl( 2134 VD, /*CurrentRegionOnly=*/true, 2135 [&CurComponents]( 2136 OMPClauseMappableExprCommon::MappableExprComponentListRef 2137 StackComponents, 2138 OpenMPClauseKind) { 2139 auto CCI = CurComponents.rbegin(); 2140 auto CCE = CurComponents.rend(); 2141 for (const auto &SC : llvm::reverse(StackComponents)) { 2142 // Do both expressions have the same kind? 2143 if (CCI->getAssociatedExpression()->getStmtClass() != 2144 SC.getAssociatedExpression()->getStmtClass()) 2145 if (!(isa<OMPArraySectionExpr>( 2146 SC.getAssociatedExpression()) && 2147 isa<ArraySubscriptExpr>( 2148 CCI->getAssociatedExpression()))) 2149 return false; 2150 2151 const Decl *CCD = CCI->getAssociatedDeclaration(); 2152 const Decl *SCD = SC.getAssociatedDeclaration(); 2153 CCD = CCD ? CCD->getCanonicalDecl() : nullptr; 2154 SCD = SCD ? SCD->getCanonicalDecl() : nullptr; 2155 if (SCD != CCD) 2156 return false; 2157 std::advance(CCI, 1); 2158 if (CCI == CCE) 2159 break; 2160 } 2161 return true; 2162 })) { 2163 Visit(E->getBase()); 2164 } 2165 } else { 2166 Visit(E->getBase()); 2167 } 2168 } 2169 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 2170 for (OMPClause *C : S->clauses()) { 2171 // Skip analysis of arguments of implicitly defined firstprivate clause 2172 // for task|target directives. 2173 // Skip analysis of arguments of implicitly defined map clause for target 2174 // directives. 2175 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) && 2176 C->isImplicit())) { 2177 for (Stmt *CC : C->children()) { 2178 if (CC) 2179 Visit(CC); 2180 } 2181 } 2182 } 2183 } 2184 void VisitStmt(Stmt *S) { 2185 for (Stmt *C : S->children()) { 2186 if (C && !isa<OMPExecutableDirective>(C)) 2187 Visit(C); 2188 } 2189 } 2190 2191 bool isErrorFound() const { return ErrorFound; } 2192 ArrayRef<Expr *> getImplicitFirstprivate() const { 2193 return ImplicitFirstprivate; 2194 } 2195 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; } 2196 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const { 2197 return VarsWithInheritedDSA; 2198 } 2199 2200 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 2201 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {} 2202 }; 2203 } // namespace 2204 2205 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 2206 switch (DKind) { 2207 case OMPD_parallel: 2208 case OMPD_parallel_for: 2209 case OMPD_parallel_for_simd: 2210 case OMPD_parallel_sections: 2211 case OMPD_teams: 2212 case OMPD_teams_distribute: 2213 case OMPD_teams_distribute_simd: { 2214 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2215 QualType KmpInt32PtrTy = 2216 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2217 Sema::CapturedParamNameType Params[] = { 2218 std::make_pair(".global_tid.", KmpInt32PtrTy), 2219 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2220 std::make_pair(StringRef(), QualType()) // __context with shared vars 2221 }; 2222 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2223 Params); 2224 break; 2225 } 2226 case OMPD_target_teams: 2227 case OMPD_target_parallel: 2228 case OMPD_target_parallel_for: 2229 case OMPD_target_parallel_for_simd: 2230 case OMPD_target_teams_distribute: 2231 case OMPD_target_teams_distribute_simd: { 2232 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2233 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2234 QualType KmpInt32PtrTy = 2235 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2236 QualType Args[] = {VoidPtrTy}; 2237 FunctionProtoType::ExtProtoInfo EPI; 2238 EPI.Variadic = true; 2239 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2240 Sema::CapturedParamNameType Params[] = { 2241 std::make_pair(".global_tid.", KmpInt32Ty), 2242 std::make_pair(".part_id.", KmpInt32PtrTy), 2243 std::make_pair(".privates.", VoidPtrTy), 2244 std::make_pair( 2245 ".copy_fn.", 2246 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2247 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2248 std::make_pair(StringRef(), QualType()) // __context with shared vars 2249 }; 2250 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2251 Params); 2252 // Mark this captured region as inlined, because we don't use outlined 2253 // function directly. 2254 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2255 AlwaysInlineAttr::CreateImplicit( 2256 Context, AlwaysInlineAttr::Keyword_forceinline)); 2257 Sema::CapturedParamNameType ParamsTarget[] = { 2258 std::make_pair(StringRef(), QualType()) // __context with shared vars 2259 }; 2260 // Start a captured region for 'target' with no implicit parameters. 2261 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2262 ParamsTarget); 2263 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 2264 std::make_pair(".global_tid.", KmpInt32PtrTy), 2265 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2266 std::make_pair(StringRef(), QualType()) // __context with shared vars 2267 }; 2268 // Start a captured region for 'teams' or 'parallel'. Both regions have 2269 // the same implicit parameters. 2270 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2271 ParamsTeamsOrParallel); 2272 break; 2273 } 2274 case OMPD_target: 2275 case OMPD_target_simd: { 2276 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2277 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2278 QualType KmpInt32PtrTy = 2279 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2280 QualType Args[] = {VoidPtrTy}; 2281 FunctionProtoType::ExtProtoInfo EPI; 2282 EPI.Variadic = true; 2283 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2284 Sema::CapturedParamNameType Params[] = { 2285 std::make_pair(".global_tid.", KmpInt32Ty), 2286 std::make_pair(".part_id.", KmpInt32PtrTy), 2287 std::make_pair(".privates.", VoidPtrTy), 2288 std::make_pair( 2289 ".copy_fn.", 2290 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2291 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2292 std::make_pair(StringRef(), QualType()) // __context with shared vars 2293 }; 2294 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2295 Params); 2296 // Mark this captured region as inlined, because we don't use outlined 2297 // function directly. 2298 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2299 AlwaysInlineAttr::CreateImplicit( 2300 Context, AlwaysInlineAttr::Keyword_forceinline)); 2301 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2302 std::make_pair(StringRef(), QualType())); 2303 break; 2304 } 2305 case OMPD_simd: 2306 case OMPD_for: 2307 case OMPD_for_simd: 2308 case OMPD_sections: 2309 case OMPD_section: 2310 case OMPD_single: 2311 case OMPD_master: 2312 case OMPD_critical: 2313 case OMPD_taskgroup: 2314 case OMPD_distribute: 2315 case OMPD_distribute_simd: 2316 case OMPD_ordered: 2317 case OMPD_atomic: 2318 case OMPD_target_data: { 2319 Sema::CapturedParamNameType Params[] = { 2320 std::make_pair(StringRef(), QualType()) // __context with shared vars 2321 }; 2322 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2323 Params); 2324 break; 2325 } 2326 case OMPD_task: { 2327 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2328 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2329 QualType KmpInt32PtrTy = 2330 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2331 QualType Args[] = {VoidPtrTy}; 2332 FunctionProtoType::ExtProtoInfo EPI; 2333 EPI.Variadic = true; 2334 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2335 Sema::CapturedParamNameType Params[] = { 2336 std::make_pair(".global_tid.", KmpInt32Ty), 2337 std::make_pair(".part_id.", KmpInt32PtrTy), 2338 std::make_pair(".privates.", VoidPtrTy), 2339 std::make_pair( 2340 ".copy_fn.", 2341 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2342 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2343 std::make_pair(StringRef(), QualType()) // __context with shared vars 2344 }; 2345 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2346 Params); 2347 // Mark this captured region as inlined, because we don't use outlined 2348 // function directly. 2349 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2350 AlwaysInlineAttr::CreateImplicit( 2351 Context, AlwaysInlineAttr::Keyword_forceinline)); 2352 break; 2353 } 2354 case OMPD_taskloop: 2355 case OMPD_taskloop_simd: { 2356 QualType KmpInt32Ty = 2357 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 2358 .withConst(); 2359 QualType KmpUInt64Ty = 2360 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 2361 .withConst(); 2362 QualType KmpInt64Ty = 2363 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 2364 .withConst(); 2365 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2366 QualType KmpInt32PtrTy = 2367 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2368 QualType Args[] = {VoidPtrTy}; 2369 FunctionProtoType::ExtProtoInfo EPI; 2370 EPI.Variadic = true; 2371 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2372 Sema::CapturedParamNameType Params[] = { 2373 std::make_pair(".global_tid.", KmpInt32Ty), 2374 std::make_pair(".part_id.", KmpInt32PtrTy), 2375 std::make_pair(".privates.", VoidPtrTy), 2376 std::make_pair( 2377 ".copy_fn.", 2378 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2379 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2380 std::make_pair(".lb.", KmpUInt64Ty), 2381 std::make_pair(".ub.", KmpUInt64Ty), 2382 std::make_pair(".st.", KmpInt64Ty), 2383 std::make_pair(".liter.", KmpInt32Ty), 2384 std::make_pair(".reductions.", VoidPtrTy), 2385 std::make_pair(StringRef(), QualType()) // __context with shared vars 2386 }; 2387 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2388 Params); 2389 // Mark this captured region as inlined, because we don't use outlined 2390 // function directly. 2391 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2392 AlwaysInlineAttr::CreateImplicit( 2393 Context, AlwaysInlineAttr::Keyword_forceinline)); 2394 break; 2395 } 2396 case OMPD_distribute_parallel_for_simd: 2397 case OMPD_distribute_parallel_for: { 2398 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2399 QualType KmpInt32PtrTy = 2400 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2401 Sema::CapturedParamNameType Params[] = { 2402 std::make_pair(".global_tid.", KmpInt32PtrTy), 2403 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2404 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 2405 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 2406 std::make_pair(StringRef(), QualType()) // __context with shared vars 2407 }; 2408 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2409 Params); 2410 break; 2411 } 2412 case OMPD_target_teams_distribute_parallel_for: 2413 case OMPD_target_teams_distribute_parallel_for_simd: { 2414 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2415 QualType KmpInt32PtrTy = 2416 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2417 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2418 2419 QualType Args[] = {VoidPtrTy}; 2420 FunctionProtoType::ExtProtoInfo EPI; 2421 EPI.Variadic = true; 2422 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2423 Sema::CapturedParamNameType Params[] = { 2424 std::make_pair(".global_tid.", KmpInt32Ty), 2425 std::make_pair(".part_id.", KmpInt32PtrTy), 2426 std::make_pair(".privates.", VoidPtrTy), 2427 std::make_pair( 2428 ".copy_fn.", 2429 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2430 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2431 std::make_pair(StringRef(), QualType()) // __context with shared vars 2432 }; 2433 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2434 Params); 2435 // Mark this captured region as inlined, because we don't use outlined 2436 // function directly. 2437 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2438 AlwaysInlineAttr::CreateImplicit( 2439 Context, AlwaysInlineAttr::Keyword_forceinline)); 2440 Sema::CapturedParamNameType ParamsTarget[] = { 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 ParamsTarget); 2446 2447 Sema::CapturedParamNameType ParamsTeams[] = { 2448 std::make_pair(".global_tid.", KmpInt32PtrTy), 2449 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2450 std::make_pair(StringRef(), QualType()) // __context with shared vars 2451 }; 2452 // Start a captured region for 'target' with no implicit parameters. 2453 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2454 ParamsTeams); 2455 2456 Sema::CapturedParamNameType ParamsParallel[] = { 2457 std::make_pair(".global_tid.", KmpInt32PtrTy), 2458 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2459 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 2460 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 2461 std::make_pair(StringRef(), QualType()) // __context with shared vars 2462 }; 2463 // Start a captured region for 'teams' or 'parallel'. Both regions have 2464 // the same implicit parameters. 2465 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2466 ParamsParallel); 2467 break; 2468 } 2469 2470 case OMPD_teams_distribute_parallel_for: 2471 case OMPD_teams_distribute_parallel_for_simd: { 2472 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2473 QualType KmpInt32PtrTy = 2474 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2475 2476 Sema::CapturedParamNameType ParamsTeams[] = { 2477 std::make_pair(".global_tid.", KmpInt32PtrTy), 2478 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2479 std::make_pair(StringRef(), QualType()) // __context with shared vars 2480 }; 2481 // Start a captured region for 'target' with no implicit parameters. 2482 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2483 ParamsTeams); 2484 2485 Sema::CapturedParamNameType ParamsParallel[] = { 2486 std::make_pair(".global_tid.", KmpInt32PtrTy), 2487 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2488 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 2489 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 2490 std::make_pair(StringRef(), QualType()) // __context with shared vars 2491 }; 2492 // Start a captured region for 'teams' or 'parallel'. Both regions have 2493 // the same implicit parameters. 2494 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2495 ParamsParallel); 2496 break; 2497 } 2498 case OMPD_target_update: 2499 case OMPD_target_enter_data: 2500 case OMPD_target_exit_data: { 2501 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2502 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2503 QualType KmpInt32PtrTy = 2504 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2505 QualType Args[] = {VoidPtrTy}; 2506 FunctionProtoType::ExtProtoInfo EPI; 2507 EPI.Variadic = true; 2508 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2509 Sema::CapturedParamNameType Params[] = { 2510 std::make_pair(".global_tid.", KmpInt32Ty), 2511 std::make_pair(".part_id.", KmpInt32PtrTy), 2512 std::make_pair(".privates.", VoidPtrTy), 2513 std::make_pair( 2514 ".copy_fn.", 2515 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2516 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2517 std::make_pair(StringRef(), QualType()) // __context with shared vars 2518 }; 2519 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2520 Params); 2521 // Mark this captured region as inlined, because we don't use outlined 2522 // function directly. 2523 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2524 AlwaysInlineAttr::CreateImplicit( 2525 Context, AlwaysInlineAttr::Keyword_forceinline)); 2526 break; 2527 } 2528 case OMPD_threadprivate: 2529 case OMPD_taskyield: 2530 case OMPD_barrier: 2531 case OMPD_taskwait: 2532 case OMPD_cancellation_point: 2533 case OMPD_cancel: 2534 case OMPD_flush: 2535 case OMPD_declare_reduction: 2536 case OMPD_declare_simd: 2537 case OMPD_declare_target: 2538 case OMPD_end_declare_target: 2539 llvm_unreachable("OpenMP Directive is not allowed"); 2540 case OMPD_unknown: 2541 llvm_unreachable("Unknown OpenMP directive"); 2542 } 2543 } 2544 2545 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 2546 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2547 getOpenMPCaptureRegions(CaptureRegions, DKind); 2548 return CaptureRegions.size(); 2549 } 2550 2551 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 2552 Expr *CaptureExpr, bool WithInit, 2553 bool AsExpression) { 2554 assert(CaptureExpr); 2555 ASTContext &C = S.getASTContext(); 2556 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 2557 QualType Ty = Init->getType(); 2558 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 2559 if (S.getLangOpts().CPlusPlus) { 2560 Ty = C.getLValueReferenceType(Ty); 2561 } else { 2562 Ty = C.getPointerType(Ty); 2563 ExprResult Res = 2564 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 2565 if (!Res.isUsable()) 2566 return nullptr; 2567 Init = Res.get(); 2568 } 2569 WithInit = true; 2570 } 2571 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 2572 CaptureExpr->getLocStart()); 2573 if (!WithInit) 2574 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C)); 2575 S.CurContext->addHiddenDecl(CED); 2576 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 2577 return CED; 2578 } 2579 2580 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 2581 bool WithInit) { 2582 OMPCapturedExprDecl *CD; 2583 if (VarDecl *VD = S.isOpenMPCapturedDecl(D)) 2584 CD = cast<OMPCapturedExprDecl>(VD); 2585 else 2586 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 2587 /*AsExpression=*/false); 2588 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 2589 CaptureExpr->getExprLoc()); 2590 } 2591 2592 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 2593 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get(); 2594 if (!Ref) { 2595 OMPCapturedExprDecl *CD = buildCaptureDecl( 2596 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr, 2597 /*WithInit=*/true, /*AsExpression=*/true); 2598 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 2599 CaptureExpr->getExprLoc()); 2600 } 2601 ExprResult Res = Ref; 2602 if (!S.getLangOpts().CPlusPlus && 2603 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 2604 Ref->getType()->isPointerType()) { 2605 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 2606 if (!Res.isUsable()) 2607 return ExprError(); 2608 } 2609 return S.DefaultLvalueConversion(Res.get()); 2610 } 2611 2612 namespace { 2613 // OpenMP directives parsed in this section are represented as a 2614 // CapturedStatement with an associated statement. If a syntax error 2615 // is detected during the parsing of the associated statement, the 2616 // compiler must abort processing and close the CapturedStatement. 2617 // 2618 // Combined directives such as 'target parallel' have more than one 2619 // nested CapturedStatements. This RAII ensures that we unwind out 2620 // of all the nested CapturedStatements when an error is found. 2621 class CaptureRegionUnwinderRAII { 2622 private: 2623 Sema &S; 2624 bool &ErrorFound; 2625 OpenMPDirectiveKind DKind = OMPD_unknown; 2626 2627 public: 2628 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 2629 OpenMPDirectiveKind DKind) 2630 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 2631 ~CaptureRegionUnwinderRAII() { 2632 if (ErrorFound) { 2633 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 2634 while (--ThisCaptureLevel >= 0) 2635 S.ActOnCapturedRegionError(); 2636 } 2637 } 2638 }; 2639 } // namespace 2640 2641 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 2642 ArrayRef<OMPClause *> Clauses) { 2643 bool ErrorFound = false; 2644 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 2645 *this, ErrorFound, DSAStack->getCurrentDirective()); 2646 if (!S.isUsable()) { 2647 ErrorFound = true; 2648 return StmtError(); 2649 } 2650 2651 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2652 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 2653 OMPOrderedClause *OC = nullptr; 2654 OMPScheduleClause *SC = nullptr; 2655 SmallVector<const OMPLinearClause *, 4> LCs; 2656 SmallVector<const OMPClauseWithPreInit *, 4> PICs; 2657 // This is required for proper codegen. 2658 for (OMPClause *Clause : Clauses) { 2659 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) && 2660 Clause->getClauseKind() == OMPC_in_reduction) { 2661 // Capture taskgroup task_reduction descriptors inside the tasking regions 2662 // with the corresponding in_reduction items. 2663 auto *IRC = cast<OMPInReductionClause>(Clause); 2664 for (Expr *E : IRC->taskgroup_descriptors()) 2665 if (E) 2666 MarkDeclarationsReferencedInExpr(E); 2667 } 2668 if (isOpenMPPrivate(Clause->getClauseKind()) || 2669 Clause->getClauseKind() == OMPC_copyprivate || 2670 (getLangOpts().OpenMPUseTLS && 2671 getASTContext().getTargetInfo().isTLSSupported() && 2672 Clause->getClauseKind() == OMPC_copyin)) { 2673 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 2674 // Mark all variables in private list clauses as used in inner region. 2675 for (Stmt *VarRef : Clause->children()) { 2676 if (auto *E = cast_or_null<Expr>(VarRef)) { 2677 MarkDeclarationsReferencedInExpr(E); 2678 } 2679 } 2680 DSAStack->setForceVarCapturing(/*V=*/false); 2681 } else if (CaptureRegions.size() > 1 || 2682 CaptureRegions.back() != OMPD_unknown) { 2683 if (auto *C = OMPClauseWithPreInit::get(Clause)) 2684 PICs.push_back(C); 2685 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 2686 if (Expr *E = C->getPostUpdateExpr()) 2687 MarkDeclarationsReferencedInExpr(E); 2688 } 2689 } 2690 if (Clause->getClauseKind() == OMPC_schedule) 2691 SC = cast<OMPScheduleClause>(Clause); 2692 else if (Clause->getClauseKind() == OMPC_ordered) 2693 OC = cast<OMPOrderedClause>(Clause); 2694 else if (Clause->getClauseKind() == OMPC_linear) 2695 LCs.push_back(cast<OMPLinearClause>(Clause)); 2696 } 2697 // OpenMP, 2.7.1 Loop Construct, Restrictions 2698 // The nonmonotonic modifier cannot be specified if an ordered clause is 2699 // specified. 2700 if (SC && 2701 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 2702 SC->getSecondScheduleModifier() == 2703 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 2704 OC) { 2705 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 2706 ? SC->getFirstScheduleModifierLoc() 2707 : SC->getSecondScheduleModifierLoc(), 2708 diag::err_omp_schedule_nonmonotonic_ordered) 2709 << SourceRange(OC->getLocStart(), OC->getLocEnd()); 2710 ErrorFound = true; 2711 } 2712 if (!LCs.empty() && OC && OC->getNumForLoops()) { 2713 for (const OMPLinearClause *C : LCs) { 2714 Diag(C->getLocStart(), diag::err_omp_linear_ordered) 2715 << SourceRange(OC->getLocStart(), OC->getLocEnd()); 2716 } 2717 ErrorFound = true; 2718 } 2719 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 2720 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 2721 OC->getNumForLoops()) { 2722 Diag(OC->getLocStart(), diag::err_omp_ordered_simd) 2723 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 2724 ErrorFound = true; 2725 } 2726 if (ErrorFound) { 2727 return StmtError(); 2728 } 2729 StmtResult SR = S; 2730 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 2731 // Mark all variables in private list clauses as used in inner region. 2732 // Required for proper codegen of combined directives. 2733 // TODO: add processing for other clauses. 2734 if (ThisCaptureRegion != OMPD_unknown) { 2735 for (const clang::OMPClauseWithPreInit *C : PICs) { 2736 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 2737 // Find the particular capture region for the clause if the 2738 // directive is a combined one with multiple capture regions. 2739 // If the directive is not a combined one, the capture region 2740 // associated with the clause is OMPD_unknown and is generated 2741 // only once. 2742 if (CaptureRegion == ThisCaptureRegion || 2743 CaptureRegion == OMPD_unknown) { 2744 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 2745 for (Decl *D : DS->decls()) 2746 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 2747 } 2748 } 2749 } 2750 } 2751 SR = ActOnCapturedRegionEnd(SR.get()); 2752 } 2753 return SR; 2754 } 2755 2756 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 2757 OpenMPDirectiveKind CancelRegion, 2758 SourceLocation StartLoc) { 2759 // CancelRegion is only needed for cancel and cancellation_point. 2760 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 2761 return false; 2762 2763 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 2764 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 2765 return false; 2766 2767 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 2768 << getOpenMPDirectiveName(CancelRegion); 2769 return true; 2770 } 2771 2772 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack, 2773 OpenMPDirectiveKind CurrentRegion, 2774 const DeclarationNameInfo &CurrentName, 2775 OpenMPDirectiveKind CancelRegion, 2776 SourceLocation StartLoc) { 2777 if (Stack->getCurScope()) { 2778 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective(); 2779 OpenMPDirectiveKind OffendingRegion = ParentRegion; 2780 bool NestingProhibited = false; 2781 bool CloseNesting = true; 2782 bool OrphanSeen = false; 2783 enum { 2784 NoRecommend, 2785 ShouldBeInParallelRegion, 2786 ShouldBeInOrderedRegion, 2787 ShouldBeInTargetRegion, 2788 ShouldBeInTeamsRegion 2789 } Recommend = NoRecommend; 2790 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) { 2791 // OpenMP [2.16, Nesting of Regions] 2792 // OpenMP constructs may not be nested inside a simd region. 2793 // OpenMP [2.8.1,simd Construct, Restrictions] 2794 // An ordered construct with the simd clause is the only OpenMP 2795 // construct that can appear in the simd region. 2796 // Allowing a SIMD construct nested in another SIMD construct is an 2797 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 2798 // message. 2799 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 2800 ? diag::err_omp_prohibited_region_simd 2801 : diag::warn_omp_nesting_simd); 2802 return CurrentRegion != OMPD_simd; 2803 } 2804 if (ParentRegion == OMPD_atomic) { 2805 // OpenMP [2.16, Nesting of Regions] 2806 // OpenMP constructs may not be nested inside an atomic region. 2807 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 2808 return true; 2809 } 2810 if (CurrentRegion == OMPD_section) { 2811 // OpenMP [2.7.2, sections Construct, Restrictions] 2812 // Orphaned section directives are prohibited. That is, the section 2813 // directives must appear within the sections construct and must not be 2814 // encountered elsewhere in the sections region. 2815 if (ParentRegion != OMPD_sections && 2816 ParentRegion != OMPD_parallel_sections) { 2817 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 2818 << (ParentRegion != OMPD_unknown) 2819 << getOpenMPDirectiveName(ParentRegion); 2820 return true; 2821 } 2822 return false; 2823 } 2824 // Allow some constructs (except teams) to be orphaned (they could be 2825 // used in functions, called from OpenMP regions with the required 2826 // preconditions). 2827 if (ParentRegion == OMPD_unknown && 2828 !isOpenMPNestingTeamsDirective(CurrentRegion)) 2829 return false; 2830 if (CurrentRegion == OMPD_cancellation_point || 2831 CurrentRegion == OMPD_cancel) { 2832 // OpenMP [2.16, Nesting of Regions] 2833 // A cancellation point construct for which construct-type-clause is 2834 // taskgroup must be nested inside a task construct. A cancellation 2835 // point construct for which construct-type-clause is not taskgroup must 2836 // be closely nested inside an OpenMP construct that matches the type 2837 // specified in construct-type-clause. 2838 // A cancel construct for which construct-type-clause is taskgroup must be 2839 // nested inside a task construct. A cancel construct for which 2840 // construct-type-clause is not taskgroup must be closely nested inside an 2841 // OpenMP construct that matches the type specified in 2842 // construct-type-clause. 2843 NestingProhibited = 2844 !((CancelRegion == OMPD_parallel && 2845 (ParentRegion == OMPD_parallel || 2846 ParentRegion == OMPD_target_parallel)) || 2847 (CancelRegion == OMPD_for && 2848 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 2849 ParentRegion == OMPD_target_parallel_for || 2850 ParentRegion == OMPD_distribute_parallel_for || 2851 ParentRegion == OMPD_teams_distribute_parallel_for || 2852 ParentRegion == OMPD_target_teams_distribute_parallel_for)) || 2853 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) || 2854 (CancelRegion == OMPD_sections && 2855 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 2856 ParentRegion == OMPD_parallel_sections))); 2857 } else if (CurrentRegion == OMPD_master) { 2858 // OpenMP [2.16, Nesting of Regions] 2859 // A master region may not be closely nested inside a worksharing, 2860 // atomic, or explicit task region. 2861 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 2862 isOpenMPTaskingDirective(ParentRegion); 2863 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 2864 // OpenMP [2.16, Nesting of Regions] 2865 // A critical region may not be nested (closely or otherwise) inside a 2866 // critical region with the same name. Note that this restriction is not 2867 // sufficient to prevent deadlock. 2868 SourceLocation PreviousCriticalLoc; 2869 bool DeadLock = Stack->hasDirective( 2870 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 2871 const DeclarationNameInfo &DNI, 2872 SourceLocation Loc) { 2873 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 2874 PreviousCriticalLoc = Loc; 2875 return true; 2876 } 2877 return false; 2878 }, 2879 false /* skip top directive */); 2880 if (DeadLock) { 2881 SemaRef.Diag(StartLoc, 2882 diag::err_omp_prohibited_region_critical_same_name) 2883 << CurrentName.getName(); 2884 if (PreviousCriticalLoc.isValid()) 2885 SemaRef.Diag(PreviousCriticalLoc, 2886 diag::note_omp_previous_critical_region); 2887 return true; 2888 } 2889 } else if (CurrentRegion == OMPD_barrier) { 2890 // OpenMP [2.16, Nesting of Regions] 2891 // A barrier region may not be closely nested inside a worksharing, 2892 // explicit task, critical, ordered, atomic, or master region. 2893 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 2894 isOpenMPTaskingDirective(ParentRegion) || 2895 ParentRegion == OMPD_master || 2896 ParentRegion == OMPD_critical || 2897 ParentRegion == OMPD_ordered; 2898 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 2899 !isOpenMPParallelDirective(CurrentRegion) && 2900 !isOpenMPTeamsDirective(CurrentRegion)) { 2901 // OpenMP [2.16, Nesting of Regions] 2902 // A worksharing region may not be closely nested inside a worksharing, 2903 // explicit task, critical, ordered, atomic, or master region. 2904 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 2905 isOpenMPTaskingDirective(ParentRegion) || 2906 ParentRegion == OMPD_master || 2907 ParentRegion == OMPD_critical || 2908 ParentRegion == OMPD_ordered; 2909 Recommend = ShouldBeInParallelRegion; 2910 } else if (CurrentRegion == OMPD_ordered) { 2911 // OpenMP [2.16, Nesting of Regions] 2912 // An ordered region may not be closely nested inside a critical, 2913 // atomic, or explicit task region. 2914 // An ordered region must be closely nested inside a loop region (or 2915 // parallel loop region) with an ordered clause. 2916 // OpenMP [2.8.1,simd Construct, Restrictions] 2917 // An ordered construct with the simd clause is the only OpenMP construct 2918 // that can appear in the simd region. 2919 NestingProhibited = ParentRegion == OMPD_critical || 2920 isOpenMPTaskingDirective(ParentRegion) || 2921 !(isOpenMPSimdDirective(ParentRegion) || 2922 Stack->isParentOrderedRegion()); 2923 Recommend = ShouldBeInOrderedRegion; 2924 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 2925 // OpenMP [2.16, Nesting of Regions] 2926 // If specified, a teams construct must be contained within a target 2927 // construct. 2928 NestingProhibited = ParentRegion != OMPD_target; 2929 OrphanSeen = ParentRegion == OMPD_unknown; 2930 Recommend = ShouldBeInTargetRegion; 2931 } 2932 if (!NestingProhibited && 2933 !isOpenMPTargetExecutionDirective(CurrentRegion) && 2934 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 2935 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 2936 // OpenMP [2.16, Nesting of Regions] 2937 // distribute, parallel, parallel sections, parallel workshare, and the 2938 // parallel loop and parallel loop SIMD constructs are the only OpenMP 2939 // constructs that can be closely nested in the teams region. 2940 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 2941 !isOpenMPDistributeDirective(CurrentRegion); 2942 Recommend = ShouldBeInParallelRegion; 2943 } 2944 if (!NestingProhibited && 2945 isOpenMPNestingDistributeDirective(CurrentRegion)) { 2946 // OpenMP 4.5 [2.17 Nesting of Regions] 2947 // The region associated with the distribute construct must be strictly 2948 // nested inside a teams region 2949 NestingProhibited = 2950 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 2951 Recommend = ShouldBeInTeamsRegion; 2952 } 2953 if (!NestingProhibited && 2954 (isOpenMPTargetExecutionDirective(CurrentRegion) || 2955 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 2956 // OpenMP 4.5 [2.17 Nesting of Regions] 2957 // If a target, target update, target data, target enter data, or 2958 // target exit data construct is encountered during execution of a 2959 // target region, the behavior is unspecified. 2960 NestingProhibited = Stack->hasDirective( 2961 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 2962 SourceLocation) { 2963 if (isOpenMPTargetExecutionDirective(K)) { 2964 OffendingRegion = K; 2965 return true; 2966 } 2967 return false; 2968 }, 2969 false /* don't skip top directive */); 2970 CloseNesting = false; 2971 } 2972 if (NestingProhibited) { 2973 if (OrphanSeen) { 2974 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 2975 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 2976 } else { 2977 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 2978 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 2979 << Recommend << getOpenMPDirectiveName(CurrentRegion); 2980 } 2981 return true; 2982 } 2983 } 2984 return false; 2985 } 2986 2987 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 2988 ArrayRef<OMPClause *> Clauses, 2989 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 2990 bool ErrorFound = false; 2991 unsigned NamedModifiersNumber = 0; 2992 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers( 2993 OMPD_unknown + 1); 2994 SmallVector<SourceLocation, 4> NameModifierLoc; 2995 for (const OMPClause *C : Clauses) { 2996 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 2997 // At most one if clause without a directive-name-modifier can appear on 2998 // the directive. 2999 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 3000 if (FoundNameModifiers[CurNM]) { 3001 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause) 3002 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 3003 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 3004 ErrorFound = true; 3005 } else if (CurNM != OMPD_unknown) { 3006 NameModifierLoc.push_back(IC->getNameModifierLoc()); 3007 ++NamedModifiersNumber; 3008 } 3009 FoundNameModifiers[CurNM] = IC; 3010 if (CurNM == OMPD_unknown) 3011 continue; 3012 // Check if the specified name modifier is allowed for the current 3013 // directive. 3014 // At most one if clause with the particular directive-name-modifier can 3015 // appear on the directive. 3016 bool MatchFound = false; 3017 for (auto NM : AllowedNameModifiers) { 3018 if (CurNM == NM) { 3019 MatchFound = true; 3020 break; 3021 } 3022 } 3023 if (!MatchFound) { 3024 S.Diag(IC->getNameModifierLoc(), 3025 diag::err_omp_wrong_if_directive_name_modifier) 3026 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 3027 ErrorFound = true; 3028 } 3029 } 3030 } 3031 // If any if clause on the directive includes a directive-name-modifier then 3032 // all if clauses on the directive must include a directive-name-modifier. 3033 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 3034 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 3035 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(), 3036 diag::err_omp_no_more_if_clause); 3037 } else { 3038 std::string Values; 3039 std::string Sep(", "); 3040 unsigned AllowedCnt = 0; 3041 unsigned TotalAllowedNum = 3042 AllowedNameModifiers.size() - NamedModifiersNumber; 3043 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 3044 ++Cnt) { 3045 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 3046 if (!FoundNameModifiers[NM]) { 3047 Values += "'"; 3048 Values += getOpenMPDirectiveName(NM); 3049 Values += "'"; 3050 if (AllowedCnt + 2 == TotalAllowedNum) 3051 Values += " or "; 3052 else if (AllowedCnt + 1 != TotalAllowedNum) 3053 Values += Sep; 3054 ++AllowedCnt; 3055 } 3056 } 3057 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(), 3058 diag::err_omp_unnamed_if_clause) 3059 << (TotalAllowedNum > 1) << Values; 3060 } 3061 for (SourceLocation Loc : NameModifierLoc) { 3062 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 3063 } 3064 ErrorFound = true; 3065 } 3066 return ErrorFound; 3067 } 3068 3069 StmtResult Sema::ActOnOpenMPExecutableDirective( 3070 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 3071 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 3072 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 3073 StmtResult Res = StmtError(); 3074 // First check CancelRegion which is then used in checkNestingOfRegions. 3075 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 3076 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 3077 StartLoc)) 3078 return StmtError(); 3079 3080 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 3081 VarsWithInheritedDSAType VarsWithInheritedDSA; 3082 bool ErrorFound = false; 3083 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 3084 if (AStmt && !CurContext->isDependentContext()) { 3085 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3086 3087 // Check default data sharing attributes for referenced variables. 3088 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 3089 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 3090 Stmt *S = AStmt; 3091 while (--ThisCaptureLevel >= 0) 3092 S = cast<CapturedStmt>(S)->getCapturedStmt(); 3093 DSAChecker.Visit(S); 3094 if (DSAChecker.isErrorFound()) 3095 return StmtError(); 3096 // Generate list of implicitly defined firstprivate variables. 3097 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 3098 3099 SmallVector<Expr *, 4> ImplicitFirstprivates( 3100 DSAChecker.getImplicitFirstprivate().begin(), 3101 DSAChecker.getImplicitFirstprivate().end()); 3102 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(), 3103 DSAChecker.getImplicitMap().end()); 3104 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 3105 for (OMPClause *C : Clauses) { 3106 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 3107 for (Expr *E : IRC->taskgroup_descriptors()) 3108 if (E) 3109 ImplicitFirstprivates.emplace_back(E); 3110 } 3111 } 3112 if (!ImplicitFirstprivates.empty()) { 3113 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 3114 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 3115 SourceLocation())) { 3116 ClausesWithImplicit.push_back(Implicit); 3117 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 3118 ImplicitFirstprivates.size(); 3119 } else { 3120 ErrorFound = true; 3121 } 3122 } 3123 if (!ImplicitMaps.empty()) { 3124 if (OMPClause *Implicit = ActOnOpenMPMapClause( 3125 OMPC_MAP_unknown, OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true, 3126 SourceLocation(), SourceLocation(), ImplicitMaps, 3127 SourceLocation(), SourceLocation(), SourceLocation())) { 3128 ClausesWithImplicit.emplace_back(Implicit); 3129 ErrorFound |= 3130 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size(); 3131 } else { 3132 ErrorFound = true; 3133 } 3134 } 3135 } 3136 3137 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 3138 switch (Kind) { 3139 case OMPD_parallel: 3140 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 3141 EndLoc); 3142 AllowedNameModifiers.push_back(OMPD_parallel); 3143 break; 3144 case OMPD_simd: 3145 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 3146 VarsWithInheritedDSA); 3147 break; 3148 case OMPD_for: 3149 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 3150 VarsWithInheritedDSA); 3151 break; 3152 case OMPD_for_simd: 3153 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 3154 EndLoc, VarsWithInheritedDSA); 3155 break; 3156 case OMPD_sections: 3157 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 3158 EndLoc); 3159 break; 3160 case OMPD_section: 3161 assert(ClausesWithImplicit.empty() && 3162 "No clauses are allowed for 'omp section' directive"); 3163 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 3164 break; 3165 case OMPD_single: 3166 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 3167 EndLoc); 3168 break; 3169 case OMPD_master: 3170 assert(ClausesWithImplicit.empty() && 3171 "No clauses are allowed for 'omp master' directive"); 3172 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 3173 break; 3174 case OMPD_critical: 3175 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 3176 StartLoc, EndLoc); 3177 break; 3178 case OMPD_parallel_for: 3179 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 3180 EndLoc, VarsWithInheritedDSA); 3181 AllowedNameModifiers.push_back(OMPD_parallel); 3182 break; 3183 case OMPD_parallel_for_simd: 3184 Res = ActOnOpenMPParallelForSimdDirective( 3185 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3186 AllowedNameModifiers.push_back(OMPD_parallel); 3187 break; 3188 case OMPD_parallel_sections: 3189 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 3190 StartLoc, EndLoc); 3191 AllowedNameModifiers.push_back(OMPD_parallel); 3192 break; 3193 case OMPD_task: 3194 Res = 3195 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 3196 AllowedNameModifiers.push_back(OMPD_task); 3197 break; 3198 case OMPD_taskyield: 3199 assert(ClausesWithImplicit.empty() && 3200 "No clauses are allowed for 'omp taskyield' directive"); 3201 assert(AStmt == nullptr && 3202 "No associated statement allowed for 'omp taskyield' directive"); 3203 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 3204 break; 3205 case OMPD_barrier: 3206 assert(ClausesWithImplicit.empty() && 3207 "No clauses are allowed for 'omp barrier' directive"); 3208 assert(AStmt == nullptr && 3209 "No associated statement allowed for 'omp barrier' directive"); 3210 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 3211 break; 3212 case OMPD_taskwait: 3213 assert(ClausesWithImplicit.empty() && 3214 "No clauses are allowed for 'omp taskwait' directive"); 3215 assert(AStmt == nullptr && 3216 "No associated statement allowed for 'omp taskwait' directive"); 3217 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 3218 break; 3219 case OMPD_taskgroup: 3220 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 3221 EndLoc); 3222 break; 3223 case OMPD_flush: 3224 assert(AStmt == nullptr && 3225 "No associated statement allowed for 'omp flush' directive"); 3226 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 3227 break; 3228 case OMPD_ordered: 3229 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 3230 EndLoc); 3231 break; 3232 case OMPD_atomic: 3233 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 3234 EndLoc); 3235 break; 3236 case OMPD_teams: 3237 Res = 3238 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 3239 break; 3240 case OMPD_target: 3241 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 3242 EndLoc); 3243 AllowedNameModifiers.push_back(OMPD_target); 3244 break; 3245 case OMPD_target_parallel: 3246 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 3247 StartLoc, EndLoc); 3248 AllowedNameModifiers.push_back(OMPD_target); 3249 AllowedNameModifiers.push_back(OMPD_parallel); 3250 break; 3251 case OMPD_target_parallel_for: 3252 Res = ActOnOpenMPTargetParallelForDirective( 3253 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3254 AllowedNameModifiers.push_back(OMPD_target); 3255 AllowedNameModifiers.push_back(OMPD_parallel); 3256 break; 3257 case OMPD_cancellation_point: 3258 assert(ClausesWithImplicit.empty() && 3259 "No clauses are allowed for 'omp cancellation point' directive"); 3260 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 3261 "cancellation point' directive"); 3262 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 3263 break; 3264 case OMPD_cancel: 3265 assert(AStmt == nullptr && 3266 "No associated statement allowed for 'omp cancel' directive"); 3267 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 3268 CancelRegion); 3269 AllowedNameModifiers.push_back(OMPD_cancel); 3270 break; 3271 case OMPD_target_data: 3272 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 3273 EndLoc); 3274 AllowedNameModifiers.push_back(OMPD_target_data); 3275 break; 3276 case OMPD_target_enter_data: 3277 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 3278 EndLoc, AStmt); 3279 AllowedNameModifiers.push_back(OMPD_target_enter_data); 3280 break; 3281 case OMPD_target_exit_data: 3282 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 3283 EndLoc, AStmt); 3284 AllowedNameModifiers.push_back(OMPD_target_exit_data); 3285 break; 3286 case OMPD_taskloop: 3287 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 3288 EndLoc, VarsWithInheritedDSA); 3289 AllowedNameModifiers.push_back(OMPD_taskloop); 3290 break; 3291 case OMPD_taskloop_simd: 3292 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 3293 EndLoc, VarsWithInheritedDSA); 3294 AllowedNameModifiers.push_back(OMPD_taskloop); 3295 break; 3296 case OMPD_distribute: 3297 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 3298 EndLoc, VarsWithInheritedDSA); 3299 break; 3300 case OMPD_target_update: 3301 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 3302 EndLoc, AStmt); 3303 AllowedNameModifiers.push_back(OMPD_target_update); 3304 break; 3305 case OMPD_distribute_parallel_for: 3306 Res = ActOnOpenMPDistributeParallelForDirective( 3307 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3308 AllowedNameModifiers.push_back(OMPD_parallel); 3309 break; 3310 case OMPD_distribute_parallel_for_simd: 3311 Res = ActOnOpenMPDistributeParallelForSimdDirective( 3312 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3313 AllowedNameModifiers.push_back(OMPD_parallel); 3314 break; 3315 case OMPD_distribute_simd: 3316 Res = ActOnOpenMPDistributeSimdDirective( 3317 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3318 break; 3319 case OMPD_target_parallel_for_simd: 3320 Res = ActOnOpenMPTargetParallelForSimdDirective( 3321 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3322 AllowedNameModifiers.push_back(OMPD_target); 3323 AllowedNameModifiers.push_back(OMPD_parallel); 3324 break; 3325 case OMPD_target_simd: 3326 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 3327 EndLoc, VarsWithInheritedDSA); 3328 AllowedNameModifiers.push_back(OMPD_target); 3329 break; 3330 case OMPD_teams_distribute: 3331 Res = ActOnOpenMPTeamsDistributeDirective( 3332 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3333 break; 3334 case OMPD_teams_distribute_simd: 3335 Res = ActOnOpenMPTeamsDistributeSimdDirective( 3336 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3337 break; 3338 case OMPD_teams_distribute_parallel_for_simd: 3339 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 3340 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3341 AllowedNameModifiers.push_back(OMPD_parallel); 3342 break; 3343 case OMPD_teams_distribute_parallel_for: 3344 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 3345 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3346 AllowedNameModifiers.push_back(OMPD_parallel); 3347 break; 3348 case OMPD_target_teams: 3349 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 3350 EndLoc); 3351 AllowedNameModifiers.push_back(OMPD_target); 3352 break; 3353 case OMPD_target_teams_distribute: 3354 Res = ActOnOpenMPTargetTeamsDistributeDirective( 3355 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3356 AllowedNameModifiers.push_back(OMPD_target); 3357 break; 3358 case OMPD_target_teams_distribute_parallel_for: 3359 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 3360 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3361 AllowedNameModifiers.push_back(OMPD_target); 3362 AllowedNameModifiers.push_back(OMPD_parallel); 3363 break; 3364 case OMPD_target_teams_distribute_parallel_for_simd: 3365 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 3366 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3367 AllowedNameModifiers.push_back(OMPD_target); 3368 AllowedNameModifiers.push_back(OMPD_parallel); 3369 break; 3370 case OMPD_target_teams_distribute_simd: 3371 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 3372 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3373 AllowedNameModifiers.push_back(OMPD_target); 3374 break; 3375 case OMPD_declare_target: 3376 case OMPD_end_declare_target: 3377 case OMPD_threadprivate: 3378 case OMPD_declare_reduction: 3379 case OMPD_declare_simd: 3380 llvm_unreachable("OpenMP Directive is not allowed"); 3381 case OMPD_unknown: 3382 llvm_unreachable("Unknown OpenMP directive"); 3383 } 3384 3385 for (const auto &P : VarsWithInheritedDSA) { 3386 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 3387 << P.first << P.second->getSourceRange(); 3388 } 3389 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound; 3390 3391 if (!AllowedNameModifiers.empty()) 3392 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 3393 ErrorFound; 3394 3395 if (ErrorFound) 3396 return StmtError(); 3397 return Res; 3398 } 3399 3400 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 3401 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 3402 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 3403 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 3404 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 3405 assert(Aligneds.size() == Alignments.size()); 3406 assert(Linears.size() == LinModifiers.size()); 3407 assert(Linears.size() == Steps.size()); 3408 if (!DG || DG.get().isNull()) 3409 return DeclGroupPtrTy(); 3410 3411 if (!DG.get().isSingleDecl()) { 3412 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd); 3413 return DG; 3414 } 3415 Decl *ADecl = DG.get().getSingleDecl(); 3416 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 3417 ADecl = FTD->getTemplatedDecl(); 3418 3419 auto *FD = dyn_cast<FunctionDecl>(ADecl); 3420 if (!FD) { 3421 Diag(ADecl->getLocation(), diag::err_omp_function_expected); 3422 return DeclGroupPtrTy(); 3423 } 3424 3425 // OpenMP [2.8.2, declare simd construct, Description] 3426 // The parameter of the simdlen clause must be a constant positive integer 3427 // expression. 3428 ExprResult SL; 3429 if (Simdlen) 3430 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 3431 // OpenMP [2.8.2, declare simd construct, Description] 3432 // The special this pointer can be used as if was one of the arguments to the 3433 // function in any of the linear, aligned, or uniform clauses. 3434 // The uniform clause declares one or more arguments to have an invariant 3435 // value for all concurrent invocations of the function in the execution of a 3436 // single SIMD loop. 3437 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 3438 const Expr *UniformedLinearThis = nullptr; 3439 for (const Expr *E : Uniforms) { 3440 E = E->IgnoreParenImpCasts(); 3441 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 3442 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 3443 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 3444 FD->getParamDecl(PVD->getFunctionScopeIndex()) 3445 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 3446 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 3447 continue; 3448 } 3449 if (isa<CXXThisExpr>(E)) { 3450 UniformedLinearThis = E; 3451 continue; 3452 } 3453 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 3454 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 3455 } 3456 // OpenMP [2.8.2, declare simd construct, Description] 3457 // The aligned clause declares that the object to which each list item points 3458 // is aligned to the number of bytes expressed in the optional parameter of 3459 // the aligned clause. 3460 // The special this pointer can be used as if was one of the arguments to the 3461 // function in any of the linear, aligned, or uniform clauses. 3462 // The type of list items appearing in the aligned clause must be array, 3463 // pointer, reference to array, or reference to pointer. 3464 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 3465 const Expr *AlignedThis = nullptr; 3466 for (const Expr *E : Aligneds) { 3467 E = E->IgnoreParenImpCasts(); 3468 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 3469 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 3470 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 3471 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 3472 FD->getParamDecl(PVD->getFunctionScopeIndex()) 3473 ->getCanonicalDecl() == CanonPVD) { 3474 // OpenMP [2.8.1, simd construct, Restrictions] 3475 // A list-item cannot appear in more than one aligned clause. 3476 if (AlignedArgs.count(CanonPVD) > 0) { 3477 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 3478 << 1 << E->getSourceRange(); 3479 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 3480 diag::note_omp_explicit_dsa) 3481 << getOpenMPClauseName(OMPC_aligned); 3482 continue; 3483 } 3484 AlignedArgs[CanonPVD] = E; 3485 QualType QTy = PVD->getType() 3486 .getNonReferenceType() 3487 .getUnqualifiedType() 3488 .getCanonicalType(); 3489 const Type *Ty = QTy.getTypePtrOrNull(); 3490 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 3491 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 3492 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 3493 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 3494 } 3495 continue; 3496 } 3497 } 3498 if (isa<CXXThisExpr>(E)) { 3499 if (AlignedThis) { 3500 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 3501 << 2 << E->getSourceRange(); 3502 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 3503 << getOpenMPClauseName(OMPC_aligned); 3504 } 3505 AlignedThis = E; 3506 continue; 3507 } 3508 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 3509 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 3510 } 3511 // The optional parameter of the aligned clause, alignment, must be a constant 3512 // positive integer expression. If no optional parameter is specified, 3513 // implementation-defined default alignments for SIMD instructions on the 3514 // target platforms are assumed. 3515 SmallVector<const Expr *, 4> NewAligns; 3516 for (Expr *E : Alignments) { 3517 ExprResult Align; 3518 if (E) 3519 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 3520 NewAligns.push_back(Align.get()); 3521 } 3522 // OpenMP [2.8.2, declare simd construct, Description] 3523 // The linear clause declares one or more list items to be private to a SIMD 3524 // lane and to have a linear relationship with respect to the iteration space 3525 // of a loop. 3526 // The special this pointer can be used as if was one of the arguments to the 3527 // function in any of the linear, aligned, or uniform clauses. 3528 // When a linear-step expression is specified in a linear clause it must be 3529 // either a constant integer expression or an integer-typed parameter that is 3530 // specified in a uniform clause on the directive. 3531 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 3532 const bool IsUniformedThis = UniformedLinearThis != nullptr; 3533 auto MI = LinModifiers.begin(); 3534 for (const Expr *E : Linears) { 3535 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 3536 ++MI; 3537 E = E->IgnoreParenImpCasts(); 3538 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 3539 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 3540 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 3541 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 3542 FD->getParamDecl(PVD->getFunctionScopeIndex()) 3543 ->getCanonicalDecl() == CanonPVD) { 3544 // OpenMP [2.15.3.7, linear Clause, Restrictions] 3545 // A list-item cannot appear in more than one linear clause. 3546 if (LinearArgs.count(CanonPVD) > 0) { 3547 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3548 << getOpenMPClauseName(OMPC_linear) 3549 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 3550 Diag(LinearArgs[CanonPVD]->getExprLoc(), 3551 diag::note_omp_explicit_dsa) 3552 << getOpenMPClauseName(OMPC_linear); 3553 continue; 3554 } 3555 // Each argument can appear in at most one uniform or linear clause. 3556 if (UniformedArgs.count(CanonPVD) > 0) { 3557 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3558 << getOpenMPClauseName(OMPC_linear) 3559 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 3560 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 3561 diag::note_omp_explicit_dsa) 3562 << getOpenMPClauseName(OMPC_uniform); 3563 continue; 3564 } 3565 LinearArgs[CanonPVD] = E; 3566 if (E->isValueDependent() || E->isTypeDependent() || 3567 E->isInstantiationDependent() || 3568 E->containsUnexpandedParameterPack()) 3569 continue; 3570 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 3571 PVD->getOriginalType()); 3572 continue; 3573 } 3574 } 3575 if (isa<CXXThisExpr>(E)) { 3576 if (UniformedLinearThis) { 3577 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3578 << getOpenMPClauseName(OMPC_linear) 3579 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 3580 << E->getSourceRange(); 3581 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 3582 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 3583 : OMPC_linear); 3584 continue; 3585 } 3586 UniformedLinearThis = E; 3587 if (E->isValueDependent() || E->isTypeDependent() || 3588 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 3589 continue; 3590 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 3591 E->getType()); 3592 continue; 3593 } 3594 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 3595 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 3596 } 3597 Expr *Step = nullptr; 3598 Expr *NewStep = nullptr; 3599 SmallVector<Expr *, 4> NewSteps; 3600 for (Expr *E : Steps) { 3601 // Skip the same step expression, it was checked already. 3602 if (Step == E || !E) { 3603 NewSteps.push_back(E ? NewStep : nullptr); 3604 continue; 3605 } 3606 Step = E; 3607 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 3608 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 3609 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 3610 if (UniformedArgs.count(CanonPVD) == 0) { 3611 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 3612 << Step->getSourceRange(); 3613 } else if (E->isValueDependent() || E->isTypeDependent() || 3614 E->isInstantiationDependent() || 3615 E->containsUnexpandedParameterPack() || 3616 CanonPVD->getType()->hasIntegerRepresentation()) { 3617 NewSteps.push_back(Step); 3618 } else { 3619 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 3620 << Step->getSourceRange(); 3621 } 3622 continue; 3623 } 3624 NewStep = Step; 3625 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 3626 !Step->isInstantiationDependent() && 3627 !Step->containsUnexpandedParameterPack()) { 3628 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 3629 .get(); 3630 if (NewStep) 3631 NewStep = VerifyIntegerConstantExpression(NewStep).get(); 3632 } 3633 NewSteps.push_back(NewStep); 3634 } 3635 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 3636 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 3637 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 3638 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 3639 const_cast<Expr **>(Linears.data()), Linears.size(), 3640 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 3641 NewSteps.data(), NewSteps.size(), SR); 3642 ADecl->addAttr(NewAttr); 3643 return ConvertDeclToDeclGroup(ADecl); 3644 } 3645 3646 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 3647 Stmt *AStmt, 3648 SourceLocation StartLoc, 3649 SourceLocation EndLoc) { 3650 if (!AStmt) 3651 return StmtError(); 3652 3653 auto *CS = cast<CapturedStmt>(AStmt); 3654 // 1.2.2 OpenMP Language Terminology 3655 // Structured block - An executable statement with a single entry at the 3656 // top and a single exit at the bottom. 3657 // The point of exit cannot be a branch out of the structured block. 3658 // longjmp() and throw() must not violate the entry/exit criteria. 3659 CS->getCapturedDecl()->setNothrow(); 3660 3661 setFunctionHasBranchProtectedScope(); 3662 3663 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 3664 DSAStack->isCancelRegion()); 3665 } 3666 3667 namespace { 3668 /// Helper class for checking canonical form of the OpenMP loops and 3669 /// extracting iteration space of each loop in the loop nest, that will be used 3670 /// for IR generation. 3671 class OpenMPIterationSpaceChecker { 3672 /// Reference to Sema. 3673 Sema &SemaRef; 3674 /// A location for diagnostics (when there is no some better location). 3675 SourceLocation DefaultLoc; 3676 /// A location for diagnostics (when increment is not compatible). 3677 SourceLocation ConditionLoc; 3678 /// A source location for referring to loop init later. 3679 SourceRange InitSrcRange; 3680 /// A source location for referring to condition later. 3681 SourceRange ConditionSrcRange; 3682 /// A source location for referring to increment later. 3683 SourceRange IncrementSrcRange; 3684 /// Loop variable. 3685 ValueDecl *LCDecl = nullptr; 3686 /// Reference to loop variable. 3687 Expr *LCRef = nullptr; 3688 /// Lower bound (initializer for the var). 3689 Expr *LB = nullptr; 3690 /// Upper bound. 3691 Expr *UB = nullptr; 3692 /// Loop step (increment). 3693 Expr *Step = nullptr; 3694 /// This flag is true when condition is one of: 3695 /// Var < UB 3696 /// Var <= UB 3697 /// UB > Var 3698 /// UB >= Var 3699 bool TestIsLessOp = false; 3700 /// This flag is true when condition is strict ( < or > ). 3701 bool TestIsStrictOp = false; 3702 /// This flag is true when step is subtracted on each iteration. 3703 bool SubtractStep = false; 3704 3705 public: 3706 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc) 3707 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {} 3708 /// Check init-expr for canonical loop form and save loop counter 3709 /// variable - #Var and its initialization value - #LB. 3710 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 3711 /// Check test-expr for canonical form, save upper-bound (#UB), flags 3712 /// for less/greater and for strict/non-strict comparison. 3713 bool checkAndSetCond(Expr *S); 3714 /// Check incr-expr for canonical loop form and return true if it 3715 /// does not conform, otherwise save loop step (#Step). 3716 bool checkAndSetInc(Expr *S); 3717 /// Return the loop counter variable. 3718 ValueDecl *getLoopDecl() const { return LCDecl; } 3719 /// Return the reference expression to loop counter variable. 3720 Expr *getLoopDeclRefExpr() const { return LCRef; } 3721 /// Source range of the loop init. 3722 SourceRange getInitSrcRange() const { return InitSrcRange; } 3723 /// Source range of the loop condition. 3724 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 3725 /// Source range of the loop increment. 3726 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 3727 /// True if the step should be subtracted. 3728 bool shouldSubtractStep() const { return SubtractStep; } 3729 /// Build the expression to calculate the number of iterations. 3730 Expr *buildNumIterations( 3731 Scope *S, const bool LimitedType, 3732 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 3733 /// Build the precondition expression for the loops. 3734 Expr * 3735 buildPreCond(Scope *S, Expr *Cond, 3736 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 3737 /// Build reference expression to the counter be used for codegen. 3738 DeclRefExpr * 3739 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 3740 DSAStackTy &DSA) const; 3741 /// Build reference expression to the private counter be used for 3742 /// codegen. 3743 Expr *buildPrivateCounterVar() const; 3744 /// Build initialization of the counter be used for codegen. 3745 Expr *buildCounterInit() const; 3746 /// Build step of the counter be used for codegen. 3747 Expr *buildCounterStep() const; 3748 /// Return true if any expression is dependent. 3749 bool dependent() const; 3750 3751 private: 3752 /// Check the right-hand side of an assignment in the increment 3753 /// expression. 3754 bool checkAndSetIncRHS(Expr *RHS); 3755 /// Helper to set loop counter variable and its initializer. 3756 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB); 3757 /// Helper to set upper bound. 3758 bool setUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR, 3759 SourceLocation SL); 3760 /// Helper to set loop increment. 3761 bool setStep(Expr *NewStep, bool Subtract); 3762 }; 3763 3764 bool OpenMPIterationSpaceChecker::dependent() const { 3765 if (!LCDecl) { 3766 assert(!LB && !UB && !Step); 3767 return false; 3768 } 3769 return LCDecl->getType()->isDependentType() || 3770 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 3771 (Step && Step->isValueDependent()); 3772 } 3773 3774 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 3775 Expr *NewLCRefExpr, 3776 Expr *NewLB) { 3777 // State consistency checking to ensure correct usage. 3778 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 3779 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 3780 if (!NewLCDecl || !NewLB) 3781 return true; 3782 LCDecl = getCanonicalDecl(NewLCDecl); 3783 LCRef = NewLCRefExpr; 3784 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 3785 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 3786 if ((Ctor->isCopyOrMoveConstructor() || 3787 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 3788 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 3789 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 3790 LB = NewLB; 3791 return false; 3792 } 3793 3794 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, bool LessOp, bool StrictOp, 3795 SourceRange SR, SourceLocation SL) { 3796 // State consistency checking to ensure correct usage. 3797 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 3798 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 3799 if (!NewUB) 3800 return true; 3801 UB = NewUB; 3802 TestIsLessOp = LessOp; 3803 TestIsStrictOp = StrictOp; 3804 ConditionSrcRange = SR; 3805 ConditionLoc = SL; 3806 return false; 3807 } 3808 3809 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 3810 // State consistency checking to ensure correct usage. 3811 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 3812 if (!NewStep) 3813 return true; 3814 if (!NewStep->isValueDependent()) { 3815 // Check that the step is integer expression. 3816 SourceLocation StepLoc = NewStep->getLocStart(); 3817 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 3818 StepLoc, getExprAsWritten(NewStep)); 3819 if (Val.isInvalid()) 3820 return true; 3821 NewStep = Val.get(); 3822 3823 // OpenMP [2.6, Canonical Loop Form, Restrictions] 3824 // If test-expr is of form var relational-op b and relational-op is < or 3825 // <= then incr-expr must cause var to increase on each iteration of the 3826 // loop. If test-expr is of form var relational-op b and relational-op is 3827 // > or >= then incr-expr must cause var to decrease on each iteration of 3828 // the loop. 3829 // If test-expr is of form b relational-op var and relational-op is < or 3830 // <= then incr-expr must cause var to decrease on each iteration of the 3831 // loop. If test-expr is of form b relational-op var and relational-op is 3832 // > or >= then incr-expr must cause var to increase on each iteration of 3833 // the loop. 3834 llvm::APSInt Result; 3835 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); 3836 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 3837 bool IsConstNeg = 3838 IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); 3839 bool IsConstPos = 3840 IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); 3841 bool IsConstZero = IsConstant && !Result.getBoolValue(); 3842 if (UB && (IsConstZero || 3843 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract)) 3844 : (IsConstPos || (IsUnsigned && !Subtract))))) { 3845 SemaRef.Diag(NewStep->getExprLoc(), 3846 diag::err_omp_loop_incr_not_compatible) 3847 << LCDecl << TestIsLessOp << NewStep->getSourceRange(); 3848 SemaRef.Diag(ConditionLoc, 3849 diag::note_omp_loop_cond_requres_compatible_incr) 3850 << TestIsLessOp << ConditionSrcRange; 3851 return true; 3852 } 3853 if (TestIsLessOp == Subtract) { 3854 NewStep = 3855 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 3856 .get(); 3857 Subtract = !Subtract; 3858 } 3859 } 3860 3861 Step = NewStep; 3862 SubtractStep = Subtract; 3863 return false; 3864 } 3865 3866 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 3867 // Check init-expr for canonical loop form and save loop counter 3868 // variable - #Var and its initialization value - #LB. 3869 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 3870 // var = lb 3871 // integer-type var = lb 3872 // random-access-iterator-type var = lb 3873 // pointer-type var = lb 3874 // 3875 if (!S) { 3876 if (EmitDiags) { 3877 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 3878 } 3879 return true; 3880 } 3881 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 3882 if (!ExprTemp->cleanupsHaveSideEffects()) 3883 S = ExprTemp->getSubExpr(); 3884 3885 InitSrcRange = S->getSourceRange(); 3886 if (Expr *E = dyn_cast<Expr>(S)) 3887 S = E->IgnoreParens(); 3888 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 3889 if (BO->getOpcode() == BO_Assign) { 3890 Expr *LHS = BO->getLHS()->IgnoreParens(); 3891 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 3892 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 3893 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 3894 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3895 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS()); 3896 } 3897 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 3898 if (ME->isArrow() && 3899 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 3900 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3901 } 3902 } 3903 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 3904 if (DS->isSingleDecl()) { 3905 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 3906 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 3907 // Accept non-canonical init form here but emit ext. warning. 3908 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 3909 SemaRef.Diag(S->getLocStart(), 3910 diag::ext_omp_loop_not_canonical_init) 3911 << S->getSourceRange(); 3912 return setLCDeclAndLB(Var, nullptr, Var->getInit()); 3913 } 3914 } 3915 } 3916 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 3917 if (CE->getOperator() == OO_Equal) { 3918 Expr *LHS = CE->getArg(0); 3919 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 3920 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 3921 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 3922 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3923 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1)); 3924 } 3925 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 3926 if (ME->isArrow() && 3927 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 3928 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 3929 } 3930 } 3931 } 3932 3933 if (dependent() || SemaRef.CurContext->isDependentContext()) 3934 return false; 3935 if (EmitDiags) { 3936 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init) 3937 << S->getSourceRange(); 3938 } 3939 return true; 3940 } 3941 3942 /// Ignore parenthesizes, implicit casts, copy constructor and return the 3943 /// variable (which may be the loop variable) if possible. 3944 static const ValueDecl *getInitLCDecl(const Expr *E) { 3945 if (!E) 3946 return nullptr; 3947 E = getExprAsWritten(E); 3948 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 3949 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 3950 if ((Ctor->isCopyOrMoveConstructor() || 3951 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 3952 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 3953 E = CE->getArg(0)->IgnoreParenImpCasts(); 3954 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 3955 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 3956 return getCanonicalDecl(VD); 3957 } 3958 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 3959 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 3960 return getCanonicalDecl(ME->getMemberDecl()); 3961 return nullptr; 3962 } 3963 3964 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 3965 // Check test-expr for canonical form, save upper-bound UB, flags for 3966 // less/greater and for strict/non-strict comparison. 3967 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 3968 // var relational-op b 3969 // b relational-op var 3970 // 3971 if (!S) { 3972 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl; 3973 return true; 3974 } 3975 S = getExprAsWritten(S); 3976 SourceLocation CondLoc = S->getLocStart(); 3977 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 3978 if (BO->isRelationalOp()) { 3979 if (getInitLCDecl(BO->getLHS()) == LCDecl) 3980 return setUB(BO->getRHS(), 3981 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 3982 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 3983 BO->getSourceRange(), BO->getOperatorLoc()); 3984 if (getInitLCDecl(BO->getRHS()) == LCDecl) 3985 return setUB(BO->getLHS(), 3986 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 3987 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 3988 BO->getSourceRange(), BO->getOperatorLoc()); 3989 } 3990 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 3991 if (CE->getNumArgs() == 2) { 3992 auto Op = CE->getOperator(); 3993 switch (Op) { 3994 case OO_Greater: 3995 case OO_GreaterEqual: 3996 case OO_Less: 3997 case OO_LessEqual: 3998 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 3999 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 4000 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 4001 CE->getOperatorLoc()); 4002 if (getInitLCDecl(CE->getArg(1)) == LCDecl) 4003 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 4004 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 4005 CE->getOperatorLoc()); 4006 break; 4007 default: 4008 break; 4009 } 4010 } 4011 } 4012 if (dependent() || SemaRef.CurContext->isDependentContext()) 4013 return false; 4014 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 4015 << S->getSourceRange() << LCDecl; 4016 return true; 4017 } 4018 4019 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 4020 // RHS of canonical loop form increment can be: 4021 // var + incr 4022 // incr + var 4023 // var - incr 4024 // 4025 RHS = RHS->IgnoreParenImpCasts(); 4026 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 4027 if (BO->isAdditiveOp()) { 4028 bool IsAdd = BO->getOpcode() == BO_Add; 4029 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4030 return setStep(BO->getRHS(), !IsAdd); 4031 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 4032 return setStep(BO->getLHS(), /*Subtract=*/false); 4033 } 4034 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 4035 bool IsAdd = CE->getOperator() == OO_Plus; 4036 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 4037 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4038 return setStep(CE->getArg(1), !IsAdd); 4039 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 4040 return setStep(CE->getArg(0), /*Subtract=*/false); 4041 } 4042 } 4043 if (dependent() || SemaRef.CurContext->isDependentContext()) 4044 return false; 4045 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr) 4046 << RHS->getSourceRange() << LCDecl; 4047 return true; 4048 } 4049 4050 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 4051 // Check incr-expr for canonical loop form and return true if it 4052 // does not conform. 4053 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 4054 // ++var 4055 // var++ 4056 // --var 4057 // var-- 4058 // var += incr 4059 // var -= incr 4060 // var = var + incr 4061 // var = incr + var 4062 // var = var - incr 4063 // 4064 if (!S) { 4065 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 4066 return true; 4067 } 4068 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 4069 if (!ExprTemp->cleanupsHaveSideEffects()) 4070 S = ExprTemp->getSubExpr(); 4071 4072 IncrementSrcRange = S->getSourceRange(); 4073 S = S->IgnoreParens(); 4074 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 4075 if (UO->isIncrementDecrementOp() && 4076 getInitLCDecl(UO->getSubExpr()) == LCDecl) 4077 return setStep(SemaRef 4078 .ActOnIntegerConstant(UO->getLocStart(), 4079 (UO->isDecrementOp() ? -1 : 1)) 4080 .get(), 4081 /*Subtract=*/false); 4082 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 4083 switch (BO->getOpcode()) { 4084 case BO_AddAssign: 4085 case BO_SubAssign: 4086 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4087 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 4088 break; 4089 case BO_Assign: 4090 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4091 return checkAndSetIncRHS(BO->getRHS()); 4092 break; 4093 default: 4094 break; 4095 } 4096 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 4097 switch (CE->getOperator()) { 4098 case OO_PlusPlus: 4099 case OO_MinusMinus: 4100 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4101 return setStep(SemaRef 4102 .ActOnIntegerConstant( 4103 CE->getLocStart(), 4104 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 4105 .get(), 4106 /*Subtract=*/false); 4107 break; 4108 case OO_PlusEqual: 4109 case OO_MinusEqual: 4110 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4111 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 4112 break; 4113 case OO_Equal: 4114 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4115 return checkAndSetIncRHS(CE->getArg(1)); 4116 break; 4117 default: 4118 break; 4119 } 4120 } 4121 if (dependent() || SemaRef.CurContext->isDependentContext()) 4122 return false; 4123 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr) 4124 << S->getSourceRange() << LCDecl; 4125 return true; 4126 } 4127 4128 static ExprResult 4129 tryBuildCapture(Sema &SemaRef, Expr *Capture, 4130 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 4131 if (SemaRef.CurContext->isDependentContext()) 4132 return ExprResult(Capture); 4133 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 4134 return SemaRef.PerformImplicitConversion( 4135 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 4136 /*AllowExplicit=*/true); 4137 auto I = Captures.find(Capture); 4138 if (I != Captures.end()) 4139 return buildCapture(SemaRef, Capture, I->second); 4140 DeclRefExpr *Ref = nullptr; 4141 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 4142 Captures[Capture] = Ref; 4143 return Res; 4144 } 4145 4146 /// Build the expression to calculate the number of iterations. 4147 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 4148 Scope *S, const bool LimitedType, 4149 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 4150 ExprResult Diff; 4151 QualType VarType = LCDecl->getType().getNonReferenceType(); 4152 if (VarType->isIntegerType() || VarType->isPointerType() || 4153 SemaRef.getLangOpts().CPlusPlus) { 4154 // Upper - Lower 4155 Expr *UBExpr = TestIsLessOp ? UB : LB; 4156 Expr *LBExpr = TestIsLessOp ? LB : UB; 4157 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 4158 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 4159 if (!Upper || !Lower) 4160 return nullptr; 4161 4162 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 4163 4164 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 4165 // BuildBinOp already emitted error, this one is to point user to upper 4166 // and lower bound, and to tell what is passed to 'operator-'. 4167 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx) 4168 << Upper->getSourceRange() << Lower->getSourceRange(); 4169 return nullptr; 4170 } 4171 } 4172 4173 if (!Diff.isUsable()) 4174 return nullptr; 4175 4176 // Upper - Lower [- 1] 4177 if (TestIsStrictOp) 4178 Diff = SemaRef.BuildBinOp( 4179 S, DefaultLoc, BO_Sub, Diff.get(), 4180 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 4181 if (!Diff.isUsable()) 4182 return nullptr; 4183 4184 // Upper - Lower [- 1] + Step 4185 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 4186 if (!NewStep.isUsable()) 4187 return nullptr; 4188 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 4189 if (!Diff.isUsable()) 4190 return nullptr; 4191 4192 // Parentheses (for dumping/debugging purposes only). 4193 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 4194 if (!Diff.isUsable()) 4195 return nullptr; 4196 4197 // (Upper - Lower [- 1] + Step) / Step 4198 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 4199 if (!Diff.isUsable()) 4200 return nullptr; 4201 4202 // OpenMP runtime requires 32-bit or 64-bit loop variables. 4203 QualType Type = Diff.get()->getType(); 4204 ASTContext &C = SemaRef.Context; 4205 bool UseVarType = VarType->hasIntegerRepresentation() && 4206 C.getTypeSize(Type) > C.getTypeSize(VarType); 4207 if (!Type->isIntegerType() || UseVarType) { 4208 unsigned NewSize = 4209 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 4210 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 4211 : Type->hasSignedIntegerRepresentation(); 4212 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 4213 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 4214 Diff = SemaRef.PerformImplicitConversion( 4215 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 4216 if (!Diff.isUsable()) 4217 return nullptr; 4218 } 4219 } 4220 if (LimitedType) { 4221 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 4222 if (NewSize != C.getTypeSize(Type)) { 4223 if (NewSize < C.getTypeSize(Type)) { 4224 assert(NewSize == 64 && "incorrect loop var size"); 4225 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 4226 << InitSrcRange << ConditionSrcRange; 4227 } 4228 QualType NewType = C.getIntTypeForBitwidth( 4229 NewSize, Type->hasSignedIntegerRepresentation() || 4230 C.getTypeSize(Type) < NewSize); 4231 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 4232 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 4233 Sema::AA_Converting, true); 4234 if (!Diff.isUsable()) 4235 return nullptr; 4236 } 4237 } 4238 } 4239 4240 return Diff.get(); 4241 } 4242 4243 Expr *OpenMPIterationSpaceChecker::buildPreCond( 4244 Scope *S, Expr *Cond, 4245 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 4246 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 4247 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 4248 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 4249 4250 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 4251 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 4252 if (!NewLB.isUsable() || !NewUB.isUsable()) 4253 return nullptr; 4254 4255 ExprResult CondExpr = 4256 SemaRef.BuildBinOp(S, DefaultLoc, 4257 TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE) 4258 : (TestIsStrictOp ? BO_GT : BO_GE), 4259 NewLB.get(), NewUB.get()); 4260 if (CondExpr.isUsable()) { 4261 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 4262 SemaRef.Context.BoolTy)) 4263 CondExpr = SemaRef.PerformImplicitConversion( 4264 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 4265 /*AllowExplicit=*/true); 4266 } 4267 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 4268 // Otherwise use original loop conditon and evaluate it in runtime. 4269 return CondExpr.isUsable() ? CondExpr.get() : Cond; 4270 } 4271 4272 /// Build reference expression to the counter be used for codegen. 4273 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 4274 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const { 4275 auto *VD = dyn_cast<VarDecl>(LCDecl); 4276 if (!VD) { 4277 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 4278 DeclRefExpr *Ref = buildDeclRefExpr( 4279 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 4280 const DSAStackTy::DSAVarData Data = 4281 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 4282 // If the loop control decl is explicitly marked as private, do not mark it 4283 // as captured again. 4284 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 4285 Captures.insert(std::make_pair(LCRef, Ref)); 4286 return Ref; 4287 } 4288 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(), 4289 DefaultLoc); 4290 } 4291 4292 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 4293 if (LCDecl && !LCDecl->isInvalidDecl()) { 4294 QualType Type = LCDecl->getType().getNonReferenceType(); 4295 VarDecl *PrivateVar = buildVarDecl( 4296 SemaRef, DefaultLoc, Type, LCDecl->getName(), 4297 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 4298 isa<VarDecl>(LCDecl) 4299 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 4300 : nullptr); 4301 if (PrivateVar->isInvalidDecl()) 4302 return nullptr; 4303 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 4304 } 4305 return nullptr; 4306 } 4307 4308 /// Build initialization of the counter to be used for codegen. 4309 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 4310 4311 /// Build step of the counter be used for codegen. 4312 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 4313 4314 /// Iteration space of a single for loop. 4315 struct LoopIterationSpace final { 4316 /// Condition of the loop. 4317 Expr *PreCond = nullptr; 4318 /// This expression calculates the number of iterations in the loop. 4319 /// It is always possible to calculate it before starting the loop. 4320 Expr *NumIterations = nullptr; 4321 /// The loop counter variable. 4322 Expr *CounterVar = nullptr; 4323 /// Private loop counter variable. 4324 Expr *PrivateCounterVar = nullptr; 4325 /// This is initializer for the initial value of #CounterVar. 4326 Expr *CounterInit = nullptr; 4327 /// This is step for the #CounterVar used to generate its update: 4328 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 4329 Expr *CounterStep = nullptr; 4330 /// Should step be subtracted? 4331 bool Subtract = false; 4332 /// Source range of the loop init. 4333 SourceRange InitSrcRange; 4334 /// Source range of the loop condition. 4335 SourceRange CondSrcRange; 4336 /// Source range of the loop increment. 4337 SourceRange IncSrcRange; 4338 }; 4339 4340 } // namespace 4341 4342 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 4343 assert(getLangOpts().OpenMP && "OpenMP is not active."); 4344 assert(Init && "Expected loop in canonical form."); 4345 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 4346 if (AssociatedLoops > 0 && 4347 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 4348 OpenMPIterationSpaceChecker ISC(*this, ForLoc); 4349 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 4350 if (ValueDecl *D = ISC.getLoopDecl()) { 4351 auto *VD = dyn_cast<VarDecl>(D); 4352 if (!VD) { 4353 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 4354 VD = Private; 4355 } else { 4356 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 4357 /*WithInit=*/false); 4358 VD = cast<VarDecl>(Ref->getDecl()); 4359 } 4360 } 4361 DSAStack->addLoopControlVariable(D, VD); 4362 } 4363 } 4364 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 4365 } 4366 } 4367 4368 /// Called on a for stmt to check and extract its iteration space 4369 /// for further processing (such as collapsing). 4370 static bool checkOpenMPIterationSpace( 4371 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 4372 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 4373 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr, 4374 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 4375 LoopIterationSpace &ResultIterSpace, 4376 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 4377 // OpenMP [2.6, Canonical Loop Form] 4378 // for (init-expr; test-expr; incr-expr) structured-block 4379 auto *For = dyn_cast_or_null<ForStmt>(S); 4380 if (!For) { 4381 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for) 4382 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 4383 << getOpenMPDirectiveName(DKind) << NestedLoopCount 4384 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 4385 if (NestedLoopCount > 1) { 4386 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 4387 SemaRef.Diag(DSA.getConstructLoc(), 4388 diag::note_omp_collapse_ordered_expr) 4389 << 2 << CollapseLoopCountExpr->getSourceRange() 4390 << OrderedLoopCountExpr->getSourceRange(); 4391 else if (CollapseLoopCountExpr) 4392 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 4393 diag::note_omp_collapse_ordered_expr) 4394 << 0 << CollapseLoopCountExpr->getSourceRange(); 4395 else 4396 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 4397 diag::note_omp_collapse_ordered_expr) 4398 << 1 << OrderedLoopCountExpr->getSourceRange(); 4399 } 4400 return true; 4401 } 4402 assert(For->getBody()); 4403 4404 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc()); 4405 4406 // Check init. 4407 Stmt *Init = For->getInit(); 4408 if (ISC.checkAndSetInit(Init)) 4409 return true; 4410 4411 bool HasErrors = false; 4412 4413 // Check loop variable's type. 4414 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 4415 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 4416 4417 // OpenMP [2.6, Canonical Loop Form] 4418 // Var is one of the following: 4419 // A variable of signed or unsigned integer type. 4420 // For C++, a variable of a random access iterator type. 4421 // For C, a variable of a pointer type. 4422 QualType VarType = LCDecl->getType().getNonReferenceType(); 4423 if (!VarType->isDependentType() && !VarType->isIntegerType() && 4424 !VarType->isPointerType() && 4425 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 4426 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type) 4427 << SemaRef.getLangOpts().CPlusPlus; 4428 HasErrors = true; 4429 } 4430 4431 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 4432 // a Construct 4433 // The loop iteration variable(s) in the associated for-loop(s) of a for or 4434 // parallel for construct is (are) private. 4435 // The loop iteration variable in the associated for-loop of a simd 4436 // construct with just one associated for-loop is linear with a 4437 // constant-linear-step that is the increment of the associated for-loop. 4438 // Exclude loop var from the list of variables with implicitly defined data 4439 // sharing attributes. 4440 VarsWithImplicitDSA.erase(LCDecl); 4441 4442 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 4443 // in a Construct, C/C++]. 4444 // The loop iteration variable in the associated for-loop of a simd 4445 // construct with just one associated for-loop may be listed in a linear 4446 // clause with a constant-linear-step that is the increment of the 4447 // associated for-loop. 4448 // The loop iteration variable(s) in the associated for-loop(s) of a for or 4449 // parallel for construct may be listed in a private or lastprivate clause. 4450 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false); 4451 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is 4452 // declared in the loop and it is predetermined as a private. 4453 OpenMPClauseKind PredeterminedCKind = 4454 isOpenMPSimdDirective(DKind) 4455 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate) 4456 : OMPC_private; 4457 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 4458 DVar.CKind != PredeterminedCKind) || 4459 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 4460 isOpenMPDistributeDirective(DKind)) && 4461 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 4462 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 4463 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 4464 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa) 4465 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind) 4466 << getOpenMPClauseName(PredeterminedCKind); 4467 if (DVar.RefExpr == nullptr) 4468 DVar.CKind = PredeterminedCKind; 4469 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true); 4470 HasErrors = true; 4471 } else if (LoopDeclRefExpr != nullptr) { 4472 // Make the loop iteration variable private (for worksharing constructs), 4473 // linear (for simd directives with the only one associated loop) or 4474 // lastprivate (for simd directives with several collapsed or ordered 4475 // loops). 4476 if (DVar.CKind == OMPC_unknown) 4477 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate, 4478 [](OpenMPDirectiveKind) -> bool { return true; }, 4479 /*FromParent=*/false); 4480 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind); 4481 } 4482 4483 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 4484 4485 // Check test-expr. 4486 HasErrors |= ISC.checkAndSetCond(For->getCond()); 4487 4488 // Check incr-expr. 4489 HasErrors |= ISC.checkAndSetInc(For->getInc()); 4490 } 4491 4492 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 4493 return HasErrors; 4494 4495 // Build the loop's iteration space representation. 4496 ResultIterSpace.PreCond = 4497 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures); 4498 ResultIterSpace.NumIterations = ISC.buildNumIterations( 4499 DSA.getCurScope(), 4500 (isOpenMPWorksharingDirective(DKind) || 4501 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)), 4502 Captures); 4503 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA); 4504 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar(); 4505 ResultIterSpace.CounterInit = ISC.buildCounterInit(); 4506 ResultIterSpace.CounterStep = ISC.buildCounterStep(); 4507 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange(); 4508 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange(); 4509 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange(); 4510 ResultIterSpace.Subtract = ISC.shouldSubtractStep(); 4511 4512 HasErrors |= (ResultIterSpace.PreCond == nullptr || 4513 ResultIterSpace.NumIterations == nullptr || 4514 ResultIterSpace.CounterVar == nullptr || 4515 ResultIterSpace.PrivateCounterVar == nullptr || 4516 ResultIterSpace.CounterInit == nullptr || 4517 ResultIterSpace.CounterStep == nullptr); 4518 4519 return HasErrors; 4520 } 4521 4522 /// Build 'VarRef = Start. 4523 static ExprResult 4524 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 4525 ExprResult Start, 4526 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 4527 // Build 'VarRef = Start. 4528 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures); 4529 if (!NewStart.isUsable()) 4530 return ExprError(); 4531 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 4532 VarRef.get()->getType())) { 4533 NewStart = SemaRef.PerformImplicitConversion( 4534 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 4535 /*AllowExplicit=*/true); 4536 if (!NewStart.isUsable()) 4537 return ExprError(); 4538 } 4539 4540 ExprResult Init = 4541 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 4542 return Init; 4543 } 4544 4545 /// Build 'VarRef = Start + Iter * Step'. 4546 static ExprResult buildCounterUpdate( 4547 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 4548 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 4549 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 4550 // Add parentheses (for debugging purposes only). 4551 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 4552 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 4553 !Step.isUsable()) 4554 return ExprError(); 4555 4556 ExprResult NewStep = Step; 4557 if (Captures) 4558 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 4559 if (NewStep.isInvalid()) 4560 return ExprError(); 4561 ExprResult Update = 4562 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 4563 if (!Update.isUsable()) 4564 return ExprError(); 4565 4566 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 4567 // 'VarRef = Start (+|-) Iter * Step'. 4568 ExprResult NewStart = Start; 4569 if (Captures) 4570 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 4571 if (NewStart.isInvalid()) 4572 return ExprError(); 4573 4574 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 4575 ExprResult SavedUpdate = Update; 4576 ExprResult UpdateVal; 4577 if (VarRef.get()->getType()->isOverloadableType() || 4578 NewStart.get()->getType()->isOverloadableType() || 4579 Update.get()->getType()->isOverloadableType()) { 4580 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 4581 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 4582 Update = 4583 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 4584 if (Update.isUsable()) { 4585 UpdateVal = 4586 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 4587 VarRef.get(), SavedUpdate.get()); 4588 if (UpdateVal.isUsable()) { 4589 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 4590 UpdateVal.get()); 4591 } 4592 } 4593 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 4594 } 4595 4596 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 4597 if (!Update.isUsable() || !UpdateVal.isUsable()) { 4598 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 4599 NewStart.get(), SavedUpdate.get()); 4600 if (!Update.isUsable()) 4601 return ExprError(); 4602 4603 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 4604 VarRef.get()->getType())) { 4605 Update = SemaRef.PerformImplicitConversion( 4606 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 4607 if (!Update.isUsable()) 4608 return ExprError(); 4609 } 4610 4611 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 4612 } 4613 return Update; 4614 } 4615 4616 /// Convert integer expression \a E to make it have at least \a Bits 4617 /// bits. 4618 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 4619 if (E == nullptr) 4620 return ExprError(); 4621 ASTContext &C = SemaRef.Context; 4622 QualType OldType = E->getType(); 4623 unsigned HasBits = C.getTypeSize(OldType); 4624 if (HasBits >= Bits) 4625 return ExprResult(E); 4626 // OK to convert to signed, because new type has more bits than old. 4627 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 4628 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 4629 true); 4630 } 4631 4632 /// Check if the given expression \a E is a constant integer that fits 4633 /// into \a Bits bits. 4634 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 4635 if (E == nullptr) 4636 return false; 4637 llvm::APSInt Result; 4638 if (E->isIntegerConstantExpr(Result, SemaRef.Context)) 4639 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); 4640 return false; 4641 } 4642 4643 /// Build preinits statement for the given declarations. 4644 static Stmt *buildPreInits(ASTContext &Context, 4645 MutableArrayRef<Decl *> PreInits) { 4646 if (!PreInits.empty()) { 4647 return new (Context) DeclStmt( 4648 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 4649 SourceLocation(), SourceLocation()); 4650 } 4651 return nullptr; 4652 } 4653 4654 /// Build preinits statement for the given declarations. 4655 static Stmt * 4656 buildPreInits(ASTContext &Context, 4657 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 4658 if (!Captures.empty()) { 4659 SmallVector<Decl *, 16> PreInits; 4660 for (const auto &Pair : Captures) 4661 PreInits.push_back(Pair.second->getDecl()); 4662 return buildPreInits(Context, PreInits); 4663 } 4664 return nullptr; 4665 } 4666 4667 /// Build postupdate expression for the given list of postupdates expressions. 4668 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 4669 Expr *PostUpdate = nullptr; 4670 if (!PostUpdates.empty()) { 4671 for (Expr *E : PostUpdates) { 4672 Expr *ConvE = S.BuildCStyleCastExpr( 4673 E->getExprLoc(), 4674 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 4675 E->getExprLoc(), E) 4676 .get(); 4677 PostUpdate = PostUpdate 4678 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 4679 PostUpdate, ConvE) 4680 .get() 4681 : ConvE; 4682 } 4683 } 4684 return PostUpdate; 4685 } 4686 4687 /// Called on a for stmt to check itself and nested loops (if any). 4688 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 4689 /// number of collapsed loops otherwise. 4690 static unsigned 4691 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 4692 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 4693 DSAStackTy &DSA, 4694 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 4695 OMPLoopDirective::HelperExprs &Built) { 4696 unsigned NestedLoopCount = 1; 4697 if (CollapseLoopCountExpr) { 4698 // Found 'collapse' clause - calculate collapse number. 4699 llvm::APSInt Result; 4700 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) 4701 NestedLoopCount = Result.getLimitedValue(); 4702 } 4703 if (OrderedLoopCountExpr) { 4704 // Found 'ordered' clause - calculate collapse number. 4705 llvm::APSInt Result; 4706 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 4707 if (Result.getLimitedValue() < NestedLoopCount) { 4708 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 4709 diag::err_omp_wrong_ordered_loop_count) 4710 << OrderedLoopCountExpr->getSourceRange(); 4711 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 4712 diag::note_collapse_loop_count) 4713 << CollapseLoopCountExpr->getSourceRange(); 4714 } 4715 NestedLoopCount = Result.getLimitedValue(); 4716 } 4717 } 4718 // This is helper routine for loop directives (e.g., 'for', 'simd', 4719 // 'for simd', etc.). 4720 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 4721 SmallVector<LoopIterationSpace, 4> IterSpaces; 4722 IterSpaces.resize(NestedLoopCount); 4723 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); 4724 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 4725 if (checkOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt, 4726 NestedLoopCount, CollapseLoopCountExpr, 4727 OrderedLoopCountExpr, VarsWithImplicitDSA, 4728 IterSpaces[Cnt], Captures)) 4729 return 0; 4730 // Move on to the next nested for loop, or to the loop body. 4731 // OpenMP [2.8.1, simd construct, Restrictions] 4732 // All loops associated with the construct must be perfectly nested; that 4733 // is, there must be no intervening code nor any OpenMP directive between 4734 // any two loops. 4735 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); 4736 } 4737 4738 Built.clear(/* size */ NestedLoopCount); 4739 4740 if (SemaRef.CurContext->isDependentContext()) 4741 return NestedLoopCount; 4742 4743 // An example of what is generated for the following code: 4744 // 4745 // #pragma omp simd collapse(2) ordered(2) 4746 // for (i = 0; i < NI; ++i) 4747 // for (k = 0; k < NK; ++k) 4748 // for (j = J0; j < NJ; j+=2) { 4749 // <loop body> 4750 // } 4751 // 4752 // We generate the code below. 4753 // Note: the loop body may be outlined in CodeGen. 4754 // Note: some counters may be C++ classes, operator- is used to find number of 4755 // iterations and operator+= to calculate counter value. 4756 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 4757 // or i64 is currently supported). 4758 // 4759 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 4760 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 4761 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 4762 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 4763 // // similar updates for vars in clauses (e.g. 'linear') 4764 // <loop body (using local i and j)> 4765 // } 4766 // i = NI; // assign final values of counters 4767 // j = NJ; 4768 // 4769 4770 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 4771 // the iteration counts of the collapsed for loops. 4772 // Precondition tests if there is at least one iteration (all conditions are 4773 // true). 4774 auto PreCond = ExprResult(IterSpaces[0].PreCond); 4775 Expr *N0 = IterSpaces[0].NumIterations; 4776 ExprResult LastIteration32 = 4777 widenIterationCount(/*Bits=*/32, 4778 SemaRef 4779 .PerformImplicitConversion( 4780 N0->IgnoreImpCasts(), N0->getType(), 4781 Sema::AA_Converting, /*AllowExplicit=*/true) 4782 .get(), 4783 SemaRef); 4784 ExprResult LastIteration64 = widenIterationCount( 4785 /*Bits=*/64, 4786 SemaRef 4787 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 4788 Sema::AA_Converting, 4789 /*AllowExplicit=*/true) 4790 .get(), 4791 SemaRef); 4792 4793 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 4794 return NestedLoopCount; 4795 4796 ASTContext &C = SemaRef.Context; 4797 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 4798 4799 Scope *CurScope = DSA.getCurScope(); 4800 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 4801 if (PreCond.isUsable()) { 4802 PreCond = 4803 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 4804 PreCond.get(), IterSpaces[Cnt].PreCond); 4805 } 4806 Expr *N = IterSpaces[Cnt].NumIterations; 4807 SourceLocation Loc = N->getExprLoc(); 4808 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 4809 if (LastIteration32.isUsable()) 4810 LastIteration32 = SemaRef.BuildBinOp( 4811 CurScope, Loc, BO_Mul, LastIteration32.get(), 4812 SemaRef 4813 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 4814 Sema::AA_Converting, 4815 /*AllowExplicit=*/true) 4816 .get()); 4817 if (LastIteration64.isUsable()) 4818 LastIteration64 = SemaRef.BuildBinOp( 4819 CurScope, Loc, BO_Mul, LastIteration64.get(), 4820 SemaRef 4821 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 4822 Sema::AA_Converting, 4823 /*AllowExplicit=*/true) 4824 .get()); 4825 } 4826 4827 // Choose either the 32-bit or 64-bit version. 4828 ExprResult LastIteration = LastIteration64; 4829 if (LastIteration32.isUsable() && 4830 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 4831 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 4832 fitsInto( 4833 /*Bits=*/32, 4834 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 4835 LastIteration64.get(), SemaRef))) 4836 LastIteration = LastIteration32; 4837 QualType VType = LastIteration.get()->getType(); 4838 QualType RealVType = VType; 4839 QualType StrideVType = VType; 4840 if (isOpenMPTaskLoopDirective(DKind)) { 4841 VType = 4842 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 4843 StrideVType = 4844 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 4845 } 4846 4847 if (!LastIteration.isUsable()) 4848 return 0; 4849 4850 // Save the number of iterations. 4851 ExprResult NumIterations = LastIteration; 4852 { 4853 LastIteration = SemaRef.BuildBinOp( 4854 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 4855 LastIteration.get(), 4856 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 4857 if (!LastIteration.isUsable()) 4858 return 0; 4859 } 4860 4861 // Calculate the last iteration number beforehand instead of doing this on 4862 // each iteration. Do not do this if the number of iterations may be kfold-ed. 4863 llvm::APSInt Result; 4864 bool IsConstant = 4865 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); 4866 ExprResult CalcLastIteration; 4867 if (!IsConstant) { 4868 ExprResult SaveRef = 4869 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 4870 LastIteration = SaveRef; 4871 4872 // Prepare SaveRef + 1. 4873 NumIterations = SemaRef.BuildBinOp( 4874 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 4875 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 4876 if (!NumIterations.isUsable()) 4877 return 0; 4878 } 4879 4880 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 4881 4882 // Build variables passed into runtime, necessary for worksharing directives. 4883 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 4884 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 4885 isOpenMPDistributeDirective(DKind)) { 4886 // Lower bound variable, initialized with zero. 4887 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 4888 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 4889 SemaRef.AddInitializerToDecl(LBDecl, 4890 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 4891 /*DirectInit*/ false); 4892 4893 // Upper bound variable, initialized with last iteration number. 4894 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 4895 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 4896 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 4897 /*DirectInit*/ false); 4898 4899 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 4900 // This will be used to implement clause 'lastprivate'. 4901 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 4902 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 4903 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 4904 SemaRef.AddInitializerToDecl(ILDecl, 4905 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 4906 /*DirectInit*/ false); 4907 4908 // Stride variable returned by runtime (we initialize it to 1 by default). 4909 VarDecl *STDecl = 4910 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 4911 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 4912 SemaRef.AddInitializerToDecl(STDecl, 4913 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 4914 /*DirectInit*/ false); 4915 4916 // Build expression: UB = min(UB, LastIteration) 4917 // It is necessary for CodeGen of directives with static scheduling. 4918 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 4919 UB.get(), LastIteration.get()); 4920 ExprResult CondOp = SemaRef.ActOnConditionalOp( 4921 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 4922 LastIteration.get(), UB.get()); 4923 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 4924 CondOp.get()); 4925 EUB = SemaRef.ActOnFinishFullExpr(EUB.get()); 4926 4927 // If we have a combined directive that combines 'distribute', 'for' or 4928 // 'simd' we need to be able to access the bounds of the schedule of the 4929 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 4930 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 4931 if (isOpenMPLoopBoundSharingDirective(DKind)) { 4932 // Lower bound variable, initialized with zero. 4933 VarDecl *CombLBDecl = 4934 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 4935 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 4936 SemaRef.AddInitializerToDecl( 4937 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 4938 /*DirectInit*/ false); 4939 4940 // Upper bound variable, initialized with last iteration number. 4941 VarDecl *CombUBDecl = 4942 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 4943 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 4944 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 4945 /*DirectInit*/ false); 4946 4947 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 4948 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 4949 ExprResult CombCondOp = 4950 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 4951 LastIteration.get(), CombUB.get()); 4952 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 4953 CombCondOp.get()); 4954 CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get()); 4955 4956 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 4957 // We expect to have at least 2 more parameters than the 'parallel' 4958 // directive does - the lower and upper bounds of the previous schedule. 4959 assert(CD->getNumParams() >= 4 && 4960 "Unexpected number of parameters in loop combined directive"); 4961 4962 // Set the proper type for the bounds given what we learned from the 4963 // enclosed loops. 4964 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 4965 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 4966 4967 // Previous lower and upper bounds are obtained from the region 4968 // parameters. 4969 PrevLB = 4970 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 4971 PrevUB = 4972 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 4973 } 4974 } 4975 4976 // Build the iteration variable and its initialization before loop. 4977 ExprResult IV; 4978 ExprResult Init, CombInit; 4979 { 4980 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 4981 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 4982 Expr *RHS = 4983 (isOpenMPWorksharingDirective(DKind) || 4984 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 4985 ? LB.get() 4986 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 4987 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 4988 Init = SemaRef.ActOnFinishFullExpr(Init.get()); 4989 4990 if (isOpenMPLoopBoundSharingDirective(DKind)) { 4991 Expr *CombRHS = 4992 (isOpenMPWorksharingDirective(DKind) || 4993 isOpenMPTaskLoopDirective(DKind) || 4994 isOpenMPDistributeDirective(DKind)) 4995 ? CombLB.get() 4996 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 4997 CombInit = 4998 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 4999 CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get()); 5000 } 5001 } 5002 5003 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops. 5004 SourceLocation CondLoc = AStmt->getLocStart(); 5005 ExprResult Cond = 5006 (isOpenMPWorksharingDirective(DKind) || 5007 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 5008 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get()) 5009 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 5010 NumIterations.get()); 5011 ExprResult CombCond; 5012 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5013 CombCond = 5014 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get()); 5015 } 5016 // Loop increment (IV = IV + 1) 5017 SourceLocation IncLoc = AStmt->getLocStart(); 5018 ExprResult Inc = 5019 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 5020 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 5021 if (!Inc.isUsable()) 5022 return 0; 5023 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 5024 Inc = SemaRef.ActOnFinishFullExpr(Inc.get()); 5025 if (!Inc.isUsable()) 5026 return 0; 5027 5028 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 5029 // Used for directives with static scheduling. 5030 // In combined construct, add combined version that use CombLB and CombUB 5031 // base variables for the update 5032 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 5033 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 5034 isOpenMPDistributeDirective(DKind)) { 5035 // LB + ST 5036 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 5037 if (!NextLB.isUsable()) 5038 return 0; 5039 // LB = LB + ST 5040 NextLB = 5041 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 5042 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get()); 5043 if (!NextLB.isUsable()) 5044 return 0; 5045 // UB + ST 5046 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 5047 if (!NextUB.isUsable()) 5048 return 0; 5049 // UB = UB + ST 5050 NextUB = 5051 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 5052 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get()); 5053 if (!NextUB.isUsable()) 5054 return 0; 5055 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5056 CombNextLB = 5057 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 5058 if (!NextLB.isUsable()) 5059 return 0; 5060 // LB = LB + ST 5061 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 5062 CombNextLB.get()); 5063 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get()); 5064 if (!CombNextLB.isUsable()) 5065 return 0; 5066 // UB + ST 5067 CombNextUB = 5068 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 5069 if (!CombNextUB.isUsable()) 5070 return 0; 5071 // UB = UB + ST 5072 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 5073 CombNextUB.get()); 5074 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get()); 5075 if (!CombNextUB.isUsable()) 5076 return 0; 5077 } 5078 } 5079 5080 // Create increment expression for distribute loop when combined in a same 5081 // directive with for as IV = IV + ST; ensure upper bound expression based 5082 // on PrevUB instead of NumIterations - used to implement 'for' when found 5083 // in combination with 'distribute', like in 'distribute parallel for' 5084 SourceLocation DistIncLoc = AStmt->getLocStart(); 5085 ExprResult DistCond, DistInc, PrevEUB; 5086 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5087 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get()); 5088 assert(DistCond.isUsable() && "distribute cond expr was not built"); 5089 5090 DistInc = 5091 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 5092 assert(DistInc.isUsable() && "distribute inc expr was not built"); 5093 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 5094 DistInc.get()); 5095 DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get()); 5096 assert(DistInc.isUsable() && "distribute inc expr was not built"); 5097 5098 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 5099 // construct 5100 SourceLocation DistEUBLoc = AStmt->getLocStart(); 5101 ExprResult IsUBGreater = 5102 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get()); 5103 ExprResult CondOp = SemaRef.ActOnConditionalOp( 5104 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get()); 5105 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 5106 CondOp.get()); 5107 PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get()); 5108 } 5109 5110 // Build updates and final values of the loop counters. 5111 bool HasErrors = false; 5112 Built.Counters.resize(NestedLoopCount); 5113 Built.Inits.resize(NestedLoopCount); 5114 Built.Updates.resize(NestedLoopCount); 5115 Built.Finals.resize(NestedLoopCount); 5116 SmallVector<Expr *, 4> LoopMultipliers; 5117 { 5118 ExprResult Div; 5119 // Go from inner nested loop to outer. 5120 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) { 5121 LoopIterationSpace &IS = IterSpaces[Cnt]; 5122 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 5123 // Build: Iter = (IV / Div) % IS.NumIters 5124 // where Div is product of previous iterations' IS.NumIters. 5125 ExprResult Iter; 5126 if (Div.isUsable()) { 5127 Iter = 5128 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get()); 5129 } else { 5130 Iter = IV; 5131 assert((Cnt == (int)NestedLoopCount - 1) && 5132 "unusable div expected on first iteration only"); 5133 } 5134 5135 if (Cnt != 0 && Iter.isUsable()) 5136 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(), 5137 IS.NumIterations); 5138 if (!Iter.isUsable()) { 5139 HasErrors = true; 5140 break; 5141 } 5142 5143 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 5144 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 5145 DeclRefExpr *CounterVar = buildDeclRefExpr( 5146 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 5147 /*RefersToCapture=*/true); 5148 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 5149 IS.CounterInit, Captures); 5150 if (!Init.isUsable()) { 5151 HasErrors = true; 5152 break; 5153 } 5154 ExprResult Update = buildCounterUpdate( 5155 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 5156 IS.CounterStep, IS.Subtract, &Captures); 5157 if (!Update.isUsable()) { 5158 HasErrors = true; 5159 break; 5160 } 5161 5162 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 5163 ExprResult Final = buildCounterUpdate( 5164 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, 5165 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures); 5166 if (!Final.isUsable()) { 5167 HasErrors = true; 5168 break; 5169 } 5170 5171 // Build Div for the next iteration: Div <- Div * IS.NumIters 5172 if (Cnt != 0) { 5173 if (Div.isUnset()) 5174 Div = IS.NumIterations; 5175 else 5176 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(), 5177 IS.NumIterations); 5178 5179 // Add parentheses (for debugging purposes only). 5180 if (Div.isUsable()) 5181 Div = tryBuildCapture(SemaRef, Div.get(), Captures); 5182 if (!Div.isUsable()) { 5183 HasErrors = true; 5184 break; 5185 } 5186 LoopMultipliers.push_back(Div.get()); 5187 } 5188 if (!Update.isUsable() || !Final.isUsable()) { 5189 HasErrors = true; 5190 break; 5191 } 5192 // Save results 5193 Built.Counters[Cnt] = IS.CounterVar; 5194 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 5195 Built.Inits[Cnt] = Init.get(); 5196 Built.Updates[Cnt] = Update.get(); 5197 Built.Finals[Cnt] = Final.get(); 5198 } 5199 } 5200 5201 if (HasErrors) 5202 return 0; 5203 5204 // Save results 5205 Built.IterationVarRef = IV.get(); 5206 Built.LastIteration = LastIteration.get(); 5207 Built.NumIterations = NumIterations.get(); 5208 Built.CalcLastIteration = 5209 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get(); 5210 Built.PreCond = PreCond.get(); 5211 Built.PreInits = buildPreInits(C, Captures); 5212 Built.Cond = Cond.get(); 5213 Built.Init = Init.get(); 5214 Built.Inc = Inc.get(); 5215 Built.LB = LB.get(); 5216 Built.UB = UB.get(); 5217 Built.IL = IL.get(); 5218 Built.ST = ST.get(); 5219 Built.EUB = EUB.get(); 5220 Built.NLB = NextLB.get(); 5221 Built.NUB = NextUB.get(); 5222 Built.PrevLB = PrevLB.get(); 5223 Built.PrevUB = PrevUB.get(); 5224 Built.DistInc = DistInc.get(); 5225 Built.PrevEUB = PrevEUB.get(); 5226 Built.DistCombinedFields.LB = CombLB.get(); 5227 Built.DistCombinedFields.UB = CombUB.get(); 5228 Built.DistCombinedFields.EUB = CombEUB.get(); 5229 Built.DistCombinedFields.Init = CombInit.get(); 5230 Built.DistCombinedFields.Cond = CombCond.get(); 5231 Built.DistCombinedFields.NLB = CombNextLB.get(); 5232 Built.DistCombinedFields.NUB = CombNextUB.get(); 5233 5234 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get(); 5235 // Fill data for doacross depend clauses. 5236 for (const auto &Pair : DSA.getDoacrossDependClauses()) { 5237 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) { 5238 Pair.first->setCounterValue(CounterVal); 5239 } else { 5240 if (NestedLoopCount != Pair.second.size() || 5241 NestedLoopCount != LoopMultipliers.size() + 1) { 5242 // Erroneous case - clause has some problems. 5243 Pair.first->setCounterValue(CounterVal); 5244 continue; 5245 } 5246 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink); 5247 auto I = Pair.second.rbegin(); 5248 auto IS = IterSpaces.rbegin(); 5249 auto ILM = LoopMultipliers.rbegin(); 5250 Expr *UpCounterVal = CounterVal; 5251 Expr *Multiplier = nullptr; 5252 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) { 5253 if (I->first) { 5254 assert(IS->CounterStep); 5255 Expr *NormalizedOffset = 5256 SemaRef 5257 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div, 5258 I->first, IS->CounterStep) 5259 .get(); 5260 if (Multiplier) { 5261 NormalizedOffset = 5262 SemaRef 5263 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul, 5264 NormalizedOffset, Multiplier) 5265 .get(); 5266 } 5267 assert(I->second == OO_Plus || I->second == OO_Minus); 5268 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub; 5269 UpCounterVal = SemaRef 5270 .BuildBinOp(CurScope, I->first->getExprLoc(), BOK, 5271 UpCounterVal, NormalizedOffset) 5272 .get(); 5273 } 5274 Multiplier = *ILM; 5275 ++I; 5276 ++IS; 5277 ++ILM; 5278 } 5279 Pair.first->setCounterValue(UpCounterVal); 5280 } 5281 } 5282 5283 return NestedLoopCount; 5284 } 5285 5286 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 5287 auto CollapseClauses = 5288 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 5289 if (CollapseClauses.begin() != CollapseClauses.end()) 5290 return (*CollapseClauses.begin())->getNumForLoops(); 5291 return nullptr; 5292 } 5293 5294 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 5295 auto OrderedClauses = 5296 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 5297 if (OrderedClauses.begin() != OrderedClauses.end()) 5298 return (*OrderedClauses.begin())->getNumForLoops(); 5299 return nullptr; 5300 } 5301 5302 static bool checkSimdlenSafelenSpecified(Sema &S, 5303 const ArrayRef<OMPClause *> Clauses) { 5304 const OMPSafelenClause *Safelen = nullptr; 5305 const OMPSimdlenClause *Simdlen = nullptr; 5306 5307 for (const OMPClause *Clause : Clauses) { 5308 if (Clause->getClauseKind() == OMPC_safelen) 5309 Safelen = cast<OMPSafelenClause>(Clause); 5310 else if (Clause->getClauseKind() == OMPC_simdlen) 5311 Simdlen = cast<OMPSimdlenClause>(Clause); 5312 if (Safelen && Simdlen) 5313 break; 5314 } 5315 5316 if (Simdlen && Safelen) { 5317 llvm::APSInt SimdlenRes, SafelenRes; 5318 const Expr *SimdlenLength = Simdlen->getSimdlen(); 5319 const Expr *SafelenLength = Safelen->getSafelen(); 5320 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 5321 SimdlenLength->isInstantiationDependent() || 5322 SimdlenLength->containsUnexpandedParameterPack()) 5323 return false; 5324 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 5325 SafelenLength->isInstantiationDependent() || 5326 SafelenLength->containsUnexpandedParameterPack()) 5327 return false; 5328 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context); 5329 SafelenLength->EvaluateAsInt(SafelenRes, S.Context); 5330 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 5331 // If both simdlen and safelen clauses are specified, the value of the 5332 // simdlen parameter must be less than or equal to the value of the safelen 5333 // parameter. 5334 if (SimdlenRes > SafelenRes) { 5335 S.Diag(SimdlenLength->getExprLoc(), 5336 diag::err_omp_wrong_simdlen_safelen_values) 5337 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 5338 return true; 5339 } 5340 } 5341 return false; 5342 } 5343 5344 StmtResult 5345 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 5346 SourceLocation StartLoc, SourceLocation EndLoc, 5347 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5348 if (!AStmt) 5349 return StmtError(); 5350 5351 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5352 OMPLoopDirective::HelperExprs B; 5353 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5354 // define the nested loops number. 5355 unsigned NestedLoopCount = checkOpenMPLoop( 5356 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 5357 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 5358 if (NestedLoopCount == 0) 5359 return StmtError(); 5360 5361 assert((CurContext->isDependentContext() || B.builtAll()) && 5362 "omp simd loop exprs were not built"); 5363 5364 if (!CurContext->isDependentContext()) { 5365 // Finalize the clauses that need pre-built expressions for CodeGen. 5366 for (OMPClause *C : Clauses) { 5367 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5368 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5369 B.NumIterations, *this, CurScope, 5370 DSAStack)) 5371 return StmtError(); 5372 } 5373 } 5374 5375 if (checkSimdlenSafelenSpecified(*this, Clauses)) 5376 return StmtError(); 5377 5378 setFunctionHasBranchProtectedScope(); 5379 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 5380 Clauses, AStmt, B); 5381 } 5382 5383 StmtResult 5384 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 5385 SourceLocation StartLoc, SourceLocation EndLoc, 5386 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5387 if (!AStmt) 5388 return StmtError(); 5389 5390 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5391 OMPLoopDirective::HelperExprs B; 5392 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5393 // define the nested loops number. 5394 unsigned NestedLoopCount = checkOpenMPLoop( 5395 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 5396 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 5397 if (NestedLoopCount == 0) 5398 return StmtError(); 5399 5400 assert((CurContext->isDependentContext() || B.builtAll()) && 5401 "omp for loop exprs were not built"); 5402 5403 if (!CurContext->isDependentContext()) { 5404 // Finalize the clauses that need pre-built expressions for CodeGen. 5405 for (OMPClause *C : Clauses) { 5406 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5407 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5408 B.NumIterations, *this, CurScope, 5409 DSAStack)) 5410 return StmtError(); 5411 } 5412 } 5413 5414 setFunctionHasBranchProtectedScope(); 5415 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 5416 Clauses, AStmt, B, DSAStack->isCancelRegion()); 5417 } 5418 5419 StmtResult Sema::ActOnOpenMPForSimdDirective( 5420 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5421 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5422 if (!AStmt) 5423 return StmtError(); 5424 5425 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5426 OMPLoopDirective::HelperExprs B; 5427 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5428 // define the nested loops number. 5429 unsigned NestedLoopCount = 5430 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 5431 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 5432 VarsWithImplicitDSA, B); 5433 if (NestedLoopCount == 0) 5434 return StmtError(); 5435 5436 assert((CurContext->isDependentContext() || B.builtAll()) && 5437 "omp for simd loop exprs were not built"); 5438 5439 if (!CurContext->isDependentContext()) { 5440 // Finalize the clauses that need pre-built expressions for CodeGen. 5441 for (OMPClause *C : Clauses) { 5442 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5443 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5444 B.NumIterations, *this, CurScope, 5445 DSAStack)) 5446 return StmtError(); 5447 } 5448 } 5449 5450 if (checkSimdlenSafelenSpecified(*this, Clauses)) 5451 return StmtError(); 5452 5453 setFunctionHasBranchProtectedScope(); 5454 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 5455 Clauses, AStmt, B); 5456 } 5457 5458 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 5459 Stmt *AStmt, 5460 SourceLocation StartLoc, 5461 SourceLocation EndLoc) { 5462 if (!AStmt) 5463 return StmtError(); 5464 5465 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5466 auto BaseStmt = AStmt; 5467 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 5468 BaseStmt = CS->getCapturedStmt(); 5469 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 5470 auto S = C->children(); 5471 if (S.begin() == S.end()) 5472 return StmtError(); 5473 // All associated statements must be '#pragma omp section' except for 5474 // the first one. 5475 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 5476 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 5477 if (SectionStmt) 5478 Diag(SectionStmt->getLocStart(), 5479 diag::err_omp_sections_substmt_not_section); 5480 return StmtError(); 5481 } 5482 cast<OMPSectionDirective>(SectionStmt) 5483 ->setHasCancel(DSAStack->isCancelRegion()); 5484 } 5485 } else { 5486 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt); 5487 return StmtError(); 5488 } 5489 5490 setFunctionHasBranchProtectedScope(); 5491 5492 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 5493 DSAStack->isCancelRegion()); 5494 } 5495 5496 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 5497 SourceLocation StartLoc, 5498 SourceLocation EndLoc) { 5499 if (!AStmt) 5500 return StmtError(); 5501 5502 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5503 5504 setFunctionHasBranchProtectedScope(); 5505 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 5506 5507 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 5508 DSAStack->isCancelRegion()); 5509 } 5510 5511 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 5512 Stmt *AStmt, 5513 SourceLocation StartLoc, 5514 SourceLocation EndLoc) { 5515 if (!AStmt) 5516 return StmtError(); 5517 5518 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5519 5520 setFunctionHasBranchProtectedScope(); 5521 5522 // OpenMP [2.7.3, single Construct, Restrictions] 5523 // The copyprivate clause must not be used with the nowait clause. 5524 const OMPClause *Nowait = nullptr; 5525 const OMPClause *Copyprivate = nullptr; 5526 for (const OMPClause *Clause : Clauses) { 5527 if (Clause->getClauseKind() == OMPC_nowait) 5528 Nowait = Clause; 5529 else if (Clause->getClauseKind() == OMPC_copyprivate) 5530 Copyprivate = Clause; 5531 if (Copyprivate && Nowait) { 5532 Diag(Copyprivate->getLocStart(), 5533 diag::err_omp_single_copyprivate_with_nowait); 5534 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here); 5535 return StmtError(); 5536 } 5537 } 5538 5539 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 5540 } 5541 5542 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 5543 SourceLocation StartLoc, 5544 SourceLocation EndLoc) { 5545 if (!AStmt) 5546 return StmtError(); 5547 5548 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5549 5550 setFunctionHasBranchProtectedScope(); 5551 5552 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 5553 } 5554 5555 StmtResult Sema::ActOnOpenMPCriticalDirective( 5556 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 5557 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 5558 if (!AStmt) 5559 return StmtError(); 5560 5561 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5562 5563 bool ErrorFound = false; 5564 llvm::APSInt Hint; 5565 SourceLocation HintLoc; 5566 bool DependentHint = false; 5567 for (const OMPClause *C : Clauses) { 5568 if (C->getClauseKind() == OMPC_hint) { 5569 if (!DirName.getName()) { 5570 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name); 5571 ErrorFound = true; 5572 } 5573 Expr *E = cast<OMPHintClause>(C)->getHint(); 5574 if (E->isTypeDependent() || E->isValueDependent() || 5575 E->isInstantiationDependent()) { 5576 DependentHint = true; 5577 } else { 5578 Hint = E->EvaluateKnownConstInt(Context); 5579 HintLoc = C->getLocStart(); 5580 } 5581 } 5582 } 5583 if (ErrorFound) 5584 return StmtError(); 5585 const auto Pair = DSAStack->getCriticalWithHint(DirName); 5586 if (Pair.first && DirName.getName() && !DependentHint) { 5587 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 5588 Diag(StartLoc, diag::err_omp_critical_with_hint); 5589 if (HintLoc.isValid()) 5590 Diag(HintLoc, diag::note_omp_critical_hint_here) 5591 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); 5592 else 5593 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 5594 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 5595 Diag(C->getLocStart(), diag::note_omp_critical_hint_here) 5596 << 1 5597 << C->getHint()->EvaluateKnownConstInt(Context).toString( 5598 /*Radix=*/10, /*Signed=*/false); 5599 } else { 5600 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1; 5601 } 5602 } 5603 } 5604 5605 setFunctionHasBranchProtectedScope(); 5606 5607 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 5608 Clauses, AStmt); 5609 if (!Pair.first && DirName.getName() && !DependentHint) 5610 DSAStack->addCriticalWithHint(Dir, Hint); 5611 return Dir; 5612 } 5613 5614 StmtResult Sema::ActOnOpenMPParallelForDirective( 5615 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5616 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5617 if (!AStmt) 5618 return StmtError(); 5619 5620 auto *CS = cast<CapturedStmt>(AStmt); 5621 // 1.2.2 OpenMP Language Terminology 5622 // Structured block - An executable statement with a single entry at the 5623 // top and a single exit at the bottom. 5624 // The point of exit cannot be a branch out of the structured block. 5625 // longjmp() and throw() must not violate the entry/exit criteria. 5626 CS->getCapturedDecl()->setNothrow(); 5627 5628 OMPLoopDirective::HelperExprs B; 5629 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5630 // define the nested loops number. 5631 unsigned NestedLoopCount = 5632 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 5633 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 5634 VarsWithImplicitDSA, B); 5635 if (NestedLoopCount == 0) 5636 return StmtError(); 5637 5638 assert((CurContext->isDependentContext() || B.builtAll()) && 5639 "omp parallel for loop exprs were not built"); 5640 5641 if (!CurContext->isDependentContext()) { 5642 // Finalize the clauses that need pre-built expressions for CodeGen. 5643 for (OMPClause *C : Clauses) { 5644 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5645 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5646 B.NumIterations, *this, CurScope, 5647 DSAStack)) 5648 return StmtError(); 5649 } 5650 } 5651 5652 setFunctionHasBranchProtectedScope(); 5653 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc, 5654 NestedLoopCount, Clauses, AStmt, B, 5655 DSAStack->isCancelRegion()); 5656 } 5657 5658 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 5659 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5660 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5661 if (!AStmt) 5662 return StmtError(); 5663 5664 auto *CS = cast<CapturedStmt>(AStmt); 5665 // 1.2.2 OpenMP Language Terminology 5666 // Structured block - An executable statement with a single entry at the 5667 // top and a single exit at the bottom. 5668 // The point of exit cannot be a branch out of the structured block. 5669 // longjmp() and throw() must not violate the entry/exit criteria. 5670 CS->getCapturedDecl()->setNothrow(); 5671 5672 OMPLoopDirective::HelperExprs B; 5673 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5674 // define the nested loops number. 5675 unsigned NestedLoopCount = 5676 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 5677 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 5678 VarsWithImplicitDSA, B); 5679 if (NestedLoopCount == 0) 5680 return StmtError(); 5681 5682 if (!CurContext->isDependentContext()) { 5683 // Finalize the clauses that need pre-built expressions for CodeGen. 5684 for (OMPClause *C : Clauses) { 5685 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5686 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5687 B.NumIterations, *this, CurScope, 5688 DSAStack)) 5689 return StmtError(); 5690 } 5691 } 5692 5693 if (checkSimdlenSafelenSpecified(*this, Clauses)) 5694 return StmtError(); 5695 5696 setFunctionHasBranchProtectedScope(); 5697 return OMPParallelForSimdDirective::Create( 5698 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 5699 } 5700 5701 StmtResult 5702 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 5703 Stmt *AStmt, SourceLocation StartLoc, 5704 SourceLocation EndLoc) { 5705 if (!AStmt) 5706 return StmtError(); 5707 5708 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5709 auto BaseStmt = AStmt; 5710 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 5711 BaseStmt = CS->getCapturedStmt(); 5712 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 5713 auto S = C->children(); 5714 if (S.begin() == S.end()) 5715 return StmtError(); 5716 // All associated statements must be '#pragma omp section' except for 5717 // the first one. 5718 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 5719 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 5720 if (SectionStmt) 5721 Diag(SectionStmt->getLocStart(), 5722 diag::err_omp_parallel_sections_substmt_not_section); 5723 return StmtError(); 5724 } 5725 cast<OMPSectionDirective>(SectionStmt) 5726 ->setHasCancel(DSAStack->isCancelRegion()); 5727 } 5728 } else { 5729 Diag(AStmt->getLocStart(), 5730 diag::err_omp_parallel_sections_not_compound_stmt); 5731 return StmtError(); 5732 } 5733 5734 setFunctionHasBranchProtectedScope(); 5735 5736 return OMPParallelSectionsDirective::Create( 5737 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion()); 5738 } 5739 5740 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 5741 Stmt *AStmt, SourceLocation StartLoc, 5742 SourceLocation EndLoc) { 5743 if (!AStmt) 5744 return StmtError(); 5745 5746 auto *CS = cast<CapturedStmt>(AStmt); 5747 // 1.2.2 OpenMP Language Terminology 5748 // Structured block - An executable statement with a single entry at the 5749 // top and a single exit at the bottom. 5750 // The point of exit cannot be a branch out of the structured block. 5751 // longjmp() and throw() must not violate the entry/exit criteria. 5752 CS->getCapturedDecl()->setNothrow(); 5753 5754 setFunctionHasBranchProtectedScope(); 5755 5756 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 5757 DSAStack->isCancelRegion()); 5758 } 5759 5760 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 5761 SourceLocation EndLoc) { 5762 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 5763 } 5764 5765 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 5766 SourceLocation EndLoc) { 5767 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 5768 } 5769 5770 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 5771 SourceLocation EndLoc) { 5772 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 5773 } 5774 5775 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 5776 Stmt *AStmt, 5777 SourceLocation StartLoc, 5778 SourceLocation EndLoc) { 5779 if (!AStmt) 5780 return StmtError(); 5781 5782 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5783 5784 setFunctionHasBranchProtectedScope(); 5785 5786 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 5787 AStmt, 5788 DSAStack->getTaskgroupReductionRef()); 5789 } 5790 5791 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 5792 SourceLocation StartLoc, 5793 SourceLocation EndLoc) { 5794 assert(Clauses.size() <= 1 && "Extra clauses in flush directive"); 5795 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 5796 } 5797 5798 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 5799 Stmt *AStmt, 5800 SourceLocation StartLoc, 5801 SourceLocation EndLoc) { 5802 const OMPClause *DependFound = nullptr; 5803 const OMPClause *DependSourceClause = nullptr; 5804 const OMPClause *DependSinkClause = nullptr; 5805 bool ErrorFound = false; 5806 const OMPThreadsClause *TC = nullptr; 5807 const OMPSIMDClause *SC = nullptr; 5808 for (const OMPClause *C : Clauses) { 5809 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 5810 DependFound = C; 5811 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 5812 if (DependSourceClause) { 5813 Diag(C->getLocStart(), diag::err_omp_more_one_clause) 5814 << getOpenMPDirectiveName(OMPD_ordered) 5815 << getOpenMPClauseName(OMPC_depend) << 2; 5816 ErrorFound = true; 5817 } else { 5818 DependSourceClause = C; 5819 } 5820 if (DependSinkClause) { 5821 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed) 5822 << 0; 5823 ErrorFound = true; 5824 } 5825 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 5826 if (DependSourceClause) { 5827 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed) 5828 << 1; 5829 ErrorFound = true; 5830 } 5831 DependSinkClause = C; 5832 } 5833 } else if (C->getClauseKind() == OMPC_threads) { 5834 TC = cast<OMPThreadsClause>(C); 5835 } else if (C->getClauseKind() == OMPC_simd) { 5836 SC = cast<OMPSIMDClause>(C); 5837 } 5838 } 5839 if (!ErrorFound && !SC && 5840 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 5841 // OpenMP [2.8.1,simd Construct, Restrictions] 5842 // An ordered construct with the simd clause is the only OpenMP construct 5843 // that can appear in the simd region. 5844 Diag(StartLoc, diag::err_omp_prohibited_region_simd); 5845 ErrorFound = true; 5846 } else if (DependFound && (TC || SC)) { 5847 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd) 5848 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 5849 ErrorFound = true; 5850 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) { 5851 Diag(DependFound->getLocStart(), 5852 diag::err_omp_ordered_directive_without_param); 5853 ErrorFound = true; 5854 } else if (TC || Clauses.empty()) { 5855 if (const Expr *Param = DSAStack->getParentOrderedRegionParam()) { 5856 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc; 5857 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 5858 << (TC != nullptr); 5859 Diag(Param->getLocStart(), diag::note_omp_ordered_param); 5860 ErrorFound = true; 5861 } 5862 } 5863 if ((!AStmt && !DependFound) || ErrorFound) 5864 return StmtError(); 5865 5866 if (AStmt) { 5867 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5868 5869 setFunctionHasBranchProtectedScope(); 5870 } 5871 5872 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 5873 } 5874 5875 namespace { 5876 /// Helper class for checking expression in 'omp atomic [update]' 5877 /// construct. 5878 class OpenMPAtomicUpdateChecker { 5879 /// Error results for atomic update expressions. 5880 enum ExprAnalysisErrorCode { 5881 /// A statement is not an expression statement. 5882 NotAnExpression, 5883 /// Expression is not builtin binary or unary operation. 5884 NotABinaryOrUnaryExpression, 5885 /// Unary operation is not post-/pre- increment/decrement operation. 5886 NotAnUnaryIncDecExpression, 5887 /// An expression is not of scalar type. 5888 NotAScalarType, 5889 /// A binary operation is not an assignment operation. 5890 NotAnAssignmentOp, 5891 /// RHS part of the binary operation is not a binary expression. 5892 NotABinaryExpression, 5893 /// RHS part is not additive/multiplicative/shift/biwise binary 5894 /// expression. 5895 NotABinaryOperator, 5896 /// RHS binary operation does not have reference to the updated LHS 5897 /// part. 5898 NotAnUpdateExpression, 5899 /// No errors is found. 5900 NoError 5901 }; 5902 /// Reference to Sema. 5903 Sema &SemaRef; 5904 /// A location for note diagnostics (when error is found). 5905 SourceLocation NoteLoc; 5906 /// 'x' lvalue part of the source atomic expression. 5907 Expr *X; 5908 /// 'expr' rvalue part of the source atomic expression. 5909 Expr *E; 5910 /// Helper expression of the form 5911 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 5912 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 5913 Expr *UpdateExpr; 5914 /// Is 'x' a LHS in a RHS part of full update expression. It is 5915 /// important for non-associative operations. 5916 bool IsXLHSInRHSPart; 5917 BinaryOperatorKind Op; 5918 SourceLocation OpLoc; 5919 /// true if the source expression is a postfix unary operation, false 5920 /// if it is a prefix unary operation. 5921 bool IsPostfixUpdate; 5922 5923 public: 5924 OpenMPAtomicUpdateChecker(Sema &SemaRef) 5925 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 5926 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 5927 /// Check specified statement that it is suitable for 'atomic update' 5928 /// constructs and extract 'x', 'expr' and Operation from the original 5929 /// expression. If DiagId and NoteId == 0, then only check is performed 5930 /// without error notification. 5931 /// \param DiagId Diagnostic which should be emitted if error is found. 5932 /// \param NoteId Diagnostic note for the main error message. 5933 /// \return true if statement is not an update expression, false otherwise. 5934 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 5935 /// Return the 'x' lvalue part of the source atomic expression. 5936 Expr *getX() const { return X; } 5937 /// Return the 'expr' rvalue part of the source atomic expression. 5938 Expr *getExpr() const { return E; } 5939 /// Return the update expression used in calculation of the updated 5940 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 5941 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 5942 Expr *getUpdateExpr() const { return UpdateExpr; } 5943 /// Return true if 'x' is LHS in RHS part of full update expression, 5944 /// false otherwise. 5945 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 5946 5947 /// true if the source expression is a postfix unary operation, false 5948 /// if it is a prefix unary operation. 5949 bool isPostfixUpdate() const { return IsPostfixUpdate; } 5950 5951 private: 5952 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 5953 unsigned NoteId = 0); 5954 }; 5955 } // namespace 5956 5957 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 5958 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 5959 ExprAnalysisErrorCode ErrorFound = NoError; 5960 SourceLocation ErrorLoc, NoteLoc; 5961 SourceRange ErrorRange, NoteRange; 5962 // Allowed constructs are: 5963 // x = x binop expr; 5964 // x = expr binop x; 5965 if (AtomicBinOp->getOpcode() == BO_Assign) { 5966 X = AtomicBinOp->getLHS(); 5967 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 5968 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 5969 if (AtomicInnerBinOp->isMultiplicativeOp() || 5970 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 5971 AtomicInnerBinOp->isBitwiseOp()) { 5972 Op = AtomicInnerBinOp->getOpcode(); 5973 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 5974 Expr *LHS = AtomicInnerBinOp->getLHS(); 5975 Expr *RHS = AtomicInnerBinOp->getRHS(); 5976 llvm::FoldingSetNodeID XId, LHSId, RHSId; 5977 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 5978 /*Canonical=*/true); 5979 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 5980 /*Canonical=*/true); 5981 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 5982 /*Canonical=*/true); 5983 if (XId == LHSId) { 5984 E = RHS; 5985 IsXLHSInRHSPart = true; 5986 } else if (XId == RHSId) { 5987 E = LHS; 5988 IsXLHSInRHSPart = false; 5989 } else { 5990 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 5991 ErrorRange = AtomicInnerBinOp->getSourceRange(); 5992 NoteLoc = X->getExprLoc(); 5993 NoteRange = X->getSourceRange(); 5994 ErrorFound = NotAnUpdateExpression; 5995 } 5996 } else { 5997 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 5998 ErrorRange = AtomicInnerBinOp->getSourceRange(); 5999 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 6000 NoteRange = SourceRange(NoteLoc, NoteLoc); 6001 ErrorFound = NotABinaryOperator; 6002 } 6003 } else { 6004 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 6005 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 6006 ErrorFound = NotABinaryExpression; 6007 } 6008 } else { 6009 ErrorLoc = AtomicBinOp->getExprLoc(); 6010 ErrorRange = AtomicBinOp->getSourceRange(); 6011 NoteLoc = AtomicBinOp->getOperatorLoc(); 6012 NoteRange = SourceRange(NoteLoc, NoteLoc); 6013 ErrorFound = NotAnAssignmentOp; 6014 } 6015 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 6016 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 6017 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 6018 return true; 6019 } 6020 if (SemaRef.CurContext->isDependentContext()) 6021 E = X = UpdateExpr = nullptr; 6022 return ErrorFound != NoError; 6023 } 6024 6025 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 6026 unsigned NoteId) { 6027 ExprAnalysisErrorCode ErrorFound = NoError; 6028 SourceLocation ErrorLoc, NoteLoc; 6029 SourceRange ErrorRange, NoteRange; 6030 // Allowed constructs are: 6031 // x++; 6032 // x--; 6033 // ++x; 6034 // --x; 6035 // x binop= expr; 6036 // x = x binop expr; 6037 // x = expr binop x; 6038 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 6039 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 6040 if (AtomicBody->getType()->isScalarType() || 6041 AtomicBody->isInstantiationDependent()) { 6042 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 6043 AtomicBody->IgnoreParenImpCasts())) { 6044 // Check for Compound Assignment Operation 6045 Op = BinaryOperator::getOpForCompoundAssignment( 6046 AtomicCompAssignOp->getOpcode()); 6047 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 6048 E = AtomicCompAssignOp->getRHS(); 6049 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 6050 IsXLHSInRHSPart = true; 6051 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 6052 AtomicBody->IgnoreParenImpCasts())) { 6053 // Check for Binary Operation 6054 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 6055 return true; 6056 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 6057 AtomicBody->IgnoreParenImpCasts())) { 6058 // Check for Unary Operation 6059 if (AtomicUnaryOp->isIncrementDecrementOp()) { 6060 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 6061 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 6062 OpLoc = AtomicUnaryOp->getOperatorLoc(); 6063 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 6064 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 6065 IsXLHSInRHSPart = true; 6066 } else { 6067 ErrorFound = NotAnUnaryIncDecExpression; 6068 ErrorLoc = AtomicUnaryOp->getExprLoc(); 6069 ErrorRange = AtomicUnaryOp->getSourceRange(); 6070 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 6071 NoteRange = SourceRange(NoteLoc, NoteLoc); 6072 } 6073 } else if (!AtomicBody->isInstantiationDependent()) { 6074 ErrorFound = NotABinaryOrUnaryExpression; 6075 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 6076 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 6077 } 6078 } else { 6079 ErrorFound = NotAScalarType; 6080 NoteLoc = ErrorLoc = AtomicBody->getLocStart(); 6081 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6082 } 6083 } else { 6084 ErrorFound = NotAnExpression; 6085 NoteLoc = ErrorLoc = S->getLocStart(); 6086 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6087 } 6088 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 6089 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 6090 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 6091 return true; 6092 } 6093 if (SemaRef.CurContext->isDependentContext()) 6094 E = X = UpdateExpr = nullptr; 6095 if (ErrorFound == NoError && E && X) { 6096 // Build an update expression of form 'OpaqueValueExpr(x) binop 6097 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 6098 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 6099 auto *OVEX = new (SemaRef.getASTContext()) 6100 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); 6101 auto *OVEExpr = new (SemaRef.getASTContext()) 6102 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); 6103 ExprResult Update = 6104 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 6105 IsXLHSInRHSPart ? OVEExpr : OVEX); 6106 if (Update.isInvalid()) 6107 return true; 6108 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 6109 Sema::AA_Casting); 6110 if (Update.isInvalid()) 6111 return true; 6112 UpdateExpr = Update.get(); 6113 } 6114 return ErrorFound != NoError; 6115 } 6116 6117 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 6118 Stmt *AStmt, 6119 SourceLocation StartLoc, 6120 SourceLocation EndLoc) { 6121 if (!AStmt) 6122 return StmtError(); 6123 6124 auto *CS = cast<CapturedStmt>(AStmt); 6125 // 1.2.2 OpenMP Language Terminology 6126 // Structured block - An executable statement with a single entry at the 6127 // top and a single exit at the bottom. 6128 // The point of exit cannot be a branch out of the structured block. 6129 // longjmp() and throw() must not violate the entry/exit criteria. 6130 OpenMPClauseKind AtomicKind = OMPC_unknown; 6131 SourceLocation AtomicKindLoc; 6132 for (const OMPClause *C : Clauses) { 6133 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 6134 C->getClauseKind() == OMPC_update || 6135 C->getClauseKind() == OMPC_capture) { 6136 if (AtomicKind != OMPC_unknown) { 6137 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses) 6138 << SourceRange(C->getLocStart(), C->getLocEnd()); 6139 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause) 6140 << getOpenMPClauseName(AtomicKind); 6141 } else { 6142 AtomicKind = C->getClauseKind(); 6143 AtomicKindLoc = C->getLocStart(); 6144 } 6145 } 6146 } 6147 6148 Stmt *Body = CS->getCapturedStmt(); 6149 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 6150 Body = EWC->getSubExpr(); 6151 6152 Expr *X = nullptr; 6153 Expr *V = nullptr; 6154 Expr *E = nullptr; 6155 Expr *UE = nullptr; 6156 bool IsXLHSInRHSPart = false; 6157 bool IsPostfixUpdate = false; 6158 // OpenMP [2.12.6, atomic Construct] 6159 // In the next expressions: 6160 // * x and v (as applicable) are both l-value expressions with scalar type. 6161 // * During the execution of an atomic region, multiple syntactic 6162 // occurrences of x must designate the same storage location. 6163 // * Neither of v and expr (as applicable) may access the storage location 6164 // designated by x. 6165 // * Neither of x and expr (as applicable) may access the storage location 6166 // designated by v. 6167 // * expr is an expression with scalar type. 6168 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 6169 // * binop, binop=, ++, and -- are not overloaded operators. 6170 // * The expression x binop expr must be numerically equivalent to x binop 6171 // (expr). This requirement is satisfied if the operators in expr have 6172 // precedence greater than binop, or by using parentheses around expr or 6173 // subexpressions of expr. 6174 // * The expression expr binop x must be numerically equivalent to (expr) 6175 // binop x. This requirement is satisfied if the operators in expr have 6176 // precedence equal to or greater than binop, or by using parentheses around 6177 // expr or subexpressions of expr. 6178 // * For forms that allow multiple occurrences of x, the number of times 6179 // that x is evaluated is unspecified. 6180 if (AtomicKind == OMPC_read) { 6181 enum { 6182 NotAnExpression, 6183 NotAnAssignmentOp, 6184 NotAScalarType, 6185 NotAnLValue, 6186 NoError 6187 } ErrorFound = NoError; 6188 SourceLocation ErrorLoc, NoteLoc; 6189 SourceRange ErrorRange, NoteRange; 6190 // If clause is read: 6191 // v = x; 6192 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 6193 const auto *AtomicBinOp = 6194 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 6195 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 6196 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 6197 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 6198 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 6199 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 6200 if (!X->isLValue() || !V->isLValue()) { 6201 const Expr *NotLValueExpr = X->isLValue() ? V : X; 6202 ErrorFound = NotAnLValue; 6203 ErrorLoc = AtomicBinOp->getExprLoc(); 6204 ErrorRange = AtomicBinOp->getSourceRange(); 6205 NoteLoc = NotLValueExpr->getExprLoc(); 6206 NoteRange = NotLValueExpr->getSourceRange(); 6207 } 6208 } else if (!X->isInstantiationDependent() || 6209 !V->isInstantiationDependent()) { 6210 const Expr *NotScalarExpr = 6211 (X->isInstantiationDependent() || X->getType()->isScalarType()) 6212 ? V 6213 : X; 6214 ErrorFound = NotAScalarType; 6215 ErrorLoc = AtomicBinOp->getExprLoc(); 6216 ErrorRange = AtomicBinOp->getSourceRange(); 6217 NoteLoc = NotScalarExpr->getExprLoc(); 6218 NoteRange = NotScalarExpr->getSourceRange(); 6219 } 6220 } else if (!AtomicBody->isInstantiationDependent()) { 6221 ErrorFound = NotAnAssignmentOp; 6222 ErrorLoc = AtomicBody->getExprLoc(); 6223 ErrorRange = AtomicBody->getSourceRange(); 6224 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 6225 : AtomicBody->getExprLoc(); 6226 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 6227 : AtomicBody->getSourceRange(); 6228 } 6229 } else { 6230 ErrorFound = NotAnExpression; 6231 NoteLoc = ErrorLoc = Body->getLocStart(); 6232 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6233 } 6234 if (ErrorFound != NoError) { 6235 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 6236 << ErrorRange; 6237 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 6238 << NoteRange; 6239 return StmtError(); 6240 } 6241 if (CurContext->isDependentContext()) 6242 V = X = nullptr; 6243 } else if (AtomicKind == OMPC_write) { 6244 enum { 6245 NotAnExpression, 6246 NotAnAssignmentOp, 6247 NotAScalarType, 6248 NotAnLValue, 6249 NoError 6250 } ErrorFound = NoError; 6251 SourceLocation ErrorLoc, NoteLoc; 6252 SourceRange ErrorRange, NoteRange; 6253 // If clause is write: 6254 // x = expr; 6255 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 6256 const auto *AtomicBinOp = 6257 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 6258 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 6259 X = AtomicBinOp->getLHS(); 6260 E = AtomicBinOp->getRHS(); 6261 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 6262 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 6263 if (!X->isLValue()) { 6264 ErrorFound = NotAnLValue; 6265 ErrorLoc = AtomicBinOp->getExprLoc(); 6266 ErrorRange = AtomicBinOp->getSourceRange(); 6267 NoteLoc = X->getExprLoc(); 6268 NoteRange = X->getSourceRange(); 6269 } 6270 } else if (!X->isInstantiationDependent() || 6271 !E->isInstantiationDependent()) { 6272 const Expr *NotScalarExpr = 6273 (X->isInstantiationDependent() || X->getType()->isScalarType()) 6274 ? E 6275 : X; 6276 ErrorFound = NotAScalarType; 6277 ErrorLoc = AtomicBinOp->getExprLoc(); 6278 ErrorRange = AtomicBinOp->getSourceRange(); 6279 NoteLoc = NotScalarExpr->getExprLoc(); 6280 NoteRange = NotScalarExpr->getSourceRange(); 6281 } 6282 } else if (!AtomicBody->isInstantiationDependent()) { 6283 ErrorFound = NotAnAssignmentOp; 6284 ErrorLoc = AtomicBody->getExprLoc(); 6285 ErrorRange = AtomicBody->getSourceRange(); 6286 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 6287 : AtomicBody->getExprLoc(); 6288 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 6289 : AtomicBody->getSourceRange(); 6290 } 6291 } else { 6292 ErrorFound = NotAnExpression; 6293 NoteLoc = ErrorLoc = Body->getLocStart(); 6294 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6295 } 6296 if (ErrorFound != NoError) { 6297 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 6298 << ErrorRange; 6299 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 6300 << NoteRange; 6301 return StmtError(); 6302 } 6303 if (CurContext->isDependentContext()) 6304 E = X = nullptr; 6305 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 6306 // If clause is update: 6307 // x++; 6308 // x--; 6309 // ++x; 6310 // --x; 6311 // x binop= expr; 6312 // x = x binop expr; 6313 // x = expr binop x; 6314 OpenMPAtomicUpdateChecker Checker(*this); 6315 if (Checker.checkStatement( 6316 Body, (AtomicKind == OMPC_update) 6317 ? diag::err_omp_atomic_update_not_expression_statement 6318 : diag::err_omp_atomic_not_expression_statement, 6319 diag::note_omp_atomic_update)) 6320 return StmtError(); 6321 if (!CurContext->isDependentContext()) { 6322 E = Checker.getExpr(); 6323 X = Checker.getX(); 6324 UE = Checker.getUpdateExpr(); 6325 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6326 } 6327 } else if (AtomicKind == OMPC_capture) { 6328 enum { 6329 NotAnAssignmentOp, 6330 NotACompoundStatement, 6331 NotTwoSubstatements, 6332 NotASpecificExpression, 6333 NoError 6334 } ErrorFound = NoError; 6335 SourceLocation ErrorLoc, NoteLoc; 6336 SourceRange ErrorRange, NoteRange; 6337 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 6338 // If clause is a capture: 6339 // v = x++; 6340 // v = x--; 6341 // v = ++x; 6342 // v = --x; 6343 // v = x binop= expr; 6344 // v = x = x binop expr; 6345 // v = x = expr binop x; 6346 const auto *AtomicBinOp = 6347 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 6348 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 6349 V = AtomicBinOp->getLHS(); 6350 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 6351 OpenMPAtomicUpdateChecker Checker(*this); 6352 if (Checker.checkStatement( 6353 Body, diag::err_omp_atomic_capture_not_expression_statement, 6354 diag::note_omp_atomic_update)) 6355 return StmtError(); 6356 E = Checker.getExpr(); 6357 X = Checker.getX(); 6358 UE = Checker.getUpdateExpr(); 6359 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6360 IsPostfixUpdate = Checker.isPostfixUpdate(); 6361 } else if (!AtomicBody->isInstantiationDependent()) { 6362 ErrorLoc = AtomicBody->getExprLoc(); 6363 ErrorRange = AtomicBody->getSourceRange(); 6364 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 6365 : AtomicBody->getExprLoc(); 6366 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 6367 : AtomicBody->getSourceRange(); 6368 ErrorFound = NotAnAssignmentOp; 6369 } 6370 if (ErrorFound != NoError) { 6371 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 6372 << ErrorRange; 6373 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 6374 return StmtError(); 6375 } 6376 if (CurContext->isDependentContext()) 6377 UE = V = E = X = nullptr; 6378 } else { 6379 // If clause is a capture: 6380 // { v = x; x = expr; } 6381 // { v = x; x++; } 6382 // { v = x; x--; } 6383 // { v = x; ++x; } 6384 // { v = x; --x; } 6385 // { v = x; x binop= expr; } 6386 // { v = x; x = x binop expr; } 6387 // { v = x; x = expr binop x; } 6388 // { x++; v = x; } 6389 // { x--; v = x; } 6390 // { ++x; v = x; } 6391 // { --x; v = x; } 6392 // { x binop= expr; v = x; } 6393 // { x = x binop expr; v = x; } 6394 // { x = expr binop x; v = x; } 6395 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 6396 // Check that this is { expr1; expr2; } 6397 if (CS->size() == 2) { 6398 Stmt *First = CS->body_front(); 6399 Stmt *Second = CS->body_back(); 6400 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 6401 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 6402 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 6403 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 6404 // Need to find what subexpression is 'v' and what is 'x'. 6405 OpenMPAtomicUpdateChecker Checker(*this); 6406 bool IsUpdateExprFound = !Checker.checkStatement(Second); 6407 BinaryOperator *BinOp = nullptr; 6408 if (IsUpdateExprFound) { 6409 BinOp = dyn_cast<BinaryOperator>(First); 6410 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 6411 } 6412 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 6413 // { v = x; x++; } 6414 // { v = x; x--; } 6415 // { v = x; ++x; } 6416 // { v = x; --x; } 6417 // { v = x; x binop= expr; } 6418 // { v = x; x = x binop expr; } 6419 // { v = x; x = expr binop x; } 6420 // Check that the first expression has form v = x. 6421 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 6422 llvm::FoldingSetNodeID XId, PossibleXId; 6423 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 6424 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 6425 IsUpdateExprFound = XId == PossibleXId; 6426 if (IsUpdateExprFound) { 6427 V = BinOp->getLHS(); 6428 X = Checker.getX(); 6429 E = Checker.getExpr(); 6430 UE = Checker.getUpdateExpr(); 6431 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6432 IsPostfixUpdate = true; 6433 } 6434 } 6435 if (!IsUpdateExprFound) { 6436 IsUpdateExprFound = !Checker.checkStatement(First); 6437 BinOp = nullptr; 6438 if (IsUpdateExprFound) { 6439 BinOp = dyn_cast<BinaryOperator>(Second); 6440 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 6441 } 6442 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 6443 // { x++; v = x; } 6444 // { x--; v = x; } 6445 // { ++x; v = x; } 6446 // { --x; v = x; } 6447 // { x binop= expr; v = x; } 6448 // { x = x binop expr; v = x; } 6449 // { x = expr binop x; v = x; } 6450 // Check that the second expression has form v = x. 6451 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 6452 llvm::FoldingSetNodeID XId, PossibleXId; 6453 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 6454 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 6455 IsUpdateExprFound = XId == PossibleXId; 6456 if (IsUpdateExprFound) { 6457 V = BinOp->getLHS(); 6458 X = Checker.getX(); 6459 E = Checker.getExpr(); 6460 UE = Checker.getUpdateExpr(); 6461 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6462 IsPostfixUpdate = false; 6463 } 6464 } 6465 } 6466 if (!IsUpdateExprFound) { 6467 // { v = x; x = expr; } 6468 auto *FirstExpr = dyn_cast<Expr>(First); 6469 auto *SecondExpr = dyn_cast<Expr>(Second); 6470 if (!FirstExpr || !SecondExpr || 6471 !(FirstExpr->isInstantiationDependent() || 6472 SecondExpr->isInstantiationDependent())) { 6473 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 6474 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 6475 ErrorFound = NotAnAssignmentOp; 6476 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 6477 : First->getLocStart(); 6478 NoteRange = ErrorRange = FirstBinOp 6479 ? FirstBinOp->getSourceRange() 6480 : SourceRange(ErrorLoc, ErrorLoc); 6481 } else { 6482 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 6483 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 6484 ErrorFound = NotAnAssignmentOp; 6485 NoteLoc = ErrorLoc = SecondBinOp 6486 ? SecondBinOp->getOperatorLoc() 6487 : Second->getLocStart(); 6488 NoteRange = ErrorRange = 6489 SecondBinOp ? SecondBinOp->getSourceRange() 6490 : SourceRange(ErrorLoc, ErrorLoc); 6491 } else { 6492 Expr *PossibleXRHSInFirst = 6493 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 6494 Expr *PossibleXLHSInSecond = 6495 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 6496 llvm::FoldingSetNodeID X1Id, X2Id; 6497 PossibleXRHSInFirst->Profile(X1Id, Context, 6498 /*Canonical=*/true); 6499 PossibleXLHSInSecond->Profile(X2Id, Context, 6500 /*Canonical=*/true); 6501 IsUpdateExprFound = X1Id == X2Id; 6502 if (IsUpdateExprFound) { 6503 V = FirstBinOp->getLHS(); 6504 X = SecondBinOp->getLHS(); 6505 E = SecondBinOp->getRHS(); 6506 UE = nullptr; 6507 IsXLHSInRHSPart = false; 6508 IsPostfixUpdate = true; 6509 } else { 6510 ErrorFound = NotASpecificExpression; 6511 ErrorLoc = FirstBinOp->getExprLoc(); 6512 ErrorRange = FirstBinOp->getSourceRange(); 6513 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 6514 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 6515 } 6516 } 6517 } 6518 } 6519 } 6520 } else { 6521 NoteLoc = ErrorLoc = Body->getLocStart(); 6522 NoteRange = ErrorRange = 6523 SourceRange(Body->getLocStart(), Body->getLocStart()); 6524 ErrorFound = NotTwoSubstatements; 6525 } 6526 } else { 6527 NoteLoc = ErrorLoc = Body->getLocStart(); 6528 NoteRange = ErrorRange = 6529 SourceRange(Body->getLocStart(), Body->getLocStart()); 6530 ErrorFound = NotACompoundStatement; 6531 } 6532 if (ErrorFound != NoError) { 6533 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 6534 << ErrorRange; 6535 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 6536 return StmtError(); 6537 } 6538 if (CurContext->isDependentContext()) 6539 UE = V = E = X = nullptr; 6540 } 6541 } 6542 6543 setFunctionHasBranchProtectedScope(); 6544 6545 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 6546 X, V, E, UE, IsXLHSInRHSPart, 6547 IsPostfixUpdate); 6548 } 6549 6550 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 6551 Stmt *AStmt, 6552 SourceLocation StartLoc, 6553 SourceLocation EndLoc) { 6554 if (!AStmt) 6555 return StmtError(); 6556 6557 auto *CS = cast<CapturedStmt>(AStmt); 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 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 6565 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6566 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6567 // 1.2.2 OpenMP Language Terminology 6568 // Structured block - An executable statement with a single entry at the 6569 // top and a single exit at the bottom. 6570 // The point of exit cannot be a branch out of the structured block. 6571 // longjmp() and throw() must not violate the entry/exit criteria. 6572 CS->getCapturedDecl()->setNothrow(); 6573 } 6574 6575 // OpenMP [2.16, Nesting of Regions] 6576 // If specified, a teams construct must be contained within a target 6577 // construct. That target construct must contain no statements or directives 6578 // outside of the teams construct. 6579 if (DSAStack->hasInnerTeamsRegion()) { 6580 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 6581 bool OMPTeamsFound = true; 6582 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 6583 auto I = CS->body_begin(); 6584 while (I != CS->body_end()) { 6585 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 6586 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) { 6587 OMPTeamsFound = false; 6588 break; 6589 } 6590 ++I; 6591 } 6592 assert(I != CS->body_end() && "Not found statement"); 6593 S = *I; 6594 } else { 6595 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 6596 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 6597 } 6598 if (!OMPTeamsFound) { 6599 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 6600 Diag(DSAStack->getInnerTeamsRegionLoc(), 6601 diag::note_omp_nested_teams_construct_here); 6602 Diag(S->getLocStart(), diag::note_omp_nested_statement_here) 6603 << isa<OMPExecutableDirective>(S); 6604 return StmtError(); 6605 } 6606 } 6607 6608 setFunctionHasBranchProtectedScope(); 6609 6610 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 6611 } 6612 6613 StmtResult 6614 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 6615 Stmt *AStmt, SourceLocation StartLoc, 6616 SourceLocation EndLoc) { 6617 if (!AStmt) 6618 return StmtError(); 6619 6620 auto *CS = cast<CapturedStmt>(AStmt); 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 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 6628 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6629 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6630 // 1.2.2 OpenMP Language Terminology 6631 // Structured block - An executable statement with a single entry at the 6632 // top and a single exit at the bottom. 6633 // The point of exit cannot be a branch out of the structured block. 6634 // longjmp() and throw() must not violate the entry/exit criteria. 6635 CS->getCapturedDecl()->setNothrow(); 6636 } 6637 6638 setFunctionHasBranchProtectedScope(); 6639 6640 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, 6641 AStmt); 6642 } 6643 6644 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 6645 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6646 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6647 if (!AStmt) 6648 return StmtError(); 6649 6650 auto *CS = cast<CapturedStmt>(AStmt); 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 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 6658 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6659 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6660 // 1.2.2 OpenMP Language Terminology 6661 // Structured block - An executable statement with a single entry at the 6662 // top and a single exit at the bottom. 6663 // The point of exit cannot be a branch out of the structured block. 6664 // longjmp() and throw() must not violate the entry/exit criteria. 6665 CS->getCapturedDecl()->setNothrow(); 6666 } 6667 6668 OMPLoopDirective::HelperExprs B; 6669 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6670 // define the nested loops number. 6671 unsigned NestedLoopCount = 6672 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 6673 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 6674 VarsWithImplicitDSA, B); 6675 if (NestedLoopCount == 0) 6676 return StmtError(); 6677 6678 assert((CurContext->isDependentContext() || B.builtAll()) && 6679 "omp target parallel for loop exprs were not built"); 6680 6681 if (!CurContext->isDependentContext()) { 6682 // Finalize the clauses that need pre-built expressions for CodeGen. 6683 for (OMPClause *C : Clauses) { 6684 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6685 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6686 B.NumIterations, *this, CurScope, 6687 DSAStack)) 6688 return StmtError(); 6689 } 6690 } 6691 6692 setFunctionHasBranchProtectedScope(); 6693 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc, 6694 NestedLoopCount, Clauses, AStmt, 6695 B, DSAStack->isCancelRegion()); 6696 } 6697 6698 /// Check for existence of a map clause in the list of clauses. 6699 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 6700 const OpenMPClauseKind K) { 6701 return llvm::any_of( 6702 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 6703 } 6704 6705 template <typename... Params> 6706 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 6707 const Params... ClauseTypes) { 6708 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 6709 } 6710 6711 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 6712 Stmt *AStmt, 6713 SourceLocation StartLoc, 6714 SourceLocation EndLoc) { 6715 if (!AStmt) 6716 return StmtError(); 6717 6718 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6719 6720 // OpenMP [2.10.1, Restrictions, p. 97] 6721 // At least one map clause must appear on the directive. 6722 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) { 6723 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 6724 << "'map' or 'use_device_ptr'" 6725 << getOpenMPDirectiveName(OMPD_target_data); 6726 return StmtError(); 6727 } 6728 6729 setFunctionHasBranchProtectedScope(); 6730 6731 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 6732 AStmt); 6733 } 6734 6735 StmtResult 6736 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 6737 SourceLocation StartLoc, 6738 SourceLocation EndLoc, Stmt *AStmt) { 6739 if (!AStmt) 6740 return StmtError(); 6741 6742 auto *CS = cast<CapturedStmt>(AStmt); 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 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 6750 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6751 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6752 // 1.2.2 OpenMP Language Terminology 6753 // Structured block - An executable statement with a single entry at the 6754 // top and a single exit at the bottom. 6755 // The point of exit cannot be a branch out of the structured block. 6756 // longjmp() and throw() must not violate the entry/exit criteria. 6757 CS->getCapturedDecl()->setNothrow(); 6758 } 6759 6760 // OpenMP [2.10.2, Restrictions, p. 99] 6761 // At least one map clause must appear on the directive. 6762 if (!hasClauses(Clauses, OMPC_map)) { 6763 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 6764 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 6765 return StmtError(); 6766 } 6767 6768 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 6769 AStmt); 6770 } 6771 6772 StmtResult 6773 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 6774 SourceLocation StartLoc, 6775 SourceLocation EndLoc, Stmt *AStmt) { 6776 if (!AStmt) 6777 return StmtError(); 6778 6779 auto *CS = cast<CapturedStmt>(AStmt); 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 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 6787 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6788 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6789 // 1.2.2 OpenMP Language Terminology 6790 // Structured block - An executable statement with a single entry at the 6791 // top and a single exit at the bottom. 6792 // The point of exit cannot be a branch out of the structured block. 6793 // longjmp() and throw() must not violate the entry/exit criteria. 6794 CS->getCapturedDecl()->setNothrow(); 6795 } 6796 6797 // OpenMP [2.10.3, Restrictions, p. 102] 6798 // At least one map clause must appear on the directive. 6799 if (!hasClauses(Clauses, OMPC_map)) { 6800 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 6801 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 6802 return StmtError(); 6803 } 6804 6805 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 6806 AStmt); 6807 } 6808 6809 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 6810 SourceLocation StartLoc, 6811 SourceLocation EndLoc, 6812 Stmt *AStmt) { 6813 if (!AStmt) 6814 return StmtError(); 6815 6816 auto *CS = cast<CapturedStmt>(AStmt); 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 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 6824 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6825 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6826 // 1.2.2 OpenMP Language Terminology 6827 // Structured block - An executable statement with a single entry at the 6828 // top and a single exit at the bottom. 6829 // The point of exit cannot be a branch out of the structured block. 6830 // longjmp() and throw() must not violate the entry/exit criteria. 6831 CS->getCapturedDecl()->setNothrow(); 6832 } 6833 6834 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 6835 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 6836 return StmtError(); 6837 } 6838 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 6839 AStmt); 6840 } 6841 6842 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 6843 Stmt *AStmt, SourceLocation StartLoc, 6844 SourceLocation EndLoc) { 6845 if (!AStmt) 6846 return StmtError(); 6847 6848 auto *CS = cast<CapturedStmt>(AStmt); 6849 // 1.2.2 OpenMP Language Terminology 6850 // Structured block - An executable statement with a single entry at the 6851 // top and a single exit at the bottom. 6852 // The point of exit cannot be a branch out of the structured block. 6853 // longjmp() and throw() must not violate the entry/exit criteria. 6854 CS->getCapturedDecl()->setNothrow(); 6855 6856 setFunctionHasBranchProtectedScope(); 6857 6858 DSAStack->setParentTeamsRegionLoc(StartLoc); 6859 6860 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 6861 } 6862 6863 StmtResult 6864 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 6865 SourceLocation EndLoc, 6866 OpenMPDirectiveKind CancelRegion) { 6867 if (DSAStack->isParentNowaitRegion()) { 6868 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 6869 return StmtError(); 6870 } 6871 if (DSAStack->isParentOrderedRegion()) { 6872 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 6873 return StmtError(); 6874 } 6875 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 6876 CancelRegion); 6877 } 6878 6879 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 6880 SourceLocation StartLoc, 6881 SourceLocation EndLoc, 6882 OpenMPDirectiveKind CancelRegion) { 6883 if (DSAStack->isParentNowaitRegion()) { 6884 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 6885 return StmtError(); 6886 } 6887 if (DSAStack->isParentOrderedRegion()) { 6888 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 6889 return StmtError(); 6890 } 6891 DSAStack->setParentCancelRegion(/*Cancel=*/true); 6892 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 6893 CancelRegion); 6894 } 6895 6896 static bool checkGrainsizeNumTasksClauses(Sema &S, 6897 ArrayRef<OMPClause *> Clauses) { 6898 const OMPClause *PrevClause = nullptr; 6899 bool ErrorFound = false; 6900 for (const OMPClause *C : Clauses) { 6901 if (C->getClauseKind() == OMPC_grainsize || 6902 C->getClauseKind() == OMPC_num_tasks) { 6903 if (!PrevClause) 6904 PrevClause = C; 6905 else if (PrevClause->getClauseKind() != C->getClauseKind()) { 6906 S.Diag(C->getLocStart(), 6907 diag::err_omp_grainsize_num_tasks_mutually_exclusive) 6908 << getOpenMPClauseName(C->getClauseKind()) 6909 << getOpenMPClauseName(PrevClause->getClauseKind()); 6910 S.Diag(PrevClause->getLocStart(), 6911 diag::note_omp_previous_grainsize_num_tasks) 6912 << getOpenMPClauseName(PrevClause->getClauseKind()); 6913 ErrorFound = true; 6914 } 6915 } 6916 } 6917 return ErrorFound; 6918 } 6919 6920 static bool checkReductionClauseWithNogroup(Sema &S, 6921 ArrayRef<OMPClause *> Clauses) { 6922 const OMPClause *ReductionClause = nullptr; 6923 const OMPClause *NogroupClause = nullptr; 6924 for (const OMPClause *C : Clauses) { 6925 if (C->getClauseKind() == OMPC_reduction) { 6926 ReductionClause = C; 6927 if (NogroupClause) 6928 break; 6929 continue; 6930 } 6931 if (C->getClauseKind() == OMPC_nogroup) { 6932 NogroupClause = C; 6933 if (ReductionClause) 6934 break; 6935 continue; 6936 } 6937 } 6938 if (ReductionClause && NogroupClause) { 6939 S.Diag(ReductionClause->getLocStart(), diag::err_omp_reduction_with_nogroup) 6940 << SourceRange(NogroupClause->getLocStart(), 6941 NogroupClause->getLocEnd()); 6942 return true; 6943 } 6944 return false; 6945 } 6946 6947 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 6948 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6949 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6950 if (!AStmt) 6951 return StmtError(); 6952 6953 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6954 OMPLoopDirective::HelperExprs B; 6955 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6956 // define the nested loops number. 6957 unsigned NestedLoopCount = 6958 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 6959 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 6960 VarsWithImplicitDSA, B); 6961 if (NestedLoopCount == 0) 6962 return StmtError(); 6963 6964 assert((CurContext->isDependentContext() || B.builtAll()) && 6965 "omp for loop exprs were not built"); 6966 6967 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 6968 // The grainsize clause and num_tasks clause are mutually exclusive and may 6969 // not appear on the same taskloop directive. 6970 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 6971 return StmtError(); 6972 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 6973 // If a reduction clause is present on the taskloop directive, the nogroup 6974 // clause must not be specified. 6975 if (checkReductionClauseWithNogroup(*this, Clauses)) 6976 return StmtError(); 6977 6978 setFunctionHasBranchProtectedScope(); 6979 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 6980 NestedLoopCount, Clauses, AStmt, B); 6981 } 6982 6983 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 6984 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6985 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6986 if (!AStmt) 6987 return StmtError(); 6988 6989 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6990 OMPLoopDirective::HelperExprs B; 6991 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6992 // define the nested loops number. 6993 unsigned NestedLoopCount = 6994 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 6995 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 6996 VarsWithImplicitDSA, B); 6997 if (NestedLoopCount == 0) 6998 return StmtError(); 6999 7000 assert((CurContext->isDependentContext() || B.builtAll()) && 7001 "omp for loop exprs were not built"); 7002 7003 if (!CurContext->isDependentContext()) { 7004 // Finalize the clauses that need pre-built expressions for CodeGen. 7005 for (OMPClause *C : Clauses) { 7006 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7007 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7008 B.NumIterations, *this, CurScope, 7009 DSAStack)) 7010 return StmtError(); 7011 } 7012 } 7013 7014 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 7015 // The grainsize clause and num_tasks clause are mutually exclusive and may 7016 // not appear on the same taskloop directive. 7017 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 7018 return StmtError(); 7019 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 7020 // If a reduction clause is present on the taskloop directive, the nogroup 7021 // clause must not be specified. 7022 if (checkReductionClauseWithNogroup(*this, Clauses)) 7023 return StmtError(); 7024 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7025 return StmtError(); 7026 7027 setFunctionHasBranchProtectedScope(); 7028 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 7029 NestedLoopCount, Clauses, AStmt, B); 7030 } 7031 7032 StmtResult Sema::ActOnOpenMPDistributeDirective( 7033 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7034 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7035 if (!AStmt) 7036 return StmtError(); 7037 7038 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 7039 OMPLoopDirective::HelperExprs B; 7040 // In presence of clause 'collapse' with number of loops, it will 7041 // define the nested loops number. 7042 unsigned NestedLoopCount = 7043 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 7044 nullptr /*ordered not a clause on distribute*/, AStmt, 7045 *this, *DSAStack, VarsWithImplicitDSA, B); 7046 if (NestedLoopCount == 0) 7047 return StmtError(); 7048 7049 assert((CurContext->isDependentContext() || B.builtAll()) && 7050 "omp for loop exprs were not built"); 7051 7052 setFunctionHasBranchProtectedScope(); 7053 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 7054 NestedLoopCount, Clauses, AStmt, B); 7055 } 7056 7057 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 7058 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7059 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7060 if (!AStmt) 7061 return StmtError(); 7062 7063 auto *CS = cast<CapturedStmt>(AStmt); 7064 // 1.2.2 OpenMP Language Terminology 7065 // Structured block - An executable statement with a single entry at the 7066 // top and a single exit at the bottom. 7067 // The point of exit cannot be a branch out of the structured block. 7068 // longjmp() and throw() must not violate the entry/exit criteria. 7069 CS->getCapturedDecl()->setNothrow(); 7070 for (int ThisCaptureLevel = 7071 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 7072 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7073 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7074 // 1.2.2 OpenMP Language Terminology 7075 // Structured block - An executable statement with a single entry at the 7076 // top and a single exit at the bottom. 7077 // The point of exit cannot be a branch out of the structured block. 7078 // longjmp() and throw() must not violate the entry/exit criteria. 7079 CS->getCapturedDecl()->setNothrow(); 7080 } 7081 7082 OMPLoopDirective::HelperExprs B; 7083 // In presence of clause 'collapse' with number of loops, it will 7084 // define the nested loops number. 7085 unsigned NestedLoopCount = checkOpenMPLoop( 7086 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 7087 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7088 VarsWithImplicitDSA, B); 7089 if (NestedLoopCount == 0) 7090 return StmtError(); 7091 7092 assert((CurContext->isDependentContext() || B.builtAll()) && 7093 "omp for loop exprs were not built"); 7094 7095 setFunctionHasBranchProtectedScope(); 7096 return OMPDistributeParallelForDirective::Create( 7097 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 7098 DSAStack->isCancelRegion()); 7099 } 7100 7101 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 7102 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7103 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7104 if (!AStmt) 7105 return StmtError(); 7106 7107 auto *CS = cast<CapturedStmt>(AStmt); 7108 // 1.2.2 OpenMP Language Terminology 7109 // Structured block - An executable statement with a single entry at the 7110 // top and a single exit at the bottom. 7111 // The point of exit cannot be a branch out of the structured block. 7112 // longjmp() and throw() must not violate the entry/exit criteria. 7113 CS->getCapturedDecl()->setNothrow(); 7114 for (int ThisCaptureLevel = 7115 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 7116 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7117 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7118 // 1.2.2 OpenMP Language Terminology 7119 // Structured block - An executable statement with a single entry at the 7120 // top and a single exit at the bottom. 7121 // The point of exit cannot be a branch out of the structured block. 7122 // longjmp() and throw() must not violate the entry/exit criteria. 7123 CS->getCapturedDecl()->setNothrow(); 7124 } 7125 7126 OMPLoopDirective::HelperExprs B; 7127 // In presence of clause 'collapse' with number of loops, it will 7128 // define the nested loops number. 7129 unsigned NestedLoopCount = checkOpenMPLoop( 7130 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 7131 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7132 VarsWithImplicitDSA, B); 7133 if (NestedLoopCount == 0) 7134 return StmtError(); 7135 7136 assert((CurContext->isDependentContext() || B.builtAll()) && 7137 "omp for loop exprs were not built"); 7138 7139 if (!CurContext->isDependentContext()) { 7140 // Finalize the clauses that need pre-built expressions for CodeGen. 7141 for (OMPClause *C : Clauses) { 7142 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7143 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7144 B.NumIterations, *this, CurScope, 7145 DSAStack)) 7146 return StmtError(); 7147 } 7148 } 7149 7150 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7151 return StmtError(); 7152 7153 setFunctionHasBranchProtectedScope(); 7154 return OMPDistributeParallelForSimdDirective::Create( 7155 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7156 } 7157 7158 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 7159 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7160 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7161 if (!AStmt) 7162 return StmtError(); 7163 7164 auto *CS = cast<CapturedStmt>(AStmt); 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 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 7172 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7173 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7174 // 1.2.2 OpenMP Language Terminology 7175 // Structured block - An executable statement with a single entry at the 7176 // top and a single exit at the bottom. 7177 // The point of exit cannot be a branch out of the structured block. 7178 // longjmp() and throw() must not violate the entry/exit criteria. 7179 CS->getCapturedDecl()->setNothrow(); 7180 } 7181 7182 OMPLoopDirective::HelperExprs B; 7183 // In presence of clause 'collapse' with number of loops, it will 7184 // define the nested loops number. 7185 unsigned NestedLoopCount = 7186 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 7187 nullptr /*ordered not a clause on distribute*/, CS, *this, 7188 *DSAStack, VarsWithImplicitDSA, B); 7189 if (NestedLoopCount == 0) 7190 return StmtError(); 7191 7192 assert((CurContext->isDependentContext() || B.builtAll()) && 7193 "omp for loop exprs were not built"); 7194 7195 if (!CurContext->isDependentContext()) { 7196 // Finalize the clauses that need pre-built expressions for CodeGen. 7197 for (OMPClause *C : Clauses) { 7198 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7199 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7200 B.NumIterations, *this, CurScope, 7201 DSAStack)) 7202 return StmtError(); 7203 } 7204 } 7205 7206 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7207 return StmtError(); 7208 7209 setFunctionHasBranchProtectedScope(); 7210 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 7211 NestedLoopCount, Clauses, AStmt, B); 7212 } 7213 7214 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 7215 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7216 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7217 if (!AStmt) 7218 return StmtError(); 7219 7220 auto *CS = cast<CapturedStmt>(AStmt); 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 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 7228 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7229 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7230 // 1.2.2 OpenMP Language Terminology 7231 // Structured block - An executable statement with a single entry at the 7232 // top and a single exit at the bottom. 7233 // The point of exit cannot be a branch out of the structured block. 7234 // longjmp() and throw() must not violate the entry/exit criteria. 7235 CS->getCapturedDecl()->setNothrow(); 7236 } 7237 7238 OMPLoopDirective::HelperExprs B; 7239 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 7240 // define the nested loops number. 7241 unsigned NestedLoopCount = checkOpenMPLoop( 7242 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 7243 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 7244 VarsWithImplicitDSA, B); 7245 if (NestedLoopCount == 0) 7246 return StmtError(); 7247 7248 assert((CurContext->isDependentContext() || B.builtAll()) && 7249 "omp target parallel for simd loop exprs were not built"); 7250 7251 if (!CurContext->isDependentContext()) { 7252 // Finalize the clauses that need pre-built expressions for CodeGen. 7253 for (OMPClause *C : Clauses) { 7254 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7255 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7256 B.NumIterations, *this, CurScope, 7257 DSAStack)) 7258 return StmtError(); 7259 } 7260 } 7261 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7262 return StmtError(); 7263 7264 setFunctionHasBranchProtectedScope(); 7265 return OMPTargetParallelForSimdDirective::Create( 7266 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7267 } 7268 7269 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 7270 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7271 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7272 if (!AStmt) 7273 return StmtError(); 7274 7275 auto *CS = cast<CapturedStmt>(AStmt); 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 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 7283 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7284 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7285 // 1.2.2 OpenMP Language Terminology 7286 // Structured block - An executable statement with a single entry at the 7287 // top and a single exit at the bottom. 7288 // The point of exit cannot be a branch out of the structured block. 7289 // longjmp() and throw() must not violate the entry/exit criteria. 7290 CS->getCapturedDecl()->setNothrow(); 7291 } 7292 7293 OMPLoopDirective::HelperExprs B; 7294 // In presence of clause 'collapse' with number of loops, it will define the 7295 // nested loops number. 7296 unsigned NestedLoopCount = 7297 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 7298 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 7299 VarsWithImplicitDSA, B); 7300 if (NestedLoopCount == 0) 7301 return StmtError(); 7302 7303 assert((CurContext->isDependentContext() || B.builtAll()) && 7304 "omp target simd loop exprs were not built"); 7305 7306 if (!CurContext->isDependentContext()) { 7307 // Finalize the clauses that need pre-built expressions for CodeGen. 7308 for (OMPClause *C : Clauses) { 7309 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7310 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7311 B.NumIterations, *this, CurScope, 7312 DSAStack)) 7313 return StmtError(); 7314 } 7315 } 7316 7317 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7318 return StmtError(); 7319 7320 setFunctionHasBranchProtectedScope(); 7321 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 7322 NestedLoopCount, Clauses, AStmt, B); 7323 } 7324 7325 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 7326 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7327 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7328 if (!AStmt) 7329 return StmtError(); 7330 7331 auto *CS = cast<CapturedStmt>(AStmt); 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 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 7339 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7340 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7341 // 1.2.2 OpenMP Language Terminology 7342 // Structured block - An executable statement with a single entry at the 7343 // top and a single exit at the bottom. 7344 // The point of exit cannot be a branch out of the structured block. 7345 // longjmp() and throw() must not violate the entry/exit criteria. 7346 CS->getCapturedDecl()->setNothrow(); 7347 } 7348 7349 OMPLoopDirective::HelperExprs B; 7350 // In presence of clause 'collapse' with number of loops, it will 7351 // define the nested loops number. 7352 unsigned NestedLoopCount = 7353 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 7354 nullptr /*ordered not a clause on distribute*/, CS, *this, 7355 *DSAStack, VarsWithImplicitDSA, B); 7356 if (NestedLoopCount == 0) 7357 return StmtError(); 7358 7359 assert((CurContext->isDependentContext() || B.builtAll()) && 7360 "omp teams distribute loop exprs were not built"); 7361 7362 setFunctionHasBranchProtectedScope(); 7363 7364 DSAStack->setParentTeamsRegionLoc(StartLoc); 7365 7366 return OMPTeamsDistributeDirective::Create( 7367 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7368 } 7369 7370 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 7371 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7372 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7373 if (!AStmt) 7374 return StmtError(); 7375 7376 auto *CS = cast<CapturedStmt>(AStmt); 7377 // 1.2.2 OpenMP Language Terminology 7378 // Structured block - An executable statement with a single entry at the 7379 // top and a single exit at the bottom. 7380 // The point of exit cannot be a branch out of the structured block. 7381 // longjmp() and throw() must not violate the entry/exit criteria. 7382 CS->getCapturedDecl()->setNothrow(); 7383 for (int ThisCaptureLevel = 7384 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 7385 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7386 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7387 // 1.2.2 OpenMP Language Terminology 7388 // Structured block - An executable statement with a single entry at the 7389 // top and a single exit at the bottom. 7390 // The point of exit cannot be a branch out of the structured block. 7391 // longjmp() and throw() must not violate the entry/exit criteria. 7392 CS->getCapturedDecl()->setNothrow(); 7393 } 7394 7395 7396 OMPLoopDirective::HelperExprs B; 7397 // In presence of clause 'collapse' with number of loops, it will 7398 // define the nested loops number. 7399 unsigned NestedLoopCount = checkOpenMPLoop( 7400 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 7401 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7402 VarsWithImplicitDSA, B); 7403 7404 if (NestedLoopCount == 0) 7405 return StmtError(); 7406 7407 assert((CurContext->isDependentContext() || B.builtAll()) && 7408 "omp teams distribute simd loop exprs were not built"); 7409 7410 if (!CurContext->isDependentContext()) { 7411 // Finalize the clauses that need pre-built expressions for CodeGen. 7412 for (OMPClause *C : Clauses) { 7413 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7414 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7415 B.NumIterations, *this, CurScope, 7416 DSAStack)) 7417 return StmtError(); 7418 } 7419 } 7420 7421 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7422 return StmtError(); 7423 7424 setFunctionHasBranchProtectedScope(); 7425 7426 DSAStack->setParentTeamsRegionLoc(StartLoc); 7427 7428 return OMPTeamsDistributeSimdDirective::Create( 7429 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7430 } 7431 7432 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 7433 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7434 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7435 if (!AStmt) 7436 return StmtError(); 7437 7438 auto *CS = cast<CapturedStmt>(AStmt); 7439 // 1.2.2 OpenMP Language Terminology 7440 // Structured block - An executable statement with a single entry at the 7441 // top and a single exit at the bottom. 7442 // The point of exit cannot be a branch out of the structured block. 7443 // longjmp() and throw() must not violate the entry/exit criteria. 7444 CS->getCapturedDecl()->setNothrow(); 7445 7446 for (int ThisCaptureLevel = 7447 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 7448 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7449 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7450 // 1.2.2 OpenMP Language Terminology 7451 // Structured block - An executable statement with a single entry at the 7452 // top and a single exit at the bottom. 7453 // The point of exit cannot be a branch out of the structured block. 7454 // longjmp() and throw() must not violate the entry/exit criteria. 7455 CS->getCapturedDecl()->setNothrow(); 7456 } 7457 7458 OMPLoopDirective::HelperExprs B; 7459 // In presence of clause 'collapse' with number of loops, it will 7460 // define the nested loops number. 7461 unsigned NestedLoopCount = checkOpenMPLoop( 7462 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 7463 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7464 VarsWithImplicitDSA, B); 7465 7466 if (NestedLoopCount == 0) 7467 return StmtError(); 7468 7469 assert((CurContext->isDependentContext() || B.builtAll()) && 7470 "omp for loop exprs were not built"); 7471 7472 if (!CurContext->isDependentContext()) { 7473 // Finalize the clauses that need pre-built expressions for CodeGen. 7474 for (OMPClause *C : Clauses) { 7475 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7476 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7477 B.NumIterations, *this, CurScope, 7478 DSAStack)) 7479 return StmtError(); 7480 } 7481 } 7482 7483 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7484 return StmtError(); 7485 7486 setFunctionHasBranchProtectedScope(); 7487 7488 DSAStack->setParentTeamsRegionLoc(StartLoc); 7489 7490 return OMPTeamsDistributeParallelForSimdDirective::Create( 7491 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7492 } 7493 7494 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 7495 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7496 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7497 if (!AStmt) 7498 return StmtError(); 7499 7500 auto *CS = cast<CapturedStmt>(AStmt); 7501 // 1.2.2 OpenMP Language Terminology 7502 // Structured block - An executable statement with a single entry at the 7503 // top and a single exit at the bottom. 7504 // The point of exit cannot be a branch out of the structured block. 7505 // longjmp() and throw() must not violate the entry/exit criteria. 7506 CS->getCapturedDecl()->setNothrow(); 7507 7508 for (int ThisCaptureLevel = 7509 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 7510 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7511 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7512 // 1.2.2 OpenMP Language Terminology 7513 // Structured block - An executable statement with a single entry at the 7514 // top and a single exit at the bottom. 7515 // The point of exit cannot be a branch out of the structured block. 7516 // longjmp() and throw() must not violate the entry/exit criteria. 7517 CS->getCapturedDecl()->setNothrow(); 7518 } 7519 7520 OMPLoopDirective::HelperExprs B; 7521 // In presence of clause 'collapse' with number of loops, it will 7522 // define the nested loops number. 7523 unsigned NestedLoopCount = checkOpenMPLoop( 7524 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 7525 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7526 VarsWithImplicitDSA, B); 7527 7528 if (NestedLoopCount == 0) 7529 return StmtError(); 7530 7531 assert((CurContext->isDependentContext() || B.builtAll()) && 7532 "omp for loop exprs were not built"); 7533 7534 setFunctionHasBranchProtectedScope(); 7535 7536 DSAStack->setParentTeamsRegionLoc(StartLoc); 7537 7538 return OMPTeamsDistributeParallelForDirective::Create( 7539 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 7540 DSAStack->isCancelRegion()); 7541 } 7542 7543 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 7544 Stmt *AStmt, 7545 SourceLocation StartLoc, 7546 SourceLocation EndLoc) { 7547 if (!AStmt) 7548 return StmtError(); 7549 7550 auto *CS = cast<CapturedStmt>(AStmt); 7551 // 1.2.2 OpenMP Language Terminology 7552 // Structured block - An executable statement with a single entry at the 7553 // top and a single exit at the bottom. 7554 // The point of exit cannot be a branch out of the structured block. 7555 // longjmp() and throw() must not violate the entry/exit criteria. 7556 CS->getCapturedDecl()->setNothrow(); 7557 7558 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 7559 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7560 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7561 // 1.2.2 OpenMP Language Terminology 7562 // Structured block - An executable statement with a single entry at the 7563 // top and a single exit at the bottom. 7564 // The point of exit cannot be a branch out of the structured block. 7565 // longjmp() and throw() must not violate the entry/exit criteria. 7566 CS->getCapturedDecl()->setNothrow(); 7567 } 7568 setFunctionHasBranchProtectedScope(); 7569 7570 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 7571 AStmt); 7572 } 7573 7574 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 7575 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7576 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7577 if (!AStmt) 7578 return StmtError(); 7579 7580 auto *CS = cast<CapturedStmt>(AStmt); 7581 // 1.2.2 OpenMP Language Terminology 7582 // Structured block - An executable statement with a single entry at the 7583 // top and a single exit at the bottom. 7584 // The point of exit cannot be a branch out of the structured block. 7585 // longjmp() and throw() must not violate the entry/exit criteria. 7586 CS->getCapturedDecl()->setNothrow(); 7587 for (int ThisCaptureLevel = 7588 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 7589 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7590 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7591 // 1.2.2 OpenMP Language Terminology 7592 // Structured block - An executable statement with a single entry at the 7593 // top and a single exit at the bottom. 7594 // The point of exit cannot be a branch out of the structured block. 7595 // longjmp() and throw() must not violate the entry/exit criteria. 7596 CS->getCapturedDecl()->setNothrow(); 7597 } 7598 7599 OMPLoopDirective::HelperExprs B; 7600 // In presence of clause 'collapse' with number of loops, it will 7601 // define the nested loops number. 7602 unsigned NestedLoopCount = checkOpenMPLoop( 7603 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 7604 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7605 VarsWithImplicitDSA, B); 7606 if (NestedLoopCount == 0) 7607 return StmtError(); 7608 7609 assert((CurContext->isDependentContext() || B.builtAll()) && 7610 "omp target teams distribute loop exprs were not built"); 7611 7612 setFunctionHasBranchProtectedScope(); 7613 return OMPTargetTeamsDistributeDirective::Create( 7614 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7615 } 7616 7617 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 7618 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7619 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7620 if (!AStmt) 7621 return StmtError(); 7622 7623 auto *CS = cast<CapturedStmt>(AStmt); 7624 // 1.2.2 OpenMP Language Terminology 7625 // Structured block - An executable statement with a single entry at the 7626 // top and a single exit at the bottom. 7627 // The point of exit cannot be a branch out of the structured block. 7628 // longjmp() and throw() must not violate the entry/exit criteria. 7629 CS->getCapturedDecl()->setNothrow(); 7630 for (int ThisCaptureLevel = 7631 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 7632 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7633 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7634 // 1.2.2 OpenMP Language Terminology 7635 // Structured block - An executable statement with a single entry at the 7636 // top and a single exit at the bottom. 7637 // The point of exit cannot be a branch out of the structured block. 7638 // longjmp() and throw() must not violate the entry/exit criteria. 7639 CS->getCapturedDecl()->setNothrow(); 7640 } 7641 7642 OMPLoopDirective::HelperExprs B; 7643 // In presence of clause 'collapse' with number of loops, it will 7644 // define the nested loops number. 7645 unsigned NestedLoopCount = checkOpenMPLoop( 7646 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 7647 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7648 VarsWithImplicitDSA, B); 7649 if (NestedLoopCount == 0) 7650 return StmtError(); 7651 7652 assert((CurContext->isDependentContext() || B.builtAll()) && 7653 "omp target teams distribute parallel for loop exprs were not built"); 7654 7655 if (!CurContext->isDependentContext()) { 7656 // Finalize the clauses that need pre-built expressions for CodeGen. 7657 for (OMPClause *C : Clauses) { 7658 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7659 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7660 B.NumIterations, *this, CurScope, 7661 DSAStack)) 7662 return StmtError(); 7663 } 7664 } 7665 7666 setFunctionHasBranchProtectedScope(); 7667 return OMPTargetTeamsDistributeParallelForDirective::Create( 7668 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 7669 DSAStack->isCancelRegion()); 7670 } 7671 7672 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 7673 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7674 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7675 if (!AStmt) 7676 return StmtError(); 7677 7678 auto *CS = cast<CapturedStmt>(AStmt); 7679 // 1.2.2 OpenMP Language Terminology 7680 // Structured block - An executable statement with a single entry at the 7681 // top and a single exit at the bottom. 7682 // The point of exit cannot be a branch out of the structured block. 7683 // longjmp() and throw() must not violate the entry/exit criteria. 7684 CS->getCapturedDecl()->setNothrow(); 7685 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 7686 OMPD_target_teams_distribute_parallel_for_simd); 7687 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7688 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7689 // 1.2.2 OpenMP Language Terminology 7690 // Structured block - An executable statement with a single entry at the 7691 // top and a single exit at the bottom. 7692 // The point of exit cannot be a branch out of the structured block. 7693 // longjmp() and throw() must not violate the entry/exit criteria. 7694 CS->getCapturedDecl()->setNothrow(); 7695 } 7696 7697 OMPLoopDirective::HelperExprs B; 7698 // In presence of clause 'collapse' with number of loops, it will 7699 // define the nested loops number. 7700 unsigned NestedLoopCount = 7701 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 7702 getCollapseNumberExpr(Clauses), 7703 nullptr /*ordered not a clause on distribute*/, CS, *this, 7704 *DSAStack, VarsWithImplicitDSA, B); 7705 if (NestedLoopCount == 0) 7706 return StmtError(); 7707 7708 assert((CurContext->isDependentContext() || B.builtAll()) && 7709 "omp target teams distribute parallel for simd loop exprs were not " 7710 "built"); 7711 7712 if (!CurContext->isDependentContext()) { 7713 // Finalize the clauses that need pre-built expressions for CodeGen. 7714 for (OMPClause *C : Clauses) { 7715 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7716 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7717 B.NumIterations, *this, CurScope, 7718 DSAStack)) 7719 return StmtError(); 7720 } 7721 } 7722 7723 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7724 return StmtError(); 7725 7726 setFunctionHasBranchProtectedScope(); 7727 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 7728 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7729 } 7730 7731 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 7732 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7733 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7734 if (!AStmt) 7735 return StmtError(); 7736 7737 auto *CS = cast<CapturedStmt>(AStmt); 7738 // 1.2.2 OpenMP Language Terminology 7739 // Structured block - An executable statement with a single entry at the 7740 // top and a single exit at the bottom. 7741 // The point of exit cannot be a branch out of the structured block. 7742 // longjmp() and throw() must not violate the entry/exit criteria. 7743 CS->getCapturedDecl()->setNothrow(); 7744 for (int ThisCaptureLevel = 7745 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 7746 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7747 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7748 // 1.2.2 OpenMP Language Terminology 7749 // Structured block - An executable statement with a single entry at the 7750 // top and a single exit at the bottom. 7751 // The point of exit cannot be a branch out of the structured block. 7752 // longjmp() and throw() must not violate the entry/exit criteria. 7753 CS->getCapturedDecl()->setNothrow(); 7754 } 7755 7756 OMPLoopDirective::HelperExprs B; 7757 // In presence of clause 'collapse' with number of loops, it will 7758 // define the nested loops number. 7759 unsigned NestedLoopCount = checkOpenMPLoop( 7760 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 7761 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7762 VarsWithImplicitDSA, B); 7763 if (NestedLoopCount == 0) 7764 return StmtError(); 7765 7766 assert((CurContext->isDependentContext() || B.builtAll()) && 7767 "omp target teams distribute simd loop exprs were not built"); 7768 7769 if (!CurContext->isDependentContext()) { 7770 // Finalize the clauses that need pre-built expressions for CodeGen. 7771 for (OMPClause *C : Clauses) { 7772 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7773 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7774 B.NumIterations, *this, CurScope, 7775 DSAStack)) 7776 return StmtError(); 7777 } 7778 } 7779 7780 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7781 return StmtError(); 7782 7783 setFunctionHasBranchProtectedScope(); 7784 return OMPTargetTeamsDistributeSimdDirective::Create( 7785 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7786 } 7787 7788 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 7789 SourceLocation StartLoc, 7790 SourceLocation LParenLoc, 7791 SourceLocation EndLoc) { 7792 OMPClause *Res = nullptr; 7793 switch (Kind) { 7794 case OMPC_final: 7795 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 7796 break; 7797 case OMPC_num_threads: 7798 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 7799 break; 7800 case OMPC_safelen: 7801 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 7802 break; 7803 case OMPC_simdlen: 7804 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 7805 break; 7806 case OMPC_collapse: 7807 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 7808 break; 7809 case OMPC_ordered: 7810 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 7811 break; 7812 case OMPC_device: 7813 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc); 7814 break; 7815 case OMPC_num_teams: 7816 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 7817 break; 7818 case OMPC_thread_limit: 7819 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 7820 break; 7821 case OMPC_priority: 7822 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 7823 break; 7824 case OMPC_grainsize: 7825 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 7826 break; 7827 case OMPC_num_tasks: 7828 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 7829 break; 7830 case OMPC_hint: 7831 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 7832 break; 7833 case OMPC_if: 7834 case OMPC_default: 7835 case OMPC_proc_bind: 7836 case OMPC_schedule: 7837 case OMPC_private: 7838 case OMPC_firstprivate: 7839 case OMPC_lastprivate: 7840 case OMPC_shared: 7841 case OMPC_reduction: 7842 case OMPC_task_reduction: 7843 case OMPC_in_reduction: 7844 case OMPC_linear: 7845 case OMPC_aligned: 7846 case OMPC_copyin: 7847 case OMPC_copyprivate: 7848 case OMPC_nowait: 7849 case OMPC_untied: 7850 case OMPC_mergeable: 7851 case OMPC_threadprivate: 7852 case OMPC_flush: 7853 case OMPC_read: 7854 case OMPC_write: 7855 case OMPC_update: 7856 case OMPC_capture: 7857 case OMPC_seq_cst: 7858 case OMPC_depend: 7859 case OMPC_threads: 7860 case OMPC_simd: 7861 case OMPC_map: 7862 case OMPC_nogroup: 7863 case OMPC_dist_schedule: 7864 case OMPC_defaultmap: 7865 case OMPC_unknown: 7866 case OMPC_uniform: 7867 case OMPC_to: 7868 case OMPC_from: 7869 case OMPC_use_device_ptr: 7870 case OMPC_is_device_ptr: 7871 llvm_unreachable("Clause is not allowed."); 7872 } 7873 return Res; 7874 } 7875 7876 // An OpenMP directive such as 'target parallel' has two captured regions: 7877 // for the 'target' and 'parallel' respectively. This function returns 7878 // the region in which to capture expressions associated with a clause. 7879 // A return value of OMPD_unknown signifies that the expression should not 7880 // be captured. 7881 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 7882 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 7883 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 7884 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 7885 switch (CKind) { 7886 case OMPC_if: 7887 switch (DKind) { 7888 case OMPD_target_parallel: 7889 case OMPD_target_parallel_for: 7890 case OMPD_target_parallel_for_simd: 7891 // If this clause applies to the nested 'parallel' region, capture within 7892 // the 'target' region, otherwise do not capture. 7893 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 7894 CaptureRegion = OMPD_target; 7895 break; 7896 case OMPD_target_teams_distribute_parallel_for: 7897 case OMPD_target_teams_distribute_parallel_for_simd: 7898 // If this clause applies to the nested 'parallel' region, capture within 7899 // the 'teams' region, otherwise do not capture. 7900 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 7901 CaptureRegion = OMPD_teams; 7902 break; 7903 case OMPD_teams_distribute_parallel_for: 7904 case OMPD_teams_distribute_parallel_for_simd: 7905 CaptureRegion = OMPD_teams; 7906 break; 7907 case OMPD_target_update: 7908 case OMPD_target_enter_data: 7909 case OMPD_target_exit_data: 7910 CaptureRegion = OMPD_task; 7911 break; 7912 case OMPD_cancel: 7913 case OMPD_parallel: 7914 case OMPD_parallel_sections: 7915 case OMPD_parallel_for: 7916 case OMPD_parallel_for_simd: 7917 case OMPD_target: 7918 case OMPD_target_simd: 7919 case OMPD_target_teams: 7920 case OMPD_target_teams_distribute: 7921 case OMPD_target_teams_distribute_simd: 7922 case OMPD_distribute_parallel_for: 7923 case OMPD_distribute_parallel_for_simd: 7924 case OMPD_task: 7925 case OMPD_taskloop: 7926 case OMPD_taskloop_simd: 7927 case OMPD_target_data: 7928 // Do not capture if-clause expressions. 7929 break; 7930 case OMPD_threadprivate: 7931 case OMPD_taskyield: 7932 case OMPD_barrier: 7933 case OMPD_taskwait: 7934 case OMPD_cancellation_point: 7935 case OMPD_flush: 7936 case OMPD_declare_reduction: 7937 case OMPD_declare_simd: 7938 case OMPD_declare_target: 7939 case OMPD_end_declare_target: 7940 case OMPD_teams: 7941 case OMPD_simd: 7942 case OMPD_for: 7943 case OMPD_for_simd: 7944 case OMPD_sections: 7945 case OMPD_section: 7946 case OMPD_single: 7947 case OMPD_master: 7948 case OMPD_critical: 7949 case OMPD_taskgroup: 7950 case OMPD_distribute: 7951 case OMPD_ordered: 7952 case OMPD_atomic: 7953 case OMPD_distribute_simd: 7954 case OMPD_teams_distribute: 7955 case OMPD_teams_distribute_simd: 7956 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 7957 case OMPD_unknown: 7958 llvm_unreachable("Unknown OpenMP directive"); 7959 } 7960 break; 7961 case OMPC_num_threads: 7962 switch (DKind) { 7963 case OMPD_target_parallel: 7964 case OMPD_target_parallel_for: 7965 case OMPD_target_parallel_for_simd: 7966 CaptureRegion = OMPD_target; 7967 break; 7968 case OMPD_teams_distribute_parallel_for: 7969 case OMPD_teams_distribute_parallel_for_simd: 7970 case OMPD_target_teams_distribute_parallel_for: 7971 case OMPD_target_teams_distribute_parallel_for_simd: 7972 CaptureRegion = OMPD_teams; 7973 break; 7974 case OMPD_parallel: 7975 case OMPD_parallel_sections: 7976 case OMPD_parallel_for: 7977 case OMPD_parallel_for_simd: 7978 case OMPD_distribute_parallel_for: 7979 case OMPD_distribute_parallel_for_simd: 7980 // Do not capture num_threads-clause expressions. 7981 break; 7982 case OMPD_target_data: 7983 case OMPD_target_enter_data: 7984 case OMPD_target_exit_data: 7985 case OMPD_target_update: 7986 case OMPD_target: 7987 case OMPD_target_simd: 7988 case OMPD_target_teams: 7989 case OMPD_target_teams_distribute: 7990 case OMPD_target_teams_distribute_simd: 7991 case OMPD_cancel: 7992 case OMPD_task: 7993 case OMPD_taskloop: 7994 case OMPD_taskloop_simd: 7995 case OMPD_threadprivate: 7996 case OMPD_taskyield: 7997 case OMPD_barrier: 7998 case OMPD_taskwait: 7999 case OMPD_cancellation_point: 8000 case OMPD_flush: 8001 case OMPD_declare_reduction: 8002 case OMPD_declare_simd: 8003 case OMPD_declare_target: 8004 case OMPD_end_declare_target: 8005 case OMPD_teams: 8006 case OMPD_simd: 8007 case OMPD_for: 8008 case OMPD_for_simd: 8009 case OMPD_sections: 8010 case OMPD_section: 8011 case OMPD_single: 8012 case OMPD_master: 8013 case OMPD_critical: 8014 case OMPD_taskgroup: 8015 case OMPD_distribute: 8016 case OMPD_ordered: 8017 case OMPD_atomic: 8018 case OMPD_distribute_simd: 8019 case OMPD_teams_distribute: 8020 case OMPD_teams_distribute_simd: 8021 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 8022 case OMPD_unknown: 8023 llvm_unreachable("Unknown OpenMP directive"); 8024 } 8025 break; 8026 case OMPC_num_teams: 8027 switch (DKind) { 8028 case OMPD_target_teams: 8029 case OMPD_target_teams_distribute: 8030 case OMPD_target_teams_distribute_simd: 8031 case OMPD_target_teams_distribute_parallel_for: 8032 case OMPD_target_teams_distribute_parallel_for_simd: 8033 CaptureRegion = OMPD_target; 8034 break; 8035 case OMPD_teams_distribute_parallel_for: 8036 case OMPD_teams_distribute_parallel_for_simd: 8037 case OMPD_teams: 8038 case OMPD_teams_distribute: 8039 case OMPD_teams_distribute_simd: 8040 // Do not capture num_teams-clause expressions. 8041 break; 8042 case OMPD_distribute_parallel_for: 8043 case OMPD_distribute_parallel_for_simd: 8044 case OMPD_task: 8045 case OMPD_taskloop: 8046 case OMPD_taskloop_simd: 8047 case OMPD_target_data: 8048 case OMPD_target_enter_data: 8049 case OMPD_target_exit_data: 8050 case OMPD_target_update: 8051 case OMPD_cancel: 8052 case OMPD_parallel: 8053 case OMPD_parallel_sections: 8054 case OMPD_parallel_for: 8055 case OMPD_parallel_for_simd: 8056 case OMPD_target: 8057 case OMPD_target_simd: 8058 case OMPD_target_parallel: 8059 case OMPD_target_parallel_for: 8060 case OMPD_target_parallel_for_simd: 8061 case OMPD_threadprivate: 8062 case OMPD_taskyield: 8063 case OMPD_barrier: 8064 case OMPD_taskwait: 8065 case OMPD_cancellation_point: 8066 case OMPD_flush: 8067 case OMPD_declare_reduction: 8068 case OMPD_declare_simd: 8069 case OMPD_declare_target: 8070 case OMPD_end_declare_target: 8071 case OMPD_simd: 8072 case OMPD_for: 8073 case OMPD_for_simd: 8074 case OMPD_sections: 8075 case OMPD_section: 8076 case OMPD_single: 8077 case OMPD_master: 8078 case OMPD_critical: 8079 case OMPD_taskgroup: 8080 case OMPD_distribute: 8081 case OMPD_ordered: 8082 case OMPD_atomic: 8083 case OMPD_distribute_simd: 8084 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 8085 case OMPD_unknown: 8086 llvm_unreachable("Unknown OpenMP directive"); 8087 } 8088 break; 8089 case OMPC_thread_limit: 8090 switch (DKind) { 8091 case OMPD_target_teams: 8092 case OMPD_target_teams_distribute: 8093 case OMPD_target_teams_distribute_simd: 8094 case OMPD_target_teams_distribute_parallel_for: 8095 case OMPD_target_teams_distribute_parallel_for_simd: 8096 CaptureRegion = OMPD_target; 8097 break; 8098 case OMPD_teams_distribute_parallel_for: 8099 case OMPD_teams_distribute_parallel_for_simd: 8100 case OMPD_teams: 8101 case OMPD_teams_distribute: 8102 case OMPD_teams_distribute_simd: 8103 // Do not capture thread_limit-clause expressions. 8104 break; 8105 case OMPD_distribute_parallel_for: 8106 case OMPD_distribute_parallel_for_simd: 8107 case OMPD_task: 8108 case OMPD_taskloop: 8109 case OMPD_taskloop_simd: 8110 case OMPD_target_data: 8111 case OMPD_target_enter_data: 8112 case OMPD_target_exit_data: 8113 case OMPD_target_update: 8114 case OMPD_cancel: 8115 case OMPD_parallel: 8116 case OMPD_parallel_sections: 8117 case OMPD_parallel_for: 8118 case OMPD_parallel_for_simd: 8119 case OMPD_target: 8120 case OMPD_target_simd: 8121 case OMPD_target_parallel: 8122 case OMPD_target_parallel_for: 8123 case OMPD_target_parallel_for_simd: 8124 case OMPD_threadprivate: 8125 case OMPD_taskyield: 8126 case OMPD_barrier: 8127 case OMPD_taskwait: 8128 case OMPD_cancellation_point: 8129 case OMPD_flush: 8130 case OMPD_declare_reduction: 8131 case OMPD_declare_simd: 8132 case OMPD_declare_target: 8133 case OMPD_end_declare_target: 8134 case OMPD_simd: 8135 case OMPD_for: 8136 case OMPD_for_simd: 8137 case OMPD_sections: 8138 case OMPD_section: 8139 case OMPD_single: 8140 case OMPD_master: 8141 case OMPD_critical: 8142 case OMPD_taskgroup: 8143 case OMPD_distribute: 8144 case OMPD_ordered: 8145 case OMPD_atomic: 8146 case OMPD_distribute_simd: 8147 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 8148 case OMPD_unknown: 8149 llvm_unreachable("Unknown OpenMP directive"); 8150 } 8151 break; 8152 case OMPC_schedule: 8153 switch (DKind) { 8154 case OMPD_parallel_for: 8155 case OMPD_parallel_for_simd: 8156 case OMPD_distribute_parallel_for: 8157 case OMPD_distribute_parallel_for_simd: 8158 case OMPD_teams_distribute_parallel_for: 8159 case OMPD_teams_distribute_parallel_for_simd: 8160 case OMPD_target_parallel_for: 8161 case OMPD_target_parallel_for_simd: 8162 case OMPD_target_teams_distribute_parallel_for: 8163 case OMPD_target_teams_distribute_parallel_for_simd: 8164 CaptureRegion = OMPD_parallel; 8165 break; 8166 case OMPD_for: 8167 case OMPD_for_simd: 8168 // Do not capture schedule-clause expressions. 8169 break; 8170 case OMPD_task: 8171 case OMPD_taskloop: 8172 case OMPD_taskloop_simd: 8173 case OMPD_target_data: 8174 case OMPD_target_enter_data: 8175 case OMPD_target_exit_data: 8176 case OMPD_target_update: 8177 case OMPD_teams: 8178 case OMPD_teams_distribute: 8179 case OMPD_teams_distribute_simd: 8180 case OMPD_target_teams_distribute: 8181 case OMPD_target_teams_distribute_simd: 8182 case OMPD_target: 8183 case OMPD_target_simd: 8184 case OMPD_target_parallel: 8185 case OMPD_cancel: 8186 case OMPD_parallel: 8187 case OMPD_parallel_sections: 8188 case OMPD_threadprivate: 8189 case OMPD_taskyield: 8190 case OMPD_barrier: 8191 case OMPD_taskwait: 8192 case OMPD_cancellation_point: 8193 case OMPD_flush: 8194 case OMPD_declare_reduction: 8195 case OMPD_declare_simd: 8196 case OMPD_declare_target: 8197 case OMPD_end_declare_target: 8198 case OMPD_simd: 8199 case OMPD_sections: 8200 case OMPD_section: 8201 case OMPD_single: 8202 case OMPD_master: 8203 case OMPD_critical: 8204 case OMPD_taskgroup: 8205 case OMPD_distribute: 8206 case OMPD_ordered: 8207 case OMPD_atomic: 8208 case OMPD_distribute_simd: 8209 case OMPD_target_teams: 8210 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 8211 case OMPD_unknown: 8212 llvm_unreachable("Unknown OpenMP directive"); 8213 } 8214 break; 8215 case OMPC_dist_schedule: 8216 switch (DKind) { 8217 case OMPD_teams_distribute_parallel_for: 8218 case OMPD_teams_distribute_parallel_for_simd: 8219 case OMPD_teams_distribute: 8220 case OMPD_teams_distribute_simd: 8221 case OMPD_target_teams_distribute_parallel_for: 8222 case OMPD_target_teams_distribute_parallel_for_simd: 8223 case OMPD_target_teams_distribute: 8224 case OMPD_target_teams_distribute_simd: 8225 CaptureRegion = OMPD_teams; 8226 break; 8227 case OMPD_distribute_parallel_for: 8228 case OMPD_distribute_parallel_for_simd: 8229 case OMPD_distribute: 8230 case OMPD_distribute_simd: 8231 // Do not capture thread_limit-clause expressions. 8232 break; 8233 case OMPD_parallel_for: 8234 case OMPD_parallel_for_simd: 8235 case OMPD_target_parallel_for_simd: 8236 case OMPD_target_parallel_for: 8237 case OMPD_task: 8238 case OMPD_taskloop: 8239 case OMPD_taskloop_simd: 8240 case OMPD_target_data: 8241 case OMPD_target_enter_data: 8242 case OMPD_target_exit_data: 8243 case OMPD_target_update: 8244 case OMPD_teams: 8245 case OMPD_target: 8246 case OMPD_target_simd: 8247 case OMPD_target_parallel: 8248 case OMPD_cancel: 8249 case OMPD_parallel: 8250 case OMPD_parallel_sections: 8251 case OMPD_threadprivate: 8252 case OMPD_taskyield: 8253 case OMPD_barrier: 8254 case OMPD_taskwait: 8255 case OMPD_cancellation_point: 8256 case OMPD_flush: 8257 case OMPD_declare_reduction: 8258 case OMPD_declare_simd: 8259 case OMPD_declare_target: 8260 case OMPD_end_declare_target: 8261 case OMPD_simd: 8262 case OMPD_for: 8263 case OMPD_for_simd: 8264 case OMPD_sections: 8265 case OMPD_section: 8266 case OMPD_single: 8267 case OMPD_master: 8268 case OMPD_critical: 8269 case OMPD_taskgroup: 8270 case OMPD_ordered: 8271 case OMPD_atomic: 8272 case OMPD_target_teams: 8273 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 8274 case OMPD_unknown: 8275 llvm_unreachable("Unknown OpenMP directive"); 8276 } 8277 break; 8278 case OMPC_device: 8279 switch (DKind) { 8280 case OMPD_target_update: 8281 case OMPD_target_enter_data: 8282 case OMPD_target_exit_data: 8283 case OMPD_target: 8284 case OMPD_target_simd: 8285 case OMPD_target_teams: 8286 case OMPD_target_parallel: 8287 case OMPD_target_teams_distribute: 8288 case OMPD_target_teams_distribute_simd: 8289 case OMPD_target_parallel_for: 8290 case OMPD_target_parallel_for_simd: 8291 case OMPD_target_teams_distribute_parallel_for: 8292 case OMPD_target_teams_distribute_parallel_for_simd: 8293 CaptureRegion = OMPD_task; 8294 break; 8295 case OMPD_target_data: 8296 // Do not capture device-clause expressions. 8297 break; 8298 case OMPD_teams_distribute_parallel_for: 8299 case OMPD_teams_distribute_parallel_for_simd: 8300 case OMPD_teams: 8301 case OMPD_teams_distribute: 8302 case OMPD_teams_distribute_simd: 8303 case OMPD_distribute_parallel_for: 8304 case OMPD_distribute_parallel_for_simd: 8305 case OMPD_task: 8306 case OMPD_taskloop: 8307 case OMPD_taskloop_simd: 8308 case OMPD_cancel: 8309 case OMPD_parallel: 8310 case OMPD_parallel_sections: 8311 case OMPD_parallel_for: 8312 case OMPD_parallel_for_simd: 8313 case OMPD_threadprivate: 8314 case OMPD_taskyield: 8315 case OMPD_barrier: 8316 case OMPD_taskwait: 8317 case OMPD_cancellation_point: 8318 case OMPD_flush: 8319 case OMPD_declare_reduction: 8320 case OMPD_declare_simd: 8321 case OMPD_declare_target: 8322 case OMPD_end_declare_target: 8323 case OMPD_simd: 8324 case OMPD_for: 8325 case OMPD_for_simd: 8326 case OMPD_sections: 8327 case OMPD_section: 8328 case OMPD_single: 8329 case OMPD_master: 8330 case OMPD_critical: 8331 case OMPD_taskgroup: 8332 case OMPD_distribute: 8333 case OMPD_ordered: 8334 case OMPD_atomic: 8335 case OMPD_distribute_simd: 8336 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 8337 case OMPD_unknown: 8338 llvm_unreachable("Unknown OpenMP directive"); 8339 } 8340 break; 8341 case OMPC_firstprivate: 8342 case OMPC_lastprivate: 8343 case OMPC_reduction: 8344 case OMPC_task_reduction: 8345 case OMPC_in_reduction: 8346 case OMPC_linear: 8347 case OMPC_default: 8348 case OMPC_proc_bind: 8349 case OMPC_final: 8350 case OMPC_safelen: 8351 case OMPC_simdlen: 8352 case OMPC_collapse: 8353 case OMPC_private: 8354 case OMPC_shared: 8355 case OMPC_aligned: 8356 case OMPC_copyin: 8357 case OMPC_copyprivate: 8358 case OMPC_ordered: 8359 case OMPC_nowait: 8360 case OMPC_untied: 8361 case OMPC_mergeable: 8362 case OMPC_threadprivate: 8363 case OMPC_flush: 8364 case OMPC_read: 8365 case OMPC_write: 8366 case OMPC_update: 8367 case OMPC_capture: 8368 case OMPC_seq_cst: 8369 case OMPC_depend: 8370 case OMPC_threads: 8371 case OMPC_simd: 8372 case OMPC_map: 8373 case OMPC_priority: 8374 case OMPC_grainsize: 8375 case OMPC_nogroup: 8376 case OMPC_num_tasks: 8377 case OMPC_hint: 8378 case OMPC_defaultmap: 8379 case OMPC_unknown: 8380 case OMPC_uniform: 8381 case OMPC_to: 8382 case OMPC_from: 8383 case OMPC_use_device_ptr: 8384 case OMPC_is_device_ptr: 8385 llvm_unreachable("Unexpected OpenMP clause."); 8386 } 8387 return CaptureRegion; 8388 } 8389 8390 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 8391 Expr *Condition, SourceLocation StartLoc, 8392 SourceLocation LParenLoc, 8393 SourceLocation NameModifierLoc, 8394 SourceLocation ColonLoc, 8395 SourceLocation EndLoc) { 8396 Expr *ValExpr = Condition; 8397 Stmt *HelperValStmt = nullptr; 8398 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 8399 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 8400 !Condition->isInstantiationDependent() && 8401 !Condition->containsUnexpandedParameterPack()) { 8402 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 8403 if (Val.isInvalid()) 8404 return nullptr; 8405 8406 ValExpr = Val.get(); 8407 8408 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 8409 CaptureRegion = 8410 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier); 8411 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 8412 ValExpr = MakeFullExpr(ValExpr).get(); 8413 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 8414 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 8415 HelperValStmt = buildPreInits(Context, Captures); 8416 } 8417 } 8418 8419 return new (Context) 8420 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 8421 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 8422 } 8423 8424 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 8425 SourceLocation StartLoc, 8426 SourceLocation LParenLoc, 8427 SourceLocation EndLoc) { 8428 Expr *ValExpr = Condition; 8429 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 8430 !Condition->isInstantiationDependent() && 8431 !Condition->containsUnexpandedParameterPack()) { 8432 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 8433 if (Val.isInvalid()) 8434 return nullptr; 8435 8436 ValExpr = MakeFullExpr(Val.get()).get(); 8437 } 8438 8439 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc); 8440 } 8441 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 8442 Expr *Op) { 8443 if (!Op) 8444 return ExprError(); 8445 8446 class IntConvertDiagnoser : public ICEConvertDiagnoser { 8447 public: 8448 IntConvertDiagnoser() 8449 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 8450 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 8451 QualType T) override { 8452 return S.Diag(Loc, diag::err_omp_not_integral) << T; 8453 } 8454 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 8455 QualType T) override { 8456 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 8457 } 8458 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 8459 QualType T, 8460 QualType ConvTy) override { 8461 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 8462 } 8463 SemaDiagnosticBuilder noteExplicitConv(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 diagnoseAmbiguous(Sema &S, SourceLocation Loc, 8469 QualType T) override { 8470 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 8471 } 8472 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 8473 QualType ConvTy) override { 8474 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 8475 << ConvTy->isEnumeralType() << ConvTy; 8476 } 8477 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 8478 QualType) override { 8479 llvm_unreachable("conversion functions are permitted"); 8480 } 8481 } ConvertDiagnoser; 8482 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 8483 } 8484 8485 static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, 8486 OpenMPClauseKind CKind, 8487 bool StrictlyPositive) { 8488 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 8489 !ValExpr->isInstantiationDependent()) { 8490 SourceLocation Loc = ValExpr->getExprLoc(); 8491 ExprResult Value = 8492 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 8493 if (Value.isInvalid()) 8494 return false; 8495 8496 ValExpr = Value.get(); 8497 // The expression must evaluate to a non-negative integer value. 8498 llvm::APSInt Result; 8499 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) && 8500 Result.isSigned() && 8501 !((!StrictlyPositive && Result.isNonNegative()) || 8502 (StrictlyPositive && Result.isStrictlyPositive()))) { 8503 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 8504 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 8505 << ValExpr->getSourceRange(); 8506 return false; 8507 } 8508 } 8509 return true; 8510 } 8511 8512 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 8513 SourceLocation StartLoc, 8514 SourceLocation LParenLoc, 8515 SourceLocation EndLoc) { 8516 Expr *ValExpr = NumThreads; 8517 Stmt *HelperValStmt = nullptr; 8518 8519 // OpenMP [2.5, Restrictions] 8520 // The num_threads expression must evaluate to a positive integer value. 8521 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 8522 /*StrictlyPositive=*/true)) 8523 return nullptr; 8524 8525 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 8526 OpenMPDirectiveKind CaptureRegion = 8527 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads); 8528 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 8529 ValExpr = MakeFullExpr(ValExpr).get(); 8530 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 8531 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 8532 HelperValStmt = buildPreInits(Context, Captures); 8533 } 8534 8535 return new (Context) OMPNumThreadsClause( 8536 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 8537 } 8538 8539 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 8540 OpenMPClauseKind CKind, 8541 bool StrictlyPositive) { 8542 if (!E) 8543 return ExprError(); 8544 if (E->isValueDependent() || E->isTypeDependent() || 8545 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 8546 return E; 8547 llvm::APSInt Result; 8548 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); 8549 if (ICE.isInvalid()) 8550 return ExprError(); 8551 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 8552 (!StrictlyPositive && !Result.isNonNegative())) { 8553 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 8554 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 8555 << E->getSourceRange(); 8556 return ExprError(); 8557 } 8558 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 8559 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 8560 << E->getSourceRange(); 8561 return ExprError(); 8562 } 8563 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 8564 DSAStack->setAssociatedLoops(Result.getExtValue()); 8565 else if (CKind == OMPC_ordered) 8566 DSAStack->setAssociatedLoops(Result.getExtValue()); 8567 return ICE; 8568 } 8569 8570 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 8571 SourceLocation LParenLoc, 8572 SourceLocation EndLoc) { 8573 // OpenMP [2.8.1, simd construct, Description] 8574 // The parameter of the safelen clause must be a constant 8575 // positive integer expression. 8576 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 8577 if (Safelen.isInvalid()) 8578 return nullptr; 8579 return new (Context) 8580 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 8581 } 8582 8583 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 8584 SourceLocation LParenLoc, 8585 SourceLocation EndLoc) { 8586 // OpenMP [2.8.1, simd construct, Description] 8587 // The parameter of the simdlen clause must be a constant 8588 // positive integer expression. 8589 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 8590 if (Simdlen.isInvalid()) 8591 return nullptr; 8592 return new (Context) 8593 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 8594 } 8595 8596 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 8597 SourceLocation StartLoc, 8598 SourceLocation LParenLoc, 8599 SourceLocation EndLoc) { 8600 // OpenMP [2.7.1, loop construct, Description] 8601 // OpenMP [2.8.1, simd construct, Description] 8602 // OpenMP [2.9.6, distribute construct, Description] 8603 // The parameter of the collapse clause must be a constant 8604 // positive integer expression. 8605 ExprResult NumForLoopsResult = 8606 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 8607 if (NumForLoopsResult.isInvalid()) 8608 return nullptr; 8609 return new (Context) 8610 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 8611 } 8612 8613 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 8614 SourceLocation EndLoc, 8615 SourceLocation LParenLoc, 8616 Expr *NumForLoops) { 8617 // OpenMP [2.7.1, loop construct, Description] 8618 // OpenMP [2.8.1, simd construct, Description] 8619 // OpenMP [2.9.6, distribute construct, Description] 8620 // The parameter of the ordered clause must be a constant 8621 // positive integer expression if any. 8622 if (NumForLoops && LParenLoc.isValid()) { 8623 ExprResult NumForLoopsResult = 8624 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 8625 if (NumForLoopsResult.isInvalid()) 8626 return nullptr; 8627 NumForLoops = NumForLoopsResult.get(); 8628 } else { 8629 NumForLoops = nullptr; 8630 } 8631 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops); 8632 return new (Context) 8633 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc); 8634 } 8635 8636 OMPClause *Sema::ActOnOpenMPSimpleClause( 8637 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 8638 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 8639 OMPClause *Res = nullptr; 8640 switch (Kind) { 8641 case OMPC_default: 8642 Res = 8643 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), 8644 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 8645 break; 8646 case OMPC_proc_bind: 8647 Res = ActOnOpenMPProcBindClause( 8648 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, 8649 LParenLoc, EndLoc); 8650 break; 8651 case OMPC_if: 8652 case OMPC_final: 8653 case OMPC_num_threads: 8654 case OMPC_safelen: 8655 case OMPC_simdlen: 8656 case OMPC_collapse: 8657 case OMPC_schedule: 8658 case OMPC_private: 8659 case OMPC_firstprivate: 8660 case OMPC_lastprivate: 8661 case OMPC_shared: 8662 case OMPC_reduction: 8663 case OMPC_task_reduction: 8664 case OMPC_in_reduction: 8665 case OMPC_linear: 8666 case OMPC_aligned: 8667 case OMPC_copyin: 8668 case OMPC_copyprivate: 8669 case OMPC_ordered: 8670 case OMPC_nowait: 8671 case OMPC_untied: 8672 case OMPC_mergeable: 8673 case OMPC_threadprivate: 8674 case OMPC_flush: 8675 case OMPC_read: 8676 case OMPC_write: 8677 case OMPC_update: 8678 case OMPC_capture: 8679 case OMPC_seq_cst: 8680 case OMPC_depend: 8681 case OMPC_device: 8682 case OMPC_threads: 8683 case OMPC_simd: 8684 case OMPC_map: 8685 case OMPC_num_teams: 8686 case OMPC_thread_limit: 8687 case OMPC_priority: 8688 case OMPC_grainsize: 8689 case OMPC_nogroup: 8690 case OMPC_num_tasks: 8691 case OMPC_hint: 8692 case OMPC_dist_schedule: 8693 case OMPC_defaultmap: 8694 case OMPC_unknown: 8695 case OMPC_uniform: 8696 case OMPC_to: 8697 case OMPC_from: 8698 case OMPC_use_device_ptr: 8699 case OMPC_is_device_ptr: 8700 llvm_unreachable("Clause is not allowed."); 8701 } 8702 return Res; 8703 } 8704 8705 static std::string 8706 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 8707 ArrayRef<unsigned> Exclude = llvm::None) { 8708 SmallString<256> Buffer; 8709 llvm::raw_svector_ostream Out(Buffer); 8710 unsigned Bound = Last >= 2 ? Last - 2 : 0; 8711 unsigned Skipped = Exclude.size(); 8712 auto S = Exclude.begin(), E = Exclude.end(); 8713 for (unsigned I = First; I < Last; ++I) { 8714 if (std::find(S, E, I) != E) { 8715 --Skipped; 8716 continue; 8717 } 8718 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 8719 if (I == Bound - Skipped) 8720 Out << " or "; 8721 else if (I != Bound + 1 - Skipped) 8722 Out << ", "; 8723 } 8724 return Out.str(); 8725 } 8726 8727 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, 8728 SourceLocation KindKwLoc, 8729 SourceLocation StartLoc, 8730 SourceLocation LParenLoc, 8731 SourceLocation EndLoc) { 8732 if (Kind == OMPC_DEFAULT_unknown) { 8733 static_assert(OMPC_DEFAULT_unknown > 0, 8734 "OMPC_DEFAULT_unknown not greater than 0"); 8735 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 8736 << getListOfPossibleValues(OMPC_default, /*First=*/0, 8737 /*Last=*/OMPC_DEFAULT_unknown) 8738 << getOpenMPClauseName(OMPC_default); 8739 return nullptr; 8740 } 8741 switch (Kind) { 8742 case OMPC_DEFAULT_none: 8743 DSAStack->setDefaultDSANone(KindKwLoc); 8744 break; 8745 case OMPC_DEFAULT_shared: 8746 DSAStack->setDefaultDSAShared(KindKwLoc); 8747 break; 8748 case OMPC_DEFAULT_unknown: 8749 llvm_unreachable("Clause kind is not allowed."); 8750 break; 8751 } 8752 return new (Context) 8753 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 8754 } 8755 8756 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, 8757 SourceLocation KindKwLoc, 8758 SourceLocation StartLoc, 8759 SourceLocation LParenLoc, 8760 SourceLocation EndLoc) { 8761 if (Kind == OMPC_PROC_BIND_unknown) { 8762 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 8763 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0, 8764 /*Last=*/OMPC_PROC_BIND_unknown) 8765 << getOpenMPClauseName(OMPC_proc_bind); 8766 return nullptr; 8767 } 8768 return new (Context) 8769 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 8770 } 8771 8772 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 8773 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 8774 SourceLocation StartLoc, SourceLocation LParenLoc, 8775 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 8776 SourceLocation EndLoc) { 8777 OMPClause *Res = nullptr; 8778 switch (Kind) { 8779 case OMPC_schedule: 8780 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 8781 assert(Argument.size() == NumberOfElements && 8782 ArgumentLoc.size() == NumberOfElements); 8783 Res = ActOnOpenMPScheduleClause( 8784 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 8785 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 8786 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 8787 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 8788 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 8789 break; 8790 case OMPC_if: 8791 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 8792 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 8793 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 8794 DelimLoc, EndLoc); 8795 break; 8796 case OMPC_dist_schedule: 8797 Res = ActOnOpenMPDistScheduleClause( 8798 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 8799 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 8800 break; 8801 case OMPC_defaultmap: 8802 enum { Modifier, DefaultmapKind }; 8803 Res = ActOnOpenMPDefaultmapClause( 8804 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 8805 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 8806 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 8807 EndLoc); 8808 break; 8809 case OMPC_final: 8810 case OMPC_num_threads: 8811 case OMPC_safelen: 8812 case OMPC_simdlen: 8813 case OMPC_collapse: 8814 case OMPC_default: 8815 case OMPC_proc_bind: 8816 case OMPC_private: 8817 case OMPC_firstprivate: 8818 case OMPC_lastprivate: 8819 case OMPC_shared: 8820 case OMPC_reduction: 8821 case OMPC_task_reduction: 8822 case OMPC_in_reduction: 8823 case OMPC_linear: 8824 case OMPC_aligned: 8825 case OMPC_copyin: 8826 case OMPC_copyprivate: 8827 case OMPC_ordered: 8828 case OMPC_nowait: 8829 case OMPC_untied: 8830 case OMPC_mergeable: 8831 case OMPC_threadprivate: 8832 case OMPC_flush: 8833 case OMPC_read: 8834 case OMPC_write: 8835 case OMPC_update: 8836 case OMPC_capture: 8837 case OMPC_seq_cst: 8838 case OMPC_depend: 8839 case OMPC_device: 8840 case OMPC_threads: 8841 case OMPC_simd: 8842 case OMPC_map: 8843 case OMPC_num_teams: 8844 case OMPC_thread_limit: 8845 case OMPC_priority: 8846 case OMPC_grainsize: 8847 case OMPC_nogroup: 8848 case OMPC_num_tasks: 8849 case OMPC_hint: 8850 case OMPC_unknown: 8851 case OMPC_uniform: 8852 case OMPC_to: 8853 case OMPC_from: 8854 case OMPC_use_device_ptr: 8855 case OMPC_is_device_ptr: 8856 llvm_unreachable("Clause is not allowed."); 8857 } 8858 return Res; 8859 } 8860 8861 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 8862 OpenMPScheduleClauseModifier M2, 8863 SourceLocation M1Loc, SourceLocation M2Loc) { 8864 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 8865 SmallVector<unsigned, 2> Excluded; 8866 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 8867 Excluded.push_back(M2); 8868 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 8869 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 8870 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 8871 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 8872 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 8873 << getListOfPossibleValues(OMPC_schedule, 8874 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 8875 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 8876 Excluded) 8877 << getOpenMPClauseName(OMPC_schedule); 8878 return true; 8879 } 8880 return false; 8881 } 8882 8883 OMPClause *Sema::ActOnOpenMPScheduleClause( 8884 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 8885 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 8886 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 8887 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 8888 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 8889 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 8890 return nullptr; 8891 // OpenMP, 2.7.1, Loop Construct, Restrictions 8892 // Either the monotonic modifier or the nonmonotonic modifier can be specified 8893 // but not both. 8894 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 8895 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 8896 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 8897 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 8898 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 8899 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 8900 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 8901 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 8902 return nullptr; 8903 } 8904 if (Kind == OMPC_SCHEDULE_unknown) { 8905 std::string Values; 8906 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 8907 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 8908 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 8909 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 8910 Exclude); 8911 } else { 8912 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 8913 /*Last=*/OMPC_SCHEDULE_unknown); 8914 } 8915 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 8916 << Values << getOpenMPClauseName(OMPC_schedule); 8917 return nullptr; 8918 } 8919 // OpenMP, 2.7.1, Loop Construct, Restrictions 8920 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 8921 // schedule(guided). 8922 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 8923 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 8924 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 8925 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 8926 diag::err_omp_schedule_nonmonotonic_static); 8927 return nullptr; 8928 } 8929 Expr *ValExpr = ChunkSize; 8930 Stmt *HelperValStmt = nullptr; 8931 if (ChunkSize) { 8932 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 8933 !ChunkSize->isInstantiationDependent() && 8934 !ChunkSize->containsUnexpandedParameterPack()) { 8935 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); 8936 ExprResult Val = 8937 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 8938 if (Val.isInvalid()) 8939 return nullptr; 8940 8941 ValExpr = Val.get(); 8942 8943 // OpenMP [2.7.1, Restrictions] 8944 // chunk_size must be a loop invariant integer expression with a positive 8945 // value. 8946 llvm::APSInt Result; 8947 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 8948 if (Result.isSigned() && !Result.isStrictlyPositive()) { 8949 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 8950 << "schedule" << 1 << ChunkSize->getSourceRange(); 8951 return nullptr; 8952 } 8953 } else if (getOpenMPCaptureRegionForClause( 8954 DSAStack->getCurrentDirective(), OMPC_schedule) != 8955 OMPD_unknown && 8956 !CurContext->isDependentContext()) { 8957 ValExpr = MakeFullExpr(ValExpr).get(); 8958 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 8959 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 8960 HelperValStmt = buildPreInits(Context, Captures); 8961 } 8962 } 8963 } 8964 8965 return new (Context) 8966 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 8967 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 8968 } 8969 8970 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 8971 SourceLocation StartLoc, 8972 SourceLocation EndLoc) { 8973 OMPClause *Res = nullptr; 8974 switch (Kind) { 8975 case OMPC_ordered: 8976 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 8977 break; 8978 case OMPC_nowait: 8979 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 8980 break; 8981 case OMPC_untied: 8982 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 8983 break; 8984 case OMPC_mergeable: 8985 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 8986 break; 8987 case OMPC_read: 8988 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 8989 break; 8990 case OMPC_write: 8991 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 8992 break; 8993 case OMPC_update: 8994 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 8995 break; 8996 case OMPC_capture: 8997 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 8998 break; 8999 case OMPC_seq_cst: 9000 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 9001 break; 9002 case OMPC_threads: 9003 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 9004 break; 9005 case OMPC_simd: 9006 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 9007 break; 9008 case OMPC_nogroup: 9009 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 9010 break; 9011 case OMPC_if: 9012 case OMPC_final: 9013 case OMPC_num_threads: 9014 case OMPC_safelen: 9015 case OMPC_simdlen: 9016 case OMPC_collapse: 9017 case OMPC_schedule: 9018 case OMPC_private: 9019 case OMPC_firstprivate: 9020 case OMPC_lastprivate: 9021 case OMPC_shared: 9022 case OMPC_reduction: 9023 case OMPC_task_reduction: 9024 case OMPC_in_reduction: 9025 case OMPC_linear: 9026 case OMPC_aligned: 9027 case OMPC_copyin: 9028 case OMPC_copyprivate: 9029 case OMPC_default: 9030 case OMPC_proc_bind: 9031 case OMPC_threadprivate: 9032 case OMPC_flush: 9033 case OMPC_depend: 9034 case OMPC_device: 9035 case OMPC_map: 9036 case OMPC_num_teams: 9037 case OMPC_thread_limit: 9038 case OMPC_priority: 9039 case OMPC_grainsize: 9040 case OMPC_num_tasks: 9041 case OMPC_hint: 9042 case OMPC_dist_schedule: 9043 case OMPC_defaultmap: 9044 case OMPC_unknown: 9045 case OMPC_uniform: 9046 case OMPC_to: 9047 case OMPC_from: 9048 case OMPC_use_device_ptr: 9049 case OMPC_is_device_ptr: 9050 llvm_unreachable("Clause is not allowed."); 9051 } 9052 return Res; 9053 } 9054 9055 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 9056 SourceLocation EndLoc) { 9057 DSAStack->setNowaitRegion(); 9058 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 9059 } 9060 9061 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 9062 SourceLocation EndLoc) { 9063 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 9064 } 9065 9066 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 9067 SourceLocation EndLoc) { 9068 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 9069 } 9070 9071 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 9072 SourceLocation EndLoc) { 9073 return new (Context) OMPReadClause(StartLoc, EndLoc); 9074 } 9075 9076 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 9077 SourceLocation EndLoc) { 9078 return new (Context) OMPWriteClause(StartLoc, EndLoc); 9079 } 9080 9081 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 9082 SourceLocation EndLoc) { 9083 return new (Context) OMPUpdateClause(StartLoc, EndLoc); 9084 } 9085 9086 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 9087 SourceLocation EndLoc) { 9088 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 9089 } 9090 9091 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 9092 SourceLocation EndLoc) { 9093 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 9094 } 9095 9096 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 9097 SourceLocation EndLoc) { 9098 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 9099 } 9100 9101 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 9102 SourceLocation EndLoc) { 9103 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 9104 } 9105 9106 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 9107 SourceLocation EndLoc) { 9108 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 9109 } 9110 9111 OMPClause *Sema::ActOnOpenMPVarListClause( 9112 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, 9113 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 9114 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec, 9115 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind, 9116 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier, 9117 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, 9118 SourceLocation DepLinMapLoc) { 9119 OMPClause *Res = nullptr; 9120 switch (Kind) { 9121 case OMPC_private: 9122 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9123 break; 9124 case OMPC_firstprivate: 9125 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9126 break; 9127 case OMPC_lastprivate: 9128 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9129 break; 9130 case OMPC_shared: 9131 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 9132 break; 9133 case OMPC_reduction: 9134 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 9135 EndLoc, ReductionIdScopeSpec, ReductionId); 9136 break; 9137 case OMPC_task_reduction: 9138 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 9139 EndLoc, ReductionIdScopeSpec, 9140 ReductionId); 9141 break; 9142 case OMPC_in_reduction: 9143 Res = 9144 ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 9145 EndLoc, ReductionIdScopeSpec, ReductionId); 9146 break; 9147 case OMPC_linear: 9148 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, 9149 LinKind, DepLinMapLoc, ColonLoc, EndLoc); 9150 break; 9151 case OMPC_aligned: 9152 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, 9153 ColonLoc, EndLoc); 9154 break; 9155 case OMPC_copyin: 9156 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 9157 break; 9158 case OMPC_copyprivate: 9159 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9160 break; 9161 case OMPC_flush: 9162 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 9163 break; 9164 case OMPC_depend: 9165 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList, 9166 StartLoc, LParenLoc, EndLoc); 9167 break; 9168 case OMPC_map: 9169 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit, 9170 DepLinMapLoc, ColonLoc, VarList, StartLoc, 9171 LParenLoc, EndLoc); 9172 break; 9173 case OMPC_to: 9174 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc); 9175 break; 9176 case OMPC_from: 9177 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc); 9178 break; 9179 case OMPC_use_device_ptr: 9180 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc); 9181 break; 9182 case OMPC_is_device_ptr: 9183 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc); 9184 break; 9185 case OMPC_if: 9186 case OMPC_final: 9187 case OMPC_num_threads: 9188 case OMPC_safelen: 9189 case OMPC_simdlen: 9190 case OMPC_collapse: 9191 case OMPC_default: 9192 case OMPC_proc_bind: 9193 case OMPC_schedule: 9194 case OMPC_ordered: 9195 case OMPC_nowait: 9196 case OMPC_untied: 9197 case OMPC_mergeable: 9198 case OMPC_threadprivate: 9199 case OMPC_read: 9200 case OMPC_write: 9201 case OMPC_update: 9202 case OMPC_capture: 9203 case OMPC_seq_cst: 9204 case OMPC_device: 9205 case OMPC_threads: 9206 case OMPC_simd: 9207 case OMPC_num_teams: 9208 case OMPC_thread_limit: 9209 case OMPC_priority: 9210 case OMPC_grainsize: 9211 case OMPC_nogroup: 9212 case OMPC_num_tasks: 9213 case OMPC_hint: 9214 case OMPC_dist_schedule: 9215 case OMPC_defaultmap: 9216 case OMPC_unknown: 9217 case OMPC_uniform: 9218 llvm_unreachable("Clause is not allowed."); 9219 } 9220 return Res; 9221 } 9222 9223 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 9224 ExprObjectKind OK, SourceLocation Loc) { 9225 ExprResult Res = BuildDeclRefExpr( 9226 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 9227 if (!Res.isUsable()) 9228 return ExprError(); 9229 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 9230 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 9231 if (!Res.isUsable()) 9232 return ExprError(); 9233 } 9234 if (VK != VK_LValue && Res.get()->isGLValue()) { 9235 Res = DefaultLvalueConversion(Res.get()); 9236 if (!Res.isUsable()) 9237 return ExprError(); 9238 } 9239 return Res; 9240 } 9241 9242 static std::pair<ValueDecl *, bool> 9243 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 9244 SourceRange &ERange, bool AllowArraySection = false) { 9245 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 9246 RefExpr->containsUnexpandedParameterPack()) 9247 return std::make_pair(nullptr, true); 9248 9249 // OpenMP [3.1, C/C++] 9250 // A list item is a variable name. 9251 // OpenMP [2.9.3.3, Restrictions, p.1] 9252 // A variable that is part of another variable (as an array or 9253 // structure element) cannot appear in a private clause. 9254 RefExpr = RefExpr->IgnoreParens(); 9255 enum { 9256 NoArrayExpr = -1, 9257 ArraySubscript = 0, 9258 OMPArraySection = 1 9259 } IsArrayExpr = NoArrayExpr; 9260 if (AllowArraySection) { 9261 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 9262 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 9263 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 9264 Base = TempASE->getBase()->IgnoreParenImpCasts(); 9265 RefExpr = Base; 9266 IsArrayExpr = ArraySubscript; 9267 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 9268 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 9269 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 9270 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 9271 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 9272 Base = TempASE->getBase()->IgnoreParenImpCasts(); 9273 RefExpr = Base; 9274 IsArrayExpr = OMPArraySection; 9275 } 9276 } 9277 ELoc = RefExpr->getExprLoc(); 9278 ERange = RefExpr->getSourceRange(); 9279 RefExpr = RefExpr->IgnoreParenImpCasts(); 9280 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 9281 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 9282 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 9283 (S.getCurrentThisType().isNull() || !ME || 9284 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 9285 !isa<FieldDecl>(ME->getMemberDecl()))) { 9286 if (IsArrayExpr != NoArrayExpr) { 9287 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 9288 << ERange; 9289 } else { 9290 S.Diag(ELoc, 9291 AllowArraySection 9292 ? diag::err_omp_expected_var_name_member_expr_or_array_item 9293 : diag::err_omp_expected_var_name_member_expr) 9294 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 9295 } 9296 return std::make_pair(nullptr, false); 9297 } 9298 return std::make_pair( 9299 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 9300 } 9301 9302 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 9303 SourceLocation StartLoc, 9304 SourceLocation LParenLoc, 9305 SourceLocation EndLoc) { 9306 SmallVector<Expr *, 8> Vars; 9307 SmallVector<Expr *, 8> PrivateCopies; 9308 for (Expr *RefExpr : VarList) { 9309 assert(RefExpr && "NULL expr in OpenMP private clause."); 9310 SourceLocation ELoc; 9311 SourceRange ERange; 9312 Expr *SimpleRefExpr = RefExpr; 9313 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 9314 if (Res.second) { 9315 // It will be analyzed later. 9316 Vars.push_back(RefExpr); 9317 PrivateCopies.push_back(nullptr); 9318 } 9319 ValueDecl *D = Res.first; 9320 if (!D) 9321 continue; 9322 9323 QualType Type = D->getType(); 9324 auto *VD = dyn_cast<VarDecl>(D); 9325 9326 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 9327 // A variable that appears in a private clause must not have an incomplete 9328 // type or a reference type. 9329 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 9330 continue; 9331 Type = Type.getNonReferenceType(); 9332 9333 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 9334 // in a Construct] 9335 // Variables with the predetermined data-sharing attributes may not be 9336 // listed in data-sharing attributes clauses, except for the cases 9337 // listed below. For these exceptions only, listing a predetermined 9338 // variable in a data-sharing attribute clause is allowed and overrides 9339 // the variable's predetermined data-sharing attributes. 9340 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 9341 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 9342 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 9343 << getOpenMPClauseName(OMPC_private); 9344 reportOriginalDsa(*this, DSAStack, D, DVar); 9345 continue; 9346 } 9347 9348 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 9349 // Variably modified types are not supported for tasks. 9350 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 9351 isOpenMPTaskingDirective(CurrDir)) { 9352 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 9353 << getOpenMPClauseName(OMPC_private) << Type 9354 << getOpenMPDirectiveName(CurrDir); 9355 bool IsDecl = 9356 !VD || 9357 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 9358 Diag(D->getLocation(), 9359 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 9360 << D; 9361 continue; 9362 } 9363 9364 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 9365 // A list item cannot appear in both a map clause and a data-sharing 9366 // attribute clause on the same construct 9367 if (isOpenMPTargetExecutionDirective(CurrDir)) { 9368 OpenMPClauseKind ConflictKind; 9369 if (DSAStack->checkMappableExprComponentListsForDecl( 9370 VD, /*CurrentRegionOnly=*/true, 9371 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 9372 OpenMPClauseKind WhereFoundClauseKind) -> bool { 9373 ConflictKind = WhereFoundClauseKind; 9374 return true; 9375 })) { 9376 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 9377 << getOpenMPClauseName(OMPC_private) 9378 << getOpenMPClauseName(ConflictKind) 9379 << getOpenMPDirectiveName(CurrDir); 9380 reportOriginalDsa(*this, DSAStack, D, DVar); 9381 continue; 9382 } 9383 } 9384 9385 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 9386 // A variable of class type (or array thereof) that appears in a private 9387 // clause requires an accessible, unambiguous default constructor for the 9388 // class type. 9389 // Generate helper private variable and initialize it with the default 9390 // value. The address of the original variable is replaced by the address of 9391 // the new private variable in CodeGen. This new variable is not added to 9392 // IdResolver, so the code in the OpenMP region uses original variable for 9393 // proper diagnostics. 9394 Type = Type.getUnqualifiedType(); 9395 VarDecl *VDPrivate = 9396 buildVarDecl(*this, ELoc, Type, D->getName(), 9397 D->hasAttrs() ? &D->getAttrs() : nullptr, 9398 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 9399 ActOnUninitializedDecl(VDPrivate); 9400 if (VDPrivate->isInvalidDecl()) 9401 continue; 9402 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 9403 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 9404 9405 DeclRefExpr *Ref = nullptr; 9406 if (!VD && !CurContext->isDependentContext()) 9407 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 9408 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 9409 Vars.push_back((VD || CurContext->isDependentContext()) 9410 ? RefExpr->IgnoreParens() 9411 : Ref); 9412 PrivateCopies.push_back(VDPrivateRefExpr); 9413 } 9414 9415 if (Vars.empty()) 9416 return nullptr; 9417 9418 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 9419 PrivateCopies); 9420 } 9421 9422 namespace { 9423 class DiagsUninitializedSeveretyRAII { 9424 private: 9425 DiagnosticsEngine &Diags; 9426 SourceLocation SavedLoc; 9427 bool IsIgnored = false; 9428 9429 public: 9430 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 9431 bool IsIgnored) 9432 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 9433 if (!IsIgnored) { 9434 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 9435 /*Map*/ diag::Severity::Ignored, Loc); 9436 } 9437 } 9438 ~DiagsUninitializedSeveretyRAII() { 9439 if (!IsIgnored) 9440 Diags.popMappings(SavedLoc); 9441 } 9442 }; 9443 } 9444 9445 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 9446 SourceLocation StartLoc, 9447 SourceLocation LParenLoc, 9448 SourceLocation EndLoc) { 9449 SmallVector<Expr *, 8> Vars; 9450 SmallVector<Expr *, 8> PrivateCopies; 9451 SmallVector<Expr *, 8> Inits; 9452 SmallVector<Decl *, 4> ExprCaptures; 9453 bool IsImplicitClause = 9454 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 9455 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 9456 9457 for (Expr *RefExpr : VarList) { 9458 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 9459 SourceLocation ELoc; 9460 SourceRange ERange; 9461 Expr *SimpleRefExpr = RefExpr; 9462 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 9463 if (Res.second) { 9464 // It will be analyzed later. 9465 Vars.push_back(RefExpr); 9466 PrivateCopies.push_back(nullptr); 9467 Inits.push_back(nullptr); 9468 } 9469 ValueDecl *D = Res.first; 9470 if (!D) 9471 continue; 9472 9473 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 9474 QualType Type = D->getType(); 9475 auto *VD = dyn_cast<VarDecl>(D); 9476 9477 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 9478 // A variable that appears in a private clause must not have an incomplete 9479 // type or a reference type. 9480 if (RequireCompleteType(ELoc, Type, 9481 diag::err_omp_firstprivate_incomplete_type)) 9482 continue; 9483 Type = Type.getNonReferenceType(); 9484 9485 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 9486 // A variable of class type (or array thereof) that appears in a private 9487 // clause requires an accessible, unambiguous copy constructor for the 9488 // class type. 9489 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 9490 9491 // If an implicit firstprivate variable found it was checked already. 9492 DSAStackTy::DSAVarData TopDVar; 9493 if (!IsImplicitClause) { 9494 DSAStackTy::DSAVarData DVar = 9495 DSAStack->getTopDSA(D, /*FromParent=*/false); 9496 TopDVar = DVar; 9497 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 9498 bool IsConstant = ElemType.isConstant(Context); 9499 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 9500 // A list item that specifies a given variable may not appear in more 9501 // than one clause on the same directive, except that a variable may be 9502 // specified in both firstprivate and lastprivate clauses. 9503 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 9504 // A list item may appear in a firstprivate or lastprivate clause but not 9505 // both. 9506 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 9507 (isOpenMPDistributeDirective(CurrDir) || 9508 DVar.CKind != OMPC_lastprivate) && 9509 DVar.RefExpr) { 9510 Diag(ELoc, diag::err_omp_wrong_dsa) 9511 << getOpenMPClauseName(DVar.CKind) 9512 << getOpenMPClauseName(OMPC_firstprivate); 9513 reportOriginalDsa(*this, DSAStack, D, DVar); 9514 continue; 9515 } 9516 9517 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 9518 // in a Construct] 9519 // Variables with the predetermined data-sharing attributes may not be 9520 // listed in data-sharing attributes clauses, except for the cases 9521 // listed below. For these exceptions only, listing a predetermined 9522 // variable in a data-sharing attribute clause is allowed and overrides 9523 // the variable's predetermined data-sharing attributes. 9524 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 9525 // in a Construct, C/C++, p.2] 9526 // Variables with const-qualified type having no mutable member may be 9527 // listed in a firstprivate clause, even if they are static data members. 9528 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 9529 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 9530 Diag(ELoc, diag::err_omp_wrong_dsa) 9531 << getOpenMPClauseName(DVar.CKind) 9532 << getOpenMPClauseName(OMPC_firstprivate); 9533 reportOriginalDsa(*this, DSAStack, D, DVar); 9534 continue; 9535 } 9536 9537 // OpenMP [2.9.3.4, Restrictions, p.2] 9538 // A list item that is private within a parallel region must not appear 9539 // in a firstprivate clause on a worksharing construct if any of the 9540 // worksharing regions arising from the worksharing construct ever bind 9541 // to any of the parallel regions arising from the parallel construct. 9542 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 9543 // A list item that is private within a teams region must not appear in a 9544 // firstprivate clause on a distribute construct if any of the distribute 9545 // regions arising from the distribute construct ever bind to any of the 9546 // teams regions arising from the teams construct. 9547 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 9548 // A list item that appears in a reduction clause of a teams construct 9549 // must not appear in a firstprivate clause on a distribute construct if 9550 // any of the distribute regions arising from the distribute construct 9551 // ever bind to any of the teams regions arising from the teams construct. 9552 if ((isOpenMPWorksharingDirective(CurrDir) || 9553 isOpenMPDistributeDirective(CurrDir)) && 9554 !isOpenMPParallelDirective(CurrDir) && 9555 !isOpenMPTeamsDirective(CurrDir)) { 9556 DVar = DSAStack->getImplicitDSA(D, true); 9557 if (DVar.CKind != OMPC_shared && 9558 (isOpenMPParallelDirective(DVar.DKind) || 9559 isOpenMPTeamsDirective(DVar.DKind) || 9560 DVar.DKind == OMPD_unknown)) { 9561 Diag(ELoc, diag::err_omp_required_access) 9562 << getOpenMPClauseName(OMPC_firstprivate) 9563 << getOpenMPClauseName(OMPC_shared); 9564 reportOriginalDsa(*this, DSAStack, D, DVar); 9565 continue; 9566 } 9567 } 9568 // OpenMP [2.9.3.4, Restrictions, p.3] 9569 // A list item that appears in a reduction clause of a parallel construct 9570 // must not appear in a firstprivate clause on a worksharing or task 9571 // construct if any of the worksharing or task regions arising from the 9572 // worksharing or task construct ever bind to any of the parallel regions 9573 // arising from the parallel construct. 9574 // OpenMP [2.9.3.4, Restrictions, p.4] 9575 // A list item that appears in a reduction clause in worksharing 9576 // construct must not appear in a firstprivate clause in a task construct 9577 // encountered during execution of any of the worksharing regions arising 9578 // from the worksharing construct. 9579 if (isOpenMPTaskingDirective(CurrDir)) { 9580 DVar = DSAStack->hasInnermostDSA( 9581 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 9582 [](OpenMPDirectiveKind K) { 9583 return isOpenMPParallelDirective(K) || 9584 isOpenMPWorksharingDirective(K) || 9585 isOpenMPTeamsDirective(K); 9586 }, 9587 /*FromParent=*/true); 9588 if (DVar.CKind == OMPC_reduction && 9589 (isOpenMPParallelDirective(DVar.DKind) || 9590 isOpenMPWorksharingDirective(DVar.DKind) || 9591 isOpenMPTeamsDirective(DVar.DKind))) { 9592 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 9593 << getOpenMPDirectiveName(DVar.DKind); 9594 reportOriginalDsa(*this, DSAStack, D, DVar); 9595 continue; 9596 } 9597 } 9598 9599 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 9600 // A list item cannot appear in both a map clause and a data-sharing 9601 // attribute clause on the same construct 9602 if (isOpenMPTargetExecutionDirective(CurrDir)) { 9603 OpenMPClauseKind ConflictKind; 9604 if (DSAStack->checkMappableExprComponentListsForDecl( 9605 VD, /*CurrentRegionOnly=*/true, 9606 [&ConflictKind]( 9607 OMPClauseMappableExprCommon::MappableExprComponentListRef, 9608 OpenMPClauseKind WhereFoundClauseKind) { 9609 ConflictKind = WhereFoundClauseKind; 9610 return true; 9611 })) { 9612 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 9613 << getOpenMPClauseName(OMPC_firstprivate) 9614 << getOpenMPClauseName(ConflictKind) 9615 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 9616 reportOriginalDsa(*this, DSAStack, D, DVar); 9617 continue; 9618 } 9619 } 9620 } 9621 9622 // Variably modified types are not supported for tasks. 9623 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 9624 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 9625 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 9626 << getOpenMPClauseName(OMPC_firstprivate) << Type 9627 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 9628 bool IsDecl = 9629 !VD || 9630 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 9631 Diag(D->getLocation(), 9632 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 9633 << D; 9634 continue; 9635 } 9636 9637 Type = Type.getUnqualifiedType(); 9638 VarDecl *VDPrivate = 9639 buildVarDecl(*this, ELoc, Type, D->getName(), 9640 D->hasAttrs() ? &D->getAttrs() : nullptr, 9641 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 9642 // Generate helper private variable and initialize it with the value of the 9643 // original variable. The address of the original variable is replaced by 9644 // the address of the new private variable in the CodeGen. This new variable 9645 // is not added to IdResolver, so the code in the OpenMP region uses 9646 // original variable for proper diagnostics and variable capturing. 9647 Expr *VDInitRefExpr = nullptr; 9648 // For arrays generate initializer for single element and replace it by the 9649 // original array element in CodeGen. 9650 if (Type->isArrayType()) { 9651 VarDecl *VDInit = 9652 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 9653 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 9654 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 9655 ElemType = ElemType.getUnqualifiedType(); 9656 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 9657 ".firstprivate.temp"); 9658 InitializedEntity Entity = 9659 InitializedEntity::InitializeVariable(VDInitTemp); 9660 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 9661 9662 InitializationSequence InitSeq(*this, Entity, Kind, Init); 9663 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 9664 if (Result.isInvalid()) 9665 VDPrivate->setInvalidDecl(); 9666 else 9667 VDPrivate->setInit(Result.getAs<Expr>()); 9668 // Remove temp variable declaration. 9669 Context.Deallocate(VDInitTemp); 9670 } else { 9671 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 9672 ".firstprivate.temp"); 9673 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 9674 RefExpr->getExprLoc()); 9675 AddInitializerToDecl(VDPrivate, 9676 DefaultLvalueConversion(VDInitRefExpr).get(), 9677 /*DirectInit=*/false); 9678 } 9679 if (VDPrivate->isInvalidDecl()) { 9680 if (IsImplicitClause) { 9681 Diag(RefExpr->getExprLoc(), 9682 diag::note_omp_task_predetermined_firstprivate_here); 9683 } 9684 continue; 9685 } 9686 CurContext->addDecl(VDPrivate); 9687 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 9688 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 9689 RefExpr->getExprLoc()); 9690 DeclRefExpr *Ref = nullptr; 9691 if (!VD && !CurContext->isDependentContext()) { 9692 if (TopDVar.CKind == OMPC_lastprivate) { 9693 Ref = TopDVar.PrivateCopy; 9694 } else { 9695 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 9696 if (!isOpenMPCapturedDecl(D)) 9697 ExprCaptures.push_back(Ref->getDecl()); 9698 } 9699 } 9700 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 9701 Vars.push_back((VD || CurContext->isDependentContext()) 9702 ? RefExpr->IgnoreParens() 9703 : Ref); 9704 PrivateCopies.push_back(VDPrivateRefExpr); 9705 Inits.push_back(VDInitRefExpr); 9706 } 9707 9708 if (Vars.empty()) 9709 return nullptr; 9710 9711 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 9712 Vars, PrivateCopies, Inits, 9713 buildPreInits(Context, ExprCaptures)); 9714 } 9715 9716 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList, 9717 SourceLocation StartLoc, 9718 SourceLocation LParenLoc, 9719 SourceLocation EndLoc) { 9720 SmallVector<Expr *, 8> Vars; 9721 SmallVector<Expr *, 8> SrcExprs; 9722 SmallVector<Expr *, 8> DstExprs; 9723 SmallVector<Expr *, 8> AssignmentOps; 9724 SmallVector<Decl *, 4> ExprCaptures; 9725 SmallVector<Expr *, 4> ExprPostUpdates; 9726 for (Expr *RefExpr : VarList) { 9727 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 9728 SourceLocation ELoc; 9729 SourceRange ERange; 9730 Expr *SimpleRefExpr = RefExpr; 9731 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 9732 if (Res.second) { 9733 // It will be analyzed later. 9734 Vars.push_back(RefExpr); 9735 SrcExprs.push_back(nullptr); 9736 DstExprs.push_back(nullptr); 9737 AssignmentOps.push_back(nullptr); 9738 } 9739 ValueDecl *D = Res.first; 9740 if (!D) 9741 continue; 9742 9743 QualType Type = D->getType(); 9744 auto *VD = dyn_cast<VarDecl>(D); 9745 9746 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 9747 // A variable that appears in a lastprivate clause must not have an 9748 // incomplete type or a reference type. 9749 if (RequireCompleteType(ELoc, Type, 9750 diag::err_omp_lastprivate_incomplete_type)) 9751 continue; 9752 Type = Type.getNonReferenceType(); 9753 9754 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 9755 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 9756 // in a Construct] 9757 // Variables with the predetermined data-sharing attributes may not be 9758 // listed in data-sharing attributes clauses, except for the cases 9759 // listed below. 9760 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 9761 // A list item may appear in a firstprivate or lastprivate clause but not 9762 // both. 9763 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 9764 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 9765 (isOpenMPDistributeDirective(CurrDir) || 9766 DVar.CKind != OMPC_firstprivate) && 9767 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 9768 Diag(ELoc, diag::err_omp_wrong_dsa) 9769 << getOpenMPClauseName(DVar.CKind) 9770 << getOpenMPClauseName(OMPC_lastprivate); 9771 reportOriginalDsa(*this, DSAStack, D, DVar); 9772 continue; 9773 } 9774 9775 // OpenMP [2.14.3.5, Restrictions, p.2] 9776 // A list item that is private within a parallel region, or that appears in 9777 // the reduction clause of a parallel construct, must not appear in a 9778 // lastprivate clause on a worksharing construct if any of the corresponding 9779 // worksharing regions ever binds to any of the corresponding parallel 9780 // regions. 9781 DSAStackTy::DSAVarData TopDVar = DVar; 9782 if (isOpenMPWorksharingDirective(CurrDir) && 9783 !isOpenMPParallelDirective(CurrDir) && 9784 !isOpenMPTeamsDirective(CurrDir)) { 9785 DVar = DSAStack->getImplicitDSA(D, true); 9786 if (DVar.CKind != OMPC_shared) { 9787 Diag(ELoc, diag::err_omp_required_access) 9788 << getOpenMPClauseName(OMPC_lastprivate) 9789 << getOpenMPClauseName(OMPC_shared); 9790 reportOriginalDsa(*this, DSAStack, D, DVar); 9791 continue; 9792 } 9793 } 9794 9795 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 9796 // A variable of class type (or array thereof) that appears in a 9797 // lastprivate clause requires an accessible, unambiguous default 9798 // constructor for the class type, unless the list item is also specified 9799 // in a firstprivate clause. 9800 // A variable of class type (or array thereof) that appears in a 9801 // lastprivate clause requires an accessible, unambiguous copy assignment 9802 // operator for the class type. 9803 Type = Context.getBaseElementType(Type).getNonReferenceType(); 9804 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 9805 Type.getUnqualifiedType(), ".lastprivate.src", 9806 D->hasAttrs() ? &D->getAttrs() : nullptr); 9807 DeclRefExpr *PseudoSrcExpr = 9808 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 9809 VarDecl *DstVD = 9810 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 9811 D->hasAttrs() ? &D->getAttrs() : nullptr); 9812 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 9813 // For arrays generate assignment operation for single element and replace 9814 // it by the original array element in CodeGen. 9815 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 9816 PseudoDstExpr, PseudoSrcExpr); 9817 if (AssignmentOp.isInvalid()) 9818 continue; 9819 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc, 9820 /*DiscardedValue=*/true); 9821 if (AssignmentOp.isInvalid()) 9822 continue; 9823 9824 DeclRefExpr *Ref = nullptr; 9825 if (!VD && !CurContext->isDependentContext()) { 9826 if (TopDVar.CKind == OMPC_firstprivate) { 9827 Ref = TopDVar.PrivateCopy; 9828 } else { 9829 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 9830 if (!isOpenMPCapturedDecl(D)) 9831 ExprCaptures.push_back(Ref->getDecl()); 9832 } 9833 if (TopDVar.CKind == OMPC_firstprivate || 9834 (!isOpenMPCapturedDecl(D) && 9835 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 9836 ExprResult RefRes = DefaultLvalueConversion(Ref); 9837 if (!RefRes.isUsable()) 9838 continue; 9839 ExprResult PostUpdateRes = 9840 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 9841 RefRes.get()); 9842 if (!PostUpdateRes.isUsable()) 9843 continue; 9844 ExprPostUpdates.push_back( 9845 IgnoredValueConversions(PostUpdateRes.get()).get()); 9846 } 9847 } 9848 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 9849 Vars.push_back((VD || CurContext->isDependentContext()) 9850 ? RefExpr->IgnoreParens() 9851 : Ref); 9852 SrcExprs.push_back(PseudoSrcExpr); 9853 DstExprs.push_back(PseudoDstExpr); 9854 AssignmentOps.push_back(AssignmentOp.get()); 9855 } 9856 9857 if (Vars.empty()) 9858 return nullptr; 9859 9860 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 9861 Vars, SrcExprs, DstExprs, AssignmentOps, 9862 buildPreInits(Context, ExprCaptures), 9863 buildPostUpdate(*this, ExprPostUpdates)); 9864 } 9865 9866 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 9867 SourceLocation StartLoc, 9868 SourceLocation LParenLoc, 9869 SourceLocation EndLoc) { 9870 SmallVector<Expr *, 8> Vars; 9871 for (Expr *RefExpr : VarList) { 9872 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 9873 SourceLocation ELoc; 9874 SourceRange ERange; 9875 Expr *SimpleRefExpr = RefExpr; 9876 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 9877 if (Res.second) { 9878 // It will be analyzed later. 9879 Vars.push_back(RefExpr); 9880 } 9881 ValueDecl *D = Res.first; 9882 if (!D) 9883 continue; 9884 9885 auto *VD = dyn_cast<VarDecl>(D); 9886 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 9887 // in a Construct] 9888 // Variables with the predetermined data-sharing attributes may not be 9889 // listed in data-sharing attributes clauses, except for the cases 9890 // listed below. For these exceptions only, listing a predetermined 9891 // variable in a data-sharing attribute clause is allowed and overrides 9892 // the variable's predetermined data-sharing attributes. 9893 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 9894 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 9895 DVar.RefExpr) { 9896 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 9897 << getOpenMPClauseName(OMPC_shared); 9898 reportOriginalDsa(*this, DSAStack, D, DVar); 9899 continue; 9900 } 9901 9902 DeclRefExpr *Ref = nullptr; 9903 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 9904 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 9905 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 9906 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 9907 ? RefExpr->IgnoreParens() 9908 : Ref); 9909 } 9910 9911 if (Vars.empty()) 9912 return nullptr; 9913 9914 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 9915 } 9916 9917 namespace { 9918 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 9919 DSAStackTy *Stack; 9920 9921 public: 9922 bool VisitDeclRefExpr(DeclRefExpr *E) { 9923 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 9924 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 9925 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 9926 return false; 9927 if (DVar.CKind != OMPC_unknown) 9928 return true; 9929 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 9930 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; }, 9931 /*FromParent=*/true); 9932 return DVarPrivate.CKind != OMPC_unknown; 9933 } 9934 return false; 9935 } 9936 bool VisitStmt(Stmt *S) { 9937 for (Stmt *Child : S->children()) { 9938 if (Child && Visit(Child)) 9939 return true; 9940 } 9941 return false; 9942 } 9943 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 9944 }; 9945 } // namespace 9946 9947 namespace { 9948 // Transform MemberExpression for specified FieldDecl of current class to 9949 // DeclRefExpr to specified OMPCapturedExprDecl. 9950 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 9951 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 9952 ValueDecl *Field = nullptr; 9953 DeclRefExpr *CapturedExpr = nullptr; 9954 9955 public: 9956 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 9957 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 9958 9959 ExprResult TransformMemberExpr(MemberExpr *E) { 9960 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 9961 E->getMemberDecl() == Field) { 9962 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 9963 return CapturedExpr; 9964 } 9965 return BaseTransform::TransformMemberExpr(E); 9966 } 9967 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 9968 }; 9969 } // namespace 9970 9971 template <typename T, typename U> 9972 static T filterLookupForUDR(SmallVectorImpl<U> &Lookups, 9973 const llvm::function_ref<T(ValueDecl *)> Gen) { 9974 for (U &Set : Lookups) { 9975 for (auto *D : Set) { 9976 if (T Res = Gen(cast<ValueDecl>(D))) 9977 return Res; 9978 } 9979 } 9980 return T(); 9981 } 9982 9983 static ExprResult 9984 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 9985 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 9986 const DeclarationNameInfo &ReductionId, QualType Ty, 9987 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 9988 if (ReductionIdScopeSpec.isInvalid()) 9989 return ExprError(); 9990 SmallVector<UnresolvedSet<8>, 4> Lookups; 9991 if (S) { 9992 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 9993 Lookup.suppressDiagnostics(); 9994 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 9995 NamedDecl *D = Lookup.getRepresentativeDecl(); 9996 do { 9997 S = S->getParent(); 9998 } while (S && !S->isDeclScope(D)); 9999 if (S) 10000 S = S->getParent(); 10001 Lookups.push_back(UnresolvedSet<8>()); 10002 Lookups.back().append(Lookup.begin(), Lookup.end()); 10003 Lookup.clear(); 10004 } 10005 } else if (auto *ULE = 10006 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 10007 Lookups.push_back(UnresolvedSet<8>()); 10008 Decl *PrevD = nullptr; 10009 for (NamedDecl *D : ULE->decls()) { 10010 if (D == PrevD) 10011 Lookups.push_back(UnresolvedSet<8>()); 10012 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D)) 10013 Lookups.back().addDecl(DRD); 10014 PrevD = D; 10015 } 10016 } 10017 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 10018 Ty->isInstantiationDependentType() || 10019 Ty->containsUnexpandedParameterPack() || 10020 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) { 10021 return !D->isInvalidDecl() && 10022 (D->getType()->isDependentType() || 10023 D->getType()->isInstantiationDependentType() || 10024 D->getType()->containsUnexpandedParameterPack()); 10025 })) { 10026 UnresolvedSet<8> ResSet; 10027 for (const UnresolvedSet<8> &Set : Lookups) { 10028 ResSet.append(Set.begin(), Set.end()); 10029 // The last item marks the end of all declarations at the specified scope. 10030 ResSet.addDecl(Set[Set.size() - 1]); 10031 } 10032 return UnresolvedLookupExpr::Create( 10033 SemaRef.Context, /*NamingClass=*/nullptr, 10034 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 10035 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 10036 } 10037 if (auto *VD = filterLookupForUDR<ValueDecl *>( 10038 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 10039 if (!D->isInvalidDecl() && 10040 SemaRef.Context.hasSameType(D->getType(), Ty)) 10041 return D; 10042 return nullptr; 10043 })) 10044 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc); 10045 if (auto *VD = filterLookupForUDR<ValueDecl *>( 10046 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 10047 if (!D->isInvalidDecl() && 10048 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 10049 !Ty.isMoreQualifiedThan(D->getType())) 10050 return D; 10051 return nullptr; 10052 })) { 10053 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 10054 /*DetectVirtual=*/false); 10055 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 10056 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 10057 VD->getType().getUnqualifiedType()))) { 10058 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(), 10059 /*DiagID=*/0) != 10060 Sema::AR_inaccessible) { 10061 SemaRef.BuildBasePathArray(Paths, BasePath); 10062 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc); 10063 } 10064 } 10065 } 10066 } 10067 if (ReductionIdScopeSpec.isSet()) { 10068 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range; 10069 return ExprError(); 10070 } 10071 return ExprEmpty(); 10072 } 10073 10074 namespace { 10075 /// Data for the reduction-based clauses. 10076 struct ReductionData { 10077 /// List of original reduction items. 10078 SmallVector<Expr *, 8> Vars; 10079 /// List of private copies of the reduction items. 10080 SmallVector<Expr *, 8> Privates; 10081 /// LHS expressions for the reduction_op expressions. 10082 SmallVector<Expr *, 8> LHSs; 10083 /// RHS expressions for the reduction_op expressions. 10084 SmallVector<Expr *, 8> RHSs; 10085 /// Reduction operation expression. 10086 SmallVector<Expr *, 8> ReductionOps; 10087 /// Taskgroup descriptors for the corresponding reduction items in 10088 /// in_reduction clauses. 10089 SmallVector<Expr *, 8> TaskgroupDescriptors; 10090 /// List of captures for clause. 10091 SmallVector<Decl *, 4> ExprCaptures; 10092 /// List of postupdate expressions. 10093 SmallVector<Expr *, 4> ExprPostUpdates; 10094 ReductionData() = delete; 10095 /// Reserves required memory for the reduction data. 10096 ReductionData(unsigned Size) { 10097 Vars.reserve(Size); 10098 Privates.reserve(Size); 10099 LHSs.reserve(Size); 10100 RHSs.reserve(Size); 10101 ReductionOps.reserve(Size); 10102 TaskgroupDescriptors.reserve(Size); 10103 ExprCaptures.reserve(Size); 10104 ExprPostUpdates.reserve(Size); 10105 } 10106 /// Stores reduction item and reduction operation only (required for dependent 10107 /// reduction item). 10108 void push(Expr *Item, Expr *ReductionOp) { 10109 Vars.emplace_back(Item); 10110 Privates.emplace_back(nullptr); 10111 LHSs.emplace_back(nullptr); 10112 RHSs.emplace_back(nullptr); 10113 ReductionOps.emplace_back(ReductionOp); 10114 TaskgroupDescriptors.emplace_back(nullptr); 10115 } 10116 /// Stores reduction data. 10117 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 10118 Expr *TaskgroupDescriptor) { 10119 Vars.emplace_back(Item); 10120 Privates.emplace_back(Private); 10121 LHSs.emplace_back(LHS); 10122 RHSs.emplace_back(RHS); 10123 ReductionOps.emplace_back(ReductionOp); 10124 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 10125 } 10126 }; 10127 } // namespace 10128 10129 static bool checkOMPArraySectionConstantForReduction( 10130 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 10131 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 10132 const Expr *Length = OASE->getLength(); 10133 if (Length == nullptr) { 10134 // For array sections of the form [1:] or [:], we would need to analyze 10135 // the lower bound... 10136 if (OASE->getColonLoc().isValid()) 10137 return false; 10138 10139 // This is an array subscript which has implicit length 1! 10140 SingleElement = true; 10141 ArraySizes.push_back(llvm::APSInt::get(1)); 10142 } else { 10143 llvm::APSInt ConstantLengthValue; 10144 if (!Length->EvaluateAsInt(ConstantLengthValue, Context)) 10145 return false; 10146 10147 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 10148 ArraySizes.push_back(ConstantLengthValue); 10149 } 10150 10151 // Get the base of this array section and walk up from there. 10152 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 10153 10154 // We require length = 1 for all array sections except the right-most to 10155 // guarantee that the memory region is contiguous and has no holes in it. 10156 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 10157 Length = TempOASE->getLength(); 10158 if (Length == nullptr) { 10159 // For array sections of the form [1:] or [:], we would need to analyze 10160 // the lower bound... 10161 if (OASE->getColonLoc().isValid()) 10162 return false; 10163 10164 // This is an array subscript which has implicit length 1! 10165 ArraySizes.push_back(llvm::APSInt::get(1)); 10166 } else { 10167 llvm::APSInt ConstantLengthValue; 10168 if (!Length->EvaluateAsInt(ConstantLengthValue, Context) || 10169 ConstantLengthValue.getSExtValue() != 1) 10170 return false; 10171 10172 ArraySizes.push_back(ConstantLengthValue); 10173 } 10174 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 10175 } 10176 10177 // If we have a single element, we don't need to add the implicit lengths. 10178 if (!SingleElement) { 10179 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 10180 // Has implicit length 1! 10181 ArraySizes.push_back(llvm::APSInt::get(1)); 10182 Base = TempASE->getBase()->IgnoreParenImpCasts(); 10183 } 10184 } 10185 10186 // This array section can be privatized as a single value or as a constant 10187 // sized array. 10188 return true; 10189 } 10190 10191 static bool actOnOMPReductionKindClause( 10192 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 10193 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 10194 SourceLocation ColonLoc, SourceLocation EndLoc, 10195 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 10196 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 10197 DeclarationName DN = ReductionId.getName(); 10198 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 10199 BinaryOperatorKind BOK = BO_Comma; 10200 10201 ASTContext &Context = S.Context; 10202 // OpenMP [2.14.3.6, reduction clause] 10203 // C 10204 // reduction-identifier is either an identifier or one of the following 10205 // operators: +, -, *, &, |, ^, && and || 10206 // C++ 10207 // reduction-identifier is either an id-expression or one of the following 10208 // operators: +, -, *, &, |, ^, && and || 10209 switch (OOK) { 10210 case OO_Plus: 10211 case OO_Minus: 10212 BOK = BO_Add; 10213 break; 10214 case OO_Star: 10215 BOK = BO_Mul; 10216 break; 10217 case OO_Amp: 10218 BOK = BO_And; 10219 break; 10220 case OO_Pipe: 10221 BOK = BO_Or; 10222 break; 10223 case OO_Caret: 10224 BOK = BO_Xor; 10225 break; 10226 case OO_AmpAmp: 10227 BOK = BO_LAnd; 10228 break; 10229 case OO_PipePipe: 10230 BOK = BO_LOr; 10231 break; 10232 case OO_New: 10233 case OO_Delete: 10234 case OO_Array_New: 10235 case OO_Array_Delete: 10236 case OO_Slash: 10237 case OO_Percent: 10238 case OO_Tilde: 10239 case OO_Exclaim: 10240 case OO_Equal: 10241 case OO_Less: 10242 case OO_Greater: 10243 case OO_LessEqual: 10244 case OO_GreaterEqual: 10245 case OO_PlusEqual: 10246 case OO_MinusEqual: 10247 case OO_StarEqual: 10248 case OO_SlashEqual: 10249 case OO_PercentEqual: 10250 case OO_CaretEqual: 10251 case OO_AmpEqual: 10252 case OO_PipeEqual: 10253 case OO_LessLess: 10254 case OO_GreaterGreater: 10255 case OO_LessLessEqual: 10256 case OO_GreaterGreaterEqual: 10257 case OO_EqualEqual: 10258 case OO_ExclaimEqual: 10259 case OO_Spaceship: 10260 case OO_PlusPlus: 10261 case OO_MinusMinus: 10262 case OO_Comma: 10263 case OO_ArrowStar: 10264 case OO_Arrow: 10265 case OO_Call: 10266 case OO_Subscript: 10267 case OO_Conditional: 10268 case OO_Coawait: 10269 case NUM_OVERLOADED_OPERATORS: 10270 llvm_unreachable("Unexpected reduction identifier"); 10271 case OO_None: 10272 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 10273 if (II->isStr("max")) 10274 BOK = BO_GT; 10275 else if (II->isStr("min")) 10276 BOK = BO_LT; 10277 } 10278 break; 10279 } 10280 SourceRange ReductionIdRange; 10281 if (ReductionIdScopeSpec.isValid()) 10282 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 10283 else 10284 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 10285 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 10286 10287 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 10288 bool FirstIter = true; 10289 for (Expr *RefExpr : VarList) { 10290 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 10291 // OpenMP [2.1, C/C++] 10292 // A list item is a variable or array section, subject to the restrictions 10293 // specified in Section 2.4 on page 42 and in each of the sections 10294 // describing clauses and directives for which a list appears. 10295 // OpenMP [2.14.3.3, Restrictions, p.1] 10296 // A variable that is part of another variable (as an array or 10297 // structure element) cannot appear in a private clause. 10298 if (!FirstIter && IR != ER) 10299 ++IR; 10300 FirstIter = false; 10301 SourceLocation ELoc; 10302 SourceRange ERange; 10303 Expr *SimpleRefExpr = RefExpr; 10304 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 10305 /*AllowArraySection=*/true); 10306 if (Res.second) { 10307 // Try to find 'declare reduction' corresponding construct before using 10308 // builtin/overloaded operators. 10309 QualType Type = Context.DependentTy; 10310 CXXCastPath BasePath; 10311 ExprResult DeclareReductionRef = buildDeclareReductionRef( 10312 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 10313 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 10314 Expr *ReductionOp = nullptr; 10315 if (S.CurContext->isDependentContext() && 10316 (DeclareReductionRef.isUnset() || 10317 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 10318 ReductionOp = DeclareReductionRef.get(); 10319 // It will be analyzed later. 10320 RD.push(RefExpr, ReductionOp); 10321 } 10322 ValueDecl *D = Res.first; 10323 if (!D) 10324 continue; 10325 10326 Expr *TaskgroupDescriptor = nullptr; 10327 QualType Type; 10328 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 10329 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 10330 if (ASE) { 10331 Type = ASE->getType().getNonReferenceType(); 10332 } else if (OASE) { 10333 QualType BaseType = 10334 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 10335 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 10336 Type = ATy->getElementType(); 10337 else 10338 Type = BaseType->getPointeeType(); 10339 Type = Type.getNonReferenceType(); 10340 } else { 10341 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 10342 } 10343 auto *VD = dyn_cast<VarDecl>(D); 10344 10345 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 10346 // A variable that appears in a private clause must not have an incomplete 10347 // type or a reference type. 10348 if (S.RequireCompleteType(ELoc, D->getType(), 10349 diag::err_omp_reduction_incomplete_type)) 10350 continue; 10351 // OpenMP [2.14.3.6, reduction clause, Restrictions] 10352 // A list item that appears in a reduction clause must not be 10353 // const-qualified. 10354 if (Type.getNonReferenceType().isConstant(Context)) { 10355 S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange; 10356 if (!ASE && !OASE) { 10357 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 10358 VarDecl::DeclarationOnly; 10359 S.Diag(D->getLocation(), 10360 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10361 << D; 10362 } 10363 continue; 10364 } 10365 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 10366 // If a list-item is a reference type then it must bind to the same object 10367 // for all threads of the team. 10368 if (!ASE && !OASE && VD) { 10369 VarDecl *VDDef = VD->getDefinition(); 10370 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 10371 DSARefChecker Check(Stack); 10372 if (Check.Visit(VDDef->getInit())) { 10373 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 10374 << getOpenMPClauseName(ClauseKind) << ERange; 10375 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 10376 continue; 10377 } 10378 } 10379 } 10380 10381 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 10382 // in a Construct] 10383 // Variables with the predetermined data-sharing attributes may not be 10384 // listed in data-sharing attributes clauses, except for the cases 10385 // listed below. For these exceptions only, listing a predetermined 10386 // variable in a data-sharing attribute clause is allowed and overrides 10387 // the variable's predetermined data-sharing attributes. 10388 // OpenMP [2.14.3.6, Restrictions, p.3] 10389 // Any number of reduction clauses can be specified on the directive, 10390 // but a list item can appear only once in the reduction clauses for that 10391 // directive. 10392 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 10393 if (DVar.CKind == OMPC_reduction) { 10394 S.Diag(ELoc, diag::err_omp_once_referenced) 10395 << getOpenMPClauseName(ClauseKind); 10396 if (DVar.RefExpr) 10397 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 10398 continue; 10399 } 10400 if (DVar.CKind != OMPC_unknown) { 10401 S.Diag(ELoc, diag::err_omp_wrong_dsa) 10402 << getOpenMPClauseName(DVar.CKind) 10403 << getOpenMPClauseName(OMPC_reduction); 10404 reportOriginalDsa(S, Stack, D, DVar); 10405 continue; 10406 } 10407 10408 // OpenMP [2.14.3.6, Restrictions, p.1] 10409 // A list item that appears in a reduction clause of a worksharing 10410 // construct must be shared in the parallel regions to which any of the 10411 // worksharing regions arising from the worksharing construct bind. 10412 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 10413 if (isOpenMPWorksharingDirective(CurrDir) && 10414 !isOpenMPParallelDirective(CurrDir) && 10415 !isOpenMPTeamsDirective(CurrDir)) { 10416 DVar = Stack->getImplicitDSA(D, true); 10417 if (DVar.CKind != OMPC_shared) { 10418 S.Diag(ELoc, diag::err_omp_required_access) 10419 << getOpenMPClauseName(OMPC_reduction) 10420 << getOpenMPClauseName(OMPC_shared); 10421 reportOriginalDsa(S, Stack, D, DVar); 10422 continue; 10423 } 10424 } 10425 10426 // Try to find 'declare reduction' corresponding construct before using 10427 // builtin/overloaded operators. 10428 CXXCastPath BasePath; 10429 ExprResult DeclareReductionRef = buildDeclareReductionRef( 10430 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 10431 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 10432 if (DeclareReductionRef.isInvalid()) 10433 continue; 10434 if (S.CurContext->isDependentContext() && 10435 (DeclareReductionRef.isUnset() || 10436 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 10437 RD.push(RefExpr, DeclareReductionRef.get()); 10438 continue; 10439 } 10440 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 10441 // Not allowed reduction identifier is found. 10442 S.Diag(ReductionId.getLocStart(), 10443 diag::err_omp_unknown_reduction_identifier) 10444 << Type << ReductionIdRange; 10445 continue; 10446 } 10447 10448 // OpenMP [2.14.3.6, reduction clause, Restrictions] 10449 // The type of a list item that appears in a reduction clause must be valid 10450 // for the reduction-identifier. For a max or min reduction in C, the type 10451 // of the list item must be an allowed arithmetic data type: char, int, 10452 // float, double, or _Bool, possibly modified with long, short, signed, or 10453 // unsigned. For a max or min reduction in C++, the type of the list item 10454 // must be an allowed arithmetic data type: char, wchar_t, int, float, 10455 // double, or bool, possibly modified with long, short, signed, or unsigned. 10456 if (DeclareReductionRef.isUnset()) { 10457 if ((BOK == BO_GT || BOK == BO_LT) && 10458 !(Type->isScalarType() || 10459 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 10460 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 10461 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 10462 if (!ASE && !OASE) { 10463 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 10464 VarDecl::DeclarationOnly; 10465 S.Diag(D->getLocation(), 10466 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10467 << D; 10468 } 10469 continue; 10470 } 10471 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 10472 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 10473 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 10474 << getOpenMPClauseName(ClauseKind); 10475 if (!ASE && !OASE) { 10476 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 10477 VarDecl::DeclarationOnly; 10478 S.Diag(D->getLocation(), 10479 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10480 << D; 10481 } 10482 continue; 10483 } 10484 } 10485 10486 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 10487 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 10488 D->hasAttrs() ? &D->getAttrs() : nullptr); 10489 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 10490 D->hasAttrs() ? &D->getAttrs() : nullptr); 10491 QualType PrivateTy = Type; 10492 10493 // Try if we can determine constant lengths for all array sections and avoid 10494 // the VLA. 10495 bool ConstantLengthOASE = false; 10496 if (OASE) { 10497 bool SingleElement; 10498 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 10499 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 10500 Context, OASE, SingleElement, ArraySizes); 10501 10502 // If we don't have a single element, we must emit a constant array type. 10503 if (ConstantLengthOASE && !SingleElement) { 10504 for (llvm::APSInt &Size : ArraySizes) 10505 PrivateTy = Context.getConstantArrayType( 10506 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0); 10507 } 10508 } 10509 10510 if ((OASE && !ConstantLengthOASE) || 10511 (!OASE && !ASE && 10512 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 10513 if (!Context.getTargetInfo().isVLASupported() && 10514 S.shouldDiagnoseTargetSupportFromOpenMP()) { 10515 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 10516 S.Diag(ELoc, diag::note_vla_unsupported); 10517 continue; 10518 } 10519 // For arrays/array sections only: 10520 // Create pseudo array type for private copy. The size for this array will 10521 // be generated during codegen. 10522 // For array subscripts or single variables Private Ty is the same as Type 10523 // (type of the variable or single array element). 10524 PrivateTy = Context.getVariableArrayType( 10525 Type, 10526 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue), 10527 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 10528 } else if (!ASE && !OASE && 10529 Context.getAsArrayType(D->getType().getNonReferenceType())) { 10530 PrivateTy = D->getType().getNonReferenceType(); 10531 } 10532 // Private copy. 10533 VarDecl *PrivateVD = 10534 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 10535 D->hasAttrs() ? &D->getAttrs() : nullptr, 10536 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 10537 // Add initializer for private variable. 10538 Expr *Init = nullptr; 10539 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 10540 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 10541 if (DeclareReductionRef.isUsable()) { 10542 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 10543 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 10544 if (DRD->getInitializer()) { 10545 Init = DRDRef; 10546 RHSVD->setInit(DRDRef); 10547 RHSVD->setInitStyle(VarDecl::CallInit); 10548 } 10549 } else { 10550 switch (BOK) { 10551 case BO_Add: 10552 case BO_Xor: 10553 case BO_Or: 10554 case BO_LOr: 10555 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 10556 if (Type->isScalarType() || Type->isAnyComplexType()) 10557 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 10558 break; 10559 case BO_Mul: 10560 case BO_LAnd: 10561 if (Type->isScalarType() || Type->isAnyComplexType()) { 10562 // '*' and '&&' reduction ops - initializer is '1'. 10563 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 10564 } 10565 break; 10566 case BO_And: { 10567 // '&' reduction op - initializer is '~0'. 10568 QualType OrigType = Type; 10569 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 10570 Type = ComplexTy->getElementType(); 10571 if (Type->isRealFloatingType()) { 10572 llvm::APFloat InitValue = 10573 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type), 10574 /*isIEEE=*/true); 10575 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 10576 Type, ELoc); 10577 } else if (Type->isScalarType()) { 10578 uint64_t Size = Context.getTypeSize(Type); 10579 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 10580 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 10581 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 10582 } 10583 if (Init && OrigType->isAnyComplexType()) { 10584 // Init = 0xFFFF + 0xFFFFi; 10585 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 10586 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 10587 } 10588 Type = OrigType; 10589 break; 10590 } 10591 case BO_LT: 10592 case BO_GT: { 10593 // 'min' reduction op - initializer is 'Largest representable number in 10594 // the reduction list item type'. 10595 // 'max' reduction op - initializer is 'Least representable number in 10596 // the reduction list item type'. 10597 if (Type->isIntegerType() || Type->isPointerType()) { 10598 bool IsSigned = Type->hasSignedIntegerRepresentation(); 10599 uint64_t Size = Context.getTypeSize(Type); 10600 QualType IntTy = 10601 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 10602 llvm::APInt InitValue = 10603 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 10604 : llvm::APInt::getMinValue(Size) 10605 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 10606 : llvm::APInt::getMaxValue(Size); 10607 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 10608 if (Type->isPointerType()) { 10609 // Cast to pointer type. 10610 ExprResult CastExpr = S.BuildCStyleCastExpr( 10611 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 10612 if (CastExpr.isInvalid()) 10613 continue; 10614 Init = CastExpr.get(); 10615 } 10616 } else if (Type->isRealFloatingType()) { 10617 llvm::APFloat InitValue = llvm::APFloat::getLargest( 10618 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 10619 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 10620 Type, ELoc); 10621 } 10622 break; 10623 } 10624 case BO_PtrMemD: 10625 case BO_PtrMemI: 10626 case BO_MulAssign: 10627 case BO_Div: 10628 case BO_Rem: 10629 case BO_Sub: 10630 case BO_Shl: 10631 case BO_Shr: 10632 case BO_LE: 10633 case BO_GE: 10634 case BO_EQ: 10635 case BO_NE: 10636 case BO_Cmp: 10637 case BO_AndAssign: 10638 case BO_XorAssign: 10639 case BO_OrAssign: 10640 case BO_Assign: 10641 case BO_AddAssign: 10642 case BO_SubAssign: 10643 case BO_DivAssign: 10644 case BO_RemAssign: 10645 case BO_ShlAssign: 10646 case BO_ShrAssign: 10647 case BO_Comma: 10648 llvm_unreachable("Unexpected reduction operation"); 10649 } 10650 } 10651 if (Init && DeclareReductionRef.isUnset()) 10652 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 10653 else if (!Init) 10654 S.ActOnUninitializedDecl(RHSVD); 10655 if (RHSVD->isInvalidDecl()) 10656 continue; 10657 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) { 10658 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 10659 << Type << ReductionIdRange; 10660 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 10661 VarDecl::DeclarationOnly; 10662 S.Diag(D->getLocation(), 10663 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10664 << D; 10665 continue; 10666 } 10667 // Store initializer for single element in private copy. Will be used during 10668 // codegen. 10669 PrivateVD->setInit(RHSVD->getInit()); 10670 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 10671 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 10672 ExprResult ReductionOp; 10673 if (DeclareReductionRef.isUsable()) { 10674 QualType RedTy = DeclareReductionRef.get()->getType(); 10675 QualType PtrRedTy = Context.getPointerType(RedTy); 10676 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 10677 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 10678 if (!BasePath.empty()) { 10679 LHS = S.DefaultLvalueConversion(LHS.get()); 10680 RHS = S.DefaultLvalueConversion(RHS.get()); 10681 LHS = ImplicitCastExpr::Create(Context, PtrRedTy, 10682 CK_UncheckedDerivedToBase, LHS.get(), 10683 &BasePath, LHS.get()->getValueKind()); 10684 RHS = ImplicitCastExpr::Create(Context, PtrRedTy, 10685 CK_UncheckedDerivedToBase, RHS.get(), 10686 &BasePath, RHS.get()->getValueKind()); 10687 } 10688 FunctionProtoType::ExtProtoInfo EPI; 10689 QualType Params[] = {PtrRedTy, PtrRedTy}; 10690 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 10691 auto *OVE = new (Context) OpaqueValueExpr( 10692 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, 10693 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 10694 Expr *Args[] = {LHS.get(), RHS.get()}; 10695 ReductionOp = new (Context) 10696 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc); 10697 } else { 10698 ReductionOp = S.BuildBinOp( 10699 Stack->getCurScope(), ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE); 10700 if (ReductionOp.isUsable()) { 10701 if (BOK != BO_LT && BOK != BO_GT) { 10702 ReductionOp = 10703 S.BuildBinOp(Stack->getCurScope(), ReductionId.getLocStart(), 10704 BO_Assign, LHSDRE, ReductionOp.get()); 10705 } else { 10706 auto *ConditionalOp = new (Context) 10707 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE, 10708 Type, VK_LValue, OK_Ordinary); 10709 ReductionOp = 10710 S.BuildBinOp(Stack->getCurScope(), ReductionId.getLocStart(), 10711 BO_Assign, LHSDRE, ConditionalOp); 10712 } 10713 if (ReductionOp.isUsable()) 10714 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get()); 10715 } 10716 if (!ReductionOp.isUsable()) 10717 continue; 10718 } 10719 10720 // OpenMP [2.15.4.6, Restrictions, p.2] 10721 // A list item that appears in an in_reduction clause of a task construct 10722 // must appear in a task_reduction clause of a construct associated with a 10723 // taskgroup region that includes the participating task in its taskgroup 10724 // set. The construct associated with the innermost region that meets this 10725 // condition must specify the same reduction-identifier as the in_reduction 10726 // clause. 10727 if (ClauseKind == OMPC_in_reduction) { 10728 SourceRange ParentSR; 10729 BinaryOperatorKind ParentBOK; 10730 const Expr *ParentReductionOp; 10731 Expr *ParentBOKTD, *ParentReductionOpTD; 10732 DSAStackTy::DSAVarData ParentBOKDSA = 10733 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 10734 ParentBOKTD); 10735 DSAStackTy::DSAVarData ParentReductionOpDSA = 10736 Stack->getTopMostTaskgroupReductionData( 10737 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 10738 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 10739 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 10740 if (!IsParentBOK && !IsParentReductionOp) { 10741 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction); 10742 continue; 10743 } 10744 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 10745 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK || 10746 IsParentReductionOp) { 10747 bool EmitError = true; 10748 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 10749 llvm::FoldingSetNodeID RedId, ParentRedId; 10750 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 10751 DeclareReductionRef.get()->Profile(RedId, Context, 10752 /*Canonical=*/true); 10753 EmitError = RedId != ParentRedId; 10754 } 10755 if (EmitError) { 10756 S.Diag(ReductionId.getLocStart(), 10757 diag::err_omp_reduction_identifier_mismatch) 10758 << ReductionIdRange << RefExpr->getSourceRange(); 10759 S.Diag(ParentSR.getBegin(), 10760 diag::note_omp_previous_reduction_identifier) 10761 << ParentSR 10762 << (IsParentBOK ? ParentBOKDSA.RefExpr 10763 : ParentReductionOpDSA.RefExpr) 10764 ->getSourceRange(); 10765 continue; 10766 } 10767 } 10768 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 10769 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined."); 10770 } 10771 10772 DeclRefExpr *Ref = nullptr; 10773 Expr *VarsExpr = RefExpr->IgnoreParens(); 10774 if (!VD && !S.CurContext->isDependentContext()) { 10775 if (ASE || OASE) { 10776 TransformExprToCaptures RebuildToCapture(S, D); 10777 VarsExpr = 10778 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 10779 Ref = RebuildToCapture.getCapturedExpr(); 10780 } else { 10781 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 10782 } 10783 if (!S.isOpenMPCapturedDecl(D)) { 10784 RD.ExprCaptures.emplace_back(Ref->getDecl()); 10785 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 10786 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 10787 if (!RefRes.isUsable()) 10788 continue; 10789 ExprResult PostUpdateRes = 10790 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 10791 RefRes.get()); 10792 if (!PostUpdateRes.isUsable()) 10793 continue; 10794 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 10795 Stack->getCurrentDirective() == OMPD_taskgroup) { 10796 S.Diag(RefExpr->getExprLoc(), 10797 diag::err_omp_reduction_non_addressable_expression) 10798 << RefExpr->getSourceRange(); 10799 continue; 10800 } 10801 RD.ExprPostUpdates.emplace_back( 10802 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 10803 } 10804 } 10805 } 10806 // All reduction items are still marked as reduction (to do not increase 10807 // code base size). 10808 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref); 10809 if (CurrDir == OMPD_taskgroup) { 10810 if (DeclareReductionRef.isUsable()) 10811 Stack->addTaskgroupReductionData(D, ReductionIdRange, 10812 DeclareReductionRef.get()); 10813 else 10814 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 10815 } 10816 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 10817 TaskgroupDescriptor); 10818 } 10819 return RD.Vars.empty(); 10820 } 10821 10822 OMPClause *Sema::ActOnOpenMPReductionClause( 10823 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 10824 SourceLocation ColonLoc, SourceLocation EndLoc, 10825 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 10826 ArrayRef<Expr *> UnresolvedReductions) { 10827 ReductionData RD(VarList.size()); 10828 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 10829 StartLoc, LParenLoc, ColonLoc, EndLoc, 10830 ReductionIdScopeSpec, ReductionId, 10831 UnresolvedReductions, RD)) 10832 return nullptr; 10833 10834 return OMPReductionClause::Create( 10835 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 10836 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 10837 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 10838 buildPreInits(Context, RD.ExprCaptures), 10839 buildPostUpdate(*this, RD.ExprPostUpdates)); 10840 } 10841 10842 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 10843 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 10844 SourceLocation ColonLoc, SourceLocation EndLoc, 10845 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 10846 ArrayRef<Expr *> UnresolvedReductions) { 10847 ReductionData RD(VarList.size()); 10848 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 10849 StartLoc, LParenLoc, ColonLoc, EndLoc, 10850 ReductionIdScopeSpec, ReductionId, 10851 UnresolvedReductions, RD)) 10852 return nullptr; 10853 10854 return OMPTaskReductionClause::Create( 10855 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 10856 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 10857 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 10858 buildPreInits(Context, RD.ExprCaptures), 10859 buildPostUpdate(*this, RD.ExprPostUpdates)); 10860 } 10861 10862 OMPClause *Sema::ActOnOpenMPInReductionClause( 10863 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 10864 SourceLocation ColonLoc, SourceLocation EndLoc, 10865 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 10866 ArrayRef<Expr *> UnresolvedReductions) { 10867 ReductionData RD(VarList.size()); 10868 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 10869 StartLoc, LParenLoc, ColonLoc, EndLoc, 10870 ReductionIdScopeSpec, ReductionId, 10871 UnresolvedReductions, RD)) 10872 return nullptr; 10873 10874 return OMPInReductionClause::Create( 10875 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 10876 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 10877 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 10878 buildPreInits(Context, RD.ExprCaptures), 10879 buildPostUpdate(*this, RD.ExprPostUpdates)); 10880 } 10881 10882 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 10883 SourceLocation LinLoc) { 10884 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 10885 LinKind == OMPC_LINEAR_unknown) { 10886 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 10887 return true; 10888 } 10889 return false; 10890 } 10891 10892 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 10893 OpenMPLinearClauseKind LinKind, 10894 QualType Type) { 10895 const auto *VD = dyn_cast_or_null<VarDecl>(D); 10896 // A variable must not have an incomplete type or a reference type. 10897 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 10898 return true; 10899 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 10900 !Type->isReferenceType()) { 10901 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 10902 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 10903 return true; 10904 } 10905 Type = Type.getNonReferenceType(); 10906 10907 // A list item must not be const-qualified. 10908 if (Type.isConstant(Context)) { 10909 Diag(ELoc, diag::err_omp_const_variable) 10910 << getOpenMPClauseName(OMPC_linear); 10911 if (D) { 10912 bool IsDecl = 10913 !VD || 10914 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 10915 Diag(D->getLocation(), 10916 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10917 << D; 10918 } 10919 return true; 10920 } 10921 10922 // A list item must be of integral or pointer type. 10923 Type = Type.getUnqualifiedType().getCanonicalType(); 10924 const auto *Ty = Type.getTypePtrOrNull(); 10925 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && 10926 !Ty->isPointerType())) { 10927 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 10928 if (D) { 10929 bool IsDecl = 10930 !VD || 10931 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 10932 Diag(D->getLocation(), 10933 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10934 << D; 10935 } 10936 return true; 10937 } 10938 return false; 10939 } 10940 10941 OMPClause *Sema::ActOnOpenMPLinearClause( 10942 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 10943 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 10944 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 10945 SmallVector<Expr *, 8> Vars; 10946 SmallVector<Expr *, 8> Privates; 10947 SmallVector<Expr *, 8> Inits; 10948 SmallVector<Decl *, 4> ExprCaptures; 10949 SmallVector<Expr *, 4> ExprPostUpdates; 10950 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 10951 LinKind = OMPC_LINEAR_val; 10952 for (Expr *RefExpr : VarList) { 10953 assert(RefExpr && "NULL expr in OpenMP linear clause."); 10954 SourceLocation ELoc; 10955 SourceRange ERange; 10956 Expr *SimpleRefExpr = RefExpr; 10957 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 10958 /*AllowArraySection=*/false); 10959 if (Res.second) { 10960 // It will be analyzed later. 10961 Vars.push_back(RefExpr); 10962 Privates.push_back(nullptr); 10963 Inits.push_back(nullptr); 10964 } 10965 ValueDecl *D = Res.first; 10966 if (!D) 10967 continue; 10968 10969 QualType Type = D->getType(); 10970 auto *VD = dyn_cast<VarDecl>(D); 10971 10972 // OpenMP [2.14.3.7, linear clause] 10973 // A list-item cannot appear in more than one linear clause. 10974 // A list-item that appears in a linear clause cannot appear in any 10975 // other data-sharing attribute clause. 10976 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 10977 if (DVar.RefExpr) { 10978 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 10979 << getOpenMPClauseName(OMPC_linear); 10980 reportOriginalDsa(*this, DSAStack, D, DVar); 10981 continue; 10982 } 10983 10984 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 10985 continue; 10986 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 10987 10988 // Build private copy of original var. 10989 VarDecl *Private = 10990 buildVarDecl(*this, ELoc, Type, D->getName(), 10991 D->hasAttrs() ? &D->getAttrs() : nullptr, 10992 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 10993 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 10994 // Build var to save initial value. 10995 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 10996 Expr *InitExpr; 10997 DeclRefExpr *Ref = nullptr; 10998 if (!VD && !CurContext->isDependentContext()) { 10999 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 11000 if (!isOpenMPCapturedDecl(D)) { 11001 ExprCaptures.push_back(Ref->getDecl()); 11002 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 11003 ExprResult RefRes = DefaultLvalueConversion(Ref); 11004 if (!RefRes.isUsable()) 11005 continue; 11006 ExprResult PostUpdateRes = 11007 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 11008 SimpleRefExpr, RefRes.get()); 11009 if (!PostUpdateRes.isUsable()) 11010 continue; 11011 ExprPostUpdates.push_back( 11012 IgnoredValueConversions(PostUpdateRes.get()).get()); 11013 } 11014 } 11015 } 11016 if (LinKind == OMPC_LINEAR_uval) 11017 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 11018 else 11019 InitExpr = VD ? SimpleRefExpr : Ref; 11020 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 11021 /*DirectInit=*/false); 11022 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 11023 11024 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 11025 Vars.push_back((VD || CurContext->isDependentContext()) 11026 ? RefExpr->IgnoreParens() 11027 : Ref); 11028 Privates.push_back(PrivateRef); 11029 Inits.push_back(InitRef); 11030 } 11031 11032 if (Vars.empty()) 11033 return nullptr; 11034 11035 Expr *StepExpr = Step; 11036 Expr *CalcStepExpr = nullptr; 11037 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 11038 !Step->isInstantiationDependent() && 11039 !Step->containsUnexpandedParameterPack()) { 11040 SourceLocation StepLoc = Step->getLocStart(); 11041 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 11042 if (Val.isInvalid()) 11043 return nullptr; 11044 StepExpr = Val.get(); 11045 11046 // Build var to save the step value. 11047 VarDecl *SaveVar = 11048 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 11049 ExprResult SaveRef = 11050 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 11051 ExprResult CalcStep = 11052 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 11053 CalcStep = ActOnFinishFullExpr(CalcStep.get()); 11054 11055 // Warn about zero linear step (it would be probably better specified as 11056 // making corresponding variables 'const'). 11057 llvm::APSInt Result; 11058 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); 11059 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) 11060 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] 11061 << (Vars.size() > 1); 11062 if (!IsConstant && CalcStep.isUsable()) { 11063 // Calculate the step beforehand instead of doing this on each iteration. 11064 // (This is not used if the number of iterations may be kfold-ed). 11065 CalcStepExpr = CalcStep.get(); 11066 } 11067 } 11068 11069 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 11070 ColonLoc, EndLoc, Vars, Privates, Inits, 11071 StepExpr, CalcStepExpr, 11072 buildPreInits(Context, ExprCaptures), 11073 buildPostUpdate(*this, ExprPostUpdates)); 11074 } 11075 11076 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 11077 Expr *NumIterations, Sema &SemaRef, 11078 Scope *S, DSAStackTy *Stack) { 11079 // Walk the vars and build update/final expressions for the CodeGen. 11080 SmallVector<Expr *, 8> Updates; 11081 SmallVector<Expr *, 8> Finals; 11082 Expr *Step = Clause.getStep(); 11083 Expr *CalcStep = Clause.getCalcStep(); 11084 // OpenMP [2.14.3.7, linear clause] 11085 // If linear-step is not specified it is assumed to be 1. 11086 if (!Step) 11087 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 11088 else if (CalcStep) 11089 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 11090 bool HasErrors = false; 11091 auto CurInit = Clause.inits().begin(); 11092 auto CurPrivate = Clause.privates().begin(); 11093 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 11094 for (Expr *RefExpr : Clause.varlists()) { 11095 SourceLocation ELoc; 11096 SourceRange ERange; 11097 Expr *SimpleRefExpr = RefExpr; 11098 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange, 11099 /*AllowArraySection=*/false); 11100 ValueDecl *D = Res.first; 11101 if (Res.second || !D) { 11102 Updates.push_back(nullptr); 11103 Finals.push_back(nullptr); 11104 HasErrors = true; 11105 continue; 11106 } 11107 auto &&Info = Stack->isLoopControlVariable(D); 11108 // OpenMP [2.15.11, distribute simd Construct] 11109 // A list item may not appear in a linear clause, unless it is the loop 11110 // iteration variable. 11111 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 11112 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 11113 SemaRef.Diag(ELoc, 11114 diag::err_omp_linear_distribute_var_non_loop_iteration); 11115 Updates.push_back(nullptr); 11116 Finals.push_back(nullptr); 11117 HasErrors = true; 11118 continue; 11119 } 11120 Expr *InitExpr = *CurInit; 11121 11122 // Build privatized reference to the current linear var. 11123 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 11124 Expr *CapturedRef; 11125 if (LinKind == OMPC_LINEAR_uval) 11126 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 11127 else 11128 CapturedRef = 11129 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 11130 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 11131 /*RefersToCapture=*/true); 11132 11133 // Build update: Var = InitExpr + IV * Step 11134 ExprResult Update; 11135 if (!Info.first) 11136 Update = 11137 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, 11138 InitExpr, IV, Step, /* Subtract */ false); 11139 else 11140 Update = *CurPrivate; 11141 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(), 11142 /*DiscardedValue=*/true); 11143 11144 // Build final: Var = InitExpr + NumIterations * Step 11145 ExprResult Final; 11146 if (!Info.first) 11147 Final = 11148 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 11149 InitExpr, NumIterations, Step, /*Subtract=*/false); 11150 else 11151 Final = *CurPrivate; 11152 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(), 11153 /*DiscardedValue=*/true); 11154 11155 if (!Update.isUsable() || !Final.isUsable()) { 11156 Updates.push_back(nullptr); 11157 Finals.push_back(nullptr); 11158 HasErrors = true; 11159 } else { 11160 Updates.push_back(Update.get()); 11161 Finals.push_back(Final.get()); 11162 } 11163 ++CurInit; 11164 ++CurPrivate; 11165 } 11166 Clause.setUpdates(Updates); 11167 Clause.setFinals(Finals); 11168 return HasErrors; 11169 } 11170 11171 OMPClause *Sema::ActOnOpenMPAlignedClause( 11172 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 11173 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 11174 SmallVector<Expr *, 8> Vars; 11175 for (Expr *RefExpr : VarList) { 11176 assert(RefExpr && "NULL expr in OpenMP linear clause."); 11177 SourceLocation ELoc; 11178 SourceRange ERange; 11179 Expr *SimpleRefExpr = RefExpr; 11180 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 11181 /*AllowArraySection=*/false); 11182 if (Res.second) { 11183 // It will be analyzed later. 11184 Vars.push_back(RefExpr); 11185 } 11186 ValueDecl *D = Res.first; 11187 if (!D) 11188 continue; 11189 11190 QualType QType = D->getType(); 11191 auto *VD = dyn_cast<VarDecl>(D); 11192 11193 // OpenMP [2.8.1, simd construct, Restrictions] 11194 // The type of list items appearing in the aligned clause must be 11195 // array, pointer, reference to array, or reference to pointer. 11196 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 11197 const Type *Ty = QType.getTypePtrOrNull(); 11198 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 11199 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 11200 << QType << getLangOpts().CPlusPlus << ERange; 11201 bool IsDecl = 11202 !VD || 11203 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 11204 Diag(D->getLocation(), 11205 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11206 << D; 11207 continue; 11208 } 11209 11210 // OpenMP [2.8.1, simd construct, Restrictions] 11211 // A list-item cannot appear in more than one aligned clause. 11212 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 11213 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange; 11214 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 11215 << getOpenMPClauseName(OMPC_aligned); 11216 continue; 11217 } 11218 11219 DeclRefExpr *Ref = nullptr; 11220 if (!VD && isOpenMPCapturedDecl(D)) 11221 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 11222 Vars.push_back(DefaultFunctionArrayConversion( 11223 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 11224 .get()); 11225 } 11226 11227 // OpenMP [2.8.1, simd construct, Description] 11228 // The parameter of the aligned clause, alignment, must be a constant 11229 // positive integer expression. 11230 // If no optional parameter is specified, implementation-defined default 11231 // alignments for SIMD instructions on the target platforms are assumed. 11232 if (Alignment != nullptr) { 11233 ExprResult AlignResult = 11234 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 11235 if (AlignResult.isInvalid()) 11236 return nullptr; 11237 Alignment = AlignResult.get(); 11238 } 11239 if (Vars.empty()) 11240 return nullptr; 11241 11242 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 11243 EndLoc, Vars, Alignment); 11244 } 11245 11246 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 11247 SourceLocation StartLoc, 11248 SourceLocation LParenLoc, 11249 SourceLocation EndLoc) { 11250 SmallVector<Expr *, 8> Vars; 11251 SmallVector<Expr *, 8> SrcExprs; 11252 SmallVector<Expr *, 8> DstExprs; 11253 SmallVector<Expr *, 8> AssignmentOps; 11254 for (Expr *RefExpr : VarList) { 11255 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 11256 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 11257 // It will be analyzed later. 11258 Vars.push_back(RefExpr); 11259 SrcExprs.push_back(nullptr); 11260 DstExprs.push_back(nullptr); 11261 AssignmentOps.push_back(nullptr); 11262 continue; 11263 } 11264 11265 SourceLocation ELoc = RefExpr->getExprLoc(); 11266 // OpenMP [2.1, C/C++] 11267 // A list item is a variable name. 11268 // OpenMP [2.14.4.1, Restrictions, p.1] 11269 // A list item that appears in a copyin clause must be threadprivate. 11270 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 11271 if (!DE || !isa<VarDecl>(DE->getDecl())) { 11272 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 11273 << 0 << RefExpr->getSourceRange(); 11274 continue; 11275 } 11276 11277 Decl *D = DE->getDecl(); 11278 auto *VD = cast<VarDecl>(D); 11279 11280 QualType Type = VD->getType(); 11281 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 11282 // It will be analyzed later. 11283 Vars.push_back(DE); 11284 SrcExprs.push_back(nullptr); 11285 DstExprs.push_back(nullptr); 11286 AssignmentOps.push_back(nullptr); 11287 continue; 11288 } 11289 11290 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 11291 // A list item that appears in a copyin clause must be threadprivate. 11292 if (!DSAStack->isThreadPrivate(VD)) { 11293 Diag(ELoc, diag::err_omp_required_access) 11294 << getOpenMPClauseName(OMPC_copyin) 11295 << getOpenMPDirectiveName(OMPD_threadprivate); 11296 continue; 11297 } 11298 11299 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 11300 // A variable of class type (or array thereof) that appears in a 11301 // copyin clause requires an accessible, unambiguous copy assignment 11302 // operator for the class type. 11303 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 11304 VarDecl *SrcVD = 11305 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(), 11306 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 11307 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 11308 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 11309 VarDecl *DstVD = 11310 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst", 11311 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 11312 DeclRefExpr *PseudoDstExpr = 11313 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 11314 // For arrays generate assignment operation for single element and replace 11315 // it by the original array element in CodeGen. 11316 ExprResult AssignmentOp = 11317 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 11318 PseudoSrcExpr); 11319 if (AssignmentOp.isInvalid()) 11320 continue; 11321 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 11322 /*DiscardedValue=*/true); 11323 if (AssignmentOp.isInvalid()) 11324 continue; 11325 11326 DSAStack->addDSA(VD, DE, OMPC_copyin); 11327 Vars.push_back(DE); 11328 SrcExprs.push_back(PseudoSrcExpr); 11329 DstExprs.push_back(PseudoDstExpr); 11330 AssignmentOps.push_back(AssignmentOp.get()); 11331 } 11332 11333 if (Vars.empty()) 11334 return nullptr; 11335 11336 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 11337 SrcExprs, DstExprs, AssignmentOps); 11338 } 11339 11340 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 11341 SourceLocation StartLoc, 11342 SourceLocation LParenLoc, 11343 SourceLocation EndLoc) { 11344 SmallVector<Expr *, 8> Vars; 11345 SmallVector<Expr *, 8> SrcExprs; 11346 SmallVector<Expr *, 8> DstExprs; 11347 SmallVector<Expr *, 8> AssignmentOps; 11348 for (Expr *RefExpr : VarList) { 11349 assert(RefExpr && "NULL expr in OpenMP linear clause."); 11350 SourceLocation ELoc; 11351 SourceRange ERange; 11352 Expr *SimpleRefExpr = RefExpr; 11353 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 11354 /*AllowArraySection=*/false); 11355 if (Res.second) { 11356 // It will be analyzed later. 11357 Vars.push_back(RefExpr); 11358 SrcExprs.push_back(nullptr); 11359 DstExprs.push_back(nullptr); 11360 AssignmentOps.push_back(nullptr); 11361 } 11362 ValueDecl *D = Res.first; 11363 if (!D) 11364 continue; 11365 11366 QualType Type = D->getType(); 11367 auto *VD = dyn_cast<VarDecl>(D); 11368 11369 // OpenMP [2.14.4.2, Restrictions, p.2] 11370 // A list item that appears in a copyprivate clause may not appear in a 11371 // private or firstprivate clause on the single construct. 11372 if (!VD || !DSAStack->isThreadPrivate(VD)) { 11373 DSAStackTy::DSAVarData DVar = 11374 DSAStack->getTopDSA(D, /*FromParent=*/false); 11375 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 11376 DVar.RefExpr) { 11377 Diag(ELoc, diag::err_omp_wrong_dsa) 11378 << getOpenMPClauseName(DVar.CKind) 11379 << getOpenMPClauseName(OMPC_copyprivate); 11380 reportOriginalDsa(*this, DSAStack, D, DVar); 11381 continue; 11382 } 11383 11384 // OpenMP [2.11.4.2, Restrictions, p.1] 11385 // All list items that appear in a copyprivate clause must be either 11386 // threadprivate or private in the enclosing context. 11387 if (DVar.CKind == OMPC_unknown) { 11388 DVar = DSAStack->getImplicitDSA(D, false); 11389 if (DVar.CKind == OMPC_shared) { 11390 Diag(ELoc, diag::err_omp_required_access) 11391 << getOpenMPClauseName(OMPC_copyprivate) 11392 << "threadprivate or private in the enclosing context"; 11393 reportOriginalDsa(*this, DSAStack, D, DVar); 11394 continue; 11395 } 11396 } 11397 } 11398 11399 // Variably modified types are not supported. 11400 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 11401 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 11402 << getOpenMPClauseName(OMPC_copyprivate) << Type 11403 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 11404 bool IsDecl = 11405 !VD || 11406 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 11407 Diag(D->getLocation(), 11408 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11409 << D; 11410 continue; 11411 } 11412 11413 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 11414 // A variable of class type (or array thereof) that appears in a 11415 // copyin clause requires an accessible, unambiguous copy assignment 11416 // operator for the class type. 11417 Type = Context.getBaseElementType(Type.getNonReferenceType()) 11418 .getUnqualifiedType(); 11419 VarDecl *SrcVD = 11420 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src", 11421 D->hasAttrs() ? &D->getAttrs() : nullptr); 11422 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 11423 VarDecl *DstVD = 11424 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst", 11425 D->hasAttrs() ? &D->getAttrs() : nullptr); 11426 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 11427 ExprResult AssignmentOp = BuildBinOp( 11428 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 11429 if (AssignmentOp.isInvalid()) 11430 continue; 11431 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc, 11432 /*DiscardedValue=*/true); 11433 if (AssignmentOp.isInvalid()) 11434 continue; 11435 11436 // No need to mark vars as copyprivate, they are already threadprivate or 11437 // implicitly private. 11438 assert(VD || isOpenMPCapturedDecl(D)); 11439 Vars.push_back( 11440 VD ? RefExpr->IgnoreParens() 11441 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 11442 SrcExprs.push_back(PseudoSrcExpr); 11443 DstExprs.push_back(PseudoDstExpr); 11444 AssignmentOps.push_back(AssignmentOp.get()); 11445 } 11446 11447 if (Vars.empty()) 11448 return nullptr; 11449 11450 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 11451 Vars, SrcExprs, DstExprs, AssignmentOps); 11452 } 11453 11454 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 11455 SourceLocation StartLoc, 11456 SourceLocation LParenLoc, 11457 SourceLocation EndLoc) { 11458 if (VarList.empty()) 11459 return nullptr; 11460 11461 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 11462 } 11463 11464 OMPClause * 11465 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind, 11466 SourceLocation DepLoc, SourceLocation ColonLoc, 11467 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 11468 SourceLocation LParenLoc, SourceLocation EndLoc) { 11469 if (DSAStack->getCurrentDirective() == OMPD_ordered && 11470 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 11471 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 11472 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 11473 return nullptr; 11474 } 11475 if (DSAStack->getCurrentDirective() != OMPD_ordered && 11476 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 11477 DepKind == OMPC_DEPEND_sink)) { 11478 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink}; 11479 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 11480 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 11481 /*Last=*/OMPC_DEPEND_unknown, Except) 11482 << getOpenMPClauseName(OMPC_depend); 11483 return nullptr; 11484 } 11485 SmallVector<Expr *, 8> Vars; 11486 DSAStackTy::OperatorOffsetTy OpsOffs; 11487 llvm::APSInt DepCounter(/*BitWidth=*/32); 11488 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 11489 if (DepKind == OMPC_DEPEND_sink) { 11490 if (const Expr *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) { 11491 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 11492 TotalDepCount.setIsUnsigned(/*Val=*/true); 11493 } 11494 } 11495 for (Expr *RefExpr : VarList) { 11496 assert(RefExpr && "NULL expr in OpenMP shared clause."); 11497 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 11498 // It will be analyzed later. 11499 Vars.push_back(RefExpr); 11500 continue; 11501 } 11502 11503 SourceLocation ELoc = RefExpr->getExprLoc(); 11504 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 11505 if (DepKind == OMPC_DEPEND_sink) { 11506 if (DSAStack->getParentOrderedRegionParam() && 11507 DepCounter >= TotalDepCount) { 11508 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 11509 continue; 11510 } 11511 ++DepCounter; 11512 // OpenMP [2.13.9, Summary] 11513 // depend(dependence-type : vec), where dependence-type is: 11514 // 'sink' and where vec is the iteration vector, which has the form: 11515 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 11516 // where n is the value specified by the ordered clause in the loop 11517 // directive, xi denotes the loop iteration variable of the i-th nested 11518 // loop associated with the loop directive, and di is a constant 11519 // non-negative integer. 11520 if (CurContext->isDependentContext()) { 11521 // It will be analyzed later. 11522 Vars.push_back(RefExpr); 11523 continue; 11524 } 11525 SimpleExpr = SimpleExpr->IgnoreImplicit(); 11526 OverloadedOperatorKind OOK = OO_None; 11527 SourceLocation OOLoc; 11528 Expr *LHS = SimpleExpr; 11529 Expr *RHS = nullptr; 11530 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 11531 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 11532 OOLoc = BO->getOperatorLoc(); 11533 LHS = BO->getLHS()->IgnoreParenImpCasts(); 11534 RHS = BO->getRHS()->IgnoreParenImpCasts(); 11535 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 11536 OOK = OCE->getOperator(); 11537 OOLoc = OCE->getOperatorLoc(); 11538 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 11539 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 11540 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 11541 OOK = MCE->getMethodDecl() 11542 ->getNameInfo() 11543 .getName() 11544 .getCXXOverloadedOperator(); 11545 OOLoc = MCE->getCallee()->getExprLoc(); 11546 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 11547 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 11548 } 11549 SourceLocation ELoc; 11550 SourceRange ERange; 11551 auto Res = getPrivateItem(*this, LHS, ELoc, ERange, 11552 /*AllowArraySection=*/false); 11553 if (Res.second) { 11554 // It will be analyzed later. 11555 Vars.push_back(RefExpr); 11556 } 11557 ValueDecl *D = Res.first; 11558 if (!D) 11559 continue; 11560 11561 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 11562 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 11563 continue; 11564 } 11565 if (RHS) { 11566 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 11567 RHS, OMPC_depend, /*StrictlyPositive=*/false); 11568 if (RHSRes.isInvalid()) 11569 continue; 11570 } 11571 if (!CurContext->isDependentContext() && 11572 DSAStack->getParentOrderedRegionParam() && 11573 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 11574 const ValueDecl *VD = 11575 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 11576 if (VD) 11577 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 11578 << 1 << VD; 11579 else 11580 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 11581 continue; 11582 } 11583 OpsOffs.emplace_back(RHS, OOK); 11584 } else { 11585 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 11586 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 11587 (ASE && 11588 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() && 11589 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 11590 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 11591 << RefExpr->getSourceRange(); 11592 continue; 11593 } 11594 bool Suppress = getDiagnostics().getSuppressAllDiagnostics(); 11595 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 11596 ExprResult Res = 11597 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts()); 11598 getDiagnostics().setSuppressAllDiagnostics(Suppress); 11599 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) { 11600 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 11601 << RefExpr->getSourceRange(); 11602 continue; 11603 } 11604 } 11605 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 11606 } 11607 11608 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 11609 TotalDepCount > VarList.size() && 11610 DSAStack->getParentOrderedRegionParam() && 11611 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 11612 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 11613 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 11614 } 11615 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 11616 Vars.empty()) 11617 return nullptr; 11618 11619 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 11620 DepKind, DepLoc, ColonLoc, Vars); 11621 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 11622 DSAStack->isParentOrderedRegion()) 11623 DSAStack->addDoacrossDependClause(C, OpsOffs); 11624 return C; 11625 } 11626 11627 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc, 11628 SourceLocation LParenLoc, 11629 SourceLocation EndLoc) { 11630 Expr *ValExpr = Device; 11631 Stmt *HelperValStmt = nullptr; 11632 11633 // OpenMP [2.9.1, Restrictions] 11634 // The device expression must evaluate to a non-negative integer value. 11635 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 11636 /*StrictlyPositive=*/false)) 11637 return nullptr; 11638 11639 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 11640 OpenMPDirectiveKind CaptureRegion = 11641 getOpenMPCaptureRegionForClause(DKind, OMPC_device); 11642 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 11643 ValExpr = MakeFullExpr(ValExpr).get(); 11644 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 11645 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 11646 HelperValStmt = buildPreInits(Context, Captures); 11647 } 11648 11649 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion, 11650 StartLoc, LParenLoc, EndLoc); 11651 } 11652 11653 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 11654 DSAStackTy *Stack, QualType QTy, 11655 bool FullCheck = true) { 11656 NamedDecl *ND; 11657 if (QTy->isIncompleteType(&ND)) { 11658 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 11659 return false; 11660 } 11661 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 11662 !QTy.isTrivialType(SemaRef.Context)) 11663 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 11664 return true; 11665 } 11666 11667 /// Return true if it can be proven that the provided array expression 11668 /// (array section or array subscript) does NOT specify the whole size of the 11669 /// array whose base type is \a BaseQTy. 11670 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 11671 const Expr *E, 11672 QualType BaseQTy) { 11673 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 11674 11675 // If this is an array subscript, it refers to the whole size if the size of 11676 // the dimension is constant and equals 1. Also, an array section assumes the 11677 // format of an array subscript if no colon is used. 11678 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) { 11679 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 11680 return ATy->getSize().getSExtValue() != 1; 11681 // Size can't be evaluated statically. 11682 return false; 11683 } 11684 11685 assert(OASE && "Expecting array section if not an array subscript."); 11686 const Expr *LowerBound = OASE->getLowerBound(); 11687 const Expr *Length = OASE->getLength(); 11688 11689 // If there is a lower bound that does not evaluates to zero, we are not 11690 // covering the whole dimension. 11691 if (LowerBound) { 11692 llvm::APSInt ConstLowerBound; 11693 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext())) 11694 return false; // Can't get the integer value as a constant. 11695 if (ConstLowerBound.getSExtValue()) 11696 return true; 11697 } 11698 11699 // If we don't have a length we covering the whole dimension. 11700 if (!Length) 11701 return false; 11702 11703 // If the base is a pointer, we don't have a way to get the size of the 11704 // pointee. 11705 if (BaseQTy->isPointerType()) 11706 return false; 11707 11708 // We can only check if the length is the same as the size of the dimension 11709 // if we have a constant array. 11710 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 11711 if (!CATy) 11712 return false; 11713 11714 llvm::APSInt ConstLength; 11715 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext())) 11716 return false; // Can't get the integer value as a constant. 11717 11718 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 11719 } 11720 11721 // Return true if it can be proven that the provided array expression (array 11722 // section or array subscript) does NOT specify a single element of the array 11723 // whose base type is \a BaseQTy. 11724 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 11725 const Expr *E, 11726 QualType BaseQTy) { 11727 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 11728 11729 // An array subscript always refer to a single element. Also, an array section 11730 // assumes the format of an array subscript if no colon is used. 11731 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) 11732 return false; 11733 11734 assert(OASE && "Expecting array section if not an array subscript."); 11735 const Expr *Length = OASE->getLength(); 11736 11737 // If we don't have a length we have to check if the array has unitary size 11738 // for this dimension. Also, we should always expect a length if the base type 11739 // is pointer. 11740 if (!Length) { 11741 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 11742 return ATy->getSize().getSExtValue() != 1; 11743 // We cannot assume anything. 11744 return false; 11745 } 11746 11747 // Check if the length evaluates to 1. 11748 llvm::APSInt ConstLength; 11749 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext())) 11750 return false; // Can't get the integer value as a constant. 11751 11752 return ConstLength.getSExtValue() != 1; 11753 } 11754 11755 // Return the expression of the base of the mappable expression or null if it 11756 // cannot be determined and do all the necessary checks to see if the expression 11757 // is valid as a standalone mappable expression. In the process, record all the 11758 // components of the expression. 11759 static const Expr *checkMapClauseExpressionBase( 11760 Sema &SemaRef, Expr *E, 11761 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 11762 OpenMPClauseKind CKind, bool NoDiagnose) { 11763 SourceLocation ELoc = E->getExprLoc(); 11764 SourceRange ERange = E->getSourceRange(); 11765 11766 // The base of elements of list in a map clause have to be either: 11767 // - a reference to variable or field. 11768 // - a member expression. 11769 // - an array expression. 11770 // 11771 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 11772 // reference to 'r'. 11773 // 11774 // If we have: 11775 // 11776 // struct SS { 11777 // Bla S; 11778 // foo() { 11779 // #pragma omp target map (S.Arr[:12]); 11780 // } 11781 // } 11782 // 11783 // We want to retrieve the member expression 'this->S'; 11784 11785 const Expr *RelevantExpr = nullptr; 11786 11787 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2] 11788 // If a list item is an array section, it must specify contiguous storage. 11789 // 11790 // For this restriction it is sufficient that we make sure only references 11791 // to variables or fields and array expressions, and that no array sections 11792 // exist except in the rightmost expression (unless they cover the whole 11793 // dimension of the array). E.g. these would be invalid: 11794 // 11795 // r.ArrS[3:5].Arr[6:7] 11796 // 11797 // r.ArrS[3:5].x 11798 // 11799 // but these would be valid: 11800 // r.ArrS[3].Arr[6:7] 11801 // 11802 // r.ArrS[3].x 11803 11804 bool AllowUnitySizeArraySection = true; 11805 bool AllowWholeSizeArraySection = true; 11806 11807 while (!RelevantExpr) { 11808 E = E->IgnoreParenImpCasts(); 11809 11810 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) { 11811 if (!isa<VarDecl>(CurE->getDecl())) 11812 return nullptr; 11813 11814 RelevantExpr = CurE; 11815 11816 // If we got a reference to a declaration, we should not expect any array 11817 // section before that. 11818 AllowUnitySizeArraySection = false; 11819 AllowWholeSizeArraySection = false; 11820 11821 // Record the component. 11822 CurComponents.emplace_back(CurE, CurE->getDecl()); 11823 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) { 11824 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts(); 11825 11826 if (isa<CXXThisExpr>(BaseE)) 11827 // We found a base expression: this->Val. 11828 RelevantExpr = CurE; 11829 else 11830 E = BaseE; 11831 11832 if (!isa<FieldDecl>(CurE->getMemberDecl())) { 11833 if (!NoDiagnose) { 11834 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 11835 << CurE->getSourceRange(); 11836 return nullptr; 11837 } 11838 if (RelevantExpr) 11839 return nullptr; 11840 continue; 11841 } 11842 11843 auto *FD = cast<FieldDecl>(CurE->getMemberDecl()); 11844 11845 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 11846 // A bit-field cannot appear in a map clause. 11847 // 11848 if (FD->isBitField()) { 11849 if (!NoDiagnose) { 11850 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 11851 << CurE->getSourceRange() << getOpenMPClauseName(CKind); 11852 return nullptr; 11853 } 11854 if (RelevantExpr) 11855 return nullptr; 11856 continue; 11857 } 11858 11859 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 11860 // If the type of a list item is a reference to a type T then the type 11861 // will be considered to be T for all purposes of this clause. 11862 QualType CurType = BaseE->getType().getNonReferenceType(); 11863 11864 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 11865 // A list item cannot be a variable that is a member of a structure with 11866 // a union type. 11867 // 11868 if (CurType->isUnionType()) { 11869 if (!NoDiagnose) { 11870 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 11871 << CurE->getSourceRange(); 11872 return nullptr; 11873 } 11874 continue; 11875 } 11876 11877 // If we got a member expression, we should not expect any array section 11878 // before that: 11879 // 11880 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 11881 // If a list item is an element of a structure, only the rightmost symbol 11882 // of the variable reference can be an array section. 11883 // 11884 AllowUnitySizeArraySection = false; 11885 AllowWholeSizeArraySection = false; 11886 11887 // Record the component. 11888 CurComponents.emplace_back(CurE, FD); 11889 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) { 11890 E = CurE->getBase()->IgnoreParenImpCasts(); 11891 11892 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 11893 if (!NoDiagnose) { 11894 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 11895 << 0 << CurE->getSourceRange(); 11896 return nullptr; 11897 } 11898 continue; 11899 } 11900 11901 // If we got an array subscript that express the whole dimension we 11902 // can have any array expressions before. If it only expressing part of 11903 // the dimension, we can only have unitary-size array expressions. 11904 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, 11905 E->getType())) 11906 AllowWholeSizeArraySection = false; 11907 11908 // Record the component - we don't have any declaration associated. 11909 CurComponents.emplace_back(CurE, nullptr); 11910 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) { 11911 assert(!NoDiagnose && "Array sections cannot be implicitly mapped."); 11912 E = CurE->getBase()->IgnoreParenImpCasts(); 11913 11914 QualType CurType = 11915 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 11916 11917 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 11918 // If the type of a list item is a reference to a type T then the type 11919 // will be considered to be T for all purposes of this clause. 11920 if (CurType->isReferenceType()) 11921 CurType = CurType->getPointeeType(); 11922 11923 bool IsPointer = CurType->isAnyPointerType(); 11924 11925 if (!IsPointer && !CurType->isArrayType()) { 11926 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 11927 << 0 << CurE->getSourceRange(); 11928 return nullptr; 11929 } 11930 11931 bool NotWhole = 11932 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType); 11933 bool NotUnity = 11934 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType); 11935 11936 if (AllowWholeSizeArraySection) { 11937 // Any array section is currently allowed. Allowing a whole size array 11938 // section implies allowing a unity array section as well. 11939 // 11940 // If this array section refers to the whole dimension we can still 11941 // accept other array sections before this one, except if the base is a 11942 // pointer. Otherwise, only unitary sections are accepted. 11943 if (NotWhole || IsPointer) 11944 AllowWholeSizeArraySection = false; 11945 } else if (AllowUnitySizeArraySection && NotUnity) { 11946 // A unity or whole array section is not allowed and that is not 11947 // compatible with the properties of the current array section. 11948 SemaRef.Diag( 11949 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 11950 << CurE->getSourceRange(); 11951 return nullptr; 11952 } 11953 11954 // Record the component - we don't have any declaration associated. 11955 CurComponents.emplace_back(CurE, nullptr); 11956 } else { 11957 if (!NoDiagnose) { 11958 // If nothing else worked, this is not a valid map clause expression. 11959 SemaRef.Diag( 11960 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 11961 << ERange; 11962 } 11963 return nullptr; 11964 } 11965 } 11966 11967 return RelevantExpr; 11968 } 11969 11970 // Return true if expression E associated with value VD has conflicts with other 11971 // map information. 11972 static bool checkMapConflicts( 11973 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 11974 bool CurrentRegionOnly, 11975 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 11976 OpenMPClauseKind CKind) { 11977 assert(VD && E); 11978 SourceLocation ELoc = E->getExprLoc(); 11979 SourceRange ERange = E->getSourceRange(); 11980 11981 // In order to easily check the conflicts we need to match each component of 11982 // the expression under test with the components of the expressions that are 11983 // already in the stack. 11984 11985 assert(!CurComponents.empty() && "Map clause expression with no components!"); 11986 assert(CurComponents.back().getAssociatedDeclaration() == VD && 11987 "Map clause expression with unexpected base!"); 11988 11989 // Variables to help detecting enclosing problems in data environment nests. 11990 bool IsEnclosedByDataEnvironmentExpr = false; 11991 const Expr *EnclosingExpr = nullptr; 11992 11993 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 11994 VD, CurrentRegionOnly, 11995 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 11996 ERange, CKind, &EnclosingExpr, 11997 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 11998 StackComponents, 11999 OpenMPClauseKind) { 12000 assert(!StackComponents.empty() && 12001 "Map clause expression with no components!"); 12002 assert(StackComponents.back().getAssociatedDeclaration() == VD && 12003 "Map clause expression with unexpected base!"); 12004 (void)VD; 12005 12006 // The whole expression in the stack. 12007 const Expr *RE = StackComponents.front().getAssociatedExpression(); 12008 12009 // Expressions must start from the same base. Here we detect at which 12010 // point both expressions diverge from each other and see if we can 12011 // detect if the memory referred to both expressions is contiguous and 12012 // do not overlap. 12013 auto CI = CurComponents.rbegin(); 12014 auto CE = CurComponents.rend(); 12015 auto SI = StackComponents.rbegin(); 12016 auto SE = StackComponents.rend(); 12017 for (; CI != CE && SI != SE; ++CI, ++SI) { 12018 12019 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 12020 // At most one list item can be an array item derived from a given 12021 // variable in map clauses of the same construct. 12022 if (CurrentRegionOnly && 12023 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 12024 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) && 12025 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 12026 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) { 12027 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 12028 diag::err_omp_multiple_array_items_in_map_clause) 12029 << CI->getAssociatedExpression()->getSourceRange(); 12030 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 12031 diag::note_used_here) 12032 << SI->getAssociatedExpression()->getSourceRange(); 12033 return true; 12034 } 12035 12036 // Do both expressions have the same kind? 12037 if (CI->getAssociatedExpression()->getStmtClass() != 12038 SI->getAssociatedExpression()->getStmtClass()) 12039 break; 12040 12041 // Are we dealing with different variables/fields? 12042 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 12043 break; 12044 } 12045 // Check if the extra components of the expressions in the enclosing 12046 // data environment are redundant for the current base declaration. 12047 // If they are, the maps completely overlap, which is legal. 12048 for (; SI != SE; ++SI) { 12049 QualType Type; 12050 if (const auto *ASE = 12051 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 12052 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 12053 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 12054 SI->getAssociatedExpression())) { 12055 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 12056 Type = 12057 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 12058 } 12059 if (Type.isNull() || Type->isAnyPointerType() || 12060 checkArrayExpressionDoesNotReferToWholeSize( 12061 SemaRef, SI->getAssociatedExpression(), Type)) 12062 break; 12063 } 12064 12065 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 12066 // List items of map clauses in the same construct must not share 12067 // original storage. 12068 // 12069 // If the expressions are exactly the same or one is a subset of the 12070 // other, it means they are sharing storage. 12071 if (CI == CE && SI == SE) { 12072 if (CurrentRegionOnly) { 12073 if (CKind == OMPC_map) { 12074 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 12075 } else { 12076 assert(CKind == OMPC_to || CKind == OMPC_from); 12077 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 12078 << ERange; 12079 } 12080 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12081 << RE->getSourceRange(); 12082 return true; 12083 } 12084 // If we find the same expression in the enclosing data environment, 12085 // that is legal. 12086 IsEnclosedByDataEnvironmentExpr = true; 12087 return false; 12088 } 12089 12090 QualType DerivedType = 12091 std::prev(CI)->getAssociatedDeclaration()->getType(); 12092 SourceLocation DerivedLoc = 12093 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 12094 12095 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 12096 // If the type of a list item is a reference to a type T then the type 12097 // will be considered to be T for all purposes of this clause. 12098 DerivedType = DerivedType.getNonReferenceType(); 12099 12100 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 12101 // A variable for which the type is pointer and an array section 12102 // derived from that variable must not appear as list items of map 12103 // clauses of the same construct. 12104 // 12105 // Also, cover one of the cases in: 12106 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 12107 // If any part of the original storage of a list item has corresponding 12108 // storage in the device data environment, all of the original storage 12109 // must have corresponding storage in the device data environment. 12110 // 12111 if (DerivedType->isAnyPointerType()) { 12112 if (CI == CE || SI == SE) { 12113 SemaRef.Diag( 12114 DerivedLoc, 12115 diag::err_omp_pointer_mapped_along_with_derived_section) 12116 << DerivedLoc; 12117 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12118 << RE->getSourceRange(); 12119 return true; 12120 } 12121 if (CI->getAssociatedExpression()->getStmtClass() != 12122 SI->getAssociatedExpression()->getStmtClass() || 12123 CI->getAssociatedDeclaration()->getCanonicalDecl() == 12124 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 12125 assert(CI != CE && SI != SE); 12126 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 12127 << DerivedLoc; 12128 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12129 << RE->getSourceRange(); 12130 return true; 12131 } 12132 } 12133 12134 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 12135 // List items of map clauses in the same construct must not share 12136 // original storage. 12137 // 12138 // An expression is a subset of the other. 12139 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 12140 if (CKind == OMPC_map) { 12141 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 12142 } else { 12143 assert(CKind == OMPC_to || CKind == OMPC_from); 12144 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 12145 << ERange; 12146 } 12147 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12148 << RE->getSourceRange(); 12149 return true; 12150 } 12151 12152 // The current expression uses the same base as other expression in the 12153 // data environment but does not contain it completely. 12154 if (!CurrentRegionOnly && SI != SE) 12155 EnclosingExpr = RE; 12156 12157 // The current expression is a subset of the expression in the data 12158 // environment. 12159 IsEnclosedByDataEnvironmentExpr |= 12160 (!CurrentRegionOnly && CI != CE && SI == SE); 12161 12162 return false; 12163 }); 12164 12165 if (CurrentRegionOnly) 12166 return FoundError; 12167 12168 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 12169 // If any part of the original storage of a list item has corresponding 12170 // storage in the device data environment, all of the original storage must 12171 // have corresponding storage in the device data environment. 12172 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 12173 // If a list item is an element of a structure, and a different element of 12174 // the structure has a corresponding list item in the device data environment 12175 // prior to a task encountering the construct associated with the map clause, 12176 // then the list item must also have a corresponding list item in the device 12177 // data environment prior to the task encountering the construct. 12178 // 12179 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 12180 SemaRef.Diag(ELoc, 12181 diag::err_omp_original_storage_is_shared_and_does_not_contain) 12182 << ERange; 12183 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 12184 << EnclosingExpr->getSourceRange(); 12185 return true; 12186 } 12187 12188 return FoundError; 12189 } 12190 12191 namespace { 12192 // Utility struct that gathers all the related lists associated with a mappable 12193 // expression. 12194 struct MappableVarListInfo { 12195 // The list of expressions. 12196 ArrayRef<Expr *> VarList; 12197 // The list of processed expressions. 12198 SmallVector<Expr *, 16> ProcessedVarList; 12199 // The mappble components for each expression. 12200 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 12201 // The base declaration of the variable. 12202 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 12203 12204 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 12205 // We have a list of components and base declarations for each entry in the 12206 // variable list. 12207 VarComponents.reserve(VarList.size()); 12208 VarBaseDeclarations.reserve(VarList.size()); 12209 } 12210 }; 12211 } 12212 12213 // Check the validity of the provided variable list for the provided clause kind 12214 // \a CKind. In the check process the valid expressions, and mappable expression 12215 // components and variables are extracted and used to fill \a Vars, 12216 // \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and 12217 // \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'. 12218 static void 12219 checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS, 12220 OpenMPClauseKind CKind, MappableVarListInfo &MVLI, 12221 SourceLocation StartLoc, 12222 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 12223 bool IsMapTypeImplicit = false) { 12224 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 12225 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 12226 "Unexpected clause kind with mappable expressions!"); 12227 12228 // Keep track of the mappable components and base declarations in this clause. 12229 // Each entry in the list is going to have a list of components associated. We 12230 // record each set of the components so that we can build the clause later on. 12231 // In the end we should have the same amount of declarations and component 12232 // lists. 12233 12234 for (Expr *RE : MVLI.VarList) { 12235 assert(RE && "Null expr in omp to/from/map clause"); 12236 SourceLocation ELoc = RE->getExprLoc(); 12237 12238 const Expr *VE = RE->IgnoreParenLValueCasts(); 12239 12240 if (VE->isValueDependent() || VE->isTypeDependent() || 12241 VE->isInstantiationDependent() || 12242 VE->containsUnexpandedParameterPack()) { 12243 // We can only analyze this information once the missing information is 12244 // resolved. 12245 MVLI.ProcessedVarList.push_back(RE); 12246 continue; 12247 } 12248 12249 Expr *SimpleExpr = RE->IgnoreParenCasts(); 12250 12251 if (!RE->IgnoreParenImpCasts()->isLValue()) { 12252 SemaRef.Diag(ELoc, 12253 diag::err_omp_expected_named_var_member_or_array_expression) 12254 << RE->getSourceRange(); 12255 continue; 12256 } 12257 12258 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 12259 ValueDecl *CurDeclaration = nullptr; 12260 12261 // Obtain the array or member expression bases if required. Also, fill the 12262 // components array with all the components identified in the process. 12263 const Expr *BE = checkMapClauseExpressionBase( 12264 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false); 12265 if (!BE) 12266 continue; 12267 12268 assert(!CurComponents.empty() && 12269 "Invalid mappable expression information."); 12270 12271 // For the following checks, we rely on the base declaration which is 12272 // expected to be associated with the last component. The declaration is 12273 // expected to be a variable or a field (if 'this' is being mapped). 12274 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 12275 assert(CurDeclaration && "Null decl on map clause."); 12276 assert( 12277 CurDeclaration->isCanonicalDecl() && 12278 "Expecting components to have associated only canonical declarations."); 12279 12280 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 12281 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 12282 12283 assert((VD || FD) && "Only variables or fields are expected here!"); 12284 (void)FD; 12285 12286 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 12287 // threadprivate variables cannot appear in a map clause. 12288 // OpenMP 4.5 [2.10.5, target update Construct] 12289 // threadprivate variables cannot appear in a from clause. 12290 if (VD && DSAS->isThreadPrivate(VD)) { 12291 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 12292 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 12293 << getOpenMPClauseName(CKind); 12294 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 12295 continue; 12296 } 12297 12298 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 12299 // A list item cannot appear in both a map clause and a data-sharing 12300 // attribute clause on the same construct. 12301 12302 // Check conflicts with other map clause expressions. We check the conflicts 12303 // with the current construct separately from the enclosing data 12304 // environment, because the restrictions are different. We only have to 12305 // check conflicts across regions for the map clauses. 12306 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 12307 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 12308 break; 12309 if (CKind == OMPC_map && 12310 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 12311 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 12312 break; 12313 12314 // OpenMP 4.5 [2.10.5, target update Construct] 12315 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 12316 // If the type of a list item is a reference to a type T then the type will 12317 // be considered to be T for all purposes of this clause. 12318 auto I = llvm::find_if( 12319 CurComponents, 12320 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 12321 return MC.getAssociatedDeclaration(); 12322 }); 12323 assert(I != CurComponents.end() && "Null decl on map clause."); 12324 QualType Type = 12325 I->getAssociatedDeclaration()->getType().getNonReferenceType(); 12326 12327 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 12328 // A list item in a to or from clause must have a mappable type. 12329 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 12330 // A list item must have a mappable type. 12331 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 12332 DSAS, Type)) 12333 continue; 12334 12335 if (CKind == OMPC_map) { 12336 // target enter data 12337 // OpenMP [2.10.2, Restrictions, p. 99] 12338 // A map-type must be specified in all map clauses and must be either 12339 // to or alloc. 12340 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 12341 if (DKind == OMPD_target_enter_data && 12342 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 12343 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 12344 << (IsMapTypeImplicit ? 1 : 0) 12345 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 12346 << getOpenMPDirectiveName(DKind); 12347 continue; 12348 } 12349 12350 // target exit_data 12351 // OpenMP [2.10.3, Restrictions, p. 102] 12352 // A map-type must be specified in all map clauses and must be either 12353 // from, release, or delete. 12354 if (DKind == OMPD_target_exit_data && 12355 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 12356 MapType == OMPC_MAP_delete)) { 12357 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 12358 << (IsMapTypeImplicit ? 1 : 0) 12359 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 12360 << getOpenMPDirectiveName(DKind); 12361 continue; 12362 } 12363 12364 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 12365 // A list item cannot appear in both a map clause and a data-sharing 12366 // attribute clause on the same construct 12367 if (VD && isOpenMPTargetExecutionDirective(DKind)) { 12368 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 12369 if (isOpenMPPrivate(DVar.CKind)) { 12370 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 12371 << getOpenMPClauseName(DVar.CKind) 12372 << getOpenMPClauseName(OMPC_map) 12373 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 12374 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 12375 continue; 12376 } 12377 } 12378 } 12379 12380 // Save the current expression. 12381 MVLI.ProcessedVarList.push_back(RE); 12382 12383 // Store the components in the stack so that they can be used to check 12384 // against other clauses later on. 12385 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 12386 /*WhereFoundClauseKind=*/OMPC_map); 12387 12388 // Save the components and declaration to create the clause. For purposes of 12389 // the clause creation, any component list that has has base 'this' uses 12390 // null as base declaration. 12391 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 12392 MVLI.VarComponents.back().append(CurComponents.begin(), 12393 CurComponents.end()); 12394 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 12395 : CurDeclaration); 12396 } 12397 } 12398 12399 OMPClause * 12400 Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier, 12401 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, 12402 SourceLocation MapLoc, SourceLocation ColonLoc, 12403 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 12404 SourceLocation LParenLoc, SourceLocation EndLoc) { 12405 MappableVarListInfo MVLI(VarList); 12406 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc, 12407 MapType, IsMapTypeImplicit); 12408 12409 // We need to produce a map clause even if we don't have variables so that 12410 // other diagnostics related with non-existing map clauses are accurate. 12411 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, 12412 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 12413 MVLI.VarComponents, MapTypeModifier, MapType, 12414 IsMapTypeImplicit, MapLoc); 12415 } 12416 12417 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 12418 TypeResult ParsedType) { 12419 assert(ParsedType.isUsable()); 12420 12421 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 12422 if (ReductionType.isNull()) 12423 return QualType(); 12424 12425 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 12426 // A type name in a declare reduction directive cannot be a function type, an 12427 // array type, a reference type, or a type qualified with const, volatile or 12428 // restrict. 12429 if (ReductionType.hasQualifiers()) { 12430 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 12431 return QualType(); 12432 } 12433 12434 if (ReductionType->isFunctionType()) { 12435 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 12436 return QualType(); 12437 } 12438 if (ReductionType->isReferenceType()) { 12439 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 12440 return QualType(); 12441 } 12442 if (ReductionType->isArrayType()) { 12443 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 12444 return QualType(); 12445 } 12446 return ReductionType; 12447 } 12448 12449 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 12450 Scope *S, DeclContext *DC, DeclarationName Name, 12451 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 12452 AccessSpecifier AS, Decl *PrevDeclInScope) { 12453 SmallVector<Decl *, 8> Decls; 12454 Decls.reserve(ReductionTypes.size()); 12455 12456 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 12457 forRedeclarationInCurContext()); 12458 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 12459 // A reduction-identifier may not be re-declared in the current scope for the 12460 // same type or for a type that is compatible according to the base language 12461 // rules. 12462 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 12463 OMPDeclareReductionDecl *PrevDRD = nullptr; 12464 bool InCompoundScope = true; 12465 if (S != nullptr) { 12466 // Find previous declaration with the same name not referenced in other 12467 // declarations. 12468 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 12469 InCompoundScope = 12470 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 12471 LookupName(Lookup, S); 12472 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 12473 /*AllowInlineNamespace=*/false); 12474 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 12475 LookupResult::Filter Filter = Lookup.makeFilter(); 12476 while (Filter.hasNext()) { 12477 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 12478 if (InCompoundScope) { 12479 auto I = UsedAsPrevious.find(PrevDecl); 12480 if (I == UsedAsPrevious.end()) 12481 UsedAsPrevious[PrevDecl] = false; 12482 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 12483 UsedAsPrevious[D] = true; 12484 } 12485 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 12486 PrevDecl->getLocation(); 12487 } 12488 Filter.done(); 12489 if (InCompoundScope) { 12490 for (const auto &PrevData : UsedAsPrevious) { 12491 if (!PrevData.second) { 12492 PrevDRD = PrevData.first; 12493 break; 12494 } 12495 } 12496 } 12497 } else if (PrevDeclInScope != nullptr) { 12498 auto *PrevDRDInScope = PrevDRD = 12499 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 12500 do { 12501 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 12502 PrevDRDInScope->getLocation(); 12503 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 12504 } while (PrevDRDInScope != nullptr); 12505 } 12506 for (const auto &TyData : ReductionTypes) { 12507 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 12508 bool Invalid = false; 12509 if (I != PreviousRedeclTypes.end()) { 12510 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 12511 << TyData.first; 12512 Diag(I->second, diag::note_previous_definition); 12513 Invalid = true; 12514 } 12515 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 12516 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 12517 Name, TyData.first, PrevDRD); 12518 DC->addDecl(DRD); 12519 DRD->setAccess(AS); 12520 Decls.push_back(DRD); 12521 if (Invalid) 12522 DRD->setInvalidDecl(); 12523 else 12524 PrevDRD = DRD; 12525 } 12526 12527 return DeclGroupPtrTy::make( 12528 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 12529 } 12530 12531 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 12532 auto *DRD = cast<OMPDeclareReductionDecl>(D); 12533 12534 // Enter new function scope. 12535 PushFunctionScope(); 12536 setFunctionHasBranchProtectedScope(); 12537 getCurFunction()->setHasOMPDeclareReductionCombiner(); 12538 12539 if (S != nullptr) 12540 PushDeclContext(S, DRD); 12541 else 12542 CurContext = DRD; 12543 12544 PushExpressionEvaluationContext( 12545 ExpressionEvaluationContext::PotentiallyEvaluated); 12546 12547 QualType ReductionType = DRD->getType(); 12548 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 12549 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 12550 // uses semantics of argument handles by value, but it should be passed by 12551 // reference. C lang does not support references, so pass all parameters as 12552 // pointers. 12553 // Create 'T omp_in;' variable. 12554 VarDecl *OmpInParm = 12555 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 12556 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 12557 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 12558 // uses semantics of argument handles by value, but it should be passed by 12559 // reference. C lang does not support references, so pass all parameters as 12560 // pointers. 12561 // Create 'T omp_out;' variable. 12562 VarDecl *OmpOutParm = 12563 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 12564 if (S != nullptr) { 12565 PushOnScopeChains(OmpInParm, S); 12566 PushOnScopeChains(OmpOutParm, S); 12567 } else { 12568 DRD->addDecl(OmpInParm); 12569 DRD->addDecl(OmpOutParm); 12570 } 12571 } 12572 12573 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 12574 auto *DRD = cast<OMPDeclareReductionDecl>(D); 12575 DiscardCleanupsInEvaluationContext(); 12576 PopExpressionEvaluationContext(); 12577 12578 PopDeclContext(); 12579 PopFunctionScopeInfo(); 12580 12581 if (Combiner != nullptr) 12582 DRD->setCombiner(Combiner); 12583 else 12584 DRD->setInvalidDecl(); 12585 } 12586 12587 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 12588 auto *DRD = cast<OMPDeclareReductionDecl>(D); 12589 12590 // Enter new function scope. 12591 PushFunctionScope(); 12592 setFunctionHasBranchProtectedScope(); 12593 12594 if (S != nullptr) 12595 PushDeclContext(S, DRD); 12596 else 12597 CurContext = DRD; 12598 12599 PushExpressionEvaluationContext( 12600 ExpressionEvaluationContext::PotentiallyEvaluated); 12601 12602 QualType ReductionType = DRD->getType(); 12603 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 12604 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 12605 // uses semantics of argument handles by value, but it should be passed by 12606 // reference. C lang does not support references, so pass all parameters as 12607 // pointers. 12608 // Create 'T omp_priv;' variable. 12609 VarDecl *OmpPrivParm = 12610 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 12611 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 12612 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 12613 // uses semantics of argument handles by value, but it should be passed by 12614 // reference. C lang does not support references, so pass all parameters as 12615 // pointers. 12616 // Create 'T omp_orig;' variable. 12617 VarDecl *OmpOrigParm = 12618 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 12619 if (S != nullptr) { 12620 PushOnScopeChains(OmpPrivParm, S); 12621 PushOnScopeChains(OmpOrigParm, S); 12622 } else { 12623 DRD->addDecl(OmpPrivParm); 12624 DRD->addDecl(OmpOrigParm); 12625 } 12626 return OmpPrivParm; 12627 } 12628 12629 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 12630 VarDecl *OmpPrivParm) { 12631 auto *DRD = cast<OMPDeclareReductionDecl>(D); 12632 DiscardCleanupsInEvaluationContext(); 12633 PopExpressionEvaluationContext(); 12634 12635 PopDeclContext(); 12636 PopFunctionScopeInfo(); 12637 12638 if (Initializer != nullptr) { 12639 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 12640 } else if (OmpPrivParm->hasInit()) { 12641 DRD->setInitializer(OmpPrivParm->getInit(), 12642 OmpPrivParm->isDirectInit() 12643 ? OMPDeclareReductionDecl::DirectInit 12644 : OMPDeclareReductionDecl::CopyInit); 12645 } else { 12646 DRD->setInvalidDecl(); 12647 } 12648 } 12649 12650 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 12651 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 12652 for (Decl *D : DeclReductions.get()) { 12653 if (IsValid) { 12654 if (S) 12655 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 12656 /*AddToContext=*/false); 12657 } else { 12658 D->setInvalidDecl(); 12659 } 12660 } 12661 return DeclReductions; 12662 } 12663 12664 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 12665 SourceLocation StartLoc, 12666 SourceLocation LParenLoc, 12667 SourceLocation EndLoc) { 12668 Expr *ValExpr = NumTeams; 12669 Stmt *HelperValStmt = nullptr; 12670 12671 // OpenMP [teams Constrcut, Restrictions] 12672 // The num_teams expression must evaluate to a positive integer value. 12673 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 12674 /*StrictlyPositive=*/true)) 12675 return nullptr; 12676 12677 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12678 OpenMPDirectiveKind CaptureRegion = 12679 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams); 12680 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12681 ValExpr = MakeFullExpr(ValExpr).get(); 12682 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12683 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12684 HelperValStmt = buildPreInits(Context, Captures); 12685 } 12686 12687 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 12688 StartLoc, LParenLoc, EndLoc); 12689 } 12690 12691 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 12692 SourceLocation StartLoc, 12693 SourceLocation LParenLoc, 12694 SourceLocation EndLoc) { 12695 Expr *ValExpr = ThreadLimit; 12696 Stmt *HelperValStmt = nullptr; 12697 12698 // OpenMP [teams Constrcut, Restrictions] 12699 // The thread_limit expression must evaluate to a positive integer value. 12700 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 12701 /*StrictlyPositive=*/true)) 12702 return nullptr; 12703 12704 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12705 OpenMPDirectiveKind CaptureRegion = 12706 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit); 12707 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12708 ValExpr = MakeFullExpr(ValExpr).get(); 12709 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12710 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12711 HelperValStmt = buildPreInits(Context, Captures); 12712 } 12713 12714 return new (Context) OMPThreadLimitClause( 12715 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 12716 } 12717 12718 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 12719 SourceLocation StartLoc, 12720 SourceLocation LParenLoc, 12721 SourceLocation EndLoc) { 12722 Expr *ValExpr = Priority; 12723 12724 // OpenMP [2.9.1, task Constrcut] 12725 // The priority-value is a non-negative numerical scalar expression. 12726 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority, 12727 /*StrictlyPositive=*/false)) 12728 return nullptr; 12729 12730 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc); 12731 } 12732 12733 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 12734 SourceLocation StartLoc, 12735 SourceLocation LParenLoc, 12736 SourceLocation EndLoc) { 12737 Expr *ValExpr = Grainsize; 12738 12739 // OpenMP [2.9.2, taskloop Constrcut] 12740 // The parameter of the grainsize clause must be a positive integer 12741 // expression. 12742 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize, 12743 /*StrictlyPositive=*/true)) 12744 return nullptr; 12745 12746 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc); 12747 } 12748 12749 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 12750 SourceLocation StartLoc, 12751 SourceLocation LParenLoc, 12752 SourceLocation EndLoc) { 12753 Expr *ValExpr = NumTasks; 12754 12755 // OpenMP [2.9.2, taskloop Constrcut] 12756 // The parameter of the num_tasks clause must be a positive integer 12757 // expression. 12758 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks, 12759 /*StrictlyPositive=*/true)) 12760 return nullptr; 12761 12762 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc); 12763 } 12764 12765 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 12766 SourceLocation LParenLoc, 12767 SourceLocation EndLoc) { 12768 // OpenMP [2.13.2, critical construct, Description] 12769 // ... where hint-expression is an integer constant expression that evaluates 12770 // to a valid lock hint. 12771 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 12772 if (HintExpr.isInvalid()) 12773 return nullptr; 12774 return new (Context) 12775 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 12776 } 12777 12778 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 12779 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 12780 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 12781 SourceLocation EndLoc) { 12782 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 12783 std::string Values; 12784 Values += "'"; 12785 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 12786 Values += "'"; 12787 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 12788 << Values << getOpenMPClauseName(OMPC_dist_schedule); 12789 return nullptr; 12790 } 12791 Expr *ValExpr = ChunkSize; 12792 Stmt *HelperValStmt = nullptr; 12793 if (ChunkSize) { 12794 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 12795 !ChunkSize->isInstantiationDependent() && 12796 !ChunkSize->containsUnexpandedParameterPack()) { 12797 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); 12798 ExprResult Val = 12799 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 12800 if (Val.isInvalid()) 12801 return nullptr; 12802 12803 ValExpr = Val.get(); 12804 12805 // OpenMP [2.7.1, Restrictions] 12806 // chunk_size must be a loop invariant integer expression with a positive 12807 // value. 12808 llvm::APSInt Result; 12809 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 12810 if (Result.isSigned() && !Result.isStrictlyPositive()) { 12811 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 12812 << "dist_schedule" << ChunkSize->getSourceRange(); 12813 return nullptr; 12814 } 12815 } else if (getOpenMPCaptureRegionForClause( 12816 DSAStack->getCurrentDirective(), OMPC_dist_schedule) != 12817 OMPD_unknown && 12818 !CurContext->isDependentContext()) { 12819 ValExpr = MakeFullExpr(ValExpr).get(); 12820 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12821 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12822 HelperValStmt = buildPreInits(Context, Captures); 12823 } 12824 } 12825 } 12826 12827 return new (Context) 12828 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 12829 Kind, ValExpr, HelperValStmt); 12830 } 12831 12832 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 12833 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 12834 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 12835 SourceLocation KindLoc, SourceLocation EndLoc) { 12836 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)' 12837 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) { 12838 std::string Value; 12839 SourceLocation Loc; 12840 Value += "'"; 12841 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 12842 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 12843 OMPC_DEFAULTMAP_MODIFIER_tofrom); 12844 Loc = MLoc; 12845 } else { 12846 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 12847 OMPC_DEFAULTMAP_scalar); 12848 Loc = KindLoc; 12849 } 12850 Value += "'"; 12851 Diag(Loc, diag::err_omp_unexpected_clause_value) 12852 << Value << getOpenMPClauseName(OMPC_defaultmap); 12853 return nullptr; 12854 } 12855 DSAStack->setDefaultDMAToFromScalar(StartLoc); 12856 12857 return new (Context) 12858 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 12859 } 12860 12861 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { 12862 DeclContext *CurLexicalContext = getCurLexicalContext(); 12863 if (!CurLexicalContext->isFileContext() && 12864 !CurLexicalContext->isExternCContext() && 12865 !CurLexicalContext->isExternCXXContext() && 12866 !isa<CXXRecordDecl>(CurLexicalContext) && 12867 !isa<ClassTemplateDecl>(CurLexicalContext) && 12868 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 12869 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 12870 Diag(Loc, diag::err_omp_region_not_file_context); 12871 return false; 12872 } 12873 if (IsInOpenMPDeclareTargetContext) { 12874 Diag(Loc, diag::err_omp_enclosed_declare_target); 12875 return false; 12876 } 12877 12878 IsInOpenMPDeclareTargetContext = true; 12879 return true; 12880 } 12881 12882 void Sema::ActOnFinishOpenMPDeclareTargetDirective() { 12883 assert(IsInOpenMPDeclareTargetContext && 12884 "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); 12885 IsInOpenMPDeclareTargetContext = false; 12886 } 12887 12888 void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, 12889 CXXScopeSpec &ScopeSpec, 12890 const DeclarationNameInfo &Id, 12891 OMPDeclareTargetDeclAttr::MapTypeTy MT, 12892 NamedDeclSetType &SameDirectiveDecls) { 12893 LookupResult Lookup(*this, Id, LookupOrdinaryName); 12894 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 12895 12896 if (Lookup.isAmbiguous()) 12897 return; 12898 Lookup.suppressDiagnostics(); 12899 12900 if (!Lookup.isSingleResult()) { 12901 if (TypoCorrection Corrected = 12902 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, 12903 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this), 12904 CTK_ErrorRecovery)) { 12905 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 12906 << Id.getName()); 12907 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 12908 return; 12909 } 12910 12911 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 12912 return; 12913 } 12914 12915 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 12916 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) { 12917 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) 12918 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); 12919 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) { 12920 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT); 12921 ND->addAttr(A); 12922 if (ASTMutationListener *ML = Context.getASTMutationListener()) 12923 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 12924 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc()); 12925 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) { 12926 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link) 12927 << Id.getName(); 12928 } 12929 } else { 12930 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 12931 } 12932 } 12933 12934 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 12935 Sema &SemaRef, Decl *D) { 12936 if (!D) 12937 return; 12938 const Decl *LD = nullptr; 12939 if (isa<TagDecl>(D)) { 12940 LD = cast<TagDecl>(D)->getDefinition(); 12941 } else if (isa<VarDecl>(D)) { 12942 LD = cast<VarDecl>(D)->getDefinition(); 12943 12944 // If this is an implicit variable that is legal and we do not need to do 12945 // anything. 12946 if (cast<VarDecl>(D)->isImplicit()) { 12947 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 12948 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); 12949 D->addAttr(A); 12950 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) 12951 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 12952 return; 12953 } 12954 } else if (const auto *F = dyn_cast<FunctionDecl>(D)) { 12955 const FunctionDecl *FD = nullptr; 12956 if (cast<FunctionDecl>(D)->hasBody(FD)) { 12957 LD = FD; 12958 // If the definition is associated with the current declaration in the 12959 // target region (it can be e.g. a lambda) that is legal and we do not 12960 // need to do anything else. 12961 if (LD == D) { 12962 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 12963 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); 12964 D->addAttr(A); 12965 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) 12966 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 12967 return; 12968 } 12969 } else if (F->isFunctionTemplateSpecialization() && 12970 F->getTemplateSpecializationKind() == 12971 TSK_ImplicitInstantiation) { 12972 // Check if the function is implicitly instantiated from the template 12973 // defined in the declare target region. 12974 const FunctionTemplateDecl *FTD = F->getPrimaryTemplate(); 12975 if (FTD && FTD->hasAttr<OMPDeclareTargetDeclAttr>()) 12976 return; 12977 } 12978 } 12979 if (!LD) 12980 LD = D; 12981 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() && 12982 ((isa<VarDecl>(LD) && !isa<ParmVarDecl>(LD)) || isa<FunctionDecl>(LD))) { 12983 // Outlined declaration is not declared target. 12984 if (!isa<FunctionDecl>(LD)) { 12985 if (LD->isOutOfLine()) { 12986 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context); 12987 SemaRef.Diag(SL, diag::note_used_here) << SR; 12988 } else { 12989 const DeclContext *DC = LD->getDeclContext(); 12990 while (DC && 12991 (!isa<FunctionDecl>(DC) || 12992 !cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())) 12993 DC = DC->getParent(); 12994 if (DC) 12995 return; 12996 12997 // Is not declared in target context. 12998 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context); 12999 SemaRef.Diag(SL, diag::note_used_here) << SR; 13000 } 13001 } 13002 // Mark decl as declared target to prevent further diagnostic. 13003 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 13004 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); 13005 D->addAttr(A); 13006 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) 13007 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 13008 } 13009 } 13010 13011 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 13012 Sema &SemaRef, DSAStackTy *Stack, 13013 ValueDecl *VD) { 13014 return VD->hasAttr<OMPDeclareTargetDeclAttr>() || 13015 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 13016 /*FullCheck=*/false); 13017 } 13018 13019 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 13020 SourceLocation IdLoc) { 13021 if (!D || D->isInvalidDecl()) 13022 return; 13023 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 13024 SourceLocation SL = E ? E->getLocStart() : D->getLocation(); 13025 if (auto *VD = dyn_cast<VarDecl>(D)) { 13026 // Only global variables can be marked as declare target. 13027 if (VD->isLocalVarDeclOrParm()) 13028 return; 13029 // 2.10.6: threadprivate variable cannot appear in a declare target 13030 // directive. 13031 if (DSAStack->isThreadPrivate(VD)) { 13032 Diag(SL, diag::err_omp_threadprivate_in_target); 13033 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 13034 return; 13035 } 13036 } 13037 if (auto *VD = dyn_cast<ValueDecl>(D)) { 13038 // Problem if any with var declared with incomplete type will be reported 13039 // as normal, so no need to check it here. 13040 if ((E || !VD->getType()->isIncompleteType()) && 13041 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) { 13042 // Mark decl as declared target to prevent further diagnostic. 13043 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD) || 13044 isa<FunctionTemplateDecl>(VD)) { 13045 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 13046 Context, OMPDeclareTargetDeclAttr::MT_To); 13047 VD->addAttr(A); 13048 if (ASTMutationListener *ML = Context.getASTMutationListener()) 13049 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A); 13050 } 13051 return; 13052 } 13053 } 13054 if (const auto *FD = dyn_cast<FunctionDecl>(D)) { 13055 if (FD->hasAttr<OMPDeclareTargetDeclAttr>() && 13056 (FD->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() == 13057 OMPDeclareTargetDeclAttr::MT_Link)) { 13058 assert(IdLoc.isValid() && "Source location is expected"); 13059 Diag(IdLoc, diag::err_omp_function_in_link_clause); 13060 Diag(FD->getLocation(), diag::note_defined_here) << FD; 13061 return; 13062 } 13063 } 13064 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) { 13065 if (FTD->hasAttr<OMPDeclareTargetDeclAttr>() && 13066 (FTD->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() == 13067 OMPDeclareTargetDeclAttr::MT_Link)) { 13068 assert(IdLoc.isValid() && "Source location is expected"); 13069 Diag(IdLoc, diag::err_omp_function_in_link_clause); 13070 Diag(FTD->getLocation(), diag::note_defined_here) << FTD; 13071 return; 13072 } 13073 } 13074 if (!E) { 13075 // Checking declaration inside declare target region. 13076 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() && 13077 (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 13078 isa<FunctionTemplateDecl>(D))) { 13079 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 13080 Context, OMPDeclareTargetDeclAttr::MT_To); 13081 D->addAttr(A); 13082 if (ASTMutationListener *ML = Context.getASTMutationListener()) 13083 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 13084 } 13085 return; 13086 } 13087 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 13088 } 13089 13090 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList, 13091 SourceLocation StartLoc, 13092 SourceLocation LParenLoc, 13093 SourceLocation EndLoc) { 13094 MappableVarListInfo MVLI(VarList); 13095 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc); 13096 if (MVLI.ProcessedVarList.empty()) 13097 return nullptr; 13098 13099 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc, 13100 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 13101 MVLI.VarComponents); 13102 } 13103 13104 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList, 13105 SourceLocation StartLoc, 13106 SourceLocation LParenLoc, 13107 SourceLocation EndLoc) { 13108 MappableVarListInfo MVLI(VarList); 13109 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc); 13110 if (MVLI.ProcessedVarList.empty()) 13111 return nullptr; 13112 13113 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc, 13114 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 13115 MVLI.VarComponents); 13116 } 13117 13118 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 13119 SourceLocation StartLoc, 13120 SourceLocation LParenLoc, 13121 SourceLocation EndLoc) { 13122 MappableVarListInfo MVLI(VarList); 13123 SmallVector<Expr *, 8> PrivateCopies; 13124 SmallVector<Expr *, 8> Inits; 13125 13126 for (Expr *RefExpr : VarList) { 13127 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 13128 SourceLocation ELoc; 13129 SourceRange ERange; 13130 Expr *SimpleRefExpr = RefExpr; 13131 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 13132 if (Res.second) { 13133 // It will be analyzed later. 13134 MVLI.ProcessedVarList.push_back(RefExpr); 13135 PrivateCopies.push_back(nullptr); 13136 Inits.push_back(nullptr); 13137 } 13138 ValueDecl *D = Res.first; 13139 if (!D) 13140 continue; 13141 13142 QualType Type = D->getType(); 13143 Type = Type.getNonReferenceType().getUnqualifiedType(); 13144 13145 auto *VD = dyn_cast<VarDecl>(D); 13146 13147 // Item should be a pointer or reference to pointer. 13148 if (!Type->isPointerType()) { 13149 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 13150 << 0 << RefExpr->getSourceRange(); 13151 continue; 13152 } 13153 13154 // Build the private variable and the expression that refers to it. 13155 auto VDPrivate = 13156 buildVarDecl(*this, ELoc, Type, D->getName(), 13157 D->hasAttrs() ? &D->getAttrs() : nullptr, 13158 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 13159 if (VDPrivate->isInvalidDecl()) 13160 continue; 13161 13162 CurContext->addDecl(VDPrivate); 13163 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 13164 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 13165 13166 // Add temporary variable to initialize the private copy of the pointer. 13167 VarDecl *VDInit = 13168 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 13169 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 13170 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 13171 AddInitializerToDecl(VDPrivate, 13172 DefaultLvalueConversion(VDInitRefExpr).get(), 13173 /*DirectInit=*/false); 13174 13175 // If required, build a capture to implement the privatization initialized 13176 // with the current list item value. 13177 DeclRefExpr *Ref = nullptr; 13178 if (!VD) 13179 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 13180 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 13181 PrivateCopies.push_back(VDPrivateRefExpr); 13182 Inits.push_back(VDInitRefExpr); 13183 13184 // We need to add a data sharing attribute for this variable to make sure it 13185 // is correctly captured. A variable that shows up in a use_device_ptr has 13186 // similar properties of a first private variable. 13187 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 13188 13189 // Create a mappable component for the list item. List items in this clause 13190 // only need a component. 13191 MVLI.VarBaseDeclarations.push_back(D); 13192 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 13193 MVLI.VarComponents.back().push_back( 13194 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D)); 13195 } 13196 13197 if (MVLI.ProcessedVarList.empty()) 13198 return nullptr; 13199 13200 return OMPUseDevicePtrClause::Create( 13201 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList, 13202 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents); 13203 } 13204 13205 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 13206 SourceLocation StartLoc, 13207 SourceLocation LParenLoc, 13208 SourceLocation EndLoc) { 13209 MappableVarListInfo MVLI(VarList); 13210 for (Expr *RefExpr : VarList) { 13211 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 13212 SourceLocation ELoc; 13213 SourceRange ERange; 13214 Expr *SimpleRefExpr = RefExpr; 13215 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 13216 if (Res.second) { 13217 // It will be analyzed later. 13218 MVLI.ProcessedVarList.push_back(RefExpr); 13219 } 13220 ValueDecl *D = Res.first; 13221 if (!D) 13222 continue; 13223 13224 QualType Type = D->getType(); 13225 // item should be a pointer or array or reference to pointer or array 13226 if (!Type.getNonReferenceType()->isPointerType() && 13227 !Type.getNonReferenceType()->isArrayType()) { 13228 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 13229 << 0 << RefExpr->getSourceRange(); 13230 continue; 13231 } 13232 13233 // Check if the declaration in the clause does not show up in any data 13234 // sharing attribute. 13235 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 13236 if (isOpenMPPrivate(DVar.CKind)) { 13237 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 13238 << getOpenMPClauseName(DVar.CKind) 13239 << getOpenMPClauseName(OMPC_is_device_ptr) 13240 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 13241 reportOriginalDsa(*this, DSAStack, D, DVar); 13242 continue; 13243 } 13244 13245 const Expr *ConflictExpr; 13246 if (DSAStack->checkMappableExprComponentListsForDecl( 13247 D, /*CurrentRegionOnly=*/true, 13248 [&ConflictExpr]( 13249 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 13250 OpenMPClauseKind) -> bool { 13251 ConflictExpr = R.front().getAssociatedExpression(); 13252 return true; 13253 })) { 13254 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 13255 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 13256 << ConflictExpr->getSourceRange(); 13257 continue; 13258 } 13259 13260 // Store the components in the stack so that they can be used to check 13261 // against other clauses later on. 13262 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D); 13263 DSAStack->addMappableExpressionComponents( 13264 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 13265 13266 // Record the expression we've just processed. 13267 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 13268 13269 // Create a mappable component for the list item. List items in this clause 13270 // only need a component. We use a null declaration to signal fields in 13271 // 'this'. 13272 assert((isa<DeclRefExpr>(SimpleRefExpr) || 13273 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 13274 "Unexpected device pointer expression!"); 13275 MVLI.VarBaseDeclarations.push_back( 13276 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 13277 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 13278 MVLI.VarComponents.back().push_back(MC); 13279 } 13280 13281 if (MVLI.ProcessedVarList.empty()) 13282 return nullptr; 13283 13284 return OMPIsDevicePtrClause::Create( 13285 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList, 13286 MVLI.VarBaseDeclarations, MVLI.VarComponents); 13287 } 13288