1 //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// \file 10 /// This file implements semantic analysis for OpenMP directives and 11 /// clauses. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #include "TreeTransform.h" 16 #include "clang/AST/ASTContext.h" 17 #include "clang/AST/ASTMutationListener.h" 18 #include "clang/AST/CXXInheritance.h" 19 #include "clang/AST/Decl.h" 20 #include "clang/AST/DeclCXX.h" 21 #include "clang/AST/DeclOpenMP.h" 22 #include "clang/AST/StmtCXX.h" 23 #include "clang/AST/StmtOpenMP.h" 24 #include "clang/AST/StmtVisitor.h" 25 #include "clang/Basic/OpenMPKinds.h" 26 #include "clang/Sema/Initialization.h" 27 #include "clang/Sema/Lookup.h" 28 #include "clang/Sema/Scope.h" 29 #include "clang/Sema/ScopeInfo.h" 30 #include "clang/Sema/SemaInternal.h" 31 #include "llvm/ADT/PointerEmbeddedInt.h" 32 using namespace clang; 33 34 //===----------------------------------------------------------------------===// 35 // Stack of data-sharing attributes for variables 36 //===----------------------------------------------------------------------===// 37 38 static const Expr *checkMapClauseExpressionBase( 39 Sema &SemaRef, Expr *E, 40 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 41 OpenMPClauseKind CKind, bool NoDiagnose); 42 43 namespace { 44 /// Default data sharing attributes, which can be applied to directive. 45 enum DefaultDataSharingAttributes { 46 DSA_unspecified = 0, /// Data sharing attribute not specified. 47 DSA_none = 1 << 0, /// Default data sharing attribute 'none'. 48 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'. 49 }; 50 51 /// Attributes of the defaultmap clause. 52 enum DefaultMapAttributes { 53 DMA_unspecified, /// Default mapping is not specified. 54 DMA_tofrom_scalar, /// Default mapping is 'tofrom:scalar'. 55 }; 56 57 /// Stack for tracking declarations used in OpenMP directives and 58 /// clauses and their data-sharing attributes. 59 class DSAStackTy { 60 public: 61 struct DSAVarData { 62 OpenMPDirectiveKind DKind = OMPD_unknown; 63 OpenMPClauseKind CKind = OMPC_unknown; 64 const Expr *RefExpr = nullptr; 65 DeclRefExpr *PrivateCopy = nullptr; 66 SourceLocation ImplicitDSALoc; 67 DSAVarData() = default; 68 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 69 const Expr *RefExpr, DeclRefExpr *PrivateCopy, 70 SourceLocation ImplicitDSALoc) 71 : DKind(DKind), CKind(CKind), RefExpr(RefExpr), 72 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {} 73 }; 74 using OperatorOffsetTy = 75 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>; 76 using DoacrossDependMapTy = 77 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>; 78 79 private: 80 struct DSAInfo { 81 OpenMPClauseKind Attributes = OMPC_unknown; 82 /// Pointer to a reference expression and a flag which shows that the 83 /// variable is marked as lastprivate(true) or not (false). 84 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr; 85 DeclRefExpr *PrivateCopy = nullptr; 86 }; 87 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>; 88 using AlignedMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>; 89 using LCDeclInfo = std::pair<unsigned, VarDecl *>; 90 using LoopControlVariablesMapTy = 91 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>; 92 /// Struct that associates a component with the clause kind where they are 93 /// found. 94 struct MappedExprComponentTy { 95 OMPClauseMappableExprCommon::MappableExprComponentLists Components; 96 OpenMPClauseKind Kind = OMPC_unknown; 97 }; 98 using MappedExprComponentsTy = 99 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>; 100 using CriticalsWithHintsTy = 101 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>; 102 struct ReductionData { 103 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>; 104 SourceRange ReductionRange; 105 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp; 106 ReductionData() = default; 107 void set(BinaryOperatorKind BO, SourceRange RR) { 108 ReductionRange = RR; 109 ReductionOp = BO; 110 } 111 void set(const Expr *RefExpr, SourceRange RR) { 112 ReductionRange = RR; 113 ReductionOp = RefExpr; 114 } 115 }; 116 using DeclReductionMapTy = 117 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>; 118 119 struct SharingMapTy { 120 DeclSAMapTy SharingMap; 121 DeclReductionMapTy ReductionMap; 122 AlignedMapTy AlignedMap; 123 MappedExprComponentsTy MappedExprComponents; 124 LoopControlVariablesMapTy LCVMap; 125 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified; 126 SourceLocation DefaultAttrLoc; 127 DefaultMapAttributes DefaultMapAttr = DMA_unspecified; 128 SourceLocation DefaultMapAttrLoc; 129 OpenMPDirectiveKind Directive = OMPD_unknown; 130 DeclarationNameInfo DirectiveName; 131 Scope *CurScope = nullptr; 132 SourceLocation ConstructLoc; 133 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to 134 /// get the data (loop counters etc.) about enclosing loop-based construct. 135 /// This data is required during codegen. 136 DoacrossDependMapTy DoacrossDepends; 137 /// first argument (Expr *) contains optional argument of the 138 /// 'ordered' clause, the second one is true if the regions has 'ordered' 139 /// clause, false otherwise. 140 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion; 141 unsigned AssociatedLoops = 1; 142 const Decl *PossiblyLoopCounter = nullptr; 143 bool NowaitRegion = false; 144 bool CancelRegion = false; 145 bool LoopStart = false; 146 SourceLocation InnerTeamsRegionLoc; 147 /// Reference to the taskgroup task_reduction reference expression. 148 Expr *TaskgroupReductionRef = nullptr; 149 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 150 Scope *CurScope, SourceLocation Loc) 151 : Directive(DKind), DirectiveName(Name), CurScope(CurScope), 152 ConstructLoc(Loc) {} 153 SharingMapTy() = default; 154 }; 155 156 using StackTy = SmallVector<SharingMapTy, 4>; 157 158 /// Stack of used declaration and their data-sharing attributes. 159 DeclSAMapTy Threadprivates; 160 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr; 161 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack; 162 /// true, if check for DSA must be from parent directive, false, if 163 /// from current directive. 164 OpenMPClauseKind ClauseKindMode = OMPC_unknown; 165 Sema &SemaRef; 166 bool ForceCapturing = false; 167 /// true if all the vaiables in the target executable directives must be 168 /// captured by reference. 169 bool ForceCaptureByReferenceInTargetExecutable = false; 170 CriticalsWithHintsTy Criticals; 171 172 using iterator = StackTy::const_reverse_iterator; 173 174 DSAVarData getDSA(iterator &Iter, ValueDecl *D) const; 175 176 /// Checks if the variable is a local for OpenMP region. 177 bool isOpenMPLocal(VarDecl *D, iterator Iter) const; 178 179 bool isStackEmpty() const { 180 return Stack.empty() || 181 Stack.back().second != CurrentNonCapturingFunctionScope || 182 Stack.back().first.empty(); 183 } 184 185 /// Vector of previously declared requires directives 186 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls; 187 188 public: 189 explicit DSAStackTy(Sema &S) : SemaRef(S) {} 190 191 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 192 OpenMPClauseKind getClauseParsingMode() const { 193 assert(isClauseParsingMode() && "Must be in clause parsing mode."); 194 return ClauseKindMode; 195 } 196 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 197 198 bool isForceVarCapturing() const { return ForceCapturing; } 199 void setForceVarCapturing(bool V) { ForceCapturing = V; } 200 201 void setForceCaptureByReferenceInTargetExecutable(bool V) { 202 ForceCaptureByReferenceInTargetExecutable = V; 203 } 204 bool isForceCaptureByReferenceInTargetExecutable() const { 205 return ForceCaptureByReferenceInTargetExecutable; 206 } 207 208 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 209 Scope *CurScope, SourceLocation Loc) { 210 if (Stack.empty() || 211 Stack.back().second != CurrentNonCapturingFunctionScope) 212 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope); 213 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc); 214 Stack.back().first.back().DefaultAttrLoc = Loc; 215 } 216 217 void pop() { 218 assert(!Stack.back().first.empty() && 219 "Data-sharing attributes stack is empty!"); 220 Stack.back().first.pop_back(); 221 } 222 223 /// Marks that we're started loop parsing. 224 void loopInit() { 225 assert(isOpenMPLoopDirective(getCurrentDirective()) && 226 "Expected loop-based directive."); 227 Stack.back().first.back().LoopStart = true; 228 } 229 /// Start capturing of the variables in the loop context. 230 void loopStart() { 231 assert(isOpenMPLoopDirective(getCurrentDirective()) && 232 "Expected loop-based directive."); 233 Stack.back().first.back().LoopStart = false; 234 } 235 /// true, if variables are captured, false otherwise. 236 bool isLoopStarted() const { 237 assert(isOpenMPLoopDirective(getCurrentDirective()) && 238 "Expected loop-based directive."); 239 return !Stack.back().first.back().LoopStart; 240 } 241 /// Marks (or clears) declaration as possibly loop counter. 242 void resetPossibleLoopCounter(const Decl *D = nullptr) { 243 Stack.back().first.back().PossiblyLoopCounter = 244 D ? D->getCanonicalDecl() : D; 245 } 246 /// Gets the possible loop counter decl. 247 const Decl *getPossiblyLoopCunter() const { 248 return Stack.back().first.back().PossiblyLoopCounter; 249 } 250 /// Start new OpenMP region stack in new non-capturing function. 251 void pushFunction() { 252 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction(); 253 assert(!isa<CapturingScopeInfo>(CurFnScope)); 254 CurrentNonCapturingFunctionScope = CurFnScope; 255 } 256 /// Pop region stack for non-capturing function. 257 void popFunction(const FunctionScopeInfo *OldFSI) { 258 if (!Stack.empty() && Stack.back().second == OldFSI) { 259 assert(Stack.back().first.empty()); 260 Stack.pop_back(); 261 } 262 CurrentNonCapturingFunctionScope = nullptr; 263 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) { 264 if (!isa<CapturingScopeInfo>(FSI)) { 265 CurrentNonCapturingFunctionScope = FSI; 266 break; 267 } 268 } 269 } 270 271 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) { 272 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint); 273 } 274 const std::pair<const OMPCriticalDirective *, llvm::APSInt> 275 getCriticalWithHint(const DeclarationNameInfo &Name) const { 276 auto I = Criticals.find(Name.getAsString()); 277 if (I != Criticals.end()) 278 return I->second; 279 return std::make_pair(nullptr, llvm::APSInt()); 280 } 281 /// If 'aligned' declaration for given variable \a D was not seen yet, 282 /// add it and return NULL; otherwise return previous occurrence's expression 283 /// for diagnostics. 284 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE); 285 286 /// Register specified variable as loop control variable. 287 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture); 288 /// Check if the specified variable is a loop control variable for 289 /// current region. 290 /// \return The index of the loop control variable in the list of associated 291 /// for-loops (from outer to inner). 292 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const; 293 /// Check if the specified variable is a loop control variable for 294 /// parent region. 295 /// \return The index of the loop control variable in the list of associated 296 /// for-loops (from outer to inner). 297 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const; 298 /// Get the loop control variable for the I-th loop (or nullptr) in 299 /// parent directive. 300 const ValueDecl *getParentLoopControlVariable(unsigned I) const; 301 302 /// Adds explicit data sharing attribute to the specified declaration. 303 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 304 DeclRefExpr *PrivateCopy = nullptr); 305 306 /// Adds additional information for the reduction items with the reduction id 307 /// represented as an operator. 308 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 309 BinaryOperatorKind BOK); 310 /// Adds additional information for the reduction items with the reduction id 311 /// represented as reduction identifier. 312 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 313 const Expr *ReductionRef); 314 /// Returns the location and reduction operation from the innermost parent 315 /// region for the given \p D. 316 const DSAVarData 317 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 318 BinaryOperatorKind &BOK, 319 Expr *&TaskgroupDescriptor) const; 320 /// Returns the location and reduction operation from the innermost parent 321 /// region for the given \p D. 322 const DSAVarData 323 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 324 const Expr *&ReductionRef, 325 Expr *&TaskgroupDescriptor) const; 326 /// Return reduction reference expression for the current taskgroup. 327 Expr *getTaskgroupReductionRef() const { 328 assert(Stack.back().first.back().Directive == OMPD_taskgroup && 329 "taskgroup reference expression requested for non taskgroup " 330 "directive."); 331 return Stack.back().first.back().TaskgroupReductionRef; 332 } 333 /// Checks if the given \p VD declaration is actually a taskgroup reduction 334 /// descriptor variable at the \p Level of OpenMP regions. 335 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const { 336 return Stack.back().first[Level].TaskgroupReductionRef && 337 cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef) 338 ->getDecl() == VD; 339 } 340 341 /// Returns data sharing attributes from top of the stack for the 342 /// specified declaration. 343 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 344 /// Returns data-sharing attributes for the specified declaration. 345 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const; 346 /// Checks if the specified variables has data-sharing attributes which 347 /// match specified \a CPred predicate in any directive which matches \a DPred 348 /// predicate. 349 const DSAVarData 350 hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 351 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 352 bool FromParent) const; 353 /// Checks if the specified variables has data-sharing attributes which 354 /// match specified \a CPred predicate in any innermost directive which 355 /// matches \a DPred predicate. 356 const DSAVarData 357 hasInnermostDSA(ValueDecl *D, 358 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 359 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 360 bool FromParent) const; 361 /// Checks if the specified variables has explicit data-sharing 362 /// attributes which match specified \a CPred predicate at the specified 363 /// OpenMP region. 364 bool hasExplicitDSA(const ValueDecl *D, 365 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 366 unsigned Level, bool NotLastprivate = false) const; 367 368 /// Returns true if the directive at level \Level matches in the 369 /// specified \a DPred predicate. 370 bool hasExplicitDirective( 371 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 372 unsigned Level) const; 373 374 /// Finds a directive which matches specified \a DPred predicate. 375 bool hasDirective( 376 const llvm::function_ref<bool( 377 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)> 378 DPred, 379 bool FromParent) const; 380 381 /// Returns currently analyzed directive. 382 OpenMPDirectiveKind getCurrentDirective() const { 383 return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive; 384 } 385 /// Returns directive kind at specified level. 386 OpenMPDirectiveKind getDirective(unsigned Level) const { 387 assert(!isStackEmpty() && "No directive at specified level."); 388 return Stack.back().first[Level].Directive; 389 } 390 /// Returns parent directive. 391 OpenMPDirectiveKind getParentDirective() const { 392 if (isStackEmpty() || Stack.back().first.size() == 1) 393 return OMPD_unknown; 394 return std::next(Stack.back().first.rbegin())->Directive; 395 } 396 397 /// Add requires decl to internal vector 398 void addRequiresDecl(OMPRequiresDecl *RD) { 399 RequiresDecls.push_back(RD); 400 } 401 402 /// Checks for a duplicate clause amongst previously declared requires 403 /// directives 404 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const { 405 bool IsDuplicate = false; 406 for (OMPClause *CNew : ClauseList) { 407 for (const OMPRequiresDecl *D : RequiresDecls) { 408 for (const OMPClause *CPrev : D->clauselists()) { 409 if (CNew->getClauseKind() == CPrev->getClauseKind()) { 410 SemaRef.Diag(CNew->getBeginLoc(), 411 diag::err_omp_requires_clause_redeclaration) 412 << getOpenMPClauseName(CNew->getClauseKind()); 413 SemaRef.Diag(CPrev->getBeginLoc(), 414 diag::note_omp_requires_previous_clause) 415 << getOpenMPClauseName(CPrev->getClauseKind()); 416 IsDuplicate = true; 417 } 418 } 419 } 420 } 421 return IsDuplicate; 422 } 423 424 /// Set default data sharing attribute to none. 425 void setDefaultDSANone(SourceLocation Loc) { 426 assert(!isStackEmpty()); 427 Stack.back().first.back().DefaultAttr = DSA_none; 428 Stack.back().first.back().DefaultAttrLoc = Loc; 429 } 430 /// Set default data sharing attribute to shared. 431 void setDefaultDSAShared(SourceLocation Loc) { 432 assert(!isStackEmpty()); 433 Stack.back().first.back().DefaultAttr = DSA_shared; 434 Stack.back().first.back().DefaultAttrLoc = Loc; 435 } 436 /// Set default data mapping attribute to 'tofrom:scalar'. 437 void setDefaultDMAToFromScalar(SourceLocation Loc) { 438 assert(!isStackEmpty()); 439 Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar; 440 Stack.back().first.back().DefaultMapAttrLoc = Loc; 441 } 442 443 DefaultDataSharingAttributes getDefaultDSA() const { 444 return isStackEmpty() ? DSA_unspecified 445 : Stack.back().first.back().DefaultAttr; 446 } 447 SourceLocation getDefaultDSALocation() const { 448 return isStackEmpty() ? SourceLocation() 449 : Stack.back().first.back().DefaultAttrLoc; 450 } 451 DefaultMapAttributes getDefaultDMA() const { 452 return isStackEmpty() ? DMA_unspecified 453 : Stack.back().first.back().DefaultMapAttr; 454 } 455 DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const { 456 return Stack.back().first[Level].DefaultMapAttr; 457 } 458 SourceLocation getDefaultDMALocation() const { 459 return isStackEmpty() ? SourceLocation() 460 : Stack.back().first.back().DefaultMapAttrLoc; 461 } 462 463 /// Checks if the specified variable is a threadprivate. 464 bool isThreadPrivate(VarDecl *D) { 465 const DSAVarData DVar = getTopDSA(D, false); 466 return isOpenMPThreadPrivate(DVar.CKind); 467 } 468 469 /// Marks current region as ordered (it has an 'ordered' clause). 470 void setOrderedRegion(bool IsOrdered, const Expr *Param, 471 OMPOrderedClause *Clause) { 472 assert(!isStackEmpty()); 473 if (IsOrdered) 474 Stack.back().first.back().OrderedRegion.emplace(Param, Clause); 475 else 476 Stack.back().first.back().OrderedRegion.reset(); 477 } 478 /// Returns true, if region is ordered (has associated 'ordered' clause), 479 /// false - otherwise. 480 bool isOrderedRegion() const { 481 if (isStackEmpty()) 482 return false; 483 return Stack.back().first.rbegin()->OrderedRegion.hasValue(); 484 } 485 /// Returns optional parameter for the ordered region. 486 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const { 487 if (isStackEmpty() || 488 !Stack.back().first.rbegin()->OrderedRegion.hasValue()) 489 return std::make_pair(nullptr, nullptr); 490 return Stack.back().first.rbegin()->OrderedRegion.getValue(); 491 } 492 /// Returns true, if parent region is ordered (has associated 493 /// 'ordered' clause), false - otherwise. 494 bool isParentOrderedRegion() const { 495 if (isStackEmpty() || Stack.back().first.size() == 1) 496 return false; 497 return std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue(); 498 } 499 /// Returns optional parameter for the ordered region. 500 std::pair<const Expr *, OMPOrderedClause *> 501 getParentOrderedRegionParam() const { 502 if (isStackEmpty() || Stack.back().first.size() == 1 || 503 !std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue()) 504 return std::make_pair(nullptr, nullptr); 505 return std::next(Stack.back().first.rbegin())->OrderedRegion.getValue(); 506 } 507 /// Marks current region as nowait (it has a 'nowait' clause). 508 void setNowaitRegion(bool IsNowait = true) { 509 assert(!isStackEmpty()); 510 Stack.back().first.back().NowaitRegion = IsNowait; 511 } 512 /// Returns true, if parent region is nowait (has associated 513 /// 'nowait' clause), false - otherwise. 514 bool isParentNowaitRegion() const { 515 if (isStackEmpty() || Stack.back().first.size() == 1) 516 return false; 517 return std::next(Stack.back().first.rbegin())->NowaitRegion; 518 } 519 /// Marks parent region as cancel region. 520 void setParentCancelRegion(bool Cancel = true) { 521 if (!isStackEmpty() && Stack.back().first.size() > 1) { 522 auto &StackElemRef = *std::next(Stack.back().first.rbegin()); 523 StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel; 524 } 525 } 526 /// Return true if current region has inner cancel construct. 527 bool isCancelRegion() const { 528 return isStackEmpty() ? false : Stack.back().first.back().CancelRegion; 529 } 530 531 /// Set collapse value for the region. 532 void setAssociatedLoops(unsigned Val) { 533 assert(!isStackEmpty()); 534 Stack.back().first.back().AssociatedLoops = Val; 535 } 536 /// Return collapse value for region. 537 unsigned getAssociatedLoops() const { 538 return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops; 539 } 540 541 /// Marks current target region as one with closely nested teams 542 /// region. 543 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 544 if (!isStackEmpty() && Stack.back().first.size() > 1) { 545 std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc = 546 TeamsRegionLoc; 547 } 548 } 549 /// Returns true, if current region has closely nested teams region. 550 bool hasInnerTeamsRegion() const { 551 return getInnerTeamsRegionLoc().isValid(); 552 } 553 /// Returns location of the nested teams region (if any). 554 SourceLocation getInnerTeamsRegionLoc() const { 555 return isStackEmpty() ? SourceLocation() 556 : Stack.back().first.back().InnerTeamsRegionLoc; 557 } 558 559 Scope *getCurScope() const { 560 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope; 561 } 562 SourceLocation getConstructLoc() const { 563 return isStackEmpty() ? SourceLocation() 564 : Stack.back().first.back().ConstructLoc; 565 } 566 567 /// Do the check specified in \a Check to all component lists and return true 568 /// if any issue is found. 569 bool checkMappableExprComponentListsForDecl( 570 const ValueDecl *VD, bool CurrentRegionOnly, 571 const llvm::function_ref< 572 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 573 OpenMPClauseKind)> 574 Check) const { 575 if (isStackEmpty()) 576 return false; 577 auto SI = Stack.back().first.rbegin(); 578 auto SE = Stack.back().first.rend(); 579 580 if (SI == SE) 581 return false; 582 583 if (CurrentRegionOnly) 584 SE = std::next(SI); 585 else 586 std::advance(SI, 1); 587 588 for (; SI != SE; ++SI) { 589 auto MI = SI->MappedExprComponents.find(VD); 590 if (MI != SI->MappedExprComponents.end()) 591 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 592 MI->second.Components) 593 if (Check(L, MI->second.Kind)) 594 return true; 595 } 596 return false; 597 } 598 599 /// Do the check specified in \a Check to all component lists at a given level 600 /// and return true if any issue is found. 601 bool checkMappableExprComponentListsForDeclAtLevel( 602 const ValueDecl *VD, unsigned Level, 603 const llvm::function_ref< 604 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 605 OpenMPClauseKind)> 606 Check) const { 607 if (isStackEmpty()) 608 return false; 609 610 auto StartI = Stack.back().first.begin(); 611 auto EndI = Stack.back().first.end(); 612 if (std::distance(StartI, EndI) <= (int)Level) 613 return false; 614 std::advance(StartI, Level); 615 616 auto MI = StartI->MappedExprComponents.find(VD); 617 if (MI != StartI->MappedExprComponents.end()) 618 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 619 MI->second.Components) 620 if (Check(L, MI->second.Kind)) 621 return true; 622 return false; 623 } 624 625 /// Create a new mappable expression component list associated with a given 626 /// declaration and initialize it with the provided list of components. 627 void addMappableExpressionComponents( 628 const ValueDecl *VD, 629 OMPClauseMappableExprCommon::MappableExprComponentListRef Components, 630 OpenMPClauseKind WhereFoundClauseKind) { 631 assert(!isStackEmpty() && 632 "Not expecting to retrieve components from a empty stack!"); 633 MappedExprComponentTy &MEC = 634 Stack.back().first.back().MappedExprComponents[VD]; 635 // Create new entry and append the new components there. 636 MEC.Components.resize(MEC.Components.size() + 1); 637 MEC.Components.back().append(Components.begin(), Components.end()); 638 MEC.Kind = WhereFoundClauseKind; 639 } 640 641 unsigned getNestingLevel() const { 642 assert(!isStackEmpty()); 643 return Stack.back().first.size() - 1; 644 } 645 void addDoacrossDependClause(OMPDependClause *C, 646 const OperatorOffsetTy &OpsOffs) { 647 assert(!isStackEmpty() && Stack.back().first.size() > 1); 648 SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin()); 649 assert(isOpenMPWorksharingDirective(StackElem.Directive)); 650 StackElem.DoacrossDepends.try_emplace(C, OpsOffs); 651 } 652 llvm::iterator_range<DoacrossDependMapTy::const_iterator> 653 getDoacrossDependClauses() const { 654 assert(!isStackEmpty()); 655 const SharingMapTy &StackElem = Stack.back().first.back(); 656 if (isOpenMPWorksharingDirective(StackElem.Directive)) { 657 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends; 658 return llvm::make_range(Ref.begin(), Ref.end()); 659 } 660 return llvm::make_range(StackElem.DoacrossDepends.end(), 661 StackElem.DoacrossDepends.end()); 662 } 663 }; 664 bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) { 665 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) || 666 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown; 667 } 668 669 } // namespace 670 671 static const Expr *getExprAsWritten(const Expr *E) { 672 if (const auto *FE = dyn_cast<FullExpr>(E)) 673 E = FE->getSubExpr(); 674 675 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 676 E = MTE->GetTemporaryExpr(); 677 678 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 679 E = Binder->getSubExpr(); 680 681 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 682 E = ICE->getSubExprAsWritten(); 683 return E->IgnoreParens(); 684 } 685 686 static Expr *getExprAsWritten(Expr *E) { 687 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E))); 688 } 689 690 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) { 691 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) 692 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 693 D = ME->getMemberDecl(); 694 const auto *VD = dyn_cast<VarDecl>(D); 695 const auto *FD = dyn_cast<FieldDecl>(D); 696 if (VD != nullptr) { 697 VD = VD->getCanonicalDecl(); 698 D = VD; 699 } else { 700 assert(FD); 701 FD = FD->getCanonicalDecl(); 702 D = FD; 703 } 704 return D; 705 } 706 707 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 708 return const_cast<ValueDecl *>( 709 getCanonicalDecl(const_cast<const ValueDecl *>(D))); 710 } 711 712 DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter, 713 ValueDecl *D) const { 714 D = getCanonicalDecl(D); 715 auto *VD = dyn_cast<VarDecl>(D); 716 const auto *FD = dyn_cast<FieldDecl>(D); 717 DSAVarData DVar; 718 if (isStackEmpty() || Iter == Stack.back().first.rend()) { 719 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 720 // in a region but not in construct] 721 // File-scope or namespace-scope variables referenced in called routines 722 // in the region are shared unless they appear in a threadprivate 723 // directive. 724 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD)) 725 DVar.CKind = OMPC_shared; 726 727 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 728 // in a region but not in construct] 729 // Variables with static storage duration that are declared in called 730 // routines in the region are shared. 731 if (VD && VD->hasGlobalStorage()) 732 DVar.CKind = OMPC_shared; 733 734 // Non-static data members are shared by default. 735 if (FD) 736 DVar.CKind = OMPC_shared; 737 738 return DVar; 739 } 740 741 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 742 // in a Construct, C/C++, predetermined, p.1] 743 // Variables with automatic storage duration that are declared in a scope 744 // inside the construct are private. 745 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 746 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 747 DVar.CKind = OMPC_private; 748 return DVar; 749 } 750 751 DVar.DKind = Iter->Directive; 752 // Explicitly specified attributes and local variables with predetermined 753 // attributes. 754 if (Iter->SharingMap.count(D)) { 755 const DSAInfo &Data = Iter->SharingMap.lookup(D); 756 DVar.RefExpr = Data.RefExpr.getPointer(); 757 DVar.PrivateCopy = Data.PrivateCopy; 758 DVar.CKind = Data.Attributes; 759 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 760 return DVar; 761 } 762 763 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 764 // in a Construct, C/C++, implicitly determined, p.1] 765 // In a parallel or task construct, the data-sharing attributes of these 766 // variables are determined by the default clause, if present. 767 switch (Iter->DefaultAttr) { 768 case DSA_shared: 769 DVar.CKind = OMPC_shared; 770 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 771 return DVar; 772 case DSA_none: 773 return DVar; 774 case DSA_unspecified: 775 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 776 // in a Construct, implicitly determined, p.2] 777 // In a parallel construct, if no default clause is present, these 778 // variables are shared. 779 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 780 if (isOpenMPParallelDirective(DVar.DKind) || 781 isOpenMPTeamsDirective(DVar.DKind)) { 782 DVar.CKind = OMPC_shared; 783 return DVar; 784 } 785 786 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 787 // in a Construct, implicitly determined, p.4] 788 // In a task construct, if no default clause is present, a variable that in 789 // the enclosing context is determined to be shared by all implicit tasks 790 // bound to the current team is shared. 791 if (isOpenMPTaskingDirective(DVar.DKind)) { 792 DSAVarData DVarTemp; 793 iterator I = Iter, E = Stack.back().first.rend(); 794 do { 795 ++I; 796 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 797 // Referenced in a Construct, implicitly determined, p.6] 798 // In a task construct, if no default clause is present, a variable 799 // whose data-sharing attribute is not determined by the rules above is 800 // firstprivate. 801 DVarTemp = getDSA(I, D); 802 if (DVarTemp.CKind != OMPC_shared) { 803 DVar.RefExpr = nullptr; 804 DVar.CKind = OMPC_firstprivate; 805 return DVar; 806 } 807 } while (I != E && !isParallelOrTaskRegion(I->Directive)); 808 DVar.CKind = 809 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 810 return DVar; 811 } 812 } 813 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 814 // in a Construct, implicitly determined, p.3] 815 // For constructs other than task, if no default clause is present, these 816 // variables inherit their data-sharing attributes from the enclosing 817 // context. 818 return getDSA(++Iter, D); 819 } 820 821 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D, 822 const Expr *NewDE) { 823 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 824 D = getCanonicalDecl(D); 825 SharingMapTy &StackElem = Stack.back().first.back(); 826 auto It = StackElem.AlignedMap.find(D); 827 if (It == StackElem.AlignedMap.end()) { 828 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 829 StackElem.AlignedMap[D] = NewDE; 830 return nullptr; 831 } 832 assert(It->second && "Unexpected nullptr expr in the aligned map"); 833 return It->second; 834 } 835 836 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) { 837 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 838 D = getCanonicalDecl(D); 839 SharingMapTy &StackElem = Stack.back().first.back(); 840 StackElem.LCVMap.try_emplace( 841 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)); 842 } 843 844 const DSAStackTy::LCDeclInfo 845 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const { 846 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 847 D = getCanonicalDecl(D); 848 const SharingMapTy &StackElem = Stack.back().first.back(); 849 auto It = StackElem.LCVMap.find(D); 850 if (It != StackElem.LCVMap.end()) 851 return It->second; 852 return {0, nullptr}; 853 } 854 855 const DSAStackTy::LCDeclInfo 856 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const { 857 assert(!isStackEmpty() && Stack.back().first.size() > 1 && 858 "Data-sharing attributes stack is empty"); 859 D = getCanonicalDecl(D); 860 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin()); 861 auto It = StackElem.LCVMap.find(D); 862 if (It != StackElem.LCVMap.end()) 863 return It->second; 864 return {0, nullptr}; 865 } 866 867 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const { 868 assert(!isStackEmpty() && Stack.back().first.size() > 1 && 869 "Data-sharing attributes stack is empty"); 870 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin()); 871 if (StackElem.LCVMap.size() < I) 872 return nullptr; 873 for (const auto &Pair : StackElem.LCVMap) 874 if (Pair.second.first == I) 875 return Pair.first; 876 return nullptr; 877 } 878 879 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 880 DeclRefExpr *PrivateCopy) { 881 D = getCanonicalDecl(D); 882 if (A == OMPC_threadprivate) { 883 DSAInfo &Data = Threadprivates[D]; 884 Data.Attributes = A; 885 Data.RefExpr.setPointer(E); 886 Data.PrivateCopy = nullptr; 887 } else { 888 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 889 DSAInfo &Data = Stack.back().first.back().SharingMap[D]; 890 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 891 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 892 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 893 (isLoopControlVariable(D).first && A == OMPC_private)); 894 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 895 Data.RefExpr.setInt(/*IntVal=*/true); 896 return; 897 } 898 const bool IsLastprivate = 899 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 900 Data.Attributes = A; 901 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 902 Data.PrivateCopy = PrivateCopy; 903 if (PrivateCopy) { 904 DSAInfo &Data = 905 Stack.back().first.back().SharingMap[PrivateCopy->getDecl()]; 906 Data.Attributes = A; 907 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 908 Data.PrivateCopy = nullptr; 909 } 910 } 911 } 912 913 /// Build a variable declaration for OpenMP loop iteration variable. 914 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 915 StringRef Name, const AttrVec *Attrs = nullptr, 916 DeclRefExpr *OrigRef = nullptr) { 917 DeclContext *DC = SemaRef.CurContext; 918 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 919 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 920 auto *Decl = 921 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 922 if (Attrs) { 923 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 924 I != E; ++I) 925 Decl->addAttr(*I); 926 } 927 Decl->setImplicit(); 928 if (OrigRef) { 929 Decl->addAttr( 930 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef)); 931 } 932 return Decl; 933 } 934 935 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 936 SourceLocation Loc, 937 bool RefersToCapture = false) { 938 D->setReferenced(); 939 D->markUsed(S.Context); 940 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 941 SourceLocation(), D, RefersToCapture, Loc, Ty, 942 VK_LValue); 943 } 944 945 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 946 BinaryOperatorKind BOK) { 947 D = getCanonicalDecl(D); 948 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 949 assert( 950 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction && 951 "Additional reduction info may be specified only for reduction items."); 952 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D]; 953 assert(ReductionData.ReductionRange.isInvalid() && 954 Stack.back().first.back().Directive == OMPD_taskgroup && 955 "Additional reduction info may be specified only once for reduction " 956 "items."); 957 ReductionData.set(BOK, SR); 958 Expr *&TaskgroupReductionRef = 959 Stack.back().first.back().TaskgroupReductionRef; 960 if (!TaskgroupReductionRef) { 961 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 962 SemaRef.Context.VoidPtrTy, ".task_red."); 963 TaskgroupReductionRef = 964 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 965 } 966 } 967 968 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 969 const Expr *ReductionRef) { 970 D = getCanonicalDecl(D); 971 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 972 assert( 973 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction && 974 "Additional reduction info may be specified only for reduction items."); 975 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D]; 976 assert(ReductionData.ReductionRange.isInvalid() && 977 Stack.back().first.back().Directive == OMPD_taskgroup && 978 "Additional reduction info may be specified only once for reduction " 979 "items."); 980 ReductionData.set(ReductionRef, SR); 981 Expr *&TaskgroupReductionRef = 982 Stack.back().first.back().TaskgroupReductionRef; 983 if (!TaskgroupReductionRef) { 984 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 985 SemaRef.Context.VoidPtrTy, ".task_red."); 986 TaskgroupReductionRef = 987 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 988 } 989 } 990 991 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 992 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK, 993 Expr *&TaskgroupDescriptor) const { 994 D = getCanonicalDecl(D); 995 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 996 if (Stack.back().first.empty()) 997 return DSAVarData(); 998 for (iterator I = std::next(Stack.back().first.rbegin(), 1), 999 E = Stack.back().first.rend(); 1000 I != E; std::advance(I, 1)) { 1001 const DSAInfo &Data = I->SharingMap.lookup(D); 1002 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup) 1003 continue; 1004 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1005 if (!ReductionData.ReductionOp || 1006 ReductionData.ReductionOp.is<const Expr *>()) 1007 return DSAVarData(); 1008 SR = ReductionData.ReductionRange; 1009 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>(); 1010 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1011 "expression for the descriptor is not " 1012 "set."); 1013 TaskgroupDescriptor = I->TaskgroupReductionRef; 1014 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(), 1015 Data.PrivateCopy, I->DefaultAttrLoc); 1016 } 1017 return DSAVarData(); 1018 } 1019 1020 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1021 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef, 1022 Expr *&TaskgroupDescriptor) const { 1023 D = getCanonicalDecl(D); 1024 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1025 if (Stack.back().first.empty()) 1026 return DSAVarData(); 1027 for (iterator I = std::next(Stack.back().first.rbegin(), 1), 1028 E = Stack.back().first.rend(); 1029 I != E; std::advance(I, 1)) { 1030 const DSAInfo &Data = I->SharingMap.lookup(D); 1031 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup) 1032 continue; 1033 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1034 if (!ReductionData.ReductionOp || 1035 !ReductionData.ReductionOp.is<const Expr *>()) 1036 return DSAVarData(); 1037 SR = ReductionData.ReductionRange; 1038 ReductionRef = ReductionData.ReductionOp.get<const Expr *>(); 1039 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1040 "expression for the descriptor is not " 1041 "set."); 1042 TaskgroupDescriptor = I->TaskgroupReductionRef; 1043 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(), 1044 Data.PrivateCopy, I->DefaultAttrLoc); 1045 } 1046 return DSAVarData(); 1047 } 1048 1049 bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const { 1050 D = D->getCanonicalDecl(); 1051 if (!isStackEmpty()) { 1052 iterator I = Iter, E = Stack.back().first.rend(); 1053 Scope *TopScope = nullptr; 1054 while (I != E && !isParallelOrTaskRegion(I->Directive) && 1055 !isOpenMPTargetExecutionDirective(I->Directive)) 1056 ++I; 1057 if (I == E) 1058 return false; 1059 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; 1060 Scope *CurScope = getCurScope(); 1061 while (CurScope != TopScope && !CurScope->isDeclScope(D)) 1062 CurScope = CurScope->getParent(); 1063 return CurScope != TopScope; 1064 } 1065 return false; 1066 } 1067 1068 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, 1069 bool FromParent) { 1070 D = getCanonicalDecl(D); 1071 DSAVarData DVar; 1072 1073 auto *VD = dyn_cast<VarDecl>(D); 1074 auto TI = Threadprivates.find(D); 1075 if (TI != Threadprivates.end()) { 1076 DVar.RefExpr = TI->getSecond().RefExpr.getPointer(); 1077 DVar.CKind = OMPC_threadprivate; 1078 return DVar; 1079 } 1080 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) { 1081 DVar.RefExpr = buildDeclRefExpr( 1082 SemaRef, VD, D->getType().getNonReferenceType(), 1083 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation()); 1084 DVar.CKind = OMPC_threadprivate; 1085 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1086 return DVar; 1087 } 1088 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1089 // in a Construct, C/C++, predetermined, p.1] 1090 // Variables appearing in threadprivate directives are threadprivate. 1091 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 1092 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1093 SemaRef.getLangOpts().OpenMPUseTLS && 1094 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 1095 (VD && VD->getStorageClass() == SC_Register && 1096 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 1097 DVar.RefExpr = buildDeclRefExpr( 1098 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation()); 1099 DVar.CKind = OMPC_threadprivate; 1100 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1101 return DVar; 1102 } 1103 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD && 1104 VD->isLocalVarDeclOrParm() && !isStackEmpty() && 1105 !isLoopControlVariable(D).first) { 1106 iterator IterTarget = 1107 std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(), 1108 [](const SharingMapTy &Data) { 1109 return isOpenMPTargetExecutionDirective(Data.Directive); 1110 }); 1111 if (IterTarget != Stack.back().first.rend()) { 1112 iterator ParentIterTarget = std::next(IterTarget, 1); 1113 for (iterator Iter = Stack.back().first.rbegin(); 1114 Iter != ParentIterTarget; std::advance(Iter, 1)) { 1115 if (isOpenMPLocal(VD, Iter)) { 1116 DVar.RefExpr = 1117 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1118 D->getLocation()); 1119 DVar.CKind = OMPC_threadprivate; 1120 return DVar; 1121 } 1122 } 1123 if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) { 1124 auto DSAIter = IterTarget->SharingMap.find(D); 1125 if (DSAIter != IterTarget->SharingMap.end() && 1126 isOpenMPPrivate(DSAIter->getSecond().Attributes)) { 1127 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer(); 1128 DVar.CKind = OMPC_threadprivate; 1129 return DVar; 1130 } 1131 iterator End = Stack.back().first.rend(); 1132 if (!SemaRef.isOpenMPCapturedByRef( 1133 D, std::distance(ParentIterTarget, End))) { 1134 DVar.RefExpr = 1135 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1136 IterTarget->ConstructLoc); 1137 DVar.CKind = OMPC_threadprivate; 1138 return DVar; 1139 } 1140 } 1141 } 1142 } 1143 1144 if (isStackEmpty()) 1145 // Not in OpenMP execution region and top scope was already checked. 1146 return DVar; 1147 1148 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1149 // in a Construct, C/C++, predetermined, p.4] 1150 // Static data members are shared. 1151 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1152 // in a Construct, C/C++, predetermined, p.7] 1153 // Variables with static storage duration that are declared in a scope 1154 // inside the construct are shared. 1155 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; }; 1156 if (VD && VD->isStaticDataMember()) { 1157 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent); 1158 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1159 return DVar; 1160 1161 DVar.CKind = OMPC_shared; 1162 return DVar; 1163 } 1164 1165 QualType Type = D->getType().getNonReferenceType().getCanonicalType(); 1166 bool IsConstant = Type.isConstant(SemaRef.getASTContext()); 1167 Type = SemaRef.getASTContext().getBaseElementType(Type); 1168 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1169 // in a Construct, C/C++, predetermined, p.6] 1170 // Variables with const qualified type having no mutable member are 1171 // shared. 1172 const CXXRecordDecl *RD = 1173 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; 1174 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 1175 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate()) 1176 RD = CTD->getTemplatedDecl(); 1177 if (IsConstant && 1178 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() && 1179 RD->hasMutableFields())) { 1180 // Variables with const-qualified type having no mutable member may be 1181 // listed in a firstprivate clause, even if they are static data members. 1182 DSAVarData DVarTemp = 1183 hasDSA(D, [](OpenMPClauseKind C) { return C == OMPC_firstprivate; }, 1184 MatchesAlways, FromParent); 1185 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr) 1186 return DVarTemp; 1187 1188 DVar.CKind = OMPC_shared; 1189 return DVar; 1190 } 1191 1192 // Explicitly specified attributes and local variables with predetermined 1193 // attributes. 1194 iterator I = Stack.back().first.rbegin(); 1195 iterator EndI = Stack.back().first.rend(); 1196 if (FromParent && I != EndI) 1197 std::advance(I, 1); 1198 auto It = I->SharingMap.find(D); 1199 if (It != I->SharingMap.end()) { 1200 const DSAInfo &Data = It->getSecond(); 1201 DVar.RefExpr = Data.RefExpr.getPointer(); 1202 DVar.PrivateCopy = Data.PrivateCopy; 1203 DVar.CKind = Data.Attributes; 1204 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1205 DVar.DKind = I->Directive; 1206 } 1207 1208 return DVar; 1209 } 1210 1211 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1212 bool FromParent) const { 1213 if (isStackEmpty()) { 1214 iterator I; 1215 return getDSA(I, D); 1216 } 1217 D = getCanonicalDecl(D); 1218 iterator StartI = Stack.back().first.rbegin(); 1219 iterator EndI = Stack.back().first.rend(); 1220 if (FromParent && StartI != EndI) 1221 std::advance(StartI, 1); 1222 return getDSA(StartI, D); 1223 } 1224 1225 const DSAStackTy::DSAVarData 1226 DSAStackTy::hasDSA(ValueDecl *D, 1227 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1228 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1229 bool FromParent) const { 1230 if (isStackEmpty()) 1231 return {}; 1232 D = getCanonicalDecl(D); 1233 iterator I = Stack.back().first.rbegin(); 1234 iterator EndI = Stack.back().first.rend(); 1235 if (FromParent && I != EndI) 1236 std::advance(I, 1); 1237 for (; I != EndI; std::advance(I, 1)) { 1238 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive)) 1239 continue; 1240 iterator NewI = I; 1241 DSAVarData DVar = getDSA(NewI, D); 1242 if (I == NewI && CPred(DVar.CKind)) 1243 return DVar; 1244 } 1245 return {}; 1246 } 1247 1248 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 1249 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1250 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1251 bool FromParent) const { 1252 if (isStackEmpty()) 1253 return {}; 1254 D = getCanonicalDecl(D); 1255 iterator StartI = Stack.back().first.rbegin(); 1256 iterator EndI = Stack.back().first.rend(); 1257 if (FromParent && StartI != EndI) 1258 std::advance(StartI, 1); 1259 if (StartI == EndI || !DPred(StartI->Directive)) 1260 return {}; 1261 iterator NewI = StartI; 1262 DSAVarData DVar = getDSA(NewI, D); 1263 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData(); 1264 } 1265 1266 bool DSAStackTy::hasExplicitDSA( 1267 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1268 unsigned Level, bool NotLastprivate) const { 1269 if (isStackEmpty()) 1270 return false; 1271 D = getCanonicalDecl(D); 1272 auto StartI = Stack.back().first.begin(); 1273 auto EndI = Stack.back().first.end(); 1274 if (std::distance(StartI, EndI) <= (int)Level) 1275 return false; 1276 std::advance(StartI, Level); 1277 auto I = StartI->SharingMap.find(D); 1278 return (I != StartI->SharingMap.end()) && 1279 I->getSecond().RefExpr.getPointer() && 1280 CPred(I->getSecond().Attributes) && 1281 (!NotLastprivate || !I->getSecond().RefExpr.getInt()); 1282 } 1283 1284 bool DSAStackTy::hasExplicitDirective( 1285 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1286 unsigned Level) const { 1287 if (isStackEmpty()) 1288 return false; 1289 auto StartI = Stack.back().first.begin(); 1290 auto EndI = Stack.back().first.end(); 1291 if (std::distance(StartI, EndI) <= (int)Level) 1292 return false; 1293 std::advance(StartI, Level); 1294 return DPred(StartI->Directive); 1295 } 1296 1297 bool DSAStackTy::hasDirective( 1298 const llvm::function_ref<bool(OpenMPDirectiveKind, 1299 const DeclarationNameInfo &, SourceLocation)> 1300 DPred, 1301 bool FromParent) const { 1302 // We look only in the enclosing region. 1303 if (isStackEmpty()) 1304 return false; 1305 auto StartI = std::next(Stack.back().first.rbegin()); 1306 auto EndI = Stack.back().first.rend(); 1307 if (FromParent && StartI != EndI) 1308 StartI = std::next(StartI); 1309 for (auto I = StartI, EE = EndI; I != EE; ++I) { 1310 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 1311 return true; 1312 } 1313 return false; 1314 } 1315 1316 void Sema::InitDataSharingAttributesStack() { 1317 VarDataSharingAttributesStack = new DSAStackTy(*this); 1318 } 1319 1320 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 1321 1322 void Sema::pushOpenMPFunctionRegion() { 1323 DSAStack->pushFunction(); 1324 } 1325 1326 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) { 1327 DSAStack->popFunction(OldFSI); 1328 } 1329 1330 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const { 1331 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1332 1333 ASTContext &Ctx = getASTContext(); 1334 bool IsByRef = true; 1335 1336 // Find the directive that is associated with the provided scope. 1337 D = cast<ValueDecl>(D->getCanonicalDecl()); 1338 QualType Ty = D->getType(); 1339 1340 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 1341 // This table summarizes how a given variable should be passed to the device 1342 // given its type and the clauses where it appears. This table is based on 1343 // the description in OpenMP 4.5 [2.10.4, target Construct] and 1344 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 1345 // 1346 // ========================================================================= 1347 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 1348 // | |(tofrom:scalar)| | pvt | | | | 1349 // ========================================================================= 1350 // | scl | | | | - | | bycopy| 1351 // | scl | | - | x | - | - | bycopy| 1352 // | scl | | x | - | - | - | null | 1353 // | scl | x | | | - | | byref | 1354 // | scl | x | - | x | - | - | bycopy| 1355 // | scl | x | x | - | - | - | null | 1356 // | scl | | - | - | - | x | byref | 1357 // | scl | x | - | - | - | x | byref | 1358 // 1359 // | agg | n.a. | | | - | | byref | 1360 // | agg | n.a. | - | x | - | - | byref | 1361 // | agg | n.a. | x | - | - | - | null | 1362 // | agg | n.a. | - | - | - | x | byref | 1363 // | agg | n.a. | - | - | - | x[] | byref | 1364 // 1365 // | ptr | n.a. | | | - | | bycopy| 1366 // | ptr | n.a. | - | x | - | - | bycopy| 1367 // | ptr | n.a. | x | - | - | - | null | 1368 // | ptr | n.a. | - | - | - | x | byref | 1369 // | ptr | n.a. | - | - | - | x[] | bycopy| 1370 // | ptr | n.a. | - | - | x | | bycopy| 1371 // | ptr | n.a. | - | - | x | x | bycopy| 1372 // | ptr | n.a. | - | - | x | x[] | bycopy| 1373 // ========================================================================= 1374 // Legend: 1375 // scl - scalar 1376 // ptr - pointer 1377 // agg - aggregate 1378 // x - applies 1379 // - - invalid in this combination 1380 // [] - mapped with an array section 1381 // byref - should be mapped by reference 1382 // byval - should be mapped by value 1383 // null - initialize a local variable to null on the device 1384 // 1385 // Observations: 1386 // - All scalar declarations that show up in a map clause have to be passed 1387 // by reference, because they may have been mapped in the enclosing data 1388 // environment. 1389 // - If the scalar value does not fit the size of uintptr, it has to be 1390 // passed by reference, regardless the result in the table above. 1391 // - For pointers mapped by value that have either an implicit map or an 1392 // array section, the runtime library may pass the NULL value to the 1393 // device instead of the value passed to it by the compiler. 1394 1395 if (Ty->isReferenceType()) 1396 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 1397 1398 // Locate map clauses and see if the variable being captured is referred to 1399 // in any of those clauses. Here we only care about variables, not fields, 1400 // because fields are part of aggregates. 1401 bool IsVariableUsedInMapClause = false; 1402 bool IsVariableAssociatedWithSection = false; 1403 1404 DSAStack->checkMappableExprComponentListsForDeclAtLevel( 1405 D, Level, 1406 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D]( 1407 OMPClauseMappableExprCommon::MappableExprComponentListRef 1408 MapExprComponents, 1409 OpenMPClauseKind WhereFoundClauseKind) { 1410 // Only the map clause information influences how a variable is 1411 // captured. E.g. is_device_ptr does not require changing the default 1412 // behavior. 1413 if (WhereFoundClauseKind != OMPC_map) 1414 return false; 1415 1416 auto EI = MapExprComponents.rbegin(); 1417 auto EE = MapExprComponents.rend(); 1418 1419 assert(EI != EE && "Invalid map expression!"); 1420 1421 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 1422 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 1423 1424 ++EI; 1425 if (EI == EE) 1426 return false; 1427 1428 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 1429 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 1430 isa<MemberExpr>(EI->getAssociatedExpression())) { 1431 IsVariableAssociatedWithSection = true; 1432 // There is nothing more we need to know about this variable. 1433 return true; 1434 } 1435 1436 // Keep looking for more map info. 1437 return false; 1438 }); 1439 1440 if (IsVariableUsedInMapClause) { 1441 // If variable is identified in a map clause it is always captured by 1442 // reference except if it is a pointer that is dereferenced somehow. 1443 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 1444 } else { 1445 // By default, all the data that has a scalar type is mapped by copy 1446 // (except for reduction variables). 1447 IsByRef = 1448 (DSAStack->isForceCaptureByReferenceInTargetExecutable() && 1449 !Ty->isAnyPointerType()) || 1450 !Ty->isScalarType() || 1451 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar || 1452 DSAStack->hasExplicitDSA( 1453 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level); 1454 } 1455 } 1456 1457 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 1458 IsByRef = 1459 ((DSAStack->isForceCaptureByReferenceInTargetExecutable() && 1460 !Ty->isAnyPointerType()) || 1461 !DSAStack->hasExplicitDSA( 1462 D, 1463 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; }, 1464 Level, /*NotLastprivate=*/true)) && 1465 // If the variable is artificial and must be captured by value - try to 1466 // capture by value. 1467 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() && 1468 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()); 1469 } 1470 1471 // When passing data by copy, we need to make sure it fits the uintptr size 1472 // and alignment, because the runtime library only deals with uintptr types. 1473 // If it does not fit the uintptr size, we need to pass the data by reference 1474 // instead. 1475 if (!IsByRef && 1476 (Ctx.getTypeSizeInChars(Ty) > 1477 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 1478 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 1479 IsByRef = true; 1480 } 1481 1482 return IsByRef; 1483 } 1484 1485 unsigned Sema::getOpenMPNestingLevel() const { 1486 assert(getLangOpts().OpenMP); 1487 return DSAStack->getNestingLevel(); 1488 } 1489 1490 bool Sema::isInOpenMPTargetExecutionDirective() const { 1491 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) && 1492 !DSAStack->isClauseParsingMode()) || 1493 DSAStack->hasDirective( 1494 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 1495 SourceLocation) -> bool { 1496 return isOpenMPTargetExecutionDirective(K); 1497 }, 1498 false); 1499 } 1500 1501 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) { 1502 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1503 D = getCanonicalDecl(D); 1504 1505 // If we are attempting to capture a global variable in a directive with 1506 // 'target' we return true so that this global is also mapped to the device. 1507 // 1508 auto *VD = dyn_cast<VarDecl>(D); 1509 if (VD && !VD->hasLocalStorage()) { 1510 if (isInOpenMPDeclareTargetContext() && 1511 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) { 1512 // Try to mark variable as declare target if it is used in capturing 1513 // regions. 1514 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 1515 checkDeclIsAllowedInOpenMPTarget(nullptr, VD); 1516 return nullptr; 1517 } else if (isInOpenMPTargetExecutionDirective()) { 1518 // If the declaration is enclosed in a 'declare target' directive, 1519 // then it should not be captured. 1520 // 1521 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 1522 return nullptr; 1523 return VD; 1524 } 1525 } 1526 // Capture variables captured by reference in lambdas for target-based 1527 // directives. 1528 if (VD && !DSAStack->isClauseParsingMode()) { 1529 if (const auto *RD = VD->getType() 1530 .getCanonicalType() 1531 .getNonReferenceType() 1532 ->getAsCXXRecordDecl()) { 1533 bool SavedForceCaptureByReferenceInTargetExecutable = 1534 DSAStack->isForceCaptureByReferenceInTargetExecutable(); 1535 DSAStack->setForceCaptureByReferenceInTargetExecutable(/*V=*/true); 1536 if (RD->isLambda()) 1537 for (const LambdaCapture &LC : RD->captures()) { 1538 if (LC.getCaptureKind() == LCK_ByRef) { 1539 VarDecl *VD = LC.getCapturedVar(); 1540 DeclContext *VDC = VD->getDeclContext(); 1541 if (!VDC->Encloses(CurContext)) 1542 continue; 1543 DSAStackTy::DSAVarData DVarPrivate = 1544 DSAStack->getTopDSA(VD, /*FromParent=*/false); 1545 // Do not capture already captured variables. 1546 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) && 1547 DVarPrivate.CKind == OMPC_unknown && 1548 !DSAStack->checkMappableExprComponentListsForDecl( 1549 D, /*CurrentRegionOnly=*/true, 1550 [](OMPClauseMappableExprCommon:: 1551 MappableExprComponentListRef, 1552 OpenMPClauseKind) { return true; })) 1553 MarkVariableReferenced(LC.getLocation(), LC.getCapturedVar()); 1554 } else if (LC.getCaptureKind() == LCK_This) { 1555 CheckCXXThisCapture(LC.getLocation()); 1556 } 1557 } 1558 DSAStack->setForceCaptureByReferenceInTargetExecutable( 1559 SavedForceCaptureByReferenceInTargetExecutable); 1560 } 1561 } 1562 1563 if (DSAStack->getCurrentDirective() != OMPD_unknown && 1564 (!DSAStack->isClauseParsingMode() || 1565 DSAStack->getParentDirective() != OMPD_unknown)) { 1566 auto &&Info = DSAStack->isLoopControlVariable(D); 1567 if (Info.first || 1568 (VD && VD->hasLocalStorage() && 1569 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) || 1570 (VD && DSAStack->isForceVarCapturing())) 1571 return VD ? VD : Info.second; 1572 DSAStackTy::DSAVarData DVarPrivate = 1573 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 1574 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind)) 1575 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1576 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, 1577 [](OpenMPDirectiveKind) { return true; }, 1578 DSAStack->isClauseParsingMode()); 1579 if (DVarPrivate.CKind != OMPC_unknown) 1580 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1581 } 1582 return nullptr; 1583 } 1584 1585 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex, 1586 unsigned Level) const { 1587 SmallVector<OpenMPDirectiveKind, 4> Regions; 1588 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level)); 1589 FunctionScopesIndex -= Regions.size(); 1590 } 1591 1592 void Sema::startOpenMPLoop() { 1593 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 1594 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) 1595 DSAStack->loopInit(); 1596 } 1597 1598 bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const { 1599 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1600 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 1601 if (DSAStack->getAssociatedLoops() > 0 && 1602 !DSAStack->isLoopStarted()) { 1603 DSAStack->resetPossibleLoopCounter(D); 1604 DSAStack->loopStart(); 1605 return true; 1606 } 1607 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() || 1608 DSAStack->isLoopControlVariable(D).first) && 1609 !DSAStack->hasExplicitDSA( 1610 D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) && 1611 !isOpenMPSimdDirective(DSAStack->getCurrentDirective())) 1612 return true; 1613 } 1614 return DSAStack->hasExplicitDSA( 1615 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) || 1616 (DSAStack->isClauseParsingMode() && 1617 DSAStack->getClauseParsingMode() == OMPC_private) || 1618 // Consider taskgroup reduction descriptor variable a private to avoid 1619 // possible capture in the region. 1620 (DSAStack->hasExplicitDirective( 1621 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; }, 1622 Level) && 1623 DSAStack->isTaskgroupReductionRef(D, Level)); 1624 } 1625 1626 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D, 1627 unsigned Level) { 1628 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1629 D = getCanonicalDecl(D); 1630 OpenMPClauseKind OMPC = OMPC_unknown; 1631 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) { 1632 const unsigned NewLevel = I - 1; 1633 if (DSAStack->hasExplicitDSA(D, 1634 [&OMPC](const OpenMPClauseKind K) { 1635 if (isOpenMPPrivate(K)) { 1636 OMPC = K; 1637 return true; 1638 } 1639 return false; 1640 }, 1641 NewLevel)) 1642 break; 1643 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel( 1644 D, NewLevel, 1645 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 1646 OpenMPClauseKind) { return true; })) { 1647 OMPC = OMPC_map; 1648 break; 1649 } 1650 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 1651 NewLevel)) { 1652 OMPC = OMPC_map; 1653 if (D->getType()->isScalarType() && 1654 DSAStack->getDefaultDMAAtLevel(NewLevel) != 1655 DefaultMapAttributes::DMA_tofrom_scalar) 1656 OMPC = OMPC_firstprivate; 1657 break; 1658 } 1659 } 1660 if (OMPC != OMPC_unknown) 1661 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC)); 1662 } 1663 1664 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, 1665 unsigned Level) const { 1666 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1667 // Return true if the current level is no longer enclosed in a target region. 1668 1669 const auto *VD = dyn_cast<VarDecl>(D); 1670 return VD && !VD->hasLocalStorage() && 1671 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 1672 Level); 1673 } 1674 1675 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 1676 1677 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 1678 const DeclarationNameInfo &DirName, 1679 Scope *CurScope, SourceLocation Loc) { 1680 DSAStack->push(DKind, DirName, CurScope, Loc); 1681 PushExpressionEvaluationContext( 1682 ExpressionEvaluationContext::PotentiallyEvaluated); 1683 } 1684 1685 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 1686 DSAStack->setClauseParsingMode(K); 1687 } 1688 1689 void Sema::EndOpenMPClause() { 1690 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 1691 } 1692 1693 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 1694 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 1695 // A variable of class type (or array thereof) that appears in a lastprivate 1696 // clause requires an accessible, unambiguous default constructor for the 1697 // class type, unless the list item is also specified in a firstprivate 1698 // clause. 1699 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 1700 for (OMPClause *C : D->clauses()) { 1701 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 1702 SmallVector<Expr *, 8> PrivateCopies; 1703 for (Expr *DE : Clause->varlists()) { 1704 if (DE->isValueDependent() || DE->isTypeDependent()) { 1705 PrivateCopies.push_back(nullptr); 1706 continue; 1707 } 1708 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 1709 auto *VD = cast<VarDecl>(DRE->getDecl()); 1710 QualType Type = VD->getType().getNonReferenceType(); 1711 const DSAStackTy::DSAVarData DVar = 1712 DSAStack->getTopDSA(VD, /*FromParent=*/false); 1713 if (DVar.CKind == OMPC_lastprivate) { 1714 // Generate helper private variable and initialize it with the 1715 // default value. The address of the original variable is replaced 1716 // by the address of the new private variable in CodeGen. This new 1717 // variable is not added to IdResolver, so the code in the OpenMP 1718 // region uses original variable for proper diagnostics. 1719 VarDecl *VDPrivate = buildVarDecl( 1720 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 1721 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE); 1722 ActOnUninitializedDecl(VDPrivate); 1723 if (VDPrivate->isInvalidDecl()) 1724 continue; 1725 PrivateCopies.push_back(buildDeclRefExpr( 1726 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 1727 } else { 1728 // The variable is also a firstprivate, so initialization sequence 1729 // for private copy is generated already. 1730 PrivateCopies.push_back(nullptr); 1731 } 1732 } 1733 // Set initializers to private copies if no errors were found. 1734 if (PrivateCopies.size() == Clause->varlist_size()) 1735 Clause->setPrivateCopies(PrivateCopies); 1736 } 1737 } 1738 } 1739 1740 DSAStack->pop(); 1741 DiscardCleanupsInEvaluationContext(); 1742 PopExpressionEvaluationContext(); 1743 } 1744 1745 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 1746 Expr *NumIterations, Sema &SemaRef, 1747 Scope *S, DSAStackTy *Stack); 1748 1749 namespace { 1750 1751 class VarDeclFilterCCC final : public CorrectionCandidateCallback { 1752 private: 1753 Sema &SemaRef; 1754 1755 public: 1756 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 1757 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1758 NamedDecl *ND = Candidate.getCorrectionDecl(); 1759 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 1760 return VD->hasGlobalStorage() && 1761 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1762 SemaRef.getCurScope()); 1763 } 1764 return false; 1765 } 1766 }; 1767 1768 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback { 1769 private: 1770 Sema &SemaRef; 1771 1772 public: 1773 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 1774 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1775 NamedDecl *ND = Candidate.getCorrectionDecl(); 1776 if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) { 1777 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1778 SemaRef.getCurScope()); 1779 } 1780 return false; 1781 } 1782 }; 1783 1784 } // namespace 1785 1786 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 1787 CXXScopeSpec &ScopeSpec, 1788 const DeclarationNameInfo &Id) { 1789 LookupResult Lookup(*this, Id, LookupOrdinaryName); 1790 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 1791 1792 if (Lookup.isAmbiguous()) 1793 return ExprError(); 1794 1795 VarDecl *VD; 1796 if (!Lookup.isSingleResult()) { 1797 if (TypoCorrection Corrected = CorrectTypo( 1798 Id, LookupOrdinaryName, CurScope, nullptr, 1799 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) { 1800 diagnoseTypo(Corrected, 1801 PDiag(Lookup.empty() 1802 ? diag::err_undeclared_var_use_suggest 1803 : diag::err_omp_expected_var_arg_suggest) 1804 << Id.getName()); 1805 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 1806 } else { 1807 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 1808 : diag::err_omp_expected_var_arg) 1809 << Id.getName(); 1810 return ExprError(); 1811 } 1812 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) { 1813 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 1814 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 1815 return ExprError(); 1816 } 1817 Lookup.suppressDiagnostics(); 1818 1819 // OpenMP [2.9.2, Syntax, C/C++] 1820 // Variables must be file-scope, namespace-scope, or static block-scope. 1821 if (!VD->hasGlobalStorage()) { 1822 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 1823 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal(); 1824 bool IsDecl = 1825 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1826 Diag(VD->getLocation(), 1827 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1828 << VD; 1829 return ExprError(); 1830 } 1831 1832 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 1833 NamedDecl *ND = CanonicalVD; 1834 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 1835 // A threadprivate directive for file-scope variables must appear outside 1836 // any definition or declaration. 1837 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 1838 !getCurLexicalContext()->isTranslationUnit()) { 1839 Diag(Id.getLoc(), diag::err_omp_var_scope) 1840 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1841 bool IsDecl = 1842 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1843 Diag(VD->getLocation(), 1844 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1845 << VD; 1846 return ExprError(); 1847 } 1848 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 1849 // A threadprivate directive for static class member variables must appear 1850 // in the class definition, in the same scope in which the member 1851 // variables are declared. 1852 if (CanonicalVD->isStaticDataMember() && 1853 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 1854 Diag(Id.getLoc(), diag::err_omp_var_scope) 1855 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1856 bool IsDecl = 1857 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1858 Diag(VD->getLocation(), 1859 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1860 << VD; 1861 return ExprError(); 1862 } 1863 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 1864 // A threadprivate directive for namespace-scope variables must appear 1865 // outside any definition or declaration other than the namespace 1866 // definition itself. 1867 if (CanonicalVD->getDeclContext()->isNamespace() && 1868 (!getCurLexicalContext()->isFileContext() || 1869 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 1870 Diag(Id.getLoc(), diag::err_omp_var_scope) 1871 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1872 bool IsDecl = 1873 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1874 Diag(VD->getLocation(), 1875 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1876 << VD; 1877 return ExprError(); 1878 } 1879 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 1880 // A threadprivate directive for static block-scope variables must appear 1881 // in the scope of the variable and not in a nested scope. 1882 if (CanonicalVD->isStaticLocal() && CurScope && 1883 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 1884 Diag(Id.getLoc(), diag::err_omp_var_scope) 1885 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1886 bool IsDecl = 1887 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1888 Diag(VD->getLocation(), 1889 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1890 << VD; 1891 return ExprError(); 1892 } 1893 1894 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 1895 // A threadprivate directive must lexically precede all references to any 1896 // of the variables in its list. 1897 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) { 1898 Diag(Id.getLoc(), diag::err_omp_var_used) 1899 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 1900 return ExprError(); 1901 } 1902 1903 QualType ExprType = VD->getType().getNonReferenceType(); 1904 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 1905 SourceLocation(), VD, 1906 /*RefersToEnclosingVariableOrCapture=*/false, 1907 Id.getLoc(), ExprType, VK_LValue); 1908 } 1909 1910 Sema::DeclGroupPtrTy 1911 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 1912 ArrayRef<Expr *> VarList) { 1913 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 1914 CurContext->addDecl(D); 1915 return DeclGroupPtrTy::make(DeclGroupRef(D)); 1916 } 1917 return nullptr; 1918 } 1919 1920 namespace { 1921 class LocalVarRefChecker final 1922 : public ConstStmtVisitor<LocalVarRefChecker, bool> { 1923 Sema &SemaRef; 1924 1925 public: 1926 bool VisitDeclRefExpr(const DeclRefExpr *E) { 1927 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 1928 if (VD->hasLocalStorage()) { 1929 SemaRef.Diag(E->getBeginLoc(), 1930 diag::err_omp_local_var_in_threadprivate_init) 1931 << E->getSourceRange(); 1932 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 1933 << VD << VD->getSourceRange(); 1934 return true; 1935 } 1936 } 1937 return false; 1938 } 1939 bool VisitStmt(const Stmt *S) { 1940 for (const Stmt *Child : S->children()) { 1941 if (Child && Visit(Child)) 1942 return true; 1943 } 1944 return false; 1945 } 1946 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 1947 }; 1948 } // namespace 1949 1950 OMPThreadPrivateDecl * 1951 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 1952 SmallVector<Expr *, 8> Vars; 1953 for (Expr *RefExpr : VarList) { 1954 auto *DE = cast<DeclRefExpr>(RefExpr); 1955 auto *VD = cast<VarDecl>(DE->getDecl()); 1956 SourceLocation ILoc = DE->getExprLoc(); 1957 1958 // Mark variable as used. 1959 VD->setReferenced(); 1960 VD->markUsed(Context); 1961 1962 QualType QType = VD->getType(); 1963 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 1964 // It will be analyzed later. 1965 Vars.push_back(DE); 1966 continue; 1967 } 1968 1969 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 1970 // A threadprivate variable must not have an incomplete type. 1971 if (RequireCompleteType(ILoc, VD->getType(), 1972 diag::err_omp_threadprivate_incomplete_type)) { 1973 continue; 1974 } 1975 1976 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 1977 // A threadprivate variable must not have a reference type. 1978 if (VD->getType()->isReferenceType()) { 1979 Diag(ILoc, diag::err_omp_ref_type_arg) 1980 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 1981 bool IsDecl = 1982 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 1983 Diag(VD->getLocation(), 1984 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1985 << VD; 1986 continue; 1987 } 1988 1989 // Check if this is a TLS variable. If TLS is not being supported, produce 1990 // the corresponding diagnostic. 1991 if ((VD->getTLSKind() != VarDecl::TLS_None && 1992 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1993 getLangOpts().OpenMPUseTLS && 1994 getASTContext().getTargetInfo().isTLSSupported())) || 1995 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 1996 !VD->isLocalVarDecl())) { 1997 Diag(ILoc, diag::err_omp_var_thread_local) 1998 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 1999 bool IsDecl = 2000 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2001 Diag(VD->getLocation(), 2002 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2003 << VD; 2004 continue; 2005 } 2006 2007 // Check if initial value of threadprivate variable reference variable with 2008 // local storage (it is not supported by runtime). 2009 if (const Expr *Init = VD->getAnyInitializer()) { 2010 LocalVarRefChecker Checker(*this); 2011 if (Checker.Visit(Init)) 2012 continue; 2013 } 2014 2015 Vars.push_back(RefExpr); 2016 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 2017 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 2018 Context, SourceRange(Loc, Loc))); 2019 if (ASTMutationListener *ML = Context.getASTMutationListener()) 2020 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 2021 } 2022 OMPThreadPrivateDecl *D = nullptr; 2023 if (!Vars.empty()) { 2024 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 2025 Vars); 2026 D->setAccess(AS_public); 2027 } 2028 return D; 2029 } 2030 2031 Sema::DeclGroupPtrTy 2032 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc, 2033 ArrayRef<OMPClause *> ClauseList) { 2034 OMPRequiresDecl *D = nullptr; 2035 if (!CurContext->isFileContext()) { 2036 Diag(Loc, diag::err_omp_invalid_scope) << "requires"; 2037 } else { 2038 D = CheckOMPRequiresDecl(Loc, ClauseList); 2039 if (D) { 2040 CurContext->addDecl(D); 2041 DSAStack->addRequiresDecl(D); 2042 } 2043 } 2044 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2045 } 2046 2047 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc, 2048 ArrayRef<OMPClause *> ClauseList) { 2049 if (!DSAStack->hasDuplicateRequiresClause(ClauseList)) 2050 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc, 2051 ClauseList); 2052 return nullptr; 2053 } 2054 2055 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 2056 const ValueDecl *D, 2057 const DSAStackTy::DSAVarData &DVar, 2058 bool IsLoopIterVar = false) { 2059 if (DVar.RefExpr) { 2060 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 2061 << getOpenMPClauseName(DVar.CKind); 2062 return; 2063 } 2064 enum { 2065 PDSA_StaticMemberShared, 2066 PDSA_StaticLocalVarShared, 2067 PDSA_LoopIterVarPrivate, 2068 PDSA_LoopIterVarLinear, 2069 PDSA_LoopIterVarLastprivate, 2070 PDSA_ConstVarShared, 2071 PDSA_GlobalVarShared, 2072 PDSA_TaskVarFirstprivate, 2073 PDSA_LocalVarPrivate, 2074 PDSA_Implicit 2075 } Reason = PDSA_Implicit; 2076 bool ReportHint = false; 2077 auto ReportLoc = D->getLocation(); 2078 auto *VD = dyn_cast<VarDecl>(D); 2079 if (IsLoopIterVar) { 2080 if (DVar.CKind == OMPC_private) 2081 Reason = PDSA_LoopIterVarPrivate; 2082 else if (DVar.CKind == OMPC_lastprivate) 2083 Reason = PDSA_LoopIterVarLastprivate; 2084 else 2085 Reason = PDSA_LoopIterVarLinear; 2086 } else if (isOpenMPTaskingDirective(DVar.DKind) && 2087 DVar.CKind == OMPC_firstprivate) { 2088 Reason = PDSA_TaskVarFirstprivate; 2089 ReportLoc = DVar.ImplicitDSALoc; 2090 } else if (VD && VD->isStaticLocal()) 2091 Reason = PDSA_StaticLocalVarShared; 2092 else if (VD && VD->isStaticDataMember()) 2093 Reason = PDSA_StaticMemberShared; 2094 else if (VD && VD->isFileVarDecl()) 2095 Reason = PDSA_GlobalVarShared; 2096 else if (D->getType().isConstant(SemaRef.getASTContext())) 2097 Reason = PDSA_ConstVarShared; 2098 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 2099 ReportHint = true; 2100 Reason = PDSA_LocalVarPrivate; 2101 } 2102 if (Reason != PDSA_Implicit) { 2103 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 2104 << Reason << ReportHint 2105 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 2106 } else if (DVar.ImplicitDSALoc.isValid()) { 2107 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 2108 << getOpenMPClauseName(DVar.CKind); 2109 } 2110 } 2111 2112 namespace { 2113 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> { 2114 DSAStackTy *Stack; 2115 Sema &SemaRef; 2116 bool ErrorFound = false; 2117 CapturedStmt *CS = nullptr; 2118 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate; 2119 llvm::SmallVector<Expr *, 4> ImplicitMap; 2120 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA; 2121 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations; 2122 2123 void VisitSubCaptures(OMPExecutableDirective *S) { 2124 // Check implicitly captured variables. 2125 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt()) 2126 return; 2127 for (const CapturedStmt::Capture &Cap : 2128 S->getInnermostCapturedStmt()->captures()) { 2129 if (!Cap.capturesVariable()) 2130 continue; 2131 VarDecl *VD = Cap.getCapturedVar(); 2132 // Do not try to map the variable if it or its sub-component was mapped 2133 // already. 2134 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 2135 Stack->checkMappableExprComponentListsForDecl( 2136 VD, /*CurrentRegionOnly=*/true, 2137 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 2138 OpenMPClauseKind) { return true; })) 2139 continue; 2140 DeclRefExpr *DRE = buildDeclRefExpr( 2141 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context), 2142 Cap.getLocation(), /*RefersToCapture=*/true); 2143 Visit(DRE); 2144 } 2145 } 2146 2147 public: 2148 void VisitDeclRefExpr(DeclRefExpr *E) { 2149 if (E->isTypeDependent() || E->isValueDependent() || 2150 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 2151 return; 2152 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2153 VD = VD->getCanonicalDecl(); 2154 // Skip internally declared variables. 2155 if (VD->hasLocalStorage() && !CS->capturesVariable(VD)) 2156 return; 2157 2158 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 2159 // Check if the variable has explicit DSA set and stop analysis if it so. 2160 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second) 2161 return; 2162 2163 // Skip internally declared static variables. 2164 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 2165 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 2166 if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) && 2167 (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link)) 2168 return; 2169 2170 SourceLocation ELoc = E->getExprLoc(); 2171 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 2172 // The default(none) clause requires that each variable that is referenced 2173 // in the construct, and does not have a predetermined data-sharing 2174 // attribute, must have its data-sharing attribute explicitly determined 2175 // by being listed in a data-sharing attribute clause. 2176 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && 2177 isParallelOrTaskRegion(DKind) && 2178 VarsWithInheritedDSA.count(VD) == 0) { 2179 VarsWithInheritedDSA[VD] = E; 2180 return; 2181 } 2182 2183 if (isOpenMPTargetExecutionDirective(DKind) && 2184 !Stack->isLoopControlVariable(VD).first) { 2185 if (!Stack->checkMappableExprComponentListsForDecl( 2186 VD, /*CurrentRegionOnly=*/true, 2187 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 2188 StackComponents, 2189 OpenMPClauseKind) { 2190 // Variable is used if it has been marked as an array, array 2191 // section or the variable iself. 2192 return StackComponents.size() == 1 || 2193 std::all_of( 2194 std::next(StackComponents.rbegin()), 2195 StackComponents.rend(), 2196 [](const OMPClauseMappableExprCommon:: 2197 MappableComponent &MC) { 2198 return MC.getAssociatedDeclaration() == 2199 nullptr && 2200 (isa<OMPArraySectionExpr>( 2201 MC.getAssociatedExpression()) || 2202 isa<ArraySubscriptExpr>( 2203 MC.getAssociatedExpression())); 2204 }); 2205 })) { 2206 bool IsFirstprivate = false; 2207 // By default lambdas are captured as firstprivates. 2208 if (const auto *RD = 2209 VD->getType().getNonReferenceType()->getAsCXXRecordDecl()) 2210 IsFirstprivate = RD->isLambda(); 2211 IsFirstprivate = 2212 IsFirstprivate || 2213 (VD->getType().getNonReferenceType()->isScalarType() && 2214 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res); 2215 if (IsFirstprivate) 2216 ImplicitFirstprivate.emplace_back(E); 2217 else 2218 ImplicitMap.emplace_back(E); 2219 return; 2220 } 2221 } 2222 2223 // OpenMP [2.9.3.6, Restrictions, p.2] 2224 // A list item that appears in a reduction clause of the innermost 2225 // enclosing worksharing or parallel construct may not be accessed in an 2226 // explicit task. 2227 DVar = Stack->hasInnermostDSA( 2228 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 2229 [](OpenMPDirectiveKind K) { 2230 return isOpenMPParallelDirective(K) || 2231 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 2232 }, 2233 /*FromParent=*/true); 2234 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 2235 ErrorFound = true; 2236 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 2237 reportOriginalDsa(SemaRef, Stack, VD, DVar); 2238 return; 2239 } 2240 2241 // Define implicit data-sharing attributes for task. 2242 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false); 2243 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 2244 !Stack->isLoopControlVariable(VD).first) 2245 ImplicitFirstprivate.push_back(E); 2246 } 2247 } 2248 void VisitMemberExpr(MemberExpr *E) { 2249 if (E->isTypeDependent() || E->isValueDependent() || 2250 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 2251 return; 2252 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 2253 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 2254 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 2255 if (!FD) 2256 return; 2257 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false); 2258 // Check if the variable has explicit DSA set and stop analysis if it 2259 // so. 2260 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second) 2261 return; 2262 2263 if (isOpenMPTargetExecutionDirective(DKind) && 2264 !Stack->isLoopControlVariable(FD).first && 2265 !Stack->checkMappableExprComponentListsForDecl( 2266 FD, /*CurrentRegionOnly=*/true, 2267 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 2268 StackComponents, 2269 OpenMPClauseKind) { 2270 return isa<CXXThisExpr>( 2271 cast<MemberExpr>( 2272 StackComponents.back().getAssociatedExpression()) 2273 ->getBase() 2274 ->IgnoreParens()); 2275 })) { 2276 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 2277 // A bit-field cannot appear in a map clause. 2278 // 2279 if (FD->isBitField()) 2280 return; 2281 ImplicitMap.emplace_back(E); 2282 return; 2283 } 2284 2285 SourceLocation ELoc = E->getExprLoc(); 2286 // OpenMP [2.9.3.6, Restrictions, p.2] 2287 // A list item that appears in a reduction clause of the innermost 2288 // enclosing worksharing or parallel construct may not be accessed in 2289 // an explicit task. 2290 DVar = Stack->hasInnermostDSA( 2291 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 2292 [](OpenMPDirectiveKind K) { 2293 return isOpenMPParallelDirective(K) || 2294 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 2295 }, 2296 /*FromParent=*/true); 2297 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 2298 ErrorFound = true; 2299 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 2300 reportOriginalDsa(SemaRef, Stack, FD, DVar); 2301 return; 2302 } 2303 2304 // Define implicit data-sharing attributes for task. 2305 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false); 2306 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 2307 !Stack->isLoopControlVariable(FD).first) { 2308 // Check if there is a captured expression for the current field in the 2309 // region. Do not mark it as firstprivate unless there is no captured 2310 // expression. 2311 // TODO: try to make it firstprivate. 2312 if (DVar.CKind != OMPC_unknown) 2313 ImplicitFirstprivate.push_back(E); 2314 } 2315 return; 2316 } 2317 if (isOpenMPTargetExecutionDirective(DKind)) { 2318 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 2319 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map, 2320 /*NoDiagnose=*/true)) 2321 return; 2322 const auto *VD = cast<ValueDecl>( 2323 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl()); 2324 if (!Stack->checkMappableExprComponentListsForDecl( 2325 VD, /*CurrentRegionOnly=*/true, 2326 [&CurComponents]( 2327 OMPClauseMappableExprCommon::MappableExprComponentListRef 2328 StackComponents, 2329 OpenMPClauseKind) { 2330 auto CCI = CurComponents.rbegin(); 2331 auto CCE = CurComponents.rend(); 2332 for (const auto &SC : llvm::reverse(StackComponents)) { 2333 // Do both expressions have the same kind? 2334 if (CCI->getAssociatedExpression()->getStmtClass() != 2335 SC.getAssociatedExpression()->getStmtClass()) 2336 if (!(isa<OMPArraySectionExpr>( 2337 SC.getAssociatedExpression()) && 2338 isa<ArraySubscriptExpr>( 2339 CCI->getAssociatedExpression()))) 2340 return false; 2341 2342 const Decl *CCD = CCI->getAssociatedDeclaration(); 2343 const Decl *SCD = SC.getAssociatedDeclaration(); 2344 CCD = CCD ? CCD->getCanonicalDecl() : nullptr; 2345 SCD = SCD ? SCD->getCanonicalDecl() : nullptr; 2346 if (SCD != CCD) 2347 return false; 2348 std::advance(CCI, 1); 2349 if (CCI == CCE) 2350 break; 2351 } 2352 return true; 2353 })) { 2354 Visit(E->getBase()); 2355 } 2356 } else { 2357 Visit(E->getBase()); 2358 } 2359 } 2360 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 2361 for (OMPClause *C : S->clauses()) { 2362 // Skip analysis of arguments of implicitly defined firstprivate clause 2363 // for task|target directives. 2364 // Skip analysis of arguments of implicitly defined map clause for target 2365 // directives. 2366 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) && 2367 C->isImplicit())) { 2368 for (Stmt *CC : C->children()) { 2369 if (CC) 2370 Visit(CC); 2371 } 2372 } 2373 } 2374 // Check implicitly captured variables. 2375 VisitSubCaptures(S); 2376 } 2377 void VisitStmt(Stmt *S) { 2378 for (Stmt *C : S->children()) { 2379 if (C) { 2380 if (auto *OED = dyn_cast<OMPExecutableDirective>(C)) { 2381 // Check implicitly captured variables in the task-based directives to 2382 // check if they must be firstprivatized. 2383 VisitSubCaptures(OED); 2384 } else { 2385 Visit(C); 2386 } 2387 } 2388 } 2389 } 2390 2391 bool isErrorFound() const { return ErrorFound; } 2392 ArrayRef<Expr *> getImplicitFirstprivate() const { 2393 return ImplicitFirstprivate; 2394 } 2395 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; } 2396 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const { 2397 return VarsWithInheritedDSA; 2398 } 2399 2400 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 2401 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {} 2402 }; 2403 } // namespace 2404 2405 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 2406 switch (DKind) { 2407 case OMPD_parallel: 2408 case OMPD_parallel_for: 2409 case OMPD_parallel_for_simd: 2410 case OMPD_parallel_sections: 2411 case OMPD_teams: 2412 case OMPD_teams_distribute: 2413 case OMPD_teams_distribute_simd: { 2414 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2415 QualType KmpInt32PtrTy = 2416 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2417 Sema::CapturedParamNameType Params[] = { 2418 std::make_pair(".global_tid.", KmpInt32PtrTy), 2419 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2420 std::make_pair(StringRef(), QualType()) // __context with shared vars 2421 }; 2422 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2423 Params); 2424 break; 2425 } 2426 case OMPD_target_teams: 2427 case OMPD_target_parallel: 2428 case OMPD_target_parallel_for: 2429 case OMPD_target_parallel_for_simd: 2430 case OMPD_target_teams_distribute: 2431 case OMPD_target_teams_distribute_simd: { 2432 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2433 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2434 QualType KmpInt32PtrTy = 2435 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2436 QualType Args[] = {VoidPtrTy}; 2437 FunctionProtoType::ExtProtoInfo EPI; 2438 EPI.Variadic = true; 2439 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2440 Sema::CapturedParamNameType Params[] = { 2441 std::make_pair(".global_tid.", KmpInt32Ty), 2442 std::make_pair(".part_id.", KmpInt32PtrTy), 2443 std::make_pair(".privates.", VoidPtrTy), 2444 std::make_pair( 2445 ".copy_fn.", 2446 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2447 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2448 std::make_pair(StringRef(), QualType()) // __context with shared vars 2449 }; 2450 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2451 Params); 2452 // Mark this captured region as inlined, because we don't use outlined 2453 // function directly. 2454 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2455 AlwaysInlineAttr::CreateImplicit( 2456 Context, AlwaysInlineAttr::Keyword_forceinline)); 2457 Sema::CapturedParamNameType ParamsTarget[] = { 2458 std::make_pair(StringRef(), QualType()) // __context with shared vars 2459 }; 2460 // Start a captured region for 'target' with no implicit parameters. 2461 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2462 ParamsTarget); 2463 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 2464 std::make_pair(".global_tid.", KmpInt32PtrTy), 2465 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2466 std::make_pair(StringRef(), QualType()) // __context with shared vars 2467 }; 2468 // Start a captured region for 'teams' or 'parallel'. Both regions have 2469 // the same implicit parameters. 2470 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2471 ParamsTeamsOrParallel); 2472 break; 2473 } 2474 case OMPD_target: 2475 case OMPD_target_simd: { 2476 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2477 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2478 QualType KmpInt32PtrTy = 2479 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2480 QualType Args[] = {VoidPtrTy}; 2481 FunctionProtoType::ExtProtoInfo EPI; 2482 EPI.Variadic = true; 2483 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2484 Sema::CapturedParamNameType Params[] = { 2485 std::make_pair(".global_tid.", KmpInt32Ty), 2486 std::make_pair(".part_id.", KmpInt32PtrTy), 2487 std::make_pair(".privates.", VoidPtrTy), 2488 std::make_pair( 2489 ".copy_fn.", 2490 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2491 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2492 std::make_pair(StringRef(), QualType()) // __context with shared vars 2493 }; 2494 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2495 Params); 2496 // Mark this captured region as inlined, because we don't use outlined 2497 // function directly. 2498 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2499 AlwaysInlineAttr::CreateImplicit( 2500 Context, AlwaysInlineAttr::Keyword_forceinline)); 2501 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2502 std::make_pair(StringRef(), QualType())); 2503 break; 2504 } 2505 case OMPD_simd: 2506 case OMPD_for: 2507 case OMPD_for_simd: 2508 case OMPD_sections: 2509 case OMPD_section: 2510 case OMPD_single: 2511 case OMPD_master: 2512 case OMPD_critical: 2513 case OMPD_taskgroup: 2514 case OMPD_distribute: 2515 case OMPD_distribute_simd: 2516 case OMPD_ordered: 2517 case OMPD_atomic: 2518 case OMPD_target_data: { 2519 Sema::CapturedParamNameType Params[] = { 2520 std::make_pair(StringRef(), QualType()) // __context with shared vars 2521 }; 2522 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2523 Params); 2524 break; 2525 } 2526 case OMPD_task: { 2527 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2528 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2529 QualType KmpInt32PtrTy = 2530 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2531 QualType Args[] = {VoidPtrTy}; 2532 FunctionProtoType::ExtProtoInfo EPI; 2533 EPI.Variadic = true; 2534 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2535 Sema::CapturedParamNameType Params[] = { 2536 std::make_pair(".global_tid.", KmpInt32Ty), 2537 std::make_pair(".part_id.", KmpInt32PtrTy), 2538 std::make_pair(".privates.", VoidPtrTy), 2539 std::make_pair( 2540 ".copy_fn.", 2541 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2542 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2543 std::make_pair(StringRef(), QualType()) // __context with shared vars 2544 }; 2545 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2546 Params); 2547 // Mark this captured region as inlined, because we don't use outlined 2548 // function directly. 2549 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2550 AlwaysInlineAttr::CreateImplicit( 2551 Context, AlwaysInlineAttr::Keyword_forceinline)); 2552 break; 2553 } 2554 case OMPD_taskloop: 2555 case OMPD_taskloop_simd: { 2556 QualType KmpInt32Ty = 2557 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 2558 .withConst(); 2559 QualType KmpUInt64Ty = 2560 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 2561 .withConst(); 2562 QualType KmpInt64Ty = 2563 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 2564 .withConst(); 2565 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2566 QualType KmpInt32PtrTy = 2567 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2568 QualType Args[] = {VoidPtrTy}; 2569 FunctionProtoType::ExtProtoInfo EPI; 2570 EPI.Variadic = true; 2571 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2572 Sema::CapturedParamNameType Params[] = { 2573 std::make_pair(".global_tid.", KmpInt32Ty), 2574 std::make_pair(".part_id.", KmpInt32PtrTy), 2575 std::make_pair(".privates.", VoidPtrTy), 2576 std::make_pair( 2577 ".copy_fn.", 2578 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2579 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2580 std::make_pair(".lb.", KmpUInt64Ty), 2581 std::make_pair(".ub.", KmpUInt64Ty), 2582 std::make_pair(".st.", KmpInt64Ty), 2583 std::make_pair(".liter.", KmpInt32Ty), 2584 std::make_pair(".reductions.", VoidPtrTy), 2585 std::make_pair(StringRef(), QualType()) // __context with shared vars 2586 }; 2587 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2588 Params); 2589 // Mark this captured region as inlined, because we don't use outlined 2590 // function directly. 2591 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2592 AlwaysInlineAttr::CreateImplicit( 2593 Context, AlwaysInlineAttr::Keyword_forceinline)); 2594 break; 2595 } 2596 case OMPD_distribute_parallel_for_simd: 2597 case OMPD_distribute_parallel_for: { 2598 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2599 QualType KmpInt32PtrTy = 2600 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2601 Sema::CapturedParamNameType Params[] = { 2602 std::make_pair(".global_tid.", KmpInt32PtrTy), 2603 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2604 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 2605 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 2606 std::make_pair(StringRef(), QualType()) // __context with shared vars 2607 }; 2608 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2609 Params); 2610 break; 2611 } 2612 case OMPD_target_teams_distribute_parallel_for: 2613 case OMPD_target_teams_distribute_parallel_for_simd: { 2614 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2615 QualType KmpInt32PtrTy = 2616 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2617 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2618 2619 QualType Args[] = {VoidPtrTy}; 2620 FunctionProtoType::ExtProtoInfo EPI; 2621 EPI.Variadic = true; 2622 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2623 Sema::CapturedParamNameType Params[] = { 2624 std::make_pair(".global_tid.", KmpInt32Ty), 2625 std::make_pair(".part_id.", KmpInt32PtrTy), 2626 std::make_pair(".privates.", VoidPtrTy), 2627 std::make_pair( 2628 ".copy_fn.", 2629 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2630 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2631 std::make_pair(StringRef(), QualType()) // __context with shared vars 2632 }; 2633 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2634 Params); 2635 // Mark this captured region as inlined, because we don't use outlined 2636 // function directly. 2637 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2638 AlwaysInlineAttr::CreateImplicit( 2639 Context, AlwaysInlineAttr::Keyword_forceinline)); 2640 Sema::CapturedParamNameType ParamsTarget[] = { 2641 std::make_pair(StringRef(), QualType()) // __context with shared vars 2642 }; 2643 // Start a captured region for 'target' with no implicit parameters. 2644 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2645 ParamsTarget); 2646 2647 Sema::CapturedParamNameType ParamsTeams[] = { 2648 std::make_pair(".global_tid.", KmpInt32PtrTy), 2649 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2650 std::make_pair(StringRef(), QualType()) // __context with shared vars 2651 }; 2652 // Start a captured region for 'target' with no implicit parameters. 2653 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2654 ParamsTeams); 2655 2656 Sema::CapturedParamNameType ParamsParallel[] = { 2657 std::make_pair(".global_tid.", KmpInt32PtrTy), 2658 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2659 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 2660 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 2661 std::make_pair(StringRef(), QualType()) // __context with shared vars 2662 }; 2663 // Start a captured region for 'teams' or 'parallel'. Both regions have 2664 // the same implicit parameters. 2665 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2666 ParamsParallel); 2667 break; 2668 } 2669 2670 case OMPD_teams_distribute_parallel_for: 2671 case OMPD_teams_distribute_parallel_for_simd: { 2672 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2673 QualType KmpInt32PtrTy = 2674 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2675 2676 Sema::CapturedParamNameType ParamsTeams[] = { 2677 std::make_pair(".global_tid.", KmpInt32PtrTy), 2678 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2679 std::make_pair(StringRef(), QualType()) // __context with shared vars 2680 }; 2681 // Start a captured region for 'target' with no implicit parameters. 2682 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2683 ParamsTeams); 2684 2685 Sema::CapturedParamNameType ParamsParallel[] = { 2686 std::make_pair(".global_tid.", KmpInt32PtrTy), 2687 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2688 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 2689 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 2690 std::make_pair(StringRef(), QualType()) // __context with shared vars 2691 }; 2692 // Start a captured region for 'teams' or 'parallel'. Both regions have 2693 // the same implicit parameters. 2694 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2695 ParamsParallel); 2696 break; 2697 } 2698 case OMPD_target_update: 2699 case OMPD_target_enter_data: 2700 case OMPD_target_exit_data: { 2701 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2702 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2703 QualType KmpInt32PtrTy = 2704 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2705 QualType Args[] = {VoidPtrTy}; 2706 FunctionProtoType::ExtProtoInfo EPI; 2707 EPI.Variadic = true; 2708 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2709 Sema::CapturedParamNameType Params[] = { 2710 std::make_pair(".global_tid.", KmpInt32Ty), 2711 std::make_pair(".part_id.", KmpInt32PtrTy), 2712 std::make_pair(".privates.", VoidPtrTy), 2713 std::make_pair( 2714 ".copy_fn.", 2715 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2716 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2717 std::make_pair(StringRef(), QualType()) // __context with shared vars 2718 }; 2719 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2720 Params); 2721 // Mark this captured region as inlined, because we don't use outlined 2722 // function directly. 2723 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2724 AlwaysInlineAttr::CreateImplicit( 2725 Context, AlwaysInlineAttr::Keyword_forceinline)); 2726 break; 2727 } 2728 case OMPD_threadprivate: 2729 case OMPD_taskyield: 2730 case OMPD_barrier: 2731 case OMPD_taskwait: 2732 case OMPD_cancellation_point: 2733 case OMPD_cancel: 2734 case OMPD_flush: 2735 case OMPD_declare_reduction: 2736 case OMPD_declare_simd: 2737 case OMPD_declare_target: 2738 case OMPD_end_declare_target: 2739 case OMPD_requires: 2740 llvm_unreachable("OpenMP Directive is not allowed"); 2741 case OMPD_unknown: 2742 llvm_unreachable("Unknown OpenMP directive"); 2743 } 2744 } 2745 2746 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 2747 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2748 getOpenMPCaptureRegions(CaptureRegions, DKind); 2749 return CaptureRegions.size(); 2750 } 2751 2752 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 2753 Expr *CaptureExpr, bool WithInit, 2754 bool AsExpression) { 2755 assert(CaptureExpr); 2756 ASTContext &C = S.getASTContext(); 2757 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 2758 QualType Ty = Init->getType(); 2759 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 2760 if (S.getLangOpts().CPlusPlus) { 2761 Ty = C.getLValueReferenceType(Ty); 2762 } else { 2763 Ty = C.getPointerType(Ty); 2764 ExprResult Res = 2765 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 2766 if (!Res.isUsable()) 2767 return nullptr; 2768 Init = Res.get(); 2769 } 2770 WithInit = true; 2771 } 2772 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 2773 CaptureExpr->getBeginLoc()); 2774 if (!WithInit) 2775 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C)); 2776 S.CurContext->addHiddenDecl(CED); 2777 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 2778 return CED; 2779 } 2780 2781 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 2782 bool WithInit) { 2783 OMPCapturedExprDecl *CD; 2784 if (VarDecl *VD = S.isOpenMPCapturedDecl(D)) 2785 CD = cast<OMPCapturedExprDecl>(VD); 2786 else 2787 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 2788 /*AsExpression=*/false); 2789 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 2790 CaptureExpr->getExprLoc()); 2791 } 2792 2793 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 2794 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get(); 2795 if (!Ref) { 2796 OMPCapturedExprDecl *CD = buildCaptureDecl( 2797 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr, 2798 /*WithInit=*/true, /*AsExpression=*/true); 2799 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 2800 CaptureExpr->getExprLoc()); 2801 } 2802 ExprResult Res = Ref; 2803 if (!S.getLangOpts().CPlusPlus && 2804 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 2805 Ref->getType()->isPointerType()) { 2806 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 2807 if (!Res.isUsable()) 2808 return ExprError(); 2809 } 2810 return S.DefaultLvalueConversion(Res.get()); 2811 } 2812 2813 namespace { 2814 // OpenMP directives parsed in this section are represented as a 2815 // CapturedStatement with an associated statement. If a syntax error 2816 // is detected during the parsing of the associated statement, the 2817 // compiler must abort processing and close the CapturedStatement. 2818 // 2819 // Combined directives such as 'target parallel' have more than one 2820 // nested CapturedStatements. This RAII ensures that we unwind out 2821 // of all the nested CapturedStatements when an error is found. 2822 class CaptureRegionUnwinderRAII { 2823 private: 2824 Sema &S; 2825 bool &ErrorFound; 2826 OpenMPDirectiveKind DKind = OMPD_unknown; 2827 2828 public: 2829 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 2830 OpenMPDirectiveKind DKind) 2831 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 2832 ~CaptureRegionUnwinderRAII() { 2833 if (ErrorFound) { 2834 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 2835 while (--ThisCaptureLevel >= 0) 2836 S.ActOnCapturedRegionError(); 2837 } 2838 } 2839 }; 2840 } // namespace 2841 2842 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 2843 ArrayRef<OMPClause *> Clauses) { 2844 bool ErrorFound = false; 2845 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 2846 *this, ErrorFound, DSAStack->getCurrentDirective()); 2847 if (!S.isUsable()) { 2848 ErrorFound = true; 2849 return StmtError(); 2850 } 2851 2852 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2853 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 2854 OMPOrderedClause *OC = nullptr; 2855 OMPScheduleClause *SC = nullptr; 2856 SmallVector<const OMPLinearClause *, 4> LCs; 2857 SmallVector<const OMPClauseWithPreInit *, 4> PICs; 2858 // This is required for proper codegen. 2859 for (OMPClause *Clause : Clauses) { 2860 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) && 2861 Clause->getClauseKind() == OMPC_in_reduction) { 2862 // Capture taskgroup task_reduction descriptors inside the tasking regions 2863 // with the corresponding in_reduction items. 2864 auto *IRC = cast<OMPInReductionClause>(Clause); 2865 for (Expr *E : IRC->taskgroup_descriptors()) 2866 if (E) 2867 MarkDeclarationsReferencedInExpr(E); 2868 } 2869 if (isOpenMPPrivate(Clause->getClauseKind()) || 2870 Clause->getClauseKind() == OMPC_copyprivate || 2871 (getLangOpts().OpenMPUseTLS && 2872 getASTContext().getTargetInfo().isTLSSupported() && 2873 Clause->getClauseKind() == OMPC_copyin)) { 2874 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 2875 // Mark all variables in private list clauses as used in inner region. 2876 for (Stmt *VarRef : Clause->children()) { 2877 if (auto *E = cast_or_null<Expr>(VarRef)) { 2878 MarkDeclarationsReferencedInExpr(E); 2879 } 2880 } 2881 DSAStack->setForceVarCapturing(/*V=*/false); 2882 } else if (CaptureRegions.size() > 1 || 2883 CaptureRegions.back() != OMPD_unknown) { 2884 if (auto *C = OMPClauseWithPreInit::get(Clause)) 2885 PICs.push_back(C); 2886 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 2887 if (Expr *E = C->getPostUpdateExpr()) 2888 MarkDeclarationsReferencedInExpr(E); 2889 } 2890 } 2891 if (Clause->getClauseKind() == OMPC_schedule) 2892 SC = cast<OMPScheduleClause>(Clause); 2893 else if (Clause->getClauseKind() == OMPC_ordered) 2894 OC = cast<OMPOrderedClause>(Clause); 2895 else if (Clause->getClauseKind() == OMPC_linear) 2896 LCs.push_back(cast<OMPLinearClause>(Clause)); 2897 } 2898 // OpenMP, 2.7.1 Loop Construct, Restrictions 2899 // The nonmonotonic modifier cannot be specified if an ordered clause is 2900 // specified. 2901 if (SC && 2902 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 2903 SC->getSecondScheduleModifier() == 2904 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 2905 OC) { 2906 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 2907 ? SC->getFirstScheduleModifierLoc() 2908 : SC->getSecondScheduleModifierLoc(), 2909 diag::err_omp_schedule_nonmonotonic_ordered) 2910 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 2911 ErrorFound = true; 2912 } 2913 if (!LCs.empty() && OC && OC->getNumForLoops()) { 2914 for (const OMPLinearClause *C : LCs) { 2915 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered) 2916 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 2917 } 2918 ErrorFound = true; 2919 } 2920 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 2921 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 2922 OC->getNumForLoops()) { 2923 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd) 2924 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 2925 ErrorFound = true; 2926 } 2927 if (ErrorFound) { 2928 return StmtError(); 2929 } 2930 StmtResult SR = S; 2931 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 2932 // Mark all variables in private list clauses as used in inner region. 2933 // Required for proper codegen of combined directives. 2934 // TODO: add processing for other clauses. 2935 if (ThisCaptureRegion != OMPD_unknown) { 2936 for (const clang::OMPClauseWithPreInit *C : PICs) { 2937 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 2938 // Find the particular capture region for the clause if the 2939 // directive is a combined one with multiple capture regions. 2940 // If the directive is not a combined one, the capture region 2941 // associated with the clause is OMPD_unknown and is generated 2942 // only once. 2943 if (CaptureRegion == ThisCaptureRegion || 2944 CaptureRegion == OMPD_unknown) { 2945 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 2946 for (Decl *D : DS->decls()) 2947 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 2948 } 2949 } 2950 } 2951 } 2952 SR = ActOnCapturedRegionEnd(SR.get()); 2953 } 2954 return SR; 2955 } 2956 2957 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 2958 OpenMPDirectiveKind CancelRegion, 2959 SourceLocation StartLoc) { 2960 // CancelRegion is only needed for cancel and cancellation_point. 2961 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 2962 return false; 2963 2964 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 2965 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 2966 return false; 2967 2968 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 2969 << getOpenMPDirectiveName(CancelRegion); 2970 return true; 2971 } 2972 2973 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack, 2974 OpenMPDirectiveKind CurrentRegion, 2975 const DeclarationNameInfo &CurrentName, 2976 OpenMPDirectiveKind CancelRegion, 2977 SourceLocation StartLoc) { 2978 if (Stack->getCurScope()) { 2979 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective(); 2980 OpenMPDirectiveKind OffendingRegion = ParentRegion; 2981 bool NestingProhibited = false; 2982 bool CloseNesting = true; 2983 bool OrphanSeen = false; 2984 enum { 2985 NoRecommend, 2986 ShouldBeInParallelRegion, 2987 ShouldBeInOrderedRegion, 2988 ShouldBeInTargetRegion, 2989 ShouldBeInTeamsRegion 2990 } Recommend = NoRecommend; 2991 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) { 2992 // OpenMP [2.16, Nesting of Regions] 2993 // OpenMP constructs may not be nested inside a simd region. 2994 // OpenMP [2.8.1,simd Construct, Restrictions] 2995 // An ordered construct with the simd clause is the only OpenMP 2996 // construct that can appear in the simd region. 2997 // Allowing a SIMD construct nested in another SIMD construct is an 2998 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 2999 // message. 3000 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 3001 ? diag::err_omp_prohibited_region_simd 3002 : diag::warn_omp_nesting_simd); 3003 return CurrentRegion != OMPD_simd; 3004 } 3005 if (ParentRegion == OMPD_atomic) { 3006 // OpenMP [2.16, Nesting of Regions] 3007 // OpenMP constructs may not be nested inside an atomic region. 3008 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 3009 return true; 3010 } 3011 if (CurrentRegion == OMPD_section) { 3012 // OpenMP [2.7.2, sections Construct, Restrictions] 3013 // Orphaned section directives are prohibited. That is, the section 3014 // directives must appear within the sections construct and must not be 3015 // encountered elsewhere in the sections region. 3016 if (ParentRegion != OMPD_sections && 3017 ParentRegion != OMPD_parallel_sections) { 3018 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 3019 << (ParentRegion != OMPD_unknown) 3020 << getOpenMPDirectiveName(ParentRegion); 3021 return true; 3022 } 3023 return false; 3024 } 3025 // Allow some constructs (except teams) to be orphaned (they could be 3026 // used in functions, called from OpenMP regions with the required 3027 // preconditions). 3028 if (ParentRegion == OMPD_unknown && 3029 !isOpenMPNestingTeamsDirective(CurrentRegion)) 3030 return false; 3031 if (CurrentRegion == OMPD_cancellation_point || 3032 CurrentRegion == OMPD_cancel) { 3033 // OpenMP [2.16, Nesting of Regions] 3034 // A cancellation point construct for which construct-type-clause is 3035 // taskgroup must be nested inside a task construct. A cancellation 3036 // point construct for which construct-type-clause is not taskgroup must 3037 // be closely nested inside an OpenMP construct that matches the type 3038 // specified in construct-type-clause. 3039 // A cancel construct for which construct-type-clause is taskgroup must be 3040 // nested inside a task construct. A cancel construct for which 3041 // construct-type-clause is not taskgroup must be closely nested inside an 3042 // OpenMP construct that matches the type specified in 3043 // construct-type-clause. 3044 NestingProhibited = 3045 !((CancelRegion == OMPD_parallel && 3046 (ParentRegion == OMPD_parallel || 3047 ParentRegion == OMPD_target_parallel)) || 3048 (CancelRegion == OMPD_for && 3049 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 3050 ParentRegion == OMPD_target_parallel_for || 3051 ParentRegion == OMPD_distribute_parallel_for || 3052 ParentRegion == OMPD_teams_distribute_parallel_for || 3053 ParentRegion == OMPD_target_teams_distribute_parallel_for)) || 3054 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) || 3055 (CancelRegion == OMPD_sections && 3056 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 3057 ParentRegion == OMPD_parallel_sections))); 3058 } else if (CurrentRegion == OMPD_master) { 3059 // OpenMP [2.16, Nesting of Regions] 3060 // A master region may not be closely nested inside a worksharing, 3061 // atomic, or explicit task region. 3062 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 3063 isOpenMPTaskingDirective(ParentRegion); 3064 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 3065 // OpenMP [2.16, Nesting of Regions] 3066 // A critical region may not be nested (closely or otherwise) inside a 3067 // critical region with the same name. Note that this restriction is not 3068 // sufficient to prevent deadlock. 3069 SourceLocation PreviousCriticalLoc; 3070 bool DeadLock = Stack->hasDirective( 3071 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 3072 const DeclarationNameInfo &DNI, 3073 SourceLocation Loc) { 3074 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 3075 PreviousCriticalLoc = Loc; 3076 return true; 3077 } 3078 return false; 3079 }, 3080 false /* skip top directive */); 3081 if (DeadLock) { 3082 SemaRef.Diag(StartLoc, 3083 diag::err_omp_prohibited_region_critical_same_name) 3084 << CurrentName.getName(); 3085 if (PreviousCriticalLoc.isValid()) 3086 SemaRef.Diag(PreviousCriticalLoc, 3087 diag::note_omp_previous_critical_region); 3088 return true; 3089 } 3090 } else if (CurrentRegion == OMPD_barrier) { 3091 // OpenMP [2.16, Nesting of Regions] 3092 // A barrier region may not be closely nested inside a worksharing, 3093 // explicit task, critical, ordered, atomic, or master region. 3094 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 3095 isOpenMPTaskingDirective(ParentRegion) || 3096 ParentRegion == OMPD_master || 3097 ParentRegion == OMPD_critical || 3098 ParentRegion == OMPD_ordered; 3099 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 3100 !isOpenMPParallelDirective(CurrentRegion) && 3101 !isOpenMPTeamsDirective(CurrentRegion)) { 3102 // OpenMP [2.16, Nesting of Regions] 3103 // A worksharing region may not be closely nested inside a worksharing, 3104 // explicit task, critical, ordered, atomic, or master region. 3105 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 3106 isOpenMPTaskingDirective(ParentRegion) || 3107 ParentRegion == OMPD_master || 3108 ParentRegion == OMPD_critical || 3109 ParentRegion == OMPD_ordered; 3110 Recommend = ShouldBeInParallelRegion; 3111 } else if (CurrentRegion == OMPD_ordered) { 3112 // OpenMP [2.16, Nesting of Regions] 3113 // An ordered region may not be closely nested inside a critical, 3114 // atomic, or explicit task region. 3115 // An ordered region must be closely nested inside a loop region (or 3116 // parallel loop region) with an ordered clause. 3117 // OpenMP [2.8.1,simd Construct, Restrictions] 3118 // An ordered construct with the simd clause is the only OpenMP construct 3119 // that can appear in the simd region. 3120 NestingProhibited = ParentRegion == OMPD_critical || 3121 isOpenMPTaskingDirective(ParentRegion) || 3122 !(isOpenMPSimdDirective(ParentRegion) || 3123 Stack->isParentOrderedRegion()); 3124 Recommend = ShouldBeInOrderedRegion; 3125 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 3126 // OpenMP [2.16, Nesting of Regions] 3127 // If specified, a teams construct must be contained within a target 3128 // construct. 3129 NestingProhibited = ParentRegion != OMPD_target; 3130 OrphanSeen = ParentRegion == OMPD_unknown; 3131 Recommend = ShouldBeInTargetRegion; 3132 } 3133 if (!NestingProhibited && 3134 !isOpenMPTargetExecutionDirective(CurrentRegion) && 3135 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 3136 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 3137 // OpenMP [2.16, Nesting of Regions] 3138 // distribute, parallel, parallel sections, parallel workshare, and the 3139 // parallel loop and parallel loop SIMD constructs are the only OpenMP 3140 // constructs that can be closely nested in the teams region. 3141 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 3142 !isOpenMPDistributeDirective(CurrentRegion); 3143 Recommend = ShouldBeInParallelRegion; 3144 } 3145 if (!NestingProhibited && 3146 isOpenMPNestingDistributeDirective(CurrentRegion)) { 3147 // OpenMP 4.5 [2.17 Nesting of Regions] 3148 // The region associated with the distribute construct must be strictly 3149 // nested inside a teams region 3150 NestingProhibited = 3151 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 3152 Recommend = ShouldBeInTeamsRegion; 3153 } 3154 if (!NestingProhibited && 3155 (isOpenMPTargetExecutionDirective(CurrentRegion) || 3156 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 3157 // OpenMP 4.5 [2.17 Nesting of Regions] 3158 // If a target, target update, target data, target enter data, or 3159 // target exit data construct is encountered during execution of a 3160 // target region, the behavior is unspecified. 3161 NestingProhibited = Stack->hasDirective( 3162 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 3163 SourceLocation) { 3164 if (isOpenMPTargetExecutionDirective(K)) { 3165 OffendingRegion = K; 3166 return true; 3167 } 3168 return false; 3169 }, 3170 false /* don't skip top directive */); 3171 CloseNesting = false; 3172 } 3173 if (NestingProhibited) { 3174 if (OrphanSeen) { 3175 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 3176 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 3177 } else { 3178 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 3179 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 3180 << Recommend << getOpenMPDirectiveName(CurrentRegion); 3181 } 3182 return true; 3183 } 3184 } 3185 return false; 3186 } 3187 3188 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 3189 ArrayRef<OMPClause *> Clauses, 3190 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 3191 bool ErrorFound = false; 3192 unsigned NamedModifiersNumber = 0; 3193 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers( 3194 OMPD_unknown + 1); 3195 SmallVector<SourceLocation, 4> NameModifierLoc; 3196 for (const OMPClause *C : Clauses) { 3197 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 3198 // At most one if clause without a directive-name-modifier can appear on 3199 // the directive. 3200 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 3201 if (FoundNameModifiers[CurNM]) { 3202 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 3203 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 3204 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 3205 ErrorFound = true; 3206 } else if (CurNM != OMPD_unknown) { 3207 NameModifierLoc.push_back(IC->getNameModifierLoc()); 3208 ++NamedModifiersNumber; 3209 } 3210 FoundNameModifiers[CurNM] = IC; 3211 if (CurNM == OMPD_unknown) 3212 continue; 3213 // Check if the specified name modifier is allowed for the current 3214 // directive. 3215 // At most one if clause with the particular directive-name-modifier can 3216 // appear on the directive. 3217 bool MatchFound = false; 3218 for (auto NM : AllowedNameModifiers) { 3219 if (CurNM == NM) { 3220 MatchFound = true; 3221 break; 3222 } 3223 } 3224 if (!MatchFound) { 3225 S.Diag(IC->getNameModifierLoc(), 3226 diag::err_omp_wrong_if_directive_name_modifier) 3227 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 3228 ErrorFound = true; 3229 } 3230 } 3231 } 3232 // If any if clause on the directive includes a directive-name-modifier then 3233 // all if clauses on the directive must include a directive-name-modifier. 3234 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 3235 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 3236 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(), 3237 diag::err_omp_no_more_if_clause); 3238 } else { 3239 std::string Values; 3240 std::string Sep(", "); 3241 unsigned AllowedCnt = 0; 3242 unsigned TotalAllowedNum = 3243 AllowedNameModifiers.size() - NamedModifiersNumber; 3244 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 3245 ++Cnt) { 3246 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 3247 if (!FoundNameModifiers[NM]) { 3248 Values += "'"; 3249 Values += getOpenMPDirectiveName(NM); 3250 Values += "'"; 3251 if (AllowedCnt + 2 == TotalAllowedNum) 3252 Values += " or "; 3253 else if (AllowedCnt + 1 != TotalAllowedNum) 3254 Values += Sep; 3255 ++AllowedCnt; 3256 } 3257 } 3258 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(), 3259 diag::err_omp_unnamed_if_clause) 3260 << (TotalAllowedNum > 1) << Values; 3261 } 3262 for (SourceLocation Loc : NameModifierLoc) { 3263 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 3264 } 3265 ErrorFound = true; 3266 } 3267 return ErrorFound; 3268 } 3269 3270 StmtResult Sema::ActOnOpenMPExecutableDirective( 3271 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 3272 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 3273 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 3274 StmtResult Res = StmtError(); 3275 // First check CancelRegion which is then used in checkNestingOfRegions. 3276 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 3277 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 3278 StartLoc)) 3279 return StmtError(); 3280 3281 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 3282 VarsWithInheritedDSAType VarsWithInheritedDSA; 3283 bool ErrorFound = false; 3284 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 3285 if (AStmt && !CurContext->isDependentContext()) { 3286 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3287 3288 // Check default data sharing attributes for referenced variables. 3289 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 3290 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 3291 Stmt *S = AStmt; 3292 while (--ThisCaptureLevel >= 0) 3293 S = cast<CapturedStmt>(S)->getCapturedStmt(); 3294 DSAChecker.Visit(S); 3295 if (DSAChecker.isErrorFound()) 3296 return StmtError(); 3297 // Generate list of implicitly defined firstprivate variables. 3298 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 3299 3300 SmallVector<Expr *, 4> ImplicitFirstprivates( 3301 DSAChecker.getImplicitFirstprivate().begin(), 3302 DSAChecker.getImplicitFirstprivate().end()); 3303 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(), 3304 DSAChecker.getImplicitMap().end()); 3305 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 3306 for (OMPClause *C : Clauses) { 3307 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 3308 for (Expr *E : IRC->taskgroup_descriptors()) 3309 if (E) 3310 ImplicitFirstprivates.emplace_back(E); 3311 } 3312 } 3313 if (!ImplicitFirstprivates.empty()) { 3314 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 3315 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 3316 SourceLocation())) { 3317 ClausesWithImplicit.push_back(Implicit); 3318 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 3319 ImplicitFirstprivates.size(); 3320 } else { 3321 ErrorFound = true; 3322 } 3323 } 3324 if (!ImplicitMaps.empty()) { 3325 if (OMPClause *Implicit = ActOnOpenMPMapClause( 3326 OMPC_MAP_unknown, OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true, 3327 SourceLocation(), SourceLocation(), ImplicitMaps, 3328 SourceLocation(), SourceLocation(), SourceLocation())) { 3329 ClausesWithImplicit.emplace_back(Implicit); 3330 ErrorFound |= 3331 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size(); 3332 } else { 3333 ErrorFound = true; 3334 } 3335 } 3336 } 3337 3338 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 3339 switch (Kind) { 3340 case OMPD_parallel: 3341 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 3342 EndLoc); 3343 AllowedNameModifiers.push_back(OMPD_parallel); 3344 break; 3345 case OMPD_simd: 3346 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 3347 VarsWithInheritedDSA); 3348 break; 3349 case OMPD_for: 3350 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 3351 VarsWithInheritedDSA); 3352 break; 3353 case OMPD_for_simd: 3354 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 3355 EndLoc, VarsWithInheritedDSA); 3356 break; 3357 case OMPD_sections: 3358 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 3359 EndLoc); 3360 break; 3361 case OMPD_section: 3362 assert(ClausesWithImplicit.empty() && 3363 "No clauses are allowed for 'omp section' directive"); 3364 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 3365 break; 3366 case OMPD_single: 3367 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 3368 EndLoc); 3369 break; 3370 case OMPD_master: 3371 assert(ClausesWithImplicit.empty() && 3372 "No clauses are allowed for 'omp master' directive"); 3373 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 3374 break; 3375 case OMPD_critical: 3376 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 3377 StartLoc, EndLoc); 3378 break; 3379 case OMPD_parallel_for: 3380 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 3381 EndLoc, VarsWithInheritedDSA); 3382 AllowedNameModifiers.push_back(OMPD_parallel); 3383 break; 3384 case OMPD_parallel_for_simd: 3385 Res = ActOnOpenMPParallelForSimdDirective( 3386 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3387 AllowedNameModifiers.push_back(OMPD_parallel); 3388 break; 3389 case OMPD_parallel_sections: 3390 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 3391 StartLoc, EndLoc); 3392 AllowedNameModifiers.push_back(OMPD_parallel); 3393 break; 3394 case OMPD_task: 3395 Res = 3396 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 3397 AllowedNameModifiers.push_back(OMPD_task); 3398 break; 3399 case OMPD_taskyield: 3400 assert(ClausesWithImplicit.empty() && 3401 "No clauses are allowed for 'omp taskyield' directive"); 3402 assert(AStmt == nullptr && 3403 "No associated statement allowed for 'omp taskyield' directive"); 3404 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 3405 break; 3406 case OMPD_barrier: 3407 assert(ClausesWithImplicit.empty() && 3408 "No clauses are allowed for 'omp barrier' directive"); 3409 assert(AStmt == nullptr && 3410 "No associated statement allowed for 'omp barrier' directive"); 3411 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 3412 break; 3413 case OMPD_taskwait: 3414 assert(ClausesWithImplicit.empty() && 3415 "No clauses are allowed for 'omp taskwait' directive"); 3416 assert(AStmt == nullptr && 3417 "No associated statement allowed for 'omp taskwait' directive"); 3418 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 3419 break; 3420 case OMPD_taskgroup: 3421 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 3422 EndLoc); 3423 break; 3424 case OMPD_flush: 3425 assert(AStmt == nullptr && 3426 "No associated statement allowed for 'omp flush' directive"); 3427 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 3428 break; 3429 case OMPD_ordered: 3430 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 3431 EndLoc); 3432 break; 3433 case OMPD_atomic: 3434 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 3435 EndLoc); 3436 break; 3437 case OMPD_teams: 3438 Res = 3439 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 3440 break; 3441 case OMPD_target: 3442 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 3443 EndLoc); 3444 AllowedNameModifiers.push_back(OMPD_target); 3445 break; 3446 case OMPD_target_parallel: 3447 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 3448 StartLoc, EndLoc); 3449 AllowedNameModifiers.push_back(OMPD_target); 3450 AllowedNameModifiers.push_back(OMPD_parallel); 3451 break; 3452 case OMPD_target_parallel_for: 3453 Res = ActOnOpenMPTargetParallelForDirective( 3454 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3455 AllowedNameModifiers.push_back(OMPD_target); 3456 AllowedNameModifiers.push_back(OMPD_parallel); 3457 break; 3458 case OMPD_cancellation_point: 3459 assert(ClausesWithImplicit.empty() && 3460 "No clauses are allowed for 'omp cancellation point' directive"); 3461 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 3462 "cancellation point' directive"); 3463 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 3464 break; 3465 case OMPD_cancel: 3466 assert(AStmt == nullptr && 3467 "No associated statement allowed for 'omp cancel' directive"); 3468 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 3469 CancelRegion); 3470 AllowedNameModifiers.push_back(OMPD_cancel); 3471 break; 3472 case OMPD_target_data: 3473 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 3474 EndLoc); 3475 AllowedNameModifiers.push_back(OMPD_target_data); 3476 break; 3477 case OMPD_target_enter_data: 3478 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 3479 EndLoc, AStmt); 3480 AllowedNameModifiers.push_back(OMPD_target_enter_data); 3481 break; 3482 case OMPD_target_exit_data: 3483 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 3484 EndLoc, AStmt); 3485 AllowedNameModifiers.push_back(OMPD_target_exit_data); 3486 break; 3487 case OMPD_taskloop: 3488 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 3489 EndLoc, VarsWithInheritedDSA); 3490 AllowedNameModifiers.push_back(OMPD_taskloop); 3491 break; 3492 case OMPD_taskloop_simd: 3493 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 3494 EndLoc, VarsWithInheritedDSA); 3495 AllowedNameModifiers.push_back(OMPD_taskloop); 3496 break; 3497 case OMPD_distribute: 3498 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 3499 EndLoc, VarsWithInheritedDSA); 3500 break; 3501 case OMPD_target_update: 3502 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 3503 EndLoc, AStmt); 3504 AllowedNameModifiers.push_back(OMPD_target_update); 3505 break; 3506 case OMPD_distribute_parallel_for: 3507 Res = ActOnOpenMPDistributeParallelForDirective( 3508 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3509 AllowedNameModifiers.push_back(OMPD_parallel); 3510 break; 3511 case OMPD_distribute_parallel_for_simd: 3512 Res = ActOnOpenMPDistributeParallelForSimdDirective( 3513 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3514 AllowedNameModifiers.push_back(OMPD_parallel); 3515 break; 3516 case OMPD_distribute_simd: 3517 Res = ActOnOpenMPDistributeSimdDirective( 3518 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3519 break; 3520 case OMPD_target_parallel_for_simd: 3521 Res = ActOnOpenMPTargetParallelForSimdDirective( 3522 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3523 AllowedNameModifiers.push_back(OMPD_target); 3524 AllowedNameModifiers.push_back(OMPD_parallel); 3525 break; 3526 case OMPD_target_simd: 3527 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 3528 EndLoc, VarsWithInheritedDSA); 3529 AllowedNameModifiers.push_back(OMPD_target); 3530 break; 3531 case OMPD_teams_distribute: 3532 Res = ActOnOpenMPTeamsDistributeDirective( 3533 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3534 break; 3535 case OMPD_teams_distribute_simd: 3536 Res = ActOnOpenMPTeamsDistributeSimdDirective( 3537 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3538 break; 3539 case OMPD_teams_distribute_parallel_for_simd: 3540 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 3541 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3542 AllowedNameModifiers.push_back(OMPD_parallel); 3543 break; 3544 case OMPD_teams_distribute_parallel_for: 3545 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 3546 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3547 AllowedNameModifiers.push_back(OMPD_parallel); 3548 break; 3549 case OMPD_target_teams: 3550 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 3551 EndLoc); 3552 AllowedNameModifiers.push_back(OMPD_target); 3553 break; 3554 case OMPD_target_teams_distribute: 3555 Res = ActOnOpenMPTargetTeamsDistributeDirective( 3556 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3557 AllowedNameModifiers.push_back(OMPD_target); 3558 break; 3559 case OMPD_target_teams_distribute_parallel_for: 3560 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 3561 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3562 AllowedNameModifiers.push_back(OMPD_target); 3563 AllowedNameModifiers.push_back(OMPD_parallel); 3564 break; 3565 case OMPD_target_teams_distribute_parallel_for_simd: 3566 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 3567 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3568 AllowedNameModifiers.push_back(OMPD_target); 3569 AllowedNameModifiers.push_back(OMPD_parallel); 3570 break; 3571 case OMPD_target_teams_distribute_simd: 3572 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 3573 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3574 AllowedNameModifiers.push_back(OMPD_target); 3575 break; 3576 case OMPD_declare_target: 3577 case OMPD_end_declare_target: 3578 case OMPD_threadprivate: 3579 case OMPD_declare_reduction: 3580 case OMPD_declare_simd: 3581 case OMPD_requires: 3582 llvm_unreachable("OpenMP Directive is not allowed"); 3583 case OMPD_unknown: 3584 llvm_unreachable("Unknown OpenMP directive"); 3585 } 3586 3587 for (const auto &P : VarsWithInheritedDSA) { 3588 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 3589 << P.first << P.second->getSourceRange(); 3590 } 3591 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound; 3592 3593 if (!AllowedNameModifiers.empty()) 3594 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 3595 ErrorFound; 3596 3597 if (ErrorFound) 3598 return StmtError(); 3599 return Res; 3600 } 3601 3602 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 3603 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 3604 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 3605 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 3606 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 3607 assert(Aligneds.size() == Alignments.size()); 3608 assert(Linears.size() == LinModifiers.size()); 3609 assert(Linears.size() == Steps.size()); 3610 if (!DG || DG.get().isNull()) 3611 return DeclGroupPtrTy(); 3612 3613 if (!DG.get().isSingleDecl()) { 3614 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd); 3615 return DG; 3616 } 3617 Decl *ADecl = DG.get().getSingleDecl(); 3618 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 3619 ADecl = FTD->getTemplatedDecl(); 3620 3621 auto *FD = dyn_cast<FunctionDecl>(ADecl); 3622 if (!FD) { 3623 Diag(ADecl->getLocation(), diag::err_omp_function_expected); 3624 return DeclGroupPtrTy(); 3625 } 3626 3627 // OpenMP [2.8.2, declare simd construct, Description] 3628 // The parameter of the simdlen clause must be a constant positive integer 3629 // expression. 3630 ExprResult SL; 3631 if (Simdlen) 3632 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 3633 // OpenMP [2.8.2, declare simd construct, Description] 3634 // The special this pointer can be used as if was one of the arguments to the 3635 // function in any of the linear, aligned, or uniform clauses. 3636 // The uniform clause declares one or more arguments to have an invariant 3637 // value for all concurrent invocations of the function in the execution of a 3638 // single SIMD loop. 3639 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 3640 const Expr *UniformedLinearThis = nullptr; 3641 for (const Expr *E : Uniforms) { 3642 E = E->IgnoreParenImpCasts(); 3643 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 3644 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 3645 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 3646 FD->getParamDecl(PVD->getFunctionScopeIndex()) 3647 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 3648 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 3649 continue; 3650 } 3651 if (isa<CXXThisExpr>(E)) { 3652 UniformedLinearThis = E; 3653 continue; 3654 } 3655 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 3656 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 3657 } 3658 // OpenMP [2.8.2, declare simd construct, Description] 3659 // The aligned clause declares that the object to which each list item points 3660 // is aligned to the number of bytes expressed in the optional parameter of 3661 // the aligned clause. 3662 // The special this pointer can be used as if was one of the arguments to the 3663 // function in any of the linear, aligned, or uniform clauses. 3664 // The type of list items appearing in the aligned clause must be array, 3665 // pointer, reference to array, or reference to pointer. 3666 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 3667 const Expr *AlignedThis = nullptr; 3668 for (const Expr *E : Aligneds) { 3669 E = E->IgnoreParenImpCasts(); 3670 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 3671 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 3672 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 3673 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 3674 FD->getParamDecl(PVD->getFunctionScopeIndex()) 3675 ->getCanonicalDecl() == CanonPVD) { 3676 // OpenMP [2.8.1, simd construct, Restrictions] 3677 // A list-item cannot appear in more than one aligned clause. 3678 if (AlignedArgs.count(CanonPVD) > 0) { 3679 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 3680 << 1 << E->getSourceRange(); 3681 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 3682 diag::note_omp_explicit_dsa) 3683 << getOpenMPClauseName(OMPC_aligned); 3684 continue; 3685 } 3686 AlignedArgs[CanonPVD] = E; 3687 QualType QTy = PVD->getType() 3688 .getNonReferenceType() 3689 .getUnqualifiedType() 3690 .getCanonicalType(); 3691 const Type *Ty = QTy.getTypePtrOrNull(); 3692 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 3693 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 3694 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 3695 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 3696 } 3697 continue; 3698 } 3699 } 3700 if (isa<CXXThisExpr>(E)) { 3701 if (AlignedThis) { 3702 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 3703 << 2 << E->getSourceRange(); 3704 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 3705 << getOpenMPClauseName(OMPC_aligned); 3706 } 3707 AlignedThis = E; 3708 continue; 3709 } 3710 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 3711 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 3712 } 3713 // The optional parameter of the aligned clause, alignment, must be a constant 3714 // positive integer expression. If no optional parameter is specified, 3715 // implementation-defined default alignments for SIMD instructions on the 3716 // target platforms are assumed. 3717 SmallVector<const Expr *, 4> NewAligns; 3718 for (Expr *E : Alignments) { 3719 ExprResult Align; 3720 if (E) 3721 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 3722 NewAligns.push_back(Align.get()); 3723 } 3724 // OpenMP [2.8.2, declare simd construct, Description] 3725 // The linear clause declares one or more list items to be private to a SIMD 3726 // lane and to have a linear relationship with respect to the iteration space 3727 // of a loop. 3728 // The special this pointer can be used as if was one of the arguments to the 3729 // function in any of the linear, aligned, or uniform clauses. 3730 // When a linear-step expression is specified in a linear clause it must be 3731 // either a constant integer expression or an integer-typed parameter that is 3732 // specified in a uniform clause on the directive. 3733 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 3734 const bool IsUniformedThis = UniformedLinearThis != nullptr; 3735 auto MI = LinModifiers.begin(); 3736 for (const Expr *E : Linears) { 3737 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 3738 ++MI; 3739 E = E->IgnoreParenImpCasts(); 3740 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 3741 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 3742 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 3743 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 3744 FD->getParamDecl(PVD->getFunctionScopeIndex()) 3745 ->getCanonicalDecl() == CanonPVD) { 3746 // OpenMP [2.15.3.7, linear Clause, Restrictions] 3747 // A list-item cannot appear in more than one linear clause. 3748 if (LinearArgs.count(CanonPVD) > 0) { 3749 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3750 << getOpenMPClauseName(OMPC_linear) 3751 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 3752 Diag(LinearArgs[CanonPVD]->getExprLoc(), 3753 diag::note_omp_explicit_dsa) 3754 << getOpenMPClauseName(OMPC_linear); 3755 continue; 3756 } 3757 // Each argument can appear in at most one uniform or linear clause. 3758 if (UniformedArgs.count(CanonPVD) > 0) { 3759 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3760 << getOpenMPClauseName(OMPC_linear) 3761 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 3762 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 3763 diag::note_omp_explicit_dsa) 3764 << getOpenMPClauseName(OMPC_uniform); 3765 continue; 3766 } 3767 LinearArgs[CanonPVD] = E; 3768 if (E->isValueDependent() || E->isTypeDependent() || 3769 E->isInstantiationDependent() || 3770 E->containsUnexpandedParameterPack()) 3771 continue; 3772 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 3773 PVD->getOriginalType()); 3774 continue; 3775 } 3776 } 3777 if (isa<CXXThisExpr>(E)) { 3778 if (UniformedLinearThis) { 3779 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 3780 << getOpenMPClauseName(OMPC_linear) 3781 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 3782 << E->getSourceRange(); 3783 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 3784 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 3785 : OMPC_linear); 3786 continue; 3787 } 3788 UniformedLinearThis = E; 3789 if (E->isValueDependent() || E->isTypeDependent() || 3790 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 3791 continue; 3792 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 3793 E->getType()); 3794 continue; 3795 } 3796 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 3797 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 3798 } 3799 Expr *Step = nullptr; 3800 Expr *NewStep = nullptr; 3801 SmallVector<Expr *, 4> NewSteps; 3802 for (Expr *E : Steps) { 3803 // Skip the same step expression, it was checked already. 3804 if (Step == E || !E) { 3805 NewSteps.push_back(E ? NewStep : nullptr); 3806 continue; 3807 } 3808 Step = E; 3809 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 3810 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 3811 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 3812 if (UniformedArgs.count(CanonPVD) == 0) { 3813 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 3814 << Step->getSourceRange(); 3815 } else if (E->isValueDependent() || E->isTypeDependent() || 3816 E->isInstantiationDependent() || 3817 E->containsUnexpandedParameterPack() || 3818 CanonPVD->getType()->hasIntegerRepresentation()) { 3819 NewSteps.push_back(Step); 3820 } else { 3821 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 3822 << Step->getSourceRange(); 3823 } 3824 continue; 3825 } 3826 NewStep = Step; 3827 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 3828 !Step->isInstantiationDependent() && 3829 !Step->containsUnexpandedParameterPack()) { 3830 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 3831 .get(); 3832 if (NewStep) 3833 NewStep = VerifyIntegerConstantExpression(NewStep).get(); 3834 } 3835 NewSteps.push_back(NewStep); 3836 } 3837 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 3838 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 3839 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 3840 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 3841 const_cast<Expr **>(Linears.data()), Linears.size(), 3842 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 3843 NewSteps.data(), NewSteps.size(), SR); 3844 ADecl->addAttr(NewAttr); 3845 return ConvertDeclToDeclGroup(ADecl); 3846 } 3847 3848 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 3849 Stmt *AStmt, 3850 SourceLocation StartLoc, 3851 SourceLocation EndLoc) { 3852 if (!AStmt) 3853 return StmtError(); 3854 3855 auto *CS = cast<CapturedStmt>(AStmt); 3856 // 1.2.2 OpenMP Language Terminology 3857 // Structured block - An executable statement with a single entry at the 3858 // top and a single exit at the bottom. 3859 // The point of exit cannot be a branch out of the structured block. 3860 // longjmp() and throw() must not violate the entry/exit criteria. 3861 CS->getCapturedDecl()->setNothrow(); 3862 3863 setFunctionHasBranchProtectedScope(); 3864 3865 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 3866 DSAStack->isCancelRegion()); 3867 } 3868 3869 namespace { 3870 /// Helper class for checking canonical form of the OpenMP loops and 3871 /// extracting iteration space of each loop in the loop nest, that will be used 3872 /// for IR generation. 3873 class OpenMPIterationSpaceChecker { 3874 /// Reference to Sema. 3875 Sema &SemaRef; 3876 /// A location for diagnostics (when there is no some better location). 3877 SourceLocation DefaultLoc; 3878 /// A location for diagnostics (when increment is not compatible). 3879 SourceLocation ConditionLoc; 3880 /// A source location for referring to loop init later. 3881 SourceRange InitSrcRange; 3882 /// A source location for referring to condition later. 3883 SourceRange ConditionSrcRange; 3884 /// A source location for referring to increment later. 3885 SourceRange IncrementSrcRange; 3886 /// Loop variable. 3887 ValueDecl *LCDecl = nullptr; 3888 /// Reference to loop variable. 3889 Expr *LCRef = nullptr; 3890 /// Lower bound (initializer for the var). 3891 Expr *LB = nullptr; 3892 /// Upper bound. 3893 Expr *UB = nullptr; 3894 /// Loop step (increment). 3895 Expr *Step = nullptr; 3896 /// This flag is true when condition is one of: 3897 /// Var < UB 3898 /// Var <= UB 3899 /// UB > Var 3900 /// UB >= Var 3901 bool TestIsLessOp = false; 3902 /// This flag is true when condition is strict ( < or > ). 3903 bool TestIsStrictOp = false; 3904 /// This flag is true when step is subtracted on each iteration. 3905 bool SubtractStep = false; 3906 3907 public: 3908 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc) 3909 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {} 3910 /// Check init-expr for canonical loop form and save loop counter 3911 /// variable - #Var and its initialization value - #LB. 3912 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 3913 /// Check test-expr for canonical form, save upper-bound (#UB), flags 3914 /// for less/greater and for strict/non-strict comparison. 3915 bool checkAndSetCond(Expr *S); 3916 /// Check incr-expr for canonical loop form and return true if it 3917 /// does not conform, otherwise save loop step (#Step). 3918 bool checkAndSetInc(Expr *S); 3919 /// Return the loop counter variable. 3920 ValueDecl *getLoopDecl() const { return LCDecl; } 3921 /// Return the reference expression to loop counter variable. 3922 Expr *getLoopDeclRefExpr() const { return LCRef; } 3923 /// Source range of the loop init. 3924 SourceRange getInitSrcRange() const { return InitSrcRange; } 3925 /// Source range of the loop condition. 3926 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 3927 /// Source range of the loop increment. 3928 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 3929 /// True if the step should be subtracted. 3930 bool shouldSubtractStep() const { return SubtractStep; } 3931 /// Build the expression to calculate the number of iterations. 3932 Expr *buildNumIterations( 3933 Scope *S, const bool LimitedType, 3934 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 3935 /// Build the precondition expression for the loops. 3936 Expr * 3937 buildPreCond(Scope *S, Expr *Cond, 3938 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 3939 /// Build reference expression to the counter be used for codegen. 3940 DeclRefExpr * 3941 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 3942 DSAStackTy &DSA) const; 3943 /// Build reference expression to the private counter be used for 3944 /// codegen. 3945 Expr *buildPrivateCounterVar() const; 3946 /// Build initialization of the counter be used for codegen. 3947 Expr *buildCounterInit() const; 3948 /// Build step of the counter be used for codegen. 3949 Expr *buildCounterStep() const; 3950 /// Build loop data with counter value for depend clauses in ordered 3951 /// directives. 3952 Expr * 3953 buildOrderedLoopData(Scope *S, Expr *Counter, 3954 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 3955 SourceLocation Loc, Expr *Inc = nullptr, 3956 OverloadedOperatorKind OOK = OO_Amp); 3957 /// Return true if any expression is dependent. 3958 bool dependent() const; 3959 3960 private: 3961 /// Check the right-hand side of an assignment in the increment 3962 /// expression. 3963 bool checkAndSetIncRHS(Expr *RHS); 3964 /// Helper to set loop counter variable and its initializer. 3965 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB); 3966 /// Helper to set upper bound. 3967 bool setUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR, 3968 SourceLocation SL); 3969 /// Helper to set loop increment. 3970 bool setStep(Expr *NewStep, bool Subtract); 3971 }; 3972 3973 bool OpenMPIterationSpaceChecker::dependent() const { 3974 if (!LCDecl) { 3975 assert(!LB && !UB && !Step); 3976 return false; 3977 } 3978 return LCDecl->getType()->isDependentType() || 3979 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 3980 (Step && Step->isValueDependent()); 3981 } 3982 3983 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 3984 Expr *NewLCRefExpr, 3985 Expr *NewLB) { 3986 // State consistency checking to ensure correct usage. 3987 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 3988 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 3989 if (!NewLCDecl || !NewLB) 3990 return true; 3991 LCDecl = getCanonicalDecl(NewLCDecl); 3992 LCRef = NewLCRefExpr; 3993 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 3994 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 3995 if ((Ctor->isCopyOrMoveConstructor() || 3996 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 3997 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 3998 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 3999 LB = NewLB; 4000 return false; 4001 } 4002 4003 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, bool LessOp, bool StrictOp, 4004 SourceRange SR, SourceLocation SL) { 4005 // State consistency checking to ensure correct usage. 4006 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 4007 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 4008 if (!NewUB) 4009 return true; 4010 UB = NewUB; 4011 TestIsLessOp = LessOp; 4012 TestIsStrictOp = StrictOp; 4013 ConditionSrcRange = SR; 4014 ConditionLoc = SL; 4015 return false; 4016 } 4017 4018 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 4019 // State consistency checking to ensure correct usage. 4020 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 4021 if (!NewStep) 4022 return true; 4023 if (!NewStep->isValueDependent()) { 4024 // Check that the step is integer expression. 4025 SourceLocation StepLoc = NewStep->getBeginLoc(); 4026 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 4027 StepLoc, getExprAsWritten(NewStep)); 4028 if (Val.isInvalid()) 4029 return true; 4030 NewStep = Val.get(); 4031 4032 // OpenMP [2.6, Canonical Loop Form, Restrictions] 4033 // If test-expr is of form var relational-op b and relational-op is < or 4034 // <= then incr-expr must cause var to increase on each iteration of the 4035 // loop. If test-expr is of form var relational-op b and relational-op is 4036 // > or >= then incr-expr must cause var to decrease on each iteration of 4037 // the loop. 4038 // If test-expr is of form b relational-op var and relational-op is < or 4039 // <= then incr-expr must cause var to decrease on each iteration of the 4040 // loop. If test-expr is of form b relational-op var and relational-op is 4041 // > or >= then incr-expr must cause var to increase on each iteration of 4042 // the loop. 4043 llvm::APSInt Result; 4044 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); 4045 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 4046 bool IsConstNeg = 4047 IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); 4048 bool IsConstPos = 4049 IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); 4050 bool IsConstZero = IsConstant && !Result.getBoolValue(); 4051 if (UB && (IsConstZero || 4052 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract)) 4053 : (IsConstPos || (IsUnsigned && !Subtract))))) { 4054 SemaRef.Diag(NewStep->getExprLoc(), 4055 diag::err_omp_loop_incr_not_compatible) 4056 << LCDecl << TestIsLessOp << NewStep->getSourceRange(); 4057 SemaRef.Diag(ConditionLoc, 4058 diag::note_omp_loop_cond_requres_compatible_incr) 4059 << TestIsLessOp << ConditionSrcRange; 4060 return true; 4061 } 4062 if (TestIsLessOp == Subtract) { 4063 NewStep = 4064 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 4065 .get(); 4066 Subtract = !Subtract; 4067 } 4068 } 4069 4070 Step = NewStep; 4071 SubtractStep = Subtract; 4072 return false; 4073 } 4074 4075 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 4076 // Check init-expr for canonical loop form and save loop counter 4077 // variable - #Var and its initialization value - #LB. 4078 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 4079 // var = lb 4080 // integer-type var = lb 4081 // random-access-iterator-type var = lb 4082 // pointer-type var = lb 4083 // 4084 if (!S) { 4085 if (EmitDiags) { 4086 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 4087 } 4088 return true; 4089 } 4090 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 4091 if (!ExprTemp->cleanupsHaveSideEffects()) 4092 S = ExprTemp->getSubExpr(); 4093 4094 InitSrcRange = S->getSourceRange(); 4095 if (Expr *E = dyn_cast<Expr>(S)) 4096 S = E->IgnoreParens(); 4097 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 4098 if (BO->getOpcode() == BO_Assign) { 4099 Expr *LHS = BO->getLHS()->IgnoreParens(); 4100 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 4101 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 4102 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 4103 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 4104 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS()); 4105 } 4106 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 4107 if (ME->isArrow() && 4108 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 4109 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 4110 } 4111 } 4112 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 4113 if (DS->isSingleDecl()) { 4114 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 4115 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 4116 // Accept non-canonical init form here but emit ext. warning. 4117 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 4118 SemaRef.Diag(S->getBeginLoc(), 4119 diag::ext_omp_loop_not_canonical_init) 4120 << S->getSourceRange(); 4121 return setLCDeclAndLB( 4122 Var, 4123 buildDeclRefExpr(SemaRef, Var, 4124 Var->getType().getNonReferenceType(), 4125 DS->getBeginLoc()), 4126 Var->getInit()); 4127 } 4128 } 4129 } 4130 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 4131 if (CE->getOperator() == OO_Equal) { 4132 Expr *LHS = CE->getArg(0); 4133 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 4134 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 4135 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 4136 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 4137 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1)); 4138 } 4139 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 4140 if (ME->isArrow() && 4141 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 4142 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS()); 4143 } 4144 } 4145 } 4146 4147 if (dependent() || SemaRef.CurContext->isDependentContext()) 4148 return false; 4149 if (EmitDiags) { 4150 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init) 4151 << S->getSourceRange(); 4152 } 4153 return true; 4154 } 4155 4156 /// Ignore parenthesizes, implicit casts, copy constructor and return the 4157 /// variable (which may be the loop variable) if possible. 4158 static const ValueDecl *getInitLCDecl(const Expr *E) { 4159 if (!E) 4160 return nullptr; 4161 E = getExprAsWritten(E); 4162 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 4163 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 4164 if ((Ctor->isCopyOrMoveConstructor() || 4165 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 4166 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 4167 E = CE->getArg(0)->IgnoreParenImpCasts(); 4168 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 4169 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 4170 return getCanonicalDecl(VD); 4171 } 4172 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 4173 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 4174 return getCanonicalDecl(ME->getMemberDecl()); 4175 return nullptr; 4176 } 4177 4178 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 4179 // Check test-expr for canonical form, save upper-bound UB, flags for 4180 // less/greater and for strict/non-strict comparison. 4181 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 4182 // var relational-op b 4183 // b relational-op var 4184 // 4185 if (!S) { 4186 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl; 4187 return true; 4188 } 4189 S = getExprAsWritten(S); 4190 SourceLocation CondLoc = S->getBeginLoc(); 4191 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 4192 if (BO->isRelationalOp()) { 4193 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4194 return setUB(BO->getRHS(), 4195 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 4196 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 4197 BO->getSourceRange(), BO->getOperatorLoc()); 4198 if (getInitLCDecl(BO->getRHS()) == LCDecl) 4199 return setUB(BO->getLHS(), 4200 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 4201 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 4202 BO->getSourceRange(), BO->getOperatorLoc()); 4203 } 4204 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 4205 if (CE->getNumArgs() == 2) { 4206 auto Op = CE->getOperator(); 4207 switch (Op) { 4208 case OO_Greater: 4209 case OO_GreaterEqual: 4210 case OO_Less: 4211 case OO_LessEqual: 4212 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4213 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 4214 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 4215 CE->getOperatorLoc()); 4216 if (getInitLCDecl(CE->getArg(1)) == LCDecl) 4217 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 4218 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 4219 CE->getOperatorLoc()); 4220 break; 4221 default: 4222 break; 4223 } 4224 } 4225 } 4226 if (dependent() || SemaRef.CurContext->isDependentContext()) 4227 return false; 4228 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 4229 << S->getSourceRange() << LCDecl; 4230 return true; 4231 } 4232 4233 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 4234 // RHS of canonical loop form increment can be: 4235 // var + incr 4236 // incr + var 4237 // var - incr 4238 // 4239 RHS = RHS->IgnoreParenImpCasts(); 4240 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 4241 if (BO->isAdditiveOp()) { 4242 bool IsAdd = BO->getOpcode() == BO_Add; 4243 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4244 return setStep(BO->getRHS(), !IsAdd); 4245 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 4246 return setStep(BO->getLHS(), /*Subtract=*/false); 4247 } 4248 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 4249 bool IsAdd = CE->getOperator() == OO_Plus; 4250 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 4251 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4252 return setStep(CE->getArg(1), !IsAdd); 4253 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 4254 return setStep(CE->getArg(0), /*Subtract=*/false); 4255 } 4256 } 4257 if (dependent() || SemaRef.CurContext->isDependentContext()) 4258 return false; 4259 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 4260 << RHS->getSourceRange() << LCDecl; 4261 return true; 4262 } 4263 4264 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 4265 // Check incr-expr for canonical loop form and return true if it 4266 // does not conform. 4267 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 4268 // ++var 4269 // var++ 4270 // --var 4271 // var-- 4272 // var += incr 4273 // var -= incr 4274 // var = var + incr 4275 // var = incr + var 4276 // var = var - incr 4277 // 4278 if (!S) { 4279 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 4280 return true; 4281 } 4282 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 4283 if (!ExprTemp->cleanupsHaveSideEffects()) 4284 S = ExprTemp->getSubExpr(); 4285 4286 IncrementSrcRange = S->getSourceRange(); 4287 S = S->IgnoreParens(); 4288 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 4289 if (UO->isIncrementDecrementOp() && 4290 getInitLCDecl(UO->getSubExpr()) == LCDecl) 4291 return setStep(SemaRef 4292 .ActOnIntegerConstant(UO->getBeginLoc(), 4293 (UO->isDecrementOp() ? -1 : 1)) 4294 .get(), 4295 /*Subtract=*/false); 4296 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 4297 switch (BO->getOpcode()) { 4298 case BO_AddAssign: 4299 case BO_SubAssign: 4300 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4301 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 4302 break; 4303 case BO_Assign: 4304 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4305 return checkAndSetIncRHS(BO->getRHS()); 4306 break; 4307 default: 4308 break; 4309 } 4310 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 4311 switch (CE->getOperator()) { 4312 case OO_PlusPlus: 4313 case OO_MinusMinus: 4314 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4315 return setStep(SemaRef 4316 .ActOnIntegerConstant( 4317 CE->getBeginLoc(), 4318 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 4319 .get(), 4320 /*Subtract=*/false); 4321 break; 4322 case OO_PlusEqual: 4323 case OO_MinusEqual: 4324 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4325 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 4326 break; 4327 case OO_Equal: 4328 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4329 return checkAndSetIncRHS(CE->getArg(1)); 4330 break; 4331 default: 4332 break; 4333 } 4334 } 4335 if (dependent() || SemaRef.CurContext->isDependentContext()) 4336 return false; 4337 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 4338 << S->getSourceRange() << LCDecl; 4339 return true; 4340 } 4341 4342 static ExprResult 4343 tryBuildCapture(Sema &SemaRef, Expr *Capture, 4344 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 4345 if (SemaRef.CurContext->isDependentContext()) 4346 return ExprResult(Capture); 4347 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 4348 return SemaRef.PerformImplicitConversion( 4349 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 4350 /*AllowExplicit=*/true); 4351 auto I = Captures.find(Capture); 4352 if (I != Captures.end()) 4353 return buildCapture(SemaRef, Capture, I->second); 4354 DeclRefExpr *Ref = nullptr; 4355 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 4356 Captures[Capture] = Ref; 4357 return Res; 4358 } 4359 4360 /// Build the expression to calculate the number of iterations. 4361 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 4362 Scope *S, const bool LimitedType, 4363 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 4364 ExprResult Diff; 4365 QualType VarType = LCDecl->getType().getNonReferenceType(); 4366 if (VarType->isIntegerType() || VarType->isPointerType() || 4367 SemaRef.getLangOpts().CPlusPlus) { 4368 // Upper - Lower 4369 Expr *UBExpr = TestIsLessOp ? UB : LB; 4370 Expr *LBExpr = TestIsLessOp ? LB : UB; 4371 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 4372 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 4373 if (!Upper || !Lower) 4374 return nullptr; 4375 4376 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 4377 4378 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 4379 // BuildBinOp already emitted error, this one is to point user to upper 4380 // and lower bound, and to tell what is passed to 'operator-'. 4381 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 4382 << Upper->getSourceRange() << Lower->getSourceRange(); 4383 return nullptr; 4384 } 4385 } 4386 4387 if (!Diff.isUsable()) 4388 return nullptr; 4389 4390 // Upper - Lower [- 1] 4391 if (TestIsStrictOp) 4392 Diff = SemaRef.BuildBinOp( 4393 S, DefaultLoc, BO_Sub, Diff.get(), 4394 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 4395 if (!Diff.isUsable()) 4396 return nullptr; 4397 4398 // Upper - Lower [- 1] + Step 4399 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 4400 if (!NewStep.isUsable()) 4401 return nullptr; 4402 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 4403 if (!Diff.isUsable()) 4404 return nullptr; 4405 4406 // Parentheses (for dumping/debugging purposes only). 4407 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 4408 if (!Diff.isUsable()) 4409 return nullptr; 4410 4411 // (Upper - Lower [- 1] + Step) / Step 4412 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 4413 if (!Diff.isUsable()) 4414 return nullptr; 4415 4416 // OpenMP runtime requires 32-bit or 64-bit loop variables. 4417 QualType Type = Diff.get()->getType(); 4418 ASTContext &C = SemaRef.Context; 4419 bool UseVarType = VarType->hasIntegerRepresentation() && 4420 C.getTypeSize(Type) > C.getTypeSize(VarType); 4421 if (!Type->isIntegerType() || UseVarType) { 4422 unsigned NewSize = 4423 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 4424 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 4425 : Type->hasSignedIntegerRepresentation(); 4426 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 4427 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 4428 Diff = SemaRef.PerformImplicitConversion( 4429 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 4430 if (!Diff.isUsable()) 4431 return nullptr; 4432 } 4433 } 4434 if (LimitedType) { 4435 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 4436 if (NewSize != C.getTypeSize(Type)) { 4437 if (NewSize < C.getTypeSize(Type)) { 4438 assert(NewSize == 64 && "incorrect loop var size"); 4439 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 4440 << InitSrcRange << ConditionSrcRange; 4441 } 4442 QualType NewType = C.getIntTypeForBitwidth( 4443 NewSize, Type->hasSignedIntegerRepresentation() || 4444 C.getTypeSize(Type) < NewSize); 4445 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 4446 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 4447 Sema::AA_Converting, true); 4448 if (!Diff.isUsable()) 4449 return nullptr; 4450 } 4451 } 4452 } 4453 4454 return Diff.get(); 4455 } 4456 4457 Expr *OpenMPIterationSpaceChecker::buildPreCond( 4458 Scope *S, Expr *Cond, 4459 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 4460 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 4461 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 4462 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 4463 4464 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 4465 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 4466 if (!NewLB.isUsable() || !NewUB.isUsable()) 4467 return nullptr; 4468 4469 ExprResult CondExpr = 4470 SemaRef.BuildBinOp(S, DefaultLoc, 4471 TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE) 4472 : (TestIsStrictOp ? BO_GT : BO_GE), 4473 NewLB.get(), NewUB.get()); 4474 if (CondExpr.isUsable()) { 4475 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 4476 SemaRef.Context.BoolTy)) 4477 CondExpr = SemaRef.PerformImplicitConversion( 4478 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 4479 /*AllowExplicit=*/true); 4480 } 4481 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 4482 // Otherwise use original loop conditon and evaluate it in runtime. 4483 return CondExpr.isUsable() ? CondExpr.get() : Cond; 4484 } 4485 4486 /// Build reference expression to the counter be used for codegen. 4487 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 4488 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 4489 DSAStackTy &DSA) const { 4490 auto *VD = dyn_cast<VarDecl>(LCDecl); 4491 if (!VD) { 4492 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 4493 DeclRefExpr *Ref = buildDeclRefExpr( 4494 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 4495 const DSAStackTy::DSAVarData Data = 4496 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 4497 // If the loop control decl is explicitly marked as private, do not mark it 4498 // as captured again. 4499 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 4500 Captures.insert(std::make_pair(LCRef, Ref)); 4501 return Ref; 4502 } 4503 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(), 4504 DefaultLoc); 4505 } 4506 4507 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 4508 if (LCDecl && !LCDecl->isInvalidDecl()) { 4509 QualType Type = LCDecl->getType().getNonReferenceType(); 4510 VarDecl *PrivateVar = buildVarDecl( 4511 SemaRef, DefaultLoc, Type, LCDecl->getName(), 4512 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 4513 isa<VarDecl>(LCDecl) 4514 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 4515 : nullptr); 4516 if (PrivateVar->isInvalidDecl()) 4517 return nullptr; 4518 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 4519 } 4520 return nullptr; 4521 } 4522 4523 /// Build initialization of the counter to be used for codegen. 4524 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 4525 4526 /// Build step of the counter be used for codegen. 4527 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 4528 4529 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData( 4530 Scope *S, Expr *Counter, 4531 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc, 4532 Expr *Inc, OverloadedOperatorKind OOK) { 4533 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get(); 4534 if (!Cnt) 4535 return nullptr; 4536 if (Inc) { 4537 assert((OOK == OO_Plus || OOK == OO_Minus) && 4538 "Expected only + or - operations for depend clauses."); 4539 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub; 4540 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get(); 4541 if (!Cnt) 4542 return nullptr; 4543 } 4544 ExprResult Diff; 4545 QualType VarType = LCDecl->getType().getNonReferenceType(); 4546 if (VarType->isIntegerType() || VarType->isPointerType() || 4547 SemaRef.getLangOpts().CPlusPlus) { 4548 // Upper - Lower 4549 Expr *Upper = 4550 TestIsLessOp ? Cnt : tryBuildCapture(SemaRef, UB, Captures).get(); 4551 Expr *Lower = 4552 TestIsLessOp ? tryBuildCapture(SemaRef, LB, Captures).get() : Cnt; 4553 if (!Upper || !Lower) 4554 return nullptr; 4555 4556 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 4557 4558 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 4559 // BuildBinOp already emitted error, this one is to point user to upper 4560 // and lower bound, and to tell what is passed to 'operator-'. 4561 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 4562 << Upper->getSourceRange() << Lower->getSourceRange(); 4563 return nullptr; 4564 } 4565 } 4566 4567 if (!Diff.isUsable()) 4568 return nullptr; 4569 4570 // Parentheses (for dumping/debugging purposes only). 4571 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 4572 if (!Diff.isUsable()) 4573 return nullptr; 4574 4575 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 4576 if (!NewStep.isUsable()) 4577 return nullptr; 4578 // (Upper - Lower) / Step 4579 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 4580 if (!Diff.isUsable()) 4581 return nullptr; 4582 4583 return Diff.get(); 4584 } 4585 4586 /// Iteration space of a single for loop. 4587 struct LoopIterationSpace final { 4588 /// Condition of the loop. 4589 Expr *PreCond = nullptr; 4590 /// This expression calculates the number of iterations in the loop. 4591 /// It is always possible to calculate it before starting the loop. 4592 Expr *NumIterations = nullptr; 4593 /// The loop counter variable. 4594 Expr *CounterVar = nullptr; 4595 /// Private loop counter variable. 4596 Expr *PrivateCounterVar = nullptr; 4597 /// This is initializer for the initial value of #CounterVar. 4598 Expr *CounterInit = nullptr; 4599 /// This is step for the #CounterVar used to generate its update: 4600 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 4601 Expr *CounterStep = nullptr; 4602 /// Should step be subtracted? 4603 bool Subtract = false; 4604 /// Source range of the loop init. 4605 SourceRange InitSrcRange; 4606 /// Source range of the loop condition. 4607 SourceRange CondSrcRange; 4608 /// Source range of the loop increment. 4609 SourceRange IncSrcRange; 4610 }; 4611 4612 } // namespace 4613 4614 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 4615 assert(getLangOpts().OpenMP && "OpenMP is not active."); 4616 assert(Init && "Expected loop in canonical form."); 4617 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 4618 if (AssociatedLoops > 0 && 4619 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 4620 OpenMPIterationSpaceChecker ISC(*this, ForLoc); 4621 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 4622 if (ValueDecl *D = ISC.getLoopDecl()) { 4623 auto *VD = dyn_cast<VarDecl>(D); 4624 if (!VD) { 4625 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 4626 VD = Private; 4627 } else { 4628 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 4629 /*WithInit=*/false); 4630 VD = cast<VarDecl>(Ref->getDecl()); 4631 } 4632 } 4633 DSAStack->addLoopControlVariable(D, VD); 4634 const Decl *LD = DSAStack->getPossiblyLoopCunter(); 4635 if (LD != D->getCanonicalDecl()) { 4636 DSAStack->resetPossibleLoopCounter(); 4637 if (auto *Var = dyn_cast_or_null<VarDecl>(LD)) 4638 MarkDeclarationsReferencedInExpr( 4639 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var), 4640 Var->getType().getNonLValueExprType(Context), 4641 ForLoc, /*RefersToCapture=*/true)); 4642 } 4643 } 4644 } 4645 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 4646 } 4647 } 4648 4649 /// Called on a for stmt to check and extract its iteration space 4650 /// for further processing (such as collapsing). 4651 static bool checkOpenMPIterationSpace( 4652 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 4653 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 4654 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr, 4655 Expr *OrderedLoopCountExpr, 4656 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 4657 LoopIterationSpace &ResultIterSpace, 4658 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 4659 // OpenMP [2.6, Canonical Loop Form] 4660 // for (init-expr; test-expr; incr-expr) structured-block 4661 auto *For = dyn_cast_or_null<ForStmt>(S); 4662 if (!For) { 4663 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for) 4664 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 4665 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount 4666 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 4667 if (TotalNestedLoopCount > 1) { 4668 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 4669 SemaRef.Diag(DSA.getConstructLoc(), 4670 diag::note_omp_collapse_ordered_expr) 4671 << 2 << CollapseLoopCountExpr->getSourceRange() 4672 << OrderedLoopCountExpr->getSourceRange(); 4673 else if (CollapseLoopCountExpr) 4674 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 4675 diag::note_omp_collapse_ordered_expr) 4676 << 0 << CollapseLoopCountExpr->getSourceRange(); 4677 else 4678 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 4679 diag::note_omp_collapse_ordered_expr) 4680 << 1 << OrderedLoopCountExpr->getSourceRange(); 4681 } 4682 return true; 4683 } 4684 assert(For->getBody()); 4685 4686 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc()); 4687 4688 // Check init. 4689 Stmt *Init = For->getInit(); 4690 if (ISC.checkAndSetInit(Init)) 4691 return true; 4692 4693 bool HasErrors = false; 4694 4695 // Check loop variable's type. 4696 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 4697 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 4698 4699 // OpenMP [2.6, Canonical Loop Form] 4700 // Var is one of the following: 4701 // A variable of signed or unsigned integer type. 4702 // For C++, a variable of a random access iterator type. 4703 // For C, a variable of a pointer type. 4704 QualType VarType = LCDecl->getType().getNonReferenceType(); 4705 if (!VarType->isDependentType() && !VarType->isIntegerType() && 4706 !VarType->isPointerType() && 4707 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 4708 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type) 4709 << SemaRef.getLangOpts().CPlusPlus; 4710 HasErrors = true; 4711 } 4712 4713 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 4714 // a Construct 4715 // The loop iteration variable(s) in the associated for-loop(s) of a for or 4716 // parallel for construct is (are) private. 4717 // The loop iteration variable in the associated for-loop of a simd 4718 // construct with just one associated for-loop is linear with a 4719 // constant-linear-step that is the increment of the associated for-loop. 4720 // Exclude loop var from the list of variables with implicitly defined data 4721 // sharing attributes. 4722 VarsWithImplicitDSA.erase(LCDecl); 4723 4724 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 4725 // in a Construct, C/C++]. 4726 // The loop iteration variable in the associated for-loop of a simd 4727 // construct with just one associated for-loop may be listed in a linear 4728 // clause with a constant-linear-step that is the increment of the 4729 // associated for-loop. 4730 // The loop iteration variable(s) in the associated for-loop(s) of a for or 4731 // parallel for construct may be listed in a private or lastprivate clause. 4732 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false); 4733 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is 4734 // declared in the loop and it is predetermined as a private. 4735 OpenMPClauseKind PredeterminedCKind = 4736 isOpenMPSimdDirective(DKind) 4737 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate) 4738 : OMPC_private; 4739 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 4740 DVar.CKind != PredeterminedCKind) || 4741 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 4742 isOpenMPDistributeDirective(DKind)) && 4743 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 4744 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 4745 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 4746 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa) 4747 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind) 4748 << getOpenMPClauseName(PredeterminedCKind); 4749 if (DVar.RefExpr == nullptr) 4750 DVar.CKind = PredeterminedCKind; 4751 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true); 4752 HasErrors = true; 4753 } else if (LoopDeclRefExpr != nullptr) { 4754 // Make the loop iteration variable private (for worksharing constructs), 4755 // linear (for simd directives with the only one associated loop) or 4756 // lastprivate (for simd directives with several collapsed or ordered 4757 // loops). 4758 if (DVar.CKind == OMPC_unknown) 4759 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate, 4760 [](OpenMPDirectiveKind) -> bool { return true; }, 4761 /*FromParent=*/false); 4762 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind); 4763 } 4764 4765 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 4766 4767 // Check test-expr. 4768 HasErrors |= ISC.checkAndSetCond(For->getCond()); 4769 4770 // Check incr-expr. 4771 HasErrors |= ISC.checkAndSetInc(For->getInc()); 4772 } 4773 4774 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 4775 return HasErrors; 4776 4777 // Build the loop's iteration space representation. 4778 ResultIterSpace.PreCond = 4779 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures); 4780 ResultIterSpace.NumIterations = ISC.buildNumIterations( 4781 DSA.getCurScope(), 4782 (isOpenMPWorksharingDirective(DKind) || 4783 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)), 4784 Captures); 4785 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA); 4786 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar(); 4787 ResultIterSpace.CounterInit = ISC.buildCounterInit(); 4788 ResultIterSpace.CounterStep = ISC.buildCounterStep(); 4789 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange(); 4790 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange(); 4791 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange(); 4792 ResultIterSpace.Subtract = ISC.shouldSubtractStep(); 4793 4794 HasErrors |= (ResultIterSpace.PreCond == nullptr || 4795 ResultIterSpace.NumIterations == nullptr || 4796 ResultIterSpace.CounterVar == nullptr || 4797 ResultIterSpace.PrivateCounterVar == nullptr || 4798 ResultIterSpace.CounterInit == nullptr || 4799 ResultIterSpace.CounterStep == nullptr); 4800 if (!HasErrors && DSA.isOrderedRegion()) { 4801 if (DSA.getOrderedRegionParam().second->getNumForLoops()) { 4802 if (CurrentNestedLoopCount < 4803 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) { 4804 DSA.getOrderedRegionParam().second->setLoopNumIterations( 4805 CurrentNestedLoopCount, ResultIterSpace.NumIterations); 4806 DSA.getOrderedRegionParam().second->setLoopCounter( 4807 CurrentNestedLoopCount, ResultIterSpace.CounterVar); 4808 } 4809 } 4810 for (auto &Pair : DSA.getDoacrossDependClauses()) { 4811 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) { 4812 // Erroneous case - clause has some problems. 4813 continue; 4814 } 4815 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink && 4816 Pair.second.size() <= CurrentNestedLoopCount) { 4817 // Erroneous case - clause has some problems. 4818 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr); 4819 continue; 4820 } 4821 Expr *CntValue; 4822 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 4823 CntValue = ISC.buildOrderedLoopData( 4824 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures, 4825 Pair.first->getDependencyLoc()); 4826 else 4827 CntValue = ISC.buildOrderedLoopData( 4828 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures, 4829 Pair.first->getDependencyLoc(), 4830 Pair.second[CurrentNestedLoopCount].first, 4831 Pair.second[CurrentNestedLoopCount].second); 4832 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue); 4833 } 4834 } 4835 4836 return HasErrors; 4837 } 4838 4839 /// Build 'VarRef = Start. 4840 static ExprResult 4841 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 4842 ExprResult Start, 4843 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 4844 // Build 'VarRef = Start. 4845 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures); 4846 if (!NewStart.isUsable()) 4847 return ExprError(); 4848 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 4849 VarRef.get()->getType())) { 4850 NewStart = SemaRef.PerformImplicitConversion( 4851 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 4852 /*AllowExplicit=*/true); 4853 if (!NewStart.isUsable()) 4854 return ExprError(); 4855 } 4856 4857 ExprResult Init = 4858 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 4859 return Init; 4860 } 4861 4862 /// Build 'VarRef = Start + Iter * Step'. 4863 static ExprResult buildCounterUpdate( 4864 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 4865 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 4866 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 4867 // Add parentheses (for debugging purposes only). 4868 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 4869 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 4870 !Step.isUsable()) 4871 return ExprError(); 4872 4873 ExprResult NewStep = Step; 4874 if (Captures) 4875 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 4876 if (NewStep.isInvalid()) 4877 return ExprError(); 4878 ExprResult Update = 4879 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 4880 if (!Update.isUsable()) 4881 return ExprError(); 4882 4883 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 4884 // 'VarRef = Start (+|-) Iter * Step'. 4885 ExprResult NewStart = Start; 4886 if (Captures) 4887 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 4888 if (NewStart.isInvalid()) 4889 return ExprError(); 4890 4891 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 4892 ExprResult SavedUpdate = Update; 4893 ExprResult UpdateVal; 4894 if (VarRef.get()->getType()->isOverloadableType() || 4895 NewStart.get()->getType()->isOverloadableType() || 4896 Update.get()->getType()->isOverloadableType()) { 4897 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 4898 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 4899 Update = 4900 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 4901 if (Update.isUsable()) { 4902 UpdateVal = 4903 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 4904 VarRef.get(), SavedUpdate.get()); 4905 if (UpdateVal.isUsable()) { 4906 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 4907 UpdateVal.get()); 4908 } 4909 } 4910 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 4911 } 4912 4913 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 4914 if (!Update.isUsable() || !UpdateVal.isUsable()) { 4915 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 4916 NewStart.get(), SavedUpdate.get()); 4917 if (!Update.isUsable()) 4918 return ExprError(); 4919 4920 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 4921 VarRef.get()->getType())) { 4922 Update = SemaRef.PerformImplicitConversion( 4923 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 4924 if (!Update.isUsable()) 4925 return ExprError(); 4926 } 4927 4928 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 4929 } 4930 return Update; 4931 } 4932 4933 /// Convert integer expression \a E to make it have at least \a Bits 4934 /// bits. 4935 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 4936 if (E == nullptr) 4937 return ExprError(); 4938 ASTContext &C = SemaRef.Context; 4939 QualType OldType = E->getType(); 4940 unsigned HasBits = C.getTypeSize(OldType); 4941 if (HasBits >= Bits) 4942 return ExprResult(E); 4943 // OK to convert to signed, because new type has more bits than old. 4944 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 4945 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 4946 true); 4947 } 4948 4949 /// Check if the given expression \a E is a constant integer that fits 4950 /// into \a Bits bits. 4951 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 4952 if (E == nullptr) 4953 return false; 4954 llvm::APSInt Result; 4955 if (E->isIntegerConstantExpr(Result, SemaRef.Context)) 4956 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); 4957 return false; 4958 } 4959 4960 /// Build preinits statement for the given declarations. 4961 static Stmt *buildPreInits(ASTContext &Context, 4962 MutableArrayRef<Decl *> PreInits) { 4963 if (!PreInits.empty()) { 4964 return new (Context) DeclStmt( 4965 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 4966 SourceLocation(), SourceLocation()); 4967 } 4968 return nullptr; 4969 } 4970 4971 /// Build preinits statement for the given declarations. 4972 static Stmt * 4973 buildPreInits(ASTContext &Context, 4974 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 4975 if (!Captures.empty()) { 4976 SmallVector<Decl *, 16> PreInits; 4977 for (const auto &Pair : Captures) 4978 PreInits.push_back(Pair.second->getDecl()); 4979 return buildPreInits(Context, PreInits); 4980 } 4981 return nullptr; 4982 } 4983 4984 /// Build postupdate expression for the given list of postupdates expressions. 4985 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 4986 Expr *PostUpdate = nullptr; 4987 if (!PostUpdates.empty()) { 4988 for (Expr *E : PostUpdates) { 4989 Expr *ConvE = S.BuildCStyleCastExpr( 4990 E->getExprLoc(), 4991 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 4992 E->getExprLoc(), E) 4993 .get(); 4994 PostUpdate = PostUpdate 4995 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 4996 PostUpdate, ConvE) 4997 .get() 4998 : ConvE; 4999 } 5000 } 5001 return PostUpdate; 5002 } 5003 5004 /// Called on a for stmt to check itself and nested loops (if any). 5005 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 5006 /// number of collapsed loops otherwise. 5007 static unsigned 5008 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 5009 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 5010 DSAStackTy &DSA, 5011 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 5012 OMPLoopDirective::HelperExprs &Built) { 5013 unsigned NestedLoopCount = 1; 5014 if (CollapseLoopCountExpr) { 5015 // Found 'collapse' clause - calculate collapse number. 5016 llvm::APSInt Result; 5017 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) 5018 NestedLoopCount = Result.getLimitedValue(); 5019 } 5020 unsigned OrderedLoopCount = 1; 5021 if (OrderedLoopCountExpr) { 5022 // Found 'ordered' clause - calculate collapse number. 5023 llvm::APSInt Result; 5024 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 5025 if (Result.getLimitedValue() < NestedLoopCount) { 5026 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 5027 diag::err_omp_wrong_ordered_loop_count) 5028 << OrderedLoopCountExpr->getSourceRange(); 5029 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 5030 diag::note_collapse_loop_count) 5031 << CollapseLoopCountExpr->getSourceRange(); 5032 } 5033 OrderedLoopCount = Result.getLimitedValue(); 5034 } 5035 } 5036 // This is helper routine for loop directives (e.g., 'for', 'simd', 5037 // 'for simd', etc.). 5038 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 5039 SmallVector<LoopIterationSpace, 4> IterSpaces; 5040 IterSpaces.resize(std::max(OrderedLoopCount, NestedLoopCount)); 5041 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); 5042 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 5043 if (checkOpenMPIterationSpace( 5044 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 5045 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 5046 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt], 5047 Captures)) 5048 return 0; 5049 // Move on to the next nested for loop, or to the loop body. 5050 // OpenMP [2.8.1, simd construct, Restrictions] 5051 // All loops associated with the construct must be perfectly nested; that 5052 // is, there must be no intervening code nor any OpenMP directive between 5053 // any two loops. 5054 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); 5055 } 5056 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) { 5057 if (checkOpenMPIterationSpace( 5058 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 5059 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 5060 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt], 5061 Captures)) 5062 return 0; 5063 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) { 5064 // Handle initialization of captured loop iterator variables. 5065 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar); 5066 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) { 5067 Captures[DRE] = DRE; 5068 } 5069 } 5070 // Move on to the next nested for loop, or to the loop body. 5071 // OpenMP [2.8.1, simd construct, Restrictions] 5072 // All loops associated with the construct must be perfectly nested; that 5073 // is, there must be no intervening code nor any OpenMP directive between 5074 // any two loops. 5075 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); 5076 } 5077 5078 Built.clear(/* size */ NestedLoopCount); 5079 5080 if (SemaRef.CurContext->isDependentContext()) 5081 return NestedLoopCount; 5082 5083 // An example of what is generated for the following code: 5084 // 5085 // #pragma omp simd collapse(2) ordered(2) 5086 // for (i = 0; i < NI; ++i) 5087 // for (k = 0; k < NK; ++k) 5088 // for (j = J0; j < NJ; j+=2) { 5089 // <loop body> 5090 // } 5091 // 5092 // We generate the code below. 5093 // Note: the loop body may be outlined in CodeGen. 5094 // Note: some counters may be C++ classes, operator- is used to find number of 5095 // iterations and operator+= to calculate counter value. 5096 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 5097 // or i64 is currently supported). 5098 // 5099 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 5100 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 5101 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 5102 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 5103 // // similar updates for vars in clauses (e.g. 'linear') 5104 // <loop body (using local i and j)> 5105 // } 5106 // i = NI; // assign final values of counters 5107 // j = NJ; 5108 // 5109 5110 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 5111 // the iteration counts of the collapsed for loops. 5112 // Precondition tests if there is at least one iteration (all conditions are 5113 // true). 5114 auto PreCond = ExprResult(IterSpaces[0].PreCond); 5115 Expr *N0 = IterSpaces[0].NumIterations; 5116 ExprResult LastIteration32 = 5117 widenIterationCount(/*Bits=*/32, 5118 SemaRef 5119 .PerformImplicitConversion( 5120 N0->IgnoreImpCasts(), N0->getType(), 5121 Sema::AA_Converting, /*AllowExplicit=*/true) 5122 .get(), 5123 SemaRef); 5124 ExprResult LastIteration64 = widenIterationCount( 5125 /*Bits=*/64, 5126 SemaRef 5127 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 5128 Sema::AA_Converting, 5129 /*AllowExplicit=*/true) 5130 .get(), 5131 SemaRef); 5132 5133 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 5134 return NestedLoopCount; 5135 5136 ASTContext &C = SemaRef.Context; 5137 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 5138 5139 Scope *CurScope = DSA.getCurScope(); 5140 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 5141 if (PreCond.isUsable()) { 5142 PreCond = 5143 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 5144 PreCond.get(), IterSpaces[Cnt].PreCond); 5145 } 5146 Expr *N = IterSpaces[Cnt].NumIterations; 5147 SourceLocation Loc = N->getExprLoc(); 5148 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 5149 if (LastIteration32.isUsable()) 5150 LastIteration32 = SemaRef.BuildBinOp( 5151 CurScope, Loc, BO_Mul, LastIteration32.get(), 5152 SemaRef 5153 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 5154 Sema::AA_Converting, 5155 /*AllowExplicit=*/true) 5156 .get()); 5157 if (LastIteration64.isUsable()) 5158 LastIteration64 = SemaRef.BuildBinOp( 5159 CurScope, Loc, BO_Mul, LastIteration64.get(), 5160 SemaRef 5161 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 5162 Sema::AA_Converting, 5163 /*AllowExplicit=*/true) 5164 .get()); 5165 } 5166 5167 // Choose either the 32-bit or 64-bit version. 5168 ExprResult LastIteration = LastIteration64; 5169 if (LastIteration32.isUsable() && 5170 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 5171 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 5172 fitsInto( 5173 /*Bits=*/32, 5174 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 5175 LastIteration64.get(), SemaRef))) 5176 LastIteration = LastIteration32; 5177 QualType VType = LastIteration.get()->getType(); 5178 QualType RealVType = VType; 5179 QualType StrideVType = VType; 5180 if (isOpenMPTaskLoopDirective(DKind)) { 5181 VType = 5182 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 5183 StrideVType = 5184 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 5185 } 5186 5187 if (!LastIteration.isUsable()) 5188 return 0; 5189 5190 // Save the number of iterations. 5191 ExprResult NumIterations = LastIteration; 5192 { 5193 LastIteration = SemaRef.BuildBinOp( 5194 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 5195 LastIteration.get(), 5196 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 5197 if (!LastIteration.isUsable()) 5198 return 0; 5199 } 5200 5201 // Calculate the last iteration number beforehand instead of doing this on 5202 // each iteration. Do not do this if the number of iterations may be kfold-ed. 5203 llvm::APSInt Result; 5204 bool IsConstant = 5205 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); 5206 ExprResult CalcLastIteration; 5207 if (!IsConstant) { 5208 ExprResult SaveRef = 5209 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 5210 LastIteration = SaveRef; 5211 5212 // Prepare SaveRef + 1. 5213 NumIterations = SemaRef.BuildBinOp( 5214 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 5215 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 5216 if (!NumIterations.isUsable()) 5217 return 0; 5218 } 5219 5220 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 5221 5222 // Build variables passed into runtime, necessary for worksharing directives. 5223 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 5224 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 5225 isOpenMPDistributeDirective(DKind)) { 5226 // Lower bound variable, initialized with zero. 5227 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 5228 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 5229 SemaRef.AddInitializerToDecl(LBDecl, 5230 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 5231 /*DirectInit*/ false); 5232 5233 // Upper bound variable, initialized with last iteration number. 5234 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 5235 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 5236 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 5237 /*DirectInit*/ false); 5238 5239 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 5240 // This will be used to implement clause 'lastprivate'. 5241 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 5242 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 5243 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 5244 SemaRef.AddInitializerToDecl(ILDecl, 5245 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 5246 /*DirectInit*/ false); 5247 5248 // Stride variable returned by runtime (we initialize it to 1 by default). 5249 VarDecl *STDecl = 5250 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 5251 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 5252 SemaRef.AddInitializerToDecl(STDecl, 5253 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 5254 /*DirectInit*/ false); 5255 5256 // Build expression: UB = min(UB, LastIteration) 5257 // It is necessary for CodeGen of directives with static scheduling. 5258 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 5259 UB.get(), LastIteration.get()); 5260 ExprResult CondOp = SemaRef.ActOnConditionalOp( 5261 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 5262 LastIteration.get(), UB.get()); 5263 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 5264 CondOp.get()); 5265 EUB = SemaRef.ActOnFinishFullExpr(EUB.get()); 5266 5267 // If we have a combined directive that combines 'distribute', 'for' or 5268 // 'simd' we need to be able to access the bounds of the schedule of the 5269 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 5270 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 5271 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5272 // Lower bound variable, initialized with zero. 5273 VarDecl *CombLBDecl = 5274 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 5275 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 5276 SemaRef.AddInitializerToDecl( 5277 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 5278 /*DirectInit*/ false); 5279 5280 // Upper bound variable, initialized with last iteration number. 5281 VarDecl *CombUBDecl = 5282 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 5283 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 5284 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 5285 /*DirectInit*/ false); 5286 5287 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 5288 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 5289 ExprResult CombCondOp = 5290 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 5291 LastIteration.get(), CombUB.get()); 5292 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 5293 CombCondOp.get()); 5294 CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get()); 5295 5296 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 5297 // We expect to have at least 2 more parameters than the 'parallel' 5298 // directive does - the lower and upper bounds of the previous schedule. 5299 assert(CD->getNumParams() >= 4 && 5300 "Unexpected number of parameters in loop combined directive"); 5301 5302 // Set the proper type for the bounds given what we learned from the 5303 // enclosed loops. 5304 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 5305 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 5306 5307 // Previous lower and upper bounds are obtained from the region 5308 // parameters. 5309 PrevLB = 5310 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 5311 PrevUB = 5312 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 5313 } 5314 } 5315 5316 // Build the iteration variable and its initialization before loop. 5317 ExprResult IV; 5318 ExprResult Init, CombInit; 5319 { 5320 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 5321 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 5322 Expr *RHS = 5323 (isOpenMPWorksharingDirective(DKind) || 5324 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 5325 ? LB.get() 5326 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 5327 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 5328 Init = SemaRef.ActOnFinishFullExpr(Init.get()); 5329 5330 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5331 Expr *CombRHS = 5332 (isOpenMPWorksharingDirective(DKind) || 5333 isOpenMPTaskLoopDirective(DKind) || 5334 isOpenMPDistributeDirective(DKind)) 5335 ? CombLB.get() 5336 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 5337 CombInit = 5338 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 5339 CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get()); 5340 } 5341 } 5342 5343 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops. 5344 SourceLocation CondLoc = AStmt->getBeginLoc(); 5345 ExprResult Cond = 5346 (isOpenMPWorksharingDirective(DKind) || 5347 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 5348 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get()) 5349 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 5350 NumIterations.get()); 5351 ExprResult CombDistCond; 5352 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5353 CombDistCond = 5354 SemaRef.BuildBinOp( 5355 CurScope, CondLoc, BO_LT, IV.get(), NumIterations.get()); 5356 } 5357 5358 ExprResult CombCond; 5359 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5360 CombCond = 5361 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get()); 5362 } 5363 // Loop increment (IV = IV + 1) 5364 SourceLocation IncLoc = AStmt->getBeginLoc(); 5365 ExprResult Inc = 5366 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 5367 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 5368 if (!Inc.isUsable()) 5369 return 0; 5370 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 5371 Inc = SemaRef.ActOnFinishFullExpr(Inc.get()); 5372 if (!Inc.isUsable()) 5373 return 0; 5374 5375 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 5376 // Used for directives with static scheduling. 5377 // In combined construct, add combined version that use CombLB and CombUB 5378 // base variables for the update 5379 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 5380 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 5381 isOpenMPDistributeDirective(DKind)) { 5382 // LB + ST 5383 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 5384 if (!NextLB.isUsable()) 5385 return 0; 5386 // LB = LB + ST 5387 NextLB = 5388 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 5389 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get()); 5390 if (!NextLB.isUsable()) 5391 return 0; 5392 // UB + ST 5393 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 5394 if (!NextUB.isUsable()) 5395 return 0; 5396 // UB = UB + ST 5397 NextUB = 5398 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 5399 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get()); 5400 if (!NextUB.isUsable()) 5401 return 0; 5402 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5403 CombNextLB = 5404 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 5405 if (!NextLB.isUsable()) 5406 return 0; 5407 // LB = LB + ST 5408 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 5409 CombNextLB.get()); 5410 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get()); 5411 if (!CombNextLB.isUsable()) 5412 return 0; 5413 // UB + ST 5414 CombNextUB = 5415 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 5416 if (!CombNextUB.isUsable()) 5417 return 0; 5418 // UB = UB + ST 5419 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 5420 CombNextUB.get()); 5421 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get()); 5422 if (!CombNextUB.isUsable()) 5423 return 0; 5424 } 5425 } 5426 5427 // Create increment expression for distribute loop when combined in a same 5428 // directive with for as IV = IV + ST; ensure upper bound expression based 5429 // on PrevUB instead of NumIterations - used to implement 'for' when found 5430 // in combination with 'distribute', like in 'distribute parallel for' 5431 SourceLocation DistIncLoc = AStmt->getBeginLoc(); 5432 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond; 5433 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5434 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get()); 5435 assert(DistCond.isUsable() && "distribute cond expr was not built"); 5436 5437 DistInc = 5438 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 5439 assert(DistInc.isUsable() && "distribute inc expr was not built"); 5440 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 5441 DistInc.get()); 5442 DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get()); 5443 assert(DistInc.isUsable() && "distribute inc expr was not built"); 5444 5445 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 5446 // construct 5447 SourceLocation DistEUBLoc = AStmt->getBeginLoc(); 5448 ExprResult IsUBGreater = 5449 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get()); 5450 ExprResult CondOp = SemaRef.ActOnConditionalOp( 5451 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get()); 5452 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 5453 CondOp.get()); 5454 PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get()); 5455 5456 // Build IV <= PrevUB to be used in parallel for is in combination with 5457 // a distribute directive with schedule(static, 1) 5458 ParForInDistCond = 5459 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), PrevUB.get()); 5460 } 5461 5462 // Build updates and final values of the loop counters. 5463 bool HasErrors = false; 5464 Built.Counters.resize(NestedLoopCount); 5465 Built.Inits.resize(NestedLoopCount); 5466 Built.Updates.resize(NestedLoopCount); 5467 Built.Finals.resize(NestedLoopCount); 5468 { 5469 ExprResult Div; 5470 // Go from inner nested loop to outer. 5471 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) { 5472 LoopIterationSpace &IS = IterSpaces[Cnt]; 5473 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 5474 // Build: Iter = (IV / Div) % IS.NumIters 5475 // where Div is product of previous iterations' IS.NumIters. 5476 ExprResult Iter; 5477 if (Div.isUsable()) { 5478 Iter = 5479 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get()); 5480 } else { 5481 Iter = IV; 5482 assert((Cnt == (int)NestedLoopCount - 1) && 5483 "unusable div expected on first iteration only"); 5484 } 5485 5486 if (Cnt != 0 && Iter.isUsable()) 5487 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(), 5488 IS.NumIterations); 5489 if (!Iter.isUsable()) { 5490 HasErrors = true; 5491 break; 5492 } 5493 5494 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 5495 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 5496 DeclRefExpr *CounterVar = buildDeclRefExpr( 5497 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 5498 /*RefersToCapture=*/true); 5499 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 5500 IS.CounterInit, Captures); 5501 if (!Init.isUsable()) { 5502 HasErrors = true; 5503 break; 5504 } 5505 ExprResult Update = buildCounterUpdate( 5506 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 5507 IS.CounterStep, IS.Subtract, &Captures); 5508 if (!Update.isUsable()) { 5509 HasErrors = true; 5510 break; 5511 } 5512 5513 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 5514 ExprResult Final = buildCounterUpdate( 5515 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, 5516 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures); 5517 if (!Final.isUsable()) { 5518 HasErrors = true; 5519 break; 5520 } 5521 5522 // Build Div for the next iteration: Div <- Div * IS.NumIters 5523 if (Cnt != 0) { 5524 if (Div.isUnset()) 5525 Div = IS.NumIterations; 5526 else 5527 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(), 5528 IS.NumIterations); 5529 5530 // Add parentheses (for debugging purposes only). 5531 if (Div.isUsable()) 5532 Div = tryBuildCapture(SemaRef, Div.get(), Captures); 5533 if (!Div.isUsable()) { 5534 HasErrors = true; 5535 break; 5536 } 5537 } 5538 if (!Update.isUsable() || !Final.isUsable()) { 5539 HasErrors = true; 5540 break; 5541 } 5542 // Save results 5543 Built.Counters[Cnt] = IS.CounterVar; 5544 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 5545 Built.Inits[Cnt] = Init.get(); 5546 Built.Updates[Cnt] = Update.get(); 5547 Built.Finals[Cnt] = Final.get(); 5548 } 5549 } 5550 5551 if (HasErrors) 5552 return 0; 5553 5554 // Save results 5555 Built.IterationVarRef = IV.get(); 5556 Built.LastIteration = LastIteration.get(); 5557 Built.NumIterations = NumIterations.get(); 5558 Built.CalcLastIteration = 5559 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get(); 5560 Built.PreCond = PreCond.get(); 5561 Built.PreInits = buildPreInits(C, Captures); 5562 Built.Cond = Cond.get(); 5563 Built.Init = Init.get(); 5564 Built.Inc = Inc.get(); 5565 Built.LB = LB.get(); 5566 Built.UB = UB.get(); 5567 Built.IL = IL.get(); 5568 Built.ST = ST.get(); 5569 Built.EUB = EUB.get(); 5570 Built.NLB = NextLB.get(); 5571 Built.NUB = NextUB.get(); 5572 Built.PrevLB = PrevLB.get(); 5573 Built.PrevUB = PrevUB.get(); 5574 Built.DistInc = DistInc.get(); 5575 Built.PrevEUB = PrevEUB.get(); 5576 Built.DistCombinedFields.LB = CombLB.get(); 5577 Built.DistCombinedFields.UB = CombUB.get(); 5578 Built.DistCombinedFields.EUB = CombEUB.get(); 5579 Built.DistCombinedFields.Init = CombInit.get(); 5580 Built.DistCombinedFields.Cond = CombCond.get(); 5581 Built.DistCombinedFields.NLB = CombNextLB.get(); 5582 Built.DistCombinedFields.NUB = CombNextUB.get(); 5583 Built.DistCombinedFields.DistCond = CombDistCond.get(); 5584 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get(); 5585 5586 return NestedLoopCount; 5587 } 5588 5589 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 5590 auto CollapseClauses = 5591 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 5592 if (CollapseClauses.begin() != CollapseClauses.end()) 5593 return (*CollapseClauses.begin())->getNumForLoops(); 5594 return nullptr; 5595 } 5596 5597 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 5598 auto OrderedClauses = 5599 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 5600 if (OrderedClauses.begin() != OrderedClauses.end()) 5601 return (*OrderedClauses.begin())->getNumForLoops(); 5602 return nullptr; 5603 } 5604 5605 static bool checkSimdlenSafelenSpecified(Sema &S, 5606 const ArrayRef<OMPClause *> Clauses) { 5607 const OMPSafelenClause *Safelen = nullptr; 5608 const OMPSimdlenClause *Simdlen = nullptr; 5609 5610 for (const OMPClause *Clause : Clauses) { 5611 if (Clause->getClauseKind() == OMPC_safelen) 5612 Safelen = cast<OMPSafelenClause>(Clause); 5613 else if (Clause->getClauseKind() == OMPC_simdlen) 5614 Simdlen = cast<OMPSimdlenClause>(Clause); 5615 if (Safelen && Simdlen) 5616 break; 5617 } 5618 5619 if (Simdlen && Safelen) { 5620 llvm::APSInt SimdlenRes, SafelenRes; 5621 const Expr *SimdlenLength = Simdlen->getSimdlen(); 5622 const Expr *SafelenLength = Safelen->getSafelen(); 5623 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 5624 SimdlenLength->isInstantiationDependent() || 5625 SimdlenLength->containsUnexpandedParameterPack()) 5626 return false; 5627 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 5628 SafelenLength->isInstantiationDependent() || 5629 SafelenLength->containsUnexpandedParameterPack()) 5630 return false; 5631 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context); 5632 SafelenLength->EvaluateAsInt(SafelenRes, S.Context); 5633 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 5634 // If both simdlen and safelen clauses are specified, the value of the 5635 // simdlen parameter must be less than or equal to the value of the safelen 5636 // parameter. 5637 if (SimdlenRes > SafelenRes) { 5638 S.Diag(SimdlenLength->getExprLoc(), 5639 diag::err_omp_wrong_simdlen_safelen_values) 5640 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 5641 return true; 5642 } 5643 } 5644 return false; 5645 } 5646 5647 StmtResult 5648 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 5649 SourceLocation StartLoc, SourceLocation EndLoc, 5650 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5651 if (!AStmt) 5652 return StmtError(); 5653 5654 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5655 OMPLoopDirective::HelperExprs B; 5656 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5657 // define the nested loops number. 5658 unsigned NestedLoopCount = checkOpenMPLoop( 5659 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 5660 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 5661 if (NestedLoopCount == 0) 5662 return StmtError(); 5663 5664 assert((CurContext->isDependentContext() || B.builtAll()) && 5665 "omp simd loop exprs were not built"); 5666 5667 if (!CurContext->isDependentContext()) { 5668 // Finalize the clauses that need pre-built expressions for CodeGen. 5669 for (OMPClause *C : Clauses) { 5670 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5671 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5672 B.NumIterations, *this, CurScope, 5673 DSAStack)) 5674 return StmtError(); 5675 } 5676 } 5677 5678 if (checkSimdlenSafelenSpecified(*this, Clauses)) 5679 return StmtError(); 5680 5681 setFunctionHasBranchProtectedScope(); 5682 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 5683 Clauses, AStmt, B); 5684 } 5685 5686 StmtResult 5687 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 5688 SourceLocation StartLoc, SourceLocation EndLoc, 5689 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5690 if (!AStmt) 5691 return StmtError(); 5692 5693 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5694 OMPLoopDirective::HelperExprs B; 5695 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5696 // define the nested loops number. 5697 unsigned NestedLoopCount = checkOpenMPLoop( 5698 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 5699 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 5700 if (NestedLoopCount == 0) 5701 return StmtError(); 5702 5703 assert((CurContext->isDependentContext() || B.builtAll()) && 5704 "omp for loop exprs were not built"); 5705 5706 if (!CurContext->isDependentContext()) { 5707 // Finalize the clauses that need pre-built expressions for CodeGen. 5708 for (OMPClause *C : Clauses) { 5709 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5710 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5711 B.NumIterations, *this, CurScope, 5712 DSAStack)) 5713 return StmtError(); 5714 } 5715 } 5716 5717 setFunctionHasBranchProtectedScope(); 5718 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 5719 Clauses, AStmt, B, DSAStack->isCancelRegion()); 5720 } 5721 5722 StmtResult Sema::ActOnOpenMPForSimdDirective( 5723 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5724 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5725 if (!AStmt) 5726 return StmtError(); 5727 5728 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5729 OMPLoopDirective::HelperExprs B; 5730 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5731 // define the nested loops number. 5732 unsigned NestedLoopCount = 5733 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 5734 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 5735 VarsWithImplicitDSA, B); 5736 if (NestedLoopCount == 0) 5737 return StmtError(); 5738 5739 assert((CurContext->isDependentContext() || B.builtAll()) && 5740 "omp for simd loop exprs were not built"); 5741 5742 if (!CurContext->isDependentContext()) { 5743 // Finalize the clauses that need pre-built expressions for CodeGen. 5744 for (OMPClause *C : Clauses) { 5745 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5746 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5747 B.NumIterations, *this, CurScope, 5748 DSAStack)) 5749 return StmtError(); 5750 } 5751 } 5752 5753 if (checkSimdlenSafelenSpecified(*this, Clauses)) 5754 return StmtError(); 5755 5756 setFunctionHasBranchProtectedScope(); 5757 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 5758 Clauses, AStmt, B); 5759 } 5760 5761 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 5762 Stmt *AStmt, 5763 SourceLocation StartLoc, 5764 SourceLocation EndLoc) { 5765 if (!AStmt) 5766 return StmtError(); 5767 5768 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5769 auto BaseStmt = AStmt; 5770 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 5771 BaseStmt = CS->getCapturedStmt(); 5772 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 5773 auto S = C->children(); 5774 if (S.begin() == S.end()) 5775 return StmtError(); 5776 // All associated statements must be '#pragma omp section' except for 5777 // the first one. 5778 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 5779 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 5780 if (SectionStmt) 5781 Diag(SectionStmt->getBeginLoc(), 5782 diag::err_omp_sections_substmt_not_section); 5783 return StmtError(); 5784 } 5785 cast<OMPSectionDirective>(SectionStmt) 5786 ->setHasCancel(DSAStack->isCancelRegion()); 5787 } 5788 } else { 5789 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt); 5790 return StmtError(); 5791 } 5792 5793 setFunctionHasBranchProtectedScope(); 5794 5795 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 5796 DSAStack->isCancelRegion()); 5797 } 5798 5799 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 5800 SourceLocation StartLoc, 5801 SourceLocation EndLoc) { 5802 if (!AStmt) 5803 return StmtError(); 5804 5805 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5806 5807 setFunctionHasBranchProtectedScope(); 5808 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 5809 5810 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 5811 DSAStack->isCancelRegion()); 5812 } 5813 5814 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 5815 Stmt *AStmt, 5816 SourceLocation StartLoc, 5817 SourceLocation EndLoc) { 5818 if (!AStmt) 5819 return StmtError(); 5820 5821 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5822 5823 setFunctionHasBranchProtectedScope(); 5824 5825 // OpenMP [2.7.3, single Construct, Restrictions] 5826 // The copyprivate clause must not be used with the nowait clause. 5827 const OMPClause *Nowait = nullptr; 5828 const OMPClause *Copyprivate = nullptr; 5829 for (const OMPClause *Clause : Clauses) { 5830 if (Clause->getClauseKind() == OMPC_nowait) 5831 Nowait = Clause; 5832 else if (Clause->getClauseKind() == OMPC_copyprivate) 5833 Copyprivate = Clause; 5834 if (Copyprivate && Nowait) { 5835 Diag(Copyprivate->getBeginLoc(), 5836 diag::err_omp_single_copyprivate_with_nowait); 5837 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here); 5838 return StmtError(); 5839 } 5840 } 5841 5842 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 5843 } 5844 5845 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 5846 SourceLocation StartLoc, 5847 SourceLocation EndLoc) { 5848 if (!AStmt) 5849 return StmtError(); 5850 5851 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5852 5853 setFunctionHasBranchProtectedScope(); 5854 5855 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 5856 } 5857 5858 StmtResult Sema::ActOnOpenMPCriticalDirective( 5859 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 5860 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 5861 if (!AStmt) 5862 return StmtError(); 5863 5864 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 5865 5866 bool ErrorFound = false; 5867 llvm::APSInt Hint; 5868 SourceLocation HintLoc; 5869 bool DependentHint = false; 5870 for (const OMPClause *C : Clauses) { 5871 if (C->getClauseKind() == OMPC_hint) { 5872 if (!DirName.getName()) { 5873 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name); 5874 ErrorFound = true; 5875 } 5876 Expr *E = cast<OMPHintClause>(C)->getHint(); 5877 if (E->isTypeDependent() || E->isValueDependent() || 5878 E->isInstantiationDependent()) { 5879 DependentHint = true; 5880 } else { 5881 Hint = E->EvaluateKnownConstInt(Context); 5882 HintLoc = C->getBeginLoc(); 5883 } 5884 } 5885 } 5886 if (ErrorFound) 5887 return StmtError(); 5888 const auto Pair = DSAStack->getCriticalWithHint(DirName); 5889 if (Pair.first && DirName.getName() && !DependentHint) { 5890 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 5891 Diag(StartLoc, diag::err_omp_critical_with_hint); 5892 if (HintLoc.isValid()) 5893 Diag(HintLoc, diag::note_omp_critical_hint_here) 5894 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); 5895 else 5896 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 5897 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 5898 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here) 5899 << 1 5900 << C->getHint()->EvaluateKnownConstInt(Context).toString( 5901 /*Radix=*/10, /*Signed=*/false); 5902 } else { 5903 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1; 5904 } 5905 } 5906 } 5907 5908 setFunctionHasBranchProtectedScope(); 5909 5910 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 5911 Clauses, AStmt); 5912 if (!Pair.first && DirName.getName() && !DependentHint) 5913 DSAStack->addCriticalWithHint(Dir, Hint); 5914 return Dir; 5915 } 5916 5917 StmtResult Sema::ActOnOpenMPParallelForDirective( 5918 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5919 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5920 if (!AStmt) 5921 return StmtError(); 5922 5923 auto *CS = cast<CapturedStmt>(AStmt); 5924 // 1.2.2 OpenMP Language Terminology 5925 // Structured block - An executable statement with a single entry at the 5926 // top and a single exit at the bottom. 5927 // The point of exit cannot be a branch out of the structured block. 5928 // longjmp() and throw() must not violate the entry/exit criteria. 5929 CS->getCapturedDecl()->setNothrow(); 5930 5931 OMPLoopDirective::HelperExprs B; 5932 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5933 // define the nested loops number. 5934 unsigned NestedLoopCount = 5935 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 5936 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 5937 VarsWithImplicitDSA, B); 5938 if (NestedLoopCount == 0) 5939 return StmtError(); 5940 5941 assert((CurContext->isDependentContext() || B.builtAll()) && 5942 "omp parallel for loop exprs were not built"); 5943 5944 if (!CurContext->isDependentContext()) { 5945 // Finalize the clauses that need pre-built expressions for CodeGen. 5946 for (OMPClause *C : Clauses) { 5947 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5948 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5949 B.NumIterations, *this, CurScope, 5950 DSAStack)) 5951 return StmtError(); 5952 } 5953 } 5954 5955 setFunctionHasBranchProtectedScope(); 5956 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc, 5957 NestedLoopCount, Clauses, AStmt, B, 5958 DSAStack->isCancelRegion()); 5959 } 5960 5961 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 5962 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 5963 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 5964 if (!AStmt) 5965 return StmtError(); 5966 5967 auto *CS = cast<CapturedStmt>(AStmt); 5968 // 1.2.2 OpenMP Language Terminology 5969 // Structured block - An executable statement with a single entry at the 5970 // top and a single exit at the bottom. 5971 // The point of exit cannot be a branch out of the structured block. 5972 // longjmp() and throw() must not violate the entry/exit criteria. 5973 CS->getCapturedDecl()->setNothrow(); 5974 5975 OMPLoopDirective::HelperExprs B; 5976 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 5977 // define the nested loops number. 5978 unsigned NestedLoopCount = 5979 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 5980 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 5981 VarsWithImplicitDSA, B); 5982 if (NestedLoopCount == 0) 5983 return StmtError(); 5984 5985 if (!CurContext->isDependentContext()) { 5986 // Finalize the clauses that need pre-built expressions for CodeGen. 5987 for (OMPClause *C : Clauses) { 5988 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 5989 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 5990 B.NumIterations, *this, CurScope, 5991 DSAStack)) 5992 return StmtError(); 5993 } 5994 } 5995 5996 if (checkSimdlenSafelenSpecified(*this, Clauses)) 5997 return StmtError(); 5998 5999 setFunctionHasBranchProtectedScope(); 6000 return OMPParallelForSimdDirective::Create( 6001 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6002 } 6003 6004 StmtResult 6005 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 6006 Stmt *AStmt, SourceLocation StartLoc, 6007 SourceLocation EndLoc) { 6008 if (!AStmt) 6009 return StmtError(); 6010 6011 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6012 auto BaseStmt = AStmt; 6013 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 6014 BaseStmt = CS->getCapturedStmt(); 6015 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 6016 auto S = C->children(); 6017 if (S.begin() == S.end()) 6018 return StmtError(); 6019 // All associated statements must be '#pragma omp section' except for 6020 // the first one. 6021 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 6022 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 6023 if (SectionStmt) 6024 Diag(SectionStmt->getBeginLoc(), 6025 diag::err_omp_parallel_sections_substmt_not_section); 6026 return StmtError(); 6027 } 6028 cast<OMPSectionDirective>(SectionStmt) 6029 ->setHasCancel(DSAStack->isCancelRegion()); 6030 } 6031 } else { 6032 Diag(AStmt->getBeginLoc(), 6033 diag::err_omp_parallel_sections_not_compound_stmt); 6034 return StmtError(); 6035 } 6036 6037 setFunctionHasBranchProtectedScope(); 6038 6039 return OMPParallelSectionsDirective::Create( 6040 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion()); 6041 } 6042 6043 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 6044 Stmt *AStmt, SourceLocation StartLoc, 6045 SourceLocation EndLoc) { 6046 if (!AStmt) 6047 return StmtError(); 6048 6049 auto *CS = cast<CapturedStmt>(AStmt); 6050 // 1.2.2 OpenMP Language Terminology 6051 // Structured block - An executable statement with a single entry at the 6052 // top and a single exit at the bottom. 6053 // The point of exit cannot be a branch out of the structured block. 6054 // longjmp() and throw() must not violate the entry/exit criteria. 6055 CS->getCapturedDecl()->setNothrow(); 6056 6057 setFunctionHasBranchProtectedScope(); 6058 6059 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 6060 DSAStack->isCancelRegion()); 6061 } 6062 6063 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 6064 SourceLocation EndLoc) { 6065 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 6066 } 6067 6068 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 6069 SourceLocation EndLoc) { 6070 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 6071 } 6072 6073 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 6074 SourceLocation EndLoc) { 6075 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 6076 } 6077 6078 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 6079 Stmt *AStmt, 6080 SourceLocation StartLoc, 6081 SourceLocation EndLoc) { 6082 if (!AStmt) 6083 return StmtError(); 6084 6085 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6086 6087 setFunctionHasBranchProtectedScope(); 6088 6089 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 6090 AStmt, 6091 DSAStack->getTaskgroupReductionRef()); 6092 } 6093 6094 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 6095 SourceLocation StartLoc, 6096 SourceLocation EndLoc) { 6097 assert(Clauses.size() <= 1 && "Extra clauses in flush directive"); 6098 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 6099 } 6100 6101 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 6102 Stmt *AStmt, 6103 SourceLocation StartLoc, 6104 SourceLocation EndLoc) { 6105 const OMPClause *DependFound = nullptr; 6106 const OMPClause *DependSourceClause = nullptr; 6107 const OMPClause *DependSinkClause = nullptr; 6108 bool ErrorFound = false; 6109 const OMPThreadsClause *TC = nullptr; 6110 const OMPSIMDClause *SC = nullptr; 6111 for (const OMPClause *C : Clauses) { 6112 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 6113 DependFound = C; 6114 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 6115 if (DependSourceClause) { 6116 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 6117 << getOpenMPDirectiveName(OMPD_ordered) 6118 << getOpenMPClauseName(OMPC_depend) << 2; 6119 ErrorFound = true; 6120 } else { 6121 DependSourceClause = C; 6122 } 6123 if (DependSinkClause) { 6124 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 6125 << 0; 6126 ErrorFound = true; 6127 } 6128 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 6129 if (DependSourceClause) { 6130 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 6131 << 1; 6132 ErrorFound = true; 6133 } 6134 DependSinkClause = C; 6135 } 6136 } else if (C->getClauseKind() == OMPC_threads) { 6137 TC = cast<OMPThreadsClause>(C); 6138 } else if (C->getClauseKind() == OMPC_simd) { 6139 SC = cast<OMPSIMDClause>(C); 6140 } 6141 } 6142 if (!ErrorFound && !SC && 6143 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 6144 // OpenMP [2.8.1,simd Construct, Restrictions] 6145 // An ordered construct with the simd clause is the only OpenMP construct 6146 // that can appear in the simd region. 6147 Diag(StartLoc, diag::err_omp_prohibited_region_simd); 6148 ErrorFound = true; 6149 } else if (DependFound && (TC || SC)) { 6150 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd) 6151 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 6152 ErrorFound = true; 6153 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) { 6154 Diag(DependFound->getBeginLoc(), 6155 diag::err_omp_ordered_directive_without_param); 6156 ErrorFound = true; 6157 } else if (TC || Clauses.empty()) { 6158 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) { 6159 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc; 6160 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 6161 << (TC != nullptr); 6162 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param); 6163 ErrorFound = true; 6164 } 6165 } 6166 if ((!AStmt && !DependFound) || ErrorFound) 6167 return StmtError(); 6168 6169 if (AStmt) { 6170 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6171 6172 setFunctionHasBranchProtectedScope(); 6173 } 6174 6175 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 6176 } 6177 6178 namespace { 6179 /// Helper class for checking expression in 'omp atomic [update]' 6180 /// construct. 6181 class OpenMPAtomicUpdateChecker { 6182 /// Error results for atomic update expressions. 6183 enum ExprAnalysisErrorCode { 6184 /// A statement is not an expression statement. 6185 NotAnExpression, 6186 /// Expression is not builtin binary or unary operation. 6187 NotABinaryOrUnaryExpression, 6188 /// Unary operation is not post-/pre- increment/decrement operation. 6189 NotAnUnaryIncDecExpression, 6190 /// An expression is not of scalar type. 6191 NotAScalarType, 6192 /// A binary operation is not an assignment operation. 6193 NotAnAssignmentOp, 6194 /// RHS part of the binary operation is not a binary expression. 6195 NotABinaryExpression, 6196 /// RHS part is not additive/multiplicative/shift/biwise binary 6197 /// expression. 6198 NotABinaryOperator, 6199 /// RHS binary operation does not have reference to the updated LHS 6200 /// part. 6201 NotAnUpdateExpression, 6202 /// No errors is found. 6203 NoError 6204 }; 6205 /// Reference to Sema. 6206 Sema &SemaRef; 6207 /// A location for note diagnostics (when error is found). 6208 SourceLocation NoteLoc; 6209 /// 'x' lvalue part of the source atomic expression. 6210 Expr *X; 6211 /// 'expr' rvalue part of the source atomic expression. 6212 Expr *E; 6213 /// Helper expression of the form 6214 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 6215 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 6216 Expr *UpdateExpr; 6217 /// Is 'x' a LHS in a RHS part of full update expression. It is 6218 /// important for non-associative operations. 6219 bool IsXLHSInRHSPart; 6220 BinaryOperatorKind Op; 6221 SourceLocation OpLoc; 6222 /// true if the source expression is a postfix unary operation, false 6223 /// if it is a prefix unary operation. 6224 bool IsPostfixUpdate; 6225 6226 public: 6227 OpenMPAtomicUpdateChecker(Sema &SemaRef) 6228 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 6229 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 6230 /// Check specified statement that it is suitable for 'atomic update' 6231 /// constructs and extract 'x', 'expr' and Operation from the original 6232 /// expression. If DiagId and NoteId == 0, then only check is performed 6233 /// without error notification. 6234 /// \param DiagId Diagnostic which should be emitted if error is found. 6235 /// \param NoteId Diagnostic note for the main error message. 6236 /// \return true if statement is not an update expression, false otherwise. 6237 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 6238 /// Return the 'x' lvalue part of the source atomic expression. 6239 Expr *getX() const { return X; } 6240 /// Return the 'expr' rvalue part of the source atomic expression. 6241 Expr *getExpr() const { return E; } 6242 /// Return the update expression used in calculation of the updated 6243 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 6244 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 6245 Expr *getUpdateExpr() const { return UpdateExpr; } 6246 /// Return true if 'x' is LHS in RHS part of full update expression, 6247 /// false otherwise. 6248 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 6249 6250 /// true if the source expression is a postfix unary operation, false 6251 /// if it is a prefix unary operation. 6252 bool isPostfixUpdate() const { return IsPostfixUpdate; } 6253 6254 private: 6255 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 6256 unsigned NoteId = 0); 6257 }; 6258 } // namespace 6259 6260 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 6261 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 6262 ExprAnalysisErrorCode ErrorFound = NoError; 6263 SourceLocation ErrorLoc, NoteLoc; 6264 SourceRange ErrorRange, NoteRange; 6265 // Allowed constructs are: 6266 // x = x binop expr; 6267 // x = expr binop x; 6268 if (AtomicBinOp->getOpcode() == BO_Assign) { 6269 X = AtomicBinOp->getLHS(); 6270 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 6271 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 6272 if (AtomicInnerBinOp->isMultiplicativeOp() || 6273 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 6274 AtomicInnerBinOp->isBitwiseOp()) { 6275 Op = AtomicInnerBinOp->getOpcode(); 6276 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 6277 Expr *LHS = AtomicInnerBinOp->getLHS(); 6278 Expr *RHS = AtomicInnerBinOp->getRHS(); 6279 llvm::FoldingSetNodeID XId, LHSId, RHSId; 6280 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 6281 /*Canonical=*/true); 6282 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 6283 /*Canonical=*/true); 6284 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 6285 /*Canonical=*/true); 6286 if (XId == LHSId) { 6287 E = RHS; 6288 IsXLHSInRHSPart = true; 6289 } else if (XId == RHSId) { 6290 E = LHS; 6291 IsXLHSInRHSPart = false; 6292 } else { 6293 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 6294 ErrorRange = AtomicInnerBinOp->getSourceRange(); 6295 NoteLoc = X->getExprLoc(); 6296 NoteRange = X->getSourceRange(); 6297 ErrorFound = NotAnUpdateExpression; 6298 } 6299 } else { 6300 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 6301 ErrorRange = AtomicInnerBinOp->getSourceRange(); 6302 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 6303 NoteRange = SourceRange(NoteLoc, NoteLoc); 6304 ErrorFound = NotABinaryOperator; 6305 } 6306 } else { 6307 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 6308 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 6309 ErrorFound = NotABinaryExpression; 6310 } 6311 } else { 6312 ErrorLoc = AtomicBinOp->getExprLoc(); 6313 ErrorRange = AtomicBinOp->getSourceRange(); 6314 NoteLoc = AtomicBinOp->getOperatorLoc(); 6315 NoteRange = SourceRange(NoteLoc, NoteLoc); 6316 ErrorFound = NotAnAssignmentOp; 6317 } 6318 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 6319 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 6320 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 6321 return true; 6322 } 6323 if (SemaRef.CurContext->isDependentContext()) 6324 E = X = UpdateExpr = nullptr; 6325 return ErrorFound != NoError; 6326 } 6327 6328 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 6329 unsigned NoteId) { 6330 ExprAnalysisErrorCode ErrorFound = NoError; 6331 SourceLocation ErrorLoc, NoteLoc; 6332 SourceRange ErrorRange, NoteRange; 6333 // Allowed constructs are: 6334 // x++; 6335 // x--; 6336 // ++x; 6337 // --x; 6338 // x binop= expr; 6339 // x = x binop expr; 6340 // x = expr binop x; 6341 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 6342 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 6343 if (AtomicBody->getType()->isScalarType() || 6344 AtomicBody->isInstantiationDependent()) { 6345 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 6346 AtomicBody->IgnoreParenImpCasts())) { 6347 // Check for Compound Assignment Operation 6348 Op = BinaryOperator::getOpForCompoundAssignment( 6349 AtomicCompAssignOp->getOpcode()); 6350 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 6351 E = AtomicCompAssignOp->getRHS(); 6352 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 6353 IsXLHSInRHSPart = true; 6354 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 6355 AtomicBody->IgnoreParenImpCasts())) { 6356 // Check for Binary Operation 6357 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 6358 return true; 6359 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 6360 AtomicBody->IgnoreParenImpCasts())) { 6361 // Check for Unary Operation 6362 if (AtomicUnaryOp->isIncrementDecrementOp()) { 6363 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 6364 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 6365 OpLoc = AtomicUnaryOp->getOperatorLoc(); 6366 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 6367 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 6368 IsXLHSInRHSPart = true; 6369 } else { 6370 ErrorFound = NotAnUnaryIncDecExpression; 6371 ErrorLoc = AtomicUnaryOp->getExprLoc(); 6372 ErrorRange = AtomicUnaryOp->getSourceRange(); 6373 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 6374 NoteRange = SourceRange(NoteLoc, NoteLoc); 6375 } 6376 } else if (!AtomicBody->isInstantiationDependent()) { 6377 ErrorFound = NotABinaryOrUnaryExpression; 6378 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 6379 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 6380 } 6381 } else { 6382 ErrorFound = NotAScalarType; 6383 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc(); 6384 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6385 } 6386 } else { 6387 ErrorFound = NotAnExpression; 6388 NoteLoc = ErrorLoc = S->getBeginLoc(); 6389 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6390 } 6391 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 6392 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 6393 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 6394 return true; 6395 } 6396 if (SemaRef.CurContext->isDependentContext()) 6397 E = X = UpdateExpr = nullptr; 6398 if (ErrorFound == NoError && E && X) { 6399 // Build an update expression of form 'OpaqueValueExpr(x) binop 6400 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 6401 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 6402 auto *OVEX = new (SemaRef.getASTContext()) 6403 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); 6404 auto *OVEExpr = new (SemaRef.getASTContext()) 6405 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); 6406 ExprResult Update = 6407 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 6408 IsXLHSInRHSPart ? OVEExpr : OVEX); 6409 if (Update.isInvalid()) 6410 return true; 6411 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 6412 Sema::AA_Casting); 6413 if (Update.isInvalid()) 6414 return true; 6415 UpdateExpr = Update.get(); 6416 } 6417 return ErrorFound != NoError; 6418 } 6419 6420 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 6421 Stmt *AStmt, 6422 SourceLocation StartLoc, 6423 SourceLocation EndLoc) { 6424 if (!AStmt) 6425 return StmtError(); 6426 6427 auto *CS = cast<CapturedStmt>(AStmt); 6428 // 1.2.2 OpenMP Language Terminology 6429 // Structured block - An executable statement with a single entry at the 6430 // top and a single exit at the bottom. 6431 // The point of exit cannot be a branch out of the structured block. 6432 // longjmp() and throw() must not violate the entry/exit criteria. 6433 OpenMPClauseKind AtomicKind = OMPC_unknown; 6434 SourceLocation AtomicKindLoc; 6435 for (const OMPClause *C : Clauses) { 6436 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 6437 C->getClauseKind() == OMPC_update || 6438 C->getClauseKind() == OMPC_capture) { 6439 if (AtomicKind != OMPC_unknown) { 6440 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses) 6441 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 6442 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause) 6443 << getOpenMPClauseName(AtomicKind); 6444 } else { 6445 AtomicKind = C->getClauseKind(); 6446 AtomicKindLoc = C->getBeginLoc(); 6447 } 6448 } 6449 } 6450 6451 Stmt *Body = CS->getCapturedStmt(); 6452 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 6453 Body = EWC->getSubExpr(); 6454 6455 Expr *X = nullptr; 6456 Expr *V = nullptr; 6457 Expr *E = nullptr; 6458 Expr *UE = nullptr; 6459 bool IsXLHSInRHSPart = false; 6460 bool IsPostfixUpdate = false; 6461 // OpenMP [2.12.6, atomic Construct] 6462 // In the next expressions: 6463 // * x and v (as applicable) are both l-value expressions with scalar type. 6464 // * During the execution of an atomic region, multiple syntactic 6465 // occurrences of x must designate the same storage location. 6466 // * Neither of v and expr (as applicable) may access the storage location 6467 // designated by x. 6468 // * Neither of x and expr (as applicable) may access the storage location 6469 // designated by v. 6470 // * expr is an expression with scalar type. 6471 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 6472 // * binop, binop=, ++, and -- are not overloaded operators. 6473 // * The expression x binop expr must be numerically equivalent to x binop 6474 // (expr). This requirement is satisfied if the operators in expr have 6475 // precedence greater than binop, or by using parentheses around expr or 6476 // subexpressions of expr. 6477 // * The expression expr binop x must be numerically equivalent to (expr) 6478 // binop x. This requirement is satisfied if the operators in expr have 6479 // precedence equal to or greater than binop, or by using parentheses around 6480 // expr or subexpressions of expr. 6481 // * For forms that allow multiple occurrences of x, the number of times 6482 // that x is evaluated is unspecified. 6483 if (AtomicKind == OMPC_read) { 6484 enum { 6485 NotAnExpression, 6486 NotAnAssignmentOp, 6487 NotAScalarType, 6488 NotAnLValue, 6489 NoError 6490 } ErrorFound = NoError; 6491 SourceLocation ErrorLoc, NoteLoc; 6492 SourceRange ErrorRange, NoteRange; 6493 // If clause is read: 6494 // v = x; 6495 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 6496 const auto *AtomicBinOp = 6497 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 6498 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 6499 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 6500 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 6501 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 6502 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 6503 if (!X->isLValue() || !V->isLValue()) { 6504 const Expr *NotLValueExpr = X->isLValue() ? V : X; 6505 ErrorFound = NotAnLValue; 6506 ErrorLoc = AtomicBinOp->getExprLoc(); 6507 ErrorRange = AtomicBinOp->getSourceRange(); 6508 NoteLoc = NotLValueExpr->getExprLoc(); 6509 NoteRange = NotLValueExpr->getSourceRange(); 6510 } 6511 } else if (!X->isInstantiationDependent() || 6512 !V->isInstantiationDependent()) { 6513 const Expr *NotScalarExpr = 6514 (X->isInstantiationDependent() || X->getType()->isScalarType()) 6515 ? V 6516 : X; 6517 ErrorFound = NotAScalarType; 6518 ErrorLoc = AtomicBinOp->getExprLoc(); 6519 ErrorRange = AtomicBinOp->getSourceRange(); 6520 NoteLoc = NotScalarExpr->getExprLoc(); 6521 NoteRange = NotScalarExpr->getSourceRange(); 6522 } 6523 } else if (!AtomicBody->isInstantiationDependent()) { 6524 ErrorFound = NotAnAssignmentOp; 6525 ErrorLoc = AtomicBody->getExprLoc(); 6526 ErrorRange = AtomicBody->getSourceRange(); 6527 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 6528 : AtomicBody->getExprLoc(); 6529 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 6530 : AtomicBody->getSourceRange(); 6531 } 6532 } else { 6533 ErrorFound = NotAnExpression; 6534 NoteLoc = ErrorLoc = Body->getBeginLoc(); 6535 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6536 } 6537 if (ErrorFound != NoError) { 6538 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 6539 << ErrorRange; 6540 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 6541 << NoteRange; 6542 return StmtError(); 6543 } 6544 if (CurContext->isDependentContext()) 6545 V = X = nullptr; 6546 } else if (AtomicKind == OMPC_write) { 6547 enum { 6548 NotAnExpression, 6549 NotAnAssignmentOp, 6550 NotAScalarType, 6551 NotAnLValue, 6552 NoError 6553 } ErrorFound = NoError; 6554 SourceLocation ErrorLoc, NoteLoc; 6555 SourceRange ErrorRange, NoteRange; 6556 // If clause is write: 6557 // x = expr; 6558 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 6559 const auto *AtomicBinOp = 6560 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 6561 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 6562 X = AtomicBinOp->getLHS(); 6563 E = AtomicBinOp->getRHS(); 6564 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 6565 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 6566 if (!X->isLValue()) { 6567 ErrorFound = NotAnLValue; 6568 ErrorLoc = AtomicBinOp->getExprLoc(); 6569 ErrorRange = AtomicBinOp->getSourceRange(); 6570 NoteLoc = X->getExprLoc(); 6571 NoteRange = X->getSourceRange(); 6572 } 6573 } else if (!X->isInstantiationDependent() || 6574 !E->isInstantiationDependent()) { 6575 const Expr *NotScalarExpr = 6576 (X->isInstantiationDependent() || X->getType()->isScalarType()) 6577 ? E 6578 : X; 6579 ErrorFound = NotAScalarType; 6580 ErrorLoc = AtomicBinOp->getExprLoc(); 6581 ErrorRange = AtomicBinOp->getSourceRange(); 6582 NoteLoc = NotScalarExpr->getExprLoc(); 6583 NoteRange = NotScalarExpr->getSourceRange(); 6584 } 6585 } else if (!AtomicBody->isInstantiationDependent()) { 6586 ErrorFound = NotAnAssignmentOp; 6587 ErrorLoc = AtomicBody->getExprLoc(); 6588 ErrorRange = AtomicBody->getSourceRange(); 6589 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 6590 : AtomicBody->getExprLoc(); 6591 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 6592 : AtomicBody->getSourceRange(); 6593 } 6594 } else { 6595 ErrorFound = NotAnExpression; 6596 NoteLoc = ErrorLoc = Body->getBeginLoc(); 6597 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 6598 } 6599 if (ErrorFound != NoError) { 6600 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 6601 << ErrorRange; 6602 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 6603 << NoteRange; 6604 return StmtError(); 6605 } 6606 if (CurContext->isDependentContext()) 6607 E = X = nullptr; 6608 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 6609 // If clause is update: 6610 // x++; 6611 // x--; 6612 // ++x; 6613 // --x; 6614 // x binop= expr; 6615 // x = x binop expr; 6616 // x = expr binop x; 6617 OpenMPAtomicUpdateChecker Checker(*this); 6618 if (Checker.checkStatement( 6619 Body, (AtomicKind == OMPC_update) 6620 ? diag::err_omp_atomic_update_not_expression_statement 6621 : diag::err_omp_atomic_not_expression_statement, 6622 diag::note_omp_atomic_update)) 6623 return StmtError(); 6624 if (!CurContext->isDependentContext()) { 6625 E = Checker.getExpr(); 6626 X = Checker.getX(); 6627 UE = Checker.getUpdateExpr(); 6628 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6629 } 6630 } else if (AtomicKind == OMPC_capture) { 6631 enum { 6632 NotAnAssignmentOp, 6633 NotACompoundStatement, 6634 NotTwoSubstatements, 6635 NotASpecificExpression, 6636 NoError 6637 } ErrorFound = NoError; 6638 SourceLocation ErrorLoc, NoteLoc; 6639 SourceRange ErrorRange, NoteRange; 6640 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 6641 // If clause is a capture: 6642 // v = x++; 6643 // v = x--; 6644 // v = ++x; 6645 // v = --x; 6646 // v = x binop= expr; 6647 // v = x = x binop expr; 6648 // v = x = expr binop x; 6649 const auto *AtomicBinOp = 6650 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 6651 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 6652 V = AtomicBinOp->getLHS(); 6653 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 6654 OpenMPAtomicUpdateChecker Checker(*this); 6655 if (Checker.checkStatement( 6656 Body, diag::err_omp_atomic_capture_not_expression_statement, 6657 diag::note_omp_atomic_update)) 6658 return StmtError(); 6659 E = Checker.getExpr(); 6660 X = Checker.getX(); 6661 UE = Checker.getUpdateExpr(); 6662 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6663 IsPostfixUpdate = Checker.isPostfixUpdate(); 6664 } else if (!AtomicBody->isInstantiationDependent()) { 6665 ErrorLoc = AtomicBody->getExprLoc(); 6666 ErrorRange = AtomicBody->getSourceRange(); 6667 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 6668 : AtomicBody->getExprLoc(); 6669 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 6670 : AtomicBody->getSourceRange(); 6671 ErrorFound = NotAnAssignmentOp; 6672 } 6673 if (ErrorFound != NoError) { 6674 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 6675 << ErrorRange; 6676 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 6677 return StmtError(); 6678 } 6679 if (CurContext->isDependentContext()) 6680 UE = V = E = X = nullptr; 6681 } else { 6682 // If clause is a capture: 6683 // { v = x; x = expr; } 6684 // { v = x; x++; } 6685 // { v = x; x--; } 6686 // { v = x; ++x; } 6687 // { v = x; --x; } 6688 // { v = x; x binop= expr; } 6689 // { v = x; x = x binop expr; } 6690 // { v = x; x = expr binop x; } 6691 // { x++; v = x; } 6692 // { x--; v = x; } 6693 // { ++x; v = x; } 6694 // { --x; v = x; } 6695 // { x binop= expr; v = x; } 6696 // { x = x binop expr; v = x; } 6697 // { x = expr binop x; v = x; } 6698 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 6699 // Check that this is { expr1; expr2; } 6700 if (CS->size() == 2) { 6701 Stmt *First = CS->body_front(); 6702 Stmt *Second = CS->body_back(); 6703 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 6704 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 6705 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 6706 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 6707 // Need to find what subexpression is 'v' and what is 'x'. 6708 OpenMPAtomicUpdateChecker Checker(*this); 6709 bool IsUpdateExprFound = !Checker.checkStatement(Second); 6710 BinaryOperator *BinOp = nullptr; 6711 if (IsUpdateExprFound) { 6712 BinOp = dyn_cast<BinaryOperator>(First); 6713 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 6714 } 6715 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 6716 // { v = x; x++; } 6717 // { v = x; x--; } 6718 // { v = x; ++x; } 6719 // { v = x; --x; } 6720 // { v = x; x binop= expr; } 6721 // { v = x; x = x binop expr; } 6722 // { v = x; x = expr binop x; } 6723 // Check that the first expression has form v = x. 6724 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 6725 llvm::FoldingSetNodeID XId, PossibleXId; 6726 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 6727 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 6728 IsUpdateExprFound = XId == PossibleXId; 6729 if (IsUpdateExprFound) { 6730 V = BinOp->getLHS(); 6731 X = Checker.getX(); 6732 E = Checker.getExpr(); 6733 UE = Checker.getUpdateExpr(); 6734 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6735 IsPostfixUpdate = true; 6736 } 6737 } 6738 if (!IsUpdateExprFound) { 6739 IsUpdateExprFound = !Checker.checkStatement(First); 6740 BinOp = nullptr; 6741 if (IsUpdateExprFound) { 6742 BinOp = dyn_cast<BinaryOperator>(Second); 6743 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 6744 } 6745 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 6746 // { x++; v = x; } 6747 // { x--; v = x; } 6748 // { ++x; v = x; } 6749 // { --x; v = x; } 6750 // { x binop= expr; v = x; } 6751 // { x = x binop expr; v = x; } 6752 // { x = expr binop x; v = x; } 6753 // Check that the second expression has form v = x. 6754 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 6755 llvm::FoldingSetNodeID XId, PossibleXId; 6756 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 6757 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 6758 IsUpdateExprFound = XId == PossibleXId; 6759 if (IsUpdateExprFound) { 6760 V = BinOp->getLHS(); 6761 X = Checker.getX(); 6762 E = Checker.getExpr(); 6763 UE = Checker.getUpdateExpr(); 6764 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 6765 IsPostfixUpdate = false; 6766 } 6767 } 6768 } 6769 if (!IsUpdateExprFound) { 6770 // { v = x; x = expr; } 6771 auto *FirstExpr = dyn_cast<Expr>(First); 6772 auto *SecondExpr = dyn_cast<Expr>(Second); 6773 if (!FirstExpr || !SecondExpr || 6774 !(FirstExpr->isInstantiationDependent() || 6775 SecondExpr->isInstantiationDependent())) { 6776 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 6777 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 6778 ErrorFound = NotAnAssignmentOp; 6779 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 6780 : First->getBeginLoc(); 6781 NoteRange = ErrorRange = FirstBinOp 6782 ? FirstBinOp->getSourceRange() 6783 : SourceRange(ErrorLoc, ErrorLoc); 6784 } else { 6785 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 6786 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 6787 ErrorFound = NotAnAssignmentOp; 6788 NoteLoc = ErrorLoc = SecondBinOp 6789 ? SecondBinOp->getOperatorLoc() 6790 : Second->getBeginLoc(); 6791 NoteRange = ErrorRange = 6792 SecondBinOp ? SecondBinOp->getSourceRange() 6793 : SourceRange(ErrorLoc, ErrorLoc); 6794 } else { 6795 Expr *PossibleXRHSInFirst = 6796 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 6797 Expr *PossibleXLHSInSecond = 6798 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 6799 llvm::FoldingSetNodeID X1Id, X2Id; 6800 PossibleXRHSInFirst->Profile(X1Id, Context, 6801 /*Canonical=*/true); 6802 PossibleXLHSInSecond->Profile(X2Id, Context, 6803 /*Canonical=*/true); 6804 IsUpdateExprFound = X1Id == X2Id; 6805 if (IsUpdateExprFound) { 6806 V = FirstBinOp->getLHS(); 6807 X = SecondBinOp->getLHS(); 6808 E = SecondBinOp->getRHS(); 6809 UE = nullptr; 6810 IsXLHSInRHSPart = false; 6811 IsPostfixUpdate = true; 6812 } else { 6813 ErrorFound = NotASpecificExpression; 6814 ErrorLoc = FirstBinOp->getExprLoc(); 6815 ErrorRange = FirstBinOp->getSourceRange(); 6816 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 6817 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 6818 } 6819 } 6820 } 6821 } 6822 } 6823 } else { 6824 NoteLoc = ErrorLoc = Body->getBeginLoc(); 6825 NoteRange = ErrorRange = 6826 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 6827 ErrorFound = NotTwoSubstatements; 6828 } 6829 } else { 6830 NoteLoc = ErrorLoc = Body->getBeginLoc(); 6831 NoteRange = ErrorRange = 6832 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 6833 ErrorFound = NotACompoundStatement; 6834 } 6835 if (ErrorFound != NoError) { 6836 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 6837 << ErrorRange; 6838 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 6839 return StmtError(); 6840 } 6841 if (CurContext->isDependentContext()) 6842 UE = V = E = X = nullptr; 6843 } 6844 } 6845 6846 setFunctionHasBranchProtectedScope(); 6847 6848 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 6849 X, V, E, UE, IsXLHSInRHSPart, 6850 IsPostfixUpdate); 6851 } 6852 6853 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 6854 Stmt *AStmt, 6855 SourceLocation StartLoc, 6856 SourceLocation EndLoc) { 6857 if (!AStmt) 6858 return StmtError(); 6859 6860 auto *CS = cast<CapturedStmt>(AStmt); 6861 // 1.2.2 OpenMP Language Terminology 6862 // Structured block - An executable statement with a single entry at the 6863 // top and a single exit at the bottom. 6864 // The point of exit cannot be a branch out of the structured block. 6865 // longjmp() and throw() must not violate the entry/exit criteria. 6866 CS->getCapturedDecl()->setNothrow(); 6867 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 6868 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6869 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6870 // 1.2.2 OpenMP Language Terminology 6871 // Structured block - An executable statement with a single entry at the 6872 // top and a single exit at the bottom. 6873 // The point of exit cannot be a branch out of the structured block. 6874 // longjmp() and throw() must not violate the entry/exit criteria. 6875 CS->getCapturedDecl()->setNothrow(); 6876 } 6877 6878 // OpenMP [2.16, Nesting of Regions] 6879 // If specified, a teams construct must be contained within a target 6880 // construct. That target construct must contain no statements or directives 6881 // outside of the teams construct. 6882 if (DSAStack->hasInnerTeamsRegion()) { 6883 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 6884 bool OMPTeamsFound = true; 6885 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 6886 auto I = CS->body_begin(); 6887 while (I != CS->body_end()) { 6888 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 6889 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) { 6890 OMPTeamsFound = false; 6891 break; 6892 } 6893 ++I; 6894 } 6895 assert(I != CS->body_end() && "Not found statement"); 6896 S = *I; 6897 } else { 6898 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 6899 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 6900 } 6901 if (!OMPTeamsFound) { 6902 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 6903 Diag(DSAStack->getInnerTeamsRegionLoc(), 6904 diag::note_omp_nested_teams_construct_here); 6905 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here) 6906 << isa<OMPExecutableDirective>(S); 6907 return StmtError(); 6908 } 6909 } 6910 6911 setFunctionHasBranchProtectedScope(); 6912 6913 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 6914 } 6915 6916 StmtResult 6917 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 6918 Stmt *AStmt, SourceLocation StartLoc, 6919 SourceLocation EndLoc) { 6920 if (!AStmt) 6921 return StmtError(); 6922 6923 auto *CS = cast<CapturedStmt>(AStmt); 6924 // 1.2.2 OpenMP Language Terminology 6925 // Structured block - An executable statement with a single entry at the 6926 // top and a single exit at the bottom. 6927 // The point of exit cannot be a branch out of the structured block. 6928 // longjmp() and throw() must not violate the entry/exit criteria. 6929 CS->getCapturedDecl()->setNothrow(); 6930 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 6931 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6932 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6933 // 1.2.2 OpenMP Language Terminology 6934 // Structured block - An executable statement with a single entry at the 6935 // top and a single exit at the bottom. 6936 // The point of exit cannot be a branch out of the structured block. 6937 // longjmp() and throw() must not violate the entry/exit criteria. 6938 CS->getCapturedDecl()->setNothrow(); 6939 } 6940 6941 setFunctionHasBranchProtectedScope(); 6942 6943 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, 6944 AStmt); 6945 } 6946 6947 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 6948 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6949 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6950 if (!AStmt) 6951 return StmtError(); 6952 6953 auto *CS = cast<CapturedStmt>(AStmt); 6954 // 1.2.2 OpenMP Language Terminology 6955 // Structured block - An executable statement with a single entry at the 6956 // top and a single exit at the bottom. 6957 // The point of exit cannot be a branch out of the structured block. 6958 // longjmp() and throw() must not violate the entry/exit criteria. 6959 CS->getCapturedDecl()->setNothrow(); 6960 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 6961 ThisCaptureLevel > 1; --ThisCaptureLevel) { 6962 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 6963 // 1.2.2 OpenMP Language Terminology 6964 // Structured block - An executable statement with a single entry at the 6965 // top and a single exit at the bottom. 6966 // The point of exit cannot be a branch out of the structured block. 6967 // longjmp() and throw() must not violate the entry/exit criteria. 6968 CS->getCapturedDecl()->setNothrow(); 6969 } 6970 6971 OMPLoopDirective::HelperExprs B; 6972 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6973 // define the nested loops number. 6974 unsigned NestedLoopCount = 6975 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 6976 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 6977 VarsWithImplicitDSA, B); 6978 if (NestedLoopCount == 0) 6979 return StmtError(); 6980 6981 assert((CurContext->isDependentContext() || B.builtAll()) && 6982 "omp target parallel for loop exprs were not built"); 6983 6984 if (!CurContext->isDependentContext()) { 6985 // Finalize the clauses that need pre-built expressions for CodeGen. 6986 for (OMPClause *C : Clauses) { 6987 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6988 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6989 B.NumIterations, *this, CurScope, 6990 DSAStack)) 6991 return StmtError(); 6992 } 6993 } 6994 6995 setFunctionHasBranchProtectedScope(); 6996 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc, 6997 NestedLoopCount, Clauses, AStmt, 6998 B, DSAStack->isCancelRegion()); 6999 } 7000 7001 /// Check for existence of a map clause in the list of clauses. 7002 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 7003 const OpenMPClauseKind K) { 7004 return llvm::any_of( 7005 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 7006 } 7007 7008 template <typename... Params> 7009 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 7010 const Params... ClauseTypes) { 7011 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 7012 } 7013 7014 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 7015 Stmt *AStmt, 7016 SourceLocation StartLoc, 7017 SourceLocation EndLoc) { 7018 if (!AStmt) 7019 return StmtError(); 7020 7021 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 7022 7023 // OpenMP [2.10.1, Restrictions, p. 97] 7024 // At least one map clause must appear on the directive. 7025 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) { 7026 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 7027 << "'map' or 'use_device_ptr'" 7028 << getOpenMPDirectiveName(OMPD_target_data); 7029 return StmtError(); 7030 } 7031 7032 setFunctionHasBranchProtectedScope(); 7033 7034 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 7035 AStmt); 7036 } 7037 7038 StmtResult 7039 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 7040 SourceLocation StartLoc, 7041 SourceLocation EndLoc, Stmt *AStmt) { 7042 if (!AStmt) 7043 return StmtError(); 7044 7045 auto *CS = cast<CapturedStmt>(AStmt); 7046 // 1.2.2 OpenMP Language Terminology 7047 // Structured block - An executable statement with a single entry at the 7048 // top and a single exit at the bottom. 7049 // The point of exit cannot be a branch out of the structured block. 7050 // longjmp() and throw() must not violate the entry/exit criteria. 7051 CS->getCapturedDecl()->setNothrow(); 7052 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 7053 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7054 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7055 // 1.2.2 OpenMP Language Terminology 7056 // Structured block - An executable statement with a single entry at the 7057 // top and a single exit at the bottom. 7058 // The point of exit cannot be a branch out of the structured block. 7059 // longjmp() and throw() must not violate the entry/exit criteria. 7060 CS->getCapturedDecl()->setNothrow(); 7061 } 7062 7063 // OpenMP [2.10.2, Restrictions, p. 99] 7064 // At least one map clause must appear on the directive. 7065 if (!hasClauses(Clauses, OMPC_map)) { 7066 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 7067 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 7068 return StmtError(); 7069 } 7070 7071 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 7072 AStmt); 7073 } 7074 7075 StmtResult 7076 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 7077 SourceLocation StartLoc, 7078 SourceLocation EndLoc, Stmt *AStmt) { 7079 if (!AStmt) 7080 return StmtError(); 7081 7082 auto *CS = cast<CapturedStmt>(AStmt); 7083 // 1.2.2 OpenMP Language Terminology 7084 // Structured block - An executable statement with a single entry at the 7085 // top and a single exit at the bottom. 7086 // The point of exit cannot be a branch out of the structured block. 7087 // longjmp() and throw() must not violate the entry/exit criteria. 7088 CS->getCapturedDecl()->setNothrow(); 7089 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 7090 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7091 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7092 // 1.2.2 OpenMP Language Terminology 7093 // Structured block - An executable statement with a single entry at the 7094 // top and a single exit at the bottom. 7095 // The point of exit cannot be a branch out of the structured block. 7096 // longjmp() and throw() must not violate the entry/exit criteria. 7097 CS->getCapturedDecl()->setNothrow(); 7098 } 7099 7100 // OpenMP [2.10.3, Restrictions, p. 102] 7101 // At least one map clause must appear on the directive. 7102 if (!hasClauses(Clauses, OMPC_map)) { 7103 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 7104 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 7105 return StmtError(); 7106 } 7107 7108 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 7109 AStmt); 7110 } 7111 7112 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 7113 SourceLocation StartLoc, 7114 SourceLocation EndLoc, 7115 Stmt *AStmt) { 7116 if (!AStmt) 7117 return StmtError(); 7118 7119 auto *CS = cast<CapturedStmt>(AStmt); 7120 // 1.2.2 OpenMP Language Terminology 7121 // Structured block - An executable statement with a single entry at the 7122 // top and a single exit at the bottom. 7123 // The point of exit cannot be a branch out of the structured block. 7124 // longjmp() and throw() must not violate the entry/exit criteria. 7125 CS->getCapturedDecl()->setNothrow(); 7126 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 7127 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7128 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7129 // 1.2.2 OpenMP Language Terminology 7130 // Structured block - An executable statement with a single entry at the 7131 // top and a single exit at the bottom. 7132 // The point of exit cannot be a branch out of the structured block. 7133 // longjmp() and throw() must not violate the entry/exit criteria. 7134 CS->getCapturedDecl()->setNothrow(); 7135 } 7136 7137 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 7138 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 7139 return StmtError(); 7140 } 7141 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 7142 AStmt); 7143 } 7144 7145 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 7146 Stmt *AStmt, SourceLocation StartLoc, 7147 SourceLocation EndLoc) { 7148 if (!AStmt) 7149 return StmtError(); 7150 7151 auto *CS = cast<CapturedStmt>(AStmt); 7152 // 1.2.2 OpenMP Language Terminology 7153 // Structured block - An executable statement with a single entry at the 7154 // top and a single exit at the bottom. 7155 // The point of exit cannot be a branch out of the structured block. 7156 // longjmp() and throw() must not violate the entry/exit criteria. 7157 CS->getCapturedDecl()->setNothrow(); 7158 7159 setFunctionHasBranchProtectedScope(); 7160 7161 DSAStack->setParentTeamsRegionLoc(StartLoc); 7162 7163 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 7164 } 7165 7166 StmtResult 7167 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 7168 SourceLocation EndLoc, 7169 OpenMPDirectiveKind CancelRegion) { 7170 if (DSAStack->isParentNowaitRegion()) { 7171 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 7172 return StmtError(); 7173 } 7174 if (DSAStack->isParentOrderedRegion()) { 7175 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 7176 return StmtError(); 7177 } 7178 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 7179 CancelRegion); 7180 } 7181 7182 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 7183 SourceLocation StartLoc, 7184 SourceLocation EndLoc, 7185 OpenMPDirectiveKind CancelRegion) { 7186 if (DSAStack->isParentNowaitRegion()) { 7187 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 7188 return StmtError(); 7189 } 7190 if (DSAStack->isParentOrderedRegion()) { 7191 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 7192 return StmtError(); 7193 } 7194 DSAStack->setParentCancelRegion(/*Cancel=*/true); 7195 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 7196 CancelRegion); 7197 } 7198 7199 static bool checkGrainsizeNumTasksClauses(Sema &S, 7200 ArrayRef<OMPClause *> Clauses) { 7201 const OMPClause *PrevClause = nullptr; 7202 bool ErrorFound = false; 7203 for (const OMPClause *C : Clauses) { 7204 if (C->getClauseKind() == OMPC_grainsize || 7205 C->getClauseKind() == OMPC_num_tasks) { 7206 if (!PrevClause) 7207 PrevClause = C; 7208 else if (PrevClause->getClauseKind() != C->getClauseKind()) { 7209 S.Diag(C->getBeginLoc(), 7210 diag::err_omp_grainsize_num_tasks_mutually_exclusive) 7211 << getOpenMPClauseName(C->getClauseKind()) 7212 << getOpenMPClauseName(PrevClause->getClauseKind()); 7213 S.Diag(PrevClause->getBeginLoc(), 7214 diag::note_omp_previous_grainsize_num_tasks) 7215 << getOpenMPClauseName(PrevClause->getClauseKind()); 7216 ErrorFound = true; 7217 } 7218 } 7219 } 7220 return ErrorFound; 7221 } 7222 7223 static bool checkReductionClauseWithNogroup(Sema &S, 7224 ArrayRef<OMPClause *> Clauses) { 7225 const OMPClause *ReductionClause = nullptr; 7226 const OMPClause *NogroupClause = nullptr; 7227 for (const OMPClause *C : Clauses) { 7228 if (C->getClauseKind() == OMPC_reduction) { 7229 ReductionClause = C; 7230 if (NogroupClause) 7231 break; 7232 continue; 7233 } 7234 if (C->getClauseKind() == OMPC_nogroup) { 7235 NogroupClause = C; 7236 if (ReductionClause) 7237 break; 7238 continue; 7239 } 7240 } 7241 if (ReductionClause && NogroupClause) { 7242 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup) 7243 << SourceRange(NogroupClause->getBeginLoc(), 7244 NogroupClause->getEndLoc()); 7245 return true; 7246 } 7247 return false; 7248 } 7249 7250 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 7251 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7252 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7253 if (!AStmt) 7254 return StmtError(); 7255 7256 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 7257 OMPLoopDirective::HelperExprs B; 7258 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 7259 // define the nested loops number. 7260 unsigned NestedLoopCount = 7261 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 7262 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 7263 VarsWithImplicitDSA, B); 7264 if (NestedLoopCount == 0) 7265 return StmtError(); 7266 7267 assert((CurContext->isDependentContext() || B.builtAll()) && 7268 "omp for loop exprs were not built"); 7269 7270 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 7271 // The grainsize clause and num_tasks clause are mutually exclusive and may 7272 // not appear on the same taskloop directive. 7273 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 7274 return StmtError(); 7275 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 7276 // If a reduction clause is present on the taskloop directive, the nogroup 7277 // clause must not be specified. 7278 if (checkReductionClauseWithNogroup(*this, Clauses)) 7279 return StmtError(); 7280 7281 setFunctionHasBranchProtectedScope(); 7282 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 7283 NestedLoopCount, Clauses, AStmt, B); 7284 } 7285 7286 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 7287 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7288 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7289 if (!AStmt) 7290 return StmtError(); 7291 7292 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 7293 OMPLoopDirective::HelperExprs B; 7294 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 7295 // define the nested loops number. 7296 unsigned NestedLoopCount = 7297 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 7298 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 7299 VarsWithImplicitDSA, B); 7300 if (NestedLoopCount == 0) 7301 return StmtError(); 7302 7303 assert((CurContext->isDependentContext() || B.builtAll()) && 7304 "omp for 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 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 7318 // The grainsize clause and num_tasks clause are mutually exclusive and may 7319 // not appear on the same taskloop directive. 7320 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 7321 return StmtError(); 7322 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 7323 // If a reduction clause is present on the taskloop directive, the nogroup 7324 // clause must not be specified. 7325 if (checkReductionClauseWithNogroup(*this, Clauses)) 7326 return StmtError(); 7327 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7328 return StmtError(); 7329 7330 setFunctionHasBranchProtectedScope(); 7331 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 7332 NestedLoopCount, Clauses, AStmt, B); 7333 } 7334 7335 StmtResult Sema::ActOnOpenMPDistributeDirective( 7336 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7337 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7338 if (!AStmt) 7339 return StmtError(); 7340 7341 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 7342 OMPLoopDirective::HelperExprs B; 7343 // In presence of clause 'collapse' with number of loops, it will 7344 // define the nested loops number. 7345 unsigned NestedLoopCount = 7346 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 7347 nullptr /*ordered not a clause on distribute*/, AStmt, 7348 *this, *DSAStack, VarsWithImplicitDSA, B); 7349 if (NestedLoopCount == 0) 7350 return StmtError(); 7351 7352 assert((CurContext->isDependentContext() || B.builtAll()) && 7353 "omp for loop exprs were not built"); 7354 7355 setFunctionHasBranchProtectedScope(); 7356 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 7357 NestedLoopCount, Clauses, AStmt, B); 7358 } 7359 7360 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 7361 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7362 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7363 if (!AStmt) 7364 return StmtError(); 7365 7366 auto *CS = cast<CapturedStmt>(AStmt); 7367 // 1.2.2 OpenMP Language Terminology 7368 // Structured block - An executable statement with a single entry at the 7369 // top and a single exit at the bottom. 7370 // The point of exit cannot be a branch out of the structured block. 7371 // longjmp() and throw() must not violate the entry/exit criteria. 7372 CS->getCapturedDecl()->setNothrow(); 7373 for (int ThisCaptureLevel = 7374 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 7375 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7376 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 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 } 7384 7385 OMPLoopDirective::HelperExprs B; 7386 // In presence of clause 'collapse' with number of loops, it will 7387 // define the nested loops number. 7388 unsigned NestedLoopCount = checkOpenMPLoop( 7389 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 7390 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7391 VarsWithImplicitDSA, B); 7392 if (NestedLoopCount == 0) 7393 return StmtError(); 7394 7395 assert((CurContext->isDependentContext() || B.builtAll()) && 7396 "omp for loop exprs were not built"); 7397 7398 setFunctionHasBranchProtectedScope(); 7399 return OMPDistributeParallelForDirective::Create( 7400 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 7401 DSAStack->isCancelRegion()); 7402 } 7403 7404 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 7405 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7406 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7407 if (!AStmt) 7408 return StmtError(); 7409 7410 auto *CS = cast<CapturedStmt>(AStmt); 7411 // 1.2.2 OpenMP Language Terminology 7412 // Structured block - An executable statement with a single entry at the 7413 // top and a single exit at the bottom. 7414 // The point of exit cannot be a branch out of the structured block. 7415 // longjmp() and throw() must not violate the entry/exit criteria. 7416 CS->getCapturedDecl()->setNothrow(); 7417 for (int ThisCaptureLevel = 7418 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 7419 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7420 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7421 // 1.2.2 OpenMP Language Terminology 7422 // Structured block - An executable statement with a single entry at the 7423 // top and a single exit at the bottom. 7424 // The point of exit cannot be a branch out of the structured block. 7425 // longjmp() and throw() must not violate the entry/exit criteria. 7426 CS->getCapturedDecl()->setNothrow(); 7427 } 7428 7429 OMPLoopDirective::HelperExprs B; 7430 // In presence of clause 'collapse' with number of loops, it will 7431 // define the nested loops number. 7432 unsigned NestedLoopCount = checkOpenMPLoop( 7433 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 7434 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7435 VarsWithImplicitDSA, B); 7436 if (NestedLoopCount == 0) 7437 return StmtError(); 7438 7439 assert((CurContext->isDependentContext() || B.builtAll()) && 7440 "omp for loop exprs were not built"); 7441 7442 if (!CurContext->isDependentContext()) { 7443 // Finalize the clauses that need pre-built expressions for CodeGen. 7444 for (OMPClause *C : Clauses) { 7445 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7446 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7447 B.NumIterations, *this, CurScope, 7448 DSAStack)) 7449 return StmtError(); 7450 } 7451 } 7452 7453 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7454 return StmtError(); 7455 7456 setFunctionHasBranchProtectedScope(); 7457 return OMPDistributeParallelForSimdDirective::Create( 7458 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7459 } 7460 7461 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 7462 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7463 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7464 if (!AStmt) 7465 return StmtError(); 7466 7467 auto *CS = cast<CapturedStmt>(AStmt); 7468 // 1.2.2 OpenMP Language Terminology 7469 // Structured block - An executable statement with a single entry at the 7470 // top and a single exit at the bottom. 7471 // The point of exit cannot be a branch out of the structured block. 7472 // longjmp() and throw() must not violate the entry/exit criteria. 7473 CS->getCapturedDecl()->setNothrow(); 7474 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 7475 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7476 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7477 // 1.2.2 OpenMP Language Terminology 7478 // Structured block - An executable statement with a single entry at the 7479 // top and a single exit at the bottom. 7480 // The point of exit cannot be a branch out of the structured block. 7481 // longjmp() and throw() must not violate the entry/exit criteria. 7482 CS->getCapturedDecl()->setNothrow(); 7483 } 7484 7485 OMPLoopDirective::HelperExprs B; 7486 // In presence of clause 'collapse' with number of loops, it will 7487 // define the nested loops number. 7488 unsigned NestedLoopCount = 7489 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 7490 nullptr /*ordered not a clause on distribute*/, CS, *this, 7491 *DSAStack, VarsWithImplicitDSA, B); 7492 if (NestedLoopCount == 0) 7493 return StmtError(); 7494 7495 assert((CurContext->isDependentContext() || B.builtAll()) && 7496 "omp for loop exprs were not built"); 7497 7498 if (!CurContext->isDependentContext()) { 7499 // Finalize the clauses that need pre-built expressions for CodeGen. 7500 for (OMPClause *C : Clauses) { 7501 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7502 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7503 B.NumIterations, *this, CurScope, 7504 DSAStack)) 7505 return StmtError(); 7506 } 7507 } 7508 7509 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7510 return StmtError(); 7511 7512 setFunctionHasBranchProtectedScope(); 7513 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 7514 NestedLoopCount, Clauses, AStmt, B); 7515 } 7516 7517 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 7518 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7519 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7520 if (!AStmt) 7521 return StmtError(); 7522 7523 auto *CS = cast<CapturedStmt>(AStmt); 7524 // 1.2.2 OpenMP Language Terminology 7525 // Structured block - An executable statement with a single entry at the 7526 // top and a single exit at the bottom. 7527 // The point of exit cannot be a branch out of the structured block. 7528 // longjmp() and throw() must not violate the entry/exit criteria. 7529 CS->getCapturedDecl()->setNothrow(); 7530 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 7531 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7532 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7533 // 1.2.2 OpenMP Language Terminology 7534 // Structured block - An executable statement with a single entry at the 7535 // top and a single exit at the bottom. 7536 // The point of exit cannot be a branch out of the structured block. 7537 // longjmp() and throw() must not violate the entry/exit criteria. 7538 CS->getCapturedDecl()->setNothrow(); 7539 } 7540 7541 OMPLoopDirective::HelperExprs B; 7542 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 7543 // define the nested loops number. 7544 unsigned NestedLoopCount = checkOpenMPLoop( 7545 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 7546 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 7547 VarsWithImplicitDSA, B); 7548 if (NestedLoopCount == 0) 7549 return StmtError(); 7550 7551 assert((CurContext->isDependentContext() || B.builtAll()) && 7552 "omp target parallel for simd loop exprs were not built"); 7553 7554 if (!CurContext->isDependentContext()) { 7555 // Finalize the clauses that need pre-built expressions for CodeGen. 7556 for (OMPClause *C : Clauses) { 7557 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7558 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7559 B.NumIterations, *this, CurScope, 7560 DSAStack)) 7561 return StmtError(); 7562 } 7563 } 7564 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7565 return StmtError(); 7566 7567 setFunctionHasBranchProtectedScope(); 7568 return OMPTargetParallelForSimdDirective::Create( 7569 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7570 } 7571 7572 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 7573 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7574 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7575 if (!AStmt) 7576 return StmtError(); 7577 7578 auto *CS = cast<CapturedStmt>(AStmt); 7579 // 1.2.2 OpenMP Language Terminology 7580 // Structured block - An executable statement with a single entry at the 7581 // top and a single exit at the bottom. 7582 // The point of exit cannot be a branch out of the structured block. 7583 // longjmp() and throw() must not violate the entry/exit criteria. 7584 CS->getCapturedDecl()->setNothrow(); 7585 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 7586 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7587 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7588 // 1.2.2 OpenMP Language Terminology 7589 // Structured block - An executable statement with a single entry at the 7590 // top and a single exit at the bottom. 7591 // The point of exit cannot be a branch out of the structured block. 7592 // longjmp() and throw() must not violate the entry/exit criteria. 7593 CS->getCapturedDecl()->setNothrow(); 7594 } 7595 7596 OMPLoopDirective::HelperExprs B; 7597 // In presence of clause 'collapse' with number of loops, it will define the 7598 // nested loops number. 7599 unsigned NestedLoopCount = 7600 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 7601 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 7602 VarsWithImplicitDSA, B); 7603 if (NestedLoopCount == 0) 7604 return StmtError(); 7605 7606 assert((CurContext->isDependentContext() || B.builtAll()) && 7607 "omp target simd loop exprs were not built"); 7608 7609 if (!CurContext->isDependentContext()) { 7610 // Finalize the clauses that need pre-built expressions for CodeGen. 7611 for (OMPClause *C : Clauses) { 7612 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7613 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7614 B.NumIterations, *this, CurScope, 7615 DSAStack)) 7616 return StmtError(); 7617 } 7618 } 7619 7620 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7621 return StmtError(); 7622 7623 setFunctionHasBranchProtectedScope(); 7624 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 7625 NestedLoopCount, Clauses, AStmt, B); 7626 } 7627 7628 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 7629 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7630 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7631 if (!AStmt) 7632 return StmtError(); 7633 7634 auto *CS = cast<CapturedStmt>(AStmt); 7635 // 1.2.2 OpenMP Language Terminology 7636 // Structured block - An executable statement with a single entry at the 7637 // top and a single exit at the bottom. 7638 // The point of exit cannot be a branch out of the structured block. 7639 // longjmp() and throw() must not violate the entry/exit criteria. 7640 CS->getCapturedDecl()->setNothrow(); 7641 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 7642 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7643 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7644 // 1.2.2 OpenMP Language Terminology 7645 // Structured block - An executable statement with a single entry at the 7646 // top and a single exit at the bottom. 7647 // The point of exit cannot be a branch out of the structured block. 7648 // longjmp() and throw() must not violate the entry/exit criteria. 7649 CS->getCapturedDecl()->setNothrow(); 7650 } 7651 7652 OMPLoopDirective::HelperExprs B; 7653 // In presence of clause 'collapse' with number of loops, it will 7654 // define the nested loops number. 7655 unsigned NestedLoopCount = 7656 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 7657 nullptr /*ordered not a clause on distribute*/, CS, *this, 7658 *DSAStack, VarsWithImplicitDSA, B); 7659 if (NestedLoopCount == 0) 7660 return StmtError(); 7661 7662 assert((CurContext->isDependentContext() || B.builtAll()) && 7663 "omp teams distribute loop exprs were not built"); 7664 7665 setFunctionHasBranchProtectedScope(); 7666 7667 DSAStack->setParentTeamsRegionLoc(StartLoc); 7668 7669 return OMPTeamsDistributeDirective::Create( 7670 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7671 } 7672 7673 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 7674 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7675 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7676 if (!AStmt) 7677 return StmtError(); 7678 7679 auto *CS = cast<CapturedStmt>(AStmt); 7680 // 1.2.2 OpenMP Language Terminology 7681 // Structured block - An executable statement with a single entry at the 7682 // top and a single exit at the bottom. 7683 // The point of exit cannot be a branch out of the structured block. 7684 // longjmp() and throw() must not violate the entry/exit criteria. 7685 CS->getCapturedDecl()->setNothrow(); 7686 for (int ThisCaptureLevel = 7687 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 7688 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7689 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7690 // 1.2.2 OpenMP Language Terminology 7691 // Structured block - An executable statement with a single entry at the 7692 // top and a single exit at the bottom. 7693 // The point of exit cannot be a branch out of the structured block. 7694 // longjmp() and throw() must not violate the entry/exit criteria. 7695 CS->getCapturedDecl()->setNothrow(); 7696 } 7697 7698 7699 OMPLoopDirective::HelperExprs B; 7700 // In presence of clause 'collapse' with number of loops, it will 7701 // define the nested loops number. 7702 unsigned NestedLoopCount = checkOpenMPLoop( 7703 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 7704 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7705 VarsWithImplicitDSA, B); 7706 7707 if (NestedLoopCount == 0) 7708 return StmtError(); 7709 7710 assert((CurContext->isDependentContext() || B.builtAll()) && 7711 "omp teams distribute simd loop exprs were not built"); 7712 7713 if (!CurContext->isDependentContext()) { 7714 // Finalize the clauses that need pre-built expressions for CodeGen. 7715 for (OMPClause *C : Clauses) { 7716 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7717 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7718 B.NumIterations, *this, CurScope, 7719 DSAStack)) 7720 return StmtError(); 7721 } 7722 } 7723 7724 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7725 return StmtError(); 7726 7727 setFunctionHasBranchProtectedScope(); 7728 7729 DSAStack->setParentTeamsRegionLoc(StartLoc); 7730 7731 return OMPTeamsDistributeSimdDirective::Create( 7732 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7733 } 7734 7735 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 7736 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7737 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7738 if (!AStmt) 7739 return StmtError(); 7740 7741 auto *CS = cast<CapturedStmt>(AStmt); 7742 // 1.2.2 OpenMP Language Terminology 7743 // Structured block - An executable statement with a single entry at the 7744 // top and a single exit at the bottom. 7745 // The point of exit cannot be a branch out of the structured block. 7746 // longjmp() and throw() must not violate the entry/exit criteria. 7747 CS->getCapturedDecl()->setNothrow(); 7748 7749 for (int ThisCaptureLevel = 7750 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 7751 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7752 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7753 // 1.2.2 OpenMP Language Terminology 7754 // Structured block - An executable statement with a single entry at the 7755 // top and a single exit at the bottom. 7756 // The point of exit cannot be a branch out of the structured block. 7757 // longjmp() and throw() must not violate the entry/exit criteria. 7758 CS->getCapturedDecl()->setNothrow(); 7759 } 7760 7761 OMPLoopDirective::HelperExprs B; 7762 // In presence of clause 'collapse' with number of loops, it will 7763 // define the nested loops number. 7764 unsigned NestedLoopCount = checkOpenMPLoop( 7765 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 7766 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7767 VarsWithImplicitDSA, B); 7768 7769 if (NestedLoopCount == 0) 7770 return StmtError(); 7771 7772 assert((CurContext->isDependentContext() || B.builtAll()) && 7773 "omp for loop exprs were not built"); 7774 7775 if (!CurContext->isDependentContext()) { 7776 // Finalize the clauses that need pre-built expressions for CodeGen. 7777 for (OMPClause *C : Clauses) { 7778 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7779 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7780 B.NumIterations, *this, CurScope, 7781 DSAStack)) 7782 return StmtError(); 7783 } 7784 } 7785 7786 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7787 return StmtError(); 7788 7789 setFunctionHasBranchProtectedScope(); 7790 7791 DSAStack->setParentTeamsRegionLoc(StartLoc); 7792 7793 return OMPTeamsDistributeParallelForSimdDirective::Create( 7794 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7795 } 7796 7797 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 7798 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7799 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7800 if (!AStmt) 7801 return StmtError(); 7802 7803 auto *CS = cast<CapturedStmt>(AStmt); 7804 // 1.2.2 OpenMP Language Terminology 7805 // Structured block - An executable statement with a single entry at the 7806 // top and a single exit at the bottom. 7807 // The point of exit cannot be a branch out of the structured block. 7808 // longjmp() and throw() must not violate the entry/exit criteria. 7809 CS->getCapturedDecl()->setNothrow(); 7810 7811 for (int ThisCaptureLevel = 7812 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 7813 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7814 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7815 // 1.2.2 OpenMP Language Terminology 7816 // Structured block - An executable statement with a single entry at the 7817 // top and a single exit at the bottom. 7818 // The point of exit cannot be a branch out of the structured block. 7819 // longjmp() and throw() must not violate the entry/exit criteria. 7820 CS->getCapturedDecl()->setNothrow(); 7821 } 7822 7823 OMPLoopDirective::HelperExprs B; 7824 // In presence of clause 'collapse' with number of loops, it will 7825 // define the nested loops number. 7826 unsigned NestedLoopCount = checkOpenMPLoop( 7827 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 7828 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7829 VarsWithImplicitDSA, B); 7830 7831 if (NestedLoopCount == 0) 7832 return StmtError(); 7833 7834 assert((CurContext->isDependentContext() || B.builtAll()) && 7835 "omp for loop exprs were not built"); 7836 7837 setFunctionHasBranchProtectedScope(); 7838 7839 DSAStack->setParentTeamsRegionLoc(StartLoc); 7840 7841 return OMPTeamsDistributeParallelForDirective::Create( 7842 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 7843 DSAStack->isCancelRegion()); 7844 } 7845 7846 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 7847 Stmt *AStmt, 7848 SourceLocation StartLoc, 7849 SourceLocation EndLoc) { 7850 if (!AStmt) 7851 return StmtError(); 7852 7853 auto *CS = cast<CapturedStmt>(AStmt); 7854 // 1.2.2 OpenMP Language Terminology 7855 // Structured block - An executable statement with a single entry at the 7856 // top and a single exit at the bottom. 7857 // The point of exit cannot be a branch out of the structured block. 7858 // longjmp() and throw() must not violate the entry/exit criteria. 7859 CS->getCapturedDecl()->setNothrow(); 7860 7861 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 7862 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7863 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7864 // 1.2.2 OpenMP Language Terminology 7865 // Structured block - An executable statement with a single entry at the 7866 // top and a single exit at the bottom. 7867 // The point of exit cannot be a branch out of the structured block. 7868 // longjmp() and throw() must not violate the entry/exit criteria. 7869 CS->getCapturedDecl()->setNothrow(); 7870 } 7871 setFunctionHasBranchProtectedScope(); 7872 7873 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 7874 AStmt); 7875 } 7876 7877 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 7878 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7879 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7880 if (!AStmt) 7881 return StmtError(); 7882 7883 auto *CS = cast<CapturedStmt>(AStmt); 7884 // 1.2.2 OpenMP Language Terminology 7885 // Structured block - An executable statement with a single entry at the 7886 // top and a single exit at the bottom. 7887 // The point of exit cannot be a branch out of the structured block. 7888 // longjmp() and throw() must not violate the entry/exit criteria. 7889 CS->getCapturedDecl()->setNothrow(); 7890 for (int ThisCaptureLevel = 7891 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 7892 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7893 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7894 // 1.2.2 OpenMP Language Terminology 7895 // Structured block - An executable statement with a single entry at the 7896 // top and a single exit at the bottom. 7897 // The point of exit cannot be a branch out of the structured block. 7898 // longjmp() and throw() must not violate the entry/exit criteria. 7899 CS->getCapturedDecl()->setNothrow(); 7900 } 7901 7902 OMPLoopDirective::HelperExprs B; 7903 // In presence of clause 'collapse' with number of loops, it will 7904 // define the nested loops number. 7905 unsigned NestedLoopCount = checkOpenMPLoop( 7906 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 7907 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7908 VarsWithImplicitDSA, B); 7909 if (NestedLoopCount == 0) 7910 return StmtError(); 7911 7912 assert((CurContext->isDependentContext() || B.builtAll()) && 7913 "omp target teams distribute loop exprs were not built"); 7914 7915 setFunctionHasBranchProtectedScope(); 7916 return OMPTargetTeamsDistributeDirective::Create( 7917 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7918 } 7919 7920 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 7921 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7922 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7923 if (!AStmt) 7924 return StmtError(); 7925 7926 auto *CS = cast<CapturedStmt>(AStmt); 7927 // 1.2.2 OpenMP Language Terminology 7928 // Structured block - An executable statement with a single entry at the 7929 // top and a single exit at the bottom. 7930 // The point of exit cannot be a branch out of the structured block. 7931 // longjmp() and throw() must not violate the entry/exit criteria. 7932 CS->getCapturedDecl()->setNothrow(); 7933 for (int ThisCaptureLevel = 7934 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 7935 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7936 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7937 // 1.2.2 OpenMP Language Terminology 7938 // Structured block - An executable statement with a single entry at the 7939 // top and a single exit at the bottom. 7940 // The point of exit cannot be a branch out of the structured block. 7941 // longjmp() and throw() must not violate the entry/exit criteria. 7942 CS->getCapturedDecl()->setNothrow(); 7943 } 7944 7945 OMPLoopDirective::HelperExprs B; 7946 // In presence of clause 'collapse' with number of loops, it will 7947 // define the nested loops number. 7948 unsigned NestedLoopCount = checkOpenMPLoop( 7949 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 7950 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 7951 VarsWithImplicitDSA, B); 7952 if (NestedLoopCount == 0) 7953 return StmtError(); 7954 7955 assert((CurContext->isDependentContext() || B.builtAll()) && 7956 "omp target teams distribute parallel for loop exprs were not built"); 7957 7958 if (!CurContext->isDependentContext()) { 7959 // Finalize the clauses that need pre-built expressions for CodeGen. 7960 for (OMPClause *C : Clauses) { 7961 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7962 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7963 B.NumIterations, *this, CurScope, 7964 DSAStack)) 7965 return StmtError(); 7966 } 7967 } 7968 7969 setFunctionHasBranchProtectedScope(); 7970 return OMPTargetTeamsDistributeParallelForDirective::Create( 7971 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 7972 DSAStack->isCancelRegion()); 7973 } 7974 7975 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 7976 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7977 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7978 if (!AStmt) 7979 return StmtError(); 7980 7981 auto *CS = cast<CapturedStmt>(AStmt); 7982 // 1.2.2 OpenMP Language Terminology 7983 // Structured block - An executable statement with a single entry at the 7984 // top and a single exit at the bottom. 7985 // The point of exit cannot be a branch out of the structured block. 7986 // longjmp() and throw() must not violate the entry/exit criteria. 7987 CS->getCapturedDecl()->setNothrow(); 7988 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 7989 OMPD_target_teams_distribute_parallel_for_simd); 7990 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7991 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7992 // 1.2.2 OpenMP Language Terminology 7993 // Structured block - An executable statement with a single entry at the 7994 // top and a single exit at the bottom. 7995 // The point of exit cannot be a branch out of the structured block. 7996 // longjmp() and throw() must not violate the entry/exit criteria. 7997 CS->getCapturedDecl()->setNothrow(); 7998 } 7999 8000 OMPLoopDirective::HelperExprs B; 8001 // In presence of clause 'collapse' with number of loops, it will 8002 // define the nested loops number. 8003 unsigned NestedLoopCount = 8004 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 8005 getCollapseNumberExpr(Clauses), 8006 nullptr /*ordered not a clause on distribute*/, CS, *this, 8007 *DSAStack, VarsWithImplicitDSA, B); 8008 if (NestedLoopCount == 0) 8009 return StmtError(); 8010 8011 assert((CurContext->isDependentContext() || B.builtAll()) && 8012 "omp target teams distribute parallel for simd loop exprs were not " 8013 "built"); 8014 8015 if (!CurContext->isDependentContext()) { 8016 // Finalize the clauses that need pre-built expressions for CodeGen. 8017 for (OMPClause *C : Clauses) { 8018 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8019 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8020 B.NumIterations, *this, CurScope, 8021 DSAStack)) 8022 return StmtError(); 8023 } 8024 } 8025 8026 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8027 return StmtError(); 8028 8029 setFunctionHasBranchProtectedScope(); 8030 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 8031 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8032 } 8033 8034 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 8035 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8036 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8037 if (!AStmt) 8038 return StmtError(); 8039 8040 auto *CS = cast<CapturedStmt>(AStmt); 8041 // 1.2.2 OpenMP Language Terminology 8042 // Structured block - An executable statement with a single entry at the 8043 // top and a single exit at the bottom. 8044 // The point of exit cannot be a branch out of the structured block. 8045 // longjmp() and throw() must not violate the entry/exit criteria. 8046 CS->getCapturedDecl()->setNothrow(); 8047 for (int ThisCaptureLevel = 8048 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 8049 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8050 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8051 // 1.2.2 OpenMP Language Terminology 8052 // Structured block - An executable statement with a single entry at the 8053 // top and a single exit at the bottom. 8054 // The point of exit cannot be a branch out of the structured block. 8055 // longjmp() and throw() must not violate the entry/exit criteria. 8056 CS->getCapturedDecl()->setNothrow(); 8057 } 8058 8059 OMPLoopDirective::HelperExprs B; 8060 // In presence of clause 'collapse' with number of loops, it will 8061 // define the nested loops number. 8062 unsigned NestedLoopCount = checkOpenMPLoop( 8063 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 8064 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8065 VarsWithImplicitDSA, B); 8066 if (NestedLoopCount == 0) 8067 return StmtError(); 8068 8069 assert((CurContext->isDependentContext() || B.builtAll()) && 8070 "omp target teams distribute simd loop exprs were not built"); 8071 8072 if (!CurContext->isDependentContext()) { 8073 // Finalize the clauses that need pre-built expressions for CodeGen. 8074 for (OMPClause *C : Clauses) { 8075 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8076 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8077 B.NumIterations, *this, CurScope, 8078 DSAStack)) 8079 return StmtError(); 8080 } 8081 } 8082 8083 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8084 return StmtError(); 8085 8086 setFunctionHasBranchProtectedScope(); 8087 return OMPTargetTeamsDistributeSimdDirective::Create( 8088 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8089 } 8090 8091 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 8092 SourceLocation StartLoc, 8093 SourceLocation LParenLoc, 8094 SourceLocation EndLoc) { 8095 OMPClause *Res = nullptr; 8096 switch (Kind) { 8097 case OMPC_final: 8098 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 8099 break; 8100 case OMPC_num_threads: 8101 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 8102 break; 8103 case OMPC_safelen: 8104 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 8105 break; 8106 case OMPC_simdlen: 8107 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 8108 break; 8109 case OMPC_collapse: 8110 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 8111 break; 8112 case OMPC_ordered: 8113 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 8114 break; 8115 case OMPC_device: 8116 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc); 8117 break; 8118 case OMPC_num_teams: 8119 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 8120 break; 8121 case OMPC_thread_limit: 8122 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 8123 break; 8124 case OMPC_priority: 8125 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 8126 break; 8127 case OMPC_grainsize: 8128 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 8129 break; 8130 case OMPC_num_tasks: 8131 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 8132 break; 8133 case OMPC_hint: 8134 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 8135 break; 8136 case OMPC_if: 8137 case OMPC_default: 8138 case OMPC_proc_bind: 8139 case OMPC_schedule: 8140 case OMPC_private: 8141 case OMPC_firstprivate: 8142 case OMPC_lastprivate: 8143 case OMPC_shared: 8144 case OMPC_reduction: 8145 case OMPC_task_reduction: 8146 case OMPC_in_reduction: 8147 case OMPC_linear: 8148 case OMPC_aligned: 8149 case OMPC_copyin: 8150 case OMPC_copyprivate: 8151 case OMPC_nowait: 8152 case OMPC_untied: 8153 case OMPC_mergeable: 8154 case OMPC_threadprivate: 8155 case OMPC_flush: 8156 case OMPC_read: 8157 case OMPC_write: 8158 case OMPC_update: 8159 case OMPC_capture: 8160 case OMPC_seq_cst: 8161 case OMPC_depend: 8162 case OMPC_threads: 8163 case OMPC_simd: 8164 case OMPC_map: 8165 case OMPC_nogroup: 8166 case OMPC_dist_schedule: 8167 case OMPC_defaultmap: 8168 case OMPC_unknown: 8169 case OMPC_uniform: 8170 case OMPC_to: 8171 case OMPC_from: 8172 case OMPC_use_device_ptr: 8173 case OMPC_is_device_ptr: 8174 case OMPC_unified_address: 8175 case OMPC_unified_shared_memory: 8176 case OMPC_reverse_offload: 8177 case OMPC_dynamic_allocators: 8178 case OMPC_atomic_default_mem_order: 8179 llvm_unreachable("Clause is not allowed."); 8180 } 8181 return Res; 8182 } 8183 8184 // An OpenMP directive such as 'target parallel' has two captured regions: 8185 // for the 'target' and 'parallel' respectively. This function returns 8186 // the region in which to capture expressions associated with a clause. 8187 // A return value of OMPD_unknown signifies that the expression should not 8188 // be captured. 8189 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 8190 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 8191 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 8192 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 8193 switch (CKind) { 8194 case OMPC_if: 8195 switch (DKind) { 8196 case OMPD_target_parallel: 8197 case OMPD_target_parallel_for: 8198 case OMPD_target_parallel_for_simd: 8199 // If this clause applies to the nested 'parallel' region, capture within 8200 // the 'target' region, otherwise do not capture. 8201 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 8202 CaptureRegion = OMPD_target; 8203 break; 8204 case OMPD_target_teams_distribute_parallel_for: 8205 case OMPD_target_teams_distribute_parallel_for_simd: 8206 // If this clause applies to the nested 'parallel' region, capture within 8207 // the 'teams' region, otherwise do not capture. 8208 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 8209 CaptureRegion = OMPD_teams; 8210 break; 8211 case OMPD_teams_distribute_parallel_for: 8212 case OMPD_teams_distribute_parallel_for_simd: 8213 CaptureRegion = OMPD_teams; 8214 break; 8215 case OMPD_target_update: 8216 case OMPD_target_enter_data: 8217 case OMPD_target_exit_data: 8218 CaptureRegion = OMPD_task; 8219 break; 8220 case OMPD_cancel: 8221 case OMPD_parallel: 8222 case OMPD_parallel_sections: 8223 case OMPD_parallel_for: 8224 case OMPD_parallel_for_simd: 8225 case OMPD_target: 8226 case OMPD_target_simd: 8227 case OMPD_target_teams: 8228 case OMPD_target_teams_distribute: 8229 case OMPD_target_teams_distribute_simd: 8230 case OMPD_distribute_parallel_for: 8231 case OMPD_distribute_parallel_for_simd: 8232 case OMPD_task: 8233 case OMPD_taskloop: 8234 case OMPD_taskloop_simd: 8235 case OMPD_target_data: 8236 // Do not capture if-clause expressions. 8237 break; 8238 case OMPD_threadprivate: 8239 case OMPD_taskyield: 8240 case OMPD_barrier: 8241 case OMPD_taskwait: 8242 case OMPD_cancellation_point: 8243 case OMPD_flush: 8244 case OMPD_declare_reduction: 8245 case OMPD_declare_simd: 8246 case OMPD_declare_target: 8247 case OMPD_end_declare_target: 8248 case OMPD_teams: 8249 case OMPD_simd: 8250 case OMPD_for: 8251 case OMPD_for_simd: 8252 case OMPD_sections: 8253 case OMPD_section: 8254 case OMPD_single: 8255 case OMPD_master: 8256 case OMPD_critical: 8257 case OMPD_taskgroup: 8258 case OMPD_distribute: 8259 case OMPD_ordered: 8260 case OMPD_atomic: 8261 case OMPD_distribute_simd: 8262 case OMPD_teams_distribute: 8263 case OMPD_teams_distribute_simd: 8264 case OMPD_requires: 8265 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 8266 case OMPD_unknown: 8267 llvm_unreachable("Unknown OpenMP directive"); 8268 } 8269 break; 8270 case OMPC_num_threads: 8271 switch (DKind) { 8272 case OMPD_target_parallel: 8273 case OMPD_target_parallel_for: 8274 case OMPD_target_parallel_for_simd: 8275 CaptureRegion = OMPD_target; 8276 break; 8277 case OMPD_teams_distribute_parallel_for: 8278 case OMPD_teams_distribute_parallel_for_simd: 8279 case OMPD_target_teams_distribute_parallel_for: 8280 case OMPD_target_teams_distribute_parallel_for_simd: 8281 CaptureRegion = OMPD_teams; 8282 break; 8283 case OMPD_parallel: 8284 case OMPD_parallel_sections: 8285 case OMPD_parallel_for: 8286 case OMPD_parallel_for_simd: 8287 case OMPD_distribute_parallel_for: 8288 case OMPD_distribute_parallel_for_simd: 8289 // Do not capture num_threads-clause expressions. 8290 break; 8291 case OMPD_target_data: 8292 case OMPD_target_enter_data: 8293 case OMPD_target_exit_data: 8294 case OMPD_target_update: 8295 case OMPD_target: 8296 case OMPD_target_simd: 8297 case OMPD_target_teams: 8298 case OMPD_target_teams_distribute: 8299 case OMPD_target_teams_distribute_simd: 8300 case OMPD_cancel: 8301 case OMPD_task: 8302 case OMPD_taskloop: 8303 case OMPD_taskloop_simd: 8304 case OMPD_threadprivate: 8305 case OMPD_taskyield: 8306 case OMPD_barrier: 8307 case OMPD_taskwait: 8308 case OMPD_cancellation_point: 8309 case OMPD_flush: 8310 case OMPD_declare_reduction: 8311 case OMPD_declare_simd: 8312 case OMPD_declare_target: 8313 case OMPD_end_declare_target: 8314 case OMPD_teams: 8315 case OMPD_simd: 8316 case OMPD_for: 8317 case OMPD_for_simd: 8318 case OMPD_sections: 8319 case OMPD_section: 8320 case OMPD_single: 8321 case OMPD_master: 8322 case OMPD_critical: 8323 case OMPD_taskgroup: 8324 case OMPD_distribute: 8325 case OMPD_ordered: 8326 case OMPD_atomic: 8327 case OMPD_distribute_simd: 8328 case OMPD_teams_distribute: 8329 case OMPD_teams_distribute_simd: 8330 case OMPD_requires: 8331 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 8332 case OMPD_unknown: 8333 llvm_unreachable("Unknown OpenMP directive"); 8334 } 8335 break; 8336 case OMPC_num_teams: 8337 switch (DKind) { 8338 case OMPD_target_teams: 8339 case OMPD_target_teams_distribute: 8340 case OMPD_target_teams_distribute_simd: 8341 case OMPD_target_teams_distribute_parallel_for: 8342 case OMPD_target_teams_distribute_parallel_for_simd: 8343 CaptureRegion = OMPD_target; 8344 break; 8345 case OMPD_teams_distribute_parallel_for: 8346 case OMPD_teams_distribute_parallel_for_simd: 8347 case OMPD_teams: 8348 case OMPD_teams_distribute: 8349 case OMPD_teams_distribute_simd: 8350 // Do not capture num_teams-clause expressions. 8351 break; 8352 case OMPD_distribute_parallel_for: 8353 case OMPD_distribute_parallel_for_simd: 8354 case OMPD_task: 8355 case OMPD_taskloop: 8356 case OMPD_taskloop_simd: 8357 case OMPD_target_data: 8358 case OMPD_target_enter_data: 8359 case OMPD_target_exit_data: 8360 case OMPD_target_update: 8361 case OMPD_cancel: 8362 case OMPD_parallel: 8363 case OMPD_parallel_sections: 8364 case OMPD_parallel_for: 8365 case OMPD_parallel_for_simd: 8366 case OMPD_target: 8367 case OMPD_target_simd: 8368 case OMPD_target_parallel: 8369 case OMPD_target_parallel_for: 8370 case OMPD_target_parallel_for_simd: 8371 case OMPD_threadprivate: 8372 case OMPD_taskyield: 8373 case OMPD_barrier: 8374 case OMPD_taskwait: 8375 case OMPD_cancellation_point: 8376 case OMPD_flush: 8377 case OMPD_declare_reduction: 8378 case OMPD_declare_simd: 8379 case OMPD_declare_target: 8380 case OMPD_end_declare_target: 8381 case OMPD_simd: 8382 case OMPD_for: 8383 case OMPD_for_simd: 8384 case OMPD_sections: 8385 case OMPD_section: 8386 case OMPD_single: 8387 case OMPD_master: 8388 case OMPD_critical: 8389 case OMPD_taskgroup: 8390 case OMPD_distribute: 8391 case OMPD_ordered: 8392 case OMPD_atomic: 8393 case OMPD_distribute_simd: 8394 case OMPD_requires: 8395 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 8396 case OMPD_unknown: 8397 llvm_unreachable("Unknown OpenMP directive"); 8398 } 8399 break; 8400 case OMPC_thread_limit: 8401 switch (DKind) { 8402 case OMPD_target_teams: 8403 case OMPD_target_teams_distribute: 8404 case OMPD_target_teams_distribute_simd: 8405 case OMPD_target_teams_distribute_parallel_for: 8406 case OMPD_target_teams_distribute_parallel_for_simd: 8407 CaptureRegion = OMPD_target; 8408 break; 8409 case OMPD_teams_distribute_parallel_for: 8410 case OMPD_teams_distribute_parallel_for_simd: 8411 case OMPD_teams: 8412 case OMPD_teams_distribute: 8413 case OMPD_teams_distribute_simd: 8414 // Do not capture thread_limit-clause expressions. 8415 break; 8416 case OMPD_distribute_parallel_for: 8417 case OMPD_distribute_parallel_for_simd: 8418 case OMPD_task: 8419 case OMPD_taskloop: 8420 case OMPD_taskloop_simd: 8421 case OMPD_target_data: 8422 case OMPD_target_enter_data: 8423 case OMPD_target_exit_data: 8424 case OMPD_target_update: 8425 case OMPD_cancel: 8426 case OMPD_parallel: 8427 case OMPD_parallel_sections: 8428 case OMPD_parallel_for: 8429 case OMPD_parallel_for_simd: 8430 case OMPD_target: 8431 case OMPD_target_simd: 8432 case OMPD_target_parallel: 8433 case OMPD_target_parallel_for: 8434 case OMPD_target_parallel_for_simd: 8435 case OMPD_threadprivate: 8436 case OMPD_taskyield: 8437 case OMPD_barrier: 8438 case OMPD_taskwait: 8439 case OMPD_cancellation_point: 8440 case OMPD_flush: 8441 case OMPD_declare_reduction: 8442 case OMPD_declare_simd: 8443 case OMPD_declare_target: 8444 case OMPD_end_declare_target: 8445 case OMPD_simd: 8446 case OMPD_for: 8447 case OMPD_for_simd: 8448 case OMPD_sections: 8449 case OMPD_section: 8450 case OMPD_single: 8451 case OMPD_master: 8452 case OMPD_critical: 8453 case OMPD_taskgroup: 8454 case OMPD_distribute: 8455 case OMPD_ordered: 8456 case OMPD_atomic: 8457 case OMPD_distribute_simd: 8458 case OMPD_requires: 8459 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 8460 case OMPD_unknown: 8461 llvm_unreachable("Unknown OpenMP directive"); 8462 } 8463 break; 8464 case OMPC_schedule: 8465 switch (DKind) { 8466 case OMPD_parallel_for: 8467 case OMPD_parallel_for_simd: 8468 case OMPD_distribute_parallel_for: 8469 case OMPD_distribute_parallel_for_simd: 8470 case OMPD_teams_distribute_parallel_for: 8471 case OMPD_teams_distribute_parallel_for_simd: 8472 case OMPD_target_parallel_for: 8473 case OMPD_target_parallel_for_simd: 8474 case OMPD_target_teams_distribute_parallel_for: 8475 case OMPD_target_teams_distribute_parallel_for_simd: 8476 CaptureRegion = OMPD_parallel; 8477 break; 8478 case OMPD_for: 8479 case OMPD_for_simd: 8480 // Do not capture schedule-clause expressions. 8481 break; 8482 case OMPD_task: 8483 case OMPD_taskloop: 8484 case OMPD_taskloop_simd: 8485 case OMPD_target_data: 8486 case OMPD_target_enter_data: 8487 case OMPD_target_exit_data: 8488 case OMPD_target_update: 8489 case OMPD_teams: 8490 case OMPD_teams_distribute: 8491 case OMPD_teams_distribute_simd: 8492 case OMPD_target_teams_distribute: 8493 case OMPD_target_teams_distribute_simd: 8494 case OMPD_target: 8495 case OMPD_target_simd: 8496 case OMPD_target_parallel: 8497 case OMPD_cancel: 8498 case OMPD_parallel: 8499 case OMPD_parallel_sections: 8500 case OMPD_threadprivate: 8501 case OMPD_taskyield: 8502 case OMPD_barrier: 8503 case OMPD_taskwait: 8504 case OMPD_cancellation_point: 8505 case OMPD_flush: 8506 case OMPD_declare_reduction: 8507 case OMPD_declare_simd: 8508 case OMPD_declare_target: 8509 case OMPD_end_declare_target: 8510 case OMPD_simd: 8511 case OMPD_sections: 8512 case OMPD_section: 8513 case OMPD_single: 8514 case OMPD_master: 8515 case OMPD_critical: 8516 case OMPD_taskgroup: 8517 case OMPD_distribute: 8518 case OMPD_ordered: 8519 case OMPD_atomic: 8520 case OMPD_distribute_simd: 8521 case OMPD_target_teams: 8522 case OMPD_requires: 8523 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 8524 case OMPD_unknown: 8525 llvm_unreachable("Unknown OpenMP directive"); 8526 } 8527 break; 8528 case OMPC_dist_schedule: 8529 switch (DKind) { 8530 case OMPD_teams_distribute_parallel_for: 8531 case OMPD_teams_distribute_parallel_for_simd: 8532 case OMPD_teams_distribute: 8533 case OMPD_teams_distribute_simd: 8534 case OMPD_target_teams_distribute_parallel_for: 8535 case OMPD_target_teams_distribute_parallel_for_simd: 8536 case OMPD_target_teams_distribute: 8537 case OMPD_target_teams_distribute_simd: 8538 CaptureRegion = OMPD_teams; 8539 break; 8540 case OMPD_distribute_parallel_for: 8541 case OMPD_distribute_parallel_for_simd: 8542 case OMPD_distribute: 8543 case OMPD_distribute_simd: 8544 // Do not capture thread_limit-clause expressions. 8545 break; 8546 case OMPD_parallel_for: 8547 case OMPD_parallel_for_simd: 8548 case OMPD_target_parallel_for_simd: 8549 case OMPD_target_parallel_for: 8550 case OMPD_task: 8551 case OMPD_taskloop: 8552 case OMPD_taskloop_simd: 8553 case OMPD_target_data: 8554 case OMPD_target_enter_data: 8555 case OMPD_target_exit_data: 8556 case OMPD_target_update: 8557 case OMPD_teams: 8558 case OMPD_target: 8559 case OMPD_target_simd: 8560 case OMPD_target_parallel: 8561 case OMPD_cancel: 8562 case OMPD_parallel: 8563 case OMPD_parallel_sections: 8564 case OMPD_threadprivate: 8565 case OMPD_taskyield: 8566 case OMPD_barrier: 8567 case OMPD_taskwait: 8568 case OMPD_cancellation_point: 8569 case OMPD_flush: 8570 case OMPD_declare_reduction: 8571 case OMPD_declare_simd: 8572 case OMPD_declare_target: 8573 case OMPD_end_declare_target: 8574 case OMPD_simd: 8575 case OMPD_for: 8576 case OMPD_for_simd: 8577 case OMPD_sections: 8578 case OMPD_section: 8579 case OMPD_single: 8580 case OMPD_master: 8581 case OMPD_critical: 8582 case OMPD_taskgroup: 8583 case OMPD_ordered: 8584 case OMPD_atomic: 8585 case OMPD_target_teams: 8586 case OMPD_requires: 8587 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 8588 case OMPD_unknown: 8589 llvm_unreachable("Unknown OpenMP directive"); 8590 } 8591 break; 8592 case OMPC_device: 8593 switch (DKind) { 8594 case OMPD_target_update: 8595 case OMPD_target_enter_data: 8596 case OMPD_target_exit_data: 8597 case OMPD_target: 8598 case OMPD_target_simd: 8599 case OMPD_target_teams: 8600 case OMPD_target_parallel: 8601 case OMPD_target_teams_distribute: 8602 case OMPD_target_teams_distribute_simd: 8603 case OMPD_target_parallel_for: 8604 case OMPD_target_parallel_for_simd: 8605 case OMPD_target_teams_distribute_parallel_for: 8606 case OMPD_target_teams_distribute_parallel_for_simd: 8607 CaptureRegion = OMPD_task; 8608 break; 8609 case OMPD_target_data: 8610 // Do not capture device-clause expressions. 8611 break; 8612 case OMPD_teams_distribute_parallel_for: 8613 case OMPD_teams_distribute_parallel_for_simd: 8614 case OMPD_teams: 8615 case OMPD_teams_distribute: 8616 case OMPD_teams_distribute_simd: 8617 case OMPD_distribute_parallel_for: 8618 case OMPD_distribute_parallel_for_simd: 8619 case OMPD_task: 8620 case OMPD_taskloop: 8621 case OMPD_taskloop_simd: 8622 case OMPD_cancel: 8623 case OMPD_parallel: 8624 case OMPD_parallel_sections: 8625 case OMPD_parallel_for: 8626 case OMPD_parallel_for_simd: 8627 case OMPD_threadprivate: 8628 case OMPD_taskyield: 8629 case OMPD_barrier: 8630 case OMPD_taskwait: 8631 case OMPD_cancellation_point: 8632 case OMPD_flush: 8633 case OMPD_declare_reduction: 8634 case OMPD_declare_simd: 8635 case OMPD_declare_target: 8636 case OMPD_end_declare_target: 8637 case OMPD_simd: 8638 case OMPD_for: 8639 case OMPD_for_simd: 8640 case OMPD_sections: 8641 case OMPD_section: 8642 case OMPD_single: 8643 case OMPD_master: 8644 case OMPD_critical: 8645 case OMPD_taskgroup: 8646 case OMPD_distribute: 8647 case OMPD_ordered: 8648 case OMPD_atomic: 8649 case OMPD_distribute_simd: 8650 case OMPD_requires: 8651 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 8652 case OMPD_unknown: 8653 llvm_unreachable("Unknown OpenMP directive"); 8654 } 8655 break; 8656 case OMPC_firstprivate: 8657 case OMPC_lastprivate: 8658 case OMPC_reduction: 8659 case OMPC_task_reduction: 8660 case OMPC_in_reduction: 8661 case OMPC_linear: 8662 case OMPC_default: 8663 case OMPC_proc_bind: 8664 case OMPC_final: 8665 case OMPC_safelen: 8666 case OMPC_simdlen: 8667 case OMPC_collapse: 8668 case OMPC_private: 8669 case OMPC_shared: 8670 case OMPC_aligned: 8671 case OMPC_copyin: 8672 case OMPC_copyprivate: 8673 case OMPC_ordered: 8674 case OMPC_nowait: 8675 case OMPC_untied: 8676 case OMPC_mergeable: 8677 case OMPC_threadprivate: 8678 case OMPC_flush: 8679 case OMPC_read: 8680 case OMPC_write: 8681 case OMPC_update: 8682 case OMPC_capture: 8683 case OMPC_seq_cst: 8684 case OMPC_depend: 8685 case OMPC_threads: 8686 case OMPC_simd: 8687 case OMPC_map: 8688 case OMPC_priority: 8689 case OMPC_grainsize: 8690 case OMPC_nogroup: 8691 case OMPC_num_tasks: 8692 case OMPC_hint: 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 case OMPC_unified_address: 8701 case OMPC_unified_shared_memory: 8702 case OMPC_reverse_offload: 8703 case OMPC_dynamic_allocators: 8704 case OMPC_atomic_default_mem_order: 8705 llvm_unreachable("Unexpected OpenMP clause."); 8706 } 8707 return CaptureRegion; 8708 } 8709 8710 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 8711 Expr *Condition, SourceLocation StartLoc, 8712 SourceLocation LParenLoc, 8713 SourceLocation NameModifierLoc, 8714 SourceLocation ColonLoc, 8715 SourceLocation EndLoc) { 8716 Expr *ValExpr = Condition; 8717 Stmt *HelperValStmt = nullptr; 8718 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 8719 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 8720 !Condition->isInstantiationDependent() && 8721 !Condition->containsUnexpandedParameterPack()) { 8722 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 8723 if (Val.isInvalid()) 8724 return nullptr; 8725 8726 ValExpr = Val.get(); 8727 8728 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 8729 CaptureRegion = 8730 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier); 8731 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 8732 ValExpr = MakeFullExpr(ValExpr).get(); 8733 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 8734 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 8735 HelperValStmt = buildPreInits(Context, Captures); 8736 } 8737 } 8738 8739 return new (Context) 8740 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 8741 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 8742 } 8743 8744 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 8745 SourceLocation StartLoc, 8746 SourceLocation LParenLoc, 8747 SourceLocation EndLoc) { 8748 Expr *ValExpr = Condition; 8749 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 8750 !Condition->isInstantiationDependent() && 8751 !Condition->containsUnexpandedParameterPack()) { 8752 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 8753 if (Val.isInvalid()) 8754 return nullptr; 8755 8756 ValExpr = MakeFullExpr(Val.get()).get(); 8757 } 8758 8759 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc); 8760 } 8761 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 8762 Expr *Op) { 8763 if (!Op) 8764 return ExprError(); 8765 8766 class IntConvertDiagnoser : public ICEConvertDiagnoser { 8767 public: 8768 IntConvertDiagnoser() 8769 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 8770 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 8771 QualType T) override { 8772 return S.Diag(Loc, diag::err_omp_not_integral) << T; 8773 } 8774 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 8775 QualType T) override { 8776 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 8777 } 8778 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 8779 QualType T, 8780 QualType ConvTy) override { 8781 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 8782 } 8783 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 8784 QualType ConvTy) override { 8785 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 8786 << ConvTy->isEnumeralType() << ConvTy; 8787 } 8788 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 8789 QualType T) override { 8790 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 8791 } 8792 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 8793 QualType ConvTy) override { 8794 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 8795 << ConvTy->isEnumeralType() << ConvTy; 8796 } 8797 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 8798 QualType) override { 8799 llvm_unreachable("conversion functions are permitted"); 8800 } 8801 } ConvertDiagnoser; 8802 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 8803 } 8804 8805 static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, 8806 OpenMPClauseKind CKind, 8807 bool StrictlyPositive) { 8808 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 8809 !ValExpr->isInstantiationDependent()) { 8810 SourceLocation Loc = ValExpr->getExprLoc(); 8811 ExprResult Value = 8812 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 8813 if (Value.isInvalid()) 8814 return false; 8815 8816 ValExpr = Value.get(); 8817 // The expression must evaluate to a non-negative integer value. 8818 llvm::APSInt Result; 8819 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) && 8820 Result.isSigned() && 8821 !((!StrictlyPositive && Result.isNonNegative()) || 8822 (StrictlyPositive && Result.isStrictlyPositive()))) { 8823 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 8824 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 8825 << ValExpr->getSourceRange(); 8826 return false; 8827 } 8828 } 8829 return true; 8830 } 8831 8832 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 8833 SourceLocation StartLoc, 8834 SourceLocation LParenLoc, 8835 SourceLocation EndLoc) { 8836 Expr *ValExpr = NumThreads; 8837 Stmt *HelperValStmt = nullptr; 8838 8839 // OpenMP [2.5, Restrictions] 8840 // The num_threads expression must evaluate to a positive integer value. 8841 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 8842 /*StrictlyPositive=*/true)) 8843 return nullptr; 8844 8845 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 8846 OpenMPDirectiveKind CaptureRegion = 8847 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads); 8848 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 8849 ValExpr = MakeFullExpr(ValExpr).get(); 8850 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 8851 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 8852 HelperValStmt = buildPreInits(Context, Captures); 8853 } 8854 8855 return new (Context) OMPNumThreadsClause( 8856 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 8857 } 8858 8859 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 8860 OpenMPClauseKind CKind, 8861 bool StrictlyPositive) { 8862 if (!E) 8863 return ExprError(); 8864 if (E->isValueDependent() || E->isTypeDependent() || 8865 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 8866 return E; 8867 llvm::APSInt Result; 8868 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); 8869 if (ICE.isInvalid()) 8870 return ExprError(); 8871 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 8872 (!StrictlyPositive && !Result.isNonNegative())) { 8873 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 8874 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 8875 << E->getSourceRange(); 8876 return ExprError(); 8877 } 8878 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 8879 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 8880 << E->getSourceRange(); 8881 return ExprError(); 8882 } 8883 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 8884 DSAStack->setAssociatedLoops(Result.getExtValue()); 8885 else if (CKind == OMPC_ordered) 8886 DSAStack->setAssociatedLoops(Result.getExtValue()); 8887 return ICE; 8888 } 8889 8890 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 8891 SourceLocation LParenLoc, 8892 SourceLocation EndLoc) { 8893 // OpenMP [2.8.1, simd construct, Description] 8894 // The parameter of the safelen clause must be a constant 8895 // positive integer expression. 8896 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 8897 if (Safelen.isInvalid()) 8898 return nullptr; 8899 return new (Context) 8900 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 8901 } 8902 8903 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 8904 SourceLocation LParenLoc, 8905 SourceLocation EndLoc) { 8906 // OpenMP [2.8.1, simd construct, Description] 8907 // The parameter of the simdlen clause must be a constant 8908 // positive integer expression. 8909 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 8910 if (Simdlen.isInvalid()) 8911 return nullptr; 8912 return new (Context) 8913 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 8914 } 8915 8916 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 8917 SourceLocation StartLoc, 8918 SourceLocation LParenLoc, 8919 SourceLocation EndLoc) { 8920 // OpenMP [2.7.1, loop construct, Description] 8921 // OpenMP [2.8.1, simd construct, Description] 8922 // OpenMP [2.9.6, distribute construct, Description] 8923 // The parameter of the collapse clause must be a constant 8924 // positive integer expression. 8925 ExprResult NumForLoopsResult = 8926 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 8927 if (NumForLoopsResult.isInvalid()) 8928 return nullptr; 8929 return new (Context) 8930 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 8931 } 8932 8933 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 8934 SourceLocation EndLoc, 8935 SourceLocation LParenLoc, 8936 Expr *NumForLoops) { 8937 // OpenMP [2.7.1, loop construct, Description] 8938 // OpenMP [2.8.1, simd construct, Description] 8939 // OpenMP [2.9.6, distribute construct, Description] 8940 // The parameter of the ordered clause must be a constant 8941 // positive integer expression if any. 8942 if (NumForLoops && LParenLoc.isValid()) { 8943 ExprResult NumForLoopsResult = 8944 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 8945 if (NumForLoopsResult.isInvalid()) 8946 return nullptr; 8947 NumForLoops = NumForLoopsResult.get(); 8948 } else { 8949 NumForLoops = nullptr; 8950 } 8951 auto *Clause = OMPOrderedClause::Create( 8952 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0, 8953 StartLoc, LParenLoc, EndLoc); 8954 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause); 8955 return Clause; 8956 } 8957 8958 OMPClause *Sema::ActOnOpenMPSimpleClause( 8959 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 8960 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 8961 OMPClause *Res = nullptr; 8962 switch (Kind) { 8963 case OMPC_default: 8964 Res = 8965 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), 8966 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 8967 break; 8968 case OMPC_proc_bind: 8969 Res = ActOnOpenMPProcBindClause( 8970 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, 8971 LParenLoc, EndLoc); 8972 break; 8973 case OMPC_atomic_default_mem_order: 8974 Res = ActOnOpenMPAtomicDefaultMemOrderClause( 8975 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument), 8976 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 8977 break; 8978 case OMPC_if: 8979 case OMPC_final: 8980 case OMPC_num_threads: 8981 case OMPC_safelen: 8982 case OMPC_simdlen: 8983 case OMPC_collapse: 8984 case OMPC_schedule: 8985 case OMPC_private: 8986 case OMPC_firstprivate: 8987 case OMPC_lastprivate: 8988 case OMPC_shared: 8989 case OMPC_reduction: 8990 case OMPC_task_reduction: 8991 case OMPC_in_reduction: 8992 case OMPC_linear: 8993 case OMPC_aligned: 8994 case OMPC_copyin: 8995 case OMPC_copyprivate: 8996 case OMPC_ordered: 8997 case OMPC_nowait: 8998 case OMPC_untied: 8999 case OMPC_mergeable: 9000 case OMPC_threadprivate: 9001 case OMPC_flush: 9002 case OMPC_read: 9003 case OMPC_write: 9004 case OMPC_update: 9005 case OMPC_capture: 9006 case OMPC_seq_cst: 9007 case OMPC_depend: 9008 case OMPC_device: 9009 case OMPC_threads: 9010 case OMPC_simd: 9011 case OMPC_map: 9012 case OMPC_num_teams: 9013 case OMPC_thread_limit: 9014 case OMPC_priority: 9015 case OMPC_grainsize: 9016 case OMPC_nogroup: 9017 case OMPC_num_tasks: 9018 case OMPC_hint: 9019 case OMPC_dist_schedule: 9020 case OMPC_defaultmap: 9021 case OMPC_unknown: 9022 case OMPC_uniform: 9023 case OMPC_to: 9024 case OMPC_from: 9025 case OMPC_use_device_ptr: 9026 case OMPC_is_device_ptr: 9027 case OMPC_unified_address: 9028 case OMPC_unified_shared_memory: 9029 case OMPC_reverse_offload: 9030 case OMPC_dynamic_allocators: 9031 llvm_unreachable("Clause is not allowed."); 9032 } 9033 return Res; 9034 } 9035 9036 static std::string 9037 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 9038 ArrayRef<unsigned> Exclude = llvm::None) { 9039 SmallString<256> Buffer; 9040 llvm::raw_svector_ostream Out(Buffer); 9041 unsigned Bound = Last >= 2 ? Last - 2 : 0; 9042 unsigned Skipped = Exclude.size(); 9043 auto S = Exclude.begin(), E = Exclude.end(); 9044 for (unsigned I = First; I < Last; ++I) { 9045 if (std::find(S, E, I) != E) { 9046 --Skipped; 9047 continue; 9048 } 9049 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 9050 if (I == Bound - Skipped) 9051 Out << " or "; 9052 else if (I != Bound + 1 - Skipped) 9053 Out << ", "; 9054 } 9055 return Out.str(); 9056 } 9057 9058 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, 9059 SourceLocation KindKwLoc, 9060 SourceLocation StartLoc, 9061 SourceLocation LParenLoc, 9062 SourceLocation EndLoc) { 9063 if (Kind == OMPC_DEFAULT_unknown) { 9064 static_assert(OMPC_DEFAULT_unknown > 0, 9065 "OMPC_DEFAULT_unknown not greater than 0"); 9066 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 9067 << getListOfPossibleValues(OMPC_default, /*First=*/0, 9068 /*Last=*/OMPC_DEFAULT_unknown) 9069 << getOpenMPClauseName(OMPC_default); 9070 return nullptr; 9071 } 9072 switch (Kind) { 9073 case OMPC_DEFAULT_none: 9074 DSAStack->setDefaultDSANone(KindKwLoc); 9075 break; 9076 case OMPC_DEFAULT_shared: 9077 DSAStack->setDefaultDSAShared(KindKwLoc); 9078 break; 9079 case OMPC_DEFAULT_unknown: 9080 llvm_unreachable("Clause kind is not allowed."); 9081 break; 9082 } 9083 return new (Context) 9084 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 9085 } 9086 9087 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, 9088 SourceLocation KindKwLoc, 9089 SourceLocation StartLoc, 9090 SourceLocation LParenLoc, 9091 SourceLocation EndLoc) { 9092 if (Kind == OMPC_PROC_BIND_unknown) { 9093 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 9094 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0, 9095 /*Last=*/OMPC_PROC_BIND_unknown) 9096 << getOpenMPClauseName(OMPC_proc_bind); 9097 return nullptr; 9098 } 9099 return new (Context) 9100 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 9101 } 9102 9103 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause( 9104 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc, 9105 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 9106 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) { 9107 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 9108 << getListOfPossibleValues( 9109 OMPC_atomic_default_mem_order, /*First=*/0, 9110 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) 9111 << getOpenMPClauseName(OMPC_atomic_default_mem_order); 9112 return nullptr; 9113 } 9114 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc, 9115 LParenLoc, EndLoc); 9116 } 9117 9118 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 9119 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 9120 SourceLocation StartLoc, SourceLocation LParenLoc, 9121 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 9122 SourceLocation EndLoc) { 9123 OMPClause *Res = nullptr; 9124 switch (Kind) { 9125 case OMPC_schedule: 9126 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 9127 assert(Argument.size() == NumberOfElements && 9128 ArgumentLoc.size() == NumberOfElements); 9129 Res = ActOnOpenMPScheduleClause( 9130 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 9131 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 9132 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 9133 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 9134 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 9135 break; 9136 case OMPC_if: 9137 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 9138 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 9139 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 9140 DelimLoc, EndLoc); 9141 break; 9142 case OMPC_dist_schedule: 9143 Res = ActOnOpenMPDistScheduleClause( 9144 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 9145 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 9146 break; 9147 case OMPC_defaultmap: 9148 enum { Modifier, DefaultmapKind }; 9149 Res = ActOnOpenMPDefaultmapClause( 9150 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 9151 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 9152 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 9153 EndLoc); 9154 break; 9155 case OMPC_final: 9156 case OMPC_num_threads: 9157 case OMPC_safelen: 9158 case OMPC_simdlen: 9159 case OMPC_collapse: 9160 case OMPC_default: 9161 case OMPC_proc_bind: 9162 case OMPC_private: 9163 case OMPC_firstprivate: 9164 case OMPC_lastprivate: 9165 case OMPC_shared: 9166 case OMPC_reduction: 9167 case OMPC_task_reduction: 9168 case OMPC_in_reduction: 9169 case OMPC_linear: 9170 case OMPC_aligned: 9171 case OMPC_copyin: 9172 case OMPC_copyprivate: 9173 case OMPC_ordered: 9174 case OMPC_nowait: 9175 case OMPC_untied: 9176 case OMPC_mergeable: 9177 case OMPC_threadprivate: 9178 case OMPC_flush: 9179 case OMPC_read: 9180 case OMPC_write: 9181 case OMPC_update: 9182 case OMPC_capture: 9183 case OMPC_seq_cst: 9184 case OMPC_depend: 9185 case OMPC_device: 9186 case OMPC_threads: 9187 case OMPC_simd: 9188 case OMPC_map: 9189 case OMPC_num_teams: 9190 case OMPC_thread_limit: 9191 case OMPC_priority: 9192 case OMPC_grainsize: 9193 case OMPC_nogroup: 9194 case OMPC_num_tasks: 9195 case OMPC_hint: 9196 case OMPC_unknown: 9197 case OMPC_uniform: 9198 case OMPC_to: 9199 case OMPC_from: 9200 case OMPC_use_device_ptr: 9201 case OMPC_is_device_ptr: 9202 case OMPC_unified_address: 9203 case OMPC_unified_shared_memory: 9204 case OMPC_reverse_offload: 9205 case OMPC_dynamic_allocators: 9206 case OMPC_atomic_default_mem_order: 9207 llvm_unreachable("Clause is not allowed."); 9208 } 9209 return Res; 9210 } 9211 9212 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 9213 OpenMPScheduleClauseModifier M2, 9214 SourceLocation M1Loc, SourceLocation M2Loc) { 9215 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 9216 SmallVector<unsigned, 2> Excluded; 9217 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 9218 Excluded.push_back(M2); 9219 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 9220 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 9221 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 9222 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 9223 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 9224 << getListOfPossibleValues(OMPC_schedule, 9225 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 9226 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 9227 Excluded) 9228 << getOpenMPClauseName(OMPC_schedule); 9229 return true; 9230 } 9231 return false; 9232 } 9233 9234 OMPClause *Sema::ActOnOpenMPScheduleClause( 9235 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 9236 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 9237 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 9238 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 9239 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 9240 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 9241 return nullptr; 9242 // OpenMP, 2.7.1, Loop Construct, Restrictions 9243 // Either the monotonic modifier or the nonmonotonic modifier can be specified 9244 // but not both. 9245 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 9246 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 9247 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 9248 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 9249 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 9250 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 9251 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 9252 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 9253 return nullptr; 9254 } 9255 if (Kind == OMPC_SCHEDULE_unknown) { 9256 std::string Values; 9257 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 9258 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 9259 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 9260 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 9261 Exclude); 9262 } else { 9263 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 9264 /*Last=*/OMPC_SCHEDULE_unknown); 9265 } 9266 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 9267 << Values << getOpenMPClauseName(OMPC_schedule); 9268 return nullptr; 9269 } 9270 // OpenMP, 2.7.1, Loop Construct, Restrictions 9271 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 9272 // schedule(guided). 9273 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 9274 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 9275 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 9276 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 9277 diag::err_omp_schedule_nonmonotonic_static); 9278 return nullptr; 9279 } 9280 Expr *ValExpr = ChunkSize; 9281 Stmt *HelperValStmt = nullptr; 9282 if (ChunkSize) { 9283 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 9284 !ChunkSize->isInstantiationDependent() && 9285 !ChunkSize->containsUnexpandedParameterPack()) { 9286 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 9287 ExprResult Val = 9288 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 9289 if (Val.isInvalid()) 9290 return nullptr; 9291 9292 ValExpr = Val.get(); 9293 9294 // OpenMP [2.7.1, Restrictions] 9295 // chunk_size must be a loop invariant integer expression with a positive 9296 // value. 9297 llvm::APSInt Result; 9298 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 9299 if (Result.isSigned() && !Result.isStrictlyPositive()) { 9300 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 9301 << "schedule" << 1 << ChunkSize->getSourceRange(); 9302 return nullptr; 9303 } 9304 } else if (getOpenMPCaptureRegionForClause( 9305 DSAStack->getCurrentDirective(), OMPC_schedule) != 9306 OMPD_unknown && 9307 !CurContext->isDependentContext()) { 9308 ValExpr = MakeFullExpr(ValExpr).get(); 9309 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 9310 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 9311 HelperValStmt = buildPreInits(Context, Captures); 9312 } 9313 } 9314 } 9315 9316 return new (Context) 9317 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 9318 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 9319 } 9320 9321 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 9322 SourceLocation StartLoc, 9323 SourceLocation EndLoc) { 9324 OMPClause *Res = nullptr; 9325 switch (Kind) { 9326 case OMPC_ordered: 9327 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 9328 break; 9329 case OMPC_nowait: 9330 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 9331 break; 9332 case OMPC_untied: 9333 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 9334 break; 9335 case OMPC_mergeable: 9336 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 9337 break; 9338 case OMPC_read: 9339 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 9340 break; 9341 case OMPC_write: 9342 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 9343 break; 9344 case OMPC_update: 9345 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 9346 break; 9347 case OMPC_capture: 9348 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 9349 break; 9350 case OMPC_seq_cst: 9351 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 9352 break; 9353 case OMPC_threads: 9354 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 9355 break; 9356 case OMPC_simd: 9357 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 9358 break; 9359 case OMPC_nogroup: 9360 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 9361 break; 9362 case OMPC_unified_address: 9363 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc); 9364 break; 9365 case OMPC_unified_shared_memory: 9366 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 9367 break; 9368 case OMPC_reverse_offload: 9369 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc); 9370 break; 9371 case OMPC_dynamic_allocators: 9372 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc); 9373 break; 9374 case OMPC_if: 9375 case OMPC_final: 9376 case OMPC_num_threads: 9377 case OMPC_safelen: 9378 case OMPC_simdlen: 9379 case OMPC_collapse: 9380 case OMPC_schedule: 9381 case OMPC_private: 9382 case OMPC_firstprivate: 9383 case OMPC_lastprivate: 9384 case OMPC_shared: 9385 case OMPC_reduction: 9386 case OMPC_task_reduction: 9387 case OMPC_in_reduction: 9388 case OMPC_linear: 9389 case OMPC_aligned: 9390 case OMPC_copyin: 9391 case OMPC_copyprivate: 9392 case OMPC_default: 9393 case OMPC_proc_bind: 9394 case OMPC_threadprivate: 9395 case OMPC_flush: 9396 case OMPC_depend: 9397 case OMPC_device: 9398 case OMPC_map: 9399 case OMPC_num_teams: 9400 case OMPC_thread_limit: 9401 case OMPC_priority: 9402 case OMPC_grainsize: 9403 case OMPC_num_tasks: 9404 case OMPC_hint: 9405 case OMPC_dist_schedule: 9406 case OMPC_defaultmap: 9407 case OMPC_unknown: 9408 case OMPC_uniform: 9409 case OMPC_to: 9410 case OMPC_from: 9411 case OMPC_use_device_ptr: 9412 case OMPC_is_device_ptr: 9413 case OMPC_atomic_default_mem_order: 9414 llvm_unreachable("Clause is not allowed."); 9415 } 9416 return Res; 9417 } 9418 9419 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 9420 SourceLocation EndLoc) { 9421 DSAStack->setNowaitRegion(); 9422 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 9423 } 9424 9425 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 9426 SourceLocation EndLoc) { 9427 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 9428 } 9429 9430 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 9431 SourceLocation EndLoc) { 9432 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 9433 } 9434 9435 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 9436 SourceLocation EndLoc) { 9437 return new (Context) OMPReadClause(StartLoc, EndLoc); 9438 } 9439 9440 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 9441 SourceLocation EndLoc) { 9442 return new (Context) OMPWriteClause(StartLoc, EndLoc); 9443 } 9444 9445 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 9446 SourceLocation EndLoc) { 9447 return new (Context) OMPUpdateClause(StartLoc, EndLoc); 9448 } 9449 9450 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 9451 SourceLocation EndLoc) { 9452 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 9453 } 9454 9455 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 9456 SourceLocation EndLoc) { 9457 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 9458 } 9459 9460 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 9461 SourceLocation EndLoc) { 9462 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 9463 } 9464 9465 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 9466 SourceLocation EndLoc) { 9467 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 9468 } 9469 9470 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 9471 SourceLocation EndLoc) { 9472 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 9473 } 9474 9475 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc, 9476 SourceLocation EndLoc) { 9477 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc); 9478 } 9479 9480 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc, 9481 SourceLocation EndLoc) { 9482 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 9483 } 9484 9485 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc, 9486 SourceLocation EndLoc) { 9487 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc); 9488 } 9489 9490 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc, 9491 SourceLocation EndLoc) { 9492 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc); 9493 } 9494 9495 OMPClause *Sema::ActOnOpenMPVarListClause( 9496 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, 9497 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 9498 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec, 9499 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind, 9500 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier, 9501 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, 9502 SourceLocation DepLinMapLoc) { 9503 OMPClause *Res = nullptr; 9504 switch (Kind) { 9505 case OMPC_private: 9506 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9507 break; 9508 case OMPC_firstprivate: 9509 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9510 break; 9511 case OMPC_lastprivate: 9512 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9513 break; 9514 case OMPC_shared: 9515 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 9516 break; 9517 case OMPC_reduction: 9518 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 9519 EndLoc, ReductionIdScopeSpec, ReductionId); 9520 break; 9521 case OMPC_task_reduction: 9522 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 9523 EndLoc, ReductionIdScopeSpec, 9524 ReductionId); 9525 break; 9526 case OMPC_in_reduction: 9527 Res = 9528 ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 9529 EndLoc, ReductionIdScopeSpec, ReductionId); 9530 break; 9531 case OMPC_linear: 9532 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, 9533 LinKind, DepLinMapLoc, ColonLoc, EndLoc); 9534 break; 9535 case OMPC_aligned: 9536 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, 9537 ColonLoc, EndLoc); 9538 break; 9539 case OMPC_copyin: 9540 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 9541 break; 9542 case OMPC_copyprivate: 9543 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 9544 break; 9545 case OMPC_flush: 9546 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 9547 break; 9548 case OMPC_depend: 9549 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList, 9550 StartLoc, LParenLoc, EndLoc); 9551 break; 9552 case OMPC_map: 9553 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit, 9554 DepLinMapLoc, ColonLoc, VarList, StartLoc, 9555 LParenLoc, EndLoc); 9556 break; 9557 case OMPC_to: 9558 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc); 9559 break; 9560 case OMPC_from: 9561 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc); 9562 break; 9563 case OMPC_use_device_ptr: 9564 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc); 9565 break; 9566 case OMPC_is_device_ptr: 9567 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc); 9568 break; 9569 case OMPC_if: 9570 case OMPC_final: 9571 case OMPC_num_threads: 9572 case OMPC_safelen: 9573 case OMPC_simdlen: 9574 case OMPC_collapse: 9575 case OMPC_default: 9576 case OMPC_proc_bind: 9577 case OMPC_schedule: 9578 case OMPC_ordered: 9579 case OMPC_nowait: 9580 case OMPC_untied: 9581 case OMPC_mergeable: 9582 case OMPC_threadprivate: 9583 case OMPC_read: 9584 case OMPC_write: 9585 case OMPC_update: 9586 case OMPC_capture: 9587 case OMPC_seq_cst: 9588 case OMPC_device: 9589 case OMPC_threads: 9590 case OMPC_simd: 9591 case OMPC_num_teams: 9592 case OMPC_thread_limit: 9593 case OMPC_priority: 9594 case OMPC_grainsize: 9595 case OMPC_nogroup: 9596 case OMPC_num_tasks: 9597 case OMPC_hint: 9598 case OMPC_dist_schedule: 9599 case OMPC_defaultmap: 9600 case OMPC_unknown: 9601 case OMPC_uniform: 9602 case OMPC_unified_address: 9603 case OMPC_unified_shared_memory: 9604 case OMPC_reverse_offload: 9605 case OMPC_dynamic_allocators: 9606 case OMPC_atomic_default_mem_order: 9607 llvm_unreachable("Clause is not allowed."); 9608 } 9609 return Res; 9610 } 9611 9612 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 9613 ExprObjectKind OK, SourceLocation Loc) { 9614 ExprResult Res = BuildDeclRefExpr( 9615 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 9616 if (!Res.isUsable()) 9617 return ExprError(); 9618 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 9619 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 9620 if (!Res.isUsable()) 9621 return ExprError(); 9622 } 9623 if (VK != VK_LValue && Res.get()->isGLValue()) { 9624 Res = DefaultLvalueConversion(Res.get()); 9625 if (!Res.isUsable()) 9626 return ExprError(); 9627 } 9628 return Res; 9629 } 9630 9631 static std::pair<ValueDecl *, bool> 9632 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 9633 SourceRange &ERange, bool AllowArraySection = false) { 9634 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 9635 RefExpr->containsUnexpandedParameterPack()) 9636 return std::make_pair(nullptr, true); 9637 9638 // OpenMP [3.1, C/C++] 9639 // A list item is a variable name. 9640 // OpenMP [2.9.3.3, Restrictions, p.1] 9641 // A variable that is part of another variable (as an array or 9642 // structure element) cannot appear in a private clause. 9643 RefExpr = RefExpr->IgnoreParens(); 9644 enum { 9645 NoArrayExpr = -1, 9646 ArraySubscript = 0, 9647 OMPArraySection = 1 9648 } IsArrayExpr = NoArrayExpr; 9649 if (AllowArraySection) { 9650 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 9651 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 9652 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 9653 Base = TempASE->getBase()->IgnoreParenImpCasts(); 9654 RefExpr = Base; 9655 IsArrayExpr = ArraySubscript; 9656 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 9657 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 9658 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 9659 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 9660 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 9661 Base = TempASE->getBase()->IgnoreParenImpCasts(); 9662 RefExpr = Base; 9663 IsArrayExpr = OMPArraySection; 9664 } 9665 } 9666 ELoc = RefExpr->getExprLoc(); 9667 ERange = RefExpr->getSourceRange(); 9668 RefExpr = RefExpr->IgnoreParenImpCasts(); 9669 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 9670 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 9671 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 9672 (S.getCurrentThisType().isNull() || !ME || 9673 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 9674 !isa<FieldDecl>(ME->getMemberDecl()))) { 9675 if (IsArrayExpr != NoArrayExpr) { 9676 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 9677 << ERange; 9678 } else { 9679 S.Diag(ELoc, 9680 AllowArraySection 9681 ? diag::err_omp_expected_var_name_member_expr_or_array_item 9682 : diag::err_omp_expected_var_name_member_expr) 9683 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 9684 } 9685 return std::make_pair(nullptr, false); 9686 } 9687 return std::make_pair( 9688 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 9689 } 9690 9691 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 9692 SourceLocation StartLoc, 9693 SourceLocation LParenLoc, 9694 SourceLocation EndLoc) { 9695 SmallVector<Expr *, 8> Vars; 9696 SmallVector<Expr *, 8> PrivateCopies; 9697 for (Expr *RefExpr : VarList) { 9698 assert(RefExpr && "NULL expr in OpenMP private clause."); 9699 SourceLocation ELoc; 9700 SourceRange ERange; 9701 Expr *SimpleRefExpr = RefExpr; 9702 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 9703 if (Res.second) { 9704 // It will be analyzed later. 9705 Vars.push_back(RefExpr); 9706 PrivateCopies.push_back(nullptr); 9707 } 9708 ValueDecl *D = Res.first; 9709 if (!D) 9710 continue; 9711 9712 QualType Type = D->getType(); 9713 auto *VD = dyn_cast<VarDecl>(D); 9714 9715 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 9716 // A variable that appears in a private clause must not have an incomplete 9717 // type or a reference type. 9718 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 9719 continue; 9720 Type = Type.getNonReferenceType(); 9721 9722 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 9723 // in a Construct] 9724 // Variables with the predetermined data-sharing attributes may not be 9725 // listed in data-sharing attributes clauses, except for the cases 9726 // listed below. For these exceptions only, listing a predetermined 9727 // variable in a data-sharing attribute clause is allowed and overrides 9728 // the variable's predetermined data-sharing attributes. 9729 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 9730 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 9731 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 9732 << getOpenMPClauseName(OMPC_private); 9733 reportOriginalDsa(*this, DSAStack, D, DVar); 9734 continue; 9735 } 9736 9737 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 9738 // Variably modified types are not supported for tasks. 9739 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 9740 isOpenMPTaskingDirective(CurrDir)) { 9741 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 9742 << getOpenMPClauseName(OMPC_private) << Type 9743 << getOpenMPDirectiveName(CurrDir); 9744 bool IsDecl = 9745 !VD || 9746 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 9747 Diag(D->getLocation(), 9748 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 9749 << D; 9750 continue; 9751 } 9752 9753 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 9754 // A list item cannot appear in both a map clause and a data-sharing 9755 // attribute clause on the same construct 9756 if (isOpenMPTargetExecutionDirective(CurrDir)) { 9757 OpenMPClauseKind ConflictKind; 9758 if (DSAStack->checkMappableExprComponentListsForDecl( 9759 VD, /*CurrentRegionOnly=*/true, 9760 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 9761 OpenMPClauseKind WhereFoundClauseKind) -> bool { 9762 ConflictKind = WhereFoundClauseKind; 9763 return true; 9764 })) { 9765 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 9766 << getOpenMPClauseName(OMPC_private) 9767 << getOpenMPClauseName(ConflictKind) 9768 << getOpenMPDirectiveName(CurrDir); 9769 reportOriginalDsa(*this, DSAStack, D, DVar); 9770 continue; 9771 } 9772 } 9773 9774 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 9775 // A variable of class type (or array thereof) that appears in a private 9776 // clause requires an accessible, unambiguous default constructor for the 9777 // class type. 9778 // Generate helper private variable and initialize it with the default 9779 // value. The address of the original variable is replaced by the address of 9780 // the new private variable in CodeGen. This new variable is not added to 9781 // IdResolver, so the code in the OpenMP region uses original variable for 9782 // proper diagnostics. 9783 Type = Type.getUnqualifiedType(); 9784 VarDecl *VDPrivate = 9785 buildVarDecl(*this, ELoc, Type, D->getName(), 9786 D->hasAttrs() ? &D->getAttrs() : nullptr, 9787 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 9788 ActOnUninitializedDecl(VDPrivate); 9789 if (VDPrivate->isInvalidDecl()) 9790 continue; 9791 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 9792 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 9793 9794 DeclRefExpr *Ref = nullptr; 9795 if (!VD && !CurContext->isDependentContext()) 9796 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 9797 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 9798 Vars.push_back((VD || CurContext->isDependentContext()) 9799 ? RefExpr->IgnoreParens() 9800 : Ref); 9801 PrivateCopies.push_back(VDPrivateRefExpr); 9802 } 9803 9804 if (Vars.empty()) 9805 return nullptr; 9806 9807 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 9808 PrivateCopies); 9809 } 9810 9811 namespace { 9812 class DiagsUninitializedSeveretyRAII { 9813 private: 9814 DiagnosticsEngine &Diags; 9815 SourceLocation SavedLoc; 9816 bool IsIgnored = false; 9817 9818 public: 9819 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 9820 bool IsIgnored) 9821 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 9822 if (!IsIgnored) { 9823 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 9824 /*Map*/ diag::Severity::Ignored, Loc); 9825 } 9826 } 9827 ~DiagsUninitializedSeveretyRAII() { 9828 if (!IsIgnored) 9829 Diags.popMappings(SavedLoc); 9830 } 9831 }; 9832 } 9833 9834 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 9835 SourceLocation StartLoc, 9836 SourceLocation LParenLoc, 9837 SourceLocation EndLoc) { 9838 SmallVector<Expr *, 8> Vars; 9839 SmallVector<Expr *, 8> PrivateCopies; 9840 SmallVector<Expr *, 8> Inits; 9841 SmallVector<Decl *, 4> ExprCaptures; 9842 bool IsImplicitClause = 9843 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 9844 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 9845 9846 for (Expr *RefExpr : VarList) { 9847 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 9848 SourceLocation ELoc; 9849 SourceRange ERange; 9850 Expr *SimpleRefExpr = RefExpr; 9851 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 9852 if (Res.second) { 9853 // It will be analyzed later. 9854 Vars.push_back(RefExpr); 9855 PrivateCopies.push_back(nullptr); 9856 Inits.push_back(nullptr); 9857 } 9858 ValueDecl *D = Res.first; 9859 if (!D) 9860 continue; 9861 9862 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 9863 QualType Type = D->getType(); 9864 auto *VD = dyn_cast<VarDecl>(D); 9865 9866 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 9867 // A variable that appears in a private clause must not have an incomplete 9868 // type or a reference type. 9869 if (RequireCompleteType(ELoc, Type, 9870 diag::err_omp_firstprivate_incomplete_type)) 9871 continue; 9872 Type = Type.getNonReferenceType(); 9873 9874 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 9875 // A variable of class type (or array thereof) that appears in a private 9876 // clause requires an accessible, unambiguous copy constructor for the 9877 // class type. 9878 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 9879 9880 // If an implicit firstprivate variable found it was checked already. 9881 DSAStackTy::DSAVarData TopDVar; 9882 if (!IsImplicitClause) { 9883 DSAStackTy::DSAVarData DVar = 9884 DSAStack->getTopDSA(D, /*FromParent=*/false); 9885 TopDVar = DVar; 9886 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 9887 bool IsConstant = ElemType.isConstant(Context); 9888 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 9889 // A list item that specifies a given variable may not appear in more 9890 // than one clause on the same directive, except that a variable may be 9891 // specified in both firstprivate and lastprivate clauses. 9892 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 9893 // A list item may appear in a firstprivate or lastprivate clause but not 9894 // both. 9895 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 9896 (isOpenMPDistributeDirective(CurrDir) || 9897 DVar.CKind != OMPC_lastprivate) && 9898 DVar.RefExpr) { 9899 Diag(ELoc, diag::err_omp_wrong_dsa) 9900 << getOpenMPClauseName(DVar.CKind) 9901 << getOpenMPClauseName(OMPC_firstprivate); 9902 reportOriginalDsa(*this, DSAStack, D, DVar); 9903 continue; 9904 } 9905 9906 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 9907 // in a Construct] 9908 // Variables with the predetermined data-sharing attributes may not be 9909 // listed in data-sharing attributes clauses, except for the cases 9910 // listed below. For these exceptions only, listing a predetermined 9911 // variable in a data-sharing attribute clause is allowed and overrides 9912 // the variable's predetermined data-sharing attributes. 9913 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 9914 // in a Construct, C/C++, p.2] 9915 // Variables with const-qualified type having no mutable member may be 9916 // listed in a firstprivate clause, even if they are static data members. 9917 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 9918 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 9919 Diag(ELoc, diag::err_omp_wrong_dsa) 9920 << getOpenMPClauseName(DVar.CKind) 9921 << getOpenMPClauseName(OMPC_firstprivate); 9922 reportOriginalDsa(*this, DSAStack, D, DVar); 9923 continue; 9924 } 9925 9926 // OpenMP [2.9.3.4, Restrictions, p.2] 9927 // A list item that is private within a parallel region must not appear 9928 // in a firstprivate clause on a worksharing construct if any of the 9929 // worksharing regions arising from the worksharing construct ever bind 9930 // to any of the parallel regions arising from the parallel construct. 9931 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 9932 // A list item that is private within a teams region must not appear in a 9933 // firstprivate clause on a distribute construct if any of the distribute 9934 // regions arising from the distribute construct ever bind to any of the 9935 // teams regions arising from the teams construct. 9936 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 9937 // A list item that appears in a reduction clause of a teams construct 9938 // must not appear in a firstprivate clause on a distribute construct if 9939 // any of the distribute regions arising from the distribute construct 9940 // ever bind to any of the teams regions arising from the teams construct. 9941 if ((isOpenMPWorksharingDirective(CurrDir) || 9942 isOpenMPDistributeDirective(CurrDir)) && 9943 !isOpenMPParallelDirective(CurrDir) && 9944 !isOpenMPTeamsDirective(CurrDir)) { 9945 DVar = DSAStack->getImplicitDSA(D, true); 9946 if (DVar.CKind != OMPC_shared && 9947 (isOpenMPParallelDirective(DVar.DKind) || 9948 isOpenMPTeamsDirective(DVar.DKind) || 9949 DVar.DKind == OMPD_unknown)) { 9950 Diag(ELoc, diag::err_omp_required_access) 9951 << getOpenMPClauseName(OMPC_firstprivate) 9952 << getOpenMPClauseName(OMPC_shared); 9953 reportOriginalDsa(*this, DSAStack, D, DVar); 9954 continue; 9955 } 9956 } 9957 // OpenMP [2.9.3.4, Restrictions, p.3] 9958 // A list item that appears in a reduction clause of a parallel construct 9959 // must not appear in a firstprivate clause on a worksharing or task 9960 // construct if any of the worksharing or task regions arising from the 9961 // worksharing or task construct ever bind to any of the parallel regions 9962 // arising from the parallel construct. 9963 // OpenMP [2.9.3.4, Restrictions, p.4] 9964 // A list item that appears in a reduction clause in worksharing 9965 // construct must not appear in a firstprivate clause in a task construct 9966 // encountered during execution of any of the worksharing regions arising 9967 // from the worksharing construct. 9968 if (isOpenMPTaskingDirective(CurrDir)) { 9969 DVar = DSAStack->hasInnermostDSA( 9970 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 9971 [](OpenMPDirectiveKind K) { 9972 return isOpenMPParallelDirective(K) || 9973 isOpenMPWorksharingDirective(K) || 9974 isOpenMPTeamsDirective(K); 9975 }, 9976 /*FromParent=*/true); 9977 if (DVar.CKind == OMPC_reduction && 9978 (isOpenMPParallelDirective(DVar.DKind) || 9979 isOpenMPWorksharingDirective(DVar.DKind) || 9980 isOpenMPTeamsDirective(DVar.DKind))) { 9981 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 9982 << getOpenMPDirectiveName(DVar.DKind); 9983 reportOriginalDsa(*this, DSAStack, D, DVar); 9984 continue; 9985 } 9986 } 9987 9988 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 9989 // A list item cannot appear in both a map clause and a data-sharing 9990 // attribute clause on the same construct 9991 if (isOpenMPTargetExecutionDirective(CurrDir)) { 9992 OpenMPClauseKind ConflictKind; 9993 if (DSAStack->checkMappableExprComponentListsForDecl( 9994 VD, /*CurrentRegionOnly=*/true, 9995 [&ConflictKind]( 9996 OMPClauseMappableExprCommon::MappableExprComponentListRef, 9997 OpenMPClauseKind WhereFoundClauseKind) { 9998 ConflictKind = WhereFoundClauseKind; 9999 return true; 10000 })) { 10001 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 10002 << getOpenMPClauseName(OMPC_firstprivate) 10003 << getOpenMPClauseName(ConflictKind) 10004 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 10005 reportOriginalDsa(*this, DSAStack, D, DVar); 10006 continue; 10007 } 10008 } 10009 } 10010 10011 // Variably modified types are not supported for tasks. 10012 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 10013 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 10014 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 10015 << getOpenMPClauseName(OMPC_firstprivate) << Type 10016 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 10017 bool IsDecl = 10018 !VD || 10019 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 10020 Diag(D->getLocation(), 10021 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10022 << D; 10023 continue; 10024 } 10025 10026 Type = Type.getUnqualifiedType(); 10027 VarDecl *VDPrivate = 10028 buildVarDecl(*this, ELoc, Type, D->getName(), 10029 D->hasAttrs() ? &D->getAttrs() : nullptr, 10030 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 10031 // Generate helper private variable and initialize it with the value of the 10032 // original variable. The address of the original variable is replaced by 10033 // the address of the new private variable in the CodeGen. This new variable 10034 // is not added to IdResolver, so the code in the OpenMP region uses 10035 // original variable for proper diagnostics and variable capturing. 10036 Expr *VDInitRefExpr = nullptr; 10037 // For arrays generate initializer for single element and replace it by the 10038 // original array element in CodeGen. 10039 if (Type->isArrayType()) { 10040 VarDecl *VDInit = 10041 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 10042 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 10043 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 10044 ElemType = ElemType.getUnqualifiedType(); 10045 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 10046 ".firstprivate.temp"); 10047 InitializedEntity Entity = 10048 InitializedEntity::InitializeVariable(VDInitTemp); 10049 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 10050 10051 InitializationSequence InitSeq(*this, Entity, Kind, Init); 10052 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 10053 if (Result.isInvalid()) 10054 VDPrivate->setInvalidDecl(); 10055 else 10056 VDPrivate->setInit(Result.getAs<Expr>()); 10057 // Remove temp variable declaration. 10058 Context.Deallocate(VDInitTemp); 10059 } else { 10060 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 10061 ".firstprivate.temp"); 10062 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 10063 RefExpr->getExprLoc()); 10064 AddInitializerToDecl(VDPrivate, 10065 DefaultLvalueConversion(VDInitRefExpr).get(), 10066 /*DirectInit=*/false); 10067 } 10068 if (VDPrivate->isInvalidDecl()) { 10069 if (IsImplicitClause) { 10070 Diag(RefExpr->getExprLoc(), 10071 diag::note_omp_task_predetermined_firstprivate_here); 10072 } 10073 continue; 10074 } 10075 CurContext->addDecl(VDPrivate); 10076 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 10077 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 10078 RefExpr->getExprLoc()); 10079 DeclRefExpr *Ref = nullptr; 10080 if (!VD && !CurContext->isDependentContext()) { 10081 if (TopDVar.CKind == OMPC_lastprivate) { 10082 Ref = TopDVar.PrivateCopy; 10083 } else { 10084 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 10085 if (!isOpenMPCapturedDecl(D)) 10086 ExprCaptures.push_back(Ref->getDecl()); 10087 } 10088 } 10089 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 10090 Vars.push_back((VD || CurContext->isDependentContext()) 10091 ? RefExpr->IgnoreParens() 10092 : Ref); 10093 PrivateCopies.push_back(VDPrivateRefExpr); 10094 Inits.push_back(VDInitRefExpr); 10095 } 10096 10097 if (Vars.empty()) 10098 return nullptr; 10099 10100 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 10101 Vars, PrivateCopies, Inits, 10102 buildPreInits(Context, ExprCaptures)); 10103 } 10104 10105 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList, 10106 SourceLocation StartLoc, 10107 SourceLocation LParenLoc, 10108 SourceLocation EndLoc) { 10109 SmallVector<Expr *, 8> Vars; 10110 SmallVector<Expr *, 8> SrcExprs; 10111 SmallVector<Expr *, 8> DstExprs; 10112 SmallVector<Expr *, 8> AssignmentOps; 10113 SmallVector<Decl *, 4> ExprCaptures; 10114 SmallVector<Expr *, 4> ExprPostUpdates; 10115 for (Expr *RefExpr : VarList) { 10116 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 10117 SourceLocation ELoc; 10118 SourceRange ERange; 10119 Expr *SimpleRefExpr = RefExpr; 10120 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 10121 if (Res.second) { 10122 // It will be analyzed later. 10123 Vars.push_back(RefExpr); 10124 SrcExprs.push_back(nullptr); 10125 DstExprs.push_back(nullptr); 10126 AssignmentOps.push_back(nullptr); 10127 } 10128 ValueDecl *D = Res.first; 10129 if (!D) 10130 continue; 10131 10132 QualType Type = D->getType(); 10133 auto *VD = dyn_cast<VarDecl>(D); 10134 10135 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 10136 // A variable that appears in a lastprivate clause must not have an 10137 // incomplete type or a reference type. 10138 if (RequireCompleteType(ELoc, Type, 10139 diag::err_omp_lastprivate_incomplete_type)) 10140 continue; 10141 Type = Type.getNonReferenceType(); 10142 10143 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 10144 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 10145 // in a Construct] 10146 // Variables with the predetermined data-sharing attributes may not be 10147 // listed in data-sharing attributes clauses, except for the cases 10148 // listed below. 10149 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 10150 // A list item may appear in a firstprivate or lastprivate clause but not 10151 // both. 10152 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 10153 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 10154 (isOpenMPDistributeDirective(CurrDir) || 10155 DVar.CKind != OMPC_firstprivate) && 10156 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 10157 Diag(ELoc, diag::err_omp_wrong_dsa) 10158 << getOpenMPClauseName(DVar.CKind) 10159 << getOpenMPClauseName(OMPC_lastprivate); 10160 reportOriginalDsa(*this, DSAStack, D, DVar); 10161 continue; 10162 } 10163 10164 // OpenMP [2.14.3.5, Restrictions, p.2] 10165 // A list item that is private within a parallel region, or that appears in 10166 // the reduction clause of a parallel construct, must not appear in a 10167 // lastprivate clause on a worksharing construct if any of the corresponding 10168 // worksharing regions ever binds to any of the corresponding parallel 10169 // regions. 10170 DSAStackTy::DSAVarData TopDVar = DVar; 10171 if (isOpenMPWorksharingDirective(CurrDir) && 10172 !isOpenMPParallelDirective(CurrDir) && 10173 !isOpenMPTeamsDirective(CurrDir)) { 10174 DVar = DSAStack->getImplicitDSA(D, true); 10175 if (DVar.CKind != OMPC_shared) { 10176 Diag(ELoc, diag::err_omp_required_access) 10177 << getOpenMPClauseName(OMPC_lastprivate) 10178 << getOpenMPClauseName(OMPC_shared); 10179 reportOriginalDsa(*this, DSAStack, D, DVar); 10180 continue; 10181 } 10182 } 10183 10184 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 10185 // A variable of class type (or array thereof) that appears in a 10186 // lastprivate clause requires an accessible, unambiguous default 10187 // constructor for the class type, unless the list item is also specified 10188 // in a firstprivate clause. 10189 // A variable of class type (or array thereof) that appears in a 10190 // lastprivate clause requires an accessible, unambiguous copy assignment 10191 // operator for the class type. 10192 Type = Context.getBaseElementType(Type).getNonReferenceType(); 10193 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 10194 Type.getUnqualifiedType(), ".lastprivate.src", 10195 D->hasAttrs() ? &D->getAttrs() : nullptr); 10196 DeclRefExpr *PseudoSrcExpr = 10197 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 10198 VarDecl *DstVD = 10199 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 10200 D->hasAttrs() ? &D->getAttrs() : nullptr); 10201 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 10202 // For arrays generate assignment operation for single element and replace 10203 // it by the original array element in CodeGen. 10204 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 10205 PseudoDstExpr, PseudoSrcExpr); 10206 if (AssignmentOp.isInvalid()) 10207 continue; 10208 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc, 10209 /*DiscardedValue=*/true); 10210 if (AssignmentOp.isInvalid()) 10211 continue; 10212 10213 DeclRefExpr *Ref = nullptr; 10214 if (!VD && !CurContext->isDependentContext()) { 10215 if (TopDVar.CKind == OMPC_firstprivate) { 10216 Ref = TopDVar.PrivateCopy; 10217 } else { 10218 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 10219 if (!isOpenMPCapturedDecl(D)) 10220 ExprCaptures.push_back(Ref->getDecl()); 10221 } 10222 if (TopDVar.CKind == OMPC_firstprivate || 10223 (!isOpenMPCapturedDecl(D) && 10224 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 10225 ExprResult RefRes = DefaultLvalueConversion(Ref); 10226 if (!RefRes.isUsable()) 10227 continue; 10228 ExprResult PostUpdateRes = 10229 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 10230 RefRes.get()); 10231 if (!PostUpdateRes.isUsable()) 10232 continue; 10233 ExprPostUpdates.push_back( 10234 IgnoredValueConversions(PostUpdateRes.get()).get()); 10235 } 10236 } 10237 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 10238 Vars.push_back((VD || CurContext->isDependentContext()) 10239 ? RefExpr->IgnoreParens() 10240 : Ref); 10241 SrcExprs.push_back(PseudoSrcExpr); 10242 DstExprs.push_back(PseudoDstExpr); 10243 AssignmentOps.push_back(AssignmentOp.get()); 10244 } 10245 10246 if (Vars.empty()) 10247 return nullptr; 10248 10249 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 10250 Vars, SrcExprs, DstExprs, AssignmentOps, 10251 buildPreInits(Context, ExprCaptures), 10252 buildPostUpdate(*this, ExprPostUpdates)); 10253 } 10254 10255 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 10256 SourceLocation StartLoc, 10257 SourceLocation LParenLoc, 10258 SourceLocation EndLoc) { 10259 SmallVector<Expr *, 8> Vars; 10260 for (Expr *RefExpr : VarList) { 10261 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 10262 SourceLocation ELoc; 10263 SourceRange ERange; 10264 Expr *SimpleRefExpr = RefExpr; 10265 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 10266 if (Res.second) { 10267 // It will be analyzed later. 10268 Vars.push_back(RefExpr); 10269 } 10270 ValueDecl *D = Res.first; 10271 if (!D) 10272 continue; 10273 10274 auto *VD = dyn_cast<VarDecl>(D); 10275 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 10276 // in a Construct] 10277 // Variables with the predetermined data-sharing attributes may not be 10278 // listed in data-sharing attributes clauses, except for the cases 10279 // listed below. For these exceptions only, listing a predetermined 10280 // variable in a data-sharing attribute clause is allowed and overrides 10281 // the variable's predetermined data-sharing attributes. 10282 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 10283 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 10284 DVar.RefExpr) { 10285 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 10286 << getOpenMPClauseName(OMPC_shared); 10287 reportOriginalDsa(*this, DSAStack, D, DVar); 10288 continue; 10289 } 10290 10291 DeclRefExpr *Ref = nullptr; 10292 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 10293 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 10294 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 10295 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 10296 ? RefExpr->IgnoreParens() 10297 : Ref); 10298 } 10299 10300 if (Vars.empty()) 10301 return nullptr; 10302 10303 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 10304 } 10305 10306 namespace { 10307 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 10308 DSAStackTy *Stack; 10309 10310 public: 10311 bool VisitDeclRefExpr(DeclRefExpr *E) { 10312 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 10313 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 10314 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 10315 return false; 10316 if (DVar.CKind != OMPC_unknown) 10317 return true; 10318 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 10319 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; }, 10320 /*FromParent=*/true); 10321 return DVarPrivate.CKind != OMPC_unknown; 10322 } 10323 return false; 10324 } 10325 bool VisitStmt(Stmt *S) { 10326 for (Stmt *Child : S->children()) { 10327 if (Child && Visit(Child)) 10328 return true; 10329 } 10330 return false; 10331 } 10332 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 10333 }; 10334 } // namespace 10335 10336 namespace { 10337 // Transform MemberExpression for specified FieldDecl of current class to 10338 // DeclRefExpr to specified OMPCapturedExprDecl. 10339 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 10340 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 10341 ValueDecl *Field = nullptr; 10342 DeclRefExpr *CapturedExpr = nullptr; 10343 10344 public: 10345 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 10346 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 10347 10348 ExprResult TransformMemberExpr(MemberExpr *E) { 10349 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 10350 E->getMemberDecl() == Field) { 10351 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 10352 return CapturedExpr; 10353 } 10354 return BaseTransform::TransformMemberExpr(E); 10355 } 10356 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 10357 }; 10358 } // namespace 10359 10360 template <typename T, typename U> 10361 static T filterLookupForUDR(SmallVectorImpl<U> &Lookups, 10362 const llvm::function_ref<T(ValueDecl *)> Gen) { 10363 for (U &Set : Lookups) { 10364 for (auto *D : Set) { 10365 if (T Res = Gen(cast<ValueDecl>(D))) 10366 return Res; 10367 } 10368 } 10369 return T(); 10370 } 10371 10372 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 10373 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 10374 10375 for (auto RD : D->redecls()) { 10376 // Don't bother with extra checks if we already know this one isn't visible. 10377 if (RD == D) 10378 continue; 10379 10380 auto ND = cast<NamedDecl>(RD); 10381 if (LookupResult::isVisible(SemaRef, ND)) 10382 return ND; 10383 } 10384 10385 return nullptr; 10386 } 10387 10388 static void 10389 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &ReductionId, 10390 SourceLocation Loc, QualType Ty, 10391 SmallVectorImpl<UnresolvedSet<8>> &Lookups) { 10392 // Find all of the associated namespaces and classes based on the 10393 // arguments we have. 10394 Sema::AssociatedNamespaceSet AssociatedNamespaces; 10395 Sema::AssociatedClassSet AssociatedClasses; 10396 OpaqueValueExpr OVE(Loc, Ty, VK_LValue); 10397 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces, 10398 AssociatedClasses); 10399 10400 // C++ [basic.lookup.argdep]p3: 10401 // Let X be the lookup set produced by unqualified lookup (3.4.1) 10402 // and let Y be the lookup set produced by argument dependent 10403 // lookup (defined as follows). If X contains [...] then Y is 10404 // empty. Otherwise Y is the set of declarations found in the 10405 // namespaces associated with the argument types as described 10406 // below. The set of declarations found by the lookup of the name 10407 // is the union of X and Y. 10408 // 10409 // Here, we compute Y and add its members to the overloaded 10410 // candidate set. 10411 for (auto *NS : AssociatedNamespaces) { 10412 // When considering an associated namespace, the lookup is the 10413 // same as the lookup performed when the associated namespace is 10414 // used as a qualifier (3.4.3.2) except that: 10415 // 10416 // -- Any using-directives in the associated namespace are 10417 // ignored. 10418 // 10419 // -- Any namespace-scope friend functions declared in 10420 // associated classes are visible within their respective 10421 // namespaces even if they are not visible during an ordinary 10422 // lookup (11.4). 10423 DeclContext::lookup_result R = NS->lookup(ReductionId.getName()); 10424 for (auto *D : R) { 10425 auto *Underlying = D; 10426 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 10427 Underlying = USD->getTargetDecl(); 10428 10429 if (!isa<OMPDeclareReductionDecl>(Underlying)) 10430 continue; 10431 10432 if (!SemaRef.isVisible(D)) { 10433 D = findAcceptableDecl(SemaRef, D); 10434 if (!D) 10435 continue; 10436 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 10437 Underlying = USD->getTargetDecl(); 10438 } 10439 Lookups.emplace_back(); 10440 Lookups.back().addDecl(Underlying); 10441 } 10442 } 10443 } 10444 10445 static ExprResult 10446 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 10447 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 10448 const DeclarationNameInfo &ReductionId, QualType Ty, 10449 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 10450 if (ReductionIdScopeSpec.isInvalid()) 10451 return ExprError(); 10452 SmallVector<UnresolvedSet<8>, 4> Lookups; 10453 if (S) { 10454 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 10455 Lookup.suppressDiagnostics(); 10456 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 10457 NamedDecl *D = Lookup.getRepresentativeDecl(); 10458 do { 10459 S = S->getParent(); 10460 } while (S && !S->isDeclScope(D)); 10461 if (S) 10462 S = S->getParent(); 10463 Lookups.emplace_back(); 10464 Lookups.back().append(Lookup.begin(), Lookup.end()); 10465 Lookup.clear(); 10466 } 10467 } else if (auto *ULE = 10468 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 10469 Lookups.push_back(UnresolvedSet<8>()); 10470 Decl *PrevD = nullptr; 10471 for (NamedDecl *D : ULE->decls()) { 10472 if (D == PrevD) 10473 Lookups.push_back(UnresolvedSet<8>()); 10474 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D)) 10475 Lookups.back().addDecl(DRD); 10476 PrevD = D; 10477 } 10478 } 10479 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 10480 Ty->isInstantiationDependentType() || 10481 Ty->containsUnexpandedParameterPack() || 10482 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) { 10483 return !D->isInvalidDecl() && 10484 (D->getType()->isDependentType() || 10485 D->getType()->isInstantiationDependentType() || 10486 D->getType()->containsUnexpandedParameterPack()); 10487 })) { 10488 UnresolvedSet<8> ResSet; 10489 for (const UnresolvedSet<8> &Set : Lookups) { 10490 if (Set.empty()) 10491 continue; 10492 ResSet.append(Set.begin(), Set.end()); 10493 // The last item marks the end of all declarations at the specified scope. 10494 ResSet.addDecl(Set[Set.size() - 1]); 10495 } 10496 return UnresolvedLookupExpr::Create( 10497 SemaRef.Context, /*NamingClass=*/nullptr, 10498 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 10499 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 10500 } 10501 // Lookup inside the classes. 10502 // C++ [over.match.oper]p3: 10503 // For a unary operator @ with an operand of a type whose 10504 // cv-unqualified version is T1, and for a binary operator @ with 10505 // a left operand of a type whose cv-unqualified version is T1 and 10506 // a right operand of a type whose cv-unqualified version is T2, 10507 // three sets of candidate functions, designated member 10508 // candidates, non-member candidates and built-in candidates, are 10509 // constructed as follows: 10510 // -- If T1 is a complete class type or a class currently being 10511 // defined, the set of member candidates is the result of the 10512 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, 10513 // the set of member candidates is empty. 10514 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 10515 Lookup.suppressDiagnostics(); 10516 if (const auto *TyRec = Ty->getAs<RecordType>()) { 10517 // Complete the type if it can be completed. 10518 // If the type is neither complete nor being defined, bail out now. 10519 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() || 10520 TyRec->getDecl()->getDefinition()) { 10521 Lookup.clear(); 10522 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl()); 10523 if (Lookup.empty()) { 10524 Lookups.emplace_back(); 10525 Lookups.back().append(Lookup.begin(), Lookup.end()); 10526 } 10527 } 10528 } 10529 // Perform ADL. 10530 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups); 10531 if (auto *VD = filterLookupForUDR<ValueDecl *>( 10532 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 10533 if (!D->isInvalidDecl() && 10534 SemaRef.Context.hasSameType(D->getType(), Ty)) 10535 return D; 10536 return nullptr; 10537 })) 10538 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc); 10539 if (auto *VD = filterLookupForUDR<ValueDecl *>( 10540 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 10541 if (!D->isInvalidDecl() && 10542 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 10543 !Ty.isMoreQualifiedThan(D->getType())) 10544 return D; 10545 return nullptr; 10546 })) { 10547 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 10548 /*DetectVirtual=*/false); 10549 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 10550 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 10551 VD->getType().getUnqualifiedType()))) { 10552 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(), 10553 /*DiagID=*/0) != 10554 Sema::AR_inaccessible) { 10555 SemaRef.BuildBasePathArray(Paths, BasePath); 10556 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc); 10557 } 10558 } 10559 } 10560 } 10561 if (ReductionIdScopeSpec.isSet()) { 10562 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range; 10563 return ExprError(); 10564 } 10565 return ExprEmpty(); 10566 } 10567 10568 namespace { 10569 /// Data for the reduction-based clauses. 10570 struct ReductionData { 10571 /// List of original reduction items. 10572 SmallVector<Expr *, 8> Vars; 10573 /// List of private copies of the reduction items. 10574 SmallVector<Expr *, 8> Privates; 10575 /// LHS expressions for the reduction_op expressions. 10576 SmallVector<Expr *, 8> LHSs; 10577 /// RHS expressions for the reduction_op expressions. 10578 SmallVector<Expr *, 8> RHSs; 10579 /// Reduction operation expression. 10580 SmallVector<Expr *, 8> ReductionOps; 10581 /// Taskgroup descriptors for the corresponding reduction items in 10582 /// in_reduction clauses. 10583 SmallVector<Expr *, 8> TaskgroupDescriptors; 10584 /// List of captures for clause. 10585 SmallVector<Decl *, 4> ExprCaptures; 10586 /// List of postupdate expressions. 10587 SmallVector<Expr *, 4> ExprPostUpdates; 10588 ReductionData() = delete; 10589 /// Reserves required memory for the reduction data. 10590 ReductionData(unsigned Size) { 10591 Vars.reserve(Size); 10592 Privates.reserve(Size); 10593 LHSs.reserve(Size); 10594 RHSs.reserve(Size); 10595 ReductionOps.reserve(Size); 10596 TaskgroupDescriptors.reserve(Size); 10597 ExprCaptures.reserve(Size); 10598 ExprPostUpdates.reserve(Size); 10599 } 10600 /// Stores reduction item and reduction operation only (required for dependent 10601 /// reduction item). 10602 void push(Expr *Item, Expr *ReductionOp) { 10603 Vars.emplace_back(Item); 10604 Privates.emplace_back(nullptr); 10605 LHSs.emplace_back(nullptr); 10606 RHSs.emplace_back(nullptr); 10607 ReductionOps.emplace_back(ReductionOp); 10608 TaskgroupDescriptors.emplace_back(nullptr); 10609 } 10610 /// Stores reduction data. 10611 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 10612 Expr *TaskgroupDescriptor) { 10613 Vars.emplace_back(Item); 10614 Privates.emplace_back(Private); 10615 LHSs.emplace_back(LHS); 10616 RHSs.emplace_back(RHS); 10617 ReductionOps.emplace_back(ReductionOp); 10618 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 10619 } 10620 }; 10621 } // namespace 10622 10623 static bool checkOMPArraySectionConstantForReduction( 10624 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 10625 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 10626 const Expr *Length = OASE->getLength(); 10627 if (Length == nullptr) { 10628 // For array sections of the form [1:] or [:], we would need to analyze 10629 // the lower bound... 10630 if (OASE->getColonLoc().isValid()) 10631 return false; 10632 10633 // This is an array subscript which has implicit length 1! 10634 SingleElement = true; 10635 ArraySizes.push_back(llvm::APSInt::get(1)); 10636 } else { 10637 llvm::APSInt ConstantLengthValue; 10638 if (!Length->EvaluateAsInt(ConstantLengthValue, Context)) 10639 return false; 10640 10641 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 10642 ArraySizes.push_back(ConstantLengthValue); 10643 } 10644 10645 // Get the base of this array section and walk up from there. 10646 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 10647 10648 // We require length = 1 for all array sections except the right-most to 10649 // guarantee that the memory region is contiguous and has no holes in it. 10650 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 10651 Length = TempOASE->getLength(); 10652 if (Length == nullptr) { 10653 // For array sections of the form [1:] or [:], we would need to analyze 10654 // the lower bound... 10655 if (OASE->getColonLoc().isValid()) 10656 return false; 10657 10658 // This is an array subscript which has implicit length 1! 10659 ArraySizes.push_back(llvm::APSInt::get(1)); 10660 } else { 10661 llvm::APSInt ConstantLengthValue; 10662 if (!Length->EvaluateAsInt(ConstantLengthValue, Context) || 10663 ConstantLengthValue.getSExtValue() != 1) 10664 return false; 10665 10666 ArraySizes.push_back(ConstantLengthValue); 10667 } 10668 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 10669 } 10670 10671 // If we have a single element, we don't need to add the implicit lengths. 10672 if (!SingleElement) { 10673 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 10674 // Has implicit length 1! 10675 ArraySizes.push_back(llvm::APSInt::get(1)); 10676 Base = TempASE->getBase()->IgnoreParenImpCasts(); 10677 } 10678 } 10679 10680 // This array section can be privatized as a single value or as a constant 10681 // sized array. 10682 return true; 10683 } 10684 10685 static bool actOnOMPReductionKindClause( 10686 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 10687 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 10688 SourceLocation ColonLoc, SourceLocation EndLoc, 10689 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 10690 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 10691 DeclarationName DN = ReductionId.getName(); 10692 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 10693 BinaryOperatorKind BOK = BO_Comma; 10694 10695 ASTContext &Context = S.Context; 10696 // OpenMP [2.14.3.6, reduction clause] 10697 // C 10698 // reduction-identifier is either an identifier or one of the following 10699 // operators: +, -, *, &, |, ^, && and || 10700 // C++ 10701 // reduction-identifier is either an id-expression or one of the following 10702 // operators: +, -, *, &, |, ^, && and || 10703 switch (OOK) { 10704 case OO_Plus: 10705 case OO_Minus: 10706 BOK = BO_Add; 10707 break; 10708 case OO_Star: 10709 BOK = BO_Mul; 10710 break; 10711 case OO_Amp: 10712 BOK = BO_And; 10713 break; 10714 case OO_Pipe: 10715 BOK = BO_Or; 10716 break; 10717 case OO_Caret: 10718 BOK = BO_Xor; 10719 break; 10720 case OO_AmpAmp: 10721 BOK = BO_LAnd; 10722 break; 10723 case OO_PipePipe: 10724 BOK = BO_LOr; 10725 break; 10726 case OO_New: 10727 case OO_Delete: 10728 case OO_Array_New: 10729 case OO_Array_Delete: 10730 case OO_Slash: 10731 case OO_Percent: 10732 case OO_Tilde: 10733 case OO_Exclaim: 10734 case OO_Equal: 10735 case OO_Less: 10736 case OO_Greater: 10737 case OO_LessEqual: 10738 case OO_GreaterEqual: 10739 case OO_PlusEqual: 10740 case OO_MinusEqual: 10741 case OO_StarEqual: 10742 case OO_SlashEqual: 10743 case OO_PercentEqual: 10744 case OO_CaretEqual: 10745 case OO_AmpEqual: 10746 case OO_PipeEqual: 10747 case OO_LessLess: 10748 case OO_GreaterGreater: 10749 case OO_LessLessEqual: 10750 case OO_GreaterGreaterEqual: 10751 case OO_EqualEqual: 10752 case OO_ExclaimEqual: 10753 case OO_Spaceship: 10754 case OO_PlusPlus: 10755 case OO_MinusMinus: 10756 case OO_Comma: 10757 case OO_ArrowStar: 10758 case OO_Arrow: 10759 case OO_Call: 10760 case OO_Subscript: 10761 case OO_Conditional: 10762 case OO_Coawait: 10763 case NUM_OVERLOADED_OPERATORS: 10764 llvm_unreachable("Unexpected reduction identifier"); 10765 case OO_None: 10766 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 10767 if (II->isStr("max")) 10768 BOK = BO_GT; 10769 else if (II->isStr("min")) 10770 BOK = BO_LT; 10771 } 10772 break; 10773 } 10774 SourceRange ReductionIdRange; 10775 if (ReductionIdScopeSpec.isValid()) 10776 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 10777 else 10778 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 10779 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 10780 10781 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 10782 bool FirstIter = true; 10783 for (Expr *RefExpr : VarList) { 10784 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 10785 // OpenMP [2.1, C/C++] 10786 // A list item is a variable or array section, subject to the restrictions 10787 // specified in Section 2.4 on page 42 and in each of the sections 10788 // describing clauses and directives for which a list appears. 10789 // OpenMP [2.14.3.3, Restrictions, p.1] 10790 // A variable that is part of another variable (as an array or 10791 // structure element) cannot appear in a private clause. 10792 if (!FirstIter && IR != ER) 10793 ++IR; 10794 FirstIter = false; 10795 SourceLocation ELoc; 10796 SourceRange ERange; 10797 Expr *SimpleRefExpr = RefExpr; 10798 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 10799 /*AllowArraySection=*/true); 10800 if (Res.second) { 10801 // Try to find 'declare reduction' corresponding construct before using 10802 // builtin/overloaded operators. 10803 QualType Type = Context.DependentTy; 10804 CXXCastPath BasePath; 10805 ExprResult DeclareReductionRef = buildDeclareReductionRef( 10806 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 10807 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 10808 Expr *ReductionOp = nullptr; 10809 if (S.CurContext->isDependentContext() && 10810 (DeclareReductionRef.isUnset() || 10811 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 10812 ReductionOp = DeclareReductionRef.get(); 10813 // It will be analyzed later. 10814 RD.push(RefExpr, ReductionOp); 10815 } 10816 ValueDecl *D = Res.first; 10817 if (!D) 10818 continue; 10819 10820 Expr *TaskgroupDescriptor = nullptr; 10821 QualType Type; 10822 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 10823 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 10824 if (ASE) { 10825 Type = ASE->getType().getNonReferenceType(); 10826 } else if (OASE) { 10827 QualType BaseType = 10828 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 10829 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 10830 Type = ATy->getElementType(); 10831 else 10832 Type = BaseType->getPointeeType(); 10833 Type = Type.getNonReferenceType(); 10834 } else { 10835 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 10836 } 10837 auto *VD = dyn_cast<VarDecl>(D); 10838 10839 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 10840 // A variable that appears in a private clause must not have an incomplete 10841 // type or a reference type. 10842 if (S.RequireCompleteType(ELoc, D->getType(), 10843 diag::err_omp_reduction_incomplete_type)) 10844 continue; 10845 // OpenMP [2.14.3.6, reduction clause, Restrictions] 10846 // A list item that appears in a reduction clause must not be 10847 // const-qualified. 10848 if (Type.getNonReferenceType().isConstant(Context)) { 10849 S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange; 10850 if (!ASE && !OASE) { 10851 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 10852 VarDecl::DeclarationOnly; 10853 S.Diag(D->getLocation(), 10854 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10855 << D; 10856 } 10857 continue; 10858 } 10859 10860 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 10861 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 10862 // If a list-item is a reference type then it must bind to the same object 10863 // for all threads of the team. 10864 if (!ASE && !OASE) { 10865 if (VD) { 10866 VarDecl *VDDef = VD->getDefinition(); 10867 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 10868 DSARefChecker Check(Stack); 10869 if (Check.Visit(VDDef->getInit())) { 10870 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 10871 << getOpenMPClauseName(ClauseKind) << ERange; 10872 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 10873 continue; 10874 } 10875 } 10876 } 10877 10878 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 10879 // in a Construct] 10880 // Variables with the predetermined data-sharing attributes may not be 10881 // listed in data-sharing attributes clauses, except for the cases 10882 // listed below. For these exceptions only, listing a predetermined 10883 // variable in a data-sharing attribute clause is allowed and overrides 10884 // the variable's predetermined data-sharing attributes. 10885 // OpenMP [2.14.3.6, Restrictions, p.3] 10886 // Any number of reduction clauses can be specified on the directive, 10887 // but a list item can appear only once in the reduction clauses for that 10888 // directive. 10889 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 10890 if (DVar.CKind == OMPC_reduction) { 10891 S.Diag(ELoc, diag::err_omp_once_referenced) 10892 << getOpenMPClauseName(ClauseKind); 10893 if (DVar.RefExpr) 10894 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 10895 continue; 10896 } 10897 if (DVar.CKind != OMPC_unknown) { 10898 S.Diag(ELoc, diag::err_omp_wrong_dsa) 10899 << getOpenMPClauseName(DVar.CKind) 10900 << getOpenMPClauseName(OMPC_reduction); 10901 reportOriginalDsa(S, Stack, D, DVar); 10902 continue; 10903 } 10904 10905 // OpenMP [2.14.3.6, Restrictions, p.1] 10906 // A list item that appears in a reduction clause of a worksharing 10907 // construct must be shared in the parallel regions to which any of the 10908 // worksharing regions arising from the worksharing construct bind. 10909 if (isOpenMPWorksharingDirective(CurrDir) && 10910 !isOpenMPParallelDirective(CurrDir) && 10911 !isOpenMPTeamsDirective(CurrDir)) { 10912 DVar = Stack->getImplicitDSA(D, true); 10913 if (DVar.CKind != OMPC_shared) { 10914 S.Diag(ELoc, diag::err_omp_required_access) 10915 << getOpenMPClauseName(OMPC_reduction) 10916 << getOpenMPClauseName(OMPC_shared); 10917 reportOriginalDsa(S, Stack, D, DVar); 10918 continue; 10919 } 10920 } 10921 } 10922 10923 // Try to find 'declare reduction' corresponding construct before using 10924 // builtin/overloaded operators. 10925 CXXCastPath BasePath; 10926 ExprResult DeclareReductionRef = buildDeclareReductionRef( 10927 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 10928 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 10929 if (DeclareReductionRef.isInvalid()) 10930 continue; 10931 if (S.CurContext->isDependentContext() && 10932 (DeclareReductionRef.isUnset() || 10933 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 10934 RD.push(RefExpr, DeclareReductionRef.get()); 10935 continue; 10936 } 10937 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 10938 // Not allowed reduction identifier is found. 10939 S.Diag(ReductionId.getBeginLoc(), 10940 diag::err_omp_unknown_reduction_identifier) 10941 << Type << ReductionIdRange; 10942 continue; 10943 } 10944 10945 // OpenMP [2.14.3.6, reduction clause, Restrictions] 10946 // The type of a list item that appears in a reduction clause must be valid 10947 // for the reduction-identifier. For a max or min reduction in C, the type 10948 // of the list item must be an allowed arithmetic data type: char, int, 10949 // float, double, or _Bool, possibly modified with long, short, signed, or 10950 // unsigned. For a max or min reduction in C++, the type of the list item 10951 // must be an allowed arithmetic data type: char, wchar_t, int, float, 10952 // double, or bool, possibly modified with long, short, signed, or unsigned. 10953 if (DeclareReductionRef.isUnset()) { 10954 if ((BOK == BO_GT || BOK == BO_LT) && 10955 !(Type->isScalarType() || 10956 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 10957 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 10958 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 10959 if (!ASE && !OASE) { 10960 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 10961 VarDecl::DeclarationOnly; 10962 S.Diag(D->getLocation(), 10963 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10964 << D; 10965 } 10966 continue; 10967 } 10968 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 10969 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 10970 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 10971 << getOpenMPClauseName(ClauseKind); 10972 if (!ASE && !OASE) { 10973 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 10974 VarDecl::DeclarationOnly; 10975 S.Diag(D->getLocation(), 10976 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10977 << D; 10978 } 10979 continue; 10980 } 10981 } 10982 10983 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 10984 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 10985 D->hasAttrs() ? &D->getAttrs() : nullptr); 10986 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 10987 D->hasAttrs() ? &D->getAttrs() : nullptr); 10988 QualType PrivateTy = Type; 10989 10990 // Try if we can determine constant lengths for all array sections and avoid 10991 // the VLA. 10992 bool ConstantLengthOASE = false; 10993 if (OASE) { 10994 bool SingleElement; 10995 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 10996 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 10997 Context, OASE, SingleElement, ArraySizes); 10998 10999 // If we don't have a single element, we must emit a constant array type. 11000 if (ConstantLengthOASE && !SingleElement) { 11001 for (llvm::APSInt &Size : ArraySizes) 11002 PrivateTy = Context.getConstantArrayType( 11003 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0); 11004 } 11005 } 11006 11007 if ((OASE && !ConstantLengthOASE) || 11008 (!OASE && !ASE && 11009 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 11010 if (!Context.getTargetInfo().isVLASupported() && 11011 S.shouldDiagnoseTargetSupportFromOpenMP()) { 11012 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 11013 S.Diag(ELoc, diag::note_vla_unsupported); 11014 continue; 11015 } 11016 // For arrays/array sections only: 11017 // Create pseudo array type for private copy. The size for this array will 11018 // be generated during codegen. 11019 // For array subscripts or single variables Private Ty is the same as Type 11020 // (type of the variable or single array element). 11021 PrivateTy = Context.getVariableArrayType( 11022 Type, 11023 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue), 11024 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 11025 } else if (!ASE && !OASE && 11026 Context.getAsArrayType(D->getType().getNonReferenceType())) { 11027 PrivateTy = D->getType().getNonReferenceType(); 11028 } 11029 // Private copy. 11030 VarDecl *PrivateVD = 11031 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 11032 D->hasAttrs() ? &D->getAttrs() : nullptr, 11033 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 11034 // Add initializer for private variable. 11035 Expr *Init = nullptr; 11036 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 11037 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 11038 if (DeclareReductionRef.isUsable()) { 11039 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 11040 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 11041 if (DRD->getInitializer()) { 11042 Init = DRDRef; 11043 RHSVD->setInit(DRDRef); 11044 RHSVD->setInitStyle(VarDecl::CallInit); 11045 } 11046 } else { 11047 switch (BOK) { 11048 case BO_Add: 11049 case BO_Xor: 11050 case BO_Or: 11051 case BO_LOr: 11052 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 11053 if (Type->isScalarType() || Type->isAnyComplexType()) 11054 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 11055 break; 11056 case BO_Mul: 11057 case BO_LAnd: 11058 if (Type->isScalarType() || Type->isAnyComplexType()) { 11059 // '*' and '&&' reduction ops - initializer is '1'. 11060 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 11061 } 11062 break; 11063 case BO_And: { 11064 // '&' reduction op - initializer is '~0'. 11065 QualType OrigType = Type; 11066 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 11067 Type = ComplexTy->getElementType(); 11068 if (Type->isRealFloatingType()) { 11069 llvm::APFloat InitValue = 11070 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type), 11071 /*isIEEE=*/true); 11072 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 11073 Type, ELoc); 11074 } else if (Type->isScalarType()) { 11075 uint64_t Size = Context.getTypeSize(Type); 11076 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 11077 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 11078 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 11079 } 11080 if (Init && OrigType->isAnyComplexType()) { 11081 // Init = 0xFFFF + 0xFFFFi; 11082 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 11083 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 11084 } 11085 Type = OrigType; 11086 break; 11087 } 11088 case BO_LT: 11089 case BO_GT: { 11090 // 'min' reduction op - initializer is 'Largest representable number in 11091 // the reduction list item type'. 11092 // 'max' reduction op - initializer is 'Least representable number in 11093 // the reduction list item type'. 11094 if (Type->isIntegerType() || Type->isPointerType()) { 11095 bool IsSigned = Type->hasSignedIntegerRepresentation(); 11096 uint64_t Size = Context.getTypeSize(Type); 11097 QualType IntTy = 11098 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 11099 llvm::APInt InitValue = 11100 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 11101 : llvm::APInt::getMinValue(Size) 11102 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 11103 : llvm::APInt::getMaxValue(Size); 11104 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 11105 if (Type->isPointerType()) { 11106 // Cast to pointer type. 11107 ExprResult CastExpr = S.BuildCStyleCastExpr( 11108 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 11109 if (CastExpr.isInvalid()) 11110 continue; 11111 Init = CastExpr.get(); 11112 } 11113 } else if (Type->isRealFloatingType()) { 11114 llvm::APFloat InitValue = llvm::APFloat::getLargest( 11115 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 11116 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 11117 Type, ELoc); 11118 } 11119 break; 11120 } 11121 case BO_PtrMemD: 11122 case BO_PtrMemI: 11123 case BO_MulAssign: 11124 case BO_Div: 11125 case BO_Rem: 11126 case BO_Sub: 11127 case BO_Shl: 11128 case BO_Shr: 11129 case BO_LE: 11130 case BO_GE: 11131 case BO_EQ: 11132 case BO_NE: 11133 case BO_Cmp: 11134 case BO_AndAssign: 11135 case BO_XorAssign: 11136 case BO_OrAssign: 11137 case BO_Assign: 11138 case BO_AddAssign: 11139 case BO_SubAssign: 11140 case BO_DivAssign: 11141 case BO_RemAssign: 11142 case BO_ShlAssign: 11143 case BO_ShrAssign: 11144 case BO_Comma: 11145 llvm_unreachable("Unexpected reduction operation"); 11146 } 11147 } 11148 if (Init && DeclareReductionRef.isUnset()) 11149 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 11150 else if (!Init) 11151 S.ActOnUninitializedDecl(RHSVD); 11152 if (RHSVD->isInvalidDecl()) 11153 continue; 11154 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) { 11155 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 11156 << Type << ReductionIdRange; 11157 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 11158 VarDecl::DeclarationOnly; 11159 S.Diag(D->getLocation(), 11160 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11161 << D; 11162 continue; 11163 } 11164 // Store initializer for single element in private copy. Will be used during 11165 // codegen. 11166 PrivateVD->setInit(RHSVD->getInit()); 11167 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 11168 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 11169 ExprResult ReductionOp; 11170 if (DeclareReductionRef.isUsable()) { 11171 QualType RedTy = DeclareReductionRef.get()->getType(); 11172 QualType PtrRedTy = Context.getPointerType(RedTy); 11173 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 11174 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 11175 if (!BasePath.empty()) { 11176 LHS = S.DefaultLvalueConversion(LHS.get()); 11177 RHS = S.DefaultLvalueConversion(RHS.get()); 11178 LHS = ImplicitCastExpr::Create(Context, PtrRedTy, 11179 CK_UncheckedDerivedToBase, LHS.get(), 11180 &BasePath, LHS.get()->getValueKind()); 11181 RHS = ImplicitCastExpr::Create(Context, PtrRedTy, 11182 CK_UncheckedDerivedToBase, RHS.get(), 11183 &BasePath, RHS.get()->getValueKind()); 11184 } 11185 FunctionProtoType::ExtProtoInfo EPI; 11186 QualType Params[] = {PtrRedTy, PtrRedTy}; 11187 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 11188 auto *OVE = new (Context) OpaqueValueExpr( 11189 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, 11190 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 11191 Expr *Args[] = {LHS.get(), RHS.get()}; 11192 ReductionOp = new (Context) 11193 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc); 11194 } else { 11195 ReductionOp = S.BuildBinOp( 11196 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE); 11197 if (ReductionOp.isUsable()) { 11198 if (BOK != BO_LT && BOK != BO_GT) { 11199 ReductionOp = 11200 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 11201 BO_Assign, LHSDRE, ReductionOp.get()); 11202 } else { 11203 auto *ConditionalOp = new (Context) 11204 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE, 11205 Type, VK_LValue, OK_Ordinary); 11206 ReductionOp = 11207 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 11208 BO_Assign, LHSDRE, ConditionalOp); 11209 } 11210 if (ReductionOp.isUsable()) 11211 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get()); 11212 } 11213 if (!ReductionOp.isUsable()) 11214 continue; 11215 } 11216 11217 // OpenMP [2.15.4.6, Restrictions, p.2] 11218 // A list item that appears in an in_reduction clause of a task construct 11219 // must appear in a task_reduction clause of a construct associated with a 11220 // taskgroup region that includes the participating task in its taskgroup 11221 // set. The construct associated with the innermost region that meets this 11222 // condition must specify the same reduction-identifier as the in_reduction 11223 // clause. 11224 if (ClauseKind == OMPC_in_reduction) { 11225 SourceRange ParentSR; 11226 BinaryOperatorKind ParentBOK; 11227 const Expr *ParentReductionOp; 11228 Expr *ParentBOKTD, *ParentReductionOpTD; 11229 DSAStackTy::DSAVarData ParentBOKDSA = 11230 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 11231 ParentBOKTD); 11232 DSAStackTy::DSAVarData ParentReductionOpDSA = 11233 Stack->getTopMostTaskgroupReductionData( 11234 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 11235 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 11236 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 11237 if (!IsParentBOK && !IsParentReductionOp) { 11238 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction); 11239 continue; 11240 } 11241 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 11242 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK || 11243 IsParentReductionOp) { 11244 bool EmitError = true; 11245 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 11246 llvm::FoldingSetNodeID RedId, ParentRedId; 11247 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 11248 DeclareReductionRef.get()->Profile(RedId, Context, 11249 /*Canonical=*/true); 11250 EmitError = RedId != ParentRedId; 11251 } 11252 if (EmitError) { 11253 S.Diag(ReductionId.getBeginLoc(), 11254 diag::err_omp_reduction_identifier_mismatch) 11255 << ReductionIdRange << RefExpr->getSourceRange(); 11256 S.Diag(ParentSR.getBegin(), 11257 diag::note_omp_previous_reduction_identifier) 11258 << ParentSR 11259 << (IsParentBOK ? ParentBOKDSA.RefExpr 11260 : ParentReductionOpDSA.RefExpr) 11261 ->getSourceRange(); 11262 continue; 11263 } 11264 } 11265 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 11266 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined."); 11267 } 11268 11269 DeclRefExpr *Ref = nullptr; 11270 Expr *VarsExpr = RefExpr->IgnoreParens(); 11271 if (!VD && !S.CurContext->isDependentContext()) { 11272 if (ASE || OASE) { 11273 TransformExprToCaptures RebuildToCapture(S, D); 11274 VarsExpr = 11275 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 11276 Ref = RebuildToCapture.getCapturedExpr(); 11277 } else { 11278 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 11279 } 11280 if (!S.isOpenMPCapturedDecl(D)) { 11281 RD.ExprCaptures.emplace_back(Ref->getDecl()); 11282 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 11283 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 11284 if (!RefRes.isUsable()) 11285 continue; 11286 ExprResult PostUpdateRes = 11287 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 11288 RefRes.get()); 11289 if (!PostUpdateRes.isUsable()) 11290 continue; 11291 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 11292 Stack->getCurrentDirective() == OMPD_taskgroup) { 11293 S.Diag(RefExpr->getExprLoc(), 11294 diag::err_omp_reduction_non_addressable_expression) 11295 << RefExpr->getSourceRange(); 11296 continue; 11297 } 11298 RD.ExprPostUpdates.emplace_back( 11299 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 11300 } 11301 } 11302 } 11303 // All reduction items are still marked as reduction (to do not increase 11304 // code base size). 11305 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref); 11306 if (CurrDir == OMPD_taskgroup) { 11307 if (DeclareReductionRef.isUsable()) 11308 Stack->addTaskgroupReductionData(D, ReductionIdRange, 11309 DeclareReductionRef.get()); 11310 else 11311 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 11312 } 11313 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 11314 TaskgroupDescriptor); 11315 } 11316 return RD.Vars.empty(); 11317 } 11318 11319 OMPClause *Sema::ActOnOpenMPReductionClause( 11320 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 11321 SourceLocation ColonLoc, SourceLocation EndLoc, 11322 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 11323 ArrayRef<Expr *> UnresolvedReductions) { 11324 ReductionData RD(VarList.size()); 11325 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 11326 StartLoc, LParenLoc, ColonLoc, EndLoc, 11327 ReductionIdScopeSpec, ReductionId, 11328 UnresolvedReductions, RD)) 11329 return nullptr; 11330 11331 return OMPReductionClause::Create( 11332 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 11333 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 11334 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 11335 buildPreInits(Context, RD.ExprCaptures), 11336 buildPostUpdate(*this, RD.ExprPostUpdates)); 11337 } 11338 11339 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 11340 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 11341 SourceLocation ColonLoc, SourceLocation EndLoc, 11342 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 11343 ArrayRef<Expr *> UnresolvedReductions) { 11344 ReductionData RD(VarList.size()); 11345 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 11346 StartLoc, LParenLoc, ColonLoc, EndLoc, 11347 ReductionIdScopeSpec, ReductionId, 11348 UnresolvedReductions, RD)) 11349 return nullptr; 11350 11351 return OMPTaskReductionClause::Create( 11352 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 11353 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 11354 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 11355 buildPreInits(Context, RD.ExprCaptures), 11356 buildPostUpdate(*this, RD.ExprPostUpdates)); 11357 } 11358 11359 OMPClause *Sema::ActOnOpenMPInReductionClause( 11360 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 11361 SourceLocation ColonLoc, SourceLocation EndLoc, 11362 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 11363 ArrayRef<Expr *> UnresolvedReductions) { 11364 ReductionData RD(VarList.size()); 11365 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 11366 StartLoc, LParenLoc, ColonLoc, EndLoc, 11367 ReductionIdScopeSpec, ReductionId, 11368 UnresolvedReductions, RD)) 11369 return nullptr; 11370 11371 return OMPInReductionClause::Create( 11372 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 11373 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 11374 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 11375 buildPreInits(Context, RD.ExprCaptures), 11376 buildPostUpdate(*this, RD.ExprPostUpdates)); 11377 } 11378 11379 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 11380 SourceLocation LinLoc) { 11381 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 11382 LinKind == OMPC_LINEAR_unknown) { 11383 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 11384 return true; 11385 } 11386 return false; 11387 } 11388 11389 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 11390 OpenMPLinearClauseKind LinKind, 11391 QualType Type) { 11392 const auto *VD = dyn_cast_or_null<VarDecl>(D); 11393 // A variable must not have an incomplete type or a reference type. 11394 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 11395 return true; 11396 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 11397 !Type->isReferenceType()) { 11398 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 11399 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 11400 return true; 11401 } 11402 Type = Type.getNonReferenceType(); 11403 11404 // A list item must not be const-qualified. 11405 if (Type.isConstant(Context)) { 11406 Diag(ELoc, diag::err_omp_const_variable) 11407 << getOpenMPClauseName(OMPC_linear); 11408 if (D) { 11409 bool IsDecl = 11410 !VD || 11411 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 11412 Diag(D->getLocation(), 11413 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11414 << D; 11415 } 11416 return true; 11417 } 11418 11419 // A list item must be of integral or pointer type. 11420 Type = Type.getUnqualifiedType().getCanonicalType(); 11421 const auto *Ty = Type.getTypePtrOrNull(); 11422 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && 11423 !Ty->isPointerType())) { 11424 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 11425 if (D) { 11426 bool IsDecl = 11427 !VD || 11428 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 11429 Diag(D->getLocation(), 11430 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11431 << D; 11432 } 11433 return true; 11434 } 11435 return false; 11436 } 11437 11438 OMPClause *Sema::ActOnOpenMPLinearClause( 11439 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 11440 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 11441 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 11442 SmallVector<Expr *, 8> Vars; 11443 SmallVector<Expr *, 8> Privates; 11444 SmallVector<Expr *, 8> Inits; 11445 SmallVector<Decl *, 4> ExprCaptures; 11446 SmallVector<Expr *, 4> ExprPostUpdates; 11447 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 11448 LinKind = OMPC_LINEAR_val; 11449 for (Expr *RefExpr : VarList) { 11450 assert(RefExpr && "NULL expr in OpenMP linear clause."); 11451 SourceLocation ELoc; 11452 SourceRange ERange; 11453 Expr *SimpleRefExpr = RefExpr; 11454 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 11455 if (Res.second) { 11456 // It will be analyzed later. 11457 Vars.push_back(RefExpr); 11458 Privates.push_back(nullptr); 11459 Inits.push_back(nullptr); 11460 } 11461 ValueDecl *D = Res.first; 11462 if (!D) 11463 continue; 11464 11465 QualType Type = D->getType(); 11466 auto *VD = dyn_cast<VarDecl>(D); 11467 11468 // OpenMP [2.14.3.7, linear clause] 11469 // A list-item cannot appear in more than one linear clause. 11470 // A list-item that appears in a linear clause cannot appear in any 11471 // other data-sharing attribute clause. 11472 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 11473 if (DVar.RefExpr) { 11474 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 11475 << getOpenMPClauseName(OMPC_linear); 11476 reportOriginalDsa(*this, DSAStack, D, DVar); 11477 continue; 11478 } 11479 11480 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 11481 continue; 11482 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 11483 11484 // Build private copy of original var. 11485 VarDecl *Private = 11486 buildVarDecl(*this, ELoc, Type, D->getName(), 11487 D->hasAttrs() ? &D->getAttrs() : nullptr, 11488 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 11489 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 11490 // Build var to save initial value. 11491 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 11492 Expr *InitExpr; 11493 DeclRefExpr *Ref = nullptr; 11494 if (!VD && !CurContext->isDependentContext()) { 11495 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 11496 if (!isOpenMPCapturedDecl(D)) { 11497 ExprCaptures.push_back(Ref->getDecl()); 11498 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 11499 ExprResult RefRes = DefaultLvalueConversion(Ref); 11500 if (!RefRes.isUsable()) 11501 continue; 11502 ExprResult PostUpdateRes = 11503 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 11504 SimpleRefExpr, RefRes.get()); 11505 if (!PostUpdateRes.isUsable()) 11506 continue; 11507 ExprPostUpdates.push_back( 11508 IgnoredValueConversions(PostUpdateRes.get()).get()); 11509 } 11510 } 11511 } 11512 if (LinKind == OMPC_LINEAR_uval) 11513 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 11514 else 11515 InitExpr = VD ? SimpleRefExpr : Ref; 11516 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 11517 /*DirectInit=*/false); 11518 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 11519 11520 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 11521 Vars.push_back((VD || CurContext->isDependentContext()) 11522 ? RefExpr->IgnoreParens() 11523 : Ref); 11524 Privates.push_back(PrivateRef); 11525 Inits.push_back(InitRef); 11526 } 11527 11528 if (Vars.empty()) 11529 return nullptr; 11530 11531 Expr *StepExpr = Step; 11532 Expr *CalcStepExpr = nullptr; 11533 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 11534 !Step->isInstantiationDependent() && 11535 !Step->containsUnexpandedParameterPack()) { 11536 SourceLocation StepLoc = Step->getBeginLoc(); 11537 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 11538 if (Val.isInvalid()) 11539 return nullptr; 11540 StepExpr = Val.get(); 11541 11542 // Build var to save the step value. 11543 VarDecl *SaveVar = 11544 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 11545 ExprResult SaveRef = 11546 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 11547 ExprResult CalcStep = 11548 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 11549 CalcStep = ActOnFinishFullExpr(CalcStep.get()); 11550 11551 // Warn about zero linear step (it would be probably better specified as 11552 // making corresponding variables 'const'). 11553 llvm::APSInt Result; 11554 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); 11555 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) 11556 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] 11557 << (Vars.size() > 1); 11558 if (!IsConstant && CalcStep.isUsable()) { 11559 // Calculate the step beforehand instead of doing this on each iteration. 11560 // (This is not used if the number of iterations may be kfold-ed). 11561 CalcStepExpr = CalcStep.get(); 11562 } 11563 } 11564 11565 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 11566 ColonLoc, EndLoc, Vars, Privates, Inits, 11567 StepExpr, CalcStepExpr, 11568 buildPreInits(Context, ExprCaptures), 11569 buildPostUpdate(*this, ExprPostUpdates)); 11570 } 11571 11572 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 11573 Expr *NumIterations, Sema &SemaRef, 11574 Scope *S, DSAStackTy *Stack) { 11575 // Walk the vars and build update/final expressions for the CodeGen. 11576 SmallVector<Expr *, 8> Updates; 11577 SmallVector<Expr *, 8> Finals; 11578 Expr *Step = Clause.getStep(); 11579 Expr *CalcStep = Clause.getCalcStep(); 11580 // OpenMP [2.14.3.7, linear clause] 11581 // If linear-step is not specified it is assumed to be 1. 11582 if (!Step) 11583 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 11584 else if (CalcStep) 11585 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 11586 bool HasErrors = false; 11587 auto CurInit = Clause.inits().begin(); 11588 auto CurPrivate = Clause.privates().begin(); 11589 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 11590 for (Expr *RefExpr : Clause.varlists()) { 11591 SourceLocation ELoc; 11592 SourceRange ERange; 11593 Expr *SimpleRefExpr = RefExpr; 11594 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange); 11595 ValueDecl *D = Res.first; 11596 if (Res.second || !D) { 11597 Updates.push_back(nullptr); 11598 Finals.push_back(nullptr); 11599 HasErrors = true; 11600 continue; 11601 } 11602 auto &&Info = Stack->isLoopControlVariable(D); 11603 // OpenMP [2.15.11, distribute simd Construct] 11604 // A list item may not appear in a linear clause, unless it is the loop 11605 // iteration variable. 11606 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 11607 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 11608 SemaRef.Diag(ELoc, 11609 diag::err_omp_linear_distribute_var_non_loop_iteration); 11610 Updates.push_back(nullptr); 11611 Finals.push_back(nullptr); 11612 HasErrors = true; 11613 continue; 11614 } 11615 Expr *InitExpr = *CurInit; 11616 11617 // Build privatized reference to the current linear var. 11618 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 11619 Expr *CapturedRef; 11620 if (LinKind == OMPC_LINEAR_uval) 11621 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 11622 else 11623 CapturedRef = 11624 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 11625 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 11626 /*RefersToCapture=*/true); 11627 11628 // Build update: Var = InitExpr + IV * Step 11629 ExprResult Update; 11630 if (!Info.first) 11631 Update = 11632 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, 11633 InitExpr, IV, Step, /* Subtract */ false); 11634 else 11635 Update = *CurPrivate; 11636 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(), 11637 /*DiscardedValue=*/true); 11638 11639 // Build final: Var = InitExpr + NumIterations * Step 11640 ExprResult Final; 11641 if (!Info.first) 11642 Final = 11643 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 11644 InitExpr, NumIterations, Step, /*Subtract=*/false); 11645 else 11646 Final = *CurPrivate; 11647 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(), 11648 /*DiscardedValue=*/true); 11649 11650 if (!Update.isUsable() || !Final.isUsable()) { 11651 Updates.push_back(nullptr); 11652 Finals.push_back(nullptr); 11653 HasErrors = true; 11654 } else { 11655 Updates.push_back(Update.get()); 11656 Finals.push_back(Final.get()); 11657 } 11658 ++CurInit; 11659 ++CurPrivate; 11660 } 11661 Clause.setUpdates(Updates); 11662 Clause.setFinals(Finals); 11663 return HasErrors; 11664 } 11665 11666 OMPClause *Sema::ActOnOpenMPAlignedClause( 11667 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 11668 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 11669 SmallVector<Expr *, 8> Vars; 11670 for (Expr *RefExpr : VarList) { 11671 assert(RefExpr && "NULL expr in OpenMP linear clause."); 11672 SourceLocation ELoc; 11673 SourceRange ERange; 11674 Expr *SimpleRefExpr = RefExpr; 11675 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 11676 if (Res.second) { 11677 // It will be analyzed later. 11678 Vars.push_back(RefExpr); 11679 } 11680 ValueDecl *D = Res.first; 11681 if (!D) 11682 continue; 11683 11684 QualType QType = D->getType(); 11685 auto *VD = dyn_cast<VarDecl>(D); 11686 11687 // OpenMP [2.8.1, simd construct, Restrictions] 11688 // The type of list items appearing in the aligned clause must be 11689 // array, pointer, reference to array, or reference to pointer. 11690 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 11691 const Type *Ty = QType.getTypePtrOrNull(); 11692 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 11693 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 11694 << QType << getLangOpts().CPlusPlus << ERange; 11695 bool IsDecl = 11696 !VD || 11697 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 11698 Diag(D->getLocation(), 11699 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11700 << D; 11701 continue; 11702 } 11703 11704 // OpenMP [2.8.1, simd construct, Restrictions] 11705 // A list-item cannot appear in more than one aligned clause. 11706 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 11707 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange; 11708 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 11709 << getOpenMPClauseName(OMPC_aligned); 11710 continue; 11711 } 11712 11713 DeclRefExpr *Ref = nullptr; 11714 if (!VD && isOpenMPCapturedDecl(D)) 11715 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 11716 Vars.push_back(DefaultFunctionArrayConversion( 11717 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 11718 .get()); 11719 } 11720 11721 // OpenMP [2.8.1, simd construct, Description] 11722 // The parameter of the aligned clause, alignment, must be a constant 11723 // positive integer expression. 11724 // If no optional parameter is specified, implementation-defined default 11725 // alignments for SIMD instructions on the target platforms are assumed. 11726 if (Alignment != nullptr) { 11727 ExprResult AlignResult = 11728 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 11729 if (AlignResult.isInvalid()) 11730 return nullptr; 11731 Alignment = AlignResult.get(); 11732 } 11733 if (Vars.empty()) 11734 return nullptr; 11735 11736 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 11737 EndLoc, Vars, Alignment); 11738 } 11739 11740 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 11741 SourceLocation StartLoc, 11742 SourceLocation LParenLoc, 11743 SourceLocation EndLoc) { 11744 SmallVector<Expr *, 8> Vars; 11745 SmallVector<Expr *, 8> SrcExprs; 11746 SmallVector<Expr *, 8> DstExprs; 11747 SmallVector<Expr *, 8> AssignmentOps; 11748 for (Expr *RefExpr : VarList) { 11749 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 11750 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 11751 // It will be analyzed later. 11752 Vars.push_back(RefExpr); 11753 SrcExprs.push_back(nullptr); 11754 DstExprs.push_back(nullptr); 11755 AssignmentOps.push_back(nullptr); 11756 continue; 11757 } 11758 11759 SourceLocation ELoc = RefExpr->getExprLoc(); 11760 // OpenMP [2.1, C/C++] 11761 // A list item is a variable name. 11762 // OpenMP [2.14.4.1, Restrictions, p.1] 11763 // A list item that appears in a copyin clause must be threadprivate. 11764 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 11765 if (!DE || !isa<VarDecl>(DE->getDecl())) { 11766 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 11767 << 0 << RefExpr->getSourceRange(); 11768 continue; 11769 } 11770 11771 Decl *D = DE->getDecl(); 11772 auto *VD = cast<VarDecl>(D); 11773 11774 QualType Type = VD->getType(); 11775 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 11776 // It will be analyzed later. 11777 Vars.push_back(DE); 11778 SrcExprs.push_back(nullptr); 11779 DstExprs.push_back(nullptr); 11780 AssignmentOps.push_back(nullptr); 11781 continue; 11782 } 11783 11784 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 11785 // A list item that appears in a copyin clause must be threadprivate. 11786 if (!DSAStack->isThreadPrivate(VD)) { 11787 Diag(ELoc, diag::err_omp_required_access) 11788 << getOpenMPClauseName(OMPC_copyin) 11789 << getOpenMPDirectiveName(OMPD_threadprivate); 11790 continue; 11791 } 11792 11793 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 11794 // A variable of class type (or array thereof) that appears in a 11795 // copyin clause requires an accessible, unambiguous copy assignment 11796 // operator for the class type. 11797 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 11798 VarDecl *SrcVD = 11799 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(), 11800 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 11801 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 11802 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 11803 VarDecl *DstVD = 11804 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst", 11805 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 11806 DeclRefExpr *PseudoDstExpr = 11807 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 11808 // For arrays generate assignment operation for single element and replace 11809 // it by the original array element in CodeGen. 11810 ExprResult AssignmentOp = 11811 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 11812 PseudoSrcExpr); 11813 if (AssignmentOp.isInvalid()) 11814 continue; 11815 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 11816 /*DiscardedValue=*/true); 11817 if (AssignmentOp.isInvalid()) 11818 continue; 11819 11820 DSAStack->addDSA(VD, DE, OMPC_copyin); 11821 Vars.push_back(DE); 11822 SrcExprs.push_back(PseudoSrcExpr); 11823 DstExprs.push_back(PseudoDstExpr); 11824 AssignmentOps.push_back(AssignmentOp.get()); 11825 } 11826 11827 if (Vars.empty()) 11828 return nullptr; 11829 11830 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 11831 SrcExprs, DstExprs, AssignmentOps); 11832 } 11833 11834 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 11835 SourceLocation StartLoc, 11836 SourceLocation LParenLoc, 11837 SourceLocation EndLoc) { 11838 SmallVector<Expr *, 8> Vars; 11839 SmallVector<Expr *, 8> SrcExprs; 11840 SmallVector<Expr *, 8> DstExprs; 11841 SmallVector<Expr *, 8> AssignmentOps; 11842 for (Expr *RefExpr : VarList) { 11843 assert(RefExpr && "NULL expr in OpenMP linear clause."); 11844 SourceLocation ELoc; 11845 SourceRange ERange; 11846 Expr *SimpleRefExpr = RefExpr; 11847 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 11848 if (Res.second) { 11849 // It will be analyzed later. 11850 Vars.push_back(RefExpr); 11851 SrcExprs.push_back(nullptr); 11852 DstExprs.push_back(nullptr); 11853 AssignmentOps.push_back(nullptr); 11854 } 11855 ValueDecl *D = Res.first; 11856 if (!D) 11857 continue; 11858 11859 QualType Type = D->getType(); 11860 auto *VD = dyn_cast<VarDecl>(D); 11861 11862 // OpenMP [2.14.4.2, Restrictions, p.2] 11863 // A list item that appears in a copyprivate clause may not appear in a 11864 // private or firstprivate clause on the single construct. 11865 if (!VD || !DSAStack->isThreadPrivate(VD)) { 11866 DSAStackTy::DSAVarData DVar = 11867 DSAStack->getTopDSA(D, /*FromParent=*/false); 11868 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 11869 DVar.RefExpr) { 11870 Diag(ELoc, diag::err_omp_wrong_dsa) 11871 << getOpenMPClauseName(DVar.CKind) 11872 << getOpenMPClauseName(OMPC_copyprivate); 11873 reportOriginalDsa(*this, DSAStack, D, DVar); 11874 continue; 11875 } 11876 11877 // OpenMP [2.11.4.2, Restrictions, p.1] 11878 // All list items that appear in a copyprivate clause must be either 11879 // threadprivate or private in the enclosing context. 11880 if (DVar.CKind == OMPC_unknown) { 11881 DVar = DSAStack->getImplicitDSA(D, false); 11882 if (DVar.CKind == OMPC_shared) { 11883 Diag(ELoc, diag::err_omp_required_access) 11884 << getOpenMPClauseName(OMPC_copyprivate) 11885 << "threadprivate or private in the enclosing context"; 11886 reportOriginalDsa(*this, DSAStack, D, DVar); 11887 continue; 11888 } 11889 } 11890 } 11891 11892 // Variably modified types are not supported. 11893 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 11894 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 11895 << getOpenMPClauseName(OMPC_copyprivate) << Type 11896 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 11897 bool IsDecl = 11898 !VD || 11899 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 11900 Diag(D->getLocation(), 11901 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11902 << D; 11903 continue; 11904 } 11905 11906 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 11907 // A variable of class type (or array thereof) that appears in a 11908 // copyin clause requires an accessible, unambiguous copy assignment 11909 // operator for the class type. 11910 Type = Context.getBaseElementType(Type.getNonReferenceType()) 11911 .getUnqualifiedType(); 11912 VarDecl *SrcVD = 11913 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src", 11914 D->hasAttrs() ? &D->getAttrs() : nullptr); 11915 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 11916 VarDecl *DstVD = 11917 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst", 11918 D->hasAttrs() ? &D->getAttrs() : nullptr); 11919 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 11920 ExprResult AssignmentOp = BuildBinOp( 11921 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 11922 if (AssignmentOp.isInvalid()) 11923 continue; 11924 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc, 11925 /*DiscardedValue=*/true); 11926 if (AssignmentOp.isInvalid()) 11927 continue; 11928 11929 // No need to mark vars as copyprivate, they are already threadprivate or 11930 // implicitly private. 11931 assert(VD || isOpenMPCapturedDecl(D)); 11932 Vars.push_back( 11933 VD ? RefExpr->IgnoreParens() 11934 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 11935 SrcExprs.push_back(PseudoSrcExpr); 11936 DstExprs.push_back(PseudoDstExpr); 11937 AssignmentOps.push_back(AssignmentOp.get()); 11938 } 11939 11940 if (Vars.empty()) 11941 return nullptr; 11942 11943 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 11944 Vars, SrcExprs, DstExprs, AssignmentOps); 11945 } 11946 11947 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 11948 SourceLocation StartLoc, 11949 SourceLocation LParenLoc, 11950 SourceLocation EndLoc) { 11951 if (VarList.empty()) 11952 return nullptr; 11953 11954 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 11955 } 11956 11957 OMPClause * 11958 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind, 11959 SourceLocation DepLoc, SourceLocation ColonLoc, 11960 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 11961 SourceLocation LParenLoc, SourceLocation EndLoc) { 11962 if (DSAStack->getCurrentDirective() == OMPD_ordered && 11963 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 11964 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 11965 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 11966 return nullptr; 11967 } 11968 if (DSAStack->getCurrentDirective() != OMPD_ordered && 11969 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 11970 DepKind == OMPC_DEPEND_sink)) { 11971 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink}; 11972 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 11973 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 11974 /*Last=*/OMPC_DEPEND_unknown, Except) 11975 << getOpenMPClauseName(OMPC_depend); 11976 return nullptr; 11977 } 11978 SmallVector<Expr *, 8> Vars; 11979 DSAStackTy::OperatorOffsetTy OpsOffs; 11980 llvm::APSInt DepCounter(/*BitWidth=*/32); 11981 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 11982 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) { 11983 if (const Expr *OrderedCountExpr = 11984 DSAStack->getParentOrderedRegionParam().first) { 11985 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 11986 TotalDepCount.setIsUnsigned(/*Val=*/true); 11987 } 11988 } 11989 for (Expr *RefExpr : VarList) { 11990 assert(RefExpr && "NULL expr in OpenMP shared clause."); 11991 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 11992 // It will be analyzed later. 11993 Vars.push_back(RefExpr); 11994 continue; 11995 } 11996 11997 SourceLocation ELoc = RefExpr->getExprLoc(); 11998 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 11999 if (DepKind == OMPC_DEPEND_sink) { 12000 if (DSAStack->getParentOrderedRegionParam().first && 12001 DepCounter >= TotalDepCount) { 12002 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 12003 continue; 12004 } 12005 ++DepCounter; 12006 // OpenMP [2.13.9, Summary] 12007 // depend(dependence-type : vec), where dependence-type is: 12008 // 'sink' and where vec is the iteration vector, which has the form: 12009 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 12010 // where n is the value specified by the ordered clause in the loop 12011 // directive, xi denotes the loop iteration variable of the i-th nested 12012 // loop associated with the loop directive, and di is a constant 12013 // non-negative integer. 12014 if (CurContext->isDependentContext()) { 12015 // It will be analyzed later. 12016 Vars.push_back(RefExpr); 12017 continue; 12018 } 12019 SimpleExpr = SimpleExpr->IgnoreImplicit(); 12020 OverloadedOperatorKind OOK = OO_None; 12021 SourceLocation OOLoc; 12022 Expr *LHS = SimpleExpr; 12023 Expr *RHS = nullptr; 12024 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 12025 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 12026 OOLoc = BO->getOperatorLoc(); 12027 LHS = BO->getLHS()->IgnoreParenImpCasts(); 12028 RHS = BO->getRHS()->IgnoreParenImpCasts(); 12029 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 12030 OOK = OCE->getOperator(); 12031 OOLoc = OCE->getOperatorLoc(); 12032 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 12033 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 12034 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 12035 OOK = MCE->getMethodDecl() 12036 ->getNameInfo() 12037 .getName() 12038 .getCXXOverloadedOperator(); 12039 OOLoc = MCE->getCallee()->getExprLoc(); 12040 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 12041 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 12042 } 12043 SourceLocation ELoc; 12044 SourceRange ERange; 12045 auto Res = getPrivateItem(*this, LHS, ELoc, ERange); 12046 if (Res.second) { 12047 // It will be analyzed later. 12048 Vars.push_back(RefExpr); 12049 } 12050 ValueDecl *D = Res.first; 12051 if (!D) 12052 continue; 12053 12054 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 12055 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 12056 continue; 12057 } 12058 if (RHS) { 12059 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 12060 RHS, OMPC_depend, /*StrictlyPositive=*/false); 12061 if (RHSRes.isInvalid()) 12062 continue; 12063 } 12064 if (!CurContext->isDependentContext() && 12065 DSAStack->getParentOrderedRegionParam().first && 12066 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 12067 const ValueDecl *VD = 12068 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 12069 if (VD) 12070 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 12071 << 1 << VD; 12072 else 12073 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 12074 continue; 12075 } 12076 OpsOffs.emplace_back(RHS, OOK); 12077 } else { 12078 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 12079 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 12080 (ASE && 12081 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() && 12082 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 12083 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 12084 << RefExpr->getSourceRange(); 12085 continue; 12086 } 12087 bool Suppress = getDiagnostics().getSuppressAllDiagnostics(); 12088 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 12089 ExprResult Res = 12090 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts()); 12091 getDiagnostics().setSuppressAllDiagnostics(Suppress); 12092 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) { 12093 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 12094 << RefExpr->getSourceRange(); 12095 continue; 12096 } 12097 } 12098 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 12099 } 12100 12101 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 12102 TotalDepCount > VarList.size() && 12103 DSAStack->getParentOrderedRegionParam().first && 12104 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 12105 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 12106 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 12107 } 12108 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 12109 Vars.empty()) 12110 return nullptr; 12111 12112 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 12113 DepKind, DepLoc, ColonLoc, Vars, 12114 TotalDepCount.getZExtValue()); 12115 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 12116 DSAStack->isParentOrderedRegion()) 12117 DSAStack->addDoacrossDependClause(C, OpsOffs); 12118 return C; 12119 } 12120 12121 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc, 12122 SourceLocation LParenLoc, 12123 SourceLocation EndLoc) { 12124 Expr *ValExpr = Device; 12125 Stmt *HelperValStmt = nullptr; 12126 12127 // OpenMP [2.9.1, Restrictions] 12128 // The device expression must evaluate to a non-negative integer value. 12129 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 12130 /*StrictlyPositive=*/false)) 12131 return nullptr; 12132 12133 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12134 OpenMPDirectiveKind CaptureRegion = 12135 getOpenMPCaptureRegionForClause(DKind, OMPC_device); 12136 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12137 ValExpr = MakeFullExpr(ValExpr).get(); 12138 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12139 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12140 HelperValStmt = buildPreInits(Context, Captures); 12141 } 12142 12143 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion, 12144 StartLoc, LParenLoc, EndLoc); 12145 } 12146 12147 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 12148 DSAStackTy *Stack, QualType QTy, 12149 bool FullCheck = true) { 12150 NamedDecl *ND; 12151 if (QTy->isIncompleteType(&ND)) { 12152 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 12153 return false; 12154 } 12155 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 12156 !QTy.isTrivialType(SemaRef.Context)) 12157 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 12158 return true; 12159 } 12160 12161 /// Return true if it can be proven that the provided array expression 12162 /// (array section or array subscript) does NOT specify the whole size of the 12163 /// array whose base type is \a BaseQTy. 12164 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 12165 const Expr *E, 12166 QualType BaseQTy) { 12167 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 12168 12169 // If this is an array subscript, it refers to the whole size if the size of 12170 // the dimension is constant and equals 1. Also, an array section assumes the 12171 // format of an array subscript if no colon is used. 12172 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) { 12173 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 12174 return ATy->getSize().getSExtValue() != 1; 12175 // Size can't be evaluated statically. 12176 return false; 12177 } 12178 12179 assert(OASE && "Expecting array section if not an array subscript."); 12180 const Expr *LowerBound = OASE->getLowerBound(); 12181 const Expr *Length = OASE->getLength(); 12182 12183 // If there is a lower bound that does not evaluates to zero, we are not 12184 // covering the whole dimension. 12185 if (LowerBound) { 12186 llvm::APSInt ConstLowerBound; 12187 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext())) 12188 return false; // Can't get the integer value as a constant. 12189 if (ConstLowerBound.getSExtValue()) 12190 return true; 12191 } 12192 12193 // If we don't have a length we covering the whole dimension. 12194 if (!Length) 12195 return false; 12196 12197 // If the base is a pointer, we don't have a way to get the size of the 12198 // pointee. 12199 if (BaseQTy->isPointerType()) 12200 return false; 12201 12202 // We can only check if the length is the same as the size of the dimension 12203 // if we have a constant array. 12204 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 12205 if (!CATy) 12206 return false; 12207 12208 llvm::APSInt ConstLength; 12209 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext())) 12210 return false; // Can't get the integer value as a constant. 12211 12212 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 12213 } 12214 12215 // Return true if it can be proven that the provided array expression (array 12216 // section or array subscript) does NOT specify a single element of the array 12217 // whose base type is \a BaseQTy. 12218 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 12219 const Expr *E, 12220 QualType BaseQTy) { 12221 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 12222 12223 // An array subscript always refer to a single element. Also, an array section 12224 // assumes the format of an array subscript if no colon is used. 12225 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) 12226 return false; 12227 12228 assert(OASE && "Expecting array section if not an array subscript."); 12229 const Expr *Length = OASE->getLength(); 12230 12231 // If we don't have a length we have to check if the array has unitary size 12232 // for this dimension. Also, we should always expect a length if the base type 12233 // is pointer. 12234 if (!Length) { 12235 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 12236 return ATy->getSize().getSExtValue() != 1; 12237 // We cannot assume anything. 12238 return false; 12239 } 12240 12241 // Check if the length evaluates to 1. 12242 llvm::APSInt ConstLength; 12243 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext())) 12244 return false; // Can't get the integer value as a constant. 12245 12246 return ConstLength.getSExtValue() != 1; 12247 } 12248 12249 // Return the expression of the base of the mappable expression or null if it 12250 // cannot be determined and do all the necessary checks to see if the expression 12251 // is valid as a standalone mappable expression. In the process, record all the 12252 // components of the expression. 12253 static const Expr *checkMapClauseExpressionBase( 12254 Sema &SemaRef, Expr *E, 12255 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 12256 OpenMPClauseKind CKind, bool NoDiagnose) { 12257 SourceLocation ELoc = E->getExprLoc(); 12258 SourceRange ERange = E->getSourceRange(); 12259 12260 // The base of elements of list in a map clause have to be either: 12261 // - a reference to variable or field. 12262 // - a member expression. 12263 // - an array expression. 12264 // 12265 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 12266 // reference to 'r'. 12267 // 12268 // If we have: 12269 // 12270 // struct SS { 12271 // Bla S; 12272 // foo() { 12273 // #pragma omp target map (S.Arr[:12]); 12274 // } 12275 // } 12276 // 12277 // We want to retrieve the member expression 'this->S'; 12278 12279 const Expr *RelevantExpr = nullptr; 12280 12281 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2] 12282 // If a list item is an array section, it must specify contiguous storage. 12283 // 12284 // For this restriction it is sufficient that we make sure only references 12285 // to variables or fields and array expressions, and that no array sections 12286 // exist except in the rightmost expression (unless they cover the whole 12287 // dimension of the array). E.g. these would be invalid: 12288 // 12289 // r.ArrS[3:5].Arr[6:7] 12290 // 12291 // r.ArrS[3:5].x 12292 // 12293 // but these would be valid: 12294 // r.ArrS[3].Arr[6:7] 12295 // 12296 // r.ArrS[3].x 12297 12298 bool AllowUnitySizeArraySection = true; 12299 bool AllowWholeSizeArraySection = true; 12300 12301 while (!RelevantExpr) { 12302 E = E->IgnoreParenImpCasts(); 12303 12304 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) { 12305 if (!isa<VarDecl>(CurE->getDecl())) 12306 return nullptr; 12307 12308 RelevantExpr = CurE; 12309 12310 // If we got a reference to a declaration, we should not expect any array 12311 // section before that. 12312 AllowUnitySizeArraySection = false; 12313 AllowWholeSizeArraySection = false; 12314 12315 // Record the component. 12316 CurComponents.emplace_back(CurE, CurE->getDecl()); 12317 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) { 12318 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts(); 12319 12320 if (isa<CXXThisExpr>(BaseE)) 12321 // We found a base expression: this->Val. 12322 RelevantExpr = CurE; 12323 else 12324 E = BaseE; 12325 12326 if (!isa<FieldDecl>(CurE->getMemberDecl())) { 12327 if (!NoDiagnose) { 12328 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 12329 << CurE->getSourceRange(); 12330 return nullptr; 12331 } 12332 if (RelevantExpr) 12333 return nullptr; 12334 continue; 12335 } 12336 12337 auto *FD = cast<FieldDecl>(CurE->getMemberDecl()); 12338 12339 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 12340 // A bit-field cannot appear in a map clause. 12341 // 12342 if (FD->isBitField()) { 12343 if (!NoDiagnose) { 12344 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 12345 << CurE->getSourceRange() << getOpenMPClauseName(CKind); 12346 return nullptr; 12347 } 12348 if (RelevantExpr) 12349 return nullptr; 12350 continue; 12351 } 12352 12353 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 12354 // If the type of a list item is a reference to a type T then the type 12355 // will be considered to be T for all purposes of this clause. 12356 QualType CurType = BaseE->getType().getNonReferenceType(); 12357 12358 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 12359 // A list item cannot be a variable that is a member of a structure with 12360 // a union type. 12361 // 12362 if (CurType->isUnionType()) { 12363 if (!NoDiagnose) { 12364 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 12365 << CurE->getSourceRange(); 12366 return nullptr; 12367 } 12368 continue; 12369 } 12370 12371 // If we got a member expression, we should not expect any array section 12372 // before that: 12373 // 12374 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 12375 // If a list item is an element of a structure, only the rightmost symbol 12376 // of the variable reference can be an array section. 12377 // 12378 AllowUnitySizeArraySection = false; 12379 AllowWholeSizeArraySection = false; 12380 12381 // Record the component. 12382 CurComponents.emplace_back(CurE, FD); 12383 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) { 12384 E = CurE->getBase()->IgnoreParenImpCasts(); 12385 12386 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 12387 if (!NoDiagnose) { 12388 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 12389 << 0 << CurE->getSourceRange(); 12390 return nullptr; 12391 } 12392 continue; 12393 } 12394 12395 // If we got an array subscript that express the whole dimension we 12396 // can have any array expressions before. If it only expressing part of 12397 // the dimension, we can only have unitary-size array expressions. 12398 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, 12399 E->getType())) 12400 AllowWholeSizeArraySection = false; 12401 12402 // Record the component - we don't have any declaration associated. 12403 CurComponents.emplace_back(CurE, nullptr); 12404 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) { 12405 assert(!NoDiagnose && "Array sections cannot be implicitly mapped."); 12406 E = CurE->getBase()->IgnoreParenImpCasts(); 12407 12408 QualType CurType = 12409 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 12410 12411 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 12412 // If the type of a list item is a reference to a type T then the type 12413 // will be considered to be T for all purposes of this clause. 12414 if (CurType->isReferenceType()) 12415 CurType = CurType->getPointeeType(); 12416 12417 bool IsPointer = CurType->isAnyPointerType(); 12418 12419 if (!IsPointer && !CurType->isArrayType()) { 12420 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 12421 << 0 << CurE->getSourceRange(); 12422 return nullptr; 12423 } 12424 12425 bool NotWhole = 12426 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType); 12427 bool NotUnity = 12428 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType); 12429 12430 if (AllowWholeSizeArraySection) { 12431 // Any array section is currently allowed. Allowing a whole size array 12432 // section implies allowing a unity array section as well. 12433 // 12434 // If this array section refers to the whole dimension we can still 12435 // accept other array sections before this one, except if the base is a 12436 // pointer. Otherwise, only unitary sections are accepted. 12437 if (NotWhole || IsPointer) 12438 AllowWholeSizeArraySection = false; 12439 } else if (AllowUnitySizeArraySection && NotUnity) { 12440 // A unity or whole array section is not allowed and that is not 12441 // compatible with the properties of the current array section. 12442 SemaRef.Diag( 12443 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 12444 << CurE->getSourceRange(); 12445 return nullptr; 12446 } 12447 12448 // Record the component - we don't have any declaration associated. 12449 CurComponents.emplace_back(CurE, nullptr); 12450 } else { 12451 if (!NoDiagnose) { 12452 // If nothing else worked, this is not a valid map clause expression. 12453 SemaRef.Diag( 12454 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 12455 << ERange; 12456 } 12457 return nullptr; 12458 } 12459 } 12460 12461 return RelevantExpr; 12462 } 12463 12464 // Return true if expression E associated with value VD has conflicts with other 12465 // map information. 12466 static bool checkMapConflicts( 12467 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 12468 bool CurrentRegionOnly, 12469 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 12470 OpenMPClauseKind CKind) { 12471 assert(VD && E); 12472 SourceLocation ELoc = E->getExprLoc(); 12473 SourceRange ERange = E->getSourceRange(); 12474 12475 // In order to easily check the conflicts we need to match each component of 12476 // the expression under test with the components of the expressions that are 12477 // already in the stack. 12478 12479 assert(!CurComponents.empty() && "Map clause expression with no components!"); 12480 assert(CurComponents.back().getAssociatedDeclaration() == VD && 12481 "Map clause expression with unexpected base!"); 12482 12483 // Variables to help detecting enclosing problems in data environment nests. 12484 bool IsEnclosedByDataEnvironmentExpr = false; 12485 const Expr *EnclosingExpr = nullptr; 12486 12487 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 12488 VD, CurrentRegionOnly, 12489 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 12490 ERange, CKind, &EnclosingExpr, 12491 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 12492 StackComponents, 12493 OpenMPClauseKind) { 12494 assert(!StackComponents.empty() && 12495 "Map clause expression with no components!"); 12496 assert(StackComponents.back().getAssociatedDeclaration() == VD && 12497 "Map clause expression with unexpected base!"); 12498 (void)VD; 12499 12500 // The whole expression in the stack. 12501 const Expr *RE = StackComponents.front().getAssociatedExpression(); 12502 12503 // Expressions must start from the same base. Here we detect at which 12504 // point both expressions diverge from each other and see if we can 12505 // detect if the memory referred to both expressions is contiguous and 12506 // do not overlap. 12507 auto CI = CurComponents.rbegin(); 12508 auto CE = CurComponents.rend(); 12509 auto SI = StackComponents.rbegin(); 12510 auto SE = StackComponents.rend(); 12511 for (; CI != CE && SI != SE; ++CI, ++SI) { 12512 12513 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 12514 // At most one list item can be an array item derived from a given 12515 // variable in map clauses of the same construct. 12516 if (CurrentRegionOnly && 12517 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 12518 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) && 12519 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 12520 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) { 12521 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 12522 diag::err_omp_multiple_array_items_in_map_clause) 12523 << CI->getAssociatedExpression()->getSourceRange(); 12524 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 12525 diag::note_used_here) 12526 << SI->getAssociatedExpression()->getSourceRange(); 12527 return true; 12528 } 12529 12530 // Do both expressions have the same kind? 12531 if (CI->getAssociatedExpression()->getStmtClass() != 12532 SI->getAssociatedExpression()->getStmtClass()) 12533 break; 12534 12535 // Are we dealing with different variables/fields? 12536 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 12537 break; 12538 } 12539 // Check if the extra components of the expressions in the enclosing 12540 // data environment are redundant for the current base declaration. 12541 // If they are, the maps completely overlap, which is legal. 12542 for (; SI != SE; ++SI) { 12543 QualType Type; 12544 if (const auto *ASE = 12545 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 12546 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 12547 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 12548 SI->getAssociatedExpression())) { 12549 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 12550 Type = 12551 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 12552 } 12553 if (Type.isNull() || Type->isAnyPointerType() || 12554 checkArrayExpressionDoesNotReferToWholeSize( 12555 SemaRef, SI->getAssociatedExpression(), Type)) 12556 break; 12557 } 12558 12559 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 12560 // List items of map clauses in the same construct must not share 12561 // original storage. 12562 // 12563 // If the expressions are exactly the same or one is a subset of the 12564 // other, it means they are sharing storage. 12565 if (CI == CE && SI == SE) { 12566 if (CurrentRegionOnly) { 12567 if (CKind == OMPC_map) { 12568 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 12569 } else { 12570 assert(CKind == OMPC_to || CKind == OMPC_from); 12571 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 12572 << ERange; 12573 } 12574 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12575 << RE->getSourceRange(); 12576 return true; 12577 } 12578 // If we find the same expression in the enclosing data environment, 12579 // that is legal. 12580 IsEnclosedByDataEnvironmentExpr = true; 12581 return false; 12582 } 12583 12584 QualType DerivedType = 12585 std::prev(CI)->getAssociatedDeclaration()->getType(); 12586 SourceLocation DerivedLoc = 12587 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 12588 12589 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 12590 // If the type of a list item is a reference to a type T then the type 12591 // will be considered to be T for all purposes of this clause. 12592 DerivedType = DerivedType.getNonReferenceType(); 12593 12594 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 12595 // A variable for which the type is pointer and an array section 12596 // derived from that variable must not appear as list items of map 12597 // clauses of the same construct. 12598 // 12599 // Also, cover one of the cases in: 12600 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 12601 // If any part of the original storage of a list item has corresponding 12602 // storage in the device data environment, all of the original storage 12603 // must have corresponding storage in the device data environment. 12604 // 12605 if (DerivedType->isAnyPointerType()) { 12606 if (CI == CE || SI == SE) { 12607 SemaRef.Diag( 12608 DerivedLoc, 12609 diag::err_omp_pointer_mapped_along_with_derived_section) 12610 << DerivedLoc; 12611 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12612 << RE->getSourceRange(); 12613 return true; 12614 } 12615 if (CI->getAssociatedExpression()->getStmtClass() != 12616 SI->getAssociatedExpression()->getStmtClass() || 12617 CI->getAssociatedDeclaration()->getCanonicalDecl() == 12618 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 12619 assert(CI != CE && SI != SE); 12620 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 12621 << DerivedLoc; 12622 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12623 << RE->getSourceRange(); 12624 return true; 12625 } 12626 } 12627 12628 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 12629 // List items of map clauses in the same construct must not share 12630 // original storage. 12631 // 12632 // An expression is a subset of the other. 12633 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 12634 if (CKind == OMPC_map) { 12635 if (CI != CE || SI != SE) { 12636 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is 12637 // a pointer. 12638 auto Begin = 12639 CI != CE ? CurComponents.begin() : StackComponents.begin(); 12640 auto End = CI != CE ? CurComponents.end() : StackComponents.end(); 12641 auto It = Begin; 12642 while (It != End && !It->getAssociatedDeclaration()) 12643 std::advance(It, 1); 12644 assert(It != End && 12645 "Expected at least one component with the declaration."); 12646 if (It != Begin && It->getAssociatedDeclaration() 12647 ->getType() 12648 .getCanonicalType() 12649 ->isAnyPointerType()) { 12650 IsEnclosedByDataEnvironmentExpr = false; 12651 EnclosingExpr = nullptr; 12652 return false; 12653 } 12654 } 12655 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 12656 } else { 12657 assert(CKind == OMPC_to || CKind == OMPC_from); 12658 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 12659 << ERange; 12660 } 12661 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 12662 << RE->getSourceRange(); 12663 return true; 12664 } 12665 12666 // The current expression uses the same base as other expression in the 12667 // data environment but does not contain it completely. 12668 if (!CurrentRegionOnly && SI != SE) 12669 EnclosingExpr = RE; 12670 12671 // The current expression is a subset of the expression in the data 12672 // environment. 12673 IsEnclosedByDataEnvironmentExpr |= 12674 (!CurrentRegionOnly && CI != CE && SI == SE); 12675 12676 return false; 12677 }); 12678 12679 if (CurrentRegionOnly) 12680 return FoundError; 12681 12682 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 12683 // If any part of the original storage of a list item has corresponding 12684 // storage in the device data environment, all of the original storage must 12685 // have corresponding storage in the device data environment. 12686 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 12687 // If a list item is an element of a structure, and a different element of 12688 // the structure has a corresponding list item in the device data environment 12689 // prior to a task encountering the construct associated with the map clause, 12690 // then the list item must also have a corresponding list item in the device 12691 // data environment prior to the task encountering the construct. 12692 // 12693 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 12694 SemaRef.Diag(ELoc, 12695 diag::err_omp_original_storage_is_shared_and_does_not_contain) 12696 << ERange; 12697 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 12698 << EnclosingExpr->getSourceRange(); 12699 return true; 12700 } 12701 12702 return FoundError; 12703 } 12704 12705 namespace { 12706 // Utility struct that gathers all the related lists associated with a mappable 12707 // expression. 12708 struct MappableVarListInfo { 12709 // The list of expressions. 12710 ArrayRef<Expr *> VarList; 12711 // The list of processed expressions. 12712 SmallVector<Expr *, 16> ProcessedVarList; 12713 // The mappble components for each expression. 12714 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 12715 // The base declaration of the variable. 12716 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 12717 12718 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 12719 // We have a list of components and base declarations for each entry in the 12720 // variable list. 12721 VarComponents.reserve(VarList.size()); 12722 VarBaseDeclarations.reserve(VarList.size()); 12723 } 12724 }; 12725 } 12726 12727 // Check the validity of the provided variable list for the provided clause kind 12728 // \a CKind. In the check process the valid expressions, and mappable expression 12729 // components and variables are extracted and used to fill \a Vars, 12730 // \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and 12731 // \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'. 12732 static void 12733 checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS, 12734 OpenMPClauseKind CKind, MappableVarListInfo &MVLI, 12735 SourceLocation StartLoc, 12736 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 12737 bool IsMapTypeImplicit = false) { 12738 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 12739 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 12740 "Unexpected clause kind with mappable expressions!"); 12741 12742 // Keep track of the mappable components and base declarations in this clause. 12743 // Each entry in the list is going to have a list of components associated. We 12744 // record each set of the components so that we can build the clause later on. 12745 // In the end we should have the same amount of declarations and component 12746 // lists. 12747 12748 for (Expr *RE : MVLI.VarList) { 12749 assert(RE && "Null expr in omp to/from/map clause"); 12750 SourceLocation ELoc = RE->getExprLoc(); 12751 12752 const Expr *VE = RE->IgnoreParenLValueCasts(); 12753 12754 if (VE->isValueDependent() || VE->isTypeDependent() || 12755 VE->isInstantiationDependent() || 12756 VE->containsUnexpandedParameterPack()) { 12757 // We can only analyze this information once the missing information is 12758 // resolved. 12759 MVLI.ProcessedVarList.push_back(RE); 12760 continue; 12761 } 12762 12763 Expr *SimpleExpr = RE->IgnoreParenCasts(); 12764 12765 if (!RE->IgnoreParenImpCasts()->isLValue()) { 12766 SemaRef.Diag(ELoc, 12767 diag::err_omp_expected_named_var_member_or_array_expression) 12768 << RE->getSourceRange(); 12769 continue; 12770 } 12771 12772 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 12773 ValueDecl *CurDeclaration = nullptr; 12774 12775 // Obtain the array or member expression bases if required. Also, fill the 12776 // components array with all the components identified in the process. 12777 const Expr *BE = checkMapClauseExpressionBase( 12778 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false); 12779 if (!BE) 12780 continue; 12781 12782 assert(!CurComponents.empty() && 12783 "Invalid mappable expression information."); 12784 12785 // For the following checks, we rely on the base declaration which is 12786 // expected to be associated with the last component. The declaration is 12787 // expected to be a variable or a field (if 'this' is being mapped). 12788 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 12789 assert(CurDeclaration && "Null decl on map clause."); 12790 assert( 12791 CurDeclaration->isCanonicalDecl() && 12792 "Expecting components to have associated only canonical declarations."); 12793 12794 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 12795 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 12796 12797 assert((VD || FD) && "Only variables or fields are expected here!"); 12798 (void)FD; 12799 12800 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 12801 // threadprivate variables cannot appear in a map clause. 12802 // OpenMP 4.5 [2.10.5, target update Construct] 12803 // threadprivate variables cannot appear in a from clause. 12804 if (VD && DSAS->isThreadPrivate(VD)) { 12805 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 12806 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 12807 << getOpenMPClauseName(CKind); 12808 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 12809 continue; 12810 } 12811 12812 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 12813 // A list item cannot appear in both a map clause and a data-sharing 12814 // attribute clause on the same construct. 12815 12816 // Check conflicts with other map clause expressions. We check the conflicts 12817 // with the current construct separately from the enclosing data 12818 // environment, because the restrictions are different. We only have to 12819 // check conflicts across regions for the map clauses. 12820 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 12821 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 12822 break; 12823 if (CKind == OMPC_map && 12824 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 12825 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 12826 break; 12827 12828 // OpenMP 4.5 [2.10.5, target update Construct] 12829 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 12830 // If the type of a list item is a reference to a type T then the type will 12831 // be considered to be T for all purposes of this clause. 12832 auto I = llvm::find_if( 12833 CurComponents, 12834 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 12835 return MC.getAssociatedDeclaration(); 12836 }); 12837 assert(I != CurComponents.end() && "Null decl on map clause."); 12838 QualType Type = 12839 I->getAssociatedDeclaration()->getType().getNonReferenceType(); 12840 12841 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 12842 // A list item in a to or from clause must have a mappable type. 12843 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 12844 // A list item must have a mappable type. 12845 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 12846 DSAS, Type)) 12847 continue; 12848 12849 if (CKind == OMPC_map) { 12850 // target enter data 12851 // OpenMP [2.10.2, Restrictions, p. 99] 12852 // A map-type must be specified in all map clauses and must be either 12853 // to or alloc. 12854 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 12855 if (DKind == OMPD_target_enter_data && 12856 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 12857 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 12858 << (IsMapTypeImplicit ? 1 : 0) 12859 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 12860 << getOpenMPDirectiveName(DKind); 12861 continue; 12862 } 12863 12864 // target exit_data 12865 // OpenMP [2.10.3, Restrictions, p. 102] 12866 // A map-type must be specified in all map clauses and must be either 12867 // from, release, or delete. 12868 if (DKind == OMPD_target_exit_data && 12869 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 12870 MapType == OMPC_MAP_delete)) { 12871 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 12872 << (IsMapTypeImplicit ? 1 : 0) 12873 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 12874 << getOpenMPDirectiveName(DKind); 12875 continue; 12876 } 12877 12878 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 12879 // A list item cannot appear in both a map clause and a data-sharing 12880 // attribute clause on the same construct 12881 if (VD && isOpenMPTargetExecutionDirective(DKind)) { 12882 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 12883 if (isOpenMPPrivate(DVar.CKind)) { 12884 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 12885 << getOpenMPClauseName(DVar.CKind) 12886 << getOpenMPClauseName(OMPC_map) 12887 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 12888 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 12889 continue; 12890 } 12891 } 12892 } 12893 12894 // Save the current expression. 12895 MVLI.ProcessedVarList.push_back(RE); 12896 12897 // Store the components in the stack so that they can be used to check 12898 // against other clauses later on. 12899 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 12900 /*WhereFoundClauseKind=*/OMPC_map); 12901 12902 // Save the components and declaration to create the clause. For purposes of 12903 // the clause creation, any component list that has has base 'this' uses 12904 // null as base declaration. 12905 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 12906 MVLI.VarComponents.back().append(CurComponents.begin(), 12907 CurComponents.end()); 12908 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 12909 : CurDeclaration); 12910 } 12911 } 12912 12913 OMPClause * 12914 Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier, 12915 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, 12916 SourceLocation MapLoc, SourceLocation ColonLoc, 12917 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 12918 SourceLocation LParenLoc, SourceLocation EndLoc) { 12919 MappableVarListInfo MVLI(VarList); 12920 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc, 12921 MapType, IsMapTypeImplicit); 12922 12923 // We need to produce a map clause even if we don't have variables so that 12924 // other diagnostics related with non-existing map clauses are accurate. 12925 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, 12926 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 12927 MVLI.VarComponents, MapTypeModifier, MapType, 12928 IsMapTypeImplicit, MapLoc); 12929 } 12930 12931 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 12932 TypeResult ParsedType) { 12933 assert(ParsedType.isUsable()); 12934 12935 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 12936 if (ReductionType.isNull()) 12937 return QualType(); 12938 12939 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 12940 // A type name in a declare reduction directive cannot be a function type, an 12941 // array type, a reference type, or a type qualified with const, volatile or 12942 // restrict. 12943 if (ReductionType.hasQualifiers()) { 12944 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 12945 return QualType(); 12946 } 12947 12948 if (ReductionType->isFunctionType()) { 12949 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 12950 return QualType(); 12951 } 12952 if (ReductionType->isReferenceType()) { 12953 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 12954 return QualType(); 12955 } 12956 if (ReductionType->isArrayType()) { 12957 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 12958 return QualType(); 12959 } 12960 return ReductionType; 12961 } 12962 12963 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 12964 Scope *S, DeclContext *DC, DeclarationName Name, 12965 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 12966 AccessSpecifier AS, Decl *PrevDeclInScope) { 12967 SmallVector<Decl *, 8> Decls; 12968 Decls.reserve(ReductionTypes.size()); 12969 12970 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 12971 forRedeclarationInCurContext()); 12972 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 12973 // A reduction-identifier may not be re-declared in the current scope for the 12974 // same type or for a type that is compatible according to the base language 12975 // rules. 12976 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 12977 OMPDeclareReductionDecl *PrevDRD = nullptr; 12978 bool InCompoundScope = true; 12979 if (S != nullptr) { 12980 // Find previous declaration with the same name not referenced in other 12981 // declarations. 12982 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 12983 InCompoundScope = 12984 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 12985 LookupName(Lookup, S); 12986 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 12987 /*AllowInlineNamespace=*/false); 12988 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 12989 LookupResult::Filter Filter = Lookup.makeFilter(); 12990 while (Filter.hasNext()) { 12991 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 12992 if (InCompoundScope) { 12993 auto I = UsedAsPrevious.find(PrevDecl); 12994 if (I == UsedAsPrevious.end()) 12995 UsedAsPrevious[PrevDecl] = false; 12996 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 12997 UsedAsPrevious[D] = true; 12998 } 12999 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 13000 PrevDecl->getLocation(); 13001 } 13002 Filter.done(); 13003 if (InCompoundScope) { 13004 for (const auto &PrevData : UsedAsPrevious) { 13005 if (!PrevData.second) { 13006 PrevDRD = PrevData.first; 13007 break; 13008 } 13009 } 13010 } 13011 } else if (PrevDeclInScope != nullptr) { 13012 auto *PrevDRDInScope = PrevDRD = 13013 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 13014 do { 13015 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 13016 PrevDRDInScope->getLocation(); 13017 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 13018 } while (PrevDRDInScope != nullptr); 13019 } 13020 for (const auto &TyData : ReductionTypes) { 13021 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 13022 bool Invalid = false; 13023 if (I != PreviousRedeclTypes.end()) { 13024 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 13025 << TyData.first; 13026 Diag(I->second, diag::note_previous_definition); 13027 Invalid = true; 13028 } 13029 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 13030 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 13031 Name, TyData.first, PrevDRD); 13032 DC->addDecl(DRD); 13033 DRD->setAccess(AS); 13034 Decls.push_back(DRD); 13035 if (Invalid) 13036 DRD->setInvalidDecl(); 13037 else 13038 PrevDRD = DRD; 13039 } 13040 13041 return DeclGroupPtrTy::make( 13042 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 13043 } 13044 13045 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 13046 auto *DRD = cast<OMPDeclareReductionDecl>(D); 13047 13048 // Enter new function scope. 13049 PushFunctionScope(); 13050 setFunctionHasBranchProtectedScope(); 13051 getCurFunction()->setHasOMPDeclareReductionCombiner(); 13052 13053 if (S != nullptr) 13054 PushDeclContext(S, DRD); 13055 else 13056 CurContext = DRD; 13057 13058 PushExpressionEvaluationContext( 13059 ExpressionEvaluationContext::PotentiallyEvaluated); 13060 13061 QualType ReductionType = DRD->getType(); 13062 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 13063 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 13064 // uses semantics of argument handles by value, but it should be passed by 13065 // reference. C lang does not support references, so pass all parameters as 13066 // pointers. 13067 // Create 'T omp_in;' variable. 13068 VarDecl *OmpInParm = 13069 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 13070 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 13071 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 13072 // uses semantics of argument handles by value, but it should be passed by 13073 // reference. C lang does not support references, so pass all parameters as 13074 // pointers. 13075 // Create 'T omp_out;' variable. 13076 VarDecl *OmpOutParm = 13077 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 13078 if (S != nullptr) { 13079 PushOnScopeChains(OmpInParm, S); 13080 PushOnScopeChains(OmpOutParm, S); 13081 } else { 13082 DRD->addDecl(OmpInParm); 13083 DRD->addDecl(OmpOutParm); 13084 } 13085 Expr *InE = 13086 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation()); 13087 Expr *OutE = 13088 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation()); 13089 DRD->setCombinerData(InE, OutE); 13090 } 13091 13092 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 13093 auto *DRD = cast<OMPDeclareReductionDecl>(D); 13094 DiscardCleanupsInEvaluationContext(); 13095 PopExpressionEvaluationContext(); 13096 13097 PopDeclContext(); 13098 PopFunctionScopeInfo(); 13099 13100 if (Combiner != nullptr) 13101 DRD->setCombiner(Combiner); 13102 else 13103 DRD->setInvalidDecl(); 13104 } 13105 13106 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 13107 auto *DRD = cast<OMPDeclareReductionDecl>(D); 13108 13109 // Enter new function scope. 13110 PushFunctionScope(); 13111 setFunctionHasBranchProtectedScope(); 13112 13113 if (S != nullptr) 13114 PushDeclContext(S, DRD); 13115 else 13116 CurContext = DRD; 13117 13118 PushExpressionEvaluationContext( 13119 ExpressionEvaluationContext::PotentiallyEvaluated); 13120 13121 QualType ReductionType = DRD->getType(); 13122 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 13123 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 13124 // uses semantics of argument handles by value, but it should be passed by 13125 // reference. C lang does not support references, so pass all parameters as 13126 // pointers. 13127 // Create 'T omp_priv;' variable. 13128 VarDecl *OmpPrivParm = 13129 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 13130 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 13131 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 13132 // uses semantics of argument handles by value, but it should be passed by 13133 // reference. C lang does not support references, so pass all parameters as 13134 // pointers. 13135 // Create 'T omp_orig;' variable. 13136 VarDecl *OmpOrigParm = 13137 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 13138 if (S != nullptr) { 13139 PushOnScopeChains(OmpPrivParm, S); 13140 PushOnScopeChains(OmpOrigParm, S); 13141 } else { 13142 DRD->addDecl(OmpPrivParm); 13143 DRD->addDecl(OmpOrigParm); 13144 } 13145 Expr *OrigE = 13146 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation()); 13147 Expr *PrivE = 13148 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation()); 13149 DRD->setInitializerData(OrigE, PrivE); 13150 return OmpPrivParm; 13151 } 13152 13153 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 13154 VarDecl *OmpPrivParm) { 13155 auto *DRD = cast<OMPDeclareReductionDecl>(D); 13156 DiscardCleanupsInEvaluationContext(); 13157 PopExpressionEvaluationContext(); 13158 13159 PopDeclContext(); 13160 PopFunctionScopeInfo(); 13161 13162 if (Initializer != nullptr) { 13163 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 13164 } else if (OmpPrivParm->hasInit()) { 13165 DRD->setInitializer(OmpPrivParm->getInit(), 13166 OmpPrivParm->isDirectInit() 13167 ? OMPDeclareReductionDecl::DirectInit 13168 : OMPDeclareReductionDecl::CopyInit); 13169 } else { 13170 DRD->setInvalidDecl(); 13171 } 13172 } 13173 13174 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 13175 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 13176 for (Decl *D : DeclReductions.get()) { 13177 if (IsValid) { 13178 if (S) 13179 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 13180 /*AddToContext=*/false); 13181 } else { 13182 D->setInvalidDecl(); 13183 } 13184 } 13185 return DeclReductions; 13186 } 13187 13188 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 13189 SourceLocation StartLoc, 13190 SourceLocation LParenLoc, 13191 SourceLocation EndLoc) { 13192 Expr *ValExpr = NumTeams; 13193 Stmt *HelperValStmt = nullptr; 13194 13195 // OpenMP [teams Constrcut, Restrictions] 13196 // The num_teams expression must evaluate to a positive integer value. 13197 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 13198 /*StrictlyPositive=*/true)) 13199 return nullptr; 13200 13201 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 13202 OpenMPDirectiveKind CaptureRegion = 13203 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams); 13204 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 13205 ValExpr = MakeFullExpr(ValExpr).get(); 13206 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13207 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13208 HelperValStmt = buildPreInits(Context, Captures); 13209 } 13210 13211 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 13212 StartLoc, LParenLoc, EndLoc); 13213 } 13214 13215 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 13216 SourceLocation StartLoc, 13217 SourceLocation LParenLoc, 13218 SourceLocation EndLoc) { 13219 Expr *ValExpr = ThreadLimit; 13220 Stmt *HelperValStmt = nullptr; 13221 13222 // OpenMP [teams Constrcut, Restrictions] 13223 // The thread_limit expression must evaluate to a positive integer value. 13224 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 13225 /*StrictlyPositive=*/true)) 13226 return nullptr; 13227 13228 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 13229 OpenMPDirectiveKind CaptureRegion = 13230 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit); 13231 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 13232 ValExpr = MakeFullExpr(ValExpr).get(); 13233 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13234 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13235 HelperValStmt = buildPreInits(Context, Captures); 13236 } 13237 13238 return new (Context) OMPThreadLimitClause( 13239 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 13240 } 13241 13242 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 13243 SourceLocation StartLoc, 13244 SourceLocation LParenLoc, 13245 SourceLocation EndLoc) { 13246 Expr *ValExpr = Priority; 13247 13248 // OpenMP [2.9.1, task Constrcut] 13249 // The priority-value is a non-negative numerical scalar expression. 13250 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority, 13251 /*StrictlyPositive=*/false)) 13252 return nullptr; 13253 13254 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc); 13255 } 13256 13257 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 13258 SourceLocation StartLoc, 13259 SourceLocation LParenLoc, 13260 SourceLocation EndLoc) { 13261 Expr *ValExpr = Grainsize; 13262 13263 // OpenMP [2.9.2, taskloop Constrcut] 13264 // The parameter of the grainsize clause must be a positive integer 13265 // expression. 13266 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize, 13267 /*StrictlyPositive=*/true)) 13268 return nullptr; 13269 13270 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc); 13271 } 13272 13273 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 13274 SourceLocation StartLoc, 13275 SourceLocation LParenLoc, 13276 SourceLocation EndLoc) { 13277 Expr *ValExpr = NumTasks; 13278 13279 // OpenMP [2.9.2, taskloop Constrcut] 13280 // The parameter of the num_tasks clause must be a positive integer 13281 // expression. 13282 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks, 13283 /*StrictlyPositive=*/true)) 13284 return nullptr; 13285 13286 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc); 13287 } 13288 13289 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 13290 SourceLocation LParenLoc, 13291 SourceLocation EndLoc) { 13292 // OpenMP [2.13.2, critical construct, Description] 13293 // ... where hint-expression is an integer constant expression that evaluates 13294 // to a valid lock hint. 13295 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 13296 if (HintExpr.isInvalid()) 13297 return nullptr; 13298 return new (Context) 13299 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 13300 } 13301 13302 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 13303 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 13304 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 13305 SourceLocation EndLoc) { 13306 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 13307 std::string Values; 13308 Values += "'"; 13309 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 13310 Values += "'"; 13311 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 13312 << Values << getOpenMPClauseName(OMPC_dist_schedule); 13313 return nullptr; 13314 } 13315 Expr *ValExpr = ChunkSize; 13316 Stmt *HelperValStmt = nullptr; 13317 if (ChunkSize) { 13318 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 13319 !ChunkSize->isInstantiationDependent() && 13320 !ChunkSize->containsUnexpandedParameterPack()) { 13321 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 13322 ExprResult Val = 13323 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 13324 if (Val.isInvalid()) 13325 return nullptr; 13326 13327 ValExpr = Val.get(); 13328 13329 // OpenMP [2.7.1, Restrictions] 13330 // chunk_size must be a loop invariant integer expression with a positive 13331 // value. 13332 llvm::APSInt Result; 13333 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 13334 if (Result.isSigned() && !Result.isStrictlyPositive()) { 13335 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 13336 << "dist_schedule" << ChunkSize->getSourceRange(); 13337 return nullptr; 13338 } 13339 } else if (getOpenMPCaptureRegionForClause( 13340 DSAStack->getCurrentDirective(), OMPC_dist_schedule) != 13341 OMPD_unknown && 13342 !CurContext->isDependentContext()) { 13343 ValExpr = MakeFullExpr(ValExpr).get(); 13344 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13345 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13346 HelperValStmt = buildPreInits(Context, Captures); 13347 } 13348 } 13349 } 13350 13351 return new (Context) 13352 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 13353 Kind, ValExpr, HelperValStmt); 13354 } 13355 13356 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 13357 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 13358 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 13359 SourceLocation KindLoc, SourceLocation EndLoc) { 13360 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)' 13361 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) { 13362 std::string Value; 13363 SourceLocation Loc; 13364 Value += "'"; 13365 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 13366 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 13367 OMPC_DEFAULTMAP_MODIFIER_tofrom); 13368 Loc = MLoc; 13369 } else { 13370 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 13371 OMPC_DEFAULTMAP_scalar); 13372 Loc = KindLoc; 13373 } 13374 Value += "'"; 13375 Diag(Loc, diag::err_omp_unexpected_clause_value) 13376 << Value << getOpenMPClauseName(OMPC_defaultmap); 13377 return nullptr; 13378 } 13379 DSAStack->setDefaultDMAToFromScalar(StartLoc); 13380 13381 return new (Context) 13382 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 13383 } 13384 13385 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { 13386 DeclContext *CurLexicalContext = getCurLexicalContext(); 13387 if (!CurLexicalContext->isFileContext() && 13388 !CurLexicalContext->isExternCContext() && 13389 !CurLexicalContext->isExternCXXContext() && 13390 !isa<CXXRecordDecl>(CurLexicalContext) && 13391 !isa<ClassTemplateDecl>(CurLexicalContext) && 13392 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 13393 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 13394 Diag(Loc, diag::err_omp_region_not_file_context); 13395 return false; 13396 } 13397 ++DeclareTargetNestingLevel; 13398 return true; 13399 } 13400 13401 void Sema::ActOnFinishOpenMPDeclareTargetDirective() { 13402 assert(DeclareTargetNestingLevel > 0 && 13403 "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); 13404 --DeclareTargetNestingLevel; 13405 } 13406 13407 void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, 13408 CXXScopeSpec &ScopeSpec, 13409 const DeclarationNameInfo &Id, 13410 OMPDeclareTargetDeclAttr::MapTypeTy MT, 13411 NamedDeclSetType &SameDirectiveDecls) { 13412 LookupResult Lookup(*this, Id, LookupOrdinaryName); 13413 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 13414 13415 if (Lookup.isAmbiguous()) 13416 return; 13417 Lookup.suppressDiagnostics(); 13418 13419 if (!Lookup.isSingleResult()) { 13420 if (TypoCorrection Corrected = 13421 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, 13422 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this), 13423 CTK_ErrorRecovery)) { 13424 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 13425 << Id.getName()); 13426 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 13427 return; 13428 } 13429 13430 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 13431 return; 13432 } 13433 13434 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 13435 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) || 13436 isa<FunctionTemplateDecl>(ND)) { 13437 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) 13438 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); 13439 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 13440 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 13441 cast<ValueDecl>(ND)); 13442 if (!Res) { 13443 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT); 13444 ND->addAttr(A); 13445 if (ASTMutationListener *ML = Context.getASTMutationListener()) 13446 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 13447 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc()); 13448 } else if (*Res != MT) { 13449 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link) 13450 << Id.getName(); 13451 } 13452 } else { 13453 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 13454 } 13455 } 13456 13457 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 13458 Sema &SemaRef, Decl *D) { 13459 if (!D || !isa<VarDecl>(D)) 13460 return; 13461 auto *VD = cast<VarDecl>(D); 13462 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 13463 return; 13464 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context); 13465 SemaRef.Diag(SL, diag::note_used_here) << SR; 13466 } 13467 13468 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 13469 Sema &SemaRef, DSAStackTy *Stack, 13470 ValueDecl *VD) { 13471 return VD->hasAttr<OMPDeclareTargetDeclAttr>() || 13472 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 13473 /*FullCheck=*/false); 13474 } 13475 13476 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 13477 SourceLocation IdLoc) { 13478 if (!D || D->isInvalidDecl()) 13479 return; 13480 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 13481 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation(); 13482 if (auto *VD = dyn_cast<VarDecl>(D)) { 13483 // Only global variables can be marked as declare target. 13484 if (!VD->isFileVarDecl() && !VD->isStaticLocal() && 13485 !VD->isStaticDataMember()) 13486 return; 13487 // 2.10.6: threadprivate variable cannot appear in a declare target 13488 // directive. 13489 if (DSAStack->isThreadPrivate(VD)) { 13490 Diag(SL, diag::err_omp_threadprivate_in_target); 13491 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 13492 return; 13493 } 13494 } 13495 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) 13496 D = FTD->getTemplatedDecl(); 13497 if (const auto *FD = dyn_cast<FunctionDecl>(D)) { 13498 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 13499 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD); 13500 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { 13501 assert(IdLoc.isValid() && "Source location is expected"); 13502 Diag(IdLoc, diag::err_omp_function_in_link_clause); 13503 Diag(FD->getLocation(), diag::note_defined_here) << FD; 13504 return; 13505 } 13506 } 13507 if (auto *VD = dyn_cast<ValueDecl>(D)) { 13508 // Problem if any with var declared with incomplete type will be reported 13509 // as normal, so no need to check it here. 13510 if ((E || !VD->getType()->isIncompleteType()) && 13511 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) 13512 return; 13513 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { 13514 // Checking declaration inside declare target region. 13515 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 13516 isa<FunctionTemplateDecl>(D)) { 13517 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 13518 Context, OMPDeclareTargetDeclAttr::MT_To); 13519 D->addAttr(A); 13520 if (ASTMutationListener *ML = Context.getASTMutationListener()) 13521 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 13522 } 13523 return; 13524 } 13525 } 13526 if (!E) 13527 return; 13528 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 13529 } 13530 13531 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList, 13532 SourceLocation StartLoc, 13533 SourceLocation LParenLoc, 13534 SourceLocation EndLoc) { 13535 MappableVarListInfo MVLI(VarList); 13536 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc); 13537 if (MVLI.ProcessedVarList.empty()) 13538 return nullptr; 13539 13540 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc, 13541 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 13542 MVLI.VarComponents); 13543 } 13544 13545 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList, 13546 SourceLocation StartLoc, 13547 SourceLocation LParenLoc, 13548 SourceLocation EndLoc) { 13549 MappableVarListInfo MVLI(VarList); 13550 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc); 13551 if (MVLI.ProcessedVarList.empty()) 13552 return nullptr; 13553 13554 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc, 13555 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 13556 MVLI.VarComponents); 13557 } 13558 13559 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 13560 SourceLocation StartLoc, 13561 SourceLocation LParenLoc, 13562 SourceLocation EndLoc) { 13563 MappableVarListInfo MVLI(VarList); 13564 SmallVector<Expr *, 8> PrivateCopies; 13565 SmallVector<Expr *, 8> Inits; 13566 13567 for (Expr *RefExpr : VarList) { 13568 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 13569 SourceLocation ELoc; 13570 SourceRange ERange; 13571 Expr *SimpleRefExpr = RefExpr; 13572 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 13573 if (Res.second) { 13574 // It will be analyzed later. 13575 MVLI.ProcessedVarList.push_back(RefExpr); 13576 PrivateCopies.push_back(nullptr); 13577 Inits.push_back(nullptr); 13578 } 13579 ValueDecl *D = Res.first; 13580 if (!D) 13581 continue; 13582 13583 QualType Type = D->getType(); 13584 Type = Type.getNonReferenceType().getUnqualifiedType(); 13585 13586 auto *VD = dyn_cast<VarDecl>(D); 13587 13588 // Item should be a pointer or reference to pointer. 13589 if (!Type->isPointerType()) { 13590 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 13591 << 0 << RefExpr->getSourceRange(); 13592 continue; 13593 } 13594 13595 // Build the private variable and the expression that refers to it. 13596 auto VDPrivate = 13597 buildVarDecl(*this, ELoc, Type, D->getName(), 13598 D->hasAttrs() ? &D->getAttrs() : nullptr, 13599 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 13600 if (VDPrivate->isInvalidDecl()) 13601 continue; 13602 13603 CurContext->addDecl(VDPrivate); 13604 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 13605 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 13606 13607 // Add temporary variable to initialize the private copy of the pointer. 13608 VarDecl *VDInit = 13609 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 13610 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 13611 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 13612 AddInitializerToDecl(VDPrivate, 13613 DefaultLvalueConversion(VDInitRefExpr).get(), 13614 /*DirectInit=*/false); 13615 13616 // If required, build a capture to implement the privatization initialized 13617 // with the current list item value. 13618 DeclRefExpr *Ref = nullptr; 13619 if (!VD) 13620 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 13621 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 13622 PrivateCopies.push_back(VDPrivateRefExpr); 13623 Inits.push_back(VDInitRefExpr); 13624 13625 // We need to add a data sharing attribute for this variable to make sure it 13626 // is correctly captured. A variable that shows up in a use_device_ptr has 13627 // similar properties of a first private variable. 13628 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 13629 13630 // Create a mappable component for the list item. List items in this clause 13631 // only need a component. 13632 MVLI.VarBaseDeclarations.push_back(D); 13633 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 13634 MVLI.VarComponents.back().push_back( 13635 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D)); 13636 } 13637 13638 if (MVLI.ProcessedVarList.empty()) 13639 return nullptr; 13640 13641 return OMPUseDevicePtrClause::Create( 13642 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList, 13643 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents); 13644 } 13645 13646 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 13647 SourceLocation StartLoc, 13648 SourceLocation LParenLoc, 13649 SourceLocation EndLoc) { 13650 MappableVarListInfo MVLI(VarList); 13651 for (Expr *RefExpr : VarList) { 13652 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 13653 SourceLocation ELoc; 13654 SourceRange ERange; 13655 Expr *SimpleRefExpr = RefExpr; 13656 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 13657 if (Res.second) { 13658 // It will be analyzed later. 13659 MVLI.ProcessedVarList.push_back(RefExpr); 13660 } 13661 ValueDecl *D = Res.first; 13662 if (!D) 13663 continue; 13664 13665 QualType Type = D->getType(); 13666 // item should be a pointer or array or reference to pointer or array 13667 if (!Type.getNonReferenceType()->isPointerType() && 13668 !Type.getNonReferenceType()->isArrayType()) { 13669 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 13670 << 0 << RefExpr->getSourceRange(); 13671 continue; 13672 } 13673 13674 // Check if the declaration in the clause does not show up in any data 13675 // sharing attribute. 13676 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 13677 if (isOpenMPPrivate(DVar.CKind)) { 13678 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 13679 << getOpenMPClauseName(DVar.CKind) 13680 << getOpenMPClauseName(OMPC_is_device_ptr) 13681 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 13682 reportOriginalDsa(*this, DSAStack, D, DVar); 13683 continue; 13684 } 13685 13686 const Expr *ConflictExpr; 13687 if (DSAStack->checkMappableExprComponentListsForDecl( 13688 D, /*CurrentRegionOnly=*/true, 13689 [&ConflictExpr]( 13690 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 13691 OpenMPClauseKind) -> bool { 13692 ConflictExpr = R.front().getAssociatedExpression(); 13693 return true; 13694 })) { 13695 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 13696 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 13697 << ConflictExpr->getSourceRange(); 13698 continue; 13699 } 13700 13701 // Store the components in the stack so that they can be used to check 13702 // against other clauses later on. 13703 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D); 13704 DSAStack->addMappableExpressionComponents( 13705 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 13706 13707 // Record the expression we've just processed. 13708 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 13709 13710 // Create a mappable component for the list item. List items in this clause 13711 // only need a component. We use a null declaration to signal fields in 13712 // 'this'. 13713 assert((isa<DeclRefExpr>(SimpleRefExpr) || 13714 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 13715 "Unexpected device pointer expression!"); 13716 MVLI.VarBaseDeclarations.push_back( 13717 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 13718 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 13719 MVLI.VarComponents.back().push_back(MC); 13720 } 13721 13722 if (MVLI.ProcessedVarList.empty()) 13723 return nullptr; 13724 13725 return OMPIsDevicePtrClause::Create( 13726 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList, 13727 MVLI.VarBaseDeclarations, MVLI.VarComponents); 13728 } 13729