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 bool BodyComplete = false; 147 SourceLocation InnerTeamsRegionLoc; 148 /// Reference to the taskgroup task_reduction reference expression. 149 Expr *TaskgroupReductionRef = nullptr; 150 llvm::DenseSet<QualType> MappedClassesQualTypes; 151 /// List of globals marked as declare target link in this target region 152 /// (isOpenMPTargetExecutionDirective(Directive) == true). 153 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls; 154 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 155 Scope *CurScope, SourceLocation Loc) 156 : Directive(DKind), DirectiveName(Name), CurScope(CurScope), 157 ConstructLoc(Loc) {} 158 SharingMapTy() = default; 159 }; 160 161 using StackTy = SmallVector<SharingMapTy, 4>; 162 163 /// Stack of used declaration and their data-sharing attributes. 164 DeclSAMapTy Threadprivates; 165 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr; 166 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack; 167 /// true, if check for DSA must be from parent directive, false, if 168 /// from current directive. 169 OpenMPClauseKind ClauseKindMode = OMPC_unknown; 170 Sema &SemaRef; 171 bool ForceCapturing = false; 172 /// true if all the vaiables in the target executable directives must be 173 /// captured by reference. 174 bool ForceCaptureByReferenceInTargetExecutable = false; 175 CriticalsWithHintsTy Criticals; 176 unsigned IgnoredStackElements = 0; 177 178 /// Iterators over the stack iterate in order from innermost to outermost 179 /// directive. 180 using const_iterator = StackTy::const_reverse_iterator; 181 const_iterator begin() const { 182 return Stack.empty() ? const_iterator() 183 : Stack.back().first.rbegin() + IgnoredStackElements; 184 } 185 const_iterator end() const { 186 return Stack.empty() ? const_iterator() : Stack.back().first.rend(); 187 } 188 using iterator = StackTy::reverse_iterator; 189 iterator begin() { 190 return Stack.empty() ? iterator() 191 : Stack.back().first.rbegin() + IgnoredStackElements; 192 } 193 iterator end() { 194 return Stack.empty() ? iterator() : Stack.back().first.rend(); 195 } 196 197 // Convenience operations to get at the elements of the stack. 198 199 bool isStackEmpty() const { 200 return Stack.empty() || 201 Stack.back().second != CurrentNonCapturingFunctionScope || 202 Stack.back().first.size() <= IgnoredStackElements; 203 } 204 size_t getStackSize() const { 205 return isStackEmpty() ? 0 206 : Stack.back().first.size() - IgnoredStackElements; 207 } 208 209 SharingMapTy *getTopOfStackOrNull() { 210 size_t Size = getStackSize(); 211 if (Size == 0) 212 return nullptr; 213 return &Stack.back().first[Size - 1]; 214 } 215 const SharingMapTy *getTopOfStackOrNull() const { 216 return const_cast<DSAStackTy&>(*this).getTopOfStackOrNull(); 217 } 218 SharingMapTy &getTopOfStack() { 219 assert(!isStackEmpty() && "no current directive"); 220 return *getTopOfStackOrNull(); 221 } 222 const SharingMapTy &getTopOfStack() const { 223 return const_cast<DSAStackTy&>(*this).getTopOfStack(); 224 } 225 226 SharingMapTy *getSecondOnStackOrNull() { 227 size_t Size = getStackSize(); 228 if (Size <= 1) 229 return nullptr; 230 return &Stack.back().first[Size - 2]; 231 } 232 const SharingMapTy *getSecondOnStackOrNull() const { 233 return const_cast<DSAStackTy&>(*this).getSecondOnStackOrNull(); 234 } 235 236 /// Get the stack element at a certain level (previously returned by 237 /// \c getNestingLevel). 238 /// 239 /// Note that nesting levels count from outermost to innermost, and this is 240 /// the reverse of our iteration order where new inner levels are pushed at 241 /// the front of the stack. 242 SharingMapTy &getStackElemAtLevel(unsigned Level) { 243 assert(Level < getStackSize() && "no such stack element"); 244 return Stack.back().first[Level]; 245 } 246 const SharingMapTy &getStackElemAtLevel(unsigned Level) const { 247 return const_cast<DSAStackTy&>(*this).getStackElemAtLevel(Level); 248 } 249 250 DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const; 251 252 /// Checks if the variable is a local for OpenMP region. 253 bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const; 254 255 /// Vector of previously declared requires directives 256 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls; 257 /// omp_allocator_handle_t type. 258 QualType OMPAllocatorHandleT; 259 /// Expression for the predefined allocators. 260 Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = { 261 nullptr}; 262 /// Vector of previously encountered target directives 263 SmallVector<SourceLocation, 2> TargetLocations; 264 265 public: 266 explicit DSAStackTy(Sema &S) : SemaRef(S) {} 267 268 /// Sets omp_allocator_handle_t type. 269 void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; } 270 /// Gets omp_allocator_handle_t type. 271 QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; } 272 /// Sets the given default allocator. 273 void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 274 Expr *Allocator) { 275 OMPPredefinedAllocators[AllocatorKind] = Allocator; 276 } 277 /// Returns the specified default allocator. 278 Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const { 279 return OMPPredefinedAllocators[AllocatorKind]; 280 } 281 282 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 283 OpenMPClauseKind getClauseParsingMode() const { 284 assert(isClauseParsingMode() && "Must be in clause parsing mode."); 285 return ClauseKindMode; 286 } 287 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 288 289 bool isBodyComplete() const { 290 const SharingMapTy *Top = getTopOfStackOrNull(); 291 return Top && Top->BodyComplete; 292 } 293 void setBodyComplete() { 294 getTopOfStack().BodyComplete = true; 295 } 296 297 bool isForceVarCapturing() const { return ForceCapturing; } 298 void setForceVarCapturing(bool V) { ForceCapturing = V; } 299 300 void setForceCaptureByReferenceInTargetExecutable(bool V) { 301 ForceCaptureByReferenceInTargetExecutable = V; 302 } 303 bool isForceCaptureByReferenceInTargetExecutable() const { 304 return ForceCaptureByReferenceInTargetExecutable; 305 } 306 307 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 308 Scope *CurScope, SourceLocation Loc) { 309 assert(!IgnoredStackElements && 310 "cannot change stack while ignoring elements"); 311 if (Stack.empty() || 312 Stack.back().second != CurrentNonCapturingFunctionScope) 313 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope); 314 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc); 315 Stack.back().first.back().DefaultAttrLoc = Loc; 316 } 317 318 void pop() { 319 assert(!IgnoredStackElements && 320 "cannot change stack while ignoring elements"); 321 assert(!Stack.back().first.empty() && 322 "Data-sharing attributes stack is empty!"); 323 Stack.back().first.pop_back(); 324 } 325 326 /// RAII object to temporarily leave the scope of a directive when we want to 327 /// logically operate in its parent. 328 class ParentDirectiveScope { 329 DSAStackTy &Self; 330 bool Active; 331 public: 332 ParentDirectiveScope(DSAStackTy &Self, bool Activate) 333 : Self(Self), Active(false) { 334 if (Activate) 335 enable(); 336 } 337 ~ParentDirectiveScope() { disable(); } 338 void disable() { 339 if (Active) { 340 --Self.IgnoredStackElements; 341 Active = false; 342 } 343 } 344 void enable() { 345 if (!Active) { 346 ++Self.IgnoredStackElements; 347 Active = true; 348 } 349 } 350 }; 351 352 /// Marks that we're started loop parsing. 353 void loopInit() { 354 assert(isOpenMPLoopDirective(getCurrentDirective()) && 355 "Expected loop-based directive."); 356 getTopOfStack().LoopStart = true; 357 } 358 /// Start capturing of the variables in the loop context. 359 void loopStart() { 360 assert(isOpenMPLoopDirective(getCurrentDirective()) && 361 "Expected loop-based directive."); 362 getTopOfStack().LoopStart = false; 363 } 364 /// true, if variables are captured, false otherwise. 365 bool isLoopStarted() const { 366 assert(isOpenMPLoopDirective(getCurrentDirective()) && 367 "Expected loop-based directive."); 368 return !getTopOfStack().LoopStart; 369 } 370 /// Marks (or clears) declaration as possibly loop counter. 371 void resetPossibleLoopCounter(const Decl *D = nullptr) { 372 getTopOfStack().PossiblyLoopCounter = 373 D ? D->getCanonicalDecl() : D; 374 } 375 /// Gets the possible loop counter decl. 376 const Decl *getPossiblyLoopCunter() const { 377 return getTopOfStack().PossiblyLoopCounter; 378 } 379 /// Start new OpenMP region stack in new non-capturing function. 380 void pushFunction() { 381 assert(!IgnoredStackElements && 382 "cannot change stack while ignoring elements"); 383 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction(); 384 assert(!isa<CapturingScopeInfo>(CurFnScope)); 385 CurrentNonCapturingFunctionScope = CurFnScope; 386 } 387 /// Pop region stack for non-capturing function. 388 void popFunction(const FunctionScopeInfo *OldFSI) { 389 assert(!IgnoredStackElements && 390 "cannot change stack while ignoring elements"); 391 if (!Stack.empty() && Stack.back().second == OldFSI) { 392 assert(Stack.back().first.empty()); 393 Stack.pop_back(); 394 } 395 CurrentNonCapturingFunctionScope = nullptr; 396 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) { 397 if (!isa<CapturingScopeInfo>(FSI)) { 398 CurrentNonCapturingFunctionScope = FSI; 399 break; 400 } 401 } 402 } 403 404 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) { 405 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint); 406 } 407 const std::pair<const OMPCriticalDirective *, llvm::APSInt> 408 getCriticalWithHint(const DeclarationNameInfo &Name) const { 409 auto I = Criticals.find(Name.getAsString()); 410 if (I != Criticals.end()) 411 return I->second; 412 return std::make_pair(nullptr, llvm::APSInt()); 413 } 414 /// If 'aligned' declaration for given variable \a D was not seen yet, 415 /// add it and return NULL; otherwise return previous occurrence's expression 416 /// for diagnostics. 417 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE); 418 419 /// Register specified variable as loop control variable. 420 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture); 421 /// Check if the specified variable is a loop control variable for 422 /// current region. 423 /// \return The index of the loop control variable in the list of associated 424 /// for-loops (from outer to inner). 425 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const; 426 /// Check if the specified variable is a loop control variable for 427 /// parent region. 428 /// \return The index of the loop control variable in the list of associated 429 /// for-loops (from outer to inner). 430 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const; 431 /// Get the loop control variable for the I-th loop (or nullptr) in 432 /// parent directive. 433 const ValueDecl *getParentLoopControlVariable(unsigned I) const; 434 435 /// Adds explicit data sharing attribute to the specified declaration. 436 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 437 DeclRefExpr *PrivateCopy = nullptr); 438 439 /// Adds additional information for the reduction items with the reduction id 440 /// represented as an operator. 441 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 442 BinaryOperatorKind BOK); 443 /// Adds additional information for the reduction items with the reduction id 444 /// represented as reduction identifier. 445 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 446 const Expr *ReductionRef); 447 /// Returns the location and reduction operation from the innermost parent 448 /// region for the given \p D. 449 const DSAVarData 450 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 451 BinaryOperatorKind &BOK, 452 Expr *&TaskgroupDescriptor) const; 453 /// Returns the location and reduction operation from the innermost parent 454 /// region for the given \p D. 455 const DSAVarData 456 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 457 const Expr *&ReductionRef, 458 Expr *&TaskgroupDescriptor) const; 459 /// Return reduction reference expression for the current taskgroup. 460 Expr *getTaskgroupReductionRef() const { 461 assert(getTopOfStack().Directive == OMPD_taskgroup && 462 "taskgroup reference expression requested for non taskgroup " 463 "directive."); 464 return getTopOfStack().TaskgroupReductionRef; 465 } 466 /// Checks if the given \p VD declaration is actually a taskgroup reduction 467 /// descriptor variable at the \p Level of OpenMP regions. 468 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const { 469 return getStackElemAtLevel(Level).TaskgroupReductionRef && 470 cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef) 471 ->getDecl() == VD; 472 } 473 474 /// Returns data sharing attributes from top of the stack for the 475 /// specified declaration. 476 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 477 /// Returns data-sharing attributes for the specified declaration. 478 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const; 479 /// Checks if the specified variables has data-sharing attributes which 480 /// match specified \a CPred predicate in any directive which matches \a DPred 481 /// predicate. 482 const DSAVarData 483 hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 484 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 485 bool FromParent) const; 486 /// Checks if the specified variables has data-sharing attributes which 487 /// match specified \a CPred predicate in any innermost directive which 488 /// matches \a DPred predicate. 489 const DSAVarData 490 hasInnermostDSA(ValueDecl *D, 491 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 492 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 493 bool FromParent) const; 494 /// Checks if the specified variables has explicit data-sharing 495 /// attributes which match specified \a CPred predicate at the specified 496 /// OpenMP region. 497 bool hasExplicitDSA(const ValueDecl *D, 498 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 499 unsigned Level, bool NotLastprivate = false) const; 500 501 /// Returns true if the directive at level \Level matches in the 502 /// specified \a DPred predicate. 503 bool hasExplicitDirective( 504 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 505 unsigned Level) const; 506 507 /// Finds a directive which matches specified \a DPred predicate. 508 bool hasDirective( 509 const llvm::function_ref<bool( 510 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)> 511 DPred, 512 bool FromParent) const; 513 514 /// Returns currently analyzed directive. 515 OpenMPDirectiveKind getCurrentDirective() const { 516 const SharingMapTy *Top = getTopOfStackOrNull(); 517 return Top ? Top->Directive : OMPD_unknown; 518 } 519 /// Returns directive kind at specified level. 520 OpenMPDirectiveKind getDirective(unsigned Level) const { 521 assert(!isStackEmpty() && "No directive at specified level."); 522 return getStackElemAtLevel(Level).Directive; 523 } 524 /// Returns parent directive. 525 OpenMPDirectiveKind getParentDirective() const { 526 const SharingMapTy *Parent = getSecondOnStackOrNull(); 527 return Parent ? Parent->Directive : OMPD_unknown; 528 } 529 530 /// Add requires decl to internal vector 531 void addRequiresDecl(OMPRequiresDecl *RD) { 532 RequiresDecls.push_back(RD); 533 } 534 535 /// Checks if the defined 'requires' directive has specified type of clause. 536 template <typename ClauseType> 537 bool hasRequiresDeclWithClause() { 538 return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) { 539 return llvm::any_of(D->clauselists(), [](const OMPClause *C) { 540 return isa<ClauseType>(C); 541 }); 542 }); 543 } 544 545 /// Checks for a duplicate clause amongst previously declared requires 546 /// directives 547 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const { 548 bool IsDuplicate = false; 549 for (OMPClause *CNew : ClauseList) { 550 for (const OMPRequiresDecl *D : RequiresDecls) { 551 for (const OMPClause *CPrev : D->clauselists()) { 552 if (CNew->getClauseKind() == CPrev->getClauseKind()) { 553 SemaRef.Diag(CNew->getBeginLoc(), 554 diag::err_omp_requires_clause_redeclaration) 555 << getOpenMPClauseName(CNew->getClauseKind()); 556 SemaRef.Diag(CPrev->getBeginLoc(), 557 diag::note_omp_requires_previous_clause) 558 << getOpenMPClauseName(CPrev->getClauseKind()); 559 IsDuplicate = true; 560 } 561 } 562 } 563 } 564 return IsDuplicate; 565 } 566 567 /// Add location of previously encountered target to internal vector 568 void addTargetDirLocation(SourceLocation LocStart) { 569 TargetLocations.push_back(LocStart); 570 } 571 572 // Return previously encountered target region locations. 573 ArrayRef<SourceLocation> getEncounteredTargetLocs() const { 574 return TargetLocations; 575 } 576 577 /// Set default data sharing attribute to none. 578 void setDefaultDSANone(SourceLocation Loc) { 579 getTopOfStack().DefaultAttr = DSA_none; 580 getTopOfStack().DefaultAttrLoc = Loc; 581 } 582 /// Set default data sharing attribute to shared. 583 void setDefaultDSAShared(SourceLocation Loc) { 584 getTopOfStack().DefaultAttr = DSA_shared; 585 getTopOfStack().DefaultAttrLoc = Loc; 586 } 587 /// Set default data mapping attribute to 'tofrom:scalar'. 588 void setDefaultDMAToFromScalar(SourceLocation Loc) { 589 getTopOfStack().DefaultMapAttr = DMA_tofrom_scalar; 590 getTopOfStack().DefaultMapAttrLoc = Loc; 591 } 592 593 DefaultDataSharingAttributes getDefaultDSA() const { 594 return isStackEmpty() ? DSA_unspecified 595 : getTopOfStack().DefaultAttr; 596 } 597 SourceLocation getDefaultDSALocation() const { 598 return isStackEmpty() ? SourceLocation() 599 : getTopOfStack().DefaultAttrLoc; 600 } 601 DefaultMapAttributes getDefaultDMA() const { 602 return isStackEmpty() ? DMA_unspecified 603 : getTopOfStack().DefaultMapAttr; 604 } 605 DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const { 606 return getStackElemAtLevel(Level).DefaultMapAttr; 607 } 608 SourceLocation getDefaultDMALocation() const { 609 return isStackEmpty() ? SourceLocation() 610 : getTopOfStack().DefaultMapAttrLoc; 611 } 612 613 /// Checks if the specified variable is a threadprivate. 614 bool isThreadPrivate(VarDecl *D) { 615 const DSAVarData DVar = getTopDSA(D, false); 616 return isOpenMPThreadPrivate(DVar.CKind); 617 } 618 619 /// Marks current region as ordered (it has an 'ordered' clause). 620 void setOrderedRegion(bool IsOrdered, const Expr *Param, 621 OMPOrderedClause *Clause) { 622 if (IsOrdered) 623 getTopOfStack().OrderedRegion.emplace(Param, Clause); 624 else 625 getTopOfStack().OrderedRegion.reset(); 626 } 627 /// Returns true, if region is ordered (has associated 'ordered' clause), 628 /// false - otherwise. 629 bool isOrderedRegion() const { 630 if (const SharingMapTy *Top = getTopOfStackOrNull()) 631 return Top->OrderedRegion.hasValue(); 632 return false; 633 } 634 /// Returns optional parameter for the ordered region. 635 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const { 636 if (const SharingMapTy *Top = getTopOfStackOrNull()) 637 if (Top->OrderedRegion.hasValue()) 638 return Top->OrderedRegion.getValue(); 639 return std::make_pair(nullptr, nullptr); 640 } 641 /// Returns true, if parent region is ordered (has associated 642 /// 'ordered' clause), false - otherwise. 643 bool isParentOrderedRegion() const { 644 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 645 return Parent->OrderedRegion.hasValue(); 646 return false; 647 } 648 /// Returns optional parameter for the ordered region. 649 std::pair<const Expr *, OMPOrderedClause *> 650 getParentOrderedRegionParam() const { 651 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 652 if (Parent->OrderedRegion.hasValue()) 653 return Parent->OrderedRegion.getValue(); 654 return std::make_pair(nullptr, nullptr); 655 } 656 /// Marks current region as nowait (it has a 'nowait' clause). 657 void setNowaitRegion(bool IsNowait = true) { 658 getTopOfStack().NowaitRegion = IsNowait; 659 } 660 /// Returns true, if parent region is nowait (has associated 661 /// 'nowait' clause), false - otherwise. 662 bool isParentNowaitRegion() const { 663 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 664 return Parent->NowaitRegion; 665 return false; 666 } 667 /// Marks parent region as cancel region. 668 void setParentCancelRegion(bool Cancel = true) { 669 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 670 Parent->CancelRegion |= Cancel; 671 } 672 /// Return true if current region has inner cancel construct. 673 bool isCancelRegion() const { 674 const SharingMapTy *Top = getTopOfStackOrNull(); 675 return Top ? Top->CancelRegion : false; 676 } 677 678 /// Set collapse value for the region. 679 void setAssociatedLoops(unsigned Val) { 680 getTopOfStack().AssociatedLoops = Val; 681 } 682 /// Return collapse value for region. 683 unsigned getAssociatedLoops() const { 684 const SharingMapTy *Top = getTopOfStackOrNull(); 685 return Top ? Top->AssociatedLoops : 0; 686 } 687 688 /// Marks current target region as one with closely nested teams 689 /// region. 690 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 691 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 692 Parent->InnerTeamsRegionLoc = TeamsRegionLoc; 693 } 694 /// Returns true, if current region has closely nested teams region. 695 bool hasInnerTeamsRegion() const { 696 return getInnerTeamsRegionLoc().isValid(); 697 } 698 /// Returns location of the nested teams region (if any). 699 SourceLocation getInnerTeamsRegionLoc() const { 700 const SharingMapTy *Top = getTopOfStackOrNull(); 701 return Top ? Top->InnerTeamsRegionLoc : SourceLocation(); 702 } 703 704 Scope *getCurScope() const { 705 const SharingMapTy *Top = getTopOfStackOrNull(); 706 return Top ? Top->CurScope : nullptr; 707 } 708 SourceLocation getConstructLoc() const { 709 const SharingMapTy *Top = getTopOfStackOrNull(); 710 return Top ? Top->ConstructLoc : SourceLocation(); 711 } 712 713 /// Do the check specified in \a Check to all component lists and return true 714 /// if any issue is found. 715 bool checkMappableExprComponentListsForDecl( 716 const ValueDecl *VD, bool CurrentRegionOnly, 717 const llvm::function_ref< 718 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 719 OpenMPClauseKind)> 720 Check) const { 721 if (isStackEmpty()) 722 return false; 723 auto SI = begin(); 724 auto SE = end(); 725 726 if (SI == SE) 727 return false; 728 729 if (CurrentRegionOnly) 730 SE = std::next(SI); 731 else 732 std::advance(SI, 1); 733 734 for (; SI != SE; ++SI) { 735 auto MI = SI->MappedExprComponents.find(VD); 736 if (MI != SI->MappedExprComponents.end()) 737 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 738 MI->second.Components) 739 if (Check(L, MI->second.Kind)) 740 return true; 741 } 742 return false; 743 } 744 745 /// Do the check specified in \a Check to all component lists at a given level 746 /// and return true if any issue is found. 747 bool checkMappableExprComponentListsForDeclAtLevel( 748 const ValueDecl *VD, unsigned Level, 749 const llvm::function_ref< 750 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 751 OpenMPClauseKind)> 752 Check) const { 753 if (getStackSize() <= Level) 754 return false; 755 756 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 757 auto MI = StackElem.MappedExprComponents.find(VD); 758 if (MI != StackElem.MappedExprComponents.end()) 759 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 760 MI->second.Components) 761 if (Check(L, MI->second.Kind)) 762 return true; 763 return false; 764 } 765 766 /// Create a new mappable expression component list associated with a given 767 /// declaration and initialize it with the provided list of components. 768 void addMappableExpressionComponents( 769 const ValueDecl *VD, 770 OMPClauseMappableExprCommon::MappableExprComponentListRef Components, 771 OpenMPClauseKind WhereFoundClauseKind) { 772 MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD]; 773 // Create new entry and append the new components there. 774 MEC.Components.resize(MEC.Components.size() + 1); 775 MEC.Components.back().append(Components.begin(), Components.end()); 776 MEC.Kind = WhereFoundClauseKind; 777 } 778 779 unsigned getNestingLevel() const { 780 assert(!isStackEmpty()); 781 return getStackSize() - 1; 782 } 783 void addDoacrossDependClause(OMPDependClause *C, 784 const OperatorOffsetTy &OpsOffs) { 785 SharingMapTy *Parent = getSecondOnStackOrNull(); 786 assert(Parent && isOpenMPWorksharingDirective(Parent->Directive)); 787 Parent->DoacrossDepends.try_emplace(C, OpsOffs); 788 } 789 llvm::iterator_range<DoacrossDependMapTy::const_iterator> 790 getDoacrossDependClauses() const { 791 const SharingMapTy &StackElem = getTopOfStack(); 792 if (isOpenMPWorksharingDirective(StackElem.Directive)) { 793 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends; 794 return llvm::make_range(Ref.begin(), Ref.end()); 795 } 796 return llvm::make_range(StackElem.DoacrossDepends.end(), 797 StackElem.DoacrossDepends.end()); 798 } 799 800 // Store types of classes which have been explicitly mapped 801 void addMappedClassesQualTypes(QualType QT) { 802 SharingMapTy &StackElem = getTopOfStack(); 803 StackElem.MappedClassesQualTypes.insert(QT); 804 } 805 806 // Return set of mapped classes types 807 bool isClassPreviouslyMapped(QualType QT) const { 808 const SharingMapTy &StackElem = getTopOfStack(); 809 return StackElem.MappedClassesQualTypes.count(QT) != 0; 810 } 811 812 /// Adds global declare target to the parent target region. 813 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) { 814 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 815 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link && 816 "Expected declare target link global."); 817 for (auto &Elem : *this) { 818 if (isOpenMPTargetExecutionDirective(Elem.Directive)) { 819 Elem.DeclareTargetLinkVarDecls.push_back(E); 820 return; 821 } 822 } 823 } 824 825 /// Returns the list of globals with declare target link if current directive 826 /// is target. 827 ArrayRef<DeclRefExpr *> getLinkGlobals() const { 828 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) && 829 "Expected target executable directive."); 830 return getTopOfStack().DeclareTargetLinkVarDecls; 831 } 832 }; 833 834 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) { 835 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind); 836 } 837 838 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) { 839 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) || 840 DKind == OMPD_unknown; 841 } 842 843 } // namespace 844 845 static const Expr *getExprAsWritten(const Expr *E) { 846 if (const auto *FE = dyn_cast<FullExpr>(E)) 847 E = FE->getSubExpr(); 848 849 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 850 E = MTE->GetTemporaryExpr(); 851 852 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 853 E = Binder->getSubExpr(); 854 855 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 856 E = ICE->getSubExprAsWritten(); 857 return E->IgnoreParens(); 858 } 859 860 static Expr *getExprAsWritten(Expr *E) { 861 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E))); 862 } 863 864 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) { 865 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) 866 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 867 D = ME->getMemberDecl(); 868 const auto *VD = dyn_cast<VarDecl>(D); 869 const auto *FD = dyn_cast<FieldDecl>(D); 870 if (VD != nullptr) { 871 VD = VD->getCanonicalDecl(); 872 D = VD; 873 } else { 874 assert(FD); 875 FD = FD->getCanonicalDecl(); 876 D = FD; 877 } 878 return D; 879 } 880 881 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 882 return const_cast<ValueDecl *>( 883 getCanonicalDecl(const_cast<const ValueDecl *>(D))); 884 } 885 886 DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter, 887 ValueDecl *D) const { 888 D = getCanonicalDecl(D); 889 auto *VD = dyn_cast<VarDecl>(D); 890 const auto *FD = dyn_cast<FieldDecl>(D); 891 DSAVarData DVar; 892 if (Iter == end()) { 893 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 894 // in a region but not in construct] 895 // File-scope or namespace-scope variables referenced in called routines 896 // in the region are shared unless they appear in a threadprivate 897 // directive. 898 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD)) 899 DVar.CKind = OMPC_shared; 900 901 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 902 // in a region but not in construct] 903 // Variables with static storage duration that are declared in called 904 // routines in the region are shared. 905 if (VD && VD->hasGlobalStorage()) 906 DVar.CKind = OMPC_shared; 907 908 // Non-static data members are shared by default. 909 if (FD) 910 DVar.CKind = OMPC_shared; 911 912 return DVar; 913 } 914 915 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 916 // in a Construct, C/C++, predetermined, p.1] 917 // Variables with automatic storage duration that are declared in a scope 918 // inside the construct are private. 919 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 920 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 921 DVar.CKind = OMPC_private; 922 return DVar; 923 } 924 925 DVar.DKind = Iter->Directive; 926 // Explicitly specified attributes and local variables with predetermined 927 // attributes. 928 if (Iter->SharingMap.count(D)) { 929 const DSAInfo &Data = Iter->SharingMap.lookup(D); 930 DVar.RefExpr = Data.RefExpr.getPointer(); 931 DVar.PrivateCopy = Data.PrivateCopy; 932 DVar.CKind = Data.Attributes; 933 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 934 return DVar; 935 } 936 937 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 938 // in a Construct, C/C++, implicitly determined, p.1] 939 // In a parallel or task construct, the data-sharing attributes of these 940 // variables are determined by the default clause, if present. 941 switch (Iter->DefaultAttr) { 942 case DSA_shared: 943 DVar.CKind = OMPC_shared; 944 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 945 return DVar; 946 case DSA_none: 947 return DVar; 948 case DSA_unspecified: 949 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 950 // in a Construct, implicitly determined, p.2] 951 // In a parallel construct, if no default clause is present, these 952 // variables are shared. 953 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 954 if (isOpenMPParallelDirective(DVar.DKind) || 955 isOpenMPTeamsDirective(DVar.DKind)) { 956 DVar.CKind = OMPC_shared; 957 return DVar; 958 } 959 960 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 961 // in a Construct, implicitly determined, p.4] 962 // In a task construct, if no default clause is present, a variable that in 963 // the enclosing context is determined to be shared by all implicit tasks 964 // bound to the current team is shared. 965 if (isOpenMPTaskingDirective(DVar.DKind)) { 966 DSAVarData DVarTemp; 967 const_iterator I = Iter, E = end(); 968 do { 969 ++I; 970 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 971 // Referenced in a Construct, implicitly determined, p.6] 972 // In a task construct, if no default clause is present, a variable 973 // whose data-sharing attribute is not determined by the rules above is 974 // firstprivate. 975 DVarTemp = getDSA(I, D); 976 if (DVarTemp.CKind != OMPC_shared) { 977 DVar.RefExpr = nullptr; 978 DVar.CKind = OMPC_firstprivate; 979 return DVar; 980 } 981 } while (I != E && !isImplicitTaskingRegion(I->Directive)); 982 DVar.CKind = 983 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 984 return DVar; 985 } 986 } 987 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 988 // in a Construct, implicitly determined, p.3] 989 // For constructs other than task, if no default clause is present, these 990 // variables inherit their data-sharing attributes from the enclosing 991 // context. 992 return getDSA(++Iter, D); 993 } 994 995 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D, 996 const Expr *NewDE) { 997 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 998 D = getCanonicalDecl(D); 999 SharingMapTy &StackElem = getTopOfStack(); 1000 auto It = StackElem.AlignedMap.find(D); 1001 if (It == StackElem.AlignedMap.end()) { 1002 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1003 StackElem.AlignedMap[D] = NewDE; 1004 return nullptr; 1005 } 1006 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1007 return It->second; 1008 } 1009 1010 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) { 1011 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1012 D = getCanonicalDecl(D); 1013 SharingMapTy &StackElem = getTopOfStack(); 1014 StackElem.LCVMap.try_emplace( 1015 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)); 1016 } 1017 1018 const DSAStackTy::LCDeclInfo 1019 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const { 1020 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1021 D = getCanonicalDecl(D); 1022 const SharingMapTy &StackElem = getTopOfStack(); 1023 auto It = StackElem.LCVMap.find(D); 1024 if (It != StackElem.LCVMap.end()) 1025 return It->second; 1026 return {0, nullptr}; 1027 } 1028 1029 const DSAStackTy::LCDeclInfo 1030 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const { 1031 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1032 assert(Parent && "Data-sharing attributes stack is empty"); 1033 D = getCanonicalDecl(D); 1034 auto It = Parent->LCVMap.find(D); 1035 if (It != Parent->LCVMap.end()) 1036 return It->second; 1037 return {0, nullptr}; 1038 } 1039 1040 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const { 1041 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1042 assert(Parent && "Data-sharing attributes stack is empty"); 1043 if (Parent->LCVMap.size() < I) 1044 return nullptr; 1045 for (const auto &Pair : Parent->LCVMap) 1046 if (Pair.second.first == I) 1047 return Pair.first; 1048 return nullptr; 1049 } 1050 1051 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 1052 DeclRefExpr *PrivateCopy) { 1053 D = getCanonicalDecl(D); 1054 if (A == OMPC_threadprivate) { 1055 DSAInfo &Data = Threadprivates[D]; 1056 Data.Attributes = A; 1057 Data.RefExpr.setPointer(E); 1058 Data.PrivateCopy = nullptr; 1059 } else { 1060 DSAInfo &Data = getTopOfStack().SharingMap[D]; 1061 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 1062 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 1063 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 1064 (isLoopControlVariable(D).first && A == OMPC_private)); 1065 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 1066 Data.RefExpr.setInt(/*IntVal=*/true); 1067 return; 1068 } 1069 const bool IsLastprivate = 1070 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 1071 Data.Attributes = A; 1072 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 1073 Data.PrivateCopy = PrivateCopy; 1074 if (PrivateCopy) { 1075 DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()]; 1076 Data.Attributes = A; 1077 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 1078 Data.PrivateCopy = nullptr; 1079 } 1080 } 1081 } 1082 1083 /// Build a variable declaration for OpenMP loop iteration variable. 1084 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 1085 StringRef Name, const AttrVec *Attrs = nullptr, 1086 DeclRefExpr *OrigRef = nullptr) { 1087 DeclContext *DC = SemaRef.CurContext; 1088 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 1089 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 1090 auto *Decl = 1091 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 1092 if (Attrs) { 1093 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 1094 I != E; ++I) 1095 Decl->addAttr(*I); 1096 } 1097 Decl->setImplicit(); 1098 if (OrigRef) { 1099 Decl->addAttr( 1100 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef)); 1101 } 1102 return Decl; 1103 } 1104 1105 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 1106 SourceLocation Loc, 1107 bool RefersToCapture = false) { 1108 D->setReferenced(); 1109 D->markUsed(S.Context); 1110 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 1111 SourceLocation(), D, RefersToCapture, Loc, Ty, 1112 VK_LValue); 1113 } 1114 1115 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1116 BinaryOperatorKind BOK) { 1117 D = getCanonicalDecl(D); 1118 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1119 assert( 1120 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1121 "Additional reduction info may be specified only for reduction items."); 1122 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1123 assert(ReductionData.ReductionRange.isInvalid() && 1124 getTopOfStack().Directive == OMPD_taskgroup && 1125 "Additional reduction info may be specified only once for reduction " 1126 "items."); 1127 ReductionData.set(BOK, SR); 1128 Expr *&TaskgroupReductionRef = 1129 getTopOfStack().TaskgroupReductionRef; 1130 if (!TaskgroupReductionRef) { 1131 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1132 SemaRef.Context.VoidPtrTy, ".task_red."); 1133 TaskgroupReductionRef = 1134 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1135 } 1136 } 1137 1138 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1139 const Expr *ReductionRef) { 1140 D = getCanonicalDecl(D); 1141 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1142 assert( 1143 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1144 "Additional reduction info may be specified only for reduction items."); 1145 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1146 assert(ReductionData.ReductionRange.isInvalid() && 1147 getTopOfStack().Directive == OMPD_taskgroup && 1148 "Additional reduction info may be specified only once for reduction " 1149 "items."); 1150 ReductionData.set(ReductionRef, SR); 1151 Expr *&TaskgroupReductionRef = 1152 getTopOfStack().TaskgroupReductionRef; 1153 if (!TaskgroupReductionRef) { 1154 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1155 SemaRef.Context.VoidPtrTy, ".task_red."); 1156 TaskgroupReductionRef = 1157 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1158 } 1159 } 1160 1161 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1162 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK, 1163 Expr *&TaskgroupDescriptor) const { 1164 D = getCanonicalDecl(D); 1165 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1166 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1167 const DSAInfo &Data = I->SharingMap.lookup(D); 1168 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup) 1169 continue; 1170 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1171 if (!ReductionData.ReductionOp || 1172 ReductionData.ReductionOp.is<const Expr *>()) 1173 return DSAVarData(); 1174 SR = ReductionData.ReductionRange; 1175 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>(); 1176 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1177 "expression for the descriptor is not " 1178 "set."); 1179 TaskgroupDescriptor = I->TaskgroupReductionRef; 1180 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(), 1181 Data.PrivateCopy, I->DefaultAttrLoc); 1182 } 1183 return DSAVarData(); 1184 } 1185 1186 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1187 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef, 1188 Expr *&TaskgroupDescriptor) const { 1189 D = getCanonicalDecl(D); 1190 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1191 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1192 const DSAInfo &Data = I->SharingMap.lookup(D); 1193 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup) 1194 continue; 1195 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1196 if (!ReductionData.ReductionOp || 1197 !ReductionData.ReductionOp.is<const Expr *>()) 1198 return DSAVarData(); 1199 SR = ReductionData.ReductionRange; 1200 ReductionRef = ReductionData.ReductionOp.get<const Expr *>(); 1201 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1202 "expression for the descriptor is not " 1203 "set."); 1204 TaskgroupDescriptor = I->TaskgroupReductionRef; 1205 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(), 1206 Data.PrivateCopy, I->DefaultAttrLoc); 1207 } 1208 return DSAVarData(); 1209 } 1210 1211 bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const { 1212 D = D->getCanonicalDecl(); 1213 for (const_iterator E = end(); I != E; ++I) { 1214 if (isImplicitOrExplicitTaskingRegion(I->Directive) || 1215 isOpenMPTargetExecutionDirective(I->Directive)) { 1216 Scope *TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; 1217 Scope *CurScope = getCurScope(); 1218 while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D)) 1219 CurScope = CurScope->getParent(); 1220 return CurScope != TopScope; 1221 } 1222 } 1223 return false; 1224 } 1225 1226 static bool isConstNotMutableType(Sema &SemaRef, QualType Type, 1227 bool AcceptIfMutable = true, 1228 bool *IsClassType = nullptr) { 1229 ASTContext &Context = SemaRef.getASTContext(); 1230 Type = Type.getNonReferenceType().getCanonicalType(); 1231 bool IsConstant = Type.isConstant(Context); 1232 Type = Context.getBaseElementType(Type); 1233 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus 1234 ? Type->getAsCXXRecordDecl() 1235 : nullptr; 1236 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 1237 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate()) 1238 RD = CTD->getTemplatedDecl(); 1239 if (IsClassType) 1240 *IsClassType = RD; 1241 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD && 1242 RD->hasDefinition() && RD->hasMutableFields()); 1243 } 1244 1245 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D, 1246 QualType Type, OpenMPClauseKind CKind, 1247 SourceLocation ELoc, 1248 bool AcceptIfMutable = true, 1249 bool ListItemNotVar = false) { 1250 ASTContext &Context = SemaRef.getASTContext(); 1251 bool IsClassType; 1252 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) { 1253 unsigned Diag = ListItemNotVar 1254 ? diag::err_omp_const_list_item 1255 : IsClassType ? diag::err_omp_const_not_mutable_variable 1256 : diag::err_omp_const_variable; 1257 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind); 1258 if (!ListItemNotVar && D) { 1259 const VarDecl *VD = dyn_cast<VarDecl>(D); 1260 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 1261 VarDecl::DeclarationOnly; 1262 SemaRef.Diag(D->getLocation(), 1263 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1264 << D; 1265 } 1266 return true; 1267 } 1268 return false; 1269 } 1270 1271 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, 1272 bool FromParent) { 1273 D = getCanonicalDecl(D); 1274 DSAVarData DVar; 1275 1276 auto *VD = dyn_cast<VarDecl>(D); 1277 auto TI = Threadprivates.find(D); 1278 if (TI != Threadprivates.end()) { 1279 DVar.RefExpr = TI->getSecond().RefExpr.getPointer(); 1280 DVar.CKind = OMPC_threadprivate; 1281 return DVar; 1282 } 1283 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) { 1284 DVar.RefExpr = buildDeclRefExpr( 1285 SemaRef, VD, D->getType().getNonReferenceType(), 1286 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation()); 1287 DVar.CKind = OMPC_threadprivate; 1288 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1289 return DVar; 1290 } 1291 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1292 // in a Construct, C/C++, predetermined, p.1] 1293 // Variables appearing in threadprivate directives are threadprivate. 1294 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 1295 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1296 SemaRef.getLangOpts().OpenMPUseTLS && 1297 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 1298 (VD && VD->getStorageClass() == SC_Register && 1299 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 1300 DVar.RefExpr = buildDeclRefExpr( 1301 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation()); 1302 DVar.CKind = OMPC_threadprivate; 1303 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1304 return DVar; 1305 } 1306 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD && 1307 VD->isLocalVarDeclOrParm() && !isStackEmpty() && 1308 !isLoopControlVariable(D).first) { 1309 const_iterator IterTarget = 1310 std::find_if(begin(), end(), [](const SharingMapTy &Data) { 1311 return isOpenMPTargetExecutionDirective(Data.Directive); 1312 }); 1313 if (IterTarget != end()) { 1314 const_iterator ParentIterTarget = IterTarget + 1; 1315 for (const_iterator Iter = begin(); 1316 Iter != ParentIterTarget; ++Iter) { 1317 if (isOpenMPLocal(VD, Iter)) { 1318 DVar.RefExpr = 1319 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1320 D->getLocation()); 1321 DVar.CKind = OMPC_threadprivate; 1322 return DVar; 1323 } 1324 } 1325 if (!isClauseParsingMode() || IterTarget != begin()) { 1326 auto DSAIter = IterTarget->SharingMap.find(D); 1327 if (DSAIter != IterTarget->SharingMap.end() && 1328 isOpenMPPrivate(DSAIter->getSecond().Attributes)) { 1329 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer(); 1330 DVar.CKind = OMPC_threadprivate; 1331 return DVar; 1332 } 1333 const_iterator End = end(); 1334 if (!SemaRef.isOpenMPCapturedByRef( 1335 D, std::distance(ParentIterTarget, End))) { 1336 DVar.RefExpr = 1337 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1338 IterTarget->ConstructLoc); 1339 DVar.CKind = OMPC_threadprivate; 1340 return DVar; 1341 } 1342 } 1343 } 1344 } 1345 1346 if (isStackEmpty()) 1347 // Not in OpenMP execution region and top scope was already checked. 1348 return DVar; 1349 1350 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1351 // in a Construct, C/C++, predetermined, p.4] 1352 // Static data members are shared. 1353 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1354 // in a Construct, C/C++, predetermined, p.7] 1355 // Variables with static storage duration that are declared in a scope 1356 // inside the construct are shared. 1357 if (VD && VD->isStaticDataMember()) { 1358 // Check for explicitly specified attributes. 1359 const_iterator I = begin(); 1360 const_iterator EndI = end(); 1361 if (FromParent && I != EndI) 1362 ++I; 1363 auto It = I->SharingMap.find(D); 1364 if (It != I->SharingMap.end()) { 1365 const DSAInfo &Data = It->getSecond(); 1366 DVar.RefExpr = Data.RefExpr.getPointer(); 1367 DVar.PrivateCopy = Data.PrivateCopy; 1368 DVar.CKind = Data.Attributes; 1369 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1370 DVar.DKind = I->Directive; 1371 return DVar; 1372 } 1373 1374 DVar.CKind = OMPC_shared; 1375 return DVar; 1376 } 1377 1378 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; }; 1379 // The predetermined shared attribute for const-qualified types having no 1380 // mutable members was removed after OpenMP 3.1. 1381 if (SemaRef.LangOpts.OpenMP <= 31) { 1382 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1383 // in a Construct, C/C++, predetermined, p.6] 1384 // Variables with const qualified type having no mutable member are 1385 // shared. 1386 if (isConstNotMutableType(SemaRef, D->getType())) { 1387 // Variables with const-qualified type having no mutable member may be 1388 // listed in a firstprivate clause, even if they are static data members. 1389 DSAVarData DVarTemp = hasInnermostDSA( 1390 D, 1391 [](OpenMPClauseKind C) { 1392 return C == OMPC_firstprivate || C == OMPC_shared; 1393 }, 1394 MatchesAlways, FromParent); 1395 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1396 return DVarTemp; 1397 1398 DVar.CKind = OMPC_shared; 1399 return DVar; 1400 } 1401 } 1402 1403 // Explicitly specified attributes and local variables with predetermined 1404 // attributes. 1405 const_iterator I = begin(); 1406 const_iterator EndI = end(); 1407 if (FromParent && I != EndI) 1408 ++I; 1409 auto It = I->SharingMap.find(D); 1410 if (It != I->SharingMap.end()) { 1411 const DSAInfo &Data = It->getSecond(); 1412 DVar.RefExpr = Data.RefExpr.getPointer(); 1413 DVar.PrivateCopy = Data.PrivateCopy; 1414 DVar.CKind = Data.Attributes; 1415 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1416 DVar.DKind = I->Directive; 1417 } 1418 1419 return DVar; 1420 } 1421 1422 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1423 bool FromParent) const { 1424 if (isStackEmpty()) { 1425 const_iterator I; 1426 return getDSA(I, D); 1427 } 1428 D = getCanonicalDecl(D); 1429 const_iterator StartI = begin(); 1430 const_iterator EndI = end(); 1431 if (FromParent && StartI != EndI) 1432 ++StartI; 1433 return getDSA(StartI, D); 1434 } 1435 1436 const DSAStackTy::DSAVarData 1437 DSAStackTy::hasDSA(ValueDecl *D, 1438 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1439 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1440 bool FromParent) const { 1441 if (isStackEmpty()) 1442 return {}; 1443 D = getCanonicalDecl(D); 1444 const_iterator I = begin(); 1445 const_iterator EndI = end(); 1446 if (FromParent && I != EndI) 1447 ++I; 1448 for (; I != EndI; ++I) { 1449 if (!DPred(I->Directive) && 1450 !isImplicitOrExplicitTaskingRegion(I->Directive)) 1451 continue; 1452 const_iterator NewI = I; 1453 DSAVarData DVar = getDSA(NewI, D); 1454 if (I == NewI && CPred(DVar.CKind)) 1455 return DVar; 1456 } 1457 return {}; 1458 } 1459 1460 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 1461 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1462 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1463 bool FromParent) const { 1464 if (isStackEmpty()) 1465 return {}; 1466 D = getCanonicalDecl(D); 1467 const_iterator StartI = begin(); 1468 const_iterator EndI = end(); 1469 if (FromParent && StartI != EndI) 1470 ++StartI; 1471 if (StartI == EndI || !DPred(StartI->Directive)) 1472 return {}; 1473 const_iterator NewI = StartI; 1474 DSAVarData DVar = getDSA(NewI, D); 1475 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData(); 1476 } 1477 1478 bool DSAStackTy::hasExplicitDSA( 1479 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1480 unsigned Level, bool NotLastprivate) const { 1481 if (getStackSize() <= Level) 1482 return false; 1483 D = getCanonicalDecl(D); 1484 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1485 auto I = StackElem.SharingMap.find(D); 1486 if (I != StackElem.SharingMap.end() && 1487 I->getSecond().RefExpr.getPointer() && 1488 CPred(I->getSecond().Attributes) && 1489 (!NotLastprivate || !I->getSecond().RefExpr.getInt())) 1490 return true; 1491 // Check predetermined rules for the loop control variables. 1492 auto LI = StackElem.LCVMap.find(D); 1493 if (LI != StackElem.LCVMap.end()) 1494 return CPred(OMPC_private); 1495 return false; 1496 } 1497 1498 bool DSAStackTy::hasExplicitDirective( 1499 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1500 unsigned Level) const { 1501 if (getStackSize() <= Level) 1502 return false; 1503 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1504 return DPred(StackElem.Directive); 1505 } 1506 1507 bool DSAStackTy::hasDirective( 1508 const llvm::function_ref<bool(OpenMPDirectiveKind, 1509 const DeclarationNameInfo &, SourceLocation)> 1510 DPred, 1511 bool FromParent) const { 1512 // We look only in the enclosing region. 1513 size_t Skip = FromParent ? 2 : 1; 1514 for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end(); 1515 I != E; ++I) { 1516 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 1517 return true; 1518 } 1519 return false; 1520 } 1521 1522 void Sema::InitDataSharingAttributesStack() { 1523 VarDataSharingAttributesStack = new DSAStackTy(*this); 1524 } 1525 1526 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 1527 1528 void Sema::pushOpenMPFunctionRegion() { 1529 DSAStack->pushFunction(); 1530 } 1531 1532 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) { 1533 DSAStack->popFunction(OldFSI); 1534 } 1535 1536 static bool isOpenMPDeviceDelayedContext(Sema &S) { 1537 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice && 1538 "Expected OpenMP device compilation."); 1539 return !S.isInOpenMPTargetExecutionDirective() && 1540 !S.isInOpenMPDeclareTargetContext(); 1541 } 1542 1543 /// Do we know that we will eventually codegen the given function? 1544 static bool isKnownEmitted(Sema &S, FunctionDecl *FD) { 1545 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice && 1546 "Expected OpenMP device compilation."); 1547 // Templates are emitted when they're instantiated. 1548 if (FD->isDependentContext()) 1549 return false; 1550 1551 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 1552 FD->getCanonicalDecl())) 1553 return true; 1554 1555 // Otherwise, the function is known-emitted if it's in our set of 1556 // known-emitted functions. 1557 return S.DeviceKnownEmittedFns.count(FD) > 0; 1558 } 1559 1560 Sema::DeviceDiagBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc, 1561 unsigned DiagID) { 1562 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice && 1563 "Expected OpenMP device compilation."); 1564 return DeviceDiagBuilder((isOpenMPDeviceDelayedContext(*this) && 1565 !isKnownEmitted(*this, getCurFunctionDecl())) 1566 ? DeviceDiagBuilder::K_Deferred 1567 : DeviceDiagBuilder::K_Immediate, 1568 Loc, DiagID, getCurFunctionDecl(), *this); 1569 } 1570 1571 void Sema::checkOpenMPDeviceFunction(SourceLocation Loc, FunctionDecl *Callee) { 1572 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice && 1573 "Expected OpenMP device compilation."); 1574 assert(Callee && "Callee may not be null."); 1575 FunctionDecl *Caller = getCurFunctionDecl(); 1576 1577 // If the caller is known-emitted, mark the callee as known-emitted. 1578 // Otherwise, mark the call in our call graph so we can traverse it later. 1579 if (!isOpenMPDeviceDelayedContext(*this) || 1580 (Caller && isKnownEmitted(*this, Caller))) 1581 markKnownEmitted(*this, Caller, Callee, Loc, isKnownEmitted); 1582 else if (Caller) 1583 DeviceCallGraph[Caller].insert({Callee, Loc}); 1584 } 1585 1586 void Sema::checkOpenMPDeviceExpr(const Expr *E) { 1587 assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice && 1588 "OpenMP device compilation mode is expected."); 1589 QualType Ty = E->getType(); 1590 if ((Ty->isFloat16Type() && !Context.getTargetInfo().hasFloat16Type()) || 1591 ((Ty->isFloat128Type() || 1592 (Ty->isRealFloatingType() && Context.getTypeSize(Ty) == 128)) && 1593 !Context.getTargetInfo().hasFloat128Type()) || 1594 (Ty->isIntegerType() && Context.getTypeSize(Ty) == 128 && 1595 !Context.getTargetInfo().hasInt128Type())) 1596 targetDiag(E->getExprLoc(), diag::err_type_unsupported) 1597 << Ty << E->getSourceRange(); 1598 } 1599 1600 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const { 1601 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1602 1603 ASTContext &Ctx = getASTContext(); 1604 bool IsByRef = true; 1605 1606 // Find the directive that is associated with the provided scope. 1607 D = cast<ValueDecl>(D->getCanonicalDecl()); 1608 QualType Ty = D->getType(); 1609 1610 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 1611 // This table summarizes how a given variable should be passed to the device 1612 // given its type and the clauses where it appears. This table is based on 1613 // the description in OpenMP 4.5 [2.10.4, target Construct] and 1614 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 1615 // 1616 // ========================================================================= 1617 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 1618 // | |(tofrom:scalar)| | pvt | | | | 1619 // ========================================================================= 1620 // | scl | | | | - | | bycopy| 1621 // | scl | | - | x | - | - | bycopy| 1622 // | scl | | x | - | - | - | null | 1623 // | scl | x | | | - | | byref | 1624 // | scl | x | - | x | - | - | bycopy| 1625 // | scl | x | x | - | - | - | null | 1626 // | scl | | - | - | - | x | byref | 1627 // | scl | x | - | - | - | x | byref | 1628 // 1629 // | agg | n.a. | | | - | | byref | 1630 // | agg | n.a. | - | x | - | - | byref | 1631 // | agg | n.a. | x | - | - | - | null | 1632 // | agg | n.a. | - | - | - | x | byref | 1633 // | agg | n.a. | - | - | - | x[] | byref | 1634 // 1635 // | ptr | n.a. | | | - | | bycopy| 1636 // | ptr | n.a. | - | x | - | - | bycopy| 1637 // | ptr | n.a. | x | - | - | - | null | 1638 // | ptr | n.a. | - | - | - | x | byref | 1639 // | ptr | n.a. | - | - | - | x[] | bycopy| 1640 // | ptr | n.a. | - | - | x | | bycopy| 1641 // | ptr | n.a. | - | - | x | x | bycopy| 1642 // | ptr | n.a. | - | - | x | x[] | bycopy| 1643 // ========================================================================= 1644 // Legend: 1645 // scl - scalar 1646 // ptr - pointer 1647 // agg - aggregate 1648 // x - applies 1649 // - - invalid in this combination 1650 // [] - mapped with an array section 1651 // byref - should be mapped by reference 1652 // byval - should be mapped by value 1653 // null - initialize a local variable to null on the device 1654 // 1655 // Observations: 1656 // - All scalar declarations that show up in a map clause have to be passed 1657 // by reference, because they may have been mapped in the enclosing data 1658 // environment. 1659 // - If the scalar value does not fit the size of uintptr, it has to be 1660 // passed by reference, regardless the result in the table above. 1661 // - For pointers mapped by value that have either an implicit map or an 1662 // array section, the runtime library may pass the NULL value to the 1663 // device instead of the value passed to it by the compiler. 1664 1665 if (Ty->isReferenceType()) 1666 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 1667 1668 // Locate map clauses and see if the variable being captured is referred to 1669 // in any of those clauses. Here we only care about variables, not fields, 1670 // because fields are part of aggregates. 1671 bool IsVariableUsedInMapClause = false; 1672 bool IsVariableAssociatedWithSection = false; 1673 1674 DSAStack->checkMappableExprComponentListsForDeclAtLevel( 1675 D, Level, 1676 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D]( 1677 OMPClauseMappableExprCommon::MappableExprComponentListRef 1678 MapExprComponents, 1679 OpenMPClauseKind WhereFoundClauseKind) { 1680 // Only the map clause information influences how a variable is 1681 // captured. E.g. is_device_ptr does not require changing the default 1682 // behavior. 1683 if (WhereFoundClauseKind != OMPC_map) 1684 return false; 1685 1686 auto EI = MapExprComponents.rbegin(); 1687 auto EE = MapExprComponents.rend(); 1688 1689 assert(EI != EE && "Invalid map expression!"); 1690 1691 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 1692 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 1693 1694 ++EI; 1695 if (EI == EE) 1696 return false; 1697 1698 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 1699 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 1700 isa<MemberExpr>(EI->getAssociatedExpression())) { 1701 IsVariableAssociatedWithSection = true; 1702 // There is nothing more we need to know about this variable. 1703 return true; 1704 } 1705 1706 // Keep looking for more map info. 1707 return false; 1708 }); 1709 1710 if (IsVariableUsedInMapClause) { 1711 // If variable is identified in a map clause it is always captured by 1712 // reference except if it is a pointer that is dereferenced somehow. 1713 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 1714 } else { 1715 // By default, all the data that has a scalar type is mapped by copy 1716 // (except for reduction variables). 1717 IsByRef = 1718 (DSAStack->isForceCaptureByReferenceInTargetExecutable() && 1719 !Ty->isAnyPointerType()) || 1720 !Ty->isScalarType() || 1721 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar || 1722 DSAStack->hasExplicitDSA( 1723 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level); 1724 } 1725 } 1726 1727 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 1728 IsByRef = 1729 !DSAStack->hasExplicitDSA( 1730 D, 1731 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; }, 1732 Level, /*NotLastprivate=*/true) && 1733 // If the variable is artificial and must be captured by value - try to 1734 // capture by value. 1735 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() && 1736 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()); 1737 } 1738 1739 // When passing data by copy, we need to make sure it fits the uintptr size 1740 // and alignment, because the runtime library only deals with uintptr types. 1741 // If it does not fit the uintptr size, we need to pass the data by reference 1742 // instead. 1743 if (!IsByRef && 1744 (Ctx.getTypeSizeInChars(Ty) > 1745 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 1746 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 1747 IsByRef = true; 1748 } 1749 1750 return IsByRef; 1751 } 1752 1753 unsigned Sema::getOpenMPNestingLevel() const { 1754 assert(getLangOpts().OpenMP); 1755 return DSAStack->getNestingLevel(); 1756 } 1757 1758 bool Sema::isInOpenMPTargetExecutionDirective() const { 1759 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) && 1760 !DSAStack->isClauseParsingMode()) || 1761 DSAStack->hasDirective( 1762 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 1763 SourceLocation) -> bool { 1764 return isOpenMPTargetExecutionDirective(K); 1765 }, 1766 false); 1767 } 1768 1769 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo, 1770 unsigned StopAt) { 1771 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1772 D = getCanonicalDecl(D); 1773 1774 // If we want to determine whether the variable should be captured from the 1775 // perspective of the current capturing scope, and we've already left all the 1776 // capturing scopes of the top directive on the stack, check from the 1777 // perspective of its parent directive (if any) instead. 1778 DSAStackTy::ParentDirectiveScope InParentDirectiveRAII( 1779 *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete()); 1780 1781 // If we are attempting to capture a global variable in a directive with 1782 // 'target' we return true so that this global is also mapped to the device. 1783 // 1784 auto *VD = dyn_cast<VarDecl>(D); 1785 if (VD && !VD->hasLocalStorage() && 1786 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) { 1787 if (isInOpenMPDeclareTargetContext()) { 1788 // Try to mark variable as declare target if it is used in capturing 1789 // regions. 1790 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 1791 checkDeclIsAllowedInOpenMPTarget(nullptr, VD); 1792 return nullptr; 1793 } else if (isInOpenMPTargetExecutionDirective()) { 1794 // If the declaration is enclosed in a 'declare target' directive, 1795 // then it should not be captured. 1796 // 1797 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 1798 return nullptr; 1799 return VD; 1800 } 1801 } 1802 1803 if (CheckScopeInfo) { 1804 bool OpenMPFound = false; 1805 for (unsigned I = StopAt + 1; I > 0; --I) { 1806 FunctionScopeInfo *FSI = FunctionScopes[I - 1]; 1807 if(!isa<CapturingScopeInfo>(FSI)) 1808 return nullptr; 1809 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 1810 if (RSI->CapRegionKind == CR_OpenMP) { 1811 OpenMPFound = true; 1812 break; 1813 } 1814 } 1815 if (!OpenMPFound) 1816 return nullptr; 1817 } 1818 1819 if (DSAStack->getCurrentDirective() != OMPD_unknown && 1820 (!DSAStack->isClauseParsingMode() || 1821 DSAStack->getParentDirective() != OMPD_unknown)) { 1822 auto &&Info = DSAStack->isLoopControlVariable(D); 1823 if (Info.first || 1824 (VD && VD->hasLocalStorage() && 1825 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) || 1826 (VD && DSAStack->isForceVarCapturing())) 1827 return VD ? VD : Info.second; 1828 DSAStackTy::DSAVarData DVarPrivate = 1829 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 1830 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind)) 1831 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1832 // Threadprivate variables must not be captured. 1833 if (isOpenMPThreadPrivate(DVarPrivate.CKind)) 1834 return nullptr; 1835 // The variable is not private or it is the variable in the directive with 1836 // default(none) clause and not used in any clause. 1837 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, 1838 [](OpenMPDirectiveKind) { return true; }, 1839 DSAStack->isClauseParsingMode()); 1840 if (DVarPrivate.CKind != OMPC_unknown || 1841 (VD && DSAStack->getDefaultDSA() == DSA_none)) 1842 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 1843 } 1844 return nullptr; 1845 } 1846 1847 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex, 1848 unsigned Level) const { 1849 SmallVector<OpenMPDirectiveKind, 4> Regions; 1850 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level)); 1851 FunctionScopesIndex -= Regions.size(); 1852 } 1853 1854 void Sema::startOpenMPLoop() { 1855 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 1856 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) 1857 DSAStack->loopInit(); 1858 } 1859 1860 bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const { 1861 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1862 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 1863 if (DSAStack->getAssociatedLoops() > 0 && 1864 !DSAStack->isLoopStarted()) { 1865 DSAStack->resetPossibleLoopCounter(D); 1866 DSAStack->loopStart(); 1867 return true; 1868 } 1869 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() || 1870 DSAStack->isLoopControlVariable(D).first) && 1871 !DSAStack->hasExplicitDSA( 1872 D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) && 1873 !isOpenMPSimdDirective(DSAStack->getCurrentDirective())) 1874 return true; 1875 } 1876 return DSAStack->hasExplicitDSA( 1877 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) || 1878 (DSAStack->isClauseParsingMode() && 1879 DSAStack->getClauseParsingMode() == OMPC_private) || 1880 // Consider taskgroup reduction descriptor variable a private to avoid 1881 // possible capture in the region. 1882 (DSAStack->hasExplicitDirective( 1883 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; }, 1884 Level) && 1885 DSAStack->isTaskgroupReductionRef(D, Level)); 1886 } 1887 1888 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D, 1889 unsigned Level) { 1890 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1891 D = getCanonicalDecl(D); 1892 OpenMPClauseKind OMPC = OMPC_unknown; 1893 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) { 1894 const unsigned NewLevel = I - 1; 1895 if (DSAStack->hasExplicitDSA(D, 1896 [&OMPC](const OpenMPClauseKind K) { 1897 if (isOpenMPPrivate(K)) { 1898 OMPC = K; 1899 return true; 1900 } 1901 return false; 1902 }, 1903 NewLevel)) 1904 break; 1905 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel( 1906 D, NewLevel, 1907 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 1908 OpenMPClauseKind) { return true; })) { 1909 OMPC = OMPC_map; 1910 break; 1911 } 1912 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 1913 NewLevel)) { 1914 OMPC = OMPC_map; 1915 if (D->getType()->isScalarType() && 1916 DSAStack->getDefaultDMAAtLevel(NewLevel) != 1917 DefaultMapAttributes::DMA_tofrom_scalar) 1918 OMPC = OMPC_firstprivate; 1919 break; 1920 } 1921 } 1922 if (OMPC != OMPC_unknown) 1923 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC)); 1924 } 1925 1926 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, 1927 unsigned Level) const { 1928 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1929 // Return true if the current level is no longer enclosed in a target region. 1930 1931 const auto *VD = dyn_cast<VarDecl>(D); 1932 return VD && !VD->hasLocalStorage() && 1933 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 1934 Level); 1935 } 1936 1937 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 1938 1939 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 1940 const DeclarationNameInfo &DirName, 1941 Scope *CurScope, SourceLocation Loc) { 1942 DSAStack->push(DKind, DirName, CurScope, Loc); 1943 PushExpressionEvaluationContext( 1944 ExpressionEvaluationContext::PotentiallyEvaluated); 1945 } 1946 1947 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 1948 DSAStack->setClauseParsingMode(K); 1949 } 1950 1951 void Sema::EndOpenMPClause() { 1952 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 1953 } 1954 1955 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 1956 ArrayRef<OMPClause *> Clauses); 1957 1958 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 1959 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 1960 // A variable of class type (or array thereof) that appears in a lastprivate 1961 // clause requires an accessible, unambiguous default constructor for the 1962 // class type, unless the list item is also specified in a firstprivate 1963 // clause. 1964 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 1965 for (OMPClause *C : D->clauses()) { 1966 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 1967 SmallVector<Expr *, 8> PrivateCopies; 1968 for (Expr *DE : Clause->varlists()) { 1969 if (DE->isValueDependent() || DE->isTypeDependent()) { 1970 PrivateCopies.push_back(nullptr); 1971 continue; 1972 } 1973 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 1974 auto *VD = cast<VarDecl>(DRE->getDecl()); 1975 QualType Type = VD->getType().getNonReferenceType(); 1976 const DSAStackTy::DSAVarData DVar = 1977 DSAStack->getTopDSA(VD, /*FromParent=*/false); 1978 if (DVar.CKind == OMPC_lastprivate) { 1979 // Generate helper private variable and initialize it with the 1980 // default value. The address of the original variable is replaced 1981 // by the address of the new private variable in CodeGen. This new 1982 // variable is not added to IdResolver, so the code in the OpenMP 1983 // region uses original variable for proper diagnostics. 1984 VarDecl *VDPrivate = buildVarDecl( 1985 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 1986 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE); 1987 ActOnUninitializedDecl(VDPrivate); 1988 if (VDPrivate->isInvalidDecl()) { 1989 PrivateCopies.push_back(nullptr); 1990 continue; 1991 } 1992 PrivateCopies.push_back(buildDeclRefExpr( 1993 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 1994 } else { 1995 // The variable is also a firstprivate, so initialization sequence 1996 // for private copy is generated already. 1997 PrivateCopies.push_back(nullptr); 1998 } 1999 } 2000 Clause->setPrivateCopies(PrivateCopies); 2001 } 2002 } 2003 // Check allocate clauses. 2004 if (!CurContext->isDependentContext()) 2005 checkAllocateClauses(*this, DSAStack, D->clauses()); 2006 } 2007 2008 DSAStack->pop(); 2009 DiscardCleanupsInEvaluationContext(); 2010 PopExpressionEvaluationContext(); 2011 } 2012 2013 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 2014 Expr *NumIterations, Sema &SemaRef, 2015 Scope *S, DSAStackTy *Stack); 2016 2017 namespace { 2018 2019 class VarDeclFilterCCC final : public CorrectionCandidateCallback { 2020 private: 2021 Sema &SemaRef; 2022 2023 public: 2024 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 2025 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2026 NamedDecl *ND = Candidate.getCorrectionDecl(); 2027 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 2028 return VD->hasGlobalStorage() && 2029 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2030 SemaRef.getCurScope()); 2031 } 2032 return false; 2033 } 2034 2035 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2036 return llvm::make_unique<VarDeclFilterCCC>(*this); 2037 } 2038 2039 }; 2040 2041 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback { 2042 private: 2043 Sema &SemaRef; 2044 2045 public: 2046 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 2047 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2048 NamedDecl *ND = Candidate.getCorrectionDecl(); 2049 if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) || 2050 isa<FunctionDecl>(ND))) { 2051 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2052 SemaRef.getCurScope()); 2053 } 2054 return false; 2055 } 2056 2057 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2058 return llvm::make_unique<VarOrFuncDeclFilterCCC>(*this); 2059 } 2060 }; 2061 2062 } // namespace 2063 2064 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 2065 CXXScopeSpec &ScopeSpec, 2066 const DeclarationNameInfo &Id, 2067 OpenMPDirectiveKind Kind) { 2068 LookupResult Lookup(*this, Id, LookupOrdinaryName); 2069 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 2070 2071 if (Lookup.isAmbiguous()) 2072 return ExprError(); 2073 2074 VarDecl *VD; 2075 if (!Lookup.isSingleResult()) { 2076 VarDeclFilterCCC CCC(*this); 2077 if (TypoCorrection Corrected = 2078 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 2079 CTK_ErrorRecovery)) { 2080 diagnoseTypo(Corrected, 2081 PDiag(Lookup.empty() 2082 ? diag::err_undeclared_var_use_suggest 2083 : diag::err_omp_expected_var_arg_suggest) 2084 << Id.getName()); 2085 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 2086 } else { 2087 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 2088 : diag::err_omp_expected_var_arg) 2089 << Id.getName(); 2090 return ExprError(); 2091 } 2092 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) { 2093 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 2094 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 2095 return ExprError(); 2096 } 2097 Lookup.suppressDiagnostics(); 2098 2099 // OpenMP [2.9.2, Syntax, C/C++] 2100 // Variables must be file-scope, namespace-scope, or static block-scope. 2101 if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) { 2102 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 2103 << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal(); 2104 bool IsDecl = 2105 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2106 Diag(VD->getLocation(), 2107 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2108 << VD; 2109 return ExprError(); 2110 } 2111 2112 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 2113 NamedDecl *ND = CanonicalVD; 2114 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 2115 // A threadprivate directive for file-scope variables must appear outside 2116 // any definition or declaration. 2117 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 2118 !getCurLexicalContext()->isTranslationUnit()) { 2119 Diag(Id.getLoc(), diag::err_omp_var_scope) 2120 << getOpenMPDirectiveName(Kind) << VD; 2121 bool IsDecl = 2122 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2123 Diag(VD->getLocation(), 2124 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2125 << VD; 2126 return ExprError(); 2127 } 2128 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 2129 // A threadprivate directive for static class member variables must appear 2130 // in the class definition, in the same scope in which the member 2131 // variables are declared. 2132 if (CanonicalVD->isStaticDataMember() && 2133 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 2134 Diag(Id.getLoc(), diag::err_omp_var_scope) 2135 << getOpenMPDirectiveName(Kind) << VD; 2136 bool IsDecl = 2137 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2138 Diag(VD->getLocation(), 2139 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2140 << VD; 2141 return ExprError(); 2142 } 2143 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 2144 // A threadprivate directive for namespace-scope variables must appear 2145 // outside any definition or declaration other than the namespace 2146 // definition itself. 2147 if (CanonicalVD->getDeclContext()->isNamespace() && 2148 (!getCurLexicalContext()->isFileContext() || 2149 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 2150 Diag(Id.getLoc(), diag::err_omp_var_scope) 2151 << getOpenMPDirectiveName(Kind) << VD; 2152 bool IsDecl = 2153 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2154 Diag(VD->getLocation(), 2155 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2156 << VD; 2157 return ExprError(); 2158 } 2159 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 2160 // A threadprivate directive for static block-scope variables must appear 2161 // in the scope of the variable and not in a nested scope. 2162 if (CanonicalVD->isLocalVarDecl() && CurScope && 2163 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 2164 Diag(Id.getLoc(), diag::err_omp_var_scope) 2165 << getOpenMPDirectiveName(Kind) << VD; 2166 bool IsDecl = 2167 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2168 Diag(VD->getLocation(), 2169 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2170 << VD; 2171 return ExprError(); 2172 } 2173 2174 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 2175 // A threadprivate directive must lexically precede all references to any 2176 // of the variables in its list. 2177 if (Kind == OMPD_threadprivate && VD->isUsed() && 2178 !DSAStack->isThreadPrivate(VD)) { 2179 Diag(Id.getLoc(), diag::err_omp_var_used) 2180 << getOpenMPDirectiveName(Kind) << VD; 2181 return ExprError(); 2182 } 2183 2184 QualType ExprType = VD->getType().getNonReferenceType(); 2185 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 2186 SourceLocation(), VD, 2187 /*RefersToEnclosingVariableOrCapture=*/false, 2188 Id.getLoc(), ExprType, VK_LValue); 2189 } 2190 2191 Sema::DeclGroupPtrTy 2192 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 2193 ArrayRef<Expr *> VarList) { 2194 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 2195 CurContext->addDecl(D); 2196 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2197 } 2198 return nullptr; 2199 } 2200 2201 namespace { 2202 class LocalVarRefChecker final 2203 : public ConstStmtVisitor<LocalVarRefChecker, bool> { 2204 Sema &SemaRef; 2205 2206 public: 2207 bool VisitDeclRefExpr(const DeclRefExpr *E) { 2208 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2209 if (VD->hasLocalStorage()) { 2210 SemaRef.Diag(E->getBeginLoc(), 2211 diag::err_omp_local_var_in_threadprivate_init) 2212 << E->getSourceRange(); 2213 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 2214 << VD << VD->getSourceRange(); 2215 return true; 2216 } 2217 } 2218 return false; 2219 } 2220 bool VisitStmt(const Stmt *S) { 2221 for (const Stmt *Child : S->children()) { 2222 if (Child && Visit(Child)) 2223 return true; 2224 } 2225 return false; 2226 } 2227 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 2228 }; 2229 } // namespace 2230 2231 OMPThreadPrivateDecl * 2232 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 2233 SmallVector<Expr *, 8> Vars; 2234 for (Expr *RefExpr : VarList) { 2235 auto *DE = cast<DeclRefExpr>(RefExpr); 2236 auto *VD = cast<VarDecl>(DE->getDecl()); 2237 SourceLocation ILoc = DE->getExprLoc(); 2238 2239 // Mark variable as used. 2240 VD->setReferenced(); 2241 VD->markUsed(Context); 2242 2243 QualType QType = VD->getType(); 2244 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 2245 // It will be analyzed later. 2246 Vars.push_back(DE); 2247 continue; 2248 } 2249 2250 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2251 // A threadprivate variable must not have an incomplete type. 2252 if (RequireCompleteType(ILoc, VD->getType(), 2253 diag::err_omp_threadprivate_incomplete_type)) { 2254 continue; 2255 } 2256 2257 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2258 // A threadprivate variable must not have a reference type. 2259 if (VD->getType()->isReferenceType()) { 2260 Diag(ILoc, diag::err_omp_ref_type_arg) 2261 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 2262 bool IsDecl = 2263 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2264 Diag(VD->getLocation(), 2265 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2266 << VD; 2267 continue; 2268 } 2269 2270 // Check if this is a TLS variable. If TLS is not being supported, produce 2271 // the corresponding diagnostic. 2272 if ((VD->getTLSKind() != VarDecl::TLS_None && 2273 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 2274 getLangOpts().OpenMPUseTLS && 2275 getASTContext().getTargetInfo().isTLSSupported())) || 2276 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 2277 !VD->isLocalVarDecl())) { 2278 Diag(ILoc, diag::err_omp_var_thread_local) 2279 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 2280 bool IsDecl = 2281 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2282 Diag(VD->getLocation(), 2283 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2284 << VD; 2285 continue; 2286 } 2287 2288 // Check if initial value of threadprivate variable reference variable with 2289 // local storage (it is not supported by runtime). 2290 if (const Expr *Init = VD->getAnyInitializer()) { 2291 LocalVarRefChecker Checker(*this); 2292 if (Checker.Visit(Init)) 2293 continue; 2294 } 2295 2296 Vars.push_back(RefExpr); 2297 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 2298 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 2299 Context, SourceRange(Loc, Loc))); 2300 if (ASTMutationListener *ML = Context.getASTMutationListener()) 2301 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 2302 } 2303 OMPThreadPrivateDecl *D = nullptr; 2304 if (!Vars.empty()) { 2305 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 2306 Vars); 2307 D->setAccess(AS_public); 2308 } 2309 return D; 2310 } 2311 2312 static OMPAllocateDeclAttr::AllocatorTypeTy 2313 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) { 2314 if (!Allocator) 2315 return OMPAllocateDeclAttr::OMPDefaultMemAlloc; 2316 if (Allocator->isTypeDependent() || Allocator->isValueDependent() || 2317 Allocator->isInstantiationDependent() || 2318 Allocator->containsUnexpandedParameterPack()) 2319 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 2320 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 2321 const Expr *AE = Allocator->IgnoreParenImpCasts(); 2322 for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc; 2323 I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 2324 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 2325 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind); 2326 llvm::FoldingSetNodeID AEId, DAEId; 2327 AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true); 2328 DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true); 2329 if (AEId == DAEId) { 2330 AllocatorKindRes = AllocatorKind; 2331 break; 2332 } 2333 } 2334 return AllocatorKindRes; 2335 } 2336 2337 static bool checkPreviousOMPAllocateAttribute( 2338 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD, 2339 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) { 2340 if (!VD->hasAttr<OMPAllocateDeclAttr>()) 2341 return false; 2342 const auto *A = VD->getAttr<OMPAllocateDeclAttr>(); 2343 Expr *PrevAllocator = A->getAllocator(); 2344 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind = 2345 getAllocatorKind(S, Stack, PrevAllocator); 2346 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind; 2347 if (AllocatorsMatch && 2348 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc && 2349 Allocator && PrevAllocator) { 2350 const Expr *AE = Allocator->IgnoreParenImpCasts(); 2351 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts(); 2352 llvm::FoldingSetNodeID AEId, PAEId; 2353 AE->Profile(AEId, S.Context, /*Canonical=*/true); 2354 PAE->Profile(PAEId, S.Context, /*Canonical=*/true); 2355 AllocatorsMatch = AEId == PAEId; 2356 } 2357 if (!AllocatorsMatch) { 2358 SmallString<256> AllocatorBuffer; 2359 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer); 2360 if (Allocator) 2361 Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy()); 2362 SmallString<256> PrevAllocatorBuffer; 2363 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer); 2364 if (PrevAllocator) 2365 PrevAllocator->printPretty(PrevAllocatorStream, nullptr, 2366 S.getPrintingPolicy()); 2367 2368 SourceLocation AllocatorLoc = 2369 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc(); 2370 SourceRange AllocatorRange = 2371 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange(); 2372 SourceLocation PrevAllocatorLoc = 2373 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation(); 2374 SourceRange PrevAllocatorRange = 2375 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange(); 2376 S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator) 2377 << (Allocator ? 1 : 0) << AllocatorStream.str() 2378 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str() 2379 << AllocatorRange; 2380 S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator) 2381 << PrevAllocatorRange; 2382 return true; 2383 } 2384 return false; 2385 } 2386 2387 static void 2388 applyOMPAllocateAttribute(Sema &S, VarDecl *VD, 2389 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 2390 Expr *Allocator, SourceRange SR) { 2391 if (VD->hasAttr<OMPAllocateDeclAttr>()) 2392 return; 2393 if (Allocator && 2394 (Allocator->isTypeDependent() || Allocator->isValueDependent() || 2395 Allocator->isInstantiationDependent() || 2396 Allocator->containsUnexpandedParameterPack())) 2397 return; 2398 auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind, 2399 Allocator, SR); 2400 VD->addAttr(A); 2401 if (ASTMutationListener *ML = S.Context.getASTMutationListener()) 2402 ML->DeclarationMarkedOpenMPAllocate(VD, A); 2403 } 2404 2405 Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective( 2406 SourceLocation Loc, ArrayRef<Expr *> VarList, 2407 ArrayRef<OMPClause *> Clauses, DeclContext *Owner) { 2408 assert(Clauses.size() <= 1 && "Expected at most one clause."); 2409 Expr *Allocator = nullptr; 2410 if (Clauses.empty()) { 2411 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions. 2412 // allocate directives that appear in a target region must specify an 2413 // allocator clause unless a requires directive with the dynamic_allocators 2414 // clause is present in the same compilation unit. 2415 if (LangOpts.OpenMPIsDevice && 2416 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 2417 targetDiag(Loc, diag::err_expected_allocator_clause); 2418 } else { 2419 Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator(); 2420 } 2421 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 2422 getAllocatorKind(*this, DSAStack, Allocator); 2423 SmallVector<Expr *, 8> Vars; 2424 for (Expr *RefExpr : VarList) { 2425 auto *DE = cast<DeclRefExpr>(RefExpr); 2426 auto *VD = cast<VarDecl>(DE->getDecl()); 2427 2428 // Check if this is a TLS variable or global register. 2429 if (VD->getTLSKind() != VarDecl::TLS_None || 2430 VD->hasAttr<OMPThreadPrivateDeclAttr>() || 2431 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 2432 !VD->isLocalVarDecl())) 2433 continue; 2434 2435 // If the used several times in the allocate directive, the same allocator 2436 // must be used. 2437 if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD, 2438 AllocatorKind, Allocator)) 2439 continue; 2440 2441 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++ 2442 // If a list item has a static storage type, the allocator expression in the 2443 // allocator clause must be a constant expression that evaluates to one of 2444 // the predefined memory allocator values. 2445 if (Allocator && VD->hasGlobalStorage()) { 2446 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) { 2447 Diag(Allocator->getExprLoc(), 2448 diag::err_omp_expected_predefined_allocator) 2449 << Allocator->getSourceRange(); 2450 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 2451 VarDecl::DeclarationOnly; 2452 Diag(VD->getLocation(), 2453 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2454 << VD; 2455 continue; 2456 } 2457 } 2458 2459 Vars.push_back(RefExpr); 2460 applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator, 2461 DE->getSourceRange()); 2462 } 2463 if (Vars.empty()) 2464 return nullptr; 2465 if (!Owner) 2466 Owner = getCurLexicalContext(); 2467 auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses); 2468 D->setAccess(AS_public); 2469 Owner->addDecl(D); 2470 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2471 } 2472 2473 Sema::DeclGroupPtrTy 2474 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc, 2475 ArrayRef<OMPClause *> ClauseList) { 2476 OMPRequiresDecl *D = nullptr; 2477 if (!CurContext->isFileContext()) { 2478 Diag(Loc, diag::err_omp_invalid_scope) << "requires"; 2479 } else { 2480 D = CheckOMPRequiresDecl(Loc, ClauseList); 2481 if (D) { 2482 CurContext->addDecl(D); 2483 DSAStack->addRequiresDecl(D); 2484 } 2485 } 2486 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2487 } 2488 2489 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc, 2490 ArrayRef<OMPClause *> ClauseList) { 2491 /// For target specific clauses, the requires directive cannot be 2492 /// specified after the handling of any of the target regions in the 2493 /// current compilation unit. 2494 ArrayRef<SourceLocation> TargetLocations = 2495 DSAStack->getEncounteredTargetLocs(); 2496 if (!TargetLocations.empty()) { 2497 for (const OMPClause *CNew : ClauseList) { 2498 // Check if any of the requires clauses affect target regions. 2499 if (isa<OMPUnifiedSharedMemoryClause>(CNew) || 2500 isa<OMPUnifiedAddressClause>(CNew) || 2501 isa<OMPReverseOffloadClause>(CNew) || 2502 isa<OMPDynamicAllocatorsClause>(CNew)) { 2503 Diag(Loc, diag::err_omp_target_before_requires) 2504 << getOpenMPClauseName(CNew->getClauseKind()); 2505 for (SourceLocation TargetLoc : TargetLocations) { 2506 Diag(TargetLoc, diag::note_omp_requires_encountered_target); 2507 } 2508 } 2509 } 2510 } 2511 2512 if (!DSAStack->hasDuplicateRequiresClause(ClauseList)) 2513 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc, 2514 ClauseList); 2515 return nullptr; 2516 } 2517 2518 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 2519 const ValueDecl *D, 2520 const DSAStackTy::DSAVarData &DVar, 2521 bool IsLoopIterVar = false) { 2522 if (DVar.RefExpr) { 2523 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 2524 << getOpenMPClauseName(DVar.CKind); 2525 return; 2526 } 2527 enum { 2528 PDSA_StaticMemberShared, 2529 PDSA_StaticLocalVarShared, 2530 PDSA_LoopIterVarPrivate, 2531 PDSA_LoopIterVarLinear, 2532 PDSA_LoopIterVarLastprivate, 2533 PDSA_ConstVarShared, 2534 PDSA_GlobalVarShared, 2535 PDSA_TaskVarFirstprivate, 2536 PDSA_LocalVarPrivate, 2537 PDSA_Implicit 2538 } Reason = PDSA_Implicit; 2539 bool ReportHint = false; 2540 auto ReportLoc = D->getLocation(); 2541 auto *VD = dyn_cast<VarDecl>(D); 2542 if (IsLoopIterVar) { 2543 if (DVar.CKind == OMPC_private) 2544 Reason = PDSA_LoopIterVarPrivate; 2545 else if (DVar.CKind == OMPC_lastprivate) 2546 Reason = PDSA_LoopIterVarLastprivate; 2547 else 2548 Reason = PDSA_LoopIterVarLinear; 2549 } else if (isOpenMPTaskingDirective(DVar.DKind) && 2550 DVar.CKind == OMPC_firstprivate) { 2551 Reason = PDSA_TaskVarFirstprivate; 2552 ReportLoc = DVar.ImplicitDSALoc; 2553 } else if (VD && VD->isStaticLocal()) 2554 Reason = PDSA_StaticLocalVarShared; 2555 else if (VD && VD->isStaticDataMember()) 2556 Reason = PDSA_StaticMemberShared; 2557 else if (VD && VD->isFileVarDecl()) 2558 Reason = PDSA_GlobalVarShared; 2559 else if (D->getType().isConstant(SemaRef.getASTContext())) 2560 Reason = PDSA_ConstVarShared; 2561 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 2562 ReportHint = true; 2563 Reason = PDSA_LocalVarPrivate; 2564 } 2565 if (Reason != PDSA_Implicit) { 2566 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 2567 << Reason << ReportHint 2568 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 2569 } else if (DVar.ImplicitDSALoc.isValid()) { 2570 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 2571 << getOpenMPClauseName(DVar.CKind); 2572 } 2573 } 2574 2575 namespace { 2576 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> { 2577 DSAStackTy *Stack; 2578 Sema &SemaRef; 2579 bool ErrorFound = false; 2580 CapturedStmt *CS = nullptr; 2581 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate; 2582 llvm::SmallVector<Expr *, 4> ImplicitMap; 2583 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA; 2584 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations; 2585 2586 void VisitSubCaptures(OMPExecutableDirective *S) { 2587 // Check implicitly captured variables. 2588 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt()) 2589 return; 2590 visitSubCaptures(S->getInnermostCapturedStmt()); 2591 } 2592 2593 public: 2594 void VisitDeclRefExpr(DeclRefExpr *E) { 2595 if (E->isTypeDependent() || E->isValueDependent() || 2596 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 2597 return; 2598 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2599 // Check the datasharing rules for the expressions in the clauses. 2600 if (!CS) { 2601 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD)) 2602 if (!CED->hasAttr<OMPCaptureNoInitAttr>()) { 2603 Visit(CED->getInit()); 2604 return; 2605 } 2606 } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD)) 2607 // Do not analyze internal variables and do not enclose them into 2608 // implicit clauses. 2609 return; 2610 VD = VD->getCanonicalDecl(); 2611 // Skip internally declared variables. 2612 if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD)) 2613 return; 2614 2615 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 2616 // Check if the variable has explicit DSA set and stop analysis if it so. 2617 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second) 2618 return; 2619 2620 // Skip internally declared static variables. 2621 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 2622 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 2623 if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) && 2624 (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 2625 !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link)) 2626 return; 2627 2628 SourceLocation ELoc = E->getExprLoc(); 2629 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 2630 // The default(none) clause requires that each variable that is referenced 2631 // in the construct, and does not have a predetermined data-sharing 2632 // attribute, must have its data-sharing attribute explicitly determined 2633 // by being listed in a data-sharing attribute clause. 2634 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && 2635 isImplicitOrExplicitTaskingRegion(DKind) && 2636 VarsWithInheritedDSA.count(VD) == 0) { 2637 VarsWithInheritedDSA[VD] = E; 2638 return; 2639 } 2640 2641 if (isOpenMPTargetExecutionDirective(DKind) && 2642 !Stack->isLoopControlVariable(VD).first) { 2643 if (!Stack->checkMappableExprComponentListsForDecl( 2644 VD, /*CurrentRegionOnly=*/true, 2645 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 2646 StackComponents, 2647 OpenMPClauseKind) { 2648 // Variable is used if it has been marked as an array, array 2649 // section or the variable iself. 2650 return StackComponents.size() == 1 || 2651 std::all_of( 2652 std::next(StackComponents.rbegin()), 2653 StackComponents.rend(), 2654 [](const OMPClauseMappableExprCommon:: 2655 MappableComponent &MC) { 2656 return MC.getAssociatedDeclaration() == 2657 nullptr && 2658 (isa<OMPArraySectionExpr>( 2659 MC.getAssociatedExpression()) || 2660 isa<ArraySubscriptExpr>( 2661 MC.getAssociatedExpression())); 2662 }); 2663 })) { 2664 bool IsFirstprivate = false; 2665 // By default lambdas are captured as firstprivates. 2666 if (const auto *RD = 2667 VD->getType().getNonReferenceType()->getAsCXXRecordDecl()) 2668 IsFirstprivate = RD->isLambda(); 2669 IsFirstprivate = 2670 IsFirstprivate || 2671 (VD->getType().getNonReferenceType()->isScalarType() && 2672 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res); 2673 if (IsFirstprivate) 2674 ImplicitFirstprivate.emplace_back(E); 2675 else 2676 ImplicitMap.emplace_back(E); 2677 return; 2678 } 2679 } 2680 2681 // OpenMP [2.9.3.6, Restrictions, p.2] 2682 // A list item that appears in a reduction clause of the innermost 2683 // enclosing worksharing or parallel construct may not be accessed in an 2684 // explicit task. 2685 DVar = Stack->hasInnermostDSA( 2686 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 2687 [](OpenMPDirectiveKind K) { 2688 return isOpenMPParallelDirective(K) || 2689 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 2690 }, 2691 /*FromParent=*/true); 2692 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 2693 ErrorFound = true; 2694 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 2695 reportOriginalDsa(SemaRef, Stack, VD, DVar); 2696 return; 2697 } 2698 2699 // Define implicit data-sharing attributes for task. 2700 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false); 2701 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 2702 !Stack->isLoopControlVariable(VD).first) { 2703 ImplicitFirstprivate.push_back(E); 2704 return; 2705 } 2706 2707 // Store implicitly used globals with declare target link for parent 2708 // target. 2709 if (!isOpenMPTargetExecutionDirective(DKind) && Res && 2710 *Res == OMPDeclareTargetDeclAttr::MT_Link) { 2711 Stack->addToParentTargetRegionLinkGlobals(E); 2712 return; 2713 } 2714 } 2715 } 2716 void VisitMemberExpr(MemberExpr *E) { 2717 if (E->isTypeDependent() || E->isValueDependent() || 2718 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 2719 return; 2720 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 2721 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 2722 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParens())) { 2723 if (!FD) 2724 return; 2725 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false); 2726 // Check if the variable has explicit DSA set and stop analysis if it 2727 // so. 2728 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second) 2729 return; 2730 2731 if (isOpenMPTargetExecutionDirective(DKind) && 2732 !Stack->isLoopControlVariable(FD).first && 2733 !Stack->checkMappableExprComponentListsForDecl( 2734 FD, /*CurrentRegionOnly=*/true, 2735 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 2736 StackComponents, 2737 OpenMPClauseKind) { 2738 return isa<CXXThisExpr>( 2739 cast<MemberExpr>( 2740 StackComponents.back().getAssociatedExpression()) 2741 ->getBase() 2742 ->IgnoreParens()); 2743 })) { 2744 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 2745 // A bit-field cannot appear in a map clause. 2746 // 2747 if (FD->isBitField()) 2748 return; 2749 2750 // Check to see if the member expression is referencing a class that 2751 // has already been explicitly mapped 2752 if (Stack->isClassPreviouslyMapped(TE->getType())) 2753 return; 2754 2755 ImplicitMap.emplace_back(E); 2756 return; 2757 } 2758 2759 SourceLocation ELoc = E->getExprLoc(); 2760 // OpenMP [2.9.3.6, Restrictions, p.2] 2761 // A list item that appears in a reduction clause of the innermost 2762 // enclosing worksharing or parallel construct may not be accessed in 2763 // an explicit task. 2764 DVar = Stack->hasInnermostDSA( 2765 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 2766 [](OpenMPDirectiveKind K) { 2767 return isOpenMPParallelDirective(K) || 2768 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 2769 }, 2770 /*FromParent=*/true); 2771 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 2772 ErrorFound = true; 2773 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 2774 reportOriginalDsa(SemaRef, Stack, FD, DVar); 2775 return; 2776 } 2777 2778 // Define implicit data-sharing attributes for task. 2779 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false); 2780 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 2781 !Stack->isLoopControlVariable(FD).first) { 2782 // Check if there is a captured expression for the current field in the 2783 // region. Do not mark it as firstprivate unless there is no captured 2784 // expression. 2785 // TODO: try to make it firstprivate. 2786 if (DVar.CKind != OMPC_unknown) 2787 ImplicitFirstprivate.push_back(E); 2788 } 2789 return; 2790 } 2791 if (isOpenMPTargetExecutionDirective(DKind)) { 2792 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 2793 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map, 2794 /*NoDiagnose=*/true)) 2795 return; 2796 const auto *VD = cast<ValueDecl>( 2797 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl()); 2798 if (!Stack->checkMappableExprComponentListsForDecl( 2799 VD, /*CurrentRegionOnly=*/true, 2800 [&CurComponents]( 2801 OMPClauseMappableExprCommon::MappableExprComponentListRef 2802 StackComponents, 2803 OpenMPClauseKind) { 2804 auto CCI = CurComponents.rbegin(); 2805 auto CCE = CurComponents.rend(); 2806 for (const auto &SC : llvm::reverse(StackComponents)) { 2807 // Do both expressions have the same kind? 2808 if (CCI->getAssociatedExpression()->getStmtClass() != 2809 SC.getAssociatedExpression()->getStmtClass()) 2810 if (!(isa<OMPArraySectionExpr>( 2811 SC.getAssociatedExpression()) && 2812 isa<ArraySubscriptExpr>( 2813 CCI->getAssociatedExpression()))) 2814 return false; 2815 2816 const Decl *CCD = CCI->getAssociatedDeclaration(); 2817 const Decl *SCD = SC.getAssociatedDeclaration(); 2818 CCD = CCD ? CCD->getCanonicalDecl() : nullptr; 2819 SCD = SCD ? SCD->getCanonicalDecl() : nullptr; 2820 if (SCD != CCD) 2821 return false; 2822 std::advance(CCI, 1); 2823 if (CCI == CCE) 2824 break; 2825 } 2826 return true; 2827 })) { 2828 Visit(E->getBase()); 2829 } 2830 } else { 2831 Visit(E->getBase()); 2832 } 2833 } 2834 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 2835 for (OMPClause *C : S->clauses()) { 2836 // Skip analysis of arguments of implicitly defined firstprivate clause 2837 // for task|target directives. 2838 // Skip analysis of arguments of implicitly defined map clause for target 2839 // directives. 2840 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) && 2841 C->isImplicit())) { 2842 for (Stmt *CC : C->children()) { 2843 if (CC) 2844 Visit(CC); 2845 } 2846 } 2847 } 2848 // Check implicitly captured variables. 2849 VisitSubCaptures(S); 2850 } 2851 void VisitStmt(Stmt *S) { 2852 for (Stmt *C : S->children()) { 2853 if (C) { 2854 // Check implicitly captured variables in the task-based directives to 2855 // check if they must be firstprivatized. 2856 Visit(C); 2857 } 2858 } 2859 } 2860 2861 void visitSubCaptures(CapturedStmt *S) { 2862 for (const CapturedStmt::Capture &Cap : S->captures()) { 2863 if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy()) 2864 continue; 2865 VarDecl *VD = Cap.getCapturedVar(); 2866 // Do not try to map the variable if it or its sub-component was mapped 2867 // already. 2868 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 2869 Stack->checkMappableExprComponentListsForDecl( 2870 VD, /*CurrentRegionOnly=*/true, 2871 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 2872 OpenMPClauseKind) { return true; })) 2873 continue; 2874 DeclRefExpr *DRE = buildDeclRefExpr( 2875 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context), 2876 Cap.getLocation(), /*RefersToCapture=*/true); 2877 Visit(DRE); 2878 } 2879 } 2880 bool isErrorFound() const { return ErrorFound; } 2881 ArrayRef<Expr *> getImplicitFirstprivate() const { 2882 return ImplicitFirstprivate; 2883 } 2884 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; } 2885 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const { 2886 return VarsWithInheritedDSA; 2887 } 2888 2889 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 2890 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) { 2891 // Process declare target link variables for the target directives. 2892 if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) { 2893 for (DeclRefExpr *E : Stack->getLinkGlobals()) 2894 Visit(E); 2895 } 2896 } 2897 }; 2898 } // namespace 2899 2900 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 2901 switch (DKind) { 2902 case OMPD_parallel: 2903 case OMPD_parallel_for: 2904 case OMPD_parallel_for_simd: 2905 case OMPD_parallel_sections: 2906 case OMPD_teams: 2907 case OMPD_teams_distribute: 2908 case OMPD_teams_distribute_simd: { 2909 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2910 QualType KmpInt32PtrTy = 2911 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2912 Sema::CapturedParamNameType Params[] = { 2913 std::make_pair(".global_tid.", KmpInt32PtrTy), 2914 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2915 std::make_pair(StringRef(), QualType()) // __context with shared vars 2916 }; 2917 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2918 Params); 2919 break; 2920 } 2921 case OMPD_target_teams: 2922 case OMPD_target_parallel: 2923 case OMPD_target_parallel_for: 2924 case OMPD_target_parallel_for_simd: 2925 case OMPD_target_teams_distribute: 2926 case OMPD_target_teams_distribute_simd: { 2927 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2928 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2929 QualType KmpInt32PtrTy = 2930 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2931 QualType Args[] = {VoidPtrTy}; 2932 FunctionProtoType::ExtProtoInfo EPI; 2933 EPI.Variadic = true; 2934 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2935 Sema::CapturedParamNameType Params[] = { 2936 std::make_pair(".global_tid.", KmpInt32Ty), 2937 std::make_pair(".part_id.", KmpInt32PtrTy), 2938 std::make_pair(".privates.", VoidPtrTy), 2939 std::make_pair( 2940 ".copy_fn.", 2941 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2942 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2943 std::make_pair(StringRef(), QualType()) // __context with shared vars 2944 }; 2945 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2946 Params); 2947 // Mark this captured region as inlined, because we don't use outlined 2948 // function directly. 2949 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2950 AlwaysInlineAttr::CreateImplicit( 2951 Context, AlwaysInlineAttr::Keyword_forceinline)); 2952 Sema::CapturedParamNameType ParamsTarget[] = { 2953 std::make_pair(StringRef(), QualType()) // __context with shared vars 2954 }; 2955 // Start a captured region for 'target' with no implicit parameters. 2956 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2957 ParamsTarget); 2958 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 2959 std::make_pair(".global_tid.", KmpInt32PtrTy), 2960 std::make_pair(".bound_tid.", KmpInt32PtrTy), 2961 std::make_pair(StringRef(), QualType()) // __context with shared vars 2962 }; 2963 // Start a captured region for 'teams' or 'parallel'. Both regions have 2964 // the same implicit parameters. 2965 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2966 ParamsTeamsOrParallel); 2967 break; 2968 } 2969 case OMPD_target: 2970 case OMPD_target_simd: { 2971 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 2972 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 2973 QualType KmpInt32PtrTy = 2974 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 2975 QualType Args[] = {VoidPtrTy}; 2976 FunctionProtoType::ExtProtoInfo EPI; 2977 EPI.Variadic = true; 2978 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 2979 Sema::CapturedParamNameType Params[] = { 2980 std::make_pair(".global_tid.", KmpInt32Ty), 2981 std::make_pair(".part_id.", KmpInt32PtrTy), 2982 std::make_pair(".privates.", VoidPtrTy), 2983 std::make_pair( 2984 ".copy_fn.", 2985 Context.getPointerType(CopyFnType).withConst().withRestrict()), 2986 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 2987 std::make_pair(StringRef(), QualType()) // __context with shared vars 2988 }; 2989 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2990 Params); 2991 // Mark this captured region as inlined, because we don't use outlined 2992 // function directly. 2993 getCurCapturedRegion()->TheCapturedDecl->addAttr( 2994 AlwaysInlineAttr::CreateImplicit( 2995 Context, AlwaysInlineAttr::Keyword_forceinline)); 2996 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 2997 std::make_pair(StringRef(), QualType())); 2998 break; 2999 } 3000 case OMPD_simd: 3001 case OMPD_for: 3002 case OMPD_for_simd: 3003 case OMPD_sections: 3004 case OMPD_section: 3005 case OMPD_single: 3006 case OMPD_master: 3007 case OMPD_critical: 3008 case OMPD_taskgroup: 3009 case OMPD_distribute: 3010 case OMPD_distribute_simd: 3011 case OMPD_ordered: 3012 case OMPD_atomic: 3013 case OMPD_target_data: { 3014 Sema::CapturedParamNameType Params[] = { 3015 std::make_pair(StringRef(), QualType()) // __context with shared vars 3016 }; 3017 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3018 Params); 3019 break; 3020 } 3021 case OMPD_task: { 3022 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3023 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3024 QualType KmpInt32PtrTy = 3025 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3026 QualType Args[] = {VoidPtrTy}; 3027 FunctionProtoType::ExtProtoInfo EPI; 3028 EPI.Variadic = true; 3029 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3030 Sema::CapturedParamNameType Params[] = { 3031 std::make_pair(".global_tid.", KmpInt32Ty), 3032 std::make_pair(".part_id.", KmpInt32PtrTy), 3033 std::make_pair(".privates.", VoidPtrTy), 3034 std::make_pair( 3035 ".copy_fn.", 3036 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3037 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3038 std::make_pair(StringRef(), QualType()) // __context with shared vars 3039 }; 3040 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3041 Params); 3042 // Mark this captured region as inlined, because we don't use outlined 3043 // function directly. 3044 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3045 AlwaysInlineAttr::CreateImplicit( 3046 Context, AlwaysInlineAttr::Keyword_forceinline)); 3047 break; 3048 } 3049 case OMPD_taskloop: 3050 case OMPD_taskloop_simd: { 3051 QualType KmpInt32Ty = 3052 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 3053 .withConst(); 3054 QualType KmpUInt64Ty = 3055 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 3056 .withConst(); 3057 QualType KmpInt64Ty = 3058 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 3059 .withConst(); 3060 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3061 QualType KmpInt32PtrTy = 3062 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3063 QualType Args[] = {VoidPtrTy}; 3064 FunctionProtoType::ExtProtoInfo EPI; 3065 EPI.Variadic = true; 3066 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3067 Sema::CapturedParamNameType Params[] = { 3068 std::make_pair(".global_tid.", KmpInt32Ty), 3069 std::make_pair(".part_id.", KmpInt32PtrTy), 3070 std::make_pair(".privates.", VoidPtrTy), 3071 std::make_pair( 3072 ".copy_fn.", 3073 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3074 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3075 std::make_pair(".lb.", KmpUInt64Ty), 3076 std::make_pair(".ub.", KmpUInt64Ty), 3077 std::make_pair(".st.", KmpInt64Ty), 3078 std::make_pair(".liter.", KmpInt32Ty), 3079 std::make_pair(".reductions.", VoidPtrTy), 3080 std::make_pair(StringRef(), QualType()) // __context with shared vars 3081 }; 3082 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3083 Params); 3084 // Mark this captured region as inlined, because we don't use outlined 3085 // function directly. 3086 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3087 AlwaysInlineAttr::CreateImplicit( 3088 Context, AlwaysInlineAttr::Keyword_forceinline)); 3089 break; 3090 } 3091 case OMPD_distribute_parallel_for_simd: 3092 case OMPD_distribute_parallel_for: { 3093 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3094 QualType KmpInt32PtrTy = 3095 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3096 Sema::CapturedParamNameType Params[] = { 3097 std::make_pair(".global_tid.", KmpInt32PtrTy), 3098 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3099 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 3100 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 3101 std::make_pair(StringRef(), QualType()) // __context with shared vars 3102 }; 3103 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3104 Params); 3105 break; 3106 } 3107 case OMPD_target_teams_distribute_parallel_for: 3108 case OMPD_target_teams_distribute_parallel_for_simd: { 3109 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3110 QualType KmpInt32PtrTy = 3111 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3112 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3113 3114 QualType Args[] = {VoidPtrTy}; 3115 FunctionProtoType::ExtProtoInfo EPI; 3116 EPI.Variadic = true; 3117 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3118 Sema::CapturedParamNameType Params[] = { 3119 std::make_pair(".global_tid.", KmpInt32Ty), 3120 std::make_pair(".part_id.", KmpInt32PtrTy), 3121 std::make_pair(".privates.", VoidPtrTy), 3122 std::make_pair( 3123 ".copy_fn.", 3124 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3125 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3126 std::make_pair(StringRef(), QualType()) // __context with shared vars 3127 }; 3128 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3129 Params); 3130 // Mark this captured region as inlined, because we don't use outlined 3131 // function directly. 3132 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3133 AlwaysInlineAttr::CreateImplicit( 3134 Context, AlwaysInlineAttr::Keyword_forceinline)); 3135 Sema::CapturedParamNameType ParamsTarget[] = { 3136 std::make_pair(StringRef(), QualType()) // __context with shared vars 3137 }; 3138 // Start a captured region for 'target' with no implicit parameters. 3139 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3140 ParamsTarget); 3141 3142 Sema::CapturedParamNameType ParamsTeams[] = { 3143 std::make_pair(".global_tid.", KmpInt32PtrTy), 3144 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3145 std::make_pair(StringRef(), QualType()) // __context with shared vars 3146 }; 3147 // Start a captured region for 'target' with no implicit parameters. 3148 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3149 ParamsTeams); 3150 3151 Sema::CapturedParamNameType ParamsParallel[] = { 3152 std::make_pair(".global_tid.", KmpInt32PtrTy), 3153 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3154 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 3155 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 3156 std::make_pair(StringRef(), QualType()) // __context with shared vars 3157 }; 3158 // Start a captured region for 'teams' or 'parallel'. Both regions have 3159 // the same implicit parameters. 3160 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3161 ParamsParallel); 3162 break; 3163 } 3164 3165 case OMPD_teams_distribute_parallel_for: 3166 case OMPD_teams_distribute_parallel_for_simd: { 3167 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3168 QualType KmpInt32PtrTy = 3169 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3170 3171 Sema::CapturedParamNameType ParamsTeams[] = { 3172 std::make_pair(".global_tid.", KmpInt32PtrTy), 3173 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3174 std::make_pair(StringRef(), QualType()) // __context with shared vars 3175 }; 3176 // Start a captured region for 'target' with no implicit parameters. 3177 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3178 ParamsTeams); 3179 3180 Sema::CapturedParamNameType ParamsParallel[] = { 3181 std::make_pair(".global_tid.", KmpInt32PtrTy), 3182 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3183 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 3184 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 3185 std::make_pair(StringRef(), QualType()) // __context with shared vars 3186 }; 3187 // Start a captured region for 'teams' or 'parallel'. Both regions have 3188 // the same implicit parameters. 3189 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3190 ParamsParallel); 3191 break; 3192 } 3193 case OMPD_target_update: 3194 case OMPD_target_enter_data: 3195 case OMPD_target_exit_data: { 3196 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3197 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3198 QualType KmpInt32PtrTy = 3199 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3200 QualType Args[] = {VoidPtrTy}; 3201 FunctionProtoType::ExtProtoInfo EPI; 3202 EPI.Variadic = true; 3203 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3204 Sema::CapturedParamNameType Params[] = { 3205 std::make_pair(".global_tid.", KmpInt32Ty), 3206 std::make_pair(".part_id.", KmpInt32PtrTy), 3207 std::make_pair(".privates.", VoidPtrTy), 3208 std::make_pair( 3209 ".copy_fn.", 3210 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3211 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3212 std::make_pair(StringRef(), QualType()) // __context with shared vars 3213 }; 3214 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3215 Params); 3216 // Mark this captured region as inlined, because we don't use outlined 3217 // function directly. 3218 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3219 AlwaysInlineAttr::CreateImplicit( 3220 Context, AlwaysInlineAttr::Keyword_forceinline)); 3221 break; 3222 } 3223 case OMPD_threadprivate: 3224 case OMPD_allocate: 3225 case OMPD_taskyield: 3226 case OMPD_barrier: 3227 case OMPD_taskwait: 3228 case OMPD_cancellation_point: 3229 case OMPD_cancel: 3230 case OMPD_flush: 3231 case OMPD_declare_reduction: 3232 case OMPD_declare_mapper: 3233 case OMPD_declare_simd: 3234 case OMPD_declare_target: 3235 case OMPD_end_declare_target: 3236 case OMPD_requires: 3237 llvm_unreachable("OpenMP Directive is not allowed"); 3238 case OMPD_unknown: 3239 llvm_unreachable("Unknown OpenMP directive"); 3240 } 3241 } 3242 3243 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 3244 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 3245 getOpenMPCaptureRegions(CaptureRegions, DKind); 3246 return CaptureRegions.size(); 3247 } 3248 3249 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 3250 Expr *CaptureExpr, bool WithInit, 3251 bool AsExpression) { 3252 assert(CaptureExpr); 3253 ASTContext &C = S.getASTContext(); 3254 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 3255 QualType Ty = Init->getType(); 3256 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 3257 if (S.getLangOpts().CPlusPlus) { 3258 Ty = C.getLValueReferenceType(Ty); 3259 } else { 3260 Ty = C.getPointerType(Ty); 3261 ExprResult Res = 3262 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 3263 if (!Res.isUsable()) 3264 return nullptr; 3265 Init = Res.get(); 3266 } 3267 WithInit = true; 3268 } 3269 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 3270 CaptureExpr->getBeginLoc()); 3271 if (!WithInit) 3272 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C)); 3273 S.CurContext->addHiddenDecl(CED); 3274 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 3275 return CED; 3276 } 3277 3278 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 3279 bool WithInit) { 3280 OMPCapturedExprDecl *CD; 3281 if (VarDecl *VD = S.isOpenMPCapturedDecl(D)) 3282 CD = cast<OMPCapturedExprDecl>(VD); 3283 else 3284 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 3285 /*AsExpression=*/false); 3286 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 3287 CaptureExpr->getExprLoc()); 3288 } 3289 3290 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 3291 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get(); 3292 if (!Ref) { 3293 OMPCapturedExprDecl *CD = buildCaptureDecl( 3294 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr, 3295 /*WithInit=*/true, /*AsExpression=*/true); 3296 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 3297 CaptureExpr->getExprLoc()); 3298 } 3299 ExprResult Res = Ref; 3300 if (!S.getLangOpts().CPlusPlus && 3301 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 3302 Ref->getType()->isPointerType()) { 3303 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 3304 if (!Res.isUsable()) 3305 return ExprError(); 3306 } 3307 return S.DefaultLvalueConversion(Res.get()); 3308 } 3309 3310 namespace { 3311 // OpenMP directives parsed in this section are represented as a 3312 // CapturedStatement with an associated statement. If a syntax error 3313 // is detected during the parsing of the associated statement, the 3314 // compiler must abort processing and close the CapturedStatement. 3315 // 3316 // Combined directives such as 'target parallel' have more than one 3317 // nested CapturedStatements. This RAII ensures that we unwind out 3318 // of all the nested CapturedStatements when an error is found. 3319 class CaptureRegionUnwinderRAII { 3320 private: 3321 Sema &S; 3322 bool &ErrorFound; 3323 OpenMPDirectiveKind DKind = OMPD_unknown; 3324 3325 public: 3326 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 3327 OpenMPDirectiveKind DKind) 3328 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 3329 ~CaptureRegionUnwinderRAII() { 3330 if (ErrorFound) { 3331 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 3332 while (--ThisCaptureLevel >= 0) 3333 S.ActOnCapturedRegionError(); 3334 } 3335 } 3336 }; 3337 } // namespace 3338 3339 void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) { 3340 // Capture variables captured by reference in lambdas for target-based 3341 // directives. 3342 if (!CurContext->isDependentContext() && 3343 (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) || 3344 isOpenMPTargetDataManagementDirective( 3345 DSAStack->getCurrentDirective()))) { 3346 QualType Type = V->getType(); 3347 if (const auto *RD = Type.getCanonicalType() 3348 .getNonReferenceType() 3349 ->getAsCXXRecordDecl()) { 3350 bool SavedForceCaptureByReferenceInTargetExecutable = 3351 DSAStack->isForceCaptureByReferenceInTargetExecutable(); 3352 DSAStack->setForceCaptureByReferenceInTargetExecutable( 3353 /*V=*/true); 3354 if (RD->isLambda()) { 3355 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures; 3356 FieldDecl *ThisCapture; 3357 RD->getCaptureFields(Captures, ThisCapture); 3358 for (const LambdaCapture &LC : RD->captures()) { 3359 if (LC.getCaptureKind() == LCK_ByRef) { 3360 VarDecl *VD = LC.getCapturedVar(); 3361 DeclContext *VDC = VD->getDeclContext(); 3362 if (!VDC->Encloses(CurContext)) 3363 continue; 3364 MarkVariableReferenced(LC.getLocation(), VD); 3365 } else if (LC.getCaptureKind() == LCK_This) { 3366 QualType ThisTy = getCurrentThisType(); 3367 if (!ThisTy.isNull() && 3368 Context.typesAreCompatible(ThisTy, ThisCapture->getType())) 3369 CheckCXXThisCapture(LC.getLocation()); 3370 } 3371 } 3372 } 3373 DSAStack->setForceCaptureByReferenceInTargetExecutable( 3374 SavedForceCaptureByReferenceInTargetExecutable); 3375 } 3376 } 3377 } 3378 3379 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 3380 ArrayRef<OMPClause *> Clauses) { 3381 bool ErrorFound = false; 3382 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 3383 *this, ErrorFound, DSAStack->getCurrentDirective()); 3384 if (!S.isUsable()) { 3385 ErrorFound = true; 3386 return StmtError(); 3387 } 3388 3389 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 3390 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 3391 OMPOrderedClause *OC = nullptr; 3392 OMPScheduleClause *SC = nullptr; 3393 SmallVector<const OMPLinearClause *, 4> LCs; 3394 SmallVector<const OMPClauseWithPreInit *, 4> PICs; 3395 // This is required for proper codegen. 3396 for (OMPClause *Clause : Clauses) { 3397 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) && 3398 Clause->getClauseKind() == OMPC_in_reduction) { 3399 // Capture taskgroup task_reduction descriptors inside the tasking regions 3400 // with the corresponding in_reduction items. 3401 auto *IRC = cast<OMPInReductionClause>(Clause); 3402 for (Expr *E : IRC->taskgroup_descriptors()) 3403 if (E) 3404 MarkDeclarationsReferencedInExpr(E); 3405 } 3406 if (isOpenMPPrivate(Clause->getClauseKind()) || 3407 Clause->getClauseKind() == OMPC_copyprivate || 3408 (getLangOpts().OpenMPUseTLS && 3409 getASTContext().getTargetInfo().isTLSSupported() && 3410 Clause->getClauseKind() == OMPC_copyin)) { 3411 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 3412 // Mark all variables in private list clauses as used in inner region. 3413 for (Stmt *VarRef : Clause->children()) { 3414 if (auto *E = cast_or_null<Expr>(VarRef)) { 3415 MarkDeclarationsReferencedInExpr(E); 3416 } 3417 } 3418 DSAStack->setForceVarCapturing(/*V=*/false); 3419 } else if (CaptureRegions.size() > 1 || 3420 CaptureRegions.back() != OMPD_unknown) { 3421 if (auto *C = OMPClauseWithPreInit::get(Clause)) 3422 PICs.push_back(C); 3423 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 3424 if (Expr *E = C->getPostUpdateExpr()) 3425 MarkDeclarationsReferencedInExpr(E); 3426 } 3427 } 3428 if (Clause->getClauseKind() == OMPC_schedule) 3429 SC = cast<OMPScheduleClause>(Clause); 3430 else if (Clause->getClauseKind() == OMPC_ordered) 3431 OC = cast<OMPOrderedClause>(Clause); 3432 else if (Clause->getClauseKind() == OMPC_linear) 3433 LCs.push_back(cast<OMPLinearClause>(Clause)); 3434 } 3435 // OpenMP, 2.7.1 Loop Construct, Restrictions 3436 // The nonmonotonic modifier cannot be specified if an ordered clause is 3437 // specified. 3438 if (SC && 3439 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 3440 SC->getSecondScheduleModifier() == 3441 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 3442 OC) { 3443 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 3444 ? SC->getFirstScheduleModifierLoc() 3445 : SC->getSecondScheduleModifierLoc(), 3446 diag::err_omp_schedule_nonmonotonic_ordered) 3447 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 3448 ErrorFound = true; 3449 } 3450 if (!LCs.empty() && OC && OC->getNumForLoops()) { 3451 for (const OMPLinearClause *C : LCs) { 3452 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered) 3453 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 3454 } 3455 ErrorFound = true; 3456 } 3457 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 3458 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 3459 OC->getNumForLoops()) { 3460 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd) 3461 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 3462 ErrorFound = true; 3463 } 3464 if (ErrorFound) { 3465 return StmtError(); 3466 } 3467 StmtResult SR = S; 3468 unsigned CompletedRegions = 0; 3469 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 3470 // Mark all variables in private list clauses as used in inner region. 3471 // Required for proper codegen of combined directives. 3472 // TODO: add processing for other clauses. 3473 if (ThisCaptureRegion != OMPD_unknown) { 3474 for (const clang::OMPClauseWithPreInit *C : PICs) { 3475 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 3476 // Find the particular capture region for the clause if the 3477 // directive is a combined one with multiple capture regions. 3478 // If the directive is not a combined one, the capture region 3479 // associated with the clause is OMPD_unknown and is generated 3480 // only once. 3481 if (CaptureRegion == ThisCaptureRegion || 3482 CaptureRegion == OMPD_unknown) { 3483 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 3484 for (Decl *D : DS->decls()) 3485 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 3486 } 3487 } 3488 } 3489 } 3490 if (++CompletedRegions == CaptureRegions.size()) 3491 DSAStack->setBodyComplete(); 3492 SR = ActOnCapturedRegionEnd(SR.get()); 3493 } 3494 return SR; 3495 } 3496 3497 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 3498 OpenMPDirectiveKind CancelRegion, 3499 SourceLocation StartLoc) { 3500 // CancelRegion is only needed for cancel and cancellation_point. 3501 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 3502 return false; 3503 3504 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 3505 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 3506 return false; 3507 3508 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 3509 << getOpenMPDirectiveName(CancelRegion); 3510 return true; 3511 } 3512 3513 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack, 3514 OpenMPDirectiveKind CurrentRegion, 3515 const DeclarationNameInfo &CurrentName, 3516 OpenMPDirectiveKind CancelRegion, 3517 SourceLocation StartLoc) { 3518 if (Stack->getCurScope()) { 3519 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective(); 3520 OpenMPDirectiveKind OffendingRegion = ParentRegion; 3521 bool NestingProhibited = false; 3522 bool CloseNesting = true; 3523 bool OrphanSeen = false; 3524 enum { 3525 NoRecommend, 3526 ShouldBeInParallelRegion, 3527 ShouldBeInOrderedRegion, 3528 ShouldBeInTargetRegion, 3529 ShouldBeInTeamsRegion 3530 } Recommend = NoRecommend; 3531 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) { 3532 // OpenMP [2.16, Nesting of Regions] 3533 // OpenMP constructs may not be nested inside a simd region. 3534 // OpenMP [2.8.1,simd Construct, Restrictions] 3535 // An ordered construct with the simd clause is the only OpenMP 3536 // construct that can appear in the simd region. 3537 // Allowing a SIMD construct nested in another SIMD construct is an 3538 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 3539 // message. 3540 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 3541 ? diag::err_omp_prohibited_region_simd 3542 : diag::warn_omp_nesting_simd); 3543 return CurrentRegion != OMPD_simd; 3544 } 3545 if (ParentRegion == OMPD_atomic) { 3546 // OpenMP [2.16, Nesting of Regions] 3547 // OpenMP constructs may not be nested inside an atomic region. 3548 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 3549 return true; 3550 } 3551 if (CurrentRegion == OMPD_section) { 3552 // OpenMP [2.7.2, sections Construct, Restrictions] 3553 // Orphaned section directives are prohibited. That is, the section 3554 // directives must appear within the sections construct and must not be 3555 // encountered elsewhere in the sections region. 3556 if (ParentRegion != OMPD_sections && 3557 ParentRegion != OMPD_parallel_sections) { 3558 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 3559 << (ParentRegion != OMPD_unknown) 3560 << getOpenMPDirectiveName(ParentRegion); 3561 return true; 3562 } 3563 return false; 3564 } 3565 // Allow some constructs (except teams and cancellation constructs) to be 3566 // orphaned (they could be used in functions, called from OpenMP regions 3567 // with the required preconditions). 3568 if (ParentRegion == OMPD_unknown && 3569 !isOpenMPNestingTeamsDirective(CurrentRegion) && 3570 CurrentRegion != OMPD_cancellation_point && 3571 CurrentRegion != OMPD_cancel) 3572 return false; 3573 if (CurrentRegion == OMPD_cancellation_point || 3574 CurrentRegion == OMPD_cancel) { 3575 // OpenMP [2.16, Nesting of Regions] 3576 // A cancellation point construct for which construct-type-clause is 3577 // taskgroup must be nested inside a task construct. A cancellation 3578 // point construct for which construct-type-clause is not taskgroup must 3579 // be closely nested inside an OpenMP construct that matches the type 3580 // specified in construct-type-clause. 3581 // A cancel construct for which construct-type-clause is taskgroup must be 3582 // nested inside a task construct. A cancel construct for which 3583 // construct-type-clause is not taskgroup must be closely nested inside an 3584 // OpenMP construct that matches the type specified in 3585 // construct-type-clause. 3586 NestingProhibited = 3587 !((CancelRegion == OMPD_parallel && 3588 (ParentRegion == OMPD_parallel || 3589 ParentRegion == OMPD_target_parallel)) || 3590 (CancelRegion == OMPD_for && 3591 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 3592 ParentRegion == OMPD_target_parallel_for || 3593 ParentRegion == OMPD_distribute_parallel_for || 3594 ParentRegion == OMPD_teams_distribute_parallel_for || 3595 ParentRegion == OMPD_target_teams_distribute_parallel_for)) || 3596 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) || 3597 (CancelRegion == OMPD_sections && 3598 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 3599 ParentRegion == OMPD_parallel_sections))); 3600 OrphanSeen = ParentRegion == OMPD_unknown; 3601 } else if (CurrentRegion == OMPD_master) { 3602 // OpenMP [2.16, Nesting of Regions] 3603 // A master region may not be closely nested inside a worksharing, 3604 // atomic, or explicit task region. 3605 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 3606 isOpenMPTaskingDirective(ParentRegion); 3607 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 3608 // OpenMP [2.16, Nesting of Regions] 3609 // A critical region may not be nested (closely or otherwise) inside a 3610 // critical region with the same name. Note that this restriction is not 3611 // sufficient to prevent deadlock. 3612 SourceLocation PreviousCriticalLoc; 3613 bool DeadLock = Stack->hasDirective( 3614 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 3615 const DeclarationNameInfo &DNI, 3616 SourceLocation Loc) { 3617 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 3618 PreviousCriticalLoc = Loc; 3619 return true; 3620 } 3621 return false; 3622 }, 3623 false /* skip top directive */); 3624 if (DeadLock) { 3625 SemaRef.Diag(StartLoc, 3626 diag::err_omp_prohibited_region_critical_same_name) 3627 << CurrentName.getName(); 3628 if (PreviousCriticalLoc.isValid()) 3629 SemaRef.Diag(PreviousCriticalLoc, 3630 diag::note_omp_previous_critical_region); 3631 return true; 3632 } 3633 } else if (CurrentRegion == OMPD_barrier) { 3634 // OpenMP [2.16, Nesting of Regions] 3635 // A barrier region may not be closely nested inside a worksharing, 3636 // explicit task, critical, ordered, atomic, or master region. 3637 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 3638 isOpenMPTaskingDirective(ParentRegion) || 3639 ParentRegion == OMPD_master || 3640 ParentRegion == OMPD_critical || 3641 ParentRegion == OMPD_ordered; 3642 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 3643 !isOpenMPParallelDirective(CurrentRegion) && 3644 !isOpenMPTeamsDirective(CurrentRegion)) { 3645 // OpenMP [2.16, Nesting of Regions] 3646 // A worksharing region may not be closely nested inside a worksharing, 3647 // explicit task, critical, ordered, atomic, or master region. 3648 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 3649 isOpenMPTaskingDirective(ParentRegion) || 3650 ParentRegion == OMPD_master || 3651 ParentRegion == OMPD_critical || 3652 ParentRegion == OMPD_ordered; 3653 Recommend = ShouldBeInParallelRegion; 3654 } else if (CurrentRegion == OMPD_ordered) { 3655 // OpenMP [2.16, Nesting of Regions] 3656 // An ordered region may not be closely nested inside a critical, 3657 // atomic, or explicit task region. 3658 // An ordered region must be closely nested inside a loop region (or 3659 // parallel loop region) with an ordered clause. 3660 // OpenMP [2.8.1,simd Construct, Restrictions] 3661 // An ordered construct with the simd clause is the only OpenMP construct 3662 // that can appear in the simd region. 3663 NestingProhibited = ParentRegion == OMPD_critical || 3664 isOpenMPTaskingDirective(ParentRegion) || 3665 !(isOpenMPSimdDirective(ParentRegion) || 3666 Stack->isParentOrderedRegion()); 3667 Recommend = ShouldBeInOrderedRegion; 3668 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 3669 // OpenMP [2.16, Nesting of Regions] 3670 // If specified, a teams construct must be contained within a target 3671 // construct. 3672 NestingProhibited = ParentRegion != OMPD_target; 3673 OrphanSeen = ParentRegion == OMPD_unknown; 3674 Recommend = ShouldBeInTargetRegion; 3675 } 3676 if (!NestingProhibited && 3677 !isOpenMPTargetExecutionDirective(CurrentRegion) && 3678 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 3679 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 3680 // OpenMP [2.16, Nesting of Regions] 3681 // distribute, parallel, parallel sections, parallel workshare, and the 3682 // parallel loop and parallel loop SIMD constructs are the only OpenMP 3683 // constructs that can be closely nested in the teams region. 3684 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 3685 !isOpenMPDistributeDirective(CurrentRegion); 3686 Recommend = ShouldBeInParallelRegion; 3687 } 3688 if (!NestingProhibited && 3689 isOpenMPNestingDistributeDirective(CurrentRegion)) { 3690 // OpenMP 4.5 [2.17 Nesting of Regions] 3691 // The region associated with the distribute construct must be strictly 3692 // nested inside a teams region 3693 NestingProhibited = 3694 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 3695 Recommend = ShouldBeInTeamsRegion; 3696 } 3697 if (!NestingProhibited && 3698 (isOpenMPTargetExecutionDirective(CurrentRegion) || 3699 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 3700 // OpenMP 4.5 [2.17 Nesting of Regions] 3701 // If a target, target update, target data, target enter data, or 3702 // target exit data construct is encountered during execution of a 3703 // target region, the behavior is unspecified. 3704 NestingProhibited = Stack->hasDirective( 3705 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 3706 SourceLocation) { 3707 if (isOpenMPTargetExecutionDirective(K)) { 3708 OffendingRegion = K; 3709 return true; 3710 } 3711 return false; 3712 }, 3713 false /* don't skip top directive */); 3714 CloseNesting = false; 3715 } 3716 if (NestingProhibited) { 3717 if (OrphanSeen) { 3718 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 3719 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 3720 } else { 3721 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 3722 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 3723 << Recommend << getOpenMPDirectiveName(CurrentRegion); 3724 } 3725 return true; 3726 } 3727 } 3728 return false; 3729 } 3730 3731 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 3732 ArrayRef<OMPClause *> Clauses, 3733 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 3734 bool ErrorFound = false; 3735 unsigned NamedModifiersNumber = 0; 3736 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers( 3737 OMPD_unknown + 1); 3738 SmallVector<SourceLocation, 4> NameModifierLoc; 3739 for (const OMPClause *C : Clauses) { 3740 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 3741 // At most one if clause without a directive-name-modifier can appear on 3742 // the directive. 3743 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 3744 if (FoundNameModifiers[CurNM]) { 3745 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 3746 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 3747 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 3748 ErrorFound = true; 3749 } else if (CurNM != OMPD_unknown) { 3750 NameModifierLoc.push_back(IC->getNameModifierLoc()); 3751 ++NamedModifiersNumber; 3752 } 3753 FoundNameModifiers[CurNM] = IC; 3754 if (CurNM == OMPD_unknown) 3755 continue; 3756 // Check if the specified name modifier is allowed for the current 3757 // directive. 3758 // At most one if clause with the particular directive-name-modifier can 3759 // appear on the directive. 3760 bool MatchFound = false; 3761 for (auto NM : AllowedNameModifiers) { 3762 if (CurNM == NM) { 3763 MatchFound = true; 3764 break; 3765 } 3766 } 3767 if (!MatchFound) { 3768 S.Diag(IC->getNameModifierLoc(), 3769 diag::err_omp_wrong_if_directive_name_modifier) 3770 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 3771 ErrorFound = true; 3772 } 3773 } 3774 } 3775 // If any if clause on the directive includes a directive-name-modifier then 3776 // all if clauses on the directive must include a directive-name-modifier. 3777 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 3778 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 3779 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(), 3780 diag::err_omp_no_more_if_clause); 3781 } else { 3782 std::string Values; 3783 std::string Sep(", "); 3784 unsigned AllowedCnt = 0; 3785 unsigned TotalAllowedNum = 3786 AllowedNameModifiers.size() - NamedModifiersNumber; 3787 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 3788 ++Cnt) { 3789 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 3790 if (!FoundNameModifiers[NM]) { 3791 Values += "'"; 3792 Values += getOpenMPDirectiveName(NM); 3793 Values += "'"; 3794 if (AllowedCnt + 2 == TotalAllowedNum) 3795 Values += " or "; 3796 else if (AllowedCnt + 1 != TotalAllowedNum) 3797 Values += Sep; 3798 ++AllowedCnt; 3799 } 3800 } 3801 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(), 3802 diag::err_omp_unnamed_if_clause) 3803 << (TotalAllowedNum > 1) << Values; 3804 } 3805 for (SourceLocation Loc : NameModifierLoc) { 3806 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 3807 } 3808 ErrorFound = true; 3809 } 3810 return ErrorFound; 3811 } 3812 3813 static std::pair<ValueDecl *, bool> 3814 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 3815 SourceRange &ERange, bool AllowArraySection = false) { 3816 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 3817 RefExpr->containsUnexpandedParameterPack()) 3818 return std::make_pair(nullptr, true); 3819 3820 // OpenMP [3.1, C/C++] 3821 // A list item is a variable name. 3822 // OpenMP [2.9.3.3, Restrictions, p.1] 3823 // A variable that is part of another variable (as an array or 3824 // structure element) cannot appear in a private clause. 3825 RefExpr = RefExpr->IgnoreParens(); 3826 enum { 3827 NoArrayExpr = -1, 3828 ArraySubscript = 0, 3829 OMPArraySection = 1 3830 } IsArrayExpr = NoArrayExpr; 3831 if (AllowArraySection) { 3832 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 3833 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 3834 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 3835 Base = TempASE->getBase()->IgnoreParenImpCasts(); 3836 RefExpr = Base; 3837 IsArrayExpr = ArraySubscript; 3838 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 3839 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 3840 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 3841 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 3842 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 3843 Base = TempASE->getBase()->IgnoreParenImpCasts(); 3844 RefExpr = Base; 3845 IsArrayExpr = OMPArraySection; 3846 } 3847 } 3848 ELoc = RefExpr->getExprLoc(); 3849 ERange = RefExpr->getSourceRange(); 3850 RefExpr = RefExpr->IgnoreParenImpCasts(); 3851 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 3852 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 3853 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 3854 (S.getCurrentThisType().isNull() || !ME || 3855 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 3856 !isa<FieldDecl>(ME->getMemberDecl()))) { 3857 if (IsArrayExpr != NoArrayExpr) { 3858 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 3859 << ERange; 3860 } else { 3861 S.Diag(ELoc, 3862 AllowArraySection 3863 ? diag::err_omp_expected_var_name_member_expr_or_array_item 3864 : diag::err_omp_expected_var_name_member_expr) 3865 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 3866 } 3867 return std::make_pair(nullptr, false); 3868 } 3869 return std::make_pair( 3870 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 3871 } 3872 3873 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 3874 ArrayRef<OMPClause *> Clauses) { 3875 assert(!S.CurContext->isDependentContext() && 3876 "Expected non-dependent context."); 3877 auto AllocateRange = 3878 llvm::make_filter_range(Clauses, OMPAllocateClause::classof); 3879 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> 3880 DeclToCopy; 3881 auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) { 3882 return isOpenMPPrivate(C->getClauseKind()); 3883 }); 3884 for (OMPClause *Cl : PrivateRange) { 3885 MutableArrayRef<Expr *>::iterator I, It, Et; 3886 if (Cl->getClauseKind() == OMPC_private) { 3887 auto *PC = cast<OMPPrivateClause>(Cl); 3888 I = PC->private_copies().begin(); 3889 It = PC->varlist_begin(); 3890 Et = PC->varlist_end(); 3891 } else if (Cl->getClauseKind() == OMPC_firstprivate) { 3892 auto *PC = cast<OMPFirstprivateClause>(Cl); 3893 I = PC->private_copies().begin(); 3894 It = PC->varlist_begin(); 3895 Et = PC->varlist_end(); 3896 } else if (Cl->getClauseKind() == OMPC_lastprivate) { 3897 auto *PC = cast<OMPLastprivateClause>(Cl); 3898 I = PC->private_copies().begin(); 3899 It = PC->varlist_begin(); 3900 Et = PC->varlist_end(); 3901 } else if (Cl->getClauseKind() == OMPC_linear) { 3902 auto *PC = cast<OMPLinearClause>(Cl); 3903 I = PC->privates().begin(); 3904 It = PC->varlist_begin(); 3905 Et = PC->varlist_end(); 3906 } else if (Cl->getClauseKind() == OMPC_reduction) { 3907 auto *PC = cast<OMPReductionClause>(Cl); 3908 I = PC->privates().begin(); 3909 It = PC->varlist_begin(); 3910 Et = PC->varlist_end(); 3911 } else if (Cl->getClauseKind() == OMPC_task_reduction) { 3912 auto *PC = cast<OMPTaskReductionClause>(Cl); 3913 I = PC->privates().begin(); 3914 It = PC->varlist_begin(); 3915 Et = PC->varlist_end(); 3916 } else if (Cl->getClauseKind() == OMPC_in_reduction) { 3917 auto *PC = cast<OMPInReductionClause>(Cl); 3918 I = PC->privates().begin(); 3919 It = PC->varlist_begin(); 3920 Et = PC->varlist_end(); 3921 } else { 3922 llvm_unreachable("Expected private clause."); 3923 } 3924 for (Expr *E : llvm::make_range(It, Et)) { 3925 if (!*I) { 3926 ++I; 3927 continue; 3928 } 3929 SourceLocation ELoc; 3930 SourceRange ERange; 3931 Expr *SimpleRefExpr = E; 3932 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 3933 /*AllowArraySection=*/true); 3934 DeclToCopy.try_emplace(Res.first, 3935 cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl())); 3936 ++I; 3937 } 3938 } 3939 for (OMPClause *C : AllocateRange) { 3940 auto *AC = cast<OMPAllocateClause>(C); 3941 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 3942 getAllocatorKind(S, Stack, AC->getAllocator()); 3943 // OpenMP, 2.11.4 allocate Clause, Restrictions. 3944 // For task, taskloop or target directives, allocation requests to memory 3945 // allocators with the trait access set to thread result in unspecified 3946 // behavior. 3947 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc && 3948 (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 3949 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) { 3950 S.Diag(AC->getAllocator()->getExprLoc(), 3951 diag::warn_omp_allocate_thread_on_task_target_directive) 3952 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 3953 } 3954 for (Expr *E : AC->varlists()) { 3955 SourceLocation ELoc; 3956 SourceRange ERange; 3957 Expr *SimpleRefExpr = E; 3958 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange); 3959 ValueDecl *VD = Res.first; 3960 DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false); 3961 if (!isOpenMPPrivate(Data.CKind)) { 3962 S.Diag(E->getExprLoc(), 3963 diag::err_omp_expected_private_copy_for_allocate); 3964 continue; 3965 } 3966 VarDecl *PrivateVD = DeclToCopy[VD]; 3967 if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD, 3968 AllocatorKind, AC->getAllocator())) 3969 continue; 3970 applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(), 3971 E->getSourceRange()); 3972 } 3973 } 3974 } 3975 3976 StmtResult Sema::ActOnOpenMPExecutableDirective( 3977 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 3978 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 3979 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 3980 StmtResult Res = StmtError(); 3981 // First check CancelRegion which is then used in checkNestingOfRegions. 3982 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 3983 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 3984 StartLoc)) 3985 return StmtError(); 3986 3987 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 3988 VarsWithInheritedDSAType VarsWithInheritedDSA; 3989 bool ErrorFound = false; 3990 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 3991 if (AStmt && !CurContext->isDependentContext()) { 3992 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 3993 3994 // Check default data sharing attributes for referenced variables. 3995 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 3996 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 3997 Stmt *S = AStmt; 3998 while (--ThisCaptureLevel >= 0) 3999 S = cast<CapturedStmt>(S)->getCapturedStmt(); 4000 DSAChecker.Visit(S); 4001 if (!isOpenMPTargetDataManagementDirective(Kind) && 4002 !isOpenMPTaskingDirective(Kind)) { 4003 // Visit subcaptures to generate implicit clauses for captured vars. 4004 auto *CS = cast<CapturedStmt>(AStmt); 4005 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4006 getOpenMPCaptureRegions(CaptureRegions, Kind); 4007 // Ignore outer tasking regions for target directives. 4008 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task) 4009 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 4010 DSAChecker.visitSubCaptures(CS); 4011 } 4012 if (DSAChecker.isErrorFound()) 4013 return StmtError(); 4014 // Generate list of implicitly defined firstprivate variables. 4015 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 4016 4017 SmallVector<Expr *, 4> ImplicitFirstprivates( 4018 DSAChecker.getImplicitFirstprivate().begin(), 4019 DSAChecker.getImplicitFirstprivate().end()); 4020 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(), 4021 DSAChecker.getImplicitMap().end()); 4022 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 4023 for (OMPClause *C : Clauses) { 4024 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 4025 for (Expr *E : IRC->taskgroup_descriptors()) 4026 if (E) 4027 ImplicitFirstprivates.emplace_back(E); 4028 } 4029 } 4030 if (!ImplicitFirstprivates.empty()) { 4031 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 4032 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 4033 SourceLocation())) { 4034 ClausesWithImplicit.push_back(Implicit); 4035 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 4036 ImplicitFirstprivates.size(); 4037 } else { 4038 ErrorFound = true; 4039 } 4040 } 4041 if (!ImplicitMaps.empty()) { 4042 CXXScopeSpec MapperIdScopeSpec; 4043 DeclarationNameInfo MapperId; 4044 if (OMPClause *Implicit = ActOnOpenMPMapClause( 4045 llvm::None, llvm::None, MapperIdScopeSpec, MapperId, 4046 OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true, SourceLocation(), 4047 SourceLocation(), ImplicitMaps, OMPVarListLocTy())) { 4048 ClausesWithImplicit.emplace_back(Implicit); 4049 ErrorFound |= 4050 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size(); 4051 } else { 4052 ErrorFound = true; 4053 } 4054 } 4055 } 4056 4057 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 4058 switch (Kind) { 4059 case OMPD_parallel: 4060 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 4061 EndLoc); 4062 AllowedNameModifiers.push_back(OMPD_parallel); 4063 break; 4064 case OMPD_simd: 4065 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 4066 VarsWithInheritedDSA); 4067 break; 4068 case OMPD_for: 4069 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 4070 VarsWithInheritedDSA); 4071 break; 4072 case OMPD_for_simd: 4073 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 4074 EndLoc, VarsWithInheritedDSA); 4075 break; 4076 case OMPD_sections: 4077 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 4078 EndLoc); 4079 break; 4080 case OMPD_section: 4081 assert(ClausesWithImplicit.empty() && 4082 "No clauses are allowed for 'omp section' directive"); 4083 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 4084 break; 4085 case OMPD_single: 4086 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 4087 EndLoc); 4088 break; 4089 case OMPD_master: 4090 assert(ClausesWithImplicit.empty() && 4091 "No clauses are allowed for 'omp master' directive"); 4092 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 4093 break; 4094 case OMPD_critical: 4095 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 4096 StartLoc, EndLoc); 4097 break; 4098 case OMPD_parallel_for: 4099 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 4100 EndLoc, VarsWithInheritedDSA); 4101 AllowedNameModifiers.push_back(OMPD_parallel); 4102 break; 4103 case OMPD_parallel_for_simd: 4104 Res = ActOnOpenMPParallelForSimdDirective( 4105 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4106 AllowedNameModifiers.push_back(OMPD_parallel); 4107 break; 4108 case OMPD_parallel_sections: 4109 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 4110 StartLoc, EndLoc); 4111 AllowedNameModifiers.push_back(OMPD_parallel); 4112 break; 4113 case OMPD_task: 4114 Res = 4115 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 4116 AllowedNameModifiers.push_back(OMPD_task); 4117 break; 4118 case OMPD_taskyield: 4119 assert(ClausesWithImplicit.empty() && 4120 "No clauses are allowed for 'omp taskyield' directive"); 4121 assert(AStmt == nullptr && 4122 "No associated statement allowed for 'omp taskyield' directive"); 4123 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 4124 break; 4125 case OMPD_barrier: 4126 assert(ClausesWithImplicit.empty() && 4127 "No clauses are allowed for 'omp barrier' directive"); 4128 assert(AStmt == nullptr && 4129 "No associated statement allowed for 'omp barrier' directive"); 4130 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 4131 break; 4132 case OMPD_taskwait: 4133 assert(ClausesWithImplicit.empty() && 4134 "No clauses are allowed for 'omp taskwait' directive"); 4135 assert(AStmt == nullptr && 4136 "No associated statement allowed for 'omp taskwait' directive"); 4137 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 4138 break; 4139 case OMPD_taskgroup: 4140 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 4141 EndLoc); 4142 break; 4143 case OMPD_flush: 4144 assert(AStmt == nullptr && 4145 "No associated statement allowed for 'omp flush' directive"); 4146 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 4147 break; 4148 case OMPD_ordered: 4149 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 4150 EndLoc); 4151 break; 4152 case OMPD_atomic: 4153 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 4154 EndLoc); 4155 break; 4156 case OMPD_teams: 4157 Res = 4158 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 4159 break; 4160 case OMPD_target: 4161 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 4162 EndLoc); 4163 AllowedNameModifiers.push_back(OMPD_target); 4164 break; 4165 case OMPD_target_parallel: 4166 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 4167 StartLoc, EndLoc); 4168 AllowedNameModifiers.push_back(OMPD_target); 4169 AllowedNameModifiers.push_back(OMPD_parallel); 4170 break; 4171 case OMPD_target_parallel_for: 4172 Res = ActOnOpenMPTargetParallelForDirective( 4173 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4174 AllowedNameModifiers.push_back(OMPD_target); 4175 AllowedNameModifiers.push_back(OMPD_parallel); 4176 break; 4177 case OMPD_cancellation_point: 4178 assert(ClausesWithImplicit.empty() && 4179 "No clauses are allowed for 'omp cancellation point' directive"); 4180 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 4181 "cancellation point' directive"); 4182 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 4183 break; 4184 case OMPD_cancel: 4185 assert(AStmt == nullptr && 4186 "No associated statement allowed for 'omp cancel' directive"); 4187 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 4188 CancelRegion); 4189 AllowedNameModifiers.push_back(OMPD_cancel); 4190 break; 4191 case OMPD_target_data: 4192 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 4193 EndLoc); 4194 AllowedNameModifiers.push_back(OMPD_target_data); 4195 break; 4196 case OMPD_target_enter_data: 4197 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 4198 EndLoc, AStmt); 4199 AllowedNameModifiers.push_back(OMPD_target_enter_data); 4200 break; 4201 case OMPD_target_exit_data: 4202 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 4203 EndLoc, AStmt); 4204 AllowedNameModifiers.push_back(OMPD_target_exit_data); 4205 break; 4206 case OMPD_taskloop: 4207 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 4208 EndLoc, VarsWithInheritedDSA); 4209 AllowedNameModifiers.push_back(OMPD_taskloop); 4210 break; 4211 case OMPD_taskloop_simd: 4212 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 4213 EndLoc, VarsWithInheritedDSA); 4214 AllowedNameModifiers.push_back(OMPD_taskloop); 4215 break; 4216 case OMPD_distribute: 4217 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 4218 EndLoc, VarsWithInheritedDSA); 4219 break; 4220 case OMPD_target_update: 4221 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 4222 EndLoc, AStmt); 4223 AllowedNameModifiers.push_back(OMPD_target_update); 4224 break; 4225 case OMPD_distribute_parallel_for: 4226 Res = ActOnOpenMPDistributeParallelForDirective( 4227 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4228 AllowedNameModifiers.push_back(OMPD_parallel); 4229 break; 4230 case OMPD_distribute_parallel_for_simd: 4231 Res = ActOnOpenMPDistributeParallelForSimdDirective( 4232 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4233 AllowedNameModifiers.push_back(OMPD_parallel); 4234 break; 4235 case OMPD_distribute_simd: 4236 Res = ActOnOpenMPDistributeSimdDirective( 4237 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4238 break; 4239 case OMPD_target_parallel_for_simd: 4240 Res = ActOnOpenMPTargetParallelForSimdDirective( 4241 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4242 AllowedNameModifiers.push_back(OMPD_target); 4243 AllowedNameModifiers.push_back(OMPD_parallel); 4244 break; 4245 case OMPD_target_simd: 4246 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 4247 EndLoc, VarsWithInheritedDSA); 4248 AllowedNameModifiers.push_back(OMPD_target); 4249 break; 4250 case OMPD_teams_distribute: 4251 Res = ActOnOpenMPTeamsDistributeDirective( 4252 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4253 break; 4254 case OMPD_teams_distribute_simd: 4255 Res = ActOnOpenMPTeamsDistributeSimdDirective( 4256 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4257 break; 4258 case OMPD_teams_distribute_parallel_for_simd: 4259 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 4260 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4261 AllowedNameModifiers.push_back(OMPD_parallel); 4262 break; 4263 case OMPD_teams_distribute_parallel_for: 4264 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 4265 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4266 AllowedNameModifiers.push_back(OMPD_parallel); 4267 break; 4268 case OMPD_target_teams: 4269 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 4270 EndLoc); 4271 AllowedNameModifiers.push_back(OMPD_target); 4272 break; 4273 case OMPD_target_teams_distribute: 4274 Res = ActOnOpenMPTargetTeamsDistributeDirective( 4275 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4276 AllowedNameModifiers.push_back(OMPD_target); 4277 break; 4278 case OMPD_target_teams_distribute_parallel_for: 4279 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 4280 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4281 AllowedNameModifiers.push_back(OMPD_target); 4282 AllowedNameModifiers.push_back(OMPD_parallel); 4283 break; 4284 case OMPD_target_teams_distribute_parallel_for_simd: 4285 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 4286 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4287 AllowedNameModifiers.push_back(OMPD_target); 4288 AllowedNameModifiers.push_back(OMPD_parallel); 4289 break; 4290 case OMPD_target_teams_distribute_simd: 4291 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 4292 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 4293 AllowedNameModifiers.push_back(OMPD_target); 4294 break; 4295 case OMPD_declare_target: 4296 case OMPD_end_declare_target: 4297 case OMPD_threadprivate: 4298 case OMPD_allocate: 4299 case OMPD_declare_reduction: 4300 case OMPD_declare_mapper: 4301 case OMPD_declare_simd: 4302 case OMPD_requires: 4303 llvm_unreachable("OpenMP Directive is not allowed"); 4304 case OMPD_unknown: 4305 llvm_unreachable("Unknown OpenMP directive"); 4306 } 4307 4308 ErrorFound = Res.isInvalid() || ErrorFound; 4309 4310 // Check variables in the clauses if default(none) was specified. 4311 if (DSAStack->getDefaultDSA() == DSA_none) { 4312 DSAAttrChecker DSAChecker(DSAStack, *this, nullptr); 4313 for (OMPClause *C : Clauses) { 4314 switch (C->getClauseKind()) { 4315 case OMPC_num_threads: 4316 case OMPC_dist_schedule: 4317 // Do not analyse if no parent teams directive. 4318 if (isOpenMPTeamsDirective(DSAStack->getCurrentDirective())) 4319 break; 4320 continue; 4321 case OMPC_if: 4322 if (isOpenMPTeamsDirective(DSAStack->getCurrentDirective()) && 4323 cast<OMPIfClause>(C)->getNameModifier() != OMPD_target) 4324 break; 4325 continue; 4326 case OMPC_schedule: 4327 break; 4328 case OMPC_ordered: 4329 case OMPC_device: 4330 case OMPC_num_teams: 4331 case OMPC_thread_limit: 4332 case OMPC_priority: 4333 case OMPC_grainsize: 4334 case OMPC_num_tasks: 4335 case OMPC_hint: 4336 case OMPC_collapse: 4337 case OMPC_safelen: 4338 case OMPC_simdlen: 4339 case OMPC_final: 4340 case OMPC_default: 4341 case OMPC_proc_bind: 4342 case OMPC_private: 4343 case OMPC_firstprivate: 4344 case OMPC_lastprivate: 4345 case OMPC_shared: 4346 case OMPC_reduction: 4347 case OMPC_task_reduction: 4348 case OMPC_in_reduction: 4349 case OMPC_linear: 4350 case OMPC_aligned: 4351 case OMPC_copyin: 4352 case OMPC_copyprivate: 4353 case OMPC_nowait: 4354 case OMPC_untied: 4355 case OMPC_mergeable: 4356 case OMPC_allocate: 4357 case OMPC_read: 4358 case OMPC_write: 4359 case OMPC_update: 4360 case OMPC_capture: 4361 case OMPC_seq_cst: 4362 case OMPC_depend: 4363 case OMPC_threads: 4364 case OMPC_simd: 4365 case OMPC_map: 4366 case OMPC_nogroup: 4367 case OMPC_defaultmap: 4368 case OMPC_to: 4369 case OMPC_from: 4370 case OMPC_use_device_ptr: 4371 case OMPC_is_device_ptr: 4372 continue; 4373 case OMPC_allocator: 4374 case OMPC_flush: 4375 case OMPC_threadprivate: 4376 case OMPC_uniform: 4377 case OMPC_unknown: 4378 case OMPC_unified_address: 4379 case OMPC_unified_shared_memory: 4380 case OMPC_reverse_offload: 4381 case OMPC_dynamic_allocators: 4382 case OMPC_atomic_default_mem_order: 4383 llvm_unreachable("Unexpected clause"); 4384 } 4385 for (Stmt *CC : C->children()) { 4386 if (CC) 4387 DSAChecker.Visit(CC); 4388 } 4389 } 4390 for (auto &P : DSAChecker.getVarsWithInheritedDSA()) 4391 VarsWithInheritedDSA[P.getFirst()] = P.getSecond(); 4392 } 4393 for (const auto &P : VarsWithInheritedDSA) { 4394 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst())) 4395 continue; 4396 ErrorFound = true; 4397 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 4398 << P.first << P.second->getSourceRange(); 4399 Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none); 4400 } 4401 4402 if (!AllowedNameModifiers.empty()) 4403 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 4404 ErrorFound; 4405 4406 if (ErrorFound) 4407 return StmtError(); 4408 4409 if (!(Res.getAs<OMPExecutableDirective>()->isStandaloneDirective())) { 4410 Res.getAs<OMPExecutableDirective>() 4411 ->getStructuredBlock() 4412 ->setIsOMPStructuredBlock(true); 4413 } 4414 4415 if (!CurContext->isDependentContext() && 4416 isOpenMPTargetExecutionDirective(Kind) && 4417 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 4418 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() || 4419 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() || 4420 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) { 4421 // Register target to DSA Stack. 4422 DSAStack->addTargetDirLocation(StartLoc); 4423 } 4424 4425 return Res; 4426 } 4427 4428 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 4429 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 4430 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 4431 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 4432 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 4433 assert(Aligneds.size() == Alignments.size()); 4434 assert(Linears.size() == LinModifiers.size()); 4435 assert(Linears.size() == Steps.size()); 4436 if (!DG || DG.get().isNull()) 4437 return DeclGroupPtrTy(); 4438 4439 if (!DG.get().isSingleDecl()) { 4440 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd); 4441 return DG; 4442 } 4443 Decl *ADecl = DG.get().getSingleDecl(); 4444 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 4445 ADecl = FTD->getTemplatedDecl(); 4446 4447 auto *FD = dyn_cast<FunctionDecl>(ADecl); 4448 if (!FD) { 4449 Diag(ADecl->getLocation(), diag::err_omp_function_expected); 4450 return DeclGroupPtrTy(); 4451 } 4452 4453 // OpenMP [2.8.2, declare simd construct, Description] 4454 // The parameter of the simdlen clause must be a constant positive integer 4455 // expression. 4456 ExprResult SL; 4457 if (Simdlen) 4458 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 4459 // OpenMP [2.8.2, declare simd construct, Description] 4460 // The special this pointer can be used as if was one of the arguments to the 4461 // function in any of the linear, aligned, or uniform clauses. 4462 // The uniform clause declares one or more arguments to have an invariant 4463 // value for all concurrent invocations of the function in the execution of a 4464 // single SIMD loop. 4465 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 4466 const Expr *UniformedLinearThis = nullptr; 4467 for (const Expr *E : Uniforms) { 4468 E = E->IgnoreParenImpCasts(); 4469 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 4470 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 4471 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 4472 FD->getParamDecl(PVD->getFunctionScopeIndex()) 4473 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 4474 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 4475 continue; 4476 } 4477 if (isa<CXXThisExpr>(E)) { 4478 UniformedLinearThis = E; 4479 continue; 4480 } 4481 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 4482 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 4483 } 4484 // OpenMP [2.8.2, declare simd construct, Description] 4485 // The aligned clause declares that the object to which each list item points 4486 // is aligned to the number of bytes expressed in the optional parameter of 4487 // the aligned clause. 4488 // The special this pointer can be used as if was one of the arguments to the 4489 // function in any of the linear, aligned, or uniform clauses. 4490 // The type of list items appearing in the aligned clause must be array, 4491 // pointer, reference to array, or reference to pointer. 4492 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 4493 const Expr *AlignedThis = nullptr; 4494 for (const Expr *E : Aligneds) { 4495 E = E->IgnoreParenImpCasts(); 4496 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 4497 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 4498 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 4499 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 4500 FD->getParamDecl(PVD->getFunctionScopeIndex()) 4501 ->getCanonicalDecl() == CanonPVD) { 4502 // OpenMP [2.8.1, simd construct, Restrictions] 4503 // A list-item cannot appear in more than one aligned clause. 4504 if (AlignedArgs.count(CanonPVD) > 0) { 4505 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 4506 << 1 << E->getSourceRange(); 4507 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 4508 diag::note_omp_explicit_dsa) 4509 << getOpenMPClauseName(OMPC_aligned); 4510 continue; 4511 } 4512 AlignedArgs[CanonPVD] = E; 4513 QualType QTy = PVD->getType() 4514 .getNonReferenceType() 4515 .getUnqualifiedType() 4516 .getCanonicalType(); 4517 const Type *Ty = QTy.getTypePtrOrNull(); 4518 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 4519 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 4520 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 4521 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 4522 } 4523 continue; 4524 } 4525 } 4526 if (isa<CXXThisExpr>(E)) { 4527 if (AlignedThis) { 4528 Diag(E->getExprLoc(), diag::err_omp_aligned_twice) 4529 << 2 << E->getSourceRange(); 4530 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 4531 << getOpenMPClauseName(OMPC_aligned); 4532 } 4533 AlignedThis = E; 4534 continue; 4535 } 4536 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 4537 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 4538 } 4539 // The optional parameter of the aligned clause, alignment, must be a constant 4540 // positive integer expression. If no optional parameter is specified, 4541 // implementation-defined default alignments for SIMD instructions on the 4542 // target platforms are assumed. 4543 SmallVector<const Expr *, 4> NewAligns; 4544 for (Expr *E : Alignments) { 4545 ExprResult Align; 4546 if (E) 4547 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 4548 NewAligns.push_back(Align.get()); 4549 } 4550 // OpenMP [2.8.2, declare simd construct, Description] 4551 // The linear clause declares one or more list items to be private to a SIMD 4552 // lane and to have a linear relationship with respect to the iteration space 4553 // of a loop. 4554 // The special this pointer can be used as if was one of the arguments to the 4555 // function in any of the linear, aligned, or uniform clauses. 4556 // When a linear-step expression is specified in a linear clause it must be 4557 // either a constant integer expression or an integer-typed parameter that is 4558 // specified in a uniform clause on the directive. 4559 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 4560 const bool IsUniformedThis = UniformedLinearThis != nullptr; 4561 auto MI = LinModifiers.begin(); 4562 for (const Expr *E : Linears) { 4563 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 4564 ++MI; 4565 E = E->IgnoreParenImpCasts(); 4566 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 4567 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 4568 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 4569 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 4570 FD->getParamDecl(PVD->getFunctionScopeIndex()) 4571 ->getCanonicalDecl() == CanonPVD) { 4572 // OpenMP [2.15.3.7, linear Clause, Restrictions] 4573 // A list-item cannot appear in more than one linear clause. 4574 if (LinearArgs.count(CanonPVD) > 0) { 4575 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 4576 << getOpenMPClauseName(OMPC_linear) 4577 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 4578 Diag(LinearArgs[CanonPVD]->getExprLoc(), 4579 diag::note_omp_explicit_dsa) 4580 << getOpenMPClauseName(OMPC_linear); 4581 continue; 4582 } 4583 // Each argument can appear in at most one uniform or linear clause. 4584 if (UniformedArgs.count(CanonPVD) > 0) { 4585 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 4586 << getOpenMPClauseName(OMPC_linear) 4587 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 4588 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 4589 diag::note_omp_explicit_dsa) 4590 << getOpenMPClauseName(OMPC_uniform); 4591 continue; 4592 } 4593 LinearArgs[CanonPVD] = E; 4594 if (E->isValueDependent() || E->isTypeDependent() || 4595 E->isInstantiationDependent() || 4596 E->containsUnexpandedParameterPack()) 4597 continue; 4598 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 4599 PVD->getOriginalType()); 4600 continue; 4601 } 4602 } 4603 if (isa<CXXThisExpr>(E)) { 4604 if (UniformedLinearThis) { 4605 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 4606 << getOpenMPClauseName(OMPC_linear) 4607 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 4608 << E->getSourceRange(); 4609 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 4610 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 4611 : OMPC_linear); 4612 continue; 4613 } 4614 UniformedLinearThis = E; 4615 if (E->isValueDependent() || E->isTypeDependent() || 4616 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 4617 continue; 4618 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 4619 E->getType()); 4620 continue; 4621 } 4622 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 4623 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 4624 } 4625 Expr *Step = nullptr; 4626 Expr *NewStep = nullptr; 4627 SmallVector<Expr *, 4> NewSteps; 4628 for (Expr *E : Steps) { 4629 // Skip the same step expression, it was checked already. 4630 if (Step == E || !E) { 4631 NewSteps.push_back(E ? NewStep : nullptr); 4632 continue; 4633 } 4634 Step = E; 4635 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 4636 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 4637 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 4638 if (UniformedArgs.count(CanonPVD) == 0) { 4639 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 4640 << Step->getSourceRange(); 4641 } else if (E->isValueDependent() || E->isTypeDependent() || 4642 E->isInstantiationDependent() || 4643 E->containsUnexpandedParameterPack() || 4644 CanonPVD->getType()->hasIntegerRepresentation()) { 4645 NewSteps.push_back(Step); 4646 } else { 4647 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 4648 << Step->getSourceRange(); 4649 } 4650 continue; 4651 } 4652 NewStep = Step; 4653 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 4654 !Step->isInstantiationDependent() && 4655 !Step->containsUnexpandedParameterPack()) { 4656 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 4657 .get(); 4658 if (NewStep) 4659 NewStep = VerifyIntegerConstantExpression(NewStep).get(); 4660 } 4661 NewSteps.push_back(NewStep); 4662 } 4663 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 4664 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 4665 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 4666 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 4667 const_cast<Expr **>(Linears.data()), Linears.size(), 4668 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 4669 NewSteps.data(), NewSteps.size(), SR); 4670 ADecl->addAttr(NewAttr); 4671 return ConvertDeclToDeclGroup(ADecl); 4672 } 4673 4674 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 4675 Stmt *AStmt, 4676 SourceLocation StartLoc, 4677 SourceLocation EndLoc) { 4678 if (!AStmt) 4679 return StmtError(); 4680 4681 auto *CS = cast<CapturedStmt>(AStmt); 4682 // 1.2.2 OpenMP Language Terminology 4683 // Structured block - An executable statement with a single entry at the 4684 // top and a single exit at the bottom. 4685 // The point of exit cannot be a branch out of the structured block. 4686 // longjmp() and throw() must not violate the entry/exit criteria. 4687 CS->getCapturedDecl()->setNothrow(); 4688 4689 setFunctionHasBranchProtectedScope(); 4690 4691 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 4692 DSAStack->isCancelRegion()); 4693 } 4694 4695 namespace { 4696 /// Helper class for checking canonical form of the OpenMP loops and 4697 /// extracting iteration space of each loop in the loop nest, that will be used 4698 /// for IR generation. 4699 class OpenMPIterationSpaceChecker { 4700 /// Reference to Sema. 4701 Sema &SemaRef; 4702 /// Data-sharing stack. 4703 DSAStackTy &Stack; 4704 /// A location for diagnostics (when there is no some better location). 4705 SourceLocation DefaultLoc; 4706 /// A location for diagnostics (when increment is not compatible). 4707 SourceLocation ConditionLoc; 4708 /// A source location for referring to loop init later. 4709 SourceRange InitSrcRange; 4710 /// A source location for referring to condition later. 4711 SourceRange ConditionSrcRange; 4712 /// A source location for referring to increment later. 4713 SourceRange IncrementSrcRange; 4714 /// Loop variable. 4715 ValueDecl *LCDecl = nullptr; 4716 /// Reference to loop variable. 4717 Expr *LCRef = nullptr; 4718 /// Lower bound (initializer for the var). 4719 Expr *LB = nullptr; 4720 /// Upper bound. 4721 Expr *UB = nullptr; 4722 /// Loop step (increment). 4723 Expr *Step = nullptr; 4724 /// This flag is true when condition is one of: 4725 /// Var < UB 4726 /// Var <= UB 4727 /// UB > Var 4728 /// UB >= Var 4729 /// This will have no value when the condition is != 4730 llvm::Optional<bool> TestIsLessOp; 4731 /// This flag is true when condition is strict ( < or > ). 4732 bool TestIsStrictOp = false; 4733 /// This flag is true when step is subtracted on each iteration. 4734 bool SubtractStep = false; 4735 /// The outer loop counter this loop depends on (if any). 4736 const ValueDecl *DepDecl = nullptr; 4737 /// Contains number of loop (starts from 1) on which loop counter init 4738 /// expression of this loop depends on. 4739 Optional<unsigned> InitDependOnLC; 4740 /// Contains number of loop (starts from 1) on which loop counter condition 4741 /// expression of this loop depends on. 4742 Optional<unsigned> CondDependOnLC; 4743 /// Checks if the provide statement depends on the loop counter. 4744 Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer); 4745 4746 public: 4747 OpenMPIterationSpaceChecker(Sema &SemaRef, DSAStackTy &Stack, 4748 SourceLocation DefaultLoc) 4749 : SemaRef(SemaRef), Stack(Stack), DefaultLoc(DefaultLoc), 4750 ConditionLoc(DefaultLoc) {} 4751 /// Check init-expr for canonical loop form and save loop counter 4752 /// variable - #Var and its initialization value - #LB. 4753 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 4754 /// Check test-expr for canonical form, save upper-bound (#UB), flags 4755 /// for less/greater and for strict/non-strict comparison. 4756 bool checkAndSetCond(Expr *S); 4757 /// Check incr-expr for canonical loop form and return true if it 4758 /// does not conform, otherwise save loop step (#Step). 4759 bool checkAndSetInc(Expr *S); 4760 /// Return the loop counter variable. 4761 ValueDecl *getLoopDecl() const { return LCDecl; } 4762 /// Return the reference expression to loop counter variable. 4763 Expr *getLoopDeclRefExpr() const { return LCRef; } 4764 /// Source range of the loop init. 4765 SourceRange getInitSrcRange() const { return InitSrcRange; } 4766 /// Source range of the loop condition. 4767 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 4768 /// Source range of the loop increment. 4769 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 4770 /// True if the step should be subtracted. 4771 bool shouldSubtractStep() const { return SubtractStep; } 4772 /// True, if the compare operator is strict (<, > or !=). 4773 bool isStrictTestOp() const { return TestIsStrictOp; } 4774 /// Build the expression to calculate the number of iterations. 4775 Expr *buildNumIterations( 4776 Scope *S, const bool LimitedType, 4777 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 4778 /// Build the precondition expression for the loops. 4779 Expr * 4780 buildPreCond(Scope *S, Expr *Cond, 4781 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 4782 /// Build reference expression to the counter be used for codegen. 4783 DeclRefExpr * 4784 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 4785 DSAStackTy &DSA) const; 4786 /// Build reference expression to the private counter be used for 4787 /// codegen. 4788 Expr *buildPrivateCounterVar() const; 4789 /// Build initialization of the counter be used for codegen. 4790 Expr *buildCounterInit() const; 4791 /// Build step of the counter be used for codegen. 4792 Expr *buildCounterStep() const; 4793 /// Build loop data with counter value for depend clauses in ordered 4794 /// directives. 4795 Expr * 4796 buildOrderedLoopData(Scope *S, Expr *Counter, 4797 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 4798 SourceLocation Loc, Expr *Inc = nullptr, 4799 OverloadedOperatorKind OOK = OO_Amp); 4800 /// Return true if any expression is dependent. 4801 bool dependent() const; 4802 4803 private: 4804 /// Check the right-hand side of an assignment in the increment 4805 /// expression. 4806 bool checkAndSetIncRHS(Expr *RHS); 4807 /// Helper to set loop counter variable and its initializer. 4808 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB, 4809 bool EmitDiags); 4810 /// Helper to set upper bound. 4811 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp, 4812 SourceRange SR, SourceLocation SL); 4813 /// Helper to set loop increment. 4814 bool setStep(Expr *NewStep, bool Subtract); 4815 }; 4816 4817 bool OpenMPIterationSpaceChecker::dependent() const { 4818 if (!LCDecl) { 4819 assert(!LB && !UB && !Step); 4820 return false; 4821 } 4822 return LCDecl->getType()->isDependentType() || 4823 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 4824 (Step && Step->isValueDependent()); 4825 } 4826 4827 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 4828 Expr *NewLCRefExpr, 4829 Expr *NewLB, bool EmitDiags) { 4830 // State consistency checking to ensure correct usage. 4831 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 4832 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 4833 if (!NewLCDecl || !NewLB) 4834 return true; 4835 LCDecl = getCanonicalDecl(NewLCDecl); 4836 LCRef = NewLCRefExpr; 4837 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 4838 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 4839 if ((Ctor->isCopyOrMoveConstructor() || 4840 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 4841 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 4842 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 4843 LB = NewLB; 4844 if (EmitDiags) 4845 InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true); 4846 return false; 4847 } 4848 4849 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, 4850 llvm::Optional<bool> LessOp, 4851 bool StrictOp, SourceRange SR, 4852 SourceLocation SL) { 4853 // State consistency checking to ensure correct usage. 4854 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 4855 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 4856 if (!NewUB) 4857 return true; 4858 UB = NewUB; 4859 if (LessOp) 4860 TestIsLessOp = LessOp; 4861 TestIsStrictOp = StrictOp; 4862 ConditionSrcRange = SR; 4863 ConditionLoc = SL; 4864 CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false); 4865 return false; 4866 } 4867 4868 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 4869 // State consistency checking to ensure correct usage. 4870 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 4871 if (!NewStep) 4872 return true; 4873 if (!NewStep->isValueDependent()) { 4874 // Check that the step is integer expression. 4875 SourceLocation StepLoc = NewStep->getBeginLoc(); 4876 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 4877 StepLoc, getExprAsWritten(NewStep)); 4878 if (Val.isInvalid()) 4879 return true; 4880 NewStep = Val.get(); 4881 4882 // OpenMP [2.6, Canonical Loop Form, Restrictions] 4883 // If test-expr is of form var relational-op b and relational-op is < or 4884 // <= then incr-expr must cause var to increase on each iteration of the 4885 // loop. If test-expr is of form var relational-op b and relational-op is 4886 // > or >= then incr-expr must cause var to decrease on each iteration of 4887 // the loop. 4888 // If test-expr is of form b relational-op var and relational-op is < or 4889 // <= then incr-expr must cause var to decrease on each iteration of the 4890 // loop. If test-expr is of form b relational-op var and relational-op is 4891 // > or >= then incr-expr must cause var to increase on each iteration of 4892 // the loop. 4893 llvm::APSInt Result; 4894 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); 4895 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 4896 bool IsConstNeg = 4897 IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); 4898 bool IsConstPos = 4899 IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); 4900 bool IsConstZero = IsConstant && !Result.getBoolValue(); 4901 4902 // != with increment is treated as <; != with decrement is treated as > 4903 if (!TestIsLessOp.hasValue()) 4904 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract); 4905 if (UB && (IsConstZero || 4906 (TestIsLessOp.getValue() ? 4907 (IsConstNeg || (IsUnsigned && Subtract)) : 4908 (IsConstPos || (IsUnsigned && !Subtract))))) { 4909 SemaRef.Diag(NewStep->getExprLoc(), 4910 diag::err_omp_loop_incr_not_compatible) 4911 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange(); 4912 SemaRef.Diag(ConditionLoc, 4913 diag::note_omp_loop_cond_requres_compatible_incr) 4914 << TestIsLessOp.getValue() << ConditionSrcRange; 4915 return true; 4916 } 4917 if (TestIsLessOp.getValue() == Subtract) { 4918 NewStep = 4919 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 4920 .get(); 4921 Subtract = !Subtract; 4922 } 4923 } 4924 4925 Step = NewStep; 4926 SubtractStep = Subtract; 4927 return false; 4928 } 4929 4930 namespace { 4931 /// Checker for the non-rectangular loops. Checks if the initializer or 4932 /// condition expression references loop counter variable. 4933 class LoopCounterRefChecker final 4934 : public ConstStmtVisitor<LoopCounterRefChecker, bool> { 4935 Sema &SemaRef; 4936 DSAStackTy &Stack; 4937 const ValueDecl *CurLCDecl = nullptr; 4938 const ValueDecl *DepDecl = nullptr; 4939 const ValueDecl *PrevDepDecl = nullptr; 4940 bool IsInitializer = true; 4941 unsigned BaseLoopId = 0; 4942 bool checkDecl(const Expr *E, const ValueDecl *VD) { 4943 if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) { 4944 SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter) 4945 << (IsInitializer ? 0 : 1); 4946 return false; 4947 } 4948 const auto &&Data = Stack.isLoopControlVariable(VD); 4949 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions. 4950 // The type of the loop iterator on which we depend may not have a random 4951 // access iterator type. 4952 if (Data.first && VD->getType()->isRecordType()) { 4953 SmallString<128> Name; 4954 llvm::raw_svector_ostream OS(Name); 4955 VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 4956 /*Qualified=*/true); 4957 SemaRef.Diag(E->getExprLoc(), 4958 diag::err_omp_wrong_dependency_iterator_type) 4959 << OS.str(); 4960 SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD; 4961 return false; 4962 } 4963 if (Data.first && 4964 (DepDecl || (PrevDepDecl && 4965 getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) { 4966 if (!DepDecl && PrevDepDecl) 4967 DepDecl = PrevDepDecl; 4968 SmallString<128> Name; 4969 llvm::raw_svector_ostream OS(Name); 4970 DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 4971 /*Qualified=*/true); 4972 SemaRef.Diag(E->getExprLoc(), 4973 diag::err_omp_invariant_or_linear_dependency) 4974 << OS.str(); 4975 return false; 4976 } 4977 if (Data.first) { 4978 DepDecl = VD; 4979 BaseLoopId = Data.first; 4980 } 4981 return Data.first; 4982 } 4983 4984 public: 4985 bool VisitDeclRefExpr(const DeclRefExpr *E) { 4986 const ValueDecl *VD = E->getDecl(); 4987 if (isa<VarDecl>(VD)) 4988 return checkDecl(E, VD); 4989 return false; 4990 } 4991 bool VisitMemberExpr(const MemberExpr *E) { 4992 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 4993 const ValueDecl *VD = E->getMemberDecl(); 4994 return checkDecl(E, VD); 4995 } 4996 return false; 4997 } 4998 bool VisitStmt(const Stmt *S) { 4999 bool Res = true; 5000 for (const Stmt *Child : S->children()) 5001 Res = Child && Visit(Child) && Res; 5002 return Res; 5003 } 5004 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack, 5005 const ValueDecl *CurLCDecl, bool IsInitializer, 5006 const ValueDecl *PrevDepDecl = nullptr) 5007 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl), 5008 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer) {} 5009 unsigned getBaseLoopId() const { 5010 assert(CurLCDecl && "Expected loop dependency."); 5011 return BaseLoopId; 5012 } 5013 const ValueDecl *getDepDecl() const { 5014 assert(CurLCDecl && "Expected loop dependency."); 5015 return DepDecl; 5016 } 5017 }; 5018 } // namespace 5019 5020 Optional<unsigned> 5021 OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S, 5022 bool IsInitializer) { 5023 // Check for the non-rectangular loops. 5024 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer, 5025 DepDecl); 5026 if (LoopStmtChecker.Visit(S)) { 5027 DepDecl = LoopStmtChecker.getDepDecl(); 5028 return LoopStmtChecker.getBaseLoopId(); 5029 } 5030 return llvm::None; 5031 } 5032 5033 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 5034 // Check init-expr for canonical loop form and save loop counter 5035 // variable - #Var and its initialization value - #LB. 5036 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 5037 // var = lb 5038 // integer-type var = lb 5039 // random-access-iterator-type var = lb 5040 // pointer-type var = lb 5041 // 5042 if (!S) { 5043 if (EmitDiags) { 5044 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 5045 } 5046 return true; 5047 } 5048 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 5049 if (!ExprTemp->cleanupsHaveSideEffects()) 5050 S = ExprTemp->getSubExpr(); 5051 5052 InitSrcRange = S->getSourceRange(); 5053 if (Expr *E = dyn_cast<Expr>(S)) 5054 S = E->IgnoreParens(); 5055 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 5056 if (BO->getOpcode() == BO_Assign) { 5057 Expr *LHS = BO->getLHS()->IgnoreParens(); 5058 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 5059 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 5060 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 5061 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 5062 EmitDiags); 5063 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags); 5064 } 5065 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 5066 if (ME->isArrow() && 5067 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 5068 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 5069 EmitDiags); 5070 } 5071 } 5072 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 5073 if (DS->isSingleDecl()) { 5074 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 5075 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 5076 // Accept non-canonical init form here but emit ext. warning. 5077 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 5078 SemaRef.Diag(S->getBeginLoc(), 5079 diag::ext_omp_loop_not_canonical_init) 5080 << S->getSourceRange(); 5081 return setLCDeclAndLB( 5082 Var, 5083 buildDeclRefExpr(SemaRef, Var, 5084 Var->getType().getNonReferenceType(), 5085 DS->getBeginLoc()), 5086 Var->getInit(), EmitDiags); 5087 } 5088 } 5089 } 5090 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 5091 if (CE->getOperator() == OO_Equal) { 5092 Expr *LHS = CE->getArg(0); 5093 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 5094 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 5095 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 5096 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 5097 EmitDiags); 5098 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags); 5099 } 5100 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 5101 if (ME->isArrow() && 5102 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 5103 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 5104 EmitDiags); 5105 } 5106 } 5107 } 5108 5109 if (dependent() || SemaRef.CurContext->isDependentContext()) 5110 return false; 5111 if (EmitDiags) { 5112 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init) 5113 << S->getSourceRange(); 5114 } 5115 return true; 5116 } 5117 5118 /// Ignore parenthesizes, implicit casts, copy constructor and return the 5119 /// variable (which may be the loop variable) if possible. 5120 static const ValueDecl *getInitLCDecl(const Expr *E) { 5121 if (!E) 5122 return nullptr; 5123 E = getExprAsWritten(E); 5124 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 5125 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 5126 if ((Ctor->isCopyOrMoveConstructor() || 5127 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 5128 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 5129 E = CE->getArg(0)->IgnoreParenImpCasts(); 5130 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 5131 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 5132 return getCanonicalDecl(VD); 5133 } 5134 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 5135 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 5136 return getCanonicalDecl(ME->getMemberDecl()); 5137 return nullptr; 5138 } 5139 5140 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 5141 // Check test-expr for canonical form, save upper-bound UB, flags for 5142 // less/greater and for strict/non-strict comparison. 5143 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 5144 // var relational-op b 5145 // b relational-op var 5146 // 5147 if (!S) { 5148 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl; 5149 return true; 5150 } 5151 S = getExprAsWritten(S); 5152 SourceLocation CondLoc = S->getBeginLoc(); 5153 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 5154 if (BO->isRelationalOp()) { 5155 if (getInitLCDecl(BO->getLHS()) == LCDecl) 5156 return setUB(BO->getRHS(), 5157 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 5158 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 5159 BO->getSourceRange(), BO->getOperatorLoc()); 5160 if (getInitLCDecl(BO->getRHS()) == LCDecl) 5161 return setUB(BO->getLHS(), 5162 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 5163 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 5164 BO->getSourceRange(), BO->getOperatorLoc()); 5165 } else if (BO->getOpcode() == BO_NE) 5166 return setUB(getInitLCDecl(BO->getLHS()) == LCDecl ? 5167 BO->getRHS() : BO->getLHS(), 5168 /*LessOp=*/llvm::None, 5169 /*StrictOp=*/true, 5170 BO->getSourceRange(), BO->getOperatorLoc()); 5171 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 5172 if (CE->getNumArgs() == 2) { 5173 auto Op = CE->getOperator(); 5174 switch (Op) { 5175 case OO_Greater: 5176 case OO_GreaterEqual: 5177 case OO_Less: 5178 case OO_LessEqual: 5179 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 5180 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 5181 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 5182 CE->getOperatorLoc()); 5183 if (getInitLCDecl(CE->getArg(1)) == LCDecl) 5184 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 5185 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 5186 CE->getOperatorLoc()); 5187 break; 5188 case OO_ExclaimEqual: 5189 return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ? 5190 CE->getArg(1) : CE->getArg(0), 5191 /*LessOp=*/llvm::None, 5192 /*StrictOp=*/true, 5193 CE->getSourceRange(), 5194 CE->getOperatorLoc()); 5195 break; 5196 default: 5197 break; 5198 } 5199 } 5200 } 5201 if (dependent() || SemaRef.CurContext->isDependentContext()) 5202 return false; 5203 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 5204 << S->getSourceRange() << LCDecl; 5205 return true; 5206 } 5207 5208 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 5209 // RHS of canonical loop form increment can be: 5210 // var + incr 5211 // incr + var 5212 // var - incr 5213 // 5214 RHS = RHS->IgnoreParenImpCasts(); 5215 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 5216 if (BO->isAdditiveOp()) { 5217 bool IsAdd = BO->getOpcode() == BO_Add; 5218 if (getInitLCDecl(BO->getLHS()) == LCDecl) 5219 return setStep(BO->getRHS(), !IsAdd); 5220 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 5221 return setStep(BO->getLHS(), /*Subtract=*/false); 5222 } 5223 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 5224 bool IsAdd = CE->getOperator() == OO_Plus; 5225 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 5226 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 5227 return setStep(CE->getArg(1), !IsAdd); 5228 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 5229 return setStep(CE->getArg(0), /*Subtract=*/false); 5230 } 5231 } 5232 if (dependent() || SemaRef.CurContext->isDependentContext()) 5233 return false; 5234 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 5235 << RHS->getSourceRange() << LCDecl; 5236 return true; 5237 } 5238 5239 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 5240 // Check incr-expr for canonical loop form and return true if it 5241 // does not conform. 5242 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 5243 // ++var 5244 // var++ 5245 // --var 5246 // var-- 5247 // var += incr 5248 // var -= incr 5249 // var = var + incr 5250 // var = incr + var 5251 // var = var - incr 5252 // 5253 if (!S) { 5254 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 5255 return true; 5256 } 5257 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 5258 if (!ExprTemp->cleanupsHaveSideEffects()) 5259 S = ExprTemp->getSubExpr(); 5260 5261 IncrementSrcRange = S->getSourceRange(); 5262 S = S->IgnoreParens(); 5263 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 5264 if (UO->isIncrementDecrementOp() && 5265 getInitLCDecl(UO->getSubExpr()) == LCDecl) 5266 return setStep(SemaRef 5267 .ActOnIntegerConstant(UO->getBeginLoc(), 5268 (UO->isDecrementOp() ? -1 : 1)) 5269 .get(), 5270 /*Subtract=*/false); 5271 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 5272 switch (BO->getOpcode()) { 5273 case BO_AddAssign: 5274 case BO_SubAssign: 5275 if (getInitLCDecl(BO->getLHS()) == LCDecl) 5276 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 5277 break; 5278 case BO_Assign: 5279 if (getInitLCDecl(BO->getLHS()) == LCDecl) 5280 return checkAndSetIncRHS(BO->getRHS()); 5281 break; 5282 default: 5283 break; 5284 } 5285 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 5286 switch (CE->getOperator()) { 5287 case OO_PlusPlus: 5288 case OO_MinusMinus: 5289 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 5290 return setStep(SemaRef 5291 .ActOnIntegerConstant( 5292 CE->getBeginLoc(), 5293 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 5294 .get(), 5295 /*Subtract=*/false); 5296 break; 5297 case OO_PlusEqual: 5298 case OO_MinusEqual: 5299 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 5300 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 5301 break; 5302 case OO_Equal: 5303 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 5304 return checkAndSetIncRHS(CE->getArg(1)); 5305 break; 5306 default: 5307 break; 5308 } 5309 } 5310 if (dependent() || SemaRef.CurContext->isDependentContext()) 5311 return false; 5312 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 5313 << S->getSourceRange() << LCDecl; 5314 return true; 5315 } 5316 5317 static ExprResult 5318 tryBuildCapture(Sema &SemaRef, Expr *Capture, 5319 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 5320 if (SemaRef.CurContext->isDependentContext()) 5321 return ExprResult(Capture); 5322 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 5323 return SemaRef.PerformImplicitConversion( 5324 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 5325 /*AllowExplicit=*/true); 5326 auto I = Captures.find(Capture); 5327 if (I != Captures.end()) 5328 return buildCapture(SemaRef, Capture, I->second); 5329 DeclRefExpr *Ref = nullptr; 5330 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 5331 Captures[Capture] = Ref; 5332 return Res; 5333 } 5334 5335 /// Build the expression to calculate the number of iterations. 5336 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 5337 Scope *S, const bool LimitedType, 5338 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 5339 ExprResult Diff; 5340 QualType VarType = LCDecl->getType().getNonReferenceType(); 5341 if (VarType->isIntegerType() || VarType->isPointerType() || 5342 SemaRef.getLangOpts().CPlusPlus) { 5343 // Upper - Lower 5344 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB; 5345 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB; 5346 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 5347 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 5348 if (!Upper || !Lower) 5349 return nullptr; 5350 5351 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 5352 5353 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 5354 // BuildBinOp already emitted error, this one is to point user to upper 5355 // and lower bound, and to tell what is passed to 'operator-'. 5356 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 5357 << Upper->getSourceRange() << Lower->getSourceRange(); 5358 return nullptr; 5359 } 5360 } 5361 5362 if (!Diff.isUsable()) 5363 return nullptr; 5364 5365 // Upper - Lower [- 1] 5366 if (TestIsStrictOp) 5367 Diff = SemaRef.BuildBinOp( 5368 S, DefaultLoc, BO_Sub, Diff.get(), 5369 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 5370 if (!Diff.isUsable()) 5371 return nullptr; 5372 5373 // Upper - Lower [- 1] + Step 5374 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 5375 if (!NewStep.isUsable()) 5376 return nullptr; 5377 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 5378 if (!Diff.isUsable()) 5379 return nullptr; 5380 5381 // Parentheses (for dumping/debugging purposes only). 5382 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 5383 if (!Diff.isUsable()) 5384 return nullptr; 5385 5386 // (Upper - Lower [- 1] + Step) / Step 5387 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 5388 if (!Diff.isUsable()) 5389 return nullptr; 5390 5391 // OpenMP runtime requires 32-bit or 64-bit loop variables. 5392 QualType Type = Diff.get()->getType(); 5393 ASTContext &C = SemaRef.Context; 5394 bool UseVarType = VarType->hasIntegerRepresentation() && 5395 C.getTypeSize(Type) > C.getTypeSize(VarType); 5396 if (!Type->isIntegerType() || UseVarType) { 5397 unsigned NewSize = 5398 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 5399 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 5400 : Type->hasSignedIntegerRepresentation(); 5401 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 5402 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 5403 Diff = SemaRef.PerformImplicitConversion( 5404 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 5405 if (!Diff.isUsable()) 5406 return nullptr; 5407 } 5408 } 5409 if (LimitedType) { 5410 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 5411 if (NewSize != C.getTypeSize(Type)) { 5412 if (NewSize < C.getTypeSize(Type)) { 5413 assert(NewSize == 64 && "incorrect loop var size"); 5414 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 5415 << InitSrcRange << ConditionSrcRange; 5416 } 5417 QualType NewType = C.getIntTypeForBitwidth( 5418 NewSize, Type->hasSignedIntegerRepresentation() || 5419 C.getTypeSize(Type) < NewSize); 5420 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 5421 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 5422 Sema::AA_Converting, true); 5423 if (!Diff.isUsable()) 5424 return nullptr; 5425 } 5426 } 5427 } 5428 5429 return Diff.get(); 5430 } 5431 5432 Expr *OpenMPIterationSpaceChecker::buildPreCond( 5433 Scope *S, Expr *Cond, 5434 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 5435 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 5436 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 5437 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 5438 5439 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 5440 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 5441 if (!NewLB.isUsable() || !NewUB.isUsable()) 5442 return nullptr; 5443 5444 ExprResult CondExpr = 5445 SemaRef.BuildBinOp(S, DefaultLoc, 5446 TestIsLessOp.getValue() ? 5447 (TestIsStrictOp ? BO_LT : BO_LE) : 5448 (TestIsStrictOp ? BO_GT : BO_GE), 5449 NewLB.get(), NewUB.get()); 5450 if (CondExpr.isUsable()) { 5451 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 5452 SemaRef.Context.BoolTy)) 5453 CondExpr = SemaRef.PerformImplicitConversion( 5454 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 5455 /*AllowExplicit=*/true); 5456 } 5457 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 5458 // Otherwise use original loop condition and evaluate it in runtime. 5459 return CondExpr.isUsable() ? CondExpr.get() : Cond; 5460 } 5461 5462 /// Build reference expression to the counter be used for codegen. 5463 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 5464 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 5465 DSAStackTy &DSA) const { 5466 auto *VD = dyn_cast<VarDecl>(LCDecl); 5467 if (!VD) { 5468 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 5469 DeclRefExpr *Ref = buildDeclRefExpr( 5470 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 5471 const DSAStackTy::DSAVarData Data = 5472 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 5473 // If the loop control decl is explicitly marked as private, do not mark it 5474 // as captured again. 5475 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 5476 Captures.insert(std::make_pair(LCRef, Ref)); 5477 return Ref; 5478 } 5479 return cast<DeclRefExpr>(LCRef); 5480 } 5481 5482 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 5483 if (LCDecl && !LCDecl->isInvalidDecl()) { 5484 QualType Type = LCDecl->getType().getNonReferenceType(); 5485 VarDecl *PrivateVar = buildVarDecl( 5486 SemaRef, DefaultLoc, Type, LCDecl->getName(), 5487 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 5488 isa<VarDecl>(LCDecl) 5489 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 5490 : nullptr); 5491 if (PrivateVar->isInvalidDecl()) 5492 return nullptr; 5493 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 5494 } 5495 return nullptr; 5496 } 5497 5498 /// Build initialization of the counter to be used for codegen. 5499 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 5500 5501 /// Build step of the counter be used for codegen. 5502 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 5503 5504 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData( 5505 Scope *S, Expr *Counter, 5506 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc, 5507 Expr *Inc, OverloadedOperatorKind OOK) { 5508 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get(); 5509 if (!Cnt) 5510 return nullptr; 5511 if (Inc) { 5512 assert((OOK == OO_Plus || OOK == OO_Minus) && 5513 "Expected only + or - operations for depend clauses."); 5514 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub; 5515 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get(); 5516 if (!Cnt) 5517 return nullptr; 5518 } 5519 ExprResult Diff; 5520 QualType VarType = LCDecl->getType().getNonReferenceType(); 5521 if (VarType->isIntegerType() || VarType->isPointerType() || 5522 SemaRef.getLangOpts().CPlusPlus) { 5523 // Upper - Lower 5524 Expr *Upper = TestIsLessOp.getValue() 5525 ? Cnt 5526 : tryBuildCapture(SemaRef, UB, Captures).get(); 5527 Expr *Lower = TestIsLessOp.getValue() 5528 ? tryBuildCapture(SemaRef, LB, Captures).get() 5529 : Cnt; 5530 if (!Upper || !Lower) 5531 return nullptr; 5532 5533 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 5534 5535 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) { 5536 // BuildBinOp already emitted error, this one is to point user to upper 5537 // and lower bound, and to tell what is passed to 'operator-'. 5538 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 5539 << Upper->getSourceRange() << Lower->getSourceRange(); 5540 return nullptr; 5541 } 5542 } 5543 5544 if (!Diff.isUsable()) 5545 return nullptr; 5546 5547 // Parentheses (for dumping/debugging purposes only). 5548 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 5549 if (!Diff.isUsable()) 5550 return nullptr; 5551 5552 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 5553 if (!NewStep.isUsable()) 5554 return nullptr; 5555 // (Upper - Lower) / Step 5556 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 5557 if (!Diff.isUsable()) 5558 return nullptr; 5559 5560 return Diff.get(); 5561 } 5562 5563 /// Iteration space of a single for loop. 5564 struct LoopIterationSpace final { 5565 /// True if the condition operator is the strict compare operator (<, > or 5566 /// !=). 5567 bool IsStrictCompare = false; 5568 /// Condition of the loop. 5569 Expr *PreCond = nullptr; 5570 /// This expression calculates the number of iterations in the loop. 5571 /// It is always possible to calculate it before starting the loop. 5572 Expr *NumIterations = nullptr; 5573 /// The loop counter variable. 5574 Expr *CounterVar = nullptr; 5575 /// Private loop counter variable. 5576 Expr *PrivateCounterVar = nullptr; 5577 /// This is initializer for the initial value of #CounterVar. 5578 Expr *CounterInit = nullptr; 5579 /// This is step for the #CounterVar used to generate its update: 5580 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 5581 Expr *CounterStep = nullptr; 5582 /// Should step be subtracted? 5583 bool Subtract = false; 5584 /// Source range of the loop init. 5585 SourceRange InitSrcRange; 5586 /// Source range of the loop condition. 5587 SourceRange CondSrcRange; 5588 /// Source range of the loop increment. 5589 SourceRange IncSrcRange; 5590 }; 5591 5592 } // namespace 5593 5594 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 5595 assert(getLangOpts().OpenMP && "OpenMP is not active."); 5596 assert(Init && "Expected loop in canonical form."); 5597 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 5598 if (AssociatedLoops > 0 && 5599 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 5600 DSAStack->loopStart(); 5601 OpenMPIterationSpaceChecker ISC(*this, *DSAStack, ForLoc); 5602 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 5603 if (ValueDecl *D = ISC.getLoopDecl()) { 5604 auto *VD = dyn_cast<VarDecl>(D); 5605 if (!VD) { 5606 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 5607 VD = Private; 5608 } else { 5609 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 5610 /*WithInit=*/false); 5611 VD = cast<VarDecl>(Ref->getDecl()); 5612 } 5613 } 5614 DSAStack->addLoopControlVariable(D, VD); 5615 const Decl *LD = DSAStack->getPossiblyLoopCunter(); 5616 if (LD != D->getCanonicalDecl()) { 5617 DSAStack->resetPossibleLoopCounter(); 5618 if (auto *Var = dyn_cast_or_null<VarDecl>(LD)) 5619 MarkDeclarationsReferencedInExpr( 5620 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var), 5621 Var->getType().getNonLValueExprType(Context), 5622 ForLoc, /*RefersToCapture=*/true)); 5623 } 5624 } 5625 } 5626 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 5627 } 5628 } 5629 5630 /// Called on a for stmt to check and extract its iteration space 5631 /// for further processing (such as collapsing). 5632 static bool checkOpenMPIterationSpace( 5633 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 5634 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 5635 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr, 5636 Expr *OrderedLoopCountExpr, 5637 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 5638 LoopIterationSpace &ResultIterSpace, 5639 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 5640 // OpenMP [2.6, Canonical Loop Form] 5641 // for (init-expr; test-expr; incr-expr) structured-block 5642 auto *For = dyn_cast_or_null<ForStmt>(S); 5643 if (!For) { 5644 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for) 5645 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 5646 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount 5647 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 5648 if (TotalNestedLoopCount > 1) { 5649 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 5650 SemaRef.Diag(DSA.getConstructLoc(), 5651 diag::note_omp_collapse_ordered_expr) 5652 << 2 << CollapseLoopCountExpr->getSourceRange() 5653 << OrderedLoopCountExpr->getSourceRange(); 5654 else if (CollapseLoopCountExpr) 5655 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 5656 diag::note_omp_collapse_ordered_expr) 5657 << 0 << CollapseLoopCountExpr->getSourceRange(); 5658 else 5659 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 5660 diag::note_omp_collapse_ordered_expr) 5661 << 1 << OrderedLoopCountExpr->getSourceRange(); 5662 } 5663 return true; 5664 } 5665 assert(For->getBody()); 5666 5667 OpenMPIterationSpaceChecker ISC(SemaRef, DSA, For->getForLoc()); 5668 5669 // Check init. 5670 Stmt *Init = For->getInit(); 5671 if (ISC.checkAndSetInit(Init)) 5672 return true; 5673 5674 bool HasErrors = false; 5675 5676 // Check loop variable's type. 5677 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 5678 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 5679 5680 // OpenMP [2.6, Canonical Loop Form] 5681 // Var is one of the following: 5682 // A variable of signed or unsigned integer type. 5683 // For C++, a variable of a random access iterator type. 5684 // For C, a variable of a pointer type. 5685 QualType VarType = LCDecl->getType().getNonReferenceType(); 5686 if (!VarType->isDependentType() && !VarType->isIntegerType() && 5687 !VarType->isPointerType() && 5688 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 5689 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type) 5690 << SemaRef.getLangOpts().CPlusPlus; 5691 HasErrors = true; 5692 } 5693 5694 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 5695 // a Construct 5696 // The loop iteration variable(s) in the associated for-loop(s) of a for or 5697 // parallel for construct is (are) private. 5698 // The loop iteration variable in the associated for-loop of a simd 5699 // construct with just one associated for-loop is linear with a 5700 // constant-linear-step that is the increment of the associated for-loop. 5701 // Exclude loop var from the list of variables with implicitly defined data 5702 // sharing attributes. 5703 VarsWithImplicitDSA.erase(LCDecl); 5704 5705 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 5706 // in a Construct, C/C++]. 5707 // The loop iteration variable in the associated for-loop of a simd 5708 // construct with just one associated for-loop may be listed in a linear 5709 // clause with a constant-linear-step that is the increment of the 5710 // associated for-loop. 5711 // The loop iteration variable(s) in the associated for-loop(s) of a for or 5712 // parallel for construct may be listed in a private or lastprivate clause. 5713 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false); 5714 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is 5715 // declared in the loop and it is predetermined as a private. 5716 OpenMPClauseKind PredeterminedCKind = 5717 isOpenMPSimdDirective(DKind) 5718 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate) 5719 : OMPC_private; 5720 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 5721 DVar.CKind != PredeterminedCKind && DVar.RefExpr && 5722 (SemaRef.getLangOpts().OpenMP <= 45 || 5723 (DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_private))) || 5724 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 5725 isOpenMPDistributeDirective(DKind)) && 5726 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 5727 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 5728 (DVar.CKind != OMPC_private || DVar.RefExpr)) { 5729 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa) 5730 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind) 5731 << getOpenMPClauseName(PredeterminedCKind); 5732 if (DVar.RefExpr == nullptr) 5733 DVar.CKind = PredeterminedCKind; 5734 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true); 5735 HasErrors = true; 5736 } else if (LoopDeclRefExpr != nullptr) { 5737 // Make the loop iteration variable private (for worksharing constructs), 5738 // linear (for simd directives with the only one associated loop) or 5739 // lastprivate (for simd directives with several collapsed or ordered 5740 // loops). 5741 if (DVar.CKind == OMPC_unknown) 5742 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind); 5743 } 5744 5745 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 5746 5747 // Check test-expr. 5748 HasErrors |= ISC.checkAndSetCond(For->getCond()); 5749 5750 // Check incr-expr. 5751 HasErrors |= ISC.checkAndSetInc(For->getInc()); 5752 } 5753 5754 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 5755 return HasErrors; 5756 5757 // Build the loop's iteration space representation. 5758 ResultIterSpace.PreCond = 5759 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures); 5760 ResultIterSpace.NumIterations = ISC.buildNumIterations( 5761 DSA.getCurScope(), 5762 (isOpenMPWorksharingDirective(DKind) || 5763 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)), 5764 Captures); 5765 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA); 5766 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar(); 5767 ResultIterSpace.CounterInit = ISC.buildCounterInit(); 5768 ResultIterSpace.CounterStep = ISC.buildCounterStep(); 5769 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange(); 5770 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange(); 5771 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange(); 5772 ResultIterSpace.Subtract = ISC.shouldSubtractStep(); 5773 ResultIterSpace.IsStrictCompare = ISC.isStrictTestOp(); 5774 5775 HasErrors |= (ResultIterSpace.PreCond == nullptr || 5776 ResultIterSpace.NumIterations == nullptr || 5777 ResultIterSpace.CounterVar == nullptr || 5778 ResultIterSpace.PrivateCounterVar == nullptr || 5779 ResultIterSpace.CounterInit == nullptr || 5780 ResultIterSpace.CounterStep == nullptr); 5781 if (!HasErrors && DSA.isOrderedRegion()) { 5782 if (DSA.getOrderedRegionParam().second->getNumForLoops()) { 5783 if (CurrentNestedLoopCount < 5784 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) { 5785 DSA.getOrderedRegionParam().second->setLoopNumIterations( 5786 CurrentNestedLoopCount, ResultIterSpace.NumIterations); 5787 DSA.getOrderedRegionParam().second->setLoopCounter( 5788 CurrentNestedLoopCount, ResultIterSpace.CounterVar); 5789 } 5790 } 5791 for (auto &Pair : DSA.getDoacrossDependClauses()) { 5792 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) { 5793 // Erroneous case - clause has some problems. 5794 continue; 5795 } 5796 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink && 5797 Pair.second.size() <= CurrentNestedLoopCount) { 5798 // Erroneous case - clause has some problems. 5799 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr); 5800 continue; 5801 } 5802 Expr *CntValue; 5803 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 5804 CntValue = ISC.buildOrderedLoopData( 5805 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures, 5806 Pair.first->getDependencyLoc()); 5807 else 5808 CntValue = ISC.buildOrderedLoopData( 5809 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures, 5810 Pair.first->getDependencyLoc(), 5811 Pair.second[CurrentNestedLoopCount].first, 5812 Pair.second[CurrentNestedLoopCount].second); 5813 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue); 5814 } 5815 } 5816 5817 return HasErrors; 5818 } 5819 5820 /// Build 'VarRef = Start. 5821 static ExprResult 5822 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 5823 ExprResult Start, 5824 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 5825 // Build 'VarRef = Start. 5826 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures); 5827 if (!NewStart.isUsable()) 5828 return ExprError(); 5829 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 5830 VarRef.get()->getType())) { 5831 NewStart = SemaRef.PerformImplicitConversion( 5832 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 5833 /*AllowExplicit=*/true); 5834 if (!NewStart.isUsable()) 5835 return ExprError(); 5836 } 5837 5838 ExprResult Init = 5839 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 5840 return Init; 5841 } 5842 5843 /// Build 'VarRef = Start + Iter * Step'. 5844 static ExprResult buildCounterUpdate( 5845 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 5846 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 5847 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 5848 // Add parentheses (for debugging purposes only). 5849 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 5850 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 5851 !Step.isUsable()) 5852 return ExprError(); 5853 5854 ExprResult NewStep = Step; 5855 if (Captures) 5856 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 5857 if (NewStep.isInvalid()) 5858 return ExprError(); 5859 ExprResult Update = 5860 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 5861 if (!Update.isUsable()) 5862 return ExprError(); 5863 5864 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 5865 // 'VarRef = Start (+|-) Iter * Step'. 5866 ExprResult NewStart = Start; 5867 if (Captures) 5868 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 5869 if (NewStart.isInvalid()) 5870 return ExprError(); 5871 5872 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 5873 ExprResult SavedUpdate = Update; 5874 ExprResult UpdateVal; 5875 if (VarRef.get()->getType()->isOverloadableType() || 5876 NewStart.get()->getType()->isOverloadableType() || 5877 Update.get()->getType()->isOverloadableType()) { 5878 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics(); 5879 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 5880 Update = 5881 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 5882 if (Update.isUsable()) { 5883 UpdateVal = 5884 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 5885 VarRef.get(), SavedUpdate.get()); 5886 if (UpdateVal.isUsable()) { 5887 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 5888 UpdateVal.get()); 5889 } 5890 } 5891 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress); 5892 } 5893 5894 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 5895 if (!Update.isUsable() || !UpdateVal.isUsable()) { 5896 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 5897 NewStart.get(), SavedUpdate.get()); 5898 if (!Update.isUsable()) 5899 return ExprError(); 5900 5901 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 5902 VarRef.get()->getType())) { 5903 Update = SemaRef.PerformImplicitConversion( 5904 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 5905 if (!Update.isUsable()) 5906 return ExprError(); 5907 } 5908 5909 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 5910 } 5911 return Update; 5912 } 5913 5914 /// Convert integer expression \a E to make it have at least \a Bits 5915 /// bits. 5916 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 5917 if (E == nullptr) 5918 return ExprError(); 5919 ASTContext &C = SemaRef.Context; 5920 QualType OldType = E->getType(); 5921 unsigned HasBits = C.getTypeSize(OldType); 5922 if (HasBits >= Bits) 5923 return ExprResult(E); 5924 // OK to convert to signed, because new type has more bits than old. 5925 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 5926 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 5927 true); 5928 } 5929 5930 /// Check if the given expression \a E is a constant integer that fits 5931 /// into \a Bits bits. 5932 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 5933 if (E == nullptr) 5934 return false; 5935 llvm::APSInt Result; 5936 if (E->isIntegerConstantExpr(Result, SemaRef.Context)) 5937 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); 5938 return false; 5939 } 5940 5941 /// Build preinits statement for the given declarations. 5942 static Stmt *buildPreInits(ASTContext &Context, 5943 MutableArrayRef<Decl *> PreInits) { 5944 if (!PreInits.empty()) { 5945 return new (Context) DeclStmt( 5946 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 5947 SourceLocation(), SourceLocation()); 5948 } 5949 return nullptr; 5950 } 5951 5952 /// Build preinits statement for the given declarations. 5953 static Stmt * 5954 buildPreInits(ASTContext &Context, 5955 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 5956 if (!Captures.empty()) { 5957 SmallVector<Decl *, 16> PreInits; 5958 for (const auto &Pair : Captures) 5959 PreInits.push_back(Pair.second->getDecl()); 5960 return buildPreInits(Context, PreInits); 5961 } 5962 return nullptr; 5963 } 5964 5965 /// Build postupdate expression for the given list of postupdates expressions. 5966 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 5967 Expr *PostUpdate = nullptr; 5968 if (!PostUpdates.empty()) { 5969 for (Expr *E : PostUpdates) { 5970 Expr *ConvE = S.BuildCStyleCastExpr( 5971 E->getExprLoc(), 5972 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 5973 E->getExprLoc(), E) 5974 .get(); 5975 PostUpdate = PostUpdate 5976 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 5977 PostUpdate, ConvE) 5978 .get() 5979 : ConvE; 5980 } 5981 } 5982 return PostUpdate; 5983 } 5984 5985 /// Called on a for stmt to check itself and nested loops (if any). 5986 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 5987 /// number of collapsed loops otherwise. 5988 static unsigned 5989 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 5990 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 5991 DSAStackTy &DSA, 5992 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 5993 OMPLoopDirective::HelperExprs &Built) { 5994 unsigned NestedLoopCount = 1; 5995 if (CollapseLoopCountExpr) { 5996 // Found 'collapse' clause - calculate collapse number. 5997 Expr::EvalResult Result; 5998 if (!CollapseLoopCountExpr->isValueDependent() && 5999 CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 6000 NestedLoopCount = Result.Val.getInt().getLimitedValue(); 6001 } else { 6002 Built.clear(/*size=*/1); 6003 return 1; 6004 } 6005 } 6006 unsigned OrderedLoopCount = 1; 6007 if (OrderedLoopCountExpr) { 6008 // Found 'ordered' clause - calculate collapse number. 6009 Expr::EvalResult EVResult; 6010 if (!OrderedLoopCountExpr->isValueDependent() && 6011 OrderedLoopCountExpr->EvaluateAsInt(EVResult, 6012 SemaRef.getASTContext())) { 6013 llvm::APSInt Result = EVResult.Val.getInt(); 6014 if (Result.getLimitedValue() < NestedLoopCount) { 6015 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 6016 diag::err_omp_wrong_ordered_loop_count) 6017 << OrderedLoopCountExpr->getSourceRange(); 6018 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 6019 diag::note_collapse_loop_count) 6020 << CollapseLoopCountExpr->getSourceRange(); 6021 } 6022 OrderedLoopCount = Result.getLimitedValue(); 6023 } else { 6024 Built.clear(/*size=*/1); 6025 return 1; 6026 } 6027 } 6028 // This is helper routine for loop directives (e.g., 'for', 'simd', 6029 // 'for simd', etc.). 6030 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 6031 SmallVector<LoopIterationSpace, 4> IterSpaces( 6032 std::max(OrderedLoopCount, NestedLoopCount)); 6033 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); 6034 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 6035 if (checkOpenMPIterationSpace( 6036 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 6037 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 6038 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt], 6039 Captures)) 6040 return 0; 6041 // Move on to the next nested for loop, or to the loop body. 6042 // OpenMP [2.8.1, simd construct, Restrictions] 6043 // All loops associated with the construct must be perfectly nested; that 6044 // is, there must be no intervening code nor any OpenMP directive between 6045 // any two loops. 6046 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); 6047 } 6048 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) { 6049 if (checkOpenMPIterationSpace( 6050 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 6051 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 6052 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt], 6053 Captures)) 6054 return 0; 6055 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) { 6056 // Handle initialization of captured loop iterator variables. 6057 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar); 6058 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) { 6059 Captures[DRE] = DRE; 6060 } 6061 } 6062 // Move on to the next nested for loop, or to the loop body. 6063 // OpenMP [2.8.1, simd construct, Restrictions] 6064 // All loops associated with the construct must be perfectly nested; that 6065 // is, there must be no intervening code nor any OpenMP directive between 6066 // any two loops. 6067 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers(); 6068 } 6069 6070 Built.clear(/* size */ NestedLoopCount); 6071 6072 if (SemaRef.CurContext->isDependentContext()) 6073 return NestedLoopCount; 6074 6075 // An example of what is generated for the following code: 6076 // 6077 // #pragma omp simd collapse(2) ordered(2) 6078 // for (i = 0; i < NI; ++i) 6079 // for (k = 0; k < NK; ++k) 6080 // for (j = J0; j < NJ; j+=2) { 6081 // <loop body> 6082 // } 6083 // 6084 // We generate the code below. 6085 // Note: the loop body may be outlined in CodeGen. 6086 // Note: some counters may be C++ classes, operator- is used to find number of 6087 // iterations and operator+= to calculate counter value. 6088 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 6089 // or i64 is currently supported). 6090 // 6091 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 6092 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 6093 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 6094 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 6095 // // similar updates for vars in clauses (e.g. 'linear') 6096 // <loop body (using local i and j)> 6097 // } 6098 // i = NI; // assign final values of counters 6099 // j = NJ; 6100 // 6101 6102 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 6103 // the iteration counts of the collapsed for loops. 6104 // Precondition tests if there is at least one iteration (all conditions are 6105 // true). 6106 auto PreCond = ExprResult(IterSpaces[0].PreCond); 6107 Expr *N0 = IterSpaces[0].NumIterations; 6108 ExprResult LastIteration32 = 6109 widenIterationCount(/*Bits=*/32, 6110 SemaRef 6111 .PerformImplicitConversion( 6112 N0->IgnoreImpCasts(), N0->getType(), 6113 Sema::AA_Converting, /*AllowExplicit=*/true) 6114 .get(), 6115 SemaRef); 6116 ExprResult LastIteration64 = widenIterationCount( 6117 /*Bits=*/64, 6118 SemaRef 6119 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 6120 Sema::AA_Converting, 6121 /*AllowExplicit=*/true) 6122 .get(), 6123 SemaRef); 6124 6125 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 6126 return NestedLoopCount; 6127 6128 ASTContext &C = SemaRef.Context; 6129 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 6130 6131 Scope *CurScope = DSA.getCurScope(); 6132 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 6133 if (PreCond.isUsable()) { 6134 PreCond = 6135 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 6136 PreCond.get(), IterSpaces[Cnt].PreCond); 6137 } 6138 Expr *N = IterSpaces[Cnt].NumIterations; 6139 SourceLocation Loc = N->getExprLoc(); 6140 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 6141 if (LastIteration32.isUsable()) 6142 LastIteration32 = SemaRef.BuildBinOp( 6143 CurScope, Loc, BO_Mul, LastIteration32.get(), 6144 SemaRef 6145 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 6146 Sema::AA_Converting, 6147 /*AllowExplicit=*/true) 6148 .get()); 6149 if (LastIteration64.isUsable()) 6150 LastIteration64 = SemaRef.BuildBinOp( 6151 CurScope, Loc, BO_Mul, LastIteration64.get(), 6152 SemaRef 6153 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 6154 Sema::AA_Converting, 6155 /*AllowExplicit=*/true) 6156 .get()); 6157 } 6158 6159 // Choose either the 32-bit or 64-bit version. 6160 ExprResult LastIteration = LastIteration64; 6161 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse || 6162 (LastIteration32.isUsable() && 6163 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 6164 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 6165 fitsInto( 6166 /*Bits=*/32, 6167 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 6168 LastIteration64.get(), SemaRef)))) 6169 LastIteration = LastIteration32; 6170 QualType VType = LastIteration.get()->getType(); 6171 QualType RealVType = VType; 6172 QualType StrideVType = VType; 6173 if (isOpenMPTaskLoopDirective(DKind)) { 6174 VType = 6175 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 6176 StrideVType = 6177 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 6178 } 6179 6180 if (!LastIteration.isUsable()) 6181 return 0; 6182 6183 // Save the number of iterations. 6184 ExprResult NumIterations = LastIteration; 6185 { 6186 LastIteration = SemaRef.BuildBinOp( 6187 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 6188 LastIteration.get(), 6189 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 6190 if (!LastIteration.isUsable()) 6191 return 0; 6192 } 6193 6194 // Calculate the last iteration number beforehand instead of doing this on 6195 // each iteration. Do not do this if the number of iterations may be kfold-ed. 6196 llvm::APSInt Result; 6197 bool IsConstant = 6198 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); 6199 ExprResult CalcLastIteration; 6200 if (!IsConstant) { 6201 ExprResult SaveRef = 6202 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 6203 LastIteration = SaveRef; 6204 6205 // Prepare SaveRef + 1. 6206 NumIterations = SemaRef.BuildBinOp( 6207 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 6208 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 6209 if (!NumIterations.isUsable()) 6210 return 0; 6211 } 6212 6213 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 6214 6215 // Build variables passed into runtime, necessary for worksharing directives. 6216 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 6217 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 6218 isOpenMPDistributeDirective(DKind)) { 6219 // Lower bound variable, initialized with zero. 6220 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 6221 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 6222 SemaRef.AddInitializerToDecl(LBDecl, 6223 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 6224 /*DirectInit*/ false); 6225 6226 // Upper bound variable, initialized with last iteration number. 6227 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 6228 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 6229 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 6230 /*DirectInit*/ false); 6231 6232 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 6233 // This will be used to implement clause 'lastprivate'. 6234 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 6235 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 6236 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 6237 SemaRef.AddInitializerToDecl(ILDecl, 6238 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 6239 /*DirectInit*/ false); 6240 6241 // Stride variable returned by runtime (we initialize it to 1 by default). 6242 VarDecl *STDecl = 6243 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 6244 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 6245 SemaRef.AddInitializerToDecl(STDecl, 6246 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 6247 /*DirectInit*/ false); 6248 6249 // Build expression: UB = min(UB, LastIteration) 6250 // It is necessary for CodeGen of directives with static scheduling. 6251 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 6252 UB.get(), LastIteration.get()); 6253 ExprResult CondOp = SemaRef.ActOnConditionalOp( 6254 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 6255 LastIteration.get(), UB.get()); 6256 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 6257 CondOp.get()); 6258 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false); 6259 6260 // If we have a combined directive that combines 'distribute', 'for' or 6261 // 'simd' we need to be able to access the bounds of the schedule of the 6262 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 6263 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 6264 if (isOpenMPLoopBoundSharingDirective(DKind)) { 6265 // Lower bound variable, initialized with zero. 6266 VarDecl *CombLBDecl = 6267 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 6268 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 6269 SemaRef.AddInitializerToDecl( 6270 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 6271 /*DirectInit*/ false); 6272 6273 // Upper bound variable, initialized with last iteration number. 6274 VarDecl *CombUBDecl = 6275 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 6276 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 6277 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 6278 /*DirectInit*/ false); 6279 6280 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 6281 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 6282 ExprResult CombCondOp = 6283 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 6284 LastIteration.get(), CombUB.get()); 6285 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 6286 CombCondOp.get()); 6287 CombEUB = 6288 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false); 6289 6290 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 6291 // We expect to have at least 2 more parameters than the 'parallel' 6292 // directive does - the lower and upper bounds of the previous schedule. 6293 assert(CD->getNumParams() >= 4 && 6294 "Unexpected number of parameters in loop combined directive"); 6295 6296 // Set the proper type for the bounds given what we learned from the 6297 // enclosed loops. 6298 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 6299 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 6300 6301 // Previous lower and upper bounds are obtained from the region 6302 // parameters. 6303 PrevLB = 6304 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 6305 PrevUB = 6306 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 6307 } 6308 } 6309 6310 // Build the iteration variable and its initialization before loop. 6311 ExprResult IV; 6312 ExprResult Init, CombInit; 6313 { 6314 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 6315 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 6316 Expr *RHS = 6317 (isOpenMPWorksharingDirective(DKind) || 6318 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 6319 ? LB.get() 6320 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 6321 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 6322 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false); 6323 6324 if (isOpenMPLoopBoundSharingDirective(DKind)) { 6325 Expr *CombRHS = 6326 (isOpenMPWorksharingDirective(DKind) || 6327 isOpenMPTaskLoopDirective(DKind) || 6328 isOpenMPDistributeDirective(DKind)) 6329 ? CombLB.get() 6330 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 6331 CombInit = 6332 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 6333 CombInit = 6334 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false); 6335 } 6336 } 6337 6338 bool UseStrictCompare = 6339 RealVType->hasUnsignedIntegerRepresentation() && 6340 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) { 6341 return LIS.IsStrictCompare; 6342 }); 6343 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for 6344 // unsigned IV)) for worksharing loops. 6345 SourceLocation CondLoc = AStmt->getBeginLoc(); 6346 Expr *BoundUB = UB.get(); 6347 if (UseStrictCompare) { 6348 BoundUB = 6349 SemaRef 6350 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB, 6351 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 6352 .get(); 6353 BoundUB = 6354 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get(); 6355 } 6356 ExprResult Cond = 6357 (isOpenMPWorksharingDirective(DKind) || 6358 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 6359 ? SemaRef.BuildBinOp(CurScope, CondLoc, 6360 UseStrictCompare ? BO_LT : BO_LE, IV.get(), 6361 BoundUB) 6362 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 6363 NumIterations.get()); 6364 ExprResult CombDistCond; 6365 if (isOpenMPLoopBoundSharingDirective(DKind)) { 6366 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 6367 NumIterations.get()); 6368 } 6369 6370 ExprResult CombCond; 6371 if (isOpenMPLoopBoundSharingDirective(DKind)) { 6372 Expr *BoundCombUB = CombUB.get(); 6373 if (UseStrictCompare) { 6374 BoundCombUB = 6375 SemaRef 6376 .BuildBinOp( 6377 CurScope, CondLoc, BO_Add, BoundCombUB, 6378 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 6379 .get(); 6380 BoundCombUB = 6381 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false) 6382 .get(); 6383 } 6384 CombCond = 6385 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 6386 IV.get(), BoundCombUB); 6387 } 6388 // Loop increment (IV = IV + 1) 6389 SourceLocation IncLoc = AStmt->getBeginLoc(); 6390 ExprResult Inc = 6391 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 6392 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 6393 if (!Inc.isUsable()) 6394 return 0; 6395 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 6396 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false); 6397 if (!Inc.isUsable()) 6398 return 0; 6399 6400 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 6401 // Used for directives with static scheduling. 6402 // In combined construct, add combined version that use CombLB and CombUB 6403 // base variables for the update 6404 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 6405 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 6406 isOpenMPDistributeDirective(DKind)) { 6407 // LB + ST 6408 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 6409 if (!NextLB.isUsable()) 6410 return 0; 6411 // LB = LB + ST 6412 NextLB = 6413 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 6414 NextLB = 6415 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false); 6416 if (!NextLB.isUsable()) 6417 return 0; 6418 // UB + ST 6419 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 6420 if (!NextUB.isUsable()) 6421 return 0; 6422 // UB = UB + ST 6423 NextUB = 6424 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 6425 NextUB = 6426 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false); 6427 if (!NextUB.isUsable()) 6428 return 0; 6429 if (isOpenMPLoopBoundSharingDirective(DKind)) { 6430 CombNextLB = 6431 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 6432 if (!NextLB.isUsable()) 6433 return 0; 6434 // LB = LB + ST 6435 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 6436 CombNextLB.get()); 6437 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(), 6438 /*DiscardedValue*/ false); 6439 if (!CombNextLB.isUsable()) 6440 return 0; 6441 // UB + ST 6442 CombNextUB = 6443 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 6444 if (!CombNextUB.isUsable()) 6445 return 0; 6446 // UB = UB + ST 6447 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 6448 CombNextUB.get()); 6449 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(), 6450 /*DiscardedValue*/ false); 6451 if (!CombNextUB.isUsable()) 6452 return 0; 6453 } 6454 } 6455 6456 // Create increment expression for distribute loop when combined in a same 6457 // directive with for as IV = IV + ST; ensure upper bound expression based 6458 // on PrevUB instead of NumIterations - used to implement 'for' when found 6459 // in combination with 'distribute', like in 'distribute parallel for' 6460 SourceLocation DistIncLoc = AStmt->getBeginLoc(); 6461 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond; 6462 if (isOpenMPLoopBoundSharingDirective(DKind)) { 6463 DistCond = SemaRef.BuildBinOp( 6464 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB); 6465 assert(DistCond.isUsable() && "distribute cond expr was not built"); 6466 6467 DistInc = 6468 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 6469 assert(DistInc.isUsable() && "distribute inc expr was not built"); 6470 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 6471 DistInc.get()); 6472 DistInc = 6473 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false); 6474 assert(DistInc.isUsable() && "distribute inc expr was not built"); 6475 6476 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 6477 // construct 6478 SourceLocation DistEUBLoc = AStmt->getBeginLoc(); 6479 ExprResult IsUBGreater = 6480 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get()); 6481 ExprResult CondOp = SemaRef.ActOnConditionalOp( 6482 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get()); 6483 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 6484 CondOp.get()); 6485 PrevEUB = 6486 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false); 6487 6488 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in 6489 // parallel for is in combination with a distribute directive with 6490 // schedule(static, 1) 6491 Expr *BoundPrevUB = PrevUB.get(); 6492 if (UseStrictCompare) { 6493 BoundPrevUB = 6494 SemaRef 6495 .BuildBinOp( 6496 CurScope, CondLoc, BO_Add, BoundPrevUB, 6497 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 6498 .get(); 6499 BoundPrevUB = 6500 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false) 6501 .get(); 6502 } 6503 ParForInDistCond = 6504 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 6505 IV.get(), BoundPrevUB); 6506 } 6507 6508 // Build updates and final values of the loop counters. 6509 bool HasErrors = false; 6510 Built.Counters.resize(NestedLoopCount); 6511 Built.Inits.resize(NestedLoopCount); 6512 Built.Updates.resize(NestedLoopCount); 6513 Built.Finals.resize(NestedLoopCount); 6514 { 6515 // We implement the following algorithm for obtaining the 6516 // original loop iteration variable values based on the 6517 // value of the collapsed loop iteration variable IV. 6518 // 6519 // Let n+1 be the number of collapsed loops in the nest. 6520 // Iteration variables (I0, I1, .... In) 6521 // Iteration counts (N0, N1, ... Nn) 6522 // 6523 // Acc = IV; 6524 // 6525 // To compute Ik for loop k, 0 <= k <= n, generate: 6526 // Prod = N(k+1) * N(k+2) * ... * Nn; 6527 // Ik = Acc / Prod; 6528 // Acc -= Ik * Prod; 6529 // 6530 ExprResult Acc = IV; 6531 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 6532 LoopIterationSpace &IS = IterSpaces[Cnt]; 6533 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 6534 ExprResult Iter; 6535 6536 // Compute prod 6537 ExprResult Prod = 6538 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 6539 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K) 6540 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(), 6541 IterSpaces[K].NumIterations); 6542 6543 // Iter = Acc / Prod 6544 // If there is at least one more inner loop to avoid 6545 // multiplication by 1. 6546 if (Cnt + 1 < NestedLoopCount) 6547 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, 6548 Acc.get(), Prod.get()); 6549 else 6550 Iter = Acc; 6551 if (!Iter.isUsable()) { 6552 HasErrors = true; 6553 break; 6554 } 6555 6556 // Update Acc: 6557 // Acc -= Iter * Prod 6558 // Check if there is at least one more inner loop to avoid 6559 // multiplication by 1. 6560 if (Cnt + 1 < NestedLoopCount) 6561 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, 6562 Iter.get(), Prod.get()); 6563 else 6564 Prod = Iter; 6565 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub, 6566 Acc.get(), Prod.get()); 6567 6568 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 6569 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 6570 DeclRefExpr *CounterVar = buildDeclRefExpr( 6571 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 6572 /*RefersToCapture=*/true); 6573 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 6574 IS.CounterInit, Captures); 6575 if (!Init.isUsable()) { 6576 HasErrors = true; 6577 break; 6578 } 6579 ExprResult Update = buildCounterUpdate( 6580 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 6581 IS.CounterStep, IS.Subtract, &Captures); 6582 if (!Update.isUsable()) { 6583 HasErrors = true; 6584 break; 6585 } 6586 6587 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 6588 ExprResult Final = buildCounterUpdate( 6589 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, 6590 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures); 6591 if (!Final.isUsable()) { 6592 HasErrors = true; 6593 break; 6594 } 6595 6596 if (!Update.isUsable() || !Final.isUsable()) { 6597 HasErrors = true; 6598 break; 6599 } 6600 // Save results 6601 Built.Counters[Cnt] = IS.CounterVar; 6602 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 6603 Built.Inits[Cnt] = Init.get(); 6604 Built.Updates[Cnt] = Update.get(); 6605 Built.Finals[Cnt] = Final.get(); 6606 } 6607 } 6608 6609 if (HasErrors) 6610 return 0; 6611 6612 // Save results 6613 Built.IterationVarRef = IV.get(); 6614 Built.LastIteration = LastIteration.get(); 6615 Built.NumIterations = NumIterations.get(); 6616 Built.CalcLastIteration = SemaRef 6617 .ActOnFinishFullExpr(CalcLastIteration.get(), 6618 /*DiscardedValue*/ false) 6619 .get(); 6620 Built.PreCond = PreCond.get(); 6621 Built.PreInits = buildPreInits(C, Captures); 6622 Built.Cond = Cond.get(); 6623 Built.Init = Init.get(); 6624 Built.Inc = Inc.get(); 6625 Built.LB = LB.get(); 6626 Built.UB = UB.get(); 6627 Built.IL = IL.get(); 6628 Built.ST = ST.get(); 6629 Built.EUB = EUB.get(); 6630 Built.NLB = NextLB.get(); 6631 Built.NUB = NextUB.get(); 6632 Built.PrevLB = PrevLB.get(); 6633 Built.PrevUB = PrevUB.get(); 6634 Built.DistInc = DistInc.get(); 6635 Built.PrevEUB = PrevEUB.get(); 6636 Built.DistCombinedFields.LB = CombLB.get(); 6637 Built.DistCombinedFields.UB = CombUB.get(); 6638 Built.DistCombinedFields.EUB = CombEUB.get(); 6639 Built.DistCombinedFields.Init = CombInit.get(); 6640 Built.DistCombinedFields.Cond = CombCond.get(); 6641 Built.DistCombinedFields.NLB = CombNextLB.get(); 6642 Built.DistCombinedFields.NUB = CombNextUB.get(); 6643 Built.DistCombinedFields.DistCond = CombDistCond.get(); 6644 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get(); 6645 6646 return NestedLoopCount; 6647 } 6648 6649 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 6650 auto CollapseClauses = 6651 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 6652 if (CollapseClauses.begin() != CollapseClauses.end()) 6653 return (*CollapseClauses.begin())->getNumForLoops(); 6654 return nullptr; 6655 } 6656 6657 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 6658 auto OrderedClauses = 6659 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 6660 if (OrderedClauses.begin() != OrderedClauses.end()) 6661 return (*OrderedClauses.begin())->getNumForLoops(); 6662 return nullptr; 6663 } 6664 6665 static bool checkSimdlenSafelenSpecified(Sema &S, 6666 const ArrayRef<OMPClause *> Clauses) { 6667 const OMPSafelenClause *Safelen = nullptr; 6668 const OMPSimdlenClause *Simdlen = nullptr; 6669 6670 for (const OMPClause *Clause : Clauses) { 6671 if (Clause->getClauseKind() == OMPC_safelen) 6672 Safelen = cast<OMPSafelenClause>(Clause); 6673 else if (Clause->getClauseKind() == OMPC_simdlen) 6674 Simdlen = cast<OMPSimdlenClause>(Clause); 6675 if (Safelen && Simdlen) 6676 break; 6677 } 6678 6679 if (Simdlen && Safelen) { 6680 const Expr *SimdlenLength = Simdlen->getSimdlen(); 6681 const Expr *SafelenLength = Safelen->getSafelen(); 6682 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 6683 SimdlenLength->isInstantiationDependent() || 6684 SimdlenLength->containsUnexpandedParameterPack()) 6685 return false; 6686 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 6687 SafelenLength->isInstantiationDependent() || 6688 SafelenLength->containsUnexpandedParameterPack()) 6689 return false; 6690 Expr::EvalResult SimdlenResult, SafelenResult; 6691 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context); 6692 SafelenLength->EvaluateAsInt(SafelenResult, S.Context); 6693 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt(); 6694 llvm::APSInt SafelenRes = SafelenResult.Val.getInt(); 6695 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 6696 // If both simdlen and safelen clauses are specified, the value of the 6697 // simdlen parameter must be less than or equal to the value of the safelen 6698 // parameter. 6699 if (SimdlenRes > SafelenRes) { 6700 S.Diag(SimdlenLength->getExprLoc(), 6701 diag::err_omp_wrong_simdlen_safelen_values) 6702 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 6703 return true; 6704 } 6705 } 6706 return false; 6707 } 6708 6709 StmtResult 6710 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 6711 SourceLocation StartLoc, SourceLocation EndLoc, 6712 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6713 if (!AStmt) 6714 return StmtError(); 6715 6716 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6717 OMPLoopDirective::HelperExprs B; 6718 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6719 // define the nested loops number. 6720 unsigned NestedLoopCount = checkOpenMPLoop( 6721 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 6722 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 6723 if (NestedLoopCount == 0) 6724 return StmtError(); 6725 6726 assert((CurContext->isDependentContext() || B.builtAll()) && 6727 "omp simd loop exprs were not built"); 6728 6729 if (!CurContext->isDependentContext()) { 6730 // Finalize the clauses that need pre-built expressions for CodeGen. 6731 for (OMPClause *C : Clauses) { 6732 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6733 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6734 B.NumIterations, *this, CurScope, 6735 DSAStack)) 6736 return StmtError(); 6737 } 6738 } 6739 6740 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6741 return StmtError(); 6742 6743 setFunctionHasBranchProtectedScope(); 6744 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 6745 Clauses, AStmt, B); 6746 } 6747 6748 StmtResult 6749 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 6750 SourceLocation StartLoc, SourceLocation EndLoc, 6751 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6752 if (!AStmt) 6753 return StmtError(); 6754 6755 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6756 OMPLoopDirective::HelperExprs B; 6757 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6758 // define the nested loops number. 6759 unsigned NestedLoopCount = checkOpenMPLoop( 6760 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 6761 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 6762 if (NestedLoopCount == 0) 6763 return StmtError(); 6764 6765 assert((CurContext->isDependentContext() || B.builtAll()) && 6766 "omp for loop exprs were not built"); 6767 6768 if (!CurContext->isDependentContext()) { 6769 // Finalize the clauses that need pre-built expressions for CodeGen. 6770 for (OMPClause *C : Clauses) { 6771 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6772 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6773 B.NumIterations, *this, CurScope, 6774 DSAStack)) 6775 return StmtError(); 6776 } 6777 } 6778 6779 setFunctionHasBranchProtectedScope(); 6780 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 6781 Clauses, AStmt, B, DSAStack->isCancelRegion()); 6782 } 6783 6784 StmtResult Sema::ActOnOpenMPForSimdDirective( 6785 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6786 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6787 if (!AStmt) 6788 return StmtError(); 6789 6790 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6791 OMPLoopDirective::HelperExprs B; 6792 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6793 // define the nested loops number. 6794 unsigned NestedLoopCount = 6795 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 6796 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 6797 VarsWithImplicitDSA, B); 6798 if (NestedLoopCount == 0) 6799 return StmtError(); 6800 6801 assert((CurContext->isDependentContext() || B.builtAll()) && 6802 "omp for simd loop exprs were not built"); 6803 6804 if (!CurContext->isDependentContext()) { 6805 // Finalize the clauses that need pre-built expressions for CodeGen. 6806 for (OMPClause *C : Clauses) { 6807 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 6808 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 6809 B.NumIterations, *this, CurScope, 6810 DSAStack)) 6811 return StmtError(); 6812 } 6813 } 6814 6815 if (checkSimdlenSafelenSpecified(*this, Clauses)) 6816 return StmtError(); 6817 6818 setFunctionHasBranchProtectedScope(); 6819 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 6820 Clauses, AStmt, B); 6821 } 6822 6823 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 6824 Stmt *AStmt, 6825 SourceLocation StartLoc, 6826 SourceLocation EndLoc) { 6827 if (!AStmt) 6828 return StmtError(); 6829 6830 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6831 auto BaseStmt = AStmt; 6832 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 6833 BaseStmt = CS->getCapturedStmt(); 6834 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 6835 auto S = C->children(); 6836 if (S.begin() == S.end()) 6837 return StmtError(); 6838 // All associated statements must be '#pragma omp section' except for 6839 // the first one. 6840 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 6841 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 6842 if (SectionStmt) 6843 Diag(SectionStmt->getBeginLoc(), 6844 diag::err_omp_sections_substmt_not_section); 6845 return StmtError(); 6846 } 6847 cast<OMPSectionDirective>(SectionStmt) 6848 ->setHasCancel(DSAStack->isCancelRegion()); 6849 } 6850 } else { 6851 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt); 6852 return StmtError(); 6853 } 6854 6855 setFunctionHasBranchProtectedScope(); 6856 6857 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 6858 DSAStack->isCancelRegion()); 6859 } 6860 6861 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 6862 SourceLocation StartLoc, 6863 SourceLocation EndLoc) { 6864 if (!AStmt) 6865 return StmtError(); 6866 6867 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6868 6869 setFunctionHasBranchProtectedScope(); 6870 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 6871 6872 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 6873 DSAStack->isCancelRegion()); 6874 } 6875 6876 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 6877 Stmt *AStmt, 6878 SourceLocation StartLoc, 6879 SourceLocation EndLoc) { 6880 if (!AStmt) 6881 return StmtError(); 6882 6883 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6884 6885 setFunctionHasBranchProtectedScope(); 6886 6887 // OpenMP [2.7.3, single Construct, Restrictions] 6888 // The copyprivate clause must not be used with the nowait clause. 6889 const OMPClause *Nowait = nullptr; 6890 const OMPClause *Copyprivate = nullptr; 6891 for (const OMPClause *Clause : Clauses) { 6892 if (Clause->getClauseKind() == OMPC_nowait) 6893 Nowait = Clause; 6894 else if (Clause->getClauseKind() == OMPC_copyprivate) 6895 Copyprivate = Clause; 6896 if (Copyprivate && Nowait) { 6897 Diag(Copyprivate->getBeginLoc(), 6898 diag::err_omp_single_copyprivate_with_nowait); 6899 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here); 6900 return StmtError(); 6901 } 6902 } 6903 6904 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 6905 } 6906 6907 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 6908 SourceLocation StartLoc, 6909 SourceLocation EndLoc) { 6910 if (!AStmt) 6911 return StmtError(); 6912 6913 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6914 6915 setFunctionHasBranchProtectedScope(); 6916 6917 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 6918 } 6919 6920 StmtResult Sema::ActOnOpenMPCriticalDirective( 6921 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 6922 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 6923 if (!AStmt) 6924 return StmtError(); 6925 6926 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 6927 6928 bool ErrorFound = false; 6929 llvm::APSInt Hint; 6930 SourceLocation HintLoc; 6931 bool DependentHint = false; 6932 for (const OMPClause *C : Clauses) { 6933 if (C->getClauseKind() == OMPC_hint) { 6934 if (!DirName.getName()) { 6935 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name); 6936 ErrorFound = true; 6937 } 6938 Expr *E = cast<OMPHintClause>(C)->getHint(); 6939 if (E->isTypeDependent() || E->isValueDependent() || 6940 E->isInstantiationDependent()) { 6941 DependentHint = true; 6942 } else { 6943 Hint = E->EvaluateKnownConstInt(Context); 6944 HintLoc = C->getBeginLoc(); 6945 } 6946 } 6947 } 6948 if (ErrorFound) 6949 return StmtError(); 6950 const auto Pair = DSAStack->getCriticalWithHint(DirName); 6951 if (Pair.first && DirName.getName() && !DependentHint) { 6952 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 6953 Diag(StartLoc, diag::err_omp_critical_with_hint); 6954 if (HintLoc.isValid()) 6955 Diag(HintLoc, diag::note_omp_critical_hint_here) 6956 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); 6957 else 6958 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 6959 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 6960 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here) 6961 << 1 6962 << C->getHint()->EvaluateKnownConstInt(Context).toString( 6963 /*Radix=*/10, /*Signed=*/false); 6964 } else { 6965 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1; 6966 } 6967 } 6968 } 6969 6970 setFunctionHasBranchProtectedScope(); 6971 6972 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 6973 Clauses, AStmt); 6974 if (!Pair.first && DirName.getName() && !DependentHint) 6975 DSAStack->addCriticalWithHint(Dir, Hint); 6976 return Dir; 6977 } 6978 6979 StmtResult Sema::ActOnOpenMPParallelForDirective( 6980 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 6981 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 6982 if (!AStmt) 6983 return StmtError(); 6984 6985 auto *CS = cast<CapturedStmt>(AStmt); 6986 // 1.2.2 OpenMP Language Terminology 6987 // Structured block - An executable statement with a single entry at the 6988 // top and a single exit at the bottom. 6989 // The point of exit cannot be a branch out of the structured block. 6990 // longjmp() and throw() must not violate the entry/exit criteria. 6991 CS->getCapturedDecl()->setNothrow(); 6992 6993 OMPLoopDirective::HelperExprs B; 6994 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 6995 // define the nested loops number. 6996 unsigned NestedLoopCount = 6997 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 6998 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 6999 VarsWithImplicitDSA, B); 7000 if (NestedLoopCount == 0) 7001 return StmtError(); 7002 7003 assert((CurContext->isDependentContext() || B.builtAll()) && 7004 "omp parallel for loop exprs were not built"); 7005 7006 if (!CurContext->isDependentContext()) { 7007 // Finalize the clauses that need pre-built expressions for CodeGen. 7008 for (OMPClause *C : Clauses) { 7009 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7010 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7011 B.NumIterations, *this, CurScope, 7012 DSAStack)) 7013 return StmtError(); 7014 } 7015 } 7016 7017 setFunctionHasBranchProtectedScope(); 7018 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc, 7019 NestedLoopCount, Clauses, AStmt, B, 7020 DSAStack->isCancelRegion()); 7021 } 7022 7023 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 7024 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 7025 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 7026 if (!AStmt) 7027 return StmtError(); 7028 7029 auto *CS = cast<CapturedStmt>(AStmt); 7030 // 1.2.2 OpenMP Language Terminology 7031 // Structured block - An executable statement with a single entry at the 7032 // top and a single exit at the bottom. 7033 // The point of exit cannot be a branch out of the structured block. 7034 // longjmp() and throw() must not violate the entry/exit criteria. 7035 CS->getCapturedDecl()->setNothrow(); 7036 7037 OMPLoopDirective::HelperExprs B; 7038 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 7039 // define the nested loops number. 7040 unsigned NestedLoopCount = 7041 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 7042 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 7043 VarsWithImplicitDSA, B); 7044 if (NestedLoopCount == 0) 7045 return StmtError(); 7046 7047 if (!CurContext->isDependentContext()) { 7048 // Finalize the clauses that need pre-built expressions for CodeGen. 7049 for (OMPClause *C : Clauses) { 7050 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 7051 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 7052 B.NumIterations, *this, CurScope, 7053 DSAStack)) 7054 return StmtError(); 7055 } 7056 } 7057 7058 if (checkSimdlenSafelenSpecified(*this, Clauses)) 7059 return StmtError(); 7060 7061 setFunctionHasBranchProtectedScope(); 7062 return OMPParallelForSimdDirective::Create( 7063 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 7064 } 7065 7066 StmtResult 7067 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 7068 Stmt *AStmt, SourceLocation StartLoc, 7069 SourceLocation EndLoc) { 7070 if (!AStmt) 7071 return StmtError(); 7072 7073 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 7074 auto BaseStmt = AStmt; 7075 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 7076 BaseStmt = CS->getCapturedStmt(); 7077 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 7078 auto S = C->children(); 7079 if (S.begin() == S.end()) 7080 return StmtError(); 7081 // All associated statements must be '#pragma omp section' except for 7082 // the first one. 7083 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 7084 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 7085 if (SectionStmt) 7086 Diag(SectionStmt->getBeginLoc(), 7087 diag::err_omp_parallel_sections_substmt_not_section); 7088 return StmtError(); 7089 } 7090 cast<OMPSectionDirective>(SectionStmt) 7091 ->setHasCancel(DSAStack->isCancelRegion()); 7092 } 7093 } else { 7094 Diag(AStmt->getBeginLoc(), 7095 diag::err_omp_parallel_sections_not_compound_stmt); 7096 return StmtError(); 7097 } 7098 7099 setFunctionHasBranchProtectedScope(); 7100 7101 return OMPParallelSectionsDirective::Create( 7102 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion()); 7103 } 7104 7105 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 7106 Stmt *AStmt, SourceLocation StartLoc, 7107 SourceLocation EndLoc) { 7108 if (!AStmt) 7109 return StmtError(); 7110 7111 auto *CS = cast<CapturedStmt>(AStmt); 7112 // 1.2.2 OpenMP Language Terminology 7113 // Structured block - An executable statement with a single entry at the 7114 // top and a single exit at the bottom. 7115 // The point of exit cannot be a branch out of the structured block. 7116 // longjmp() and throw() must not violate the entry/exit criteria. 7117 CS->getCapturedDecl()->setNothrow(); 7118 7119 setFunctionHasBranchProtectedScope(); 7120 7121 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 7122 DSAStack->isCancelRegion()); 7123 } 7124 7125 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 7126 SourceLocation EndLoc) { 7127 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 7128 } 7129 7130 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 7131 SourceLocation EndLoc) { 7132 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 7133 } 7134 7135 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 7136 SourceLocation EndLoc) { 7137 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 7138 } 7139 7140 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 7141 Stmt *AStmt, 7142 SourceLocation StartLoc, 7143 SourceLocation EndLoc) { 7144 if (!AStmt) 7145 return StmtError(); 7146 7147 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 7148 7149 setFunctionHasBranchProtectedScope(); 7150 7151 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 7152 AStmt, 7153 DSAStack->getTaskgroupReductionRef()); 7154 } 7155 7156 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 7157 SourceLocation StartLoc, 7158 SourceLocation EndLoc) { 7159 assert(Clauses.size() <= 1 && "Extra clauses in flush directive"); 7160 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 7161 } 7162 7163 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 7164 Stmt *AStmt, 7165 SourceLocation StartLoc, 7166 SourceLocation EndLoc) { 7167 const OMPClause *DependFound = nullptr; 7168 const OMPClause *DependSourceClause = nullptr; 7169 const OMPClause *DependSinkClause = nullptr; 7170 bool ErrorFound = false; 7171 const OMPThreadsClause *TC = nullptr; 7172 const OMPSIMDClause *SC = nullptr; 7173 for (const OMPClause *C : Clauses) { 7174 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 7175 DependFound = C; 7176 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 7177 if (DependSourceClause) { 7178 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 7179 << getOpenMPDirectiveName(OMPD_ordered) 7180 << getOpenMPClauseName(OMPC_depend) << 2; 7181 ErrorFound = true; 7182 } else { 7183 DependSourceClause = C; 7184 } 7185 if (DependSinkClause) { 7186 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 7187 << 0; 7188 ErrorFound = true; 7189 } 7190 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 7191 if (DependSourceClause) { 7192 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 7193 << 1; 7194 ErrorFound = true; 7195 } 7196 DependSinkClause = C; 7197 } 7198 } else if (C->getClauseKind() == OMPC_threads) { 7199 TC = cast<OMPThreadsClause>(C); 7200 } else if (C->getClauseKind() == OMPC_simd) { 7201 SC = cast<OMPSIMDClause>(C); 7202 } 7203 } 7204 if (!ErrorFound && !SC && 7205 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 7206 // OpenMP [2.8.1,simd Construct, Restrictions] 7207 // An ordered construct with the simd clause is the only OpenMP construct 7208 // that can appear in the simd region. 7209 Diag(StartLoc, diag::err_omp_prohibited_region_simd); 7210 ErrorFound = true; 7211 } else if (DependFound && (TC || SC)) { 7212 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd) 7213 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 7214 ErrorFound = true; 7215 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) { 7216 Diag(DependFound->getBeginLoc(), 7217 diag::err_omp_ordered_directive_without_param); 7218 ErrorFound = true; 7219 } else if (TC || Clauses.empty()) { 7220 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) { 7221 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc; 7222 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 7223 << (TC != nullptr); 7224 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param); 7225 ErrorFound = true; 7226 } 7227 } 7228 if ((!AStmt && !DependFound) || ErrorFound) 7229 return StmtError(); 7230 7231 if (AStmt) { 7232 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 7233 7234 setFunctionHasBranchProtectedScope(); 7235 } 7236 7237 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 7238 } 7239 7240 namespace { 7241 /// Helper class for checking expression in 'omp atomic [update]' 7242 /// construct. 7243 class OpenMPAtomicUpdateChecker { 7244 /// Error results for atomic update expressions. 7245 enum ExprAnalysisErrorCode { 7246 /// A statement is not an expression statement. 7247 NotAnExpression, 7248 /// Expression is not builtin binary or unary operation. 7249 NotABinaryOrUnaryExpression, 7250 /// Unary operation is not post-/pre- increment/decrement operation. 7251 NotAnUnaryIncDecExpression, 7252 /// An expression is not of scalar type. 7253 NotAScalarType, 7254 /// A binary operation is not an assignment operation. 7255 NotAnAssignmentOp, 7256 /// RHS part of the binary operation is not a binary expression. 7257 NotABinaryExpression, 7258 /// RHS part is not additive/multiplicative/shift/biwise binary 7259 /// expression. 7260 NotABinaryOperator, 7261 /// RHS binary operation does not have reference to the updated LHS 7262 /// part. 7263 NotAnUpdateExpression, 7264 /// No errors is found. 7265 NoError 7266 }; 7267 /// Reference to Sema. 7268 Sema &SemaRef; 7269 /// A location for note diagnostics (when error is found). 7270 SourceLocation NoteLoc; 7271 /// 'x' lvalue part of the source atomic expression. 7272 Expr *X; 7273 /// 'expr' rvalue part of the source atomic expression. 7274 Expr *E; 7275 /// Helper expression of the form 7276 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 7277 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 7278 Expr *UpdateExpr; 7279 /// Is 'x' a LHS in a RHS part of full update expression. It is 7280 /// important for non-associative operations. 7281 bool IsXLHSInRHSPart; 7282 BinaryOperatorKind Op; 7283 SourceLocation OpLoc; 7284 /// true if the source expression is a postfix unary operation, false 7285 /// if it is a prefix unary operation. 7286 bool IsPostfixUpdate; 7287 7288 public: 7289 OpenMPAtomicUpdateChecker(Sema &SemaRef) 7290 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 7291 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 7292 /// Check specified statement that it is suitable for 'atomic update' 7293 /// constructs and extract 'x', 'expr' and Operation from the original 7294 /// expression. If DiagId and NoteId == 0, then only check is performed 7295 /// without error notification. 7296 /// \param DiagId Diagnostic which should be emitted if error is found. 7297 /// \param NoteId Diagnostic note for the main error message. 7298 /// \return true if statement is not an update expression, false otherwise. 7299 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 7300 /// Return the 'x' lvalue part of the source atomic expression. 7301 Expr *getX() const { return X; } 7302 /// Return the 'expr' rvalue part of the source atomic expression. 7303 Expr *getExpr() const { return E; } 7304 /// Return the update expression used in calculation of the updated 7305 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 7306 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 7307 Expr *getUpdateExpr() const { return UpdateExpr; } 7308 /// Return true if 'x' is LHS in RHS part of full update expression, 7309 /// false otherwise. 7310 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 7311 7312 /// true if the source expression is a postfix unary operation, false 7313 /// if it is a prefix unary operation. 7314 bool isPostfixUpdate() const { return IsPostfixUpdate; } 7315 7316 private: 7317 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 7318 unsigned NoteId = 0); 7319 }; 7320 } // namespace 7321 7322 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 7323 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 7324 ExprAnalysisErrorCode ErrorFound = NoError; 7325 SourceLocation ErrorLoc, NoteLoc; 7326 SourceRange ErrorRange, NoteRange; 7327 // Allowed constructs are: 7328 // x = x binop expr; 7329 // x = expr binop x; 7330 if (AtomicBinOp->getOpcode() == BO_Assign) { 7331 X = AtomicBinOp->getLHS(); 7332 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 7333 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 7334 if (AtomicInnerBinOp->isMultiplicativeOp() || 7335 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 7336 AtomicInnerBinOp->isBitwiseOp()) { 7337 Op = AtomicInnerBinOp->getOpcode(); 7338 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 7339 Expr *LHS = AtomicInnerBinOp->getLHS(); 7340 Expr *RHS = AtomicInnerBinOp->getRHS(); 7341 llvm::FoldingSetNodeID XId, LHSId, RHSId; 7342 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 7343 /*Canonical=*/true); 7344 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 7345 /*Canonical=*/true); 7346 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 7347 /*Canonical=*/true); 7348 if (XId == LHSId) { 7349 E = RHS; 7350 IsXLHSInRHSPart = true; 7351 } else if (XId == RHSId) { 7352 E = LHS; 7353 IsXLHSInRHSPart = false; 7354 } else { 7355 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 7356 ErrorRange = AtomicInnerBinOp->getSourceRange(); 7357 NoteLoc = X->getExprLoc(); 7358 NoteRange = X->getSourceRange(); 7359 ErrorFound = NotAnUpdateExpression; 7360 } 7361 } else { 7362 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 7363 ErrorRange = AtomicInnerBinOp->getSourceRange(); 7364 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 7365 NoteRange = SourceRange(NoteLoc, NoteLoc); 7366 ErrorFound = NotABinaryOperator; 7367 } 7368 } else { 7369 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 7370 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 7371 ErrorFound = NotABinaryExpression; 7372 } 7373 } else { 7374 ErrorLoc = AtomicBinOp->getExprLoc(); 7375 ErrorRange = AtomicBinOp->getSourceRange(); 7376 NoteLoc = AtomicBinOp->getOperatorLoc(); 7377 NoteRange = SourceRange(NoteLoc, NoteLoc); 7378 ErrorFound = NotAnAssignmentOp; 7379 } 7380 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 7381 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 7382 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 7383 return true; 7384 } 7385 if (SemaRef.CurContext->isDependentContext()) 7386 E = X = UpdateExpr = nullptr; 7387 return ErrorFound != NoError; 7388 } 7389 7390 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 7391 unsigned NoteId) { 7392 ExprAnalysisErrorCode ErrorFound = NoError; 7393 SourceLocation ErrorLoc, NoteLoc; 7394 SourceRange ErrorRange, NoteRange; 7395 // Allowed constructs are: 7396 // x++; 7397 // x--; 7398 // ++x; 7399 // --x; 7400 // x binop= expr; 7401 // x = x binop expr; 7402 // x = expr binop x; 7403 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 7404 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 7405 if (AtomicBody->getType()->isScalarType() || 7406 AtomicBody->isInstantiationDependent()) { 7407 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 7408 AtomicBody->IgnoreParenImpCasts())) { 7409 // Check for Compound Assignment Operation 7410 Op = BinaryOperator::getOpForCompoundAssignment( 7411 AtomicCompAssignOp->getOpcode()); 7412 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 7413 E = AtomicCompAssignOp->getRHS(); 7414 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 7415 IsXLHSInRHSPart = true; 7416 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 7417 AtomicBody->IgnoreParenImpCasts())) { 7418 // Check for Binary Operation 7419 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 7420 return true; 7421 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 7422 AtomicBody->IgnoreParenImpCasts())) { 7423 // Check for Unary Operation 7424 if (AtomicUnaryOp->isIncrementDecrementOp()) { 7425 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 7426 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 7427 OpLoc = AtomicUnaryOp->getOperatorLoc(); 7428 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 7429 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 7430 IsXLHSInRHSPart = true; 7431 } else { 7432 ErrorFound = NotAnUnaryIncDecExpression; 7433 ErrorLoc = AtomicUnaryOp->getExprLoc(); 7434 ErrorRange = AtomicUnaryOp->getSourceRange(); 7435 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 7436 NoteRange = SourceRange(NoteLoc, NoteLoc); 7437 } 7438 } else if (!AtomicBody->isInstantiationDependent()) { 7439 ErrorFound = NotABinaryOrUnaryExpression; 7440 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 7441 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 7442 } 7443 } else { 7444 ErrorFound = NotAScalarType; 7445 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc(); 7446 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 7447 } 7448 } else { 7449 ErrorFound = NotAnExpression; 7450 NoteLoc = ErrorLoc = S->getBeginLoc(); 7451 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 7452 } 7453 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 7454 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 7455 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 7456 return true; 7457 } 7458 if (SemaRef.CurContext->isDependentContext()) 7459 E = X = UpdateExpr = nullptr; 7460 if (ErrorFound == NoError && E && X) { 7461 // Build an update expression of form 'OpaqueValueExpr(x) binop 7462 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 7463 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 7464 auto *OVEX = new (SemaRef.getASTContext()) 7465 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); 7466 auto *OVEExpr = new (SemaRef.getASTContext()) 7467 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); 7468 ExprResult Update = 7469 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 7470 IsXLHSInRHSPart ? OVEExpr : OVEX); 7471 if (Update.isInvalid()) 7472 return true; 7473 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 7474 Sema::AA_Casting); 7475 if (Update.isInvalid()) 7476 return true; 7477 UpdateExpr = Update.get(); 7478 } 7479 return ErrorFound != NoError; 7480 } 7481 7482 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 7483 Stmt *AStmt, 7484 SourceLocation StartLoc, 7485 SourceLocation EndLoc) { 7486 if (!AStmt) 7487 return StmtError(); 7488 7489 auto *CS = cast<CapturedStmt>(AStmt); 7490 // 1.2.2 OpenMP Language Terminology 7491 // Structured block - An executable statement with a single entry at the 7492 // top and a single exit at the bottom. 7493 // The point of exit cannot be a branch out of the structured block. 7494 // longjmp() and throw() must not violate the entry/exit criteria. 7495 OpenMPClauseKind AtomicKind = OMPC_unknown; 7496 SourceLocation AtomicKindLoc; 7497 for (const OMPClause *C : Clauses) { 7498 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 7499 C->getClauseKind() == OMPC_update || 7500 C->getClauseKind() == OMPC_capture) { 7501 if (AtomicKind != OMPC_unknown) { 7502 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses) 7503 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 7504 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause) 7505 << getOpenMPClauseName(AtomicKind); 7506 } else { 7507 AtomicKind = C->getClauseKind(); 7508 AtomicKindLoc = C->getBeginLoc(); 7509 } 7510 } 7511 } 7512 7513 Stmt *Body = CS->getCapturedStmt(); 7514 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 7515 Body = EWC->getSubExpr(); 7516 7517 Expr *X = nullptr; 7518 Expr *V = nullptr; 7519 Expr *E = nullptr; 7520 Expr *UE = nullptr; 7521 bool IsXLHSInRHSPart = false; 7522 bool IsPostfixUpdate = false; 7523 // OpenMP [2.12.6, atomic Construct] 7524 // In the next expressions: 7525 // * x and v (as applicable) are both l-value expressions with scalar type. 7526 // * During the execution of an atomic region, multiple syntactic 7527 // occurrences of x must designate the same storage location. 7528 // * Neither of v and expr (as applicable) may access the storage location 7529 // designated by x. 7530 // * Neither of x and expr (as applicable) may access the storage location 7531 // designated by v. 7532 // * expr is an expression with scalar type. 7533 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 7534 // * binop, binop=, ++, and -- are not overloaded operators. 7535 // * The expression x binop expr must be numerically equivalent to x binop 7536 // (expr). This requirement is satisfied if the operators in expr have 7537 // precedence greater than binop, or by using parentheses around expr or 7538 // subexpressions of expr. 7539 // * The expression expr binop x must be numerically equivalent to (expr) 7540 // binop x. This requirement is satisfied if the operators in expr have 7541 // precedence equal to or greater than binop, or by using parentheses around 7542 // expr or subexpressions of expr. 7543 // * For forms that allow multiple occurrences of x, the number of times 7544 // that x is evaluated is unspecified. 7545 if (AtomicKind == OMPC_read) { 7546 enum { 7547 NotAnExpression, 7548 NotAnAssignmentOp, 7549 NotAScalarType, 7550 NotAnLValue, 7551 NoError 7552 } ErrorFound = NoError; 7553 SourceLocation ErrorLoc, NoteLoc; 7554 SourceRange ErrorRange, NoteRange; 7555 // If clause is read: 7556 // v = x; 7557 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 7558 const auto *AtomicBinOp = 7559 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 7560 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 7561 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 7562 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 7563 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 7564 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 7565 if (!X->isLValue() || !V->isLValue()) { 7566 const Expr *NotLValueExpr = X->isLValue() ? V : X; 7567 ErrorFound = NotAnLValue; 7568 ErrorLoc = AtomicBinOp->getExprLoc(); 7569 ErrorRange = AtomicBinOp->getSourceRange(); 7570 NoteLoc = NotLValueExpr->getExprLoc(); 7571 NoteRange = NotLValueExpr->getSourceRange(); 7572 } 7573 } else if (!X->isInstantiationDependent() || 7574 !V->isInstantiationDependent()) { 7575 const Expr *NotScalarExpr = 7576 (X->isInstantiationDependent() || X->getType()->isScalarType()) 7577 ? V 7578 : X; 7579 ErrorFound = NotAScalarType; 7580 ErrorLoc = AtomicBinOp->getExprLoc(); 7581 ErrorRange = AtomicBinOp->getSourceRange(); 7582 NoteLoc = NotScalarExpr->getExprLoc(); 7583 NoteRange = NotScalarExpr->getSourceRange(); 7584 } 7585 } else if (!AtomicBody->isInstantiationDependent()) { 7586 ErrorFound = NotAnAssignmentOp; 7587 ErrorLoc = AtomicBody->getExprLoc(); 7588 ErrorRange = AtomicBody->getSourceRange(); 7589 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 7590 : AtomicBody->getExprLoc(); 7591 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 7592 : AtomicBody->getSourceRange(); 7593 } 7594 } else { 7595 ErrorFound = NotAnExpression; 7596 NoteLoc = ErrorLoc = Body->getBeginLoc(); 7597 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 7598 } 7599 if (ErrorFound != NoError) { 7600 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 7601 << ErrorRange; 7602 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 7603 << NoteRange; 7604 return StmtError(); 7605 } 7606 if (CurContext->isDependentContext()) 7607 V = X = nullptr; 7608 } else if (AtomicKind == OMPC_write) { 7609 enum { 7610 NotAnExpression, 7611 NotAnAssignmentOp, 7612 NotAScalarType, 7613 NotAnLValue, 7614 NoError 7615 } ErrorFound = NoError; 7616 SourceLocation ErrorLoc, NoteLoc; 7617 SourceRange ErrorRange, NoteRange; 7618 // If clause is write: 7619 // x = expr; 7620 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 7621 const auto *AtomicBinOp = 7622 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 7623 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 7624 X = AtomicBinOp->getLHS(); 7625 E = AtomicBinOp->getRHS(); 7626 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 7627 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 7628 if (!X->isLValue()) { 7629 ErrorFound = NotAnLValue; 7630 ErrorLoc = AtomicBinOp->getExprLoc(); 7631 ErrorRange = AtomicBinOp->getSourceRange(); 7632 NoteLoc = X->getExprLoc(); 7633 NoteRange = X->getSourceRange(); 7634 } 7635 } else if (!X->isInstantiationDependent() || 7636 !E->isInstantiationDependent()) { 7637 const Expr *NotScalarExpr = 7638 (X->isInstantiationDependent() || X->getType()->isScalarType()) 7639 ? E 7640 : X; 7641 ErrorFound = NotAScalarType; 7642 ErrorLoc = AtomicBinOp->getExprLoc(); 7643 ErrorRange = AtomicBinOp->getSourceRange(); 7644 NoteLoc = NotScalarExpr->getExprLoc(); 7645 NoteRange = NotScalarExpr->getSourceRange(); 7646 } 7647 } else if (!AtomicBody->isInstantiationDependent()) { 7648 ErrorFound = NotAnAssignmentOp; 7649 ErrorLoc = AtomicBody->getExprLoc(); 7650 ErrorRange = AtomicBody->getSourceRange(); 7651 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 7652 : AtomicBody->getExprLoc(); 7653 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 7654 : AtomicBody->getSourceRange(); 7655 } 7656 } else { 7657 ErrorFound = NotAnExpression; 7658 NoteLoc = ErrorLoc = Body->getBeginLoc(); 7659 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 7660 } 7661 if (ErrorFound != NoError) { 7662 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 7663 << ErrorRange; 7664 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 7665 << NoteRange; 7666 return StmtError(); 7667 } 7668 if (CurContext->isDependentContext()) 7669 E = X = nullptr; 7670 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 7671 // If clause is update: 7672 // x++; 7673 // x--; 7674 // ++x; 7675 // --x; 7676 // x binop= expr; 7677 // x = x binop expr; 7678 // x = expr binop x; 7679 OpenMPAtomicUpdateChecker Checker(*this); 7680 if (Checker.checkStatement( 7681 Body, (AtomicKind == OMPC_update) 7682 ? diag::err_omp_atomic_update_not_expression_statement 7683 : diag::err_omp_atomic_not_expression_statement, 7684 diag::note_omp_atomic_update)) 7685 return StmtError(); 7686 if (!CurContext->isDependentContext()) { 7687 E = Checker.getExpr(); 7688 X = Checker.getX(); 7689 UE = Checker.getUpdateExpr(); 7690 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 7691 } 7692 } else if (AtomicKind == OMPC_capture) { 7693 enum { 7694 NotAnAssignmentOp, 7695 NotACompoundStatement, 7696 NotTwoSubstatements, 7697 NotASpecificExpression, 7698 NoError 7699 } ErrorFound = NoError; 7700 SourceLocation ErrorLoc, NoteLoc; 7701 SourceRange ErrorRange, NoteRange; 7702 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 7703 // If clause is a capture: 7704 // v = x++; 7705 // v = x--; 7706 // v = ++x; 7707 // v = --x; 7708 // v = x binop= expr; 7709 // v = x = x binop expr; 7710 // v = x = expr binop x; 7711 const auto *AtomicBinOp = 7712 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 7713 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 7714 V = AtomicBinOp->getLHS(); 7715 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 7716 OpenMPAtomicUpdateChecker Checker(*this); 7717 if (Checker.checkStatement( 7718 Body, diag::err_omp_atomic_capture_not_expression_statement, 7719 diag::note_omp_atomic_update)) 7720 return StmtError(); 7721 E = Checker.getExpr(); 7722 X = Checker.getX(); 7723 UE = Checker.getUpdateExpr(); 7724 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 7725 IsPostfixUpdate = Checker.isPostfixUpdate(); 7726 } else if (!AtomicBody->isInstantiationDependent()) { 7727 ErrorLoc = AtomicBody->getExprLoc(); 7728 ErrorRange = AtomicBody->getSourceRange(); 7729 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 7730 : AtomicBody->getExprLoc(); 7731 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 7732 : AtomicBody->getSourceRange(); 7733 ErrorFound = NotAnAssignmentOp; 7734 } 7735 if (ErrorFound != NoError) { 7736 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 7737 << ErrorRange; 7738 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 7739 return StmtError(); 7740 } 7741 if (CurContext->isDependentContext()) 7742 UE = V = E = X = nullptr; 7743 } else { 7744 // If clause is a capture: 7745 // { v = x; x = expr; } 7746 // { v = x; x++; } 7747 // { v = x; x--; } 7748 // { v = x; ++x; } 7749 // { v = x; --x; } 7750 // { v = x; x binop= expr; } 7751 // { v = x; x = x binop expr; } 7752 // { v = x; x = expr binop x; } 7753 // { x++; v = x; } 7754 // { x--; v = x; } 7755 // { ++x; v = x; } 7756 // { --x; v = x; } 7757 // { x binop= expr; v = x; } 7758 // { x = x binop expr; v = x; } 7759 // { x = expr binop x; v = x; } 7760 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 7761 // Check that this is { expr1; expr2; } 7762 if (CS->size() == 2) { 7763 Stmt *First = CS->body_front(); 7764 Stmt *Second = CS->body_back(); 7765 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 7766 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 7767 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 7768 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 7769 // Need to find what subexpression is 'v' and what is 'x'. 7770 OpenMPAtomicUpdateChecker Checker(*this); 7771 bool IsUpdateExprFound = !Checker.checkStatement(Second); 7772 BinaryOperator *BinOp = nullptr; 7773 if (IsUpdateExprFound) { 7774 BinOp = dyn_cast<BinaryOperator>(First); 7775 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 7776 } 7777 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 7778 // { v = x; x++; } 7779 // { v = x; x--; } 7780 // { v = x; ++x; } 7781 // { v = x; --x; } 7782 // { v = x; x binop= expr; } 7783 // { v = x; x = x binop expr; } 7784 // { v = x; x = expr binop x; } 7785 // Check that the first expression has form v = x. 7786 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 7787 llvm::FoldingSetNodeID XId, PossibleXId; 7788 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 7789 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 7790 IsUpdateExprFound = XId == PossibleXId; 7791 if (IsUpdateExprFound) { 7792 V = BinOp->getLHS(); 7793 X = Checker.getX(); 7794 E = Checker.getExpr(); 7795 UE = Checker.getUpdateExpr(); 7796 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 7797 IsPostfixUpdate = true; 7798 } 7799 } 7800 if (!IsUpdateExprFound) { 7801 IsUpdateExprFound = !Checker.checkStatement(First); 7802 BinOp = nullptr; 7803 if (IsUpdateExprFound) { 7804 BinOp = dyn_cast<BinaryOperator>(Second); 7805 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 7806 } 7807 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 7808 // { x++; v = x; } 7809 // { x--; v = x; } 7810 // { ++x; v = x; } 7811 // { --x; v = x; } 7812 // { x binop= expr; v = x; } 7813 // { x = x binop expr; v = x; } 7814 // { x = expr binop x; v = x; } 7815 // Check that the second expression has form v = x. 7816 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 7817 llvm::FoldingSetNodeID XId, PossibleXId; 7818 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 7819 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 7820 IsUpdateExprFound = XId == PossibleXId; 7821 if (IsUpdateExprFound) { 7822 V = BinOp->getLHS(); 7823 X = Checker.getX(); 7824 E = Checker.getExpr(); 7825 UE = Checker.getUpdateExpr(); 7826 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 7827 IsPostfixUpdate = false; 7828 } 7829 } 7830 } 7831 if (!IsUpdateExprFound) { 7832 // { v = x; x = expr; } 7833 auto *FirstExpr = dyn_cast<Expr>(First); 7834 auto *SecondExpr = dyn_cast<Expr>(Second); 7835 if (!FirstExpr || !SecondExpr || 7836 !(FirstExpr->isInstantiationDependent() || 7837 SecondExpr->isInstantiationDependent())) { 7838 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 7839 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 7840 ErrorFound = NotAnAssignmentOp; 7841 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 7842 : First->getBeginLoc(); 7843 NoteRange = ErrorRange = FirstBinOp 7844 ? FirstBinOp->getSourceRange() 7845 : SourceRange(ErrorLoc, ErrorLoc); 7846 } else { 7847 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 7848 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 7849 ErrorFound = NotAnAssignmentOp; 7850 NoteLoc = ErrorLoc = SecondBinOp 7851 ? SecondBinOp->getOperatorLoc() 7852 : Second->getBeginLoc(); 7853 NoteRange = ErrorRange = 7854 SecondBinOp ? SecondBinOp->getSourceRange() 7855 : SourceRange(ErrorLoc, ErrorLoc); 7856 } else { 7857 Expr *PossibleXRHSInFirst = 7858 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 7859 Expr *PossibleXLHSInSecond = 7860 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 7861 llvm::FoldingSetNodeID X1Id, X2Id; 7862 PossibleXRHSInFirst->Profile(X1Id, Context, 7863 /*Canonical=*/true); 7864 PossibleXLHSInSecond->Profile(X2Id, Context, 7865 /*Canonical=*/true); 7866 IsUpdateExprFound = X1Id == X2Id; 7867 if (IsUpdateExprFound) { 7868 V = FirstBinOp->getLHS(); 7869 X = SecondBinOp->getLHS(); 7870 E = SecondBinOp->getRHS(); 7871 UE = nullptr; 7872 IsXLHSInRHSPart = false; 7873 IsPostfixUpdate = true; 7874 } else { 7875 ErrorFound = NotASpecificExpression; 7876 ErrorLoc = FirstBinOp->getExprLoc(); 7877 ErrorRange = FirstBinOp->getSourceRange(); 7878 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 7879 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 7880 } 7881 } 7882 } 7883 } 7884 } 7885 } else { 7886 NoteLoc = ErrorLoc = Body->getBeginLoc(); 7887 NoteRange = ErrorRange = 7888 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 7889 ErrorFound = NotTwoSubstatements; 7890 } 7891 } else { 7892 NoteLoc = ErrorLoc = Body->getBeginLoc(); 7893 NoteRange = ErrorRange = 7894 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 7895 ErrorFound = NotACompoundStatement; 7896 } 7897 if (ErrorFound != NoError) { 7898 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 7899 << ErrorRange; 7900 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 7901 return StmtError(); 7902 } 7903 if (CurContext->isDependentContext()) 7904 UE = V = E = X = nullptr; 7905 } 7906 } 7907 7908 setFunctionHasBranchProtectedScope(); 7909 7910 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 7911 X, V, E, UE, IsXLHSInRHSPart, 7912 IsPostfixUpdate); 7913 } 7914 7915 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 7916 Stmt *AStmt, 7917 SourceLocation StartLoc, 7918 SourceLocation EndLoc) { 7919 if (!AStmt) 7920 return StmtError(); 7921 7922 auto *CS = cast<CapturedStmt>(AStmt); 7923 // 1.2.2 OpenMP Language Terminology 7924 // Structured block - An executable statement with a single entry at the 7925 // top and a single exit at the bottom. 7926 // The point of exit cannot be a branch out of the structured block. 7927 // longjmp() and throw() must not violate the entry/exit criteria. 7928 CS->getCapturedDecl()->setNothrow(); 7929 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 7930 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7931 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7932 // 1.2.2 OpenMP Language Terminology 7933 // Structured block - An executable statement with a single entry at the 7934 // top and a single exit at the bottom. 7935 // The point of exit cannot be a branch out of the structured block. 7936 // longjmp() and throw() must not violate the entry/exit criteria. 7937 CS->getCapturedDecl()->setNothrow(); 7938 } 7939 7940 // OpenMP [2.16, Nesting of Regions] 7941 // If specified, a teams construct must be contained within a target 7942 // construct. That target construct must contain no statements or directives 7943 // outside of the teams construct. 7944 if (DSAStack->hasInnerTeamsRegion()) { 7945 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 7946 bool OMPTeamsFound = true; 7947 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 7948 auto I = CS->body_begin(); 7949 while (I != CS->body_end()) { 7950 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 7951 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) || 7952 OMPTeamsFound) { 7953 7954 OMPTeamsFound = false; 7955 break; 7956 } 7957 ++I; 7958 } 7959 assert(I != CS->body_end() && "Not found statement"); 7960 S = *I; 7961 } else { 7962 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 7963 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 7964 } 7965 if (!OMPTeamsFound) { 7966 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 7967 Diag(DSAStack->getInnerTeamsRegionLoc(), 7968 diag::note_omp_nested_teams_construct_here); 7969 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here) 7970 << isa<OMPExecutableDirective>(S); 7971 return StmtError(); 7972 } 7973 } 7974 7975 setFunctionHasBranchProtectedScope(); 7976 7977 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 7978 } 7979 7980 StmtResult 7981 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 7982 Stmt *AStmt, SourceLocation StartLoc, 7983 SourceLocation EndLoc) { 7984 if (!AStmt) 7985 return StmtError(); 7986 7987 auto *CS = cast<CapturedStmt>(AStmt); 7988 // 1.2.2 OpenMP Language Terminology 7989 // Structured block - An executable statement with a single entry at the 7990 // top and a single exit at the bottom. 7991 // The point of exit cannot be a branch out of the structured block. 7992 // longjmp() and throw() must not violate the entry/exit criteria. 7993 CS->getCapturedDecl()->setNothrow(); 7994 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 7995 ThisCaptureLevel > 1; --ThisCaptureLevel) { 7996 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 7997 // 1.2.2 OpenMP Language Terminology 7998 // Structured block - An executable statement with a single entry at the 7999 // top and a single exit at the bottom. 8000 // The point of exit cannot be a branch out of the structured block. 8001 // longjmp() and throw() must not violate the entry/exit criteria. 8002 CS->getCapturedDecl()->setNothrow(); 8003 } 8004 8005 setFunctionHasBranchProtectedScope(); 8006 8007 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, 8008 AStmt); 8009 } 8010 8011 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 8012 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8013 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8014 if (!AStmt) 8015 return StmtError(); 8016 8017 auto *CS = cast<CapturedStmt>(AStmt); 8018 // 1.2.2 OpenMP Language Terminology 8019 // Structured block - An executable statement with a single entry at the 8020 // top and a single exit at the bottom. 8021 // The point of exit cannot be a branch out of the structured block. 8022 // longjmp() and throw() must not violate the entry/exit criteria. 8023 CS->getCapturedDecl()->setNothrow(); 8024 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 8025 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8026 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8027 // 1.2.2 OpenMP Language Terminology 8028 // Structured block - An executable statement with a single entry at the 8029 // top and a single exit at the bottom. 8030 // The point of exit cannot be a branch out of the structured block. 8031 // longjmp() and throw() must not violate the entry/exit criteria. 8032 CS->getCapturedDecl()->setNothrow(); 8033 } 8034 8035 OMPLoopDirective::HelperExprs B; 8036 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8037 // define the nested loops number. 8038 unsigned NestedLoopCount = 8039 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 8040 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 8041 VarsWithImplicitDSA, B); 8042 if (NestedLoopCount == 0) 8043 return StmtError(); 8044 8045 assert((CurContext->isDependentContext() || B.builtAll()) && 8046 "omp target parallel for loop exprs were not built"); 8047 8048 if (!CurContext->isDependentContext()) { 8049 // Finalize the clauses that need pre-built expressions for CodeGen. 8050 for (OMPClause *C : Clauses) { 8051 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8052 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8053 B.NumIterations, *this, CurScope, 8054 DSAStack)) 8055 return StmtError(); 8056 } 8057 } 8058 8059 setFunctionHasBranchProtectedScope(); 8060 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc, 8061 NestedLoopCount, Clauses, AStmt, 8062 B, DSAStack->isCancelRegion()); 8063 } 8064 8065 /// Check for existence of a map clause in the list of clauses. 8066 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 8067 const OpenMPClauseKind K) { 8068 return llvm::any_of( 8069 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 8070 } 8071 8072 template <typename... Params> 8073 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 8074 const Params... ClauseTypes) { 8075 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 8076 } 8077 8078 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 8079 Stmt *AStmt, 8080 SourceLocation StartLoc, 8081 SourceLocation EndLoc) { 8082 if (!AStmt) 8083 return StmtError(); 8084 8085 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8086 8087 // OpenMP [2.10.1, Restrictions, p. 97] 8088 // At least one map clause must appear on the directive. 8089 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) { 8090 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 8091 << "'map' or 'use_device_ptr'" 8092 << getOpenMPDirectiveName(OMPD_target_data); 8093 return StmtError(); 8094 } 8095 8096 setFunctionHasBranchProtectedScope(); 8097 8098 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 8099 AStmt); 8100 } 8101 8102 StmtResult 8103 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 8104 SourceLocation StartLoc, 8105 SourceLocation EndLoc, Stmt *AStmt) { 8106 if (!AStmt) 8107 return StmtError(); 8108 8109 auto *CS = cast<CapturedStmt>(AStmt); 8110 // 1.2.2 OpenMP Language Terminology 8111 // Structured block - An executable statement with a single entry at the 8112 // top and a single exit at the bottom. 8113 // The point of exit cannot be a branch out of the structured block. 8114 // longjmp() and throw() must not violate the entry/exit criteria. 8115 CS->getCapturedDecl()->setNothrow(); 8116 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 8117 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8118 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8119 // 1.2.2 OpenMP Language Terminology 8120 // Structured block - An executable statement with a single entry at the 8121 // top and a single exit at the bottom. 8122 // The point of exit cannot be a branch out of the structured block. 8123 // longjmp() and throw() must not violate the entry/exit criteria. 8124 CS->getCapturedDecl()->setNothrow(); 8125 } 8126 8127 // OpenMP [2.10.2, Restrictions, p. 99] 8128 // At least one map clause must appear on the directive. 8129 if (!hasClauses(Clauses, OMPC_map)) { 8130 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 8131 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 8132 return StmtError(); 8133 } 8134 8135 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 8136 AStmt); 8137 } 8138 8139 StmtResult 8140 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 8141 SourceLocation StartLoc, 8142 SourceLocation EndLoc, Stmt *AStmt) { 8143 if (!AStmt) 8144 return StmtError(); 8145 8146 auto *CS = cast<CapturedStmt>(AStmt); 8147 // 1.2.2 OpenMP Language Terminology 8148 // Structured block - An executable statement with a single entry at the 8149 // top and a single exit at the bottom. 8150 // The point of exit cannot be a branch out of the structured block. 8151 // longjmp() and throw() must not violate the entry/exit criteria. 8152 CS->getCapturedDecl()->setNothrow(); 8153 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 8154 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8155 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 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 } 8163 8164 // OpenMP [2.10.3, Restrictions, p. 102] 8165 // At least one map clause must appear on the directive. 8166 if (!hasClauses(Clauses, OMPC_map)) { 8167 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 8168 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 8169 return StmtError(); 8170 } 8171 8172 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 8173 AStmt); 8174 } 8175 8176 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 8177 SourceLocation StartLoc, 8178 SourceLocation EndLoc, 8179 Stmt *AStmt) { 8180 if (!AStmt) 8181 return StmtError(); 8182 8183 auto *CS = cast<CapturedStmt>(AStmt); 8184 // 1.2.2 OpenMP Language Terminology 8185 // Structured block - An executable statement with a single entry at the 8186 // top and a single exit at the bottom. 8187 // The point of exit cannot be a branch out of the structured block. 8188 // longjmp() and throw() must not violate the entry/exit criteria. 8189 CS->getCapturedDecl()->setNothrow(); 8190 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 8191 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8192 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8193 // 1.2.2 OpenMP Language Terminology 8194 // Structured block - An executable statement with a single entry at the 8195 // top and a single exit at the bottom. 8196 // The point of exit cannot be a branch out of the structured block. 8197 // longjmp() and throw() must not violate the entry/exit criteria. 8198 CS->getCapturedDecl()->setNothrow(); 8199 } 8200 8201 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 8202 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 8203 return StmtError(); 8204 } 8205 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 8206 AStmt); 8207 } 8208 8209 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 8210 Stmt *AStmt, SourceLocation StartLoc, 8211 SourceLocation EndLoc) { 8212 if (!AStmt) 8213 return StmtError(); 8214 8215 auto *CS = cast<CapturedStmt>(AStmt); 8216 // 1.2.2 OpenMP Language Terminology 8217 // Structured block - An executable statement with a single entry at the 8218 // top and a single exit at the bottom. 8219 // The point of exit cannot be a branch out of the structured block. 8220 // longjmp() and throw() must not violate the entry/exit criteria. 8221 CS->getCapturedDecl()->setNothrow(); 8222 8223 setFunctionHasBranchProtectedScope(); 8224 8225 DSAStack->setParentTeamsRegionLoc(StartLoc); 8226 8227 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 8228 } 8229 8230 StmtResult 8231 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 8232 SourceLocation EndLoc, 8233 OpenMPDirectiveKind CancelRegion) { 8234 if (DSAStack->isParentNowaitRegion()) { 8235 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 8236 return StmtError(); 8237 } 8238 if (DSAStack->isParentOrderedRegion()) { 8239 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 8240 return StmtError(); 8241 } 8242 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 8243 CancelRegion); 8244 } 8245 8246 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 8247 SourceLocation StartLoc, 8248 SourceLocation EndLoc, 8249 OpenMPDirectiveKind CancelRegion) { 8250 if (DSAStack->isParentNowaitRegion()) { 8251 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 8252 return StmtError(); 8253 } 8254 if (DSAStack->isParentOrderedRegion()) { 8255 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 8256 return StmtError(); 8257 } 8258 DSAStack->setParentCancelRegion(/*Cancel=*/true); 8259 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 8260 CancelRegion); 8261 } 8262 8263 static bool checkGrainsizeNumTasksClauses(Sema &S, 8264 ArrayRef<OMPClause *> Clauses) { 8265 const OMPClause *PrevClause = nullptr; 8266 bool ErrorFound = false; 8267 for (const OMPClause *C : Clauses) { 8268 if (C->getClauseKind() == OMPC_grainsize || 8269 C->getClauseKind() == OMPC_num_tasks) { 8270 if (!PrevClause) 8271 PrevClause = C; 8272 else if (PrevClause->getClauseKind() != C->getClauseKind()) { 8273 S.Diag(C->getBeginLoc(), 8274 diag::err_omp_grainsize_num_tasks_mutually_exclusive) 8275 << getOpenMPClauseName(C->getClauseKind()) 8276 << getOpenMPClauseName(PrevClause->getClauseKind()); 8277 S.Diag(PrevClause->getBeginLoc(), 8278 diag::note_omp_previous_grainsize_num_tasks) 8279 << getOpenMPClauseName(PrevClause->getClauseKind()); 8280 ErrorFound = true; 8281 } 8282 } 8283 } 8284 return ErrorFound; 8285 } 8286 8287 static bool checkReductionClauseWithNogroup(Sema &S, 8288 ArrayRef<OMPClause *> Clauses) { 8289 const OMPClause *ReductionClause = nullptr; 8290 const OMPClause *NogroupClause = nullptr; 8291 for (const OMPClause *C : Clauses) { 8292 if (C->getClauseKind() == OMPC_reduction) { 8293 ReductionClause = C; 8294 if (NogroupClause) 8295 break; 8296 continue; 8297 } 8298 if (C->getClauseKind() == OMPC_nogroup) { 8299 NogroupClause = C; 8300 if (ReductionClause) 8301 break; 8302 continue; 8303 } 8304 } 8305 if (ReductionClause && NogroupClause) { 8306 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup) 8307 << SourceRange(NogroupClause->getBeginLoc(), 8308 NogroupClause->getEndLoc()); 8309 return true; 8310 } 8311 return false; 8312 } 8313 8314 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 8315 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8316 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8317 if (!AStmt) 8318 return StmtError(); 8319 8320 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8321 OMPLoopDirective::HelperExprs B; 8322 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8323 // define the nested loops number. 8324 unsigned NestedLoopCount = 8325 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 8326 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 8327 VarsWithImplicitDSA, B); 8328 if (NestedLoopCount == 0) 8329 return StmtError(); 8330 8331 assert((CurContext->isDependentContext() || B.builtAll()) && 8332 "omp for loop exprs were not built"); 8333 8334 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 8335 // The grainsize clause and num_tasks clause are mutually exclusive and may 8336 // not appear on the same taskloop directive. 8337 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 8338 return StmtError(); 8339 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 8340 // If a reduction clause is present on the taskloop directive, the nogroup 8341 // clause must not be specified. 8342 if (checkReductionClauseWithNogroup(*this, Clauses)) 8343 return StmtError(); 8344 8345 setFunctionHasBranchProtectedScope(); 8346 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 8347 NestedLoopCount, Clauses, AStmt, B); 8348 } 8349 8350 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 8351 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8352 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8353 if (!AStmt) 8354 return StmtError(); 8355 8356 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8357 OMPLoopDirective::HelperExprs B; 8358 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8359 // define the nested loops number. 8360 unsigned NestedLoopCount = 8361 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 8362 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 8363 VarsWithImplicitDSA, B); 8364 if (NestedLoopCount == 0) 8365 return StmtError(); 8366 8367 assert((CurContext->isDependentContext() || B.builtAll()) && 8368 "omp for loop exprs were not built"); 8369 8370 if (!CurContext->isDependentContext()) { 8371 // Finalize the clauses that need pre-built expressions for CodeGen. 8372 for (OMPClause *C : Clauses) { 8373 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8374 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8375 B.NumIterations, *this, CurScope, 8376 DSAStack)) 8377 return StmtError(); 8378 } 8379 } 8380 8381 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 8382 // The grainsize clause and num_tasks clause are mutually exclusive and may 8383 // not appear on the same taskloop directive. 8384 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 8385 return StmtError(); 8386 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 8387 // If a reduction clause is present on the taskloop directive, the nogroup 8388 // clause must not be specified. 8389 if (checkReductionClauseWithNogroup(*this, Clauses)) 8390 return StmtError(); 8391 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8392 return StmtError(); 8393 8394 setFunctionHasBranchProtectedScope(); 8395 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 8396 NestedLoopCount, Clauses, AStmt, B); 8397 } 8398 8399 StmtResult Sema::ActOnOpenMPDistributeDirective( 8400 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8401 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8402 if (!AStmt) 8403 return StmtError(); 8404 8405 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8406 OMPLoopDirective::HelperExprs B; 8407 // In presence of clause 'collapse' with number of loops, it will 8408 // define the nested loops number. 8409 unsigned NestedLoopCount = 8410 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 8411 nullptr /*ordered not a clause on distribute*/, AStmt, 8412 *this, *DSAStack, VarsWithImplicitDSA, B); 8413 if (NestedLoopCount == 0) 8414 return StmtError(); 8415 8416 assert((CurContext->isDependentContext() || B.builtAll()) && 8417 "omp for loop exprs were not built"); 8418 8419 setFunctionHasBranchProtectedScope(); 8420 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 8421 NestedLoopCount, Clauses, AStmt, B); 8422 } 8423 8424 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 8425 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8426 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8427 if (!AStmt) 8428 return StmtError(); 8429 8430 auto *CS = cast<CapturedStmt>(AStmt); 8431 // 1.2.2 OpenMP Language Terminology 8432 // Structured block - An executable statement with a single entry at the 8433 // top and a single exit at the bottom. 8434 // The point of exit cannot be a branch out of the structured block. 8435 // longjmp() and throw() must not violate the entry/exit criteria. 8436 CS->getCapturedDecl()->setNothrow(); 8437 for (int ThisCaptureLevel = 8438 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 8439 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8440 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8441 // 1.2.2 OpenMP Language Terminology 8442 // Structured block - An executable statement with a single entry at the 8443 // top and a single exit at the bottom. 8444 // The point of exit cannot be a branch out of the structured block. 8445 // longjmp() and throw() must not violate the entry/exit criteria. 8446 CS->getCapturedDecl()->setNothrow(); 8447 } 8448 8449 OMPLoopDirective::HelperExprs B; 8450 // In presence of clause 'collapse' with number of loops, it will 8451 // define the nested loops number. 8452 unsigned NestedLoopCount = checkOpenMPLoop( 8453 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 8454 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8455 VarsWithImplicitDSA, B); 8456 if (NestedLoopCount == 0) 8457 return StmtError(); 8458 8459 assert((CurContext->isDependentContext() || B.builtAll()) && 8460 "omp for loop exprs were not built"); 8461 8462 setFunctionHasBranchProtectedScope(); 8463 return OMPDistributeParallelForDirective::Create( 8464 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 8465 DSAStack->isCancelRegion()); 8466 } 8467 8468 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 8469 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8470 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8471 if (!AStmt) 8472 return StmtError(); 8473 8474 auto *CS = cast<CapturedStmt>(AStmt); 8475 // 1.2.2 OpenMP Language Terminology 8476 // Structured block - An executable statement with a single entry at the 8477 // top and a single exit at the bottom. 8478 // The point of exit cannot be a branch out of the structured block. 8479 // longjmp() and throw() must not violate the entry/exit criteria. 8480 CS->getCapturedDecl()->setNothrow(); 8481 for (int ThisCaptureLevel = 8482 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 8483 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8484 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8485 // 1.2.2 OpenMP Language Terminology 8486 // Structured block - An executable statement with a single entry at the 8487 // top and a single exit at the bottom. 8488 // The point of exit cannot be a branch out of the structured block. 8489 // longjmp() and throw() must not violate the entry/exit criteria. 8490 CS->getCapturedDecl()->setNothrow(); 8491 } 8492 8493 OMPLoopDirective::HelperExprs B; 8494 // In presence of clause 'collapse' with number of loops, it will 8495 // define the nested loops number. 8496 unsigned NestedLoopCount = checkOpenMPLoop( 8497 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 8498 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8499 VarsWithImplicitDSA, B); 8500 if (NestedLoopCount == 0) 8501 return StmtError(); 8502 8503 assert((CurContext->isDependentContext() || B.builtAll()) && 8504 "omp for loop exprs were not built"); 8505 8506 if (!CurContext->isDependentContext()) { 8507 // Finalize the clauses that need pre-built expressions for CodeGen. 8508 for (OMPClause *C : Clauses) { 8509 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8510 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8511 B.NumIterations, *this, CurScope, 8512 DSAStack)) 8513 return StmtError(); 8514 } 8515 } 8516 8517 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8518 return StmtError(); 8519 8520 setFunctionHasBranchProtectedScope(); 8521 return OMPDistributeParallelForSimdDirective::Create( 8522 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8523 } 8524 8525 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 8526 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8527 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8528 if (!AStmt) 8529 return StmtError(); 8530 8531 auto *CS = cast<CapturedStmt>(AStmt); 8532 // 1.2.2 OpenMP Language Terminology 8533 // Structured block - An executable statement with a single entry at the 8534 // top and a single exit at the bottom. 8535 // The point of exit cannot be a branch out of the structured block. 8536 // longjmp() and throw() must not violate the entry/exit criteria. 8537 CS->getCapturedDecl()->setNothrow(); 8538 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 8539 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8540 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8541 // 1.2.2 OpenMP Language Terminology 8542 // Structured block - An executable statement with a single entry at the 8543 // top and a single exit at the bottom. 8544 // The point of exit cannot be a branch out of the structured block. 8545 // longjmp() and throw() must not violate the entry/exit criteria. 8546 CS->getCapturedDecl()->setNothrow(); 8547 } 8548 8549 OMPLoopDirective::HelperExprs B; 8550 // In presence of clause 'collapse' with number of loops, it will 8551 // define the nested loops number. 8552 unsigned NestedLoopCount = 8553 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 8554 nullptr /*ordered not a clause on distribute*/, CS, *this, 8555 *DSAStack, VarsWithImplicitDSA, B); 8556 if (NestedLoopCount == 0) 8557 return StmtError(); 8558 8559 assert((CurContext->isDependentContext() || B.builtAll()) && 8560 "omp for loop exprs were not built"); 8561 8562 if (!CurContext->isDependentContext()) { 8563 // Finalize the clauses that need pre-built expressions for CodeGen. 8564 for (OMPClause *C : Clauses) { 8565 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8566 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8567 B.NumIterations, *this, CurScope, 8568 DSAStack)) 8569 return StmtError(); 8570 } 8571 } 8572 8573 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8574 return StmtError(); 8575 8576 setFunctionHasBranchProtectedScope(); 8577 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 8578 NestedLoopCount, Clauses, AStmt, B); 8579 } 8580 8581 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 8582 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8583 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8584 if (!AStmt) 8585 return StmtError(); 8586 8587 auto *CS = cast<CapturedStmt>(AStmt); 8588 // 1.2.2 OpenMP Language Terminology 8589 // Structured block - An executable statement with a single entry at the 8590 // top and a single exit at the bottom. 8591 // The point of exit cannot be a branch out of the structured block. 8592 // longjmp() and throw() must not violate the entry/exit criteria. 8593 CS->getCapturedDecl()->setNothrow(); 8594 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 8595 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8596 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8597 // 1.2.2 OpenMP Language Terminology 8598 // Structured block - An executable statement with a single entry at the 8599 // top and a single exit at the bottom. 8600 // The point of exit cannot be a branch out of the structured block. 8601 // longjmp() and throw() must not violate the entry/exit criteria. 8602 CS->getCapturedDecl()->setNothrow(); 8603 } 8604 8605 OMPLoopDirective::HelperExprs B; 8606 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8607 // define the nested loops number. 8608 unsigned NestedLoopCount = checkOpenMPLoop( 8609 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 8610 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 8611 VarsWithImplicitDSA, B); 8612 if (NestedLoopCount == 0) 8613 return StmtError(); 8614 8615 assert((CurContext->isDependentContext() || B.builtAll()) && 8616 "omp target parallel for simd loop exprs were not built"); 8617 8618 if (!CurContext->isDependentContext()) { 8619 // Finalize the clauses that need pre-built expressions for CodeGen. 8620 for (OMPClause *C : Clauses) { 8621 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8622 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8623 B.NumIterations, *this, CurScope, 8624 DSAStack)) 8625 return StmtError(); 8626 } 8627 } 8628 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8629 return StmtError(); 8630 8631 setFunctionHasBranchProtectedScope(); 8632 return OMPTargetParallelForSimdDirective::Create( 8633 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8634 } 8635 8636 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 8637 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8638 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 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 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 8650 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8651 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8652 // 1.2.2 OpenMP Language Terminology 8653 // Structured block - An executable statement with a single entry at the 8654 // top and a single exit at the bottom. 8655 // The point of exit cannot be a branch out of the structured block. 8656 // longjmp() and throw() must not violate the entry/exit criteria. 8657 CS->getCapturedDecl()->setNothrow(); 8658 } 8659 8660 OMPLoopDirective::HelperExprs B; 8661 // In presence of clause 'collapse' with number of loops, it will define the 8662 // nested loops number. 8663 unsigned NestedLoopCount = 8664 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 8665 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 8666 VarsWithImplicitDSA, B); 8667 if (NestedLoopCount == 0) 8668 return StmtError(); 8669 8670 assert((CurContext->isDependentContext() || B.builtAll()) && 8671 "omp target simd loop exprs were not built"); 8672 8673 if (!CurContext->isDependentContext()) { 8674 // Finalize the clauses that need pre-built expressions for CodeGen. 8675 for (OMPClause *C : Clauses) { 8676 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8677 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8678 B.NumIterations, *this, CurScope, 8679 DSAStack)) 8680 return StmtError(); 8681 } 8682 } 8683 8684 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8685 return StmtError(); 8686 8687 setFunctionHasBranchProtectedScope(); 8688 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 8689 NestedLoopCount, Clauses, AStmt, B); 8690 } 8691 8692 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 8693 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8694 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8695 if (!AStmt) 8696 return StmtError(); 8697 8698 auto *CS = cast<CapturedStmt>(AStmt); 8699 // 1.2.2 OpenMP Language Terminology 8700 // Structured block - An executable statement with a single entry at the 8701 // top and a single exit at the bottom. 8702 // The point of exit cannot be a branch out of the structured block. 8703 // longjmp() and throw() must not violate the entry/exit criteria. 8704 CS->getCapturedDecl()->setNothrow(); 8705 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 8706 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8707 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8708 // 1.2.2 OpenMP Language Terminology 8709 // Structured block - An executable statement with a single entry at the 8710 // top and a single exit at the bottom. 8711 // The point of exit cannot be a branch out of the structured block. 8712 // longjmp() and throw() must not violate the entry/exit criteria. 8713 CS->getCapturedDecl()->setNothrow(); 8714 } 8715 8716 OMPLoopDirective::HelperExprs B; 8717 // In presence of clause 'collapse' with number of loops, it will 8718 // define the nested loops number. 8719 unsigned NestedLoopCount = 8720 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 8721 nullptr /*ordered not a clause on distribute*/, CS, *this, 8722 *DSAStack, VarsWithImplicitDSA, B); 8723 if (NestedLoopCount == 0) 8724 return StmtError(); 8725 8726 assert((CurContext->isDependentContext() || B.builtAll()) && 8727 "omp teams distribute loop exprs were not built"); 8728 8729 setFunctionHasBranchProtectedScope(); 8730 8731 DSAStack->setParentTeamsRegionLoc(StartLoc); 8732 8733 return OMPTeamsDistributeDirective::Create( 8734 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8735 } 8736 8737 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 8738 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8739 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8740 if (!AStmt) 8741 return StmtError(); 8742 8743 auto *CS = cast<CapturedStmt>(AStmt); 8744 // 1.2.2 OpenMP Language Terminology 8745 // Structured block - An executable statement with a single entry at the 8746 // top and a single exit at the bottom. 8747 // The point of exit cannot be a branch out of the structured block. 8748 // longjmp() and throw() must not violate the entry/exit criteria. 8749 CS->getCapturedDecl()->setNothrow(); 8750 for (int ThisCaptureLevel = 8751 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 8752 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8753 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8754 // 1.2.2 OpenMP Language Terminology 8755 // Structured block - An executable statement with a single entry at the 8756 // top and a single exit at the bottom. 8757 // The point of exit cannot be a branch out of the structured block. 8758 // longjmp() and throw() must not violate the entry/exit criteria. 8759 CS->getCapturedDecl()->setNothrow(); 8760 } 8761 8762 8763 OMPLoopDirective::HelperExprs B; 8764 // In presence of clause 'collapse' with number of loops, it will 8765 // define the nested loops number. 8766 unsigned NestedLoopCount = checkOpenMPLoop( 8767 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 8768 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8769 VarsWithImplicitDSA, B); 8770 8771 if (NestedLoopCount == 0) 8772 return StmtError(); 8773 8774 assert((CurContext->isDependentContext() || B.builtAll()) && 8775 "omp teams distribute simd loop exprs were not built"); 8776 8777 if (!CurContext->isDependentContext()) { 8778 // Finalize the clauses that need pre-built expressions for CodeGen. 8779 for (OMPClause *C : Clauses) { 8780 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8781 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8782 B.NumIterations, *this, CurScope, 8783 DSAStack)) 8784 return StmtError(); 8785 } 8786 } 8787 8788 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8789 return StmtError(); 8790 8791 setFunctionHasBranchProtectedScope(); 8792 8793 DSAStack->setParentTeamsRegionLoc(StartLoc); 8794 8795 return OMPTeamsDistributeSimdDirective::Create( 8796 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8797 } 8798 8799 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 8800 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8801 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8802 if (!AStmt) 8803 return StmtError(); 8804 8805 auto *CS = cast<CapturedStmt>(AStmt); 8806 // 1.2.2 OpenMP Language Terminology 8807 // Structured block - An executable statement with a single entry at the 8808 // top and a single exit at the bottom. 8809 // The point of exit cannot be a branch out of the structured block. 8810 // longjmp() and throw() must not violate the entry/exit criteria. 8811 CS->getCapturedDecl()->setNothrow(); 8812 8813 for (int ThisCaptureLevel = 8814 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 8815 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8816 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8817 // 1.2.2 OpenMP Language Terminology 8818 // Structured block - An executable statement with a single entry at the 8819 // top and a single exit at the bottom. 8820 // The point of exit cannot be a branch out of the structured block. 8821 // longjmp() and throw() must not violate the entry/exit criteria. 8822 CS->getCapturedDecl()->setNothrow(); 8823 } 8824 8825 OMPLoopDirective::HelperExprs B; 8826 // In presence of clause 'collapse' with number of loops, it will 8827 // define the nested loops number. 8828 unsigned NestedLoopCount = checkOpenMPLoop( 8829 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 8830 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8831 VarsWithImplicitDSA, B); 8832 8833 if (NestedLoopCount == 0) 8834 return StmtError(); 8835 8836 assert((CurContext->isDependentContext() || B.builtAll()) && 8837 "omp for loop exprs were not built"); 8838 8839 if (!CurContext->isDependentContext()) { 8840 // Finalize the clauses that need pre-built expressions for CodeGen. 8841 for (OMPClause *C : Clauses) { 8842 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8843 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8844 B.NumIterations, *this, CurScope, 8845 DSAStack)) 8846 return StmtError(); 8847 } 8848 } 8849 8850 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8851 return StmtError(); 8852 8853 setFunctionHasBranchProtectedScope(); 8854 8855 DSAStack->setParentTeamsRegionLoc(StartLoc); 8856 8857 return OMPTeamsDistributeParallelForSimdDirective::Create( 8858 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8859 } 8860 8861 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 8862 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8863 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8864 if (!AStmt) 8865 return StmtError(); 8866 8867 auto *CS = cast<CapturedStmt>(AStmt); 8868 // 1.2.2 OpenMP Language Terminology 8869 // Structured block - An executable statement with a single entry at the 8870 // top and a single exit at the bottom. 8871 // The point of exit cannot be a branch out of the structured block. 8872 // longjmp() and throw() must not violate the entry/exit criteria. 8873 CS->getCapturedDecl()->setNothrow(); 8874 8875 for (int ThisCaptureLevel = 8876 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 8877 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8878 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8879 // 1.2.2 OpenMP Language Terminology 8880 // Structured block - An executable statement with a single entry at the 8881 // top and a single exit at the bottom. 8882 // The point of exit cannot be a branch out of the structured block. 8883 // longjmp() and throw() must not violate the entry/exit criteria. 8884 CS->getCapturedDecl()->setNothrow(); 8885 } 8886 8887 OMPLoopDirective::HelperExprs B; 8888 // In presence of clause 'collapse' with number of loops, it will 8889 // define the nested loops number. 8890 unsigned NestedLoopCount = checkOpenMPLoop( 8891 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 8892 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8893 VarsWithImplicitDSA, B); 8894 8895 if (NestedLoopCount == 0) 8896 return StmtError(); 8897 8898 assert((CurContext->isDependentContext() || B.builtAll()) && 8899 "omp for loop exprs were not built"); 8900 8901 setFunctionHasBranchProtectedScope(); 8902 8903 DSAStack->setParentTeamsRegionLoc(StartLoc); 8904 8905 return OMPTeamsDistributeParallelForDirective::Create( 8906 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 8907 DSAStack->isCancelRegion()); 8908 } 8909 8910 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 8911 Stmt *AStmt, 8912 SourceLocation StartLoc, 8913 SourceLocation EndLoc) { 8914 if (!AStmt) 8915 return StmtError(); 8916 8917 auto *CS = cast<CapturedStmt>(AStmt); 8918 // 1.2.2 OpenMP Language Terminology 8919 // Structured block - An executable statement with a single entry at the 8920 // top and a single exit at the bottom. 8921 // The point of exit cannot be a branch out of the structured block. 8922 // longjmp() and throw() must not violate the entry/exit criteria. 8923 CS->getCapturedDecl()->setNothrow(); 8924 8925 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 8926 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8927 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8928 // 1.2.2 OpenMP Language Terminology 8929 // Structured block - An executable statement with a single entry at the 8930 // top and a single exit at the bottom. 8931 // The point of exit cannot be a branch out of the structured block. 8932 // longjmp() and throw() must not violate the entry/exit criteria. 8933 CS->getCapturedDecl()->setNothrow(); 8934 } 8935 setFunctionHasBranchProtectedScope(); 8936 8937 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 8938 AStmt); 8939 } 8940 8941 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 8942 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8943 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8944 if (!AStmt) 8945 return StmtError(); 8946 8947 auto *CS = cast<CapturedStmt>(AStmt); 8948 // 1.2.2 OpenMP Language Terminology 8949 // Structured block - An executable statement with a single entry at the 8950 // top and a single exit at the bottom. 8951 // The point of exit cannot be a branch out of the structured block. 8952 // longjmp() and throw() must not violate the entry/exit criteria. 8953 CS->getCapturedDecl()->setNothrow(); 8954 for (int ThisCaptureLevel = 8955 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 8956 ThisCaptureLevel > 1; --ThisCaptureLevel) { 8957 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 8958 // 1.2.2 OpenMP Language Terminology 8959 // Structured block - An executable statement with a single entry at the 8960 // top and a single exit at the bottom. 8961 // The point of exit cannot be a branch out of the structured block. 8962 // longjmp() and throw() must not violate the entry/exit criteria. 8963 CS->getCapturedDecl()->setNothrow(); 8964 } 8965 8966 OMPLoopDirective::HelperExprs B; 8967 // In presence of clause 'collapse' with number of loops, it will 8968 // define the nested loops number. 8969 unsigned NestedLoopCount = checkOpenMPLoop( 8970 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 8971 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 8972 VarsWithImplicitDSA, B); 8973 if (NestedLoopCount == 0) 8974 return StmtError(); 8975 8976 assert((CurContext->isDependentContext() || B.builtAll()) && 8977 "omp target teams distribute loop exprs were not built"); 8978 8979 setFunctionHasBranchProtectedScope(); 8980 return OMPTargetTeamsDistributeDirective::Create( 8981 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 8982 } 8983 8984 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 8985 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8986 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8987 if (!AStmt) 8988 return StmtError(); 8989 8990 auto *CS = cast<CapturedStmt>(AStmt); 8991 // 1.2.2 OpenMP Language Terminology 8992 // Structured block - An executable statement with a single entry at the 8993 // top and a single exit at the bottom. 8994 // The point of exit cannot be a branch out of the structured block. 8995 // longjmp() and throw() must not violate the entry/exit criteria. 8996 CS->getCapturedDecl()->setNothrow(); 8997 for (int ThisCaptureLevel = 8998 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 8999 ThisCaptureLevel > 1; --ThisCaptureLevel) { 9000 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 9001 // 1.2.2 OpenMP Language Terminology 9002 // Structured block - An executable statement with a single entry at the 9003 // top and a single exit at the bottom. 9004 // The point of exit cannot be a branch out of the structured block. 9005 // longjmp() and throw() must not violate the entry/exit criteria. 9006 CS->getCapturedDecl()->setNothrow(); 9007 } 9008 9009 OMPLoopDirective::HelperExprs B; 9010 // In presence of clause 'collapse' with number of loops, it will 9011 // define the nested loops number. 9012 unsigned NestedLoopCount = checkOpenMPLoop( 9013 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 9014 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 9015 VarsWithImplicitDSA, B); 9016 if (NestedLoopCount == 0) 9017 return StmtError(); 9018 9019 assert((CurContext->isDependentContext() || B.builtAll()) && 9020 "omp target teams distribute parallel for loop exprs were not built"); 9021 9022 if (!CurContext->isDependentContext()) { 9023 // Finalize the clauses that need pre-built expressions for CodeGen. 9024 for (OMPClause *C : Clauses) { 9025 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9026 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9027 B.NumIterations, *this, CurScope, 9028 DSAStack)) 9029 return StmtError(); 9030 } 9031 } 9032 9033 setFunctionHasBranchProtectedScope(); 9034 return OMPTargetTeamsDistributeParallelForDirective::Create( 9035 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 9036 DSAStack->isCancelRegion()); 9037 } 9038 9039 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 9040 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9041 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9042 if (!AStmt) 9043 return StmtError(); 9044 9045 auto *CS = cast<CapturedStmt>(AStmt); 9046 // 1.2.2 OpenMP Language Terminology 9047 // Structured block - An executable statement with a single entry at the 9048 // top and a single exit at the bottom. 9049 // The point of exit cannot be a branch out of the structured block. 9050 // longjmp() and throw() must not violate the entry/exit criteria. 9051 CS->getCapturedDecl()->setNothrow(); 9052 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 9053 OMPD_target_teams_distribute_parallel_for_simd); 9054 ThisCaptureLevel > 1; --ThisCaptureLevel) { 9055 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 9056 // 1.2.2 OpenMP Language Terminology 9057 // Structured block - An executable statement with a single entry at the 9058 // top and a single exit at the bottom. 9059 // The point of exit cannot be a branch out of the structured block. 9060 // longjmp() and throw() must not violate the entry/exit criteria. 9061 CS->getCapturedDecl()->setNothrow(); 9062 } 9063 9064 OMPLoopDirective::HelperExprs B; 9065 // In presence of clause 'collapse' with number of loops, it will 9066 // define the nested loops number. 9067 unsigned NestedLoopCount = 9068 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 9069 getCollapseNumberExpr(Clauses), 9070 nullptr /*ordered not a clause on distribute*/, CS, *this, 9071 *DSAStack, VarsWithImplicitDSA, B); 9072 if (NestedLoopCount == 0) 9073 return StmtError(); 9074 9075 assert((CurContext->isDependentContext() || B.builtAll()) && 9076 "omp target teams distribute parallel for simd loop exprs were not " 9077 "built"); 9078 9079 if (!CurContext->isDependentContext()) { 9080 // Finalize the clauses that need pre-built expressions for CodeGen. 9081 for (OMPClause *C : Clauses) { 9082 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9083 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9084 B.NumIterations, *this, CurScope, 9085 DSAStack)) 9086 return StmtError(); 9087 } 9088 } 9089 9090 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9091 return StmtError(); 9092 9093 setFunctionHasBranchProtectedScope(); 9094 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 9095 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 9096 } 9097 9098 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 9099 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9100 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9101 if (!AStmt) 9102 return StmtError(); 9103 9104 auto *CS = cast<CapturedStmt>(AStmt); 9105 // 1.2.2 OpenMP Language Terminology 9106 // Structured block - An executable statement with a single entry at the 9107 // top and a single exit at the bottom. 9108 // The point of exit cannot be a branch out of the structured block. 9109 // longjmp() and throw() must not violate the entry/exit criteria. 9110 CS->getCapturedDecl()->setNothrow(); 9111 for (int ThisCaptureLevel = 9112 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 9113 ThisCaptureLevel > 1; --ThisCaptureLevel) { 9114 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 9115 // 1.2.2 OpenMP Language Terminology 9116 // Structured block - An executable statement with a single entry at the 9117 // top and a single exit at the bottom. 9118 // The point of exit cannot be a branch out of the structured block. 9119 // longjmp() and throw() must not violate the entry/exit criteria. 9120 CS->getCapturedDecl()->setNothrow(); 9121 } 9122 9123 OMPLoopDirective::HelperExprs B; 9124 // In presence of clause 'collapse' with number of loops, it will 9125 // define the nested loops number. 9126 unsigned NestedLoopCount = checkOpenMPLoop( 9127 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 9128 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 9129 VarsWithImplicitDSA, B); 9130 if (NestedLoopCount == 0) 9131 return StmtError(); 9132 9133 assert((CurContext->isDependentContext() || B.builtAll()) && 9134 "omp target teams distribute simd loop exprs were not built"); 9135 9136 if (!CurContext->isDependentContext()) { 9137 // Finalize the clauses that need pre-built expressions for CodeGen. 9138 for (OMPClause *C : Clauses) { 9139 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9140 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9141 B.NumIterations, *this, CurScope, 9142 DSAStack)) 9143 return StmtError(); 9144 } 9145 } 9146 9147 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9148 return StmtError(); 9149 9150 setFunctionHasBranchProtectedScope(); 9151 return OMPTargetTeamsDistributeSimdDirective::Create( 9152 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 9153 } 9154 9155 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 9156 SourceLocation StartLoc, 9157 SourceLocation LParenLoc, 9158 SourceLocation EndLoc) { 9159 OMPClause *Res = nullptr; 9160 switch (Kind) { 9161 case OMPC_final: 9162 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 9163 break; 9164 case OMPC_num_threads: 9165 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 9166 break; 9167 case OMPC_safelen: 9168 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 9169 break; 9170 case OMPC_simdlen: 9171 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 9172 break; 9173 case OMPC_allocator: 9174 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc); 9175 break; 9176 case OMPC_collapse: 9177 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 9178 break; 9179 case OMPC_ordered: 9180 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 9181 break; 9182 case OMPC_device: 9183 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc); 9184 break; 9185 case OMPC_num_teams: 9186 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 9187 break; 9188 case OMPC_thread_limit: 9189 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 9190 break; 9191 case OMPC_priority: 9192 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 9193 break; 9194 case OMPC_grainsize: 9195 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 9196 break; 9197 case OMPC_num_tasks: 9198 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 9199 break; 9200 case OMPC_hint: 9201 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 9202 break; 9203 case OMPC_if: 9204 case OMPC_default: 9205 case OMPC_proc_bind: 9206 case OMPC_schedule: 9207 case OMPC_private: 9208 case OMPC_firstprivate: 9209 case OMPC_lastprivate: 9210 case OMPC_shared: 9211 case OMPC_reduction: 9212 case OMPC_task_reduction: 9213 case OMPC_in_reduction: 9214 case OMPC_linear: 9215 case OMPC_aligned: 9216 case OMPC_copyin: 9217 case OMPC_copyprivate: 9218 case OMPC_nowait: 9219 case OMPC_untied: 9220 case OMPC_mergeable: 9221 case OMPC_threadprivate: 9222 case OMPC_allocate: 9223 case OMPC_flush: 9224 case OMPC_read: 9225 case OMPC_write: 9226 case OMPC_update: 9227 case OMPC_capture: 9228 case OMPC_seq_cst: 9229 case OMPC_depend: 9230 case OMPC_threads: 9231 case OMPC_simd: 9232 case OMPC_map: 9233 case OMPC_nogroup: 9234 case OMPC_dist_schedule: 9235 case OMPC_defaultmap: 9236 case OMPC_unknown: 9237 case OMPC_uniform: 9238 case OMPC_to: 9239 case OMPC_from: 9240 case OMPC_use_device_ptr: 9241 case OMPC_is_device_ptr: 9242 case OMPC_unified_address: 9243 case OMPC_unified_shared_memory: 9244 case OMPC_reverse_offload: 9245 case OMPC_dynamic_allocators: 9246 case OMPC_atomic_default_mem_order: 9247 llvm_unreachable("Clause is not allowed."); 9248 } 9249 return Res; 9250 } 9251 9252 // An OpenMP directive such as 'target parallel' has two captured regions: 9253 // for the 'target' and 'parallel' respectively. This function returns 9254 // the region in which to capture expressions associated with a clause. 9255 // A return value of OMPD_unknown signifies that the expression should not 9256 // be captured. 9257 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 9258 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 9259 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 9260 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 9261 switch (CKind) { 9262 case OMPC_if: 9263 switch (DKind) { 9264 case OMPD_target_parallel: 9265 case OMPD_target_parallel_for: 9266 case OMPD_target_parallel_for_simd: 9267 // If this clause applies to the nested 'parallel' region, capture within 9268 // the 'target' region, otherwise do not capture. 9269 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 9270 CaptureRegion = OMPD_target; 9271 break; 9272 case OMPD_target_teams_distribute_parallel_for: 9273 case OMPD_target_teams_distribute_parallel_for_simd: 9274 // If this clause applies to the nested 'parallel' region, capture within 9275 // the 'teams' region, otherwise do not capture. 9276 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 9277 CaptureRegion = OMPD_teams; 9278 break; 9279 case OMPD_teams_distribute_parallel_for: 9280 case OMPD_teams_distribute_parallel_for_simd: 9281 CaptureRegion = OMPD_teams; 9282 break; 9283 case OMPD_target_update: 9284 case OMPD_target_enter_data: 9285 case OMPD_target_exit_data: 9286 CaptureRegion = OMPD_task; 9287 break; 9288 case OMPD_cancel: 9289 case OMPD_parallel: 9290 case OMPD_parallel_sections: 9291 case OMPD_parallel_for: 9292 case OMPD_parallel_for_simd: 9293 case OMPD_target: 9294 case OMPD_target_simd: 9295 case OMPD_target_teams: 9296 case OMPD_target_teams_distribute: 9297 case OMPD_target_teams_distribute_simd: 9298 case OMPD_distribute_parallel_for: 9299 case OMPD_distribute_parallel_for_simd: 9300 case OMPD_task: 9301 case OMPD_taskloop: 9302 case OMPD_taskloop_simd: 9303 case OMPD_target_data: 9304 // Do not capture if-clause expressions. 9305 break; 9306 case OMPD_threadprivate: 9307 case OMPD_allocate: 9308 case OMPD_taskyield: 9309 case OMPD_barrier: 9310 case OMPD_taskwait: 9311 case OMPD_cancellation_point: 9312 case OMPD_flush: 9313 case OMPD_declare_reduction: 9314 case OMPD_declare_mapper: 9315 case OMPD_declare_simd: 9316 case OMPD_declare_target: 9317 case OMPD_end_declare_target: 9318 case OMPD_teams: 9319 case OMPD_simd: 9320 case OMPD_for: 9321 case OMPD_for_simd: 9322 case OMPD_sections: 9323 case OMPD_section: 9324 case OMPD_single: 9325 case OMPD_master: 9326 case OMPD_critical: 9327 case OMPD_taskgroup: 9328 case OMPD_distribute: 9329 case OMPD_ordered: 9330 case OMPD_atomic: 9331 case OMPD_distribute_simd: 9332 case OMPD_teams_distribute: 9333 case OMPD_teams_distribute_simd: 9334 case OMPD_requires: 9335 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 9336 case OMPD_unknown: 9337 llvm_unreachable("Unknown OpenMP directive"); 9338 } 9339 break; 9340 case OMPC_num_threads: 9341 switch (DKind) { 9342 case OMPD_target_parallel: 9343 case OMPD_target_parallel_for: 9344 case OMPD_target_parallel_for_simd: 9345 CaptureRegion = OMPD_target; 9346 break; 9347 case OMPD_teams_distribute_parallel_for: 9348 case OMPD_teams_distribute_parallel_for_simd: 9349 case OMPD_target_teams_distribute_parallel_for: 9350 case OMPD_target_teams_distribute_parallel_for_simd: 9351 CaptureRegion = OMPD_teams; 9352 break; 9353 case OMPD_parallel: 9354 case OMPD_parallel_sections: 9355 case OMPD_parallel_for: 9356 case OMPD_parallel_for_simd: 9357 case OMPD_distribute_parallel_for: 9358 case OMPD_distribute_parallel_for_simd: 9359 // Do not capture num_threads-clause expressions. 9360 break; 9361 case OMPD_target_data: 9362 case OMPD_target_enter_data: 9363 case OMPD_target_exit_data: 9364 case OMPD_target_update: 9365 case OMPD_target: 9366 case OMPD_target_simd: 9367 case OMPD_target_teams: 9368 case OMPD_target_teams_distribute: 9369 case OMPD_target_teams_distribute_simd: 9370 case OMPD_cancel: 9371 case OMPD_task: 9372 case OMPD_taskloop: 9373 case OMPD_taskloop_simd: 9374 case OMPD_threadprivate: 9375 case OMPD_allocate: 9376 case OMPD_taskyield: 9377 case OMPD_barrier: 9378 case OMPD_taskwait: 9379 case OMPD_cancellation_point: 9380 case OMPD_flush: 9381 case OMPD_declare_reduction: 9382 case OMPD_declare_mapper: 9383 case OMPD_declare_simd: 9384 case OMPD_declare_target: 9385 case OMPD_end_declare_target: 9386 case OMPD_teams: 9387 case OMPD_simd: 9388 case OMPD_for: 9389 case OMPD_for_simd: 9390 case OMPD_sections: 9391 case OMPD_section: 9392 case OMPD_single: 9393 case OMPD_master: 9394 case OMPD_critical: 9395 case OMPD_taskgroup: 9396 case OMPD_distribute: 9397 case OMPD_ordered: 9398 case OMPD_atomic: 9399 case OMPD_distribute_simd: 9400 case OMPD_teams_distribute: 9401 case OMPD_teams_distribute_simd: 9402 case OMPD_requires: 9403 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 9404 case OMPD_unknown: 9405 llvm_unreachable("Unknown OpenMP directive"); 9406 } 9407 break; 9408 case OMPC_num_teams: 9409 switch (DKind) { 9410 case OMPD_target_teams: 9411 case OMPD_target_teams_distribute: 9412 case OMPD_target_teams_distribute_simd: 9413 case OMPD_target_teams_distribute_parallel_for: 9414 case OMPD_target_teams_distribute_parallel_for_simd: 9415 CaptureRegion = OMPD_target; 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 // Do not capture num_teams-clause expressions. 9423 break; 9424 case OMPD_distribute_parallel_for: 9425 case OMPD_distribute_parallel_for_simd: 9426 case OMPD_task: 9427 case OMPD_taskloop: 9428 case OMPD_taskloop_simd: 9429 case OMPD_target_data: 9430 case OMPD_target_enter_data: 9431 case OMPD_target_exit_data: 9432 case OMPD_target_update: 9433 case OMPD_cancel: 9434 case OMPD_parallel: 9435 case OMPD_parallel_sections: 9436 case OMPD_parallel_for: 9437 case OMPD_parallel_for_simd: 9438 case OMPD_target: 9439 case OMPD_target_simd: 9440 case OMPD_target_parallel: 9441 case OMPD_target_parallel_for: 9442 case OMPD_target_parallel_for_simd: 9443 case OMPD_threadprivate: 9444 case OMPD_allocate: 9445 case OMPD_taskyield: 9446 case OMPD_barrier: 9447 case OMPD_taskwait: 9448 case OMPD_cancellation_point: 9449 case OMPD_flush: 9450 case OMPD_declare_reduction: 9451 case OMPD_declare_mapper: 9452 case OMPD_declare_simd: 9453 case OMPD_declare_target: 9454 case OMPD_end_declare_target: 9455 case OMPD_simd: 9456 case OMPD_for: 9457 case OMPD_for_simd: 9458 case OMPD_sections: 9459 case OMPD_section: 9460 case OMPD_single: 9461 case OMPD_master: 9462 case OMPD_critical: 9463 case OMPD_taskgroup: 9464 case OMPD_distribute: 9465 case OMPD_ordered: 9466 case OMPD_atomic: 9467 case OMPD_distribute_simd: 9468 case OMPD_requires: 9469 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 9470 case OMPD_unknown: 9471 llvm_unreachable("Unknown OpenMP directive"); 9472 } 9473 break; 9474 case OMPC_thread_limit: 9475 switch (DKind) { 9476 case OMPD_target_teams: 9477 case OMPD_target_teams_distribute: 9478 case OMPD_target_teams_distribute_simd: 9479 case OMPD_target_teams_distribute_parallel_for: 9480 case OMPD_target_teams_distribute_parallel_for_simd: 9481 CaptureRegion = OMPD_target; 9482 break; 9483 case OMPD_teams_distribute_parallel_for: 9484 case OMPD_teams_distribute_parallel_for_simd: 9485 case OMPD_teams: 9486 case OMPD_teams_distribute: 9487 case OMPD_teams_distribute_simd: 9488 // Do not capture thread_limit-clause expressions. 9489 break; 9490 case OMPD_distribute_parallel_for: 9491 case OMPD_distribute_parallel_for_simd: 9492 case OMPD_task: 9493 case OMPD_taskloop: 9494 case OMPD_taskloop_simd: 9495 case OMPD_target_data: 9496 case OMPD_target_enter_data: 9497 case OMPD_target_exit_data: 9498 case OMPD_target_update: 9499 case OMPD_cancel: 9500 case OMPD_parallel: 9501 case OMPD_parallel_sections: 9502 case OMPD_parallel_for: 9503 case OMPD_parallel_for_simd: 9504 case OMPD_target: 9505 case OMPD_target_simd: 9506 case OMPD_target_parallel: 9507 case OMPD_target_parallel_for: 9508 case OMPD_target_parallel_for_simd: 9509 case OMPD_threadprivate: 9510 case OMPD_allocate: 9511 case OMPD_taskyield: 9512 case OMPD_barrier: 9513 case OMPD_taskwait: 9514 case OMPD_cancellation_point: 9515 case OMPD_flush: 9516 case OMPD_declare_reduction: 9517 case OMPD_declare_mapper: 9518 case OMPD_declare_simd: 9519 case OMPD_declare_target: 9520 case OMPD_end_declare_target: 9521 case OMPD_simd: 9522 case OMPD_for: 9523 case OMPD_for_simd: 9524 case OMPD_sections: 9525 case OMPD_section: 9526 case OMPD_single: 9527 case OMPD_master: 9528 case OMPD_critical: 9529 case OMPD_taskgroup: 9530 case OMPD_distribute: 9531 case OMPD_ordered: 9532 case OMPD_atomic: 9533 case OMPD_distribute_simd: 9534 case OMPD_requires: 9535 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 9536 case OMPD_unknown: 9537 llvm_unreachable("Unknown OpenMP directive"); 9538 } 9539 break; 9540 case OMPC_schedule: 9541 switch (DKind) { 9542 case OMPD_parallel_for: 9543 case OMPD_parallel_for_simd: 9544 case OMPD_distribute_parallel_for: 9545 case OMPD_distribute_parallel_for_simd: 9546 case OMPD_teams_distribute_parallel_for: 9547 case OMPD_teams_distribute_parallel_for_simd: 9548 case OMPD_target_parallel_for: 9549 case OMPD_target_parallel_for_simd: 9550 case OMPD_target_teams_distribute_parallel_for: 9551 case OMPD_target_teams_distribute_parallel_for_simd: 9552 CaptureRegion = OMPD_parallel; 9553 break; 9554 case OMPD_for: 9555 case OMPD_for_simd: 9556 // Do not capture schedule-clause expressions. 9557 break; 9558 case OMPD_task: 9559 case OMPD_taskloop: 9560 case OMPD_taskloop_simd: 9561 case OMPD_target_data: 9562 case OMPD_target_enter_data: 9563 case OMPD_target_exit_data: 9564 case OMPD_target_update: 9565 case OMPD_teams: 9566 case OMPD_teams_distribute: 9567 case OMPD_teams_distribute_simd: 9568 case OMPD_target_teams_distribute: 9569 case OMPD_target_teams_distribute_simd: 9570 case OMPD_target: 9571 case OMPD_target_simd: 9572 case OMPD_target_parallel: 9573 case OMPD_cancel: 9574 case OMPD_parallel: 9575 case OMPD_parallel_sections: 9576 case OMPD_threadprivate: 9577 case OMPD_allocate: 9578 case OMPD_taskyield: 9579 case OMPD_barrier: 9580 case OMPD_taskwait: 9581 case OMPD_cancellation_point: 9582 case OMPD_flush: 9583 case OMPD_declare_reduction: 9584 case OMPD_declare_mapper: 9585 case OMPD_declare_simd: 9586 case OMPD_declare_target: 9587 case OMPD_end_declare_target: 9588 case OMPD_simd: 9589 case OMPD_sections: 9590 case OMPD_section: 9591 case OMPD_single: 9592 case OMPD_master: 9593 case OMPD_critical: 9594 case OMPD_taskgroup: 9595 case OMPD_distribute: 9596 case OMPD_ordered: 9597 case OMPD_atomic: 9598 case OMPD_distribute_simd: 9599 case OMPD_target_teams: 9600 case OMPD_requires: 9601 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 9602 case OMPD_unknown: 9603 llvm_unreachable("Unknown OpenMP directive"); 9604 } 9605 break; 9606 case OMPC_dist_schedule: 9607 switch (DKind) { 9608 case OMPD_teams_distribute_parallel_for: 9609 case OMPD_teams_distribute_parallel_for_simd: 9610 case OMPD_teams_distribute: 9611 case OMPD_teams_distribute_simd: 9612 case OMPD_target_teams_distribute_parallel_for: 9613 case OMPD_target_teams_distribute_parallel_for_simd: 9614 case OMPD_target_teams_distribute: 9615 case OMPD_target_teams_distribute_simd: 9616 CaptureRegion = OMPD_teams; 9617 break; 9618 case OMPD_distribute_parallel_for: 9619 case OMPD_distribute_parallel_for_simd: 9620 case OMPD_distribute: 9621 case OMPD_distribute_simd: 9622 // Do not capture thread_limit-clause expressions. 9623 break; 9624 case OMPD_parallel_for: 9625 case OMPD_parallel_for_simd: 9626 case OMPD_target_parallel_for_simd: 9627 case OMPD_target_parallel_for: 9628 case OMPD_task: 9629 case OMPD_taskloop: 9630 case OMPD_taskloop_simd: 9631 case OMPD_target_data: 9632 case OMPD_target_enter_data: 9633 case OMPD_target_exit_data: 9634 case OMPD_target_update: 9635 case OMPD_teams: 9636 case OMPD_target: 9637 case OMPD_target_simd: 9638 case OMPD_target_parallel: 9639 case OMPD_cancel: 9640 case OMPD_parallel: 9641 case OMPD_parallel_sections: 9642 case OMPD_threadprivate: 9643 case OMPD_allocate: 9644 case OMPD_taskyield: 9645 case OMPD_barrier: 9646 case OMPD_taskwait: 9647 case OMPD_cancellation_point: 9648 case OMPD_flush: 9649 case OMPD_declare_reduction: 9650 case OMPD_declare_mapper: 9651 case OMPD_declare_simd: 9652 case OMPD_declare_target: 9653 case OMPD_end_declare_target: 9654 case OMPD_simd: 9655 case OMPD_for: 9656 case OMPD_for_simd: 9657 case OMPD_sections: 9658 case OMPD_section: 9659 case OMPD_single: 9660 case OMPD_master: 9661 case OMPD_critical: 9662 case OMPD_taskgroup: 9663 case OMPD_ordered: 9664 case OMPD_atomic: 9665 case OMPD_target_teams: 9666 case OMPD_requires: 9667 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 9668 case OMPD_unknown: 9669 llvm_unreachable("Unknown OpenMP directive"); 9670 } 9671 break; 9672 case OMPC_device: 9673 switch (DKind) { 9674 case OMPD_target_update: 9675 case OMPD_target_enter_data: 9676 case OMPD_target_exit_data: 9677 case OMPD_target: 9678 case OMPD_target_simd: 9679 case OMPD_target_teams: 9680 case OMPD_target_parallel: 9681 case OMPD_target_teams_distribute: 9682 case OMPD_target_teams_distribute_simd: 9683 case OMPD_target_parallel_for: 9684 case OMPD_target_parallel_for_simd: 9685 case OMPD_target_teams_distribute_parallel_for: 9686 case OMPD_target_teams_distribute_parallel_for_simd: 9687 CaptureRegion = OMPD_task; 9688 break; 9689 case OMPD_target_data: 9690 // Do not capture device-clause expressions. 9691 break; 9692 case OMPD_teams_distribute_parallel_for: 9693 case OMPD_teams_distribute_parallel_for_simd: 9694 case OMPD_teams: 9695 case OMPD_teams_distribute: 9696 case OMPD_teams_distribute_simd: 9697 case OMPD_distribute_parallel_for: 9698 case OMPD_distribute_parallel_for_simd: 9699 case OMPD_task: 9700 case OMPD_taskloop: 9701 case OMPD_taskloop_simd: 9702 case OMPD_cancel: 9703 case OMPD_parallel: 9704 case OMPD_parallel_sections: 9705 case OMPD_parallel_for: 9706 case OMPD_parallel_for_simd: 9707 case OMPD_threadprivate: 9708 case OMPD_allocate: 9709 case OMPD_taskyield: 9710 case OMPD_barrier: 9711 case OMPD_taskwait: 9712 case OMPD_cancellation_point: 9713 case OMPD_flush: 9714 case OMPD_declare_reduction: 9715 case OMPD_declare_mapper: 9716 case OMPD_declare_simd: 9717 case OMPD_declare_target: 9718 case OMPD_end_declare_target: 9719 case OMPD_simd: 9720 case OMPD_for: 9721 case OMPD_for_simd: 9722 case OMPD_sections: 9723 case OMPD_section: 9724 case OMPD_single: 9725 case OMPD_master: 9726 case OMPD_critical: 9727 case OMPD_taskgroup: 9728 case OMPD_distribute: 9729 case OMPD_ordered: 9730 case OMPD_atomic: 9731 case OMPD_distribute_simd: 9732 case OMPD_requires: 9733 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 9734 case OMPD_unknown: 9735 llvm_unreachable("Unknown OpenMP directive"); 9736 } 9737 break; 9738 case OMPC_firstprivate: 9739 case OMPC_lastprivate: 9740 case OMPC_reduction: 9741 case OMPC_task_reduction: 9742 case OMPC_in_reduction: 9743 case OMPC_linear: 9744 case OMPC_default: 9745 case OMPC_proc_bind: 9746 case OMPC_final: 9747 case OMPC_safelen: 9748 case OMPC_simdlen: 9749 case OMPC_allocator: 9750 case OMPC_collapse: 9751 case OMPC_private: 9752 case OMPC_shared: 9753 case OMPC_aligned: 9754 case OMPC_copyin: 9755 case OMPC_copyprivate: 9756 case OMPC_ordered: 9757 case OMPC_nowait: 9758 case OMPC_untied: 9759 case OMPC_mergeable: 9760 case OMPC_threadprivate: 9761 case OMPC_allocate: 9762 case OMPC_flush: 9763 case OMPC_read: 9764 case OMPC_write: 9765 case OMPC_update: 9766 case OMPC_capture: 9767 case OMPC_seq_cst: 9768 case OMPC_depend: 9769 case OMPC_threads: 9770 case OMPC_simd: 9771 case OMPC_map: 9772 case OMPC_priority: 9773 case OMPC_grainsize: 9774 case OMPC_nogroup: 9775 case OMPC_num_tasks: 9776 case OMPC_hint: 9777 case OMPC_defaultmap: 9778 case OMPC_unknown: 9779 case OMPC_uniform: 9780 case OMPC_to: 9781 case OMPC_from: 9782 case OMPC_use_device_ptr: 9783 case OMPC_is_device_ptr: 9784 case OMPC_unified_address: 9785 case OMPC_unified_shared_memory: 9786 case OMPC_reverse_offload: 9787 case OMPC_dynamic_allocators: 9788 case OMPC_atomic_default_mem_order: 9789 llvm_unreachable("Unexpected OpenMP clause."); 9790 } 9791 return CaptureRegion; 9792 } 9793 9794 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 9795 Expr *Condition, SourceLocation StartLoc, 9796 SourceLocation LParenLoc, 9797 SourceLocation NameModifierLoc, 9798 SourceLocation ColonLoc, 9799 SourceLocation EndLoc) { 9800 Expr *ValExpr = Condition; 9801 Stmt *HelperValStmt = nullptr; 9802 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 9803 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 9804 !Condition->isInstantiationDependent() && 9805 !Condition->containsUnexpandedParameterPack()) { 9806 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 9807 if (Val.isInvalid()) 9808 return nullptr; 9809 9810 ValExpr = Val.get(); 9811 9812 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 9813 CaptureRegion = 9814 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier); 9815 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 9816 ValExpr = MakeFullExpr(ValExpr).get(); 9817 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 9818 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 9819 HelperValStmt = buildPreInits(Context, Captures); 9820 } 9821 } 9822 9823 return new (Context) 9824 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 9825 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 9826 } 9827 9828 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 9829 SourceLocation StartLoc, 9830 SourceLocation LParenLoc, 9831 SourceLocation EndLoc) { 9832 Expr *ValExpr = Condition; 9833 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 9834 !Condition->isInstantiationDependent() && 9835 !Condition->containsUnexpandedParameterPack()) { 9836 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 9837 if (Val.isInvalid()) 9838 return nullptr; 9839 9840 ValExpr = MakeFullExpr(Val.get()).get(); 9841 } 9842 9843 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc); 9844 } 9845 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 9846 Expr *Op) { 9847 if (!Op) 9848 return ExprError(); 9849 9850 class IntConvertDiagnoser : public ICEConvertDiagnoser { 9851 public: 9852 IntConvertDiagnoser() 9853 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 9854 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 9855 QualType T) override { 9856 return S.Diag(Loc, diag::err_omp_not_integral) << T; 9857 } 9858 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 9859 QualType T) override { 9860 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 9861 } 9862 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 9863 QualType T, 9864 QualType ConvTy) override { 9865 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 9866 } 9867 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 9868 QualType ConvTy) override { 9869 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 9870 << ConvTy->isEnumeralType() << ConvTy; 9871 } 9872 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 9873 QualType T) override { 9874 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 9875 } 9876 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 9877 QualType ConvTy) override { 9878 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 9879 << ConvTy->isEnumeralType() << ConvTy; 9880 } 9881 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 9882 QualType) override { 9883 llvm_unreachable("conversion functions are permitted"); 9884 } 9885 } ConvertDiagnoser; 9886 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 9887 } 9888 9889 static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, 9890 OpenMPClauseKind CKind, 9891 bool StrictlyPositive) { 9892 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 9893 !ValExpr->isInstantiationDependent()) { 9894 SourceLocation Loc = ValExpr->getExprLoc(); 9895 ExprResult Value = 9896 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 9897 if (Value.isInvalid()) 9898 return false; 9899 9900 ValExpr = Value.get(); 9901 // The expression must evaluate to a non-negative integer value. 9902 llvm::APSInt Result; 9903 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) && 9904 Result.isSigned() && 9905 !((!StrictlyPositive && Result.isNonNegative()) || 9906 (StrictlyPositive && Result.isStrictlyPositive()))) { 9907 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 9908 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 9909 << ValExpr->getSourceRange(); 9910 return false; 9911 } 9912 } 9913 return true; 9914 } 9915 9916 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 9917 SourceLocation StartLoc, 9918 SourceLocation LParenLoc, 9919 SourceLocation EndLoc) { 9920 Expr *ValExpr = NumThreads; 9921 Stmt *HelperValStmt = nullptr; 9922 9923 // OpenMP [2.5, Restrictions] 9924 // The num_threads expression must evaluate to a positive integer value. 9925 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 9926 /*StrictlyPositive=*/true)) 9927 return nullptr; 9928 9929 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 9930 OpenMPDirectiveKind CaptureRegion = 9931 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads); 9932 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 9933 ValExpr = MakeFullExpr(ValExpr).get(); 9934 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 9935 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 9936 HelperValStmt = buildPreInits(Context, Captures); 9937 } 9938 9939 return new (Context) OMPNumThreadsClause( 9940 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 9941 } 9942 9943 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 9944 OpenMPClauseKind CKind, 9945 bool StrictlyPositive) { 9946 if (!E) 9947 return ExprError(); 9948 if (E->isValueDependent() || E->isTypeDependent() || 9949 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 9950 return E; 9951 llvm::APSInt Result; 9952 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); 9953 if (ICE.isInvalid()) 9954 return ExprError(); 9955 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 9956 (!StrictlyPositive && !Result.isNonNegative())) { 9957 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 9958 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 9959 << E->getSourceRange(); 9960 return ExprError(); 9961 } 9962 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 9963 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 9964 << E->getSourceRange(); 9965 return ExprError(); 9966 } 9967 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 9968 DSAStack->setAssociatedLoops(Result.getExtValue()); 9969 else if (CKind == OMPC_ordered) 9970 DSAStack->setAssociatedLoops(Result.getExtValue()); 9971 return ICE; 9972 } 9973 9974 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 9975 SourceLocation LParenLoc, 9976 SourceLocation EndLoc) { 9977 // OpenMP [2.8.1, simd construct, Description] 9978 // The parameter of the safelen clause must be a constant 9979 // positive integer expression. 9980 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 9981 if (Safelen.isInvalid()) 9982 return nullptr; 9983 return new (Context) 9984 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 9985 } 9986 9987 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 9988 SourceLocation LParenLoc, 9989 SourceLocation EndLoc) { 9990 // OpenMP [2.8.1, simd construct, Description] 9991 // The parameter of the simdlen clause must be a constant 9992 // positive integer expression. 9993 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 9994 if (Simdlen.isInvalid()) 9995 return nullptr; 9996 return new (Context) 9997 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 9998 } 9999 10000 /// Tries to find omp_allocator_handle_t type. 10001 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc, 10002 DSAStackTy *Stack) { 10003 QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT(); 10004 if (!OMPAllocatorHandleT.isNull()) 10005 return true; 10006 // Build the predefined allocator expressions. 10007 bool ErrorFound = false; 10008 for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc; 10009 I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 10010 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 10011 StringRef Allocator = 10012 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 10013 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator); 10014 auto *VD = dyn_cast_or_null<ValueDecl>( 10015 S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName)); 10016 if (!VD) { 10017 ErrorFound = true; 10018 break; 10019 } 10020 QualType AllocatorType = 10021 VD->getType().getNonLValueExprType(S.getASTContext()); 10022 ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc); 10023 if (!Res.isUsable()) { 10024 ErrorFound = true; 10025 break; 10026 } 10027 if (OMPAllocatorHandleT.isNull()) 10028 OMPAllocatorHandleT = AllocatorType; 10029 if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) { 10030 ErrorFound = true; 10031 break; 10032 } 10033 Stack->setAllocator(AllocatorKind, Res.get()); 10034 } 10035 if (ErrorFound) { 10036 S.Diag(Loc, diag::err_implied_omp_allocator_handle_t_not_found); 10037 return false; 10038 } 10039 OMPAllocatorHandleT.addConst(); 10040 Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT); 10041 return true; 10042 } 10043 10044 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc, 10045 SourceLocation LParenLoc, 10046 SourceLocation EndLoc) { 10047 // OpenMP [2.11.3, allocate Directive, Description] 10048 // allocator is an expression of omp_allocator_handle_t type. 10049 if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack)) 10050 return nullptr; 10051 10052 ExprResult Allocator = DefaultLvalueConversion(A); 10053 if (Allocator.isInvalid()) 10054 return nullptr; 10055 Allocator = PerformImplicitConversion(Allocator.get(), 10056 DSAStack->getOMPAllocatorHandleT(), 10057 Sema::AA_Initializing, 10058 /*AllowExplicit=*/true); 10059 if (Allocator.isInvalid()) 10060 return nullptr; 10061 return new (Context) 10062 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc); 10063 } 10064 10065 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 10066 SourceLocation StartLoc, 10067 SourceLocation LParenLoc, 10068 SourceLocation EndLoc) { 10069 // OpenMP [2.7.1, loop construct, Description] 10070 // OpenMP [2.8.1, simd construct, Description] 10071 // OpenMP [2.9.6, distribute construct, Description] 10072 // The parameter of the collapse clause must be a constant 10073 // positive integer expression. 10074 ExprResult NumForLoopsResult = 10075 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 10076 if (NumForLoopsResult.isInvalid()) 10077 return nullptr; 10078 return new (Context) 10079 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 10080 } 10081 10082 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 10083 SourceLocation EndLoc, 10084 SourceLocation LParenLoc, 10085 Expr *NumForLoops) { 10086 // OpenMP [2.7.1, loop construct, Description] 10087 // OpenMP [2.8.1, simd construct, Description] 10088 // OpenMP [2.9.6, distribute construct, Description] 10089 // The parameter of the ordered clause must be a constant 10090 // positive integer expression if any. 10091 if (NumForLoops && LParenLoc.isValid()) { 10092 ExprResult NumForLoopsResult = 10093 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 10094 if (NumForLoopsResult.isInvalid()) 10095 return nullptr; 10096 NumForLoops = NumForLoopsResult.get(); 10097 } else { 10098 NumForLoops = nullptr; 10099 } 10100 auto *Clause = OMPOrderedClause::Create( 10101 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0, 10102 StartLoc, LParenLoc, EndLoc); 10103 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause); 10104 return Clause; 10105 } 10106 10107 OMPClause *Sema::ActOnOpenMPSimpleClause( 10108 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 10109 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 10110 OMPClause *Res = nullptr; 10111 switch (Kind) { 10112 case OMPC_default: 10113 Res = 10114 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), 10115 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 10116 break; 10117 case OMPC_proc_bind: 10118 Res = ActOnOpenMPProcBindClause( 10119 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, 10120 LParenLoc, EndLoc); 10121 break; 10122 case OMPC_atomic_default_mem_order: 10123 Res = ActOnOpenMPAtomicDefaultMemOrderClause( 10124 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument), 10125 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 10126 break; 10127 case OMPC_if: 10128 case OMPC_final: 10129 case OMPC_num_threads: 10130 case OMPC_safelen: 10131 case OMPC_simdlen: 10132 case OMPC_allocator: 10133 case OMPC_collapse: 10134 case OMPC_schedule: 10135 case OMPC_private: 10136 case OMPC_firstprivate: 10137 case OMPC_lastprivate: 10138 case OMPC_shared: 10139 case OMPC_reduction: 10140 case OMPC_task_reduction: 10141 case OMPC_in_reduction: 10142 case OMPC_linear: 10143 case OMPC_aligned: 10144 case OMPC_copyin: 10145 case OMPC_copyprivate: 10146 case OMPC_ordered: 10147 case OMPC_nowait: 10148 case OMPC_untied: 10149 case OMPC_mergeable: 10150 case OMPC_threadprivate: 10151 case OMPC_allocate: 10152 case OMPC_flush: 10153 case OMPC_read: 10154 case OMPC_write: 10155 case OMPC_update: 10156 case OMPC_capture: 10157 case OMPC_seq_cst: 10158 case OMPC_depend: 10159 case OMPC_device: 10160 case OMPC_threads: 10161 case OMPC_simd: 10162 case OMPC_map: 10163 case OMPC_num_teams: 10164 case OMPC_thread_limit: 10165 case OMPC_priority: 10166 case OMPC_grainsize: 10167 case OMPC_nogroup: 10168 case OMPC_num_tasks: 10169 case OMPC_hint: 10170 case OMPC_dist_schedule: 10171 case OMPC_defaultmap: 10172 case OMPC_unknown: 10173 case OMPC_uniform: 10174 case OMPC_to: 10175 case OMPC_from: 10176 case OMPC_use_device_ptr: 10177 case OMPC_is_device_ptr: 10178 case OMPC_unified_address: 10179 case OMPC_unified_shared_memory: 10180 case OMPC_reverse_offload: 10181 case OMPC_dynamic_allocators: 10182 llvm_unreachable("Clause is not allowed."); 10183 } 10184 return Res; 10185 } 10186 10187 static std::string 10188 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 10189 ArrayRef<unsigned> Exclude = llvm::None) { 10190 SmallString<256> Buffer; 10191 llvm::raw_svector_ostream Out(Buffer); 10192 unsigned Bound = Last >= 2 ? Last - 2 : 0; 10193 unsigned Skipped = Exclude.size(); 10194 auto S = Exclude.begin(), E = Exclude.end(); 10195 for (unsigned I = First; I < Last; ++I) { 10196 if (std::find(S, E, I) != E) { 10197 --Skipped; 10198 continue; 10199 } 10200 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 10201 if (I == Bound - Skipped) 10202 Out << " or "; 10203 else if (I != Bound + 1 - Skipped) 10204 Out << ", "; 10205 } 10206 return Out.str(); 10207 } 10208 10209 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, 10210 SourceLocation KindKwLoc, 10211 SourceLocation StartLoc, 10212 SourceLocation LParenLoc, 10213 SourceLocation EndLoc) { 10214 if (Kind == OMPC_DEFAULT_unknown) { 10215 static_assert(OMPC_DEFAULT_unknown > 0, 10216 "OMPC_DEFAULT_unknown not greater than 0"); 10217 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 10218 << getListOfPossibleValues(OMPC_default, /*First=*/0, 10219 /*Last=*/OMPC_DEFAULT_unknown) 10220 << getOpenMPClauseName(OMPC_default); 10221 return nullptr; 10222 } 10223 switch (Kind) { 10224 case OMPC_DEFAULT_none: 10225 DSAStack->setDefaultDSANone(KindKwLoc); 10226 break; 10227 case OMPC_DEFAULT_shared: 10228 DSAStack->setDefaultDSAShared(KindKwLoc); 10229 break; 10230 case OMPC_DEFAULT_unknown: 10231 llvm_unreachable("Clause kind is not allowed."); 10232 break; 10233 } 10234 return new (Context) 10235 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 10236 } 10237 10238 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, 10239 SourceLocation KindKwLoc, 10240 SourceLocation StartLoc, 10241 SourceLocation LParenLoc, 10242 SourceLocation EndLoc) { 10243 if (Kind == OMPC_PROC_BIND_unknown) { 10244 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 10245 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0, 10246 /*Last=*/OMPC_PROC_BIND_unknown) 10247 << getOpenMPClauseName(OMPC_proc_bind); 10248 return nullptr; 10249 } 10250 return new (Context) 10251 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 10252 } 10253 10254 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause( 10255 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc, 10256 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 10257 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) { 10258 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 10259 << getListOfPossibleValues( 10260 OMPC_atomic_default_mem_order, /*First=*/0, 10261 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) 10262 << getOpenMPClauseName(OMPC_atomic_default_mem_order); 10263 return nullptr; 10264 } 10265 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc, 10266 LParenLoc, EndLoc); 10267 } 10268 10269 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 10270 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 10271 SourceLocation StartLoc, SourceLocation LParenLoc, 10272 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 10273 SourceLocation EndLoc) { 10274 OMPClause *Res = nullptr; 10275 switch (Kind) { 10276 case OMPC_schedule: 10277 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 10278 assert(Argument.size() == NumberOfElements && 10279 ArgumentLoc.size() == NumberOfElements); 10280 Res = ActOnOpenMPScheduleClause( 10281 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 10282 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 10283 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 10284 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 10285 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 10286 break; 10287 case OMPC_if: 10288 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 10289 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 10290 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 10291 DelimLoc, EndLoc); 10292 break; 10293 case OMPC_dist_schedule: 10294 Res = ActOnOpenMPDistScheduleClause( 10295 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 10296 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 10297 break; 10298 case OMPC_defaultmap: 10299 enum { Modifier, DefaultmapKind }; 10300 Res = ActOnOpenMPDefaultmapClause( 10301 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 10302 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 10303 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 10304 EndLoc); 10305 break; 10306 case OMPC_final: 10307 case OMPC_num_threads: 10308 case OMPC_safelen: 10309 case OMPC_simdlen: 10310 case OMPC_allocator: 10311 case OMPC_collapse: 10312 case OMPC_default: 10313 case OMPC_proc_bind: 10314 case OMPC_private: 10315 case OMPC_firstprivate: 10316 case OMPC_lastprivate: 10317 case OMPC_shared: 10318 case OMPC_reduction: 10319 case OMPC_task_reduction: 10320 case OMPC_in_reduction: 10321 case OMPC_linear: 10322 case OMPC_aligned: 10323 case OMPC_copyin: 10324 case OMPC_copyprivate: 10325 case OMPC_ordered: 10326 case OMPC_nowait: 10327 case OMPC_untied: 10328 case OMPC_mergeable: 10329 case OMPC_threadprivate: 10330 case OMPC_allocate: 10331 case OMPC_flush: 10332 case OMPC_read: 10333 case OMPC_write: 10334 case OMPC_update: 10335 case OMPC_capture: 10336 case OMPC_seq_cst: 10337 case OMPC_depend: 10338 case OMPC_device: 10339 case OMPC_threads: 10340 case OMPC_simd: 10341 case OMPC_map: 10342 case OMPC_num_teams: 10343 case OMPC_thread_limit: 10344 case OMPC_priority: 10345 case OMPC_grainsize: 10346 case OMPC_nogroup: 10347 case OMPC_num_tasks: 10348 case OMPC_hint: 10349 case OMPC_unknown: 10350 case OMPC_uniform: 10351 case OMPC_to: 10352 case OMPC_from: 10353 case OMPC_use_device_ptr: 10354 case OMPC_is_device_ptr: 10355 case OMPC_unified_address: 10356 case OMPC_unified_shared_memory: 10357 case OMPC_reverse_offload: 10358 case OMPC_dynamic_allocators: 10359 case OMPC_atomic_default_mem_order: 10360 llvm_unreachable("Clause is not allowed."); 10361 } 10362 return Res; 10363 } 10364 10365 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 10366 OpenMPScheduleClauseModifier M2, 10367 SourceLocation M1Loc, SourceLocation M2Loc) { 10368 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 10369 SmallVector<unsigned, 2> Excluded; 10370 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 10371 Excluded.push_back(M2); 10372 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 10373 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 10374 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 10375 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 10376 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 10377 << getListOfPossibleValues(OMPC_schedule, 10378 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 10379 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 10380 Excluded) 10381 << getOpenMPClauseName(OMPC_schedule); 10382 return true; 10383 } 10384 return false; 10385 } 10386 10387 OMPClause *Sema::ActOnOpenMPScheduleClause( 10388 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 10389 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 10390 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 10391 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 10392 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 10393 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 10394 return nullptr; 10395 // OpenMP, 2.7.1, Loop Construct, Restrictions 10396 // Either the monotonic modifier or the nonmonotonic modifier can be specified 10397 // but not both. 10398 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 10399 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 10400 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 10401 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 10402 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 10403 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 10404 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 10405 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 10406 return nullptr; 10407 } 10408 if (Kind == OMPC_SCHEDULE_unknown) { 10409 std::string Values; 10410 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 10411 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 10412 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 10413 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 10414 Exclude); 10415 } else { 10416 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 10417 /*Last=*/OMPC_SCHEDULE_unknown); 10418 } 10419 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 10420 << Values << getOpenMPClauseName(OMPC_schedule); 10421 return nullptr; 10422 } 10423 // OpenMP, 2.7.1, Loop Construct, Restrictions 10424 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 10425 // schedule(guided). 10426 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 10427 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 10428 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 10429 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 10430 diag::err_omp_schedule_nonmonotonic_static); 10431 return nullptr; 10432 } 10433 Expr *ValExpr = ChunkSize; 10434 Stmt *HelperValStmt = nullptr; 10435 if (ChunkSize) { 10436 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 10437 !ChunkSize->isInstantiationDependent() && 10438 !ChunkSize->containsUnexpandedParameterPack()) { 10439 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 10440 ExprResult Val = 10441 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 10442 if (Val.isInvalid()) 10443 return nullptr; 10444 10445 ValExpr = Val.get(); 10446 10447 // OpenMP [2.7.1, Restrictions] 10448 // chunk_size must be a loop invariant integer expression with a positive 10449 // value. 10450 llvm::APSInt Result; 10451 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 10452 if (Result.isSigned() && !Result.isStrictlyPositive()) { 10453 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 10454 << "schedule" << 1 << ChunkSize->getSourceRange(); 10455 return nullptr; 10456 } 10457 } else if (getOpenMPCaptureRegionForClause( 10458 DSAStack->getCurrentDirective(), OMPC_schedule) != 10459 OMPD_unknown && 10460 !CurContext->isDependentContext()) { 10461 ValExpr = MakeFullExpr(ValExpr).get(); 10462 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 10463 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 10464 HelperValStmt = buildPreInits(Context, Captures); 10465 } 10466 } 10467 } 10468 10469 return new (Context) 10470 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 10471 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 10472 } 10473 10474 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 10475 SourceLocation StartLoc, 10476 SourceLocation EndLoc) { 10477 OMPClause *Res = nullptr; 10478 switch (Kind) { 10479 case OMPC_ordered: 10480 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 10481 break; 10482 case OMPC_nowait: 10483 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 10484 break; 10485 case OMPC_untied: 10486 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 10487 break; 10488 case OMPC_mergeable: 10489 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 10490 break; 10491 case OMPC_read: 10492 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 10493 break; 10494 case OMPC_write: 10495 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 10496 break; 10497 case OMPC_update: 10498 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 10499 break; 10500 case OMPC_capture: 10501 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 10502 break; 10503 case OMPC_seq_cst: 10504 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 10505 break; 10506 case OMPC_threads: 10507 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 10508 break; 10509 case OMPC_simd: 10510 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 10511 break; 10512 case OMPC_nogroup: 10513 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 10514 break; 10515 case OMPC_unified_address: 10516 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc); 10517 break; 10518 case OMPC_unified_shared_memory: 10519 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 10520 break; 10521 case OMPC_reverse_offload: 10522 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc); 10523 break; 10524 case OMPC_dynamic_allocators: 10525 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc); 10526 break; 10527 case OMPC_if: 10528 case OMPC_final: 10529 case OMPC_num_threads: 10530 case OMPC_safelen: 10531 case OMPC_simdlen: 10532 case OMPC_allocator: 10533 case OMPC_collapse: 10534 case OMPC_schedule: 10535 case OMPC_private: 10536 case OMPC_firstprivate: 10537 case OMPC_lastprivate: 10538 case OMPC_shared: 10539 case OMPC_reduction: 10540 case OMPC_task_reduction: 10541 case OMPC_in_reduction: 10542 case OMPC_linear: 10543 case OMPC_aligned: 10544 case OMPC_copyin: 10545 case OMPC_copyprivate: 10546 case OMPC_default: 10547 case OMPC_proc_bind: 10548 case OMPC_threadprivate: 10549 case OMPC_allocate: 10550 case OMPC_flush: 10551 case OMPC_depend: 10552 case OMPC_device: 10553 case OMPC_map: 10554 case OMPC_num_teams: 10555 case OMPC_thread_limit: 10556 case OMPC_priority: 10557 case OMPC_grainsize: 10558 case OMPC_num_tasks: 10559 case OMPC_hint: 10560 case OMPC_dist_schedule: 10561 case OMPC_defaultmap: 10562 case OMPC_unknown: 10563 case OMPC_uniform: 10564 case OMPC_to: 10565 case OMPC_from: 10566 case OMPC_use_device_ptr: 10567 case OMPC_is_device_ptr: 10568 case OMPC_atomic_default_mem_order: 10569 llvm_unreachable("Clause is not allowed."); 10570 } 10571 return Res; 10572 } 10573 10574 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 10575 SourceLocation EndLoc) { 10576 DSAStack->setNowaitRegion(); 10577 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 10578 } 10579 10580 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 10581 SourceLocation EndLoc) { 10582 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 10583 } 10584 10585 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 10586 SourceLocation EndLoc) { 10587 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 10588 } 10589 10590 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 10591 SourceLocation EndLoc) { 10592 return new (Context) OMPReadClause(StartLoc, EndLoc); 10593 } 10594 10595 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 10596 SourceLocation EndLoc) { 10597 return new (Context) OMPWriteClause(StartLoc, EndLoc); 10598 } 10599 10600 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 10601 SourceLocation EndLoc) { 10602 return new (Context) OMPUpdateClause(StartLoc, EndLoc); 10603 } 10604 10605 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 10606 SourceLocation EndLoc) { 10607 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 10608 } 10609 10610 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 10611 SourceLocation EndLoc) { 10612 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 10613 } 10614 10615 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 10616 SourceLocation EndLoc) { 10617 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 10618 } 10619 10620 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 10621 SourceLocation EndLoc) { 10622 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 10623 } 10624 10625 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 10626 SourceLocation EndLoc) { 10627 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 10628 } 10629 10630 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc, 10631 SourceLocation EndLoc) { 10632 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc); 10633 } 10634 10635 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc, 10636 SourceLocation EndLoc) { 10637 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 10638 } 10639 10640 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc, 10641 SourceLocation EndLoc) { 10642 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc); 10643 } 10644 10645 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc, 10646 SourceLocation EndLoc) { 10647 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc); 10648 } 10649 10650 OMPClause *Sema::ActOnOpenMPVarListClause( 10651 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, 10652 const OMPVarListLocTy &Locs, SourceLocation ColonLoc, 10653 CXXScopeSpec &ReductionOrMapperIdScopeSpec, 10654 DeclarationNameInfo &ReductionOrMapperId, OpenMPDependClauseKind DepKind, 10655 OpenMPLinearClauseKind LinKind, 10656 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 10657 ArrayRef<SourceLocation> MapTypeModifiersLoc, OpenMPMapClauseKind MapType, 10658 bool IsMapTypeImplicit, SourceLocation DepLinMapLoc) { 10659 SourceLocation StartLoc = Locs.StartLoc; 10660 SourceLocation LParenLoc = Locs.LParenLoc; 10661 SourceLocation EndLoc = Locs.EndLoc; 10662 OMPClause *Res = nullptr; 10663 switch (Kind) { 10664 case OMPC_private: 10665 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 10666 break; 10667 case OMPC_firstprivate: 10668 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 10669 break; 10670 case OMPC_lastprivate: 10671 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 10672 break; 10673 case OMPC_shared: 10674 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 10675 break; 10676 case OMPC_reduction: 10677 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 10678 EndLoc, ReductionOrMapperIdScopeSpec, 10679 ReductionOrMapperId); 10680 break; 10681 case OMPC_task_reduction: 10682 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 10683 EndLoc, ReductionOrMapperIdScopeSpec, 10684 ReductionOrMapperId); 10685 break; 10686 case OMPC_in_reduction: 10687 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 10688 EndLoc, ReductionOrMapperIdScopeSpec, 10689 ReductionOrMapperId); 10690 break; 10691 case OMPC_linear: 10692 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, 10693 LinKind, DepLinMapLoc, ColonLoc, EndLoc); 10694 break; 10695 case OMPC_aligned: 10696 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, 10697 ColonLoc, EndLoc); 10698 break; 10699 case OMPC_copyin: 10700 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 10701 break; 10702 case OMPC_copyprivate: 10703 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 10704 break; 10705 case OMPC_flush: 10706 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 10707 break; 10708 case OMPC_depend: 10709 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList, 10710 StartLoc, LParenLoc, EndLoc); 10711 break; 10712 case OMPC_map: 10713 Res = ActOnOpenMPMapClause(MapTypeModifiers, MapTypeModifiersLoc, 10714 ReductionOrMapperIdScopeSpec, 10715 ReductionOrMapperId, MapType, IsMapTypeImplicit, 10716 DepLinMapLoc, ColonLoc, VarList, Locs); 10717 break; 10718 case OMPC_to: 10719 Res = ActOnOpenMPToClause(VarList, ReductionOrMapperIdScopeSpec, 10720 ReductionOrMapperId, Locs); 10721 break; 10722 case OMPC_from: 10723 Res = ActOnOpenMPFromClause(VarList, ReductionOrMapperIdScopeSpec, 10724 ReductionOrMapperId, Locs); 10725 break; 10726 case OMPC_use_device_ptr: 10727 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs); 10728 break; 10729 case OMPC_is_device_ptr: 10730 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs); 10731 break; 10732 case OMPC_allocate: 10733 Res = ActOnOpenMPAllocateClause(TailExpr, VarList, StartLoc, LParenLoc, 10734 ColonLoc, EndLoc); 10735 break; 10736 case OMPC_if: 10737 case OMPC_final: 10738 case OMPC_num_threads: 10739 case OMPC_safelen: 10740 case OMPC_simdlen: 10741 case OMPC_allocator: 10742 case OMPC_collapse: 10743 case OMPC_default: 10744 case OMPC_proc_bind: 10745 case OMPC_schedule: 10746 case OMPC_ordered: 10747 case OMPC_nowait: 10748 case OMPC_untied: 10749 case OMPC_mergeable: 10750 case OMPC_threadprivate: 10751 case OMPC_read: 10752 case OMPC_write: 10753 case OMPC_update: 10754 case OMPC_capture: 10755 case OMPC_seq_cst: 10756 case OMPC_device: 10757 case OMPC_threads: 10758 case OMPC_simd: 10759 case OMPC_num_teams: 10760 case OMPC_thread_limit: 10761 case OMPC_priority: 10762 case OMPC_grainsize: 10763 case OMPC_nogroup: 10764 case OMPC_num_tasks: 10765 case OMPC_hint: 10766 case OMPC_dist_schedule: 10767 case OMPC_defaultmap: 10768 case OMPC_unknown: 10769 case OMPC_uniform: 10770 case OMPC_unified_address: 10771 case OMPC_unified_shared_memory: 10772 case OMPC_reverse_offload: 10773 case OMPC_dynamic_allocators: 10774 case OMPC_atomic_default_mem_order: 10775 llvm_unreachable("Clause is not allowed."); 10776 } 10777 return Res; 10778 } 10779 10780 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 10781 ExprObjectKind OK, SourceLocation Loc) { 10782 ExprResult Res = BuildDeclRefExpr( 10783 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 10784 if (!Res.isUsable()) 10785 return ExprError(); 10786 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 10787 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 10788 if (!Res.isUsable()) 10789 return ExprError(); 10790 } 10791 if (VK != VK_LValue && Res.get()->isGLValue()) { 10792 Res = DefaultLvalueConversion(Res.get()); 10793 if (!Res.isUsable()) 10794 return ExprError(); 10795 } 10796 return Res; 10797 } 10798 10799 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 10800 SourceLocation StartLoc, 10801 SourceLocation LParenLoc, 10802 SourceLocation EndLoc) { 10803 SmallVector<Expr *, 8> Vars; 10804 SmallVector<Expr *, 8> PrivateCopies; 10805 for (Expr *RefExpr : VarList) { 10806 assert(RefExpr && "NULL expr in OpenMP private clause."); 10807 SourceLocation ELoc; 10808 SourceRange ERange; 10809 Expr *SimpleRefExpr = RefExpr; 10810 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 10811 if (Res.second) { 10812 // It will be analyzed later. 10813 Vars.push_back(RefExpr); 10814 PrivateCopies.push_back(nullptr); 10815 } 10816 ValueDecl *D = Res.first; 10817 if (!D) 10818 continue; 10819 10820 QualType Type = D->getType(); 10821 auto *VD = dyn_cast<VarDecl>(D); 10822 10823 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 10824 // A variable that appears in a private clause must not have an incomplete 10825 // type or a reference type. 10826 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 10827 continue; 10828 Type = Type.getNonReferenceType(); 10829 10830 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 10831 // A variable that is privatized must not have a const-qualified type 10832 // unless it is of class type with a mutable member. This restriction does 10833 // not apply to the firstprivate clause. 10834 // 10835 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions] 10836 // A variable that appears in a private clause must not have a 10837 // const-qualified type unless it is of class type with a mutable member. 10838 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc)) 10839 continue; 10840 10841 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 10842 // in a Construct] 10843 // Variables with the predetermined data-sharing attributes may not be 10844 // listed in data-sharing attributes clauses, except for the cases 10845 // listed below. For these exceptions only, listing a predetermined 10846 // variable in a data-sharing attribute clause is allowed and overrides 10847 // the variable's predetermined data-sharing attributes. 10848 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 10849 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 10850 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 10851 << getOpenMPClauseName(OMPC_private); 10852 reportOriginalDsa(*this, DSAStack, D, DVar); 10853 continue; 10854 } 10855 10856 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 10857 // Variably modified types are not supported for tasks. 10858 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 10859 isOpenMPTaskingDirective(CurrDir)) { 10860 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 10861 << getOpenMPClauseName(OMPC_private) << Type 10862 << getOpenMPDirectiveName(CurrDir); 10863 bool IsDecl = 10864 !VD || 10865 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 10866 Diag(D->getLocation(), 10867 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 10868 << D; 10869 continue; 10870 } 10871 10872 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 10873 // A list item cannot appear in both a map clause and a data-sharing 10874 // attribute clause on the same construct 10875 if (isOpenMPTargetExecutionDirective(CurrDir)) { 10876 OpenMPClauseKind ConflictKind; 10877 if (DSAStack->checkMappableExprComponentListsForDecl( 10878 VD, /*CurrentRegionOnly=*/true, 10879 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 10880 OpenMPClauseKind WhereFoundClauseKind) -> bool { 10881 ConflictKind = WhereFoundClauseKind; 10882 return true; 10883 })) { 10884 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 10885 << getOpenMPClauseName(OMPC_private) 10886 << getOpenMPClauseName(ConflictKind) 10887 << getOpenMPDirectiveName(CurrDir); 10888 reportOriginalDsa(*this, DSAStack, D, DVar); 10889 continue; 10890 } 10891 } 10892 10893 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 10894 // A variable of class type (or array thereof) that appears in a private 10895 // clause requires an accessible, unambiguous default constructor for the 10896 // class type. 10897 // Generate helper private variable and initialize it with the default 10898 // value. The address of the original variable is replaced by the address of 10899 // the new private variable in CodeGen. This new variable is not added to 10900 // IdResolver, so the code in the OpenMP region uses original variable for 10901 // proper diagnostics. 10902 Type = Type.getUnqualifiedType(); 10903 VarDecl *VDPrivate = 10904 buildVarDecl(*this, ELoc, Type, D->getName(), 10905 D->hasAttrs() ? &D->getAttrs() : nullptr, 10906 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 10907 ActOnUninitializedDecl(VDPrivate); 10908 if (VDPrivate->isInvalidDecl()) 10909 continue; 10910 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 10911 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 10912 10913 DeclRefExpr *Ref = nullptr; 10914 if (!VD && !CurContext->isDependentContext()) 10915 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 10916 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 10917 Vars.push_back((VD || CurContext->isDependentContext()) 10918 ? RefExpr->IgnoreParens() 10919 : Ref); 10920 PrivateCopies.push_back(VDPrivateRefExpr); 10921 } 10922 10923 if (Vars.empty()) 10924 return nullptr; 10925 10926 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 10927 PrivateCopies); 10928 } 10929 10930 namespace { 10931 class DiagsUninitializedSeveretyRAII { 10932 private: 10933 DiagnosticsEngine &Diags; 10934 SourceLocation SavedLoc; 10935 bool IsIgnored = false; 10936 10937 public: 10938 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 10939 bool IsIgnored) 10940 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 10941 if (!IsIgnored) { 10942 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 10943 /*Map*/ diag::Severity::Ignored, Loc); 10944 } 10945 } 10946 ~DiagsUninitializedSeveretyRAII() { 10947 if (!IsIgnored) 10948 Diags.popMappings(SavedLoc); 10949 } 10950 }; 10951 } 10952 10953 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 10954 SourceLocation StartLoc, 10955 SourceLocation LParenLoc, 10956 SourceLocation EndLoc) { 10957 SmallVector<Expr *, 8> Vars; 10958 SmallVector<Expr *, 8> PrivateCopies; 10959 SmallVector<Expr *, 8> Inits; 10960 SmallVector<Decl *, 4> ExprCaptures; 10961 bool IsImplicitClause = 10962 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 10963 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 10964 10965 for (Expr *RefExpr : VarList) { 10966 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 10967 SourceLocation ELoc; 10968 SourceRange ERange; 10969 Expr *SimpleRefExpr = RefExpr; 10970 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 10971 if (Res.second) { 10972 // It will be analyzed later. 10973 Vars.push_back(RefExpr); 10974 PrivateCopies.push_back(nullptr); 10975 Inits.push_back(nullptr); 10976 } 10977 ValueDecl *D = Res.first; 10978 if (!D) 10979 continue; 10980 10981 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 10982 QualType Type = D->getType(); 10983 auto *VD = dyn_cast<VarDecl>(D); 10984 10985 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 10986 // A variable that appears in a private clause must not have an incomplete 10987 // type or a reference type. 10988 if (RequireCompleteType(ELoc, Type, 10989 diag::err_omp_firstprivate_incomplete_type)) 10990 continue; 10991 Type = Type.getNonReferenceType(); 10992 10993 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 10994 // A variable of class type (or array thereof) that appears in a private 10995 // clause requires an accessible, unambiguous copy constructor for the 10996 // class type. 10997 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 10998 10999 // If an implicit firstprivate variable found it was checked already. 11000 DSAStackTy::DSAVarData TopDVar; 11001 if (!IsImplicitClause) { 11002 DSAStackTy::DSAVarData DVar = 11003 DSAStack->getTopDSA(D, /*FromParent=*/false); 11004 TopDVar = DVar; 11005 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 11006 bool IsConstant = ElemType.isConstant(Context); 11007 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 11008 // A list item that specifies a given variable may not appear in more 11009 // than one clause on the same directive, except that a variable may be 11010 // specified in both firstprivate and lastprivate clauses. 11011 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 11012 // A list item may appear in a firstprivate or lastprivate clause but not 11013 // both. 11014 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 11015 (isOpenMPDistributeDirective(CurrDir) || 11016 DVar.CKind != OMPC_lastprivate) && 11017 DVar.RefExpr) { 11018 Diag(ELoc, diag::err_omp_wrong_dsa) 11019 << getOpenMPClauseName(DVar.CKind) 11020 << getOpenMPClauseName(OMPC_firstprivate); 11021 reportOriginalDsa(*this, DSAStack, D, DVar); 11022 continue; 11023 } 11024 11025 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 11026 // in a Construct] 11027 // Variables with the predetermined data-sharing attributes may not be 11028 // listed in data-sharing attributes clauses, except for the cases 11029 // listed below. For these exceptions only, listing a predetermined 11030 // variable in a data-sharing attribute clause is allowed and overrides 11031 // the variable's predetermined data-sharing attributes. 11032 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 11033 // in a Construct, C/C++, p.2] 11034 // Variables with const-qualified type having no mutable member may be 11035 // listed in a firstprivate clause, even if they are static data members. 11036 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 11037 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 11038 Diag(ELoc, diag::err_omp_wrong_dsa) 11039 << getOpenMPClauseName(DVar.CKind) 11040 << getOpenMPClauseName(OMPC_firstprivate); 11041 reportOriginalDsa(*this, DSAStack, D, DVar); 11042 continue; 11043 } 11044 11045 // OpenMP [2.9.3.4, Restrictions, p.2] 11046 // A list item that is private within a parallel region must not appear 11047 // in a firstprivate clause on a worksharing construct if any of the 11048 // worksharing regions arising from the worksharing construct ever bind 11049 // to any of the parallel regions arising from the parallel construct. 11050 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 11051 // A list item that is private within a teams region must not appear in a 11052 // firstprivate clause on a distribute construct if any of the distribute 11053 // regions arising from the distribute construct ever bind to any of the 11054 // teams regions arising from the teams construct. 11055 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 11056 // A list item that appears in a reduction clause of a teams construct 11057 // must not appear in a firstprivate clause on a distribute construct if 11058 // any of the distribute regions arising from the distribute construct 11059 // ever bind to any of the teams regions arising from the teams construct. 11060 if ((isOpenMPWorksharingDirective(CurrDir) || 11061 isOpenMPDistributeDirective(CurrDir)) && 11062 !isOpenMPParallelDirective(CurrDir) && 11063 !isOpenMPTeamsDirective(CurrDir)) { 11064 DVar = DSAStack->getImplicitDSA(D, true); 11065 if (DVar.CKind != OMPC_shared && 11066 (isOpenMPParallelDirective(DVar.DKind) || 11067 isOpenMPTeamsDirective(DVar.DKind) || 11068 DVar.DKind == OMPD_unknown)) { 11069 Diag(ELoc, diag::err_omp_required_access) 11070 << getOpenMPClauseName(OMPC_firstprivate) 11071 << getOpenMPClauseName(OMPC_shared); 11072 reportOriginalDsa(*this, DSAStack, D, DVar); 11073 continue; 11074 } 11075 } 11076 // OpenMP [2.9.3.4, Restrictions, p.3] 11077 // A list item that appears in a reduction clause of a parallel construct 11078 // must not appear in a firstprivate clause on a worksharing or task 11079 // construct if any of the worksharing or task regions arising from the 11080 // worksharing or task construct ever bind to any of the parallel regions 11081 // arising from the parallel construct. 11082 // OpenMP [2.9.3.4, Restrictions, p.4] 11083 // A list item that appears in a reduction clause in worksharing 11084 // construct must not appear in a firstprivate clause in a task construct 11085 // encountered during execution of any of the worksharing regions arising 11086 // from the worksharing construct. 11087 if (isOpenMPTaskingDirective(CurrDir)) { 11088 DVar = DSAStack->hasInnermostDSA( 11089 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 11090 [](OpenMPDirectiveKind K) { 11091 return isOpenMPParallelDirective(K) || 11092 isOpenMPWorksharingDirective(K) || 11093 isOpenMPTeamsDirective(K); 11094 }, 11095 /*FromParent=*/true); 11096 if (DVar.CKind == OMPC_reduction && 11097 (isOpenMPParallelDirective(DVar.DKind) || 11098 isOpenMPWorksharingDirective(DVar.DKind) || 11099 isOpenMPTeamsDirective(DVar.DKind))) { 11100 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 11101 << getOpenMPDirectiveName(DVar.DKind); 11102 reportOriginalDsa(*this, DSAStack, D, DVar); 11103 continue; 11104 } 11105 } 11106 11107 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 11108 // A list item cannot appear in both a map clause and a data-sharing 11109 // attribute clause on the same construct 11110 if (isOpenMPTargetExecutionDirective(CurrDir)) { 11111 OpenMPClauseKind ConflictKind; 11112 if (DSAStack->checkMappableExprComponentListsForDecl( 11113 VD, /*CurrentRegionOnly=*/true, 11114 [&ConflictKind]( 11115 OMPClauseMappableExprCommon::MappableExprComponentListRef, 11116 OpenMPClauseKind WhereFoundClauseKind) { 11117 ConflictKind = WhereFoundClauseKind; 11118 return true; 11119 })) { 11120 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 11121 << getOpenMPClauseName(OMPC_firstprivate) 11122 << getOpenMPClauseName(ConflictKind) 11123 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 11124 reportOriginalDsa(*this, DSAStack, D, DVar); 11125 continue; 11126 } 11127 } 11128 } 11129 11130 // Variably modified types are not supported for tasks. 11131 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 11132 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 11133 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 11134 << getOpenMPClauseName(OMPC_firstprivate) << Type 11135 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 11136 bool IsDecl = 11137 !VD || 11138 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 11139 Diag(D->getLocation(), 11140 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 11141 << D; 11142 continue; 11143 } 11144 11145 Type = Type.getUnqualifiedType(); 11146 VarDecl *VDPrivate = 11147 buildVarDecl(*this, ELoc, Type, D->getName(), 11148 D->hasAttrs() ? &D->getAttrs() : nullptr, 11149 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 11150 // Generate helper private variable and initialize it with the value of the 11151 // original variable. The address of the original variable is replaced by 11152 // the address of the new private variable in the CodeGen. This new variable 11153 // is not added to IdResolver, so the code in the OpenMP region uses 11154 // original variable for proper diagnostics and variable capturing. 11155 Expr *VDInitRefExpr = nullptr; 11156 // For arrays generate initializer for single element and replace it by the 11157 // original array element in CodeGen. 11158 if (Type->isArrayType()) { 11159 VarDecl *VDInit = 11160 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 11161 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 11162 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 11163 ElemType = ElemType.getUnqualifiedType(); 11164 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 11165 ".firstprivate.temp"); 11166 InitializedEntity Entity = 11167 InitializedEntity::InitializeVariable(VDInitTemp); 11168 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 11169 11170 InitializationSequence InitSeq(*this, Entity, Kind, Init); 11171 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 11172 if (Result.isInvalid()) 11173 VDPrivate->setInvalidDecl(); 11174 else 11175 VDPrivate->setInit(Result.getAs<Expr>()); 11176 // Remove temp variable declaration. 11177 Context.Deallocate(VDInitTemp); 11178 } else { 11179 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 11180 ".firstprivate.temp"); 11181 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 11182 RefExpr->getExprLoc()); 11183 AddInitializerToDecl(VDPrivate, 11184 DefaultLvalueConversion(VDInitRefExpr).get(), 11185 /*DirectInit=*/false); 11186 } 11187 if (VDPrivate->isInvalidDecl()) { 11188 if (IsImplicitClause) { 11189 Diag(RefExpr->getExprLoc(), 11190 diag::note_omp_task_predetermined_firstprivate_here); 11191 } 11192 continue; 11193 } 11194 CurContext->addDecl(VDPrivate); 11195 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 11196 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 11197 RefExpr->getExprLoc()); 11198 DeclRefExpr *Ref = nullptr; 11199 if (!VD && !CurContext->isDependentContext()) { 11200 if (TopDVar.CKind == OMPC_lastprivate) { 11201 Ref = TopDVar.PrivateCopy; 11202 } else { 11203 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 11204 if (!isOpenMPCapturedDecl(D)) 11205 ExprCaptures.push_back(Ref->getDecl()); 11206 } 11207 } 11208 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 11209 Vars.push_back((VD || CurContext->isDependentContext()) 11210 ? RefExpr->IgnoreParens() 11211 : Ref); 11212 PrivateCopies.push_back(VDPrivateRefExpr); 11213 Inits.push_back(VDInitRefExpr); 11214 } 11215 11216 if (Vars.empty()) 11217 return nullptr; 11218 11219 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 11220 Vars, PrivateCopies, Inits, 11221 buildPreInits(Context, ExprCaptures)); 11222 } 11223 11224 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList, 11225 SourceLocation StartLoc, 11226 SourceLocation LParenLoc, 11227 SourceLocation EndLoc) { 11228 SmallVector<Expr *, 8> Vars; 11229 SmallVector<Expr *, 8> SrcExprs; 11230 SmallVector<Expr *, 8> DstExprs; 11231 SmallVector<Expr *, 8> AssignmentOps; 11232 SmallVector<Decl *, 4> ExprCaptures; 11233 SmallVector<Expr *, 4> ExprPostUpdates; 11234 for (Expr *RefExpr : VarList) { 11235 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 11236 SourceLocation ELoc; 11237 SourceRange ERange; 11238 Expr *SimpleRefExpr = RefExpr; 11239 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 11240 if (Res.second) { 11241 // It will be analyzed later. 11242 Vars.push_back(RefExpr); 11243 SrcExprs.push_back(nullptr); 11244 DstExprs.push_back(nullptr); 11245 AssignmentOps.push_back(nullptr); 11246 } 11247 ValueDecl *D = Res.first; 11248 if (!D) 11249 continue; 11250 11251 QualType Type = D->getType(); 11252 auto *VD = dyn_cast<VarDecl>(D); 11253 11254 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 11255 // A variable that appears in a lastprivate clause must not have an 11256 // incomplete type or a reference type. 11257 if (RequireCompleteType(ELoc, Type, 11258 diag::err_omp_lastprivate_incomplete_type)) 11259 continue; 11260 Type = Type.getNonReferenceType(); 11261 11262 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 11263 // A variable that is privatized must not have a const-qualified type 11264 // unless it is of class type with a mutable member. This restriction does 11265 // not apply to the firstprivate clause. 11266 // 11267 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions] 11268 // A variable that appears in a lastprivate clause must not have a 11269 // const-qualified type unless it is of class type with a mutable member. 11270 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc)) 11271 continue; 11272 11273 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 11274 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 11275 // in a Construct] 11276 // Variables with the predetermined data-sharing attributes may not be 11277 // listed in data-sharing attributes clauses, except for the cases 11278 // listed below. 11279 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 11280 // A list item may appear in a firstprivate or lastprivate clause but not 11281 // both. 11282 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 11283 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 11284 (isOpenMPDistributeDirective(CurrDir) || 11285 DVar.CKind != OMPC_firstprivate) && 11286 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 11287 Diag(ELoc, diag::err_omp_wrong_dsa) 11288 << getOpenMPClauseName(DVar.CKind) 11289 << getOpenMPClauseName(OMPC_lastprivate); 11290 reportOriginalDsa(*this, DSAStack, D, DVar); 11291 continue; 11292 } 11293 11294 // OpenMP [2.14.3.5, Restrictions, p.2] 11295 // A list item that is private within a parallel region, or that appears in 11296 // the reduction clause of a parallel construct, must not appear in a 11297 // lastprivate clause on a worksharing construct if any of the corresponding 11298 // worksharing regions ever binds to any of the corresponding parallel 11299 // regions. 11300 DSAStackTy::DSAVarData TopDVar = DVar; 11301 if (isOpenMPWorksharingDirective(CurrDir) && 11302 !isOpenMPParallelDirective(CurrDir) && 11303 !isOpenMPTeamsDirective(CurrDir)) { 11304 DVar = DSAStack->getImplicitDSA(D, true); 11305 if (DVar.CKind != OMPC_shared) { 11306 Diag(ELoc, diag::err_omp_required_access) 11307 << getOpenMPClauseName(OMPC_lastprivate) 11308 << getOpenMPClauseName(OMPC_shared); 11309 reportOriginalDsa(*this, DSAStack, D, DVar); 11310 continue; 11311 } 11312 } 11313 11314 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 11315 // A variable of class type (or array thereof) that appears in a 11316 // lastprivate clause requires an accessible, unambiguous default 11317 // constructor for the class type, unless the list item is also specified 11318 // in a firstprivate clause. 11319 // A variable of class type (or array thereof) that appears in a 11320 // lastprivate clause requires an accessible, unambiguous copy assignment 11321 // operator for the class type. 11322 Type = Context.getBaseElementType(Type).getNonReferenceType(); 11323 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 11324 Type.getUnqualifiedType(), ".lastprivate.src", 11325 D->hasAttrs() ? &D->getAttrs() : nullptr); 11326 DeclRefExpr *PseudoSrcExpr = 11327 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 11328 VarDecl *DstVD = 11329 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 11330 D->hasAttrs() ? &D->getAttrs() : nullptr); 11331 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 11332 // For arrays generate assignment operation for single element and replace 11333 // it by the original array element in CodeGen. 11334 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 11335 PseudoDstExpr, PseudoSrcExpr); 11336 if (AssignmentOp.isInvalid()) 11337 continue; 11338 AssignmentOp = 11339 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 11340 if (AssignmentOp.isInvalid()) 11341 continue; 11342 11343 DeclRefExpr *Ref = nullptr; 11344 if (!VD && !CurContext->isDependentContext()) { 11345 if (TopDVar.CKind == OMPC_firstprivate) { 11346 Ref = TopDVar.PrivateCopy; 11347 } else { 11348 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 11349 if (!isOpenMPCapturedDecl(D)) 11350 ExprCaptures.push_back(Ref->getDecl()); 11351 } 11352 if (TopDVar.CKind == OMPC_firstprivate || 11353 (!isOpenMPCapturedDecl(D) && 11354 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 11355 ExprResult RefRes = DefaultLvalueConversion(Ref); 11356 if (!RefRes.isUsable()) 11357 continue; 11358 ExprResult PostUpdateRes = 11359 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 11360 RefRes.get()); 11361 if (!PostUpdateRes.isUsable()) 11362 continue; 11363 ExprPostUpdates.push_back( 11364 IgnoredValueConversions(PostUpdateRes.get()).get()); 11365 } 11366 } 11367 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 11368 Vars.push_back((VD || CurContext->isDependentContext()) 11369 ? RefExpr->IgnoreParens() 11370 : Ref); 11371 SrcExprs.push_back(PseudoSrcExpr); 11372 DstExprs.push_back(PseudoDstExpr); 11373 AssignmentOps.push_back(AssignmentOp.get()); 11374 } 11375 11376 if (Vars.empty()) 11377 return nullptr; 11378 11379 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 11380 Vars, SrcExprs, DstExprs, AssignmentOps, 11381 buildPreInits(Context, ExprCaptures), 11382 buildPostUpdate(*this, ExprPostUpdates)); 11383 } 11384 11385 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 11386 SourceLocation StartLoc, 11387 SourceLocation LParenLoc, 11388 SourceLocation EndLoc) { 11389 SmallVector<Expr *, 8> Vars; 11390 for (Expr *RefExpr : VarList) { 11391 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 11392 SourceLocation ELoc; 11393 SourceRange ERange; 11394 Expr *SimpleRefExpr = RefExpr; 11395 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 11396 if (Res.second) { 11397 // It will be analyzed later. 11398 Vars.push_back(RefExpr); 11399 } 11400 ValueDecl *D = Res.first; 11401 if (!D) 11402 continue; 11403 11404 auto *VD = dyn_cast<VarDecl>(D); 11405 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 11406 // in a Construct] 11407 // Variables with the predetermined data-sharing attributes may not be 11408 // listed in data-sharing attributes clauses, except for the cases 11409 // listed below. For these exceptions only, listing a predetermined 11410 // variable in a data-sharing attribute clause is allowed and overrides 11411 // the variable's predetermined data-sharing attributes. 11412 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 11413 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 11414 DVar.RefExpr) { 11415 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 11416 << getOpenMPClauseName(OMPC_shared); 11417 reportOriginalDsa(*this, DSAStack, D, DVar); 11418 continue; 11419 } 11420 11421 DeclRefExpr *Ref = nullptr; 11422 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 11423 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 11424 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 11425 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 11426 ? RefExpr->IgnoreParens() 11427 : Ref); 11428 } 11429 11430 if (Vars.empty()) 11431 return nullptr; 11432 11433 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 11434 } 11435 11436 namespace { 11437 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 11438 DSAStackTy *Stack; 11439 11440 public: 11441 bool VisitDeclRefExpr(DeclRefExpr *E) { 11442 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 11443 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 11444 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 11445 return false; 11446 if (DVar.CKind != OMPC_unknown) 11447 return true; 11448 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 11449 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; }, 11450 /*FromParent=*/true); 11451 return DVarPrivate.CKind != OMPC_unknown; 11452 } 11453 return false; 11454 } 11455 bool VisitStmt(Stmt *S) { 11456 for (Stmt *Child : S->children()) { 11457 if (Child && Visit(Child)) 11458 return true; 11459 } 11460 return false; 11461 } 11462 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 11463 }; 11464 } // namespace 11465 11466 namespace { 11467 // Transform MemberExpression for specified FieldDecl of current class to 11468 // DeclRefExpr to specified OMPCapturedExprDecl. 11469 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 11470 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 11471 ValueDecl *Field = nullptr; 11472 DeclRefExpr *CapturedExpr = nullptr; 11473 11474 public: 11475 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 11476 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 11477 11478 ExprResult TransformMemberExpr(MemberExpr *E) { 11479 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 11480 E->getMemberDecl() == Field) { 11481 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 11482 return CapturedExpr; 11483 } 11484 return BaseTransform::TransformMemberExpr(E); 11485 } 11486 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 11487 }; 11488 } // namespace 11489 11490 template <typename T, typename U> 11491 static T filterLookupForUDReductionAndMapper( 11492 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) { 11493 for (U &Set : Lookups) { 11494 for (auto *D : Set) { 11495 if (T Res = Gen(cast<ValueDecl>(D))) 11496 return Res; 11497 } 11498 } 11499 return T(); 11500 } 11501 11502 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 11503 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 11504 11505 for (auto RD : D->redecls()) { 11506 // Don't bother with extra checks if we already know this one isn't visible. 11507 if (RD == D) 11508 continue; 11509 11510 auto ND = cast<NamedDecl>(RD); 11511 if (LookupResult::isVisible(SemaRef, ND)) 11512 return ND; 11513 } 11514 11515 return nullptr; 11516 } 11517 11518 static void 11519 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id, 11520 SourceLocation Loc, QualType Ty, 11521 SmallVectorImpl<UnresolvedSet<8>> &Lookups) { 11522 // Find all of the associated namespaces and classes based on the 11523 // arguments we have. 11524 Sema::AssociatedNamespaceSet AssociatedNamespaces; 11525 Sema::AssociatedClassSet AssociatedClasses; 11526 OpaqueValueExpr OVE(Loc, Ty, VK_LValue); 11527 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces, 11528 AssociatedClasses); 11529 11530 // C++ [basic.lookup.argdep]p3: 11531 // Let X be the lookup set produced by unqualified lookup (3.4.1) 11532 // and let Y be the lookup set produced by argument dependent 11533 // lookup (defined as follows). If X contains [...] then Y is 11534 // empty. Otherwise Y is the set of declarations found in the 11535 // namespaces associated with the argument types as described 11536 // below. The set of declarations found by the lookup of the name 11537 // is the union of X and Y. 11538 // 11539 // Here, we compute Y and add its members to the overloaded 11540 // candidate set. 11541 for (auto *NS : AssociatedNamespaces) { 11542 // When considering an associated namespace, the lookup is the 11543 // same as the lookup performed when the associated namespace is 11544 // used as a qualifier (3.4.3.2) except that: 11545 // 11546 // -- Any using-directives in the associated namespace are 11547 // ignored. 11548 // 11549 // -- Any namespace-scope friend functions declared in 11550 // associated classes are visible within their respective 11551 // namespaces even if they are not visible during an ordinary 11552 // lookup (11.4). 11553 DeclContext::lookup_result R = NS->lookup(Id.getName()); 11554 for (auto *D : R) { 11555 auto *Underlying = D; 11556 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 11557 Underlying = USD->getTargetDecl(); 11558 11559 if (!isa<OMPDeclareReductionDecl>(Underlying) && 11560 !isa<OMPDeclareMapperDecl>(Underlying)) 11561 continue; 11562 11563 if (!SemaRef.isVisible(D)) { 11564 D = findAcceptableDecl(SemaRef, D); 11565 if (!D) 11566 continue; 11567 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 11568 Underlying = USD->getTargetDecl(); 11569 } 11570 Lookups.emplace_back(); 11571 Lookups.back().addDecl(Underlying); 11572 } 11573 } 11574 } 11575 11576 static ExprResult 11577 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 11578 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 11579 const DeclarationNameInfo &ReductionId, QualType Ty, 11580 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 11581 if (ReductionIdScopeSpec.isInvalid()) 11582 return ExprError(); 11583 SmallVector<UnresolvedSet<8>, 4> Lookups; 11584 if (S) { 11585 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 11586 Lookup.suppressDiagnostics(); 11587 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 11588 NamedDecl *D = Lookup.getRepresentativeDecl(); 11589 do { 11590 S = S->getParent(); 11591 } while (S && !S->isDeclScope(D)); 11592 if (S) 11593 S = S->getParent(); 11594 Lookups.emplace_back(); 11595 Lookups.back().append(Lookup.begin(), Lookup.end()); 11596 Lookup.clear(); 11597 } 11598 } else if (auto *ULE = 11599 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 11600 Lookups.push_back(UnresolvedSet<8>()); 11601 Decl *PrevD = nullptr; 11602 for (NamedDecl *D : ULE->decls()) { 11603 if (D == PrevD) 11604 Lookups.push_back(UnresolvedSet<8>()); 11605 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D)) 11606 Lookups.back().addDecl(DRD); 11607 PrevD = D; 11608 } 11609 } 11610 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 11611 Ty->isInstantiationDependentType() || 11612 Ty->containsUnexpandedParameterPack() || 11613 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 11614 return !D->isInvalidDecl() && 11615 (D->getType()->isDependentType() || 11616 D->getType()->isInstantiationDependentType() || 11617 D->getType()->containsUnexpandedParameterPack()); 11618 })) { 11619 UnresolvedSet<8> ResSet; 11620 for (const UnresolvedSet<8> &Set : Lookups) { 11621 if (Set.empty()) 11622 continue; 11623 ResSet.append(Set.begin(), Set.end()); 11624 // The last item marks the end of all declarations at the specified scope. 11625 ResSet.addDecl(Set[Set.size() - 1]); 11626 } 11627 return UnresolvedLookupExpr::Create( 11628 SemaRef.Context, /*NamingClass=*/nullptr, 11629 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 11630 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 11631 } 11632 // Lookup inside the classes. 11633 // C++ [over.match.oper]p3: 11634 // For a unary operator @ with an operand of a type whose 11635 // cv-unqualified version is T1, and for a binary operator @ with 11636 // a left operand of a type whose cv-unqualified version is T1 and 11637 // a right operand of a type whose cv-unqualified version is T2, 11638 // three sets of candidate functions, designated member 11639 // candidates, non-member candidates and built-in candidates, are 11640 // constructed as follows: 11641 // -- If T1 is a complete class type or a class currently being 11642 // defined, the set of member candidates is the result of the 11643 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, 11644 // the set of member candidates is empty. 11645 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 11646 Lookup.suppressDiagnostics(); 11647 if (const auto *TyRec = Ty->getAs<RecordType>()) { 11648 // Complete the type if it can be completed. 11649 // If the type is neither complete nor being defined, bail out now. 11650 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() || 11651 TyRec->getDecl()->getDefinition()) { 11652 Lookup.clear(); 11653 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl()); 11654 if (Lookup.empty()) { 11655 Lookups.emplace_back(); 11656 Lookups.back().append(Lookup.begin(), Lookup.end()); 11657 } 11658 } 11659 } 11660 // Perform ADL. 11661 if (SemaRef.getLangOpts().CPlusPlus) 11662 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups); 11663 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 11664 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 11665 if (!D->isInvalidDecl() && 11666 SemaRef.Context.hasSameType(D->getType(), Ty)) 11667 return D; 11668 return nullptr; 11669 })) 11670 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), 11671 VK_LValue, Loc); 11672 if (SemaRef.getLangOpts().CPlusPlus) { 11673 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 11674 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 11675 if (!D->isInvalidDecl() && 11676 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 11677 !Ty.isMoreQualifiedThan(D->getType())) 11678 return D; 11679 return nullptr; 11680 })) { 11681 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 11682 /*DetectVirtual=*/false); 11683 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 11684 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 11685 VD->getType().getUnqualifiedType()))) { 11686 if (SemaRef.CheckBaseClassAccess( 11687 Loc, VD->getType(), Ty, Paths.front(), 11688 /*DiagID=*/0) != Sema::AR_inaccessible) { 11689 SemaRef.BuildBasePathArray(Paths, BasePath); 11690 return SemaRef.BuildDeclRefExpr( 11691 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc); 11692 } 11693 } 11694 } 11695 } 11696 } 11697 if (ReductionIdScopeSpec.isSet()) { 11698 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range; 11699 return ExprError(); 11700 } 11701 return ExprEmpty(); 11702 } 11703 11704 namespace { 11705 /// Data for the reduction-based clauses. 11706 struct ReductionData { 11707 /// List of original reduction items. 11708 SmallVector<Expr *, 8> Vars; 11709 /// List of private copies of the reduction items. 11710 SmallVector<Expr *, 8> Privates; 11711 /// LHS expressions for the reduction_op expressions. 11712 SmallVector<Expr *, 8> LHSs; 11713 /// RHS expressions for the reduction_op expressions. 11714 SmallVector<Expr *, 8> RHSs; 11715 /// Reduction operation expression. 11716 SmallVector<Expr *, 8> ReductionOps; 11717 /// Taskgroup descriptors for the corresponding reduction items in 11718 /// in_reduction clauses. 11719 SmallVector<Expr *, 8> TaskgroupDescriptors; 11720 /// List of captures for clause. 11721 SmallVector<Decl *, 4> ExprCaptures; 11722 /// List of postupdate expressions. 11723 SmallVector<Expr *, 4> ExprPostUpdates; 11724 ReductionData() = delete; 11725 /// Reserves required memory for the reduction data. 11726 ReductionData(unsigned Size) { 11727 Vars.reserve(Size); 11728 Privates.reserve(Size); 11729 LHSs.reserve(Size); 11730 RHSs.reserve(Size); 11731 ReductionOps.reserve(Size); 11732 TaskgroupDescriptors.reserve(Size); 11733 ExprCaptures.reserve(Size); 11734 ExprPostUpdates.reserve(Size); 11735 } 11736 /// Stores reduction item and reduction operation only (required for dependent 11737 /// reduction item). 11738 void push(Expr *Item, Expr *ReductionOp) { 11739 Vars.emplace_back(Item); 11740 Privates.emplace_back(nullptr); 11741 LHSs.emplace_back(nullptr); 11742 RHSs.emplace_back(nullptr); 11743 ReductionOps.emplace_back(ReductionOp); 11744 TaskgroupDescriptors.emplace_back(nullptr); 11745 } 11746 /// Stores reduction data. 11747 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 11748 Expr *TaskgroupDescriptor) { 11749 Vars.emplace_back(Item); 11750 Privates.emplace_back(Private); 11751 LHSs.emplace_back(LHS); 11752 RHSs.emplace_back(RHS); 11753 ReductionOps.emplace_back(ReductionOp); 11754 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 11755 } 11756 }; 11757 } // namespace 11758 11759 static bool checkOMPArraySectionConstantForReduction( 11760 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 11761 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 11762 const Expr *Length = OASE->getLength(); 11763 if (Length == nullptr) { 11764 // For array sections of the form [1:] or [:], we would need to analyze 11765 // the lower bound... 11766 if (OASE->getColonLoc().isValid()) 11767 return false; 11768 11769 // This is an array subscript which has implicit length 1! 11770 SingleElement = true; 11771 ArraySizes.push_back(llvm::APSInt::get(1)); 11772 } else { 11773 Expr::EvalResult Result; 11774 if (!Length->EvaluateAsInt(Result, Context)) 11775 return false; 11776 11777 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 11778 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 11779 ArraySizes.push_back(ConstantLengthValue); 11780 } 11781 11782 // Get the base of this array section and walk up from there. 11783 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 11784 11785 // We require length = 1 for all array sections except the right-most to 11786 // guarantee that the memory region is contiguous and has no holes in it. 11787 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 11788 Length = TempOASE->getLength(); 11789 if (Length == nullptr) { 11790 // For array sections of the form [1:] or [:], we would need to analyze 11791 // the lower bound... 11792 if (OASE->getColonLoc().isValid()) 11793 return false; 11794 11795 // This is an array subscript which has implicit length 1! 11796 ArraySizes.push_back(llvm::APSInt::get(1)); 11797 } else { 11798 Expr::EvalResult Result; 11799 if (!Length->EvaluateAsInt(Result, Context)) 11800 return false; 11801 11802 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 11803 if (ConstantLengthValue.getSExtValue() != 1) 11804 return false; 11805 11806 ArraySizes.push_back(ConstantLengthValue); 11807 } 11808 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 11809 } 11810 11811 // If we have a single element, we don't need to add the implicit lengths. 11812 if (!SingleElement) { 11813 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 11814 // Has implicit length 1! 11815 ArraySizes.push_back(llvm::APSInt::get(1)); 11816 Base = TempASE->getBase()->IgnoreParenImpCasts(); 11817 } 11818 } 11819 11820 // This array section can be privatized as a single value or as a constant 11821 // sized array. 11822 return true; 11823 } 11824 11825 static bool actOnOMPReductionKindClause( 11826 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 11827 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 11828 SourceLocation ColonLoc, SourceLocation EndLoc, 11829 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 11830 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 11831 DeclarationName DN = ReductionId.getName(); 11832 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 11833 BinaryOperatorKind BOK = BO_Comma; 11834 11835 ASTContext &Context = S.Context; 11836 // OpenMP [2.14.3.6, reduction clause] 11837 // C 11838 // reduction-identifier is either an identifier or one of the following 11839 // operators: +, -, *, &, |, ^, && and || 11840 // C++ 11841 // reduction-identifier is either an id-expression or one of the following 11842 // operators: +, -, *, &, |, ^, && and || 11843 switch (OOK) { 11844 case OO_Plus: 11845 case OO_Minus: 11846 BOK = BO_Add; 11847 break; 11848 case OO_Star: 11849 BOK = BO_Mul; 11850 break; 11851 case OO_Amp: 11852 BOK = BO_And; 11853 break; 11854 case OO_Pipe: 11855 BOK = BO_Or; 11856 break; 11857 case OO_Caret: 11858 BOK = BO_Xor; 11859 break; 11860 case OO_AmpAmp: 11861 BOK = BO_LAnd; 11862 break; 11863 case OO_PipePipe: 11864 BOK = BO_LOr; 11865 break; 11866 case OO_New: 11867 case OO_Delete: 11868 case OO_Array_New: 11869 case OO_Array_Delete: 11870 case OO_Slash: 11871 case OO_Percent: 11872 case OO_Tilde: 11873 case OO_Exclaim: 11874 case OO_Equal: 11875 case OO_Less: 11876 case OO_Greater: 11877 case OO_LessEqual: 11878 case OO_GreaterEqual: 11879 case OO_PlusEqual: 11880 case OO_MinusEqual: 11881 case OO_StarEqual: 11882 case OO_SlashEqual: 11883 case OO_PercentEqual: 11884 case OO_CaretEqual: 11885 case OO_AmpEqual: 11886 case OO_PipeEqual: 11887 case OO_LessLess: 11888 case OO_GreaterGreater: 11889 case OO_LessLessEqual: 11890 case OO_GreaterGreaterEqual: 11891 case OO_EqualEqual: 11892 case OO_ExclaimEqual: 11893 case OO_Spaceship: 11894 case OO_PlusPlus: 11895 case OO_MinusMinus: 11896 case OO_Comma: 11897 case OO_ArrowStar: 11898 case OO_Arrow: 11899 case OO_Call: 11900 case OO_Subscript: 11901 case OO_Conditional: 11902 case OO_Coawait: 11903 case NUM_OVERLOADED_OPERATORS: 11904 llvm_unreachable("Unexpected reduction identifier"); 11905 case OO_None: 11906 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 11907 if (II->isStr("max")) 11908 BOK = BO_GT; 11909 else if (II->isStr("min")) 11910 BOK = BO_LT; 11911 } 11912 break; 11913 } 11914 SourceRange ReductionIdRange; 11915 if (ReductionIdScopeSpec.isValid()) 11916 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 11917 else 11918 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 11919 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 11920 11921 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 11922 bool FirstIter = true; 11923 for (Expr *RefExpr : VarList) { 11924 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 11925 // OpenMP [2.1, C/C++] 11926 // A list item is a variable or array section, subject to the restrictions 11927 // specified in Section 2.4 on page 42 and in each of the sections 11928 // describing clauses and directives for which a list appears. 11929 // OpenMP [2.14.3.3, Restrictions, p.1] 11930 // A variable that is part of another variable (as an array or 11931 // structure element) cannot appear in a private clause. 11932 if (!FirstIter && IR != ER) 11933 ++IR; 11934 FirstIter = false; 11935 SourceLocation ELoc; 11936 SourceRange ERange; 11937 Expr *SimpleRefExpr = RefExpr; 11938 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 11939 /*AllowArraySection=*/true); 11940 if (Res.second) { 11941 // Try to find 'declare reduction' corresponding construct before using 11942 // builtin/overloaded operators. 11943 QualType Type = Context.DependentTy; 11944 CXXCastPath BasePath; 11945 ExprResult DeclareReductionRef = buildDeclareReductionRef( 11946 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 11947 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 11948 Expr *ReductionOp = nullptr; 11949 if (S.CurContext->isDependentContext() && 11950 (DeclareReductionRef.isUnset() || 11951 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 11952 ReductionOp = DeclareReductionRef.get(); 11953 // It will be analyzed later. 11954 RD.push(RefExpr, ReductionOp); 11955 } 11956 ValueDecl *D = Res.first; 11957 if (!D) 11958 continue; 11959 11960 Expr *TaskgroupDescriptor = nullptr; 11961 QualType Type; 11962 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 11963 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 11964 if (ASE) { 11965 Type = ASE->getType().getNonReferenceType(); 11966 } else if (OASE) { 11967 QualType BaseType = 11968 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 11969 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 11970 Type = ATy->getElementType(); 11971 else 11972 Type = BaseType->getPointeeType(); 11973 Type = Type.getNonReferenceType(); 11974 } else { 11975 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 11976 } 11977 auto *VD = dyn_cast<VarDecl>(D); 11978 11979 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 11980 // A variable that appears in a private clause must not have an incomplete 11981 // type or a reference type. 11982 if (S.RequireCompleteType(ELoc, D->getType(), 11983 diag::err_omp_reduction_incomplete_type)) 11984 continue; 11985 // OpenMP [2.14.3.6, reduction clause, Restrictions] 11986 // A list item that appears in a reduction clause must not be 11987 // const-qualified. 11988 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc, 11989 /*AcceptIfMutable*/ false, ASE || OASE)) 11990 continue; 11991 11992 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 11993 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 11994 // If a list-item is a reference type then it must bind to the same object 11995 // for all threads of the team. 11996 if (!ASE && !OASE) { 11997 if (VD) { 11998 VarDecl *VDDef = VD->getDefinition(); 11999 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 12000 DSARefChecker Check(Stack); 12001 if (Check.Visit(VDDef->getInit())) { 12002 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 12003 << getOpenMPClauseName(ClauseKind) << ERange; 12004 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 12005 continue; 12006 } 12007 } 12008 } 12009 12010 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 12011 // in a Construct] 12012 // Variables with the predetermined data-sharing attributes may not be 12013 // listed in data-sharing attributes clauses, except for the cases 12014 // listed below. For these exceptions only, listing a predetermined 12015 // variable in a data-sharing attribute clause is allowed and overrides 12016 // the variable's predetermined data-sharing attributes. 12017 // OpenMP [2.14.3.6, Restrictions, p.3] 12018 // Any number of reduction clauses can be specified on the directive, 12019 // but a list item can appear only once in the reduction clauses for that 12020 // directive. 12021 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 12022 if (DVar.CKind == OMPC_reduction) { 12023 S.Diag(ELoc, diag::err_omp_once_referenced) 12024 << getOpenMPClauseName(ClauseKind); 12025 if (DVar.RefExpr) 12026 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 12027 continue; 12028 } 12029 if (DVar.CKind != OMPC_unknown) { 12030 S.Diag(ELoc, diag::err_omp_wrong_dsa) 12031 << getOpenMPClauseName(DVar.CKind) 12032 << getOpenMPClauseName(OMPC_reduction); 12033 reportOriginalDsa(S, Stack, D, DVar); 12034 continue; 12035 } 12036 12037 // OpenMP [2.14.3.6, Restrictions, p.1] 12038 // A list item that appears in a reduction clause of a worksharing 12039 // construct must be shared in the parallel regions to which any of the 12040 // worksharing regions arising from the worksharing construct bind. 12041 if (isOpenMPWorksharingDirective(CurrDir) && 12042 !isOpenMPParallelDirective(CurrDir) && 12043 !isOpenMPTeamsDirective(CurrDir)) { 12044 DVar = Stack->getImplicitDSA(D, true); 12045 if (DVar.CKind != OMPC_shared) { 12046 S.Diag(ELoc, diag::err_omp_required_access) 12047 << getOpenMPClauseName(OMPC_reduction) 12048 << getOpenMPClauseName(OMPC_shared); 12049 reportOriginalDsa(S, Stack, D, DVar); 12050 continue; 12051 } 12052 } 12053 } 12054 12055 // Try to find 'declare reduction' corresponding construct before using 12056 // builtin/overloaded operators. 12057 CXXCastPath BasePath; 12058 ExprResult DeclareReductionRef = buildDeclareReductionRef( 12059 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 12060 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 12061 if (DeclareReductionRef.isInvalid()) 12062 continue; 12063 if (S.CurContext->isDependentContext() && 12064 (DeclareReductionRef.isUnset() || 12065 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 12066 RD.push(RefExpr, DeclareReductionRef.get()); 12067 continue; 12068 } 12069 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 12070 // Not allowed reduction identifier is found. 12071 S.Diag(ReductionId.getBeginLoc(), 12072 diag::err_omp_unknown_reduction_identifier) 12073 << Type << ReductionIdRange; 12074 continue; 12075 } 12076 12077 // OpenMP [2.14.3.6, reduction clause, Restrictions] 12078 // The type of a list item that appears in a reduction clause must be valid 12079 // for the reduction-identifier. For a max or min reduction in C, the type 12080 // of the list item must be an allowed arithmetic data type: char, int, 12081 // float, double, or _Bool, possibly modified with long, short, signed, or 12082 // unsigned. For a max or min reduction in C++, the type of the list item 12083 // must be an allowed arithmetic data type: char, wchar_t, int, float, 12084 // double, or bool, possibly modified with long, short, signed, or unsigned. 12085 if (DeclareReductionRef.isUnset()) { 12086 if ((BOK == BO_GT || BOK == BO_LT) && 12087 !(Type->isScalarType() || 12088 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 12089 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 12090 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 12091 if (!ASE && !OASE) { 12092 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 12093 VarDecl::DeclarationOnly; 12094 S.Diag(D->getLocation(), 12095 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 12096 << D; 12097 } 12098 continue; 12099 } 12100 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 12101 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 12102 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 12103 << getOpenMPClauseName(ClauseKind); 12104 if (!ASE && !OASE) { 12105 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 12106 VarDecl::DeclarationOnly; 12107 S.Diag(D->getLocation(), 12108 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 12109 << D; 12110 } 12111 continue; 12112 } 12113 } 12114 12115 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 12116 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 12117 D->hasAttrs() ? &D->getAttrs() : nullptr); 12118 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 12119 D->hasAttrs() ? &D->getAttrs() : nullptr); 12120 QualType PrivateTy = Type; 12121 12122 // Try if we can determine constant lengths for all array sections and avoid 12123 // the VLA. 12124 bool ConstantLengthOASE = false; 12125 if (OASE) { 12126 bool SingleElement; 12127 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 12128 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 12129 Context, OASE, SingleElement, ArraySizes); 12130 12131 // If we don't have a single element, we must emit a constant array type. 12132 if (ConstantLengthOASE && !SingleElement) { 12133 for (llvm::APSInt &Size : ArraySizes) 12134 PrivateTy = Context.getConstantArrayType( 12135 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0); 12136 } 12137 } 12138 12139 if ((OASE && !ConstantLengthOASE) || 12140 (!OASE && !ASE && 12141 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 12142 if (!Context.getTargetInfo().isVLASupported() && 12143 S.shouldDiagnoseTargetSupportFromOpenMP()) { 12144 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 12145 S.Diag(ELoc, diag::note_vla_unsupported); 12146 continue; 12147 } 12148 // For arrays/array sections only: 12149 // Create pseudo array type for private copy. The size for this array will 12150 // be generated during codegen. 12151 // For array subscripts or single variables Private Ty is the same as Type 12152 // (type of the variable or single array element). 12153 PrivateTy = Context.getVariableArrayType( 12154 Type, 12155 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue), 12156 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 12157 } else if (!ASE && !OASE && 12158 Context.getAsArrayType(D->getType().getNonReferenceType())) { 12159 PrivateTy = D->getType().getNonReferenceType(); 12160 } 12161 // Private copy. 12162 VarDecl *PrivateVD = 12163 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 12164 D->hasAttrs() ? &D->getAttrs() : nullptr, 12165 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 12166 // Add initializer for private variable. 12167 Expr *Init = nullptr; 12168 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 12169 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 12170 if (DeclareReductionRef.isUsable()) { 12171 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 12172 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 12173 if (DRD->getInitializer()) { 12174 Init = DRDRef; 12175 RHSVD->setInit(DRDRef); 12176 RHSVD->setInitStyle(VarDecl::CallInit); 12177 } 12178 } else { 12179 switch (BOK) { 12180 case BO_Add: 12181 case BO_Xor: 12182 case BO_Or: 12183 case BO_LOr: 12184 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 12185 if (Type->isScalarType() || Type->isAnyComplexType()) 12186 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 12187 break; 12188 case BO_Mul: 12189 case BO_LAnd: 12190 if (Type->isScalarType() || Type->isAnyComplexType()) { 12191 // '*' and '&&' reduction ops - initializer is '1'. 12192 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 12193 } 12194 break; 12195 case BO_And: { 12196 // '&' reduction op - initializer is '~0'. 12197 QualType OrigType = Type; 12198 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 12199 Type = ComplexTy->getElementType(); 12200 if (Type->isRealFloatingType()) { 12201 llvm::APFloat InitValue = 12202 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type), 12203 /*isIEEE=*/true); 12204 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 12205 Type, ELoc); 12206 } else if (Type->isScalarType()) { 12207 uint64_t Size = Context.getTypeSize(Type); 12208 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 12209 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 12210 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 12211 } 12212 if (Init && OrigType->isAnyComplexType()) { 12213 // Init = 0xFFFF + 0xFFFFi; 12214 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 12215 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 12216 } 12217 Type = OrigType; 12218 break; 12219 } 12220 case BO_LT: 12221 case BO_GT: { 12222 // 'min' reduction op - initializer is 'Largest representable number in 12223 // the reduction list item type'. 12224 // 'max' reduction op - initializer is 'Least representable number in 12225 // the reduction list item type'. 12226 if (Type->isIntegerType() || Type->isPointerType()) { 12227 bool IsSigned = Type->hasSignedIntegerRepresentation(); 12228 uint64_t Size = Context.getTypeSize(Type); 12229 QualType IntTy = 12230 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 12231 llvm::APInt InitValue = 12232 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 12233 : llvm::APInt::getMinValue(Size) 12234 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 12235 : llvm::APInt::getMaxValue(Size); 12236 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 12237 if (Type->isPointerType()) { 12238 // Cast to pointer type. 12239 ExprResult CastExpr = S.BuildCStyleCastExpr( 12240 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 12241 if (CastExpr.isInvalid()) 12242 continue; 12243 Init = CastExpr.get(); 12244 } 12245 } else if (Type->isRealFloatingType()) { 12246 llvm::APFloat InitValue = llvm::APFloat::getLargest( 12247 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 12248 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 12249 Type, ELoc); 12250 } 12251 break; 12252 } 12253 case BO_PtrMemD: 12254 case BO_PtrMemI: 12255 case BO_MulAssign: 12256 case BO_Div: 12257 case BO_Rem: 12258 case BO_Sub: 12259 case BO_Shl: 12260 case BO_Shr: 12261 case BO_LE: 12262 case BO_GE: 12263 case BO_EQ: 12264 case BO_NE: 12265 case BO_Cmp: 12266 case BO_AndAssign: 12267 case BO_XorAssign: 12268 case BO_OrAssign: 12269 case BO_Assign: 12270 case BO_AddAssign: 12271 case BO_SubAssign: 12272 case BO_DivAssign: 12273 case BO_RemAssign: 12274 case BO_ShlAssign: 12275 case BO_ShrAssign: 12276 case BO_Comma: 12277 llvm_unreachable("Unexpected reduction operation"); 12278 } 12279 } 12280 if (Init && DeclareReductionRef.isUnset()) 12281 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 12282 else if (!Init) 12283 S.ActOnUninitializedDecl(RHSVD); 12284 if (RHSVD->isInvalidDecl()) 12285 continue; 12286 if (!RHSVD->hasInit() && 12287 (DeclareReductionRef.isUnset() || !S.LangOpts.CPlusPlus)) { 12288 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 12289 << Type << ReductionIdRange; 12290 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 12291 VarDecl::DeclarationOnly; 12292 S.Diag(D->getLocation(), 12293 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 12294 << D; 12295 continue; 12296 } 12297 // Store initializer for single element in private copy. Will be used during 12298 // codegen. 12299 PrivateVD->setInit(RHSVD->getInit()); 12300 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 12301 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 12302 ExprResult ReductionOp; 12303 if (DeclareReductionRef.isUsable()) { 12304 QualType RedTy = DeclareReductionRef.get()->getType(); 12305 QualType PtrRedTy = Context.getPointerType(RedTy); 12306 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 12307 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 12308 if (!BasePath.empty()) { 12309 LHS = S.DefaultLvalueConversion(LHS.get()); 12310 RHS = S.DefaultLvalueConversion(RHS.get()); 12311 LHS = ImplicitCastExpr::Create(Context, PtrRedTy, 12312 CK_UncheckedDerivedToBase, LHS.get(), 12313 &BasePath, LHS.get()->getValueKind()); 12314 RHS = ImplicitCastExpr::Create(Context, PtrRedTy, 12315 CK_UncheckedDerivedToBase, RHS.get(), 12316 &BasePath, RHS.get()->getValueKind()); 12317 } 12318 FunctionProtoType::ExtProtoInfo EPI; 12319 QualType Params[] = {PtrRedTy, PtrRedTy}; 12320 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 12321 auto *OVE = new (Context) OpaqueValueExpr( 12322 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, 12323 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 12324 Expr *Args[] = {LHS.get(), RHS.get()}; 12325 ReductionOp = 12326 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc); 12327 } else { 12328 ReductionOp = S.BuildBinOp( 12329 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE); 12330 if (ReductionOp.isUsable()) { 12331 if (BOK != BO_LT && BOK != BO_GT) { 12332 ReductionOp = 12333 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 12334 BO_Assign, LHSDRE, ReductionOp.get()); 12335 } else { 12336 auto *ConditionalOp = new (Context) 12337 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE, 12338 Type, VK_LValue, OK_Ordinary); 12339 ReductionOp = 12340 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 12341 BO_Assign, LHSDRE, ConditionalOp); 12342 } 12343 if (ReductionOp.isUsable()) 12344 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(), 12345 /*DiscardedValue*/ false); 12346 } 12347 if (!ReductionOp.isUsable()) 12348 continue; 12349 } 12350 12351 // OpenMP [2.15.4.6, Restrictions, p.2] 12352 // A list item that appears in an in_reduction clause of a task construct 12353 // must appear in a task_reduction clause of a construct associated with a 12354 // taskgroup region that includes the participating task in its taskgroup 12355 // set. The construct associated with the innermost region that meets this 12356 // condition must specify the same reduction-identifier as the in_reduction 12357 // clause. 12358 if (ClauseKind == OMPC_in_reduction) { 12359 SourceRange ParentSR; 12360 BinaryOperatorKind ParentBOK; 12361 const Expr *ParentReductionOp; 12362 Expr *ParentBOKTD, *ParentReductionOpTD; 12363 DSAStackTy::DSAVarData ParentBOKDSA = 12364 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 12365 ParentBOKTD); 12366 DSAStackTy::DSAVarData ParentReductionOpDSA = 12367 Stack->getTopMostTaskgroupReductionData( 12368 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 12369 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 12370 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 12371 if (!IsParentBOK && !IsParentReductionOp) { 12372 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction); 12373 continue; 12374 } 12375 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 12376 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK || 12377 IsParentReductionOp) { 12378 bool EmitError = true; 12379 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 12380 llvm::FoldingSetNodeID RedId, ParentRedId; 12381 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 12382 DeclareReductionRef.get()->Profile(RedId, Context, 12383 /*Canonical=*/true); 12384 EmitError = RedId != ParentRedId; 12385 } 12386 if (EmitError) { 12387 S.Diag(ReductionId.getBeginLoc(), 12388 diag::err_omp_reduction_identifier_mismatch) 12389 << ReductionIdRange << RefExpr->getSourceRange(); 12390 S.Diag(ParentSR.getBegin(), 12391 diag::note_omp_previous_reduction_identifier) 12392 << ParentSR 12393 << (IsParentBOK ? ParentBOKDSA.RefExpr 12394 : ParentReductionOpDSA.RefExpr) 12395 ->getSourceRange(); 12396 continue; 12397 } 12398 } 12399 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 12400 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined."); 12401 } 12402 12403 DeclRefExpr *Ref = nullptr; 12404 Expr *VarsExpr = RefExpr->IgnoreParens(); 12405 if (!VD && !S.CurContext->isDependentContext()) { 12406 if (ASE || OASE) { 12407 TransformExprToCaptures RebuildToCapture(S, D); 12408 VarsExpr = 12409 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 12410 Ref = RebuildToCapture.getCapturedExpr(); 12411 } else { 12412 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 12413 } 12414 if (!S.isOpenMPCapturedDecl(D)) { 12415 RD.ExprCaptures.emplace_back(Ref->getDecl()); 12416 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 12417 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 12418 if (!RefRes.isUsable()) 12419 continue; 12420 ExprResult PostUpdateRes = 12421 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 12422 RefRes.get()); 12423 if (!PostUpdateRes.isUsable()) 12424 continue; 12425 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 12426 Stack->getCurrentDirective() == OMPD_taskgroup) { 12427 S.Diag(RefExpr->getExprLoc(), 12428 diag::err_omp_reduction_non_addressable_expression) 12429 << RefExpr->getSourceRange(); 12430 continue; 12431 } 12432 RD.ExprPostUpdates.emplace_back( 12433 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 12434 } 12435 } 12436 } 12437 // All reduction items are still marked as reduction (to do not increase 12438 // code base size). 12439 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref); 12440 if (CurrDir == OMPD_taskgroup) { 12441 if (DeclareReductionRef.isUsable()) 12442 Stack->addTaskgroupReductionData(D, ReductionIdRange, 12443 DeclareReductionRef.get()); 12444 else 12445 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 12446 } 12447 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 12448 TaskgroupDescriptor); 12449 } 12450 return RD.Vars.empty(); 12451 } 12452 12453 OMPClause *Sema::ActOnOpenMPReductionClause( 12454 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 12455 SourceLocation ColonLoc, SourceLocation EndLoc, 12456 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 12457 ArrayRef<Expr *> UnresolvedReductions) { 12458 ReductionData RD(VarList.size()); 12459 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 12460 StartLoc, LParenLoc, ColonLoc, EndLoc, 12461 ReductionIdScopeSpec, ReductionId, 12462 UnresolvedReductions, RD)) 12463 return nullptr; 12464 12465 return OMPReductionClause::Create( 12466 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 12467 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 12468 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 12469 buildPreInits(Context, RD.ExprCaptures), 12470 buildPostUpdate(*this, RD.ExprPostUpdates)); 12471 } 12472 12473 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 12474 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 12475 SourceLocation ColonLoc, SourceLocation EndLoc, 12476 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 12477 ArrayRef<Expr *> UnresolvedReductions) { 12478 ReductionData RD(VarList.size()); 12479 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 12480 StartLoc, LParenLoc, ColonLoc, EndLoc, 12481 ReductionIdScopeSpec, ReductionId, 12482 UnresolvedReductions, RD)) 12483 return nullptr; 12484 12485 return OMPTaskReductionClause::Create( 12486 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 12487 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 12488 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 12489 buildPreInits(Context, RD.ExprCaptures), 12490 buildPostUpdate(*this, RD.ExprPostUpdates)); 12491 } 12492 12493 OMPClause *Sema::ActOnOpenMPInReductionClause( 12494 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 12495 SourceLocation ColonLoc, SourceLocation EndLoc, 12496 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 12497 ArrayRef<Expr *> UnresolvedReductions) { 12498 ReductionData RD(VarList.size()); 12499 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 12500 StartLoc, LParenLoc, ColonLoc, EndLoc, 12501 ReductionIdScopeSpec, ReductionId, 12502 UnresolvedReductions, RD)) 12503 return nullptr; 12504 12505 return OMPInReductionClause::Create( 12506 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 12507 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 12508 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 12509 buildPreInits(Context, RD.ExprCaptures), 12510 buildPostUpdate(*this, RD.ExprPostUpdates)); 12511 } 12512 12513 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 12514 SourceLocation LinLoc) { 12515 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 12516 LinKind == OMPC_LINEAR_unknown) { 12517 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 12518 return true; 12519 } 12520 return false; 12521 } 12522 12523 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 12524 OpenMPLinearClauseKind LinKind, 12525 QualType Type) { 12526 const auto *VD = dyn_cast_or_null<VarDecl>(D); 12527 // A variable must not have an incomplete type or a reference type. 12528 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 12529 return true; 12530 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 12531 !Type->isReferenceType()) { 12532 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 12533 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 12534 return true; 12535 } 12536 Type = Type.getNonReferenceType(); 12537 12538 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 12539 // A variable that is privatized must not have a const-qualified type 12540 // unless it is of class type with a mutable member. This restriction does 12541 // not apply to the firstprivate clause. 12542 if (rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc)) 12543 return true; 12544 12545 // A list item must be of integral or pointer type. 12546 Type = Type.getUnqualifiedType().getCanonicalType(); 12547 const auto *Ty = Type.getTypePtrOrNull(); 12548 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && 12549 !Ty->isPointerType())) { 12550 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 12551 if (D) { 12552 bool IsDecl = 12553 !VD || 12554 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 12555 Diag(D->getLocation(), 12556 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 12557 << D; 12558 } 12559 return true; 12560 } 12561 return false; 12562 } 12563 12564 OMPClause *Sema::ActOnOpenMPLinearClause( 12565 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 12566 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 12567 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 12568 SmallVector<Expr *, 8> Vars; 12569 SmallVector<Expr *, 8> Privates; 12570 SmallVector<Expr *, 8> Inits; 12571 SmallVector<Decl *, 4> ExprCaptures; 12572 SmallVector<Expr *, 4> ExprPostUpdates; 12573 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 12574 LinKind = OMPC_LINEAR_val; 12575 for (Expr *RefExpr : VarList) { 12576 assert(RefExpr && "NULL expr in OpenMP linear clause."); 12577 SourceLocation ELoc; 12578 SourceRange ERange; 12579 Expr *SimpleRefExpr = RefExpr; 12580 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 12581 if (Res.second) { 12582 // It will be analyzed later. 12583 Vars.push_back(RefExpr); 12584 Privates.push_back(nullptr); 12585 Inits.push_back(nullptr); 12586 } 12587 ValueDecl *D = Res.first; 12588 if (!D) 12589 continue; 12590 12591 QualType Type = D->getType(); 12592 auto *VD = dyn_cast<VarDecl>(D); 12593 12594 // OpenMP [2.14.3.7, linear clause] 12595 // A list-item cannot appear in more than one linear clause. 12596 // A list-item that appears in a linear clause cannot appear in any 12597 // other data-sharing attribute clause. 12598 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 12599 if (DVar.RefExpr) { 12600 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 12601 << getOpenMPClauseName(OMPC_linear); 12602 reportOriginalDsa(*this, DSAStack, D, DVar); 12603 continue; 12604 } 12605 12606 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 12607 continue; 12608 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 12609 12610 // Build private copy of original var. 12611 VarDecl *Private = 12612 buildVarDecl(*this, ELoc, Type, D->getName(), 12613 D->hasAttrs() ? &D->getAttrs() : nullptr, 12614 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 12615 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 12616 // Build var to save initial value. 12617 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 12618 Expr *InitExpr; 12619 DeclRefExpr *Ref = nullptr; 12620 if (!VD && !CurContext->isDependentContext()) { 12621 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 12622 if (!isOpenMPCapturedDecl(D)) { 12623 ExprCaptures.push_back(Ref->getDecl()); 12624 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 12625 ExprResult RefRes = DefaultLvalueConversion(Ref); 12626 if (!RefRes.isUsable()) 12627 continue; 12628 ExprResult PostUpdateRes = 12629 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 12630 SimpleRefExpr, RefRes.get()); 12631 if (!PostUpdateRes.isUsable()) 12632 continue; 12633 ExprPostUpdates.push_back( 12634 IgnoredValueConversions(PostUpdateRes.get()).get()); 12635 } 12636 } 12637 } 12638 if (LinKind == OMPC_LINEAR_uval) 12639 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 12640 else 12641 InitExpr = VD ? SimpleRefExpr : Ref; 12642 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 12643 /*DirectInit=*/false); 12644 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 12645 12646 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 12647 Vars.push_back((VD || CurContext->isDependentContext()) 12648 ? RefExpr->IgnoreParens() 12649 : Ref); 12650 Privates.push_back(PrivateRef); 12651 Inits.push_back(InitRef); 12652 } 12653 12654 if (Vars.empty()) 12655 return nullptr; 12656 12657 Expr *StepExpr = Step; 12658 Expr *CalcStepExpr = nullptr; 12659 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 12660 !Step->isInstantiationDependent() && 12661 !Step->containsUnexpandedParameterPack()) { 12662 SourceLocation StepLoc = Step->getBeginLoc(); 12663 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 12664 if (Val.isInvalid()) 12665 return nullptr; 12666 StepExpr = Val.get(); 12667 12668 // Build var to save the step value. 12669 VarDecl *SaveVar = 12670 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 12671 ExprResult SaveRef = 12672 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 12673 ExprResult CalcStep = 12674 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 12675 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false); 12676 12677 // Warn about zero linear step (it would be probably better specified as 12678 // making corresponding variables 'const'). 12679 llvm::APSInt Result; 12680 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); 12681 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) 12682 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] 12683 << (Vars.size() > 1); 12684 if (!IsConstant && CalcStep.isUsable()) { 12685 // Calculate the step beforehand instead of doing this on each iteration. 12686 // (This is not used if the number of iterations may be kfold-ed). 12687 CalcStepExpr = CalcStep.get(); 12688 } 12689 } 12690 12691 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 12692 ColonLoc, EndLoc, Vars, Privates, Inits, 12693 StepExpr, CalcStepExpr, 12694 buildPreInits(Context, ExprCaptures), 12695 buildPostUpdate(*this, ExprPostUpdates)); 12696 } 12697 12698 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 12699 Expr *NumIterations, Sema &SemaRef, 12700 Scope *S, DSAStackTy *Stack) { 12701 // Walk the vars and build update/final expressions for the CodeGen. 12702 SmallVector<Expr *, 8> Updates; 12703 SmallVector<Expr *, 8> Finals; 12704 Expr *Step = Clause.getStep(); 12705 Expr *CalcStep = Clause.getCalcStep(); 12706 // OpenMP [2.14.3.7, linear clause] 12707 // If linear-step is not specified it is assumed to be 1. 12708 if (!Step) 12709 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 12710 else if (CalcStep) 12711 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 12712 bool HasErrors = false; 12713 auto CurInit = Clause.inits().begin(); 12714 auto CurPrivate = Clause.privates().begin(); 12715 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 12716 for (Expr *RefExpr : Clause.varlists()) { 12717 SourceLocation ELoc; 12718 SourceRange ERange; 12719 Expr *SimpleRefExpr = RefExpr; 12720 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange); 12721 ValueDecl *D = Res.first; 12722 if (Res.second || !D) { 12723 Updates.push_back(nullptr); 12724 Finals.push_back(nullptr); 12725 HasErrors = true; 12726 continue; 12727 } 12728 auto &&Info = Stack->isLoopControlVariable(D); 12729 // OpenMP [2.15.11, distribute simd Construct] 12730 // A list item may not appear in a linear clause, unless it is the loop 12731 // iteration variable. 12732 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 12733 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 12734 SemaRef.Diag(ELoc, 12735 diag::err_omp_linear_distribute_var_non_loop_iteration); 12736 Updates.push_back(nullptr); 12737 Finals.push_back(nullptr); 12738 HasErrors = true; 12739 continue; 12740 } 12741 Expr *InitExpr = *CurInit; 12742 12743 // Build privatized reference to the current linear var. 12744 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 12745 Expr *CapturedRef; 12746 if (LinKind == OMPC_LINEAR_uval) 12747 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 12748 else 12749 CapturedRef = 12750 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 12751 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 12752 /*RefersToCapture=*/true); 12753 12754 // Build update: Var = InitExpr + IV * Step 12755 ExprResult Update; 12756 if (!Info.first) 12757 Update = 12758 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, 12759 InitExpr, IV, Step, /* Subtract */ false); 12760 else 12761 Update = *CurPrivate; 12762 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(), 12763 /*DiscardedValue*/ false); 12764 12765 // Build final: Var = InitExpr + NumIterations * Step 12766 ExprResult Final; 12767 if (!Info.first) 12768 Final = 12769 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 12770 InitExpr, NumIterations, Step, /*Subtract=*/false); 12771 else 12772 Final = *CurPrivate; 12773 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(), 12774 /*DiscardedValue*/ false); 12775 12776 if (!Update.isUsable() || !Final.isUsable()) { 12777 Updates.push_back(nullptr); 12778 Finals.push_back(nullptr); 12779 HasErrors = true; 12780 } else { 12781 Updates.push_back(Update.get()); 12782 Finals.push_back(Final.get()); 12783 } 12784 ++CurInit; 12785 ++CurPrivate; 12786 } 12787 Clause.setUpdates(Updates); 12788 Clause.setFinals(Finals); 12789 return HasErrors; 12790 } 12791 12792 OMPClause *Sema::ActOnOpenMPAlignedClause( 12793 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 12794 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 12795 SmallVector<Expr *, 8> Vars; 12796 for (Expr *RefExpr : VarList) { 12797 assert(RefExpr && "NULL expr in OpenMP linear clause."); 12798 SourceLocation ELoc; 12799 SourceRange ERange; 12800 Expr *SimpleRefExpr = RefExpr; 12801 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 12802 if (Res.second) { 12803 // It will be analyzed later. 12804 Vars.push_back(RefExpr); 12805 } 12806 ValueDecl *D = Res.first; 12807 if (!D) 12808 continue; 12809 12810 QualType QType = D->getType(); 12811 auto *VD = dyn_cast<VarDecl>(D); 12812 12813 // OpenMP [2.8.1, simd construct, Restrictions] 12814 // The type of list items appearing in the aligned clause must be 12815 // array, pointer, reference to array, or reference to pointer. 12816 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 12817 const Type *Ty = QType.getTypePtrOrNull(); 12818 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 12819 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 12820 << QType << getLangOpts().CPlusPlus << ERange; 12821 bool IsDecl = 12822 !VD || 12823 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 12824 Diag(D->getLocation(), 12825 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 12826 << D; 12827 continue; 12828 } 12829 12830 // OpenMP [2.8.1, simd construct, Restrictions] 12831 // A list-item cannot appear in more than one aligned clause. 12832 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 12833 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange; 12834 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 12835 << getOpenMPClauseName(OMPC_aligned); 12836 continue; 12837 } 12838 12839 DeclRefExpr *Ref = nullptr; 12840 if (!VD && isOpenMPCapturedDecl(D)) 12841 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 12842 Vars.push_back(DefaultFunctionArrayConversion( 12843 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 12844 .get()); 12845 } 12846 12847 // OpenMP [2.8.1, simd construct, Description] 12848 // The parameter of the aligned clause, alignment, must be a constant 12849 // positive integer expression. 12850 // If no optional parameter is specified, implementation-defined default 12851 // alignments for SIMD instructions on the target platforms are assumed. 12852 if (Alignment != nullptr) { 12853 ExprResult AlignResult = 12854 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 12855 if (AlignResult.isInvalid()) 12856 return nullptr; 12857 Alignment = AlignResult.get(); 12858 } 12859 if (Vars.empty()) 12860 return nullptr; 12861 12862 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 12863 EndLoc, Vars, Alignment); 12864 } 12865 12866 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 12867 SourceLocation StartLoc, 12868 SourceLocation LParenLoc, 12869 SourceLocation EndLoc) { 12870 SmallVector<Expr *, 8> Vars; 12871 SmallVector<Expr *, 8> SrcExprs; 12872 SmallVector<Expr *, 8> DstExprs; 12873 SmallVector<Expr *, 8> AssignmentOps; 12874 for (Expr *RefExpr : VarList) { 12875 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 12876 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 12877 // It will be analyzed later. 12878 Vars.push_back(RefExpr); 12879 SrcExprs.push_back(nullptr); 12880 DstExprs.push_back(nullptr); 12881 AssignmentOps.push_back(nullptr); 12882 continue; 12883 } 12884 12885 SourceLocation ELoc = RefExpr->getExprLoc(); 12886 // OpenMP [2.1, C/C++] 12887 // A list item is a variable name. 12888 // OpenMP [2.14.4.1, Restrictions, p.1] 12889 // A list item that appears in a copyin clause must be threadprivate. 12890 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 12891 if (!DE || !isa<VarDecl>(DE->getDecl())) { 12892 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 12893 << 0 << RefExpr->getSourceRange(); 12894 continue; 12895 } 12896 12897 Decl *D = DE->getDecl(); 12898 auto *VD = cast<VarDecl>(D); 12899 12900 QualType Type = VD->getType(); 12901 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 12902 // It will be analyzed later. 12903 Vars.push_back(DE); 12904 SrcExprs.push_back(nullptr); 12905 DstExprs.push_back(nullptr); 12906 AssignmentOps.push_back(nullptr); 12907 continue; 12908 } 12909 12910 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 12911 // A list item that appears in a copyin clause must be threadprivate. 12912 if (!DSAStack->isThreadPrivate(VD)) { 12913 Diag(ELoc, diag::err_omp_required_access) 12914 << getOpenMPClauseName(OMPC_copyin) 12915 << getOpenMPDirectiveName(OMPD_threadprivate); 12916 continue; 12917 } 12918 12919 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 12920 // A variable of class type (or array thereof) that appears in a 12921 // copyin clause requires an accessible, unambiguous copy assignment 12922 // operator for the class type. 12923 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 12924 VarDecl *SrcVD = 12925 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(), 12926 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 12927 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 12928 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 12929 VarDecl *DstVD = 12930 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst", 12931 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 12932 DeclRefExpr *PseudoDstExpr = 12933 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 12934 // For arrays generate assignment operation for single element and replace 12935 // it by the original array element in CodeGen. 12936 ExprResult AssignmentOp = 12937 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 12938 PseudoSrcExpr); 12939 if (AssignmentOp.isInvalid()) 12940 continue; 12941 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 12942 /*DiscardedValue*/ false); 12943 if (AssignmentOp.isInvalid()) 12944 continue; 12945 12946 DSAStack->addDSA(VD, DE, OMPC_copyin); 12947 Vars.push_back(DE); 12948 SrcExprs.push_back(PseudoSrcExpr); 12949 DstExprs.push_back(PseudoDstExpr); 12950 AssignmentOps.push_back(AssignmentOp.get()); 12951 } 12952 12953 if (Vars.empty()) 12954 return nullptr; 12955 12956 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 12957 SrcExprs, DstExprs, AssignmentOps); 12958 } 12959 12960 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 12961 SourceLocation StartLoc, 12962 SourceLocation LParenLoc, 12963 SourceLocation EndLoc) { 12964 SmallVector<Expr *, 8> Vars; 12965 SmallVector<Expr *, 8> SrcExprs; 12966 SmallVector<Expr *, 8> DstExprs; 12967 SmallVector<Expr *, 8> AssignmentOps; 12968 for (Expr *RefExpr : VarList) { 12969 assert(RefExpr && "NULL expr in OpenMP linear clause."); 12970 SourceLocation ELoc; 12971 SourceRange ERange; 12972 Expr *SimpleRefExpr = RefExpr; 12973 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 12974 if (Res.second) { 12975 // It will be analyzed later. 12976 Vars.push_back(RefExpr); 12977 SrcExprs.push_back(nullptr); 12978 DstExprs.push_back(nullptr); 12979 AssignmentOps.push_back(nullptr); 12980 } 12981 ValueDecl *D = Res.first; 12982 if (!D) 12983 continue; 12984 12985 QualType Type = D->getType(); 12986 auto *VD = dyn_cast<VarDecl>(D); 12987 12988 // OpenMP [2.14.4.2, Restrictions, p.2] 12989 // A list item that appears in a copyprivate clause may not appear in a 12990 // private or firstprivate clause on the single construct. 12991 if (!VD || !DSAStack->isThreadPrivate(VD)) { 12992 DSAStackTy::DSAVarData DVar = 12993 DSAStack->getTopDSA(D, /*FromParent=*/false); 12994 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 12995 DVar.RefExpr) { 12996 Diag(ELoc, diag::err_omp_wrong_dsa) 12997 << getOpenMPClauseName(DVar.CKind) 12998 << getOpenMPClauseName(OMPC_copyprivate); 12999 reportOriginalDsa(*this, DSAStack, D, DVar); 13000 continue; 13001 } 13002 13003 // OpenMP [2.11.4.2, Restrictions, p.1] 13004 // All list items that appear in a copyprivate clause must be either 13005 // threadprivate or private in the enclosing context. 13006 if (DVar.CKind == OMPC_unknown) { 13007 DVar = DSAStack->getImplicitDSA(D, false); 13008 if (DVar.CKind == OMPC_shared) { 13009 Diag(ELoc, diag::err_omp_required_access) 13010 << getOpenMPClauseName(OMPC_copyprivate) 13011 << "threadprivate or private in the enclosing context"; 13012 reportOriginalDsa(*this, DSAStack, D, DVar); 13013 continue; 13014 } 13015 } 13016 } 13017 13018 // Variably modified types are not supported. 13019 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 13020 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 13021 << getOpenMPClauseName(OMPC_copyprivate) << Type 13022 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 13023 bool IsDecl = 13024 !VD || 13025 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 13026 Diag(D->getLocation(), 13027 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 13028 << D; 13029 continue; 13030 } 13031 13032 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 13033 // A variable of class type (or array thereof) that appears in a 13034 // copyin clause requires an accessible, unambiguous copy assignment 13035 // operator for the class type. 13036 Type = Context.getBaseElementType(Type.getNonReferenceType()) 13037 .getUnqualifiedType(); 13038 VarDecl *SrcVD = 13039 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src", 13040 D->hasAttrs() ? &D->getAttrs() : nullptr); 13041 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 13042 VarDecl *DstVD = 13043 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst", 13044 D->hasAttrs() ? &D->getAttrs() : nullptr); 13045 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 13046 ExprResult AssignmentOp = BuildBinOp( 13047 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 13048 if (AssignmentOp.isInvalid()) 13049 continue; 13050 AssignmentOp = 13051 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 13052 if (AssignmentOp.isInvalid()) 13053 continue; 13054 13055 // No need to mark vars as copyprivate, they are already threadprivate or 13056 // implicitly private. 13057 assert(VD || isOpenMPCapturedDecl(D)); 13058 Vars.push_back( 13059 VD ? RefExpr->IgnoreParens() 13060 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 13061 SrcExprs.push_back(PseudoSrcExpr); 13062 DstExprs.push_back(PseudoDstExpr); 13063 AssignmentOps.push_back(AssignmentOp.get()); 13064 } 13065 13066 if (Vars.empty()) 13067 return nullptr; 13068 13069 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 13070 Vars, SrcExprs, DstExprs, AssignmentOps); 13071 } 13072 13073 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 13074 SourceLocation StartLoc, 13075 SourceLocation LParenLoc, 13076 SourceLocation EndLoc) { 13077 if (VarList.empty()) 13078 return nullptr; 13079 13080 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 13081 } 13082 13083 OMPClause * 13084 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind, 13085 SourceLocation DepLoc, SourceLocation ColonLoc, 13086 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 13087 SourceLocation LParenLoc, SourceLocation EndLoc) { 13088 if (DSAStack->getCurrentDirective() == OMPD_ordered && 13089 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 13090 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 13091 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 13092 return nullptr; 13093 } 13094 if (DSAStack->getCurrentDirective() != OMPD_ordered && 13095 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 13096 DepKind == OMPC_DEPEND_sink)) { 13097 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink}; 13098 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 13099 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 13100 /*Last=*/OMPC_DEPEND_unknown, Except) 13101 << getOpenMPClauseName(OMPC_depend); 13102 return nullptr; 13103 } 13104 SmallVector<Expr *, 8> Vars; 13105 DSAStackTy::OperatorOffsetTy OpsOffs; 13106 llvm::APSInt DepCounter(/*BitWidth=*/32); 13107 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 13108 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) { 13109 if (const Expr *OrderedCountExpr = 13110 DSAStack->getParentOrderedRegionParam().first) { 13111 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 13112 TotalDepCount.setIsUnsigned(/*Val=*/true); 13113 } 13114 } 13115 for (Expr *RefExpr : VarList) { 13116 assert(RefExpr && "NULL expr in OpenMP shared clause."); 13117 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 13118 // It will be analyzed later. 13119 Vars.push_back(RefExpr); 13120 continue; 13121 } 13122 13123 SourceLocation ELoc = RefExpr->getExprLoc(); 13124 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 13125 if (DepKind == OMPC_DEPEND_sink) { 13126 if (DSAStack->getParentOrderedRegionParam().first && 13127 DepCounter >= TotalDepCount) { 13128 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 13129 continue; 13130 } 13131 ++DepCounter; 13132 // OpenMP [2.13.9, Summary] 13133 // depend(dependence-type : vec), where dependence-type is: 13134 // 'sink' and where vec is the iteration vector, which has the form: 13135 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 13136 // where n is the value specified by the ordered clause in the loop 13137 // directive, xi denotes the loop iteration variable of the i-th nested 13138 // loop associated with the loop directive, and di is a constant 13139 // non-negative integer. 13140 if (CurContext->isDependentContext()) { 13141 // It will be analyzed later. 13142 Vars.push_back(RefExpr); 13143 continue; 13144 } 13145 SimpleExpr = SimpleExpr->IgnoreImplicit(); 13146 OverloadedOperatorKind OOK = OO_None; 13147 SourceLocation OOLoc; 13148 Expr *LHS = SimpleExpr; 13149 Expr *RHS = nullptr; 13150 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 13151 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 13152 OOLoc = BO->getOperatorLoc(); 13153 LHS = BO->getLHS()->IgnoreParenImpCasts(); 13154 RHS = BO->getRHS()->IgnoreParenImpCasts(); 13155 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 13156 OOK = OCE->getOperator(); 13157 OOLoc = OCE->getOperatorLoc(); 13158 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 13159 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 13160 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 13161 OOK = MCE->getMethodDecl() 13162 ->getNameInfo() 13163 .getName() 13164 .getCXXOverloadedOperator(); 13165 OOLoc = MCE->getCallee()->getExprLoc(); 13166 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 13167 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 13168 } 13169 SourceLocation ELoc; 13170 SourceRange ERange; 13171 auto Res = getPrivateItem(*this, LHS, ELoc, ERange); 13172 if (Res.second) { 13173 // It will be analyzed later. 13174 Vars.push_back(RefExpr); 13175 } 13176 ValueDecl *D = Res.first; 13177 if (!D) 13178 continue; 13179 13180 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 13181 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 13182 continue; 13183 } 13184 if (RHS) { 13185 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 13186 RHS, OMPC_depend, /*StrictlyPositive=*/false); 13187 if (RHSRes.isInvalid()) 13188 continue; 13189 } 13190 if (!CurContext->isDependentContext() && 13191 DSAStack->getParentOrderedRegionParam().first && 13192 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 13193 const ValueDecl *VD = 13194 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 13195 if (VD) 13196 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 13197 << 1 << VD; 13198 else 13199 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 13200 continue; 13201 } 13202 OpsOffs.emplace_back(RHS, OOK); 13203 } else { 13204 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 13205 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 13206 (ASE && 13207 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() && 13208 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 13209 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 13210 << RefExpr->getSourceRange(); 13211 continue; 13212 } 13213 bool Suppress = getDiagnostics().getSuppressAllDiagnostics(); 13214 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true); 13215 ExprResult Res = 13216 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts()); 13217 getDiagnostics().setSuppressAllDiagnostics(Suppress); 13218 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) { 13219 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 13220 << RefExpr->getSourceRange(); 13221 continue; 13222 } 13223 } 13224 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 13225 } 13226 13227 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 13228 TotalDepCount > VarList.size() && 13229 DSAStack->getParentOrderedRegionParam().first && 13230 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 13231 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 13232 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 13233 } 13234 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 13235 Vars.empty()) 13236 return nullptr; 13237 13238 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 13239 DepKind, DepLoc, ColonLoc, Vars, 13240 TotalDepCount.getZExtValue()); 13241 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 13242 DSAStack->isParentOrderedRegion()) 13243 DSAStack->addDoacrossDependClause(C, OpsOffs); 13244 return C; 13245 } 13246 13247 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc, 13248 SourceLocation LParenLoc, 13249 SourceLocation EndLoc) { 13250 Expr *ValExpr = Device; 13251 Stmt *HelperValStmt = nullptr; 13252 13253 // OpenMP [2.9.1, Restrictions] 13254 // The device expression must evaluate to a non-negative integer value. 13255 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 13256 /*StrictlyPositive=*/false)) 13257 return nullptr; 13258 13259 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 13260 OpenMPDirectiveKind CaptureRegion = 13261 getOpenMPCaptureRegionForClause(DKind, OMPC_device); 13262 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 13263 ValExpr = MakeFullExpr(ValExpr).get(); 13264 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13265 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13266 HelperValStmt = buildPreInits(Context, Captures); 13267 } 13268 13269 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion, 13270 StartLoc, LParenLoc, EndLoc); 13271 } 13272 13273 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 13274 DSAStackTy *Stack, QualType QTy, 13275 bool FullCheck = true) { 13276 NamedDecl *ND; 13277 if (QTy->isIncompleteType(&ND)) { 13278 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 13279 return false; 13280 } 13281 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 13282 !QTy.isTrivialType(SemaRef.Context)) 13283 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 13284 return true; 13285 } 13286 13287 /// Return true if it can be proven that the provided array expression 13288 /// (array section or array subscript) does NOT specify the whole size of the 13289 /// array whose base type is \a BaseQTy. 13290 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 13291 const Expr *E, 13292 QualType BaseQTy) { 13293 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 13294 13295 // If this is an array subscript, it refers to the whole size if the size of 13296 // the dimension is constant and equals 1. Also, an array section assumes the 13297 // format of an array subscript if no colon is used. 13298 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) { 13299 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 13300 return ATy->getSize().getSExtValue() != 1; 13301 // Size can't be evaluated statically. 13302 return false; 13303 } 13304 13305 assert(OASE && "Expecting array section if not an array subscript."); 13306 const Expr *LowerBound = OASE->getLowerBound(); 13307 const Expr *Length = OASE->getLength(); 13308 13309 // If there is a lower bound that does not evaluates to zero, we are not 13310 // covering the whole dimension. 13311 if (LowerBound) { 13312 Expr::EvalResult Result; 13313 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext())) 13314 return false; // Can't get the integer value as a constant. 13315 13316 llvm::APSInt ConstLowerBound = Result.Val.getInt(); 13317 if (ConstLowerBound.getSExtValue()) 13318 return true; 13319 } 13320 13321 // If we don't have a length we covering the whole dimension. 13322 if (!Length) 13323 return false; 13324 13325 // If the base is a pointer, we don't have a way to get the size of the 13326 // pointee. 13327 if (BaseQTy->isPointerType()) 13328 return false; 13329 13330 // We can only check if the length is the same as the size of the dimension 13331 // if we have a constant array. 13332 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 13333 if (!CATy) 13334 return false; 13335 13336 Expr::EvalResult Result; 13337 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 13338 return false; // Can't get the integer value as a constant. 13339 13340 llvm::APSInt ConstLength = Result.Val.getInt(); 13341 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 13342 } 13343 13344 // Return true if it can be proven that the provided array expression (array 13345 // section or array subscript) does NOT specify a single element of the array 13346 // whose base type is \a BaseQTy. 13347 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 13348 const Expr *E, 13349 QualType BaseQTy) { 13350 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 13351 13352 // An array subscript always refer to a single element. Also, an array section 13353 // assumes the format of an array subscript if no colon is used. 13354 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) 13355 return false; 13356 13357 assert(OASE && "Expecting array section if not an array subscript."); 13358 const Expr *Length = OASE->getLength(); 13359 13360 // If we don't have a length we have to check if the array has unitary size 13361 // for this dimension. Also, we should always expect a length if the base type 13362 // is pointer. 13363 if (!Length) { 13364 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 13365 return ATy->getSize().getSExtValue() != 1; 13366 // We cannot assume anything. 13367 return false; 13368 } 13369 13370 // Check if the length evaluates to 1. 13371 Expr::EvalResult Result; 13372 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 13373 return false; // Can't get the integer value as a constant. 13374 13375 llvm::APSInt ConstLength = Result.Val.getInt(); 13376 return ConstLength.getSExtValue() != 1; 13377 } 13378 13379 // Return the expression of the base of the mappable expression or null if it 13380 // cannot be determined and do all the necessary checks to see if the expression 13381 // is valid as a standalone mappable expression. In the process, record all the 13382 // components of the expression. 13383 static const Expr *checkMapClauseExpressionBase( 13384 Sema &SemaRef, Expr *E, 13385 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 13386 OpenMPClauseKind CKind, bool NoDiagnose) { 13387 SourceLocation ELoc = E->getExprLoc(); 13388 SourceRange ERange = E->getSourceRange(); 13389 13390 // The base of elements of list in a map clause have to be either: 13391 // - a reference to variable or field. 13392 // - a member expression. 13393 // - an array expression. 13394 // 13395 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 13396 // reference to 'r'. 13397 // 13398 // If we have: 13399 // 13400 // struct SS { 13401 // Bla S; 13402 // foo() { 13403 // #pragma omp target map (S.Arr[:12]); 13404 // } 13405 // } 13406 // 13407 // We want to retrieve the member expression 'this->S'; 13408 13409 const Expr *RelevantExpr = nullptr; 13410 13411 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2] 13412 // If a list item is an array section, it must specify contiguous storage. 13413 // 13414 // For this restriction it is sufficient that we make sure only references 13415 // to variables or fields and array expressions, and that no array sections 13416 // exist except in the rightmost expression (unless they cover the whole 13417 // dimension of the array). E.g. these would be invalid: 13418 // 13419 // r.ArrS[3:5].Arr[6:7] 13420 // 13421 // r.ArrS[3:5].x 13422 // 13423 // but these would be valid: 13424 // r.ArrS[3].Arr[6:7] 13425 // 13426 // r.ArrS[3].x 13427 13428 bool AllowUnitySizeArraySection = true; 13429 bool AllowWholeSizeArraySection = true; 13430 13431 while (!RelevantExpr) { 13432 E = E->IgnoreParenImpCasts(); 13433 13434 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) { 13435 if (!isa<VarDecl>(CurE->getDecl())) 13436 return nullptr; 13437 13438 RelevantExpr = CurE; 13439 13440 // If we got a reference to a declaration, we should not expect any array 13441 // section before that. 13442 AllowUnitySizeArraySection = false; 13443 AllowWholeSizeArraySection = false; 13444 13445 // Record the component. 13446 CurComponents.emplace_back(CurE, CurE->getDecl()); 13447 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) { 13448 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts(); 13449 13450 if (isa<CXXThisExpr>(BaseE)) 13451 // We found a base expression: this->Val. 13452 RelevantExpr = CurE; 13453 else 13454 E = BaseE; 13455 13456 if (!isa<FieldDecl>(CurE->getMemberDecl())) { 13457 if (!NoDiagnose) { 13458 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 13459 << CurE->getSourceRange(); 13460 return nullptr; 13461 } 13462 if (RelevantExpr) 13463 return nullptr; 13464 continue; 13465 } 13466 13467 auto *FD = cast<FieldDecl>(CurE->getMemberDecl()); 13468 13469 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 13470 // A bit-field cannot appear in a map clause. 13471 // 13472 if (FD->isBitField()) { 13473 if (!NoDiagnose) { 13474 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 13475 << CurE->getSourceRange() << getOpenMPClauseName(CKind); 13476 return nullptr; 13477 } 13478 if (RelevantExpr) 13479 return nullptr; 13480 continue; 13481 } 13482 13483 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 13484 // If the type of a list item is a reference to a type T then the type 13485 // will be considered to be T for all purposes of this clause. 13486 QualType CurType = BaseE->getType().getNonReferenceType(); 13487 13488 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 13489 // A list item cannot be a variable that is a member of a structure with 13490 // a union type. 13491 // 13492 if (CurType->isUnionType()) { 13493 if (!NoDiagnose) { 13494 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 13495 << CurE->getSourceRange(); 13496 return nullptr; 13497 } 13498 continue; 13499 } 13500 13501 // If we got a member expression, we should not expect any array section 13502 // before that: 13503 // 13504 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 13505 // If a list item is an element of a structure, only the rightmost symbol 13506 // of the variable reference can be an array section. 13507 // 13508 AllowUnitySizeArraySection = false; 13509 AllowWholeSizeArraySection = false; 13510 13511 // Record the component. 13512 CurComponents.emplace_back(CurE, FD); 13513 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) { 13514 E = CurE->getBase()->IgnoreParenImpCasts(); 13515 13516 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 13517 if (!NoDiagnose) { 13518 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 13519 << 0 << CurE->getSourceRange(); 13520 return nullptr; 13521 } 13522 continue; 13523 } 13524 13525 // If we got an array subscript that express the whole dimension we 13526 // can have any array expressions before. If it only expressing part of 13527 // the dimension, we can only have unitary-size array expressions. 13528 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, 13529 E->getType())) 13530 AllowWholeSizeArraySection = false; 13531 13532 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 13533 Expr::EvalResult Result; 13534 if (CurE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext())) { 13535 if (!Result.Val.getInt().isNullValue()) { 13536 SemaRef.Diag(CurE->getIdx()->getExprLoc(), 13537 diag::err_omp_invalid_map_this_expr); 13538 SemaRef.Diag(CurE->getIdx()->getExprLoc(), 13539 diag::note_omp_invalid_subscript_on_this_ptr_map); 13540 } 13541 } 13542 RelevantExpr = TE; 13543 } 13544 13545 // Record the component - we don't have any declaration associated. 13546 CurComponents.emplace_back(CurE, nullptr); 13547 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) { 13548 assert(!NoDiagnose && "Array sections cannot be implicitly mapped."); 13549 E = CurE->getBase()->IgnoreParenImpCasts(); 13550 13551 QualType CurType = 13552 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 13553 13554 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 13555 // If the type of a list item is a reference to a type T then the type 13556 // will be considered to be T for all purposes of this clause. 13557 if (CurType->isReferenceType()) 13558 CurType = CurType->getPointeeType(); 13559 13560 bool IsPointer = CurType->isAnyPointerType(); 13561 13562 if (!IsPointer && !CurType->isArrayType()) { 13563 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 13564 << 0 << CurE->getSourceRange(); 13565 return nullptr; 13566 } 13567 13568 bool NotWhole = 13569 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType); 13570 bool NotUnity = 13571 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType); 13572 13573 if (AllowWholeSizeArraySection) { 13574 // Any array section is currently allowed. Allowing a whole size array 13575 // section implies allowing a unity array section as well. 13576 // 13577 // If this array section refers to the whole dimension we can still 13578 // accept other array sections before this one, except if the base is a 13579 // pointer. Otherwise, only unitary sections are accepted. 13580 if (NotWhole || IsPointer) 13581 AllowWholeSizeArraySection = false; 13582 } else if (AllowUnitySizeArraySection && NotUnity) { 13583 // A unity or whole array section is not allowed and that is not 13584 // compatible with the properties of the current array section. 13585 SemaRef.Diag( 13586 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 13587 << CurE->getSourceRange(); 13588 return nullptr; 13589 } 13590 13591 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 13592 Expr::EvalResult ResultR; 13593 Expr::EvalResult ResultL; 13594 if (CurE->getLength()->EvaluateAsInt(ResultR, 13595 SemaRef.getASTContext())) { 13596 if (!ResultR.Val.getInt().isOneValue()) { 13597 SemaRef.Diag(CurE->getLength()->getExprLoc(), 13598 diag::err_omp_invalid_map_this_expr); 13599 SemaRef.Diag(CurE->getLength()->getExprLoc(), 13600 diag::note_omp_invalid_length_on_this_ptr_mapping); 13601 } 13602 } 13603 if (CurE->getLowerBound() && CurE->getLowerBound()->EvaluateAsInt( 13604 ResultL, SemaRef.getASTContext())) { 13605 if (!ResultL.Val.getInt().isNullValue()) { 13606 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(), 13607 diag::err_omp_invalid_map_this_expr); 13608 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(), 13609 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping); 13610 } 13611 } 13612 RelevantExpr = TE; 13613 } 13614 13615 // Record the component - we don't have any declaration associated. 13616 CurComponents.emplace_back(CurE, nullptr); 13617 } else { 13618 if (!NoDiagnose) { 13619 // If nothing else worked, this is not a valid map clause expression. 13620 SemaRef.Diag( 13621 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 13622 << ERange; 13623 } 13624 return nullptr; 13625 } 13626 } 13627 13628 return RelevantExpr; 13629 } 13630 13631 // Return true if expression E associated with value VD has conflicts with other 13632 // map information. 13633 static bool checkMapConflicts( 13634 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 13635 bool CurrentRegionOnly, 13636 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 13637 OpenMPClauseKind CKind) { 13638 assert(VD && E); 13639 SourceLocation ELoc = E->getExprLoc(); 13640 SourceRange ERange = E->getSourceRange(); 13641 13642 // In order to easily check the conflicts we need to match each component of 13643 // the expression under test with the components of the expressions that are 13644 // already in the stack. 13645 13646 assert(!CurComponents.empty() && "Map clause expression with no components!"); 13647 assert(CurComponents.back().getAssociatedDeclaration() == VD && 13648 "Map clause expression with unexpected base!"); 13649 13650 // Variables to help detecting enclosing problems in data environment nests. 13651 bool IsEnclosedByDataEnvironmentExpr = false; 13652 const Expr *EnclosingExpr = nullptr; 13653 13654 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 13655 VD, CurrentRegionOnly, 13656 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 13657 ERange, CKind, &EnclosingExpr, 13658 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 13659 StackComponents, 13660 OpenMPClauseKind) { 13661 assert(!StackComponents.empty() && 13662 "Map clause expression with no components!"); 13663 assert(StackComponents.back().getAssociatedDeclaration() == VD && 13664 "Map clause expression with unexpected base!"); 13665 (void)VD; 13666 13667 // The whole expression in the stack. 13668 const Expr *RE = StackComponents.front().getAssociatedExpression(); 13669 13670 // Expressions must start from the same base. Here we detect at which 13671 // point both expressions diverge from each other and see if we can 13672 // detect if the memory referred to both expressions is contiguous and 13673 // do not overlap. 13674 auto CI = CurComponents.rbegin(); 13675 auto CE = CurComponents.rend(); 13676 auto SI = StackComponents.rbegin(); 13677 auto SE = StackComponents.rend(); 13678 for (; CI != CE && SI != SE; ++CI, ++SI) { 13679 13680 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 13681 // At most one list item can be an array item derived from a given 13682 // variable in map clauses of the same construct. 13683 if (CurrentRegionOnly && 13684 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 13685 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) && 13686 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 13687 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) { 13688 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 13689 diag::err_omp_multiple_array_items_in_map_clause) 13690 << CI->getAssociatedExpression()->getSourceRange(); 13691 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 13692 diag::note_used_here) 13693 << SI->getAssociatedExpression()->getSourceRange(); 13694 return true; 13695 } 13696 13697 // Do both expressions have the same kind? 13698 if (CI->getAssociatedExpression()->getStmtClass() != 13699 SI->getAssociatedExpression()->getStmtClass()) 13700 break; 13701 13702 // Are we dealing with different variables/fields? 13703 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 13704 break; 13705 } 13706 // Check if the extra components of the expressions in the enclosing 13707 // data environment are redundant for the current base declaration. 13708 // If they are, the maps completely overlap, which is legal. 13709 for (; SI != SE; ++SI) { 13710 QualType Type; 13711 if (const auto *ASE = 13712 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 13713 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 13714 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 13715 SI->getAssociatedExpression())) { 13716 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 13717 Type = 13718 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 13719 } 13720 if (Type.isNull() || Type->isAnyPointerType() || 13721 checkArrayExpressionDoesNotReferToWholeSize( 13722 SemaRef, SI->getAssociatedExpression(), Type)) 13723 break; 13724 } 13725 13726 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 13727 // List items of map clauses in the same construct must not share 13728 // original storage. 13729 // 13730 // If the expressions are exactly the same or one is a subset of the 13731 // other, it means they are sharing storage. 13732 if (CI == CE && SI == SE) { 13733 if (CurrentRegionOnly) { 13734 if (CKind == OMPC_map) { 13735 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 13736 } else { 13737 assert(CKind == OMPC_to || CKind == OMPC_from); 13738 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 13739 << ERange; 13740 } 13741 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 13742 << RE->getSourceRange(); 13743 return true; 13744 } 13745 // If we find the same expression in the enclosing data environment, 13746 // that is legal. 13747 IsEnclosedByDataEnvironmentExpr = true; 13748 return false; 13749 } 13750 13751 QualType DerivedType = 13752 std::prev(CI)->getAssociatedDeclaration()->getType(); 13753 SourceLocation DerivedLoc = 13754 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 13755 13756 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 13757 // If the type of a list item is a reference to a type T then the type 13758 // will be considered to be T for all purposes of this clause. 13759 DerivedType = DerivedType.getNonReferenceType(); 13760 13761 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 13762 // A variable for which the type is pointer and an array section 13763 // derived from that variable must not appear as list items of map 13764 // clauses of the same construct. 13765 // 13766 // Also, cover one of the cases in: 13767 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 13768 // If any part of the original storage of a list item has corresponding 13769 // storage in the device data environment, all of the original storage 13770 // must have corresponding storage in the device data environment. 13771 // 13772 if (DerivedType->isAnyPointerType()) { 13773 if (CI == CE || SI == SE) { 13774 SemaRef.Diag( 13775 DerivedLoc, 13776 diag::err_omp_pointer_mapped_along_with_derived_section) 13777 << DerivedLoc; 13778 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 13779 << RE->getSourceRange(); 13780 return true; 13781 } 13782 if (CI->getAssociatedExpression()->getStmtClass() != 13783 SI->getAssociatedExpression()->getStmtClass() || 13784 CI->getAssociatedDeclaration()->getCanonicalDecl() == 13785 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 13786 assert(CI != CE && SI != SE); 13787 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 13788 << DerivedLoc; 13789 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 13790 << RE->getSourceRange(); 13791 return true; 13792 } 13793 } 13794 13795 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 13796 // List items of map clauses in the same construct must not share 13797 // original storage. 13798 // 13799 // An expression is a subset of the other. 13800 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 13801 if (CKind == OMPC_map) { 13802 if (CI != CE || SI != SE) { 13803 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is 13804 // a pointer. 13805 auto Begin = 13806 CI != CE ? CurComponents.begin() : StackComponents.begin(); 13807 auto End = CI != CE ? CurComponents.end() : StackComponents.end(); 13808 auto It = Begin; 13809 while (It != End && !It->getAssociatedDeclaration()) 13810 std::advance(It, 1); 13811 assert(It != End && 13812 "Expected at least one component with the declaration."); 13813 if (It != Begin && It->getAssociatedDeclaration() 13814 ->getType() 13815 .getCanonicalType() 13816 ->isAnyPointerType()) { 13817 IsEnclosedByDataEnvironmentExpr = false; 13818 EnclosingExpr = nullptr; 13819 return false; 13820 } 13821 } 13822 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 13823 } else { 13824 assert(CKind == OMPC_to || CKind == OMPC_from); 13825 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 13826 << ERange; 13827 } 13828 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 13829 << RE->getSourceRange(); 13830 return true; 13831 } 13832 13833 // The current expression uses the same base as other expression in the 13834 // data environment but does not contain it completely. 13835 if (!CurrentRegionOnly && SI != SE) 13836 EnclosingExpr = RE; 13837 13838 // The current expression is a subset of the expression in the data 13839 // environment. 13840 IsEnclosedByDataEnvironmentExpr |= 13841 (!CurrentRegionOnly && CI != CE && SI == SE); 13842 13843 return false; 13844 }); 13845 13846 if (CurrentRegionOnly) 13847 return FoundError; 13848 13849 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 13850 // If any part of the original storage of a list item has corresponding 13851 // storage in the device data environment, all of the original storage must 13852 // have corresponding storage in the device data environment. 13853 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 13854 // If a list item is an element of a structure, and a different element of 13855 // the structure has a corresponding list item in the device data environment 13856 // prior to a task encountering the construct associated with the map clause, 13857 // then the list item must also have a corresponding list item in the device 13858 // data environment prior to the task encountering the construct. 13859 // 13860 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 13861 SemaRef.Diag(ELoc, 13862 diag::err_omp_original_storage_is_shared_and_does_not_contain) 13863 << ERange; 13864 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 13865 << EnclosingExpr->getSourceRange(); 13866 return true; 13867 } 13868 13869 return FoundError; 13870 } 13871 13872 // Look up the user-defined mapper given the mapper name and mapped type, and 13873 // build a reference to it. 13874 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 13875 CXXScopeSpec &MapperIdScopeSpec, 13876 const DeclarationNameInfo &MapperId, 13877 QualType Type, 13878 Expr *UnresolvedMapper) { 13879 if (MapperIdScopeSpec.isInvalid()) 13880 return ExprError(); 13881 // Find all user-defined mappers with the given MapperId. 13882 SmallVector<UnresolvedSet<8>, 4> Lookups; 13883 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName); 13884 Lookup.suppressDiagnostics(); 13885 if (S) { 13886 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) { 13887 NamedDecl *D = Lookup.getRepresentativeDecl(); 13888 while (S && !S->isDeclScope(D)) 13889 S = S->getParent(); 13890 if (S) 13891 S = S->getParent(); 13892 Lookups.emplace_back(); 13893 Lookups.back().append(Lookup.begin(), Lookup.end()); 13894 Lookup.clear(); 13895 } 13896 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) { 13897 // Extract the user-defined mappers with the given MapperId. 13898 Lookups.push_back(UnresolvedSet<8>()); 13899 for (NamedDecl *D : ULE->decls()) { 13900 auto *DMD = cast<OMPDeclareMapperDecl>(D); 13901 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation."); 13902 Lookups.back().addDecl(DMD); 13903 } 13904 } 13905 // Defer the lookup for dependent types. The results will be passed through 13906 // UnresolvedMapper on instantiation. 13907 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() || 13908 Type->isInstantiationDependentType() || 13909 Type->containsUnexpandedParameterPack() || 13910 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 13911 return !D->isInvalidDecl() && 13912 (D->getType()->isDependentType() || 13913 D->getType()->isInstantiationDependentType() || 13914 D->getType()->containsUnexpandedParameterPack()); 13915 })) { 13916 UnresolvedSet<8> URS; 13917 for (const UnresolvedSet<8> &Set : Lookups) { 13918 if (Set.empty()) 13919 continue; 13920 URS.append(Set.begin(), Set.end()); 13921 } 13922 return UnresolvedLookupExpr::Create( 13923 SemaRef.Context, /*NamingClass=*/nullptr, 13924 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId, 13925 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end()); 13926 } 13927 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 13928 // The type must be of struct, union or class type in C and C++ 13929 if (!Type->isStructureOrClassType() && !Type->isUnionType()) 13930 return ExprEmpty(); 13931 SourceLocation Loc = MapperId.getLoc(); 13932 // Perform argument dependent lookup. 13933 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet()) 13934 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups); 13935 // Return the first user-defined mapper with the desired type. 13936 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 13937 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * { 13938 if (!D->isInvalidDecl() && 13939 SemaRef.Context.hasSameType(D->getType(), Type)) 13940 return D; 13941 return nullptr; 13942 })) 13943 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 13944 // Find the first user-defined mapper with a type derived from the desired 13945 // type. 13946 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 13947 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * { 13948 if (!D->isInvalidDecl() && 13949 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) && 13950 !Type.isMoreQualifiedThan(D->getType())) 13951 return D; 13952 return nullptr; 13953 })) { 13954 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 13955 /*DetectVirtual=*/false); 13956 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) { 13957 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 13958 VD->getType().getUnqualifiedType()))) { 13959 if (SemaRef.CheckBaseClassAccess( 13960 Loc, VD->getType(), Type, Paths.front(), 13961 /*DiagID=*/0) != Sema::AR_inaccessible) { 13962 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 13963 } 13964 } 13965 } 13966 } 13967 // Report error if a mapper is specified, but cannot be found. 13968 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") { 13969 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper) 13970 << Type << MapperId.getName(); 13971 return ExprError(); 13972 } 13973 return ExprEmpty(); 13974 } 13975 13976 namespace { 13977 // Utility struct that gathers all the related lists associated with a mappable 13978 // expression. 13979 struct MappableVarListInfo { 13980 // The list of expressions. 13981 ArrayRef<Expr *> VarList; 13982 // The list of processed expressions. 13983 SmallVector<Expr *, 16> ProcessedVarList; 13984 // The mappble components for each expression. 13985 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 13986 // The base declaration of the variable. 13987 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 13988 // The reference to the user-defined mapper associated with every expression. 13989 SmallVector<Expr *, 16> UDMapperList; 13990 13991 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 13992 // We have a list of components and base declarations for each entry in the 13993 // variable list. 13994 VarComponents.reserve(VarList.size()); 13995 VarBaseDeclarations.reserve(VarList.size()); 13996 } 13997 }; 13998 } 13999 14000 // Check the validity of the provided variable list for the provided clause kind 14001 // \a CKind. In the check process the valid expressions, mappable expression 14002 // components, variables, and user-defined mappers are extracted and used to 14003 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a 14004 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec, 14005 // and \a MapperId are expected to be valid if the clause kind is 'map'. 14006 static void checkMappableExpressionList( 14007 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind, 14008 MappableVarListInfo &MVLI, SourceLocation StartLoc, 14009 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId, 14010 ArrayRef<Expr *> UnresolvedMappers, 14011 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 14012 bool IsMapTypeImplicit = false) { 14013 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 14014 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 14015 "Unexpected clause kind with mappable expressions!"); 14016 14017 // If the identifier of user-defined mapper is not specified, it is "default". 14018 // We do not change the actual name in this clause to distinguish whether a 14019 // mapper is specified explicitly, i.e., it is not explicitly specified when 14020 // MapperId.getName() is empty. 14021 if (!MapperId.getName() || MapperId.getName().isEmpty()) { 14022 auto &DeclNames = SemaRef.getASTContext().DeclarationNames; 14023 MapperId.setName(DeclNames.getIdentifier( 14024 &SemaRef.getASTContext().Idents.get("default"))); 14025 } 14026 14027 // Iterators to find the current unresolved mapper expression. 14028 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end(); 14029 bool UpdateUMIt = false; 14030 Expr *UnresolvedMapper = nullptr; 14031 14032 // Keep track of the mappable components and base declarations in this clause. 14033 // Each entry in the list is going to have a list of components associated. We 14034 // record each set of the components so that we can build the clause later on. 14035 // In the end we should have the same amount of declarations and component 14036 // lists. 14037 14038 for (Expr *RE : MVLI.VarList) { 14039 assert(RE && "Null expr in omp to/from/map clause"); 14040 SourceLocation ELoc = RE->getExprLoc(); 14041 14042 // Find the current unresolved mapper expression. 14043 if (UpdateUMIt && UMIt != UMEnd) { 14044 UMIt++; 14045 assert( 14046 UMIt != UMEnd && 14047 "Expect the size of UnresolvedMappers to match with that of VarList"); 14048 } 14049 UpdateUMIt = true; 14050 if (UMIt != UMEnd) 14051 UnresolvedMapper = *UMIt; 14052 14053 const Expr *VE = RE->IgnoreParenLValueCasts(); 14054 14055 if (VE->isValueDependent() || VE->isTypeDependent() || 14056 VE->isInstantiationDependent() || 14057 VE->containsUnexpandedParameterPack()) { 14058 // Try to find the associated user-defined mapper. 14059 ExprResult ER = buildUserDefinedMapperRef( 14060 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 14061 VE->getType().getCanonicalType(), UnresolvedMapper); 14062 if (ER.isInvalid()) 14063 continue; 14064 MVLI.UDMapperList.push_back(ER.get()); 14065 // We can only analyze this information once the missing information is 14066 // resolved. 14067 MVLI.ProcessedVarList.push_back(RE); 14068 continue; 14069 } 14070 14071 Expr *SimpleExpr = RE->IgnoreParenCasts(); 14072 14073 if (!RE->IgnoreParenImpCasts()->isLValue()) { 14074 SemaRef.Diag(ELoc, 14075 diag::err_omp_expected_named_var_member_or_array_expression) 14076 << RE->getSourceRange(); 14077 continue; 14078 } 14079 14080 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 14081 ValueDecl *CurDeclaration = nullptr; 14082 14083 // Obtain the array or member expression bases if required. Also, fill the 14084 // components array with all the components identified in the process. 14085 const Expr *BE = checkMapClauseExpressionBase( 14086 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false); 14087 if (!BE) 14088 continue; 14089 14090 assert(!CurComponents.empty() && 14091 "Invalid mappable expression information."); 14092 14093 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) { 14094 // Add store "this" pointer to class in DSAStackTy for future checking 14095 DSAS->addMappedClassesQualTypes(TE->getType()); 14096 // Try to find the associated user-defined mapper. 14097 ExprResult ER = buildUserDefinedMapperRef( 14098 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 14099 VE->getType().getCanonicalType(), UnresolvedMapper); 14100 if (ER.isInvalid()) 14101 continue; 14102 MVLI.UDMapperList.push_back(ER.get()); 14103 // Skip restriction checking for variable or field declarations 14104 MVLI.ProcessedVarList.push_back(RE); 14105 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 14106 MVLI.VarComponents.back().append(CurComponents.begin(), 14107 CurComponents.end()); 14108 MVLI.VarBaseDeclarations.push_back(nullptr); 14109 continue; 14110 } 14111 14112 // For the following checks, we rely on the base declaration which is 14113 // expected to be associated with the last component. The declaration is 14114 // expected to be a variable or a field (if 'this' is being mapped). 14115 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 14116 assert(CurDeclaration && "Null decl on map clause."); 14117 assert( 14118 CurDeclaration->isCanonicalDecl() && 14119 "Expecting components to have associated only canonical declarations."); 14120 14121 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 14122 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 14123 14124 assert((VD || FD) && "Only variables or fields are expected here!"); 14125 (void)FD; 14126 14127 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 14128 // threadprivate variables cannot appear in a map clause. 14129 // OpenMP 4.5 [2.10.5, target update Construct] 14130 // threadprivate variables cannot appear in a from clause. 14131 if (VD && DSAS->isThreadPrivate(VD)) { 14132 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 14133 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 14134 << getOpenMPClauseName(CKind); 14135 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 14136 continue; 14137 } 14138 14139 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 14140 // A list item cannot appear in both a map clause and a data-sharing 14141 // attribute clause on the same construct. 14142 14143 // Check conflicts with other map clause expressions. We check the conflicts 14144 // with the current construct separately from the enclosing data 14145 // environment, because the restrictions are different. We only have to 14146 // check conflicts across regions for the map clauses. 14147 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 14148 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 14149 break; 14150 if (CKind == OMPC_map && 14151 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 14152 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 14153 break; 14154 14155 // OpenMP 4.5 [2.10.5, target update Construct] 14156 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 14157 // If the type of a list item is a reference to a type T then the type will 14158 // be considered to be T for all purposes of this clause. 14159 auto I = llvm::find_if( 14160 CurComponents, 14161 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 14162 return MC.getAssociatedDeclaration(); 14163 }); 14164 assert(I != CurComponents.end() && "Null decl on map clause."); 14165 QualType Type = 14166 I->getAssociatedDeclaration()->getType().getNonReferenceType(); 14167 14168 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 14169 // A list item in a to or from clause must have a mappable type. 14170 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 14171 // A list item must have a mappable type. 14172 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 14173 DSAS, Type)) 14174 continue; 14175 14176 if (CKind == OMPC_map) { 14177 // target enter data 14178 // OpenMP [2.10.2, Restrictions, p. 99] 14179 // A map-type must be specified in all map clauses and must be either 14180 // to or alloc. 14181 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 14182 if (DKind == OMPD_target_enter_data && 14183 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 14184 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 14185 << (IsMapTypeImplicit ? 1 : 0) 14186 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 14187 << getOpenMPDirectiveName(DKind); 14188 continue; 14189 } 14190 14191 // target exit_data 14192 // OpenMP [2.10.3, Restrictions, p. 102] 14193 // A map-type must be specified in all map clauses and must be either 14194 // from, release, or delete. 14195 if (DKind == OMPD_target_exit_data && 14196 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 14197 MapType == OMPC_MAP_delete)) { 14198 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 14199 << (IsMapTypeImplicit ? 1 : 0) 14200 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 14201 << getOpenMPDirectiveName(DKind); 14202 continue; 14203 } 14204 14205 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 14206 // A list item cannot appear in both a map clause and a data-sharing 14207 // attribute clause on the same construct 14208 if (VD && isOpenMPTargetExecutionDirective(DKind)) { 14209 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 14210 if (isOpenMPPrivate(DVar.CKind)) { 14211 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 14212 << getOpenMPClauseName(DVar.CKind) 14213 << getOpenMPClauseName(OMPC_map) 14214 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 14215 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 14216 continue; 14217 } 14218 } 14219 } 14220 14221 // Try to find the associated user-defined mapper. 14222 ExprResult ER = buildUserDefinedMapperRef( 14223 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 14224 Type.getCanonicalType(), UnresolvedMapper); 14225 if (ER.isInvalid()) 14226 continue; 14227 MVLI.UDMapperList.push_back(ER.get()); 14228 14229 // Save the current expression. 14230 MVLI.ProcessedVarList.push_back(RE); 14231 14232 // Store the components in the stack so that they can be used to check 14233 // against other clauses later on. 14234 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 14235 /*WhereFoundClauseKind=*/OMPC_map); 14236 14237 // Save the components and declaration to create the clause. For purposes of 14238 // the clause creation, any component list that has has base 'this' uses 14239 // null as base declaration. 14240 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 14241 MVLI.VarComponents.back().append(CurComponents.begin(), 14242 CurComponents.end()); 14243 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 14244 : CurDeclaration); 14245 } 14246 } 14247 14248 OMPClause *Sema::ActOnOpenMPMapClause( 14249 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 14250 ArrayRef<SourceLocation> MapTypeModifiersLoc, 14251 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 14252 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc, 14253 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 14254 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 14255 OpenMPMapModifierKind Modifiers[] = {OMPC_MAP_MODIFIER_unknown, 14256 OMPC_MAP_MODIFIER_unknown, 14257 OMPC_MAP_MODIFIER_unknown}; 14258 SourceLocation ModifiersLoc[OMPMapClause::NumberOfModifiers]; 14259 14260 // Process map-type-modifiers, flag errors for duplicate modifiers. 14261 unsigned Count = 0; 14262 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) { 14263 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown && 14264 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) { 14265 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier); 14266 continue; 14267 } 14268 assert(Count < OMPMapClause::NumberOfModifiers && 14269 "Modifiers exceed the allowed number of map type modifiers"); 14270 Modifiers[Count] = MapTypeModifiers[I]; 14271 ModifiersLoc[Count] = MapTypeModifiersLoc[I]; 14272 ++Count; 14273 } 14274 14275 MappableVarListInfo MVLI(VarList); 14276 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc, 14277 MapperIdScopeSpec, MapperId, UnresolvedMappers, 14278 MapType, IsMapTypeImplicit); 14279 14280 // We need to produce a map clause even if we don't have variables so that 14281 // other diagnostics related with non-existing map clauses are accurate. 14282 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList, 14283 MVLI.VarBaseDeclarations, MVLI.VarComponents, 14284 MVLI.UDMapperList, Modifiers, ModifiersLoc, 14285 MapperIdScopeSpec.getWithLocInContext(Context), 14286 MapperId, MapType, IsMapTypeImplicit, MapLoc); 14287 } 14288 14289 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 14290 TypeResult ParsedType) { 14291 assert(ParsedType.isUsable()); 14292 14293 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 14294 if (ReductionType.isNull()) 14295 return QualType(); 14296 14297 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 14298 // A type name in a declare reduction directive cannot be a function type, an 14299 // array type, a reference type, or a type qualified with const, volatile or 14300 // restrict. 14301 if (ReductionType.hasQualifiers()) { 14302 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 14303 return QualType(); 14304 } 14305 14306 if (ReductionType->isFunctionType()) { 14307 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 14308 return QualType(); 14309 } 14310 if (ReductionType->isReferenceType()) { 14311 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 14312 return QualType(); 14313 } 14314 if (ReductionType->isArrayType()) { 14315 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 14316 return QualType(); 14317 } 14318 return ReductionType; 14319 } 14320 14321 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 14322 Scope *S, DeclContext *DC, DeclarationName Name, 14323 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 14324 AccessSpecifier AS, Decl *PrevDeclInScope) { 14325 SmallVector<Decl *, 8> Decls; 14326 Decls.reserve(ReductionTypes.size()); 14327 14328 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 14329 forRedeclarationInCurContext()); 14330 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 14331 // A reduction-identifier may not be re-declared in the current scope for the 14332 // same type or for a type that is compatible according to the base language 14333 // rules. 14334 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 14335 OMPDeclareReductionDecl *PrevDRD = nullptr; 14336 bool InCompoundScope = true; 14337 if (S != nullptr) { 14338 // Find previous declaration with the same name not referenced in other 14339 // declarations. 14340 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 14341 InCompoundScope = 14342 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 14343 LookupName(Lookup, S); 14344 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 14345 /*AllowInlineNamespace=*/false); 14346 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 14347 LookupResult::Filter Filter = Lookup.makeFilter(); 14348 while (Filter.hasNext()) { 14349 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 14350 if (InCompoundScope) { 14351 auto I = UsedAsPrevious.find(PrevDecl); 14352 if (I == UsedAsPrevious.end()) 14353 UsedAsPrevious[PrevDecl] = false; 14354 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 14355 UsedAsPrevious[D] = true; 14356 } 14357 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 14358 PrevDecl->getLocation(); 14359 } 14360 Filter.done(); 14361 if (InCompoundScope) { 14362 for (const auto &PrevData : UsedAsPrevious) { 14363 if (!PrevData.second) { 14364 PrevDRD = PrevData.first; 14365 break; 14366 } 14367 } 14368 } 14369 } else if (PrevDeclInScope != nullptr) { 14370 auto *PrevDRDInScope = PrevDRD = 14371 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 14372 do { 14373 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 14374 PrevDRDInScope->getLocation(); 14375 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 14376 } while (PrevDRDInScope != nullptr); 14377 } 14378 for (const auto &TyData : ReductionTypes) { 14379 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 14380 bool Invalid = false; 14381 if (I != PreviousRedeclTypes.end()) { 14382 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 14383 << TyData.first; 14384 Diag(I->second, diag::note_previous_definition); 14385 Invalid = true; 14386 } 14387 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 14388 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 14389 Name, TyData.first, PrevDRD); 14390 DC->addDecl(DRD); 14391 DRD->setAccess(AS); 14392 Decls.push_back(DRD); 14393 if (Invalid) 14394 DRD->setInvalidDecl(); 14395 else 14396 PrevDRD = DRD; 14397 } 14398 14399 return DeclGroupPtrTy::make( 14400 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 14401 } 14402 14403 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 14404 auto *DRD = cast<OMPDeclareReductionDecl>(D); 14405 14406 // Enter new function scope. 14407 PushFunctionScope(); 14408 setFunctionHasBranchProtectedScope(); 14409 getCurFunction()->setHasOMPDeclareReductionCombiner(); 14410 14411 if (S != nullptr) 14412 PushDeclContext(S, DRD); 14413 else 14414 CurContext = DRD; 14415 14416 PushExpressionEvaluationContext( 14417 ExpressionEvaluationContext::PotentiallyEvaluated); 14418 14419 QualType ReductionType = DRD->getType(); 14420 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 14421 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 14422 // uses semantics of argument handles by value, but it should be passed by 14423 // reference. C lang does not support references, so pass all parameters as 14424 // pointers. 14425 // Create 'T omp_in;' variable. 14426 VarDecl *OmpInParm = 14427 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 14428 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 14429 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 14430 // uses semantics of argument handles by value, but it should be passed by 14431 // reference. C lang does not support references, so pass all parameters as 14432 // pointers. 14433 // Create 'T omp_out;' variable. 14434 VarDecl *OmpOutParm = 14435 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 14436 if (S != nullptr) { 14437 PushOnScopeChains(OmpInParm, S); 14438 PushOnScopeChains(OmpOutParm, S); 14439 } else { 14440 DRD->addDecl(OmpInParm); 14441 DRD->addDecl(OmpOutParm); 14442 } 14443 Expr *InE = 14444 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation()); 14445 Expr *OutE = 14446 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation()); 14447 DRD->setCombinerData(InE, OutE); 14448 } 14449 14450 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 14451 auto *DRD = cast<OMPDeclareReductionDecl>(D); 14452 DiscardCleanupsInEvaluationContext(); 14453 PopExpressionEvaluationContext(); 14454 14455 PopDeclContext(); 14456 PopFunctionScopeInfo(); 14457 14458 if (Combiner != nullptr) 14459 DRD->setCombiner(Combiner); 14460 else 14461 DRD->setInvalidDecl(); 14462 } 14463 14464 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 14465 auto *DRD = cast<OMPDeclareReductionDecl>(D); 14466 14467 // Enter new function scope. 14468 PushFunctionScope(); 14469 setFunctionHasBranchProtectedScope(); 14470 14471 if (S != nullptr) 14472 PushDeclContext(S, DRD); 14473 else 14474 CurContext = DRD; 14475 14476 PushExpressionEvaluationContext( 14477 ExpressionEvaluationContext::PotentiallyEvaluated); 14478 14479 QualType ReductionType = DRD->getType(); 14480 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 14481 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 14482 // uses semantics of argument handles by value, but it should be passed by 14483 // reference. C lang does not support references, so pass all parameters as 14484 // pointers. 14485 // Create 'T omp_priv;' variable. 14486 VarDecl *OmpPrivParm = 14487 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 14488 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 14489 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 14490 // uses semantics of argument handles by value, but it should be passed by 14491 // reference. C lang does not support references, so pass all parameters as 14492 // pointers. 14493 // Create 'T omp_orig;' variable. 14494 VarDecl *OmpOrigParm = 14495 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 14496 if (S != nullptr) { 14497 PushOnScopeChains(OmpPrivParm, S); 14498 PushOnScopeChains(OmpOrigParm, S); 14499 } else { 14500 DRD->addDecl(OmpPrivParm); 14501 DRD->addDecl(OmpOrigParm); 14502 } 14503 Expr *OrigE = 14504 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation()); 14505 Expr *PrivE = 14506 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation()); 14507 DRD->setInitializerData(OrigE, PrivE); 14508 return OmpPrivParm; 14509 } 14510 14511 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 14512 VarDecl *OmpPrivParm) { 14513 auto *DRD = cast<OMPDeclareReductionDecl>(D); 14514 DiscardCleanupsInEvaluationContext(); 14515 PopExpressionEvaluationContext(); 14516 14517 PopDeclContext(); 14518 PopFunctionScopeInfo(); 14519 14520 if (Initializer != nullptr) { 14521 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 14522 } else if (OmpPrivParm->hasInit()) { 14523 DRD->setInitializer(OmpPrivParm->getInit(), 14524 OmpPrivParm->isDirectInit() 14525 ? OMPDeclareReductionDecl::DirectInit 14526 : OMPDeclareReductionDecl::CopyInit); 14527 } else { 14528 DRD->setInvalidDecl(); 14529 } 14530 } 14531 14532 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 14533 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 14534 for (Decl *D : DeclReductions.get()) { 14535 if (IsValid) { 14536 if (S) 14537 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 14538 /*AddToContext=*/false); 14539 } else { 14540 D->setInvalidDecl(); 14541 } 14542 } 14543 return DeclReductions; 14544 } 14545 14546 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) { 14547 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 14548 QualType T = TInfo->getType(); 14549 if (D.isInvalidType()) 14550 return true; 14551 14552 if (getLangOpts().CPlusPlus) { 14553 // Check that there are no default arguments (C++ only). 14554 CheckExtraCXXDefaultArguments(D); 14555 } 14556 14557 return CreateParsedType(T, TInfo); 14558 } 14559 14560 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc, 14561 TypeResult ParsedType) { 14562 assert(ParsedType.isUsable() && "Expect usable parsed mapper type"); 14563 14564 QualType MapperType = GetTypeFromParser(ParsedType.get()); 14565 assert(!MapperType.isNull() && "Expect valid mapper type"); 14566 14567 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 14568 // The type must be of struct, union or class type in C and C++ 14569 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) { 14570 Diag(TyLoc, diag::err_omp_mapper_wrong_type); 14571 return QualType(); 14572 } 14573 return MapperType; 14574 } 14575 14576 OMPDeclareMapperDecl *Sema::ActOnOpenMPDeclareMapperDirectiveStart( 14577 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType, 14578 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS, 14579 Decl *PrevDeclInScope) { 14580 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName, 14581 forRedeclarationInCurContext()); 14582 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 14583 // A mapper-identifier may not be redeclared in the current scope for the 14584 // same type or for a type that is compatible according to the base language 14585 // rules. 14586 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 14587 OMPDeclareMapperDecl *PrevDMD = nullptr; 14588 bool InCompoundScope = true; 14589 if (S != nullptr) { 14590 // Find previous declaration with the same name not referenced in other 14591 // declarations. 14592 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 14593 InCompoundScope = 14594 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 14595 LookupName(Lookup, S); 14596 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 14597 /*AllowInlineNamespace=*/false); 14598 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious; 14599 LookupResult::Filter Filter = Lookup.makeFilter(); 14600 while (Filter.hasNext()) { 14601 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next()); 14602 if (InCompoundScope) { 14603 auto I = UsedAsPrevious.find(PrevDecl); 14604 if (I == UsedAsPrevious.end()) 14605 UsedAsPrevious[PrevDecl] = false; 14606 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope()) 14607 UsedAsPrevious[D] = true; 14608 } 14609 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 14610 PrevDecl->getLocation(); 14611 } 14612 Filter.done(); 14613 if (InCompoundScope) { 14614 for (const auto &PrevData : UsedAsPrevious) { 14615 if (!PrevData.second) { 14616 PrevDMD = PrevData.first; 14617 break; 14618 } 14619 } 14620 } 14621 } else if (PrevDeclInScope) { 14622 auto *PrevDMDInScope = PrevDMD = 14623 cast<OMPDeclareMapperDecl>(PrevDeclInScope); 14624 do { 14625 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] = 14626 PrevDMDInScope->getLocation(); 14627 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope(); 14628 } while (PrevDMDInScope != nullptr); 14629 } 14630 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType()); 14631 bool Invalid = false; 14632 if (I != PreviousRedeclTypes.end()) { 14633 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition) 14634 << MapperType << Name; 14635 Diag(I->second, diag::note_previous_definition); 14636 Invalid = true; 14637 } 14638 auto *DMD = OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name, 14639 MapperType, VN, PrevDMD); 14640 DC->addDecl(DMD); 14641 DMD->setAccess(AS); 14642 if (Invalid) 14643 DMD->setInvalidDecl(); 14644 14645 // Enter new function scope. 14646 PushFunctionScope(); 14647 setFunctionHasBranchProtectedScope(); 14648 14649 CurContext = DMD; 14650 14651 return DMD; 14652 } 14653 14654 void Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(OMPDeclareMapperDecl *DMD, 14655 Scope *S, 14656 QualType MapperType, 14657 SourceLocation StartLoc, 14658 DeclarationName VN) { 14659 VarDecl *VD = buildVarDecl(*this, StartLoc, MapperType, VN.getAsString()); 14660 if (S) 14661 PushOnScopeChains(VD, S); 14662 else 14663 DMD->addDecl(VD); 14664 Expr *MapperVarRefExpr = buildDeclRefExpr(*this, VD, MapperType, StartLoc); 14665 DMD->setMapperVarRef(MapperVarRefExpr); 14666 } 14667 14668 Sema::DeclGroupPtrTy 14669 Sema::ActOnOpenMPDeclareMapperDirectiveEnd(OMPDeclareMapperDecl *D, Scope *S, 14670 ArrayRef<OMPClause *> ClauseList) { 14671 PopDeclContext(); 14672 PopFunctionScopeInfo(); 14673 14674 if (D) { 14675 if (S) 14676 PushOnScopeChains(D, S, /*AddToContext=*/false); 14677 D->CreateClauses(Context, ClauseList); 14678 } 14679 14680 return DeclGroupPtrTy::make(DeclGroupRef(D)); 14681 } 14682 14683 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 14684 SourceLocation StartLoc, 14685 SourceLocation LParenLoc, 14686 SourceLocation EndLoc) { 14687 Expr *ValExpr = NumTeams; 14688 Stmt *HelperValStmt = nullptr; 14689 14690 // OpenMP [teams Constrcut, Restrictions] 14691 // The num_teams expression must evaluate to a positive integer value. 14692 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 14693 /*StrictlyPositive=*/true)) 14694 return nullptr; 14695 14696 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14697 OpenMPDirectiveKind CaptureRegion = 14698 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams); 14699 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14700 ValExpr = MakeFullExpr(ValExpr).get(); 14701 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14702 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14703 HelperValStmt = buildPreInits(Context, Captures); 14704 } 14705 14706 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 14707 StartLoc, LParenLoc, EndLoc); 14708 } 14709 14710 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 14711 SourceLocation StartLoc, 14712 SourceLocation LParenLoc, 14713 SourceLocation EndLoc) { 14714 Expr *ValExpr = ThreadLimit; 14715 Stmt *HelperValStmt = nullptr; 14716 14717 // OpenMP [teams Constrcut, Restrictions] 14718 // The thread_limit expression must evaluate to a positive integer value. 14719 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 14720 /*StrictlyPositive=*/true)) 14721 return nullptr; 14722 14723 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 14724 OpenMPDirectiveKind CaptureRegion = 14725 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit); 14726 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 14727 ValExpr = MakeFullExpr(ValExpr).get(); 14728 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14729 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14730 HelperValStmt = buildPreInits(Context, Captures); 14731 } 14732 14733 return new (Context) OMPThreadLimitClause( 14734 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 14735 } 14736 14737 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 14738 SourceLocation StartLoc, 14739 SourceLocation LParenLoc, 14740 SourceLocation EndLoc) { 14741 Expr *ValExpr = Priority; 14742 14743 // OpenMP [2.9.1, task Constrcut] 14744 // The priority-value is a non-negative numerical scalar expression. 14745 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority, 14746 /*StrictlyPositive=*/false)) 14747 return nullptr; 14748 14749 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc); 14750 } 14751 14752 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 14753 SourceLocation StartLoc, 14754 SourceLocation LParenLoc, 14755 SourceLocation EndLoc) { 14756 Expr *ValExpr = Grainsize; 14757 14758 // OpenMP [2.9.2, taskloop Constrcut] 14759 // The parameter of the grainsize clause must be a positive integer 14760 // expression. 14761 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize, 14762 /*StrictlyPositive=*/true)) 14763 return nullptr; 14764 14765 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc); 14766 } 14767 14768 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 14769 SourceLocation StartLoc, 14770 SourceLocation LParenLoc, 14771 SourceLocation EndLoc) { 14772 Expr *ValExpr = NumTasks; 14773 14774 // OpenMP [2.9.2, taskloop Constrcut] 14775 // The parameter of the num_tasks clause must be a positive integer 14776 // expression. 14777 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks, 14778 /*StrictlyPositive=*/true)) 14779 return nullptr; 14780 14781 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc); 14782 } 14783 14784 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 14785 SourceLocation LParenLoc, 14786 SourceLocation EndLoc) { 14787 // OpenMP [2.13.2, critical construct, Description] 14788 // ... where hint-expression is an integer constant expression that evaluates 14789 // to a valid lock hint. 14790 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 14791 if (HintExpr.isInvalid()) 14792 return nullptr; 14793 return new (Context) 14794 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 14795 } 14796 14797 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 14798 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 14799 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 14800 SourceLocation EndLoc) { 14801 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 14802 std::string Values; 14803 Values += "'"; 14804 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 14805 Values += "'"; 14806 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 14807 << Values << getOpenMPClauseName(OMPC_dist_schedule); 14808 return nullptr; 14809 } 14810 Expr *ValExpr = ChunkSize; 14811 Stmt *HelperValStmt = nullptr; 14812 if (ChunkSize) { 14813 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 14814 !ChunkSize->isInstantiationDependent() && 14815 !ChunkSize->containsUnexpandedParameterPack()) { 14816 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 14817 ExprResult Val = 14818 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 14819 if (Val.isInvalid()) 14820 return nullptr; 14821 14822 ValExpr = Val.get(); 14823 14824 // OpenMP [2.7.1, Restrictions] 14825 // chunk_size must be a loop invariant integer expression with a positive 14826 // value. 14827 llvm::APSInt Result; 14828 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 14829 if (Result.isSigned() && !Result.isStrictlyPositive()) { 14830 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 14831 << "dist_schedule" << ChunkSize->getSourceRange(); 14832 return nullptr; 14833 } 14834 } else if (getOpenMPCaptureRegionForClause( 14835 DSAStack->getCurrentDirective(), OMPC_dist_schedule) != 14836 OMPD_unknown && 14837 !CurContext->isDependentContext()) { 14838 ValExpr = MakeFullExpr(ValExpr).get(); 14839 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 14840 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 14841 HelperValStmt = buildPreInits(Context, Captures); 14842 } 14843 } 14844 } 14845 14846 return new (Context) 14847 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 14848 Kind, ValExpr, HelperValStmt); 14849 } 14850 14851 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 14852 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 14853 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 14854 SourceLocation KindLoc, SourceLocation EndLoc) { 14855 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)' 14856 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) { 14857 std::string Value; 14858 SourceLocation Loc; 14859 Value += "'"; 14860 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 14861 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 14862 OMPC_DEFAULTMAP_MODIFIER_tofrom); 14863 Loc = MLoc; 14864 } else { 14865 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 14866 OMPC_DEFAULTMAP_scalar); 14867 Loc = KindLoc; 14868 } 14869 Value += "'"; 14870 Diag(Loc, diag::err_omp_unexpected_clause_value) 14871 << Value << getOpenMPClauseName(OMPC_defaultmap); 14872 return nullptr; 14873 } 14874 DSAStack->setDefaultDMAToFromScalar(StartLoc); 14875 14876 return new (Context) 14877 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 14878 } 14879 14880 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { 14881 DeclContext *CurLexicalContext = getCurLexicalContext(); 14882 if (!CurLexicalContext->isFileContext() && 14883 !CurLexicalContext->isExternCContext() && 14884 !CurLexicalContext->isExternCXXContext() && 14885 !isa<CXXRecordDecl>(CurLexicalContext) && 14886 !isa<ClassTemplateDecl>(CurLexicalContext) && 14887 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 14888 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 14889 Diag(Loc, diag::err_omp_region_not_file_context); 14890 return false; 14891 } 14892 ++DeclareTargetNestingLevel; 14893 return true; 14894 } 14895 14896 void Sema::ActOnFinishOpenMPDeclareTargetDirective() { 14897 assert(DeclareTargetNestingLevel > 0 && 14898 "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); 14899 --DeclareTargetNestingLevel; 14900 } 14901 14902 void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, 14903 CXXScopeSpec &ScopeSpec, 14904 const DeclarationNameInfo &Id, 14905 OMPDeclareTargetDeclAttr::MapTypeTy MT, 14906 NamedDeclSetType &SameDirectiveDecls) { 14907 LookupResult Lookup(*this, Id, LookupOrdinaryName); 14908 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 14909 14910 if (Lookup.isAmbiguous()) 14911 return; 14912 Lookup.suppressDiagnostics(); 14913 14914 if (!Lookup.isSingleResult()) { 14915 VarOrFuncDeclFilterCCC CCC(*this); 14916 if (TypoCorrection Corrected = 14917 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 14918 CTK_ErrorRecovery)) { 14919 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 14920 << Id.getName()); 14921 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 14922 return; 14923 } 14924 14925 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 14926 return; 14927 } 14928 14929 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 14930 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) || 14931 isa<FunctionTemplateDecl>(ND)) { 14932 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) 14933 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); 14934 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 14935 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 14936 cast<ValueDecl>(ND)); 14937 if (!Res) { 14938 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT); 14939 ND->addAttr(A); 14940 if (ASTMutationListener *ML = Context.getASTMutationListener()) 14941 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 14942 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc()); 14943 } else if (*Res != MT) { 14944 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link) 14945 << Id.getName(); 14946 } 14947 } else { 14948 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 14949 } 14950 } 14951 14952 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 14953 Sema &SemaRef, Decl *D) { 14954 if (!D || !isa<VarDecl>(D)) 14955 return; 14956 auto *VD = cast<VarDecl>(D); 14957 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 14958 return; 14959 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context); 14960 SemaRef.Diag(SL, diag::note_used_here) << SR; 14961 } 14962 14963 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 14964 Sema &SemaRef, DSAStackTy *Stack, 14965 ValueDecl *VD) { 14966 return VD->hasAttr<OMPDeclareTargetDeclAttr>() || 14967 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 14968 /*FullCheck=*/false); 14969 } 14970 14971 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 14972 SourceLocation IdLoc) { 14973 if (!D || D->isInvalidDecl()) 14974 return; 14975 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 14976 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation(); 14977 if (auto *VD = dyn_cast<VarDecl>(D)) { 14978 // Only global variables can be marked as declare target. 14979 if (!VD->isFileVarDecl() && !VD->isStaticLocal() && 14980 !VD->isStaticDataMember()) 14981 return; 14982 // 2.10.6: threadprivate variable cannot appear in a declare target 14983 // directive. 14984 if (DSAStack->isThreadPrivate(VD)) { 14985 Diag(SL, diag::err_omp_threadprivate_in_target); 14986 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 14987 return; 14988 } 14989 } 14990 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) 14991 D = FTD->getTemplatedDecl(); 14992 if (const auto *FD = dyn_cast<FunctionDecl>(D)) { 14993 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 14994 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD); 14995 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { 14996 assert(IdLoc.isValid() && "Source location is expected"); 14997 Diag(IdLoc, diag::err_omp_function_in_link_clause); 14998 Diag(FD->getLocation(), diag::note_defined_here) << FD; 14999 return; 15000 } 15001 } 15002 if (auto *VD = dyn_cast<ValueDecl>(D)) { 15003 // Problem if any with var declared with incomplete type will be reported 15004 // as normal, so no need to check it here. 15005 if ((E || !VD->getType()->isIncompleteType()) && 15006 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) 15007 return; 15008 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { 15009 // Checking declaration inside declare target region. 15010 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 15011 isa<FunctionTemplateDecl>(D)) { 15012 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 15013 Context, OMPDeclareTargetDeclAttr::MT_To); 15014 D->addAttr(A); 15015 if (ASTMutationListener *ML = Context.getASTMutationListener()) 15016 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 15017 } 15018 return; 15019 } 15020 } 15021 if (!E) 15022 return; 15023 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 15024 } 15025 15026 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList, 15027 CXXScopeSpec &MapperIdScopeSpec, 15028 DeclarationNameInfo &MapperId, 15029 const OMPVarListLocTy &Locs, 15030 ArrayRef<Expr *> UnresolvedMappers) { 15031 MappableVarListInfo MVLI(VarList); 15032 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc, 15033 MapperIdScopeSpec, MapperId, UnresolvedMappers); 15034 if (MVLI.ProcessedVarList.empty()) 15035 return nullptr; 15036 15037 return OMPToClause::Create( 15038 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 15039 MVLI.VarComponents, MVLI.UDMapperList, 15040 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 15041 } 15042 15043 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList, 15044 CXXScopeSpec &MapperIdScopeSpec, 15045 DeclarationNameInfo &MapperId, 15046 const OMPVarListLocTy &Locs, 15047 ArrayRef<Expr *> UnresolvedMappers) { 15048 MappableVarListInfo MVLI(VarList); 15049 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc, 15050 MapperIdScopeSpec, MapperId, UnresolvedMappers); 15051 if (MVLI.ProcessedVarList.empty()) 15052 return nullptr; 15053 15054 return OMPFromClause::Create( 15055 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 15056 MVLI.VarComponents, MVLI.UDMapperList, 15057 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 15058 } 15059 15060 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 15061 const OMPVarListLocTy &Locs) { 15062 MappableVarListInfo MVLI(VarList); 15063 SmallVector<Expr *, 8> PrivateCopies; 15064 SmallVector<Expr *, 8> Inits; 15065 15066 for (Expr *RefExpr : VarList) { 15067 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 15068 SourceLocation ELoc; 15069 SourceRange ERange; 15070 Expr *SimpleRefExpr = RefExpr; 15071 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15072 if (Res.second) { 15073 // It will be analyzed later. 15074 MVLI.ProcessedVarList.push_back(RefExpr); 15075 PrivateCopies.push_back(nullptr); 15076 Inits.push_back(nullptr); 15077 } 15078 ValueDecl *D = Res.first; 15079 if (!D) 15080 continue; 15081 15082 QualType Type = D->getType(); 15083 Type = Type.getNonReferenceType().getUnqualifiedType(); 15084 15085 auto *VD = dyn_cast<VarDecl>(D); 15086 15087 // Item should be a pointer or reference to pointer. 15088 if (!Type->isPointerType()) { 15089 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 15090 << 0 << RefExpr->getSourceRange(); 15091 continue; 15092 } 15093 15094 // Build the private variable and the expression that refers to it. 15095 auto VDPrivate = 15096 buildVarDecl(*this, ELoc, Type, D->getName(), 15097 D->hasAttrs() ? &D->getAttrs() : nullptr, 15098 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 15099 if (VDPrivate->isInvalidDecl()) 15100 continue; 15101 15102 CurContext->addDecl(VDPrivate); 15103 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 15104 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 15105 15106 // Add temporary variable to initialize the private copy of the pointer. 15107 VarDecl *VDInit = 15108 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 15109 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 15110 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 15111 AddInitializerToDecl(VDPrivate, 15112 DefaultLvalueConversion(VDInitRefExpr).get(), 15113 /*DirectInit=*/false); 15114 15115 // If required, build a capture to implement the privatization initialized 15116 // with the current list item value. 15117 DeclRefExpr *Ref = nullptr; 15118 if (!VD) 15119 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 15120 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 15121 PrivateCopies.push_back(VDPrivateRefExpr); 15122 Inits.push_back(VDInitRefExpr); 15123 15124 // We need to add a data sharing attribute for this variable to make sure it 15125 // is correctly captured. A variable that shows up in a use_device_ptr has 15126 // similar properties of a first private variable. 15127 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 15128 15129 // Create a mappable component for the list item. List items in this clause 15130 // only need a component. 15131 MVLI.VarBaseDeclarations.push_back(D); 15132 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 15133 MVLI.VarComponents.back().push_back( 15134 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D)); 15135 } 15136 15137 if (MVLI.ProcessedVarList.empty()) 15138 return nullptr; 15139 15140 return OMPUseDevicePtrClause::Create( 15141 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits, 15142 MVLI.VarBaseDeclarations, MVLI.VarComponents); 15143 } 15144 15145 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 15146 const OMPVarListLocTy &Locs) { 15147 MappableVarListInfo MVLI(VarList); 15148 for (Expr *RefExpr : VarList) { 15149 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 15150 SourceLocation ELoc; 15151 SourceRange ERange; 15152 Expr *SimpleRefExpr = RefExpr; 15153 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15154 if (Res.second) { 15155 // It will be analyzed later. 15156 MVLI.ProcessedVarList.push_back(RefExpr); 15157 } 15158 ValueDecl *D = Res.first; 15159 if (!D) 15160 continue; 15161 15162 QualType Type = D->getType(); 15163 // item should be a pointer or array or reference to pointer or array 15164 if (!Type.getNonReferenceType()->isPointerType() && 15165 !Type.getNonReferenceType()->isArrayType()) { 15166 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 15167 << 0 << RefExpr->getSourceRange(); 15168 continue; 15169 } 15170 15171 // Check if the declaration in the clause does not show up in any data 15172 // sharing attribute. 15173 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 15174 if (isOpenMPPrivate(DVar.CKind)) { 15175 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 15176 << getOpenMPClauseName(DVar.CKind) 15177 << getOpenMPClauseName(OMPC_is_device_ptr) 15178 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 15179 reportOriginalDsa(*this, DSAStack, D, DVar); 15180 continue; 15181 } 15182 15183 const Expr *ConflictExpr; 15184 if (DSAStack->checkMappableExprComponentListsForDecl( 15185 D, /*CurrentRegionOnly=*/true, 15186 [&ConflictExpr]( 15187 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 15188 OpenMPClauseKind) -> bool { 15189 ConflictExpr = R.front().getAssociatedExpression(); 15190 return true; 15191 })) { 15192 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 15193 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 15194 << ConflictExpr->getSourceRange(); 15195 continue; 15196 } 15197 15198 // Store the components in the stack so that they can be used to check 15199 // against other clauses later on. 15200 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D); 15201 DSAStack->addMappableExpressionComponents( 15202 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 15203 15204 // Record the expression we've just processed. 15205 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 15206 15207 // Create a mappable component for the list item. List items in this clause 15208 // only need a component. We use a null declaration to signal fields in 15209 // 'this'. 15210 assert((isa<DeclRefExpr>(SimpleRefExpr) || 15211 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 15212 "Unexpected device pointer expression!"); 15213 MVLI.VarBaseDeclarations.push_back( 15214 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 15215 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 15216 MVLI.VarComponents.back().push_back(MC); 15217 } 15218 15219 if (MVLI.ProcessedVarList.empty()) 15220 return nullptr; 15221 15222 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList, 15223 MVLI.VarBaseDeclarations, 15224 MVLI.VarComponents); 15225 } 15226 15227 OMPClause *Sema::ActOnOpenMPAllocateClause( 15228 Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc, 15229 SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 15230 if (Allocator) { 15231 // OpenMP [2.11.4 allocate Clause, Description] 15232 // allocator is an expression of omp_allocator_handle_t type. 15233 if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack)) 15234 return nullptr; 15235 15236 ExprResult AllocatorRes = DefaultLvalueConversion(Allocator); 15237 if (AllocatorRes.isInvalid()) 15238 return nullptr; 15239 AllocatorRes = PerformImplicitConversion(AllocatorRes.get(), 15240 DSAStack->getOMPAllocatorHandleT(), 15241 Sema::AA_Initializing, 15242 /*AllowExplicit=*/true); 15243 if (AllocatorRes.isInvalid()) 15244 return nullptr; 15245 Allocator = AllocatorRes.get(); 15246 } else { 15247 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions. 15248 // allocate clauses that appear on a target construct or on constructs in a 15249 // target region must specify an allocator expression unless a requires 15250 // directive with the dynamic_allocators clause is present in the same 15251 // compilation unit. 15252 if (LangOpts.OpenMPIsDevice && 15253 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 15254 targetDiag(StartLoc, diag::err_expected_allocator_expression); 15255 } 15256 // Analyze and build list of variables. 15257 SmallVector<Expr *, 8> Vars; 15258 for (Expr *RefExpr : VarList) { 15259 assert(RefExpr && "NULL expr in OpenMP private clause."); 15260 SourceLocation ELoc; 15261 SourceRange ERange; 15262 Expr *SimpleRefExpr = RefExpr; 15263 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15264 if (Res.second) { 15265 // It will be analyzed later. 15266 Vars.push_back(RefExpr); 15267 } 15268 ValueDecl *D = Res.first; 15269 if (!D) 15270 continue; 15271 15272 auto *VD = dyn_cast<VarDecl>(D); 15273 DeclRefExpr *Ref = nullptr; 15274 if (!VD && !CurContext->isDependentContext()) 15275 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 15276 Vars.push_back((VD || CurContext->isDependentContext()) 15277 ? RefExpr->IgnoreParens() 15278 : Ref); 15279 } 15280 15281 if (Vars.empty()) 15282 return nullptr; 15283 15284 return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator, 15285 ColonLoc, EndLoc, Vars); 15286 } 15287