1 //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// \file 9 /// This file implements semantic analysis for OpenMP directives and 10 /// clauses. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #include "TreeTransform.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/ASTMutationListener.h" 17 #include "clang/AST/CXXInheritance.h" 18 #include "clang/AST/Decl.h" 19 #include "clang/AST/DeclCXX.h" 20 #include "clang/AST/DeclOpenMP.h" 21 #include "clang/AST/StmtCXX.h" 22 #include "clang/AST/StmtOpenMP.h" 23 #include "clang/AST/StmtVisitor.h" 24 #include "clang/AST/TypeOrdering.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 llvm::DenseSet<QualType> MappedClassesQualTypes; 150 /// List of globals marked as declare target link in this target region 151 /// (isOpenMPTargetExecutionDirective(Directive) == true). 152 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls; 153 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 154 Scope *CurScope, SourceLocation Loc) 155 : Directive(DKind), DirectiveName(Name), CurScope(CurScope), 156 ConstructLoc(Loc) {} 157 SharingMapTy() = default; 158 }; 159 160 using StackTy = SmallVector<SharingMapTy, 4>; 161 162 /// Stack of used declaration and their data-sharing attributes. 163 DeclSAMapTy Threadprivates; 164 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr; 165 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack; 166 /// true, if check for DSA must be from parent directive, false, if 167 /// from current directive. 168 OpenMPClauseKind ClauseKindMode = OMPC_unknown; 169 Sema &SemaRef; 170 bool ForceCapturing = false; 171 /// true if all the vaiables in the target executable directives must be 172 /// captured by reference. 173 bool ForceCaptureByReferenceInTargetExecutable = false; 174 CriticalsWithHintsTy Criticals; 175 176 using iterator = StackTy::const_reverse_iterator; 177 178 DSAVarData getDSA(iterator &Iter, ValueDecl *D) const; 179 180 /// Checks if the variable is a local for OpenMP region. 181 bool isOpenMPLocal(VarDecl *D, iterator Iter) const; 182 183 bool isStackEmpty() const { 184 return Stack.empty() || 185 Stack.back().second != CurrentNonCapturingFunctionScope || 186 Stack.back().first.empty(); 187 } 188 189 /// Vector of previously declared requires directives 190 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls; 191 /// omp_allocator_handle_t type. 192 QualType OMPAllocatorHandleT; 193 /// Expression for the predefined allocators. 194 Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = { 195 nullptr}; 196 /// Vector of previously encountered target directives 197 SmallVector<SourceLocation, 2> TargetLocations; 198 199 public: 200 explicit DSAStackTy(Sema &S) : SemaRef(S) {} 201 202 /// Sets omp_allocator_handle_t type. 203 void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; } 204 /// Gets omp_allocator_handle_t type. 205 QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; } 206 /// Sets the given default allocator. 207 void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 208 Expr *Allocator) { 209 OMPPredefinedAllocators[AllocatorKind] = Allocator; 210 } 211 /// Returns the specified default allocator. 212 Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const { 213 return OMPPredefinedAllocators[AllocatorKind]; 214 } 215 216 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 217 OpenMPClauseKind getClauseParsingMode() const { 218 assert(isClauseParsingMode() && "Must be in clause parsing mode."); 219 return ClauseKindMode; 220 } 221 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 222 223 bool isForceVarCapturing() const { return ForceCapturing; } 224 void setForceVarCapturing(bool V) { ForceCapturing = V; } 225 226 void setForceCaptureByReferenceInTargetExecutable(bool V) { 227 ForceCaptureByReferenceInTargetExecutable = V; 228 } 229 bool isForceCaptureByReferenceInTargetExecutable() const { 230 return ForceCaptureByReferenceInTargetExecutable; 231 } 232 233 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 234 Scope *CurScope, SourceLocation Loc) { 235 if (Stack.empty() || 236 Stack.back().second != CurrentNonCapturingFunctionScope) 237 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope); 238 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc); 239 Stack.back().first.back().DefaultAttrLoc = Loc; 240 } 241 242 void pop() { 243 assert(!Stack.back().first.empty() && 244 "Data-sharing attributes stack is empty!"); 245 Stack.back().first.pop_back(); 246 } 247 248 /// Marks that we're started loop parsing. 249 void loopInit() { 250 assert(isOpenMPLoopDirective(getCurrentDirective()) && 251 "Expected loop-based directive."); 252 Stack.back().first.back().LoopStart = true; 253 } 254 /// Start capturing of the variables in the loop context. 255 void loopStart() { 256 assert(isOpenMPLoopDirective(getCurrentDirective()) && 257 "Expected loop-based directive."); 258 Stack.back().first.back().LoopStart = false; 259 } 260 /// true, if variables are captured, false otherwise. 261 bool isLoopStarted() const { 262 assert(isOpenMPLoopDirective(getCurrentDirective()) && 263 "Expected loop-based directive."); 264 return !Stack.back().first.back().LoopStart; 265 } 266 /// Marks (or clears) declaration as possibly loop counter. 267 void resetPossibleLoopCounter(const Decl *D = nullptr) { 268 Stack.back().first.back().PossiblyLoopCounter = 269 D ? D->getCanonicalDecl() : D; 270 } 271 /// Gets the possible loop counter decl. 272 const Decl *getPossiblyLoopCunter() const { 273 return Stack.back().first.back().PossiblyLoopCounter; 274 } 275 /// Start new OpenMP region stack in new non-capturing function. 276 void pushFunction() { 277 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction(); 278 assert(!isa<CapturingScopeInfo>(CurFnScope)); 279 CurrentNonCapturingFunctionScope = CurFnScope; 280 } 281 /// Pop region stack for non-capturing function. 282 void popFunction(const FunctionScopeInfo *OldFSI) { 283 if (!Stack.empty() && Stack.back().second == OldFSI) { 284 assert(Stack.back().first.empty()); 285 Stack.pop_back(); 286 } 287 CurrentNonCapturingFunctionScope = nullptr; 288 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) { 289 if (!isa<CapturingScopeInfo>(FSI)) { 290 CurrentNonCapturingFunctionScope = FSI; 291 break; 292 } 293 } 294 } 295 296 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) { 297 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint); 298 } 299 const std::pair<const OMPCriticalDirective *, llvm::APSInt> 300 getCriticalWithHint(const DeclarationNameInfo &Name) const { 301 auto I = Criticals.find(Name.getAsString()); 302 if (I != Criticals.end()) 303 return I->second; 304 return std::make_pair(nullptr, llvm::APSInt()); 305 } 306 /// If 'aligned' declaration for given variable \a D was not seen yet, 307 /// add it and return NULL; otherwise return previous occurrence's expression 308 /// for diagnostics. 309 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE); 310 311 /// Register specified variable as loop control variable. 312 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture); 313 /// Check if the specified variable is a loop control variable for 314 /// current region. 315 /// \return The index of the loop control variable in the list of associated 316 /// for-loops (from outer to inner). 317 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const; 318 /// Check if the specified variable is a loop control variable for 319 /// parent region. 320 /// \return The index of the loop control variable in the list of associated 321 /// for-loops (from outer to inner). 322 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const; 323 /// Get the loop control variable for the I-th loop (or nullptr) in 324 /// parent directive. 325 const ValueDecl *getParentLoopControlVariable(unsigned I) const; 326 327 /// Adds explicit data sharing attribute to the specified declaration. 328 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 329 DeclRefExpr *PrivateCopy = nullptr); 330 331 /// Adds additional information for the reduction items with the reduction id 332 /// represented as an operator. 333 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 334 BinaryOperatorKind BOK); 335 /// Adds additional information for the reduction items with the reduction id 336 /// represented as reduction identifier. 337 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 338 const Expr *ReductionRef); 339 /// Returns the location and reduction operation from the innermost parent 340 /// region for the given \p D. 341 const DSAVarData 342 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 343 BinaryOperatorKind &BOK, 344 Expr *&TaskgroupDescriptor) const; 345 /// Returns the location and reduction operation from the innermost parent 346 /// region for the given \p D. 347 const DSAVarData 348 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 349 const Expr *&ReductionRef, 350 Expr *&TaskgroupDescriptor) const; 351 /// Return reduction reference expression for the current taskgroup. 352 Expr *getTaskgroupReductionRef() const { 353 assert(Stack.back().first.back().Directive == OMPD_taskgroup && 354 "taskgroup reference expression requested for non taskgroup " 355 "directive."); 356 return Stack.back().first.back().TaskgroupReductionRef; 357 } 358 /// Checks if the given \p VD declaration is actually a taskgroup reduction 359 /// descriptor variable at the \p Level of OpenMP regions. 360 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const { 361 return Stack.back().first[Level].TaskgroupReductionRef && 362 cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef) 363 ->getDecl() == VD; 364 } 365 366 /// Returns data sharing attributes from top of the stack for the 367 /// specified declaration. 368 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 369 /// Returns data-sharing attributes for the specified declaration. 370 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const; 371 /// Checks if the specified variables has data-sharing attributes which 372 /// match specified \a CPred predicate in any directive which matches \a DPred 373 /// predicate. 374 const DSAVarData 375 hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 376 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 377 bool FromParent) const; 378 /// Checks if the specified variables has data-sharing attributes which 379 /// match specified \a CPred predicate in any innermost directive which 380 /// matches \a DPred predicate. 381 const DSAVarData 382 hasInnermostDSA(ValueDecl *D, 383 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 384 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 385 bool FromParent) const; 386 /// Checks if the specified variables has explicit data-sharing 387 /// attributes which match specified \a CPred predicate at the specified 388 /// OpenMP region. 389 bool hasExplicitDSA(const ValueDecl *D, 390 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 391 unsigned Level, bool NotLastprivate = false) const; 392 393 /// Returns true if the directive at level \Level matches in the 394 /// specified \a DPred predicate. 395 bool hasExplicitDirective( 396 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 397 unsigned Level) const; 398 399 /// Finds a directive which matches specified \a DPred predicate. 400 bool hasDirective( 401 const llvm::function_ref<bool( 402 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)> 403 DPred, 404 bool FromParent) const; 405 406 /// Returns currently analyzed directive. 407 OpenMPDirectiveKind getCurrentDirective() const { 408 return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive; 409 } 410 /// Returns directive kind at specified level. 411 OpenMPDirectiveKind getDirective(unsigned Level) const { 412 assert(!isStackEmpty() && "No directive at specified level."); 413 return Stack.back().first[Level].Directive; 414 } 415 /// Returns parent directive. 416 OpenMPDirectiveKind getParentDirective() const { 417 if (isStackEmpty() || Stack.back().first.size() == 1) 418 return OMPD_unknown; 419 return std::next(Stack.back().first.rbegin())->Directive; 420 } 421 422 /// Add requires decl to internal vector 423 void addRequiresDecl(OMPRequiresDecl *RD) { 424 RequiresDecls.push_back(RD); 425 } 426 427 /// Checks if the defined 'requires' directive has specified type of clause. 428 template <typename ClauseType> 429 bool hasRequiresDeclWithClause() { 430 return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) { 431 return llvm::any_of(D->clauselists(), [](const OMPClause *C) { 432 return isa<ClauseType>(C); 433 }); 434 }); 435 } 436 437 /// Checks for a duplicate clause amongst previously declared requires 438 /// directives 439 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const { 440 bool IsDuplicate = false; 441 for (OMPClause *CNew : ClauseList) { 442 for (const OMPRequiresDecl *D : RequiresDecls) { 443 for (const OMPClause *CPrev : D->clauselists()) { 444 if (CNew->getClauseKind() == CPrev->getClauseKind()) { 445 SemaRef.Diag(CNew->getBeginLoc(), 446 diag::err_omp_requires_clause_redeclaration) 447 << getOpenMPClauseName(CNew->getClauseKind()); 448 SemaRef.Diag(CPrev->getBeginLoc(), 449 diag::note_omp_requires_previous_clause) 450 << getOpenMPClauseName(CPrev->getClauseKind()); 451 IsDuplicate = true; 452 } 453 } 454 } 455 } 456 return IsDuplicate; 457 } 458 459 /// Add location of previously encountered target to internal vector 460 void addTargetDirLocation(SourceLocation LocStart) { 461 TargetLocations.push_back(LocStart); 462 } 463 464 // Return previously encountered target region locations. 465 ArrayRef<SourceLocation> getEncounteredTargetLocs() const { 466 return TargetLocations; 467 } 468 469 /// Set default data sharing attribute to none. 470 void setDefaultDSANone(SourceLocation Loc) { 471 assert(!isStackEmpty()); 472 Stack.back().first.back().DefaultAttr = DSA_none; 473 Stack.back().first.back().DefaultAttrLoc = Loc; 474 } 475 /// Set default data sharing attribute to shared. 476 void setDefaultDSAShared(SourceLocation Loc) { 477 assert(!isStackEmpty()); 478 Stack.back().first.back().DefaultAttr = DSA_shared; 479 Stack.back().first.back().DefaultAttrLoc = Loc; 480 } 481 /// Set default data mapping attribute to 'tofrom:scalar'. 482 void setDefaultDMAToFromScalar(SourceLocation Loc) { 483 assert(!isStackEmpty()); 484 Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar; 485 Stack.back().first.back().DefaultMapAttrLoc = Loc; 486 } 487 488 DefaultDataSharingAttributes getDefaultDSA() const { 489 return isStackEmpty() ? DSA_unspecified 490 : Stack.back().first.back().DefaultAttr; 491 } 492 SourceLocation getDefaultDSALocation() const { 493 return isStackEmpty() ? SourceLocation() 494 : Stack.back().first.back().DefaultAttrLoc; 495 } 496 DefaultMapAttributes getDefaultDMA() const { 497 return isStackEmpty() ? DMA_unspecified 498 : Stack.back().first.back().DefaultMapAttr; 499 } 500 DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const { 501 return Stack.back().first[Level].DefaultMapAttr; 502 } 503 SourceLocation getDefaultDMALocation() const { 504 return isStackEmpty() ? SourceLocation() 505 : Stack.back().first.back().DefaultMapAttrLoc; 506 } 507 508 /// Checks if the specified variable is a threadprivate. 509 bool isThreadPrivate(VarDecl *D) { 510 const DSAVarData DVar = getTopDSA(D, false); 511 return isOpenMPThreadPrivate(DVar.CKind); 512 } 513 514 /// Marks current region as ordered (it has an 'ordered' clause). 515 void setOrderedRegion(bool IsOrdered, const Expr *Param, 516 OMPOrderedClause *Clause) { 517 assert(!isStackEmpty()); 518 if (IsOrdered) 519 Stack.back().first.back().OrderedRegion.emplace(Param, Clause); 520 else 521 Stack.back().first.back().OrderedRegion.reset(); 522 } 523 /// Returns true, if region is ordered (has associated 'ordered' clause), 524 /// false - otherwise. 525 bool isOrderedRegion() const { 526 if (isStackEmpty()) 527 return false; 528 return Stack.back().first.rbegin()->OrderedRegion.hasValue(); 529 } 530 /// Returns optional parameter for the ordered region. 531 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const { 532 if (isStackEmpty() || 533 !Stack.back().first.rbegin()->OrderedRegion.hasValue()) 534 return std::make_pair(nullptr, nullptr); 535 return Stack.back().first.rbegin()->OrderedRegion.getValue(); 536 } 537 /// Returns true, if parent region is ordered (has associated 538 /// 'ordered' clause), false - otherwise. 539 bool isParentOrderedRegion() const { 540 if (isStackEmpty() || Stack.back().first.size() == 1) 541 return false; 542 return std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue(); 543 } 544 /// Returns optional parameter for the ordered region. 545 std::pair<const Expr *, OMPOrderedClause *> 546 getParentOrderedRegionParam() const { 547 if (isStackEmpty() || Stack.back().first.size() == 1 || 548 !std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue()) 549 return std::make_pair(nullptr, nullptr); 550 return std::next(Stack.back().first.rbegin())->OrderedRegion.getValue(); 551 } 552 /// Marks current region as nowait (it has a 'nowait' clause). 553 void setNowaitRegion(bool IsNowait = true) { 554 assert(!isStackEmpty()); 555 Stack.back().first.back().NowaitRegion = IsNowait; 556 } 557 /// Returns true, if parent region is nowait (has associated 558 /// 'nowait' clause), false - otherwise. 559 bool isParentNowaitRegion() const { 560 if (isStackEmpty() || Stack.back().first.size() == 1) 561 return false; 562 return std::next(Stack.back().first.rbegin())->NowaitRegion; 563 } 564 /// Marks parent region as cancel region. 565 void setParentCancelRegion(bool Cancel = true) { 566 if (!isStackEmpty() && Stack.back().first.size() > 1) { 567 auto &StackElemRef = *std::next(Stack.back().first.rbegin()); 568 StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel; 569 } 570 } 571 /// Return true if current region has inner cancel construct. 572 bool isCancelRegion() const { 573 return isStackEmpty() ? false : Stack.back().first.back().CancelRegion; 574 } 575 576 /// Set collapse value for the region. 577 void setAssociatedLoops(unsigned Val) { 578 assert(!isStackEmpty()); 579 Stack.back().first.back().AssociatedLoops = Val; 580 } 581 /// Return collapse value for region. 582 unsigned getAssociatedLoops() const { 583 return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops; 584 } 585 586 /// Marks current target region as one with closely nested teams 587 /// region. 588 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 589 if (!isStackEmpty() && Stack.back().first.size() > 1) { 590 std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc = 591 TeamsRegionLoc; 592 } 593 } 594 /// Returns true, if current region has closely nested teams region. 595 bool hasInnerTeamsRegion() const { 596 return getInnerTeamsRegionLoc().isValid(); 597 } 598 /// Returns location of the nested teams region (if any). 599 SourceLocation getInnerTeamsRegionLoc() const { 600 return isStackEmpty() ? SourceLocation() 601 : Stack.back().first.back().InnerTeamsRegionLoc; 602 } 603 604 Scope *getCurScope() const { 605 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope; 606 } 607 SourceLocation getConstructLoc() const { 608 return isStackEmpty() ? SourceLocation() 609 : Stack.back().first.back().ConstructLoc; 610 } 611 612 /// Do the check specified in \a Check to all component lists and return true 613 /// if any issue is found. 614 bool checkMappableExprComponentListsForDecl( 615 const ValueDecl *VD, bool CurrentRegionOnly, 616 const llvm::function_ref< 617 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 618 OpenMPClauseKind)> 619 Check) const { 620 if (isStackEmpty()) 621 return false; 622 auto SI = Stack.back().first.rbegin(); 623 auto SE = Stack.back().first.rend(); 624 625 if (SI == SE) 626 return false; 627 628 if (CurrentRegionOnly) 629 SE = std::next(SI); 630 else 631 std::advance(SI, 1); 632 633 for (; SI != SE; ++SI) { 634 auto MI = SI->MappedExprComponents.find(VD); 635 if (MI != SI->MappedExprComponents.end()) 636 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 637 MI->second.Components) 638 if (Check(L, MI->second.Kind)) 639 return true; 640 } 641 return false; 642 } 643 644 /// Do the check specified in \a Check to all component lists at a given level 645 /// and return true if any issue is found. 646 bool checkMappableExprComponentListsForDeclAtLevel( 647 const ValueDecl *VD, unsigned Level, 648 const llvm::function_ref< 649 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 650 OpenMPClauseKind)> 651 Check) const { 652 if (isStackEmpty()) 653 return false; 654 655 auto StartI = Stack.back().first.begin(); 656 auto EndI = Stack.back().first.end(); 657 if (std::distance(StartI, EndI) <= (int)Level) 658 return false; 659 std::advance(StartI, Level); 660 661 auto MI = StartI->MappedExprComponents.find(VD); 662 if (MI != StartI->MappedExprComponents.end()) 663 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 664 MI->second.Components) 665 if (Check(L, MI->second.Kind)) 666 return true; 667 return false; 668 } 669 670 /// Create a new mappable expression component list associated with a given 671 /// declaration and initialize it with the provided list of components. 672 void addMappableExpressionComponents( 673 const ValueDecl *VD, 674 OMPClauseMappableExprCommon::MappableExprComponentListRef Components, 675 OpenMPClauseKind WhereFoundClauseKind) { 676 assert(!isStackEmpty() && 677 "Not expecting to retrieve components from a empty stack!"); 678 MappedExprComponentTy &MEC = 679 Stack.back().first.back().MappedExprComponents[VD]; 680 // Create new entry and append the new components there. 681 MEC.Components.resize(MEC.Components.size() + 1); 682 MEC.Components.back().append(Components.begin(), Components.end()); 683 MEC.Kind = WhereFoundClauseKind; 684 } 685 686 unsigned getNestingLevel() const { 687 assert(!isStackEmpty()); 688 return Stack.back().first.size() - 1; 689 } 690 void addDoacrossDependClause(OMPDependClause *C, 691 const OperatorOffsetTy &OpsOffs) { 692 assert(!isStackEmpty() && Stack.back().first.size() > 1); 693 SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin()); 694 assert(isOpenMPWorksharingDirective(StackElem.Directive)); 695 StackElem.DoacrossDepends.try_emplace(C, OpsOffs); 696 } 697 llvm::iterator_range<DoacrossDependMapTy::const_iterator> 698 getDoacrossDependClauses() const { 699 assert(!isStackEmpty()); 700 const SharingMapTy &StackElem = Stack.back().first.back(); 701 if (isOpenMPWorksharingDirective(StackElem.Directive)) { 702 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends; 703 return llvm::make_range(Ref.begin(), Ref.end()); 704 } 705 return llvm::make_range(StackElem.DoacrossDepends.end(), 706 StackElem.DoacrossDepends.end()); 707 } 708 709 // Store types of classes which have been explicitly mapped 710 void addMappedClassesQualTypes(QualType QT) { 711 SharingMapTy &StackElem = Stack.back().first.back(); 712 StackElem.MappedClassesQualTypes.insert(QT); 713 } 714 715 // Return set of mapped classes types 716 bool isClassPreviouslyMapped(QualType QT) const { 717 const SharingMapTy &StackElem = Stack.back().first.back(); 718 return StackElem.MappedClassesQualTypes.count(QT) != 0; 719 } 720 721 /// Adds global declare target to the parent target region. 722 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) { 723 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 724 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link && 725 "Expected declare target link global."); 726 if (isStackEmpty()) 727 return; 728 auto It = Stack.back().first.rbegin(); 729 while (It != Stack.back().first.rend() && 730 !isOpenMPTargetExecutionDirective(It->Directive)) 731 ++It; 732 if (It != Stack.back().first.rend()) { 733 assert(isOpenMPTargetExecutionDirective(It->Directive) && 734 "Expected target executable directive."); 735 It->DeclareTargetLinkVarDecls.push_back(E); 736 } 737 } 738 739 /// Returns the list of globals with declare target link if current directive 740 /// is target. 741 ArrayRef<DeclRefExpr *> getLinkGlobals() const { 742 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) && 743 "Expected target executable directive."); 744 return Stack.back().first.back().DeclareTargetLinkVarDecls; 745 } 746 }; 747 748 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) { 749 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind); 750 } 751 752 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) { 753 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) || DKind == OMPD_unknown; 754 } 755 756 } // namespace 757 758 static const Expr *getExprAsWritten(const Expr *E) { 759 if (const auto *FE = dyn_cast<FullExpr>(E)) 760 E = FE->getSubExpr(); 761 762 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 763 E = MTE->GetTemporaryExpr(); 764 765 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 766 E = Binder->getSubExpr(); 767 768 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 769 E = ICE->getSubExprAsWritten(); 770 return E->IgnoreParens(); 771 } 772 773 static Expr *getExprAsWritten(Expr *E) { 774 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E))); 775 } 776 777 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) { 778 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) 779 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 780 D = ME->getMemberDecl(); 781 const auto *VD = dyn_cast<VarDecl>(D); 782 const auto *FD = dyn_cast<FieldDecl>(D); 783 if (VD != nullptr) { 784 VD = VD->getCanonicalDecl(); 785 D = VD; 786 } else { 787 assert(FD); 788 FD = FD->getCanonicalDecl(); 789 D = FD; 790 } 791 return D; 792 } 793 794 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 795 return const_cast<ValueDecl *>( 796 getCanonicalDecl(const_cast<const ValueDecl *>(D))); 797 } 798 799 DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter, 800 ValueDecl *D) const { 801 D = getCanonicalDecl(D); 802 auto *VD = dyn_cast<VarDecl>(D); 803 const auto *FD = dyn_cast<FieldDecl>(D); 804 DSAVarData DVar; 805 if (isStackEmpty() || Iter == Stack.back().first.rend()) { 806 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 807 // in a region but not in construct] 808 // File-scope or namespace-scope variables referenced in called routines 809 // in the region are shared unless they appear in a threadprivate 810 // directive. 811 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD)) 812 DVar.CKind = OMPC_shared; 813 814 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 815 // in a region but not in construct] 816 // Variables with static storage duration that are declared in called 817 // routines in the region are shared. 818 if (VD && VD->hasGlobalStorage()) 819 DVar.CKind = OMPC_shared; 820 821 // Non-static data members are shared by default. 822 if (FD) 823 DVar.CKind = OMPC_shared; 824 825 return DVar; 826 } 827 828 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 829 // in a Construct, C/C++, predetermined, p.1] 830 // Variables with automatic storage duration that are declared in a scope 831 // inside the construct are private. 832 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 833 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 834 DVar.CKind = OMPC_private; 835 return DVar; 836 } 837 838 DVar.DKind = Iter->Directive; 839 // Explicitly specified attributes and local variables with predetermined 840 // attributes. 841 if (Iter->SharingMap.count(D)) { 842 const DSAInfo &Data = Iter->SharingMap.lookup(D); 843 DVar.RefExpr = Data.RefExpr.getPointer(); 844 DVar.PrivateCopy = Data.PrivateCopy; 845 DVar.CKind = Data.Attributes; 846 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 847 return DVar; 848 } 849 850 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 851 // in a Construct, C/C++, implicitly determined, p.1] 852 // In a parallel or task construct, the data-sharing attributes of these 853 // variables are determined by the default clause, if present. 854 switch (Iter->DefaultAttr) { 855 case DSA_shared: 856 DVar.CKind = OMPC_shared; 857 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 858 return DVar; 859 case DSA_none: 860 return DVar; 861 case DSA_unspecified: 862 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 863 // in a Construct, implicitly determined, p.2] 864 // In a parallel construct, if no default clause is present, these 865 // variables are shared. 866 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 867 if (isOpenMPParallelDirective(DVar.DKind) || 868 isOpenMPTeamsDirective(DVar.DKind)) { 869 DVar.CKind = OMPC_shared; 870 return DVar; 871 } 872 873 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 874 // in a Construct, implicitly determined, p.4] 875 // In a task construct, if no default clause is present, a variable that in 876 // the enclosing context is determined to be shared by all implicit tasks 877 // bound to the current team is shared. 878 if (isOpenMPTaskingDirective(DVar.DKind)) { 879 DSAVarData DVarTemp; 880 iterator I = Iter, E = Stack.back().first.rend(); 881 do { 882 ++I; 883 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 884 // Referenced in a Construct, implicitly determined, p.6] 885 // In a task construct, if no default clause is present, a variable 886 // whose data-sharing attribute is not determined by the rules above is 887 // firstprivate. 888 DVarTemp = getDSA(I, D); 889 if (DVarTemp.CKind != OMPC_shared) { 890 DVar.RefExpr = nullptr; 891 DVar.CKind = OMPC_firstprivate; 892 return DVar; 893 } 894 } while (I != E && !isImplicitTaskingRegion(I->Directive)); 895 DVar.CKind = 896 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 897 return DVar; 898 } 899 } 900 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 901 // in a Construct, implicitly determined, p.3] 902 // For constructs other than task, if no default clause is present, these 903 // variables inherit their data-sharing attributes from the enclosing 904 // context. 905 return getDSA(++Iter, D); 906 } 907 908 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D, 909 const Expr *NewDE) { 910 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 911 D = getCanonicalDecl(D); 912 SharingMapTy &StackElem = Stack.back().first.back(); 913 auto It = StackElem.AlignedMap.find(D); 914 if (It == StackElem.AlignedMap.end()) { 915 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 916 StackElem.AlignedMap[D] = NewDE; 917 return nullptr; 918 } 919 assert(It->second && "Unexpected nullptr expr in the aligned map"); 920 return It->second; 921 } 922 923 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) { 924 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 925 D = getCanonicalDecl(D); 926 SharingMapTy &StackElem = Stack.back().first.back(); 927 StackElem.LCVMap.try_emplace( 928 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)); 929 } 930 931 const DSAStackTy::LCDeclInfo 932 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const { 933 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 934 D = getCanonicalDecl(D); 935 const SharingMapTy &StackElem = Stack.back().first.back(); 936 auto It = StackElem.LCVMap.find(D); 937 if (It != StackElem.LCVMap.end()) 938 return It->second; 939 return {0, nullptr}; 940 } 941 942 const DSAStackTy::LCDeclInfo 943 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const { 944 assert(!isStackEmpty() && Stack.back().first.size() > 1 && 945 "Data-sharing attributes stack is empty"); 946 D = getCanonicalDecl(D); 947 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin()); 948 auto It = StackElem.LCVMap.find(D); 949 if (It != StackElem.LCVMap.end()) 950 return It->second; 951 return {0, nullptr}; 952 } 953 954 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const { 955 assert(!isStackEmpty() && Stack.back().first.size() > 1 && 956 "Data-sharing attributes stack is empty"); 957 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin()); 958 if (StackElem.LCVMap.size() < I) 959 return nullptr; 960 for (const auto &Pair : StackElem.LCVMap) 961 if (Pair.second.first == I) 962 return Pair.first; 963 return nullptr; 964 } 965 966 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 967 DeclRefExpr *PrivateCopy) { 968 D = getCanonicalDecl(D); 969 if (A == OMPC_threadprivate) { 970 DSAInfo &Data = Threadprivates[D]; 971 Data.Attributes = A; 972 Data.RefExpr.setPointer(E); 973 Data.PrivateCopy = nullptr; 974 } else { 975 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 976 DSAInfo &Data = Stack.back().first.back().SharingMap[D]; 977 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 978 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 979 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 980 (isLoopControlVariable(D).first && A == OMPC_private)); 981 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 982 Data.RefExpr.setInt(/*IntVal=*/true); 983 return; 984 } 985 const bool IsLastprivate = 986 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 987 Data.Attributes = A; 988 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 989 Data.PrivateCopy = PrivateCopy; 990 if (PrivateCopy) { 991 DSAInfo &Data = 992 Stack.back().first.back().SharingMap[PrivateCopy->getDecl()]; 993 Data.Attributes = A; 994 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 995 Data.PrivateCopy = nullptr; 996 } 997 } 998 } 999 1000 /// Build a variable declaration for OpenMP loop iteration variable. 1001 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 1002 StringRef Name, const AttrVec *Attrs = nullptr, 1003 DeclRefExpr *OrigRef = nullptr) { 1004 DeclContext *DC = SemaRef.CurContext; 1005 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 1006 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 1007 auto *Decl = 1008 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 1009 if (Attrs) { 1010 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 1011 I != E; ++I) 1012 Decl->addAttr(*I); 1013 } 1014 Decl->setImplicit(); 1015 if (OrigRef) { 1016 Decl->addAttr( 1017 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef)); 1018 } 1019 return Decl; 1020 } 1021 1022 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 1023 SourceLocation Loc, 1024 bool RefersToCapture = false) { 1025 D->setReferenced(); 1026 D->markUsed(S.Context); 1027 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 1028 SourceLocation(), D, RefersToCapture, Loc, Ty, 1029 VK_LValue); 1030 } 1031 1032 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1033 BinaryOperatorKind BOK) { 1034 D = getCanonicalDecl(D); 1035 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1036 assert( 1037 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction && 1038 "Additional reduction info may be specified only for reduction items."); 1039 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D]; 1040 assert(ReductionData.ReductionRange.isInvalid() && 1041 Stack.back().first.back().Directive == OMPD_taskgroup && 1042 "Additional reduction info may be specified only once for reduction " 1043 "items."); 1044 ReductionData.set(BOK, SR); 1045 Expr *&TaskgroupReductionRef = 1046 Stack.back().first.back().TaskgroupReductionRef; 1047 if (!TaskgroupReductionRef) { 1048 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1049 SemaRef.Context.VoidPtrTy, ".task_red."); 1050 TaskgroupReductionRef = 1051 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1052 } 1053 } 1054 1055 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1056 const Expr *ReductionRef) { 1057 D = getCanonicalDecl(D); 1058 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1059 assert( 1060 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction && 1061 "Additional reduction info may be specified only for reduction items."); 1062 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D]; 1063 assert(ReductionData.ReductionRange.isInvalid() && 1064 Stack.back().first.back().Directive == OMPD_taskgroup && 1065 "Additional reduction info may be specified only once for reduction " 1066 "items."); 1067 ReductionData.set(ReductionRef, SR); 1068 Expr *&TaskgroupReductionRef = 1069 Stack.back().first.back().TaskgroupReductionRef; 1070 if (!TaskgroupReductionRef) { 1071 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1072 SemaRef.Context.VoidPtrTy, ".task_red."); 1073 TaskgroupReductionRef = 1074 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1075 } 1076 } 1077 1078 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1079 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK, 1080 Expr *&TaskgroupDescriptor) const { 1081 D = getCanonicalDecl(D); 1082 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1083 if (Stack.back().first.empty()) 1084 return DSAVarData(); 1085 for (iterator I = std::next(Stack.back().first.rbegin(), 1), 1086 E = Stack.back().first.rend(); 1087 I != E; std::advance(I, 1)) { 1088 const DSAInfo &Data = I->SharingMap.lookup(D); 1089 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup) 1090 continue; 1091 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1092 if (!ReductionData.ReductionOp || 1093 ReductionData.ReductionOp.is<const Expr *>()) 1094 return DSAVarData(); 1095 SR = ReductionData.ReductionRange; 1096 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>(); 1097 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1098 "expression for the descriptor is not " 1099 "set."); 1100 TaskgroupDescriptor = I->TaskgroupReductionRef; 1101 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(), 1102 Data.PrivateCopy, I->DefaultAttrLoc); 1103 } 1104 return DSAVarData(); 1105 } 1106 1107 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1108 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef, 1109 Expr *&TaskgroupDescriptor) const { 1110 D = getCanonicalDecl(D); 1111 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1112 if (Stack.back().first.empty()) 1113 return DSAVarData(); 1114 for (iterator I = std::next(Stack.back().first.rbegin(), 1), 1115 E = Stack.back().first.rend(); 1116 I != E; std::advance(I, 1)) { 1117 const DSAInfo &Data = I->SharingMap.lookup(D); 1118 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup) 1119 continue; 1120 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1121 if (!ReductionData.ReductionOp || 1122 !ReductionData.ReductionOp.is<const Expr *>()) 1123 return DSAVarData(); 1124 SR = ReductionData.ReductionRange; 1125 ReductionRef = ReductionData.ReductionOp.get<const Expr *>(); 1126 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1127 "expression for the descriptor is not " 1128 "set."); 1129 TaskgroupDescriptor = I->TaskgroupReductionRef; 1130 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(), 1131 Data.PrivateCopy, I->DefaultAttrLoc); 1132 } 1133 return DSAVarData(); 1134 } 1135 1136 bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const { 1137 D = D->getCanonicalDecl(); 1138 if (!isStackEmpty()) { 1139 iterator I = Iter, E = Stack.back().first.rend(); 1140 Scope *TopScope = nullptr; 1141 while (I != E && !isImplicitOrExplicitTaskingRegion(I->Directive) && 1142 !isOpenMPTargetExecutionDirective(I->Directive)) 1143 ++I; 1144 if (I == E) 1145 return false; 1146 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; 1147 Scope *CurScope = getCurScope(); 1148 while (CurScope != TopScope && !CurScope->isDeclScope(D)) 1149 CurScope = CurScope->getParent(); 1150 return CurScope != TopScope; 1151 } 1152 return false; 1153 } 1154 1155 static bool isConstNotMutableType(Sema &SemaRef, QualType Type, 1156 bool AcceptIfMutable = true, 1157 bool *IsClassType = nullptr) { 1158 ASTContext &Context = SemaRef.getASTContext(); 1159 Type = Type.getNonReferenceType().getCanonicalType(); 1160 bool IsConstant = Type.isConstant(Context); 1161 Type = Context.getBaseElementType(Type); 1162 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus 1163 ? Type->getAsCXXRecordDecl() 1164 : nullptr; 1165 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 1166 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate()) 1167 RD = CTD->getTemplatedDecl(); 1168 if (IsClassType) 1169 *IsClassType = RD; 1170 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD && 1171 RD->hasDefinition() && RD->hasMutableFields()); 1172 } 1173 1174 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D, 1175 QualType Type, OpenMPClauseKind CKind, 1176 SourceLocation ELoc, 1177 bool AcceptIfMutable = true, 1178 bool ListItemNotVar = false) { 1179 ASTContext &Context = SemaRef.getASTContext(); 1180 bool IsClassType; 1181 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) { 1182 unsigned Diag = ListItemNotVar 1183 ? diag::err_omp_const_list_item 1184 : IsClassType ? diag::err_omp_const_not_mutable_variable 1185 : diag::err_omp_const_variable; 1186 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind); 1187 if (!ListItemNotVar && D) { 1188 const VarDecl *VD = dyn_cast<VarDecl>(D); 1189 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 1190 VarDecl::DeclarationOnly; 1191 SemaRef.Diag(D->getLocation(), 1192 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1193 << D; 1194 } 1195 return true; 1196 } 1197 return false; 1198 } 1199 1200 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, 1201 bool FromParent) { 1202 D = getCanonicalDecl(D); 1203 DSAVarData DVar; 1204 1205 auto *VD = dyn_cast<VarDecl>(D); 1206 auto TI = Threadprivates.find(D); 1207 if (TI != Threadprivates.end()) { 1208 DVar.RefExpr = TI->getSecond().RefExpr.getPointer(); 1209 DVar.CKind = OMPC_threadprivate; 1210 return DVar; 1211 } 1212 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) { 1213 DVar.RefExpr = buildDeclRefExpr( 1214 SemaRef, VD, D->getType().getNonReferenceType(), 1215 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation()); 1216 DVar.CKind = OMPC_threadprivate; 1217 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1218 return DVar; 1219 } 1220 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1221 // in a Construct, C/C++, predetermined, p.1] 1222 // Variables appearing in threadprivate directives are threadprivate. 1223 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 1224 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1225 SemaRef.getLangOpts().OpenMPUseTLS && 1226 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 1227 (VD && VD->getStorageClass() == SC_Register && 1228 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 1229 DVar.RefExpr = buildDeclRefExpr( 1230 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation()); 1231 DVar.CKind = OMPC_threadprivate; 1232 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1233 return DVar; 1234 } 1235 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD && 1236 VD->isLocalVarDeclOrParm() && !isStackEmpty() && 1237 !isLoopControlVariable(D).first) { 1238 iterator IterTarget = 1239 std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(), 1240 [](const SharingMapTy &Data) { 1241 return isOpenMPTargetExecutionDirective(Data.Directive); 1242 }); 1243 if (IterTarget != Stack.back().first.rend()) { 1244 iterator ParentIterTarget = std::next(IterTarget, 1); 1245 for (iterator Iter = Stack.back().first.rbegin(); 1246 Iter != ParentIterTarget; std::advance(Iter, 1)) { 1247 if (isOpenMPLocal(VD, Iter)) { 1248 DVar.RefExpr = 1249 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1250 D->getLocation()); 1251 DVar.CKind = OMPC_threadprivate; 1252 return DVar; 1253 } 1254 } 1255 if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) { 1256 auto DSAIter = IterTarget->SharingMap.find(D); 1257 if (DSAIter != IterTarget->SharingMap.end() && 1258 isOpenMPPrivate(DSAIter->getSecond().Attributes)) { 1259 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer(); 1260 DVar.CKind = OMPC_threadprivate; 1261 return DVar; 1262 } 1263 iterator End = Stack.back().first.rend(); 1264 if (!SemaRef.isOpenMPCapturedByRef( 1265 D, std::distance(ParentIterTarget, End))) { 1266 DVar.RefExpr = 1267 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1268 IterTarget->ConstructLoc); 1269 DVar.CKind = OMPC_threadprivate; 1270 return DVar; 1271 } 1272 } 1273 } 1274 } 1275 1276 if (isStackEmpty()) 1277 // Not in OpenMP execution region and top scope was already checked. 1278 return DVar; 1279 1280 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1281 // in a Construct, C/C++, predetermined, p.4] 1282 // Static data members are shared. 1283 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1284 // in a Construct, C/C++, predetermined, p.7] 1285 // Variables with static storage duration that are declared in a scope 1286 // inside the construct are shared. 1287 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; }; 1288 if (VD && VD->isStaticDataMember()) { 1289 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent); 1290 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1291 return DVar; 1292 1293 DVar.CKind = OMPC_shared; 1294 return DVar; 1295 } 1296 1297 // The predetermined shared attribute for const-qualified types having no 1298 // mutable members was removed after OpenMP 3.1. 1299 if (SemaRef.LangOpts.OpenMP <= 31) { 1300 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1301 // in a Construct, C/C++, predetermined, p.6] 1302 // Variables with const qualified type having no mutable member are 1303 // shared. 1304 if (isConstNotMutableType(SemaRef, D->getType())) { 1305 // Variables with const-qualified type having no mutable member may be 1306 // listed in a firstprivate clause, even if they are static data members. 1307 DSAVarData DVarTemp = hasInnermostDSA( 1308 D, 1309 [](OpenMPClauseKind C) { 1310 return C == OMPC_firstprivate || C == OMPC_shared; 1311 }, 1312 MatchesAlways, FromParent); 1313 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1314 return DVarTemp; 1315 1316 DVar.CKind = OMPC_shared; 1317 return DVar; 1318 } 1319 } 1320 1321 // Explicitly specified attributes and local variables with predetermined 1322 // attributes. 1323 iterator I = Stack.back().first.rbegin(); 1324 iterator EndI = Stack.back().first.rend(); 1325 if (FromParent && I != EndI) 1326 std::advance(I, 1); 1327 auto It = I->SharingMap.find(D); 1328 if (It != I->SharingMap.end()) { 1329 const DSAInfo &Data = It->getSecond(); 1330 DVar.RefExpr = Data.RefExpr.getPointer(); 1331 DVar.PrivateCopy = Data.PrivateCopy; 1332 DVar.CKind = Data.Attributes; 1333 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1334 DVar.DKind = I->Directive; 1335 } 1336 1337 return DVar; 1338 } 1339 1340 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1341 bool FromParent) const { 1342 if (isStackEmpty()) { 1343 iterator I; 1344 return getDSA(I, D); 1345 } 1346 D = getCanonicalDecl(D); 1347 iterator StartI = Stack.back().first.rbegin(); 1348 iterator EndI = Stack.back().first.rend(); 1349 if (FromParent && StartI != EndI) 1350 std::advance(StartI, 1); 1351 return getDSA(StartI, D); 1352 } 1353 1354 const DSAStackTy::DSAVarData 1355 DSAStackTy::hasDSA(ValueDecl *D, 1356 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1357 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1358 bool FromParent) const { 1359 if (isStackEmpty()) 1360 return {}; 1361 D = getCanonicalDecl(D); 1362 iterator I = Stack.back().first.rbegin(); 1363 iterator EndI = Stack.back().first.rend(); 1364 if (FromParent && I != EndI) 1365 std::advance(I, 1); 1366 for (; I != EndI; std::advance(I, 1)) { 1367 if (!DPred(I->Directive) && !isImplicitOrExplicitTaskingRegion(I->Directive)) 1368 continue; 1369 iterator NewI = I; 1370 DSAVarData DVar = getDSA(NewI, D); 1371 if (I == NewI && CPred(DVar.CKind)) 1372 return DVar; 1373 } 1374 return {}; 1375 } 1376 1377 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 1378 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1379 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1380 bool FromParent) const { 1381 if (isStackEmpty()) 1382 return {}; 1383 D = getCanonicalDecl(D); 1384 iterator StartI = Stack.back().first.rbegin(); 1385 iterator EndI = Stack.back().first.rend(); 1386 if (FromParent && StartI != EndI) 1387 std::advance(StartI, 1); 1388 if (StartI == EndI || !DPred(StartI->Directive)) 1389 return {}; 1390 iterator NewI = StartI; 1391 DSAVarData DVar = getDSA(NewI, D); 1392 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData(); 1393 } 1394 1395 bool DSAStackTy::hasExplicitDSA( 1396 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1397 unsigned Level, bool NotLastprivate) const { 1398 if (isStackEmpty()) 1399 return false; 1400 D = getCanonicalDecl(D); 1401 auto StartI = Stack.back().first.begin(); 1402 auto EndI = Stack.back().first.end(); 1403 if (std::distance(StartI, EndI) <= (int)Level) 1404 return false; 1405 std::advance(StartI, Level); 1406 auto I = StartI->SharingMap.find(D); 1407 if ((I != StartI->SharingMap.end()) && 1408 I->getSecond().RefExpr.getPointer() && 1409 CPred(I->getSecond().Attributes) && 1410 (!NotLastprivate || !I->getSecond().RefExpr.getInt())) 1411 return true; 1412 // Check predetermined rules for the loop control variables. 1413 auto LI = StartI->LCVMap.find(D); 1414 if (LI != StartI->LCVMap.end()) 1415 return CPred(OMPC_private); 1416 return false; 1417 } 1418 1419 bool DSAStackTy::hasExplicitDirective( 1420 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1421 unsigned Level) const { 1422 if (isStackEmpty()) 1423 return false; 1424 auto StartI = Stack.back().first.begin(); 1425 auto EndI = Stack.back().first.end(); 1426 if (std::distance(StartI, EndI) <= (int)Level) 1427 return false; 1428 std::advance(StartI, Level); 1429 return DPred(StartI->Directive); 1430 } 1431 1432 bool DSAStackTy::hasDirective( 1433 const llvm::function_ref<bool(OpenMPDirectiveKind, 1434 const DeclarationNameInfo &, SourceLocation)> 1435 DPred, 1436 bool FromParent) const { 1437 // We look only in the enclosing region. 1438 if (isStackEmpty()) 1439 return false; 1440 auto StartI = std::next(Stack.back().first.rbegin()); 1441 auto EndI = Stack.back().first.rend(); 1442 if (FromParent && StartI != EndI) 1443 StartI = std::next(StartI); 1444 for (auto I = StartI, EE = EndI; I != EE; ++I) { 1445 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 1446 return true; 1447 } 1448 return false; 1449 } 1450 1451 void Sema::InitDataSharingAttributesStack() { 1452 VarDataSharingAttributesStack = new DSAStackTy(*this); 1453 } 1454 1455 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 1456 1457 void Sema::pushOpenMPFunctionRegion() { 1458 DSAStack->pushFunction(); 1459 } 1460 1461 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) { 1462 DSAStack->popFunction(OldFSI); 1463 } 1464 1465 static bool isOpenMPDeviceDelayedContext(Sema &S) { 1466 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice && 1467 "Expected OpenMP device compilation."); 1468 return !S.isInOpenMPTargetExecutionDirective() && 1469 !S.isInOpenMPDeclareTargetContext(); 1470 } 1471 1472 /// Do we know that we will eventually codegen the given function? 1473 static bool isKnownEmitted(Sema &S, FunctionDecl *FD) { 1474 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice && 1475 "Expected OpenMP device compilation."); 1476 // Templates are emitted when they're instantiated. 1477 if (FD->isDependentContext()) 1478 return false; 1479 1480 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 1481 FD->getCanonicalDecl())) 1482 return true; 1483 1484 // Otherwise, the function is known-emitted if it's in our set of 1485 // known-emitted functions. 1486 return S.DeviceKnownEmittedFns.count(FD) > 0; 1487 } 1488 1489 Sema::DeviceDiagBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc, 1490 unsigned DiagID) { 1491 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice && 1492 "Expected OpenMP device compilation."); 1493 return DeviceDiagBuilder((isOpenMPDeviceDelayedContext(*this) && 1494 !isKnownEmitted(*this, getCurFunctionDecl())) 1495 ? DeviceDiagBuilder::K_Deferred 1496 : DeviceDiagBuilder::K_Immediate, 1497 Loc, DiagID, getCurFunctionDecl(), *this); 1498 } 1499 1500 void Sema::checkOpenMPDeviceFunction(SourceLocation Loc, FunctionDecl *Callee) { 1501 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice && 1502 "Expected OpenMP device compilation."); 1503 assert(Callee && "Callee may not be null."); 1504 FunctionDecl *Caller = getCurFunctionDecl(); 1505 1506 // If the caller is known-emitted, mark the callee as known-emitted. 1507 // Otherwise, mark the call in our call graph so we can traverse it later. 1508 if (!isOpenMPDeviceDelayedContext(*this) || 1509 (Caller && isKnownEmitted(*this, Caller))) 1510 markKnownEmitted(*this, Caller, Callee, Loc, isKnownEmitted); 1511 else if (Caller) 1512 DeviceCallGraph[Caller].insert({Callee, Loc}); 1513 } 1514 1515 void Sema::checkOpenMPDeviceExpr(const Expr *E) { 1516 assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice && 1517 "OpenMP device compilation mode is expected."); 1518 QualType Ty = E->getType(); 1519 if ((Ty->isFloat16Type() && !Context.getTargetInfo().hasFloat16Type()) || 1520 (Ty->isFloat128Type() && !Context.getTargetInfo().hasFloat128Type()) || 1521 (Ty->isIntegerType() && Context.getTypeSize(Ty) == 128 && 1522 !Context.getTargetInfo().hasInt128Type())) 1523 targetDiag(E->getExprLoc(), diag::err_type_unsupported) 1524 << Ty << E->getSourceRange(); 1525 } 1526 1527 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const { 1528 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1529 1530 ASTContext &Ctx = getASTContext(); 1531 bool IsByRef = true; 1532 1533 // Find the directive that is associated with the provided scope. 1534 D = cast<ValueDecl>(D->getCanonicalDecl()); 1535 QualType Ty = D->getType(); 1536 1537 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 1538 // This table summarizes how a given variable should be passed to the device 1539 // given its type and the clauses where it appears. This table is based on 1540 // the description in OpenMP 4.5 [2.10.4, target Construct] and 1541 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 1542 // 1543 // ========================================================================= 1544 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 1545 // | |(tofrom:scalar)| | pvt | | | | 1546 // ========================================================================= 1547 // | scl | | | | - | | bycopy| 1548 // | scl | | - | x | - | - | bycopy| 1549 // | scl | | x | - | - | - | null | 1550 // | scl | x | | | - | | byref | 1551 // | scl | x | - | x | - | - | bycopy| 1552 // | scl | x | x | - | - | - | null | 1553 // | scl | | - | - | - | x | byref | 1554 // | scl | x | - | - | - | x | byref | 1555 // 1556 // | agg | n.a. | | | - | | byref | 1557 // | agg | n.a. | - | x | - | - | byref | 1558 // | agg | n.a. | x | - | - | - | null | 1559 // | agg | n.a. | - | - | - | x | byref | 1560 // | agg | n.a. | - | - | - | x[] | byref | 1561 // 1562 // | ptr | n.a. | | | - | | bycopy| 1563 // | ptr | n.a. | - | x | - | - | bycopy| 1564 // | ptr | n.a. | x | - | - | - | null | 1565 // | ptr | n.a. | - | - | - | x | byref | 1566 // | ptr | n.a. | - | - | - | x[] | bycopy| 1567 // | ptr | n.a. | - | - | x | | bycopy| 1568 // | ptr | n.a. | - | - | x | x | bycopy| 1569 // | ptr | n.a. | - | - | x | x[] | bycopy| 1570 // ========================================================================= 1571 // Legend: 1572 // scl - scalar 1573 // ptr - pointer 1574 // agg - aggregate 1575 // x - applies 1576 // - - invalid in this combination 1577 // [] - mapped with an array section 1578 // byref - should be mapped by reference 1579 // byval - should be mapped by value 1580 // null - initialize a local variable to null on the device 1581 // 1582 // Observations: 1583 // - All scalar declarations that show up in a map clause have to be passed 1584 // by reference, because they may have been mapped in the enclosing data 1585 // environment. 1586 // - If the scalar value does not fit the size of uintptr, it has to be 1587 // passed by reference, regardless the result in the table above. 1588 // - For pointers mapped by value that have either an implicit map or an 1589 // array section, the runtime library may pass the NULL value to the 1590 // device instead of the value passed to it by the compiler. 1591 1592 if (Ty->isReferenceType()) 1593 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 1594 1595 // Locate map clauses and see if the variable being captured is referred to 1596 // in any of those clauses. Here we only care about variables, not fields, 1597 // because fields are part of aggregates. 1598 bool IsVariableUsedInMapClause = false; 1599 bool IsVariableAssociatedWithSection = false; 1600 1601 DSAStack->checkMappableExprComponentListsForDeclAtLevel( 1602 D, Level, 1603 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D]( 1604 OMPClauseMappableExprCommon::MappableExprComponentListRef 1605 MapExprComponents, 1606 OpenMPClauseKind WhereFoundClauseKind) { 1607 // Only the map clause information influences how a variable is 1608 // captured. E.g. is_device_ptr does not require changing the default 1609 // behavior. 1610 if (WhereFoundClauseKind != OMPC_map) 1611 return false; 1612 1613 auto EI = MapExprComponents.rbegin(); 1614 auto EE = MapExprComponents.rend(); 1615 1616 assert(EI != EE && "Invalid map expression!"); 1617 1618 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 1619 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 1620 1621 ++EI; 1622 if (EI == EE) 1623 return false; 1624 1625 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 1626 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 1627 isa<MemberExpr>(EI->getAssociatedExpression())) { 1628 IsVariableAssociatedWithSection = true; 1629 // There is nothing more we need to know about this variable. 1630 return true; 1631 } 1632 1633 // Keep looking for more map info. 1634 return false; 1635 }); 1636 1637 if (IsVariableUsedInMapClause) { 1638 // If variable is identified in a map clause it is always captured by 1639 // reference except if it is a pointer that is dereferenced somehow. 1640 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 1641 } else { 1642 // By default, all the data that has a scalar type is mapped by copy 1643 // (except for reduction variables). 1644 IsByRef = 1645 (DSAStack->isForceCaptureByReferenceInTargetExecutable() && 1646 !Ty->isAnyPointerType()) || 1647 !Ty->isScalarType() || 1648 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar || 1649 DSAStack->hasExplicitDSA( 1650 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level); 1651 } 1652 } 1653 1654 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 1655 IsByRef = 1656 ((DSAStack->isForceCaptureByReferenceInTargetExecutable() && 1657 !Ty->isAnyPointerType()) || 1658 !DSAStack->hasExplicitDSA( 1659 D, 1660 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; }, 1661 Level, /*NotLastprivate=*/true)) && 1662 // If the variable is artificial and must be captured by value - try to 1663 // capture by value. 1664 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() && 1665 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()); 1666 } 1667 1668 // When passing data by copy, we need to make sure it fits the uintptr size 1669 // and alignment, because the runtime library only deals with uintptr types. 1670 // If it does not fit the uintptr size, we need to pass the data by reference 1671 // instead. 1672 if (!IsByRef && 1673 (Ctx.getTypeSizeInChars(Ty) > 1674 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 1675 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 1676 IsByRef = true; 1677 } 1678 1679 return IsByRef; 1680 } 1681 1682 unsigned Sema::getOpenMPNestingLevel() const { 1683 assert(getLangOpts().OpenMP); 1684 return DSAStack->getNestingLevel(); 1685 } 1686 1687 bool Sema::isInOpenMPTargetExecutionDirective() const { 1688 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) && 1689 !DSAStack->isClauseParsingMode()) || 1690 DSAStack->hasDirective( 1691 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 1692 SourceLocation) -> bool { 1693 return isOpenMPTargetExecutionDirective(K); 1694 }, 1695 false); 1696 } 1697 1698 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) { 1699 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1700 D = getCanonicalDecl(D); 1701 1702 // If we are attempting to capture a global variable in a directive with 1703 // 'target' we return true so that this global is also mapped to the device. 1704 // 1705 auto *VD = dyn_cast<VarDecl>(D); 1706 if (VD && !VD->hasLocalStorage()) { 1707 if (isInOpenMPDeclareTargetContext() && 1708 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) { 1709 // Try to mark variable as declare target if it is used in capturing 1710 // regions. 1711 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 1712 checkDeclIsAllowedInOpenMPTarget(nullptr, VD); 1713 return nullptr; 1714 } else if (isInOpenMPTargetExecutionDirective()) { 1715 // If the declaration is enclosed in a 'declare target' directive, 1716 // then it should not be captured. 1717 // 1718 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 1719 return nullptr; 1720 return VD; 1721 } 1722 } 1723 // Capture variables captured by reference in lambdas for target-based 1724 // directives. 1725 if (VD && !DSAStack->isClauseParsingMode()) { 1726 if (const auto *RD = VD->getType() 1727 .getCanonicalType() 1728 .getNonReferenceType() 1729 ->getAsCXXRecordDecl()) { 1730 bool SavedForceCaptureByReferenceInTargetExecutable = 1731 DSAStack->isForceCaptureByReferenceInTargetExecutable(); 1732 DSAStack->setForceCaptureByReferenceInTargetExecutable(/*V=*/true); 1733 if (RD->isLambda()) { 1734 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures; 1735 FieldDecl *ThisCapture; 1736 RD->getCaptureFields(Captures, ThisCapture); 1737 for (const LambdaCapture &LC : RD->captures()) { 1738 if (LC.getCaptureKind() == LCK_ByRef) { 1739 VarDecl *VD = LC.getCapturedVar(); 1740 DeclContext *VDC = VD->getDeclContext(); 1741 if (!VDC->Encloses(CurContext)) 1742 continue; 1743 DSAStackTy::DSAVarData DVarPrivate = 1744 DSAStack->getTopDSA(VD, /*FromParent=*/false); 1745 // Do not capture already captured variables. 1746 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) && 1747 DVarPrivate.CKind == OMPC_unknown && 1748 !DSAStack->checkMappableExprComponentListsForDecl( 1749 D, /*CurrentRegionOnly=*/true, 1750 [](OMPClauseMappableExprCommon:: 1751 MappableExprComponentListRef, 1752 OpenMPClauseKind) { return true; })) 1753 MarkVariableReferenced(LC.getLocation(), LC.getCapturedVar()); 1754 } else if (LC.getCaptureKind() == LCK_This) { 1755 QualType ThisTy = getCurrentThisType(); 1756 if (!ThisTy.isNull() && 1757 Context.typesAreCompatible(ThisTy, ThisCapture->getType())) 1758 CheckCXXThisCapture(LC.getLocation()); 1759 } 1760 } 1761 } 1762 DSAStack->setForceCaptureByReferenceInTargetExecutable( 1763 SavedForceCaptureByReferenceInTargetExecutable); 1764 } 1765 } 1766 1767 if (DSAStack->getCurrentDirective() != OMPD_unknown && 1768 (!DSAStack->isClauseParsingMode() || 1769 DSAStack->getParentDirective() != OMPD_unknown)) { 1770 auto &&Info = DSAStack->isLoopControlVariable(D); 1771 if (Info.first || 1772 (VD && VD->hasLocalStorage() && 1773 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) || 1774 (VD && DSAStack->isForceVarCapturing())) 1775 return VD ? VD : Info.second; 1776 DSAStackTy::DSAVarData DVarPrivate = 1777 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 1778 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind)) 1779 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1780 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, 1781 [](OpenMPDirectiveKind) { return true; }, 1782 DSAStack->isClauseParsingMode()); 1783 if (DVarPrivate.CKind != OMPC_unknown) 1784 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1785 } 1786 return nullptr; 1787 } 1788 1789 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex, 1790 unsigned Level) const { 1791 SmallVector<OpenMPDirectiveKind, 4> Regions; 1792 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level)); 1793 FunctionScopesIndex -= Regions.size(); 1794 } 1795 1796 void Sema::startOpenMPLoop() { 1797 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 1798 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) 1799 DSAStack->loopInit(); 1800 } 1801 1802 bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const { 1803 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1804 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 1805 if (DSAStack->getAssociatedLoops() > 0 && 1806 !DSAStack->isLoopStarted()) { 1807 DSAStack->resetPossibleLoopCounter(D); 1808 DSAStack->loopStart(); 1809 return true; 1810 } 1811 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() || 1812 DSAStack->isLoopControlVariable(D).first) && 1813 !DSAStack->hasExplicitDSA( 1814 D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) && 1815 !isOpenMPSimdDirective(DSAStack->getCurrentDirective())) 1816 return true; 1817 } 1818 return DSAStack->hasExplicitDSA( 1819 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) || 1820 (DSAStack->isClauseParsingMode() && 1821 DSAStack->getClauseParsingMode() == OMPC_private) || 1822 // Consider taskgroup reduction descriptor variable a private to avoid 1823 // possible capture in the region. 1824 (DSAStack->hasExplicitDirective( 1825 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; }, 1826 Level) && 1827 DSAStack->isTaskgroupReductionRef(D, Level)); 1828 } 1829 1830 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D, 1831 unsigned Level) { 1832 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1833 D = getCanonicalDecl(D); 1834 OpenMPClauseKind OMPC = OMPC_unknown; 1835 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) { 1836 const unsigned NewLevel = I - 1; 1837 if (DSAStack->hasExplicitDSA(D, 1838 [&OMPC](const OpenMPClauseKind K) { 1839 if (isOpenMPPrivate(K)) { 1840 OMPC = K; 1841 return true; 1842 } 1843 return false; 1844 }, 1845 NewLevel)) 1846 break; 1847 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel( 1848 D, NewLevel, 1849 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 1850 OpenMPClauseKind) { return true; })) { 1851 OMPC = OMPC_map; 1852 break; 1853 } 1854 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 1855 NewLevel)) { 1856 OMPC = OMPC_map; 1857 if (D->getType()->isScalarType() && 1858 DSAStack->getDefaultDMAAtLevel(NewLevel) != 1859 DefaultMapAttributes::DMA_tofrom_scalar) 1860 OMPC = OMPC_firstprivate; 1861 break; 1862 } 1863 } 1864 if (OMPC != OMPC_unknown) 1865 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC)); 1866 } 1867 1868 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, 1869 unsigned Level) const { 1870 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1871 // Return true if the current level is no longer enclosed in a target region. 1872 1873 const auto *VD = dyn_cast<VarDecl>(D); 1874 return VD && !VD->hasLocalStorage() && 1875 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 1876 Level); 1877 } 1878 1879 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 1880 1881 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 1882 const DeclarationNameInfo &DirName, 1883 Scope *CurScope, SourceLocation Loc) { 1884 DSAStack->push(DKind, DirName, CurScope, Loc); 1885 PushExpressionEvaluationContext( 1886 ExpressionEvaluationContext::PotentiallyEvaluated); 1887 } 1888 1889 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 1890 DSAStack->setClauseParsingMode(K); 1891 } 1892 1893 void Sema::EndOpenMPClause() { 1894 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 1895 } 1896 1897 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 1898 ArrayRef<OMPClause *> Clauses); 1899 1900 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 1901 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 1902 // A variable of class type (or array thereof) that appears in a lastprivate 1903 // clause requires an accessible, unambiguous default constructor for the 1904 // class type, unless the list item is also specified in a firstprivate 1905 // clause. 1906 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 1907 for (OMPClause *C : D->clauses()) { 1908 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 1909 SmallVector<Expr *, 8> PrivateCopies; 1910 for (Expr *DE : Clause->varlists()) { 1911 if (DE->isValueDependent() || DE->isTypeDependent()) { 1912 PrivateCopies.push_back(nullptr); 1913 continue; 1914 } 1915 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 1916 auto *VD = cast<VarDecl>(DRE->getDecl()); 1917 QualType Type = VD->getType().getNonReferenceType(); 1918 const DSAStackTy::DSAVarData DVar = 1919 DSAStack->getTopDSA(VD, /*FromParent=*/false); 1920 if (DVar.CKind == OMPC_lastprivate) { 1921 // Generate helper private variable and initialize it with the 1922 // default value. The address of the original variable is replaced 1923 // by the address of the new private variable in CodeGen. This new 1924 // variable is not added to IdResolver, so the code in the OpenMP 1925 // region uses original variable for proper diagnostics. 1926 VarDecl *VDPrivate = buildVarDecl( 1927 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 1928 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE); 1929 ActOnUninitializedDecl(VDPrivate); 1930 if (VDPrivate->isInvalidDecl()) { 1931 PrivateCopies.push_back(nullptr); 1932 continue; 1933 } 1934 PrivateCopies.push_back(buildDeclRefExpr( 1935 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 1936 } else { 1937 // The variable is also a firstprivate, so initialization sequence 1938 // for private copy is generated already. 1939 PrivateCopies.push_back(nullptr); 1940 } 1941 } 1942 Clause->setPrivateCopies(PrivateCopies); 1943 } 1944 } 1945 // Check allocate clauses. 1946 if (!CurContext->isDependentContext()) 1947 checkAllocateClauses(*this, DSAStack, D->clauses()); 1948 } 1949 1950 DSAStack->pop(); 1951 DiscardCleanupsInEvaluationContext(); 1952 PopExpressionEvaluationContext(); 1953 } 1954 1955 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 1956 Expr *NumIterations, Sema &SemaRef, 1957 Scope *S, DSAStackTy *Stack); 1958 1959 namespace { 1960 1961 class VarDeclFilterCCC final : public CorrectionCandidateCallback { 1962 private: 1963 Sema &SemaRef; 1964 1965 public: 1966 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 1967 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1968 NamedDecl *ND = Candidate.getCorrectionDecl(); 1969 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 1970 return VD->hasGlobalStorage() && 1971 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1972 SemaRef.getCurScope()); 1973 } 1974 return false; 1975 } 1976 1977 std::unique_ptr<CorrectionCandidateCallback> clone() override { 1978 return llvm::make_unique<VarDeclFilterCCC>(*this); 1979 } 1980 1981 }; 1982 1983 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback { 1984 private: 1985 Sema &SemaRef; 1986 1987 public: 1988 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 1989 bool ValidateCandidate(const TypoCorrection &Candidate) override { 1990 NamedDecl *ND = Candidate.getCorrectionDecl(); 1991 if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) || 1992 isa<FunctionDecl>(ND))) { 1993 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 1994 SemaRef.getCurScope()); 1995 } 1996 return false; 1997 } 1998 1999 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2000 return llvm::make_unique<VarOrFuncDeclFilterCCC>(*this); 2001 } 2002 }; 2003 2004 } // namespace 2005 2006 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 2007 CXXScopeSpec &ScopeSpec, 2008 const DeclarationNameInfo &Id, 2009 OpenMPDirectiveKind Kind) { 2010 LookupResult Lookup(*this, Id, LookupOrdinaryName); 2011 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 2012 2013 if (Lookup.isAmbiguous()) 2014 return ExprError(); 2015 2016 VarDecl *VD; 2017 if (!Lookup.isSingleResult()) { 2018 VarDeclFilterCCC CCC(*this); 2019 if (TypoCorrection Corrected = 2020 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 2021 CTK_ErrorRecovery)) { 2022 diagnoseTypo(Corrected, 2023 PDiag(Lookup.empty() 2024 ? diag::err_undeclared_var_use_suggest 2025 : diag::err_omp_expected_var_arg_suggest) 2026 << Id.getName()); 2027 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 2028 } else { 2029 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 2030 : diag::err_omp_expected_var_arg) 2031 << Id.getName(); 2032 return ExprError(); 2033 } 2034 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) { 2035 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 2036 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 2037 return ExprError(); 2038 } 2039 Lookup.suppressDiagnostics(); 2040 2041 // OpenMP [2.9.2, Syntax, C/C++] 2042 // Variables must be file-scope, namespace-scope, or static block-scope. 2043 if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) { 2044 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 2045 << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal(); 2046 bool IsDecl = 2047 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2048 Diag(VD->getLocation(), 2049 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2050 << VD; 2051 return ExprError(); 2052 } 2053 2054 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 2055 NamedDecl *ND = CanonicalVD; 2056 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 2057 // A threadprivate directive for file-scope variables must appear outside 2058 // any definition or declaration. 2059 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 2060 !getCurLexicalContext()->isTranslationUnit()) { 2061 Diag(Id.getLoc(), diag::err_omp_var_scope) 2062 << getOpenMPDirectiveName(Kind) << VD; 2063 bool IsDecl = 2064 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2065 Diag(VD->getLocation(), 2066 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2067 << VD; 2068 return ExprError(); 2069 } 2070 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 2071 // A threadprivate directive for static class member variables must appear 2072 // in the class definition, in the same scope in which the member 2073 // variables are declared. 2074 if (CanonicalVD->isStaticDataMember() && 2075 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 2076 Diag(Id.getLoc(), diag::err_omp_var_scope) 2077 << getOpenMPDirectiveName(Kind) << VD; 2078 bool IsDecl = 2079 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2080 Diag(VD->getLocation(), 2081 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2082 << VD; 2083 return ExprError(); 2084 } 2085 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 2086 // A threadprivate directive for namespace-scope variables must appear 2087 // outside any definition or declaration other than the namespace 2088 // definition itself. 2089 if (CanonicalVD->getDeclContext()->isNamespace() && 2090 (!getCurLexicalContext()->isFileContext() || 2091 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 2092 Diag(Id.getLoc(), diag::err_omp_var_scope) 2093 << getOpenMPDirectiveName(Kind) << VD; 2094 bool IsDecl = 2095 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2096 Diag(VD->getLocation(), 2097 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2098 << VD; 2099 return ExprError(); 2100 } 2101 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 2102 // A threadprivate directive for static block-scope variables must appear 2103 // in the scope of the variable and not in a nested scope. 2104 if (CanonicalVD->isLocalVarDecl() && CurScope && 2105 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 2106 Diag(Id.getLoc(), diag::err_omp_var_scope) 2107 << getOpenMPDirectiveName(Kind) << VD; 2108 bool IsDecl = 2109 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2110 Diag(VD->getLocation(), 2111 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2112 << VD; 2113 return ExprError(); 2114 } 2115 2116 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 2117 // A threadprivate directive must lexically precede all references to any 2118 // of the variables in its list. 2119 if (Kind == OMPD_threadprivate && VD->isUsed() && 2120 !DSAStack->isThreadPrivate(VD)) { 2121 Diag(Id.getLoc(), diag::err_omp_var_used) 2122 << getOpenMPDirectiveName(Kind) << VD; 2123 return ExprError(); 2124 } 2125 2126 QualType ExprType = VD->getType().getNonReferenceType(); 2127 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 2128 SourceLocation(), VD, 2129 /*RefersToEnclosingVariableOrCapture=*/false, 2130 Id.getLoc(), ExprType, VK_LValue); 2131 } 2132 2133 Sema::DeclGroupPtrTy 2134 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 2135 ArrayRef<Expr *> VarList) { 2136 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 2137 CurContext->addDecl(D); 2138 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2139 } 2140 return nullptr; 2141 } 2142 2143 namespace { 2144 class LocalVarRefChecker final 2145 : public ConstStmtVisitor<LocalVarRefChecker, bool> { 2146 Sema &SemaRef; 2147 2148 public: 2149 bool VisitDeclRefExpr(const DeclRefExpr *E) { 2150 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2151 if (VD->hasLocalStorage()) { 2152 SemaRef.Diag(E->getBeginLoc(), 2153 diag::err_omp_local_var_in_threadprivate_init) 2154 << E->getSourceRange(); 2155 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 2156 << VD << VD->getSourceRange(); 2157 return true; 2158 } 2159 } 2160 return false; 2161 } 2162 bool VisitStmt(const Stmt *S) { 2163 for (const Stmt *Child : S->children()) { 2164 if (Child && Visit(Child)) 2165 return true; 2166 } 2167 return false; 2168 } 2169 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 2170 }; 2171 } // namespace 2172 2173 OMPThreadPrivateDecl * 2174 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 2175 SmallVector<Expr *, 8> Vars; 2176 for (Expr *RefExpr : VarList) { 2177 auto *DE = cast<DeclRefExpr>(RefExpr); 2178 auto *VD = cast<VarDecl>(DE->getDecl()); 2179 SourceLocation ILoc = DE->getExprLoc(); 2180 2181 // Mark variable as used. 2182 VD->setReferenced(); 2183 VD->markUsed(Context); 2184 2185 QualType QType = VD->getType(); 2186 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 2187 // It will be analyzed later. 2188 Vars.push_back(DE); 2189 continue; 2190 } 2191 2192 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2193 // A threadprivate variable must not have an incomplete type. 2194 if (RequireCompleteType(ILoc, VD->getType(), 2195 diag::err_omp_threadprivate_incomplete_type)) { 2196 continue; 2197 } 2198 2199 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2200 // A threadprivate variable must not have a reference type. 2201 if (VD->getType()->isReferenceType()) { 2202 Diag(ILoc, diag::err_omp_ref_type_arg) 2203 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 2204 bool IsDecl = 2205 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2206 Diag(VD->getLocation(), 2207 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2208 << VD; 2209 continue; 2210 } 2211 2212 // Check if this is a TLS variable. If TLS is not being supported, produce 2213 // the corresponding diagnostic. 2214 if ((VD->getTLSKind() != VarDecl::TLS_None && 2215 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 2216 getLangOpts().OpenMPUseTLS && 2217 getASTContext().getTargetInfo().isTLSSupported())) || 2218 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 2219 !VD->isLocalVarDecl())) { 2220 Diag(ILoc, diag::err_omp_var_thread_local) 2221 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 2222 bool IsDecl = 2223 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2224 Diag(VD->getLocation(), 2225 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2226 << VD; 2227 continue; 2228 } 2229 2230 // Check if initial value of threadprivate variable reference variable with 2231 // local storage (it is not supported by runtime). 2232 if (const Expr *Init = VD->getAnyInitializer()) { 2233 LocalVarRefChecker Checker(*this); 2234 if (Checker.Visit(Init)) 2235 continue; 2236 } 2237 2238 Vars.push_back(RefExpr); 2239 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 2240 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 2241 Context, SourceRange(Loc, Loc))); 2242 if (ASTMutationListener *ML = Context.getASTMutationListener()) 2243 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 2244 } 2245 OMPThreadPrivateDecl *D = nullptr; 2246 if (!Vars.empty()) { 2247 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 2248 Vars); 2249 D->setAccess(AS_public); 2250 } 2251 return D; 2252 } 2253 2254 static OMPAllocateDeclAttr::AllocatorTypeTy 2255 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) { 2256 if (!Allocator) 2257 return OMPAllocateDeclAttr::OMPDefaultMemAlloc; 2258 if (Allocator->isTypeDependent() || Allocator->isValueDependent() || 2259 Allocator->isInstantiationDependent() || 2260 Allocator->containsUnexpandedParameterPack()) 2261 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 2262 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 2263 const Expr *AE = Allocator->IgnoreParenImpCasts(); 2264 for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc; 2265 I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 2266 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 2267 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind); 2268 llvm::FoldingSetNodeID AEId, DAEId; 2269 AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true); 2270 DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true); 2271 if (AEId == DAEId) { 2272 AllocatorKindRes = AllocatorKind; 2273 break; 2274 } 2275 } 2276 return AllocatorKindRes; 2277 } 2278 2279 static bool checkPreviousOMPAllocateAttribute( 2280 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD, 2281 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) { 2282 if (!VD->hasAttr<OMPAllocateDeclAttr>()) 2283 return false; 2284 const auto *A = VD->getAttr<OMPAllocateDeclAttr>(); 2285 Expr *PrevAllocator = A->getAllocator(); 2286 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind = 2287 getAllocatorKind(S, Stack, PrevAllocator); 2288 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind; 2289 if (AllocatorsMatch && 2290 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc && 2291 Allocator && PrevAllocator) { 2292 const Expr *AE = Allocator->IgnoreParenImpCasts(); 2293 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts(); 2294 llvm::FoldingSetNodeID AEId, PAEId; 2295 AE->Profile(AEId, S.Context, /*Canonical=*/true); 2296 PAE->Profile(PAEId, S.Context, /*Canonical=*/true); 2297 AllocatorsMatch = AEId == PAEId; 2298 } 2299 if (!AllocatorsMatch) { 2300 SmallString<256> AllocatorBuffer; 2301 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer); 2302 if (Allocator) 2303 Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy()); 2304 SmallString<256> PrevAllocatorBuffer; 2305 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer); 2306 if (PrevAllocator) 2307 PrevAllocator->printPretty(PrevAllocatorStream, nullptr, 2308 S.getPrintingPolicy()); 2309 2310 SourceLocation AllocatorLoc = 2311 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc(); 2312 SourceRange AllocatorRange = 2313 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange(); 2314 SourceLocation PrevAllocatorLoc = 2315 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation(); 2316 SourceRange PrevAllocatorRange = 2317 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange(); 2318 S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator) 2319 << (Allocator ? 1 : 0) << AllocatorStream.str() 2320 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str() 2321 << AllocatorRange; 2322 S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator) 2323 << PrevAllocatorRange; 2324 return true; 2325 } 2326 return false; 2327 } 2328 2329 static void 2330 applyOMPAllocateAttribute(Sema &S, VarDecl *VD, 2331 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 2332 Expr *Allocator, SourceRange SR) { 2333 if (VD->hasAttr<OMPAllocateDeclAttr>()) 2334 return; 2335 if (Allocator && 2336 (Allocator->isTypeDependent() || Allocator->isValueDependent() || 2337 Allocator->isInstantiationDependent() || 2338 Allocator->containsUnexpandedParameterPack())) 2339 return; 2340 auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind, 2341 Allocator, SR); 2342 VD->addAttr(A); 2343 if (ASTMutationListener *ML = S.Context.getASTMutationListener()) 2344 ML->DeclarationMarkedOpenMPAllocate(VD, A); 2345 } 2346 2347 Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective( 2348 SourceLocation Loc, ArrayRef<Expr *> VarList, 2349 ArrayRef<OMPClause *> Clauses, DeclContext *Owner) { 2350 assert(Clauses.size() <= 1 && "Expected at most one clause."); 2351 Expr *Allocator = nullptr; 2352 if (Clauses.empty()) { 2353 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions. 2354 // allocate directives that appear in a target region must specify an 2355 // allocator clause unless a requires directive with the dynamic_allocators 2356 // clause is present in the same compilation unit. 2357 if (LangOpts.OpenMPIsDevice && 2358 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 2359 targetDiag(Loc, diag::err_expected_allocator_clause); 2360 } else { 2361 Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator(); 2362 } 2363 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 2364 getAllocatorKind(*this, DSAStack, Allocator); 2365 SmallVector<Expr *, 8> Vars; 2366 for (Expr *RefExpr : VarList) { 2367 auto *DE = cast<DeclRefExpr>(RefExpr); 2368 auto *VD = cast<VarDecl>(DE->getDecl()); 2369 2370 // Check if this is a TLS variable or global register. 2371 if (VD->getTLSKind() != VarDecl::TLS_None || 2372 VD->hasAttr<OMPThreadPrivateDeclAttr>() || 2373 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 2374 !VD->isLocalVarDecl())) 2375 continue; 2376 2377 // If the used several times in the allocate directive, the same allocator 2378 // must be used. 2379 if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD, 2380 AllocatorKind, Allocator)) 2381 continue; 2382 2383 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++ 2384 // If a list item has a static storage type, the allocator expression in the 2385 // allocator clause must be a constant expression that evaluates to one of 2386 // the predefined memory allocator values. 2387 if (Allocator && VD->hasGlobalStorage()) { 2388 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) { 2389 Diag(Allocator->getExprLoc(), 2390 diag::err_omp_expected_predefined_allocator) 2391 << Allocator->getSourceRange(); 2392 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 2393 VarDecl::DeclarationOnly; 2394 Diag(VD->getLocation(), 2395 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2396 << VD; 2397 continue; 2398 } 2399 } 2400 2401 Vars.push_back(RefExpr); 2402 applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator, 2403 DE->getSourceRange()); 2404 } 2405 if (Vars.empty()) 2406 return nullptr; 2407 if (!Owner) 2408 Owner = getCurLexicalContext(); 2409 auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses); 2410 D->setAccess(AS_public); 2411 Owner->addDecl(D); 2412 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2413 } 2414 2415 Sema::DeclGroupPtrTy 2416 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc, 2417 ArrayRef<OMPClause *> ClauseList) { 2418 OMPRequiresDecl *D = nullptr; 2419 if (!CurContext->isFileContext()) { 2420 Diag(Loc, diag::err_omp_invalid_scope) << "requires"; 2421 } else { 2422 D = CheckOMPRequiresDecl(Loc, ClauseList); 2423 if (D) { 2424 CurContext->addDecl(D); 2425 DSAStack->addRequiresDecl(D); 2426 } 2427 } 2428 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2429 } 2430 2431 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc, 2432 ArrayRef<OMPClause *> ClauseList) { 2433 /// For target specific clauses, the requires directive cannot be 2434 /// specified after the handling of any of the target regions in the 2435 /// current compilation unit. 2436 ArrayRef<SourceLocation> TargetLocations = 2437 DSAStack->getEncounteredTargetLocs(); 2438 if (!TargetLocations.empty()) { 2439 for (const OMPClause *CNew : ClauseList) { 2440 // Check if any of the requires clauses affect target regions. 2441 if (isa<OMPUnifiedSharedMemoryClause>(CNew) || 2442 isa<OMPUnifiedAddressClause>(CNew) || 2443 isa<OMPReverseOffloadClause>(CNew) || 2444 isa<OMPDynamicAllocatorsClause>(CNew)) { 2445 Diag(Loc, diag::err_omp_target_before_requires) 2446 << getOpenMPClauseName(CNew->getClauseKind()); 2447 for (SourceLocation TargetLoc : TargetLocations) { 2448 Diag(TargetLoc, diag::note_omp_requires_encountered_target); 2449 } 2450 } 2451 } 2452 } 2453 2454 if (!DSAStack->hasDuplicateRequiresClause(ClauseList)) 2455 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc, 2456 ClauseList); 2457 return nullptr; 2458 } 2459 2460 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 2461 const ValueDecl *D, 2462 const DSAStackTy::DSAVarData &DVar, 2463 bool IsLoopIterVar = false) { 2464 if (DVar.RefExpr) { 2465 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 2466 << getOpenMPClauseName(DVar.CKind); 2467 return; 2468 } 2469 enum { 2470 PDSA_StaticMemberShared, 2471 PDSA_StaticLocalVarShared, 2472 PDSA_LoopIterVarPrivate, 2473 PDSA_LoopIterVarLinear, 2474 PDSA_LoopIterVarLastprivate, 2475 PDSA_ConstVarShared, 2476 PDSA_GlobalVarShared, 2477 PDSA_TaskVarFirstprivate, 2478 PDSA_LocalVarPrivate, 2479 PDSA_Implicit 2480 } Reason = PDSA_Implicit; 2481 bool ReportHint = false; 2482 auto ReportLoc = D->getLocation(); 2483 auto *VD = dyn_cast<VarDecl>(D); 2484 if (IsLoopIterVar) { 2485 if (DVar.CKind == OMPC_private) 2486 Reason = PDSA_LoopIterVarPrivate; 2487 else if (DVar.CKind == OMPC_lastprivate) 2488 Reason = PDSA_LoopIterVarLastprivate; 2489 else 2490 Reason = PDSA_LoopIterVarLinear; 2491 } else if (isOpenMPTaskingDirective(DVar.DKind) && 2492 DVar.CKind == OMPC_firstprivate) { 2493 Reason = PDSA_TaskVarFirstprivate; 2494 ReportLoc = DVar.ImplicitDSALoc; 2495 } else if (VD && VD->isStaticLocal()) 2496 Reason = PDSA_StaticLocalVarShared; 2497 else if (VD && VD->isStaticDataMember()) 2498 Reason = PDSA_StaticMemberShared; 2499 else if (VD && VD->isFileVarDecl()) 2500 Reason = PDSA_GlobalVarShared; 2501 else if (D->getType().isConstant(SemaRef.getASTContext())) 2502 Reason = PDSA_ConstVarShared; 2503 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 2504 ReportHint = true; 2505 Reason = PDSA_LocalVarPrivate; 2506 } 2507 if (Reason != PDSA_Implicit) { 2508 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 2509 << Reason << ReportHint 2510 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 2511 } else if (DVar.ImplicitDSALoc.isValid()) { 2512 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 2513 << getOpenMPClauseName(DVar.CKind); 2514 } 2515 } 2516 2517 namespace { 2518 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> { 2519 DSAStackTy *Stack; 2520 Sema &SemaRef; 2521 bool ErrorFound = false; 2522 CapturedStmt *CS = nullptr; 2523 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate; 2524 llvm::SmallVector<Expr *, 4> ImplicitMap; 2525 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA; 2526 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations; 2527 2528 void VisitSubCaptures(OMPExecutableDirective *S) { 2529 // Check implicitly captured variables. 2530 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt()) 2531 return; 2532 for (const CapturedStmt::Capture &Cap : 2533 S->getInnermostCapturedStmt()->captures()) { 2534 if (!Cap.capturesVariable()) 2535 continue; 2536 VarDecl *VD = Cap.getCapturedVar(); 2537 // Do not try to map the variable if it or its sub-component was mapped 2538 // already. 2539 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 2540 Stack->checkMappableExprComponentListsForDecl( 2541 VD, /*CurrentRegionOnly=*/true, 2542 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 2543 OpenMPClauseKind) { return true; })) 2544 continue; 2545 DeclRefExpr *DRE = buildDeclRefExpr( 2546 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context), 2547 Cap.getLocation(), /*RefersToCapture=*/true); 2548 Visit(DRE); 2549 } 2550 } 2551 2552 public: 2553 void VisitDeclRefExpr(DeclRefExpr *E) { 2554 if (E->isTypeDependent() || E->isValueDependent() || 2555 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 2556 return; 2557 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2558 VD = VD->getCanonicalDecl(); 2559 // Skip internally declared variables. 2560 if (VD->hasLocalStorage() && !CS->capturesVariable(VD)) 2561 return; 2562 2563 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 2564 // Check if the variable has explicit DSA set and stop analysis if it so. 2565 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second) 2566 return; 2567 2568 // Skip internally declared static variables. 2569 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 2570 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 2571 if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) && 2572 (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link)) 2573 return; 2574 2575 SourceLocation ELoc = E->getExprLoc(); 2576 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 2577 // The default(none) clause requires that each variable that is referenced 2578 // in the construct, and does not have a predetermined data-sharing 2579 // attribute, must have its data-sharing attribute explicitly determined 2580 // by being listed in a data-sharing attribute clause. 2581 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && 2582 isImplicitOrExplicitTaskingRegion(DKind) && 2583 VarsWithInheritedDSA.count(VD) == 0) { 2584 VarsWithInheritedDSA[VD] = E; 2585 return; 2586 } 2587 2588 if (isOpenMPTargetExecutionDirective(DKind) && 2589 !Stack->isLoopControlVariable(VD).first) { 2590 if (!Stack->checkMappableExprComponentListsForDecl( 2591 VD, /*CurrentRegionOnly=*/true, 2592 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 2593 StackComponents, 2594 OpenMPClauseKind) { 2595 // Variable is used if it has been marked as an array, array 2596 // section or the variable iself. 2597 return StackComponents.size() == 1 || 2598 std::all_of( 2599 std::next(StackComponents.rbegin()), 2600 StackComponents.rend(), 2601 [](const OMPClauseMappableExprCommon:: 2602 MappableComponent &MC) { 2603 return MC.getAssociatedDeclaration() == 2604 nullptr && 2605 (isa<OMPArraySectionExpr>( 2606 MC.getAssociatedExpression()) || 2607 isa<ArraySubscriptExpr>( 2608 MC.getAssociatedExpression())); 2609 }); 2610 })) { 2611 bool IsFirstprivate = false; 2612 // By default lambdas are captured as firstprivates. 2613 if (const auto *RD = 2614 VD->getType().getNonReferenceType()->getAsCXXRecordDecl()) 2615 IsFirstprivate = RD->isLambda(); 2616 IsFirstprivate = 2617 IsFirstprivate || 2618 (VD->getType().getNonReferenceType()->isScalarType() && 2619 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res); 2620 if (IsFirstprivate) 2621 ImplicitFirstprivate.emplace_back(E); 2622 else 2623 ImplicitMap.emplace_back(E); 2624 return; 2625 } 2626 } 2627 2628 // OpenMP [2.9.3.6, Restrictions, p.2] 2629 // A list item that appears in a reduction clause of the innermost 2630 // enclosing worksharing or parallel construct may not be accessed in an 2631 // explicit task. 2632 DVar = Stack->hasInnermostDSA( 2633 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 2634 [](OpenMPDirectiveKind K) { 2635 return isOpenMPParallelDirective(K) || 2636 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 2637 }, 2638 /*FromParent=*/true); 2639 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 2640 ErrorFound = true; 2641 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 2642 reportOriginalDsa(SemaRef, Stack, VD, DVar); 2643 return; 2644 } 2645 2646 // Define implicit data-sharing attributes for task. 2647 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false); 2648 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 2649 !Stack->isLoopControlVariable(VD).first) { 2650 ImplicitFirstprivate.push_back(E); 2651 return; 2652 } 2653 2654 // Store implicitly used globals with declare target link for parent 2655 // target. 2656 if (!isOpenMPTargetExecutionDirective(DKind) && Res && 2657 *Res == OMPDeclareTargetDeclAttr::MT_Link) { 2658 Stack->addToParentTargetRegionLinkGlobals(E); 2659 return; 2660 } 2661 } 2662 } 2663 void VisitMemberExpr(MemberExpr *E) { 2664 if (E->isTypeDependent() || E->isValueDependent() || 2665 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 2666 return; 2667 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 2668 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 2669 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParens())) { 2670 if (!FD) 2671 return; 2672 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false); 2673 // Check if the variable has explicit DSA set and stop analysis if it 2674 // so. 2675 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second) 2676 return; 2677 2678 if (isOpenMPTargetExecutionDirective(DKind) && 2679 !Stack->isLoopControlVariable(FD).first && 2680 !Stack->checkMappableExprComponentListsForDecl( 2681 FD, /*CurrentRegionOnly=*/true, 2682 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 2683 StackComponents, 2684 OpenMPClauseKind) { 2685 return isa<CXXThisExpr>( 2686 cast<MemberExpr>( 2687 StackComponents.back().getAssociatedExpression()) 2688 ->getBase() 2689 ->IgnoreParens()); 2690 })) { 2691 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 2692 // A bit-field cannot appear in a map clause. 2693 // 2694 if (FD->isBitField()) 2695 return; 2696 2697 // Check to see if the member expression is referencing a class that 2698 // has already been explicitly mapped 2699 if (Stack->isClassPreviouslyMapped(TE->getType())) 2700 return; 2701 2702 ImplicitMap.emplace_back(E); 2703 return; 2704 } 2705 2706 SourceLocation ELoc = E->getExprLoc(); 2707 // OpenMP [2.9.3.6, Restrictions, p.2] 2708 // A list item that appears in a reduction clause of the innermost 2709 // enclosing worksharing or parallel construct may not be accessed in 2710 // an explicit task. 2711 DVar = Stack->hasInnermostDSA( 2712 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 2713 [](OpenMPDirectiveKind K) { 2714 return isOpenMPParallelDirective(K) || 2715 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 2716 }, 2717 /*FromParent=*/true); 2718 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 2719 ErrorFound = true; 2720 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 2721 reportOriginalDsa(SemaRef, Stack, FD, DVar); 2722 return; 2723 } 2724 2725 // Define implicit data-sharing attributes for task. 2726 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false); 2727 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 2728 !Stack->isLoopControlVariable(FD).first) { 2729 // Check if there is a captured expression for the current field in the 2730 // region. Do not mark it as firstprivate unless there is no captured 2731 // expression. 2732 // TODO: try to make it firstprivate. 2733 if (DVar.CKind != OMPC_unknown) 2734 ImplicitFirstprivate.push_back(E); 2735 } 2736 return; 2737 } 2738 if (isOpenMPTargetExecutionDirective(DKind)) { 2739 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 2740 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map, 2741 /*NoDiagnose=*/true)) 2742 return; 2743 const auto *VD = cast<ValueDecl>( 2744 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl()); 2745 if (!Stack->checkMappableExprComponentListsForDecl( 2746 VD, /*CurrentRegionOnly=*/true, 2747 [&CurComponents]( 2748 OMPClauseMappableExprCommon::MappableExprComponentListRef 2749 StackComponents, 2750 OpenMPClauseKind) { 2751 auto CCI = CurComponents.rbegin(); 2752 auto CCE = CurComponents.rend(); 2753 for (const auto &SC : llvm::reverse(StackComponents)) { 2754 // Do both expressions have the same kind? 2755 if (CCI->getAssociatedExpression()->getStmtClass() != 2756 SC.getAssociatedExpression()->getStmtClass()) 2757 if (!(isa<OMPArraySectionExpr>( 2758 SC.getAssociatedExpression()) && 2759 isa<ArraySubscriptExpr>( 2760 CCI->getAssociatedExpression()))) 2761 return false; 2762 2763 const Decl *CCD = CCI->getAssociatedDeclaration(); 2764 const Decl *SCD = SC.getAssociatedDeclaration(); 2765 CCD = CCD ? CCD->getCanonicalDecl() : nullptr; 2766 SCD = SCD ? SCD->getCanonicalDecl() : nullptr; 2767 if (SCD != CCD) 2768 return false; 2769 std::advance(CCI, 1); 2770 if (CCI == CCE) 2771 break; 2772 } 2773 return true; 2774 })) { 2775 Visit(E->getBase()); 2776 } 2777 } else { 2778 Visit(E->getBase()); 2779 } 2780 } 2781 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 2782 for (OMPClause *C : S->clauses()) { 2783 // Skip analysis of arguments of implicitly defined firstprivate clause 2784 // for task|target directives. 2785 // Skip analysis of arguments of implicitly defined map clause for target 2786 // directives. 2787 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) && 2788 C->isImplicit())) { 2789 for (Stmt *CC : C->children()) { 2790 if (CC) 2791 Visit(CC); 2792 } 2793 } 2794 } 2795 // Check implicitly captured variables. 2796 VisitSubCaptures(S); 2797 } 2798 void VisitStmt(Stmt *S) { 2799 for (Stmt *C : S->children()) { 2800 if (C) { 2801 // Check implicitly captured variables in the task-based directives to 2802 // check if they must be firstprivatized. 2803 Visit(C); 2804 } 2805 } 2806 } 2807 2808 bool isErrorFound() const { return ErrorFound; } 2809 ArrayRef<Expr *> getImplicitFirstprivate() const { 2810 return ImplicitFirstprivate; 2811 } 2812 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; } 2813 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const { 2814 return VarsWithInheritedDSA; 2815 } 2816 2817 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 2818 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) { 2819 // Process declare target link variables for the target directives. 2820 if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) { 2821 for (DeclRefExpr *E : Stack->getLinkGlobals()) 2822 Visit(E); 2823 } 2824 } 2825 }; 2826 } // namespace 2827 2828 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 2829 switch (DKind) { 2830 case OMPD_parallel: 2831 case OMPD_parallel_for: 2832 case OMPD_parallel_for_simd: 2833 case OMPD_parallel_sections: 2834 case OMPD_teams: 2835 case OMPD_teams_distribute: 2836 case OMPD_teams_distribute_simd: { 2837 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2838 QualType KmpInt32PtrTy = 2839 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2840 Sema::CapturedParamNameType Params[] = { 2841 std::make_pair(".global_tid.", KmpInt32PtrTy), 2842 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2843 std::make_pair(StringRef(), QualType()) // __context with shared vars 2844 }; 2845 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2846 Params); 2847 break; 2848 } 2849 case OMPD_target_teams: 2850 case OMPD_target_parallel: 2851 case OMPD_target_parallel_for: 2852 case OMPD_target_parallel_for_simd: 2853 case OMPD_target_teams_distribute: 2854 case OMPD_target_teams_distribute_simd: { 2855 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2856 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2857 QualType KmpInt32PtrTy = 2858 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2859 QualType Args[] = {VoidPtrTy}; 2860 FunctionProtoType::ExtProtoInfo EPI; 2861 EPI.Variadic = true; 2862 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2863 Sema::CapturedParamNameType Params[] = { 2864 std::make_pair(".global_tid.", KmpInt32Ty), 2865 std::make_pair(".part_id.", KmpInt32PtrTy), 2866 std::make_pair(".privates.", VoidPtrTy), 2867 std::make_pair( 2868 ".copy_fn.", 2869 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2870 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2871 std::make_pair(StringRef(), QualType()) // __context with shared vars 2872 }; 2873 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2874 Params); 2875 // Mark this captured region as inlined, because we don't use outlined 2876 // function directly. 2877 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2878 AlwaysInlineAttr::CreateImplicit( 2879 Context, AlwaysInlineAttr::Keyword_forceinline)); 2880 Sema::CapturedParamNameType ParamsTarget[] = { 2881 std::make_pair(StringRef(), QualType()) // __context with shared vars 2882 }; 2883 // Start a captured region for 'target' with no implicit parameters. 2884 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2885 ParamsTarget); 2886 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 2887 std::make_pair(".global_tid.", KmpInt32PtrTy), 2888 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2889 std::make_pair(StringRef(), QualType()) // __context with shared vars 2890 }; 2891 // Start a captured region for 'teams' or 'parallel'. Both regions have 2892 // the same implicit parameters. 2893 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2894 ParamsTeamsOrParallel); 2895 break; 2896 } 2897 case OMPD_target: 2898 case OMPD_target_simd: { 2899 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2900 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2901 QualType KmpInt32PtrTy = 2902 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2903 QualType Args[] = {VoidPtrTy}; 2904 FunctionProtoType::ExtProtoInfo EPI; 2905 EPI.Variadic = true; 2906 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2907 Sema::CapturedParamNameType Params[] = { 2908 std::make_pair(".global_tid.", KmpInt32Ty), 2909 std::make_pair(".part_id.", KmpInt32PtrTy), 2910 std::make_pair(".privates.", VoidPtrTy), 2911 std::make_pair( 2912 ".copy_fn.", 2913 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2914 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2915 std::make_pair(StringRef(), QualType()) // __context with shared vars 2916 }; 2917 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2918 Params); 2919 // Mark this captured region as inlined, because we don't use outlined 2920 // function directly. 2921 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2922 AlwaysInlineAttr::CreateImplicit( 2923 Context, AlwaysInlineAttr::Keyword_forceinline)); 2924 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2925 std::make_pair(StringRef(), QualType())); 2926 break; 2927 } 2928 case OMPD_simd: 2929 case OMPD_for: 2930 case OMPD_for_simd: 2931 case OMPD_sections: 2932 case OMPD_section: 2933 case OMPD_single: 2934 case OMPD_master: 2935 case OMPD_critical: 2936 case OMPD_taskgroup: 2937 case OMPD_distribute: 2938 case OMPD_distribute_simd: 2939 case OMPD_ordered: 2940 case OMPD_atomic: 2941 case OMPD_target_data: { 2942 Sema::CapturedParamNameType Params[] = { 2943 std::make_pair(StringRef(), QualType()) // __context with shared vars 2944 }; 2945 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2946 Params); 2947 break; 2948 } 2949 case OMPD_task: { 2950 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2951 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2952 QualType KmpInt32PtrTy = 2953 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2954 QualType Args[] = {VoidPtrTy}; 2955 FunctionProtoType::ExtProtoInfo EPI; 2956 EPI.Variadic = true; 2957 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2958 Sema::CapturedParamNameType Params[] = { 2959 std::make_pair(".global_tid.", KmpInt32Ty), 2960 std::make_pair(".part_id.", KmpInt32PtrTy), 2961 std::make_pair(".privates.", VoidPtrTy), 2962 std::make_pair( 2963 ".copy_fn.", 2964 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2965 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2966 std::make_pair(StringRef(), QualType()) // __context with shared vars 2967 }; 2968 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2969 Params); 2970 // Mark this captured region as inlined, because we don't use outlined 2971 // function directly. 2972 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2973 AlwaysInlineAttr::CreateImplicit( 2974 Context, AlwaysInlineAttr::Keyword_forceinline)); 2975 break; 2976 } 2977 case OMPD_taskloop: 2978 case OMPD_taskloop_simd: { 2979 QualType KmpInt32Ty = 2980 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 2981 .withConst(); 2982 QualType KmpUInt64Ty = 2983 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 2984 .withConst(); 2985 QualType KmpInt64Ty = 2986 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 2987 .withConst(); 2988 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2989 QualType KmpInt32PtrTy = 2990 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2991 QualType Args[] = {VoidPtrTy}; 2992 FunctionProtoType::ExtProtoInfo EPI; 2993 EPI.Variadic = true; 2994 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2995 Sema::CapturedParamNameType Params[] = { 2996 std::make_pair(".global_tid.", KmpInt32Ty), 2997 std::make_pair(".part_id.", KmpInt32PtrTy), 2998 std::make_pair(".privates.", VoidPtrTy), 2999 std::make_pair( 3000 ".copy_fn.", 3001 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3002 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3003 std::make_pair(".lb.", KmpUInt64Ty), 3004 std::make_pair(".ub.", KmpUInt64Ty), 3005 std::make_pair(".st.", KmpInt64Ty), 3006 std::make_pair(".liter.", KmpInt32Ty), 3007 std::make_pair(".reductions.", VoidPtrTy), 3008 std::make_pair(StringRef(), QualType()) // __context with shared vars 3009 }; 3010 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3011 Params); 3012 // Mark this captured region as inlined, because we don't use outlined 3013 // function directly. 3014 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3015 AlwaysInlineAttr::CreateImplicit( 3016 Context, AlwaysInlineAttr::Keyword_forceinline)); 3017 break; 3018 } 3019 case OMPD_distribute_parallel_for_simd: 3020 case OMPD_distribute_parallel_for: { 3021 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3022 QualType KmpInt32PtrTy = 3023 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3024 Sema::CapturedParamNameType Params[] = { 3025 std::make_pair(".global_tid.", KmpInt32PtrTy), 3026 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3027 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 3028 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 3029 std::make_pair(StringRef(), QualType()) // __context with shared vars 3030 }; 3031 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3032 Params); 3033 break; 3034 } 3035 case OMPD_target_teams_distribute_parallel_for: 3036 case OMPD_target_teams_distribute_parallel_for_simd: { 3037 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3038 QualType KmpInt32PtrTy = 3039 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3040 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3041 3042 QualType Args[] = {VoidPtrTy}; 3043 FunctionProtoType::ExtProtoInfo EPI; 3044 EPI.Variadic = true; 3045 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3046 Sema::CapturedParamNameType Params[] = { 3047 std::make_pair(".global_tid.", KmpInt32Ty), 3048 std::make_pair(".part_id.", KmpInt32PtrTy), 3049 std::make_pair(".privates.", VoidPtrTy), 3050 std::make_pair( 3051 ".copy_fn.", 3052 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3053 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3054 std::make_pair(StringRef(), QualType()) // __context with shared vars 3055 }; 3056 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3057 Params); 3058 // Mark this captured region as inlined, because we don't use outlined 3059 // function directly. 3060 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3061 AlwaysInlineAttr::CreateImplicit( 3062 Context, AlwaysInlineAttr::Keyword_forceinline)); 3063 Sema::CapturedParamNameType ParamsTarget[] = { 3064 std::make_pair(StringRef(), QualType()) // __context with shared vars 3065 }; 3066 // Start a captured region for 'target' with no implicit parameters. 3067 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3068 ParamsTarget); 3069 3070 Sema::CapturedParamNameType ParamsTeams[] = { 3071 std::make_pair(".global_tid.", KmpInt32PtrTy), 3072 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3073 std::make_pair(StringRef(), QualType()) // __context with shared vars 3074 }; 3075 // Start a captured region for 'target' with no implicit parameters. 3076 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3077 ParamsTeams); 3078 3079 Sema::CapturedParamNameType ParamsParallel[] = { 3080 std::make_pair(".global_tid.", KmpInt32PtrTy), 3081 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3082 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 3083 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 3084 std::make_pair(StringRef(), QualType()) // __context with shared vars 3085 }; 3086 // Start a captured region for 'teams' or 'parallel'. Both regions have 3087 // the same implicit parameters. 3088 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3089 ParamsParallel); 3090 break; 3091 } 3092 3093 case OMPD_teams_distribute_parallel_for: 3094 case OMPD_teams_distribute_parallel_for_simd: { 3095 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3096 QualType KmpInt32PtrTy = 3097 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3098 3099 Sema::CapturedParamNameType ParamsTeams[] = { 3100 std::make_pair(".global_tid.", KmpInt32PtrTy), 3101 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3102 std::make_pair(StringRef(), QualType()) // __context with shared vars 3103 }; 3104 // Start a captured region for 'target' with no implicit parameters. 3105 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3106 ParamsTeams); 3107 3108 Sema::CapturedParamNameType ParamsParallel[] = { 3109 std::make_pair(".global_tid.", KmpInt32PtrTy), 3110 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3111 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 3112 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 3113 std::make_pair(StringRef(), QualType()) // __context with shared vars 3114 }; 3115 // Start a captured region for 'teams' or 'parallel'. Both regions have 3116 // the same implicit parameters. 3117 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3118 ParamsParallel); 3119 break; 3120 } 3121 case OMPD_target_update: 3122 case OMPD_target_enter_data: 3123 case OMPD_target_exit_data: { 3124 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3125 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3126 QualType KmpInt32PtrTy = 3127 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3128 QualType Args[] = {VoidPtrTy}; 3129 FunctionProtoType::ExtProtoInfo EPI; 3130 EPI.Variadic = true; 3131 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3132 Sema::CapturedParamNameType Params[] = { 3133 std::make_pair(".global_tid.", KmpInt32Ty), 3134 std::make_pair(".part_id.", KmpInt32PtrTy), 3135 std::make_pair(".privates.", VoidPtrTy), 3136 std::make_pair( 3137 ".copy_fn.", 3138 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3139 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3140 std::make_pair(StringRef(), QualType()) // __context with shared vars 3141 }; 3142 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3143 Params); 3144 // Mark this captured region as inlined, because we don't use outlined 3145 // function directly. 3146 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3147 AlwaysInlineAttr::CreateImplicit( 3148 Context, AlwaysInlineAttr::Keyword_forceinline)); 3149 break; 3150 } 3151 case OMPD_threadprivate: 3152 case OMPD_allocate: 3153 case OMPD_taskyield: 3154 case OMPD_barrier: 3155 case OMPD_taskwait: 3156 case OMPD_cancellation_point: 3157 case OMPD_cancel: 3158 case OMPD_flush: 3159 case OMPD_declare_reduction: 3160 case OMPD_declare_mapper: 3161 case OMPD_declare_simd: 3162 case OMPD_declare_target: 3163 case OMPD_end_declare_target: 3164 case OMPD_requires: 3165 llvm_unreachable("OpenMP Directive is not allowed"); 3166 case OMPD_unknown: 3167 llvm_unreachable("Unknown OpenMP directive"); 3168 } 3169 } 3170 3171 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 3172 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 3173 getOpenMPCaptureRegions(CaptureRegions, DKind); 3174 return CaptureRegions.size(); 3175 } 3176 3177 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 3178 Expr *CaptureExpr, bool WithInit, 3179 bool AsExpression) { 3180 assert(CaptureExpr); 3181 ASTContext &C = S.getASTContext(); 3182 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 3183 QualType Ty = Init->getType(); 3184 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 3185 if (S.getLangOpts().CPlusPlus) { 3186 Ty = C.getLValueReferenceType(Ty); 3187 } else { 3188 Ty = C.getPointerType(Ty); 3189 ExprResult Res = 3190 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 3191 if (!Res.isUsable()) 3192 return nullptr; 3193 Init = Res.get(); 3194 } 3195 WithInit = true; 3196 } 3197 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 3198 CaptureExpr->getBeginLoc()); 3199 if (!WithInit) 3200 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C)); 3201 S.CurContext->addHiddenDecl(CED); 3202 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 3203 return CED; 3204 } 3205 3206 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 3207 bool WithInit) { 3208 OMPCapturedExprDecl *CD; 3209 if (VarDecl *VD = S.isOpenMPCapturedDecl(D)) 3210 CD = cast<OMPCapturedExprDecl>(VD); 3211 else 3212 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 3213 /*AsExpression=*/false); 3214 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 3215 CaptureExpr->getExprLoc()); 3216 } 3217 3218 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 3219 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get(); 3220 if (!Ref) { 3221 OMPCapturedExprDecl *CD = buildCaptureDecl( 3222 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr, 3223 /*WithInit=*/true, /*AsExpression=*/true); 3224 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 3225 CaptureExpr->getExprLoc()); 3226 } 3227 ExprResult Res = Ref; 3228 if (!S.getLangOpts().CPlusPlus && 3229 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 3230 Ref->getType()->isPointerType()) { 3231 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 3232 if (!Res.isUsable()) 3233 return ExprError(); 3234 } 3235 return S.DefaultLvalueConversion(Res.get()); 3236 } 3237 3238 namespace { 3239 // OpenMP directives parsed in this section are represented as a 3240 // CapturedStatement with an associated statement. If a syntax error 3241 // is detected during the parsing of the associated statement, the 3242 // compiler must abort processing and close the CapturedStatement. 3243 // 3244 // Combined directives such as 'target parallel' have more than one 3245 // nested CapturedStatements. This RAII ensures that we unwind out 3246 // of all the nested CapturedStatements when an error is found. 3247 class CaptureRegionUnwinderRAII { 3248 private: 3249 Sema &S; 3250 bool &ErrorFound; 3251 OpenMPDirectiveKind DKind = OMPD_unknown; 3252 3253 public: 3254 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 3255 OpenMPDirectiveKind DKind) 3256 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 3257 ~CaptureRegionUnwinderRAII() { 3258 if (ErrorFound) { 3259 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 3260 while (--ThisCaptureLevel >= 0) 3261 S.ActOnCapturedRegionError(); 3262 } 3263 } 3264 }; 3265 } // namespace 3266 3267 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 3268 ArrayRef<OMPClause *> Clauses) { 3269 bool ErrorFound = false; 3270 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 3271 *this, ErrorFound, DSAStack->getCurrentDirective()); 3272 if (!S.isUsable()) { 3273 ErrorFound = true; 3274 return StmtError(); 3275 } 3276 3277 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 3278 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 3279 OMPOrderedClause *OC = nullptr; 3280 OMPScheduleClause *SC = nullptr; 3281 SmallVector<const OMPLinearClause *, 4> LCs; 3282 SmallVector<const OMPClauseWithPreInit *, 4> PICs; 3283 // This is required for proper codegen. 3284 for (OMPClause *Clause : Clauses) { 3285 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) && 3286 Clause->getClauseKind() == OMPC_in_reduction) { 3287 // Capture taskgroup task_reduction descriptors inside the tasking regions 3288 // with the corresponding in_reduction items. 3289 auto *IRC = cast<OMPInReductionClause>(Clause); 3290 for (Expr *E : IRC->taskgroup_descriptors()) 3291 if (E) 3292 MarkDeclarationsReferencedInExpr(E); 3293 } 3294 if (isOpenMPPrivate(Clause->getClauseKind()) || 3295 Clause->getClauseKind() == OMPC_copyprivate || 3296 (getLangOpts().OpenMPUseTLS && 3297 getASTContext().getTargetInfo().isTLSSupported() && 3298 Clause->getClauseKind() == OMPC_copyin)) { 3299 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 3300 // Mark all variables in private list clauses as used in inner region. 3301 for (Stmt *VarRef : Clause->children()) { 3302 if (auto *E = cast_or_null<Expr>(VarRef)) { 3303 MarkDeclarationsReferencedInExpr(E); 3304 } 3305 } 3306 DSAStack->setForceVarCapturing(/*V=*/false); 3307 } else if (CaptureRegions.size() > 1 || 3308 CaptureRegions.back() != OMPD_unknown) { 3309 if (auto *C = OMPClauseWithPreInit::get(Clause)) 3310 PICs.push_back(C); 3311 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 3312 if (Expr *E = C->getPostUpdateExpr()) 3313 MarkDeclarationsReferencedInExpr(E); 3314 } 3315 } 3316 if (Clause->getClauseKind() == OMPC_schedule) 3317 SC = cast<OMPScheduleClause>(Clause); 3318 else if (Clause->getClauseKind() == OMPC_ordered) 3319 OC = cast<OMPOrderedClause>(Clause); 3320 else if (Clause->getClauseKind() == OMPC_linear) 3321 LCs.push_back(cast<OMPLinearClause>(Clause)); 3322 } 3323 // OpenMP, 2.7.1 Loop Construct, Restrictions 3324 // The nonmonotonic modifier cannot be specified if an ordered clause is 3325 // specified. 3326 if (SC && 3327 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 3328 SC->getSecondScheduleModifier() == 3329 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 3330 OC) { 3331 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 3332 ? SC->getFirstScheduleModifierLoc() 3333 : SC->getSecondScheduleModifierLoc(), 3334 diag::err_omp_schedule_nonmonotonic_ordered) 3335 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 3336 ErrorFound = true; 3337 } 3338 if (!LCs.empty() && OC && OC->getNumForLoops()) { 3339 for (const OMPLinearClause *C : LCs) { 3340 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered) 3341 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 3342 } 3343 ErrorFound = true; 3344 } 3345 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 3346 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 3347 OC->getNumForLoops()) { 3348 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd) 3349 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 3350 ErrorFound = true; 3351 } 3352 if (ErrorFound) { 3353 return StmtError(); 3354 } 3355 StmtResult SR = S; 3356 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 3357 // Mark all variables in private list clauses as used in inner region. 3358 // Required for proper codegen of combined directives. 3359 // TODO: add processing for other clauses. 3360 if (ThisCaptureRegion != OMPD_unknown) { 3361 for (const clang::OMPClauseWithPreInit *C : PICs) { 3362 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 3363 // Find the particular capture region for the clause if the 3364 // directive is a combined one with multiple capture regions. 3365 // If the directive is not a combined one, the capture region 3366 // associated with the clause is OMPD_unknown and is generated 3367 // only once. 3368 if (CaptureRegion == ThisCaptureRegion || 3369 CaptureRegion == OMPD_unknown) { 3370 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 3371 for (Decl *D : DS->decls()) 3372 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 3373 } 3374 } 3375 } 3376 } 3377 SR = ActOnCapturedRegionEnd(SR.get()); 3378 } 3379 return SR; 3380 } 3381 3382 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 3383 OpenMPDirectiveKind CancelRegion, 3384 SourceLocation StartLoc) { 3385 // CancelRegion is only needed for cancel and cancellation_point. 3386 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 3387 return false; 3388 3389 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 3390 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 3391 return false; 3392 3393 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 3394 << getOpenMPDirectiveName(CancelRegion); 3395 return true; 3396 } 3397 3398 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack, 3399 OpenMPDirectiveKind CurrentRegion, 3400 const DeclarationNameInfo &CurrentName, 3401 OpenMPDirectiveKind CancelRegion, 3402 SourceLocation StartLoc) { 3403 if (Stack->getCurScope()) { 3404 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective(); 3405 OpenMPDirectiveKind OffendingRegion = ParentRegion; 3406 bool NestingProhibited = false; 3407 bool CloseNesting = true; 3408 bool OrphanSeen = false; 3409 enum { 3410 NoRecommend, 3411 ShouldBeInParallelRegion, 3412 ShouldBeInOrderedRegion, 3413 ShouldBeInTargetRegion, 3414 ShouldBeInTeamsRegion 3415 } Recommend = NoRecommend; 3416 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) { 3417 // OpenMP [2.16, Nesting of Regions] 3418 // OpenMP constructs may not be nested inside a simd region. 3419 // OpenMP [2.8.1,simd Construct, Restrictions] 3420 // An ordered construct with the simd clause is the only OpenMP 3421 // construct that can appear in the simd region. 3422 // Allowing a SIMD construct nested in another SIMD construct is an 3423 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 3424 // message. 3425 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 3426 ? diag::err_omp_prohibited_region_simd 3427 : diag::warn_omp_nesting_simd); 3428 return CurrentRegion != OMPD_simd; 3429 } 3430 if (ParentRegion == OMPD_atomic) { 3431 // OpenMP [2.16, Nesting of Regions] 3432 // OpenMP constructs may not be nested inside an atomic region. 3433 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 3434 return true; 3435 } 3436 if (CurrentRegion == OMPD_section) { 3437 // OpenMP [2.7.2, sections Construct, Restrictions] 3438 // Orphaned section directives are prohibited. That is, the section 3439 // directives must appear within the sections construct and must not be 3440 // encountered elsewhere in the sections region. 3441 if (ParentRegion != OMPD_sections && 3442 ParentRegion != OMPD_parallel_sections) { 3443 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 3444 << (ParentRegion != OMPD_unknown) 3445 << getOpenMPDirectiveName(ParentRegion); 3446 return true; 3447 } 3448 return false; 3449 } 3450 // Allow some constructs (except teams and cancellation constructs) to be 3451 // orphaned (they could be used in functions, called from OpenMP regions 3452 // with the required preconditions). 3453 if (ParentRegion == OMPD_unknown && 3454 !isOpenMPNestingTeamsDirective(CurrentRegion) && 3455 CurrentRegion != OMPD_cancellation_point && 3456 CurrentRegion != OMPD_cancel) 3457 return false; 3458 if (CurrentRegion == OMPD_cancellation_point || 3459 CurrentRegion == OMPD_cancel) { 3460 // OpenMP [2.16, Nesting of Regions] 3461 // A cancellation point construct for which construct-type-clause is 3462 // taskgroup must be nested inside a task construct. A cancellation 3463 // point construct for which construct-type-clause is not taskgroup must 3464 // be closely nested inside an OpenMP construct that matches the type 3465 // specified in construct-type-clause. 3466 // A cancel construct for which construct-type-clause is taskgroup must be 3467 // nested inside a task construct. A cancel construct for which 3468 // construct-type-clause is not taskgroup must be closely nested inside an 3469 // OpenMP construct that matches the type specified in 3470 // construct-type-clause. 3471 NestingProhibited = 3472 !((CancelRegion == OMPD_parallel && 3473 (ParentRegion == OMPD_parallel || 3474 ParentRegion == OMPD_target_parallel)) || 3475 (CancelRegion == OMPD_for && 3476 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 3477 ParentRegion == OMPD_target_parallel_for || 3478 ParentRegion == OMPD_distribute_parallel_for || 3479 ParentRegion == OMPD_teams_distribute_parallel_for || 3480 ParentRegion == OMPD_target_teams_distribute_parallel_for)) || 3481 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) || 3482 (CancelRegion == OMPD_sections && 3483 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 3484 ParentRegion == OMPD_parallel_sections))); 3485 OrphanSeen = ParentRegion == OMPD_unknown; 3486 } else if (CurrentRegion == OMPD_master) { 3487 // OpenMP [2.16, Nesting of Regions] 3488 // A master region may not be closely nested inside a worksharing, 3489 // atomic, or explicit task region. 3490 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 3491 isOpenMPTaskingDirective(ParentRegion); 3492 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 3493 // OpenMP [2.16, Nesting of Regions] 3494 // A critical region may not be nested (closely or otherwise) inside a 3495 // critical region with the same name. Note that this restriction is not 3496 // sufficient to prevent deadlock. 3497 SourceLocation PreviousCriticalLoc; 3498 bool DeadLock = Stack->hasDirective( 3499 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 3500 const DeclarationNameInfo &DNI, 3501 SourceLocation Loc) { 3502 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 3503 PreviousCriticalLoc = Loc; 3504 return true; 3505 } 3506 return false; 3507 }, 3508 false /* skip top directive */); 3509 if (DeadLock) { 3510 SemaRef.Diag(StartLoc, 3511 diag::err_omp_prohibited_region_critical_same_name) 3512 << CurrentName.getName(); 3513 if (PreviousCriticalLoc.isValid()) 3514 SemaRef.Diag(PreviousCriticalLoc, 3515 diag::note_omp_previous_critical_region); 3516 return true; 3517 } 3518 } else if (CurrentRegion == OMPD_barrier) { 3519 // OpenMP [2.16, Nesting of Regions] 3520 // A barrier region may not be closely nested inside a worksharing, 3521 // explicit task, critical, ordered, atomic, or master region. 3522 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 3523 isOpenMPTaskingDirective(ParentRegion) || 3524 ParentRegion == OMPD_master || 3525 ParentRegion == OMPD_critical || 3526 ParentRegion == OMPD_ordered; 3527 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 3528 !isOpenMPParallelDirective(CurrentRegion) && 3529 !isOpenMPTeamsDirective(CurrentRegion)) { 3530 // OpenMP [2.16, Nesting of Regions] 3531 // A worksharing region may not be closely nested inside a worksharing, 3532 // explicit task, critical, ordered, atomic, or master region. 3533 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 3534 isOpenMPTaskingDirective(ParentRegion) || 3535 ParentRegion == OMPD_master || 3536 ParentRegion == OMPD_critical || 3537 ParentRegion == OMPD_ordered; 3538 Recommend = ShouldBeInParallelRegion; 3539 } else if (CurrentRegion == OMPD_ordered) { 3540 // OpenMP [2.16, Nesting of Regions] 3541 // An ordered region may not be closely nested inside a critical, 3542 // atomic, or explicit task region. 3543 // An ordered region must be closely nested inside a loop region (or 3544 // parallel loop region) with an ordered clause. 3545 // OpenMP [2.8.1,simd Construct, Restrictions] 3546 // An ordered construct with the simd clause is the only OpenMP construct 3547 // that can appear in the simd region. 3548 NestingProhibited = ParentRegion == OMPD_critical || 3549 isOpenMPTaskingDirective(ParentRegion) || 3550 !(isOpenMPSimdDirective(ParentRegion) || 3551 Stack->isParentOrderedRegion()); 3552 Recommend = ShouldBeInOrderedRegion; 3553 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 3554 // OpenMP [2.16, Nesting of Regions] 3555 // If specified, a teams construct must be contained within a target 3556 // construct. 3557 NestingProhibited = ParentRegion != OMPD_target; 3558 OrphanSeen = ParentRegion == OMPD_unknown; 3559 Recommend = ShouldBeInTargetRegion; 3560 } 3561 if (!NestingProhibited && 3562 !isOpenMPTargetExecutionDirective(CurrentRegion) && 3563 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 3564 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 3565 // OpenMP [2.16, Nesting of Regions] 3566 // distribute, parallel, parallel sections, parallel workshare, and the 3567 // parallel loop and parallel loop SIMD constructs are the only OpenMP 3568 // constructs that can be closely nested in the teams region. 3569 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 3570 !isOpenMPDistributeDirective(CurrentRegion); 3571 Recommend = ShouldBeInParallelRegion; 3572 } 3573 if (!NestingProhibited && 3574 isOpenMPNestingDistributeDirective(CurrentRegion)) { 3575 // OpenMP 4.5 [2.17 Nesting of Regions] 3576 // The region associated with the distribute construct must be strictly 3577 // nested inside a teams region 3578 NestingProhibited = 3579 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 3580 Recommend = ShouldBeInTeamsRegion; 3581 } 3582 if (!NestingProhibited && 3583 (isOpenMPTargetExecutionDirective(CurrentRegion) || 3584 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 3585 // OpenMP 4.5 [2.17 Nesting of Regions] 3586 // If a target, target update, target data, target enter data, or 3587 // target exit data construct is encountered during execution of a 3588 // target region, the behavior is unspecified. 3589 NestingProhibited = Stack->hasDirective( 3590 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 3591 SourceLocation) { 3592 if (isOpenMPTargetExecutionDirective(K)) { 3593 OffendingRegion = K; 3594 return true; 3595 } 3596 return false; 3597 }, 3598 false /* don't skip top directive */); 3599 CloseNesting = false; 3600 } 3601 if (NestingProhibited) { 3602 if (OrphanSeen) { 3603 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 3604 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 3605 } else { 3606 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 3607 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 3608 << Recommend << getOpenMPDirectiveName(CurrentRegion); 3609 } 3610 return true; 3611 } 3612 } 3613 return false; 3614 } 3615 3616 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 3617 ArrayRef<OMPClause *> Clauses, 3618 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 3619 bool ErrorFound = false; 3620 unsigned NamedModifiersNumber = 0; 3621 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers( 3622 OMPD_unknown + 1); 3623 SmallVector<SourceLocation, 4> NameModifierLoc; 3624 for (const OMPClause *C : Clauses) { 3625 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 3626 // At most one if clause without a directive-name-modifier can appear on 3627 // the directive. 3628 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 3629 if (FoundNameModifiers[CurNM]) { 3630 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 3631 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 3632 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 3633 ErrorFound = true; 3634 } else if (CurNM != OMPD_unknown) { 3635 NameModifierLoc.push_back(IC->getNameModifierLoc()); 3636 ++NamedModifiersNumber; 3637 } 3638 FoundNameModifiers[CurNM] = IC; 3639 if (CurNM == OMPD_unknown) 3640 continue; 3641 // Check if the specified name modifier is allowed for the current 3642 // directive. 3643 // At most one if clause with the particular directive-name-modifier can 3644 // appear on the directive. 3645 bool MatchFound = false; 3646 for (auto NM : AllowedNameModifiers) { 3647 if (CurNM == NM) { 3648 MatchFound = true; 3649 break; 3650 } 3651 } 3652 if (!MatchFound) { 3653 S.Diag(IC->getNameModifierLoc(), 3654 diag::err_omp_wrong_if_directive_name_modifier) 3655 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 3656 ErrorFound = true; 3657 } 3658 } 3659 } 3660 // If any if clause on the directive includes a directive-name-modifier then 3661 // all if clauses on the directive must include a directive-name-modifier. 3662 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 3663 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 3664 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(), 3665 diag::err_omp_no_more_if_clause); 3666 } else { 3667 std::string Values; 3668 std::string Sep(", "); 3669 unsigned AllowedCnt = 0; 3670 unsigned TotalAllowedNum = 3671 AllowedNameModifiers.size() - NamedModifiersNumber; 3672 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 3673 ++Cnt) { 3674 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 3675 if (!FoundNameModifiers[NM]) { 3676 Values += "'"; 3677 Values += getOpenMPDirectiveName(NM); 3678 Values += "'"; 3679 if (AllowedCnt + 2 == TotalAllowedNum) 3680 Values += " or "; 3681 else if (AllowedCnt + 1 != TotalAllowedNum) 3682 Values += Sep; 3683 ++AllowedCnt; 3684 } 3685 } 3686 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(), 3687 diag::err_omp_unnamed_if_clause) 3688 << (TotalAllowedNum > 1) << Values; 3689 } 3690 for (SourceLocation Loc : NameModifierLoc) { 3691 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 3692 } 3693 ErrorFound = true; 3694 } 3695 return ErrorFound; 3696 } 3697 3698 static std::pair<ValueDecl *, bool> 3699 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 3700 SourceRange &ERange, bool AllowArraySection = false) { 3701 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 3702 RefExpr->containsUnexpandedParameterPack()) 3703 return std::make_pair(nullptr, true); 3704 3705 // OpenMP [3.1, C/C++] 3706 // A list item is a variable name. 3707 // OpenMP [2.9.3.3, Restrictions, p.1] 3708 // A variable that is part of another variable (as an array or 3709 // structure element) cannot appear in a private clause. 3710 RefExpr = RefExpr->IgnoreParens(); 3711 enum { 3712 NoArrayExpr = -1, 3713 ArraySubscript = 0, 3714 OMPArraySection = 1 3715 } IsArrayExpr = NoArrayExpr; 3716 if (AllowArraySection) { 3717 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 3718 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 3719 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 3720 Base = TempASE->getBase()->IgnoreParenImpCasts(); 3721 RefExpr = Base; 3722 IsArrayExpr = ArraySubscript; 3723 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 3724 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 3725 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 3726 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 3727 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 3728 Base = TempASE->getBase()->IgnoreParenImpCasts(); 3729 RefExpr = Base; 3730 IsArrayExpr = OMPArraySection; 3731 } 3732 } 3733 ELoc = RefExpr->getExprLoc(); 3734 ERange = RefExpr->getSourceRange(); 3735 RefExpr = RefExpr->IgnoreParenImpCasts(); 3736 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 3737 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 3738 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 3739 (S.getCurrentThisType().isNull() || !ME || 3740 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 3741 !isa<FieldDecl>(ME->getMemberDecl()))) { 3742 if (IsArrayExpr != NoArrayExpr) { 3743 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 3744 << ERange; 3745 } else { 3746 S.Diag(ELoc, 3747 AllowArraySection 3748 ? diag::err_omp_expected_var_name_member_expr_or_array_item 3749 : diag::err_omp_expected_var_name_member_expr) 3750 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 3751 } 3752 return std::make_pair(nullptr, false); 3753 } 3754 return std::make_pair( 3755 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 3756 } 3757 3758 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 3759 ArrayRef<OMPClause *> Clauses) { 3760 assert(!S.CurContext->isDependentContext() && 3761 "Expected non-dependent context."); 3762 auto AllocateRange = 3763 llvm::make_filter_range(Clauses, OMPAllocateClause::classof); 3764 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> 3765 DeclToCopy; 3766 auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) { 3767 return isOpenMPPrivate(C->getClauseKind()); 3768 }); 3769 for (OMPClause *Cl : PrivateRange) { 3770 MutableArrayRef<Expr *>::iterator I, It, Et; 3771 if (Cl->getClauseKind() == OMPC_private) { 3772 auto *PC = cast<OMPPrivateClause>(Cl); 3773 I = PC->private_copies().begin(); 3774 It = PC->varlist_begin(); 3775 Et = PC->varlist_end(); 3776 } else if (Cl->getClauseKind() == OMPC_firstprivate) { 3777 auto *PC = cast<OMPFirstprivateClause>(Cl); 3778 I = PC->private_copies().begin(); 3779 It = PC->varlist_begin(); 3780 Et = PC->varlist_end(); 3781 } else if (Cl->getClauseKind() == OMPC_lastprivate) { 3782 auto *PC = cast<OMPLastprivateClause>(Cl); 3783 I = PC->private_copies().begin(); 3784 It = PC->varlist_begin(); 3785 Et = PC->varlist_end(); 3786 } else if (Cl->getClauseKind() == OMPC_linear) { 3787 auto *PC = cast<OMPLinearClause>(Cl); 3788 I = PC->privates().begin(); 3789 It = PC->varlist_begin(); 3790 Et = PC->varlist_end(); 3791 } else if (Cl->getClauseKind() == OMPC_reduction) { 3792 auto *PC = cast<OMPReductionClause>(Cl); 3793 I = PC->privates().begin(); 3794 It = PC->varlist_begin(); 3795 Et = PC->varlist_end(); 3796 } else if (Cl->getClauseKind() == OMPC_task_reduction) { 3797 auto *PC = cast<OMPTaskReductionClause>(Cl); 3798 I = PC->privates().begin(); 3799 It = PC->varlist_begin(); 3800 Et = PC->varlist_end(); 3801 } else if (Cl->getClauseKind() == OMPC_in_reduction) { 3802 auto *PC = cast<OMPInReductionClause>(Cl); 3803 I = PC->privates().begin(); 3804 It = PC->varlist_begin(); 3805 Et = PC->varlist_end(); 3806 } else { 3807 llvm_unreachable("Expected private clause."); 3808 } 3809 for (Expr *E : llvm::make_range(It, Et)) { 3810 if (!*I) { 3811 ++I; 3812 continue; 3813 } 3814 SourceLocation ELoc; 3815 SourceRange ERange; 3816 Expr *SimpleRefExpr = E; 3817 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 3818 /*AllowArraySection=*/true); 3819 DeclToCopy.try_emplace(Res.first, 3820 cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl())); 3821 ++I; 3822 } 3823 } 3824 for (OMPClause *C : AllocateRange) { 3825 auto *AC = cast<OMPAllocateClause>(C); 3826 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 3827 getAllocatorKind(S, Stack, AC->getAllocator()); 3828 // OpenMP, 2.11.4 allocate Clause, Restrictions. 3829 // For task, taskloop or target directives, allocation requests to memory 3830 // allocators with the trait access set to thread result in unspecified 3831 // behavior. 3832 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc && 3833 (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 3834 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) { 3835 S.Diag(AC->getAllocator()->getExprLoc(), 3836 diag::warn_omp_allocate_thread_on_task_target_directive) 3837 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 3838 } 3839 for (Expr *E : AC->varlists()) { 3840 SourceLocation ELoc; 3841 SourceRange ERange; 3842 Expr *SimpleRefExpr = E; 3843 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange); 3844 ValueDecl *VD = Res.first; 3845 DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false); 3846 if (!isOpenMPPrivate(Data.CKind)) { 3847 S.Diag(E->getExprLoc(), 3848 diag::err_omp_expected_private_copy_for_allocate); 3849 continue; 3850 } 3851 VarDecl *PrivateVD = DeclToCopy[VD]; 3852 if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD, 3853 AllocatorKind, AC->getAllocator())) 3854 continue; 3855 applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(), 3856 E->getSourceRange()); 3857 } 3858 } 3859 } 3860 3861 StmtResult Sema::ActOnOpenMPExecutableDirective( 3862 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 3863 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 3864 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 3865 StmtResult Res = StmtError(); 3866 // First check CancelRegion which is then used in checkNestingOfRegions. 3867 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 3868 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 3869 StartLoc)) 3870 return StmtError(); 3871 3872 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 3873 VarsWithInheritedDSAType VarsWithInheritedDSA; 3874 bool ErrorFound = false; 3875 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 3876 if (AStmt && !CurContext->isDependentContext()) { 3877 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3878 3879 // Check default data sharing attributes for referenced variables. 3880 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 3881 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 3882 Stmt *S = AStmt; 3883 while (--ThisCaptureLevel >= 0) 3884 S = cast<CapturedStmt>(S)->getCapturedStmt(); 3885 DSAChecker.Visit(S); 3886 if (DSAChecker.isErrorFound()) 3887 return StmtError(); 3888 // Generate list of implicitly defined firstprivate variables. 3889 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 3890 3891 SmallVector<Expr *, 4> ImplicitFirstprivates( 3892 DSAChecker.getImplicitFirstprivate().begin(), 3893 DSAChecker.getImplicitFirstprivate().end()); 3894 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(), 3895 DSAChecker.getImplicitMap().end()); 3896 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 3897 for (OMPClause *C : Clauses) { 3898 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 3899 for (Expr *E : IRC->taskgroup_descriptors()) 3900 if (E) 3901 ImplicitFirstprivates.emplace_back(E); 3902 } 3903 } 3904 if (!ImplicitFirstprivates.empty()) { 3905 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 3906 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 3907 SourceLocation())) { 3908 ClausesWithImplicit.push_back(Implicit); 3909 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 3910 ImplicitFirstprivates.size(); 3911 } else { 3912 ErrorFound = true; 3913 } 3914 } 3915 if (!ImplicitMaps.empty()) { 3916 CXXScopeSpec MapperIdScopeSpec; 3917 DeclarationNameInfo MapperId; 3918 if (OMPClause *Implicit = ActOnOpenMPMapClause( 3919 llvm::None, llvm::None, MapperIdScopeSpec, MapperId, 3920 OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true, SourceLocation(), 3921 SourceLocation(), ImplicitMaps, OMPVarListLocTy())) { 3922 ClausesWithImplicit.emplace_back(Implicit); 3923 ErrorFound |= 3924 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size(); 3925 } else { 3926 ErrorFound = true; 3927 } 3928 } 3929 } 3930 3931 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 3932 switch (Kind) { 3933 case OMPD_parallel: 3934 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 3935 EndLoc); 3936 AllowedNameModifiers.push_back(OMPD_parallel); 3937 break; 3938 case OMPD_simd: 3939 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 3940 VarsWithInheritedDSA); 3941 break; 3942 case OMPD_for: 3943 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 3944 VarsWithInheritedDSA); 3945 break; 3946 case OMPD_for_simd: 3947 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 3948 EndLoc, VarsWithInheritedDSA); 3949 break; 3950 case OMPD_sections: 3951 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 3952 EndLoc); 3953 break; 3954 case OMPD_section: 3955 assert(ClausesWithImplicit.empty() && 3956 "No clauses are allowed for 'omp section' directive"); 3957 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 3958 break; 3959 case OMPD_single: 3960 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 3961 EndLoc); 3962 break; 3963 case OMPD_master: 3964 assert(ClausesWithImplicit.empty() && 3965 "No clauses are allowed for 'omp master' directive"); 3966 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 3967 break; 3968 case OMPD_critical: 3969 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 3970 StartLoc, EndLoc); 3971 break; 3972 case OMPD_parallel_for: 3973 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 3974 EndLoc, VarsWithInheritedDSA); 3975 AllowedNameModifiers.push_back(OMPD_parallel); 3976 break; 3977 case OMPD_parallel_for_simd: 3978 Res = ActOnOpenMPParallelForSimdDirective( 3979 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 3980 AllowedNameModifiers.push_back(OMPD_parallel); 3981 break; 3982 case OMPD_parallel_sections: 3983 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 3984 StartLoc, EndLoc); 3985 AllowedNameModifiers.push_back(OMPD_parallel); 3986 break; 3987 case OMPD_task: 3988 Res = 3989 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 3990 AllowedNameModifiers.push_back(OMPD_task); 3991 break; 3992 case OMPD_taskyield: 3993 assert(ClausesWithImplicit.empty() && 3994 "No clauses are allowed for 'omp taskyield' directive"); 3995 assert(AStmt == nullptr && 3996 "No associated statement allowed for 'omp taskyield' directive"); 3997 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 3998 break; 3999 case OMPD_barrier: 4000 assert(ClausesWithImplicit.empty() && 4001 "No clauses are allowed for 'omp barrier' directive"); 4002 assert(AStmt == nullptr && 4003 "No associated statement allowed for 'omp barrier' directive"); 4004 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 4005 break; 4006 case OMPD_taskwait: 4007 assert(ClausesWithImplicit.empty() && 4008 "No clauses are allowed for 'omp taskwait' directive"); 4009 assert(AStmt == nullptr && 4010 "No associated statement allowed for 'omp taskwait' directive"); 4011 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 4012 break; 4013 case OMPD_taskgroup: 4014 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 4015 EndLoc); 4016 break; 4017 case OMPD_flush: 4018 assert(AStmt == nullptr && 4019 "No associated statement allowed for 'omp flush' directive"); 4020 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 4021 break; 4022 case OMPD_ordered: 4023 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 4024 EndLoc); 4025 break; 4026 case OMPD_atomic: 4027 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 4028 EndLoc); 4029 break; 4030 case OMPD_teams: 4031 Res = 4032 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 4033 break; 4034 case OMPD_target: 4035 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 4036 EndLoc); 4037 AllowedNameModifiers.push_back(OMPD_target); 4038 break; 4039 case OMPD_target_parallel: 4040 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 4041 StartLoc, EndLoc); 4042 AllowedNameModifiers.push_back(OMPD_target); 4043 AllowedNameModifiers.push_back(OMPD_parallel); 4044 break; 4045 case OMPD_target_parallel_for: 4046 Res = ActOnOpenMPTargetParallelForDirective( 4047 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4048 AllowedNameModifiers.push_back(OMPD_target); 4049 AllowedNameModifiers.push_back(OMPD_parallel); 4050 break; 4051 case OMPD_cancellation_point: 4052 assert(ClausesWithImplicit.empty() && 4053 "No clauses are allowed for 'omp cancellation point' directive"); 4054 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 4055 "cancellation point' directive"); 4056 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 4057 break; 4058 case OMPD_cancel: 4059 assert(AStmt == nullptr && 4060 "No associated statement allowed for 'omp cancel' directive"); 4061 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 4062 CancelRegion); 4063 AllowedNameModifiers.push_back(OMPD_cancel); 4064 break; 4065 case OMPD_target_data: 4066 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 4067 EndLoc); 4068 AllowedNameModifiers.push_back(OMPD_target_data); 4069 break; 4070 case OMPD_target_enter_data: 4071 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 4072 EndLoc, AStmt); 4073 AllowedNameModifiers.push_back(OMPD_target_enter_data); 4074 break; 4075 case OMPD_target_exit_data: 4076 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 4077 EndLoc, AStmt); 4078 AllowedNameModifiers.push_back(OMPD_target_exit_data); 4079 break; 4080 case OMPD_taskloop: 4081 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 4082 EndLoc, VarsWithInheritedDSA); 4083 AllowedNameModifiers.push_back(OMPD_taskloop); 4084 break; 4085 case OMPD_taskloop_simd: 4086 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 4087 EndLoc, VarsWithInheritedDSA); 4088 AllowedNameModifiers.push_back(OMPD_taskloop); 4089 break; 4090 case OMPD_distribute: 4091 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 4092 EndLoc, VarsWithInheritedDSA); 4093 break; 4094 case OMPD_target_update: 4095 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 4096 EndLoc, AStmt); 4097 AllowedNameModifiers.push_back(OMPD_target_update); 4098 break; 4099 case OMPD_distribute_parallel_for: 4100 Res = ActOnOpenMPDistributeParallelForDirective( 4101 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4102 AllowedNameModifiers.push_back(OMPD_parallel); 4103 break; 4104 case OMPD_distribute_parallel_for_simd: 4105 Res = ActOnOpenMPDistributeParallelForSimdDirective( 4106 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4107 AllowedNameModifiers.push_back(OMPD_parallel); 4108 break; 4109 case OMPD_distribute_simd: 4110 Res = ActOnOpenMPDistributeSimdDirective( 4111 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4112 break; 4113 case OMPD_target_parallel_for_simd: 4114 Res = ActOnOpenMPTargetParallelForSimdDirective( 4115 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4116 AllowedNameModifiers.push_back(OMPD_target); 4117 AllowedNameModifiers.push_back(OMPD_parallel); 4118 break; 4119 case OMPD_target_simd: 4120 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 4121 EndLoc, VarsWithInheritedDSA); 4122 AllowedNameModifiers.push_back(OMPD_target); 4123 break; 4124 case OMPD_teams_distribute: 4125 Res = ActOnOpenMPTeamsDistributeDirective( 4126 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4127 break; 4128 case OMPD_teams_distribute_simd: 4129 Res = ActOnOpenMPTeamsDistributeSimdDirective( 4130 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4131 break; 4132 case OMPD_teams_distribute_parallel_for_simd: 4133 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 4134 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4135 AllowedNameModifiers.push_back(OMPD_parallel); 4136 break; 4137 case OMPD_teams_distribute_parallel_for: 4138 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 4139 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4140 AllowedNameModifiers.push_back(OMPD_parallel); 4141 break; 4142 case OMPD_target_teams: 4143 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 4144 EndLoc); 4145 AllowedNameModifiers.push_back(OMPD_target); 4146 break; 4147 case OMPD_target_teams_distribute: 4148 Res = ActOnOpenMPTargetTeamsDistributeDirective( 4149 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4150 AllowedNameModifiers.push_back(OMPD_target); 4151 break; 4152 case OMPD_target_teams_distribute_parallel_for: 4153 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 4154 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4155 AllowedNameModifiers.push_back(OMPD_target); 4156 AllowedNameModifiers.push_back(OMPD_parallel); 4157 break; 4158 case OMPD_target_teams_distribute_parallel_for_simd: 4159 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 4160 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4161 AllowedNameModifiers.push_back(OMPD_target); 4162 AllowedNameModifiers.push_back(OMPD_parallel); 4163 break; 4164 case OMPD_target_teams_distribute_simd: 4165 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 4166 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4167 AllowedNameModifiers.push_back(OMPD_target); 4168 break; 4169 case OMPD_declare_target: 4170 case OMPD_end_declare_target: 4171 case OMPD_threadprivate: 4172 case OMPD_allocate: 4173 case OMPD_declare_reduction: 4174 case OMPD_declare_mapper: 4175 case OMPD_declare_simd: 4176 case OMPD_requires: 4177 llvm_unreachable("OpenMP Directive is not allowed"); 4178 case OMPD_unknown: 4179 llvm_unreachable("Unknown OpenMP directive"); 4180 } 4181 4182 ErrorFound = Res.isInvalid() || ErrorFound; 4183 4184 for (const auto &P : VarsWithInheritedDSA) { 4185 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 4186 << P.first << P.second->getSourceRange(); 4187 } 4188 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound; 4189 4190 if (!AllowedNameModifiers.empty()) 4191 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 4192 ErrorFound; 4193 4194 if (ErrorFound) 4195 return StmtError(); 4196 4197 if (!(Res.getAs<OMPExecutableDirective>()->isStandaloneDirective())) { 4198 Res.getAs<OMPExecutableDirective>() 4199 ->getStructuredBlock() 4200 ->setIsOMPStructuredBlock(true); 4201 } 4202 4203 if (!CurContext->isDependentContext() && 4204 isOpenMPTargetExecutionDirective(Kind) && 4205 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 4206 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() || 4207 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() || 4208 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) { 4209 // Register target to DSA Stack. 4210 DSAStack->addTargetDirLocation(StartLoc); 4211 } 4212 4213 return Res; 4214 } 4215 4216 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 4217 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 4218 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 4219 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 4220 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 4221 assert(Aligneds.size() == Alignments.size()); 4222 assert(Linears.size() == LinModifiers.size()); 4223 assert(Linears.size() == Steps.size()); 4224 if (!DG || DG.get().isNull()) 4225 return DeclGroupPtrTy(); 4226 4227 if (!DG.get().isSingleDecl()) { 4228 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd); 4229 return DG; 4230 } 4231 Decl *ADecl = DG.get().getSingleDecl(); 4232 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 4233 ADecl = FTD->getTemplatedDecl(); 4234 4235 auto *FD = dyn_cast<FunctionDecl>(ADecl); 4236 if (!FD) { 4237 Diag(ADecl->getLocation(), diag::err_omp_function_expected); 4238 return DeclGroupPtrTy(); 4239 } 4240 4241 // OpenMP [2.8.2, declare simd construct, Description] 4242 // The parameter of the simdlen clause must be a constant positive integer 4243 // expression. 4244 ExprResult SL; 4245 if (Simdlen) 4246 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 4247 // OpenMP [2.8.2, declare simd construct, Description] 4248 // The special this pointer can be used as if was one of the arguments to the 4249 // function in any of the linear, aligned, or uniform clauses. 4250 // The uniform clause declares one or more arguments to have an invariant 4251 // value for all concurrent invocations of the function in the execution of a 4252 // single SIMD loop. 4253 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 4254 const Expr *UniformedLinearThis = nullptr; 4255 for (const Expr *E : Uniforms) { 4256 E = E->IgnoreParenImpCasts(); 4257 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 4258 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 4259 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 4260 FD->getParamDecl(PVD->getFunctionScopeIndex()) 4261 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 4262 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 4263 continue; 4264 } 4265 if (isa<CXXThisExpr>(E)) { 4266 UniformedLinearThis = E; 4267 continue; 4268 } 4269 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 4270 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 4271 } 4272 // OpenMP [2.8.2, declare simd construct, Description] 4273 // The aligned clause declares that the object to which each list item points 4274 // is aligned to the number of bytes expressed in the optional parameter of 4275 // the aligned clause. 4276 // The special this pointer can be used as if was one of the arguments to the 4277 // function in any of the linear, aligned, or uniform clauses. 4278 // The type of list items appearing in the aligned clause must be array, 4279 // pointer, reference to array, or reference to pointer. 4280 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 4281 const Expr *AlignedThis = nullptr; 4282 for (const Expr *E : Aligneds) { 4283 E = E->IgnoreParenImpCasts(); 4284 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 4285 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 4286 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 4287 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 4288 FD->getParamDecl(PVD->getFunctionScopeIndex()) 4289 ->getCanonicalDecl() == CanonPVD) { 4290 // OpenMP [2.8.1, simd construct, Restrictions] 4291 // A list-item cannot appear in more than one aligned clause. 4292 if (AlignedArgs.count(CanonPVD) > 0) { 4293 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 4294 << 1 << E->getSourceRange(); 4295 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 4296 diag::note_omp_explicit_dsa) 4297 << getOpenMPClauseName(OMPC_aligned); 4298 continue; 4299 } 4300 AlignedArgs[CanonPVD] = E; 4301 QualType QTy = PVD->getType() 4302 .getNonReferenceType() 4303 .getUnqualifiedType() 4304 .getCanonicalType(); 4305 const Type *Ty = QTy.getTypePtrOrNull(); 4306 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 4307 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 4308 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 4309 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 4310 } 4311 continue; 4312 } 4313 } 4314 if (isa<CXXThisExpr>(E)) { 4315 if (AlignedThis) { 4316 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 4317 << 2 << E->getSourceRange(); 4318 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 4319 << getOpenMPClauseName(OMPC_aligned); 4320 } 4321 AlignedThis = E; 4322 continue; 4323 } 4324 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 4325 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 4326 } 4327 // The optional parameter of the aligned clause, alignment, must be a constant 4328 // positive integer expression. If no optional parameter is specified, 4329 // implementation-defined default alignments for SIMD instructions on the 4330 // target platforms are assumed. 4331 SmallVector<const Expr *, 4> NewAligns; 4332 for (Expr *E : Alignments) { 4333 ExprResult Align; 4334 if (E) 4335 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 4336 NewAligns.push_back(Align.get()); 4337 } 4338 // OpenMP [2.8.2, declare simd construct, Description] 4339 // The linear clause declares one or more list items to be private to a SIMD 4340 // lane and to have a linear relationship with respect to the iteration space 4341 // of a loop. 4342 // The special this pointer can be used as if was one of the arguments to the 4343 // function in any of the linear, aligned, or uniform clauses. 4344 // When a linear-step expression is specified in a linear clause it must be 4345 // either a constant integer expression or an integer-typed parameter that is 4346 // specified in a uniform clause on the directive. 4347 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 4348 const bool IsUniformedThis = UniformedLinearThis != nullptr; 4349 auto MI = LinModifiers.begin(); 4350 for (const Expr *E : Linears) { 4351 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 4352 ++MI; 4353 E = E->IgnoreParenImpCasts(); 4354 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 4355 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 4356 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 4357 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 4358 FD->getParamDecl(PVD->getFunctionScopeIndex()) 4359 ->getCanonicalDecl() == CanonPVD) { 4360 // OpenMP [2.15.3.7, linear Clause, Restrictions] 4361 // A list-item cannot appear in more than one linear clause. 4362 if (LinearArgs.count(CanonPVD) > 0) { 4363 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 4364 << getOpenMPClauseName(OMPC_linear) 4365 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 4366 Diag(LinearArgs[CanonPVD]->getExprLoc(), 4367 diag::note_omp_explicit_dsa) 4368 << getOpenMPClauseName(OMPC_linear); 4369 continue; 4370 } 4371 // Each argument can appear in at most one uniform or linear clause. 4372 if (UniformedArgs.count(CanonPVD) > 0) { 4373 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 4374 << getOpenMPClauseName(OMPC_linear) 4375 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 4376 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 4377 diag::note_omp_explicit_dsa) 4378 << getOpenMPClauseName(OMPC_uniform); 4379 continue; 4380 } 4381 LinearArgs[CanonPVD] = E; 4382 if (E->isValueDependent() || E->isTypeDependent() || 4383 E->isInstantiationDependent() || 4384 E->containsUnexpandedParameterPack()) 4385 continue; 4386 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 4387 PVD->getOriginalType()); 4388 continue; 4389 } 4390 } 4391 if (isa<CXXThisExpr>(E)) { 4392 if (UniformedLinearThis) { 4393 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 4394 << getOpenMPClauseName(OMPC_linear) 4395 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 4396 << E->getSourceRange(); 4397 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 4398 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 4399 : OMPC_linear); 4400 continue; 4401 } 4402 UniformedLinearThis = E; 4403 if (E->isValueDependent() || E->isTypeDependent() || 4404 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 4405 continue; 4406 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 4407 E->getType()); 4408 continue; 4409 } 4410 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 4411 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 4412 } 4413 Expr *Step = nullptr; 4414 Expr *NewStep = nullptr; 4415 SmallVector<Expr *, 4> NewSteps; 4416 for (Expr *E : Steps) { 4417 // Skip the same step expression, it was checked already. 4418 if (Step == E || !E) { 4419 NewSteps.push_back(E ? NewStep : nullptr); 4420 continue; 4421 } 4422 Step = E; 4423 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 4424 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 4425 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 4426 if (UniformedArgs.count(CanonPVD) == 0) { 4427 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 4428 << Step->getSourceRange(); 4429 } else if (E->isValueDependent() || E->isTypeDependent() || 4430 E->isInstantiationDependent() || 4431 E->containsUnexpandedParameterPack() || 4432 CanonPVD->getType()->hasIntegerRepresentation()) { 4433 NewSteps.push_back(Step); 4434 } else { 4435 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 4436 << Step->getSourceRange(); 4437 } 4438 continue; 4439 } 4440 NewStep = Step; 4441 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 4442 !Step->isInstantiationDependent() && 4443 !Step->containsUnexpandedParameterPack()) { 4444 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 4445 .get(); 4446 if (NewStep) 4447 NewStep = VerifyIntegerConstantExpression(NewStep).get(); 4448 } 4449 NewSteps.push_back(NewStep); 4450 } 4451 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 4452 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 4453 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 4454 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 4455 const_cast<Expr **>(Linears.data()), Linears.size(), 4456 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 4457 NewSteps.data(), NewSteps.size(), SR); 4458 ADecl->addAttr(NewAttr); 4459 return ConvertDeclToDeclGroup(ADecl); 4460 } 4461 4462 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 4463 Stmt *AStmt, 4464 SourceLocation StartLoc, 4465 SourceLocation EndLoc) { 4466 if (!AStmt) 4467 return StmtError(); 4468 4469 auto *CS = cast<CapturedStmt>(AStmt); 4470 // 1.2.2 OpenMP Language Terminology 4471 // Structured block - An executable statement with a single entry at the 4472 // top and a single exit at the bottom. 4473 // The point of exit cannot be a branch out of the structured block. 4474 // longjmp() and throw() must not violate the entry/exit criteria. 4475 CS->getCapturedDecl()->setNothrow(); 4476 4477 setFunctionHasBranchProtectedScope(); 4478 4479 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 4480 DSAStack->isCancelRegion()); 4481 } 4482 4483 namespace { 4484 /// Helper class for checking canonical form of the OpenMP loops and 4485 /// extracting iteration space of each loop in the loop nest, that will be used 4486 /// for IR generation. 4487 class OpenMPIterationSpaceChecker { 4488 /// Reference to Sema. 4489 Sema &SemaRef; 4490 /// Data-sharing stack. 4491 DSAStackTy &Stack; 4492 /// A location for diagnostics (when there is no some better location). 4493 SourceLocation DefaultLoc; 4494 /// A location for diagnostics (when increment is not compatible). 4495 SourceLocation ConditionLoc; 4496 /// A source location for referring to loop init later. 4497 SourceRange InitSrcRange; 4498 /// A source location for referring to condition later. 4499 SourceRange ConditionSrcRange; 4500 /// A source location for referring to increment later. 4501 SourceRange IncrementSrcRange; 4502 /// Loop variable. 4503 ValueDecl *LCDecl = nullptr; 4504 /// Reference to loop variable. 4505 Expr *LCRef = nullptr; 4506 /// Lower bound (initializer for the var). 4507 Expr *LB = nullptr; 4508 /// Upper bound. 4509 Expr *UB = nullptr; 4510 /// Loop step (increment). 4511 Expr *Step = nullptr; 4512 /// This flag is true when condition is one of: 4513 /// Var < UB 4514 /// Var <= UB 4515 /// UB > Var 4516 /// UB >= Var 4517 /// This will have no value when the condition is != 4518 llvm::Optional<bool> TestIsLessOp; 4519 /// This flag is true when condition is strict ( < or > ). 4520 bool TestIsStrictOp = false; 4521 /// This flag is true when step is subtracted on each iteration. 4522 bool SubtractStep = false; 4523 /// Checks if the provide statement depends on the loop counter. 4524 bool doesDependOnLoopCounter(const Stmt *S, bool IsInitializer) const; 4525 4526 public: 4527 OpenMPIterationSpaceChecker(Sema &SemaRef, DSAStackTy &Stack, 4528 SourceLocation DefaultLoc) 4529 : SemaRef(SemaRef), Stack(Stack), DefaultLoc(DefaultLoc), 4530 ConditionLoc(DefaultLoc) {} 4531 /// Check init-expr for canonical loop form and save loop counter 4532 /// variable - #Var and its initialization value - #LB. 4533 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 4534 /// Check test-expr for canonical form, save upper-bound (#UB), flags 4535 /// for less/greater and for strict/non-strict comparison. 4536 bool checkAndSetCond(Expr *S); 4537 /// Check incr-expr for canonical loop form and return true if it 4538 /// does not conform, otherwise save loop step (#Step). 4539 bool checkAndSetInc(Expr *S); 4540 /// Return the loop counter variable. 4541 ValueDecl *getLoopDecl() const { return LCDecl; } 4542 /// Return the reference expression to loop counter variable. 4543 Expr *getLoopDeclRefExpr() const { return LCRef; } 4544 /// Source range of the loop init. 4545 SourceRange getInitSrcRange() const { return InitSrcRange; } 4546 /// Source range of the loop condition. 4547 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 4548 /// Source range of the loop increment. 4549 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 4550 /// True if the step should be subtracted. 4551 bool shouldSubtractStep() const { return SubtractStep; } 4552 /// True, if the compare operator is strict (<, > or !=). 4553 bool isStrictTestOp() const { return TestIsStrictOp; } 4554 /// Build the expression to calculate the number of iterations. 4555 Expr *buildNumIterations( 4556 Scope *S, const bool LimitedType, 4557 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 4558 /// Build the precondition expression for the loops. 4559 Expr * 4560 buildPreCond(Scope *S, Expr *Cond, 4561 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 4562 /// Build reference expression to the counter be used for codegen. 4563 DeclRefExpr * 4564 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 4565 DSAStackTy &DSA) const; 4566 /// Build reference expression to the private counter be used for 4567 /// codegen. 4568 Expr *buildPrivateCounterVar() const; 4569 /// Build initialization of the counter be used for codegen. 4570 Expr *buildCounterInit() const; 4571 /// Build step of the counter be used for codegen. 4572 Expr *buildCounterStep() const; 4573 /// Build loop data with counter value for depend clauses in ordered 4574 /// directives. 4575 Expr * 4576 buildOrderedLoopData(Scope *S, Expr *Counter, 4577 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 4578 SourceLocation Loc, Expr *Inc = nullptr, 4579 OverloadedOperatorKind OOK = OO_Amp); 4580 /// Return true if any expression is dependent. 4581 bool dependent() const; 4582 4583 private: 4584 /// Check the right-hand side of an assignment in the increment 4585 /// expression. 4586 bool checkAndSetIncRHS(Expr *RHS); 4587 /// Helper to set loop counter variable and its initializer. 4588 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB, 4589 bool EmitDiags); 4590 /// Helper to set upper bound. 4591 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp, 4592 SourceRange SR, SourceLocation SL); 4593 /// Helper to set loop increment. 4594 bool setStep(Expr *NewStep, bool Subtract); 4595 }; 4596 4597 bool OpenMPIterationSpaceChecker::dependent() const { 4598 if (!LCDecl) { 4599 assert(!LB && !UB && !Step); 4600 return false; 4601 } 4602 return LCDecl->getType()->isDependentType() || 4603 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 4604 (Step && Step->isValueDependent()); 4605 } 4606 4607 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 4608 Expr *NewLCRefExpr, 4609 Expr *NewLB, bool EmitDiags) { 4610 // State consistency checking to ensure correct usage. 4611 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 4612 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 4613 if (!NewLCDecl || !NewLB) 4614 return true; 4615 LCDecl = getCanonicalDecl(NewLCDecl); 4616 LCRef = NewLCRefExpr; 4617 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 4618 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 4619 if ((Ctor->isCopyOrMoveConstructor() || 4620 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 4621 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 4622 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 4623 LB = NewLB; 4624 if (EmitDiags) 4625 (void)doesDependOnLoopCounter(LB, /*IsInitializer=*/true); 4626 return false; 4627 } 4628 4629 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, 4630 llvm::Optional<bool> LessOp, 4631 bool StrictOp, SourceRange SR, 4632 SourceLocation SL) { 4633 // State consistency checking to ensure correct usage. 4634 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 4635 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 4636 if (!NewUB) 4637 return true; 4638 UB = NewUB; 4639 if (LessOp) 4640 TestIsLessOp = LessOp; 4641 TestIsStrictOp = StrictOp; 4642 ConditionSrcRange = SR; 4643 ConditionLoc = SL; 4644 (void)doesDependOnLoopCounter(UB, /*IsInitializer=*/false); 4645 return false; 4646 } 4647 4648 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 4649 // State consistency checking to ensure correct usage. 4650 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 4651 if (!NewStep) 4652 return true; 4653 if (!NewStep->isValueDependent()) { 4654 // Check that the step is integer expression. 4655 SourceLocation StepLoc = NewStep->getBeginLoc(); 4656 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 4657 StepLoc, getExprAsWritten(NewStep)); 4658 if (Val.isInvalid()) 4659 return true; 4660 NewStep = Val.get(); 4661 4662 // OpenMP [2.6, Canonical Loop Form, Restrictions] 4663 // If test-expr is of form var relational-op b and relational-op is < or 4664 // <= then incr-expr must cause var to increase on each iteration of the 4665 // loop. If test-expr is of form var relational-op b and relational-op is 4666 // > or >= then incr-expr must cause var to decrease on each iteration of 4667 // the loop. 4668 // If test-expr is of form b relational-op var and relational-op is < or 4669 // <= then incr-expr must cause var to decrease on each iteration of the 4670 // loop. If test-expr is of form b relational-op var and relational-op is 4671 // > or >= then incr-expr must cause var to increase on each iteration of 4672 // the loop. 4673 llvm::APSInt Result; 4674 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); 4675 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 4676 bool IsConstNeg = 4677 IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); 4678 bool IsConstPos = 4679 IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); 4680 bool IsConstZero = IsConstant && !Result.getBoolValue(); 4681 4682 // != with increment is treated as <; != with decrement is treated as > 4683 if (!TestIsLessOp.hasValue()) 4684 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract); 4685 if (UB && (IsConstZero || 4686 (TestIsLessOp.getValue() ? 4687 (IsConstNeg || (IsUnsigned && Subtract)) : 4688 (IsConstPos || (IsUnsigned && !Subtract))))) { 4689 SemaRef.Diag(NewStep->getExprLoc(), 4690 diag::err_omp_loop_incr_not_compatible) 4691 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange(); 4692 SemaRef.Diag(ConditionLoc, 4693 diag::note_omp_loop_cond_requres_compatible_incr) 4694 << TestIsLessOp.getValue() << ConditionSrcRange; 4695 return true; 4696 } 4697 if (TestIsLessOp.getValue() == Subtract) { 4698 NewStep = 4699 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 4700 .get(); 4701 Subtract = !Subtract; 4702 } 4703 } 4704 4705 Step = NewStep; 4706 SubtractStep = Subtract; 4707 return false; 4708 } 4709 4710 namespace { 4711 /// Checker for the non-rectangular loops. Checks if the initializer or 4712 /// condition expression references loop counter variable. 4713 class LoopCounterRefChecker final 4714 : public ConstStmtVisitor<LoopCounterRefChecker, bool> { 4715 Sema &SemaRef; 4716 DSAStackTy &Stack; 4717 const ValueDecl *CurLCDecl = nullptr; 4718 bool IsInitializer = true; 4719 4720 public: 4721 bool VisitDeclRefExpr(const DeclRefExpr *E) { 4722 const ValueDecl *VD = E->getDecl(); 4723 if (isa<VarDecl>(VD)) { 4724 if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) { 4725 SemaRef.Diag(E->getExprLoc(), 4726 diag::err_omp_stmt_depends_on_loop_counter) 4727 << (IsInitializer ? 0 : 1); 4728 return false; 4729 } 4730 const auto &&Data = Stack.isLoopControlVariable(VD); 4731 return Data.first; 4732 } 4733 return false; 4734 } 4735 bool VisitMemberExpr(const MemberExpr *E) { 4736 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 4737 const ValueDecl *VD = E->getMemberDecl(); 4738 if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) { 4739 SemaRef.Diag(E->getExprLoc(), 4740 diag::err_omp_stmt_depends_on_loop_counter) 4741 << (IsInitializer ? 0 : 1); 4742 return false; 4743 } 4744 const auto &&Data = Stack.isLoopControlVariable(VD); 4745 return Data.first; 4746 } 4747 return false; 4748 } 4749 bool VisitStmt(const Stmt *S) { 4750 for (const Stmt *Child : S->children()) { 4751 if (Child && Visit(Child)) 4752 return true; 4753 } 4754 return false; 4755 } 4756 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack, 4757 const ValueDecl *CurLCDecl, bool IsInitializer) 4758 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl), 4759 IsInitializer(IsInitializer) {} 4760 }; 4761 } // namespace 4762 4763 bool OpenMPIterationSpaceChecker::doesDependOnLoopCounter( 4764 const Stmt *S, bool IsInitializer) const { 4765 // Check for the non-rectangular loops. 4766 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer); 4767 return LoopStmtChecker.Visit(S); 4768 } 4769 4770 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 4771 // Check init-expr for canonical loop form and save loop counter 4772 // variable - #Var and its initialization value - #LB. 4773 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 4774 // var = lb 4775 // integer-type var = lb 4776 // random-access-iterator-type var = lb 4777 // pointer-type var = lb 4778 // 4779 if (!S) { 4780 if (EmitDiags) { 4781 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 4782 } 4783 return true; 4784 } 4785 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 4786 if (!ExprTemp->cleanupsHaveSideEffects()) 4787 S = ExprTemp->getSubExpr(); 4788 4789 InitSrcRange = S->getSourceRange(); 4790 if (Expr *E = dyn_cast<Expr>(S)) 4791 S = E->IgnoreParens(); 4792 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 4793 if (BO->getOpcode() == BO_Assign) { 4794 Expr *LHS = BO->getLHS()->IgnoreParens(); 4795 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 4796 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 4797 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 4798 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 4799 EmitDiags); 4800 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags); 4801 } 4802 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 4803 if (ME->isArrow() && 4804 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 4805 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 4806 EmitDiags); 4807 } 4808 } 4809 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 4810 if (DS->isSingleDecl()) { 4811 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 4812 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 4813 // Accept non-canonical init form here but emit ext. warning. 4814 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 4815 SemaRef.Diag(S->getBeginLoc(), 4816 diag::ext_omp_loop_not_canonical_init) 4817 << S->getSourceRange(); 4818 return setLCDeclAndLB( 4819 Var, 4820 buildDeclRefExpr(SemaRef, Var, 4821 Var->getType().getNonReferenceType(), 4822 DS->getBeginLoc()), 4823 Var->getInit(), EmitDiags); 4824 } 4825 } 4826 } 4827 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 4828 if (CE->getOperator() == OO_Equal) { 4829 Expr *LHS = CE->getArg(0); 4830 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 4831 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 4832 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 4833 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 4834 EmitDiags); 4835 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags); 4836 } 4837 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 4838 if (ME->isArrow() && 4839 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 4840 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 4841 EmitDiags); 4842 } 4843 } 4844 } 4845 4846 if (dependent() || SemaRef.CurContext->isDependentContext()) 4847 return false; 4848 if (EmitDiags) { 4849 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init) 4850 << S->getSourceRange(); 4851 } 4852 return true; 4853 } 4854 4855 /// Ignore parenthesizes, implicit casts, copy constructor and return the 4856 /// variable (which may be the loop variable) if possible. 4857 static const ValueDecl *getInitLCDecl(const Expr *E) { 4858 if (!E) 4859 return nullptr; 4860 E = getExprAsWritten(E); 4861 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 4862 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 4863 if ((Ctor->isCopyOrMoveConstructor() || 4864 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 4865 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 4866 E = CE->getArg(0)->IgnoreParenImpCasts(); 4867 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 4868 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 4869 return getCanonicalDecl(VD); 4870 } 4871 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 4872 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 4873 return getCanonicalDecl(ME->getMemberDecl()); 4874 return nullptr; 4875 } 4876 4877 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 4878 // Check test-expr for canonical form, save upper-bound UB, flags for 4879 // less/greater and for strict/non-strict comparison. 4880 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 4881 // var relational-op b 4882 // b relational-op var 4883 // 4884 if (!S) { 4885 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl; 4886 return true; 4887 } 4888 S = getExprAsWritten(S); 4889 SourceLocation CondLoc = S->getBeginLoc(); 4890 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 4891 if (BO->isRelationalOp()) { 4892 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4893 return setUB(BO->getRHS(), 4894 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 4895 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 4896 BO->getSourceRange(), BO->getOperatorLoc()); 4897 if (getInitLCDecl(BO->getRHS()) == LCDecl) 4898 return setUB(BO->getLHS(), 4899 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 4900 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 4901 BO->getSourceRange(), BO->getOperatorLoc()); 4902 } else if (BO->getOpcode() == BO_NE) 4903 return setUB(getInitLCDecl(BO->getLHS()) == LCDecl ? 4904 BO->getRHS() : BO->getLHS(), 4905 /*LessOp=*/llvm::None, 4906 /*StrictOp=*/true, 4907 BO->getSourceRange(), BO->getOperatorLoc()); 4908 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 4909 if (CE->getNumArgs() == 2) { 4910 auto Op = CE->getOperator(); 4911 switch (Op) { 4912 case OO_Greater: 4913 case OO_GreaterEqual: 4914 case OO_Less: 4915 case OO_LessEqual: 4916 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4917 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 4918 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 4919 CE->getOperatorLoc()); 4920 if (getInitLCDecl(CE->getArg(1)) == LCDecl) 4921 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 4922 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 4923 CE->getOperatorLoc()); 4924 break; 4925 case OO_ExclaimEqual: 4926 return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ? 4927 CE->getArg(1) : CE->getArg(0), 4928 /*LessOp=*/llvm::None, 4929 /*StrictOp=*/true, 4930 CE->getSourceRange(), 4931 CE->getOperatorLoc()); 4932 break; 4933 default: 4934 break; 4935 } 4936 } 4937 } 4938 if (dependent() || SemaRef.CurContext->isDependentContext()) 4939 return false; 4940 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 4941 << S->getSourceRange() << LCDecl; 4942 return true; 4943 } 4944 4945 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 4946 // RHS of canonical loop form increment can be: 4947 // var + incr 4948 // incr + var 4949 // var - incr 4950 // 4951 RHS = RHS->IgnoreParenImpCasts(); 4952 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 4953 if (BO->isAdditiveOp()) { 4954 bool IsAdd = BO->getOpcode() == BO_Add; 4955 if (getInitLCDecl(BO->getLHS()) == LCDecl) 4956 return setStep(BO->getRHS(), !IsAdd); 4957 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 4958 return setStep(BO->getLHS(), /*Subtract=*/false); 4959 } 4960 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 4961 bool IsAdd = CE->getOperator() == OO_Plus; 4962 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 4963 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 4964 return setStep(CE->getArg(1), !IsAdd); 4965 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 4966 return setStep(CE->getArg(0), /*Subtract=*/false); 4967 } 4968 } 4969 if (dependent() || SemaRef.CurContext->isDependentContext()) 4970 return false; 4971 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 4972 << RHS->getSourceRange() << LCDecl; 4973 return true; 4974 } 4975 4976 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 4977 // Check incr-expr for canonical loop form and return true if it 4978 // does not conform. 4979 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 4980 // ++var 4981 // var++ 4982 // --var 4983 // var-- 4984 // var += incr 4985 // var -= incr 4986 // var = var + incr 4987 // var = incr + var 4988 // var = var - incr 4989 // 4990 if (!S) { 4991 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 4992 return true; 4993 } 4994 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 4995 if (!ExprTemp->cleanupsHaveSideEffects()) 4996 S = ExprTemp->getSubExpr(); 4997 4998 IncrementSrcRange = S->getSourceRange(); 4999 S = S->IgnoreParens(); 5000 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 5001 if (UO->isIncrementDecrementOp() && 5002 getInitLCDecl(UO->getSubExpr()) == LCDecl) 5003 return setStep(SemaRef 5004 .ActOnIntegerConstant(UO->getBeginLoc(), 5005 (UO->isDecrementOp() ? -1 : 1)) 5006 .get(), 5007 /*Subtract=*/false); 5008 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 5009 switch (BO->getOpcode()) { 5010 case BO_AddAssign: 5011 case BO_SubAssign: 5012 if (getInitLCDecl(BO->getLHS()) == LCDecl) 5013 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 5014 break; 5015 case BO_Assign: 5016 if (getInitLCDecl(BO->getLHS()) == LCDecl) 5017 return checkAndSetIncRHS(BO->getRHS()); 5018 break; 5019 default: 5020 break; 5021 } 5022 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 5023 switch (CE->getOperator()) { 5024 case OO_PlusPlus: 5025 case OO_MinusMinus: 5026 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 5027 return setStep(SemaRef 5028 .ActOnIntegerConstant( 5029 CE->getBeginLoc(), 5030 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 5031 .get(), 5032 /*Subtract=*/false); 5033 break; 5034 case OO_PlusEqual: 5035 case OO_MinusEqual: 5036 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 5037 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 5038 break; 5039 case OO_Equal: 5040 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 5041 return checkAndSetIncRHS(CE->getArg(1)); 5042 break; 5043 default: 5044 break; 5045 } 5046 } 5047 if (dependent() || SemaRef.CurContext->isDependentContext()) 5048 return false; 5049 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 5050 << S->getSourceRange() << LCDecl; 5051 return true; 5052 } 5053 5054 static ExprResult 5055 tryBuildCapture(Sema &SemaRef, Expr *Capture, 5056 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 5057 if (SemaRef.CurContext->isDependentContext()) 5058 return ExprResult(Capture); 5059 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 5060 return SemaRef.PerformImplicitConversion( 5061 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 5062 /*AllowExplicit=*/true); 5063 auto I = Captures.find(Capture); 5064 if (I != Captures.end()) 5065 return buildCapture(SemaRef, Capture, I->second); 5066 DeclRefExpr *Ref = nullptr; 5067 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 5068 Captures[Capture] = Ref; 5069 return Res; 5070 } 5071 5072 /// Build the expression to calculate the number of iterations. 5073 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 5074 Scope *S, const bool LimitedType, 5075 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 5076 ExprResult Diff; 5077 QualType VarType = LCDecl->getType().getNonReferenceType(); 5078 if (VarType->isIntegerType() || VarType->isPointerType() || 5079 SemaRef.getLangOpts().CPlusPlus) { 5080 // Upper - Lower 5081 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB; 5082 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB; 5083 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 5084 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 5085 if (!Upper || !Lower) 5086 return nullptr; 5087 5088 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 5089 5090 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 5091 // BuildBinOp already emitted error, this one is to point user to upper 5092 // and lower bound, and to tell what is passed to 'operator-'. 5093 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 5094 << Upper->getSourceRange() << Lower->getSourceRange(); 5095 return nullptr; 5096 } 5097 } 5098 5099 if (!Diff.isUsable()) 5100 return nullptr; 5101 5102 // Upper - Lower [- 1] 5103 if (TestIsStrictOp) 5104 Diff = SemaRef.BuildBinOp( 5105 S, DefaultLoc, BO_Sub, Diff.get(), 5106 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 5107 if (!Diff.isUsable()) 5108 return nullptr; 5109 5110 // Upper - Lower [- 1] + Step 5111 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 5112 if (!NewStep.isUsable()) 5113 return nullptr; 5114 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 5115 if (!Diff.isUsable()) 5116 return nullptr; 5117 5118 // Parentheses (for dumping/debugging purposes only). 5119 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 5120 if (!Diff.isUsable()) 5121 return nullptr; 5122 5123 // (Upper - Lower [- 1] + Step) / Step 5124 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 5125 if (!Diff.isUsable()) 5126 return nullptr; 5127 5128 // OpenMP runtime requires 32-bit or 64-bit loop variables. 5129 QualType Type = Diff.get()->getType(); 5130 ASTContext &C = SemaRef.Context; 5131 bool UseVarType = VarType->hasIntegerRepresentation() && 5132 C.getTypeSize(Type) > C.getTypeSize(VarType); 5133 if (!Type->isIntegerType() || UseVarType) { 5134 unsigned NewSize = 5135 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 5136 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 5137 : Type->hasSignedIntegerRepresentation(); 5138 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 5139 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 5140 Diff = SemaRef.PerformImplicitConversion( 5141 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 5142 if (!Diff.isUsable()) 5143 return nullptr; 5144 } 5145 } 5146 if (LimitedType) { 5147 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 5148 if (NewSize != C.getTypeSize(Type)) { 5149 if (NewSize < C.getTypeSize(Type)) { 5150 assert(NewSize == 64 && "incorrect loop var size"); 5151 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 5152 << InitSrcRange << ConditionSrcRange; 5153 } 5154 QualType NewType = C.getIntTypeForBitwidth( 5155 NewSize, Type->hasSignedIntegerRepresentation() || 5156 C.getTypeSize(Type) < NewSize); 5157 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 5158 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 5159 Sema::AA_Converting, true); 5160 if (!Diff.isUsable()) 5161 return nullptr; 5162 } 5163 } 5164 } 5165 5166 return Diff.get(); 5167 } 5168 5169 Expr *OpenMPIterationSpaceChecker::buildPreCond( 5170 Scope *S, Expr *Cond, 5171 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 5172 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 5173 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 5174 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 5175 5176 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 5177 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 5178 if (!NewLB.isUsable() || !NewUB.isUsable()) 5179 return nullptr; 5180 5181 ExprResult CondExpr = 5182 SemaRef.BuildBinOp(S, DefaultLoc, 5183 TestIsLessOp.getValue() ? 5184 (TestIsStrictOp ? BO_LT : BO_LE) : 5185 (TestIsStrictOp ? BO_GT : BO_GE), 5186 NewLB.get(), NewUB.get()); 5187 if (CondExpr.isUsable()) { 5188 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 5189 SemaRef.Context.BoolTy)) 5190 CondExpr = SemaRef.PerformImplicitConversion( 5191 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 5192 /*AllowExplicit=*/true); 5193 } 5194 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 5195 // Otherwise use original loop condition and evaluate it in runtime. 5196 return CondExpr.isUsable() ? CondExpr.get() : Cond; 5197 } 5198 5199 /// Build reference expression to the counter be used for codegen. 5200 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 5201 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 5202 DSAStackTy &DSA) const { 5203 auto *VD = dyn_cast<VarDecl>(LCDecl); 5204 if (!VD) { 5205 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 5206 DeclRefExpr *Ref = buildDeclRefExpr( 5207 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 5208 const DSAStackTy::DSAVarData Data = 5209 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 5210 // If the loop control decl is explicitly marked as private, do not mark it 5211 // as captured again. 5212 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 5213 Captures.insert(std::make_pair(LCRef, Ref)); 5214 return Ref; 5215 } 5216 return cast<DeclRefExpr>(LCRef); 5217 } 5218 5219 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 5220 if (LCDecl && !LCDecl->isInvalidDecl()) { 5221 QualType Type = LCDecl->getType().getNonReferenceType(); 5222 VarDecl *PrivateVar = buildVarDecl( 5223 SemaRef, DefaultLoc, Type, LCDecl->getName(), 5224 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 5225 isa<VarDecl>(LCDecl) 5226 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 5227 : nullptr); 5228 if (PrivateVar->isInvalidDecl()) 5229 return nullptr; 5230 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 5231 } 5232 return nullptr; 5233 } 5234 5235 /// Build initialization of the counter to be used for codegen. 5236 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 5237 5238 /// Build step of the counter be used for codegen. 5239 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 5240 5241 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData( 5242 Scope *S, Expr *Counter, 5243 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc, 5244 Expr *Inc, OverloadedOperatorKind OOK) { 5245 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get(); 5246 if (!Cnt) 5247 return nullptr; 5248 if (Inc) { 5249 assert((OOK == OO_Plus || OOK == OO_Minus) && 5250 "Expected only + or - operations for depend clauses."); 5251 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub; 5252 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get(); 5253 if (!Cnt) 5254 return nullptr; 5255 } 5256 ExprResult Diff; 5257 QualType VarType = LCDecl->getType().getNonReferenceType(); 5258 if (VarType->isIntegerType() || VarType->isPointerType() || 5259 SemaRef.getLangOpts().CPlusPlus) { 5260 // Upper - Lower 5261 Expr *Upper = TestIsLessOp.getValue() 5262 ? Cnt 5263 : tryBuildCapture(SemaRef, UB, Captures).get(); 5264 Expr *Lower = TestIsLessOp.getValue() 5265 ? tryBuildCapture(SemaRef, LB, Captures).get() 5266 : Cnt; 5267 if (!Upper || !Lower) 5268 return nullptr; 5269 5270 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 5271 5272 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 5273 // BuildBinOp already emitted error, this one is to point user to upper 5274 // and lower bound, and to tell what is passed to 'operator-'. 5275 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 5276 << Upper->getSourceRange() << Lower->getSourceRange(); 5277 return nullptr; 5278 } 5279 } 5280 5281 if (!Diff.isUsable()) 5282 return nullptr; 5283 5284 // Parentheses (for dumping/debugging purposes only). 5285 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 5286 if (!Diff.isUsable()) 5287 return nullptr; 5288 5289 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 5290 if (!NewStep.isUsable()) 5291 return nullptr; 5292 // (Upper - Lower) / Step 5293 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 5294 if (!Diff.isUsable()) 5295 return nullptr; 5296 5297 return Diff.get(); 5298 } 5299 5300 /// Iteration space of a single for loop. 5301 struct LoopIterationSpace final { 5302 /// True if the condition operator is the strict compare operator (<, > or 5303 /// !=). 5304 bool IsStrictCompare = false; 5305 /// Condition of the loop. 5306 Expr *PreCond = nullptr; 5307 /// This expression calculates the number of iterations in the loop. 5308 /// It is always possible to calculate it before starting the loop. 5309 Expr *NumIterations = nullptr; 5310 /// The loop counter variable. 5311 Expr *CounterVar = nullptr; 5312 /// Private loop counter variable. 5313 Expr *PrivateCounterVar = nullptr; 5314 /// This is initializer for the initial value of #CounterVar. 5315 Expr *CounterInit = nullptr; 5316 /// This is step for the #CounterVar used to generate its update: 5317 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 5318 Expr *CounterStep = nullptr; 5319 /// Should step be subtracted? 5320 bool Subtract = false; 5321 /// Source range of the loop init. 5322 SourceRange InitSrcRange; 5323 /// Source range of the loop condition. 5324 SourceRange CondSrcRange; 5325 /// Source range of the loop increment. 5326 SourceRange IncSrcRange; 5327 }; 5328 5329 } // namespace 5330 5331 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 5332 assert(getLangOpts().OpenMP && "OpenMP is not active."); 5333 assert(Init && "Expected loop in canonical form."); 5334 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 5335 if (AssociatedLoops > 0 && 5336 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 5337 DSAStack->loopStart(); 5338 OpenMPIterationSpaceChecker ISC(*this, *DSAStack, ForLoc); 5339 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 5340 if (ValueDecl *D = ISC.getLoopDecl()) { 5341 auto *VD = dyn_cast<VarDecl>(D); 5342 if (!VD) { 5343 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 5344 VD = Private; 5345 } else { 5346 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 5347 /*WithInit=*/false); 5348 VD = cast<VarDecl>(Ref->getDecl()); 5349 } 5350 } 5351 DSAStack->addLoopControlVariable(D, VD); 5352 const Decl *LD = DSAStack->getPossiblyLoopCunter(); 5353 if (LD != D->getCanonicalDecl()) { 5354 DSAStack->resetPossibleLoopCounter(); 5355 if (auto *Var = dyn_cast_or_null<VarDecl>(LD)) 5356 MarkDeclarationsReferencedInExpr( 5357 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var), 5358 Var->getType().getNonLValueExprType(Context), 5359 ForLoc, /*RefersToCapture=*/true)); 5360 } 5361 } 5362 } 5363 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 5364 } 5365 } 5366 5367 /// Called on a for stmt to check and extract its iteration space 5368 /// for further processing (such as collapsing). 5369 static bool checkOpenMPIterationSpace( 5370 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 5371 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 5372 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr, 5373 Expr *OrderedLoopCountExpr, 5374 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 5375 LoopIterationSpace &ResultIterSpace, 5376 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 5377 // OpenMP [2.6, Canonical Loop Form] 5378 // for (init-expr; test-expr; incr-expr) structured-block 5379 auto *For = dyn_cast_or_null<ForStmt>(S); 5380 if (!For) { 5381 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for) 5382 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 5383 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount 5384 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 5385 if (TotalNestedLoopCount > 1) { 5386 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 5387 SemaRef.Diag(DSA.getConstructLoc(), 5388 diag::note_omp_collapse_ordered_expr) 5389 << 2 << CollapseLoopCountExpr->getSourceRange() 5390 << OrderedLoopCountExpr->getSourceRange(); 5391 else if (CollapseLoopCountExpr) 5392 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 5393 diag::note_omp_collapse_ordered_expr) 5394 << 0 << CollapseLoopCountExpr->getSourceRange(); 5395 else 5396 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 5397 diag::note_omp_collapse_ordered_expr) 5398 << 1 << OrderedLoopCountExpr->getSourceRange(); 5399 } 5400 return true; 5401 } 5402 assert(For->getBody()); 5403 5404 OpenMPIterationSpaceChecker ISC(SemaRef, DSA, For->getForLoc()); 5405 5406 // Check init. 5407 Stmt *Init = For->getInit(); 5408 if (ISC.checkAndSetInit(Init)) 5409 return true; 5410 5411 bool HasErrors = false; 5412 5413 // Check loop variable's type. 5414 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 5415 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 5416 5417 // OpenMP [2.6, Canonical Loop Form] 5418 // Var is one of the following: 5419 // A variable of signed or unsigned integer type. 5420 // For C++, a variable of a random access iterator type. 5421 // For C, a variable of a pointer type. 5422 QualType VarType = LCDecl->getType().getNonReferenceType(); 5423 if (!VarType->isDependentType() && !VarType->isIntegerType() && 5424 !VarType->isPointerType() && 5425 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 5426 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type) 5427 << SemaRef.getLangOpts().CPlusPlus; 5428 HasErrors = true; 5429 } 5430 5431 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 5432 // a Construct 5433 // The loop iteration variable(s) in the associated for-loop(s) of a for or 5434 // parallel for construct is (are) private. 5435 // The loop iteration variable in the associated for-loop of a simd 5436 // construct with just one associated for-loop is linear with a 5437 // constant-linear-step that is the increment of the associated for-loop. 5438 // Exclude loop var from the list of variables with implicitly defined data 5439 // sharing attributes. 5440 VarsWithImplicitDSA.erase(LCDecl); 5441 5442 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 5443 // in a Construct, C/C++]. 5444 // The loop iteration variable in the associated for-loop of a simd 5445 // construct with just one associated for-loop may be listed in a linear 5446 // clause with a constant-linear-step that is the increment of the 5447 // associated for-loop. 5448 // The loop iteration variable(s) in the associated for-loop(s) of a for or 5449 // parallel for construct may be listed in a private or lastprivate clause. 5450 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false); 5451 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is 5452 // declared in the loop and it is predetermined as a private. 5453 OpenMPClauseKind PredeterminedCKind = 5454 isOpenMPSimdDirective(DKind) 5455 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate) 5456 : OMPC_private; 5457 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 5458 DVar.CKind != PredeterminedCKind) || 5459 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 5460 isOpenMPDistributeDirective(DKind)) && 5461 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 5462 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 5463 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 5464 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa) 5465 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind) 5466 << getOpenMPClauseName(PredeterminedCKind); 5467 if (DVar.RefExpr == nullptr) 5468 DVar.CKind = PredeterminedCKind; 5469 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true); 5470 HasErrors = true; 5471 } else if (LoopDeclRefExpr != nullptr) { 5472 // Make the loop iteration variable private (for worksharing constructs), 5473 // linear (for simd directives with the only one associated loop) or 5474 // lastprivate (for simd directives with several collapsed or ordered 5475 // loops). 5476 if (DVar.CKind == OMPC_unknown) 5477 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind); 5478 } 5479 5480 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 5481 5482 // Check test-expr. 5483 HasErrors |= ISC.checkAndSetCond(For->getCond()); 5484 5485 // Check incr-expr. 5486 HasErrors |= ISC.checkAndSetInc(For->getInc()); 5487 } 5488 5489 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 5490 return HasErrors; 5491 5492 // Build the loop's iteration space representation. 5493 ResultIterSpace.PreCond = 5494 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures); 5495 ResultIterSpace.NumIterations = ISC.buildNumIterations( 5496 DSA.getCurScope(), 5497 (isOpenMPWorksharingDirective(DKind) || 5498 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)), 5499 Captures); 5500 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA); 5501 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar(); 5502 ResultIterSpace.CounterInit = ISC.buildCounterInit(); 5503 ResultIterSpace.CounterStep = ISC.buildCounterStep(); 5504 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange(); 5505 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange(); 5506 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange(); 5507 ResultIterSpace.Subtract = ISC.shouldSubtractStep(); 5508 ResultIterSpace.IsStrictCompare = ISC.isStrictTestOp(); 5509 5510 HasErrors |= (ResultIterSpace.PreCond == nullptr || 5511 ResultIterSpace.NumIterations == nullptr || 5512 ResultIterSpace.CounterVar == nullptr || 5513 ResultIterSpace.PrivateCounterVar == nullptr || 5514 ResultIterSpace.CounterInit == nullptr || 5515 ResultIterSpace.CounterStep == nullptr); 5516 if (!HasErrors && DSA.isOrderedRegion()) { 5517 if (DSA.getOrderedRegionParam().second->getNumForLoops()) { 5518 if (CurrentNestedLoopCount < 5519 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) { 5520 DSA.getOrderedRegionParam().second->setLoopNumIterations( 5521 CurrentNestedLoopCount, ResultIterSpace.NumIterations); 5522 DSA.getOrderedRegionParam().second->setLoopCounter( 5523 CurrentNestedLoopCount, ResultIterSpace.CounterVar); 5524 } 5525 } 5526 for (auto &Pair : DSA.getDoacrossDependClauses()) { 5527 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) { 5528 // Erroneous case - clause has some problems. 5529 continue; 5530 } 5531 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink && 5532 Pair.second.size() <= CurrentNestedLoopCount) { 5533 // Erroneous case - clause has some problems. 5534 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr); 5535 continue; 5536 } 5537 Expr *CntValue; 5538 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 5539 CntValue = ISC.buildOrderedLoopData( 5540 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures, 5541 Pair.first->getDependencyLoc()); 5542 else 5543 CntValue = ISC.buildOrderedLoopData( 5544 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures, 5545 Pair.first->getDependencyLoc(), 5546 Pair.second[CurrentNestedLoopCount].first, 5547 Pair.second[CurrentNestedLoopCount].second); 5548 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue); 5549 } 5550 } 5551 5552 return HasErrors; 5553 } 5554 5555 /// Build 'VarRef = Start. 5556 static ExprResult 5557 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 5558 ExprResult Start, 5559 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 5560 // Build 'VarRef = Start. 5561 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures); 5562 if (!NewStart.isUsable()) 5563 return ExprError(); 5564 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 5565 VarRef.get()->getType())) { 5566 NewStart = SemaRef.PerformImplicitConversion( 5567 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 5568 /*AllowExplicit=*/true); 5569 if (!NewStart.isUsable()) 5570 return ExprError(); 5571 } 5572 5573 ExprResult Init = 5574 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 5575 return Init; 5576 } 5577 5578 /// Build 'VarRef = Start + Iter * Step'. 5579 static ExprResult buildCounterUpdate( 5580 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 5581 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 5582 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 5583 // Add parentheses (for debugging purposes only). 5584 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 5585 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 5586 !Step.isUsable()) 5587 return ExprError(); 5588 5589 ExprResult NewStep = Step; 5590 if (Captures) 5591 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 5592 if (NewStep.isInvalid()) 5593 return ExprError(); 5594 ExprResult Update = 5595 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 5596 if (!Update.isUsable()) 5597 return ExprError(); 5598 5599 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 5600 // 'VarRef = Start (+|-) Iter * Step'. 5601 ExprResult NewStart = Start; 5602 if (Captures) 5603 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 5604 if (NewStart.isInvalid()) 5605 return ExprError(); 5606 5607 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 5608 ExprResult SavedUpdate = Update; 5609 ExprResult UpdateVal; 5610 if (VarRef.get()->getType()->isOverloadableType() || 5611 NewStart.get()->getType()->isOverloadableType() || 5612 Update.get()->getType()->isOverloadableType()) { 5613 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 5614 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 5615 Update = 5616 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 5617 if (Update.isUsable()) { 5618 UpdateVal = 5619 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 5620 VarRef.get(), SavedUpdate.get()); 5621 if (UpdateVal.isUsable()) { 5622 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 5623 UpdateVal.get()); 5624 } 5625 } 5626 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 5627 } 5628 5629 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 5630 if (!Update.isUsable() || !UpdateVal.isUsable()) { 5631 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 5632 NewStart.get(), SavedUpdate.get()); 5633 if (!Update.isUsable()) 5634 return ExprError(); 5635 5636 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 5637 VarRef.get()->getType())) { 5638 Update = SemaRef.PerformImplicitConversion( 5639 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 5640 if (!Update.isUsable()) 5641 return ExprError(); 5642 } 5643 5644 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 5645 } 5646 return Update; 5647 } 5648 5649 /// Convert integer expression \a E to make it have at least \a Bits 5650 /// bits. 5651 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 5652 if (E == nullptr) 5653 return ExprError(); 5654 ASTContext &C = SemaRef.Context; 5655 QualType OldType = E->getType(); 5656 unsigned HasBits = C.getTypeSize(OldType); 5657 if (HasBits >= Bits) 5658 return ExprResult(E); 5659 // OK to convert to signed, because new type has more bits than old. 5660 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 5661 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 5662 true); 5663 } 5664 5665 /// Check if the given expression \a E is a constant integer that fits 5666 /// into \a Bits bits. 5667 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 5668 if (E == nullptr) 5669 return false; 5670 llvm::APSInt Result; 5671 if (E->isIntegerConstantExpr(Result, SemaRef.Context)) 5672 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); 5673 return false; 5674 } 5675 5676 /// Build preinits statement for the given declarations. 5677 static Stmt *buildPreInits(ASTContext &Context, 5678 MutableArrayRef<Decl *> PreInits) { 5679 if (!PreInits.empty()) { 5680 return new (Context) DeclStmt( 5681 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 5682 SourceLocation(), SourceLocation()); 5683 } 5684 return nullptr; 5685 } 5686 5687 /// Build preinits statement for the given declarations. 5688 static Stmt * 5689 buildPreInits(ASTContext &Context, 5690 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 5691 if (!Captures.empty()) { 5692 SmallVector<Decl *, 16> PreInits; 5693 for (const auto &Pair : Captures) 5694 PreInits.push_back(Pair.second->getDecl()); 5695 return buildPreInits(Context, PreInits); 5696 } 5697 return nullptr; 5698 } 5699 5700 /// Build postupdate expression for the given list of postupdates expressions. 5701 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 5702 Expr *PostUpdate = nullptr; 5703 if (!PostUpdates.empty()) { 5704 for (Expr *E : PostUpdates) { 5705 Expr *ConvE = S.BuildCStyleCastExpr( 5706 E->getExprLoc(), 5707 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 5708 E->getExprLoc(), E) 5709 .get(); 5710 PostUpdate = PostUpdate 5711 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 5712 PostUpdate, ConvE) 5713 .get() 5714 : ConvE; 5715 } 5716 } 5717 return PostUpdate; 5718 } 5719 5720 /// Called on a for stmt to check itself and nested loops (if any). 5721 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 5722 /// number of collapsed loops otherwise. 5723 static unsigned 5724 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 5725 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 5726 DSAStackTy &DSA, 5727 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 5728 OMPLoopDirective::HelperExprs &Built) { 5729 unsigned NestedLoopCount = 1; 5730 if (CollapseLoopCountExpr) { 5731 // Found 'collapse' clause - calculate collapse number. 5732 Expr::EvalResult Result; 5733 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) 5734 NestedLoopCount = Result.Val.getInt().getLimitedValue(); 5735 } 5736 unsigned OrderedLoopCount = 1; 5737 if (OrderedLoopCountExpr) { 5738 // Found 'ordered' clause - calculate collapse number. 5739 Expr::EvalResult EVResult; 5740 if (OrderedLoopCountExpr->EvaluateAsInt(EVResult, SemaRef.getASTContext())) { 5741 llvm::APSInt Result = EVResult.Val.getInt(); 5742 if (Result.getLimitedValue() < NestedLoopCount) { 5743 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 5744 diag::err_omp_wrong_ordered_loop_count) 5745 << OrderedLoopCountExpr->getSourceRange(); 5746 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 5747 diag::note_collapse_loop_count) 5748 << CollapseLoopCountExpr->getSourceRange(); 5749 } 5750 OrderedLoopCount = Result.getLimitedValue(); 5751 } 5752 } 5753 // This is helper routine for loop directives (e.g., 'for', 'simd', 5754 // 'for simd', etc.). 5755 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 5756 SmallVector<LoopIterationSpace, 4> IterSpaces( 5757 std::max(OrderedLoopCount, NestedLoopCount)); 5758 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); 5759 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 5760 if (checkOpenMPIterationSpace( 5761 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 5762 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 5763 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt], 5764 Captures)) 5765 return 0; 5766 // Move on to the next nested for loop, or to the loop body. 5767 // OpenMP [2.8.1, simd construct, Restrictions] 5768 // All loops associated with the construct must be perfectly nested; that 5769 // is, there must be no intervening code nor any OpenMP directive between 5770 // any two loops. 5771 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); 5772 } 5773 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) { 5774 if (checkOpenMPIterationSpace( 5775 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 5776 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 5777 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt], 5778 Captures)) 5779 return 0; 5780 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) { 5781 // Handle initialization of captured loop iterator variables. 5782 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar); 5783 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) { 5784 Captures[DRE] = DRE; 5785 } 5786 } 5787 // Move on to the next nested for loop, or to the loop body. 5788 // OpenMP [2.8.1, simd construct, Restrictions] 5789 // All loops associated with the construct must be perfectly nested; that 5790 // is, there must be no intervening code nor any OpenMP directive between 5791 // any two loops. 5792 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); 5793 } 5794 5795 Built.clear(/* size */ NestedLoopCount); 5796 5797 if (SemaRef.CurContext->isDependentContext()) 5798 return NestedLoopCount; 5799 5800 // An example of what is generated for the following code: 5801 // 5802 // #pragma omp simd collapse(2) ordered(2) 5803 // for (i = 0; i < NI; ++i) 5804 // for (k = 0; k < NK; ++k) 5805 // for (j = J0; j < NJ; j+=2) { 5806 // <loop body> 5807 // } 5808 // 5809 // We generate the code below. 5810 // Note: the loop body may be outlined in CodeGen. 5811 // Note: some counters may be C++ classes, operator- is used to find number of 5812 // iterations and operator+= to calculate counter value. 5813 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 5814 // or i64 is currently supported). 5815 // 5816 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 5817 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 5818 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 5819 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 5820 // // similar updates for vars in clauses (e.g. 'linear') 5821 // <loop body (using local i and j)> 5822 // } 5823 // i = NI; // assign final values of counters 5824 // j = NJ; 5825 // 5826 5827 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 5828 // the iteration counts of the collapsed for loops. 5829 // Precondition tests if there is at least one iteration (all conditions are 5830 // true). 5831 auto PreCond = ExprResult(IterSpaces[0].PreCond); 5832 Expr *N0 = IterSpaces[0].NumIterations; 5833 ExprResult LastIteration32 = 5834 widenIterationCount(/*Bits=*/32, 5835 SemaRef 5836 .PerformImplicitConversion( 5837 N0->IgnoreImpCasts(), N0->getType(), 5838 Sema::AA_Converting, /*AllowExplicit=*/true) 5839 .get(), 5840 SemaRef); 5841 ExprResult LastIteration64 = widenIterationCount( 5842 /*Bits=*/64, 5843 SemaRef 5844 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 5845 Sema::AA_Converting, 5846 /*AllowExplicit=*/true) 5847 .get(), 5848 SemaRef); 5849 5850 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 5851 return NestedLoopCount; 5852 5853 ASTContext &C = SemaRef.Context; 5854 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 5855 5856 Scope *CurScope = DSA.getCurScope(); 5857 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 5858 if (PreCond.isUsable()) { 5859 PreCond = 5860 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 5861 PreCond.get(), IterSpaces[Cnt].PreCond); 5862 } 5863 Expr *N = IterSpaces[Cnt].NumIterations; 5864 SourceLocation Loc = N->getExprLoc(); 5865 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 5866 if (LastIteration32.isUsable()) 5867 LastIteration32 = SemaRef.BuildBinOp( 5868 CurScope, Loc, BO_Mul, LastIteration32.get(), 5869 SemaRef 5870 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 5871 Sema::AA_Converting, 5872 /*AllowExplicit=*/true) 5873 .get()); 5874 if (LastIteration64.isUsable()) 5875 LastIteration64 = SemaRef.BuildBinOp( 5876 CurScope, Loc, BO_Mul, LastIteration64.get(), 5877 SemaRef 5878 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 5879 Sema::AA_Converting, 5880 /*AllowExplicit=*/true) 5881 .get()); 5882 } 5883 5884 // Choose either the 32-bit or 64-bit version. 5885 ExprResult LastIteration = LastIteration64; 5886 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse || 5887 (LastIteration32.isUsable() && 5888 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 5889 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 5890 fitsInto( 5891 /*Bits=*/32, 5892 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 5893 LastIteration64.get(), SemaRef)))) 5894 LastIteration = LastIteration32; 5895 QualType VType = LastIteration.get()->getType(); 5896 QualType RealVType = VType; 5897 QualType StrideVType = VType; 5898 if (isOpenMPTaskLoopDirective(DKind)) { 5899 VType = 5900 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 5901 StrideVType = 5902 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 5903 } 5904 5905 if (!LastIteration.isUsable()) 5906 return 0; 5907 5908 // Save the number of iterations. 5909 ExprResult NumIterations = LastIteration; 5910 { 5911 LastIteration = SemaRef.BuildBinOp( 5912 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 5913 LastIteration.get(), 5914 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 5915 if (!LastIteration.isUsable()) 5916 return 0; 5917 } 5918 5919 // Calculate the last iteration number beforehand instead of doing this on 5920 // each iteration. Do not do this if the number of iterations may be kfold-ed. 5921 llvm::APSInt Result; 5922 bool IsConstant = 5923 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); 5924 ExprResult CalcLastIteration; 5925 if (!IsConstant) { 5926 ExprResult SaveRef = 5927 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 5928 LastIteration = SaveRef; 5929 5930 // Prepare SaveRef + 1. 5931 NumIterations = SemaRef.BuildBinOp( 5932 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 5933 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 5934 if (!NumIterations.isUsable()) 5935 return 0; 5936 } 5937 5938 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 5939 5940 // Build variables passed into runtime, necessary for worksharing directives. 5941 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 5942 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 5943 isOpenMPDistributeDirective(DKind)) { 5944 // Lower bound variable, initialized with zero. 5945 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 5946 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 5947 SemaRef.AddInitializerToDecl(LBDecl, 5948 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 5949 /*DirectInit*/ false); 5950 5951 // Upper bound variable, initialized with last iteration number. 5952 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 5953 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 5954 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 5955 /*DirectInit*/ false); 5956 5957 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 5958 // This will be used to implement clause 'lastprivate'. 5959 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 5960 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 5961 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 5962 SemaRef.AddInitializerToDecl(ILDecl, 5963 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 5964 /*DirectInit*/ false); 5965 5966 // Stride variable returned by runtime (we initialize it to 1 by default). 5967 VarDecl *STDecl = 5968 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 5969 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 5970 SemaRef.AddInitializerToDecl(STDecl, 5971 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 5972 /*DirectInit*/ false); 5973 5974 // Build expression: UB = min(UB, LastIteration) 5975 // It is necessary for CodeGen of directives with static scheduling. 5976 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 5977 UB.get(), LastIteration.get()); 5978 ExprResult CondOp = SemaRef.ActOnConditionalOp( 5979 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 5980 LastIteration.get(), UB.get()); 5981 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 5982 CondOp.get()); 5983 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false); 5984 5985 // If we have a combined directive that combines 'distribute', 'for' or 5986 // 'simd' we need to be able to access the bounds of the schedule of the 5987 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 5988 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 5989 if (isOpenMPLoopBoundSharingDirective(DKind)) { 5990 // Lower bound variable, initialized with zero. 5991 VarDecl *CombLBDecl = 5992 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 5993 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 5994 SemaRef.AddInitializerToDecl( 5995 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 5996 /*DirectInit*/ false); 5997 5998 // Upper bound variable, initialized with last iteration number. 5999 VarDecl *CombUBDecl = 6000 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 6001 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 6002 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 6003 /*DirectInit*/ false); 6004 6005 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 6006 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 6007 ExprResult CombCondOp = 6008 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 6009 LastIteration.get(), CombUB.get()); 6010 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 6011 CombCondOp.get()); 6012 CombEUB = 6013 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false); 6014 6015 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 6016 // We expect to have at least 2 more parameters than the 'parallel' 6017 // directive does - the lower and upper bounds of the previous schedule. 6018 assert(CD->getNumParams() >= 4 && 6019 "Unexpected number of parameters in loop combined directive"); 6020 6021 // Set the proper type for the bounds given what we learned from the 6022 // enclosed loops. 6023 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 6024 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 6025 6026 // Previous lower and upper bounds are obtained from the region 6027 // parameters. 6028 PrevLB = 6029 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 6030 PrevUB = 6031 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 6032 } 6033 } 6034 6035 // Build the iteration variable and its initialization before loop. 6036 ExprResult IV; 6037 ExprResult Init, CombInit; 6038 { 6039 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 6040 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 6041 Expr *RHS = 6042 (isOpenMPWorksharingDirective(DKind) || 6043 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 6044 ? LB.get() 6045 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 6046 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 6047 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false); 6048 6049 if (isOpenMPLoopBoundSharingDirective(DKind)) { 6050 Expr *CombRHS = 6051 (isOpenMPWorksharingDirective(DKind) || 6052 isOpenMPTaskLoopDirective(DKind) || 6053 isOpenMPDistributeDirective(DKind)) 6054 ? CombLB.get() 6055 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 6056 CombInit = 6057 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 6058 CombInit = 6059 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false); 6060 } 6061 } 6062 6063 bool UseStrictCompare = 6064 RealVType->hasUnsignedIntegerRepresentation() && 6065 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) { 6066 return LIS.IsStrictCompare; 6067 }); 6068 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for 6069 // unsigned IV)) for worksharing loops. 6070 SourceLocation CondLoc = AStmt->getBeginLoc(); 6071 Expr *BoundUB = UB.get(); 6072 if (UseStrictCompare) { 6073 BoundUB = 6074 SemaRef 6075 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB, 6076 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 6077 .get(); 6078 BoundUB = 6079 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get(); 6080 } 6081 ExprResult Cond = 6082 (isOpenMPWorksharingDirective(DKind) || 6083 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 6084 ? SemaRef.BuildBinOp(CurScope, CondLoc, 6085 UseStrictCompare ? BO_LT : BO_LE, IV.get(), 6086 BoundUB) 6087 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 6088 NumIterations.get()); 6089 ExprResult CombDistCond; 6090 if (isOpenMPLoopBoundSharingDirective(DKind)) { 6091 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 6092 NumIterations.get()); 6093 } 6094 6095 ExprResult CombCond; 6096 if (isOpenMPLoopBoundSharingDirective(DKind)) { 6097 Expr *BoundCombUB = CombUB.get(); 6098 if (UseStrictCompare) { 6099 BoundCombUB = 6100 SemaRef 6101 .BuildBinOp( 6102 CurScope, CondLoc, BO_Add, BoundCombUB, 6103 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 6104 .get(); 6105 BoundCombUB = 6106 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false) 6107 .get(); 6108 } 6109 CombCond = 6110 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 6111 IV.get(), BoundCombUB); 6112 } 6113 // Loop increment (IV = IV + 1) 6114 SourceLocation IncLoc = AStmt->getBeginLoc(); 6115 ExprResult Inc = 6116 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 6117 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 6118 if (!Inc.isUsable()) 6119 return 0; 6120 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 6121 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false); 6122 if (!Inc.isUsable()) 6123 return 0; 6124 6125 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 6126 // Used for directives with static scheduling. 6127 // In combined construct, add combined version that use CombLB and CombUB 6128 // base variables for the update 6129 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 6130 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 6131 isOpenMPDistributeDirective(DKind)) { 6132 // LB + ST 6133 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 6134 if (!NextLB.isUsable()) 6135 return 0; 6136 // LB = LB + ST 6137 NextLB = 6138 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 6139 NextLB = 6140 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false); 6141 if (!NextLB.isUsable()) 6142 return 0; 6143 // UB + ST 6144 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 6145 if (!NextUB.isUsable()) 6146 return 0; 6147 // UB = UB + ST 6148 NextUB = 6149 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 6150 NextUB = 6151 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false); 6152 if (!NextUB.isUsable()) 6153 return 0; 6154 if (isOpenMPLoopBoundSharingDirective(DKind)) { 6155 CombNextLB = 6156 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 6157 if (!NextLB.isUsable()) 6158 return 0; 6159 // LB = LB + ST 6160 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 6161 CombNextLB.get()); 6162 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(), 6163 /*DiscardedValue*/ false); 6164 if (!CombNextLB.isUsable()) 6165 return 0; 6166 // UB + ST 6167 CombNextUB = 6168 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 6169 if (!CombNextUB.isUsable()) 6170 return 0; 6171 // UB = UB + ST 6172 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 6173 CombNextUB.get()); 6174 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(), 6175 /*DiscardedValue*/ false); 6176 if (!CombNextUB.isUsable()) 6177 return 0; 6178 } 6179 } 6180 6181 // Create increment expression for distribute loop when combined in a same 6182 // directive with for as IV = IV + ST; ensure upper bound expression based 6183 // on PrevUB instead of NumIterations - used to implement 'for' when found 6184 // in combination with 'distribute', like in 'distribute parallel for' 6185 SourceLocation DistIncLoc = AStmt->getBeginLoc(); 6186 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond; 6187 if (isOpenMPLoopBoundSharingDirective(DKind)) { 6188 DistCond = SemaRef.BuildBinOp( 6189 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB); 6190 assert(DistCond.isUsable() && "distribute cond expr was not built"); 6191 6192 DistInc = 6193 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 6194 assert(DistInc.isUsable() && "distribute inc expr was not built"); 6195 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 6196 DistInc.get()); 6197 DistInc = 6198 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false); 6199 assert(DistInc.isUsable() && "distribute inc expr was not built"); 6200 6201 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 6202 // construct 6203 SourceLocation DistEUBLoc = AStmt->getBeginLoc(); 6204 ExprResult IsUBGreater = 6205 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get()); 6206 ExprResult CondOp = SemaRef.ActOnConditionalOp( 6207 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get()); 6208 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 6209 CondOp.get()); 6210 PrevEUB = 6211 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false); 6212 6213 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in 6214 // parallel for is in combination with a distribute directive with 6215 // schedule(static, 1) 6216 Expr *BoundPrevUB = PrevUB.get(); 6217 if (UseStrictCompare) { 6218 BoundPrevUB = 6219 SemaRef 6220 .BuildBinOp( 6221 CurScope, CondLoc, BO_Add, BoundPrevUB, 6222 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 6223 .get(); 6224 BoundPrevUB = 6225 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false) 6226 .get(); 6227 } 6228 ParForInDistCond = 6229 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 6230 IV.get(), BoundPrevUB); 6231 } 6232 6233 // Build updates and final values of the loop counters. 6234 bool HasErrors = false; 6235 Built.Counters.resize(NestedLoopCount); 6236 Built.Inits.resize(NestedLoopCount); 6237 Built.Updates.resize(NestedLoopCount); 6238 Built.Finals.resize(NestedLoopCount); 6239 { 6240 // We implement the following algorithm for obtaining the 6241 // original loop iteration variable values based on the 6242 // value of the collapsed loop iteration variable IV. 6243 // 6244 // Let n+1 be the number of collapsed loops in the nest. 6245 // Iteration variables (I0, I1, .... In) 6246 // Iteration counts (N0, N1, ... Nn) 6247 // 6248 // Acc = IV; 6249 // 6250 // To compute Ik for loop k, 0 <= k <= n, generate: 6251 // Prod = N(k+1) * N(k+2) * ... * Nn; 6252 // Ik = Acc / Prod; 6253 // Acc -= Ik * Prod; 6254 // 6255 ExprResult Acc = IV; 6256 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 6257 LoopIterationSpace &IS = IterSpaces[Cnt]; 6258 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 6259 ExprResult Iter; 6260 6261 // Compute prod 6262 ExprResult Prod = 6263 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 6264 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K) 6265 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(), 6266 IterSpaces[K].NumIterations); 6267 6268 // Iter = Acc / Prod 6269 // If there is at least one more inner loop to avoid 6270 // multiplication by 1. 6271 if (Cnt + 1 < NestedLoopCount) 6272 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, 6273 Acc.get(), Prod.get()); 6274 else 6275 Iter = Acc; 6276 if (!Iter.isUsable()) { 6277 HasErrors = true; 6278 break; 6279 } 6280 6281 // Update Acc: 6282 // Acc -= Iter * Prod 6283 // Check if there is at least one more inner loop to avoid 6284 // multiplication by 1. 6285 if (Cnt + 1 < NestedLoopCount) 6286 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, 6287 Iter.get(), Prod.get()); 6288 else 6289 Prod = Iter; 6290 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub, 6291 Acc.get(), Prod.get()); 6292 6293 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 6294 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 6295 DeclRefExpr *CounterVar = buildDeclRefExpr( 6296 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 6297 /*RefersToCapture=*/true); 6298 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 6299 IS.CounterInit, Captures); 6300 if (!Init.isUsable()) { 6301 HasErrors = true; 6302 break; 6303 } 6304 ExprResult Update = buildCounterUpdate( 6305 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 6306 IS.CounterStep, IS.Subtract, &Captures); 6307 if (!Update.isUsable()) { 6308 HasErrors = true; 6309 break; 6310 } 6311 6312 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 6313 ExprResult Final = buildCounterUpdate( 6314 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, 6315 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures); 6316 if (!Final.isUsable()) { 6317 HasErrors = true; 6318 break; 6319 } 6320 6321 if (!Update.isUsable() || !Final.isUsable()) { 6322 HasErrors = true; 6323 break; 6324 } 6325 // Save results 6326 Built.Counters[Cnt] = IS.CounterVar; 6327 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 6328 Built.Inits[Cnt] = Init.get(); 6329 Built.Updates[Cnt] = Update.get(); 6330 Built.Finals[Cnt] = Final.get(); 6331 } 6332 } 6333 6334 if (HasErrors) 6335 return 0; 6336 6337 // Save results 6338 Built.IterationVarRef = IV.get(); 6339 Built.LastIteration = LastIteration.get(); 6340 Built.NumIterations = NumIterations.get(); 6341 Built.CalcLastIteration = SemaRef 6342 .ActOnFinishFullExpr(CalcLastIteration.get(), 6343 /*DiscardedValue*/ false) 6344 .get(); 6345 Built.PreCond = PreCond.get(); 6346 Built.PreInits = buildPreInits(C, Captures); 6347 Built.Cond = Cond.get(); 6348 Built.Init = Init.get(); 6349 Built.Inc = Inc.get(); 6350 Built.LB = LB.get(); 6351 Built.UB = UB.get(); 6352 Built.IL = IL.get(); 6353 Built.ST = ST.get(); 6354 Built.EUB = EUB.get(); 6355 Built.NLB = NextLB.get(); 6356 Built.NUB = NextUB.get(); 6357 Built.PrevLB = PrevLB.get(); 6358 Built.PrevUB = PrevUB.get(); 6359 Built.DistInc = DistInc.get(); 6360 Built.PrevEUB = PrevEUB.get(); 6361 Built.DistCombinedFields.LB = CombLB.get(); 6362 Built.DistCombinedFields.UB = CombUB.get(); 6363 Built.DistCombinedFields.EUB = CombEUB.get(); 6364 Built.DistCombinedFields.Init = CombInit.get(); 6365 Built.DistCombinedFields.Cond = CombCond.get(); 6366 Built.DistCombinedFields.NLB = CombNextLB.get(); 6367 Built.DistCombinedFields.NUB = CombNextUB.get(); 6368 Built.DistCombinedFields.DistCond = CombDistCond.get(); 6369 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get(); 6370 6371 return NestedLoopCount; 6372 } 6373 6374 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 6375 auto CollapseClauses = 6376 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 6377 if (CollapseClauses.begin() != CollapseClauses.end()) 6378 return (*CollapseClauses.begin())->getNumForLoops(); 6379 return nullptr; 6380 } 6381 6382 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 6383 auto OrderedClauses = 6384 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 6385 if (OrderedClauses.begin() != OrderedClauses.end()) 6386 return (*OrderedClauses.begin())->getNumForLoops(); 6387 return nullptr; 6388 } 6389 6390 static bool checkSimdlenSafelenSpecified(Sema &S, 6391 const ArrayRef<OMPClause *> Clauses) { 6392 const OMPSafelenClause *Safelen = nullptr; 6393 const OMPSimdlenClause *Simdlen = nullptr; 6394 6395 for (const OMPClause *Clause : Clauses) { 6396 if (Clause->getClauseKind() == OMPC_safelen) 6397 Safelen = cast<OMPSafelenClause>(Clause); 6398 else if (Clause->getClauseKind() == OMPC_simdlen) 6399 Simdlen = cast<OMPSimdlenClause>(Clause); 6400 if (Safelen && Simdlen) 6401 break; 6402 } 6403 6404 if (Simdlen && Safelen) { 6405 const Expr *SimdlenLength = Simdlen->getSimdlen(); 6406 const Expr *SafelenLength = Safelen->getSafelen(); 6407 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 6408 SimdlenLength->isInstantiationDependent() || 6409 SimdlenLength->containsUnexpandedParameterPack()) 6410 return false; 6411 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 6412 SafelenLength->isInstantiationDependent() || 6413 SafelenLength->containsUnexpandedParameterPack()) 6414 return false; 6415 Expr::EvalResult SimdlenResult, SafelenResult; 6416 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context); 6417 SafelenLength->EvaluateAsInt(SafelenResult, S.Context); 6418 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt(); 6419 llvm::APSInt SafelenRes = SafelenResult.Val.getInt(); 6420 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 6421 // If both simdlen and safelen clauses are specified, the value of the 6422 // simdlen parameter must be less than or equal to the value of the safelen 6423 // parameter. 6424 if (SimdlenRes > SafelenRes) { 6425 S.Diag(SimdlenLength->getExprLoc(), 6426 diag::err_omp_wrong_simdlen_safelen_values) 6427 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 6428 return true; 6429 } 6430 } 6431 return false; 6432 } 6433 6434 StmtResult 6435 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 6436 SourceLocation StartLoc, SourceLocation EndLoc, 6437 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6438 if (!AStmt) 6439 return StmtError(); 6440 6441 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6442 OMPLoopDirective::HelperExprs B; 6443 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6444 // define the nested loops number. 6445 unsigned NestedLoopCount = checkOpenMPLoop( 6446 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 6447 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 6448 if (NestedLoopCount == 0) 6449 return StmtError(); 6450 6451 assert((CurContext->isDependentContext() || B.builtAll()) && 6452 "omp simd loop exprs were not built"); 6453 6454 if (!CurContext->isDependentContext()) { 6455 // Finalize the clauses that need pre-built expressions for CodeGen. 6456 for (OMPClause *C : Clauses) { 6457 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6458 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6459 B.NumIterations, *this, CurScope, 6460 DSAStack)) 6461 return StmtError(); 6462 } 6463 } 6464 6465 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6466 return StmtError(); 6467 6468 setFunctionHasBranchProtectedScope(); 6469 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 6470 Clauses, AStmt, B); 6471 } 6472 6473 StmtResult 6474 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 6475 SourceLocation StartLoc, SourceLocation EndLoc, 6476 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6477 if (!AStmt) 6478 return StmtError(); 6479 6480 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6481 OMPLoopDirective::HelperExprs B; 6482 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6483 // define the nested loops number. 6484 unsigned NestedLoopCount = checkOpenMPLoop( 6485 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 6486 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 6487 if (NestedLoopCount == 0) 6488 return StmtError(); 6489 6490 assert((CurContext->isDependentContext() || B.builtAll()) && 6491 "omp for loop exprs were not built"); 6492 6493 if (!CurContext->isDependentContext()) { 6494 // Finalize the clauses that need pre-built expressions for CodeGen. 6495 for (OMPClause *C : Clauses) { 6496 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6497 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6498 B.NumIterations, *this, CurScope, 6499 DSAStack)) 6500 return StmtError(); 6501 } 6502 } 6503 6504 setFunctionHasBranchProtectedScope(); 6505 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 6506 Clauses, AStmt, B, DSAStack->isCancelRegion()); 6507 } 6508 6509 StmtResult Sema::ActOnOpenMPForSimdDirective( 6510 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6511 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6512 if (!AStmt) 6513 return StmtError(); 6514 6515 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6516 OMPLoopDirective::HelperExprs B; 6517 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6518 // define the nested loops number. 6519 unsigned NestedLoopCount = 6520 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 6521 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 6522 VarsWithImplicitDSA, B); 6523 if (NestedLoopCount == 0) 6524 return StmtError(); 6525 6526 assert((CurContext->isDependentContext() || B.builtAll()) && 6527 "omp for simd loop exprs were not built"); 6528 6529 if (!CurContext->isDependentContext()) { 6530 // Finalize the clauses that need pre-built expressions for CodeGen. 6531 for (OMPClause *C : Clauses) { 6532 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6533 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6534 B.NumIterations, *this, CurScope, 6535 DSAStack)) 6536 return StmtError(); 6537 } 6538 } 6539 6540 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6541 return StmtError(); 6542 6543 setFunctionHasBranchProtectedScope(); 6544 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 6545 Clauses, AStmt, B); 6546 } 6547 6548 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 6549 Stmt *AStmt, 6550 SourceLocation StartLoc, 6551 SourceLocation EndLoc) { 6552 if (!AStmt) 6553 return StmtError(); 6554 6555 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6556 auto BaseStmt = AStmt; 6557 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 6558 BaseStmt = CS->getCapturedStmt(); 6559 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 6560 auto S = C->children(); 6561 if (S.begin() == S.end()) 6562 return StmtError(); 6563 // All associated statements must be '#pragma omp section' except for 6564 // the first one. 6565 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 6566 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 6567 if (SectionStmt) 6568 Diag(SectionStmt->getBeginLoc(), 6569 diag::err_omp_sections_substmt_not_section); 6570 return StmtError(); 6571 } 6572 cast<OMPSectionDirective>(SectionStmt) 6573 ->setHasCancel(DSAStack->isCancelRegion()); 6574 } 6575 } else { 6576 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt); 6577 return StmtError(); 6578 } 6579 6580 setFunctionHasBranchProtectedScope(); 6581 6582 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 6583 DSAStack->isCancelRegion()); 6584 } 6585 6586 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 6587 SourceLocation StartLoc, 6588 SourceLocation EndLoc) { 6589 if (!AStmt) 6590 return StmtError(); 6591 6592 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6593 6594 setFunctionHasBranchProtectedScope(); 6595 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 6596 6597 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 6598 DSAStack->isCancelRegion()); 6599 } 6600 6601 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 6602 Stmt *AStmt, 6603 SourceLocation StartLoc, 6604 SourceLocation EndLoc) { 6605 if (!AStmt) 6606 return StmtError(); 6607 6608 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6609 6610 setFunctionHasBranchProtectedScope(); 6611 6612 // OpenMP [2.7.3, single Construct, Restrictions] 6613 // The copyprivate clause must not be used with the nowait clause. 6614 const OMPClause *Nowait = nullptr; 6615 const OMPClause *Copyprivate = nullptr; 6616 for (const OMPClause *Clause : Clauses) { 6617 if (Clause->getClauseKind() == OMPC_nowait) 6618 Nowait = Clause; 6619 else if (Clause->getClauseKind() == OMPC_copyprivate) 6620 Copyprivate = Clause; 6621 if (Copyprivate && Nowait) { 6622 Diag(Copyprivate->getBeginLoc(), 6623 diag::err_omp_single_copyprivate_with_nowait); 6624 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here); 6625 return StmtError(); 6626 } 6627 } 6628 6629 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 6630 } 6631 6632 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 6633 SourceLocation StartLoc, 6634 SourceLocation EndLoc) { 6635 if (!AStmt) 6636 return StmtError(); 6637 6638 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6639 6640 setFunctionHasBranchProtectedScope(); 6641 6642 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 6643 } 6644 6645 StmtResult Sema::ActOnOpenMPCriticalDirective( 6646 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 6647 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 6648 if (!AStmt) 6649 return StmtError(); 6650 6651 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6652 6653 bool ErrorFound = false; 6654 llvm::APSInt Hint; 6655 SourceLocation HintLoc; 6656 bool DependentHint = false; 6657 for (const OMPClause *C : Clauses) { 6658 if (C->getClauseKind() == OMPC_hint) { 6659 if (!DirName.getName()) { 6660 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name); 6661 ErrorFound = true; 6662 } 6663 Expr *E = cast<OMPHintClause>(C)->getHint(); 6664 if (E->isTypeDependent() || E->isValueDependent() || 6665 E->isInstantiationDependent()) { 6666 DependentHint = true; 6667 } else { 6668 Hint = E->EvaluateKnownConstInt(Context); 6669 HintLoc = C->getBeginLoc(); 6670 } 6671 } 6672 } 6673 if (ErrorFound) 6674 return StmtError(); 6675 const auto Pair = DSAStack->getCriticalWithHint(DirName); 6676 if (Pair.first && DirName.getName() && !DependentHint) { 6677 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 6678 Diag(StartLoc, diag::err_omp_critical_with_hint); 6679 if (HintLoc.isValid()) 6680 Diag(HintLoc, diag::note_omp_critical_hint_here) 6681 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); 6682 else 6683 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 6684 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 6685 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here) 6686 << 1 6687 << C->getHint()->EvaluateKnownConstInt(Context).toString( 6688 /*Radix=*/10, /*Signed=*/false); 6689 } else { 6690 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1; 6691 } 6692 } 6693 } 6694 6695 setFunctionHasBranchProtectedScope(); 6696 6697 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 6698 Clauses, AStmt); 6699 if (!Pair.first && DirName.getName() && !DependentHint) 6700 DSAStack->addCriticalWithHint(Dir, Hint); 6701 return Dir; 6702 } 6703 6704 StmtResult Sema::ActOnOpenMPParallelForDirective( 6705 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6706 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6707 if (!AStmt) 6708 return StmtError(); 6709 6710 auto *CS = cast<CapturedStmt>(AStmt); 6711 // 1.2.2 OpenMP Language Terminology 6712 // Structured block - An executable statement with a single entry at the 6713 // top and a single exit at the bottom. 6714 // The point of exit cannot be a branch out of the structured block. 6715 // longjmp() and throw() must not violate the entry/exit criteria. 6716 CS->getCapturedDecl()->setNothrow(); 6717 6718 OMPLoopDirective::HelperExprs B; 6719 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6720 // define the nested loops number. 6721 unsigned NestedLoopCount = 6722 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 6723 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 6724 VarsWithImplicitDSA, B); 6725 if (NestedLoopCount == 0) 6726 return StmtError(); 6727 6728 assert((CurContext->isDependentContext() || B.builtAll()) && 6729 "omp parallel for loop exprs were not built"); 6730 6731 if (!CurContext->isDependentContext()) { 6732 // Finalize the clauses that need pre-built expressions for CodeGen. 6733 for (OMPClause *C : Clauses) { 6734 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6735 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6736 B.NumIterations, *this, CurScope, 6737 DSAStack)) 6738 return StmtError(); 6739 } 6740 } 6741 6742 setFunctionHasBranchProtectedScope(); 6743 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc, 6744 NestedLoopCount, Clauses, AStmt, B, 6745 DSAStack->isCancelRegion()); 6746 } 6747 6748 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 6749 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6750 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6751 if (!AStmt) 6752 return StmtError(); 6753 6754 auto *CS = cast<CapturedStmt>(AStmt); 6755 // 1.2.2 OpenMP Language Terminology 6756 // Structured block - An executable statement with a single entry at the 6757 // top and a single exit at the bottom. 6758 // The point of exit cannot be a branch out of the structured block. 6759 // longjmp() and throw() must not violate the entry/exit criteria. 6760 CS->getCapturedDecl()->setNothrow(); 6761 6762 OMPLoopDirective::HelperExprs B; 6763 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6764 // define the nested loops number. 6765 unsigned NestedLoopCount = 6766 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 6767 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 6768 VarsWithImplicitDSA, B); 6769 if (NestedLoopCount == 0) 6770 return StmtError(); 6771 6772 if (!CurContext->isDependentContext()) { 6773 // Finalize the clauses that need pre-built expressions for CodeGen. 6774 for (OMPClause *C : Clauses) { 6775 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6776 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6777 B.NumIterations, *this, CurScope, 6778 DSAStack)) 6779 return StmtError(); 6780 } 6781 } 6782 6783 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6784 return StmtError(); 6785 6786 setFunctionHasBranchProtectedScope(); 6787 return OMPParallelForSimdDirective::Create( 6788 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 6789 } 6790 6791 StmtResult 6792 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 6793 Stmt *AStmt, SourceLocation StartLoc, 6794 SourceLocation EndLoc) { 6795 if (!AStmt) 6796 return StmtError(); 6797 6798 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6799 auto BaseStmt = AStmt; 6800 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 6801 BaseStmt = CS->getCapturedStmt(); 6802 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 6803 auto S = C->children(); 6804 if (S.begin() == S.end()) 6805 return StmtError(); 6806 // All associated statements must be '#pragma omp section' except for 6807 // the first one. 6808 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 6809 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 6810 if (SectionStmt) 6811 Diag(SectionStmt->getBeginLoc(), 6812 diag::err_omp_parallel_sections_substmt_not_section); 6813 return StmtError(); 6814 } 6815 cast<OMPSectionDirective>(SectionStmt) 6816 ->setHasCancel(DSAStack->isCancelRegion()); 6817 } 6818 } else { 6819 Diag(AStmt->getBeginLoc(), 6820 diag::err_omp_parallel_sections_not_compound_stmt); 6821 return StmtError(); 6822 } 6823 6824 setFunctionHasBranchProtectedScope(); 6825 6826 return OMPParallelSectionsDirective::Create( 6827 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion()); 6828 } 6829 6830 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 6831 Stmt *AStmt, SourceLocation StartLoc, 6832 SourceLocation EndLoc) { 6833 if (!AStmt) 6834 return StmtError(); 6835 6836 auto *CS = cast<CapturedStmt>(AStmt); 6837 // 1.2.2 OpenMP Language Terminology 6838 // Structured block - An executable statement with a single entry at the 6839 // top and a single exit at the bottom. 6840 // The point of exit cannot be a branch out of the structured block. 6841 // longjmp() and throw() must not violate the entry/exit criteria. 6842 CS->getCapturedDecl()->setNothrow(); 6843 6844 setFunctionHasBranchProtectedScope(); 6845 6846 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 6847 DSAStack->isCancelRegion()); 6848 } 6849 6850 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 6851 SourceLocation EndLoc) { 6852 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 6853 } 6854 6855 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 6856 SourceLocation EndLoc) { 6857 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 6858 } 6859 6860 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 6861 SourceLocation EndLoc) { 6862 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 6863 } 6864 6865 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 6866 Stmt *AStmt, 6867 SourceLocation StartLoc, 6868 SourceLocation EndLoc) { 6869 if (!AStmt) 6870 return StmtError(); 6871 6872 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6873 6874 setFunctionHasBranchProtectedScope(); 6875 6876 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 6877 AStmt, 6878 DSAStack->getTaskgroupReductionRef()); 6879 } 6880 6881 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 6882 SourceLocation StartLoc, 6883 SourceLocation EndLoc) { 6884 assert(Clauses.size() <= 1 && "Extra clauses in flush directive"); 6885 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 6886 } 6887 6888 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 6889 Stmt *AStmt, 6890 SourceLocation StartLoc, 6891 SourceLocation EndLoc) { 6892 const OMPClause *DependFound = nullptr; 6893 const OMPClause *DependSourceClause = nullptr; 6894 const OMPClause *DependSinkClause = nullptr; 6895 bool ErrorFound = false; 6896 const OMPThreadsClause *TC = nullptr; 6897 const OMPSIMDClause *SC = nullptr; 6898 for (const OMPClause *C : Clauses) { 6899 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 6900 DependFound = C; 6901 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 6902 if (DependSourceClause) { 6903 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 6904 << getOpenMPDirectiveName(OMPD_ordered) 6905 << getOpenMPClauseName(OMPC_depend) << 2; 6906 ErrorFound = true; 6907 } else { 6908 DependSourceClause = C; 6909 } 6910 if (DependSinkClause) { 6911 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 6912 << 0; 6913 ErrorFound = true; 6914 } 6915 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 6916 if (DependSourceClause) { 6917 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 6918 << 1; 6919 ErrorFound = true; 6920 } 6921 DependSinkClause = C; 6922 } 6923 } else if (C->getClauseKind() == OMPC_threads) { 6924 TC = cast<OMPThreadsClause>(C); 6925 } else if (C->getClauseKind() == OMPC_simd) { 6926 SC = cast<OMPSIMDClause>(C); 6927 } 6928 } 6929 if (!ErrorFound && !SC && 6930 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 6931 // OpenMP [2.8.1,simd Construct, Restrictions] 6932 // An ordered construct with the simd clause is the only OpenMP construct 6933 // that can appear in the simd region. 6934 Diag(StartLoc, diag::err_omp_prohibited_region_simd); 6935 ErrorFound = true; 6936 } else if (DependFound && (TC || SC)) { 6937 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd) 6938 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 6939 ErrorFound = true; 6940 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) { 6941 Diag(DependFound->getBeginLoc(), 6942 diag::err_omp_ordered_directive_without_param); 6943 ErrorFound = true; 6944 } else if (TC || Clauses.empty()) { 6945 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) { 6946 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc; 6947 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 6948 << (TC != nullptr); 6949 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param); 6950 ErrorFound = true; 6951 } 6952 } 6953 if ((!AStmt && !DependFound) || ErrorFound) 6954 return StmtError(); 6955 6956 if (AStmt) { 6957 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6958 6959 setFunctionHasBranchProtectedScope(); 6960 } 6961 6962 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 6963 } 6964 6965 namespace { 6966 /// Helper class for checking expression in 'omp atomic [update]' 6967 /// construct. 6968 class OpenMPAtomicUpdateChecker { 6969 /// Error results for atomic update expressions. 6970 enum ExprAnalysisErrorCode { 6971 /// A statement is not an expression statement. 6972 NotAnExpression, 6973 /// Expression is not builtin binary or unary operation. 6974 NotABinaryOrUnaryExpression, 6975 /// Unary operation is not post-/pre- increment/decrement operation. 6976 NotAnUnaryIncDecExpression, 6977 /// An expression is not of scalar type. 6978 NotAScalarType, 6979 /// A binary operation is not an assignment operation. 6980 NotAnAssignmentOp, 6981 /// RHS part of the binary operation is not a binary expression. 6982 NotABinaryExpression, 6983 /// RHS part is not additive/multiplicative/shift/biwise binary 6984 /// expression. 6985 NotABinaryOperator, 6986 /// RHS binary operation does not have reference to the updated LHS 6987 /// part. 6988 NotAnUpdateExpression, 6989 /// No errors is found. 6990 NoError 6991 }; 6992 /// Reference to Sema. 6993 Sema &SemaRef; 6994 /// A location for note diagnostics (when error is found). 6995 SourceLocation NoteLoc; 6996 /// 'x' lvalue part of the source atomic expression. 6997 Expr *X; 6998 /// 'expr' rvalue part of the source atomic expression. 6999 Expr *E; 7000 /// Helper expression of the form 7001 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 7002 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 7003 Expr *UpdateExpr; 7004 /// Is 'x' a LHS in a RHS part of full update expression. It is 7005 /// important for non-associative operations. 7006 bool IsXLHSInRHSPart; 7007 BinaryOperatorKind Op; 7008 SourceLocation OpLoc; 7009 /// true if the source expression is a postfix unary operation, false 7010 /// if it is a prefix unary operation. 7011 bool IsPostfixUpdate; 7012 7013 public: 7014 OpenMPAtomicUpdateChecker(Sema &SemaRef) 7015 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 7016 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 7017 /// Check specified statement that it is suitable for 'atomic update' 7018 /// constructs and extract 'x', 'expr' and Operation from the original 7019 /// expression. If DiagId and NoteId == 0, then only check is performed 7020 /// without error notification. 7021 /// \param DiagId Diagnostic which should be emitted if error is found. 7022 /// \param NoteId Diagnostic note for the main error message. 7023 /// \return true if statement is not an update expression, false otherwise. 7024 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 7025 /// Return the 'x' lvalue part of the source atomic expression. 7026 Expr *getX() const { return X; } 7027 /// Return the 'expr' rvalue part of the source atomic expression. 7028 Expr *getExpr() const { return E; } 7029 /// Return the update expression used in calculation of the updated 7030 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 7031 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 7032 Expr *getUpdateExpr() const { return UpdateExpr; } 7033 /// Return true if 'x' is LHS in RHS part of full update expression, 7034 /// false otherwise. 7035 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 7036 7037 /// true if the source expression is a postfix unary operation, false 7038 /// if it is a prefix unary operation. 7039 bool isPostfixUpdate() const { return IsPostfixUpdate; } 7040 7041 private: 7042 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 7043 unsigned NoteId = 0); 7044 }; 7045 } // namespace 7046 7047 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 7048 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 7049 ExprAnalysisErrorCode ErrorFound = NoError; 7050 SourceLocation ErrorLoc, NoteLoc; 7051 SourceRange ErrorRange, NoteRange; 7052 // Allowed constructs are: 7053 // x = x binop expr; 7054 // x = expr binop x; 7055 if (AtomicBinOp->getOpcode() == BO_Assign) { 7056 X = AtomicBinOp->getLHS(); 7057 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 7058 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 7059 if (AtomicInnerBinOp->isMultiplicativeOp() || 7060 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 7061 AtomicInnerBinOp->isBitwiseOp()) { 7062 Op = AtomicInnerBinOp->getOpcode(); 7063 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 7064 Expr *LHS = AtomicInnerBinOp->getLHS(); 7065 Expr *RHS = AtomicInnerBinOp->getRHS(); 7066 llvm::FoldingSetNodeID XId, LHSId, RHSId; 7067 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 7068 /*Canonical=*/true); 7069 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 7070 /*Canonical=*/true); 7071 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 7072 /*Canonical=*/true); 7073 if (XId == LHSId) { 7074 E = RHS; 7075 IsXLHSInRHSPart = true; 7076 } else if (XId == RHSId) { 7077 E = LHS; 7078 IsXLHSInRHSPart = false; 7079 } else { 7080 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 7081 ErrorRange = AtomicInnerBinOp->getSourceRange(); 7082 NoteLoc = X->getExprLoc(); 7083 NoteRange = X->getSourceRange(); 7084 ErrorFound = NotAnUpdateExpression; 7085 } 7086 } else { 7087 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 7088 ErrorRange = AtomicInnerBinOp->getSourceRange(); 7089 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 7090 NoteRange = SourceRange(NoteLoc, NoteLoc); 7091 ErrorFound = NotABinaryOperator; 7092 } 7093 } else { 7094 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 7095 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 7096 ErrorFound = NotABinaryExpression; 7097 } 7098 } else { 7099 ErrorLoc = AtomicBinOp->getExprLoc(); 7100 ErrorRange = AtomicBinOp->getSourceRange(); 7101 NoteLoc = AtomicBinOp->getOperatorLoc(); 7102 NoteRange = SourceRange(NoteLoc, NoteLoc); 7103 ErrorFound = NotAnAssignmentOp; 7104 } 7105 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 7106 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 7107 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 7108 return true; 7109 } 7110 if (SemaRef.CurContext->isDependentContext()) 7111 E = X = UpdateExpr = nullptr; 7112 return ErrorFound != NoError; 7113 } 7114 7115 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 7116 unsigned NoteId) { 7117 ExprAnalysisErrorCode ErrorFound = NoError; 7118 SourceLocation ErrorLoc, NoteLoc; 7119 SourceRange ErrorRange, NoteRange; 7120 // Allowed constructs are: 7121 // x++; 7122 // x--; 7123 // ++x; 7124 // --x; 7125 // x binop= expr; 7126 // x = x binop expr; 7127 // x = expr binop x; 7128 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 7129 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 7130 if (AtomicBody->getType()->isScalarType() || 7131 AtomicBody->isInstantiationDependent()) { 7132 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 7133 AtomicBody->IgnoreParenImpCasts())) { 7134 // Check for Compound Assignment Operation 7135 Op = BinaryOperator::getOpForCompoundAssignment( 7136 AtomicCompAssignOp->getOpcode()); 7137 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 7138 E = AtomicCompAssignOp->getRHS(); 7139 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 7140 IsXLHSInRHSPart = true; 7141 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 7142 AtomicBody->IgnoreParenImpCasts())) { 7143 // Check for Binary Operation 7144 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 7145 return true; 7146 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 7147 AtomicBody->IgnoreParenImpCasts())) { 7148 // Check for Unary Operation 7149 if (AtomicUnaryOp->isIncrementDecrementOp()) { 7150 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 7151 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 7152 OpLoc = AtomicUnaryOp->getOperatorLoc(); 7153 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 7154 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 7155 IsXLHSInRHSPart = true; 7156 } else { 7157 ErrorFound = NotAnUnaryIncDecExpression; 7158 ErrorLoc = AtomicUnaryOp->getExprLoc(); 7159 ErrorRange = AtomicUnaryOp->getSourceRange(); 7160 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 7161 NoteRange = SourceRange(NoteLoc, NoteLoc); 7162 } 7163 } else if (!AtomicBody->isInstantiationDependent()) { 7164 ErrorFound = NotABinaryOrUnaryExpression; 7165 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 7166 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 7167 } 7168 } else { 7169 ErrorFound = NotAScalarType; 7170 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc(); 7171 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 7172 } 7173 } else { 7174 ErrorFound = NotAnExpression; 7175 NoteLoc = ErrorLoc = S->getBeginLoc(); 7176 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 7177 } 7178 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 7179 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 7180 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 7181 return true; 7182 } 7183 if (SemaRef.CurContext->isDependentContext()) 7184 E = X = UpdateExpr = nullptr; 7185 if (ErrorFound == NoError && E && X) { 7186 // Build an update expression of form 'OpaqueValueExpr(x) binop 7187 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 7188 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 7189 auto *OVEX = new (SemaRef.getASTContext()) 7190 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); 7191 auto *OVEExpr = new (SemaRef.getASTContext()) 7192 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); 7193 ExprResult Update = 7194 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 7195 IsXLHSInRHSPart ? OVEExpr : OVEX); 7196 if (Update.isInvalid()) 7197 return true; 7198 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 7199 Sema::AA_Casting); 7200 if (Update.isInvalid()) 7201 return true; 7202 UpdateExpr = Update.get(); 7203 } 7204 return ErrorFound != NoError; 7205 } 7206 7207 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 7208 Stmt *AStmt, 7209 SourceLocation StartLoc, 7210 SourceLocation EndLoc) { 7211 if (!AStmt) 7212 return StmtError(); 7213 7214 auto *CS = cast<CapturedStmt>(AStmt); 7215 // 1.2.2 OpenMP Language Terminology 7216 // Structured block - An executable statement with a single entry at the 7217 // top and a single exit at the bottom. 7218 // The point of exit cannot be a branch out of the structured block. 7219 // longjmp() and throw() must not violate the entry/exit criteria. 7220 OpenMPClauseKind AtomicKind = OMPC_unknown; 7221 SourceLocation AtomicKindLoc; 7222 for (const OMPClause *C : Clauses) { 7223 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 7224 C->getClauseKind() == OMPC_update || 7225 C->getClauseKind() == OMPC_capture) { 7226 if (AtomicKind != OMPC_unknown) { 7227 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses) 7228 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 7229 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause) 7230 << getOpenMPClauseName(AtomicKind); 7231 } else { 7232 AtomicKind = C->getClauseKind(); 7233 AtomicKindLoc = C->getBeginLoc(); 7234 } 7235 } 7236 } 7237 7238 Stmt *Body = CS->getCapturedStmt(); 7239 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 7240 Body = EWC->getSubExpr(); 7241 7242 Expr *X = nullptr; 7243 Expr *V = nullptr; 7244 Expr *E = nullptr; 7245 Expr *UE = nullptr; 7246 bool IsXLHSInRHSPart = false; 7247 bool IsPostfixUpdate = false; 7248 // OpenMP [2.12.6, atomic Construct] 7249 // In the next expressions: 7250 // * x and v (as applicable) are both l-value expressions with scalar type. 7251 // * During the execution of an atomic region, multiple syntactic 7252 // occurrences of x must designate the same storage location. 7253 // * Neither of v and expr (as applicable) may access the storage location 7254 // designated by x. 7255 // * Neither of x and expr (as applicable) may access the storage location 7256 // designated by v. 7257 // * expr is an expression with scalar type. 7258 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 7259 // * binop, binop=, ++, and -- are not overloaded operators. 7260 // * The expression x binop expr must be numerically equivalent to x binop 7261 // (expr). This requirement is satisfied if the operators in expr have 7262 // precedence greater than binop, or by using parentheses around expr or 7263 // subexpressions of expr. 7264 // * The expression expr binop x must be numerically equivalent to (expr) 7265 // binop x. This requirement is satisfied if the operators in expr have 7266 // precedence equal to or greater than binop, or by using parentheses around 7267 // expr or subexpressions of expr. 7268 // * For forms that allow multiple occurrences of x, the number of times 7269 // that x is evaluated is unspecified. 7270 if (AtomicKind == OMPC_read) { 7271 enum { 7272 NotAnExpression, 7273 NotAnAssignmentOp, 7274 NotAScalarType, 7275 NotAnLValue, 7276 NoError 7277 } ErrorFound = NoError; 7278 SourceLocation ErrorLoc, NoteLoc; 7279 SourceRange ErrorRange, NoteRange; 7280 // If clause is read: 7281 // v = x; 7282 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 7283 const auto *AtomicBinOp = 7284 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 7285 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 7286 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 7287 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 7288 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 7289 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 7290 if (!X->isLValue() || !V->isLValue()) { 7291 const Expr *NotLValueExpr = X->isLValue() ? V : X; 7292 ErrorFound = NotAnLValue; 7293 ErrorLoc = AtomicBinOp->getExprLoc(); 7294 ErrorRange = AtomicBinOp->getSourceRange(); 7295 NoteLoc = NotLValueExpr->getExprLoc(); 7296 NoteRange = NotLValueExpr->getSourceRange(); 7297 } 7298 } else if (!X->isInstantiationDependent() || 7299 !V->isInstantiationDependent()) { 7300 const Expr *NotScalarExpr = 7301 (X->isInstantiationDependent() || X->getType()->isScalarType()) 7302 ? V 7303 : X; 7304 ErrorFound = NotAScalarType; 7305 ErrorLoc = AtomicBinOp->getExprLoc(); 7306 ErrorRange = AtomicBinOp->getSourceRange(); 7307 NoteLoc = NotScalarExpr->getExprLoc(); 7308 NoteRange = NotScalarExpr->getSourceRange(); 7309 } 7310 } else if (!AtomicBody->isInstantiationDependent()) { 7311 ErrorFound = NotAnAssignmentOp; 7312 ErrorLoc = AtomicBody->getExprLoc(); 7313 ErrorRange = AtomicBody->getSourceRange(); 7314 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 7315 : AtomicBody->getExprLoc(); 7316 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 7317 : AtomicBody->getSourceRange(); 7318 } 7319 } else { 7320 ErrorFound = NotAnExpression; 7321 NoteLoc = ErrorLoc = Body->getBeginLoc(); 7322 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 7323 } 7324 if (ErrorFound != NoError) { 7325 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 7326 << ErrorRange; 7327 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 7328 << NoteRange; 7329 return StmtError(); 7330 } 7331 if (CurContext->isDependentContext()) 7332 V = X = nullptr; 7333 } else if (AtomicKind == OMPC_write) { 7334 enum { 7335 NotAnExpression, 7336 NotAnAssignmentOp, 7337 NotAScalarType, 7338 NotAnLValue, 7339 NoError 7340 } ErrorFound = NoError; 7341 SourceLocation ErrorLoc, NoteLoc; 7342 SourceRange ErrorRange, NoteRange; 7343 // If clause is write: 7344 // x = expr; 7345 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 7346 const auto *AtomicBinOp = 7347 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 7348 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 7349 X = AtomicBinOp->getLHS(); 7350 E = AtomicBinOp->getRHS(); 7351 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 7352 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 7353 if (!X->isLValue()) { 7354 ErrorFound = NotAnLValue; 7355 ErrorLoc = AtomicBinOp->getExprLoc(); 7356 ErrorRange = AtomicBinOp->getSourceRange(); 7357 NoteLoc = X->getExprLoc(); 7358 NoteRange = X->getSourceRange(); 7359 } 7360 } else if (!X->isInstantiationDependent() || 7361 !E->isInstantiationDependent()) { 7362 const Expr *NotScalarExpr = 7363 (X->isInstantiationDependent() || X->getType()->isScalarType()) 7364 ? E 7365 : X; 7366 ErrorFound = NotAScalarType; 7367 ErrorLoc = AtomicBinOp->getExprLoc(); 7368 ErrorRange = AtomicBinOp->getSourceRange(); 7369 NoteLoc = NotScalarExpr->getExprLoc(); 7370 NoteRange = NotScalarExpr->getSourceRange(); 7371 } 7372 } else if (!AtomicBody->isInstantiationDependent()) { 7373 ErrorFound = NotAnAssignmentOp; 7374 ErrorLoc = AtomicBody->getExprLoc(); 7375 ErrorRange = AtomicBody->getSourceRange(); 7376 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 7377 : AtomicBody->getExprLoc(); 7378 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 7379 : AtomicBody->getSourceRange(); 7380 } 7381 } else { 7382 ErrorFound = NotAnExpression; 7383 NoteLoc = ErrorLoc = Body->getBeginLoc(); 7384 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 7385 } 7386 if (ErrorFound != NoError) { 7387 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 7388 << ErrorRange; 7389 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 7390 << NoteRange; 7391 return StmtError(); 7392 } 7393 if (CurContext->isDependentContext()) 7394 E = X = nullptr; 7395 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 7396 // If clause is update: 7397 // x++; 7398 // x--; 7399 // ++x; 7400 // --x; 7401 // x binop= expr; 7402 // x = x binop expr; 7403 // x = expr binop x; 7404 OpenMPAtomicUpdateChecker Checker(*this); 7405 if (Checker.checkStatement( 7406 Body, (AtomicKind == OMPC_update) 7407 ? diag::err_omp_atomic_update_not_expression_statement 7408 : diag::err_omp_atomic_not_expression_statement, 7409 diag::note_omp_atomic_update)) 7410 return StmtError(); 7411 if (!CurContext->isDependentContext()) { 7412 E = Checker.getExpr(); 7413 X = Checker.getX(); 7414 UE = Checker.getUpdateExpr(); 7415 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 7416 } 7417 } else if (AtomicKind == OMPC_capture) { 7418 enum { 7419 NotAnAssignmentOp, 7420 NotACompoundStatement, 7421 NotTwoSubstatements, 7422 NotASpecificExpression, 7423 NoError 7424 } ErrorFound = NoError; 7425 SourceLocation ErrorLoc, NoteLoc; 7426 SourceRange ErrorRange, NoteRange; 7427 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 7428 // If clause is a capture: 7429 // v = x++; 7430 // v = x--; 7431 // v = ++x; 7432 // v = --x; 7433 // v = x binop= expr; 7434 // v = x = x binop expr; 7435 // v = x = expr binop x; 7436 const auto *AtomicBinOp = 7437 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 7438 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 7439 V = AtomicBinOp->getLHS(); 7440 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 7441 OpenMPAtomicUpdateChecker Checker(*this); 7442 if (Checker.checkStatement( 7443 Body, diag::err_omp_atomic_capture_not_expression_statement, 7444 diag::note_omp_atomic_update)) 7445 return StmtError(); 7446 E = Checker.getExpr(); 7447 X = Checker.getX(); 7448 UE = Checker.getUpdateExpr(); 7449 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 7450 IsPostfixUpdate = Checker.isPostfixUpdate(); 7451 } else if (!AtomicBody->isInstantiationDependent()) { 7452 ErrorLoc = AtomicBody->getExprLoc(); 7453 ErrorRange = AtomicBody->getSourceRange(); 7454 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 7455 : AtomicBody->getExprLoc(); 7456 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 7457 : AtomicBody->getSourceRange(); 7458 ErrorFound = NotAnAssignmentOp; 7459 } 7460 if (ErrorFound != NoError) { 7461 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 7462 << ErrorRange; 7463 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 7464 return StmtError(); 7465 } 7466 if (CurContext->isDependentContext()) 7467 UE = V = E = X = nullptr; 7468 } else { 7469 // If clause is a capture: 7470 // { v = x; x = expr; } 7471 // { v = x; x++; } 7472 // { v = x; x--; } 7473 // { v = x; ++x; } 7474 // { v = x; --x; } 7475 // { v = x; x binop= expr; } 7476 // { v = x; x = x binop expr; } 7477 // { v = x; x = expr binop x; } 7478 // { x++; v = x; } 7479 // { x--; v = x; } 7480 // { ++x; v = x; } 7481 // { --x; v = x; } 7482 // { x binop= expr; v = x; } 7483 // { x = x binop expr; v = x; } 7484 // { x = expr binop x; v = x; } 7485 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 7486 // Check that this is { expr1; expr2; } 7487 if (CS->size() == 2) { 7488 Stmt *First = CS->body_front(); 7489 Stmt *Second = CS->body_back(); 7490 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 7491 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 7492 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 7493 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 7494 // Need to find what subexpression is 'v' and what is 'x'. 7495 OpenMPAtomicUpdateChecker Checker(*this); 7496 bool IsUpdateExprFound = !Checker.checkStatement(Second); 7497 BinaryOperator *BinOp = nullptr; 7498 if (IsUpdateExprFound) { 7499 BinOp = dyn_cast<BinaryOperator>(First); 7500 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 7501 } 7502 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 7503 // { v = x; x++; } 7504 // { v = x; x--; } 7505 // { v = x; ++x; } 7506 // { v = x; --x; } 7507 // { v = x; x binop= expr; } 7508 // { v = x; x = x binop expr; } 7509 // { v = x; x = expr binop x; } 7510 // Check that the first expression has form v = x. 7511 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 7512 llvm::FoldingSetNodeID XId, PossibleXId; 7513 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 7514 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 7515 IsUpdateExprFound = XId == PossibleXId; 7516 if (IsUpdateExprFound) { 7517 V = BinOp->getLHS(); 7518 X = Checker.getX(); 7519 E = Checker.getExpr(); 7520 UE = Checker.getUpdateExpr(); 7521 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 7522 IsPostfixUpdate = true; 7523 } 7524 } 7525 if (!IsUpdateExprFound) { 7526 IsUpdateExprFound = !Checker.checkStatement(First); 7527 BinOp = nullptr; 7528 if (IsUpdateExprFound) { 7529 BinOp = dyn_cast<BinaryOperator>(Second); 7530 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 7531 } 7532 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 7533 // { x++; v = x; } 7534 // { x--; v = x; } 7535 // { ++x; v = x; } 7536 // { --x; v = x; } 7537 // { x binop= expr; v = x; } 7538 // { x = x binop expr; v = x; } 7539 // { x = expr binop x; v = x; } 7540 // Check that the second expression has form v = x. 7541 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 7542 llvm::FoldingSetNodeID XId, PossibleXId; 7543 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 7544 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 7545 IsUpdateExprFound = XId == PossibleXId; 7546 if (IsUpdateExprFound) { 7547 V = BinOp->getLHS(); 7548 X = Checker.getX(); 7549 E = Checker.getExpr(); 7550 UE = Checker.getUpdateExpr(); 7551 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 7552 IsPostfixUpdate = false; 7553 } 7554 } 7555 } 7556 if (!IsUpdateExprFound) { 7557 // { v = x; x = expr; } 7558 auto *FirstExpr = dyn_cast<Expr>(First); 7559 auto *SecondExpr = dyn_cast<Expr>(Second); 7560 if (!FirstExpr || !SecondExpr || 7561 !(FirstExpr->isInstantiationDependent() || 7562 SecondExpr->isInstantiationDependent())) { 7563 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 7564 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 7565 ErrorFound = NotAnAssignmentOp; 7566 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 7567 : First->getBeginLoc(); 7568 NoteRange = ErrorRange = FirstBinOp 7569 ? FirstBinOp->getSourceRange() 7570 : SourceRange(ErrorLoc, ErrorLoc); 7571 } else { 7572 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 7573 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 7574 ErrorFound = NotAnAssignmentOp; 7575 NoteLoc = ErrorLoc = SecondBinOp 7576 ? SecondBinOp->getOperatorLoc() 7577 : Second->getBeginLoc(); 7578 NoteRange = ErrorRange = 7579 SecondBinOp ? SecondBinOp->getSourceRange() 7580 : SourceRange(ErrorLoc, ErrorLoc); 7581 } else { 7582 Expr *PossibleXRHSInFirst = 7583 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 7584 Expr *PossibleXLHSInSecond = 7585 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 7586 llvm::FoldingSetNodeID X1Id, X2Id; 7587 PossibleXRHSInFirst->Profile(X1Id, Context, 7588 /*Canonical=*/true); 7589 PossibleXLHSInSecond->Profile(X2Id, Context, 7590 /*Canonical=*/true); 7591 IsUpdateExprFound = X1Id == X2Id; 7592 if (IsUpdateExprFound) { 7593 V = FirstBinOp->getLHS(); 7594 X = SecondBinOp->getLHS(); 7595 E = SecondBinOp->getRHS(); 7596 UE = nullptr; 7597 IsXLHSInRHSPart = false; 7598 IsPostfixUpdate = true; 7599 } else { 7600 ErrorFound = NotASpecificExpression; 7601 ErrorLoc = FirstBinOp->getExprLoc(); 7602 ErrorRange = FirstBinOp->getSourceRange(); 7603 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 7604 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 7605 } 7606 } 7607 } 7608 } 7609 } 7610 } else { 7611 NoteLoc = ErrorLoc = Body->getBeginLoc(); 7612 NoteRange = ErrorRange = 7613 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 7614 ErrorFound = NotTwoSubstatements; 7615 } 7616 } else { 7617 NoteLoc = ErrorLoc = Body->getBeginLoc(); 7618 NoteRange = ErrorRange = 7619 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 7620 ErrorFound = NotACompoundStatement; 7621 } 7622 if (ErrorFound != NoError) { 7623 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 7624 << ErrorRange; 7625 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 7626 return StmtError(); 7627 } 7628 if (CurContext->isDependentContext()) 7629 UE = V = E = X = nullptr; 7630 } 7631 } 7632 7633 setFunctionHasBranchProtectedScope(); 7634 7635 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 7636 X, V, E, UE, IsXLHSInRHSPart, 7637 IsPostfixUpdate); 7638 } 7639 7640 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 7641 Stmt *AStmt, 7642 SourceLocation StartLoc, 7643 SourceLocation EndLoc) { 7644 if (!AStmt) 7645 return StmtError(); 7646 7647 auto *CS = cast<CapturedStmt>(AStmt); 7648 // 1.2.2 OpenMP Language Terminology 7649 // Structured block - An executable statement with a single entry at the 7650 // top and a single exit at the bottom. 7651 // The point of exit cannot be a branch out of the structured block. 7652 // longjmp() and throw() must not violate the entry/exit criteria. 7653 CS->getCapturedDecl()->setNothrow(); 7654 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 7655 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7656 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7657 // 1.2.2 OpenMP Language Terminology 7658 // Structured block - An executable statement with a single entry at the 7659 // top and a single exit at the bottom. 7660 // The point of exit cannot be a branch out of the structured block. 7661 // longjmp() and throw() must not violate the entry/exit criteria. 7662 CS->getCapturedDecl()->setNothrow(); 7663 } 7664 7665 // OpenMP [2.16, Nesting of Regions] 7666 // If specified, a teams construct must be contained within a target 7667 // construct. That target construct must contain no statements or directives 7668 // outside of the teams construct. 7669 if (DSAStack->hasInnerTeamsRegion()) { 7670 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 7671 bool OMPTeamsFound = true; 7672 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 7673 auto I = CS->body_begin(); 7674 while (I != CS->body_end()) { 7675 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 7676 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) || 7677 OMPTeamsFound) { 7678 7679 OMPTeamsFound = false; 7680 break; 7681 } 7682 ++I; 7683 } 7684 assert(I != CS->body_end() && "Not found statement"); 7685 S = *I; 7686 } else { 7687 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 7688 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 7689 } 7690 if (!OMPTeamsFound) { 7691 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 7692 Diag(DSAStack->getInnerTeamsRegionLoc(), 7693 diag::note_omp_nested_teams_construct_here); 7694 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here) 7695 << isa<OMPExecutableDirective>(S); 7696 return StmtError(); 7697 } 7698 } 7699 7700 setFunctionHasBranchProtectedScope(); 7701 7702 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 7703 } 7704 7705 StmtResult 7706 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 7707 Stmt *AStmt, SourceLocation StartLoc, 7708 SourceLocation EndLoc) { 7709 if (!AStmt) 7710 return StmtError(); 7711 7712 auto *CS = cast<CapturedStmt>(AStmt); 7713 // 1.2.2 OpenMP Language Terminology 7714 // Structured block - An executable statement with a single entry at the 7715 // top and a single exit at the bottom. 7716 // The point of exit cannot be a branch out of the structured block. 7717 // longjmp() and throw() must not violate the entry/exit criteria. 7718 CS->getCapturedDecl()->setNothrow(); 7719 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 7720 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7721 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7722 // 1.2.2 OpenMP Language Terminology 7723 // Structured block - An executable statement with a single entry at the 7724 // top and a single exit at the bottom. 7725 // The point of exit cannot be a branch out of the structured block. 7726 // longjmp() and throw() must not violate the entry/exit criteria. 7727 CS->getCapturedDecl()->setNothrow(); 7728 } 7729 7730 setFunctionHasBranchProtectedScope(); 7731 7732 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, 7733 AStmt); 7734 } 7735 7736 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 7737 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7738 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7739 if (!AStmt) 7740 return StmtError(); 7741 7742 auto *CS = cast<CapturedStmt>(AStmt); 7743 // 1.2.2 OpenMP Language Terminology 7744 // Structured block - An executable statement with a single entry at the 7745 // top and a single exit at the bottom. 7746 // The point of exit cannot be a branch out of the structured block. 7747 // longjmp() and throw() must not violate the entry/exit criteria. 7748 CS->getCapturedDecl()->setNothrow(); 7749 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 7750 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7751 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7752 // 1.2.2 OpenMP Language Terminology 7753 // Structured block - An executable statement with a single entry at the 7754 // top and a single exit at the bottom. 7755 // The point of exit cannot be a branch out of the structured block. 7756 // longjmp() and throw() must not violate the entry/exit criteria. 7757 CS->getCapturedDecl()->setNothrow(); 7758 } 7759 7760 OMPLoopDirective::HelperExprs B; 7761 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 7762 // define the nested loops number. 7763 unsigned NestedLoopCount = 7764 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 7765 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 7766 VarsWithImplicitDSA, B); 7767 if (NestedLoopCount == 0) 7768 return StmtError(); 7769 7770 assert((CurContext->isDependentContext() || B.builtAll()) && 7771 "omp target parallel for loop exprs were not built"); 7772 7773 if (!CurContext->isDependentContext()) { 7774 // Finalize the clauses that need pre-built expressions for CodeGen. 7775 for (OMPClause *C : Clauses) { 7776 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7777 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7778 B.NumIterations, *this, CurScope, 7779 DSAStack)) 7780 return StmtError(); 7781 } 7782 } 7783 7784 setFunctionHasBranchProtectedScope(); 7785 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc, 7786 NestedLoopCount, Clauses, AStmt, 7787 B, DSAStack->isCancelRegion()); 7788 } 7789 7790 /// Check for existence of a map clause in the list of clauses. 7791 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 7792 const OpenMPClauseKind K) { 7793 return llvm::any_of( 7794 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 7795 } 7796 7797 template <typename... Params> 7798 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 7799 const Params... ClauseTypes) { 7800 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 7801 } 7802 7803 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 7804 Stmt *AStmt, 7805 SourceLocation StartLoc, 7806 SourceLocation EndLoc) { 7807 if (!AStmt) 7808 return StmtError(); 7809 7810 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 7811 7812 // OpenMP [2.10.1, Restrictions, p. 97] 7813 // At least one map clause must appear on the directive. 7814 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) { 7815 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 7816 << "'map' or 'use_device_ptr'" 7817 << getOpenMPDirectiveName(OMPD_target_data); 7818 return StmtError(); 7819 } 7820 7821 setFunctionHasBranchProtectedScope(); 7822 7823 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 7824 AStmt); 7825 } 7826 7827 StmtResult 7828 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 7829 SourceLocation StartLoc, 7830 SourceLocation EndLoc, Stmt *AStmt) { 7831 if (!AStmt) 7832 return StmtError(); 7833 7834 auto *CS = cast<CapturedStmt>(AStmt); 7835 // 1.2.2 OpenMP Language Terminology 7836 // Structured block - An executable statement with a single entry at the 7837 // top and a single exit at the bottom. 7838 // The point of exit cannot be a branch out of the structured block. 7839 // longjmp() and throw() must not violate the entry/exit criteria. 7840 CS->getCapturedDecl()->setNothrow(); 7841 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 7842 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7843 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7844 // 1.2.2 OpenMP Language Terminology 7845 // Structured block - An executable statement with a single entry at the 7846 // top and a single exit at the bottom. 7847 // The point of exit cannot be a branch out of the structured block. 7848 // longjmp() and throw() must not violate the entry/exit criteria. 7849 CS->getCapturedDecl()->setNothrow(); 7850 } 7851 7852 // OpenMP [2.10.2, Restrictions, p. 99] 7853 // At least one map clause must appear on the directive. 7854 if (!hasClauses(Clauses, OMPC_map)) { 7855 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 7856 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 7857 return StmtError(); 7858 } 7859 7860 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 7861 AStmt); 7862 } 7863 7864 StmtResult 7865 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 7866 SourceLocation StartLoc, 7867 SourceLocation EndLoc, Stmt *AStmt) { 7868 if (!AStmt) 7869 return StmtError(); 7870 7871 auto *CS = cast<CapturedStmt>(AStmt); 7872 // 1.2.2 OpenMP Language Terminology 7873 // Structured block - An executable statement with a single entry at the 7874 // top and a single exit at the bottom. 7875 // The point of exit cannot be a branch out of the structured block. 7876 // longjmp() and throw() must not violate the entry/exit criteria. 7877 CS->getCapturedDecl()->setNothrow(); 7878 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 7879 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7880 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7881 // 1.2.2 OpenMP Language Terminology 7882 // Structured block - An executable statement with a single entry at the 7883 // top and a single exit at the bottom. 7884 // The point of exit cannot be a branch out of the structured block. 7885 // longjmp() and throw() must not violate the entry/exit criteria. 7886 CS->getCapturedDecl()->setNothrow(); 7887 } 7888 7889 // OpenMP [2.10.3, Restrictions, p. 102] 7890 // At least one map clause must appear on the directive. 7891 if (!hasClauses(Clauses, OMPC_map)) { 7892 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 7893 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 7894 return StmtError(); 7895 } 7896 7897 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 7898 AStmt); 7899 } 7900 7901 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 7902 SourceLocation StartLoc, 7903 SourceLocation EndLoc, 7904 Stmt *AStmt) { 7905 if (!AStmt) 7906 return StmtError(); 7907 7908 auto *CS = cast<CapturedStmt>(AStmt); 7909 // 1.2.2 OpenMP Language Terminology 7910 // Structured block - An executable statement with a single entry at the 7911 // top and a single exit at the bottom. 7912 // The point of exit cannot be a branch out of the structured block. 7913 // longjmp() and throw() must not violate the entry/exit criteria. 7914 CS->getCapturedDecl()->setNothrow(); 7915 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 7916 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7917 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7918 // 1.2.2 OpenMP Language Terminology 7919 // Structured block - An executable statement with a single entry at the 7920 // top and a single exit at the bottom. 7921 // The point of exit cannot be a branch out of the structured block. 7922 // longjmp() and throw() must not violate the entry/exit criteria. 7923 CS->getCapturedDecl()->setNothrow(); 7924 } 7925 7926 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 7927 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 7928 return StmtError(); 7929 } 7930 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 7931 AStmt); 7932 } 7933 7934 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 7935 Stmt *AStmt, SourceLocation StartLoc, 7936 SourceLocation EndLoc) { 7937 if (!AStmt) 7938 return StmtError(); 7939 7940 auto *CS = cast<CapturedStmt>(AStmt); 7941 // 1.2.2 OpenMP Language Terminology 7942 // Structured block - An executable statement with a single entry at the 7943 // top and a single exit at the bottom. 7944 // The point of exit cannot be a branch out of the structured block. 7945 // longjmp() and throw() must not violate the entry/exit criteria. 7946 CS->getCapturedDecl()->setNothrow(); 7947 7948 setFunctionHasBranchProtectedScope(); 7949 7950 DSAStack->setParentTeamsRegionLoc(StartLoc); 7951 7952 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 7953 } 7954 7955 StmtResult 7956 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 7957 SourceLocation EndLoc, 7958 OpenMPDirectiveKind CancelRegion) { 7959 if (DSAStack->isParentNowaitRegion()) { 7960 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 7961 return StmtError(); 7962 } 7963 if (DSAStack->isParentOrderedRegion()) { 7964 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 7965 return StmtError(); 7966 } 7967 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 7968 CancelRegion); 7969 } 7970 7971 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 7972 SourceLocation StartLoc, 7973 SourceLocation EndLoc, 7974 OpenMPDirectiveKind CancelRegion) { 7975 if (DSAStack->isParentNowaitRegion()) { 7976 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 7977 return StmtError(); 7978 } 7979 if (DSAStack->isParentOrderedRegion()) { 7980 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 7981 return StmtError(); 7982 } 7983 DSAStack->setParentCancelRegion(/*Cancel=*/true); 7984 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 7985 CancelRegion); 7986 } 7987 7988 static bool checkGrainsizeNumTasksClauses(Sema &S, 7989 ArrayRef<OMPClause *> Clauses) { 7990 const OMPClause *PrevClause = nullptr; 7991 bool ErrorFound = false; 7992 for (const OMPClause *C : Clauses) { 7993 if (C->getClauseKind() == OMPC_grainsize || 7994 C->getClauseKind() == OMPC_num_tasks) { 7995 if (!PrevClause) 7996 PrevClause = C; 7997 else if (PrevClause->getClauseKind() != C->getClauseKind()) { 7998 S.Diag(C->getBeginLoc(), 7999 diag::err_omp_grainsize_num_tasks_mutually_exclusive) 8000 << getOpenMPClauseName(C->getClauseKind()) 8001 << getOpenMPClauseName(PrevClause->getClauseKind()); 8002 S.Diag(PrevClause->getBeginLoc(), 8003 diag::note_omp_previous_grainsize_num_tasks) 8004 << getOpenMPClauseName(PrevClause->getClauseKind()); 8005 ErrorFound = true; 8006 } 8007 } 8008 } 8009 return ErrorFound; 8010 } 8011 8012 static bool checkReductionClauseWithNogroup(Sema &S, 8013 ArrayRef<OMPClause *> Clauses) { 8014 const OMPClause *ReductionClause = nullptr; 8015 const OMPClause *NogroupClause = nullptr; 8016 for (const OMPClause *C : Clauses) { 8017 if (C->getClauseKind() == OMPC_reduction) { 8018 ReductionClause = C; 8019 if (NogroupClause) 8020 break; 8021 continue; 8022 } 8023 if (C->getClauseKind() == OMPC_nogroup) { 8024 NogroupClause = C; 8025 if (ReductionClause) 8026 break; 8027 continue; 8028 } 8029 } 8030 if (ReductionClause && NogroupClause) { 8031 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup) 8032 << SourceRange(NogroupClause->getBeginLoc(), 8033 NogroupClause->getEndLoc()); 8034 return true; 8035 } 8036 return false; 8037 } 8038 8039 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 8040 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8041 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8042 if (!AStmt) 8043 return StmtError(); 8044 8045 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8046 OMPLoopDirective::HelperExprs B; 8047 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8048 // define the nested loops number. 8049 unsigned NestedLoopCount = 8050 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 8051 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 8052 VarsWithImplicitDSA, B); 8053 if (NestedLoopCount == 0) 8054 return StmtError(); 8055 8056 assert((CurContext->isDependentContext() || B.builtAll()) && 8057 "omp for loop exprs were not built"); 8058 8059 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 8060 // The grainsize clause and num_tasks clause are mutually exclusive and may 8061 // not appear on the same taskloop directive. 8062 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 8063 return StmtError(); 8064 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 8065 // If a reduction clause is present on the taskloop directive, the nogroup 8066 // clause must not be specified. 8067 if (checkReductionClauseWithNogroup(*this, Clauses)) 8068 return StmtError(); 8069 8070 setFunctionHasBranchProtectedScope(); 8071 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 8072 NestedLoopCount, Clauses, AStmt, B); 8073 } 8074 8075 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 8076 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8077 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8078 if (!AStmt) 8079 return StmtError(); 8080 8081 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8082 OMPLoopDirective::HelperExprs B; 8083 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8084 // define the nested loops number. 8085 unsigned NestedLoopCount = 8086 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 8087 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 8088 VarsWithImplicitDSA, B); 8089 if (NestedLoopCount == 0) 8090 return StmtError(); 8091 8092 assert((CurContext->isDependentContext() || B.builtAll()) && 8093 "omp for loop exprs were not built"); 8094 8095 if (!CurContext->isDependentContext()) { 8096 // Finalize the clauses that need pre-built expressions for CodeGen. 8097 for (OMPClause *C : Clauses) { 8098 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8099 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8100 B.NumIterations, *this, CurScope, 8101 DSAStack)) 8102 return StmtError(); 8103 } 8104 } 8105 8106 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 8107 // The grainsize clause and num_tasks clause are mutually exclusive and may 8108 // not appear on the same taskloop directive. 8109 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 8110 return StmtError(); 8111 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 8112 // If a reduction clause is present on the taskloop directive, the nogroup 8113 // clause must not be specified. 8114 if (checkReductionClauseWithNogroup(*this, Clauses)) 8115 return StmtError(); 8116 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8117 return StmtError(); 8118 8119 setFunctionHasBranchProtectedScope(); 8120 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 8121 NestedLoopCount, Clauses, AStmt, B); 8122 } 8123 8124 StmtResult Sema::ActOnOpenMPDistributeDirective( 8125 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8126 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8127 if (!AStmt) 8128 return StmtError(); 8129 8130 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8131 OMPLoopDirective::HelperExprs B; 8132 // In presence of clause 'collapse' with number of loops, it will 8133 // define the nested loops number. 8134 unsigned NestedLoopCount = 8135 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 8136 nullptr /*ordered not a clause on distribute*/, AStmt, 8137 *this, *DSAStack, VarsWithImplicitDSA, B); 8138 if (NestedLoopCount == 0) 8139 return StmtError(); 8140 8141 assert((CurContext->isDependentContext() || B.builtAll()) && 8142 "omp for loop exprs were not built"); 8143 8144 setFunctionHasBranchProtectedScope(); 8145 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 8146 NestedLoopCount, Clauses, AStmt, B); 8147 } 8148 8149 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 8150 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8151 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8152 if (!AStmt) 8153 return StmtError(); 8154 8155 auto *CS = cast<CapturedStmt>(AStmt); 8156 // 1.2.2 OpenMP Language Terminology 8157 // Structured block - An executable statement with a single entry at the 8158 // top and a single exit at the bottom. 8159 // The point of exit cannot be a branch out of the structured block. 8160 // longjmp() and throw() must not violate the entry/exit criteria. 8161 CS->getCapturedDecl()->setNothrow(); 8162 for (int ThisCaptureLevel = 8163 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 8164 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8165 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8166 // 1.2.2 OpenMP Language Terminology 8167 // Structured block - An executable statement with a single entry at the 8168 // top and a single exit at the bottom. 8169 // The point of exit cannot be a branch out of the structured block. 8170 // longjmp() and throw() must not violate the entry/exit criteria. 8171 CS->getCapturedDecl()->setNothrow(); 8172 } 8173 8174 OMPLoopDirective::HelperExprs B; 8175 // In presence of clause 'collapse' with number of loops, it will 8176 // define the nested loops number. 8177 unsigned NestedLoopCount = checkOpenMPLoop( 8178 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 8179 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8180 VarsWithImplicitDSA, B); 8181 if (NestedLoopCount == 0) 8182 return StmtError(); 8183 8184 assert((CurContext->isDependentContext() || B.builtAll()) && 8185 "omp for loop exprs were not built"); 8186 8187 setFunctionHasBranchProtectedScope(); 8188 return OMPDistributeParallelForDirective::Create( 8189 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 8190 DSAStack->isCancelRegion()); 8191 } 8192 8193 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 8194 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8195 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8196 if (!AStmt) 8197 return StmtError(); 8198 8199 auto *CS = cast<CapturedStmt>(AStmt); 8200 // 1.2.2 OpenMP Language Terminology 8201 // Structured block - An executable statement with a single entry at the 8202 // top and a single exit at the bottom. 8203 // The point of exit cannot be a branch out of the structured block. 8204 // longjmp() and throw() must not violate the entry/exit criteria. 8205 CS->getCapturedDecl()->setNothrow(); 8206 for (int ThisCaptureLevel = 8207 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 8208 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8209 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8210 // 1.2.2 OpenMP Language Terminology 8211 // Structured block - An executable statement with a single entry at the 8212 // top and a single exit at the bottom. 8213 // The point of exit cannot be a branch out of the structured block. 8214 // longjmp() and throw() must not violate the entry/exit criteria. 8215 CS->getCapturedDecl()->setNothrow(); 8216 } 8217 8218 OMPLoopDirective::HelperExprs B; 8219 // In presence of clause 'collapse' with number of loops, it will 8220 // define the nested loops number. 8221 unsigned NestedLoopCount = checkOpenMPLoop( 8222 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 8223 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8224 VarsWithImplicitDSA, B); 8225 if (NestedLoopCount == 0) 8226 return StmtError(); 8227 8228 assert((CurContext->isDependentContext() || B.builtAll()) && 8229 "omp for loop exprs were not built"); 8230 8231 if (!CurContext->isDependentContext()) { 8232 // Finalize the clauses that need pre-built expressions for CodeGen. 8233 for (OMPClause *C : Clauses) { 8234 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8235 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8236 B.NumIterations, *this, CurScope, 8237 DSAStack)) 8238 return StmtError(); 8239 } 8240 } 8241 8242 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8243 return StmtError(); 8244 8245 setFunctionHasBranchProtectedScope(); 8246 return OMPDistributeParallelForSimdDirective::Create( 8247 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8248 } 8249 8250 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 8251 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8252 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8253 if (!AStmt) 8254 return StmtError(); 8255 8256 auto *CS = cast<CapturedStmt>(AStmt); 8257 // 1.2.2 OpenMP Language Terminology 8258 // Structured block - An executable statement with a single entry at the 8259 // top and a single exit at the bottom. 8260 // The point of exit cannot be a branch out of the structured block. 8261 // longjmp() and throw() must not violate the entry/exit criteria. 8262 CS->getCapturedDecl()->setNothrow(); 8263 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 8264 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8265 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8266 // 1.2.2 OpenMP Language Terminology 8267 // Structured block - An executable statement with a single entry at the 8268 // top and a single exit at the bottom. 8269 // The point of exit cannot be a branch out of the structured block. 8270 // longjmp() and throw() must not violate the entry/exit criteria. 8271 CS->getCapturedDecl()->setNothrow(); 8272 } 8273 8274 OMPLoopDirective::HelperExprs B; 8275 // In presence of clause 'collapse' with number of loops, it will 8276 // define the nested loops number. 8277 unsigned NestedLoopCount = 8278 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 8279 nullptr /*ordered not a clause on distribute*/, CS, *this, 8280 *DSAStack, VarsWithImplicitDSA, B); 8281 if (NestedLoopCount == 0) 8282 return StmtError(); 8283 8284 assert((CurContext->isDependentContext() || B.builtAll()) && 8285 "omp for loop exprs were not built"); 8286 8287 if (!CurContext->isDependentContext()) { 8288 // Finalize the clauses that need pre-built expressions for CodeGen. 8289 for (OMPClause *C : Clauses) { 8290 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8291 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8292 B.NumIterations, *this, CurScope, 8293 DSAStack)) 8294 return StmtError(); 8295 } 8296 } 8297 8298 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8299 return StmtError(); 8300 8301 setFunctionHasBranchProtectedScope(); 8302 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 8303 NestedLoopCount, Clauses, AStmt, B); 8304 } 8305 8306 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 8307 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8308 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8309 if (!AStmt) 8310 return StmtError(); 8311 8312 auto *CS = cast<CapturedStmt>(AStmt); 8313 // 1.2.2 OpenMP Language Terminology 8314 // Structured block - An executable statement with a single entry at the 8315 // top and a single exit at the bottom. 8316 // The point of exit cannot be a branch out of the structured block. 8317 // longjmp() and throw() must not violate the entry/exit criteria. 8318 CS->getCapturedDecl()->setNothrow(); 8319 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 8320 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8321 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8322 // 1.2.2 OpenMP Language Terminology 8323 // Structured block - An executable statement with a single entry at the 8324 // top and a single exit at the bottom. 8325 // The point of exit cannot be a branch out of the structured block. 8326 // longjmp() and throw() must not violate the entry/exit criteria. 8327 CS->getCapturedDecl()->setNothrow(); 8328 } 8329 8330 OMPLoopDirective::HelperExprs B; 8331 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8332 // define the nested loops number. 8333 unsigned NestedLoopCount = checkOpenMPLoop( 8334 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 8335 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 8336 VarsWithImplicitDSA, B); 8337 if (NestedLoopCount == 0) 8338 return StmtError(); 8339 8340 assert((CurContext->isDependentContext() || B.builtAll()) && 8341 "omp target parallel for simd loop exprs were not built"); 8342 8343 if (!CurContext->isDependentContext()) { 8344 // Finalize the clauses that need pre-built expressions for CodeGen. 8345 for (OMPClause *C : Clauses) { 8346 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8347 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8348 B.NumIterations, *this, CurScope, 8349 DSAStack)) 8350 return StmtError(); 8351 } 8352 } 8353 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8354 return StmtError(); 8355 8356 setFunctionHasBranchProtectedScope(); 8357 return OMPTargetParallelForSimdDirective::Create( 8358 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8359 } 8360 8361 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 8362 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8363 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8364 if (!AStmt) 8365 return StmtError(); 8366 8367 auto *CS = cast<CapturedStmt>(AStmt); 8368 // 1.2.2 OpenMP Language Terminology 8369 // Structured block - An executable statement with a single entry at the 8370 // top and a single exit at the bottom. 8371 // The point of exit cannot be a branch out of the structured block. 8372 // longjmp() and throw() must not violate the entry/exit criteria. 8373 CS->getCapturedDecl()->setNothrow(); 8374 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 8375 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8376 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8377 // 1.2.2 OpenMP Language Terminology 8378 // Structured block - An executable statement with a single entry at the 8379 // top and a single exit at the bottom. 8380 // The point of exit cannot be a branch out of the structured block. 8381 // longjmp() and throw() must not violate the entry/exit criteria. 8382 CS->getCapturedDecl()->setNothrow(); 8383 } 8384 8385 OMPLoopDirective::HelperExprs B; 8386 // In presence of clause 'collapse' with number of loops, it will define the 8387 // nested loops number. 8388 unsigned NestedLoopCount = 8389 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 8390 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 8391 VarsWithImplicitDSA, B); 8392 if (NestedLoopCount == 0) 8393 return StmtError(); 8394 8395 assert((CurContext->isDependentContext() || B.builtAll()) && 8396 "omp target simd loop exprs were not built"); 8397 8398 if (!CurContext->isDependentContext()) { 8399 // Finalize the clauses that need pre-built expressions for CodeGen. 8400 for (OMPClause *C : Clauses) { 8401 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8402 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8403 B.NumIterations, *this, CurScope, 8404 DSAStack)) 8405 return StmtError(); 8406 } 8407 } 8408 8409 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8410 return StmtError(); 8411 8412 setFunctionHasBranchProtectedScope(); 8413 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 8414 NestedLoopCount, Clauses, AStmt, B); 8415 } 8416 8417 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 8418 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8419 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8420 if (!AStmt) 8421 return StmtError(); 8422 8423 auto *CS = cast<CapturedStmt>(AStmt); 8424 // 1.2.2 OpenMP Language Terminology 8425 // Structured block - An executable statement with a single entry at the 8426 // top and a single exit at the bottom. 8427 // The point of exit cannot be a branch out of the structured block. 8428 // longjmp() and throw() must not violate the entry/exit criteria. 8429 CS->getCapturedDecl()->setNothrow(); 8430 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 8431 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8432 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8433 // 1.2.2 OpenMP Language Terminology 8434 // Structured block - An executable statement with a single entry at the 8435 // top and a single exit at the bottom. 8436 // The point of exit cannot be a branch out of the structured block. 8437 // longjmp() and throw() must not violate the entry/exit criteria. 8438 CS->getCapturedDecl()->setNothrow(); 8439 } 8440 8441 OMPLoopDirective::HelperExprs B; 8442 // In presence of clause 'collapse' with number of loops, it will 8443 // define the nested loops number. 8444 unsigned NestedLoopCount = 8445 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 8446 nullptr /*ordered not a clause on distribute*/, CS, *this, 8447 *DSAStack, VarsWithImplicitDSA, B); 8448 if (NestedLoopCount == 0) 8449 return StmtError(); 8450 8451 assert((CurContext->isDependentContext() || B.builtAll()) && 8452 "omp teams distribute loop exprs were not built"); 8453 8454 setFunctionHasBranchProtectedScope(); 8455 8456 DSAStack->setParentTeamsRegionLoc(StartLoc); 8457 8458 return OMPTeamsDistributeDirective::Create( 8459 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8460 } 8461 8462 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 8463 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8464 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8465 if (!AStmt) 8466 return StmtError(); 8467 8468 auto *CS = cast<CapturedStmt>(AStmt); 8469 // 1.2.2 OpenMP Language Terminology 8470 // Structured block - An executable statement with a single entry at the 8471 // top and a single exit at the bottom. 8472 // The point of exit cannot be a branch out of the structured block. 8473 // longjmp() and throw() must not violate the entry/exit criteria. 8474 CS->getCapturedDecl()->setNothrow(); 8475 for (int ThisCaptureLevel = 8476 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 8477 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8478 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8479 // 1.2.2 OpenMP Language Terminology 8480 // Structured block - An executable statement with a single entry at the 8481 // top and a single exit at the bottom. 8482 // The point of exit cannot be a branch out of the structured block. 8483 // longjmp() and throw() must not violate the entry/exit criteria. 8484 CS->getCapturedDecl()->setNothrow(); 8485 } 8486 8487 8488 OMPLoopDirective::HelperExprs B; 8489 // In presence of clause 'collapse' with number of loops, it will 8490 // define the nested loops number. 8491 unsigned NestedLoopCount = checkOpenMPLoop( 8492 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 8493 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8494 VarsWithImplicitDSA, B); 8495 8496 if (NestedLoopCount == 0) 8497 return StmtError(); 8498 8499 assert((CurContext->isDependentContext() || B.builtAll()) && 8500 "omp teams distribute simd loop exprs were not built"); 8501 8502 if (!CurContext->isDependentContext()) { 8503 // Finalize the clauses that need pre-built expressions for CodeGen. 8504 for (OMPClause *C : Clauses) { 8505 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8506 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8507 B.NumIterations, *this, CurScope, 8508 DSAStack)) 8509 return StmtError(); 8510 } 8511 } 8512 8513 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8514 return StmtError(); 8515 8516 setFunctionHasBranchProtectedScope(); 8517 8518 DSAStack->setParentTeamsRegionLoc(StartLoc); 8519 8520 return OMPTeamsDistributeSimdDirective::Create( 8521 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8522 } 8523 8524 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 8525 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8526 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8527 if (!AStmt) 8528 return StmtError(); 8529 8530 auto *CS = cast<CapturedStmt>(AStmt); 8531 // 1.2.2 OpenMP Language Terminology 8532 // Structured block - An executable statement with a single entry at the 8533 // top and a single exit at the bottom. 8534 // The point of exit cannot be a branch out of the structured block. 8535 // longjmp() and throw() must not violate the entry/exit criteria. 8536 CS->getCapturedDecl()->setNothrow(); 8537 8538 for (int ThisCaptureLevel = 8539 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 8540 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8541 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8542 // 1.2.2 OpenMP Language Terminology 8543 // Structured block - An executable statement with a single entry at the 8544 // top and a single exit at the bottom. 8545 // The point of exit cannot be a branch out of the structured block. 8546 // longjmp() and throw() must not violate the entry/exit criteria. 8547 CS->getCapturedDecl()->setNothrow(); 8548 } 8549 8550 OMPLoopDirective::HelperExprs B; 8551 // In presence of clause 'collapse' with number of loops, it will 8552 // define the nested loops number. 8553 unsigned NestedLoopCount = checkOpenMPLoop( 8554 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 8555 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8556 VarsWithImplicitDSA, B); 8557 8558 if (NestedLoopCount == 0) 8559 return StmtError(); 8560 8561 assert((CurContext->isDependentContext() || B.builtAll()) && 8562 "omp for loop exprs were not built"); 8563 8564 if (!CurContext->isDependentContext()) { 8565 // Finalize the clauses that need pre-built expressions for CodeGen. 8566 for (OMPClause *C : Clauses) { 8567 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8568 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8569 B.NumIterations, *this, CurScope, 8570 DSAStack)) 8571 return StmtError(); 8572 } 8573 } 8574 8575 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8576 return StmtError(); 8577 8578 setFunctionHasBranchProtectedScope(); 8579 8580 DSAStack->setParentTeamsRegionLoc(StartLoc); 8581 8582 return OMPTeamsDistributeParallelForSimdDirective::Create( 8583 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8584 } 8585 8586 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 8587 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8588 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8589 if (!AStmt) 8590 return StmtError(); 8591 8592 auto *CS = cast<CapturedStmt>(AStmt); 8593 // 1.2.2 OpenMP Language Terminology 8594 // Structured block - An executable statement with a single entry at the 8595 // top and a single exit at the bottom. 8596 // The point of exit cannot be a branch out of the structured block. 8597 // longjmp() and throw() must not violate the entry/exit criteria. 8598 CS->getCapturedDecl()->setNothrow(); 8599 8600 for (int ThisCaptureLevel = 8601 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 8602 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8603 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8604 // 1.2.2 OpenMP Language Terminology 8605 // Structured block - An executable statement with a single entry at the 8606 // top and a single exit at the bottom. 8607 // The point of exit cannot be a branch out of the structured block. 8608 // longjmp() and throw() must not violate the entry/exit criteria. 8609 CS->getCapturedDecl()->setNothrow(); 8610 } 8611 8612 OMPLoopDirective::HelperExprs B; 8613 // In presence of clause 'collapse' with number of loops, it will 8614 // define the nested loops number. 8615 unsigned NestedLoopCount = checkOpenMPLoop( 8616 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 8617 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8618 VarsWithImplicitDSA, B); 8619 8620 if (NestedLoopCount == 0) 8621 return StmtError(); 8622 8623 assert((CurContext->isDependentContext() || B.builtAll()) && 8624 "omp for loop exprs were not built"); 8625 8626 setFunctionHasBranchProtectedScope(); 8627 8628 DSAStack->setParentTeamsRegionLoc(StartLoc); 8629 8630 return OMPTeamsDistributeParallelForDirective::Create( 8631 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 8632 DSAStack->isCancelRegion()); 8633 } 8634 8635 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 8636 Stmt *AStmt, 8637 SourceLocation StartLoc, 8638 SourceLocation EndLoc) { 8639 if (!AStmt) 8640 return StmtError(); 8641 8642 auto *CS = cast<CapturedStmt>(AStmt); 8643 // 1.2.2 OpenMP Language Terminology 8644 // Structured block - An executable statement with a single entry at the 8645 // top and a single exit at the bottom. 8646 // The point of exit cannot be a branch out of the structured block. 8647 // longjmp() and throw() must not violate the entry/exit criteria. 8648 CS->getCapturedDecl()->setNothrow(); 8649 8650 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 8651 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8652 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8653 // 1.2.2 OpenMP Language Terminology 8654 // Structured block - An executable statement with a single entry at the 8655 // top and a single exit at the bottom. 8656 // The point of exit cannot be a branch out of the structured block. 8657 // longjmp() and throw() must not violate the entry/exit criteria. 8658 CS->getCapturedDecl()->setNothrow(); 8659 } 8660 setFunctionHasBranchProtectedScope(); 8661 8662 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 8663 AStmt); 8664 } 8665 8666 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 8667 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8668 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8669 if (!AStmt) 8670 return StmtError(); 8671 8672 auto *CS = cast<CapturedStmt>(AStmt); 8673 // 1.2.2 OpenMP Language Terminology 8674 // Structured block - An executable statement with a single entry at the 8675 // top and a single exit at the bottom. 8676 // The point of exit cannot be a branch out of the structured block. 8677 // longjmp() and throw() must not violate the entry/exit criteria. 8678 CS->getCapturedDecl()->setNothrow(); 8679 for (int ThisCaptureLevel = 8680 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 8681 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8682 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8683 // 1.2.2 OpenMP Language Terminology 8684 // Structured block - An executable statement with a single entry at the 8685 // top and a single exit at the bottom. 8686 // The point of exit cannot be a branch out of the structured block. 8687 // longjmp() and throw() must not violate the entry/exit criteria. 8688 CS->getCapturedDecl()->setNothrow(); 8689 } 8690 8691 OMPLoopDirective::HelperExprs B; 8692 // In presence of clause 'collapse' with number of loops, it will 8693 // define the nested loops number. 8694 unsigned NestedLoopCount = checkOpenMPLoop( 8695 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 8696 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8697 VarsWithImplicitDSA, B); 8698 if (NestedLoopCount == 0) 8699 return StmtError(); 8700 8701 assert((CurContext->isDependentContext() || B.builtAll()) && 8702 "omp target teams distribute loop exprs were not built"); 8703 8704 setFunctionHasBranchProtectedScope(); 8705 return OMPTargetTeamsDistributeDirective::Create( 8706 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8707 } 8708 8709 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 8710 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8711 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8712 if (!AStmt) 8713 return StmtError(); 8714 8715 auto *CS = cast<CapturedStmt>(AStmt); 8716 // 1.2.2 OpenMP Language Terminology 8717 // Structured block - An executable statement with a single entry at the 8718 // top and a single exit at the bottom. 8719 // The point of exit cannot be a branch out of the structured block. 8720 // longjmp() and throw() must not violate the entry/exit criteria. 8721 CS->getCapturedDecl()->setNothrow(); 8722 for (int ThisCaptureLevel = 8723 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 8724 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8725 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8726 // 1.2.2 OpenMP Language Terminology 8727 // Structured block - An executable statement with a single entry at the 8728 // top and a single exit at the bottom. 8729 // The point of exit cannot be a branch out of the structured block. 8730 // longjmp() and throw() must not violate the entry/exit criteria. 8731 CS->getCapturedDecl()->setNothrow(); 8732 } 8733 8734 OMPLoopDirective::HelperExprs B; 8735 // In presence of clause 'collapse' with number of loops, it will 8736 // define the nested loops number. 8737 unsigned NestedLoopCount = checkOpenMPLoop( 8738 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 8739 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8740 VarsWithImplicitDSA, B); 8741 if (NestedLoopCount == 0) 8742 return StmtError(); 8743 8744 assert((CurContext->isDependentContext() || B.builtAll()) && 8745 "omp target teams distribute parallel for loop exprs were not built"); 8746 8747 if (!CurContext->isDependentContext()) { 8748 // Finalize the clauses that need pre-built expressions for CodeGen. 8749 for (OMPClause *C : Clauses) { 8750 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8751 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8752 B.NumIterations, *this, CurScope, 8753 DSAStack)) 8754 return StmtError(); 8755 } 8756 } 8757 8758 setFunctionHasBranchProtectedScope(); 8759 return OMPTargetTeamsDistributeParallelForDirective::Create( 8760 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 8761 DSAStack->isCancelRegion()); 8762 } 8763 8764 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 8765 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8766 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8767 if (!AStmt) 8768 return StmtError(); 8769 8770 auto *CS = cast<CapturedStmt>(AStmt); 8771 // 1.2.2 OpenMP Language Terminology 8772 // Structured block - An executable statement with a single entry at the 8773 // top and a single exit at the bottom. 8774 // The point of exit cannot be a branch out of the structured block. 8775 // longjmp() and throw() must not violate the entry/exit criteria. 8776 CS->getCapturedDecl()->setNothrow(); 8777 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 8778 OMPD_target_teams_distribute_parallel_for_simd); 8779 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8780 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8781 // 1.2.2 OpenMP Language Terminology 8782 // Structured block - An executable statement with a single entry at the 8783 // top and a single exit at the bottom. 8784 // The point of exit cannot be a branch out of the structured block. 8785 // longjmp() and throw() must not violate the entry/exit criteria. 8786 CS->getCapturedDecl()->setNothrow(); 8787 } 8788 8789 OMPLoopDirective::HelperExprs B; 8790 // In presence of clause 'collapse' with number of loops, it will 8791 // define the nested loops number. 8792 unsigned NestedLoopCount = 8793 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 8794 getCollapseNumberExpr(Clauses), 8795 nullptr /*ordered not a clause on distribute*/, CS, *this, 8796 *DSAStack, VarsWithImplicitDSA, B); 8797 if (NestedLoopCount == 0) 8798 return StmtError(); 8799 8800 assert((CurContext->isDependentContext() || B.builtAll()) && 8801 "omp target teams distribute parallel for simd loop exprs were not " 8802 "built"); 8803 8804 if (!CurContext->isDependentContext()) { 8805 // Finalize the clauses that need pre-built expressions for CodeGen. 8806 for (OMPClause *C : Clauses) { 8807 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8808 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8809 B.NumIterations, *this, CurScope, 8810 DSAStack)) 8811 return StmtError(); 8812 } 8813 } 8814 8815 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8816 return StmtError(); 8817 8818 setFunctionHasBranchProtectedScope(); 8819 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 8820 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8821 } 8822 8823 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 8824 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8825 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8826 if (!AStmt) 8827 return StmtError(); 8828 8829 auto *CS = cast<CapturedStmt>(AStmt); 8830 // 1.2.2 OpenMP Language Terminology 8831 // Structured block - An executable statement with a single entry at the 8832 // top and a single exit at the bottom. 8833 // The point of exit cannot be a branch out of the structured block. 8834 // longjmp() and throw() must not violate the entry/exit criteria. 8835 CS->getCapturedDecl()->setNothrow(); 8836 for (int ThisCaptureLevel = 8837 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 8838 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8839 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8840 // 1.2.2 OpenMP Language Terminology 8841 // Structured block - An executable statement with a single entry at the 8842 // top and a single exit at the bottom. 8843 // The point of exit cannot be a branch out of the structured block. 8844 // longjmp() and throw() must not violate the entry/exit criteria. 8845 CS->getCapturedDecl()->setNothrow(); 8846 } 8847 8848 OMPLoopDirective::HelperExprs B; 8849 // In presence of clause 'collapse' with number of loops, it will 8850 // define the nested loops number. 8851 unsigned NestedLoopCount = checkOpenMPLoop( 8852 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 8853 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8854 VarsWithImplicitDSA, B); 8855 if (NestedLoopCount == 0) 8856 return StmtError(); 8857 8858 assert((CurContext->isDependentContext() || B.builtAll()) && 8859 "omp target teams distribute simd loop exprs were not built"); 8860 8861 if (!CurContext->isDependentContext()) { 8862 // Finalize the clauses that need pre-built expressions for CodeGen. 8863 for (OMPClause *C : Clauses) { 8864 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8865 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8866 B.NumIterations, *this, CurScope, 8867 DSAStack)) 8868 return StmtError(); 8869 } 8870 } 8871 8872 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8873 return StmtError(); 8874 8875 setFunctionHasBranchProtectedScope(); 8876 return OMPTargetTeamsDistributeSimdDirective::Create( 8877 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8878 } 8879 8880 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 8881 SourceLocation StartLoc, 8882 SourceLocation LParenLoc, 8883 SourceLocation EndLoc) { 8884 OMPClause *Res = nullptr; 8885 switch (Kind) { 8886 case OMPC_final: 8887 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 8888 break; 8889 case OMPC_num_threads: 8890 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 8891 break; 8892 case OMPC_safelen: 8893 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 8894 break; 8895 case OMPC_simdlen: 8896 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 8897 break; 8898 case OMPC_allocator: 8899 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc); 8900 break; 8901 case OMPC_collapse: 8902 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 8903 break; 8904 case OMPC_ordered: 8905 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 8906 break; 8907 case OMPC_device: 8908 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc); 8909 break; 8910 case OMPC_num_teams: 8911 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 8912 break; 8913 case OMPC_thread_limit: 8914 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 8915 break; 8916 case OMPC_priority: 8917 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 8918 break; 8919 case OMPC_grainsize: 8920 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 8921 break; 8922 case OMPC_num_tasks: 8923 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 8924 break; 8925 case OMPC_hint: 8926 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 8927 break; 8928 case OMPC_if: 8929 case OMPC_default: 8930 case OMPC_proc_bind: 8931 case OMPC_schedule: 8932 case OMPC_private: 8933 case OMPC_firstprivate: 8934 case OMPC_lastprivate: 8935 case OMPC_shared: 8936 case OMPC_reduction: 8937 case OMPC_task_reduction: 8938 case OMPC_in_reduction: 8939 case OMPC_linear: 8940 case OMPC_aligned: 8941 case OMPC_copyin: 8942 case OMPC_copyprivate: 8943 case OMPC_nowait: 8944 case OMPC_untied: 8945 case OMPC_mergeable: 8946 case OMPC_threadprivate: 8947 case OMPC_allocate: 8948 case OMPC_flush: 8949 case OMPC_read: 8950 case OMPC_write: 8951 case OMPC_update: 8952 case OMPC_capture: 8953 case OMPC_seq_cst: 8954 case OMPC_depend: 8955 case OMPC_threads: 8956 case OMPC_simd: 8957 case OMPC_map: 8958 case OMPC_nogroup: 8959 case OMPC_dist_schedule: 8960 case OMPC_defaultmap: 8961 case OMPC_unknown: 8962 case OMPC_uniform: 8963 case OMPC_to: 8964 case OMPC_from: 8965 case OMPC_use_device_ptr: 8966 case OMPC_is_device_ptr: 8967 case OMPC_unified_address: 8968 case OMPC_unified_shared_memory: 8969 case OMPC_reverse_offload: 8970 case OMPC_dynamic_allocators: 8971 case OMPC_atomic_default_mem_order: 8972 llvm_unreachable("Clause is not allowed."); 8973 } 8974 return Res; 8975 } 8976 8977 // An OpenMP directive such as 'target parallel' has two captured regions: 8978 // for the 'target' and 'parallel' respectively. This function returns 8979 // the region in which to capture expressions associated with a clause. 8980 // A return value of OMPD_unknown signifies that the expression should not 8981 // be captured. 8982 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 8983 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 8984 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 8985 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 8986 switch (CKind) { 8987 case OMPC_if: 8988 switch (DKind) { 8989 case OMPD_target_parallel: 8990 case OMPD_target_parallel_for: 8991 case OMPD_target_parallel_for_simd: 8992 // If this clause applies to the nested 'parallel' region, capture within 8993 // the 'target' region, otherwise do not capture. 8994 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 8995 CaptureRegion = OMPD_target; 8996 break; 8997 case OMPD_target_teams_distribute_parallel_for: 8998 case OMPD_target_teams_distribute_parallel_for_simd: 8999 // If this clause applies to the nested 'parallel' region, capture within 9000 // the 'teams' region, otherwise do not capture. 9001 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 9002 CaptureRegion = OMPD_teams; 9003 break; 9004 case OMPD_teams_distribute_parallel_for: 9005 case OMPD_teams_distribute_parallel_for_simd: 9006 CaptureRegion = OMPD_teams; 9007 break; 9008 case OMPD_target_update: 9009 case OMPD_target_enter_data: 9010 case OMPD_target_exit_data: 9011 CaptureRegion = OMPD_task; 9012 break; 9013 case OMPD_cancel: 9014 case OMPD_parallel: 9015 case OMPD_parallel_sections: 9016 case OMPD_parallel_for: 9017 case OMPD_parallel_for_simd: 9018 case OMPD_target: 9019 case OMPD_target_simd: 9020 case OMPD_target_teams: 9021 case OMPD_target_teams_distribute: 9022 case OMPD_target_teams_distribute_simd: 9023 case OMPD_distribute_parallel_for: 9024 case OMPD_distribute_parallel_for_simd: 9025 case OMPD_task: 9026 case OMPD_taskloop: 9027 case OMPD_taskloop_simd: 9028 case OMPD_target_data: 9029 // Do not capture if-clause expressions. 9030 break; 9031 case OMPD_threadprivate: 9032 case OMPD_allocate: 9033 case OMPD_taskyield: 9034 case OMPD_barrier: 9035 case OMPD_taskwait: 9036 case OMPD_cancellation_point: 9037 case OMPD_flush: 9038 case OMPD_declare_reduction: 9039 case OMPD_declare_mapper: 9040 case OMPD_declare_simd: 9041 case OMPD_declare_target: 9042 case OMPD_end_declare_target: 9043 case OMPD_teams: 9044 case OMPD_simd: 9045 case OMPD_for: 9046 case OMPD_for_simd: 9047 case OMPD_sections: 9048 case OMPD_section: 9049 case OMPD_single: 9050 case OMPD_master: 9051 case OMPD_critical: 9052 case OMPD_taskgroup: 9053 case OMPD_distribute: 9054 case OMPD_ordered: 9055 case OMPD_atomic: 9056 case OMPD_distribute_simd: 9057 case OMPD_teams_distribute: 9058 case OMPD_teams_distribute_simd: 9059 case OMPD_requires: 9060 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 9061 case OMPD_unknown: 9062 llvm_unreachable("Unknown OpenMP directive"); 9063 } 9064 break; 9065 case OMPC_num_threads: 9066 switch (DKind) { 9067 case OMPD_target_parallel: 9068 case OMPD_target_parallel_for: 9069 case OMPD_target_parallel_for_simd: 9070 CaptureRegion = OMPD_target; 9071 break; 9072 case OMPD_teams_distribute_parallel_for: 9073 case OMPD_teams_distribute_parallel_for_simd: 9074 case OMPD_target_teams_distribute_parallel_for: 9075 case OMPD_target_teams_distribute_parallel_for_simd: 9076 CaptureRegion = OMPD_teams; 9077 break; 9078 case OMPD_parallel: 9079 case OMPD_parallel_sections: 9080 case OMPD_parallel_for: 9081 case OMPD_parallel_for_simd: 9082 case OMPD_distribute_parallel_for: 9083 case OMPD_distribute_parallel_for_simd: 9084 // Do not capture num_threads-clause expressions. 9085 break; 9086 case OMPD_target_data: 9087 case OMPD_target_enter_data: 9088 case OMPD_target_exit_data: 9089 case OMPD_target_update: 9090 case OMPD_target: 9091 case OMPD_target_simd: 9092 case OMPD_target_teams: 9093 case OMPD_target_teams_distribute: 9094 case OMPD_target_teams_distribute_simd: 9095 case OMPD_cancel: 9096 case OMPD_task: 9097 case OMPD_taskloop: 9098 case OMPD_taskloop_simd: 9099 case OMPD_threadprivate: 9100 case OMPD_allocate: 9101 case OMPD_taskyield: 9102 case OMPD_barrier: 9103 case OMPD_taskwait: 9104 case OMPD_cancellation_point: 9105 case OMPD_flush: 9106 case OMPD_declare_reduction: 9107 case OMPD_declare_mapper: 9108 case OMPD_declare_simd: 9109 case OMPD_declare_target: 9110 case OMPD_end_declare_target: 9111 case OMPD_teams: 9112 case OMPD_simd: 9113 case OMPD_for: 9114 case OMPD_for_simd: 9115 case OMPD_sections: 9116 case OMPD_section: 9117 case OMPD_single: 9118 case OMPD_master: 9119 case OMPD_critical: 9120 case OMPD_taskgroup: 9121 case OMPD_distribute: 9122 case OMPD_ordered: 9123 case OMPD_atomic: 9124 case OMPD_distribute_simd: 9125 case OMPD_teams_distribute: 9126 case OMPD_teams_distribute_simd: 9127 case OMPD_requires: 9128 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 9129 case OMPD_unknown: 9130 llvm_unreachable("Unknown OpenMP directive"); 9131 } 9132 break; 9133 case OMPC_num_teams: 9134 switch (DKind) { 9135 case OMPD_target_teams: 9136 case OMPD_target_teams_distribute: 9137 case OMPD_target_teams_distribute_simd: 9138 case OMPD_target_teams_distribute_parallel_for: 9139 case OMPD_target_teams_distribute_parallel_for_simd: 9140 CaptureRegion = OMPD_target; 9141 break; 9142 case OMPD_teams_distribute_parallel_for: 9143 case OMPD_teams_distribute_parallel_for_simd: 9144 case OMPD_teams: 9145 case OMPD_teams_distribute: 9146 case OMPD_teams_distribute_simd: 9147 // Do not capture num_teams-clause expressions. 9148 break; 9149 case OMPD_distribute_parallel_for: 9150 case OMPD_distribute_parallel_for_simd: 9151 case OMPD_task: 9152 case OMPD_taskloop: 9153 case OMPD_taskloop_simd: 9154 case OMPD_target_data: 9155 case OMPD_target_enter_data: 9156 case OMPD_target_exit_data: 9157 case OMPD_target_update: 9158 case OMPD_cancel: 9159 case OMPD_parallel: 9160 case OMPD_parallel_sections: 9161 case OMPD_parallel_for: 9162 case OMPD_parallel_for_simd: 9163 case OMPD_target: 9164 case OMPD_target_simd: 9165 case OMPD_target_parallel: 9166 case OMPD_target_parallel_for: 9167 case OMPD_target_parallel_for_simd: 9168 case OMPD_threadprivate: 9169 case OMPD_allocate: 9170 case OMPD_taskyield: 9171 case OMPD_barrier: 9172 case OMPD_taskwait: 9173 case OMPD_cancellation_point: 9174 case OMPD_flush: 9175 case OMPD_declare_reduction: 9176 case OMPD_declare_mapper: 9177 case OMPD_declare_simd: 9178 case OMPD_declare_target: 9179 case OMPD_end_declare_target: 9180 case OMPD_simd: 9181 case OMPD_for: 9182 case OMPD_for_simd: 9183 case OMPD_sections: 9184 case OMPD_section: 9185 case OMPD_single: 9186 case OMPD_master: 9187 case OMPD_critical: 9188 case OMPD_taskgroup: 9189 case OMPD_distribute: 9190 case OMPD_ordered: 9191 case OMPD_atomic: 9192 case OMPD_distribute_simd: 9193 case OMPD_requires: 9194 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 9195 case OMPD_unknown: 9196 llvm_unreachable("Unknown OpenMP directive"); 9197 } 9198 break; 9199 case OMPC_thread_limit: 9200 switch (DKind) { 9201 case OMPD_target_teams: 9202 case OMPD_target_teams_distribute: 9203 case OMPD_target_teams_distribute_simd: 9204 case OMPD_target_teams_distribute_parallel_for: 9205 case OMPD_target_teams_distribute_parallel_for_simd: 9206 CaptureRegion = OMPD_target; 9207 break; 9208 case OMPD_teams_distribute_parallel_for: 9209 case OMPD_teams_distribute_parallel_for_simd: 9210 case OMPD_teams: 9211 case OMPD_teams_distribute: 9212 case OMPD_teams_distribute_simd: 9213 // Do not capture thread_limit-clause expressions. 9214 break; 9215 case OMPD_distribute_parallel_for: 9216 case OMPD_distribute_parallel_for_simd: 9217 case OMPD_task: 9218 case OMPD_taskloop: 9219 case OMPD_taskloop_simd: 9220 case OMPD_target_data: 9221 case OMPD_target_enter_data: 9222 case OMPD_target_exit_data: 9223 case OMPD_target_update: 9224 case OMPD_cancel: 9225 case OMPD_parallel: 9226 case OMPD_parallel_sections: 9227 case OMPD_parallel_for: 9228 case OMPD_parallel_for_simd: 9229 case OMPD_target: 9230 case OMPD_target_simd: 9231 case OMPD_target_parallel: 9232 case OMPD_target_parallel_for: 9233 case OMPD_target_parallel_for_simd: 9234 case OMPD_threadprivate: 9235 case OMPD_allocate: 9236 case OMPD_taskyield: 9237 case OMPD_barrier: 9238 case OMPD_taskwait: 9239 case OMPD_cancellation_point: 9240 case OMPD_flush: 9241 case OMPD_declare_reduction: 9242 case OMPD_declare_mapper: 9243 case OMPD_declare_simd: 9244 case OMPD_declare_target: 9245 case OMPD_end_declare_target: 9246 case OMPD_simd: 9247 case OMPD_for: 9248 case OMPD_for_simd: 9249 case OMPD_sections: 9250 case OMPD_section: 9251 case OMPD_single: 9252 case OMPD_master: 9253 case OMPD_critical: 9254 case OMPD_taskgroup: 9255 case OMPD_distribute: 9256 case OMPD_ordered: 9257 case OMPD_atomic: 9258 case OMPD_distribute_simd: 9259 case OMPD_requires: 9260 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 9261 case OMPD_unknown: 9262 llvm_unreachable("Unknown OpenMP directive"); 9263 } 9264 break; 9265 case OMPC_schedule: 9266 switch (DKind) { 9267 case OMPD_parallel_for: 9268 case OMPD_parallel_for_simd: 9269 case OMPD_distribute_parallel_for: 9270 case OMPD_distribute_parallel_for_simd: 9271 case OMPD_teams_distribute_parallel_for: 9272 case OMPD_teams_distribute_parallel_for_simd: 9273 case OMPD_target_parallel_for: 9274 case OMPD_target_parallel_for_simd: 9275 case OMPD_target_teams_distribute_parallel_for: 9276 case OMPD_target_teams_distribute_parallel_for_simd: 9277 CaptureRegion = OMPD_parallel; 9278 break; 9279 case OMPD_for: 9280 case OMPD_for_simd: 9281 // Do not capture schedule-clause expressions. 9282 break; 9283 case OMPD_task: 9284 case OMPD_taskloop: 9285 case OMPD_taskloop_simd: 9286 case OMPD_target_data: 9287 case OMPD_target_enter_data: 9288 case OMPD_target_exit_data: 9289 case OMPD_target_update: 9290 case OMPD_teams: 9291 case OMPD_teams_distribute: 9292 case OMPD_teams_distribute_simd: 9293 case OMPD_target_teams_distribute: 9294 case OMPD_target_teams_distribute_simd: 9295 case OMPD_target: 9296 case OMPD_target_simd: 9297 case OMPD_target_parallel: 9298 case OMPD_cancel: 9299 case OMPD_parallel: 9300 case OMPD_parallel_sections: 9301 case OMPD_threadprivate: 9302 case OMPD_allocate: 9303 case OMPD_taskyield: 9304 case OMPD_barrier: 9305 case OMPD_taskwait: 9306 case OMPD_cancellation_point: 9307 case OMPD_flush: 9308 case OMPD_declare_reduction: 9309 case OMPD_declare_mapper: 9310 case OMPD_declare_simd: 9311 case OMPD_declare_target: 9312 case OMPD_end_declare_target: 9313 case OMPD_simd: 9314 case OMPD_sections: 9315 case OMPD_section: 9316 case OMPD_single: 9317 case OMPD_master: 9318 case OMPD_critical: 9319 case OMPD_taskgroup: 9320 case OMPD_distribute: 9321 case OMPD_ordered: 9322 case OMPD_atomic: 9323 case OMPD_distribute_simd: 9324 case OMPD_target_teams: 9325 case OMPD_requires: 9326 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 9327 case OMPD_unknown: 9328 llvm_unreachable("Unknown OpenMP directive"); 9329 } 9330 break; 9331 case OMPC_dist_schedule: 9332 switch (DKind) { 9333 case OMPD_teams_distribute_parallel_for: 9334 case OMPD_teams_distribute_parallel_for_simd: 9335 case OMPD_teams_distribute: 9336 case OMPD_teams_distribute_simd: 9337 case OMPD_target_teams_distribute_parallel_for: 9338 case OMPD_target_teams_distribute_parallel_for_simd: 9339 case OMPD_target_teams_distribute: 9340 case OMPD_target_teams_distribute_simd: 9341 CaptureRegion = OMPD_teams; 9342 break; 9343 case OMPD_distribute_parallel_for: 9344 case OMPD_distribute_parallel_for_simd: 9345 case OMPD_distribute: 9346 case OMPD_distribute_simd: 9347 // Do not capture thread_limit-clause expressions. 9348 break; 9349 case OMPD_parallel_for: 9350 case OMPD_parallel_for_simd: 9351 case OMPD_target_parallel_for_simd: 9352 case OMPD_target_parallel_for: 9353 case OMPD_task: 9354 case OMPD_taskloop: 9355 case OMPD_taskloop_simd: 9356 case OMPD_target_data: 9357 case OMPD_target_enter_data: 9358 case OMPD_target_exit_data: 9359 case OMPD_target_update: 9360 case OMPD_teams: 9361 case OMPD_target: 9362 case OMPD_target_simd: 9363 case OMPD_target_parallel: 9364 case OMPD_cancel: 9365 case OMPD_parallel: 9366 case OMPD_parallel_sections: 9367 case OMPD_threadprivate: 9368 case OMPD_allocate: 9369 case OMPD_taskyield: 9370 case OMPD_barrier: 9371 case OMPD_taskwait: 9372 case OMPD_cancellation_point: 9373 case OMPD_flush: 9374 case OMPD_declare_reduction: 9375 case OMPD_declare_mapper: 9376 case OMPD_declare_simd: 9377 case OMPD_declare_target: 9378 case OMPD_end_declare_target: 9379 case OMPD_simd: 9380 case OMPD_for: 9381 case OMPD_for_simd: 9382 case OMPD_sections: 9383 case OMPD_section: 9384 case OMPD_single: 9385 case OMPD_master: 9386 case OMPD_critical: 9387 case OMPD_taskgroup: 9388 case OMPD_ordered: 9389 case OMPD_atomic: 9390 case OMPD_target_teams: 9391 case OMPD_requires: 9392 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 9393 case OMPD_unknown: 9394 llvm_unreachable("Unknown OpenMP directive"); 9395 } 9396 break; 9397 case OMPC_device: 9398 switch (DKind) { 9399 case OMPD_target_update: 9400 case OMPD_target_enter_data: 9401 case OMPD_target_exit_data: 9402 case OMPD_target: 9403 case OMPD_target_simd: 9404 case OMPD_target_teams: 9405 case OMPD_target_parallel: 9406 case OMPD_target_teams_distribute: 9407 case OMPD_target_teams_distribute_simd: 9408 case OMPD_target_parallel_for: 9409 case OMPD_target_parallel_for_simd: 9410 case OMPD_target_teams_distribute_parallel_for: 9411 case OMPD_target_teams_distribute_parallel_for_simd: 9412 CaptureRegion = OMPD_task; 9413 break; 9414 case OMPD_target_data: 9415 // Do not capture device-clause expressions. 9416 break; 9417 case OMPD_teams_distribute_parallel_for: 9418 case OMPD_teams_distribute_parallel_for_simd: 9419 case OMPD_teams: 9420 case OMPD_teams_distribute: 9421 case OMPD_teams_distribute_simd: 9422 case OMPD_distribute_parallel_for: 9423 case OMPD_distribute_parallel_for_simd: 9424 case OMPD_task: 9425 case OMPD_taskloop: 9426 case OMPD_taskloop_simd: 9427 case OMPD_cancel: 9428 case OMPD_parallel: 9429 case OMPD_parallel_sections: 9430 case OMPD_parallel_for: 9431 case OMPD_parallel_for_simd: 9432 case OMPD_threadprivate: 9433 case OMPD_allocate: 9434 case OMPD_taskyield: 9435 case OMPD_barrier: 9436 case OMPD_taskwait: 9437 case OMPD_cancellation_point: 9438 case OMPD_flush: 9439 case OMPD_declare_reduction: 9440 case OMPD_declare_mapper: 9441 case OMPD_declare_simd: 9442 case OMPD_declare_target: 9443 case OMPD_end_declare_target: 9444 case OMPD_simd: 9445 case OMPD_for: 9446 case OMPD_for_simd: 9447 case OMPD_sections: 9448 case OMPD_section: 9449 case OMPD_single: 9450 case OMPD_master: 9451 case OMPD_critical: 9452 case OMPD_taskgroup: 9453 case OMPD_distribute: 9454 case OMPD_ordered: 9455 case OMPD_atomic: 9456 case OMPD_distribute_simd: 9457 case OMPD_requires: 9458 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 9459 case OMPD_unknown: 9460 llvm_unreachable("Unknown OpenMP directive"); 9461 } 9462 break; 9463 case OMPC_firstprivate: 9464 case OMPC_lastprivate: 9465 case OMPC_reduction: 9466 case OMPC_task_reduction: 9467 case OMPC_in_reduction: 9468 case OMPC_linear: 9469 case OMPC_default: 9470 case OMPC_proc_bind: 9471 case OMPC_final: 9472 case OMPC_safelen: 9473 case OMPC_simdlen: 9474 case OMPC_allocator: 9475 case OMPC_collapse: 9476 case OMPC_private: 9477 case OMPC_shared: 9478 case OMPC_aligned: 9479 case OMPC_copyin: 9480 case OMPC_copyprivate: 9481 case OMPC_ordered: 9482 case OMPC_nowait: 9483 case OMPC_untied: 9484 case OMPC_mergeable: 9485 case OMPC_threadprivate: 9486 case OMPC_allocate: 9487 case OMPC_flush: 9488 case OMPC_read: 9489 case OMPC_write: 9490 case OMPC_update: 9491 case OMPC_capture: 9492 case OMPC_seq_cst: 9493 case OMPC_depend: 9494 case OMPC_threads: 9495 case OMPC_simd: 9496 case OMPC_map: 9497 case OMPC_priority: 9498 case OMPC_grainsize: 9499 case OMPC_nogroup: 9500 case OMPC_num_tasks: 9501 case OMPC_hint: 9502 case OMPC_defaultmap: 9503 case OMPC_unknown: 9504 case OMPC_uniform: 9505 case OMPC_to: 9506 case OMPC_from: 9507 case OMPC_use_device_ptr: 9508 case OMPC_is_device_ptr: 9509 case OMPC_unified_address: 9510 case OMPC_unified_shared_memory: 9511 case OMPC_reverse_offload: 9512 case OMPC_dynamic_allocators: 9513 case OMPC_atomic_default_mem_order: 9514 llvm_unreachable("Unexpected OpenMP clause."); 9515 } 9516 return CaptureRegion; 9517 } 9518 9519 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 9520 Expr *Condition, SourceLocation StartLoc, 9521 SourceLocation LParenLoc, 9522 SourceLocation NameModifierLoc, 9523 SourceLocation ColonLoc, 9524 SourceLocation EndLoc) { 9525 Expr *ValExpr = Condition; 9526 Stmt *HelperValStmt = nullptr; 9527 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 9528 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 9529 !Condition->isInstantiationDependent() && 9530 !Condition->containsUnexpandedParameterPack()) { 9531 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 9532 if (Val.isInvalid()) 9533 return nullptr; 9534 9535 ValExpr = Val.get(); 9536 9537 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 9538 CaptureRegion = 9539 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier); 9540 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 9541 ValExpr = MakeFullExpr(ValExpr).get(); 9542 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 9543 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 9544 HelperValStmt = buildPreInits(Context, Captures); 9545 } 9546 } 9547 9548 return new (Context) 9549 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 9550 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 9551 } 9552 9553 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 9554 SourceLocation StartLoc, 9555 SourceLocation LParenLoc, 9556 SourceLocation EndLoc) { 9557 Expr *ValExpr = Condition; 9558 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 9559 !Condition->isInstantiationDependent() && 9560 !Condition->containsUnexpandedParameterPack()) { 9561 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 9562 if (Val.isInvalid()) 9563 return nullptr; 9564 9565 ValExpr = MakeFullExpr(Val.get()).get(); 9566 } 9567 9568 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc); 9569 } 9570 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 9571 Expr *Op) { 9572 if (!Op) 9573 return ExprError(); 9574 9575 class IntConvertDiagnoser : public ICEConvertDiagnoser { 9576 public: 9577 IntConvertDiagnoser() 9578 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 9579 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 9580 QualType T) override { 9581 return S.Diag(Loc, diag::err_omp_not_integral) << T; 9582 } 9583 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 9584 QualType T) override { 9585 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 9586 } 9587 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 9588 QualType T, 9589 QualType ConvTy) override { 9590 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 9591 } 9592 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 9593 QualType ConvTy) override { 9594 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 9595 << ConvTy->isEnumeralType() << ConvTy; 9596 } 9597 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 9598 QualType T) override { 9599 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 9600 } 9601 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 9602 QualType ConvTy) override { 9603 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 9604 << ConvTy->isEnumeralType() << ConvTy; 9605 } 9606 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 9607 QualType) override { 9608 llvm_unreachable("conversion functions are permitted"); 9609 } 9610 } ConvertDiagnoser; 9611 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 9612 } 9613 9614 static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, 9615 OpenMPClauseKind CKind, 9616 bool StrictlyPositive) { 9617 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 9618 !ValExpr->isInstantiationDependent()) { 9619 SourceLocation Loc = ValExpr->getExprLoc(); 9620 ExprResult Value = 9621 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 9622 if (Value.isInvalid()) 9623 return false; 9624 9625 ValExpr = Value.get(); 9626 // The expression must evaluate to a non-negative integer value. 9627 llvm::APSInt Result; 9628 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) && 9629 Result.isSigned() && 9630 !((!StrictlyPositive && Result.isNonNegative()) || 9631 (StrictlyPositive && Result.isStrictlyPositive()))) { 9632 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 9633 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 9634 << ValExpr->getSourceRange(); 9635 return false; 9636 } 9637 } 9638 return true; 9639 } 9640 9641 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 9642 SourceLocation StartLoc, 9643 SourceLocation LParenLoc, 9644 SourceLocation EndLoc) { 9645 Expr *ValExpr = NumThreads; 9646 Stmt *HelperValStmt = nullptr; 9647 9648 // OpenMP [2.5, Restrictions] 9649 // The num_threads expression must evaluate to a positive integer value. 9650 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 9651 /*StrictlyPositive=*/true)) 9652 return nullptr; 9653 9654 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 9655 OpenMPDirectiveKind CaptureRegion = 9656 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads); 9657 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 9658 ValExpr = MakeFullExpr(ValExpr).get(); 9659 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 9660 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 9661 HelperValStmt = buildPreInits(Context, Captures); 9662 } 9663 9664 return new (Context) OMPNumThreadsClause( 9665 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 9666 } 9667 9668 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 9669 OpenMPClauseKind CKind, 9670 bool StrictlyPositive) { 9671 if (!E) 9672 return ExprError(); 9673 if (E->isValueDependent() || E->isTypeDependent() || 9674 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 9675 return E; 9676 llvm::APSInt Result; 9677 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); 9678 if (ICE.isInvalid()) 9679 return ExprError(); 9680 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 9681 (!StrictlyPositive && !Result.isNonNegative())) { 9682 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 9683 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 9684 << E->getSourceRange(); 9685 return ExprError(); 9686 } 9687 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 9688 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 9689 << E->getSourceRange(); 9690 return ExprError(); 9691 } 9692 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 9693 DSAStack->setAssociatedLoops(Result.getExtValue()); 9694 else if (CKind == OMPC_ordered) 9695 DSAStack->setAssociatedLoops(Result.getExtValue()); 9696 return ICE; 9697 } 9698 9699 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 9700 SourceLocation LParenLoc, 9701 SourceLocation EndLoc) { 9702 // OpenMP [2.8.1, simd construct, Description] 9703 // The parameter of the safelen clause must be a constant 9704 // positive integer expression. 9705 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 9706 if (Safelen.isInvalid()) 9707 return nullptr; 9708 return new (Context) 9709 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 9710 } 9711 9712 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 9713 SourceLocation LParenLoc, 9714 SourceLocation EndLoc) { 9715 // OpenMP [2.8.1, simd construct, Description] 9716 // The parameter of the simdlen clause must be a constant 9717 // positive integer expression. 9718 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 9719 if (Simdlen.isInvalid()) 9720 return nullptr; 9721 return new (Context) 9722 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 9723 } 9724 9725 /// Tries to find omp_allocator_handle_t type. 9726 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc, 9727 DSAStackTy *Stack) { 9728 QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT(); 9729 if (!OMPAllocatorHandleT.isNull()) 9730 return true; 9731 // Build the predefined allocator expressions. 9732 bool ErrorFound = false; 9733 for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc; 9734 I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 9735 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 9736 StringRef Allocator = 9737 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 9738 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator); 9739 auto *VD = dyn_cast_or_null<ValueDecl>( 9740 S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName)); 9741 if (!VD) { 9742 ErrorFound = true; 9743 break; 9744 } 9745 QualType AllocatorType = 9746 VD->getType().getNonLValueExprType(S.getASTContext()); 9747 ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc); 9748 if (!Res.isUsable()) { 9749 ErrorFound = true; 9750 break; 9751 } 9752 if (OMPAllocatorHandleT.isNull()) 9753 OMPAllocatorHandleT = AllocatorType; 9754 if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) { 9755 ErrorFound = true; 9756 break; 9757 } 9758 Stack->setAllocator(AllocatorKind, Res.get()); 9759 } 9760 if (ErrorFound) { 9761 S.Diag(Loc, diag::err_implied_omp_allocator_handle_t_not_found); 9762 return false; 9763 } 9764 OMPAllocatorHandleT.addConst(); 9765 Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT); 9766 return true; 9767 } 9768 9769 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc, 9770 SourceLocation LParenLoc, 9771 SourceLocation EndLoc) { 9772 // OpenMP [2.11.3, allocate Directive, Description] 9773 // allocator is an expression of omp_allocator_handle_t type. 9774 if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack)) 9775 return nullptr; 9776 9777 ExprResult Allocator = DefaultLvalueConversion(A); 9778 if (Allocator.isInvalid()) 9779 return nullptr; 9780 Allocator = PerformImplicitConversion(Allocator.get(), 9781 DSAStack->getOMPAllocatorHandleT(), 9782 Sema::AA_Initializing, 9783 /*AllowExplicit=*/true); 9784 if (Allocator.isInvalid()) 9785 return nullptr; 9786 return new (Context) 9787 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc); 9788 } 9789 9790 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 9791 SourceLocation StartLoc, 9792 SourceLocation LParenLoc, 9793 SourceLocation EndLoc) { 9794 // OpenMP [2.7.1, loop construct, Description] 9795 // OpenMP [2.8.1, simd construct, Description] 9796 // OpenMP [2.9.6, distribute construct, Description] 9797 // The parameter of the collapse clause must be a constant 9798 // positive integer expression. 9799 ExprResult NumForLoopsResult = 9800 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 9801 if (NumForLoopsResult.isInvalid()) 9802 return nullptr; 9803 return new (Context) 9804 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 9805 } 9806 9807 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 9808 SourceLocation EndLoc, 9809 SourceLocation LParenLoc, 9810 Expr *NumForLoops) { 9811 // OpenMP [2.7.1, loop construct, Description] 9812 // OpenMP [2.8.1, simd construct, Description] 9813 // OpenMP [2.9.6, distribute construct, Description] 9814 // The parameter of the ordered clause must be a constant 9815 // positive integer expression if any. 9816 if (NumForLoops && LParenLoc.isValid()) { 9817 ExprResult NumForLoopsResult = 9818 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 9819 if (NumForLoopsResult.isInvalid()) 9820 return nullptr; 9821 NumForLoops = NumForLoopsResult.get(); 9822 } else { 9823 NumForLoops = nullptr; 9824 } 9825 auto *Clause = OMPOrderedClause::Create( 9826 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0, 9827 StartLoc, LParenLoc, EndLoc); 9828 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause); 9829 return Clause; 9830 } 9831 9832 OMPClause *Sema::ActOnOpenMPSimpleClause( 9833 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 9834 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 9835 OMPClause *Res = nullptr; 9836 switch (Kind) { 9837 case OMPC_default: 9838 Res = 9839 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), 9840 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 9841 break; 9842 case OMPC_proc_bind: 9843 Res = ActOnOpenMPProcBindClause( 9844 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, 9845 LParenLoc, EndLoc); 9846 break; 9847 case OMPC_atomic_default_mem_order: 9848 Res = ActOnOpenMPAtomicDefaultMemOrderClause( 9849 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument), 9850 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 9851 break; 9852 case OMPC_if: 9853 case OMPC_final: 9854 case OMPC_num_threads: 9855 case OMPC_safelen: 9856 case OMPC_simdlen: 9857 case OMPC_allocator: 9858 case OMPC_collapse: 9859 case OMPC_schedule: 9860 case OMPC_private: 9861 case OMPC_firstprivate: 9862 case OMPC_lastprivate: 9863 case OMPC_shared: 9864 case OMPC_reduction: 9865 case OMPC_task_reduction: 9866 case OMPC_in_reduction: 9867 case OMPC_linear: 9868 case OMPC_aligned: 9869 case OMPC_copyin: 9870 case OMPC_copyprivate: 9871 case OMPC_ordered: 9872 case OMPC_nowait: 9873 case OMPC_untied: 9874 case OMPC_mergeable: 9875 case OMPC_threadprivate: 9876 case OMPC_allocate: 9877 case OMPC_flush: 9878 case OMPC_read: 9879 case OMPC_write: 9880 case OMPC_update: 9881 case OMPC_capture: 9882 case OMPC_seq_cst: 9883 case OMPC_depend: 9884 case OMPC_device: 9885 case OMPC_threads: 9886 case OMPC_simd: 9887 case OMPC_map: 9888 case OMPC_num_teams: 9889 case OMPC_thread_limit: 9890 case OMPC_priority: 9891 case OMPC_grainsize: 9892 case OMPC_nogroup: 9893 case OMPC_num_tasks: 9894 case OMPC_hint: 9895 case OMPC_dist_schedule: 9896 case OMPC_defaultmap: 9897 case OMPC_unknown: 9898 case OMPC_uniform: 9899 case OMPC_to: 9900 case OMPC_from: 9901 case OMPC_use_device_ptr: 9902 case OMPC_is_device_ptr: 9903 case OMPC_unified_address: 9904 case OMPC_unified_shared_memory: 9905 case OMPC_reverse_offload: 9906 case OMPC_dynamic_allocators: 9907 llvm_unreachable("Clause is not allowed."); 9908 } 9909 return Res; 9910 } 9911 9912 static std::string 9913 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 9914 ArrayRef<unsigned> Exclude = llvm::None) { 9915 SmallString<256> Buffer; 9916 llvm::raw_svector_ostream Out(Buffer); 9917 unsigned Bound = Last >= 2 ? Last - 2 : 0; 9918 unsigned Skipped = Exclude.size(); 9919 auto S = Exclude.begin(), E = Exclude.end(); 9920 for (unsigned I = First; I < Last; ++I) { 9921 if (std::find(S, E, I) != E) { 9922 --Skipped; 9923 continue; 9924 } 9925 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 9926 if (I == Bound - Skipped) 9927 Out << " or "; 9928 else if (I != Bound + 1 - Skipped) 9929 Out << ", "; 9930 } 9931 return Out.str(); 9932 } 9933 9934 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, 9935 SourceLocation KindKwLoc, 9936 SourceLocation StartLoc, 9937 SourceLocation LParenLoc, 9938 SourceLocation EndLoc) { 9939 if (Kind == OMPC_DEFAULT_unknown) { 9940 static_assert(OMPC_DEFAULT_unknown > 0, 9941 "OMPC_DEFAULT_unknown not greater than 0"); 9942 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 9943 << getListOfPossibleValues(OMPC_default, /*First=*/0, 9944 /*Last=*/OMPC_DEFAULT_unknown) 9945 << getOpenMPClauseName(OMPC_default); 9946 return nullptr; 9947 } 9948 switch (Kind) { 9949 case OMPC_DEFAULT_none: 9950 DSAStack->setDefaultDSANone(KindKwLoc); 9951 break; 9952 case OMPC_DEFAULT_shared: 9953 DSAStack->setDefaultDSAShared(KindKwLoc); 9954 break; 9955 case OMPC_DEFAULT_unknown: 9956 llvm_unreachable("Clause kind is not allowed."); 9957 break; 9958 } 9959 return new (Context) 9960 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 9961 } 9962 9963 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, 9964 SourceLocation KindKwLoc, 9965 SourceLocation StartLoc, 9966 SourceLocation LParenLoc, 9967 SourceLocation EndLoc) { 9968 if (Kind == OMPC_PROC_BIND_unknown) { 9969 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 9970 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0, 9971 /*Last=*/OMPC_PROC_BIND_unknown) 9972 << getOpenMPClauseName(OMPC_proc_bind); 9973 return nullptr; 9974 } 9975 return new (Context) 9976 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 9977 } 9978 9979 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause( 9980 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc, 9981 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 9982 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) { 9983 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 9984 << getListOfPossibleValues( 9985 OMPC_atomic_default_mem_order, /*First=*/0, 9986 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) 9987 << getOpenMPClauseName(OMPC_atomic_default_mem_order); 9988 return nullptr; 9989 } 9990 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc, 9991 LParenLoc, EndLoc); 9992 } 9993 9994 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 9995 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 9996 SourceLocation StartLoc, SourceLocation LParenLoc, 9997 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 9998 SourceLocation EndLoc) { 9999 OMPClause *Res = nullptr; 10000 switch (Kind) { 10001 case OMPC_schedule: 10002 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 10003 assert(Argument.size() == NumberOfElements && 10004 ArgumentLoc.size() == NumberOfElements); 10005 Res = ActOnOpenMPScheduleClause( 10006 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 10007 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 10008 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 10009 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 10010 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 10011 break; 10012 case OMPC_if: 10013 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 10014 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 10015 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 10016 DelimLoc, EndLoc); 10017 break; 10018 case OMPC_dist_schedule: 10019 Res = ActOnOpenMPDistScheduleClause( 10020 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 10021 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 10022 break; 10023 case OMPC_defaultmap: 10024 enum { Modifier, DefaultmapKind }; 10025 Res = ActOnOpenMPDefaultmapClause( 10026 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 10027 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 10028 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 10029 EndLoc); 10030 break; 10031 case OMPC_final: 10032 case OMPC_num_threads: 10033 case OMPC_safelen: 10034 case OMPC_simdlen: 10035 case OMPC_allocator: 10036 case OMPC_collapse: 10037 case OMPC_default: 10038 case OMPC_proc_bind: 10039 case OMPC_private: 10040 case OMPC_firstprivate: 10041 case OMPC_lastprivate: 10042 case OMPC_shared: 10043 case OMPC_reduction: 10044 case OMPC_task_reduction: 10045 case OMPC_in_reduction: 10046 case OMPC_linear: 10047 case OMPC_aligned: 10048 case OMPC_copyin: 10049 case OMPC_copyprivate: 10050 case OMPC_ordered: 10051 case OMPC_nowait: 10052 case OMPC_untied: 10053 case OMPC_mergeable: 10054 case OMPC_threadprivate: 10055 case OMPC_allocate: 10056 case OMPC_flush: 10057 case OMPC_read: 10058 case OMPC_write: 10059 case OMPC_update: 10060 case OMPC_capture: 10061 case OMPC_seq_cst: 10062 case OMPC_depend: 10063 case OMPC_device: 10064 case OMPC_threads: 10065 case OMPC_simd: 10066 case OMPC_map: 10067 case OMPC_num_teams: 10068 case OMPC_thread_limit: 10069 case OMPC_priority: 10070 case OMPC_grainsize: 10071 case OMPC_nogroup: 10072 case OMPC_num_tasks: 10073 case OMPC_hint: 10074 case OMPC_unknown: 10075 case OMPC_uniform: 10076 case OMPC_to: 10077 case OMPC_from: 10078 case OMPC_use_device_ptr: 10079 case OMPC_is_device_ptr: 10080 case OMPC_unified_address: 10081 case OMPC_unified_shared_memory: 10082 case OMPC_reverse_offload: 10083 case OMPC_dynamic_allocators: 10084 case OMPC_atomic_default_mem_order: 10085 llvm_unreachable("Clause is not allowed."); 10086 } 10087 return Res; 10088 } 10089 10090 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 10091 OpenMPScheduleClauseModifier M2, 10092 SourceLocation M1Loc, SourceLocation M2Loc) { 10093 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 10094 SmallVector<unsigned, 2> Excluded; 10095 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 10096 Excluded.push_back(M2); 10097 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 10098 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 10099 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 10100 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 10101 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 10102 << getListOfPossibleValues(OMPC_schedule, 10103 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 10104 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 10105 Excluded) 10106 << getOpenMPClauseName(OMPC_schedule); 10107 return true; 10108 } 10109 return false; 10110 } 10111 10112 OMPClause *Sema::ActOnOpenMPScheduleClause( 10113 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 10114 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 10115 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 10116 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 10117 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 10118 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 10119 return nullptr; 10120 // OpenMP, 2.7.1, Loop Construct, Restrictions 10121 // Either the monotonic modifier or the nonmonotonic modifier can be specified 10122 // but not both. 10123 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 10124 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 10125 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 10126 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 10127 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 10128 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 10129 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 10130 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 10131 return nullptr; 10132 } 10133 if (Kind == OMPC_SCHEDULE_unknown) { 10134 std::string Values; 10135 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 10136 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 10137 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 10138 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 10139 Exclude); 10140 } else { 10141 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 10142 /*Last=*/OMPC_SCHEDULE_unknown); 10143 } 10144 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 10145 << Values << getOpenMPClauseName(OMPC_schedule); 10146 return nullptr; 10147 } 10148 // OpenMP, 2.7.1, Loop Construct, Restrictions 10149 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 10150 // schedule(guided). 10151 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 10152 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 10153 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 10154 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 10155 diag::err_omp_schedule_nonmonotonic_static); 10156 return nullptr; 10157 } 10158 Expr *ValExpr = ChunkSize; 10159 Stmt *HelperValStmt = nullptr; 10160 if (ChunkSize) { 10161 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 10162 !ChunkSize->isInstantiationDependent() && 10163 !ChunkSize->containsUnexpandedParameterPack()) { 10164 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 10165 ExprResult Val = 10166 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 10167 if (Val.isInvalid()) 10168 return nullptr; 10169 10170 ValExpr = Val.get(); 10171 10172 // OpenMP [2.7.1, Restrictions] 10173 // chunk_size must be a loop invariant integer expression with a positive 10174 // value. 10175 llvm::APSInt Result; 10176 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 10177 if (Result.isSigned() && !Result.isStrictlyPositive()) { 10178 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 10179 << "schedule" << 1 << ChunkSize->getSourceRange(); 10180 return nullptr; 10181 } 10182 } else if (getOpenMPCaptureRegionForClause( 10183 DSAStack->getCurrentDirective(), OMPC_schedule) != 10184 OMPD_unknown && 10185 !CurContext->isDependentContext()) { 10186 ValExpr = MakeFullExpr(ValExpr).get(); 10187 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 10188 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 10189 HelperValStmt = buildPreInits(Context, Captures); 10190 } 10191 } 10192 } 10193 10194 return new (Context) 10195 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 10196 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 10197 } 10198 10199 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 10200 SourceLocation StartLoc, 10201 SourceLocation EndLoc) { 10202 OMPClause *Res = nullptr; 10203 switch (Kind) { 10204 case OMPC_ordered: 10205 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 10206 break; 10207 case OMPC_nowait: 10208 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 10209 break; 10210 case OMPC_untied: 10211 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 10212 break; 10213 case OMPC_mergeable: 10214 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 10215 break; 10216 case OMPC_read: 10217 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 10218 break; 10219 case OMPC_write: 10220 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 10221 break; 10222 case OMPC_update: 10223 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 10224 break; 10225 case OMPC_capture: 10226 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 10227 break; 10228 case OMPC_seq_cst: 10229 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 10230 break; 10231 case OMPC_threads: 10232 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 10233 break; 10234 case OMPC_simd: 10235 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 10236 break; 10237 case OMPC_nogroup: 10238 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 10239 break; 10240 case OMPC_unified_address: 10241 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc); 10242 break; 10243 case OMPC_unified_shared_memory: 10244 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 10245 break; 10246 case OMPC_reverse_offload: 10247 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc); 10248 break; 10249 case OMPC_dynamic_allocators: 10250 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc); 10251 break; 10252 case OMPC_if: 10253 case OMPC_final: 10254 case OMPC_num_threads: 10255 case OMPC_safelen: 10256 case OMPC_simdlen: 10257 case OMPC_allocator: 10258 case OMPC_collapse: 10259 case OMPC_schedule: 10260 case OMPC_private: 10261 case OMPC_firstprivate: 10262 case OMPC_lastprivate: 10263 case OMPC_shared: 10264 case OMPC_reduction: 10265 case OMPC_task_reduction: 10266 case OMPC_in_reduction: 10267 case OMPC_linear: 10268 case OMPC_aligned: 10269 case OMPC_copyin: 10270 case OMPC_copyprivate: 10271 case OMPC_default: 10272 case OMPC_proc_bind: 10273 case OMPC_threadprivate: 10274 case OMPC_allocate: 10275 case OMPC_flush: 10276 case OMPC_depend: 10277 case OMPC_device: 10278 case OMPC_map: 10279 case OMPC_num_teams: 10280 case OMPC_thread_limit: 10281 case OMPC_priority: 10282 case OMPC_grainsize: 10283 case OMPC_num_tasks: 10284 case OMPC_hint: 10285 case OMPC_dist_schedule: 10286 case OMPC_defaultmap: 10287 case OMPC_unknown: 10288 case OMPC_uniform: 10289 case OMPC_to: 10290 case OMPC_from: 10291 case OMPC_use_device_ptr: 10292 case OMPC_is_device_ptr: 10293 case OMPC_atomic_default_mem_order: 10294 llvm_unreachable("Clause is not allowed."); 10295 } 10296 return Res; 10297 } 10298 10299 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 10300 SourceLocation EndLoc) { 10301 DSAStack->setNowaitRegion(); 10302 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 10303 } 10304 10305 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 10306 SourceLocation EndLoc) { 10307 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 10308 } 10309 10310 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 10311 SourceLocation EndLoc) { 10312 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 10313 } 10314 10315 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 10316 SourceLocation EndLoc) { 10317 return new (Context) OMPReadClause(StartLoc, EndLoc); 10318 } 10319 10320 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 10321 SourceLocation EndLoc) { 10322 return new (Context) OMPWriteClause(StartLoc, EndLoc); 10323 } 10324 10325 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 10326 SourceLocation EndLoc) { 10327 return new (Context) OMPUpdateClause(StartLoc, EndLoc); 10328 } 10329 10330 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 10331 SourceLocation EndLoc) { 10332 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 10333 } 10334 10335 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 10336 SourceLocation EndLoc) { 10337 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 10338 } 10339 10340 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 10341 SourceLocation EndLoc) { 10342 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 10343 } 10344 10345 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 10346 SourceLocation EndLoc) { 10347 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 10348 } 10349 10350 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 10351 SourceLocation EndLoc) { 10352 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 10353 } 10354 10355 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc, 10356 SourceLocation EndLoc) { 10357 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc); 10358 } 10359 10360 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc, 10361 SourceLocation EndLoc) { 10362 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 10363 } 10364 10365 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc, 10366 SourceLocation EndLoc) { 10367 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc); 10368 } 10369 10370 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc, 10371 SourceLocation EndLoc) { 10372 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc); 10373 } 10374 10375 OMPClause *Sema::ActOnOpenMPVarListClause( 10376 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, 10377 const OMPVarListLocTy &Locs, SourceLocation ColonLoc, 10378 CXXScopeSpec &ReductionOrMapperIdScopeSpec, 10379 DeclarationNameInfo &ReductionOrMapperId, OpenMPDependClauseKind DepKind, 10380 OpenMPLinearClauseKind LinKind, 10381 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 10382 ArrayRef<SourceLocation> MapTypeModifiersLoc, OpenMPMapClauseKind MapType, 10383 bool IsMapTypeImplicit, SourceLocation DepLinMapLoc) { 10384 SourceLocation StartLoc = Locs.StartLoc; 10385 SourceLocation LParenLoc = Locs.LParenLoc; 10386 SourceLocation EndLoc = Locs.EndLoc; 10387 OMPClause *Res = nullptr; 10388 switch (Kind) { 10389 case OMPC_private: 10390 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 10391 break; 10392 case OMPC_firstprivate: 10393 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 10394 break; 10395 case OMPC_lastprivate: 10396 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 10397 break; 10398 case OMPC_shared: 10399 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 10400 break; 10401 case OMPC_reduction: 10402 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 10403 EndLoc, ReductionOrMapperIdScopeSpec, 10404 ReductionOrMapperId); 10405 break; 10406 case OMPC_task_reduction: 10407 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 10408 EndLoc, ReductionOrMapperIdScopeSpec, 10409 ReductionOrMapperId); 10410 break; 10411 case OMPC_in_reduction: 10412 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 10413 EndLoc, ReductionOrMapperIdScopeSpec, 10414 ReductionOrMapperId); 10415 break; 10416 case OMPC_linear: 10417 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, 10418 LinKind, DepLinMapLoc, ColonLoc, EndLoc); 10419 break; 10420 case OMPC_aligned: 10421 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, 10422 ColonLoc, EndLoc); 10423 break; 10424 case OMPC_copyin: 10425 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 10426 break; 10427 case OMPC_copyprivate: 10428 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 10429 break; 10430 case OMPC_flush: 10431 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 10432 break; 10433 case OMPC_depend: 10434 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList, 10435 StartLoc, LParenLoc, EndLoc); 10436 break; 10437 case OMPC_map: 10438 Res = ActOnOpenMPMapClause(MapTypeModifiers, MapTypeModifiersLoc, 10439 ReductionOrMapperIdScopeSpec, 10440 ReductionOrMapperId, MapType, IsMapTypeImplicit, 10441 DepLinMapLoc, ColonLoc, VarList, Locs); 10442 break; 10443 case OMPC_to: 10444 Res = ActOnOpenMPToClause(VarList, ReductionOrMapperIdScopeSpec, 10445 ReductionOrMapperId, Locs); 10446 break; 10447 case OMPC_from: 10448 Res = ActOnOpenMPFromClause(VarList, ReductionOrMapperIdScopeSpec, 10449 ReductionOrMapperId, Locs); 10450 break; 10451 case OMPC_use_device_ptr: 10452 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs); 10453 break; 10454 case OMPC_is_device_ptr: 10455 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs); 10456 break; 10457 case OMPC_allocate: 10458 Res = ActOnOpenMPAllocateClause(TailExpr, VarList, StartLoc, LParenLoc, 10459 ColonLoc, EndLoc); 10460 break; 10461 case OMPC_if: 10462 case OMPC_final: 10463 case OMPC_num_threads: 10464 case OMPC_safelen: 10465 case OMPC_simdlen: 10466 case OMPC_allocator: 10467 case OMPC_collapse: 10468 case OMPC_default: 10469 case OMPC_proc_bind: 10470 case OMPC_schedule: 10471 case OMPC_ordered: 10472 case OMPC_nowait: 10473 case OMPC_untied: 10474 case OMPC_mergeable: 10475 case OMPC_threadprivate: 10476 case OMPC_read: 10477 case OMPC_write: 10478 case OMPC_update: 10479 case OMPC_capture: 10480 case OMPC_seq_cst: 10481 case OMPC_device: 10482 case OMPC_threads: 10483 case OMPC_simd: 10484 case OMPC_num_teams: 10485 case OMPC_thread_limit: 10486 case OMPC_priority: 10487 case OMPC_grainsize: 10488 case OMPC_nogroup: 10489 case OMPC_num_tasks: 10490 case OMPC_hint: 10491 case OMPC_dist_schedule: 10492 case OMPC_defaultmap: 10493 case OMPC_unknown: 10494 case OMPC_uniform: 10495 case OMPC_unified_address: 10496 case OMPC_unified_shared_memory: 10497 case OMPC_reverse_offload: 10498 case OMPC_dynamic_allocators: 10499 case OMPC_atomic_default_mem_order: 10500 llvm_unreachable("Clause is not allowed."); 10501 } 10502 return Res; 10503 } 10504 10505 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 10506 ExprObjectKind OK, SourceLocation Loc) { 10507 ExprResult Res = BuildDeclRefExpr( 10508 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 10509 if (!Res.isUsable()) 10510 return ExprError(); 10511 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 10512 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 10513 if (!Res.isUsable()) 10514 return ExprError(); 10515 } 10516 if (VK != VK_LValue && Res.get()->isGLValue()) { 10517 Res = DefaultLvalueConversion(Res.get()); 10518 if (!Res.isUsable()) 10519 return ExprError(); 10520 } 10521 return Res; 10522 } 10523 10524 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 10525 SourceLocation StartLoc, 10526 SourceLocation LParenLoc, 10527 SourceLocation EndLoc) { 10528 SmallVector<Expr *, 8> Vars; 10529 SmallVector<Expr *, 8> PrivateCopies; 10530 for (Expr *RefExpr : VarList) { 10531 assert(RefExpr && "NULL expr in OpenMP private clause."); 10532 SourceLocation ELoc; 10533 SourceRange ERange; 10534 Expr *SimpleRefExpr = RefExpr; 10535 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 10536 if (Res.second) { 10537 // It will be analyzed later. 10538 Vars.push_back(RefExpr); 10539 PrivateCopies.push_back(nullptr); 10540 } 10541 ValueDecl *D = Res.first; 10542 if (!D) 10543 continue; 10544 10545 QualType Type = D->getType(); 10546 auto *VD = dyn_cast<VarDecl>(D); 10547 10548 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 10549 // A variable that appears in a private clause must not have an incomplete 10550 // type or a reference type. 10551 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 10552 continue; 10553 Type = Type.getNonReferenceType(); 10554 10555 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 10556 // A variable that is privatized must not have a const-qualified type 10557 // unless it is of class type with a mutable member. This restriction does 10558 // not apply to the firstprivate clause. 10559 // 10560 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions] 10561 // A variable that appears in a private clause must not have a 10562 // const-qualified type unless it is of class type with a mutable member. 10563 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc)) 10564 continue; 10565 10566 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 10567 // in a Construct] 10568 // Variables with the predetermined data-sharing attributes may not be 10569 // listed in data-sharing attributes clauses, except for the cases 10570 // listed below. For these exceptions only, listing a predetermined 10571 // variable in a data-sharing attribute clause is allowed and overrides 10572 // the variable's predetermined data-sharing attributes. 10573 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 10574 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 10575 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 10576 << getOpenMPClauseName(OMPC_private); 10577 reportOriginalDsa(*this, DSAStack, D, DVar); 10578 continue; 10579 } 10580 10581 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 10582 // Variably modified types are not supported for tasks. 10583 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 10584 isOpenMPTaskingDirective(CurrDir)) { 10585 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 10586 << getOpenMPClauseName(OMPC_private) << Type 10587 << getOpenMPDirectiveName(CurrDir); 10588 bool IsDecl = 10589 !VD || 10590 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 10591 Diag(D->getLocation(), 10592 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10593 << D; 10594 continue; 10595 } 10596 10597 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 10598 // A list item cannot appear in both a map clause and a data-sharing 10599 // attribute clause on the same construct 10600 if (isOpenMPTargetExecutionDirective(CurrDir)) { 10601 OpenMPClauseKind ConflictKind; 10602 if (DSAStack->checkMappableExprComponentListsForDecl( 10603 VD, /*CurrentRegionOnly=*/true, 10604 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 10605 OpenMPClauseKind WhereFoundClauseKind) -> bool { 10606 ConflictKind = WhereFoundClauseKind; 10607 return true; 10608 })) { 10609 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 10610 << getOpenMPClauseName(OMPC_private) 10611 << getOpenMPClauseName(ConflictKind) 10612 << getOpenMPDirectiveName(CurrDir); 10613 reportOriginalDsa(*this, DSAStack, D, DVar); 10614 continue; 10615 } 10616 } 10617 10618 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 10619 // A variable of class type (or array thereof) that appears in a private 10620 // clause requires an accessible, unambiguous default constructor for the 10621 // class type. 10622 // Generate helper private variable and initialize it with the default 10623 // value. The address of the original variable is replaced by the address of 10624 // the new private variable in CodeGen. This new variable is not added to 10625 // IdResolver, so the code in the OpenMP region uses original variable for 10626 // proper diagnostics. 10627 Type = Type.getUnqualifiedType(); 10628 VarDecl *VDPrivate = 10629 buildVarDecl(*this, ELoc, Type, D->getName(), 10630 D->hasAttrs() ? &D->getAttrs() : nullptr, 10631 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 10632 ActOnUninitializedDecl(VDPrivate); 10633 if (VDPrivate->isInvalidDecl()) 10634 continue; 10635 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 10636 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 10637 10638 DeclRefExpr *Ref = nullptr; 10639 if (!VD && !CurContext->isDependentContext()) 10640 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 10641 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 10642 Vars.push_back((VD || CurContext->isDependentContext()) 10643 ? RefExpr->IgnoreParens() 10644 : Ref); 10645 PrivateCopies.push_back(VDPrivateRefExpr); 10646 } 10647 10648 if (Vars.empty()) 10649 return nullptr; 10650 10651 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 10652 PrivateCopies); 10653 } 10654 10655 namespace { 10656 class DiagsUninitializedSeveretyRAII { 10657 private: 10658 DiagnosticsEngine &Diags; 10659 SourceLocation SavedLoc; 10660 bool IsIgnored = false; 10661 10662 public: 10663 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 10664 bool IsIgnored) 10665 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 10666 if (!IsIgnored) { 10667 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 10668 /*Map*/ diag::Severity::Ignored, Loc); 10669 } 10670 } 10671 ~DiagsUninitializedSeveretyRAII() { 10672 if (!IsIgnored) 10673 Diags.popMappings(SavedLoc); 10674 } 10675 }; 10676 } 10677 10678 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 10679 SourceLocation StartLoc, 10680 SourceLocation LParenLoc, 10681 SourceLocation EndLoc) { 10682 SmallVector<Expr *, 8> Vars; 10683 SmallVector<Expr *, 8> PrivateCopies; 10684 SmallVector<Expr *, 8> Inits; 10685 SmallVector<Decl *, 4> ExprCaptures; 10686 bool IsImplicitClause = 10687 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 10688 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 10689 10690 for (Expr *RefExpr : VarList) { 10691 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 10692 SourceLocation ELoc; 10693 SourceRange ERange; 10694 Expr *SimpleRefExpr = RefExpr; 10695 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 10696 if (Res.second) { 10697 // It will be analyzed later. 10698 Vars.push_back(RefExpr); 10699 PrivateCopies.push_back(nullptr); 10700 Inits.push_back(nullptr); 10701 } 10702 ValueDecl *D = Res.first; 10703 if (!D) 10704 continue; 10705 10706 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 10707 QualType Type = D->getType(); 10708 auto *VD = dyn_cast<VarDecl>(D); 10709 10710 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 10711 // A variable that appears in a private clause must not have an incomplete 10712 // type or a reference type. 10713 if (RequireCompleteType(ELoc, Type, 10714 diag::err_omp_firstprivate_incomplete_type)) 10715 continue; 10716 Type = Type.getNonReferenceType(); 10717 10718 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 10719 // A variable of class type (or array thereof) that appears in a private 10720 // clause requires an accessible, unambiguous copy constructor for the 10721 // class type. 10722 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 10723 10724 // If an implicit firstprivate variable found it was checked already. 10725 DSAStackTy::DSAVarData TopDVar; 10726 if (!IsImplicitClause) { 10727 DSAStackTy::DSAVarData DVar = 10728 DSAStack->getTopDSA(D, /*FromParent=*/false); 10729 TopDVar = DVar; 10730 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 10731 bool IsConstant = ElemType.isConstant(Context); 10732 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 10733 // A list item that specifies a given variable may not appear in more 10734 // than one clause on the same directive, except that a variable may be 10735 // specified in both firstprivate and lastprivate clauses. 10736 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 10737 // A list item may appear in a firstprivate or lastprivate clause but not 10738 // both. 10739 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 10740 (isOpenMPDistributeDirective(CurrDir) || 10741 DVar.CKind != OMPC_lastprivate) && 10742 DVar.RefExpr) { 10743 Diag(ELoc, diag::err_omp_wrong_dsa) 10744 << getOpenMPClauseName(DVar.CKind) 10745 << getOpenMPClauseName(OMPC_firstprivate); 10746 reportOriginalDsa(*this, DSAStack, D, DVar); 10747 continue; 10748 } 10749 10750 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 10751 // in a Construct] 10752 // Variables with the predetermined data-sharing attributes may not be 10753 // listed in data-sharing attributes clauses, except for the cases 10754 // listed below. For these exceptions only, listing a predetermined 10755 // variable in a data-sharing attribute clause is allowed and overrides 10756 // the variable's predetermined data-sharing attributes. 10757 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 10758 // in a Construct, C/C++, p.2] 10759 // Variables with const-qualified type having no mutable member may be 10760 // listed in a firstprivate clause, even if they are static data members. 10761 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 10762 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 10763 Diag(ELoc, diag::err_omp_wrong_dsa) 10764 << getOpenMPClauseName(DVar.CKind) 10765 << getOpenMPClauseName(OMPC_firstprivate); 10766 reportOriginalDsa(*this, DSAStack, D, DVar); 10767 continue; 10768 } 10769 10770 // OpenMP [2.9.3.4, Restrictions, p.2] 10771 // A list item that is private within a parallel region must not appear 10772 // in a firstprivate clause on a worksharing construct if any of the 10773 // worksharing regions arising from the worksharing construct ever bind 10774 // to any of the parallel regions arising from the parallel construct. 10775 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 10776 // A list item that is private within a teams region must not appear in a 10777 // firstprivate clause on a distribute construct if any of the distribute 10778 // regions arising from the distribute construct ever bind to any of the 10779 // teams regions arising from the teams construct. 10780 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 10781 // A list item that appears in a reduction clause of a teams construct 10782 // must not appear in a firstprivate clause on a distribute construct if 10783 // any of the distribute regions arising from the distribute construct 10784 // ever bind to any of the teams regions arising from the teams construct. 10785 if ((isOpenMPWorksharingDirective(CurrDir) || 10786 isOpenMPDistributeDirective(CurrDir)) && 10787 !isOpenMPParallelDirective(CurrDir) && 10788 !isOpenMPTeamsDirective(CurrDir)) { 10789 DVar = DSAStack->getImplicitDSA(D, true); 10790 if (DVar.CKind != OMPC_shared && 10791 (isOpenMPParallelDirective(DVar.DKind) || 10792 isOpenMPTeamsDirective(DVar.DKind) || 10793 DVar.DKind == OMPD_unknown)) { 10794 Diag(ELoc, diag::err_omp_required_access) 10795 << getOpenMPClauseName(OMPC_firstprivate) 10796 << getOpenMPClauseName(OMPC_shared); 10797 reportOriginalDsa(*this, DSAStack, D, DVar); 10798 continue; 10799 } 10800 } 10801 // OpenMP [2.9.3.4, Restrictions, p.3] 10802 // A list item that appears in a reduction clause of a parallel construct 10803 // must not appear in a firstprivate clause on a worksharing or task 10804 // construct if any of the worksharing or task regions arising from the 10805 // worksharing or task construct ever bind to any of the parallel regions 10806 // arising from the parallel construct. 10807 // OpenMP [2.9.3.4, Restrictions, p.4] 10808 // A list item that appears in a reduction clause in worksharing 10809 // construct must not appear in a firstprivate clause in a task construct 10810 // encountered during execution of any of the worksharing regions arising 10811 // from the worksharing construct. 10812 if (isOpenMPTaskingDirective(CurrDir)) { 10813 DVar = DSAStack->hasInnermostDSA( 10814 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 10815 [](OpenMPDirectiveKind K) { 10816 return isOpenMPParallelDirective(K) || 10817 isOpenMPWorksharingDirective(K) || 10818 isOpenMPTeamsDirective(K); 10819 }, 10820 /*FromParent=*/true); 10821 if (DVar.CKind == OMPC_reduction && 10822 (isOpenMPParallelDirective(DVar.DKind) || 10823 isOpenMPWorksharingDirective(DVar.DKind) || 10824 isOpenMPTeamsDirective(DVar.DKind))) { 10825 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 10826 << getOpenMPDirectiveName(DVar.DKind); 10827 reportOriginalDsa(*this, DSAStack, D, DVar); 10828 continue; 10829 } 10830 } 10831 10832 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 10833 // A list item cannot appear in both a map clause and a data-sharing 10834 // attribute clause on the same construct 10835 if (isOpenMPTargetExecutionDirective(CurrDir)) { 10836 OpenMPClauseKind ConflictKind; 10837 if (DSAStack->checkMappableExprComponentListsForDecl( 10838 VD, /*CurrentRegionOnly=*/true, 10839 [&ConflictKind]( 10840 OMPClauseMappableExprCommon::MappableExprComponentListRef, 10841 OpenMPClauseKind WhereFoundClauseKind) { 10842 ConflictKind = WhereFoundClauseKind; 10843 return true; 10844 })) { 10845 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 10846 << getOpenMPClauseName(OMPC_firstprivate) 10847 << getOpenMPClauseName(ConflictKind) 10848 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 10849 reportOriginalDsa(*this, DSAStack, D, DVar); 10850 continue; 10851 } 10852 } 10853 } 10854 10855 // Variably modified types are not supported for tasks. 10856 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 10857 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 10858 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 10859 << getOpenMPClauseName(OMPC_firstprivate) << Type 10860 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 10861 bool IsDecl = 10862 !VD || 10863 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 10864 Diag(D->getLocation(), 10865 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10866 << D; 10867 continue; 10868 } 10869 10870 Type = Type.getUnqualifiedType(); 10871 VarDecl *VDPrivate = 10872 buildVarDecl(*this, ELoc, Type, D->getName(), 10873 D->hasAttrs() ? &D->getAttrs() : nullptr, 10874 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 10875 // Generate helper private variable and initialize it with the value of the 10876 // original variable. The address of the original variable is replaced by 10877 // the address of the new private variable in the CodeGen. This new variable 10878 // is not added to IdResolver, so the code in the OpenMP region uses 10879 // original variable for proper diagnostics and variable capturing. 10880 Expr *VDInitRefExpr = nullptr; 10881 // For arrays generate initializer for single element and replace it by the 10882 // original array element in CodeGen. 10883 if (Type->isArrayType()) { 10884 VarDecl *VDInit = 10885 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 10886 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 10887 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 10888 ElemType = ElemType.getUnqualifiedType(); 10889 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 10890 ".firstprivate.temp"); 10891 InitializedEntity Entity = 10892 InitializedEntity::InitializeVariable(VDInitTemp); 10893 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 10894 10895 InitializationSequence InitSeq(*this, Entity, Kind, Init); 10896 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 10897 if (Result.isInvalid()) 10898 VDPrivate->setInvalidDecl(); 10899 else 10900 VDPrivate->setInit(Result.getAs<Expr>()); 10901 // Remove temp variable declaration. 10902 Context.Deallocate(VDInitTemp); 10903 } else { 10904 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 10905 ".firstprivate.temp"); 10906 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 10907 RefExpr->getExprLoc()); 10908 AddInitializerToDecl(VDPrivate, 10909 DefaultLvalueConversion(VDInitRefExpr).get(), 10910 /*DirectInit=*/false); 10911 } 10912 if (VDPrivate->isInvalidDecl()) { 10913 if (IsImplicitClause) { 10914 Diag(RefExpr->getExprLoc(), 10915 diag::note_omp_task_predetermined_firstprivate_here); 10916 } 10917 continue; 10918 } 10919 CurContext->addDecl(VDPrivate); 10920 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 10921 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 10922 RefExpr->getExprLoc()); 10923 DeclRefExpr *Ref = nullptr; 10924 if (!VD && !CurContext->isDependentContext()) { 10925 if (TopDVar.CKind == OMPC_lastprivate) { 10926 Ref = TopDVar.PrivateCopy; 10927 } else { 10928 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 10929 if (!isOpenMPCapturedDecl(D)) 10930 ExprCaptures.push_back(Ref->getDecl()); 10931 } 10932 } 10933 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 10934 Vars.push_back((VD || CurContext->isDependentContext()) 10935 ? RefExpr->IgnoreParens() 10936 : Ref); 10937 PrivateCopies.push_back(VDPrivateRefExpr); 10938 Inits.push_back(VDInitRefExpr); 10939 } 10940 10941 if (Vars.empty()) 10942 return nullptr; 10943 10944 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 10945 Vars, PrivateCopies, Inits, 10946 buildPreInits(Context, ExprCaptures)); 10947 } 10948 10949 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList, 10950 SourceLocation StartLoc, 10951 SourceLocation LParenLoc, 10952 SourceLocation EndLoc) { 10953 SmallVector<Expr *, 8> Vars; 10954 SmallVector<Expr *, 8> SrcExprs; 10955 SmallVector<Expr *, 8> DstExprs; 10956 SmallVector<Expr *, 8> AssignmentOps; 10957 SmallVector<Decl *, 4> ExprCaptures; 10958 SmallVector<Expr *, 4> ExprPostUpdates; 10959 for (Expr *RefExpr : VarList) { 10960 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 10961 SourceLocation ELoc; 10962 SourceRange ERange; 10963 Expr *SimpleRefExpr = RefExpr; 10964 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 10965 if (Res.second) { 10966 // It will be analyzed later. 10967 Vars.push_back(RefExpr); 10968 SrcExprs.push_back(nullptr); 10969 DstExprs.push_back(nullptr); 10970 AssignmentOps.push_back(nullptr); 10971 } 10972 ValueDecl *D = Res.first; 10973 if (!D) 10974 continue; 10975 10976 QualType Type = D->getType(); 10977 auto *VD = dyn_cast<VarDecl>(D); 10978 10979 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 10980 // A variable that appears in a lastprivate clause must not have an 10981 // incomplete type or a reference type. 10982 if (RequireCompleteType(ELoc, Type, 10983 diag::err_omp_lastprivate_incomplete_type)) 10984 continue; 10985 Type = Type.getNonReferenceType(); 10986 10987 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 10988 // A variable that is privatized must not have a const-qualified type 10989 // unless it is of class type with a mutable member. This restriction does 10990 // not apply to the firstprivate clause. 10991 // 10992 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions] 10993 // A variable that appears in a lastprivate clause must not have a 10994 // const-qualified type unless it is of class type with a mutable member. 10995 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc)) 10996 continue; 10997 10998 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 10999 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 11000 // in a Construct] 11001 // Variables with the predetermined data-sharing attributes may not be 11002 // listed in data-sharing attributes clauses, except for the cases 11003 // listed below. 11004 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 11005 // A list item may appear in a firstprivate or lastprivate clause but not 11006 // both. 11007 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 11008 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 11009 (isOpenMPDistributeDirective(CurrDir) || 11010 DVar.CKind != OMPC_firstprivate) && 11011 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 11012 Diag(ELoc, diag::err_omp_wrong_dsa) 11013 << getOpenMPClauseName(DVar.CKind) 11014 << getOpenMPClauseName(OMPC_lastprivate); 11015 reportOriginalDsa(*this, DSAStack, D, DVar); 11016 continue; 11017 } 11018 11019 // OpenMP [2.14.3.5, Restrictions, p.2] 11020 // A list item that is private within a parallel region, or that appears in 11021 // the reduction clause of a parallel construct, must not appear in a 11022 // lastprivate clause on a worksharing construct if any of the corresponding 11023 // worksharing regions ever binds to any of the corresponding parallel 11024 // regions. 11025 DSAStackTy::DSAVarData TopDVar = DVar; 11026 if (isOpenMPWorksharingDirective(CurrDir) && 11027 !isOpenMPParallelDirective(CurrDir) && 11028 !isOpenMPTeamsDirective(CurrDir)) { 11029 DVar = DSAStack->getImplicitDSA(D, true); 11030 if (DVar.CKind != OMPC_shared) { 11031 Diag(ELoc, diag::err_omp_required_access) 11032 << getOpenMPClauseName(OMPC_lastprivate) 11033 << getOpenMPClauseName(OMPC_shared); 11034 reportOriginalDsa(*this, DSAStack, D, DVar); 11035 continue; 11036 } 11037 } 11038 11039 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 11040 // A variable of class type (or array thereof) that appears in a 11041 // lastprivate clause requires an accessible, unambiguous default 11042 // constructor for the class type, unless the list item is also specified 11043 // in a firstprivate clause. 11044 // A variable of class type (or array thereof) that appears in a 11045 // lastprivate clause requires an accessible, unambiguous copy assignment 11046 // operator for the class type. 11047 Type = Context.getBaseElementType(Type).getNonReferenceType(); 11048 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 11049 Type.getUnqualifiedType(), ".lastprivate.src", 11050 D->hasAttrs() ? &D->getAttrs() : nullptr); 11051 DeclRefExpr *PseudoSrcExpr = 11052 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 11053 VarDecl *DstVD = 11054 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 11055 D->hasAttrs() ? &D->getAttrs() : nullptr); 11056 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 11057 // For arrays generate assignment operation for single element and replace 11058 // it by the original array element in CodeGen. 11059 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 11060 PseudoDstExpr, PseudoSrcExpr); 11061 if (AssignmentOp.isInvalid()) 11062 continue; 11063 AssignmentOp = 11064 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 11065 if (AssignmentOp.isInvalid()) 11066 continue; 11067 11068 DeclRefExpr *Ref = nullptr; 11069 if (!VD && !CurContext->isDependentContext()) { 11070 if (TopDVar.CKind == OMPC_firstprivate) { 11071 Ref = TopDVar.PrivateCopy; 11072 } else { 11073 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 11074 if (!isOpenMPCapturedDecl(D)) 11075 ExprCaptures.push_back(Ref->getDecl()); 11076 } 11077 if (TopDVar.CKind == OMPC_firstprivate || 11078 (!isOpenMPCapturedDecl(D) && 11079 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 11080 ExprResult RefRes = DefaultLvalueConversion(Ref); 11081 if (!RefRes.isUsable()) 11082 continue; 11083 ExprResult PostUpdateRes = 11084 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 11085 RefRes.get()); 11086 if (!PostUpdateRes.isUsable()) 11087 continue; 11088 ExprPostUpdates.push_back( 11089 IgnoredValueConversions(PostUpdateRes.get()).get()); 11090 } 11091 } 11092 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 11093 Vars.push_back((VD || CurContext->isDependentContext()) 11094 ? RefExpr->IgnoreParens() 11095 : Ref); 11096 SrcExprs.push_back(PseudoSrcExpr); 11097 DstExprs.push_back(PseudoDstExpr); 11098 AssignmentOps.push_back(AssignmentOp.get()); 11099 } 11100 11101 if (Vars.empty()) 11102 return nullptr; 11103 11104 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 11105 Vars, SrcExprs, DstExprs, AssignmentOps, 11106 buildPreInits(Context, ExprCaptures), 11107 buildPostUpdate(*this, ExprPostUpdates)); 11108 } 11109 11110 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 11111 SourceLocation StartLoc, 11112 SourceLocation LParenLoc, 11113 SourceLocation EndLoc) { 11114 SmallVector<Expr *, 8> Vars; 11115 for (Expr *RefExpr : VarList) { 11116 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 11117 SourceLocation ELoc; 11118 SourceRange ERange; 11119 Expr *SimpleRefExpr = RefExpr; 11120 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 11121 if (Res.second) { 11122 // It will be analyzed later. 11123 Vars.push_back(RefExpr); 11124 } 11125 ValueDecl *D = Res.first; 11126 if (!D) 11127 continue; 11128 11129 auto *VD = dyn_cast<VarDecl>(D); 11130 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 11131 // in a Construct] 11132 // Variables with the predetermined data-sharing attributes may not be 11133 // listed in data-sharing attributes clauses, except for the cases 11134 // listed below. For these exceptions only, listing a predetermined 11135 // variable in a data-sharing attribute clause is allowed and overrides 11136 // the variable's predetermined data-sharing attributes. 11137 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 11138 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 11139 DVar.RefExpr) { 11140 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 11141 << getOpenMPClauseName(OMPC_shared); 11142 reportOriginalDsa(*this, DSAStack, D, DVar); 11143 continue; 11144 } 11145 11146 DeclRefExpr *Ref = nullptr; 11147 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 11148 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 11149 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 11150 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 11151 ? RefExpr->IgnoreParens() 11152 : Ref); 11153 } 11154 11155 if (Vars.empty()) 11156 return nullptr; 11157 11158 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 11159 } 11160 11161 namespace { 11162 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 11163 DSAStackTy *Stack; 11164 11165 public: 11166 bool VisitDeclRefExpr(DeclRefExpr *E) { 11167 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 11168 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 11169 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 11170 return false; 11171 if (DVar.CKind != OMPC_unknown) 11172 return true; 11173 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 11174 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; }, 11175 /*FromParent=*/true); 11176 return DVarPrivate.CKind != OMPC_unknown; 11177 } 11178 return false; 11179 } 11180 bool VisitStmt(Stmt *S) { 11181 for (Stmt *Child : S->children()) { 11182 if (Child && Visit(Child)) 11183 return true; 11184 } 11185 return false; 11186 } 11187 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 11188 }; 11189 } // namespace 11190 11191 namespace { 11192 // Transform MemberExpression for specified FieldDecl of current class to 11193 // DeclRefExpr to specified OMPCapturedExprDecl. 11194 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 11195 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 11196 ValueDecl *Field = nullptr; 11197 DeclRefExpr *CapturedExpr = nullptr; 11198 11199 public: 11200 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 11201 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 11202 11203 ExprResult TransformMemberExpr(MemberExpr *E) { 11204 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 11205 E->getMemberDecl() == Field) { 11206 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 11207 return CapturedExpr; 11208 } 11209 return BaseTransform::TransformMemberExpr(E); 11210 } 11211 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 11212 }; 11213 } // namespace 11214 11215 template <typename T, typename U> 11216 static T filterLookupForUDReductionAndMapper( 11217 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) { 11218 for (U &Set : Lookups) { 11219 for (auto *D : Set) { 11220 if (T Res = Gen(cast<ValueDecl>(D))) 11221 return Res; 11222 } 11223 } 11224 return T(); 11225 } 11226 11227 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 11228 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 11229 11230 for (auto RD : D->redecls()) { 11231 // Don't bother with extra checks if we already know this one isn't visible. 11232 if (RD == D) 11233 continue; 11234 11235 auto ND = cast<NamedDecl>(RD); 11236 if (LookupResult::isVisible(SemaRef, ND)) 11237 return ND; 11238 } 11239 11240 return nullptr; 11241 } 11242 11243 static void 11244 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id, 11245 SourceLocation Loc, QualType Ty, 11246 SmallVectorImpl<UnresolvedSet<8>> &Lookups) { 11247 // Find all of the associated namespaces and classes based on the 11248 // arguments we have. 11249 Sema::AssociatedNamespaceSet AssociatedNamespaces; 11250 Sema::AssociatedClassSet AssociatedClasses; 11251 OpaqueValueExpr OVE(Loc, Ty, VK_LValue); 11252 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces, 11253 AssociatedClasses); 11254 11255 // C++ [basic.lookup.argdep]p3: 11256 // Let X be the lookup set produced by unqualified lookup (3.4.1) 11257 // and let Y be the lookup set produced by argument dependent 11258 // lookup (defined as follows). If X contains [...] then Y is 11259 // empty. Otherwise Y is the set of declarations found in the 11260 // namespaces associated with the argument types as described 11261 // below. The set of declarations found by the lookup of the name 11262 // is the union of X and Y. 11263 // 11264 // Here, we compute Y and add its members to the overloaded 11265 // candidate set. 11266 for (auto *NS : AssociatedNamespaces) { 11267 // When considering an associated namespace, the lookup is the 11268 // same as the lookup performed when the associated namespace is 11269 // used as a qualifier (3.4.3.2) except that: 11270 // 11271 // -- Any using-directives in the associated namespace are 11272 // ignored. 11273 // 11274 // -- Any namespace-scope friend functions declared in 11275 // associated classes are visible within their respective 11276 // namespaces even if they are not visible during an ordinary 11277 // lookup (11.4). 11278 DeclContext::lookup_result R = NS->lookup(Id.getName()); 11279 for (auto *D : R) { 11280 auto *Underlying = D; 11281 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 11282 Underlying = USD->getTargetDecl(); 11283 11284 if (!isa<OMPDeclareReductionDecl>(Underlying) && 11285 !isa<OMPDeclareMapperDecl>(Underlying)) 11286 continue; 11287 11288 if (!SemaRef.isVisible(D)) { 11289 D = findAcceptableDecl(SemaRef, D); 11290 if (!D) 11291 continue; 11292 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 11293 Underlying = USD->getTargetDecl(); 11294 } 11295 Lookups.emplace_back(); 11296 Lookups.back().addDecl(Underlying); 11297 } 11298 } 11299 } 11300 11301 static ExprResult 11302 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 11303 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 11304 const DeclarationNameInfo &ReductionId, QualType Ty, 11305 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 11306 if (ReductionIdScopeSpec.isInvalid()) 11307 return ExprError(); 11308 SmallVector<UnresolvedSet<8>, 4> Lookups; 11309 if (S) { 11310 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 11311 Lookup.suppressDiagnostics(); 11312 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 11313 NamedDecl *D = Lookup.getRepresentativeDecl(); 11314 do { 11315 S = S->getParent(); 11316 } while (S && !S->isDeclScope(D)); 11317 if (S) 11318 S = S->getParent(); 11319 Lookups.emplace_back(); 11320 Lookups.back().append(Lookup.begin(), Lookup.end()); 11321 Lookup.clear(); 11322 } 11323 } else if (auto *ULE = 11324 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 11325 Lookups.push_back(UnresolvedSet<8>()); 11326 Decl *PrevD = nullptr; 11327 for (NamedDecl *D : ULE->decls()) { 11328 if (D == PrevD) 11329 Lookups.push_back(UnresolvedSet<8>()); 11330 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D)) 11331 Lookups.back().addDecl(DRD); 11332 PrevD = D; 11333 } 11334 } 11335 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 11336 Ty->isInstantiationDependentType() || 11337 Ty->containsUnexpandedParameterPack() || 11338 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 11339 return !D->isInvalidDecl() && 11340 (D->getType()->isDependentType() || 11341 D->getType()->isInstantiationDependentType() || 11342 D->getType()->containsUnexpandedParameterPack()); 11343 })) { 11344 UnresolvedSet<8> ResSet; 11345 for (const UnresolvedSet<8> &Set : Lookups) { 11346 if (Set.empty()) 11347 continue; 11348 ResSet.append(Set.begin(), Set.end()); 11349 // The last item marks the end of all declarations at the specified scope. 11350 ResSet.addDecl(Set[Set.size() - 1]); 11351 } 11352 return UnresolvedLookupExpr::Create( 11353 SemaRef.Context, /*NamingClass=*/nullptr, 11354 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 11355 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 11356 } 11357 // Lookup inside the classes. 11358 // C++ [over.match.oper]p3: 11359 // For a unary operator @ with an operand of a type whose 11360 // cv-unqualified version is T1, and for a binary operator @ with 11361 // a left operand of a type whose cv-unqualified version is T1 and 11362 // a right operand of a type whose cv-unqualified version is T2, 11363 // three sets of candidate functions, designated member 11364 // candidates, non-member candidates and built-in candidates, are 11365 // constructed as follows: 11366 // -- If T1 is a complete class type or a class currently being 11367 // defined, the set of member candidates is the result of the 11368 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, 11369 // the set of member candidates is empty. 11370 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 11371 Lookup.suppressDiagnostics(); 11372 if (const auto *TyRec = Ty->getAs<RecordType>()) { 11373 // Complete the type if it can be completed. 11374 // If the type is neither complete nor being defined, bail out now. 11375 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() || 11376 TyRec->getDecl()->getDefinition()) { 11377 Lookup.clear(); 11378 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl()); 11379 if (Lookup.empty()) { 11380 Lookups.emplace_back(); 11381 Lookups.back().append(Lookup.begin(), Lookup.end()); 11382 } 11383 } 11384 } 11385 // Perform ADL. 11386 if (SemaRef.getLangOpts().CPlusPlus) 11387 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups); 11388 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 11389 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 11390 if (!D->isInvalidDecl() && 11391 SemaRef.Context.hasSameType(D->getType(), Ty)) 11392 return D; 11393 return nullptr; 11394 })) 11395 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), 11396 VK_LValue, Loc); 11397 if (SemaRef.getLangOpts().CPlusPlus) { 11398 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 11399 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 11400 if (!D->isInvalidDecl() && 11401 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 11402 !Ty.isMoreQualifiedThan(D->getType())) 11403 return D; 11404 return nullptr; 11405 })) { 11406 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 11407 /*DetectVirtual=*/false); 11408 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 11409 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 11410 VD->getType().getUnqualifiedType()))) { 11411 if (SemaRef.CheckBaseClassAccess( 11412 Loc, VD->getType(), Ty, Paths.front(), 11413 /*DiagID=*/0) != Sema::AR_inaccessible) { 11414 SemaRef.BuildBasePathArray(Paths, BasePath); 11415 return SemaRef.BuildDeclRefExpr( 11416 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc); 11417 } 11418 } 11419 } 11420 } 11421 } 11422 if (ReductionIdScopeSpec.isSet()) { 11423 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range; 11424 return ExprError(); 11425 } 11426 return ExprEmpty(); 11427 } 11428 11429 namespace { 11430 /// Data for the reduction-based clauses. 11431 struct ReductionData { 11432 /// List of original reduction items. 11433 SmallVector<Expr *, 8> Vars; 11434 /// List of private copies of the reduction items. 11435 SmallVector<Expr *, 8> Privates; 11436 /// LHS expressions for the reduction_op expressions. 11437 SmallVector<Expr *, 8> LHSs; 11438 /// RHS expressions for the reduction_op expressions. 11439 SmallVector<Expr *, 8> RHSs; 11440 /// Reduction operation expression. 11441 SmallVector<Expr *, 8> ReductionOps; 11442 /// Taskgroup descriptors for the corresponding reduction items in 11443 /// in_reduction clauses. 11444 SmallVector<Expr *, 8> TaskgroupDescriptors; 11445 /// List of captures for clause. 11446 SmallVector<Decl *, 4> ExprCaptures; 11447 /// List of postupdate expressions. 11448 SmallVector<Expr *, 4> ExprPostUpdates; 11449 ReductionData() = delete; 11450 /// Reserves required memory for the reduction data. 11451 ReductionData(unsigned Size) { 11452 Vars.reserve(Size); 11453 Privates.reserve(Size); 11454 LHSs.reserve(Size); 11455 RHSs.reserve(Size); 11456 ReductionOps.reserve(Size); 11457 TaskgroupDescriptors.reserve(Size); 11458 ExprCaptures.reserve(Size); 11459 ExprPostUpdates.reserve(Size); 11460 } 11461 /// Stores reduction item and reduction operation only (required for dependent 11462 /// reduction item). 11463 void push(Expr *Item, Expr *ReductionOp) { 11464 Vars.emplace_back(Item); 11465 Privates.emplace_back(nullptr); 11466 LHSs.emplace_back(nullptr); 11467 RHSs.emplace_back(nullptr); 11468 ReductionOps.emplace_back(ReductionOp); 11469 TaskgroupDescriptors.emplace_back(nullptr); 11470 } 11471 /// Stores reduction data. 11472 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 11473 Expr *TaskgroupDescriptor) { 11474 Vars.emplace_back(Item); 11475 Privates.emplace_back(Private); 11476 LHSs.emplace_back(LHS); 11477 RHSs.emplace_back(RHS); 11478 ReductionOps.emplace_back(ReductionOp); 11479 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 11480 } 11481 }; 11482 } // namespace 11483 11484 static bool checkOMPArraySectionConstantForReduction( 11485 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 11486 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 11487 const Expr *Length = OASE->getLength(); 11488 if (Length == nullptr) { 11489 // For array sections of the form [1:] or [:], we would need to analyze 11490 // the lower bound... 11491 if (OASE->getColonLoc().isValid()) 11492 return false; 11493 11494 // This is an array subscript which has implicit length 1! 11495 SingleElement = true; 11496 ArraySizes.push_back(llvm::APSInt::get(1)); 11497 } else { 11498 Expr::EvalResult Result; 11499 if (!Length->EvaluateAsInt(Result, Context)) 11500 return false; 11501 11502 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 11503 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 11504 ArraySizes.push_back(ConstantLengthValue); 11505 } 11506 11507 // Get the base of this array section and walk up from there. 11508 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 11509 11510 // We require length = 1 for all array sections except the right-most to 11511 // guarantee that the memory region is contiguous and has no holes in it. 11512 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 11513 Length = TempOASE->getLength(); 11514 if (Length == nullptr) { 11515 // For array sections of the form [1:] or [:], we would need to analyze 11516 // the lower bound... 11517 if (OASE->getColonLoc().isValid()) 11518 return false; 11519 11520 // This is an array subscript which has implicit length 1! 11521 ArraySizes.push_back(llvm::APSInt::get(1)); 11522 } else { 11523 Expr::EvalResult Result; 11524 if (!Length->EvaluateAsInt(Result, Context)) 11525 return false; 11526 11527 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 11528 if (ConstantLengthValue.getSExtValue() != 1) 11529 return false; 11530 11531 ArraySizes.push_back(ConstantLengthValue); 11532 } 11533 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 11534 } 11535 11536 // If we have a single element, we don't need to add the implicit lengths. 11537 if (!SingleElement) { 11538 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 11539 // Has implicit length 1! 11540 ArraySizes.push_back(llvm::APSInt::get(1)); 11541 Base = TempASE->getBase()->IgnoreParenImpCasts(); 11542 } 11543 } 11544 11545 // This array section can be privatized as a single value or as a constant 11546 // sized array. 11547 return true; 11548 } 11549 11550 static bool actOnOMPReductionKindClause( 11551 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 11552 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 11553 SourceLocation ColonLoc, SourceLocation EndLoc, 11554 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 11555 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 11556 DeclarationName DN = ReductionId.getName(); 11557 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 11558 BinaryOperatorKind BOK = BO_Comma; 11559 11560 ASTContext &Context = S.Context; 11561 // OpenMP [2.14.3.6, reduction clause] 11562 // C 11563 // reduction-identifier is either an identifier or one of the following 11564 // operators: +, -, *, &, |, ^, && and || 11565 // C++ 11566 // reduction-identifier is either an id-expression or one of the following 11567 // operators: +, -, *, &, |, ^, && and || 11568 switch (OOK) { 11569 case OO_Plus: 11570 case OO_Minus: 11571 BOK = BO_Add; 11572 break; 11573 case OO_Star: 11574 BOK = BO_Mul; 11575 break; 11576 case OO_Amp: 11577 BOK = BO_And; 11578 break; 11579 case OO_Pipe: 11580 BOK = BO_Or; 11581 break; 11582 case OO_Caret: 11583 BOK = BO_Xor; 11584 break; 11585 case OO_AmpAmp: 11586 BOK = BO_LAnd; 11587 break; 11588 case OO_PipePipe: 11589 BOK = BO_LOr; 11590 break; 11591 case OO_New: 11592 case OO_Delete: 11593 case OO_Array_New: 11594 case OO_Array_Delete: 11595 case OO_Slash: 11596 case OO_Percent: 11597 case OO_Tilde: 11598 case OO_Exclaim: 11599 case OO_Equal: 11600 case OO_Less: 11601 case OO_Greater: 11602 case OO_LessEqual: 11603 case OO_GreaterEqual: 11604 case OO_PlusEqual: 11605 case OO_MinusEqual: 11606 case OO_StarEqual: 11607 case OO_SlashEqual: 11608 case OO_PercentEqual: 11609 case OO_CaretEqual: 11610 case OO_AmpEqual: 11611 case OO_PipeEqual: 11612 case OO_LessLess: 11613 case OO_GreaterGreater: 11614 case OO_LessLessEqual: 11615 case OO_GreaterGreaterEqual: 11616 case OO_EqualEqual: 11617 case OO_ExclaimEqual: 11618 case OO_Spaceship: 11619 case OO_PlusPlus: 11620 case OO_MinusMinus: 11621 case OO_Comma: 11622 case OO_ArrowStar: 11623 case OO_Arrow: 11624 case OO_Call: 11625 case OO_Subscript: 11626 case OO_Conditional: 11627 case OO_Coawait: 11628 case NUM_OVERLOADED_OPERATORS: 11629 llvm_unreachable("Unexpected reduction identifier"); 11630 case OO_None: 11631 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 11632 if (II->isStr("max")) 11633 BOK = BO_GT; 11634 else if (II->isStr("min")) 11635 BOK = BO_LT; 11636 } 11637 break; 11638 } 11639 SourceRange ReductionIdRange; 11640 if (ReductionIdScopeSpec.isValid()) 11641 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 11642 else 11643 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 11644 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 11645 11646 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 11647 bool FirstIter = true; 11648 for (Expr *RefExpr : VarList) { 11649 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 11650 // OpenMP [2.1, C/C++] 11651 // A list item is a variable or array section, subject to the restrictions 11652 // specified in Section 2.4 on page 42 and in each of the sections 11653 // describing clauses and directives for which a list appears. 11654 // OpenMP [2.14.3.3, Restrictions, p.1] 11655 // A variable that is part of another variable (as an array or 11656 // structure element) cannot appear in a private clause. 11657 if (!FirstIter && IR != ER) 11658 ++IR; 11659 FirstIter = false; 11660 SourceLocation ELoc; 11661 SourceRange ERange; 11662 Expr *SimpleRefExpr = RefExpr; 11663 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 11664 /*AllowArraySection=*/true); 11665 if (Res.second) { 11666 // Try to find 'declare reduction' corresponding construct before using 11667 // builtin/overloaded operators. 11668 QualType Type = Context.DependentTy; 11669 CXXCastPath BasePath; 11670 ExprResult DeclareReductionRef = buildDeclareReductionRef( 11671 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 11672 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 11673 Expr *ReductionOp = nullptr; 11674 if (S.CurContext->isDependentContext() && 11675 (DeclareReductionRef.isUnset() || 11676 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 11677 ReductionOp = DeclareReductionRef.get(); 11678 // It will be analyzed later. 11679 RD.push(RefExpr, ReductionOp); 11680 } 11681 ValueDecl *D = Res.first; 11682 if (!D) 11683 continue; 11684 11685 Expr *TaskgroupDescriptor = nullptr; 11686 QualType Type; 11687 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 11688 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 11689 if (ASE) { 11690 Type = ASE->getType().getNonReferenceType(); 11691 } else if (OASE) { 11692 QualType BaseType = 11693 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 11694 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 11695 Type = ATy->getElementType(); 11696 else 11697 Type = BaseType->getPointeeType(); 11698 Type = Type.getNonReferenceType(); 11699 } else { 11700 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 11701 } 11702 auto *VD = dyn_cast<VarDecl>(D); 11703 11704 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 11705 // A variable that appears in a private clause must not have an incomplete 11706 // type or a reference type. 11707 if (S.RequireCompleteType(ELoc, D->getType(), 11708 diag::err_omp_reduction_incomplete_type)) 11709 continue; 11710 // OpenMP [2.14.3.6, reduction clause, Restrictions] 11711 // A list item that appears in a reduction clause must not be 11712 // const-qualified. 11713 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc, 11714 /*AcceptIfMutable*/ false, ASE || OASE)) 11715 continue; 11716 11717 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 11718 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 11719 // If a list-item is a reference type then it must bind to the same object 11720 // for all threads of the team. 11721 if (!ASE && !OASE) { 11722 if (VD) { 11723 VarDecl *VDDef = VD->getDefinition(); 11724 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 11725 DSARefChecker Check(Stack); 11726 if (Check.Visit(VDDef->getInit())) { 11727 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 11728 << getOpenMPClauseName(ClauseKind) << ERange; 11729 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 11730 continue; 11731 } 11732 } 11733 } 11734 11735 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 11736 // in a Construct] 11737 // Variables with the predetermined data-sharing attributes may not be 11738 // listed in data-sharing attributes clauses, except for the cases 11739 // listed below. For these exceptions only, listing a predetermined 11740 // variable in a data-sharing attribute clause is allowed and overrides 11741 // the variable's predetermined data-sharing attributes. 11742 // OpenMP [2.14.3.6, Restrictions, p.3] 11743 // Any number of reduction clauses can be specified on the directive, 11744 // but a list item can appear only once in the reduction clauses for that 11745 // directive. 11746 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 11747 if (DVar.CKind == OMPC_reduction) { 11748 S.Diag(ELoc, diag::err_omp_once_referenced) 11749 << getOpenMPClauseName(ClauseKind); 11750 if (DVar.RefExpr) 11751 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 11752 continue; 11753 } 11754 if (DVar.CKind != OMPC_unknown) { 11755 S.Diag(ELoc, diag::err_omp_wrong_dsa) 11756 << getOpenMPClauseName(DVar.CKind) 11757 << getOpenMPClauseName(OMPC_reduction); 11758 reportOriginalDsa(S, Stack, D, DVar); 11759 continue; 11760 } 11761 11762 // OpenMP [2.14.3.6, Restrictions, p.1] 11763 // A list item that appears in a reduction clause of a worksharing 11764 // construct must be shared in the parallel regions to which any of the 11765 // worksharing regions arising from the worksharing construct bind. 11766 if (isOpenMPWorksharingDirective(CurrDir) && 11767 !isOpenMPParallelDirective(CurrDir) && 11768 !isOpenMPTeamsDirective(CurrDir)) { 11769 DVar = Stack->getImplicitDSA(D, true); 11770 if (DVar.CKind != OMPC_shared) { 11771 S.Diag(ELoc, diag::err_omp_required_access) 11772 << getOpenMPClauseName(OMPC_reduction) 11773 << getOpenMPClauseName(OMPC_shared); 11774 reportOriginalDsa(S, Stack, D, DVar); 11775 continue; 11776 } 11777 } 11778 } 11779 11780 // Try to find 'declare reduction' corresponding construct before using 11781 // builtin/overloaded operators. 11782 CXXCastPath BasePath; 11783 ExprResult DeclareReductionRef = buildDeclareReductionRef( 11784 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 11785 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 11786 if (DeclareReductionRef.isInvalid()) 11787 continue; 11788 if (S.CurContext->isDependentContext() && 11789 (DeclareReductionRef.isUnset() || 11790 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 11791 RD.push(RefExpr, DeclareReductionRef.get()); 11792 continue; 11793 } 11794 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 11795 // Not allowed reduction identifier is found. 11796 S.Diag(ReductionId.getBeginLoc(), 11797 diag::err_omp_unknown_reduction_identifier) 11798 << Type << ReductionIdRange; 11799 continue; 11800 } 11801 11802 // OpenMP [2.14.3.6, reduction clause, Restrictions] 11803 // The type of a list item that appears in a reduction clause must be valid 11804 // for the reduction-identifier. For a max or min reduction in C, the type 11805 // of the list item must be an allowed arithmetic data type: char, int, 11806 // float, double, or _Bool, possibly modified with long, short, signed, or 11807 // unsigned. For a max or min reduction in C++, the type of the list item 11808 // must be an allowed arithmetic data type: char, wchar_t, int, float, 11809 // double, or bool, possibly modified with long, short, signed, or unsigned. 11810 if (DeclareReductionRef.isUnset()) { 11811 if ((BOK == BO_GT || BOK == BO_LT) && 11812 !(Type->isScalarType() || 11813 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 11814 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 11815 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 11816 if (!ASE && !OASE) { 11817 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 11818 VarDecl::DeclarationOnly; 11819 S.Diag(D->getLocation(), 11820 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11821 << D; 11822 } 11823 continue; 11824 } 11825 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 11826 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 11827 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 11828 << getOpenMPClauseName(ClauseKind); 11829 if (!ASE && !OASE) { 11830 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 11831 VarDecl::DeclarationOnly; 11832 S.Diag(D->getLocation(), 11833 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11834 << D; 11835 } 11836 continue; 11837 } 11838 } 11839 11840 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 11841 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 11842 D->hasAttrs() ? &D->getAttrs() : nullptr); 11843 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 11844 D->hasAttrs() ? &D->getAttrs() : nullptr); 11845 QualType PrivateTy = Type; 11846 11847 // Try if we can determine constant lengths for all array sections and avoid 11848 // the VLA. 11849 bool ConstantLengthOASE = false; 11850 if (OASE) { 11851 bool SingleElement; 11852 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 11853 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 11854 Context, OASE, SingleElement, ArraySizes); 11855 11856 // If we don't have a single element, we must emit a constant array type. 11857 if (ConstantLengthOASE && !SingleElement) { 11858 for (llvm::APSInt &Size : ArraySizes) 11859 PrivateTy = Context.getConstantArrayType( 11860 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0); 11861 } 11862 } 11863 11864 if ((OASE && !ConstantLengthOASE) || 11865 (!OASE && !ASE && 11866 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 11867 if (!Context.getTargetInfo().isVLASupported() && 11868 S.shouldDiagnoseTargetSupportFromOpenMP()) { 11869 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 11870 S.Diag(ELoc, diag::note_vla_unsupported); 11871 continue; 11872 } 11873 // For arrays/array sections only: 11874 // Create pseudo array type for private copy. The size for this array will 11875 // be generated during codegen. 11876 // For array subscripts or single variables Private Ty is the same as Type 11877 // (type of the variable or single array element). 11878 PrivateTy = Context.getVariableArrayType( 11879 Type, 11880 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue), 11881 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 11882 } else if (!ASE && !OASE && 11883 Context.getAsArrayType(D->getType().getNonReferenceType())) { 11884 PrivateTy = D->getType().getNonReferenceType(); 11885 } 11886 // Private copy. 11887 VarDecl *PrivateVD = 11888 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 11889 D->hasAttrs() ? &D->getAttrs() : nullptr, 11890 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 11891 // Add initializer for private variable. 11892 Expr *Init = nullptr; 11893 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 11894 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 11895 if (DeclareReductionRef.isUsable()) { 11896 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 11897 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 11898 if (DRD->getInitializer()) { 11899 Init = DRDRef; 11900 RHSVD->setInit(DRDRef); 11901 RHSVD->setInitStyle(VarDecl::CallInit); 11902 } 11903 } else { 11904 switch (BOK) { 11905 case BO_Add: 11906 case BO_Xor: 11907 case BO_Or: 11908 case BO_LOr: 11909 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 11910 if (Type->isScalarType() || Type->isAnyComplexType()) 11911 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 11912 break; 11913 case BO_Mul: 11914 case BO_LAnd: 11915 if (Type->isScalarType() || Type->isAnyComplexType()) { 11916 // '*' and '&&' reduction ops - initializer is '1'. 11917 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 11918 } 11919 break; 11920 case BO_And: { 11921 // '&' reduction op - initializer is '~0'. 11922 QualType OrigType = Type; 11923 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 11924 Type = ComplexTy->getElementType(); 11925 if (Type->isRealFloatingType()) { 11926 llvm::APFloat InitValue = 11927 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type), 11928 /*isIEEE=*/true); 11929 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 11930 Type, ELoc); 11931 } else if (Type->isScalarType()) { 11932 uint64_t Size = Context.getTypeSize(Type); 11933 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 11934 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 11935 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 11936 } 11937 if (Init && OrigType->isAnyComplexType()) { 11938 // Init = 0xFFFF + 0xFFFFi; 11939 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 11940 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 11941 } 11942 Type = OrigType; 11943 break; 11944 } 11945 case BO_LT: 11946 case BO_GT: { 11947 // 'min' reduction op - initializer is 'Largest representable number in 11948 // the reduction list item type'. 11949 // 'max' reduction op - initializer is 'Least representable number in 11950 // the reduction list item type'. 11951 if (Type->isIntegerType() || Type->isPointerType()) { 11952 bool IsSigned = Type->hasSignedIntegerRepresentation(); 11953 uint64_t Size = Context.getTypeSize(Type); 11954 QualType IntTy = 11955 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 11956 llvm::APInt InitValue = 11957 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 11958 : llvm::APInt::getMinValue(Size) 11959 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 11960 : llvm::APInt::getMaxValue(Size); 11961 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 11962 if (Type->isPointerType()) { 11963 // Cast to pointer type. 11964 ExprResult CastExpr = S.BuildCStyleCastExpr( 11965 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 11966 if (CastExpr.isInvalid()) 11967 continue; 11968 Init = CastExpr.get(); 11969 } 11970 } else if (Type->isRealFloatingType()) { 11971 llvm::APFloat InitValue = llvm::APFloat::getLargest( 11972 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 11973 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 11974 Type, ELoc); 11975 } 11976 break; 11977 } 11978 case BO_PtrMemD: 11979 case BO_PtrMemI: 11980 case BO_MulAssign: 11981 case BO_Div: 11982 case BO_Rem: 11983 case BO_Sub: 11984 case BO_Shl: 11985 case BO_Shr: 11986 case BO_LE: 11987 case BO_GE: 11988 case BO_EQ: 11989 case BO_NE: 11990 case BO_Cmp: 11991 case BO_AndAssign: 11992 case BO_XorAssign: 11993 case BO_OrAssign: 11994 case BO_Assign: 11995 case BO_AddAssign: 11996 case BO_SubAssign: 11997 case BO_DivAssign: 11998 case BO_RemAssign: 11999 case BO_ShlAssign: 12000 case BO_ShrAssign: 12001 case BO_Comma: 12002 llvm_unreachable("Unexpected reduction operation"); 12003 } 12004 } 12005 if (Init && DeclareReductionRef.isUnset()) 12006 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 12007 else if (!Init) 12008 S.ActOnUninitializedDecl(RHSVD); 12009 if (RHSVD->isInvalidDecl()) 12010 continue; 12011 if (!RHSVD->hasInit() && 12012 (DeclareReductionRef.isUnset() || !S.LangOpts.CPlusPlus)) { 12013 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 12014 << Type << ReductionIdRange; 12015 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 12016 VarDecl::DeclarationOnly; 12017 S.Diag(D->getLocation(), 12018 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 12019 << D; 12020 continue; 12021 } 12022 // Store initializer for single element in private copy. Will be used during 12023 // codegen. 12024 PrivateVD->setInit(RHSVD->getInit()); 12025 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 12026 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 12027 ExprResult ReductionOp; 12028 if (DeclareReductionRef.isUsable()) { 12029 QualType RedTy = DeclareReductionRef.get()->getType(); 12030 QualType PtrRedTy = Context.getPointerType(RedTy); 12031 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 12032 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 12033 if (!BasePath.empty()) { 12034 LHS = S.DefaultLvalueConversion(LHS.get()); 12035 RHS = S.DefaultLvalueConversion(RHS.get()); 12036 LHS = ImplicitCastExpr::Create(Context, PtrRedTy, 12037 CK_UncheckedDerivedToBase, LHS.get(), 12038 &BasePath, LHS.get()->getValueKind()); 12039 RHS = ImplicitCastExpr::Create(Context, PtrRedTy, 12040 CK_UncheckedDerivedToBase, RHS.get(), 12041 &BasePath, RHS.get()->getValueKind()); 12042 } 12043 FunctionProtoType::ExtProtoInfo EPI; 12044 QualType Params[] = {PtrRedTy, PtrRedTy}; 12045 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 12046 auto *OVE = new (Context) OpaqueValueExpr( 12047 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, 12048 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 12049 Expr *Args[] = {LHS.get(), RHS.get()}; 12050 ReductionOp = 12051 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc); 12052 } else { 12053 ReductionOp = S.BuildBinOp( 12054 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE); 12055 if (ReductionOp.isUsable()) { 12056 if (BOK != BO_LT && BOK != BO_GT) { 12057 ReductionOp = 12058 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 12059 BO_Assign, LHSDRE, ReductionOp.get()); 12060 } else { 12061 auto *ConditionalOp = new (Context) 12062 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE, 12063 Type, VK_LValue, OK_Ordinary); 12064 ReductionOp = 12065 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 12066 BO_Assign, LHSDRE, ConditionalOp); 12067 } 12068 if (ReductionOp.isUsable()) 12069 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(), 12070 /*DiscardedValue*/ false); 12071 } 12072 if (!ReductionOp.isUsable()) 12073 continue; 12074 } 12075 12076 // OpenMP [2.15.4.6, Restrictions, p.2] 12077 // A list item that appears in an in_reduction clause of a task construct 12078 // must appear in a task_reduction clause of a construct associated with a 12079 // taskgroup region that includes the participating task in its taskgroup 12080 // set. The construct associated with the innermost region that meets this 12081 // condition must specify the same reduction-identifier as the in_reduction 12082 // clause. 12083 if (ClauseKind == OMPC_in_reduction) { 12084 SourceRange ParentSR; 12085 BinaryOperatorKind ParentBOK; 12086 const Expr *ParentReductionOp; 12087 Expr *ParentBOKTD, *ParentReductionOpTD; 12088 DSAStackTy::DSAVarData ParentBOKDSA = 12089 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 12090 ParentBOKTD); 12091 DSAStackTy::DSAVarData ParentReductionOpDSA = 12092 Stack->getTopMostTaskgroupReductionData( 12093 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 12094 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 12095 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 12096 if (!IsParentBOK && !IsParentReductionOp) { 12097 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction); 12098 continue; 12099 } 12100 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 12101 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK || 12102 IsParentReductionOp) { 12103 bool EmitError = true; 12104 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 12105 llvm::FoldingSetNodeID RedId, ParentRedId; 12106 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 12107 DeclareReductionRef.get()->Profile(RedId, Context, 12108 /*Canonical=*/true); 12109 EmitError = RedId != ParentRedId; 12110 } 12111 if (EmitError) { 12112 S.Diag(ReductionId.getBeginLoc(), 12113 diag::err_omp_reduction_identifier_mismatch) 12114 << ReductionIdRange << RefExpr->getSourceRange(); 12115 S.Diag(ParentSR.getBegin(), 12116 diag::note_omp_previous_reduction_identifier) 12117 << ParentSR 12118 << (IsParentBOK ? ParentBOKDSA.RefExpr 12119 : ParentReductionOpDSA.RefExpr) 12120 ->getSourceRange(); 12121 continue; 12122 } 12123 } 12124 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 12125 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined."); 12126 } 12127 12128 DeclRefExpr *Ref = nullptr; 12129 Expr *VarsExpr = RefExpr->IgnoreParens(); 12130 if (!VD && !S.CurContext->isDependentContext()) { 12131 if (ASE || OASE) { 12132 TransformExprToCaptures RebuildToCapture(S, D); 12133 VarsExpr = 12134 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 12135 Ref = RebuildToCapture.getCapturedExpr(); 12136 } else { 12137 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 12138 } 12139 if (!S.isOpenMPCapturedDecl(D)) { 12140 RD.ExprCaptures.emplace_back(Ref->getDecl()); 12141 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 12142 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 12143 if (!RefRes.isUsable()) 12144 continue; 12145 ExprResult PostUpdateRes = 12146 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 12147 RefRes.get()); 12148 if (!PostUpdateRes.isUsable()) 12149 continue; 12150 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 12151 Stack->getCurrentDirective() == OMPD_taskgroup) { 12152 S.Diag(RefExpr->getExprLoc(), 12153 diag::err_omp_reduction_non_addressable_expression) 12154 << RefExpr->getSourceRange(); 12155 continue; 12156 } 12157 RD.ExprPostUpdates.emplace_back( 12158 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 12159 } 12160 } 12161 } 12162 // All reduction items are still marked as reduction (to do not increase 12163 // code base size). 12164 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref); 12165 if (CurrDir == OMPD_taskgroup) { 12166 if (DeclareReductionRef.isUsable()) 12167 Stack->addTaskgroupReductionData(D, ReductionIdRange, 12168 DeclareReductionRef.get()); 12169 else 12170 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 12171 } 12172 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 12173 TaskgroupDescriptor); 12174 } 12175 return RD.Vars.empty(); 12176 } 12177 12178 OMPClause *Sema::ActOnOpenMPReductionClause( 12179 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 12180 SourceLocation ColonLoc, SourceLocation EndLoc, 12181 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 12182 ArrayRef<Expr *> UnresolvedReductions) { 12183 ReductionData RD(VarList.size()); 12184 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 12185 StartLoc, LParenLoc, ColonLoc, EndLoc, 12186 ReductionIdScopeSpec, ReductionId, 12187 UnresolvedReductions, RD)) 12188 return nullptr; 12189 12190 return OMPReductionClause::Create( 12191 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 12192 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 12193 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 12194 buildPreInits(Context, RD.ExprCaptures), 12195 buildPostUpdate(*this, RD.ExprPostUpdates)); 12196 } 12197 12198 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 12199 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 12200 SourceLocation ColonLoc, SourceLocation EndLoc, 12201 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 12202 ArrayRef<Expr *> UnresolvedReductions) { 12203 ReductionData RD(VarList.size()); 12204 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 12205 StartLoc, LParenLoc, ColonLoc, EndLoc, 12206 ReductionIdScopeSpec, ReductionId, 12207 UnresolvedReductions, RD)) 12208 return nullptr; 12209 12210 return OMPTaskReductionClause::Create( 12211 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 12212 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 12213 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 12214 buildPreInits(Context, RD.ExprCaptures), 12215 buildPostUpdate(*this, RD.ExprPostUpdates)); 12216 } 12217 12218 OMPClause *Sema::ActOnOpenMPInReductionClause( 12219 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 12220 SourceLocation ColonLoc, SourceLocation EndLoc, 12221 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 12222 ArrayRef<Expr *> UnresolvedReductions) { 12223 ReductionData RD(VarList.size()); 12224 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 12225 StartLoc, LParenLoc, ColonLoc, EndLoc, 12226 ReductionIdScopeSpec, ReductionId, 12227 UnresolvedReductions, RD)) 12228 return nullptr; 12229 12230 return OMPInReductionClause::Create( 12231 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 12232 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 12233 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 12234 buildPreInits(Context, RD.ExprCaptures), 12235 buildPostUpdate(*this, RD.ExprPostUpdates)); 12236 } 12237 12238 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 12239 SourceLocation LinLoc) { 12240 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 12241 LinKind == OMPC_LINEAR_unknown) { 12242 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 12243 return true; 12244 } 12245 return false; 12246 } 12247 12248 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 12249 OpenMPLinearClauseKind LinKind, 12250 QualType Type) { 12251 const auto *VD = dyn_cast_or_null<VarDecl>(D); 12252 // A variable must not have an incomplete type or a reference type. 12253 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 12254 return true; 12255 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 12256 !Type->isReferenceType()) { 12257 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 12258 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 12259 return true; 12260 } 12261 Type = Type.getNonReferenceType(); 12262 12263 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 12264 // A variable that is privatized must not have a const-qualified type 12265 // unless it is of class type with a mutable member. This restriction does 12266 // not apply to the firstprivate clause. 12267 if (rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc)) 12268 return true; 12269 12270 // A list item must be of integral or pointer type. 12271 Type = Type.getUnqualifiedType().getCanonicalType(); 12272 const auto *Ty = Type.getTypePtrOrNull(); 12273 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && 12274 !Ty->isPointerType())) { 12275 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 12276 if (D) { 12277 bool IsDecl = 12278 !VD || 12279 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 12280 Diag(D->getLocation(), 12281 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 12282 << D; 12283 } 12284 return true; 12285 } 12286 return false; 12287 } 12288 12289 OMPClause *Sema::ActOnOpenMPLinearClause( 12290 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 12291 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 12292 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 12293 SmallVector<Expr *, 8> Vars; 12294 SmallVector<Expr *, 8> Privates; 12295 SmallVector<Expr *, 8> Inits; 12296 SmallVector<Decl *, 4> ExprCaptures; 12297 SmallVector<Expr *, 4> ExprPostUpdates; 12298 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 12299 LinKind = OMPC_LINEAR_val; 12300 for (Expr *RefExpr : VarList) { 12301 assert(RefExpr && "NULL expr in OpenMP linear clause."); 12302 SourceLocation ELoc; 12303 SourceRange ERange; 12304 Expr *SimpleRefExpr = RefExpr; 12305 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 12306 if (Res.second) { 12307 // It will be analyzed later. 12308 Vars.push_back(RefExpr); 12309 Privates.push_back(nullptr); 12310 Inits.push_back(nullptr); 12311 } 12312 ValueDecl *D = Res.first; 12313 if (!D) 12314 continue; 12315 12316 QualType Type = D->getType(); 12317 auto *VD = dyn_cast<VarDecl>(D); 12318 12319 // OpenMP [2.14.3.7, linear clause] 12320 // A list-item cannot appear in more than one linear clause. 12321 // A list-item that appears in a linear clause cannot appear in any 12322 // other data-sharing attribute clause. 12323 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 12324 if (DVar.RefExpr) { 12325 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 12326 << getOpenMPClauseName(OMPC_linear); 12327 reportOriginalDsa(*this, DSAStack, D, DVar); 12328 continue; 12329 } 12330 12331 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 12332 continue; 12333 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 12334 12335 // Build private copy of original var. 12336 VarDecl *Private = 12337 buildVarDecl(*this, ELoc, Type, D->getName(), 12338 D->hasAttrs() ? &D->getAttrs() : nullptr, 12339 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 12340 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 12341 // Build var to save initial value. 12342 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 12343 Expr *InitExpr; 12344 DeclRefExpr *Ref = nullptr; 12345 if (!VD && !CurContext->isDependentContext()) { 12346 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 12347 if (!isOpenMPCapturedDecl(D)) { 12348 ExprCaptures.push_back(Ref->getDecl()); 12349 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 12350 ExprResult RefRes = DefaultLvalueConversion(Ref); 12351 if (!RefRes.isUsable()) 12352 continue; 12353 ExprResult PostUpdateRes = 12354 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 12355 SimpleRefExpr, RefRes.get()); 12356 if (!PostUpdateRes.isUsable()) 12357 continue; 12358 ExprPostUpdates.push_back( 12359 IgnoredValueConversions(PostUpdateRes.get()).get()); 12360 } 12361 } 12362 } 12363 if (LinKind == OMPC_LINEAR_uval) 12364 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 12365 else 12366 InitExpr = VD ? SimpleRefExpr : Ref; 12367 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 12368 /*DirectInit=*/false); 12369 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 12370 12371 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 12372 Vars.push_back((VD || CurContext->isDependentContext()) 12373 ? RefExpr->IgnoreParens() 12374 : Ref); 12375 Privates.push_back(PrivateRef); 12376 Inits.push_back(InitRef); 12377 } 12378 12379 if (Vars.empty()) 12380 return nullptr; 12381 12382 Expr *StepExpr = Step; 12383 Expr *CalcStepExpr = nullptr; 12384 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 12385 !Step->isInstantiationDependent() && 12386 !Step->containsUnexpandedParameterPack()) { 12387 SourceLocation StepLoc = Step->getBeginLoc(); 12388 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 12389 if (Val.isInvalid()) 12390 return nullptr; 12391 StepExpr = Val.get(); 12392 12393 // Build var to save the step value. 12394 VarDecl *SaveVar = 12395 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 12396 ExprResult SaveRef = 12397 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 12398 ExprResult CalcStep = 12399 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 12400 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false); 12401 12402 // Warn about zero linear step (it would be probably better specified as 12403 // making corresponding variables 'const'). 12404 llvm::APSInt Result; 12405 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); 12406 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) 12407 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] 12408 << (Vars.size() > 1); 12409 if (!IsConstant && CalcStep.isUsable()) { 12410 // Calculate the step beforehand instead of doing this on each iteration. 12411 // (This is not used if the number of iterations may be kfold-ed). 12412 CalcStepExpr = CalcStep.get(); 12413 } 12414 } 12415 12416 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 12417 ColonLoc, EndLoc, Vars, Privates, Inits, 12418 StepExpr, CalcStepExpr, 12419 buildPreInits(Context, ExprCaptures), 12420 buildPostUpdate(*this, ExprPostUpdates)); 12421 } 12422 12423 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 12424 Expr *NumIterations, Sema &SemaRef, 12425 Scope *S, DSAStackTy *Stack) { 12426 // Walk the vars and build update/final expressions for the CodeGen. 12427 SmallVector<Expr *, 8> Updates; 12428 SmallVector<Expr *, 8> Finals; 12429 Expr *Step = Clause.getStep(); 12430 Expr *CalcStep = Clause.getCalcStep(); 12431 // OpenMP [2.14.3.7, linear clause] 12432 // If linear-step is not specified it is assumed to be 1. 12433 if (!Step) 12434 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 12435 else if (CalcStep) 12436 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 12437 bool HasErrors = false; 12438 auto CurInit = Clause.inits().begin(); 12439 auto CurPrivate = Clause.privates().begin(); 12440 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 12441 for (Expr *RefExpr : Clause.varlists()) { 12442 SourceLocation ELoc; 12443 SourceRange ERange; 12444 Expr *SimpleRefExpr = RefExpr; 12445 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange); 12446 ValueDecl *D = Res.first; 12447 if (Res.second || !D) { 12448 Updates.push_back(nullptr); 12449 Finals.push_back(nullptr); 12450 HasErrors = true; 12451 continue; 12452 } 12453 auto &&Info = Stack->isLoopControlVariable(D); 12454 // OpenMP [2.15.11, distribute simd Construct] 12455 // A list item may not appear in a linear clause, unless it is the loop 12456 // iteration variable. 12457 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 12458 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 12459 SemaRef.Diag(ELoc, 12460 diag::err_omp_linear_distribute_var_non_loop_iteration); 12461 Updates.push_back(nullptr); 12462 Finals.push_back(nullptr); 12463 HasErrors = true; 12464 continue; 12465 } 12466 Expr *InitExpr = *CurInit; 12467 12468 // Build privatized reference to the current linear var. 12469 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 12470 Expr *CapturedRef; 12471 if (LinKind == OMPC_LINEAR_uval) 12472 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 12473 else 12474 CapturedRef = 12475 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 12476 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 12477 /*RefersToCapture=*/true); 12478 12479 // Build update: Var = InitExpr + IV * Step 12480 ExprResult Update; 12481 if (!Info.first) 12482 Update = 12483 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, 12484 InitExpr, IV, Step, /* Subtract */ false); 12485 else 12486 Update = *CurPrivate; 12487 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(), 12488 /*DiscardedValue*/ false); 12489 12490 // Build final: Var = InitExpr + NumIterations * Step 12491 ExprResult Final; 12492 if (!Info.first) 12493 Final = 12494 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 12495 InitExpr, NumIterations, Step, /*Subtract=*/false); 12496 else 12497 Final = *CurPrivate; 12498 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(), 12499 /*DiscardedValue*/ false); 12500 12501 if (!Update.isUsable() || !Final.isUsable()) { 12502 Updates.push_back(nullptr); 12503 Finals.push_back(nullptr); 12504 HasErrors = true; 12505 } else { 12506 Updates.push_back(Update.get()); 12507 Finals.push_back(Final.get()); 12508 } 12509 ++CurInit; 12510 ++CurPrivate; 12511 } 12512 Clause.setUpdates(Updates); 12513 Clause.setFinals(Finals); 12514 return HasErrors; 12515 } 12516 12517 OMPClause *Sema::ActOnOpenMPAlignedClause( 12518 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 12519 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 12520 SmallVector<Expr *, 8> Vars; 12521 for (Expr *RefExpr : VarList) { 12522 assert(RefExpr && "NULL expr in OpenMP linear clause."); 12523 SourceLocation ELoc; 12524 SourceRange ERange; 12525 Expr *SimpleRefExpr = RefExpr; 12526 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 12527 if (Res.second) { 12528 // It will be analyzed later. 12529 Vars.push_back(RefExpr); 12530 } 12531 ValueDecl *D = Res.first; 12532 if (!D) 12533 continue; 12534 12535 QualType QType = D->getType(); 12536 auto *VD = dyn_cast<VarDecl>(D); 12537 12538 // OpenMP [2.8.1, simd construct, Restrictions] 12539 // The type of list items appearing in the aligned clause must be 12540 // array, pointer, reference to array, or reference to pointer. 12541 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 12542 const Type *Ty = QType.getTypePtrOrNull(); 12543 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 12544 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 12545 << QType << getLangOpts().CPlusPlus << ERange; 12546 bool IsDecl = 12547 !VD || 12548 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 12549 Diag(D->getLocation(), 12550 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 12551 << D; 12552 continue; 12553 } 12554 12555 // OpenMP [2.8.1, simd construct, Restrictions] 12556 // A list-item cannot appear in more than one aligned clause. 12557 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 12558 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange; 12559 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 12560 << getOpenMPClauseName(OMPC_aligned); 12561 continue; 12562 } 12563 12564 DeclRefExpr *Ref = nullptr; 12565 if (!VD && isOpenMPCapturedDecl(D)) 12566 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 12567 Vars.push_back(DefaultFunctionArrayConversion( 12568 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 12569 .get()); 12570 } 12571 12572 // OpenMP [2.8.1, simd construct, Description] 12573 // The parameter of the aligned clause, alignment, must be a constant 12574 // positive integer expression. 12575 // If no optional parameter is specified, implementation-defined default 12576 // alignments for SIMD instructions on the target platforms are assumed. 12577 if (Alignment != nullptr) { 12578 ExprResult AlignResult = 12579 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 12580 if (AlignResult.isInvalid()) 12581 return nullptr; 12582 Alignment = AlignResult.get(); 12583 } 12584 if (Vars.empty()) 12585 return nullptr; 12586 12587 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 12588 EndLoc, Vars, Alignment); 12589 } 12590 12591 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 12592 SourceLocation StartLoc, 12593 SourceLocation LParenLoc, 12594 SourceLocation EndLoc) { 12595 SmallVector<Expr *, 8> Vars; 12596 SmallVector<Expr *, 8> SrcExprs; 12597 SmallVector<Expr *, 8> DstExprs; 12598 SmallVector<Expr *, 8> AssignmentOps; 12599 for (Expr *RefExpr : VarList) { 12600 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 12601 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 12602 // It will be analyzed later. 12603 Vars.push_back(RefExpr); 12604 SrcExprs.push_back(nullptr); 12605 DstExprs.push_back(nullptr); 12606 AssignmentOps.push_back(nullptr); 12607 continue; 12608 } 12609 12610 SourceLocation ELoc = RefExpr->getExprLoc(); 12611 // OpenMP [2.1, C/C++] 12612 // A list item is a variable name. 12613 // OpenMP [2.14.4.1, Restrictions, p.1] 12614 // A list item that appears in a copyin clause must be threadprivate. 12615 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 12616 if (!DE || !isa<VarDecl>(DE->getDecl())) { 12617 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 12618 << 0 << RefExpr->getSourceRange(); 12619 continue; 12620 } 12621 12622 Decl *D = DE->getDecl(); 12623 auto *VD = cast<VarDecl>(D); 12624 12625 QualType Type = VD->getType(); 12626 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 12627 // It will be analyzed later. 12628 Vars.push_back(DE); 12629 SrcExprs.push_back(nullptr); 12630 DstExprs.push_back(nullptr); 12631 AssignmentOps.push_back(nullptr); 12632 continue; 12633 } 12634 12635 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 12636 // A list item that appears in a copyin clause must be threadprivate. 12637 if (!DSAStack->isThreadPrivate(VD)) { 12638 Diag(ELoc, diag::err_omp_required_access) 12639 << getOpenMPClauseName(OMPC_copyin) 12640 << getOpenMPDirectiveName(OMPD_threadprivate); 12641 continue; 12642 } 12643 12644 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 12645 // A variable of class type (or array thereof) that appears in a 12646 // copyin clause requires an accessible, unambiguous copy assignment 12647 // operator for the class type. 12648 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 12649 VarDecl *SrcVD = 12650 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(), 12651 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 12652 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 12653 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 12654 VarDecl *DstVD = 12655 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst", 12656 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 12657 DeclRefExpr *PseudoDstExpr = 12658 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 12659 // For arrays generate assignment operation for single element and replace 12660 // it by the original array element in CodeGen. 12661 ExprResult AssignmentOp = 12662 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 12663 PseudoSrcExpr); 12664 if (AssignmentOp.isInvalid()) 12665 continue; 12666 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 12667 /*DiscardedValue*/ false); 12668 if (AssignmentOp.isInvalid()) 12669 continue; 12670 12671 DSAStack->addDSA(VD, DE, OMPC_copyin); 12672 Vars.push_back(DE); 12673 SrcExprs.push_back(PseudoSrcExpr); 12674 DstExprs.push_back(PseudoDstExpr); 12675 AssignmentOps.push_back(AssignmentOp.get()); 12676 } 12677 12678 if (Vars.empty()) 12679 return nullptr; 12680 12681 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 12682 SrcExprs, DstExprs, AssignmentOps); 12683 } 12684 12685 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 12686 SourceLocation StartLoc, 12687 SourceLocation LParenLoc, 12688 SourceLocation EndLoc) { 12689 SmallVector<Expr *, 8> Vars; 12690 SmallVector<Expr *, 8> SrcExprs; 12691 SmallVector<Expr *, 8> DstExprs; 12692 SmallVector<Expr *, 8> AssignmentOps; 12693 for (Expr *RefExpr : VarList) { 12694 assert(RefExpr && "NULL expr in OpenMP linear clause."); 12695 SourceLocation ELoc; 12696 SourceRange ERange; 12697 Expr *SimpleRefExpr = RefExpr; 12698 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 12699 if (Res.second) { 12700 // It will be analyzed later. 12701 Vars.push_back(RefExpr); 12702 SrcExprs.push_back(nullptr); 12703 DstExprs.push_back(nullptr); 12704 AssignmentOps.push_back(nullptr); 12705 } 12706 ValueDecl *D = Res.first; 12707 if (!D) 12708 continue; 12709 12710 QualType Type = D->getType(); 12711 auto *VD = dyn_cast<VarDecl>(D); 12712 12713 // OpenMP [2.14.4.2, Restrictions, p.2] 12714 // A list item that appears in a copyprivate clause may not appear in a 12715 // private or firstprivate clause on the single construct. 12716 if (!VD || !DSAStack->isThreadPrivate(VD)) { 12717 DSAStackTy::DSAVarData DVar = 12718 DSAStack->getTopDSA(D, /*FromParent=*/false); 12719 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 12720 DVar.RefExpr) { 12721 Diag(ELoc, diag::err_omp_wrong_dsa) 12722 << getOpenMPClauseName(DVar.CKind) 12723 << getOpenMPClauseName(OMPC_copyprivate); 12724 reportOriginalDsa(*this, DSAStack, D, DVar); 12725 continue; 12726 } 12727 12728 // OpenMP [2.11.4.2, Restrictions, p.1] 12729 // All list items that appear in a copyprivate clause must be either 12730 // threadprivate or private in the enclosing context. 12731 if (DVar.CKind == OMPC_unknown) { 12732 DVar = DSAStack->getImplicitDSA(D, false); 12733 if (DVar.CKind == OMPC_shared) { 12734 Diag(ELoc, diag::err_omp_required_access) 12735 << getOpenMPClauseName(OMPC_copyprivate) 12736 << "threadprivate or private in the enclosing context"; 12737 reportOriginalDsa(*this, DSAStack, D, DVar); 12738 continue; 12739 } 12740 } 12741 } 12742 12743 // Variably modified types are not supported. 12744 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 12745 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 12746 << getOpenMPClauseName(OMPC_copyprivate) << Type 12747 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 12748 bool IsDecl = 12749 !VD || 12750 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 12751 Diag(D->getLocation(), 12752 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 12753 << D; 12754 continue; 12755 } 12756 12757 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 12758 // A variable of class type (or array thereof) that appears in a 12759 // copyin clause requires an accessible, unambiguous copy assignment 12760 // operator for the class type. 12761 Type = Context.getBaseElementType(Type.getNonReferenceType()) 12762 .getUnqualifiedType(); 12763 VarDecl *SrcVD = 12764 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src", 12765 D->hasAttrs() ? &D->getAttrs() : nullptr); 12766 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 12767 VarDecl *DstVD = 12768 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst", 12769 D->hasAttrs() ? &D->getAttrs() : nullptr); 12770 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 12771 ExprResult AssignmentOp = BuildBinOp( 12772 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 12773 if (AssignmentOp.isInvalid()) 12774 continue; 12775 AssignmentOp = 12776 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 12777 if (AssignmentOp.isInvalid()) 12778 continue; 12779 12780 // No need to mark vars as copyprivate, they are already threadprivate or 12781 // implicitly private. 12782 assert(VD || isOpenMPCapturedDecl(D)); 12783 Vars.push_back( 12784 VD ? RefExpr->IgnoreParens() 12785 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 12786 SrcExprs.push_back(PseudoSrcExpr); 12787 DstExprs.push_back(PseudoDstExpr); 12788 AssignmentOps.push_back(AssignmentOp.get()); 12789 } 12790 12791 if (Vars.empty()) 12792 return nullptr; 12793 12794 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 12795 Vars, SrcExprs, DstExprs, AssignmentOps); 12796 } 12797 12798 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 12799 SourceLocation StartLoc, 12800 SourceLocation LParenLoc, 12801 SourceLocation EndLoc) { 12802 if (VarList.empty()) 12803 return nullptr; 12804 12805 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 12806 } 12807 12808 OMPClause * 12809 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind, 12810 SourceLocation DepLoc, SourceLocation ColonLoc, 12811 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 12812 SourceLocation LParenLoc, SourceLocation EndLoc) { 12813 if (DSAStack->getCurrentDirective() == OMPD_ordered && 12814 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 12815 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 12816 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 12817 return nullptr; 12818 } 12819 if (DSAStack->getCurrentDirective() != OMPD_ordered && 12820 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 12821 DepKind == OMPC_DEPEND_sink)) { 12822 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink}; 12823 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 12824 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 12825 /*Last=*/OMPC_DEPEND_unknown, Except) 12826 << getOpenMPClauseName(OMPC_depend); 12827 return nullptr; 12828 } 12829 SmallVector<Expr *, 8> Vars; 12830 DSAStackTy::OperatorOffsetTy OpsOffs; 12831 llvm::APSInt DepCounter(/*BitWidth=*/32); 12832 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 12833 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) { 12834 if (const Expr *OrderedCountExpr = 12835 DSAStack->getParentOrderedRegionParam().first) { 12836 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 12837 TotalDepCount.setIsUnsigned(/*Val=*/true); 12838 } 12839 } 12840 for (Expr *RefExpr : VarList) { 12841 assert(RefExpr && "NULL expr in OpenMP shared clause."); 12842 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 12843 // It will be analyzed later. 12844 Vars.push_back(RefExpr); 12845 continue; 12846 } 12847 12848 SourceLocation ELoc = RefExpr->getExprLoc(); 12849 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 12850 if (DepKind == OMPC_DEPEND_sink) { 12851 if (DSAStack->getParentOrderedRegionParam().first && 12852 DepCounter >= TotalDepCount) { 12853 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 12854 continue; 12855 } 12856 ++DepCounter; 12857 // OpenMP [2.13.9, Summary] 12858 // depend(dependence-type : vec), where dependence-type is: 12859 // 'sink' and where vec is the iteration vector, which has the form: 12860 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 12861 // where n is the value specified by the ordered clause in the loop 12862 // directive, xi denotes the loop iteration variable of the i-th nested 12863 // loop associated with the loop directive, and di is a constant 12864 // non-negative integer. 12865 if (CurContext->isDependentContext()) { 12866 // It will be analyzed later. 12867 Vars.push_back(RefExpr); 12868 continue; 12869 } 12870 SimpleExpr = SimpleExpr->IgnoreImplicit(); 12871 OverloadedOperatorKind OOK = OO_None; 12872 SourceLocation OOLoc; 12873 Expr *LHS = SimpleExpr; 12874 Expr *RHS = nullptr; 12875 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 12876 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 12877 OOLoc = BO->getOperatorLoc(); 12878 LHS = BO->getLHS()->IgnoreParenImpCasts(); 12879 RHS = BO->getRHS()->IgnoreParenImpCasts(); 12880 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 12881 OOK = OCE->getOperator(); 12882 OOLoc = OCE->getOperatorLoc(); 12883 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 12884 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 12885 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 12886 OOK = MCE->getMethodDecl() 12887 ->getNameInfo() 12888 .getName() 12889 .getCXXOverloadedOperator(); 12890 OOLoc = MCE->getCallee()->getExprLoc(); 12891 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 12892 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 12893 } 12894 SourceLocation ELoc; 12895 SourceRange ERange; 12896 auto Res = getPrivateItem(*this, LHS, ELoc, ERange); 12897 if (Res.second) { 12898 // It will be analyzed later. 12899 Vars.push_back(RefExpr); 12900 } 12901 ValueDecl *D = Res.first; 12902 if (!D) 12903 continue; 12904 12905 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 12906 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 12907 continue; 12908 } 12909 if (RHS) { 12910 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 12911 RHS, OMPC_depend, /*StrictlyPositive=*/false); 12912 if (RHSRes.isInvalid()) 12913 continue; 12914 } 12915 if (!CurContext->isDependentContext() && 12916 DSAStack->getParentOrderedRegionParam().first && 12917 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 12918 const ValueDecl *VD = 12919 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 12920 if (VD) 12921 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 12922 << 1 << VD; 12923 else 12924 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 12925 continue; 12926 } 12927 OpsOffs.emplace_back(RHS, OOK); 12928 } else { 12929 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 12930 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 12931 (ASE && 12932 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() && 12933 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 12934 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 12935 << RefExpr->getSourceRange(); 12936 continue; 12937 } 12938 bool Suppress = getDiagnostics().getSuppressAllDiagnostics(); 12939 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 12940 ExprResult Res = 12941 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts()); 12942 getDiagnostics().setSuppressAllDiagnostics(Suppress); 12943 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) { 12944 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 12945 << RefExpr->getSourceRange(); 12946 continue; 12947 } 12948 } 12949 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 12950 } 12951 12952 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 12953 TotalDepCount > VarList.size() && 12954 DSAStack->getParentOrderedRegionParam().first && 12955 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 12956 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 12957 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 12958 } 12959 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 12960 Vars.empty()) 12961 return nullptr; 12962 12963 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 12964 DepKind, DepLoc, ColonLoc, Vars, 12965 TotalDepCount.getZExtValue()); 12966 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 12967 DSAStack->isParentOrderedRegion()) 12968 DSAStack->addDoacrossDependClause(C, OpsOffs); 12969 return C; 12970 } 12971 12972 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc, 12973 SourceLocation LParenLoc, 12974 SourceLocation EndLoc) { 12975 Expr *ValExpr = Device; 12976 Stmt *HelperValStmt = nullptr; 12977 12978 // OpenMP [2.9.1, Restrictions] 12979 // The device expression must evaluate to a non-negative integer value. 12980 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 12981 /*StrictlyPositive=*/false)) 12982 return nullptr; 12983 12984 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12985 OpenMPDirectiveKind CaptureRegion = 12986 getOpenMPCaptureRegionForClause(DKind, OMPC_device); 12987 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12988 ValExpr = MakeFullExpr(ValExpr).get(); 12989 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12990 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12991 HelperValStmt = buildPreInits(Context, Captures); 12992 } 12993 12994 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion, 12995 StartLoc, LParenLoc, EndLoc); 12996 } 12997 12998 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 12999 DSAStackTy *Stack, QualType QTy, 13000 bool FullCheck = true) { 13001 NamedDecl *ND; 13002 if (QTy->isIncompleteType(&ND)) { 13003 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 13004 return false; 13005 } 13006 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 13007 !QTy.isTrivialType(SemaRef.Context)) 13008 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 13009 return true; 13010 } 13011 13012 /// Return true if it can be proven that the provided array expression 13013 /// (array section or array subscript) does NOT specify the whole size of the 13014 /// array whose base type is \a BaseQTy. 13015 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 13016 const Expr *E, 13017 QualType BaseQTy) { 13018 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 13019 13020 // If this is an array subscript, it refers to the whole size if the size of 13021 // the dimension is constant and equals 1. Also, an array section assumes the 13022 // format of an array subscript if no colon is used. 13023 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) { 13024 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 13025 return ATy->getSize().getSExtValue() != 1; 13026 // Size can't be evaluated statically. 13027 return false; 13028 } 13029 13030 assert(OASE && "Expecting array section if not an array subscript."); 13031 const Expr *LowerBound = OASE->getLowerBound(); 13032 const Expr *Length = OASE->getLength(); 13033 13034 // If there is a lower bound that does not evaluates to zero, we are not 13035 // covering the whole dimension. 13036 if (LowerBound) { 13037 Expr::EvalResult Result; 13038 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext())) 13039 return false; // Can't get the integer value as a constant. 13040 13041 llvm::APSInt ConstLowerBound = Result.Val.getInt(); 13042 if (ConstLowerBound.getSExtValue()) 13043 return true; 13044 } 13045 13046 // If we don't have a length we covering the whole dimension. 13047 if (!Length) 13048 return false; 13049 13050 // If the base is a pointer, we don't have a way to get the size of the 13051 // pointee. 13052 if (BaseQTy->isPointerType()) 13053 return false; 13054 13055 // We can only check if the length is the same as the size of the dimension 13056 // if we have a constant array. 13057 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 13058 if (!CATy) 13059 return false; 13060 13061 Expr::EvalResult Result; 13062 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 13063 return false; // Can't get the integer value as a constant. 13064 13065 llvm::APSInt ConstLength = Result.Val.getInt(); 13066 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 13067 } 13068 13069 // Return true if it can be proven that the provided array expression (array 13070 // section or array subscript) does NOT specify a single element of the array 13071 // whose base type is \a BaseQTy. 13072 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 13073 const Expr *E, 13074 QualType BaseQTy) { 13075 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 13076 13077 // An array subscript always refer to a single element. Also, an array section 13078 // assumes the format of an array subscript if no colon is used. 13079 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) 13080 return false; 13081 13082 assert(OASE && "Expecting array section if not an array subscript."); 13083 const Expr *Length = OASE->getLength(); 13084 13085 // If we don't have a length we have to check if the array has unitary size 13086 // for this dimension. Also, we should always expect a length if the base type 13087 // is pointer. 13088 if (!Length) { 13089 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 13090 return ATy->getSize().getSExtValue() != 1; 13091 // We cannot assume anything. 13092 return false; 13093 } 13094 13095 // Check if the length evaluates to 1. 13096 Expr::EvalResult Result; 13097 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 13098 return false; // Can't get the integer value as a constant. 13099 13100 llvm::APSInt ConstLength = Result.Val.getInt(); 13101 return ConstLength.getSExtValue() != 1; 13102 } 13103 13104 // Return the expression of the base of the mappable expression or null if it 13105 // cannot be determined and do all the necessary checks to see if the expression 13106 // is valid as a standalone mappable expression. In the process, record all the 13107 // components of the expression. 13108 static const Expr *checkMapClauseExpressionBase( 13109 Sema &SemaRef, Expr *E, 13110 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 13111 OpenMPClauseKind CKind, bool NoDiagnose) { 13112 SourceLocation ELoc = E->getExprLoc(); 13113 SourceRange ERange = E->getSourceRange(); 13114 13115 // The base of elements of list in a map clause have to be either: 13116 // - a reference to variable or field. 13117 // - a member expression. 13118 // - an array expression. 13119 // 13120 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 13121 // reference to 'r'. 13122 // 13123 // If we have: 13124 // 13125 // struct SS { 13126 // Bla S; 13127 // foo() { 13128 // #pragma omp target map (S.Arr[:12]); 13129 // } 13130 // } 13131 // 13132 // We want to retrieve the member expression 'this->S'; 13133 13134 const Expr *RelevantExpr = nullptr; 13135 13136 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2] 13137 // If a list item is an array section, it must specify contiguous storage. 13138 // 13139 // For this restriction it is sufficient that we make sure only references 13140 // to variables or fields and array expressions, and that no array sections 13141 // exist except in the rightmost expression (unless they cover the whole 13142 // dimension of the array). E.g. these would be invalid: 13143 // 13144 // r.ArrS[3:5].Arr[6:7] 13145 // 13146 // r.ArrS[3:5].x 13147 // 13148 // but these would be valid: 13149 // r.ArrS[3].Arr[6:7] 13150 // 13151 // r.ArrS[3].x 13152 13153 bool AllowUnitySizeArraySection = true; 13154 bool AllowWholeSizeArraySection = true; 13155 13156 while (!RelevantExpr) { 13157 E = E->IgnoreParenImpCasts(); 13158 13159 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) { 13160 if (!isa<VarDecl>(CurE->getDecl())) 13161 return nullptr; 13162 13163 RelevantExpr = CurE; 13164 13165 // If we got a reference to a declaration, we should not expect any array 13166 // section before that. 13167 AllowUnitySizeArraySection = false; 13168 AllowWholeSizeArraySection = false; 13169 13170 // Record the component. 13171 CurComponents.emplace_back(CurE, CurE->getDecl()); 13172 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) { 13173 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts(); 13174 13175 if (isa<CXXThisExpr>(BaseE)) 13176 // We found a base expression: this->Val. 13177 RelevantExpr = CurE; 13178 else 13179 E = BaseE; 13180 13181 if (!isa<FieldDecl>(CurE->getMemberDecl())) { 13182 if (!NoDiagnose) { 13183 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 13184 << CurE->getSourceRange(); 13185 return nullptr; 13186 } 13187 if (RelevantExpr) 13188 return nullptr; 13189 continue; 13190 } 13191 13192 auto *FD = cast<FieldDecl>(CurE->getMemberDecl()); 13193 13194 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 13195 // A bit-field cannot appear in a map clause. 13196 // 13197 if (FD->isBitField()) { 13198 if (!NoDiagnose) { 13199 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 13200 << CurE->getSourceRange() << getOpenMPClauseName(CKind); 13201 return nullptr; 13202 } 13203 if (RelevantExpr) 13204 return nullptr; 13205 continue; 13206 } 13207 13208 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 13209 // If the type of a list item is a reference to a type T then the type 13210 // will be considered to be T for all purposes of this clause. 13211 QualType CurType = BaseE->getType().getNonReferenceType(); 13212 13213 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 13214 // A list item cannot be a variable that is a member of a structure with 13215 // a union type. 13216 // 13217 if (CurType->isUnionType()) { 13218 if (!NoDiagnose) { 13219 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 13220 << CurE->getSourceRange(); 13221 return nullptr; 13222 } 13223 continue; 13224 } 13225 13226 // If we got a member expression, we should not expect any array section 13227 // before that: 13228 // 13229 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 13230 // If a list item is an element of a structure, only the rightmost symbol 13231 // of the variable reference can be an array section. 13232 // 13233 AllowUnitySizeArraySection = false; 13234 AllowWholeSizeArraySection = false; 13235 13236 // Record the component. 13237 CurComponents.emplace_back(CurE, FD); 13238 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) { 13239 E = CurE->getBase()->IgnoreParenImpCasts(); 13240 13241 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 13242 if (!NoDiagnose) { 13243 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 13244 << 0 << CurE->getSourceRange(); 13245 return nullptr; 13246 } 13247 continue; 13248 } 13249 13250 // If we got an array subscript that express the whole dimension we 13251 // can have any array expressions before. If it only expressing part of 13252 // the dimension, we can only have unitary-size array expressions. 13253 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, 13254 E->getType())) 13255 AllowWholeSizeArraySection = false; 13256 13257 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 13258 Expr::EvalResult Result; 13259 if (CurE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext())) { 13260 if (!Result.Val.getInt().isNullValue()) { 13261 SemaRef.Diag(CurE->getIdx()->getExprLoc(), 13262 diag::err_omp_invalid_map_this_expr); 13263 SemaRef.Diag(CurE->getIdx()->getExprLoc(), 13264 diag::note_omp_invalid_subscript_on_this_ptr_map); 13265 } 13266 } 13267 RelevantExpr = TE; 13268 } 13269 13270 // Record the component - we don't have any declaration associated. 13271 CurComponents.emplace_back(CurE, nullptr); 13272 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) { 13273 assert(!NoDiagnose && "Array sections cannot be implicitly mapped."); 13274 E = CurE->getBase()->IgnoreParenImpCasts(); 13275 13276 QualType CurType = 13277 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 13278 13279 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 13280 // If the type of a list item is a reference to a type T then the type 13281 // will be considered to be T for all purposes of this clause. 13282 if (CurType->isReferenceType()) 13283 CurType = CurType->getPointeeType(); 13284 13285 bool IsPointer = CurType->isAnyPointerType(); 13286 13287 if (!IsPointer && !CurType->isArrayType()) { 13288 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 13289 << 0 << CurE->getSourceRange(); 13290 return nullptr; 13291 } 13292 13293 bool NotWhole = 13294 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType); 13295 bool NotUnity = 13296 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType); 13297 13298 if (AllowWholeSizeArraySection) { 13299 // Any array section is currently allowed. Allowing a whole size array 13300 // section implies allowing a unity array section as well. 13301 // 13302 // If this array section refers to the whole dimension we can still 13303 // accept other array sections before this one, except if the base is a 13304 // pointer. Otherwise, only unitary sections are accepted. 13305 if (NotWhole || IsPointer) 13306 AllowWholeSizeArraySection = false; 13307 } else if (AllowUnitySizeArraySection && NotUnity) { 13308 // A unity or whole array section is not allowed and that is not 13309 // compatible with the properties of the current array section. 13310 SemaRef.Diag( 13311 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 13312 << CurE->getSourceRange(); 13313 return nullptr; 13314 } 13315 13316 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 13317 Expr::EvalResult ResultR; 13318 Expr::EvalResult ResultL; 13319 if (CurE->getLength()->EvaluateAsInt(ResultR, 13320 SemaRef.getASTContext())) { 13321 if (!ResultR.Val.getInt().isOneValue()) { 13322 SemaRef.Diag(CurE->getLength()->getExprLoc(), 13323 diag::err_omp_invalid_map_this_expr); 13324 SemaRef.Diag(CurE->getLength()->getExprLoc(), 13325 diag::note_omp_invalid_length_on_this_ptr_mapping); 13326 } 13327 } 13328 if (CurE->getLowerBound() && CurE->getLowerBound()->EvaluateAsInt( 13329 ResultL, SemaRef.getASTContext())) { 13330 if (!ResultL.Val.getInt().isNullValue()) { 13331 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(), 13332 diag::err_omp_invalid_map_this_expr); 13333 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(), 13334 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping); 13335 } 13336 } 13337 RelevantExpr = TE; 13338 } 13339 13340 // Record the component - we don't have any declaration associated. 13341 CurComponents.emplace_back(CurE, nullptr); 13342 } else { 13343 if (!NoDiagnose) { 13344 // If nothing else worked, this is not a valid map clause expression. 13345 SemaRef.Diag( 13346 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 13347 << ERange; 13348 } 13349 return nullptr; 13350 } 13351 } 13352 13353 return RelevantExpr; 13354 } 13355 13356 // Return true if expression E associated with value VD has conflicts with other 13357 // map information. 13358 static bool checkMapConflicts( 13359 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 13360 bool CurrentRegionOnly, 13361 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 13362 OpenMPClauseKind CKind) { 13363 assert(VD && E); 13364 SourceLocation ELoc = E->getExprLoc(); 13365 SourceRange ERange = E->getSourceRange(); 13366 13367 // In order to easily check the conflicts we need to match each component of 13368 // the expression under test with the components of the expressions that are 13369 // already in the stack. 13370 13371 assert(!CurComponents.empty() && "Map clause expression with no components!"); 13372 assert(CurComponents.back().getAssociatedDeclaration() == VD && 13373 "Map clause expression with unexpected base!"); 13374 13375 // Variables to help detecting enclosing problems in data environment nests. 13376 bool IsEnclosedByDataEnvironmentExpr = false; 13377 const Expr *EnclosingExpr = nullptr; 13378 13379 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 13380 VD, CurrentRegionOnly, 13381 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 13382 ERange, CKind, &EnclosingExpr, 13383 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 13384 StackComponents, 13385 OpenMPClauseKind) { 13386 assert(!StackComponents.empty() && 13387 "Map clause expression with no components!"); 13388 assert(StackComponents.back().getAssociatedDeclaration() == VD && 13389 "Map clause expression with unexpected base!"); 13390 (void)VD; 13391 13392 // The whole expression in the stack. 13393 const Expr *RE = StackComponents.front().getAssociatedExpression(); 13394 13395 // Expressions must start from the same base. Here we detect at which 13396 // point both expressions diverge from each other and see if we can 13397 // detect if the memory referred to both expressions is contiguous and 13398 // do not overlap. 13399 auto CI = CurComponents.rbegin(); 13400 auto CE = CurComponents.rend(); 13401 auto SI = StackComponents.rbegin(); 13402 auto SE = StackComponents.rend(); 13403 for (; CI != CE && SI != SE; ++CI, ++SI) { 13404 13405 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 13406 // At most one list item can be an array item derived from a given 13407 // variable in map clauses of the same construct. 13408 if (CurrentRegionOnly && 13409 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 13410 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) && 13411 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 13412 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) { 13413 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 13414 diag::err_omp_multiple_array_items_in_map_clause) 13415 << CI->getAssociatedExpression()->getSourceRange(); 13416 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 13417 diag::note_used_here) 13418 << SI->getAssociatedExpression()->getSourceRange(); 13419 return true; 13420 } 13421 13422 // Do both expressions have the same kind? 13423 if (CI->getAssociatedExpression()->getStmtClass() != 13424 SI->getAssociatedExpression()->getStmtClass()) 13425 break; 13426 13427 // Are we dealing with different variables/fields? 13428 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 13429 break; 13430 } 13431 // Check if the extra components of the expressions in the enclosing 13432 // data environment are redundant for the current base declaration. 13433 // If they are, the maps completely overlap, which is legal. 13434 for (; SI != SE; ++SI) { 13435 QualType Type; 13436 if (const auto *ASE = 13437 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 13438 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 13439 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 13440 SI->getAssociatedExpression())) { 13441 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 13442 Type = 13443 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 13444 } 13445 if (Type.isNull() || Type->isAnyPointerType() || 13446 checkArrayExpressionDoesNotReferToWholeSize( 13447 SemaRef, SI->getAssociatedExpression(), Type)) 13448 break; 13449 } 13450 13451 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 13452 // List items of map clauses in the same construct must not share 13453 // original storage. 13454 // 13455 // If the expressions are exactly the same or one is a subset of the 13456 // other, it means they are sharing storage. 13457 if (CI == CE && SI == SE) { 13458 if (CurrentRegionOnly) { 13459 if (CKind == OMPC_map) { 13460 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 13461 } else { 13462 assert(CKind == OMPC_to || CKind == OMPC_from); 13463 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 13464 << ERange; 13465 } 13466 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 13467 << RE->getSourceRange(); 13468 return true; 13469 } 13470 // If we find the same expression in the enclosing data environment, 13471 // that is legal. 13472 IsEnclosedByDataEnvironmentExpr = true; 13473 return false; 13474 } 13475 13476 QualType DerivedType = 13477 std::prev(CI)->getAssociatedDeclaration()->getType(); 13478 SourceLocation DerivedLoc = 13479 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 13480 13481 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 13482 // If the type of a list item is a reference to a type T then the type 13483 // will be considered to be T for all purposes of this clause. 13484 DerivedType = DerivedType.getNonReferenceType(); 13485 13486 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 13487 // A variable for which the type is pointer and an array section 13488 // derived from that variable must not appear as list items of map 13489 // clauses of the same construct. 13490 // 13491 // Also, cover one of the cases in: 13492 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 13493 // If any part of the original storage of a list item has corresponding 13494 // storage in the device data environment, all of the original storage 13495 // must have corresponding storage in the device data environment. 13496 // 13497 if (DerivedType->isAnyPointerType()) { 13498 if (CI == CE || SI == SE) { 13499 SemaRef.Diag( 13500 DerivedLoc, 13501 diag::err_omp_pointer_mapped_along_with_derived_section) 13502 << DerivedLoc; 13503 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 13504 << RE->getSourceRange(); 13505 return true; 13506 } 13507 if (CI->getAssociatedExpression()->getStmtClass() != 13508 SI->getAssociatedExpression()->getStmtClass() || 13509 CI->getAssociatedDeclaration()->getCanonicalDecl() == 13510 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 13511 assert(CI != CE && SI != SE); 13512 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 13513 << DerivedLoc; 13514 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 13515 << RE->getSourceRange(); 13516 return true; 13517 } 13518 } 13519 13520 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 13521 // List items of map clauses in the same construct must not share 13522 // original storage. 13523 // 13524 // An expression is a subset of the other. 13525 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 13526 if (CKind == OMPC_map) { 13527 if (CI != CE || SI != SE) { 13528 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is 13529 // a pointer. 13530 auto Begin = 13531 CI != CE ? CurComponents.begin() : StackComponents.begin(); 13532 auto End = CI != CE ? CurComponents.end() : StackComponents.end(); 13533 auto It = Begin; 13534 while (It != End && !It->getAssociatedDeclaration()) 13535 std::advance(It, 1); 13536 assert(It != End && 13537 "Expected at least one component with the declaration."); 13538 if (It != Begin && It->getAssociatedDeclaration() 13539 ->getType() 13540 .getCanonicalType() 13541 ->isAnyPointerType()) { 13542 IsEnclosedByDataEnvironmentExpr = false; 13543 EnclosingExpr = nullptr; 13544 return false; 13545 } 13546 } 13547 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 13548 } else { 13549 assert(CKind == OMPC_to || CKind == OMPC_from); 13550 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 13551 << ERange; 13552 } 13553 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 13554 << RE->getSourceRange(); 13555 return true; 13556 } 13557 13558 // The current expression uses the same base as other expression in the 13559 // data environment but does not contain it completely. 13560 if (!CurrentRegionOnly && SI != SE) 13561 EnclosingExpr = RE; 13562 13563 // The current expression is a subset of the expression in the data 13564 // environment. 13565 IsEnclosedByDataEnvironmentExpr |= 13566 (!CurrentRegionOnly && CI != CE && SI == SE); 13567 13568 return false; 13569 }); 13570 13571 if (CurrentRegionOnly) 13572 return FoundError; 13573 13574 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 13575 // If any part of the original storage of a list item has corresponding 13576 // storage in the device data environment, all of the original storage must 13577 // have corresponding storage in the device data environment. 13578 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 13579 // If a list item is an element of a structure, and a different element of 13580 // the structure has a corresponding list item in the device data environment 13581 // prior to a task encountering the construct associated with the map clause, 13582 // then the list item must also have a corresponding list item in the device 13583 // data environment prior to the task encountering the construct. 13584 // 13585 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 13586 SemaRef.Diag(ELoc, 13587 diag::err_omp_original_storage_is_shared_and_does_not_contain) 13588 << ERange; 13589 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 13590 << EnclosingExpr->getSourceRange(); 13591 return true; 13592 } 13593 13594 return FoundError; 13595 } 13596 13597 // Look up the user-defined mapper given the mapper name and mapped type, and 13598 // build a reference to it. 13599 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 13600 CXXScopeSpec &MapperIdScopeSpec, 13601 const DeclarationNameInfo &MapperId, 13602 QualType Type, 13603 Expr *UnresolvedMapper) { 13604 if (MapperIdScopeSpec.isInvalid()) 13605 return ExprError(); 13606 // Find all user-defined mappers with the given MapperId. 13607 SmallVector<UnresolvedSet<8>, 4> Lookups; 13608 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName); 13609 Lookup.suppressDiagnostics(); 13610 if (S) { 13611 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) { 13612 NamedDecl *D = Lookup.getRepresentativeDecl(); 13613 while (S && !S->isDeclScope(D)) 13614 S = S->getParent(); 13615 if (S) 13616 S = S->getParent(); 13617 Lookups.emplace_back(); 13618 Lookups.back().append(Lookup.begin(), Lookup.end()); 13619 Lookup.clear(); 13620 } 13621 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) { 13622 // Extract the user-defined mappers with the given MapperId. 13623 Lookups.push_back(UnresolvedSet<8>()); 13624 for (NamedDecl *D : ULE->decls()) { 13625 auto *DMD = cast<OMPDeclareMapperDecl>(D); 13626 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation."); 13627 Lookups.back().addDecl(DMD); 13628 } 13629 } 13630 // Defer the lookup for dependent types. The results will be passed through 13631 // UnresolvedMapper on instantiation. 13632 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() || 13633 Type->isInstantiationDependentType() || 13634 Type->containsUnexpandedParameterPack() || 13635 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 13636 return !D->isInvalidDecl() && 13637 (D->getType()->isDependentType() || 13638 D->getType()->isInstantiationDependentType() || 13639 D->getType()->containsUnexpandedParameterPack()); 13640 })) { 13641 UnresolvedSet<8> URS; 13642 for (const UnresolvedSet<8> &Set : Lookups) { 13643 if (Set.empty()) 13644 continue; 13645 URS.append(Set.begin(), Set.end()); 13646 } 13647 return UnresolvedLookupExpr::Create( 13648 SemaRef.Context, /*NamingClass=*/nullptr, 13649 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId, 13650 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end()); 13651 } 13652 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 13653 // The type must be of struct, union or class type in C and C++ 13654 if (!Type->isStructureOrClassType() && !Type->isUnionType()) 13655 return ExprEmpty(); 13656 SourceLocation Loc = MapperId.getLoc(); 13657 // Perform argument dependent lookup. 13658 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet()) 13659 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups); 13660 // Return the first user-defined mapper with the desired type. 13661 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 13662 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * { 13663 if (!D->isInvalidDecl() && 13664 SemaRef.Context.hasSameType(D->getType(), Type)) 13665 return D; 13666 return nullptr; 13667 })) 13668 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 13669 // Find the first user-defined mapper with a type derived from the desired 13670 // type. 13671 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 13672 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * { 13673 if (!D->isInvalidDecl() && 13674 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) && 13675 !Type.isMoreQualifiedThan(D->getType())) 13676 return D; 13677 return nullptr; 13678 })) { 13679 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 13680 /*DetectVirtual=*/false); 13681 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) { 13682 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 13683 VD->getType().getUnqualifiedType()))) { 13684 if (SemaRef.CheckBaseClassAccess( 13685 Loc, VD->getType(), Type, Paths.front(), 13686 /*DiagID=*/0) != Sema::AR_inaccessible) { 13687 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 13688 } 13689 } 13690 } 13691 } 13692 // Report error if a mapper is specified, but cannot be found. 13693 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") { 13694 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper) 13695 << Type << MapperId.getName(); 13696 return ExprError(); 13697 } 13698 return ExprEmpty(); 13699 } 13700 13701 namespace { 13702 // Utility struct that gathers all the related lists associated with a mappable 13703 // expression. 13704 struct MappableVarListInfo { 13705 // The list of expressions. 13706 ArrayRef<Expr *> VarList; 13707 // The list of processed expressions. 13708 SmallVector<Expr *, 16> ProcessedVarList; 13709 // The mappble components for each expression. 13710 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 13711 // The base declaration of the variable. 13712 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 13713 // The reference to the user-defined mapper associated with every expression. 13714 SmallVector<Expr *, 16> UDMapperList; 13715 13716 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 13717 // We have a list of components and base declarations for each entry in the 13718 // variable list. 13719 VarComponents.reserve(VarList.size()); 13720 VarBaseDeclarations.reserve(VarList.size()); 13721 } 13722 }; 13723 } 13724 13725 // Check the validity of the provided variable list for the provided clause kind 13726 // \a CKind. In the check process the valid expressions, mappable expression 13727 // components, variables, and user-defined mappers are extracted and used to 13728 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a 13729 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec, 13730 // and \a MapperId are expected to be valid if the clause kind is 'map'. 13731 static void checkMappableExpressionList( 13732 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind, 13733 MappableVarListInfo &MVLI, SourceLocation StartLoc, 13734 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId, 13735 ArrayRef<Expr *> UnresolvedMappers, 13736 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 13737 bool IsMapTypeImplicit = false) { 13738 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 13739 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 13740 "Unexpected clause kind with mappable expressions!"); 13741 13742 // If the identifier of user-defined mapper is not specified, it is "default". 13743 // We do not change the actual name in this clause to distinguish whether a 13744 // mapper is specified explicitly, i.e., it is not explicitly specified when 13745 // MapperId.getName() is empty. 13746 if (!MapperId.getName() || MapperId.getName().isEmpty()) { 13747 auto &DeclNames = SemaRef.getASTContext().DeclarationNames; 13748 MapperId.setName(DeclNames.getIdentifier( 13749 &SemaRef.getASTContext().Idents.get("default"))); 13750 } 13751 13752 // Iterators to find the current unresolved mapper expression. 13753 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end(); 13754 bool UpdateUMIt = false; 13755 Expr *UnresolvedMapper = nullptr; 13756 13757 // Keep track of the mappable components and base declarations in this clause. 13758 // Each entry in the list is going to have a list of components associated. We 13759 // record each set of the components so that we can build the clause later on. 13760 // In the end we should have the same amount of declarations and component 13761 // lists. 13762 13763 for (Expr *RE : MVLI.VarList) { 13764 assert(RE && "Null expr in omp to/from/map clause"); 13765 SourceLocation ELoc = RE->getExprLoc(); 13766 13767 // Find the current unresolved mapper expression. 13768 if (UpdateUMIt && UMIt != UMEnd) { 13769 UMIt++; 13770 assert( 13771 UMIt != UMEnd && 13772 "Expect the size of UnresolvedMappers to match with that of VarList"); 13773 } 13774 UpdateUMIt = true; 13775 if (UMIt != UMEnd) 13776 UnresolvedMapper = *UMIt; 13777 13778 const Expr *VE = RE->IgnoreParenLValueCasts(); 13779 13780 if (VE->isValueDependent() || VE->isTypeDependent() || 13781 VE->isInstantiationDependent() || 13782 VE->containsUnexpandedParameterPack()) { 13783 // Try to find the associated user-defined mapper. 13784 ExprResult ER = buildUserDefinedMapperRef( 13785 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 13786 VE->getType().getCanonicalType(), UnresolvedMapper); 13787 if (ER.isInvalid()) 13788 continue; 13789 MVLI.UDMapperList.push_back(ER.get()); 13790 // We can only analyze this information once the missing information is 13791 // resolved. 13792 MVLI.ProcessedVarList.push_back(RE); 13793 continue; 13794 } 13795 13796 Expr *SimpleExpr = RE->IgnoreParenCasts(); 13797 13798 if (!RE->IgnoreParenImpCasts()->isLValue()) { 13799 SemaRef.Diag(ELoc, 13800 diag::err_omp_expected_named_var_member_or_array_expression) 13801 << RE->getSourceRange(); 13802 continue; 13803 } 13804 13805 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 13806 ValueDecl *CurDeclaration = nullptr; 13807 13808 // Obtain the array or member expression bases if required. Also, fill the 13809 // components array with all the components identified in the process. 13810 const Expr *BE = checkMapClauseExpressionBase( 13811 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false); 13812 if (!BE) 13813 continue; 13814 13815 assert(!CurComponents.empty() && 13816 "Invalid mappable expression information."); 13817 13818 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) { 13819 // Add store "this" pointer to class in DSAStackTy for future checking 13820 DSAS->addMappedClassesQualTypes(TE->getType()); 13821 // Try to find the associated user-defined mapper. 13822 ExprResult ER = buildUserDefinedMapperRef( 13823 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 13824 VE->getType().getCanonicalType(), UnresolvedMapper); 13825 if (ER.isInvalid()) 13826 continue; 13827 MVLI.UDMapperList.push_back(ER.get()); 13828 // Skip restriction checking for variable or field declarations 13829 MVLI.ProcessedVarList.push_back(RE); 13830 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 13831 MVLI.VarComponents.back().append(CurComponents.begin(), 13832 CurComponents.end()); 13833 MVLI.VarBaseDeclarations.push_back(nullptr); 13834 continue; 13835 } 13836 13837 // For the following checks, we rely on the base declaration which is 13838 // expected to be associated with the last component. The declaration is 13839 // expected to be a variable or a field (if 'this' is being mapped). 13840 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 13841 assert(CurDeclaration && "Null decl on map clause."); 13842 assert( 13843 CurDeclaration->isCanonicalDecl() && 13844 "Expecting components to have associated only canonical declarations."); 13845 13846 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 13847 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 13848 13849 assert((VD || FD) && "Only variables or fields are expected here!"); 13850 (void)FD; 13851 13852 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 13853 // threadprivate variables cannot appear in a map clause. 13854 // OpenMP 4.5 [2.10.5, target update Construct] 13855 // threadprivate variables cannot appear in a from clause. 13856 if (VD && DSAS->isThreadPrivate(VD)) { 13857 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 13858 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 13859 << getOpenMPClauseName(CKind); 13860 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 13861 continue; 13862 } 13863 13864 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 13865 // A list item cannot appear in both a map clause and a data-sharing 13866 // attribute clause on the same construct. 13867 13868 // Check conflicts with other map clause expressions. We check the conflicts 13869 // with the current construct separately from the enclosing data 13870 // environment, because the restrictions are different. We only have to 13871 // check conflicts across regions for the map clauses. 13872 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 13873 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 13874 break; 13875 if (CKind == OMPC_map && 13876 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 13877 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 13878 break; 13879 13880 // OpenMP 4.5 [2.10.5, target update Construct] 13881 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 13882 // If the type of a list item is a reference to a type T then the type will 13883 // be considered to be T for all purposes of this clause. 13884 auto I = llvm::find_if( 13885 CurComponents, 13886 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 13887 return MC.getAssociatedDeclaration(); 13888 }); 13889 assert(I != CurComponents.end() && "Null decl on map clause."); 13890 QualType Type = 13891 I->getAssociatedDeclaration()->getType().getNonReferenceType(); 13892 13893 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 13894 // A list item in a to or from clause must have a mappable type. 13895 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 13896 // A list item must have a mappable type. 13897 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 13898 DSAS, Type)) 13899 continue; 13900 13901 if (CKind == OMPC_map) { 13902 // target enter data 13903 // OpenMP [2.10.2, Restrictions, p. 99] 13904 // A map-type must be specified in all map clauses and must be either 13905 // to or alloc. 13906 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 13907 if (DKind == OMPD_target_enter_data && 13908 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 13909 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 13910 << (IsMapTypeImplicit ? 1 : 0) 13911 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 13912 << getOpenMPDirectiveName(DKind); 13913 continue; 13914 } 13915 13916 // target exit_data 13917 // OpenMP [2.10.3, Restrictions, p. 102] 13918 // A map-type must be specified in all map clauses and must be either 13919 // from, release, or delete. 13920 if (DKind == OMPD_target_exit_data && 13921 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 13922 MapType == OMPC_MAP_delete)) { 13923 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 13924 << (IsMapTypeImplicit ? 1 : 0) 13925 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 13926 << getOpenMPDirectiveName(DKind); 13927 continue; 13928 } 13929 13930 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 13931 // A list item cannot appear in both a map clause and a data-sharing 13932 // attribute clause on the same construct 13933 if (VD && isOpenMPTargetExecutionDirective(DKind)) { 13934 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 13935 if (isOpenMPPrivate(DVar.CKind)) { 13936 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 13937 << getOpenMPClauseName(DVar.CKind) 13938 << getOpenMPClauseName(OMPC_map) 13939 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 13940 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 13941 continue; 13942 } 13943 } 13944 } 13945 13946 // Try to find the associated user-defined mapper. 13947 ExprResult ER = buildUserDefinedMapperRef( 13948 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 13949 Type.getCanonicalType(), UnresolvedMapper); 13950 if (ER.isInvalid()) 13951 continue; 13952 MVLI.UDMapperList.push_back(ER.get()); 13953 13954 // Save the current expression. 13955 MVLI.ProcessedVarList.push_back(RE); 13956 13957 // Store the components in the stack so that they can be used to check 13958 // against other clauses later on. 13959 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 13960 /*WhereFoundClauseKind=*/OMPC_map); 13961 13962 // Save the components and declaration to create the clause. For purposes of 13963 // the clause creation, any component list that has has base 'this' uses 13964 // null as base declaration. 13965 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 13966 MVLI.VarComponents.back().append(CurComponents.begin(), 13967 CurComponents.end()); 13968 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 13969 : CurDeclaration); 13970 } 13971 } 13972 13973 OMPClause *Sema::ActOnOpenMPMapClause( 13974 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 13975 ArrayRef<SourceLocation> MapTypeModifiersLoc, 13976 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 13977 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc, 13978 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 13979 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 13980 OpenMPMapModifierKind Modifiers[] = {OMPC_MAP_MODIFIER_unknown, 13981 OMPC_MAP_MODIFIER_unknown, 13982 OMPC_MAP_MODIFIER_unknown}; 13983 SourceLocation ModifiersLoc[OMPMapClause::NumberOfModifiers]; 13984 13985 // Process map-type-modifiers, flag errors for duplicate modifiers. 13986 unsigned Count = 0; 13987 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) { 13988 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown && 13989 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) { 13990 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier); 13991 continue; 13992 } 13993 assert(Count < OMPMapClause::NumberOfModifiers && 13994 "Modifiers exceed the allowed number of map type modifiers"); 13995 Modifiers[Count] = MapTypeModifiers[I]; 13996 ModifiersLoc[Count] = MapTypeModifiersLoc[I]; 13997 ++Count; 13998 } 13999 14000 MappableVarListInfo MVLI(VarList); 14001 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc, 14002 MapperIdScopeSpec, MapperId, UnresolvedMappers, 14003 MapType, IsMapTypeImplicit); 14004 14005 // We need to produce a map clause even if we don't have variables so that 14006 // other diagnostics related with non-existing map clauses are accurate. 14007 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList, 14008 MVLI.VarBaseDeclarations, MVLI.VarComponents, 14009 MVLI.UDMapperList, Modifiers, ModifiersLoc, 14010 MapperIdScopeSpec.getWithLocInContext(Context), 14011 MapperId, MapType, IsMapTypeImplicit, MapLoc); 14012 } 14013 14014 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 14015 TypeResult ParsedType) { 14016 assert(ParsedType.isUsable()); 14017 14018 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 14019 if (ReductionType.isNull()) 14020 return QualType(); 14021 14022 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 14023 // A type name in a declare reduction directive cannot be a function type, an 14024 // array type, a reference type, or a type qualified with const, volatile or 14025 // restrict. 14026 if (ReductionType.hasQualifiers()) { 14027 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 14028 return QualType(); 14029 } 14030 14031 if (ReductionType->isFunctionType()) { 14032 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 14033 return QualType(); 14034 } 14035 if (ReductionType->isReferenceType()) { 14036 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 14037 return QualType(); 14038 } 14039 if (ReductionType->isArrayType()) { 14040 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 14041 return QualType(); 14042 } 14043 return ReductionType; 14044 } 14045 14046 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 14047 Scope *S, DeclContext *DC, DeclarationName Name, 14048 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 14049 AccessSpecifier AS, Decl *PrevDeclInScope) { 14050 SmallVector<Decl *, 8> Decls; 14051 Decls.reserve(ReductionTypes.size()); 14052 14053 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 14054 forRedeclarationInCurContext()); 14055 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 14056 // A reduction-identifier may not be re-declared in the current scope for the 14057 // same type or for a type that is compatible according to the base language 14058 // rules. 14059 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 14060 OMPDeclareReductionDecl *PrevDRD = nullptr; 14061 bool InCompoundScope = true; 14062 if (S != nullptr) { 14063 // Find previous declaration with the same name not referenced in other 14064 // declarations. 14065 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 14066 InCompoundScope = 14067 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 14068 LookupName(Lookup, S); 14069 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 14070 /*AllowInlineNamespace=*/false); 14071 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 14072 LookupResult::Filter Filter = Lookup.makeFilter(); 14073 while (Filter.hasNext()) { 14074 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 14075 if (InCompoundScope) { 14076 auto I = UsedAsPrevious.find(PrevDecl); 14077 if (I == UsedAsPrevious.end()) 14078 UsedAsPrevious[PrevDecl] = false; 14079 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 14080 UsedAsPrevious[D] = true; 14081 } 14082 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 14083 PrevDecl->getLocation(); 14084 } 14085 Filter.done(); 14086 if (InCompoundScope) { 14087 for (const auto &PrevData : UsedAsPrevious) { 14088 if (!PrevData.second) { 14089 PrevDRD = PrevData.first; 14090 break; 14091 } 14092 } 14093 } 14094 } else if (PrevDeclInScope != nullptr) { 14095 auto *PrevDRDInScope = PrevDRD = 14096 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 14097 do { 14098 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 14099 PrevDRDInScope->getLocation(); 14100 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 14101 } while (PrevDRDInScope != nullptr); 14102 } 14103 for (const auto &TyData : ReductionTypes) { 14104 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 14105 bool Invalid = false; 14106 if (I != PreviousRedeclTypes.end()) { 14107 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 14108 << TyData.first; 14109 Diag(I->second, diag::note_previous_definition); 14110 Invalid = true; 14111 } 14112 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 14113 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 14114 Name, TyData.first, PrevDRD); 14115 DC->addDecl(DRD); 14116 DRD->setAccess(AS); 14117 Decls.push_back(DRD); 14118 if (Invalid) 14119 DRD->setInvalidDecl(); 14120 else 14121 PrevDRD = DRD; 14122 } 14123 14124 return DeclGroupPtrTy::make( 14125 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 14126 } 14127 14128 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 14129 auto *DRD = cast<OMPDeclareReductionDecl>(D); 14130 14131 // Enter new function scope. 14132 PushFunctionScope(); 14133 setFunctionHasBranchProtectedScope(); 14134 getCurFunction()->setHasOMPDeclareReductionCombiner(); 14135 14136 if (S != nullptr) 14137 PushDeclContext(S, DRD); 14138 else 14139 CurContext = DRD; 14140 14141 PushExpressionEvaluationContext( 14142 ExpressionEvaluationContext::PotentiallyEvaluated); 14143 14144 QualType ReductionType = DRD->getType(); 14145 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 14146 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 14147 // uses semantics of argument handles by value, but it should be passed by 14148 // reference. C lang does not support references, so pass all parameters as 14149 // pointers. 14150 // Create 'T omp_in;' variable. 14151 VarDecl *OmpInParm = 14152 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 14153 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 14154 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 14155 // uses semantics of argument handles by value, but it should be passed by 14156 // reference. C lang does not support references, so pass all parameters as 14157 // pointers. 14158 // Create 'T omp_out;' variable. 14159 VarDecl *OmpOutParm = 14160 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 14161 if (S != nullptr) { 14162 PushOnScopeChains(OmpInParm, S); 14163 PushOnScopeChains(OmpOutParm, S); 14164 } else { 14165 DRD->addDecl(OmpInParm); 14166 DRD->addDecl(OmpOutParm); 14167 } 14168 Expr *InE = 14169 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation()); 14170 Expr *OutE = 14171 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation()); 14172 DRD->setCombinerData(InE, OutE); 14173 } 14174 14175 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 14176 auto *DRD = cast<OMPDeclareReductionDecl>(D); 14177 DiscardCleanupsInEvaluationContext(); 14178 PopExpressionEvaluationContext(); 14179 14180 PopDeclContext(); 14181 PopFunctionScopeInfo(); 14182 14183 if (Combiner != nullptr) 14184 DRD->setCombiner(Combiner); 14185 else 14186 DRD->setInvalidDecl(); 14187 } 14188 14189 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 14190 auto *DRD = cast<OMPDeclareReductionDecl>(D); 14191 14192 // Enter new function scope. 14193 PushFunctionScope(); 14194 setFunctionHasBranchProtectedScope(); 14195 14196 if (S != nullptr) 14197 PushDeclContext(S, DRD); 14198 else 14199 CurContext = DRD; 14200 14201 PushExpressionEvaluationContext( 14202 ExpressionEvaluationContext::PotentiallyEvaluated); 14203 14204 QualType ReductionType = DRD->getType(); 14205 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 14206 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 14207 // uses semantics of argument handles by value, but it should be passed by 14208 // reference. C lang does not support references, so pass all parameters as 14209 // pointers. 14210 // Create 'T omp_priv;' variable. 14211 VarDecl *OmpPrivParm = 14212 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 14213 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 14214 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 14215 // uses semantics of argument handles by value, but it should be passed by 14216 // reference. C lang does not support references, so pass all parameters as 14217 // pointers. 14218 // Create 'T omp_orig;' variable. 14219 VarDecl *OmpOrigParm = 14220 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 14221 if (S != nullptr) { 14222 PushOnScopeChains(OmpPrivParm, S); 14223 PushOnScopeChains(OmpOrigParm, S); 14224 } else { 14225 DRD->addDecl(OmpPrivParm); 14226 DRD->addDecl(OmpOrigParm); 14227 } 14228 Expr *OrigE = 14229 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation()); 14230 Expr *PrivE = 14231 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation()); 14232 DRD->setInitializerData(OrigE, PrivE); 14233 return OmpPrivParm; 14234 } 14235 14236 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 14237 VarDecl *OmpPrivParm) { 14238 auto *DRD = cast<OMPDeclareReductionDecl>(D); 14239 DiscardCleanupsInEvaluationContext(); 14240 PopExpressionEvaluationContext(); 14241 14242 PopDeclContext(); 14243 PopFunctionScopeInfo(); 14244 14245 if (Initializer != nullptr) { 14246 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 14247 } else if (OmpPrivParm->hasInit()) { 14248 DRD->setInitializer(OmpPrivParm->getInit(), 14249 OmpPrivParm->isDirectInit() 14250 ? OMPDeclareReductionDecl::DirectInit 14251 : OMPDeclareReductionDecl::CopyInit); 14252 } else { 14253 DRD->setInvalidDecl(); 14254 } 14255 } 14256 14257 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 14258 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 14259 for (Decl *D : DeclReductions.get()) { 14260 if (IsValid) { 14261 if (S) 14262 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 14263 /*AddToContext=*/false); 14264 } else { 14265 D->setInvalidDecl(); 14266 } 14267 } 14268 return DeclReductions; 14269 } 14270 14271 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) { 14272 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 14273 QualType T = TInfo->getType(); 14274 if (D.isInvalidType()) 14275 return true; 14276 14277 if (getLangOpts().CPlusPlus) { 14278 // Check that there are no default arguments (C++ only). 14279 CheckExtraCXXDefaultArguments(D); 14280 } 14281 14282 return CreateParsedType(T, TInfo); 14283 } 14284 14285 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc, 14286 TypeResult ParsedType) { 14287 assert(ParsedType.isUsable() && "Expect usable parsed mapper type"); 14288 14289 QualType MapperType = GetTypeFromParser(ParsedType.get()); 14290 assert(!MapperType.isNull() && "Expect valid mapper type"); 14291 14292 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 14293 // The type must be of struct, union or class type in C and C++ 14294 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) { 14295 Diag(TyLoc, diag::err_omp_mapper_wrong_type); 14296 return QualType(); 14297 } 14298 return MapperType; 14299 } 14300 14301 OMPDeclareMapperDecl *Sema::ActOnOpenMPDeclareMapperDirectiveStart( 14302 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType, 14303 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS, 14304 Decl *PrevDeclInScope) { 14305 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName, 14306 forRedeclarationInCurContext()); 14307 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 14308 // A mapper-identifier may not be redeclared in the current scope for the 14309 // same type or for a type that is compatible according to the base language 14310 // rules. 14311 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 14312 OMPDeclareMapperDecl *PrevDMD = nullptr; 14313 bool InCompoundScope = true; 14314 if (S != nullptr) { 14315 // Find previous declaration with the same name not referenced in other 14316 // declarations. 14317 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 14318 InCompoundScope = 14319 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 14320 LookupName(Lookup, S); 14321 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 14322 /*AllowInlineNamespace=*/false); 14323 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious; 14324 LookupResult::Filter Filter = Lookup.makeFilter(); 14325 while (Filter.hasNext()) { 14326 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next()); 14327 if (InCompoundScope) { 14328 auto I = UsedAsPrevious.find(PrevDecl); 14329 if (I == UsedAsPrevious.end()) 14330 UsedAsPrevious[PrevDecl] = false; 14331 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope()) 14332 UsedAsPrevious[D] = true; 14333 } 14334 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 14335 PrevDecl->getLocation(); 14336 } 14337 Filter.done(); 14338 if (InCompoundScope) { 14339 for (const auto &PrevData : UsedAsPrevious) { 14340 if (!PrevData.second) { 14341 PrevDMD = PrevData.first; 14342 break; 14343 } 14344 } 14345 } 14346 } else if (PrevDeclInScope) { 14347 auto *PrevDMDInScope = PrevDMD = 14348 cast<OMPDeclareMapperDecl>(PrevDeclInScope); 14349 do { 14350 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] = 14351 PrevDMDInScope->getLocation(); 14352 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope(); 14353 } while (PrevDMDInScope != nullptr); 14354 } 14355 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType()); 14356 bool Invalid = false; 14357 if (I != PreviousRedeclTypes.end()) { 14358 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition) 14359 << MapperType << Name; 14360 Diag(I->second, diag::note_previous_definition); 14361 Invalid = true; 14362 } 14363 auto *DMD = OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name, 14364 MapperType, VN, PrevDMD); 14365 DC->addDecl(DMD); 14366 DMD->setAccess(AS); 14367 if (Invalid) 14368 DMD->setInvalidDecl(); 14369 14370 // Enter new function scope. 14371 PushFunctionScope(); 14372 setFunctionHasBranchProtectedScope(); 14373 14374 CurContext = DMD; 14375 14376 return DMD; 14377 } 14378 14379 void Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(OMPDeclareMapperDecl *DMD, 14380 Scope *S, 14381 QualType MapperType, 14382 SourceLocation StartLoc, 14383 DeclarationName VN) { 14384 VarDecl *VD = buildVarDecl(*this, StartLoc, MapperType, VN.getAsString()); 14385 if (S) 14386 PushOnScopeChains(VD, S); 14387 else 14388 DMD->addDecl(VD); 14389 Expr *MapperVarRefExpr = buildDeclRefExpr(*this, VD, MapperType, StartLoc); 14390 DMD->setMapperVarRef(MapperVarRefExpr); 14391 } 14392 14393 Sema::DeclGroupPtrTy 14394 Sema::ActOnOpenMPDeclareMapperDirectiveEnd(OMPDeclareMapperDecl *D, Scope *S, 14395 ArrayRef<OMPClause *> ClauseList) { 14396 PopDeclContext(); 14397 PopFunctionScopeInfo(); 14398 14399 if (D) { 14400 if (S) 14401 PushOnScopeChains(D, S, /*AddToContext=*/false); 14402 D->CreateClauses(Context, ClauseList); 14403 } 14404 14405 return DeclGroupPtrTy::make(DeclGroupRef(D)); 14406 } 14407 14408 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 14409 SourceLocation StartLoc, 14410 SourceLocation LParenLoc, 14411 SourceLocation EndLoc) { 14412 Expr *ValExpr = NumTeams; 14413 Stmt *HelperValStmt = nullptr; 14414 14415 // OpenMP [teams Constrcut, Restrictions] 14416 // The num_teams expression must evaluate to a positive integer value. 14417 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 14418 /*StrictlyPositive=*/true)) 14419 return nullptr; 14420 14421 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14422 OpenMPDirectiveKind CaptureRegion = 14423 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams); 14424 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14425 ValExpr = MakeFullExpr(ValExpr).get(); 14426 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14427 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14428 HelperValStmt = buildPreInits(Context, Captures); 14429 } 14430 14431 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 14432 StartLoc, LParenLoc, EndLoc); 14433 } 14434 14435 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 14436 SourceLocation StartLoc, 14437 SourceLocation LParenLoc, 14438 SourceLocation EndLoc) { 14439 Expr *ValExpr = ThreadLimit; 14440 Stmt *HelperValStmt = nullptr; 14441 14442 // OpenMP [teams Constrcut, Restrictions] 14443 // The thread_limit expression must evaluate to a positive integer value. 14444 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 14445 /*StrictlyPositive=*/true)) 14446 return nullptr; 14447 14448 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14449 OpenMPDirectiveKind CaptureRegion = 14450 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit); 14451 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14452 ValExpr = MakeFullExpr(ValExpr).get(); 14453 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14454 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14455 HelperValStmt = buildPreInits(Context, Captures); 14456 } 14457 14458 return new (Context) OMPThreadLimitClause( 14459 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 14460 } 14461 14462 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 14463 SourceLocation StartLoc, 14464 SourceLocation LParenLoc, 14465 SourceLocation EndLoc) { 14466 Expr *ValExpr = Priority; 14467 14468 // OpenMP [2.9.1, task Constrcut] 14469 // The priority-value is a non-negative numerical scalar expression. 14470 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority, 14471 /*StrictlyPositive=*/false)) 14472 return nullptr; 14473 14474 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc); 14475 } 14476 14477 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 14478 SourceLocation StartLoc, 14479 SourceLocation LParenLoc, 14480 SourceLocation EndLoc) { 14481 Expr *ValExpr = Grainsize; 14482 14483 // OpenMP [2.9.2, taskloop Constrcut] 14484 // The parameter of the grainsize clause must be a positive integer 14485 // expression. 14486 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize, 14487 /*StrictlyPositive=*/true)) 14488 return nullptr; 14489 14490 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc); 14491 } 14492 14493 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 14494 SourceLocation StartLoc, 14495 SourceLocation LParenLoc, 14496 SourceLocation EndLoc) { 14497 Expr *ValExpr = NumTasks; 14498 14499 // OpenMP [2.9.2, taskloop Constrcut] 14500 // The parameter of the num_tasks clause must be a positive integer 14501 // expression. 14502 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks, 14503 /*StrictlyPositive=*/true)) 14504 return nullptr; 14505 14506 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc); 14507 } 14508 14509 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 14510 SourceLocation LParenLoc, 14511 SourceLocation EndLoc) { 14512 // OpenMP [2.13.2, critical construct, Description] 14513 // ... where hint-expression is an integer constant expression that evaluates 14514 // to a valid lock hint. 14515 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 14516 if (HintExpr.isInvalid()) 14517 return nullptr; 14518 return new (Context) 14519 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 14520 } 14521 14522 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 14523 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 14524 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 14525 SourceLocation EndLoc) { 14526 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 14527 std::string Values; 14528 Values += "'"; 14529 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 14530 Values += "'"; 14531 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 14532 << Values << getOpenMPClauseName(OMPC_dist_schedule); 14533 return nullptr; 14534 } 14535 Expr *ValExpr = ChunkSize; 14536 Stmt *HelperValStmt = nullptr; 14537 if (ChunkSize) { 14538 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 14539 !ChunkSize->isInstantiationDependent() && 14540 !ChunkSize->containsUnexpandedParameterPack()) { 14541 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 14542 ExprResult Val = 14543 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 14544 if (Val.isInvalid()) 14545 return nullptr; 14546 14547 ValExpr = Val.get(); 14548 14549 // OpenMP [2.7.1, Restrictions] 14550 // chunk_size must be a loop invariant integer expression with a positive 14551 // value. 14552 llvm::APSInt Result; 14553 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 14554 if (Result.isSigned() && !Result.isStrictlyPositive()) { 14555 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 14556 << "dist_schedule" << ChunkSize->getSourceRange(); 14557 return nullptr; 14558 } 14559 } else if (getOpenMPCaptureRegionForClause( 14560 DSAStack->getCurrentDirective(), OMPC_dist_schedule) != 14561 OMPD_unknown && 14562 !CurContext->isDependentContext()) { 14563 ValExpr = MakeFullExpr(ValExpr).get(); 14564 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14565 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14566 HelperValStmt = buildPreInits(Context, Captures); 14567 } 14568 } 14569 } 14570 14571 return new (Context) 14572 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 14573 Kind, ValExpr, HelperValStmt); 14574 } 14575 14576 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 14577 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 14578 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 14579 SourceLocation KindLoc, SourceLocation EndLoc) { 14580 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)' 14581 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) { 14582 std::string Value; 14583 SourceLocation Loc; 14584 Value += "'"; 14585 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 14586 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 14587 OMPC_DEFAULTMAP_MODIFIER_tofrom); 14588 Loc = MLoc; 14589 } else { 14590 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 14591 OMPC_DEFAULTMAP_scalar); 14592 Loc = KindLoc; 14593 } 14594 Value += "'"; 14595 Diag(Loc, diag::err_omp_unexpected_clause_value) 14596 << Value << getOpenMPClauseName(OMPC_defaultmap); 14597 return nullptr; 14598 } 14599 DSAStack->setDefaultDMAToFromScalar(StartLoc); 14600 14601 return new (Context) 14602 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 14603 } 14604 14605 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { 14606 DeclContext *CurLexicalContext = getCurLexicalContext(); 14607 if (!CurLexicalContext->isFileContext() && 14608 !CurLexicalContext->isExternCContext() && 14609 !CurLexicalContext->isExternCXXContext() && 14610 !isa<CXXRecordDecl>(CurLexicalContext) && 14611 !isa<ClassTemplateDecl>(CurLexicalContext) && 14612 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 14613 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 14614 Diag(Loc, diag::err_omp_region_not_file_context); 14615 return false; 14616 } 14617 ++DeclareTargetNestingLevel; 14618 return true; 14619 } 14620 14621 void Sema::ActOnFinishOpenMPDeclareTargetDirective() { 14622 assert(DeclareTargetNestingLevel > 0 && 14623 "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); 14624 --DeclareTargetNestingLevel; 14625 } 14626 14627 void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, 14628 CXXScopeSpec &ScopeSpec, 14629 const DeclarationNameInfo &Id, 14630 OMPDeclareTargetDeclAttr::MapTypeTy MT, 14631 NamedDeclSetType &SameDirectiveDecls) { 14632 LookupResult Lookup(*this, Id, LookupOrdinaryName); 14633 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 14634 14635 if (Lookup.isAmbiguous()) 14636 return; 14637 Lookup.suppressDiagnostics(); 14638 14639 if (!Lookup.isSingleResult()) { 14640 VarOrFuncDeclFilterCCC CCC(*this); 14641 if (TypoCorrection Corrected = 14642 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 14643 CTK_ErrorRecovery)) { 14644 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 14645 << Id.getName()); 14646 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 14647 return; 14648 } 14649 14650 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 14651 return; 14652 } 14653 14654 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 14655 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) || 14656 isa<FunctionTemplateDecl>(ND)) { 14657 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) 14658 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); 14659 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 14660 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 14661 cast<ValueDecl>(ND)); 14662 if (!Res) { 14663 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT); 14664 ND->addAttr(A); 14665 if (ASTMutationListener *ML = Context.getASTMutationListener()) 14666 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 14667 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc()); 14668 } else if (*Res != MT) { 14669 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link) 14670 << Id.getName(); 14671 } 14672 } else { 14673 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 14674 } 14675 } 14676 14677 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 14678 Sema &SemaRef, Decl *D) { 14679 if (!D || !isa<VarDecl>(D)) 14680 return; 14681 auto *VD = cast<VarDecl>(D); 14682 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 14683 return; 14684 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context); 14685 SemaRef.Diag(SL, diag::note_used_here) << SR; 14686 } 14687 14688 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 14689 Sema &SemaRef, DSAStackTy *Stack, 14690 ValueDecl *VD) { 14691 return VD->hasAttr<OMPDeclareTargetDeclAttr>() || 14692 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 14693 /*FullCheck=*/false); 14694 } 14695 14696 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 14697 SourceLocation IdLoc) { 14698 if (!D || D->isInvalidDecl()) 14699 return; 14700 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 14701 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation(); 14702 if (auto *VD = dyn_cast<VarDecl>(D)) { 14703 // Only global variables can be marked as declare target. 14704 if (!VD->isFileVarDecl() && !VD->isStaticLocal() && 14705 !VD->isStaticDataMember()) 14706 return; 14707 // 2.10.6: threadprivate variable cannot appear in a declare target 14708 // directive. 14709 if (DSAStack->isThreadPrivate(VD)) { 14710 Diag(SL, diag::err_omp_threadprivate_in_target); 14711 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 14712 return; 14713 } 14714 } 14715 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) 14716 D = FTD->getTemplatedDecl(); 14717 if (const auto *FD = dyn_cast<FunctionDecl>(D)) { 14718 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 14719 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD); 14720 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { 14721 assert(IdLoc.isValid() && "Source location is expected"); 14722 Diag(IdLoc, diag::err_omp_function_in_link_clause); 14723 Diag(FD->getLocation(), diag::note_defined_here) << FD; 14724 return; 14725 } 14726 } 14727 if (auto *VD = dyn_cast<ValueDecl>(D)) { 14728 // Problem if any with var declared with incomplete type will be reported 14729 // as normal, so no need to check it here. 14730 if ((E || !VD->getType()->isIncompleteType()) && 14731 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) 14732 return; 14733 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { 14734 // Checking declaration inside declare target region. 14735 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 14736 isa<FunctionTemplateDecl>(D)) { 14737 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 14738 Context, OMPDeclareTargetDeclAttr::MT_To); 14739 D->addAttr(A); 14740 if (ASTMutationListener *ML = Context.getASTMutationListener()) 14741 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 14742 } 14743 return; 14744 } 14745 } 14746 if (!E) 14747 return; 14748 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 14749 } 14750 14751 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList, 14752 CXXScopeSpec &MapperIdScopeSpec, 14753 DeclarationNameInfo &MapperId, 14754 const OMPVarListLocTy &Locs, 14755 ArrayRef<Expr *> UnresolvedMappers) { 14756 MappableVarListInfo MVLI(VarList); 14757 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc, 14758 MapperIdScopeSpec, MapperId, UnresolvedMappers); 14759 if (MVLI.ProcessedVarList.empty()) 14760 return nullptr; 14761 14762 return OMPToClause::Create( 14763 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 14764 MVLI.VarComponents, MVLI.UDMapperList, 14765 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 14766 } 14767 14768 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList, 14769 CXXScopeSpec &MapperIdScopeSpec, 14770 DeclarationNameInfo &MapperId, 14771 const OMPVarListLocTy &Locs, 14772 ArrayRef<Expr *> UnresolvedMappers) { 14773 MappableVarListInfo MVLI(VarList); 14774 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc, 14775 MapperIdScopeSpec, MapperId, UnresolvedMappers); 14776 if (MVLI.ProcessedVarList.empty()) 14777 return nullptr; 14778 14779 return OMPFromClause::Create( 14780 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 14781 MVLI.VarComponents, MVLI.UDMapperList, 14782 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 14783 } 14784 14785 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 14786 const OMPVarListLocTy &Locs) { 14787 MappableVarListInfo MVLI(VarList); 14788 SmallVector<Expr *, 8> PrivateCopies; 14789 SmallVector<Expr *, 8> Inits; 14790 14791 for (Expr *RefExpr : VarList) { 14792 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 14793 SourceLocation ELoc; 14794 SourceRange ERange; 14795 Expr *SimpleRefExpr = RefExpr; 14796 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14797 if (Res.second) { 14798 // It will be analyzed later. 14799 MVLI.ProcessedVarList.push_back(RefExpr); 14800 PrivateCopies.push_back(nullptr); 14801 Inits.push_back(nullptr); 14802 } 14803 ValueDecl *D = Res.first; 14804 if (!D) 14805 continue; 14806 14807 QualType Type = D->getType(); 14808 Type = Type.getNonReferenceType().getUnqualifiedType(); 14809 14810 auto *VD = dyn_cast<VarDecl>(D); 14811 14812 // Item should be a pointer or reference to pointer. 14813 if (!Type->isPointerType()) { 14814 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 14815 << 0 << RefExpr->getSourceRange(); 14816 continue; 14817 } 14818 14819 // Build the private variable and the expression that refers to it. 14820 auto VDPrivate = 14821 buildVarDecl(*this, ELoc, Type, D->getName(), 14822 D->hasAttrs() ? &D->getAttrs() : nullptr, 14823 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 14824 if (VDPrivate->isInvalidDecl()) 14825 continue; 14826 14827 CurContext->addDecl(VDPrivate); 14828 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 14829 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 14830 14831 // Add temporary variable to initialize the private copy of the pointer. 14832 VarDecl *VDInit = 14833 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 14834 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 14835 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 14836 AddInitializerToDecl(VDPrivate, 14837 DefaultLvalueConversion(VDInitRefExpr).get(), 14838 /*DirectInit=*/false); 14839 14840 // If required, build a capture to implement the privatization initialized 14841 // with the current list item value. 14842 DeclRefExpr *Ref = nullptr; 14843 if (!VD) 14844 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 14845 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 14846 PrivateCopies.push_back(VDPrivateRefExpr); 14847 Inits.push_back(VDInitRefExpr); 14848 14849 // We need to add a data sharing attribute for this variable to make sure it 14850 // is correctly captured. A variable that shows up in a use_device_ptr has 14851 // similar properties of a first private variable. 14852 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 14853 14854 // Create a mappable component for the list item. List items in this clause 14855 // only need a component. 14856 MVLI.VarBaseDeclarations.push_back(D); 14857 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 14858 MVLI.VarComponents.back().push_back( 14859 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D)); 14860 } 14861 14862 if (MVLI.ProcessedVarList.empty()) 14863 return nullptr; 14864 14865 return OMPUseDevicePtrClause::Create( 14866 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits, 14867 MVLI.VarBaseDeclarations, MVLI.VarComponents); 14868 } 14869 14870 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 14871 const OMPVarListLocTy &Locs) { 14872 MappableVarListInfo MVLI(VarList); 14873 for (Expr *RefExpr : VarList) { 14874 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 14875 SourceLocation ELoc; 14876 SourceRange ERange; 14877 Expr *SimpleRefExpr = RefExpr; 14878 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14879 if (Res.second) { 14880 // It will be analyzed later. 14881 MVLI.ProcessedVarList.push_back(RefExpr); 14882 } 14883 ValueDecl *D = Res.first; 14884 if (!D) 14885 continue; 14886 14887 QualType Type = D->getType(); 14888 // item should be a pointer or array or reference to pointer or array 14889 if (!Type.getNonReferenceType()->isPointerType() && 14890 !Type.getNonReferenceType()->isArrayType()) { 14891 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 14892 << 0 << RefExpr->getSourceRange(); 14893 continue; 14894 } 14895 14896 // Check if the declaration in the clause does not show up in any data 14897 // sharing attribute. 14898 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 14899 if (isOpenMPPrivate(DVar.CKind)) { 14900 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 14901 << getOpenMPClauseName(DVar.CKind) 14902 << getOpenMPClauseName(OMPC_is_device_ptr) 14903 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 14904 reportOriginalDsa(*this, DSAStack, D, DVar); 14905 continue; 14906 } 14907 14908 const Expr *ConflictExpr; 14909 if (DSAStack->checkMappableExprComponentListsForDecl( 14910 D, /*CurrentRegionOnly=*/true, 14911 [&ConflictExpr]( 14912 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 14913 OpenMPClauseKind) -> bool { 14914 ConflictExpr = R.front().getAssociatedExpression(); 14915 return true; 14916 })) { 14917 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 14918 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 14919 << ConflictExpr->getSourceRange(); 14920 continue; 14921 } 14922 14923 // Store the components in the stack so that they can be used to check 14924 // against other clauses later on. 14925 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D); 14926 DSAStack->addMappableExpressionComponents( 14927 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 14928 14929 // Record the expression we've just processed. 14930 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 14931 14932 // Create a mappable component for the list item. List items in this clause 14933 // only need a component. We use a null declaration to signal fields in 14934 // 'this'. 14935 assert((isa<DeclRefExpr>(SimpleRefExpr) || 14936 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 14937 "Unexpected device pointer expression!"); 14938 MVLI.VarBaseDeclarations.push_back( 14939 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 14940 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 14941 MVLI.VarComponents.back().push_back(MC); 14942 } 14943 14944 if (MVLI.ProcessedVarList.empty()) 14945 return nullptr; 14946 14947 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList, 14948 MVLI.VarBaseDeclarations, 14949 MVLI.VarComponents); 14950 } 14951 14952 OMPClause *Sema::ActOnOpenMPAllocateClause( 14953 Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc, 14954 SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 14955 if (Allocator) { 14956 // OpenMP [2.11.4 allocate Clause, Description] 14957 // allocator is an expression of omp_allocator_handle_t type. 14958 if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack)) 14959 return nullptr; 14960 14961 ExprResult AllocatorRes = DefaultLvalueConversion(Allocator); 14962 if (AllocatorRes.isInvalid()) 14963 return nullptr; 14964 AllocatorRes = PerformImplicitConversion(AllocatorRes.get(), 14965 DSAStack->getOMPAllocatorHandleT(), 14966 Sema::AA_Initializing, 14967 /*AllowExplicit=*/true); 14968 if (AllocatorRes.isInvalid()) 14969 return nullptr; 14970 Allocator = AllocatorRes.get(); 14971 } else { 14972 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions. 14973 // allocate clauses that appear on a target construct or on constructs in a 14974 // target region must specify an allocator expression unless a requires 14975 // directive with the dynamic_allocators clause is present in the same 14976 // compilation unit. 14977 if (LangOpts.OpenMPIsDevice && 14978 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 14979 targetDiag(StartLoc, diag::err_expected_allocator_expression); 14980 } 14981 // Analyze and build list of variables. 14982 SmallVector<Expr *, 8> Vars; 14983 for (Expr *RefExpr : VarList) { 14984 assert(RefExpr && "NULL expr in OpenMP private clause."); 14985 SourceLocation ELoc; 14986 SourceRange ERange; 14987 Expr *SimpleRefExpr = RefExpr; 14988 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14989 if (Res.second) { 14990 // It will be analyzed later. 14991 Vars.push_back(RefExpr); 14992 } 14993 ValueDecl *D = Res.first; 14994 if (!D) 14995 continue; 14996 14997 auto *VD = dyn_cast<VarDecl>(D); 14998 DeclRefExpr *Ref = nullptr; 14999 if (!VD && !CurContext->isDependentContext()) 15000 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 15001 Vars.push_back((VD || CurContext->isDependentContext()) 15002 ? RefExpr->IgnoreParens() 15003 : Ref); 15004 } 15005 15006 if (Vars.empty()) 15007 return nullptr; 15008 15009 return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator, 15010 ColonLoc, EndLoc, Vars); 15011 } 15012