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/OpenMPClause.h" 22 #include "clang/AST/StmtCXX.h" 23 #include "clang/AST/StmtOpenMP.h" 24 #include "clang/AST/StmtVisitor.h" 25 #include "clang/AST/TypeOrdering.h" 26 #include "clang/Basic/DiagnosticSema.h" 27 #include "clang/Basic/OpenMPKinds.h" 28 #include "clang/Basic/PartialDiagnostic.h" 29 #include "clang/Basic/TargetInfo.h" 30 #include "clang/Sema/Initialization.h" 31 #include "clang/Sema/Lookup.h" 32 #include "clang/Sema/Scope.h" 33 #include "clang/Sema/ScopeInfo.h" 34 #include "clang/Sema/SemaInternal.h" 35 #include "llvm/ADT/IndexedMap.h" 36 #include "llvm/ADT/PointerEmbeddedInt.h" 37 #include "llvm/ADT/STLExtras.h" 38 #include "llvm/Frontend/OpenMP/OMPConstants.h" 39 #include <set> 40 41 using namespace clang; 42 using namespace llvm::omp; 43 44 //===----------------------------------------------------------------------===// 45 // Stack of data-sharing attributes for variables 46 //===----------------------------------------------------------------------===// 47 48 static const Expr *checkMapClauseExpressionBase( 49 Sema &SemaRef, Expr *E, 50 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 51 OpenMPClauseKind CKind, bool NoDiagnose); 52 53 namespace { 54 /// Default data sharing attributes, which can be applied to directive. 55 enum DefaultDataSharingAttributes { 56 DSA_unspecified = 0, /// Data sharing attribute not specified. 57 DSA_none = 1 << 0, /// Default data sharing attribute 'none'. 58 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'. 59 DSA_firstprivate = 1 << 2, /// Default data sharing attribute 'firstprivate'. 60 }; 61 62 /// Stack for tracking declarations used in OpenMP directives and 63 /// clauses and their data-sharing attributes. 64 class DSAStackTy { 65 public: 66 struct DSAVarData { 67 OpenMPDirectiveKind DKind = OMPD_unknown; 68 OpenMPClauseKind CKind = OMPC_unknown; 69 unsigned Modifier = 0; 70 const Expr *RefExpr = nullptr; 71 DeclRefExpr *PrivateCopy = nullptr; 72 SourceLocation ImplicitDSALoc; 73 DSAVarData() = default; 74 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, 75 const Expr *RefExpr, DeclRefExpr *PrivateCopy, 76 SourceLocation ImplicitDSALoc, unsigned Modifier) 77 : DKind(DKind), CKind(CKind), Modifier(Modifier), RefExpr(RefExpr), 78 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {} 79 }; 80 using OperatorOffsetTy = 81 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>; 82 using DoacrossDependMapTy = 83 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>; 84 /// Kind of the declaration used in the uses_allocators clauses. 85 enum class UsesAllocatorsDeclKind { 86 /// Predefined allocator 87 PredefinedAllocator, 88 /// User-defined allocator 89 UserDefinedAllocator, 90 /// The declaration that represent allocator trait 91 AllocatorTrait, 92 }; 93 94 private: 95 struct DSAInfo { 96 OpenMPClauseKind Attributes = OMPC_unknown; 97 unsigned Modifier = 0; 98 /// Pointer to a reference expression and a flag which shows that the 99 /// variable is marked as lastprivate(true) or not (false). 100 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr; 101 DeclRefExpr *PrivateCopy = nullptr; 102 }; 103 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>; 104 using UsedRefMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>; 105 using LCDeclInfo = std::pair<unsigned, VarDecl *>; 106 using LoopControlVariablesMapTy = 107 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>; 108 /// Struct that associates a component with the clause kind where they are 109 /// found. 110 struct MappedExprComponentTy { 111 OMPClauseMappableExprCommon::MappableExprComponentLists Components; 112 OpenMPClauseKind Kind = OMPC_unknown; 113 }; 114 using MappedExprComponentsTy = 115 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>; 116 using CriticalsWithHintsTy = 117 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>; 118 struct ReductionData { 119 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>; 120 SourceRange ReductionRange; 121 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp; 122 ReductionData() = default; 123 void set(BinaryOperatorKind BO, SourceRange RR) { 124 ReductionRange = RR; 125 ReductionOp = BO; 126 } 127 void set(const Expr *RefExpr, SourceRange RR) { 128 ReductionRange = RR; 129 ReductionOp = RefExpr; 130 } 131 }; 132 using DeclReductionMapTy = 133 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>; 134 struct DefaultmapInfo { 135 OpenMPDefaultmapClauseModifier ImplicitBehavior = 136 OMPC_DEFAULTMAP_MODIFIER_unknown; 137 SourceLocation SLoc; 138 DefaultmapInfo() = default; 139 DefaultmapInfo(OpenMPDefaultmapClauseModifier M, SourceLocation Loc) 140 : ImplicitBehavior(M), SLoc(Loc) {} 141 }; 142 143 struct SharingMapTy { 144 DeclSAMapTy SharingMap; 145 DeclReductionMapTy ReductionMap; 146 UsedRefMapTy AlignedMap; 147 UsedRefMapTy NontemporalMap; 148 MappedExprComponentsTy MappedExprComponents; 149 LoopControlVariablesMapTy LCVMap; 150 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified; 151 SourceLocation DefaultAttrLoc; 152 DefaultmapInfo DefaultmapMap[OMPC_DEFAULTMAP_unknown]; 153 OpenMPDirectiveKind Directive = OMPD_unknown; 154 DeclarationNameInfo DirectiveName; 155 Scope *CurScope = nullptr; 156 SourceLocation ConstructLoc; 157 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to 158 /// get the data (loop counters etc.) about enclosing loop-based construct. 159 /// This data is required during codegen. 160 DoacrossDependMapTy DoacrossDepends; 161 /// First argument (Expr *) contains optional argument of the 162 /// 'ordered' clause, the second one is true if the regions has 'ordered' 163 /// clause, false otherwise. 164 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion; 165 unsigned AssociatedLoops = 1; 166 bool HasMutipleLoops = false; 167 const Decl *PossiblyLoopCounter = nullptr; 168 bool NowaitRegion = false; 169 bool CancelRegion = false; 170 bool LoopStart = false; 171 bool BodyComplete = false; 172 SourceLocation PrevScanLocation; 173 SourceLocation PrevOrderedLocation; 174 SourceLocation InnerTeamsRegionLoc; 175 /// Reference to the taskgroup task_reduction reference expression. 176 Expr *TaskgroupReductionRef = nullptr; 177 llvm::DenseSet<QualType> MappedClassesQualTypes; 178 SmallVector<Expr *, 4> InnerUsedAllocators; 179 llvm::DenseSet<CanonicalDeclPtr<Decl>> ImplicitTaskFirstprivates; 180 /// List of globals marked as declare target link in this target region 181 /// (isOpenMPTargetExecutionDirective(Directive) == true). 182 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls; 183 /// List of decls used in inclusive/exclusive clauses of the scan directive. 184 llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective; 185 llvm::DenseMap<CanonicalDeclPtr<const Decl>, UsesAllocatorsDeclKind> 186 UsesAllocatorsDecls; 187 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 188 Scope *CurScope, SourceLocation Loc) 189 : Directive(DKind), DirectiveName(Name), CurScope(CurScope), 190 ConstructLoc(Loc) {} 191 SharingMapTy() = default; 192 }; 193 194 using StackTy = SmallVector<SharingMapTy, 4>; 195 196 /// Stack of used declaration and their data-sharing attributes. 197 DeclSAMapTy Threadprivates; 198 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr; 199 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack; 200 /// true, if check for DSA must be from parent directive, false, if 201 /// from current directive. 202 OpenMPClauseKind ClauseKindMode = OMPC_unknown; 203 Sema &SemaRef; 204 bool ForceCapturing = false; 205 /// true if all the variables in the target executable directives must be 206 /// captured by reference. 207 bool ForceCaptureByReferenceInTargetExecutable = false; 208 CriticalsWithHintsTy Criticals; 209 unsigned IgnoredStackElements = 0; 210 211 /// Iterators over the stack iterate in order from innermost to outermost 212 /// directive. 213 using const_iterator = StackTy::const_reverse_iterator; 214 const_iterator begin() const { 215 return Stack.empty() ? const_iterator() 216 : Stack.back().first.rbegin() + IgnoredStackElements; 217 } 218 const_iterator end() const { 219 return Stack.empty() ? const_iterator() : Stack.back().first.rend(); 220 } 221 using iterator = StackTy::reverse_iterator; 222 iterator begin() { 223 return Stack.empty() ? iterator() 224 : Stack.back().first.rbegin() + IgnoredStackElements; 225 } 226 iterator end() { 227 return Stack.empty() ? iterator() : Stack.back().first.rend(); 228 } 229 230 // Convenience operations to get at the elements of the stack. 231 232 bool isStackEmpty() const { 233 return Stack.empty() || 234 Stack.back().second != CurrentNonCapturingFunctionScope || 235 Stack.back().first.size() <= IgnoredStackElements; 236 } 237 size_t getStackSize() const { 238 return isStackEmpty() ? 0 239 : Stack.back().first.size() - IgnoredStackElements; 240 } 241 242 SharingMapTy *getTopOfStackOrNull() { 243 size_t Size = getStackSize(); 244 if (Size == 0) 245 return nullptr; 246 return &Stack.back().first[Size - 1]; 247 } 248 const SharingMapTy *getTopOfStackOrNull() const { 249 return const_cast<DSAStackTy&>(*this).getTopOfStackOrNull(); 250 } 251 SharingMapTy &getTopOfStack() { 252 assert(!isStackEmpty() && "no current directive"); 253 return *getTopOfStackOrNull(); 254 } 255 const SharingMapTy &getTopOfStack() const { 256 return const_cast<DSAStackTy&>(*this).getTopOfStack(); 257 } 258 259 SharingMapTy *getSecondOnStackOrNull() { 260 size_t Size = getStackSize(); 261 if (Size <= 1) 262 return nullptr; 263 return &Stack.back().first[Size - 2]; 264 } 265 const SharingMapTy *getSecondOnStackOrNull() const { 266 return const_cast<DSAStackTy&>(*this).getSecondOnStackOrNull(); 267 } 268 269 /// Get the stack element at a certain level (previously returned by 270 /// \c getNestingLevel). 271 /// 272 /// Note that nesting levels count from outermost to innermost, and this is 273 /// the reverse of our iteration order where new inner levels are pushed at 274 /// the front of the stack. 275 SharingMapTy &getStackElemAtLevel(unsigned Level) { 276 assert(Level < getStackSize() && "no such stack element"); 277 return Stack.back().first[Level]; 278 } 279 const SharingMapTy &getStackElemAtLevel(unsigned Level) const { 280 return const_cast<DSAStackTy&>(*this).getStackElemAtLevel(Level); 281 } 282 283 DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const; 284 285 /// Checks if the variable is a local for OpenMP region. 286 bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const; 287 288 /// Vector of previously declared requires directives 289 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls; 290 /// omp_allocator_handle_t type. 291 QualType OMPAllocatorHandleT; 292 /// omp_depend_t type. 293 QualType OMPDependT; 294 /// omp_event_handle_t type. 295 QualType OMPEventHandleT; 296 /// omp_alloctrait_t type. 297 QualType OMPAlloctraitT; 298 /// Expression for the predefined allocators. 299 Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = { 300 nullptr}; 301 /// Vector of previously encountered target directives 302 SmallVector<SourceLocation, 2> TargetLocations; 303 SourceLocation AtomicLocation; 304 305 public: 306 explicit DSAStackTy(Sema &S) : SemaRef(S) {} 307 308 /// Sets omp_allocator_handle_t type. 309 void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; } 310 /// Gets omp_allocator_handle_t type. 311 QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; } 312 /// Sets omp_alloctrait_t type. 313 void setOMPAlloctraitT(QualType Ty) { OMPAlloctraitT = Ty; } 314 /// Gets omp_alloctrait_t type. 315 QualType getOMPAlloctraitT() const { return OMPAlloctraitT; } 316 /// Sets the given default allocator. 317 void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 318 Expr *Allocator) { 319 OMPPredefinedAllocators[AllocatorKind] = Allocator; 320 } 321 /// Returns the specified default allocator. 322 Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const { 323 return OMPPredefinedAllocators[AllocatorKind]; 324 } 325 /// Sets omp_depend_t type. 326 void setOMPDependT(QualType Ty) { OMPDependT = Ty; } 327 /// Gets omp_depend_t type. 328 QualType getOMPDependT() const { return OMPDependT; } 329 330 /// Sets omp_event_handle_t type. 331 void setOMPEventHandleT(QualType Ty) { OMPEventHandleT = Ty; } 332 /// Gets omp_event_handle_t type. 333 QualType getOMPEventHandleT() const { return OMPEventHandleT; } 334 335 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; } 336 OpenMPClauseKind getClauseParsingMode() const { 337 assert(isClauseParsingMode() && "Must be in clause parsing mode."); 338 return ClauseKindMode; 339 } 340 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; } 341 342 bool isBodyComplete() const { 343 const SharingMapTy *Top = getTopOfStackOrNull(); 344 return Top && Top->BodyComplete; 345 } 346 void setBodyComplete() { 347 getTopOfStack().BodyComplete = true; 348 } 349 350 bool isForceVarCapturing() const { return ForceCapturing; } 351 void setForceVarCapturing(bool V) { ForceCapturing = V; } 352 353 void setForceCaptureByReferenceInTargetExecutable(bool V) { 354 ForceCaptureByReferenceInTargetExecutable = V; 355 } 356 bool isForceCaptureByReferenceInTargetExecutable() const { 357 return ForceCaptureByReferenceInTargetExecutable; 358 } 359 360 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 361 Scope *CurScope, SourceLocation Loc) { 362 assert(!IgnoredStackElements && 363 "cannot change stack while ignoring elements"); 364 if (Stack.empty() || 365 Stack.back().second != CurrentNonCapturingFunctionScope) 366 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope); 367 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc); 368 Stack.back().first.back().DefaultAttrLoc = Loc; 369 } 370 371 void pop() { 372 assert(!IgnoredStackElements && 373 "cannot change stack while ignoring elements"); 374 assert(!Stack.back().first.empty() && 375 "Data-sharing attributes stack is empty!"); 376 Stack.back().first.pop_back(); 377 } 378 379 /// RAII object to temporarily leave the scope of a directive when we want to 380 /// logically operate in its parent. 381 class ParentDirectiveScope { 382 DSAStackTy &Self; 383 bool Active; 384 public: 385 ParentDirectiveScope(DSAStackTy &Self, bool Activate) 386 : Self(Self), Active(false) { 387 if (Activate) 388 enable(); 389 } 390 ~ParentDirectiveScope() { disable(); } 391 void disable() { 392 if (Active) { 393 --Self.IgnoredStackElements; 394 Active = false; 395 } 396 } 397 void enable() { 398 if (!Active) { 399 ++Self.IgnoredStackElements; 400 Active = true; 401 } 402 } 403 }; 404 405 /// Marks that we're started loop parsing. 406 void loopInit() { 407 assert(isOpenMPLoopDirective(getCurrentDirective()) && 408 "Expected loop-based directive."); 409 getTopOfStack().LoopStart = true; 410 } 411 /// Start capturing of the variables in the loop context. 412 void loopStart() { 413 assert(isOpenMPLoopDirective(getCurrentDirective()) && 414 "Expected loop-based directive."); 415 getTopOfStack().LoopStart = false; 416 } 417 /// true, if variables are captured, false otherwise. 418 bool isLoopStarted() const { 419 assert(isOpenMPLoopDirective(getCurrentDirective()) && 420 "Expected loop-based directive."); 421 return !getTopOfStack().LoopStart; 422 } 423 /// Marks (or clears) declaration as possibly loop counter. 424 void resetPossibleLoopCounter(const Decl *D = nullptr) { 425 getTopOfStack().PossiblyLoopCounter = 426 D ? D->getCanonicalDecl() : D; 427 } 428 /// Gets the possible loop counter decl. 429 const Decl *getPossiblyLoopCunter() const { 430 return getTopOfStack().PossiblyLoopCounter; 431 } 432 /// Start new OpenMP region stack in new non-capturing function. 433 void pushFunction() { 434 assert(!IgnoredStackElements && 435 "cannot change stack while ignoring elements"); 436 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction(); 437 assert(!isa<CapturingScopeInfo>(CurFnScope)); 438 CurrentNonCapturingFunctionScope = CurFnScope; 439 } 440 /// Pop region stack for non-capturing function. 441 void popFunction(const FunctionScopeInfo *OldFSI) { 442 assert(!IgnoredStackElements && 443 "cannot change stack while ignoring elements"); 444 if (!Stack.empty() && Stack.back().second == OldFSI) { 445 assert(Stack.back().first.empty()); 446 Stack.pop_back(); 447 } 448 CurrentNonCapturingFunctionScope = nullptr; 449 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) { 450 if (!isa<CapturingScopeInfo>(FSI)) { 451 CurrentNonCapturingFunctionScope = FSI; 452 break; 453 } 454 } 455 } 456 457 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) { 458 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint); 459 } 460 const std::pair<const OMPCriticalDirective *, llvm::APSInt> 461 getCriticalWithHint(const DeclarationNameInfo &Name) const { 462 auto I = Criticals.find(Name.getAsString()); 463 if (I != Criticals.end()) 464 return I->second; 465 return std::make_pair(nullptr, llvm::APSInt()); 466 } 467 /// If 'aligned' declaration for given variable \a D was not seen yet, 468 /// add it and return NULL; otherwise return previous occurrence's expression 469 /// for diagnostics. 470 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE); 471 /// If 'nontemporal' declaration for given variable \a D was not seen yet, 472 /// add it and return NULL; otherwise return previous occurrence's expression 473 /// for diagnostics. 474 const Expr *addUniqueNontemporal(const ValueDecl *D, const Expr *NewDE); 475 476 /// Register specified variable as loop control variable. 477 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture); 478 /// Check if the specified variable is a loop control variable for 479 /// current region. 480 /// \return The index of the loop control variable in the list of associated 481 /// for-loops (from outer to inner). 482 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const; 483 /// Check if the specified variable is a loop control variable for 484 /// parent region. 485 /// \return The index of the loop control variable in the list of associated 486 /// for-loops (from outer to inner). 487 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const; 488 /// Check if the specified variable is a loop control variable for 489 /// current region. 490 /// \return The index of the loop control variable in the list of associated 491 /// for-loops (from outer to inner). 492 const LCDeclInfo isLoopControlVariable(const ValueDecl *D, 493 unsigned Level) const; 494 /// Get the loop control variable for the I-th loop (or nullptr) in 495 /// parent directive. 496 const ValueDecl *getParentLoopControlVariable(unsigned I) const; 497 498 /// Marks the specified decl \p D as used in scan directive. 499 void markDeclAsUsedInScanDirective(ValueDecl *D) { 500 if (SharingMapTy *Stack = getSecondOnStackOrNull()) 501 Stack->UsedInScanDirective.insert(D); 502 } 503 504 /// Checks if the specified declaration was used in the inner scan directive. 505 bool isUsedInScanDirective(ValueDecl *D) const { 506 if (const SharingMapTy *Stack = getTopOfStackOrNull()) 507 return Stack->UsedInScanDirective.count(D) > 0; 508 return false; 509 } 510 511 /// Adds explicit data sharing attribute to the specified declaration. 512 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 513 DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0); 514 515 /// Adds additional information for the reduction items with the reduction id 516 /// represented as an operator. 517 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 518 BinaryOperatorKind BOK); 519 /// Adds additional information for the reduction items with the reduction id 520 /// represented as reduction identifier. 521 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 522 const Expr *ReductionRef); 523 /// Returns the location and reduction operation from the innermost parent 524 /// region for the given \p D. 525 const DSAVarData 526 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 527 BinaryOperatorKind &BOK, 528 Expr *&TaskgroupDescriptor) const; 529 /// Returns the location and reduction operation from the innermost parent 530 /// region for the given \p D. 531 const DSAVarData 532 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR, 533 const Expr *&ReductionRef, 534 Expr *&TaskgroupDescriptor) const; 535 /// Return reduction reference expression for the current taskgroup or 536 /// parallel/worksharing directives with task reductions. 537 Expr *getTaskgroupReductionRef() const { 538 assert((getTopOfStack().Directive == OMPD_taskgroup || 539 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 540 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 541 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 542 "taskgroup reference expression requested for non taskgroup or " 543 "parallel/worksharing directive."); 544 return getTopOfStack().TaskgroupReductionRef; 545 } 546 /// Checks if the given \p VD declaration is actually a taskgroup reduction 547 /// descriptor variable at the \p Level of OpenMP regions. 548 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const { 549 return getStackElemAtLevel(Level).TaskgroupReductionRef && 550 cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef) 551 ->getDecl() == VD; 552 } 553 554 /// Returns data sharing attributes from top of the stack for the 555 /// specified declaration. 556 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent); 557 /// Returns data-sharing attributes for the specified declaration. 558 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const; 559 /// Returns data-sharing attributes for the specified declaration. 560 const DSAVarData getImplicitDSA(ValueDecl *D, unsigned Level) const; 561 /// Checks if the specified variables has data-sharing attributes which 562 /// match specified \a CPred predicate in any directive which matches \a DPred 563 /// predicate. 564 const DSAVarData 565 hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 566 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 567 bool FromParent) const; 568 /// Checks if the specified variables has data-sharing attributes which 569 /// match specified \a CPred predicate in any innermost directive which 570 /// matches \a DPred predicate. 571 const DSAVarData 572 hasInnermostDSA(ValueDecl *D, 573 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 574 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 575 bool FromParent) const; 576 /// Checks if the specified variables has explicit data-sharing 577 /// attributes which match specified \a CPred predicate at the specified 578 /// OpenMP region. 579 bool hasExplicitDSA(const ValueDecl *D, 580 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 581 unsigned Level, bool NotLastprivate = false) const; 582 583 /// Returns true if the directive at level \Level matches in the 584 /// specified \a DPred predicate. 585 bool hasExplicitDirective( 586 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 587 unsigned Level) const; 588 589 /// Finds a directive which matches specified \a DPred predicate. 590 bool hasDirective( 591 const llvm::function_ref<bool( 592 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)> 593 DPred, 594 bool FromParent) const; 595 596 /// Returns currently analyzed directive. 597 OpenMPDirectiveKind getCurrentDirective() const { 598 const SharingMapTy *Top = getTopOfStackOrNull(); 599 return Top ? Top->Directive : OMPD_unknown; 600 } 601 /// Returns directive kind at specified level. 602 OpenMPDirectiveKind getDirective(unsigned Level) const { 603 assert(!isStackEmpty() && "No directive at specified level."); 604 return getStackElemAtLevel(Level).Directive; 605 } 606 /// Returns the capture region at the specified level. 607 OpenMPDirectiveKind getCaptureRegion(unsigned Level, 608 unsigned OpenMPCaptureLevel) const { 609 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 610 getOpenMPCaptureRegions(CaptureRegions, getDirective(Level)); 611 return CaptureRegions[OpenMPCaptureLevel]; 612 } 613 /// Returns parent directive. 614 OpenMPDirectiveKind getParentDirective() const { 615 const SharingMapTy *Parent = getSecondOnStackOrNull(); 616 return Parent ? Parent->Directive : OMPD_unknown; 617 } 618 619 /// Add requires decl to internal vector 620 void addRequiresDecl(OMPRequiresDecl *RD) { 621 RequiresDecls.push_back(RD); 622 } 623 624 /// Checks if the defined 'requires' directive has specified type of clause. 625 template <typename ClauseType> 626 bool hasRequiresDeclWithClause() const { 627 return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) { 628 return llvm::any_of(D->clauselists(), [](const OMPClause *C) { 629 return isa<ClauseType>(C); 630 }); 631 }); 632 } 633 634 /// Checks for a duplicate clause amongst previously declared requires 635 /// directives 636 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const { 637 bool IsDuplicate = false; 638 for (OMPClause *CNew : ClauseList) { 639 for (const OMPRequiresDecl *D : RequiresDecls) { 640 for (const OMPClause *CPrev : D->clauselists()) { 641 if (CNew->getClauseKind() == CPrev->getClauseKind()) { 642 SemaRef.Diag(CNew->getBeginLoc(), 643 diag::err_omp_requires_clause_redeclaration) 644 << getOpenMPClauseName(CNew->getClauseKind()); 645 SemaRef.Diag(CPrev->getBeginLoc(), 646 diag::note_omp_requires_previous_clause) 647 << getOpenMPClauseName(CPrev->getClauseKind()); 648 IsDuplicate = true; 649 } 650 } 651 } 652 } 653 return IsDuplicate; 654 } 655 656 /// Add location of previously encountered target to internal vector 657 void addTargetDirLocation(SourceLocation LocStart) { 658 TargetLocations.push_back(LocStart); 659 } 660 661 /// Add location for the first encountered atomicc directive. 662 void addAtomicDirectiveLoc(SourceLocation Loc) { 663 if (AtomicLocation.isInvalid()) 664 AtomicLocation = Loc; 665 } 666 667 /// Returns the location of the first encountered atomic directive in the 668 /// module. 669 SourceLocation getAtomicDirectiveLoc() const { 670 return AtomicLocation; 671 } 672 673 // Return previously encountered target region locations. 674 ArrayRef<SourceLocation> getEncounteredTargetLocs() const { 675 return TargetLocations; 676 } 677 678 /// Set default data sharing attribute to none. 679 void setDefaultDSANone(SourceLocation Loc) { 680 getTopOfStack().DefaultAttr = DSA_none; 681 getTopOfStack().DefaultAttrLoc = Loc; 682 } 683 /// Set default data sharing attribute to shared. 684 void setDefaultDSAShared(SourceLocation Loc) { 685 getTopOfStack().DefaultAttr = DSA_shared; 686 getTopOfStack().DefaultAttrLoc = Loc; 687 } 688 /// Set default data sharing attribute to firstprivate. 689 void setDefaultDSAFirstPrivate(SourceLocation Loc) { 690 getTopOfStack().DefaultAttr = DSA_firstprivate; 691 getTopOfStack().DefaultAttrLoc = Loc; 692 } 693 /// Set default data mapping attribute to Modifier:Kind 694 void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M, 695 OpenMPDefaultmapClauseKind Kind, 696 SourceLocation Loc) { 697 DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind]; 698 DMI.ImplicitBehavior = M; 699 DMI.SLoc = Loc; 700 } 701 /// Check whether the implicit-behavior has been set in defaultmap 702 bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) { 703 if (VariableCategory == OMPC_DEFAULTMAP_unknown) 704 return getTopOfStack() 705 .DefaultmapMap[OMPC_DEFAULTMAP_aggregate] 706 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown || 707 getTopOfStack() 708 .DefaultmapMap[OMPC_DEFAULTMAP_scalar] 709 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown || 710 getTopOfStack() 711 .DefaultmapMap[OMPC_DEFAULTMAP_pointer] 712 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown; 713 return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior != 714 OMPC_DEFAULTMAP_MODIFIER_unknown; 715 } 716 717 DefaultDataSharingAttributes getDefaultDSA(unsigned Level) const { 718 return getStackSize() <= Level ? DSA_unspecified 719 : getStackElemAtLevel(Level).DefaultAttr; 720 } 721 DefaultDataSharingAttributes getDefaultDSA() const { 722 return isStackEmpty() ? DSA_unspecified 723 : getTopOfStack().DefaultAttr; 724 } 725 SourceLocation getDefaultDSALocation() const { 726 return isStackEmpty() ? SourceLocation() 727 : getTopOfStack().DefaultAttrLoc; 728 } 729 OpenMPDefaultmapClauseModifier 730 getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const { 731 return isStackEmpty() 732 ? OMPC_DEFAULTMAP_MODIFIER_unknown 733 : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior; 734 } 735 OpenMPDefaultmapClauseModifier 736 getDefaultmapModifierAtLevel(unsigned Level, 737 OpenMPDefaultmapClauseKind Kind) const { 738 return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior; 739 } 740 bool isDefaultmapCapturedByRef(unsigned Level, 741 OpenMPDefaultmapClauseKind Kind) const { 742 OpenMPDefaultmapClauseModifier M = 743 getDefaultmapModifierAtLevel(Level, Kind); 744 if (Kind == OMPC_DEFAULTMAP_scalar || Kind == OMPC_DEFAULTMAP_pointer) { 745 return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) || 746 (M == OMPC_DEFAULTMAP_MODIFIER_to) || 747 (M == OMPC_DEFAULTMAP_MODIFIER_from) || 748 (M == OMPC_DEFAULTMAP_MODIFIER_tofrom); 749 } 750 return true; 751 } 752 static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M, 753 OpenMPDefaultmapClauseKind Kind) { 754 switch (Kind) { 755 case OMPC_DEFAULTMAP_scalar: 756 case OMPC_DEFAULTMAP_pointer: 757 return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) || 758 (M == OMPC_DEFAULTMAP_MODIFIER_firstprivate) || 759 (M == OMPC_DEFAULTMAP_MODIFIER_default); 760 case OMPC_DEFAULTMAP_aggregate: 761 return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate; 762 default: 763 break; 764 } 765 llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum"); 766 } 767 bool mustBeFirstprivateAtLevel(unsigned Level, 768 OpenMPDefaultmapClauseKind Kind) const { 769 OpenMPDefaultmapClauseModifier M = 770 getDefaultmapModifierAtLevel(Level, Kind); 771 return mustBeFirstprivateBase(M, Kind); 772 } 773 bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const { 774 OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind); 775 return mustBeFirstprivateBase(M, Kind); 776 } 777 778 /// Checks if the specified variable is a threadprivate. 779 bool isThreadPrivate(VarDecl *D) { 780 const DSAVarData DVar = getTopDSA(D, false); 781 return isOpenMPThreadPrivate(DVar.CKind); 782 } 783 784 /// Marks current region as ordered (it has an 'ordered' clause). 785 void setOrderedRegion(bool IsOrdered, const Expr *Param, 786 OMPOrderedClause *Clause) { 787 if (IsOrdered) 788 getTopOfStack().OrderedRegion.emplace(Param, Clause); 789 else 790 getTopOfStack().OrderedRegion.reset(); 791 } 792 /// Returns true, if region is ordered (has associated 'ordered' clause), 793 /// false - otherwise. 794 bool isOrderedRegion() const { 795 if (const SharingMapTy *Top = getTopOfStackOrNull()) 796 return Top->OrderedRegion.hasValue(); 797 return false; 798 } 799 /// Returns optional parameter for the ordered region. 800 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const { 801 if (const SharingMapTy *Top = getTopOfStackOrNull()) 802 if (Top->OrderedRegion.hasValue()) 803 return Top->OrderedRegion.getValue(); 804 return std::make_pair(nullptr, nullptr); 805 } 806 /// Returns true, if parent region is ordered (has associated 807 /// 'ordered' clause), false - otherwise. 808 bool isParentOrderedRegion() const { 809 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 810 return Parent->OrderedRegion.hasValue(); 811 return false; 812 } 813 /// Returns optional parameter for the ordered region. 814 std::pair<const Expr *, OMPOrderedClause *> 815 getParentOrderedRegionParam() const { 816 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 817 if (Parent->OrderedRegion.hasValue()) 818 return Parent->OrderedRegion.getValue(); 819 return std::make_pair(nullptr, nullptr); 820 } 821 /// Marks current region as nowait (it has a 'nowait' clause). 822 void setNowaitRegion(bool IsNowait = true) { 823 getTopOfStack().NowaitRegion = IsNowait; 824 } 825 /// Returns true, if parent region is nowait (has associated 826 /// 'nowait' clause), false - otherwise. 827 bool isParentNowaitRegion() const { 828 if (const SharingMapTy *Parent = getSecondOnStackOrNull()) 829 return Parent->NowaitRegion; 830 return false; 831 } 832 /// Marks parent region as cancel region. 833 void setParentCancelRegion(bool Cancel = true) { 834 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 835 Parent->CancelRegion |= Cancel; 836 } 837 /// Return true if current region has inner cancel construct. 838 bool isCancelRegion() const { 839 const SharingMapTy *Top = getTopOfStackOrNull(); 840 return Top ? Top->CancelRegion : false; 841 } 842 843 /// Mark that parent region already has scan directive. 844 void setParentHasScanDirective(SourceLocation Loc) { 845 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 846 Parent->PrevScanLocation = Loc; 847 } 848 /// Return true if current region has inner cancel construct. 849 bool doesParentHasScanDirective() const { 850 const SharingMapTy *Top = getSecondOnStackOrNull(); 851 return Top ? Top->PrevScanLocation.isValid() : false; 852 } 853 /// Return true if current region has inner cancel construct. 854 SourceLocation getParentScanDirectiveLoc() const { 855 const SharingMapTy *Top = getSecondOnStackOrNull(); 856 return Top ? Top->PrevScanLocation : SourceLocation(); 857 } 858 /// Mark that parent region already has ordered directive. 859 void setParentHasOrderedDirective(SourceLocation Loc) { 860 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 861 Parent->PrevOrderedLocation = Loc; 862 } 863 /// Return true if current region has inner ordered construct. 864 bool doesParentHasOrderedDirective() const { 865 const SharingMapTy *Top = getSecondOnStackOrNull(); 866 return Top ? Top->PrevOrderedLocation.isValid() : false; 867 } 868 /// Returns the location of the previously specified ordered directive. 869 SourceLocation getParentOrderedDirectiveLoc() const { 870 const SharingMapTy *Top = getSecondOnStackOrNull(); 871 return Top ? Top->PrevOrderedLocation : SourceLocation(); 872 } 873 874 /// Set collapse value for the region. 875 void setAssociatedLoops(unsigned Val) { 876 getTopOfStack().AssociatedLoops = Val; 877 if (Val > 1) 878 getTopOfStack().HasMutipleLoops = true; 879 } 880 /// Return collapse value for region. 881 unsigned getAssociatedLoops() const { 882 const SharingMapTy *Top = getTopOfStackOrNull(); 883 return Top ? Top->AssociatedLoops : 0; 884 } 885 /// Returns true if the construct is associated with multiple loops. 886 bool hasMutipleLoops() const { 887 const SharingMapTy *Top = getTopOfStackOrNull(); 888 return Top ? Top->HasMutipleLoops : false; 889 } 890 891 /// Marks current target region as one with closely nested teams 892 /// region. 893 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) { 894 if (SharingMapTy *Parent = getSecondOnStackOrNull()) 895 Parent->InnerTeamsRegionLoc = TeamsRegionLoc; 896 } 897 /// Returns true, if current region has closely nested teams region. 898 bool hasInnerTeamsRegion() const { 899 return getInnerTeamsRegionLoc().isValid(); 900 } 901 /// Returns location of the nested teams region (if any). 902 SourceLocation getInnerTeamsRegionLoc() const { 903 const SharingMapTy *Top = getTopOfStackOrNull(); 904 return Top ? Top->InnerTeamsRegionLoc : SourceLocation(); 905 } 906 907 Scope *getCurScope() const { 908 const SharingMapTy *Top = getTopOfStackOrNull(); 909 return Top ? Top->CurScope : nullptr; 910 } 911 SourceLocation getConstructLoc() const { 912 const SharingMapTy *Top = getTopOfStackOrNull(); 913 return Top ? Top->ConstructLoc : SourceLocation(); 914 } 915 916 /// Do the check specified in \a Check to all component lists and return true 917 /// if any issue is found. 918 bool checkMappableExprComponentListsForDecl( 919 const ValueDecl *VD, bool CurrentRegionOnly, 920 const llvm::function_ref< 921 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 922 OpenMPClauseKind)> 923 Check) const { 924 if (isStackEmpty()) 925 return false; 926 auto SI = begin(); 927 auto SE = end(); 928 929 if (SI == SE) 930 return false; 931 932 if (CurrentRegionOnly) 933 SE = std::next(SI); 934 else 935 std::advance(SI, 1); 936 937 for (; SI != SE; ++SI) { 938 auto MI = SI->MappedExprComponents.find(VD); 939 if (MI != SI->MappedExprComponents.end()) 940 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 941 MI->second.Components) 942 if (Check(L, MI->second.Kind)) 943 return true; 944 } 945 return false; 946 } 947 948 /// Do the check specified in \a Check to all component lists at a given level 949 /// and return true if any issue is found. 950 bool checkMappableExprComponentListsForDeclAtLevel( 951 const ValueDecl *VD, unsigned Level, 952 const llvm::function_ref< 953 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef, 954 OpenMPClauseKind)> 955 Check) const { 956 if (getStackSize() <= Level) 957 return false; 958 959 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 960 auto MI = StackElem.MappedExprComponents.find(VD); 961 if (MI != StackElem.MappedExprComponents.end()) 962 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L : 963 MI->second.Components) 964 if (Check(L, MI->second.Kind)) 965 return true; 966 return false; 967 } 968 969 /// Create a new mappable expression component list associated with a given 970 /// declaration and initialize it with the provided list of components. 971 void addMappableExpressionComponents( 972 const ValueDecl *VD, 973 OMPClauseMappableExprCommon::MappableExprComponentListRef Components, 974 OpenMPClauseKind WhereFoundClauseKind) { 975 MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD]; 976 // Create new entry and append the new components there. 977 MEC.Components.resize(MEC.Components.size() + 1); 978 MEC.Components.back().append(Components.begin(), Components.end()); 979 MEC.Kind = WhereFoundClauseKind; 980 } 981 982 unsigned getNestingLevel() const { 983 assert(!isStackEmpty()); 984 return getStackSize() - 1; 985 } 986 void addDoacrossDependClause(OMPDependClause *C, 987 const OperatorOffsetTy &OpsOffs) { 988 SharingMapTy *Parent = getSecondOnStackOrNull(); 989 assert(Parent && isOpenMPWorksharingDirective(Parent->Directive)); 990 Parent->DoacrossDepends.try_emplace(C, OpsOffs); 991 } 992 llvm::iterator_range<DoacrossDependMapTy::const_iterator> 993 getDoacrossDependClauses() const { 994 const SharingMapTy &StackElem = getTopOfStack(); 995 if (isOpenMPWorksharingDirective(StackElem.Directive)) { 996 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends; 997 return llvm::make_range(Ref.begin(), Ref.end()); 998 } 999 return llvm::make_range(StackElem.DoacrossDepends.end(), 1000 StackElem.DoacrossDepends.end()); 1001 } 1002 1003 // Store types of classes which have been explicitly mapped 1004 void addMappedClassesQualTypes(QualType QT) { 1005 SharingMapTy &StackElem = getTopOfStack(); 1006 StackElem.MappedClassesQualTypes.insert(QT); 1007 } 1008 1009 // Return set of mapped classes types 1010 bool isClassPreviouslyMapped(QualType QT) const { 1011 const SharingMapTy &StackElem = getTopOfStack(); 1012 return StackElem.MappedClassesQualTypes.count(QT) != 0; 1013 } 1014 1015 /// Adds global declare target to the parent target region. 1016 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) { 1017 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( 1018 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link && 1019 "Expected declare target link global."); 1020 for (auto &Elem : *this) { 1021 if (isOpenMPTargetExecutionDirective(Elem.Directive)) { 1022 Elem.DeclareTargetLinkVarDecls.push_back(E); 1023 return; 1024 } 1025 } 1026 } 1027 1028 /// Returns the list of globals with declare target link if current directive 1029 /// is target. 1030 ArrayRef<DeclRefExpr *> getLinkGlobals() const { 1031 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) && 1032 "Expected target executable directive."); 1033 return getTopOfStack().DeclareTargetLinkVarDecls; 1034 } 1035 1036 /// Adds list of allocators expressions. 1037 void addInnerAllocatorExpr(Expr *E) { 1038 getTopOfStack().InnerUsedAllocators.push_back(E); 1039 } 1040 /// Return list of used allocators. 1041 ArrayRef<Expr *> getInnerAllocators() const { 1042 return getTopOfStack().InnerUsedAllocators; 1043 } 1044 /// Marks the declaration as implicitly firstprivate nin the task-based 1045 /// regions. 1046 void addImplicitTaskFirstprivate(unsigned Level, Decl *D) { 1047 getStackElemAtLevel(Level).ImplicitTaskFirstprivates.insert(D); 1048 } 1049 /// Checks if the decl is implicitly firstprivate in the task-based region. 1050 bool isImplicitTaskFirstprivate(Decl *D) const { 1051 return getTopOfStack().ImplicitTaskFirstprivates.count(D) > 0; 1052 } 1053 1054 /// Marks decl as used in uses_allocators clause as the allocator. 1055 void addUsesAllocatorsDecl(const Decl *D, UsesAllocatorsDeclKind Kind) { 1056 getTopOfStack().UsesAllocatorsDecls.try_emplace(D, Kind); 1057 } 1058 /// Checks if specified decl is used in uses allocator clause as the 1059 /// allocator. 1060 Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(unsigned Level, 1061 const Decl *D) const { 1062 const SharingMapTy &StackElem = getTopOfStack(); 1063 auto I = StackElem.UsesAllocatorsDecls.find(D); 1064 if (I == StackElem.UsesAllocatorsDecls.end()) 1065 return None; 1066 return I->getSecond(); 1067 } 1068 Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(const Decl *D) const { 1069 const SharingMapTy &StackElem = getTopOfStack(); 1070 auto I = StackElem.UsesAllocatorsDecls.find(D); 1071 if (I == StackElem.UsesAllocatorsDecls.end()) 1072 return None; 1073 return I->getSecond(); 1074 } 1075 }; 1076 1077 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) { 1078 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind); 1079 } 1080 1081 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) { 1082 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) || 1083 DKind == OMPD_unknown; 1084 } 1085 1086 } // namespace 1087 1088 static const Expr *getExprAsWritten(const Expr *E) { 1089 if (const auto *FE = dyn_cast<FullExpr>(E)) 1090 E = FE->getSubExpr(); 1091 1092 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E)) 1093 E = MTE->getSubExpr(); 1094 1095 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E)) 1096 E = Binder->getSubExpr(); 1097 1098 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) 1099 E = ICE->getSubExprAsWritten(); 1100 return E->IgnoreParens(); 1101 } 1102 1103 static Expr *getExprAsWritten(Expr *E) { 1104 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E))); 1105 } 1106 1107 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) { 1108 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) 1109 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 1110 D = ME->getMemberDecl(); 1111 const auto *VD = dyn_cast<VarDecl>(D); 1112 const auto *FD = dyn_cast<FieldDecl>(D); 1113 if (VD != nullptr) { 1114 VD = VD->getCanonicalDecl(); 1115 D = VD; 1116 } else { 1117 assert(FD); 1118 FD = FD->getCanonicalDecl(); 1119 D = FD; 1120 } 1121 return D; 1122 } 1123 1124 static ValueDecl *getCanonicalDecl(ValueDecl *D) { 1125 return const_cast<ValueDecl *>( 1126 getCanonicalDecl(const_cast<const ValueDecl *>(D))); 1127 } 1128 1129 DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter, 1130 ValueDecl *D) const { 1131 D = getCanonicalDecl(D); 1132 auto *VD = dyn_cast<VarDecl>(D); 1133 const auto *FD = dyn_cast<FieldDecl>(D); 1134 DSAVarData DVar; 1135 if (Iter == end()) { 1136 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1137 // in a region but not in construct] 1138 // File-scope or namespace-scope variables referenced in called routines 1139 // in the region are shared unless they appear in a threadprivate 1140 // directive. 1141 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD)) 1142 DVar.CKind = OMPC_shared; 1143 1144 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 1145 // in a region but not in construct] 1146 // Variables with static storage duration that are declared in called 1147 // routines in the region are shared. 1148 if (VD && VD->hasGlobalStorage()) 1149 DVar.CKind = OMPC_shared; 1150 1151 // Non-static data members are shared by default. 1152 if (FD) 1153 DVar.CKind = OMPC_shared; 1154 1155 return DVar; 1156 } 1157 1158 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1159 // in a Construct, C/C++, predetermined, p.1] 1160 // Variables with automatic storage duration that are declared in a scope 1161 // inside the construct are private. 1162 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() && 1163 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) { 1164 DVar.CKind = OMPC_private; 1165 return DVar; 1166 } 1167 1168 DVar.DKind = Iter->Directive; 1169 // Explicitly specified attributes and local variables with predetermined 1170 // attributes. 1171 if (Iter->SharingMap.count(D)) { 1172 const DSAInfo &Data = Iter->SharingMap.lookup(D); 1173 DVar.RefExpr = Data.RefExpr.getPointer(); 1174 DVar.PrivateCopy = Data.PrivateCopy; 1175 DVar.CKind = Data.Attributes; 1176 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1177 DVar.Modifier = Data.Modifier; 1178 return DVar; 1179 } 1180 1181 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1182 // in a Construct, C/C++, implicitly determined, p.1] 1183 // In a parallel or task construct, the data-sharing attributes of these 1184 // variables are determined by the default clause, if present. 1185 switch (Iter->DefaultAttr) { 1186 case DSA_shared: 1187 DVar.CKind = OMPC_shared; 1188 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1189 return DVar; 1190 case DSA_none: 1191 return DVar; 1192 case DSA_firstprivate: 1193 if (VD->getStorageDuration() == SD_Static && 1194 VD->getDeclContext()->isFileContext()) { 1195 DVar.CKind = OMPC_unknown; 1196 } else { 1197 DVar.CKind = OMPC_firstprivate; 1198 } 1199 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1200 return DVar; 1201 case DSA_unspecified: 1202 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1203 // in a Construct, implicitly determined, p.2] 1204 // In a parallel construct, if no default clause is present, these 1205 // variables are shared. 1206 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc; 1207 if ((isOpenMPParallelDirective(DVar.DKind) && 1208 !isOpenMPTaskLoopDirective(DVar.DKind)) || 1209 isOpenMPTeamsDirective(DVar.DKind)) { 1210 DVar.CKind = OMPC_shared; 1211 return DVar; 1212 } 1213 1214 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1215 // in a Construct, implicitly determined, p.4] 1216 // In a task construct, if no default clause is present, a variable that in 1217 // the enclosing context is determined to be shared by all implicit tasks 1218 // bound to the current team is shared. 1219 if (isOpenMPTaskingDirective(DVar.DKind)) { 1220 DSAVarData DVarTemp; 1221 const_iterator I = Iter, E = end(); 1222 do { 1223 ++I; 1224 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 1225 // Referenced in a Construct, implicitly determined, p.6] 1226 // In a task construct, if no default clause is present, a variable 1227 // whose data-sharing attribute is not determined by the rules above is 1228 // firstprivate. 1229 DVarTemp = getDSA(I, D); 1230 if (DVarTemp.CKind != OMPC_shared) { 1231 DVar.RefExpr = nullptr; 1232 DVar.CKind = OMPC_firstprivate; 1233 return DVar; 1234 } 1235 } while (I != E && !isImplicitTaskingRegion(I->Directive)); 1236 DVar.CKind = 1237 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 1238 return DVar; 1239 } 1240 } 1241 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1242 // in a Construct, implicitly determined, p.3] 1243 // For constructs other than task, if no default clause is present, these 1244 // variables inherit their data-sharing attributes from the enclosing 1245 // context. 1246 return getDSA(++Iter, D); 1247 } 1248 1249 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D, 1250 const Expr *NewDE) { 1251 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1252 D = getCanonicalDecl(D); 1253 SharingMapTy &StackElem = getTopOfStack(); 1254 auto It = StackElem.AlignedMap.find(D); 1255 if (It == StackElem.AlignedMap.end()) { 1256 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1257 StackElem.AlignedMap[D] = NewDE; 1258 return nullptr; 1259 } 1260 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1261 return It->second; 1262 } 1263 1264 const Expr *DSAStackTy::addUniqueNontemporal(const ValueDecl *D, 1265 const Expr *NewDE) { 1266 assert(!isStackEmpty() && "Data sharing attributes stack is empty"); 1267 D = getCanonicalDecl(D); 1268 SharingMapTy &StackElem = getTopOfStack(); 1269 auto It = StackElem.NontemporalMap.find(D); 1270 if (It == StackElem.NontemporalMap.end()) { 1271 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 1272 StackElem.NontemporalMap[D] = NewDE; 1273 return nullptr; 1274 } 1275 assert(It->second && "Unexpected nullptr expr in the aligned map"); 1276 return It->second; 1277 } 1278 1279 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) { 1280 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1281 D = getCanonicalDecl(D); 1282 SharingMapTy &StackElem = getTopOfStack(); 1283 StackElem.LCVMap.try_emplace( 1284 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)); 1285 } 1286 1287 const DSAStackTy::LCDeclInfo 1288 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const { 1289 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1290 D = getCanonicalDecl(D); 1291 const SharingMapTy &StackElem = getTopOfStack(); 1292 auto It = StackElem.LCVMap.find(D); 1293 if (It != StackElem.LCVMap.end()) 1294 return It->second; 1295 return {0, nullptr}; 1296 } 1297 1298 const DSAStackTy::LCDeclInfo 1299 DSAStackTy::isLoopControlVariable(const ValueDecl *D, unsigned Level) const { 1300 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1301 D = getCanonicalDecl(D); 1302 for (unsigned I = Level + 1; I > 0; --I) { 1303 const SharingMapTy &StackElem = getStackElemAtLevel(I - 1); 1304 auto It = StackElem.LCVMap.find(D); 1305 if (It != StackElem.LCVMap.end()) 1306 return It->second; 1307 } 1308 return {0, nullptr}; 1309 } 1310 1311 const DSAStackTy::LCDeclInfo 1312 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const { 1313 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1314 assert(Parent && "Data-sharing attributes stack is empty"); 1315 D = getCanonicalDecl(D); 1316 auto It = Parent->LCVMap.find(D); 1317 if (It != Parent->LCVMap.end()) 1318 return It->second; 1319 return {0, nullptr}; 1320 } 1321 1322 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const { 1323 const SharingMapTy *Parent = getSecondOnStackOrNull(); 1324 assert(Parent && "Data-sharing attributes stack is empty"); 1325 if (Parent->LCVMap.size() < I) 1326 return nullptr; 1327 for (const auto &Pair : Parent->LCVMap) 1328 if (Pair.second.first == I) 1329 return Pair.first; 1330 return nullptr; 1331 } 1332 1333 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A, 1334 DeclRefExpr *PrivateCopy, unsigned Modifier) { 1335 D = getCanonicalDecl(D); 1336 if (A == OMPC_threadprivate) { 1337 DSAInfo &Data = Threadprivates[D]; 1338 Data.Attributes = A; 1339 Data.RefExpr.setPointer(E); 1340 Data.PrivateCopy = nullptr; 1341 Data.Modifier = Modifier; 1342 } else { 1343 DSAInfo &Data = getTopOfStack().SharingMap[D]; 1344 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) || 1345 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) || 1346 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) || 1347 (isLoopControlVariable(D).first && A == OMPC_private)); 1348 Data.Modifier = Modifier; 1349 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) { 1350 Data.RefExpr.setInt(/*IntVal=*/true); 1351 return; 1352 } 1353 const bool IsLastprivate = 1354 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate; 1355 Data.Attributes = A; 1356 Data.RefExpr.setPointerAndInt(E, IsLastprivate); 1357 Data.PrivateCopy = PrivateCopy; 1358 if (PrivateCopy) { 1359 DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()]; 1360 Data.Modifier = Modifier; 1361 Data.Attributes = A; 1362 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate); 1363 Data.PrivateCopy = nullptr; 1364 } 1365 } 1366 } 1367 1368 /// Build a variable declaration for OpenMP loop iteration variable. 1369 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type, 1370 StringRef Name, const AttrVec *Attrs = nullptr, 1371 DeclRefExpr *OrigRef = nullptr) { 1372 DeclContext *DC = SemaRef.CurContext; 1373 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 1374 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 1375 auto *Decl = 1376 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); 1377 if (Attrs) { 1378 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end()); 1379 I != E; ++I) 1380 Decl->addAttr(*I); 1381 } 1382 Decl->setImplicit(); 1383 if (OrigRef) { 1384 Decl->addAttr( 1385 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef)); 1386 } 1387 return Decl; 1388 } 1389 1390 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty, 1391 SourceLocation Loc, 1392 bool RefersToCapture = false) { 1393 D->setReferenced(); 1394 D->markUsed(S.Context); 1395 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(), 1396 SourceLocation(), D, RefersToCapture, Loc, Ty, 1397 VK_LValue); 1398 } 1399 1400 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1401 BinaryOperatorKind BOK) { 1402 D = getCanonicalDecl(D); 1403 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1404 assert( 1405 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1406 "Additional reduction info may be specified only for reduction items."); 1407 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1408 assert(ReductionData.ReductionRange.isInvalid() && 1409 (getTopOfStack().Directive == OMPD_taskgroup || 1410 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 1411 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 1412 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 1413 "Additional reduction info may be specified only once for reduction " 1414 "items."); 1415 ReductionData.set(BOK, SR); 1416 Expr *&TaskgroupReductionRef = 1417 getTopOfStack().TaskgroupReductionRef; 1418 if (!TaskgroupReductionRef) { 1419 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1420 SemaRef.Context.VoidPtrTy, ".task_red."); 1421 TaskgroupReductionRef = 1422 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1423 } 1424 } 1425 1426 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR, 1427 const Expr *ReductionRef) { 1428 D = getCanonicalDecl(D); 1429 assert(!isStackEmpty() && "Data-sharing attributes stack is empty"); 1430 assert( 1431 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction && 1432 "Additional reduction info may be specified only for reduction items."); 1433 ReductionData &ReductionData = getTopOfStack().ReductionMap[D]; 1434 assert(ReductionData.ReductionRange.isInvalid() && 1435 (getTopOfStack().Directive == OMPD_taskgroup || 1436 ((isOpenMPParallelDirective(getTopOfStack().Directive) || 1437 isOpenMPWorksharingDirective(getTopOfStack().Directive)) && 1438 !isOpenMPSimdDirective(getTopOfStack().Directive))) && 1439 "Additional reduction info may be specified only once for reduction " 1440 "items."); 1441 ReductionData.set(ReductionRef, SR); 1442 Expr *&TaskgroupReductionRef = 1443 getTopOfStack().TaskgroupReductionRef; 1444 if (!TaskgroupReductionRef) { 1445 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(), 1446 SemaRef.Context.VoidPtrTy, ".task_red."); 1447 TaskgroupReductionRef = 1448 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin()); 1449 } 1450 } 1451 1452 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1453 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK, 1454 Expr *&TaskgroupDescriptor) const { 1455 D = getCanonicalDecl(D); 1456 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1457 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1458 const DSAInfo &Data = I->SharingMap.lookup(D); 1459 if (Data.Attributes != OMPC_reduction || 1460 Data.Modifier != OMPC_REDUCTION_task) 1461 continue; 1462 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1463 if (!ReductionData.ReductionOp || 1464 ReductionData.ReductionOp.is<const Expr *>()) 1465 return DSAVarData(); 1466 SR = ReductionData.ReductionRange; 1467 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>(); 1468 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1469 "expression for the descriptor is not " 1470 "set."); 1471 TaskgroupDescriptor = I->TaskgroupReductionRef; 1472 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(), 1473 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task); 1474 } 1475 return DSAVarData(); 1476 } 1477 1478 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData( 1479 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef, 1480 Expr *&TaskgroupDescriptor) const { 1481 D = getCanonicalDecl(D); 1482 assert(!isStackEmpty() && "Data-sharing attributes stack is empty."); 1483 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) { 1484 const DSAInfo &Data = I->SharingMap.lookup(D); 1485 if (Data.Attributes != OMPC_reduction || 1486 Data.Modifier != OMPC_REDUCTION_task) 1487 continue; 1488 const ReductionData &ReductionData = I->ReductionMap.lookup(D); 1489 if (!ReductionData.ReductionOp || 1490 !ReductionData.ReductionOp.is<const Expr *>()) 1491 return DSAVarData(); 1492 SR = ReductionData.ReductionRange; 1493 ReductionRef = ReductionData.ReductionOp.get<const Expr *>(); 1494 assert(I->TaskgroupReductionRef && "taskgroup reduction reference " 1495 "expression for the descriptor is not " 1496 "set."); 1497 TaskgroupDescriptor = I->TaskgroupReductionRef; 1498 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(), 1499 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task); 1500 } 1501 return DSAVarData(); 1502 } 1503 1504 bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const { 1505 D = D->getCanonicalDecl(); 1506 for (const_iterator E = end(); I != E; ++I) { 1507 if (isImplicitOrExplicitTaskingRegion(I->Directive) || 1508 isOpenMPTargetExecutionDirective(I->Directive)) { 1509 Scope *TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; 1510 Scope *CurScope = getCurScope(); 1511 while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D)) 1512 CurScope = CurScope->getParent(); 1513 return CurScope != TopScope; 1514 } 1515 } 1516 return false; 1517 } 1518 1519 static bool isConstNotMutableType(Sema &SemaRef, QualType Type, 1520 bool AcceptIfMutable = true, 1521 bool *IsClassType = nullptr) { 1522 ASTContext &Context = SemaRef.getASTContext(); 1523 Type = Type.getNonReferenceType().getCanonicalType(); 1524 bool IsConstant = Type.isConstant(Context); 1525 Type = Context.getBaseElementType(Type); 1526 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus 1527 ? Type->getAsCXXRecordDecl() 1528 : nullptr; 1529 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD)) 1530 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate()) 1531 RD = CTD->getTemplatedDecl(); 1532 if (IsClassType) 1533 *IsClassType = RD; 1534 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD && 1535 RD->hasDefinition() && RD->hasMutableFields()); 1536 } 1537 1538 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D, 1539 QualType Type, OpenMPClauseKind CKind, 1540 SourceLocation ELoc, 1541 bool AcceptIfMutable = true, 1542 bool ListItemNotVar = false) { 1543 ASTContext &Context = SemaRef.getASTContext(); 1544 bool IsClassType; 1545 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) { 1546 unsigned Diag = ListItemNotVar 1547 ? diag::err_omp_const_list_item 1548 : IsClassType ? diag::err_omp_const_not_mutable_variable 1549 : diag::err_omp_const_variable; 1550 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind); 1551 if (!ListItemNotVar && D) { 1552 const VarDecl *VD = dyn_cast<VarDecl>(D); 1553 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 1554 VarDecl::DeclarationOnly; 1555 SemaRef.Diag(D->getLocation(), 1556 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 1557 << D; 1558 } 1559 return true; 1560 } 1561 return false; 1562 } 1563 1564 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, 1565 bool FromParent) { 1566 D = getCanonicalDecl(D); 1567 DSAVarData DVar; 1568 1569 auto *VD = dyn_cast<VarDecl>(D); 1570 auto TI = Threadprivates.find(D); 1571 if (TI != Threadprivates.end()) { 1572 DVar.RefExpr = TI->getSecond().RefExpr.getPointer(); 1573 DVar.CKind = OMPC_threadprivate; 1574 DVar.Modifier = TI->getSecond().Modifier; 1575 return DVar; 1576 } 1577 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) { 1578 DVar.RefExpr = buildDeclRefExpr( 1579 SemaRef, VD, D->getType().getNonReferenceType(), 1580 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation()); 1581 DVar.CKind = OMPC_threadprivate; 1582 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1583 return DVar; 1584 } 1585 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1586 // in a Construct, C/C++, predetermined, p.1] 1587 // Variables appearing in threadprivate directives are threadprivate. 1588 if ((VD && VD->getTLSKind() != VarDecl::TLS_None && 1589 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 1590 SemaRef.getLangOpts().OpenMPUseTLS && 1591 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) || 1592 (VD && VD->getStorageClass() == SC_Register && 1593 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) { 1594 DVar.RefExpr = buildDeclRefExpr( 1595 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation()); 1596 DVar.CKind = OMPC_threadprivate; 1597 addDSA(D, DVar.RefExpr, OMPC_threadprivate); 1598 return DVar; 1599 } 1600 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD && 1601 VD->isLocalVarDeclOrParm() && !isStackEmpty() && 1602 !isLoopControlVariable(D).first) { 1603 const_iterator IterTarget = 1604 std::find_if(begin(), end(), [](const SharingMapTy &Data) { 1605 return isOpenMPTargetExecutionDirective(Data.Directive); 1606 }); 1607 if (IterTarget != end()) { 1608 const_iterator ParentIterTarget = IterTarget + 1; 1609 for (const_iterator Iter = begin(); 1610 Iter != ParentIterTarget; ++Iter) { 1611 if (isOpenMPLocal(VD, Iter)) { 1612 DVar.RefExpr = 1613 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1614 D->getLocation()); 1615 DVar.CKind = OMPC_threadprivate; 1616 return DVar; 1617 } 1618 } 1619 if (!isClauseParsingMode() || IterTarget != begin()) { 1620 auto DSAIter = IterTarget->SharingMap.find(D); 1621 if (DSAIter != IterTarget->SharingMap.end() && 1622 isOpenMPPrivate(DSAIter->getSecond().Attributes)) { 1623 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer(); 1624 DVar.CKind = OMPC_threadprivate; 1625 return DVar; 1626 } 1627 const_iterator End = end(); 1628 if (!SemaRef.isOpenMPCapturedByRef( 1629 D, std::distance(ParentIterTarget, End), 1630 /*OpenMPCaptureLevel=*/0)) { 1631 DVar.RefExpr = 1632 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(), 1633 IterTarget->ConstructLoc); 1634 DVar.CKind = OMPC_threadprivate; 1635 return DVar; 1636 } 1637 } 1638 } 1639 } 1640 1641 if (isStackEmpty()) 1642 // Not in OpenMP execution region and top scope was already checked. 1643 return DVar; 1644 1645 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1646 // in a Construct, C/C++, predetermined, p.4] 1647 // Static data members are shared. 1648 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1649 // in a Construct, C/C++, predetermined, p.7] 1650 // Variables with static storage duration that are declared in a scope 1651 // inside the construct are shared. 1652 if (VD && VD->isStaticDataMember()) { 1653 // Check for explicitly specified attributes. 1654 const_iterator I = begin(); 1655 const_iterator EndI = end(); 1656 if (FromParent && I != EndI) 1657 ++I; 1658 if (I != EndI) { 1659 auto It = I->SharingMap.find(D); 1660 if (It != I->SharingMap.end()) { 1661 const DSAInfo &Data = It->getSecond(); 1662 DVar.RefExpr = Data.RefExpr.getPointer(); 1663 DVar.PrivateCopy = Data.PrivateCopy; 1664 DVar.CKind = Data.Attributes; 1665 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1666 DVar.DKind = I->Directive; 1667 DVar.Modifier = Data.Modifier; 1668 return DVar; 1669 } 1670 } 1671 1672 DVar.CKind = OMPC_shared; 1673 return DVar; 1674 } 1675 1676 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; }; 1677 // The predetermined shared attribute for const-qualified types having no 1678 // mutable members was removed after OpenMP 3.1. 1679 if (SemaRef.LangOpts.OpenMP <= 31) { 1680 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1681 // in a Construct, C/C++, predetermined, p.6] 1682 // Variables with const qualified type having no mutable member are 1683 // shared. 1684 if (isConstNotMutableType(SemaRef, D->getType())) { 1685 // Variables with const-qualified type having no mutable member may be 1686 // listed in a firstprivate clause, even if they are static data members. 1687 DSAVarData DVarTemp = hasInnermostDSA( 1688 D, 1689 [](OpenMPClauseKind C) { 1690 return C == OMPC_firstprivate || C == OMPC_shared; 1691 }, 1692 MatchesAlways, FromParent); 1693 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr) 1694 return DVarTemp; 1695 1696 DVar.CKind = OMPC_shared; 1697 return DVar; 1698 } 1699 } 1700 1701 // Explicitly specified attributes and local variables with predetermined 1702 // attributes. 1703 const_iterator I = begin(); 1704 const_iterator EndI = end(); 1705 if (FromParent && I != EndI) 1706 ++I; 1707 if (I == EndI) 1708 return DVar; 1709 auto It = I->SharingMap.find(D); 1710 if (It != I->SharingMap.end()) { 1711 const DSAInfo &Data = It->getSecond(); 1712 DVar.RefExpr = Data.RefExpr.getPointer(); 1713 DVar.PrivateCopy = Data.PrivateCopy; 1714 DVar.CKind = Data.Attributes; 1715 DVar.ImplicitDSALoc = I->DefaultAttrLoc; 1716 DVar.DKind = I->Directive; 1717 DVar.Modifier = Data.Modifier; 1718 } 1719 1720 return DVar; 1721 } 1722 1723 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1724 bool FromParent) const { 1725 if (isStackEmpty()) { 1726 const_iterator I; 1727 return getDSA(I, D); 1728 } 1729 D = getCanonicalDecl(D); 1730 const_iterator StartI = begin(); 1731 const_iterator EndI = end(); 1732 if (FromParent && StartI != EndI) 1733 ++StartI; 1734 return getDSA(StartI, D); 1735 } 1736 1737 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D, 1738 unsigned Level) const { 1739 if (getStackSize() <= Level) 1740 return DSAVarData(); 1741 D = getCanonicalDecl(D); 1742 const_iterator StartI = std::next(begin(), getStackSize() - 1 - Level); 1743 return getDSA(StartI, D); 1744 } 1745 1746 const DSAStackTy::DSAVarData 1747 DSAStackTy::hasDSA(ValueDecl *D, 1748 const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1749 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1750 bool FromParent) const { 1751 if (isStackEmpty()) 1752 return {}; 1753 D = getCanonicalDecl(D); 1754 const_iterator I = begin(); 1755 const_iterator EndI = end(); 1756 if (FromParent && I != EndI) 1757 ++I; 1758 for (; I != EndI; ++I) { 1759 if (!DPred(I->Directive) && 1760 !isImplicitOrExplicitTaskingRegion(I->Directive)) 1761 continue; 1762 const_iterator NewI = I; 1763 DSAVarData DVar = getDSA(NewI, D); 1764 if (I == NewI && CPred(DVar.CKind)) 1765 return DVar; 1766 } 1767 return {}; 1768 } 1769 1770 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA( 1771 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1772 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1773 bool FromParent) const { 1774 if (isStackEmpty()) 1775 return {}; 1776 D = getCanonicalDecl(D); 1777 const_iterator StartI = begin(); 1778 const_iterator EndI = end(); 1779 if (FromParent && StartI != EndI) 1780 ++StartI; 1781 if (StartI == EndI || !DPred(StartI->Directive)) 1782 return {}; 1783 const_iterator NewI = StartI; 1784 DSAVarData DVar = getDSA(NewI, D); 1785 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData(); 1786 } 1787 1788 bool DSAStackTy::hasExplicitDSA( 1789 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred, 1790 unsigned Level, bool NotLastprivate) const { 1791 if (getStackSize() <= Level) 1792 return false; 1793 D = getCanonicalDecl(D); 1794 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1795 auto I = StackElem.SharingMap.find(D); 1796 if (I != StackElem.SharingMap.end() && 1797 I->getSecond().RefExpr.getPointer() && 1798 CPred(I->getSecond().Attributes) && 1799 (!NotLastprivate || !I->getSecond().RefExpr.getInt())) 1800 return true; 1801 // Check predetermined rules for the loop control variables. 1802 auto LI = StackElem.LCVMap.find(D); 1803 if (LI != StackElem.LCVMap.end()) 1804 return CPred(OMPC_private); 1805 return false; 1806 } 1807 1808 bool DSAStackTy::hasExplicitDirective( 1809 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, 1810 unsigned Level) const { 1811 if (getStackSize() <= Level) 1812 return false; 1813 const SharingMapTy &StackElem = getStackElemAtLevel(Level); 1814 return DPred(StackElem.Directive); 1815 } 1816 1817 bool DSAStackTy::hasDirective( 1818 const llvm::function_ref<bool(OpenMPDirectiveKind, 1819 const DeclarationNameInfo &, SourceLocation)> 1820 DPred, 1821 bool FromParent) const { 1822 // We look only in the enclosing region. 1823 size_t Skip = FromParent ? 2 : 1; 1824 for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end(); 1825 I != E; ++I) { 1826 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc)) 1827 return true; 1828 } 1829 return false; 1830 } 1831 1832 void Sema::InitDataSharingAttributesStack() { 1833 VarDataSharingAttributesStack = new DSAStackTy(*this); 1834 } 1835 1836 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 1837 1838 void Sema::pushOpenMPFunctionRegion() { 1839 DSAStack->pushFunction(); 1840 } 1841 1842 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) { 1843 DSAStack->popFunction(OldFSI); 1844 } 1845 1846 static bool isOpenMPDeviceDelayedContext(Sema &S) { 1847 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice && 1848 "Expected OpenMP device compilation."); 1849 return !S.isInOpenMPTargetExecutionDirective() && 1850 !S.isInOpenMPDeclareTargetContext(); 1851 } 1852 1853 namespace { 1854 /// Status of the function emission on the host/device. 1855 enum class FunctionEmissionStatus { 1856 Emitted, 1857 Discarded, 1858 Unknown, 1859 }; 1860 } // anonymous namespace 1861 1862 Sema::DeviceDiagBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc, 1863 unsigned DiagID) { 1864 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice && 1865 "Expected OpenMP device compilation."); 1866 1867 FunctionDecl *FD = getCurFunctionDecl(); 1868 DeviceDiagBuilder::Kind Kind = DeviceDiagBuilder::K_Nop; 1869 if (FD) { 1870 FunctionEmissionStatus FES = getEmissionStatus(FD); 1871 switch (FES) { 1872 case FunctionEmissionStatus::Emitted: 1873 Kind = DeviceDiagBuilder::K_Immediate; 1874 break; 1875 case FunctionEmissionStatus::Unknown: 1876 Kind = isOpenMPDeviceDelayedContext(*this) 1877 ? DeviceDiagBuilder::K_Deferred 1878 : DeviceDiagBuilder::K_Immediate; 1879 break; 1880 case FunctionEmissionStatus::TemplateDiscarded: 1881 case FunctionEmissionStatus::OMPDiscarded: 1882 Kind = DeviceDiagBuilder::K_Nop; 1883 break; 1884 case FunctionEmissionStatus::CUDADiscarded: 1885 llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation"); 1886 break; 1887 } 1888 } 1889 1890 return DeviceDiagBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this); 1891 } 1892 1893 Sema::DeviceDiagBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc, 1894 unsigned DiagID) { 1895 assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice && 1896 "Expected OpenMP host compilation."); 1897 FunctionEmissionStatus FES = getEmissionStatus(getCurFunctionDecl()); 1898 DeviceDiagBuilder::Kind Kind = DeviceDiagBuilder::K_Nop; 1899 switch (FES) { 1900 case FunctionEmissionStatus::Emitted: 1901 Kind = DeviceDiagBuilder::K_Immediate; 1902 break; 1903 case FunctionEmissionStatus::Unknown: 1904 Kind = DeviceDiagBuilder::K_Deferred; 1905 break; 1906 case FunctionEmissionStatus::TemplateDiscarded: 1907 case FunctionEmissionStatus::OMPDiscarded: 1908 case FunctionEmissionStatus::CUDADiscarded: 1909 Kind = DeviceDiagBuilder::K_Nop; 1910 break; 1911 } 1912 1913 return DeviceDiagBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this); 1914 } 1915 1916 static OpenMPDefaultmapClauseKind 1917 getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) { 1918 if (LO.OpenMP <= 45) { 1919 if (VD->getType().getNonReferenceType()->isScalarType()) 1920 return OMPC_DEFAULTMAP_scalar; 1921 return OMPC_DEFAULTMAP_aggregate; 1922 } 1923 if (VD->getType().getNonReferenceType()->isAnyPointerType()) 1924 return OMPC_DEFAULTMAP_pointer; 1925 if (VD->getType().getNonReferenceType()->isScalarType()) 1926 return OMPC_DEFAULTMAP_scalar; 1927 return OMPC_DEFAULTMAP_aggregate; 1928 } 1929 1930 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level, 1931 unsigned OpenMPCaptureLevel) const { 1932 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 1933 1934 ASTContext &Ctx = getASTContext(); 1935 bool IsByRef = true; 1936 1937 // Find the directive that is associated with the provided scope. 1938 D = cast<ValueDecl>(D->getCanonicalDecl()); 1939 QualType Ty = D->getType(); 1940 1941 bool IsVariableUsedInMapClause = false; 1942 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) { 1943 // This table summarizes how a given variable should be passed to the device 1944 // given its type and the clauses where it appears. This table is based on 1945 // the description in OpenMP 4.5 [2.10.4, target Construct] and 1946 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses]. 1947 // 1948 // ========================================================================= 1949 // | type | defaultmap | pvt | first | is_device_ptr | map | res. | 1950 // | |(tofrom:scalar)| | pvt | | | | 1951 // ========================================================================= 1952 // | scl | | | | - | | bycopy| 1953 // | scl | | - | x | - | - | bycopy| 1954 // | scl | | x | - | - | - | null | 1955 // | scl | x | | | - | | byref | 1956 // | scl | x | - | x | - | - | bycopy| 1957 // | scl | x | x | - | - | - | null | 1958 // | scl | | - | - | - | x | byref | 1959 // | scl | x | - | - | - | x | byref | 1960 // 1961 // | agg | n.a. | | | - | | byref | 1962 // | agg | n.a. | - | x | - | - | byref | 1963 // | agg | n.a. | x | - | - | - | null | 1964 // | agg | n.a. | - | - | - | x | byref | 1965 // | agg | n.a. | - | - | - | x[] | byref | 1966 // 1967 // | ptr | n.a. | | | - | | bycopy| 1968 // | ptr | n.a. | - | x | - | - | bycopy| 1969 // | ptr | n.a. | x | - | - | - | null | 1970 // | ptr | n.a. | - | - | - | x | byref | 1971 // | ptr | n.a. | - | - | - | x[] | bycopy| 1972 // | ptr | n.a. | - | - | x | | bycopy| 1973 // | ptr | n.a. | - | - | x | x | bycopy| 1974 // | ptr | n.a. | - | - | x | x[] | bycopy| 1975 // ========================================================================= 1976 // Legend: 1977 // scl - scalar 1978 // ptr - pointer 1979 // agg - aggregate 1980 // x - applies 1981 // - - invalid in this combination 1982 // [] - mapped with an array section 1983 // byref - should be mapped by reference 1984 // byval - should be mapped by value 1985 // null - initialize a local variable to null on the device 1986 // 1987 // Observations: 1988 // - All scalar declarations that show up in a map clause have to be passed 1989 // by reference, because they may have been mapped in the enclosing data 1990 // environment. 1991 // - If the scalar value does not fit the size of uintptr, it has to be 1992 // passed by reference, regardless the result in the table above. 1993 // - For pointers mapped by value that have either an implicit map or an 1994 // array section, the runtime library may pass the NULL value to the 1995 // device instead of the value passed to it by the compiler. 1996 1997 if (Ty->isReferenceType()) 1998 Ty = Ty->castAs<ReferenceType>()->getPointeeType(); 1999 2000 // Locate map clauses and see if the variable being captured is referred to 2001 // in any of those clauses. Here we only care about variables, not fields, 2002 // because fields are part of aggregates. 2003 bool IsVariableAssociatedWithSection = false; 2004 2005 DSAStack->checkMappableExprComponentListsForDeclAtLevel( 2006 D, Level, 2007 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D]( 2008 OMPClauseMappableExprCommon::MappableExprComponentListRef 2009 MapExprComponents, 2010 OpenMPClauseKind WhereFoundClauseKind) { 2011 // Only the map clause information influences how a variable is 2012 // captured. E.g. is_device_ptr does not require changing the default 2013 // behavior. 2014 if (WhereFoundClauseKind != OMPC_map) 2015 return false; 2016 2017 auto EI = MapExprComponents.rbegin(); 2018 auto EE = MapExprComponents.rend(); 2019 2020 assert(EI != EE && "Invalid map expression!"); 2021 2022 if (isa<DeclRefExpr>(EI->getAssociatedExpression())) 2023 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D; 2024 2025 ++EI; 2026 if (EI == EE) 2027 return false; 2028 2029 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) || 2030 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) || 2031 isa<MemberExpr>(EI->getAssociatedExpression()) || 2032 isa<OMPArrayShapingExpr>(EI->getAssociatedExpression())) { 2033 IsVariableAssociatedWithSection = true; 2034 // There is nothing more we need to know about this variable. 2035 return true; 2036 } 2037 2038 // Keep looking for more map info. 2039 return false; 2040 }); 2041 2042 if (IsVariableUsedInMapClause) { 2043 // If variable is identified in a map clause it is always captured by 2044 // reference except if it is a pointer that is dereferenced somehow. 2045 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection); 2046 } else { 2047 // By default, all the data that has a scalar type is mapped by copy 2048 // (except for reduction variables). 2049 // Defaultmap scalar is mutual exclusive to defaultmap pointer 2050 IsByRef = 2051 (DSAStack->isForceCaptureByReferenceInTargetExecutable() && 2052 !Ty->isAnyPointerType()) || 2053 !Ty->isScalarType() || 2054 DSAStack->isDefaultmapCapturedByRef( 2055 Level, getVariableCategoryFromDecl(LangOpts, D)) || 2056 DSAStack->hasExplicitDSA( 2057 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level); 2058 } 2059 } 2060 2061 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) { 2062 IsByRef = 2063 ((IsVariableUsedInMapClause && 2064 DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) == 2065 OMPD_target) || 2066 !(DSAStack->hasExplicitDSA( 2067 D, 2068 [](OpenMPClauseKind K) -> bool { 2069 return K == OMPC_firstprivate; 2070 }, 2071 Level, /*NotLastprivate=*/true) || 2072 DSAStack->isUsesAllocatorsDecl(Level, D))) && 2073 // If the variable is artificial and must be captured by value - try to 2074 // capture by value. 2075 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() && 2076 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()) && 2077 // If the variable is implicitly firstprivate and scalar - capture by 2078 // copy 2079 !(DSAStack->getDefaultDSA() == DSA_firstprivate && 2080 !DSAStack->hasExplicitDSA( 2081 D, [](OpenMPClauseKind K) { return K != OMPC_unknown; }, Level) && 2082 !DSAStack->isLoopControlVariable(D, Level).first); 2083 } 2084 2085 // When passing data by copy, we need to make sure it fits the uintptr size 2086 // and alignment, because the runtime library only deals with uintptr types. 2087 // If it does not fit the uintptr size, we need to pass the data by reference 2088 // instead. 2089 if (!IsByRef && 2090 (Ctx.getTypeSizeInChars(Ty) > 2091 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) || 2092 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) { 2093 IsByRef = true; 2094 } 2095 2096 return IsByRef; 2097 } 2098 2099 unsigned Sema::getOpenMPNestingLevel() const { 2100 assert(getLangOpts().OpenMP); 2101 return DSAStack->getNestingLevel(); 2102 } 2103 2104 bool Sema::isInOpenMPTargetExecutionDirective() const { 2105 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) && 2106 !DSAStack->isClauseParsingMode()) || 2107 DSAStack->hasDirective( 2108 [](OpenMPDirectiveKind K, const DeclarationNameInfo &, 2109 SourceLocation) -> bool { 2110 return isOpenMPTargetExecutionDirective(K); 2111 }, 2112 false); 2113 } 2114 2115 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo, 2116 unsigned StopAt) { 2117 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2118 D = getCanonicalDecl(D); 2119 2120 auto *VD = dyn_cast<VarDecl>(D); 2121 // Do not capture constexpr variables. 2122 if (VD && VD->isConstexpr()) 2123 return nullptr; 2124 2125 // If we want to determine whether the variable should be captured from the 2126 // perspective of the current capturing scope, and we've already left all the 2127 // capturing scopes of the top directive on the stack, check from the 2128 // perspective of its parent directive (if any) instead. 2129 DSAStackTy::ParentDirectiveScope InParentDirectiveRAII( 2130 *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete()); 2131 2132 // If we are attempting to capture a global variable in a directive with 2133 // 'target' we return true so that this global is also mapped to the device. 2134 // 2135 if (VD && !VD->hasLocalStorage() && 2136 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) { 2137 if (isInOpenMPDeclareTargetContext()) { 2138 // Try to mark variable as declare target if it is used in capturing 2139 // regions. 2140 if (LangOpts.OpenMP <= 45 && 2141 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2142 checkDeclIsAllowedInOpenMPTarget(nullptr, VD); 2143 return nullptr; 2144 } else if (isInOpenMPTargetExecutionDirective()) { 2145 // If the declaration is enclosed in a 'declare target' directive, 2146 // then it should not be captured. 2147 // 2148 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) 2149 return nullptr; 2150 CapturedRegionScopeInfo *CSI = nullptr; 2151 for (FunctionScopeInfo *FSI : llvm::drop_begin( 2152 llvm::reverse(FunctionScopes), 2153 CheckScopeInfo ? (FunctionScopes.size() - (StopAt + 1)) : 0)) { 2154 if (!isa<CapturingScopeInfo>(FSI)) 2155 return nullptr; 2156 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2157 if (RSI->CapRegionKind == CR_OpenMP) { 2158 CSI = RSI; 2159 break; 2160 } 2161 } 2162 SmallVector<OpenMPDirectiveKind, 4> Regions; 2163 getOpenMPCaptureRegions(Regions, 2164 DSAStack->getDirective(CSI->OpenMPLevel)); 2165 if (Regions[CSI->OpenMPCaptureLevel] != OMPD_task) 2166 return VD; 2167 } 2168 } 2169 2170 if (CheckScopeInfo) { 2171 bool OpenMPFound = false; 2172 for (unsigned I = StopAt + 1; I > 0; --I) { 2173 FunctionScopeInfo *FSI = FunctionScopes[I - 1]; 2174 if(!isa<CapturingScopeInfo>(FSI)) 2175 return nullptr; 2176 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI)) 2177 if (RSI->CapRegionKind == CR_OpenMP) { 2178 OpenMPFound = true; 2179 break; 2180 } 2181 } 2182 if (!OpenMPFound) 2183 return nullptr; 2184 } 2185 2186 if (DSAStack->getCurrentDirective() != OMPD_unknown && 2187 (!DSAStack->isClauseParsingMode() || 2188 DSAStack->getParentDirective() != OMPD_unknown)) { 2189 auto &&Info = DSAStack->isLoopControlVariable(D); 2190 if (Info.first || 2191 (VD && VD->hasLocalStorage() && 2192 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) || 2193 (VD && DSAStack->isForceVarCapturing())) 2194 return VD ? VD : Info.second; 2195 DSAStackTy::DSAVarData DVarTop = 2196 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode()); 2197 if (DVarTop.CKind != OMPC_unknown && isOpenMPPrivate(DVarTop.CKind)) 2198 return VD ? VD : cast<VarDecl>(DVarTop.PrivateCopy->getDecl()); 2199 // Threadprivate variables must not be captured. 2200 if (isOpenMPThreadPrivate(DVarTop.CKind)) 2201 return nullptr; 2202 // The variable is not private or it is the variable in the directive with 2203 // default(none) clause and not used in any clause. 2204 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA( 2205 D, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; }, 2206 DSAStack->isClauseParsingMode()); 2207 // Global shared must not be captured. 2208 if (VD && !VD->hasLocalStorage() && DVarPrivate.CKind == OMPC_unknown && 2209 ((DSAStack->getDefaultDSA() != DSA_none && 2210 DSAStack->getDefaultDSA() != DSA_firstprivate) || 2211 DVarTop.CKind == OMPC_shared)) 2212 return nullptr; 2213 if (DVarPrivate.CKind != OMPC_unknown || 2214 (VD && (DSAStack->getDefaultDSA() == DSA_none || 2215 DSAStack->getDefaultDSA() == DSA_firstprivate))) 2216 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl()); 2217 } 2218 return nullptr; 2219 } 2220 2221 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex, 2222 unsigned Level) const { 2223 FunctionScopesIndex -= getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2224 } 2225 2226 void Sema::startOpenMPLoop() { 2227 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2228 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) 2229 DSAStack->loopInit(); 2230 } 2231 2232 void Sema::startOpenMPCXXRangeFor() { 2233 assert(LangOpts.OpenMP && "OpenMP must be enabled."); 2234 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2235 DSAStack->resetPossibleLoopCounter(); 2236 DSAStack->loopStart(); 2237 } 2238 } 2239 2240 OpenMPClauseKind Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level, 2241 unsigned CapLevel) const { 2242 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2243 if (DSAStack->hasExplicitDirective( 2244 [](OpenMPDirectiveKind K) { return isOpenMPTaskingDirective(K); }, 2245 Level)) { 2246 bool IsTriviallyCopyable = 2247 D->getType().getNonReferenceType().isTriviallyCopyableType(Context); 2248 OpenMPDirectiveKind DKind = DSAStack->getDirective(Level); 2249 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 2250 getOpenMPCaptureRegions(CaptureRegions, DKind); 2251 if (isOpenMPTaskingDirective(CaptureRegions[CapLevel]) && 2252 (IsTriviallyCopyable || 2253 !isOpenMPTaskLoopDirective(CaptureRegions[CapLevel]))) { 2254 if (DSAStack->hasExplicitDSA( 2255 D, [](OpenMPClauseKind K) { return K == OMPC_firstprivate; }, 2256 Level, /*NotLastprivate=*/true)) 2257 return OMPC_firstprivate; 2258 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level); 2259 if (DVar.CKind != OMPC_shared && 2260 !DSAStack->isLoopControlVariable(D, Level).first && !DVar.RefExpr) { 2261 DSAStack->addImplicitTaskFirstprivate(Level, D); 2262 return OMPC_firstprivate; 2263 } 2264 } 2265 } 2266 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 2267 if (DSAStack->getAssociatedLoops() > 0 && 2268 !DSAStack->isLoopStarted()) { 2269 DSAStack->resetPossibleLoopCounter(D); 2270 DSAStack->loopStart(); 2271 return OMPC_private; 2272 } 2273 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() || 2274 DSAStack->isLoopControlVariable(D).first) && 2275 !DSAStack->hasExplicitDSA( 2276 D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) && 2277 !isOpenMPSimdDirective(DSAStack->getCurrentDirective())) 2278 return OMPC_private; 2279 } 2280 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2281 if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) && 2282 DSAStack->isForceVarCapturing() && 2283 !DSAStack->hasExplicitDSA( 2284 D, [](OpenMPClauseKind K) { return K == OMPC_copyin; }, Level)) 2285 return OMPC_private; 2286 } 2287 // User-defined allocators are private since they must be defined in the 2288 // context of target region. 2289 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level) && 2290 DSAStack->isUsesAllocatorsDecl(Level, D).getValueOr( 2291 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 2292 DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator) 2293 return OMPC_private; 2294 return (DSAStack->hasExplicitDSA( 2295 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) || 2296 (DSAStack->isClauseParsingMode() && 2297 DSAStack->getClauseParsingMode() == OMPC_private) || 2298 // Consider taskgroup reduction descriptor variable a private 2299 // to avoid possible capture in the region. 2300 (DSAStack->hasExplicitDirective( 2301 [](OpenMPDirectiveKind K) { 2302 return K == OMPD_taskgroup || 2303 ((isOpenMPParallelDirective(K) || 2304 isOpenMPWorksharingDirective(K)) && 2305 !isOpenMPSimdDirective(K)); 2306 }, 2307 Level) && 2308 DSAStack->isTaskgroupReductionRef(D, Level))) 2309 ? OMPC_private 2310 : OMPC_unknown; 2311 } 2312 2313 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D, 2314 unsigned Level) { 2315 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2316 D = getCanonicalDecl(D); 2317 OpenMPClauseKind OMPC = OMPC_unknown; 2318 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) { 2319 const unsigned NewLevel = I - 1; 2320 if (DSAStack->hasExplicitDSA(D, 2321 [&OMPC](const OpenMPClauseKind K) { 2322 if (isOpenMPPrivate(K)) { 2323 OMPC = K; 2324 return true; 2325 } 2326 return false; 2327 }, 2328 NewLevel)) 2329 break; 2330 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel( 2331 D, NewLevel, 2332 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 2333 OpenMPClauseKind) { return true; })) { 2334 OMPC = OMPC_map; 2335 break; 2336 } 2337 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2338 NewLevel)) { 2339 OMPC = OMPC_map; 2340 if (DSAStack->mustBeFirstprivateAtLevel( 2341 NewLevel, getVariableCategoryFromDecl(LangOpts, D))) 2342 OMPC = OMPC_firstprivate; 2343 break; 2344 } 2345 } 2346 if (OMPC != OMPC_unknown) 2347 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, unsigned(OMPC))); 2348 } 2349 2350 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, unsigned Level, 2351 unsigned CaptureLevel) const { 2352 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2353 // Return true if the current level is no longer enclosed in a target region. 2354 2355 SmallVector<OpenMPDirectiveKind, 4> Regions; 2356 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level)); 2357 const auto *VD = dyn_cast<VarDecl>(D); 2358 return VD && !VD->hasLocalStorage() && 2359 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, 2360 Level) && 2361 Regions[CaptureLevel] != OMPD_task; 2362 } 2363 2364 bool Sema::isOpenMPGlobalCapturedDecl(ValueDecl *D, unsigned Level, 2365 unsigned CaptureLevel) const { 2366 assert(LangOpts.OpenMP && "OpenMP is not allowed"); 2367 // Return true if the current level is no longer enclosed in a target region. 2368 2369 if (const auto *VD = dyn_cast<VarDecl>(D)) { 2370 if (!VD->hasLocalStorage()) { 2371 DSAStackTy::DSAVarData TopDVar = 2372 DSAStack->getTopDSA(D, /*FromParent=*/false); 2373 unsigned NumLevels = 2374 getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 2375 if (Level == 0) 2376 return (NumLevels == CaptureLevel + 1) && TopDVar.CKind != OMPC_shared; 2377 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level - 1); 2378 return DVar.CKind != OMPC_shared || 2379 isOpenMPGlobalCapturedDecl( 2380 D, Level - 1, 2381 getOpenMPCaptureLevels(DSAStack->getDirective(Level - 1)) - 1); 2382 } 2383 } 2384 return true; 2385 } 2386 2387 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 2388 2389 void Sema::ActOnOpenMPBeginDeclareVariant(SourceLocation Loc, 2390 OMPTraitInfo &TI) { 2391 if (!OMPDeclareVariantScopes.empty()) { 2392 Diag(Loc, diag::warn_nested_declare_variant); 2393 return; 2394 } 2395 OMPDeclareVariantScopes.push_back(OMPDeclareVariantScope(TI)); 2396 } 2397 2398 void Sema::ActOnOpenMPEndDeclareVariant() { 2399 assert(isInOpenMPDeclareVariantScope() && 2400 "Not in OpenMP declare variant scope!"); 2401 2402 OMPDeclareVariantScopes.pop_back(); 2403 } 2404 2405 void Sema::finalizeOpenMPDelayedAnalysis(const FunctionDecl *Caller, 2406 const FunctionDecl *Callee, 2407 SourceLocation Loc) { 2408 assert(LangOpts.OpenMP && "Expected OpenMP compilation mode."); 2409 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = 2410 OMPDeclareTargetDeclAttr::getDeviceType(Caller->getMostRecentDecl()); 2411 // Ignore host functions during device analyzis. 2412 if (LangOpts.OpenMPIsDevice && DevTy && 2413 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) 2414 return; 2415 // Ignore nohost functions during host analyzis. 2416 if (!LangOpts.OpenMPIsDevice && DevTy && 2417 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) 2418 return; 2419 const FunctionDecl *FD = Callee->getMostRecentDecl(); 2420 DevTy = OMPDeclareTargetDeclAttr::getDeviceType(FD); 2421 if (LangOpts.OpenMPIsDevice && DevTy && 2422 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) { 2423 // Diagnose host function called during device codegen. 2424 StringRef HostDevTy = 2425 getOpenMPSimpleClauseTypeName(OMPC_device_type, OMPC_DEVICE_TYPE_host); 2426 Diag(Loc, diag::err_omp_wrong_device_function_call) << HostDevTy << 0; 2427 Diag(FD->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(), 2428 diag::note_omp_marked_device_type_here) 2429 << HostDevTy; 2430 return; 2431 } 2432 if (!LangOpts.OpenMPIsDevice && DevTy && 2433 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) { 2434 // Diagnose nohost function called during host codegen. 2435 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName( 2436 OMPC_device_type, OMPC_DEVICE_TYPE_nohost); 2437 Diag(Loc, diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1; 2438 Diag(FD->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(), 2439 diag::note_omp_marked_device_type_here) 2440 << NoHostDevTy; 2441 } 2442 } 2443 2444 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 2445 const DeclarationNameInfo &DirName, 2446 Scope *CurScope, SourceLocation Loc) { 2447 DSAStack->push(DKind, DirName, CurScope, Loc); 2448 PushExpressionEvaluationContext( 2449 ExpressionEvaluationContext::PotentiallyEvaluated); 2450 } 2451 2452 void Sema::StartOpenMPClause(OpenMPClauseKind K) { 2453 DSAStack->setClauseParsingMode(K); 2454 } 2455 2456 void Sema::EndOpenMPClause() { 2457 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown); 2458 } 2459 2460 static std::pair<ValueDecl *, bool> 2461 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc, 2462 SourceRange &ERange, bool AllowArraySection = false); 2463 2464 /// Check consistency of the reduction clauses. 2465 static void checkReductionClauses(Sema &S, DSAStackTy *Stack, 2466 ArrayRef<OMPClause *> Clauses) { 2467 bool InscanFound = false; 2468 SourceLocation InscanLoc; 2469 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions. 2470 // A reduction clause without the inscan reduction-modifier may not appear on 2471 // a construct on which a reduction clause with the inscan reduction-modifier 2472 // appears. 2473 for (OMPClause *C : Clauses) { 2474 if (C->getClauseKind() != OMPC_reduction) 2475 continue; 2476 auto *RC = cast<OMPReductionClause>(C); 2477 if (RC->getModifier() == OMPC_REDUCTION_inscan) { 2478 InscanFound = true; 2479 InscanLoc = RC->getModifierLoc(); 2480 continue; 2481 } 2482 if (RC->getModifier() == OMPC_REDUCTION_task) { 2483 // OpenMP 5.0, 2.19.5.4 reduction Clause. 2484 // A reduction clause with the task reduction-modifier may only appear on 2485 // a parallel construct, a worksharing construct or a combined or 2486 // composite construct for which any of the aforementioned constructs is a 2487 // constituent construct and simd or loop are not constituent constructs. 2488 OpenMPDirectiveKind CurDir = Stack->getCurrentDirective(); 2489 if (!(isOpenMPParallelDirective(CurDir) || 2490 isOpenMPWorksharingDirective(CurDir)) || 2491 isOpenMPSimdDirective(CurDir)) 2492 S.Diag(RC->getModifierLoc(), 2493 diag::err_omp_reduction_task_not_parallel_or_worksharing); 2494 continue; 2495 } 2496 } 2497 if (InscanFound) { 2498 for (OMPClause *C : Clauses) { 2499 if (C->getClauseKind() != OMPC_reduction) 2500 continue; 2501 auto *RC = cast<OMPReductionClause>(C); 2502 if (RC->getModifier() != OMPC_REDUCTION_inscan) { 2503 S.Diag(RC->getModifier() == OMPC_REDUCTION_unknown 2504 ? RC->getBeginLoc() 2505 : RC->getModifierLoc(), 2506 diag::err_omp_inscan_reduction_expected); 2507 S.Diag(InscanLoc, diag::note_omp_previous_inscan_reduction); 2508 continue; 2509 } 2510 for (Expr *Ref : RC->varlists()) { 2511 assert(Ref && "NULL expr in OpenMP nontemporal clause."); 2512 SourceLocation ELoc; 2513 SourceRange ERange; 2514 Expr *SimpleRefExpr = Ref; 2515 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 2516 /*AllowArraySection=*/true); 2517 ValueDecl *D = Res.first; 2518 if (!D) 2519 continue; 2520 if (!Stack->isUsedInScanDirective(getCanonicalDecl(D))) { 2521 S.Diag(Ref->getExprLoc(), 2522 diag::err_omp_reduction_not_inclusive_exclusive) 2523 << Ref->getSourceRange(); 2524 } 2525 } 2526 } 2527 } 2528 } 2529 2530 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 2531 ArrayRef<OMPClause *> Clauses); 2532 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 2533 bool WithInit); 2534 2535 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 2536 const ValueDecl *D, 2537 const DSAStackTy::DSAVarData &DVar, 2538 bool IsLoopIterVar = false); 2539 2540 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 2541 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 2542 // A variable of class type (or array thereof) that appears in a lastprivate 2543 // clause requires an accessible, unambiguous default constructor for the 2544 // class type, unless the list item is also specified in a firstprivate 2545 // clause. 2546 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 2547 for (OMPClause *C : D->clauses()) { 2548 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) { 2549 SmallVector<Expr *, 8> PrivateCopies; 2550 for (Expr *DE : Clause->varlists()) { 2551 if (DE->isValueDependent() || DE->isTypeDependent()) { 2552 PrivateCopies.push_back(nullptr); 2553 continue; 2554 } 2555 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens()); 2556 auto *VD = cast<VarDecl>(DRE->getDecl()); 2557 QualType Type = VD->getType().getNonReferenceType(); 2558 const DSAStackTy::DSAVarData DVar = 2559 DSAStack->getTopDSA(VD, /*FromParent=*/false); 2560 if (DVar.CKind == OMPC_lastprivate) { 2561 // Generate helper private variable and initialize it with the 2562 // default value. The address of the original variable is replaced 2563 // by the address of the new private variable in CodeGen. This new 2564 // variable is not added to IdResolver, so the code in the OpenMP 2565 // region uses original variable for proper diagnostics. 2566 VarDecl *VDPrivate = buildVarDecl( 2567 *this, DE->getExprLoc(), Type.getUnqualifiedType(), 2568 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE); 2569 ActOnUninitializedDecl(VDPrivate); 2570 if (VDPrivate->isInvalidDecl()) { 2571 PrivateCopies.push_back(nullptr); 2572 continue; 2573 } 2574 PrivateCopies.push_back(buildDeclRefExpr( 2575 *this, VDPrivate, DE->getType(), DE->getExprLoc())); 2576 } else { 2577 // The variable is also a firstprivate, so initialization sequence 2578 // for private copy is generated already. 2579 PrivateCopies.push_back(nullptr); 2580 } 2581 } 2582 Clause->setPrivateCopies(PrivateCopies); 2583 continue; 2584 } 2585 // Finalize nontemporal clause by handling private copies, if any. 2586 if (auto *Clause = dyn_cast<OMPNontemporalClause>(C)) { 2587 SmallVector<Expr *, 8> PrivateRefs; 2588 for (Expr *RefExpr : Clause->varlists()) { 2589 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 2590 SourceLocation ELoc; 2591 SourceRange ERange; 2592 Expr *SimpleRefExpr = RefExpr; 2593 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 2594 if (Res.second) 2595 // It will be analyzed later. 2596 PrivateRefs.push_back(RefExpr); 2597 ValueDecl *D = Res.first; 2598 if (!D) 2599 continue; 2600 2601 const DSAStackTy::DSAVarData DVar = 2602 DSAStack->getTopDSA(D, /*FromParent=*/false); 2603 PrivateRefs.push_back(DVar.PrivateCopy ? DVar.PrivateCopy 2604 : SimpleRefExpr); 2605 } 2606 Clause->setPrivateRefs(PrivateRefs); 2607 continue; 2608 } 2609 if (auto *Clause = dyn_cast<OMPUsesAllocatorsClause>(C)) { 2610 for (unsigned I = 0, E = Clause->getNumberOfAllocators(); I < E; ++I) { 2611 OMPUsesAllocatorsClause::Data D = Clause->getAllocatorData(I); 2612 auto *DRE = dyn_cast<DeclRefExpr>(D.Allocator->IgnoreParenImpCasts()); 2613 if (!DRE) 2614 continue; 2615 ValueDecl *VD = DRE->getDecl(); 2616 if (!VD || !isa<VarDecl>(VD)) 2617 continue; 2618 DSAStackTy::DSAVarData DVar = 2619 DSAStack->getTopDSA(VD, /*FromParent=*/false); 2620 // OpenMP [2.12.5, target Construct] 2621 // Memory allocators that appear in a uses_allocators clause cannot 2622 // appear in other data-sharing attribute clauses or data-mapping 2623 // attribute clauses in the same construct. 2624 Expr *MapExpr = nullptr; 2625 if (DVar.RefExpr || 2626 DSAStack->checkMappableExprComponentListsForDecl( 2627 VD, /*CurrentRegionOnly=*/true, 2628 [VD, &MapExpr]( 2629 OMPClauseMappableExprCommon::MappableExprComponentListRef 2630 MapExprComponents, 2631 OpenMPClauseKind C) { 2632 auto MI = MapExprComponents.rbegin(); 2633 auto ME = MapExprComponents.rend(); 2634 if (MI != ME && 2635 MI->getAssociatedDeclaration()->getCanonicalDecl() == 2636 VD->getCanonicalDecl()) { 2637 MapExpr = MI->getAssociatedExpression(); 2638 return true; 2639 } 2640 return false; 2641 })) { 2642 Diag(D.Allocator->getExprLoc(), 2643 diag::err_omp_allocator_used_in_clauses) 2644 << D.Allocator->getSourceRange(); 2645 if (DVar.RefExpr) 2646 reportOriginalDsa(*this, DSAStack, VD, DVar); 2647 else 2648 Diag(MapExpr->getExprLoc(), diag::note_used_here) 2649 << MapExpr->getSourceRange(); 2650 } 2651 } 2652 continue; 2653 } 2654 } 2655 // Check allocate clauses. 2656 if (!CurContext->isDependentContext()) 2657 checkAllocateClauses(*this, DSAStack, D->clauses()); 2658 checkReductionClauses(*this, DSAStack, D->clauses()); 2659 } 2660 2661 DSAStack->pop(); 2662 DiscardCleanupsInEvaluationContext(); 2663 PopExpressionEvaluationContext(); 2664 } 2665 2666 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 2667 Expr *NumIterations, Sema &SemaRef, 2668 Scope *S, DSAStackTy *Stack); 2669 2670 namespace { 2671 2672 class VarDeclFilterCCC final : public CorrectionCandidateCallback { 2673 private: 2674 Sema &SemaRef; 2675 2676 public: 2677 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 2678 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2679 NamedDecl *ND = Candidate.getCorrectionDecl(); 2680 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) { 2681 return VD->hasGlobalStorage() && 2682 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2683 SemaRef.getCurScope()); 2684 } 2685 return false; 2686 } 2687 2688 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2689 return std::make_unique<VarDeclFilterCCC>(*this); 2690 } 2691 2692 }; 2693 2694 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback { 2695 private: 2696 Sema &SemaRef; 2697 2698 public: 2699 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {} 2700 bool ValidateCandidate(const TypoCorrection &Candidate) override { 2701 NamedDecl *ND = Candidate.getCorrectionDecl(); 2702 if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) || 2703 isa<FunctionDecl>(ND))) { 2704 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 2705 SemaRef.getCurScope()); 2706 } 2707 return false; 2708 } 2709 2710 std::unique_ptr<CorrectionCandidateCallback> clone() override { 2711 return std::make_unique<VarOrFuncDeclFilterCCC>(*this); 2712 } 2713 }; 2714 2715 } // namespace 2716 2717 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 2718 CXXScopeSpec &ScopeSpec, 2719 const DeclarationNameInfo &Id, 2720 OpenMPDirectiveKind Kind) { 2721 LookupResult Lookup(*this, Id, LookupOrdinaryName); 2722 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 2723 2724 if (Lookup.isAmbiguous()) 2725 return ExprError(); 2726 2727 VarDecl *VD; 2728 if (!Lookup.isSingleResult()) { 2729 VarDeclFilterCCC CCC(*this); 2730 if (TypoCorrection Corrected = 2731 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 2732 CTK_ErrorRecovery)) { 2733 diagnoseTypo(Corrected, 2734 PDiag(Lookup.empty() 2735 ? diag::err_undeclared_var_use_suggest 2736 : diag::err_omp_expected_var_arg_suggest) 2737 << Id.getName()); 2738 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 2739 } else { 2740 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 2741 : diag::err_omp_expected_var_arg) 2742 << Id.getName(); 2743 return ExprError(); 2744 } 2745 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) { 2746 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 2747 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 2748 return ExprError(); 2749 } 2750 Lookup.suppressDiagnostics(); 2751 2752 // OpenMP [2.9.2, Syntax, C/C++] 2753 // Variables must be file-scope, namespace-scope, or static block-scope. 2754 if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) { 2755 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 2756 << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal(); 2757 bool IsDecl = 2758 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2759 Diag(VD->getLocation(), 2760 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2761 << VD; 2762 return ExprError(); 2763 } 2764 2765 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 2766 NamedDecl *ND = CanonicalVD; 2767 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 2768 // A threadprivate directive for file-scope variables must appear outside 2769 // any definition or declaration. 2770 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 2771 !getCurLexicalContext()->isTranslationUnit()) { 2772 Diag(Id.getLoc(), diag::err_omp_var_scope) 2773 << getOpenMPDirectiveName(Kind) << VD; 2774 bool IsDecl = 2775 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2776 Diag(VD->getLocation(), 2777 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2778 << VD; 2779 return ExprError(); 2780 } 2781 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 2782 // A threadprivate directive for static class member variables must appear 2783 // in the class definition, in the same scope in which the member 2784 // variables are declared. 2785 if (CanonicalVD->isStaticDataMember() && 2786 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 2787 Diag(Id.getLoc(), diag::err_omp_var_scope) 2788 << getOpenMPDirectiveName(Kind) << VD; 2789 bool IsDecl = 2790 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2791 Diag(VD->getLocation(), 2792 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2793 << VD; 2794 return ExprError(); 2795 } 2796 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 2797 // A threadprivate directive for namespace-scope variables must appear 2798 // outside any definition or declaration other than the namespace 2799 // definition itself. 2800 if (CanonicalVD->getDeclContext()->isNamespace() && 2801 (!getCurLexicalContext()->isFileContext() || 2802 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 2803 Diag(Id.getLoc(), diag::err_omp_var_scope) 2804 << getOpenMPDirectiveName(Kind) << VD; 2805 bool IsDecl = 2806 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2807 Diag(VD->getLocation(), 2808 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2809 << VD; 2810 return ExprError(); 2811 } 2812 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 2813 // A threadprivate directive for static block-scope variables must appear 2814 // in the scope of the variable and not in a nested scope. 2815 if (CanonicalVD->isLocalVarDecl() && CurScope && 2816 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 2817 Diag(Id.getLoc(), diag::err_omp_var_scope) 2818 << getOpenMPDirectiveName(Kind) << VD; 2819 bool IsDecl = 2820 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2821 Diag(VD->getLocation(), 2822 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2823 << VD; 2824 return ExprError(); 2825 } 2826 2827 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 2828 // A threadprivate directive must lexically precede all references to any 2829 // of the variables in its list. 2830 if (Kind == OMPD_threadprivate && VD->isUsed() && 2831 !DSAStack->isThreadPrivate(VD)) { 2832 Diag(Id.getLoc(), diag::err_omp_var_used) 2833 << getOpenMPDirectiveName(Kind) << VD; 2834 return ExprError(); 2835 } 2836 2837 QualType ExprType = VD->getType().getNonReferenceType(); 2838 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), 2839 SourceLocation(), VD, 2840 /*RefersToEnclosingVariableOrCapture=*/false, 2841 Id.getLoc(), ExprType, VK_LValue); 2842 } 2843 2844 Sema::DeclGroupPtrTy 2845 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 2846 ArrayRef<Expr *> VarList) { 2847 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 2848 CurContext->addDecl(D); 2849 return DeclGroupPtrTy::make(DeclGroupRef(D)); 2850 } 2851 return nullptr; 2852 } 2853 2854 namespace { 2855 class LocalVarRefChecker final 2856 : public ConstStmtVisitor<LocalVarRefChecker, bool> { 2857 Sema &SemaRef; 2858 2859 public: 2860 bool VisitDeclRefExpr(const DeclRefExpr *E) { 2861 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 2862 if (VD->hasLocalStorage()) { 2863 SemaRef.Diag(E->getBeginLoc(), 2864 diag::err_omp_local_var_in_threadprivate_init) 2865 << E->getSourceRange(); 2866 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 2867 << VD << VD->getSourceRange(); 2868 return true; 2869 } 2870 } 2871 return false; 2872 } 2873 bool VisitStmt(const Stmt *S) { 2874 for (const Stmt *Child : S->children()) { 2875 if (Child && Visit(Child)) 2876 return true; 2877 } 2878 return false; 2879 } 2880 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 2881 }; 2882 } // namespace 2883 2884 OMPThreadPrivateDecl * 2885 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 2886 SmallVector<Expr *, 8> Vars; 2887 for (Expr *RefExpr : VarList) { 2888 auto *DE = cast<DeclRefExpr>(RefExpr); 2889 auto *VD = cast<VarDecl>(DE->getDecl()); 2890 SourceLocation ILoc = DE->getExprLoc(); 2891 2892 // Mark variable as used. 2893 VD->setReferenced(); 2894 VD->markUsed(Context); 2895 2896 QualType QType = VD->getType(); 2897 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 2898 // It will be analyzed later. 2899 Vars.push_back(DE); 2900 continue; 2901 } 2902 2903 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2904 // A threadprivate variable must not have an incomplete type. 2905 if (RequireCompleteType(ILoc, VD->getType(), 2906 diag::err_omp_threadprivate_incomplete_type)) { 2907 continue; 2908 } 2909 2910 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 2911 // A threadprivate variable must not have a reference type. 2912 if (VD->getType()->isReferenceType()) { 2913 Diag(ILoc, diag::err_omp_ref_type_arg) 2914 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 2915 bool IsDecl = 2916 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2917 Diag(VD->getLocation(), 2918 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2919 << VD; 2920 continue; 2921 } 2922 2923 // Check if this is a TLS variable. If TLS is not being supported, produce 2924 // the corresponding diagnostic. 2925 if ((VD->getTLSKind() != VarDecl::TLS_None && 2926 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() && 2927 getLangOpts().OpenMPUseTLS && 2928 getASTContext().getTargetInfo().isTLSSupported())) || 2929 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 2930 !VD->isLocalVarDecl())) { 2931 Diag(ILoc, diag::err_omp_var_thread_local) 2932 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1); 2933 bool IsDecl = 2934 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2935 Diag(VD->getLocation(), 2936 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2937 << VD; 2938 continue; 2939 } 2940 2941 // Check if initial value of threadprivate variable reference variable with 2942 // local storage (it is not supported by runtime). 2943 if (const Expr *Init = VD->getAnyInitializer()) { 2944 LocalVarRefChecker Checker(*this); 2945 if (Checker.Visit(Init)) 2946 continue; 2947 } 2948 2949 Vars.push_back(RefExpr); 2950 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 2951 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit( 2952 Context, SourceRange(Loc, Loc))); 2953 if (ASTMutationListener *ML = Context.getASTMutationListener()) 2954 ML->DeclarationMarkedOpenMPThreadPrivate(VD); 2955 } 2956 OMPThreadPrivateDecl *D = nullptr; 2957 if (!Vars.empty()) { 2958 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 2959 Vars); 2960 D->setAccess(AS_public); 2961 } 2962 return D; 2963 } 2964 2965 static OMPAllocateDeclAttr::AllocatorTypeTy 2966 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) { 2967 if (!Allocator) 2968 return OMPAllocateDeclAttr::OMPNullMemAlloc; 2969 if (Allocator->isTypeDependent() || Allocator->isValueDependent() || 2970 Allocator->isInstantiationDependent() || 2971 Allocator->containsUnexpandedParameterPack()) 2972 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 2973 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 2974 const Expr *AE = Allocator->IgnoreParenImpCasts(); 2975 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 2976 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 2977 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind); 2978 llvm::FoldingSetNodeID AEId, DAEId; 2979 AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true); 2980 DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true); 2981 if (AEId == DAEId) { 2982 AllocatorKindRes = AllocatorKind; 2983 break; 2984 } 2985 } 2986 return AllocatorKindRes; 2987 } 2988 2989 static bool checkPreviousOMPAllocateAttribute( 2990 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD, 2991 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) { 2992 if (!VD->hasAttr<OMPAllocateDeclAttr>()) 2993 return false; 2994 const auto *A = VD->getAttr<OMPAllocateDeclAttr>(); 2995 Expr *PrevAllocator = A->getAllocator(); 2996 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind = 2997 getAllocatorKind(S, Stack, PrevAllocator); 2998 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind; 2999 if (AllocatorsMatch && 3000 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc && 3001 Allocator && PrevAllocator) { 3002 const Expr *AE = Allocator->IgnoreParenImpCasts(); 3003 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts(); 3004 llvm::FoldingSetNodeID AEId, PAEId; 3005 AE->Profile(AEId, S.Context, /*Canonical=*/true); 3006 PAE->Profile(PAEId, S.Context, /*Canonical=*/true); 3007 AllocatorsMatch = AEId == PAEId; 3008 } 3009 if (!AllocatorsMatch) { 3010 SmallString<256> AllocatorBuffer; 3011 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer); 3012 if (Allocator) 3013 Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy()); 3014 SmallString<256> PrevAllocatorBuffer; 3015 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer); 3016 if (PrevAllocator) 3017 PrevAllocator->printPretty(PrevAllocatorStream, nullptr, 3018 S.getPrintingPolicy()); 3019 3020 SourceLocation AllocatorLoc = 3021 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc(); 3022 SourceRange AllocatorRange = 3023 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange(); 3024 SourceLocation PrevAllocatorLoc = 3025 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation(); 3026 SourceRange PrevAllocatorRange = 3027 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange(); 3028 S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator) 3029 << (Allocator ? 1 : 0) << AllocatorStream.str() 3030 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str() 3031 << AllocatorRange; 3032 S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator) 3033 << PrevAllocatorRange; 3034 return true; 3035 } 3036 return false; 3037 } 3038 3039 static void 3040 applyOMPAllocateAttribute(Sema &S, VarDecl *VD, 3041 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, 3042 Expr *Allocator, SourceRange SR) { 3043 if (VD->hasAttr<OMPAllocateDeclAttr>()) 3044 return; 3045 if (Allocator && 3046 (Allocator->isTypeDependent() || Allocator->isValueDependent() || 3047 Allocator->isInstantiationDependent() || 3048 Allocator->containsUnexpandedParameterPack())) 3049 return; 3050 auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind, 3051 Allocator, SR); 3052 VD->addAttr(A); 3053 if (ASTMutationListener *ML = S.Context.getASTMutationListener()) 3054 ML->DeclarationMarkedOpenMPAllocate(VD, A); 3055 } 3056 3057 Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective( 3058 SourceLocation Loc, ArrayRef<Expr *> VarList, 3059 ArrayRef<OMPClause *> Clauses, DeclContext *Owner) { 3060 assert(Clauses.size() <= 1 && "Expected at most one clause."); 3061 Expr *Allocator = nullptr; 3062 if (Clauses.empty()) { 3063 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions. 3064 // allocate directives that appear in a target region must specify an 3065 // allocator clause unless a requires directive with the dynamic_allocators 3066 // clause is present in the same compilation unit. 3067 if (LangOpts.OpenMPIsDevice && 3068 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 3069 targetDiag(Loc, diag::err_expected_allocator_clause); 3070 } else { 3071 Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator(); 3072 } 3073 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 3074 getAllocatorKind(*this, DSAStack, Allocator); 3075 SmallVector<Expr *, 8> Vars; 3076 for (Expr *RefExpr : VarList) { 3077 auto *DE = cast<DeclRefExpr>(RefExpr); 3078 auto *VD = cast<VarDecl>(DE->getDecl()); 3079 3080 // Check if this is a TLS variable or global register. 3081 if (VD->getTLSKind() != VarDecl::TLS_None || 3082 VD->hasAttr<OMPThreadPrivateDeclAttr>() || 3083 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() && 3084 !VD->isLocalVarDecl())) 3085 continue; 3086 3087 // If the used several times in the allocate directive, the same allocator 3088 // must be used. 3089 if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD, 3090 AllocatorKind, Allocator)) 3091 continue; 3092 3093 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++ 3094 // If a list item has a static storage type, the allocator expression in the 3095 // allocator clause must be a constant expression that evaluates to one of 3096 // the predefined memory allocator values. 3097 if (Allocator && VD->hasGlobalStorage()) { 3098 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) { 3099 Diag(Allocator->getExprLoc(), 3100 diag::err_omp_expected_predefined_allocator) 3101 << Allocator->getSourceRange(); 3102 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 3103 VarDecl::DeclarationOnly; 3104 Diag(VD->getLocation(), 3105 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3106 << VD; 3107 continue; 3108 } 3109 } 3110 3111 Vars.push_back(RefExpr); 3112 applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator, 3113 DE->getSourceRange()); 3114 } 3115 if (Vars.empty()) 3116 return nullptr; 3117 if (!Owner) 3118 Owner = getCurLexicalContext(); 3119 auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses); 3120 D->setAccess(AS_public); 3121 Owner->addDecl(D); 3122 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3123 } 3124 3125 Sema::DeclGroupPtrTy 3126 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc, 3127 ArrayRef<OMPClause *> ClauseList) { 3128 OMPRequiresDecl *D = nullptr; 3129 if (!CurContext->isFileContext()) { 3130 Diag(Loc, diag::err_omp_invalid_scope) << "requires"; 3131 } else { 3132 D = CheckOMPRequiresDecl(Loc, ClauseList); 3133 if (D) { 3134 CurContext->addDecl(D); 3135 DSAStack->addRequiresDecl(D); 3136 } 3137 } 3138 return DeclGroupPtrTy::make(DeclGroupRef(D)); 3139 } 3140 3141 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc, 3142 ArrayRef<OMPClause *> ClauseList) { 3143 /// For target specific clauses, the requires directive cannot be 3144 /// specified after the handling of any of the target regions in the 3145 /// current compilation unit. 3146 ArrayRef<SourceLocation> TargetLocations = 3147 DSAStack->getEncounteredTargetLocs(); 3148 SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc(); 3149 if (!TargetLocations.empty() || !AtomicLoc.isInvalid()) { 3150 for (const OMPClause *CNew : ClauseList) { 3151 // Check if any of the requires clauses affect target regions. 3152 if (isa<OMPUnifiedSharedMemoryClause>(CNew) || 3153 isa<OMPUnifiedAddressClause>(CNew) || 3154 isa<OMPReverseOffloadClause>(CNew) || 3155 isa<OMPDynamicAllocatorsClause>(CNew)) { 3156 Diag(Loc, diag::err_omp_directive_before_requires) 3157 << "target" << getOpenMPClauseName(CNew->getClauseKind()); 3158 for (SourceLocation TargetLoc : TargetLocations) { 3159 Diag(TargetLoc, diag::note_omp_requires_encountered_directive) 3160 << "target"; 3161 } 3162 } else if (!AtomicLoc.isInvalid() && 3163 isa<OMPAtomicDefaultMemOrderClause>(CNew)) { 3164 Diag(Loc, diag::err_omp_directive_before_requires) 3165 << "atomic" << getOpenMPClauseName(CNew->getClauseKind()); 3166 Diag(AtomicLoc, diag::note_omp_requires_encountered_directive) 3167 << "atomic"; 3168 } 3169 } 3170 } 3171 3172 if (!DSAStack->hasDuplicateRequiresClause(ClauseList)) 3173 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc, 3174 ClauseList); 3175 return nullptr; 3176 } 3177 3178 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack, 3179 const ValueDecl *D, 3180 const DSAStackTy::DSAVarData &DVar, 3181 bool IsLoopIterVar) { 3182 if (DVar.RefExpr) { 3183 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 3184 << getOpenMPClauseName(DVar.CKind); 3185 return; 3186 } 3187 enum { 3188 PDSA_StaticMemberShared, 3189 PDSA_StaticLocalVarShared, 3190 PDSA_LoopIterVarPrivate, 3191 PDSA_LoopIterVarLinear, 3192 PDSA_LoopIterVarLastprivate, 3193 PDSA_ConstVarShared, 3194 PDSA_GlobalVarShared, 3195 PDSA_TaskVarFirstprivate, 3196 PDSA_LocalVarPrivate, 3197 PDSA_Implicit 3198 } Reason = PDSA_Implicit; 3199 bool ReportHint = false; 3200 auto ReportLoc = D->getLocation(); 3201 auto *VD = dyn_cast<VarDecl>(D); 3202 if (IsLoopIterVar) { 3203 if (DVar.CKind == OMPC_private) 3204 Reason = PDSA_LoopIterVarPrivate; 3205 else if (DVar.CKind == OMPC_lastprivate) 3206 Reason = PDSA_LoopIterVarLastprivate; 3207 else 3208 Reason = PDSA_LoopIterVarLinear; 3209 } else if (isOpenMPTaskingDirective(DVar.DKind) && 3210 DVar.CKind == OMPC_firstprivate) { 3211 Reason = PDSA_TaskVarFirstprivate; 3212 ReportLoc = DVar.ImplicitDSALoc; 3213 } else if (VD && VD->isStaticLocal()) 3214 Reason = PDSA_StaticLocalVarShared; 3215 else if (VD && VD->isStaticDataMember()) 3216 Reason = PDSA_StaticMemberShared; 3217 else if (VD && VD->isFileVarDecl()) 3218 Reason = PDSA_GlobalVarShared; 3219 else if (D->getType().isConstant(SemaRef.getASTContext())) 3220 Reason = PDSA_ConstVarShared; 3221 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) { 3222 ReportHint = true; 3223 Reason = PDSA_LocalVarPrivate; 3224 } 3225 if (Reason != PDSA_Implicit) { 3226 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa) 3227 << Reason << ReportHint 3228 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 3229 } else if (DVar.ImplicitDSALoc.isValid()) { 3230 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa) 3231 << getOpenMPClauseName(DVar.CKind); 3232 } 3233 } 3234 3235 static OpenMPMapClauseKind 3236 getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M, 3237 bool IsAggregateOrDeclareTarget) { 3238 OpenMPMapClauseKind Kind = OMPC_MAP_unknown; 3239 switch (M) { 3240 case OMPC_DEFAULTMAP_MODIFIER_alloc: 3241 Kind = OMPC_MAP_alloc; 3242 break; 3243 case OMPC_DEFAULTMAP_MODIFIER_to: 3244 Kind = OMPC_MAP_to; 3245 break; 3246 case OMPC_DEFAULTMAP_MODIFIER_from: 3247 Kind = OMPC_MAP_from; 3248 break; 3249 case OMPC_DEFAULTMAP_MODIFIER_tofrom: 3250 Kind = OMPC_MAP_tofrom; 3251 break; 3252 case OMPC_DEFAULTMAP_MODIFIER_firstprivate: 3253 case OMPC_DEFAULTMAP_MODIFIER_last: 3254 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3255 case OMPC_DEFAULTMAP_MODIFIER_none: 3256 case OMPC_DEFAULTMAP_MODIFIER_default: 3257 case OMPC_DEFAULTMAP_MODIFIER_unknown: 3258 // IsAggregateOrDeclareTarget could be true if: 3259 // 1. the implicit behavior for aggregate is tofrom 3260 // 2. it's a declare target link 3261 if (IsAggregateOrDeclareTarget) { 3262 Kind = OMPC_MAP_tofrom; 3263 break; 3264 } 3265 llvm_unreachable("Unexpected defaultmap implicit behavior"); 3266 } 3267 assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known"); 3268 return Kind; 3269 } 3270 3271 namespace { 3272 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> { 3273 DSAStackTy *Stack; 3274 Sema &SemaRef; 3275 bool ErrorFound = false; 3276 bool TryCaptureCXXThisMembers = false; 3277 CapturedStmt *CS = nullptr; 3278 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate; 3279 llvm::SmallVector<Expr *, 4> ImplicitMap[OMPC_MAP_delete]; 3280 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA; 3281 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations; 3282 3283 void VisitSubCaptures(OMPExecutableDirective *S) { 3284 // Check implicitly captured variables. 3285 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt()) 3286 return; 3287 visitSubCaptures(S->getInnermostCapturedStmt()); 3288 // Try to capture inner this->member references to generate correct mappings 3289 // and diagnostics. 3290 if (TryCaptureCXXThisMembers || 3291 (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3292 llvm::any_of(S->getInnermostCapturedStmt()->captures(), 3293 [](const CapturedStmt::Capture &C) { 3294 return C.capturesThis(); 3295 }))) { 3296 bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers; 3297 TryCaptureCXXThisMembers = true; 3298 Visit(S->getInnermostCapturedStmt()->getCapturedStmt()); 3299 TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers; 3300 } 3301 // In tasks firstprivates are not captured anymore, need to analyze them 3302 // explicitly. 3303 if (isOpenMPTaskingDirective(S->getDirectiveKind()) && 3304 !isOpenMPTaskLoopDirective(S->getDirectiveKind())) { 3305 for (OMPClause *C : S->clauses()) 3306 if (auto *FC = dyn_cast<OMPFirstprivateClause>(C)) { 3307 for (Expr *Ref : FC->varlists()) 3308 Visit(Ref); 3309 } 3310 } 3311 } 3312 3313 public: 3314 void VisitDeclRefExpr(DeclRefExpr *E) { 3315 if (TryCaptureCXXThisMembers || E->isTypeDependent() || 3316 E->isValueDependent() || E->containsUnexpandedParameterPack() || 3317 E->isInstantiationDependent()) 3318 return; 3319 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 3320 // Check the datasharing rules for the expressions in the clauses. 3321 if (!CS) { 3322 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD)) 3323 if (!CED->hasAttr<OMPCaptureNoInitAttr>()) { 3324 Visit(CED->getInit()); 3325 return; 3326 } 3327 } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD)) 3328 // Do not analyze internal variables and do not enclose them into 3329 // implicit clauses. 3330 return; 3331 VD = VD->getCanonicalDecl(); 3332 // Skip internally declared variables. 3333 if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD) && 3334 !Stack->isImplicitTaskFirstprivate(VD)) 3335 return; 3336 // Skip allocators in uses_allocators clauses. 3337 if (Stack->isUsesAllocatorsDecl(VD).hasValue()) 3338 return; 3339 3340 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 3341 // Check if the variable has explicit DSA set and stop analysis if it so. 3342 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second) 3343 return; 3344 3345 // Skip internally declared static variables. 3346 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 3347 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 3348 if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) && 3349 (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 3350 !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) && 3351 !Stack->isImplicitTaskFirstprivate(VD)) 3352 return; 3353 3354 SourceLocation ELoc = E->getExprLoc(); 3355 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3356 // The default(none) clause requires that each variable that is referenced 3357 // in the construct, and does not have a predetermined data-sharing 3358 // attribute, must have its data-sharing attribute explicitly determined 3359 // by being listed in a data-sharing attribute clause. 3360 if (DVar.CKind == OMPC_unknown && 3361 (Stack->getDefaultDSA() == DSA_none || 3362 Stack->getDefaultDSA() == DSA_firstprivate) && 3363 isImplicitOrExplicitTaskingRegion(DKind) && 3364 VarsWithInheritedDSA.count(VD) == 0) { 3365 bool InheritedDSA = Stack->getDefaultDSA() == DSA_none; 3366 if (!InheritedDSA && Stack->getDefaultDSA() == DSA_firstprivate) { 3367 DSAStackTy::DSAVarData DVar = 3368 Stack->getImplicitDSA(VD, /*FromParent=*/false); 3369 InheritedDSA = DVar.CKind == OMPC_unknown; 3370 } 3371 if (InheritedDSA) 3372 VarsWithInheritedDSA[VD] = E; 3373 return; 3374 } 3375 3376 // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description] 3377 // If implicit-behavior is none, each variable referenced in the 3378 // construct that does not have a predetermined data-sharing attribute 3379 // and does not appear in a to or link clause on a declare target 3380 // directive must be listed in a data-mapping attribute clause, a 3381 // data-haring attribute clause (including a data-sharing attribute 3382 // clause on a combined construct where target. is one of the 3383 // constituent constructs), or an is_device_ptr clause. 3384 OpenMPDefaultmapClauseKind ClauseKind = 3385 getVariableCategoryFromDecl(SemaRef.getLangOpts(), VD); 3386 if (SemaRef.getLangOpts().OpenMP >= 50) { 3387 bool IsModifierNone = Stack->getDefaultmapModifier(ClauseKind) == 3388 OMPC_DEFAULTMAP_MODIFIER_none; 3389 if (DVar.CKind == OMPC_unknown && IsModifierNone && 3390 VarsWithInheritedDSA.count(VD) == 0 && !Res) { 3391 // Only check for data-mapping attribute and is_device_ptr here 3392 // since we have already make sure that the declaration does not 3393 // have a data-sharing attribute above 3394 if (!Stack->checkMappableExprComponentListsForDecl( 3395 VD, /*CurrentRegionOnly=*/true, 3396 [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef 3397 MapExprComponents, 3398 OpenMPClauseKind) { 3399 auto MI = MapExprComponents.rbegin(); 3400 auto ME = MapExprComponents.rend(); 3401 return MI != ME && MI->getAssociatedDeclaration() == VD; 3402 })) { 3403 VarsWithInheritedDSA[VD] = E; 3404 return; 3405 } 3406 } 3407 } 3408 3409 if (isOpenMPTargetExecutionDirective(DKind) && 3410 !Stack->isLoopControlVariable(VD).first) { 3411 if (!Stack->checkMappableExprComponentListsForDecl( 3412 VD, /*CurrentRegionOnly=*/true, 3413 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 3414 StackComponents, 3415 OpenMPClauseKind) { 3416 // Variable is used if it has been marked as an array, array 3417 // section, array shaping or the variable iself. 3418 return StackComponents.size() == 1 || 3419 std::all_of( 3420 std::next(StackComponents.rbegin()), 3421 StackComponents.rend(), 3422 [](const OMPClauseMappableExprCommon:: 3423 MappableComponent &MC) { 3424 return MC.getAssociatedDeclaration() == 3425 nullptr && 3426 (isa<OMPArraySectionExpr>( 3427 MC.getAssociatedExpression()) || 3428 isa<OMPArrayShapingExpr>( 3429 MC.getAssociatedExpression()) || 3430 isa<ArraySubscriptExpr>( 3431 MC.getAssociatedExpression())); 3432 }); 3433 })) { 3434 bool IsFirstprivate = false; 3435 // By default lambdas are captured as firstprivates. 3436 if (const auto *RD = 3437 VD->getType().getNonReferenceType()->getAsCXXRecordDecl()) 3438 IsFirstprivate = RD->isLambda(); 3439 IsFirstprivate = 3440 IsFirstprivate || (Stack->mustBeFirstprivate(ClauseKind) && !Res); 3441 if (IsFirstprivate) { 3442 ImplicitFirstprivate.emplace_back(E); 3443 } else { 3444 OpenMPDefaultmapClauseModifier M = 3445 Stack->getDefaultmapModifier(ClauseKind); 3446 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3447 M, ClauseKind == OMPC_DEFAULTMAP_aggregate || Res); 3448 ImplicitMap[Kind].emplace_back(E); 3449 } 3450 return; 3451 } 3452 } 3453 3454 // OpenMP [2.9.3.6, Restrictions, p.2] 3455 // A list item that appears in a reduction clause of the innermost 3456 // enclosing worksharing or parallel construct may not be accessed in an 3457 // explicit task. 3458 DVar = Stack->hasInnermostDSA( 3459 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 3460 [](OpenMPDirectiveKind K) { 3461 return isOpenMPParallelDirective(K) || 3462 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3463 }, 3464 /*FromParent=*/true); 3465 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3466 ErrorFound = true; 3467 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3468 reportOriginalDsa(SemaRef, Stack, VD, DVar); 3469 return; 3470 } 3471 3472 // Define implicit data-sharing attributes for task. 3473 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false); 3474 if (((isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared) || 3475 (Stack->getDefaultDSA() == DSA_firstprivate && 3476 DVar.CKind == OMPC_firstprivate && !DVar.RefExpr)) && 3477 !Stack->isLoopControlVariable(VD).first) { 3478 ImplicitFirstprivate.push_back(E); 3479 return; 3480 } 3481 3482 // Store implicitly used globals with declare target link for parent 3483 // target. 3484 if (!isOpenMPTargetExecutionDirective(DKind) && Res && 3485 *Res == OMPDeclareTargetDeclAttr::MT_Link) { 3486 Stack->addToParentTargetRegionLinkGlobals(E); 3487 return; 3488 } 3489 } 3490 } 3491 void VisitMemberExpr(MemberExpr *E) { 3492 if (E->isTypeDependent() || E->isValueDependent() || 3493 E->containsUnexpandedParameterPack() || E->isInstantiationDependent()) 3494 return; 3495 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 3496 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 3497 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParenCasts())) { 3498 if (!FD) 3499 return; 3500 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false); 3501 // Check if the variable has explicit DSA set and stop analysis if it 3502 // so. 3503 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second) 3504 return; 3505 3506 if (isOpenMPTargetExecutionDirective(DKind) && 3507 !Stack->isLoopControlVariable(FD).first && 3508 !Stack->checkMappableExprComponentListsForDecl( 3509 FD, /*CurrentRegionOnly=*/true, 3510 [](OMPClauseMappableExprCommon::MappableExprComponentListRef 3511 StackComponents, 3512 OpenMPClauseKind) { 3513 return isa<CXXThisExpr>( 3514 cast<MemberExpr>( 3515 StackComponents.back().getAssociatedExpression()) 3516 ->getBase() 3517 ->IgnoreParens()); 3518 })) { 3519 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 3520 // A bit-field cannot appear in a map clause. 3521 // 3522 if (FD->isBitField()) 3523 return; 3524 3525 // Check to see if the member expression is referencing a class that 3526 // has already been explicitly mapped 3527 if (Stack->isClassPreviouslyMapped(TE->getType())) 3528 return; 3529 3530 OpenMPDefaultmapClauseModifier Modifier = 3531 Stack->getDefaultmapModifier(OMPC_DEFAULTMAP_aggregate); 3532 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier( 3533 Modifier, /*IsAggregateOrDeclareTarget*/ true); 3534 ImplicitMap[Kind].emplace_back(E); 3535 return; 3536 } 3537 3538 SourceLocation ELoc = E->getExprLoc(); 3539 // OpenMP [2.9.3.6, Restrictions, p.2] 3540 // A list item that appears in a reduction clause of the innermost 3541 // enclosing worksharing or parallel construct may not be accessed in 3542 // an explicit task. 3543 DVar = Stack->hasInnermostDSA( 3544 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 3545 [](OpenMPDirectiveKind K) { 3546 return isOpenMPParallelDirective(K) || 3547 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K); 3548 }, 3549 /*FromParent=*/true); 3550 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) { 3551 ErrorFound = true; 3552 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 3553 reportOriginalDsa(SemaRef, Stack, FD, DVar); 3554 return; 3555 } 3556 3557 // Define implicit data-sharing attributes for task. 3558 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false); 3559 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared && 3560 !Stack->isLoopControlVariable(FD).first) { 3561 // Check if there is a captured expression for the current field in the 3562 // region. Do not mark it as firstprivate unless there is no captured 3563 // expression. 3564 // TODO: try to make it firstprivate. 3565 if (DVar.CKind != OMPC_unknown) 3566 ImplicitFirstprivate.push_back(E); 3567 } 3568 return; 3569 } 3570 if (isOpenMPTargetExecutionDirective(DKind)) { 3571 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 3572 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map, 3573 /*NoDiagnose=*/true)) 3574 return; 3575 const auto *VD = cast<ValueDecl>( 3576 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl()); 3577 if (!Stack->checkMappableExprComponentListsForDecl( 3578 VD, /*CurrentRegionOnly=*/true, 3579 [&CurComponents]( 3580 OMPClauseMappableExprCommon::MappableExprComponentListRef 3581 StackComponents, 3582 OpenMPClauseKind) { 3583 auto CCI = CurComponents.rbegin(); 3584 auto CCE = CurComponents.rend(); 3585 for (const auto &SC : llvm::reverse(StackComponents)) { 3586 // Do both expressions have the same kind? 3587 if (CCI->getAssociatedExpression()->getStmtClass() != 3588 SC.getAssociatedExpression()->getStmtClass()) 3589 if (!((isa<OMPArraySectionExpr>( 3590 SC.getAssociatedExpression()) || 3591 isa<OMPArrayShapingExpr>( 3592 SC.getAssociatedExpression())) && 3593 isa<ArraySubscriptExpr>( 3594 CCI->getAssociatedExpression()))) 3595 return false; 3596 3597 const Decl *CCD = CCI->getAssociatedDeclaration(); 3598 const Decl *SCD = SC.getAssociatedDeclaration(); 3599 CCD = CCD ? CCD->getCanonicalDecl() : nullptr; 3600 SCD = SCD ? SCD->getCanonicalDecl() : nullptr; 3601 if (SCD != CCD) 3602 return false; 3603 std::advance(CCI, 1); 3604 if (CCI == CCE) 3605 break; 3606 } 3607 return true; 3608 })) { 3609 Visit(E->getBase()); 3610 } 3611 } else if (!TryCaptureCXXThisMembers) { 3612 Visit(E->getBase()); 3613 } 3614 } 3615 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 3616 for (OMPClause *C : S->clauses()) { 3617 // Skip analysis of arguments of implicitly defined firstprivate clause 3618 // for task|target directives. 3619 // Skip analysis of arguments of implicitly defined map clause for target 3620 // directives. 3621 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) && 3622 C->isImplicit())) { 3623 for (Stmt *CC : C->children()) { 3624 if (CC) 3625 Visit(CC); 3626 } 3627 } 3628 } 3629 // Check implicitly captured variables. 3630 VisitSubCaptures(S); 3631 } 3632 void VisitStmt(Stmt *S) { 3633 for (Stmt *C : S->children()) { 3634 if (C) { 3635 // Check implicitly captured variables in the task-based directives to 3636 // check if they must be firstprivatized. 3637 Visit(C); 3638 } 3639 } 3640 } 3641 3642 void visitSubCaptures(CapturedStmt *S) { 3643 for (const CapturedStmt::Capture &Cap : S->captures()) { 3644 if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy()) 3645 continue; 3646 VarDecl *VD = Cap.getCapturedVar(); 3647 // Do not try to map the variable if it or its sub-component was mapped 3648 // already. 3649 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 3650 Stack->checkMappableExprComponentListsForDecl( 3651 VD, /*CurrentRegionOnly=*/true, 3652 [](OMPClauseMappableExprCommon::MappableExprComponentListRef, 3653 OpenMPClauseKind) { return true; })) 3654 continue; 3655 DeclRefExpr *DRE = buildDeclRefExpr( 3656 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context), 3657 Cap.getLocation(), /*RefersToCapture=*/true); 3658 Visit(DRE); 3659 } 3660 } 3661 bool isErrorFound() const { return ErrorFound; } 3662 ArrayRef<Expr *> getImplicitFirstprivate() const { 3663 return ImplicitFirstprivate; 3664 } 3665 ArrayRef<Expr *> getImplicitMap(OpenMPDefaultmapClauseKind Kind) const { 3666 return ImplicitMap[Kind]; 3667 } 3668 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const { 3669 return VarsWithInheritedDSA; 3670 } 3671 3672 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 3673 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) { 3674 // Process declare target link variables for the target directives. 3675 if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) { 3676 for (DeclRefExpr *E : Stack->getLinkGlobals()) 3677 Visit(E); 3678 } 3679 } 3680 }; 3681 } // namespace 3682 3683 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { 3684 switch (DKind) { 3685 case OMPD_parallel: 3686 case OMPD_parallel_for: 3687 case OMPD_parallel_for_simd: 3688 case OMPD_parallel_sections: 3689 case OMPD_parallel_master: 3690 case OMPD_teams: 3691 case OMPD_teams_distribute: 3692 case OMPD_teams_distribute_simd: { 3693 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3694 QualType KmpInt32PtrTy = 3695 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3696 Sema::CapturedParamNameType Params[] = { 3697 std::make_pair(".global_tid.", KmpInt32PtrTy), 3698 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3699 std::make_pair(StringRef(), QualType()) // __context with shared vars 3700 }; 3701 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3702 Params); 3703 break; 3704 } 3705 case OMPD_target_teams: 3706 case OMPD_target_parallel: 3707 case OMPD_target_parallel_for: 3708 case OMPD_target_parallel_for_simd: 3709 case OMPD_target_teams_distribute: 3710 case OMPD_target_teams_distribute_simd: { 3711 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3712 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3713 QualType KmpInt32PtrTy = 3714 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3715 QualType Args[] = {VoidPtrTy}; 3716 FunctionProtoType::ExtProtoInfo EPI; 3717 EPI.Variadic = true; 3718 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3719 Sema::CapturedParamNameType Params[] = { 3720 std::make_pair(".global_tid.", KmpInt32Ty), 3721 std::make_pair(".part_id.", KmpInt32PtrTy), 3722 std::make_pair(".privates.", VoidPtrTy), 3723 std::make_pair( 3724 ".copy_fn.", 3725 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3726 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3727 std::make_pair(StringRef(), QualType()) // __context with shared vars 3728 }; 3729 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3730 Params, /*OpenMPCaptureLevel=*/0); 3731 // Mark this captured region as inlined, because we don't use outlined 3732 // function directly. 3733 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3734 AlwaysInlineAttr::CreateImplicit( 3735 Context, {}, AttributeCommonInfo::AS_Keyword, 3736 AlwaysInlineAttr::Keyword_forceinline)); 3737 Sema::CapturedParamNameType ParamsTarget[] = { 3738 std::make_pair(StringRef(), QualType()) // __context with shared vars 3739 }; 3740 // Start a captured region for 'target' with no implicit parameters. 3741 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3742 ParamsTarget, /*OpenMPCaptureLevel=*/1); 3743 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = { 3744 std::make_pair(".global_tid.", KmpInt32PtrTy), 3745 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3746 std::make_pair(StringRef(), QualType()) // __context with shared vars 3747 }; 3748 // Start a captured region for 'teams' or 'parallel'. Both regions have 3749 // the same implicit parameters. 3750 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3751 ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2); 3752 break; 3753 } 3754 case OMPD_target: 3755 case OMPD_target_simd: { 3756 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3757 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3758 QualType KmpInt32PtrTy = 3759 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3760 QualType Args[] = {VoidPtrTy}; 3761 FunctionProtoType::ExtProtoInfo EPI; 3762 EPI.Variadic = true; 3763 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3764 Sema::CapturedParamNameType Params[] = { 3765 std::make_pair(".global_tid.", KmpInt32Ty), 3766 std::make_pair(".part_id.", KmpInt32PtrTy), 3767 std::make_pair(".privates.", VoidPtrTy), 3768 std::make_pair( 3769 ".copy_fn.", 3770 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3771 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3772 std::make_pair(StringRef(), QualType()) // __context with shared vars 3773 }; 3774 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3775 Params, /*OpenMPCaptureLevel=*/0); 3776 // Mark this captured region as inlined, because we don't use outlined 3777 // function directly. 3778 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3779 AlwaysInlineAttr::CreateImplicit( 3780 Context, {}, AttributeCommonInfo::AS_Keyword, 3781 AlwaysInlineAttr::Keyword_forceinline)); 3782 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3783 std::make_pair(StringRef(), QualType()), 3784 /*OpenMPCaptureLevel=*/1); 3785 break; 3786 } 3787 case OMPD_simd: 3788 case OMPD_for: 3789 case OMPD_for_simd: 3790 case OMPD_sections: 3791 case OMPD_section: 3792 case OMPD_single: 3793 case OMPD_master: 3794 case OMPD_critical: 3795 case OMPD_taskgroup: 3796 case OMPD_distribute: 3797 case OMPD_distribute_simd: 3798 case OMPD_ordered: 3799 case OMPD_atomic: 3800 case OMPD_target_data: { 3801 Sema::CapturedParamNameType Params[] = { 3802 std::make_pair(StringRef(), QualType()) // __context with shared vars 3803 }; 3804 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3805 Params); 3806 break; 3807 } 3808 case OMPD_task: { 3809 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3810 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3811 QualType KmpInt32PtrTy = 3812 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3813 QualType Args[] = {VoidPtrTy}; 3814 FunctionProtoType::ExtProtoInfo EPI; 3815 EPI.Variadic = true; 3816 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3817 Sema::CapturedParamNameType Params[] = { 3818 std::make_pair(".global_tid.", KmpInt32Ty), 3819 std::make_pair(".part_id.", KmpInt32PtrTy), 3820 std::make_pair(".privates.", VoidPtrTy), 3821 std::make_pair( 3822 ".copy_fn.", 3823 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3824 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3825 std::make_pair(StringRef(), QualType()) // __context with shared vars 3826 }; 3827 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3828 Params); 3829 // Mark this captured region as inlined, because we don't use outlined 3830 // function directly. 3831 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3832 AlwaysInlineAttr::CreateImplicit( 3833 Context, {}, AttributeCommonInfo::AS_Keyword, 3834 AlwaysInlineAttr::Keyword_forceinline)); 3835 break; 3836 } 3837 case OMPD_taskloop: 3838 case OMPD_taskloop_simd: 3839 case OMPD_master_taskloop: 3840 case OMPD_master_taskloop_simd: { 3841 QualType KmpInt32Ty = 3842 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 3843 .withConst(); 3844 QualType KmpUInt64Ty = 3845 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 3846 .withConst(); 3847 QualType KmpInt64Ty = 3848 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 3849 .withConst(); 3850 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3851 QualType KmpInt32PtrTy = 3852 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3853 QualType Args[] = {VoidPtrTy}; 3854 FunctionProtoType::ExtProtoInfo EPI; 3855 EPI.Variadic = true; 3856 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3857 Sema::CapturedParamNameType Params[] = { 3858 std::make_pair(".global_tid.", KmpInt32Ty), 3859 std::make_pair(".part_id.", KmpInt32PtrTy), 3860 std::make_pair(".privates.", VoidPtrTy), 3861 std::make_pair( 3862 ".copy_fn.", 3863 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3864 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3865 std::make_pair(".lb.", KmpUInt64Ty), 3866 std::make_pair(".ub.", KmpUInt64Ty), 3867 std::make_pair(".st.", KmpInt64Ty), 3868 std::make_pair(".liter.", KmpInt32Ty), 3869 std::make_pair(".reductions.", VoidPtrTy), 3870 std::make_pair(StringRef(), QualType()) // __context with shared vars 3871 }; 3872 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3873 Params); 3874 // Mark this captured region as inlined, because we don't use outlined 3875 // function directly. 3876 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3877 AlwaysInlineAttr::CreateImplicit( 3878 Context, {}, AttributeCommonInfo::AS_Keyword, 3879 AlwaysInlineAttr::Keyword_forceinline)); 3880 break; 3881 } 3882 case OMPD_parallel_master_taskloop: 3883 case OMPD_parallel_master_taskloop_simd: { 3884 QualType KmpInt32Ty = 3885 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1) 3886 .withConst(); 3887 QualType KmpUInt64Ty = 3888 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0) 3889 .withConst(); 3890 QualType KmpInt64Ty = 3891 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1) 3892 .withConst(); 3893 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3894 QualType KmpInt32PtrTy = 3895 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3896 Sema::CapturedParamNameType ParamsParallel[] = { 3897 std::make_pair(".global_tid.", KmpInt32PtrTy), 3898 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3899 std::make_pair(StringRef(), QualType()) // __context with shared vars 3900 }; 3901 // Start a captured region for 'parallel'. 3902 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3903 ParamsParallel, /*OpenMPCaptureLevel=*/0); 3904 QualType Args[] = {VoidPtrTy}; 3905 FunctionProtoType::ExtProtoInfo EPI; 3906 EPI.Variadic = true; 3907 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3908 Sema::CapturedParamNameType Params[] = { 3909 std::make_pair(".global_tid.", KmpInt32Ty), 3910 std::make_pair(".part_id.", KmpInt32PtrTy), 3911 std::make_pair(".privates.", VoidPtrTy), 3912 std::make_pair( 3913 ".copy_fn.", 3914 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3915 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3916 std::make_pair(".lb.", KmpUInt64Ty), 3917 std::make_pair(".ub.", KmpUInt64Ty), 3918 std::make_pair(".st.", KmpInt64Ty), 3919 std::make_pair(".liter.", KmpInt32Ty), 3920 std::make_pair(".reductions.", VoidPtrTy), 3921 std::make_pair(StringRef(), QualType()) // __context with shared vars 3922 }; 3923 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3924 Params, /*OpenMPCaptureLevel=*/1); 3925 // Mark this captured region as inlined, because we don't use outlined 3926 // function directly. 3927 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3928 AlwaysInlineAttr::CreateImplicit( 3929 Context, {}, AttributeCommonInfo::AS_Keyword, 3930 AlwaysInlineAttr::Keyword_forceinline)); 3931 break; 3932 } 3933 case OMPD_distribute_parallel_for_simd: 3934 case OMPD_distribute_parallel_for: { 3935 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3936 QualType KmpInt32PtrTy = 3937 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3938 Sema::CapturedParamNameType Params[] = { 3939 std::make_pair(".global_tid.", KmpInt32PtrTy), 3940 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3941 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 3942 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 3943 std::make_pair(StringRef(), QualType()) // __context with shared vars 3944 }; 3945 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3946 Params); 3947 break; 3948 } 3949 case OMPD_target_teams_distribute_parallel_for: 3950 case OMPD_target_teams_distribute_parallel_for_simd: { 3951 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 3952 QualType KmpInt32PtrTy = 3953 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 3954 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 3955 3956 QualType Args[] = {VoidPtrTy}; 3957 FunctionProtoType::ExtProtoInfo EPI; 3958 EPI.Variadic = true; 3959 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 3960 Sema::CapturedParamNameType Params[] = { 3961 std::make_pair(".global_tid.", KmpInt32Ty), 3962 std::make_pair(".part_id.", KmpInt32PtrTy), 3963 std::make_pair(".privates.", VoidPtrTy), 3964 std::make_pair( 3965 ".copy_fn.", 3966 Context.getPointerType(CopyFnType).withConst().withRestrict()), 3967 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 3968 std::make_pair(StringRef(), QualType()) // __context with shared vars 3969 }; 3970 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3971 Params, /*OpenMPCaptureLevel=*/0); 3972 // Mark this captured region as inlined, because we don't use outlined 3973 // function directly. 3974 getCurCapturedRegion()->TheCapturedDecl->addAttr( 3975 AlwaysInlineAttr::CreateImplicit( 3976 Context, {}, AttributeCommonInfo::AS_Keyword, 3977 AlwaysInlineAttr::Keyword_forceinline)); 3978 Sema::CapturedParamNameType ParamsTarget[] = { 3979 std::make_pair(StringRef(), QualType()) // __context with shared vars 3980 }; 3981 // Start a captured region for 'target' with no implicit parameters. 3982 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3983 ParamsTarget, /*OpenMPCaptureLevel=*/1); 3984 3985 Sema::CapturedParamNameType ParamsTeams[] = { 3986 std::make_pair(".global_tid.", KmpInt32PtrTy), 3987 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3988 std::make_pair(StringRef(), QualType()) // __context with shared vars 3989 }; 3990 // Start a captured region for 'target' with no implicit parameters. 3991 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 3992 ParamsTeams, /*OpenMPCaptureLevel=*/2); 3993 3994 Sema::CapturedParamNameType ParamsParallel[] = { 3995 std::make_pair(".global_tid.", KmpInt32PtrTy), 3996 std::make_pair(".bound_tid.", KmpInt32PtrTy), 3997 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 3998 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 3999 std::make_pair(StringRef(), QualType()) // __context with shared vars 4000 }; 4001 // Start a captured region for 'teams' or 'parallel'. Both regions have 4002 // the same implicit parameters. 4003 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4004 ParamsParallel, /*OpenMPCaptureLevel=*/3); 4005 break; 4006 } 4007 4008 case OMPD_teams_distribute_parallel_for: 4009 case OMPD_teams_distribute_parallel_for_simd: { 4010 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4011 QualType KmpInt32PtrTy = 4012 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4013 4014 Sema::CapturedParamNameType ParamsTeams[] = { 4015 std::make_pair(".global_tid.", KmpInt32PtrTy), 4016 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4017 std::make_pair(StringRef(), QualType()) // __context with shared vars 4018 }; 4019 // Start a captured region for 'target' with no implicit parameters. 4020 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4021 ParamsTeams, /*OpenMPCaptureLevel=*/0); 4022 4023 Sema::CapturedParamNameType ParamsParallel[] = { 4024 std::make_pair(".global_tid.", KmpInt32PtrTy), 4025 std::make_pair(".bound_tid.", KmpInt32PtrTy), 4026 std::make_pair(".previous.lb.", Context.getSizeType().withConst()), 4027 std::make_pair(".previous.ub.", Context.getSizeType().withConst()), 4028 std::make_pair(StringRef(), QualType()) // __context with shared vars 4029 }; 4030 // Start a captured region for 'teams' or 'parallel'. Both regions have 4031 // the same implicit parameters. 4032 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4033 ParamsParallel, /*OpenMPCaptureLevel=*/1); 4034 break; 4035 } 4036 case OMPD_target_update: 4037 case OMPD_target_enter_data: 4038 case OMPD_target_exit_data: { 4039 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst(); 4040 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict(); 4041 QualType KmpInt32PtrTy = 4042 Context.getPointerType(KmpInt32Ty).withConst().withRestrict(); 4043 QualType Args[] = {VoidPtrTy}; 4044 FunctionProtoType::ExtProtoInfo EPI; 4045 EPI.Variadic = true; 4046 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI); 4047 Sema::CapturedParamNameType Params[] = { 4048 std::make_pair(".global_tid.", KmpInt32Ty), 4049 std::make_pair(".part_id.", KmpInt32PtrTy), 4050 std::make_pair(".privates.", VoidPtrTy), 4051 std::make_pair( 4052 ".copy_fn.", 4053 Context.getPointerType(CopyFnType).withConst().withRestrict()), 4054 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()), 4055 std::make_pair(StringRef(), QualType()) // __context with shared vars 4056 }; 4057 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP, 4058 Params); 4059 // Mark this captured region as inlined, because we don't use outlined 4060 // function directly. 4061 getCurCapturedRegion()->TheCapturedDecl->addAttr( 4062 AlwaysInlineAttr::CreateImplicit( 4063 Context, {}, AttributeCommonInfo::AS_Keyword, 4064 AlwaysInlineAttr::Keyword_forceinline)); 4065 break; 4066 } 4067 case OMPD_threadprivate: 4068 case OMPD_allocate: 4069 case OMPD_taskyield: 4070 case OMPD_barrier: 4071 case OMPD_taskwait: 4072 case OMPD_cancellation_point: 4073 case OMPD_cancel: 4074 case OMPD_flush: 4075 case OMPD_depobj: 4076 case OMPD_scan: 4077 case OMPD_declare_reduction: 4078 case OMPD_declare_mapper: 4079 case OMPD_declare_simd: 4080 case OMPD_declare_target: 4081 case OMPD_end_declare_target: 4082 case OMPD_requires: 4083 case OMPD_declare_variant: 4084 case OMPD_begin_declare_variant: 4085 case OMPD_end_declare_variant: 4086 llvm_unreachable("OpenMP Directive is not allowed"); 4087 case OMPD_unknown: 4088 default: 4089 llvm_unreachable("Unknown OpenMP directive"); 4090 } 4091 } 4092 4093 int Sema::getNumberOfConstructScopes(unsigned Level) const { 4094 return getOpenMPCaptureLevels(DSAStack->getDirective(Level)); 4095 } 4096 4097 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) { 4098 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4099 getOpenMPCaptureRegions(CaptureRegions, DKind); 4100 return CaptureRegions.size(); 4101 } 4102 4103 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id, 4104 Expr *CaptureExpr, bool WithInit, 4105 bool AsExpression) { 4106 assert(CaptureExpr); 4107 ASTContext &C = S.getASTContext(); 4108 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts(); 4109 QualType Ty = Init->getType(); 4110 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) { 4111 if (S.getLangOpts().CPlusPlus) { 4112 Ty = C.getLValueReferenceType(Ty); 4113 } else { 4114 Ty = C.getPointerType(Ty); 4115 ExprResult Res = 4116 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init); 4117 if (!Res.isUsable()) 4118 return nullptr; 4119 Init = Res.get(); 4120 } 4121 WithInit = true; 4122 } 4123 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty, 4124 CaptureExpr->getBeginLoc()); 4125 if (!WithInit) 4126 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C)); 4127 S.CurContext->addHiddenDecl(CED); 4128 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false); 4129 return CED; 4130 } 4131 4132 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr, 4133 bool WithInit) { 4134 OMPCapturedExprDecl *CD; 4135 if (VarDecl *VD = S.isOpenMPCapturedDecl(D)) 4136 CD = cast<OMPCapturedExprDecl>(VD); 4137 else 4138 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit, 4139 /*AsExpression=*/false); 4140 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4141 CaptureExpr->getExprLoc()); 4142 } 4143 4144 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) { 4145 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get(); 4146 if (!Ref) { 4147 OMPCapturedExprDecl *CD = buildCaptureDecl( 4148 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr, 4149 /*WithInit=*/true, /*AsExpression=*/true); 4150 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(), 4151 CaptureExpr->getExprLoc()); 4152 } 4153 ExprResult Res = Ref; 4154 if (!S.getLangOpts().CPlusPlus && 4155 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() && 4156 Ref->getType()->isPointerType()) { 4157 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref); 4158 if (!Res.isUsable()) 4159 return ExprError(); 4160 } 4161 return S.DefaultLvalueConversion(Res.get()); 4162 } 4163 4164 namespace { 4165 // OpenMP directives parsed in this section are represented as a 4166 // CapturedStatement with an associated statement. If a syntax error 4167 // is detected during the parsing of the associated statement, the 4168 // compiler must abort processing and close the CapturedStatement. 4169 // 4170 // Combined directives such as 'target parallel' have more than one 4171 // nested CapturedStatements. This RAII ensures that we unwind out 4172 // of all the nested CapturedStatements when an error is found. 4173 class CaptureRegionUnwinderRAII { 4174 private: 4175 Sema &S; 4176 bool &ErrorFound; 4177 OpenMPDirectiveKind DKind = OMPD_unknown; 4178 4179 public: 4180 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound, 4181 OpenMPDirectiveKind DKind) 4182 : S(S), ErrorFound(ErrorFound), DKind(DKind) {} 4183 ~CaptureRegionUnwinderRAII() { 4184 if (ErrorFound) { 4185 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind); 4186 while (--ThisCaptureLevel >= 0) 4187 S.ActOnCapturedRegionError(); 4188 } 4189 } 4190 }; 4191 } // namespace 4192 4193 void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) { 4194 // Capture variables captured by reference in lambdas for target-based 4195 // directives. 4196 if (!CurContext->isDependentContext() && 4197 (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) || 4198 isOpenMPTargetDataManagementDirective( 4199 DSAStack->getCurrentDirective()))) { 4200 QualType Type = V->getType(); 4201 if (const auto *RD = Type.getCanonicalType() 4202 .getNonReferenceType() 4203 ->getAsCXXRecordDecl()) { 4204 bool SavedForceCaptureByReferenceInTargetExecutable = 4205 DSAStack->isForceCaptureByReferenceInTargetExecutable(); 4206 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4207 /*V=*/true); 4208 if (RD->isLambda()) { 4209 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures; 4210 FieldDecl *ThisCapture; 4211 RD->getCaptureFields(Captures, ThisCapture); 4212 for (const LambdaCapture &LC : RD->captures()) { 4213 if (LC.getCaptureKind() == LCK_ByRef) { 4214 VarDecl *VD = LC.getCapturedVar(); 4215 DeclContext *VDC = VD->getDeclContext(); 4216 if (!VDC->Encloses(CurContext)) 4217 continue; 4218 MarkVariableReferenced(LC.getLocation(), VD); 4219 } else if (LC.getCaptureKind() == LCK_This) { 4220 QualType ThisTy = getCurrentThisType(); 4221 if (!ThisTy.isNull() && 4222 Context.typesAreCompatible(ThisTy, ThisCapture->getType())) 4223 CheckCXXThisCapture(LC.getLocation()); 4224 } 4225 } 4226 } 4227 DSAStack->setForceCaptureByReferenceInTargetExecutable( 4228 SavedForceCaptureByReferenceInTargetExecutable); 4229 } 4230 } 4231 } 4232 4233 static bool checkOrderedOrderSpecified(Sema &S, 4234 const ArrayRef<OMPClause *> Clauses) { 4235 const OMPOrderedClause *Ordered = nullptr; 4236 const OMPOrderClause *Order = nullptr; 4237 4238 for (const OMPClause *Clause : Clauses) { 4239 if (Clause->getClauseKind() == OMPC_ordered) 4240 Ordered = cast<OMPOrderedClause>(Clause); 4241 else if (Clause->getClauseKind() == OMPC_order) { 4242 Order = cast<OMPOrderClause>(Clause); 4243 if (Order->getKind() != OMPC_ORDER_concurrent) 4244 Order = nullptr; 4245 } 4246 if (Ordered && Order) 4247 break; 4248 } 4249 4250 if (Ordered && Order) { 4251 S.Diag(Order->getKindKwLoc(), 4252 diag::err_omp_simple_clause_incompatible_with_ordered) 4253 << getOpenMPClauseName(OMPC_order) 4254 << getOpenMPSimpleClauseTypeName(OMPC_order, OMPC_ORDER_concurrent) 4255 << SourceRange(Order->getBeginLoc(), Order->getEndLoc()); 4256 S.Diag(Ordered->getBeginLoc(), diag::note_omp_ordered_param) 4257 << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc()); 4258 return true; 4259 } 4260 return false; 4261 } 4262 4263 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, 4264 ArrayRef<OMPClause *> Clauses) { 4265 bool ErrorFound = false; 4266 CaptureRegionUnwinderRAII CaptureRegionUnwinder( 4267 *this, ErrorFound, DSAStack->getCurrentDirective()); 4268 if (!S.isUsable()) { 4269 ErrorFound = true; 4270 return StmtError(); 4271 } 4272 4273 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4274 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective()); 4275 OMPOrderedClause *OC = nullptr; 4276 OMPScheduleClause *SC = nullptr; 4277 SmallVector<const OMPLinearClause *, 4> LCs; 4278 SmallVector<const OMPClauseWithPreInit *, 4> PICs; 4279 // This is required for proper codegen. 4280 for (OMPClause *Clause : Clauses) { 4281 if (!LangOpts.OpenMPSimd && 4282 isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) && 4283 Clause->getClauseKind() == OMPC_in_reduction) { 4284 // Capture taskgroup task_reduction descriptors inside the tasking regions 4285 // with the corresponding in_reduction items. 4286 auto *IRC = cast<OMPInReductionClause>(Clause); 4287 for (Expr *E : IRC->taskgroup_descriptors()) 4288 if (E) 4289 MarkDeclarationsReferencedInExpr(E); 4290 } 4291 if (isOpenMPPrivate(Clause->getClauseKind()) || 4292 Clause->getClauseKind() == OMPC_copyprivate || 4293 (getLangOpts().OpenMPUseTLS && 4294 getASTContext().getTargetInfo().isTLSSupported() && 4295 Clause->getClauseKind() == OMPC_copyin)) { 4296 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin); 4297 // Mark all variables in private list clauses as used in inner region. 4298 for (Stmt *VarRef : Clause->children()) { 4299 if (auto *E = cast_or_null<Expr>(VarRef)) { 4300 MarkDeclarationsReferencedInExpr(E); 4301 } 4302 } 4303 DSAStack->setForceVarCapturing(/*V=*/false); 4304 } else if (CaptureRegions.size() > 1 || 4305 CaptureRegions.back() != OMPD_unknown) { 4306 if (auto *C = OMPClauseWithPreInit::get(Clause)) 4307 PICs.push_back(C); 4308 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) { 4309 if (Expr *E = C->getPostUpdateExpr()) 4310 MarkDeclarationsReferencedInExpr(E); 4311 } 4312 } 4313 if (Clause->getClauseKind() == OMPC_schedule) 4314 SC = cast<OMPScheduleClause>(Clause); 4315 else if (Clause->getClauseKind() == OMPC_ordered) 4316 OC = cast<OMPOrderedClause>(Clause); 4317 else if (Clause->getClauseKind() == OMPC_linear) 4318 LCs.push_back(cast<OMPLinearClause>(Clause)); 4319 } 4320 // Capture allocator expressions if used. 4321 for (Expr *E : DSAStack->getInnerAllocators()) 4322 MarkDeclarationsReferencedInExpr(E); 4323 // OpenMP, 2.7.1 Loop Construct, Restrictions 4324 // The nonmonotonic modifier cannot be specified if an ordered clause is 4325 // specified. 4326 if (SC && 4327 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 4328 SC->getSecondScheduleModifier() == 4329 OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 4330 OC) { 4331 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic 4332 ? SC->getFirstScheduleModifierLoc() 4333 : SC->getSecondScheduleModifierLoc(), 4334 diag::err_omp_simple_clause_incompatible_with_ordered) 4335 << getOpenMPClauseName(OMPC_schedule) 4336 << getOpenMPSimpleClauseTypeName(OMPC_schedule, 4337 OMPC_SCHEDULE_MODIFIER_nonmonotonic) 4338 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4339 ErrorFound = true; 4340 } 4341 // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions. 4342 // If an order(concurrent) clause is present, an ordered clause may not appear 4343 // on the same directive. 4344 if (checkOrderedOrderSpecified(*this, Clauses)) 4345 ErrorFound = true; 4346 if (!LCs.empty() && OC && OC->getNumForLoops()) { 4347 for (const OMPLinearClause *C : LCs) { 4348 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered) 4349 << SourceRange(OC->getBeginLoc(), OC->getEndLoc()); 4350 } 4351 ErrorFound = true; 4352 } 4353 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) && 4354 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC && 4355 OC->getNumForLoops()) { 4356 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd) 4357 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 4358 ErrorFound = true; 4359 } 4360 if (ErrorFound) { 4361 return StmtError(); 4362 } 4363 StmtResult SR = S; 4364 unsigned CompletedRegions = 0; 4365 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) { 4366 // Mark all variables in private list clauses as used in inner region. 4367 // Required for proper codegen of combined directives. 4368 // TODO: add processing for other clauses. 4369 if (ThisCaptureRegion != OMPD_unknown) { 4370 for (const clang::OMPClauseWithPreInit *C : PICs) { 4371 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion(); 4372 // Find the particular capture region for the clause if the 4373 // directive is a combined one with multiple capture regions. 4374 // If the directive is not a combined one, the capture region 4375 // associated with the clause is OMPD_unknown and is generated 4376 // only once. 4377 if (CaptureRegion == ThisCaptureRegion || 4378 CaptureRegion == OMPD_unknown) { 4379 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) { 4380 for (Decl *D : DS->decls()) 4381 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D)); 4382 } 4383 } 4384 } 4385 } 4386 if (ThisCaptureRegion == OMPD_target) { 4387 // Capture allocator traits in the target region. They are used implicitly 4388 // and, thus, are not captured by default. 4389 for (OMPClause *C : Clauses) { 4390 if (const auto *UAC = dyn_cast<OMPUsesAllocatorsClause>(C)) { 4391 for (unsigned I = 0, End = UAC->getNumberOfAllocators(); I < End; 4392 ++I) { 4393 OMPUsesAllocatorsClause::Data D = UAC->getAllocatorData(I); 4394 if (Expr *E = D.AllocatorTraits) 4395 MarkDeclarationsReferencedInExpr(E); 4396 } 4397 continue; 4398 } 4399 } 4400 } 4401 if (++CompletedRegions == CaptureRegions.size()) 4402 DSAStack->setBodyComplete(); 4403 SR = ActOnCapturedRegionEnd(SR.get()); 4404 } 4405 return SR; 4406 } 4407 4408 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion, 4409 OpenMPDirectiveKind CancelRegion, 4410 SourceLocation StartLoc) { 4411 // CancelRegion is only needed for cancel and cancellation_point. 4412 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point) 4413 return false; 4414 4415 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for || 4416 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup) 4417 return false; 4418 4419 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region) 4420 << getOpenMPDirectiveName(CancelRegion); 4421 return true; 4422 } 4423 4424 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack, 4425 OpenMPDirectiveKind CurrentRegion, 4426 const DeclarationNameInfo &CurrentName, 4427 OpenMPDirectiveKind CancelRegion, 4428 SourceLocation StartLoc) { 4429 if (Stack->getCurScope()) { 4430 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective(); 4431 OpenMPDirectiveKind OffendingRegion = ParentRegion; 4432 bool NestingProhibited = false; 4433 bool CloseNesting = true; 4434 bool OrphanSeen = false; 4435 enum { 4436 NoRecommend, 4437 ShouldBeInParallelRegion, 4438 ShouldBeInOrderedRegion, 4439 ShouldBeInTargetRegion, 4440 ShouldBeInTeamsRegion, 4441 ShouldBeInLoopSimdRegion, 4442 } Recommend = NoRecommend; 4443 if (isOpenMPSimdDirective(ParentRegion) && 4444 ((SemaRef.LangOpts.OpenMP <= 45 && CurrentRegion != OMPD_ordered) || 4445 (SemaRef.LangOpts.OpenMP >= 50 && CurrentRegion != OMPD_ordered && 4446 CurrentRegion != OMPD_simd && CurrentRegion != OMPD_atomic && 4447 CurrentRegion != OMPD_scan))) { 4448 // OpenMP [2.16, Nesting of Regions] 4449 // OpenMP constructs may not be nested inside a simd region. 4450 // OpenMP [2.8.1,simd Construct, Restrictions] 4451 // An ordered construct with the simd clause is the only OpenMP 4452 // construct that can appear in the simd region. 4453 // Allowing a SIMD construct nested in another SIMD construct is an 4454 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning 4455 // message. 4456 // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions] 4457 // The only OpenMP constructs that can be encountered during execution of 4458 // a simd region are the atomic construct, the loop construct, the simd 4459 // construct and the ordered construct with the simd clause. 4460 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd) 4461 ? diag::err_omp_prohibited_region_simd 4462 : diag::warn_omp_nesting_simd) 4463 << (SemaRef.LangOpts.OpenMP >= 50 ? 1 : 0); 4464 return CurrentRegion != OMPD_simd; 4465 } 4466 if (ParentRegion == OMPD_atomic) { 4467 // OpenMP [2.16, Nesting of Regions] 4468 // OpenMP constructs may not be nested inside an atomic region. 4469 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic); 4470 return true; 4471 } 4472 if (CurrentRegion == OMPD_section) { 4473 // OpenMP [2.7.2, sections Construct, Restrictions] 4474 // Orphaned section directives are prohibited. That is, the section 4475 // directives must appear within the sections construct and must not be 4476 // encountered elsewhere in the sections region. 4477 if (ParentRegion != OMPD_sections && 4478 ParentRegion != OMPD_parallel_sections) { 4479 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 4480 << (ParentRegion != OMPD_unknown) 4481 << getOpenMPDirectiveName(ParentRegion); 4482 return true; 4483 } 4484 return false; 4485 } 4486 // Allow some constructs (except teams and cancellation constructs) to be 4487 // orphaned (they could be used in functions, called from OpenMP regions 4488 // with the required preconditions). 4489 if (ParentRegion == OMPD_unknown && 4490 !isOpenMPNestingTeamsDirective(CurrentRegion) && 4491 CurrentRegion != OMPD_cancellation_point && 4492 CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_scan) 4493 return false; 4494 if (CurrentRegion == OMPD_cancellation_point || 4495 CurrentRegion == OMPD_cancel) { 4496 // OpenMP [2.16, Nesting of Regions] 4497 // A cancellation point construct for which construct-type-clause is 4498 // taskgroup must be nested inside a task construct. A cancellation 4499 // point construct for which construct-type-clause is not taskgroup must 4500 // be closely nested inside an OpenMP construct that matches the type 4501 // specified in construct-type-clause. 4502 // A cancel construct for which construct-type-clause is taskgroup must be 4503 // nested inside a task construct. A cancel construct for which 4504 // construct-type-clause is not taskgroup must be closely nested inside an 4505 // OpenMP construct that matches the type specified in 4506 // construct-type-clause. 4507 NestingProhibited = 4508 !((CancelRegion == OMPD_parallel && 4509 (ParentRegion == OMPD_parallel || 4510 ParentRegion == OMPD_target_parallel)) || 4511 (CancelRegion == OMPD_for && 4512 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for || 4513 ParentRegion == OMPD_target_parallel_for || 4514 ParentRegion == OMPD_distribute_parallel_for || 4515 ParentRegion == OMPD_teams_distribute_parallel_for || 4516 ParentRegion == OMPD_target_teams_distribute_parallel_for)) || 4517 (CancelRegion == OMPD_taskgroup && 4518 (ParentRegion == OMPD_task || 4519 (SemaRef.getLangOpts().OpenMP >= 50 && 4520 (ParentRegion == OMPD_taskloop || 4521 ParentRegion == OMPD_master_taskloop || 4522 ParentRegion == OMPD_parallel_master_taskloop)))) || 4523 (CancelRegion == OMPD_sections && 4524 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections || 4525 ParentRegion == OMPD_parallel_sections))); 4526 OrphanSeen = ParentRegion == OMPD_unknown; 4527 } else if (CurrentRegion == OMPD_master) { 4528 // OpenMP [2.16, Nesting of Regions] 4529 // A master region may not be closely nested inside a worksharing, 4530 // atomic, or explicit task region. 4531 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4532 isOpenMPTaskingDirective(ParentRegion); 4533 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { 4534 // OpenMP [2.16, Nesting of Regions] 4535 // A critical region may not be nested (closely or otherwise) inside a 4536 // critical region with the same name. Note that this restriction is not 4537 // sufficient to prevent deadlock. 4538 SourceLocation PreviousCriticalLoc; 4539 bool DeadLock = Stack->hasDirective( 4540 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K, 4541 const DeclarationNameInfo &DNI, 4542 SourceLocation Loc) { 4543 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) { 4544 PreviousCriticalLoc = Loc; 4545 return true; 4546 } 4547 return false; 4548 }, 4549 false /* skip top directive */); 4550 if (DeadLock) { 4551 SemaRef.Diag(StartLoc, 4552 diag::err_omp_prohibited_region_critical_same_name) 4553 << CurrentName.getName(); 4554 if (PreviousCriticalLoc.isValid()) 4555 SemaRef.Diag(PreviousCriticalLoc, 4556 diag::note_omp_previous_critical_region); 4557 return true; 4558 } 4559 } else if (CurrentRegion == OMPD_barrier) { 4560 // OpenMP [2.16, Nesting of Regions] 4561 // A barrier region may not be closely nested inside a worksharing, 4562 // explicit task, critical, ordered, atomic, or master region. 4563 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4564 isOpenMPTaskingDirective(ParentRegion) || 4565 ParentRegion == OMPD_master || 4566 ParentRegion == OMPD_parallel_master || 4567 ParentRegion == OMPD_critical || 4568 ParentRegion == OMPD_ordered; 4569 } else if (isOpenMPWorksharingDirective(CurrentRegion) && 4570 !isOpenMPParallelDirective(CurrentRegion) && 4571 !isOpenMPTeamsDirective(CurrentRegion)) { 4572 // OpenMP [2.16, Nesting of Regions] 4573 // A worksharing region may not be closely nested inside a worksharing, 4574 // explicit task, critical, ordered, atomic, or master region. 4575 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || 4576 isOpenMPTaskingDirective(ParentRegion) || 4577 ParentRegion == OMPD_master || 4578 ParentRegion == OMPD_parallel_master || 4579 ParentRegion == OMPD_critical || 4580 ParentRegion == OMPD_ordered; 4581 Recommend = ShouldBeInParallelRegion; 4582 } else if (CurrentRegion == OMPD_ordered) { 4583 // OpenMP [2.16, Nesting of Regions] 4584 // An ordered region may not be closely nested inside a critical, 4585 // atomic, or explicit task region. 4586 // An ordered region must be closely nested inside a loop region (or 4587 // parallel loop region) with an ordered clause. 4588 // OpenMP [2.8.1,simd Construct, Restrictions] 4589 // An ordered construct with the simd clause is the only OpenMP construct 4590 // that can appear in the simd region. 4591 NestingProhibited = ParentRegion == OMPD_critical || 4592 isOpenMPTaskingDirective(ParentRegion) || 4593 !(isOpenMPSimdDirective(ParentRegion) || 4594 Stack->isParentOrderedRegion()); 4595 Recommend = ShouldBeInOrderedRegion; 4596 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) { 4597 // OpenMP [2.16, Nesting of Regions] 4598 // If specified, a teams construct must be contained within a target 4599 // construct. 4600 NestingProhibited = 4601 (SemaRef.LangOpts.OpenMP <= 45 && ParentRegion != OMPD_target) || 4602 (SemaRef.LangOpts.OpenMP >= 50 && ParentRegion != OMPD_unknown && 4603 ParentRegion != OMPD_target); 4604 OrphanSeen = ParentRegion == OMPD_unknown; 4605 Recommend = ShouldBeInTargetRegion; 4606 } else if (CurrentRegion == OMPD_scan) { 4607 // OpenMP [2.16, Nesting of Regions] 4608 // If specified, a teams construct must be contained within a target 4609 // construct. 4610 NestingProhibited = 4611 SemaRef.LangOpts.OpenMP < 50 || 4612 (ParentRegion != OMPD_simd && ParentRegion != OMPD_for && 4613 ParentRegion != OMPD_for_simd && ParentRegion != OMPD_parallel_for && 4614 ParentRegion != OMPD_parallel_for_simd); 4615 OrphanSeen = ParentRegion == OMPD_unknown; 4616 Recommend = ShouldBeInLoopSimdRegion; 4617 } 4618 if (!NestingProhibited && 4619 !isOpenMPTargetExecutionDirective(CurrentRegion) && 4620 !isOpenMPTargetDataManagementDirective(CurrentRegion) && 4621 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { 4622 // OpenMP [2.16, Nesting of Regions] 4623 // distribute, parallel, parallel sections, parallel workshare, and the 4624 // parallel loop and parallel loop SIMD constructs are the only OpenMP 4625 // constructs that can be closely nested in the teams region. 4626 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && 4627 !isOpenMPDistributeDirective(CurrentRegion); 4628 Recommend = ShouldBeInParallelRegion; 4629 } 4630 if (!NestingProhibited && 4631 isOpenMPNestingDistributeDirective(CurrentRegion)) { 4632 // OpenMP 4.5 [2.17 Nesting of Regions] 4633 // The region associated with the distribute construct must be strictly 4634 // nested inside a teams region 4635 NestingProhibited = 4636 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams); 4637 Recommend = ShouldBeInTeamsRegion; 4638 } 4639 if (!NestingProhibited && 4640 (isOpenMPTargetExecutionDirective(CurrentRegion) || 4641 isOpenMPTargetDataManagementDirective(CurrentRegion))) { 4642 // OpenMP 4.5 [2.17 Nesting of Regions] 4643 // If a target, target update, target data, target enter data, or 4644 // target exit data construct is encountered during execution of a 4645 // target region, the behavior is unspecified. 4646 NestingProhibited = Stack->hasDirective( 4647 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &, 4648 SourceLocation) { 4649 if (isOpenMPTargetExecutionDirective(K)) { 4650 OffendingRegion = K; 4651 return true; 4652 } 4653 return false; 4654 }, 4655 false /* don't skip top directive */); 4656 CloseNesting = false; 4657 } 4658 if (NestingProhibited) { 4659 if (OrphanSeen) { 4660 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive) 4661 << getOpenMPDirectiveName(CurrentRegion) << Recommend; 4662 } else { 4663 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 4664 << CloseNesting << getOpenMPDirectiveName(OffendingRegion) 4665 << Recommend << getOpenMPDirectiveName(CurrentRegion); 4666 } 4667 return true; 4668 } 4669 } 4670 return false; 4671 } 4672 4673 struct Kind2Unsigned { 4674 using argument_type = OpenMPDirectiveKind; 4675 unsigned operator()(argument_type DK) { return unsigned(DK); } 4676 }; 4677 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind, 4678 ArrayRef<OMPClause *> Clauses, 4679 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) { 4680 bool ErrorFound = false; 4681 unsigned NamedModifiersNumber = 0; 4682 llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers; 4683 FoundNameModifiers.resize(llvm::omp::Directive_enumSize + 1); 4684 SmallVector<SourceLocation, 4> NameModifierLoc; 4685 for (const OMPClause *C : Clauses) { 4686 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) { 4687 // At most one if clause without a directive-name-modifier can appear on 4688 // the directive. 4689 OpenMPDirectiveKind CurNM = IC->getNameModifier(); 4690 if (FoundNameModifiers[CurNM]) { 4691 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 4692 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if) 4693 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM); 4694 ErrorFound = true; 4695 } else if (CurNM != OMPD_unknown) { 4696 NameModifierLoc.push_back(IC->getNameModifierLoc()); 4697 ++NamedModifiersNumber; 4698 } 4699 FoundNameModifiers[CurNM] = IC; 4700 if (CurNM == OMPD_unknown) 4701 continue; 4702 // Check if the specified name modifier is allowed for the current 4703 // directive. 4704 // At most one if clause with the particular directive-name-modifier can 4705 // appear on the directive. 4706 bool MatchFound = false; 4707 for (auto NM : AllowedNameModifiers) { 4708 if (CurNM == NM) { 4709 MatchFound = true; 4710 break; 4711 } 4712 } 4713 if (!MatchFound) { 4714 S.Diag(IC->getNameModifierLoc(), 4715 diag::err_omp_wrong_if_directive_name_modifier) 4716 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind); 4717 ErrorFound = true; 4718 } 4719 } 4720 } 4721 // If any if clause on the directive includes a directive-name-modifier then 4722 // all if clauses on the directive must include a directive-name-modifier. 4723 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) { 4724 if (NamedModifiersNumber == AllowedNameModifiers.size()) { 4725 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(), 4726 diag::err_omp_no_more_if_clause); 4727 } else { 4728 std::string Values; 4729 std::string Sep(", "); 4730 unsigned AllowedCnt = 0; 4731 unsigned TotalAllowedNum = 4732 AllowedNameModifiers.size() - NamedModifiersNumber; 4733 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End; 4734 ++Cnt) { 4735 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt]; 4736 if (!FoundNameModifiers[NM]) { 4737 Values += "'"; 4738 Values += getOpenMPDirectiveName(NM); 4739 Values += "'"; 4740 if (AllowedCnt + 2 == TotalAllowedNum) 4741 Values += " or "; 4742 else if (AllowedCnt + 1 != TotalAllowedNum) 4743 Values += Sep; 4744 ++AllowedCnt; 4745 } 4746 } 4747 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(), 4748 diag::err_omp_unnamed_if_clause) 4749 << (TotalAllowedNum > 1) << Values; 4750 } 4751 for (SourceLocation Loc : NameModifierLoc) { 4752 S.Diag(Loc, diag::note_omp_previous_named_if_clause); 4753 } 4754 ErrorFound = true; 4755 } 4756 return ErrorFound; 4757 } 4758 4759 static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *&RefExpr, 4760 SourceLocation &ELoc, 4761 SourceRange &ERange, 4762 bool AllowArraySection) { 4763 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 4764 RefExpr->containsUnexpandedParameterPack()) 4765 return std::make_pair(nullptr, true); 4766 4767 // OpenMP [3.1, C/C++] 4768 // A list item is a variable name. 4769 // OpenMP [2.9.3.3, Restrictions, p.1] 4770 // A variable that is part of another variable (as an array or 4771 // structure element) cannot appear in a private clause. 4772 RefExpr = RefExpr->IgnoreParens(); 4773 enum { 4774 NoArrayExpr = -1, 4775 ArraySubscript = 0, 4776 OMPArraySection = 1 4777 } IsArrayExpr = NoArrayExpr; 4778 if (AllowArraySection) { 4779 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) { 4780 Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); 4781 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 4782 Base = TempASE->getBase()->IgnoreParenImpCasts(); 4783 RefExpr = Base; 4784 IsArrayExpr = ArraySubscript; 4785 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) { 4786 Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 4787 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 4788 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 4789 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 4790 Base = TempASE->getBase()->IgnoreParenImpCasts(); 4791 RefExpr = Base; 4792 IsArrayExpr = OMPArraySection; 4793 } 4794 } 4795 ELoc = RefExpr->getExprLoc(); 4796 ERange = RefExpr->getSourceRange(); 4797 RefExpr = RefExpr->IgnoreParenImpCasts(); 4798 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 4799 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr); 4800 if ((!DE || !isa<VarDecl>(DE->getDecl())) && 4801 (S.getCurrentThisType().isNull() || !ME || 4802 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) || 4803 !isa<FieldDecl>(ME->getMemberDecl()))) { 4804 if (IsArrayExpr != NoArrayExpr) { 4805 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr 4806 << ERange; 4807 } else { 4808 S.Diag(ELoc, 4809 AllowArraySection 4810 ? diag::err_omp_expected_var_name_member_expr_or_array_item 4811 : diag::err_omp_expected_var_name_member_expr) 4812 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange; 4813 } 4814 return std::make_pair(nullptr, false); 4815 } 4816 return std::make_pair( 4817 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false); 4818 } 4819 4820 namespace { 4821 /// Checks if the allocator is used in uses_allocators clause to be allowed in 4822 /// target regions. 4823 class AllocatorChecker final : public ConstStmtVisitor<AllocatorChecker, bool> { 4824 DSAStackTy *S = nullptr; 4825 4826 public: 4827 bool VisitDeclRefExpr(const DeclRefExpr *E) { 4828 return S->isUsesAllocatorsDecl(E->getDecl()) 4829 .getValueOr( 4830 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) == 4831 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait; 4832 } 4833 bool VisitStmt(const Stmt *S) { 4834 for (const Stmt *Child : S->children()) { 4835 if (Child && Visit(Child)) 4836 return true; 4837 } 4838 return false; 4839 } 4840 explicit AllocatorChecker(DSAStackTy *S) : S(S) {} 4841 }; 4842 } // namespace 4843 4844 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack, 4845 ArrayRef<OMPClause *> Clauses) { 4846 assert(!S.CurContext->isDependentContext() && 4847 "Expected non-dependent context."); 4848 auto AllocateRange = 4849 llvm::make_filter_range(Clauses, OMPAllocateClause::classof); 4850 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> 4851 DeclToCopy; 4852 auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) { 4853 return isOpenMPPrivate(C->getClauseKind()); 4854 }); 4855 for (OMPClause *Cl : PrivateRange) { 4856 MutableArrayRef<Expr *>::iterator I, It, Et; 4857 if (Cl->getClauseKind() == OMPC_private) { 4858 auto *PC = cast<OMPPrivateClause>(Cl); 4859 I = PC->private_copies().begin(); 4860 It = PC->varlist_begin(); 4861 Et = PC->varlist_end(); 4862 } else if (Cl->getClauseKind() == OMPC_firstprivate) { 4863 auto *PC = cast<OMPFirstprivateClause>(Cl); 4864 I = PC->private_copies().begin(); 4865 It = PC->varlist_begin(); 4866 Et = PC->varlist_end(); 4867 } else if (Cl->getClauseKind() == OMPC_lastprivate) { 4868 auto *PC = cast<OMPLastprivateClause>(Cl); 4869 I = PC->private_copies().begin(); 4870 It = PC->varlist_begin(); 4871 Et = PC->varlist_end(); 4872 } else if (Cl->getClauseKind() == OMPC_linear) { 4873 auto *PC = cast<OMPLinearClause>(Cl); 4874 I = PC->privates().begin(); 4875 It = PC->varlist_begin(); 4876 Et = PC->varlist_end(); 4877 } else if (Cl->getClauseKind() == OMPC_reduction) { 4878 auto *PC = cast<OMPReductionClause>(Cl); 4879 I = PC->privates().begin(); 4880 It = PC->varlist_begin(); 4881 Et = PC->varlist_end(); 4882 } else if (Cl->getClauseKind() == OMPC_task_reduction) { 4883 auto *PC = cast<OMPTaskReductionClause>(Cl); 4884 I = PC->privates().begin(); 4885 It = PC->varlist_begin(); 4886 Et = PC->varlist_end(); 4887 } else if (Cl->getClauseKind() == OMPC_in_reduction) { 4888 auto *PC = cast<OMPInReductionClause>(Cl); 4889 I = PC->privates().begin(); 4890 It = PC->varlist_begin(); 4891 Et = PC->varlist_end(); 4892 } else { 4893 llvm_unreachable("Expected private clause."); 4894 } 4895 for (Expr *E : llvm::make_range(It, Et)) { 4896 if (!*I) { 4897 ++I; 4898 continue; 4899 } 4900 SourceLocation ELoc; 4901 SourceRange ERange; 4902 Expr *SimpleRefExpr = E; 4903 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 4904 /*AllowArraySection=*/true); 4905 DeclToCopy.try_emplace(Res.first, 4906 cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl())); 4907 ++I; 4908 } 4909 } 4910 for (OMPClause *C : AllocateRange) { 4911 auto *AC = cast<OMPAllocateClause>(C); 4912 if (S.getLangOpts().OpenMP >= 50 && 4913 !Stack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>() && 4914 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) && 4915 AC->getAllocator()) { 4916 Expr *Allocator = AC->getAllocator(); 4917 // OpenMP, 2.12.5 target Construct 4918 // Memory allocators that do not appear in a uses_allocators clause cannot 4919 // appear as an allocator in an allocate clause or be used in the target 4920 // region unless a requires directive with the dynamic_allocators clause 4921 // is present in the same compilation unit. 4922 AllocatorChecker Checker(Stack); 4923 if (Checker.Visit(Allocator)) 4924 S.Diag(Allocator->getExprLoc(), 4925 diag::err_omp_allocator_not_in_uses_allocators) 4926 << Allocator->getSourceRange(); 4927 } 4928 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind = 4929 getAllocatorKind(S, Stack, AC->getAllocator()); 4930 // OpenMP, 2.11.4 allocate Clause, Restrictions. 4931 // For task, taskloop or target directives, allocation requests to memory 4932 // allocators with the trait access set to thread result in unspecified 4933 // behavior. 4934 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc && 4935 (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 4936 isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) { 4937 S.Diag(AC->getAllocator()->getExprLoc(), 4938 diag::warn_omp_allocate_thread_on_task_target_directive) 4939 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 4940 } 4941 for (Expr *E : AC->varlists()) { 4942 SourceLocation ELoc; 4943 SourceRange ERange; 4944 Expr *SimpleRefExpr = E; 4945 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange); 4946 ValueDecl *VD = Res.first; 4947 DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false); 4948 if (!isOpenMPPrivate(Data.CKind)) { 4949 S.Diag(E->getExprLoc(), 4950 diag::err_omp_expected_private_copy_for_allocate); 4951 continue; 4952 } 4953 VarDecl *PrivateVD = DeclToCopy[VD]; 4954 if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD, 4955 AllocatorKind, AC->getAllocator())) 4956 continue; 4957 applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(), 4958 E->getSourceRange()); 4959 } 4960 } 4961 } 4962 4963 StmtResult Sema::ActOnOpenMPExecutableDirective( 4964 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, 4965 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses, 4966 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 4967 StmtResult Res = StmtError(); 4968 // First check CancelRegion which is then used in checkNestingOfRegions. 4969 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) || 4970 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, 4971 StartLoc)) 4972 return StmtError(); 4973 4974 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 4975 VarsWithInheritedDSAType VarsWithInheritedDSA; 4976 bool ErrorFound = false; 4977 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 4978 if (AStmt && !CurContext->isDependentContext()) { 4979 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 4980 4981 // Check default data sharing attributes for referenced variables. 4982 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 4983 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind); 4984 Stmt *S = AStmt; 4985 while (--ThisCaptureLevel >= 0) 4986 S = cast<CapturedStmt>(S)->getCapturedStmt(); 4987 DSAChecker.Visit(S); 4988 if (!isOpenMPTargetDataManagementDirective(Kind) && 4989 !isOpenMPTaskingDirective(Kind)) { 4990 // Visit subcaptures to generate implicit clauses for captured vars. 4991 auto *CS = cast<CapturedStmt>(AStmt); 4992 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 4993 getOpenMPCaptureRegions(CaptureRegions, Kind); 4994 // Ignore outer tasking regions for target directives. 4995 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task) 4996 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 4997 DSAChecker.visitSubCaptures(CS); 4998 } 4999 if (DSAChecker.isErrorFound()) 5000 return StmtError(); 5001 // Generate list of implicitly defined firstprivate variables. 5002 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA(); 5003 5004 SmallVector<Expr *, 4> ImplicitFirstprivates( 5005 DSAChecker.getImplicitFirstprivate().begin(), 5006 DSAChecker.getImplicitFirstprivate().end()); 5007 SmallVector<Expr *, 4> ImplicitMaps[OMPC_MAP_delete]; 5008 for (unsigned I = 0; I < OMPC_MAP_delete; ++I) { 5009 ArrayRef<Expr *> ImplicitMap = 5010 DSAChecker.getImplicitMap(static_cast<OpenMPDefaultmapClauseKind>(I)); 5011 ImplicitMaps[I].append(ImplicitMap.begin(), ImplicitMap.end()); 5012 } 5013 // Mark taskgroup task_reduction descriptors as implicitly firstprivate. 5014 for (OMPClause *C : Clauses) { 5015 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) { 5016 for (Expr *E : IRC->taskgroup_descriptors()) 5017 if (E) 5018 ImplicitFirstprivates.emplace_back(E); 5019 } 5020 // OpenMP 5.0, 2.10.1 task Construct 5021 // [detach clause]... The event-handle will be considered as if it was 5022 // specified on a firstprivate clause. 5023 if (auto *DC = dyn_cast<OMPDetachClause>(C)) 5024 ImplicitFirstprivates.push_back(DC->getEventHandler()); 5025 } 5026 if (!ImplicitFirstprivates.empty()) { 5027 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 5028 ImplicitFirstprivates, SourceLocation(), SourceLocation(), 5029 SourceLocation())) { 5030 ClausesWithImplicit.push_back(Implicit); 5031 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 5032 ImplicitFirstprivates.size(); 5033 } else { 5034 ErrorFound = true; 5035 } 5036 } 5037 int ClauseKindCnt = -1; 5038 for (ArrayRef<Expr *> ImplicitMap : ImplicitMaps) { 5039 ++ClauseKindCnt; 5040 if (ImplicitMap.empty()) 5041 continue; 5042 CXXScopeSpec MapperIdScopeSpec; 5043 DeclarationNameInfo MapperId; 5044 auto Kind = static_cast<OpenMPMapClauseKind>(ClauseKindCnt); 5045 if (OMPClause *Implicit = ActOnOpenMPMapClause( 5046 llvm::None, llvm::None, MapperIdScopeSpec, MapperId, Kind, 5047 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(), 5048 ImplicitMap, OMPVarListLocTy())) { 5049 ClausesWithImplicit.emplace_back(Implicit); 5050 ErrorFound |= 5051 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMap.size(); 5052 } else { 5053 ErrorFound = true; 5054 } 5055 } 5056 } 5057 5058 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers; 5059 switch (Kind) { 5060 case OMPD_parallel: 5061 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 5062 EndLoc); 5063 AllowedNameModifiers.push_back(OMPD_parallel); 5064 break; 5065 case OMPD_simd: 5066 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5067 VarsWithInheritedDSA); 5068 if (LangOpts.OpenMP >= 50) 5069 AllowedNameModifiers.push_back(OMPD_simd); 5070 break; 5071 case OMPD_for: 5072 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc, 5073 VarsWithInheritedDSA); 5074 break; 5075 case OMPD_for_simd: 5076 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5077 EndLoc, VarsWithInheritedDSA); 5078 if (LangOpts.OpenMP >= 50) 5079 AllowedNameModifiers.push_back(OMPD_simd); 5080 break; 5081 case OMPD_sections: 5082 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 5083 EndLoc); 5084 break; 5085 case OMPD_section: 5086 assert(ClausesWithImplicit.empty() && 5087 "No clauses are allowed for 'omp section' directive"); 5088 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 5089 break; 5090 case OMPD_single: 5091 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 5092 EndLoc); 5093 break; 5094 case OMPD_master: 5095 assert(ClausesWithImplicit.empty() && 5096 "No clauses are allowed for 'omp master' directive"); 5097 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc); 5098 break; 5099 case OMPD_critical: 5100 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt, 5101 StartLoc, EndLoc); 5102 break; 5103 case OMPD_parallel_for: 5104 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc, 5105 EndLoc, VarsWithInheritedDSA); 5106 AllowedNameModifiers.push_back(OMPD_parallel); 5107 break; 5108 case OMPD_parallel_for_simd: 5109 Res = ActOnOpenMPParallelForSimdDirective( 5110 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5111 AllowedNameModifiers.push_back(OMPD_parallel); 5112 if (LangOpts.OpenMP >= 50) 5113 AllowedNameModifiers.push_back(OMPD_simd); 5114 break; 5115 case OMPD_parallel_master: 5116 Res = ActOnOpenMPParallelMasterDirective(ClausesWithImplicit, AStmt, 5117 StartLoc, EndLoc); 5118 AllowedNameModifiers.push_back(OMPD_parallel); 5119 break; 5120 case OMPD_parallel_sections: 5121 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt, 5122 StartLoc, EndLoc); 5123 AllowedNameModifiers.push_back(OMPD_parallel); 5124 break; 5125 case OMPD_task: 5126 Res = 5127 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5128 AllowedNameModifiers.push_back(OMPD_task); 5129 break; 5130 case OMPD_taskyield: 5131 assert(ClausesWithImplicit.empty() && 5132 "No clauses are allowed for 'omp taskyield' directive"); 5133 assert(AStmt == nullptr && 5134 "No associated statement allowed for 'omp taskyield' directive"); 5135 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc); 5136 break; 5137 case OMPD_barrier: 5138 assert(ClausesWithImplicit.empty() && 5139 "No clauses are allowed for 'omp barrier' directive"); 5140 assert(AStmt == nullptr && 5141 "No associated statement allowed for 'omp barrier' directive"); 5142 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc); 5143 break; 5144 case OMPD_taskwait: 5145 assert(ClausesWithImplicit.empty() && 5146 "No clauses are allowed for 'omp taskwait' directive"); 5147 assert(AStmt == nullptr && 5148 "No associated statement allowed for 'omp taskwait' directive"); 5149 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc); 5150 break; 5151 case OMPD_taskgroup: 5152 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc, 5153 EndLoc); 5154 break; 5155 case OMPD_flush: 5156 assert(AStmt == nullptr && 5157 "No associated statement allowed for 'omp flush' directive"); 5158 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc); 5159 break; 5160 case OMPD_depobj: 5161 assert(AStmt == nullptr && 5162 "No associated statement allowed for 'omp depobj' directive"); 5163 Res = ActOnOpenMPDepobjDirective(ClausesWithImplicit, StartLoc, EndLoc); 5164 break; 5165 case OMPD_scan: 5166 assert(AStmt == nullptr && 5167 "No associated statement allowed for 'omp scan' directive"); 5168 Res = ActOnOpenMPScanDirective(ClausesWithImplicit, StartLoc, EndLoc); 5169 break; 5170 case OMPD_ordered: 5171 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc, 5172 EndLoc); 5173 break; 5174 case OMPD_atomic: 5175 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc, 5176 EndLoc); 5177 break; 5178 case OMPD_teams: 5179 Res = 5180 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 5181 break; 5182 case OMPD_target: 5183 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, 5184 EndLoc); 5185 AllowedNameModifiers.push_back(OMPD_target); 5186 break; 5187 case OMPD_target_parallel: 5188 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt, 5189 StartLoc, EndLoc); 5190 AllowedNameModifiers.push_back(OMPD_target); 5191 AllowedNameModifiers.push_back(OMPD_parallel); 5192 break; 5193 case OMPD_target_parallel_for: 5194 Res = ActOnOpenMPTargetParallelForDirective( 5195 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5196 AllowedNameModifiers.push_back(OMPD_target); 5197 AllowedNameModifiers.push_back(OMPD_parallel); 5198 break; 5199 case OMPD_cancellation_point: 5200 assert(ClausesWithImplicit.empty() && 5201 "No clauses are allowed for 'omp cancellation point' directive"); 5202 assert(AStmt == nullptr && "No associated statement allowed for 'omp " 5203 "cancellation point' directive"); 5204 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); 5205 break; 5206 case OMPD_cancel: 5207 assert(AStmt == nullptr && 5208 "No associated statement allowed for 'omp cancel' directive"); 5209 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc, 5210 CancelRegion); 5211 AllowedNameModifiers.push_back(OMPD_cancel); 5212 break; 5213 case OMPD_target_data: 5214 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc, 5215 EndLoc); 5216 AllowedNameModifiers.push_back(OMPD_target_data); 5217 break; 5218 case OMPD_target_enter_data: 5219 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc, 5220 EndLoc, AStmt); 5221 AllowedNameModifiers.push_back(OMPD_target_enter_data); 5222 break; 5223 case OMPD_target_exit_data: 5224 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc, 5225 EndLoc, AStmt); 5226 AllowedNameModifiers.push_back(OMPD_target_exit_data); 5227 break; 5228 case OMPD_taskloop: 5229 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc, 5230 EndLoc, VarsWithInheritedDSA); 5231 AllowedNameModifiers.push_back(OMPD_taskloop); 5232 break; 5233 case OMPD_taskloop_simd: 5234 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5235 EndLoc, VarsWithInheritedDSA); 5236 AllowedNameModifiers.push_back(OMPD_taskloop); 5237 if (LangOpts.OpenMP >= 50) 5238 AllowedNameModifiers.push_back(OMPD_simd); 5239 break; 5240 case OMPD_master_taskloop: 5241 Res = ActOnOpenMPMasterTaskLoopDirective( 5242 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5243 AllowedNameModifiers.push_back(OMPD_taskloop); 5244 break; 5245 case OMPD_master_taskloop_simd: 5246 Res = ActOnOpenMPMasterTaskLoopSimdDirective( 5247 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5248 AllowedNameModifiers.push_back(OMPD_taskloop); 5249 if (LangOpts.OpenMP >= 50) 5250 AllowedNameModifiers.push_back(OMPD_simd); 5251 break; 5252 case OMPD_parallel_master_taskloop: 5253 Res = ActOnOpenMPParallelMasterTaskLoopDirective( 5254 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5255 AllowedNameModifiers.push_back(OMPD_taskloop); 5256 AllowedNameModifiers.push_back(OMPD_parallel); 5257 break; 5258 case OMPD_parallel_master_taskloop_simd: 5259 Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective( 5260 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5261 AllowedNameModifiers.push_back(OMPD_taskloop); 5262 AllowedNameModifiers.push_back(OMPD_parallel); 5263 if (LangOpts.OpenMP >= 50) 5264 AllowedNameModifiers.push_back(OMPD_simd); 5265 break; 5266 case OMPD_distribute: 5267 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc, 5268 EndLoc, VarsWithInheritedDSA); 5269 break; 5270 case OMPD_target_update: 5271 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, 5272 EndLoc, AStmt); 5273 AllowedNameModifiers.push_back(OMPD_target_update); 5274 break; 5275 case OMPD_distribute_parallel_for: 5276 Res = ActOnOpenMPDistributeParallelForDirective( 5277 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5278 AllowedNameModifiers.push_back(OMPD_parallel); 5279 break; 5280 case OMPD_distribute_parallel_for_simd: 5281 Res = ActOnOpenMPDistributeParallelForSimdDirective( 5282 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5283 AllowedNameModifiers.push_back(OMPD_parallel); 5284 if (LangOpts.OpenMP >= 50) 5285 AllowedNameModifiers.push_back(OMPD_simd); 5286 break; 5287 case OMPD_distribute_simd: 5288 Res = ActOnOpenMPDistributeSimdDirective( 5289 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5290 if (LangOpts.OpenMP >= 50) 5291 AllowedNameModifiers.push_back(OMPD_simd); 5292 break; 5293 case OMPD_target_parallel_for_simd: 5294 Res = ActOnOpenMPTargetParallelForSimdDirective( 5295 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5296 AllowedNameModifiers.push_back(OMPD_target); 5297 AllowedNameModifiers.push_back(OMPD_parallel); 5298 if (LangOpts.OpenMP >= 50) 5299 AllowedNameModifiers.push_back(OMPD_simd); 5300 break; 5301 case OMPD_target_simd: 5302 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc, 5303 EndLoc, VarsWithInheritedDSA); 5304 AllowedNameModifiers.push_back(OMPD_target); 5305 if (LangOpts.OpenMP >= 50) 5306 AllowedNameModifiers.push_back(OMPD_simd); 5307 break; 5308 case OMPD_teams_distribute: 5309 Res = ActOnOpenMPTeamsDistributeDirective( 5310 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5311 break; 5312 case OMPD_teams_distribute_simd: 5313 Res = ActOnOpenMPTeamsDistributeSimdDirective( 5314 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5315 if (LangOpts.OpenMP >= 50) 5316 AllowedNameModifiers.push_back(OMPD_simd); 5317 break; 5318 case OMPD_teams_distribute_parallel_for_simd: 5319 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective( 5320 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5321 AllowedNameModifiers.push_back(OMPD_parallel); 5322 if (LangOpts.OpenMP >= 50) 5323 AllowedNameModifiers.push_back(OMPD_simd); 5324 break; 5325 case OMPD_teams_distribute_parallel_for: 5326 Res = ActOnOpenMPTeamsDistributeParallelForDirective( 5327 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5328 AllowedNameModifiers.push_back(OMPD_parallel); 5329 break; 5330 case OMPD_target_teams: 5331 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, 5332 EndLoc); 5333 AllowedNameModifiers.push_back(OMPD_target); 5334 break; 5335 case OMPD_target_teams_distribute: 5336 Res = ActOnOpenMPTargetTeamsDistributeDirective( 5337 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5338 AllowedNameModifiers.push_back(OMPD_target); 5339 break; 5340 case OMPD_target_teams_distribute_parallel_for: 5341 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective( 5342 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5343 AllowedNameModifiers.push_back(OMPD_target); 5344 AllowedNameModifiers.push_back(OMPD_parallel); 5345 break; 5346 case OMPD_target_teams_distribute_parallel_for_simd: 5347 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 5348 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5349 AllowedNameModifiers.push_back(OMPD_target); 5350 AllowedNameModifiers.push_back(OMPD_parallel); 5351 if (LangOpts.OpenMP >= 50) 5352 AllowedNameModifiers.push_back(OMPD_simd); 5353 break; 5354 case OMPD_target_teams_distribute_simd: 5355 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective( 5356 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA); 5357 AllowedNameModifiers.push_back(OMPD_target); 5358 if (LangOpts.OpenMP >= 50) 5359 AllowedNameModifiers.push_back(OMPD_simd); 5360 break; 5361 case OMPD_declare_target: 5362 case OMPD_end_declare_target: 5363 case OMPD_threadprivate: 5364 case OMPD_allocate: 5365 case OMPD_declare_reduction: 5366 case OMPD_declare_mapper: 5367 case OMPD_declare_simd: 5368 case OMPD_requires: 5369 case OMPD_declare_variant: 5370 case OMPD_begin_declare_variant: 5371 case OMPD_end_declare_variant: 5372 llvm_unreachable("OpenMP Directive is not allowed"); 5373 case OMPD_unknown: 5374 default: 5375 llvm_unreachable("Unknown OpenMP directive"); 5376 } 5377 5378 ErrorFound = Res.isInvalid() || ErrorFound; 5379 5380 // Check variables in the clauses if default(none) or 5381 // default(firstprivate) was specified. 5382 if (DSAStack->getDefaultDSA() == DSA_none || 5383 DSAStack->getDefaultDSA() == DSA_firstprivate) { 5384 DSAAttrChecker DSAChecker(DSAStack, *this, nullptr); 5385 for (OMPClause *C : Clauses) { 5386 switch (C->getClauseKind()) { 5387 case OMPC_num_threads: 5388 case OMPC_dist_schedule: 5389 // Do not analyse if no parent teams directive. 5390 if (isOpenMPTeamsDirective(Kind)) 5391 break; 5392 continue; 5393 case OMPC_if: 5394 if (isOpenMPTeamsDirective(Kind) && 5395 cast<OMPIfClause>(C)->getNameModifier() != OMPD_target) 5396 break; 5397 if (isOpenMPParallelDirective(Kind) && 5398 isOpenMPTaskLoopDirective(Kind) && 5399 cast<OMPIfClause>(C)->getNameModifier() != OMPD_parallel) 5400 break; 5401 continue; 5402 case OMPC_schedule: 5403 case OMPC_detach: 5404 break; 5405 case OMPC_grainsize: 5406 case OMPC_num_tasks: 5407 case OMPC_final: 5408 case OMPC_priority: 5409 // Do not analyze if no parent parallel directive. 5410 if (isOpenMPParallelDirective(Kind)) 5411 break; 5412 continue; 5413 case OMPC_ordered: 5414 case OMPC_device: 5415 case OMPC_num_teams: 5416 case OMPC_thread_limit: 5417 case OMPC_hint: 5418 case OMPC_collapse: 5419 case OMPC_safelen: 5420 case OMPC_simdlen: 5421 case OMPC_default: 5422 case OMPC_proc_bind: 5423 case OMPC_private: 5424 case OMPC_firstprivate: 5425 case OMPC_lastprivate: 5426 case OMPC_shared: 5427 case OMPC_reduction: 5428 case OMPC_task_reduction: 5429 case OMPC_in_reduction: 5430 case OMPC_linear: 5431 case OMPC_aligned: 5432 case OMPC_copyin: 5433 case OMPC_copyprivate: 5434 case OMPC_nowait: 5435 case OMPC_untied: 5436 case OMPC_mergeable: 5437 case OMPC_allocate: 5438 case OMPC_read: 5439 case OMPC_write: 5440 case OMPC_update: 5441 case OMPC_capture: 5442 case OMPC_seq_cst: 5443 case OMPC_acq_rel: 5444 case OMPC_acquire: 5445 case OMPC_release: 5446 case OMPC_relaxed: 5447 case OMPC_depend: 5448 case OMPC_threads: 5449 case OMPC_simd: 5450 case OMPC_map: 5451 case OMPC_nogroup: 5452 case OMPC_defaultmap: 5453 case OMPC_to: 5454 case OMPC_from: 5455 case OMPC_use_device_ptr: 5456 case OMPC_use_device_addr: 5457 case OMPC_is_device_ptr: 5458 case OMPC_nontemporal: 5459 case OMPC_order: 5460 case OMPC_destroy: 5461 case OMPC_inclusive: 5462 case OMPC_exclusive: 5463 case OMPC_uses_allocators: 5464 case OMPC_affinity: 5465 continue; 5466 case OMPC_allocator: 5467 case OMPC_flush: 5468 case OMPC_depobj: 5469 case OMPC_threadprivate: 5470 case OMPC_uniform: 5471 case OMPC_unknown: 5472 case OMPC_unified_address: 5473 case OMPC_unified_shared_memory: 5474 case OMPC_reverse_offload: 5475 case OMPC_dynamic_allocators: 5476 case OMPC_atomic_default_mem_order: 5477 case OMPC_device_type: 5478 case OMPC_match: 5479 default: 5480 llvm_unreachable("Unexpected clause"); 5481 } 5482 for (Stmt *CC : C->children()) { 5483 if (CC) 5484 DSAChecker.Visit(CC); 5485 } 5486 } 5487 for (const auto &P : DSAChecker.getVarsWithInheritedDSA()) 5488 VarsWithInheritedDSA[P.getFirst()] = P.getSecond(); 5489 } 5490 for (const auto &P : VarsWithInheritedDSA) { 5491 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst())) 5492 continue; 5493 ErrorFound = true; 5494 if (DSAStack->getDefaultDSA() == DSA_none || 5495 DSAStack->getDefaultDSA() == DSA_firstprivate) { 5496 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable) 5497 << P.first << P.second->getSourceRange(); 5498 Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none); 5499 } else if (getLangOpts().OpenMP >= 50) { 5500 Diag(P.second->getExprLoc(), 5501 diag::err_omp_defaultmap_no_attr_for_variable) 5502 << P.first << P.second->getSourceRange(); 5503 Diag(DSAStack->getDefaultDSALocation(), 5504 diag::note_omp_defaultmap_attr_none); 5505 } 5506 } 5507 5508 if (!AllowedNameModifiers.empty()) 5509 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) || 5510 ErrorFound; 5511 5512 if (ErrorFound) 5513 return StmtError(); 5514 5515 if (!CurContext->isDependentContext() && 5516 isOpenMPTargetExecutionDirective(Kind) && 5517 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() || 5518 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() || 5519 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() || 5520 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) { 5521 // Register target to DSA Stack. 5522 DSAStack->addTargetDirLocation(StartLoc); 5523 } 5524 5525 return Res; 5526 } 5527 5528 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective( 5529 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen, 5530 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds, 5531 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears, 5532 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) { 5533 assert(Aligneds.size() == Alignments.size()); 5534 assert(Linears.size() == LinModifiers.size()); 5535 assert(Linears.size() == Steps.size()); 5536 if (!DG || DG.get().isNull()) 5537 return DeclGroupPtrTy(); 5538 5539 const int SimdId = 0; 5540 if (!DG.get().isSingleDecl()) { 5541 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 5542 << SimdId; 5543 return DG; 5544 } 5545 Decl *ADecl = DG.get().getSingleDecl(); 5546 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 5547 ADecl = FTD->getTemplatedDecl(); 5548 5549 auto *FD = dyn_cast<FunctionDecl>(ADecl); 5550 if (!FD) { 5551 Diag(ADecl->getLocation(), diag::err_omp_function_expected) << SimdId; 5552 return DeclGroupPtrTy(); 5553 } 5554 5555 // OpenMP [2.8.2, declare simd construct, Description] 5556 // The parameter of the simdlen clause must be a constant positive integer 5557 // expression. 5558 ExprResult SL; 5559 if (Simdlen) 5560 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen); 5561 // OpenMP [2.8.2, declare simd construct, Description] 5562 // The special this pointer can be used as if was one of the arguments to the 5563 // function in any of the linear, aligned, or uniform clauses. 5564 // The uniform clause declares one or more arguments to have an invariant 5565 // value for all concurrent invocations of the function in the execution of a 5566 // single SIMD loop. 5567 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs; 5568 const Expr *UniformedLinearThis = nullptr; 5569 for (const Expr *E : Uniforms) { 5570 E = E->IgnoreParenImpCasts(); 5571 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 5572 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) 5573 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 5574 FD->getParamDecl(PVD->getFunctionScopeIndex()) 5575 ->getCanonicalDecl() == PVD->getCanonicalDecl()) { 5576 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E); 5577 continue; 5578 } 5579 if (isa<CXXThisExpr>(E)) { 5580 UniformedLinearThis = E; 5581 continue; 5582 } 5583 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 5584 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 5585 } 5586 // OpenMP [2.8.2, declare simd construct, Description] 5587 // The aligned clause declares that the object to which each list item points 5588 // is aligned to the number of bytes expressed in the optional parameter of 5589 // the aligned clause. 5590 // The special this pointer can be used as if was one of the arguments to the 5591 // function in any of the linear, aligned, or uniform clauses. 5592 // The type of list items appearing in the aligned clause must be array, 5593 // pointer, reference to array, or reference to pointer. 5594 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs; 5595 const Expr *AlignedThis = nullptr; 5596 for (const Expr *E : Aligneds) { 5597 E = E->IgnoreParenImpCasts(); 5598 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 5599 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 5600 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 5601 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 5602 FD->getParamDecl(PVD->getFunctionScopeIndex()) 5603 ->getCanonicalDecl() == CanonPVD) { 5604 // OpenMP [2.8.1, simd construct, Restrictions] 5605 // A list-item cannot appear in more than one aligned clause. 5606 if (AlignedArgs.count(CanonPVD) > 0) { 5607 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 5608 << 1 << getOpenMPClauseName(OMPC_aligned) 5609 << E->getSourceRange(); 5610 Diag(AlignedArgs[CanonPVD]->getExprLoc(), 5611 diag::note_omp_explicit_dsa) 5612 << getOpenMPClauseName(OMPC_aligned); 5613 continue; 5614 } 5615 AlignedArgs[CanonPVD] = E; 5616 QualType QTy = PVD->getType() 5617 .getNonReferenceType() 5618 .getUnqualifiedType() 5619 .getCanonicalType(); 5620 const Type *Ty = QTy.getTypePtrOrNull(); 5621 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 5622 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr) 5623 << QTy << getLangOpts().CPlusPlus << E->getSourceRange(); 5624 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD; 5625 } 5626 continue; 5627 } 5628 } 5629 if (isa<CXXThisExpr>(E)) { 5630 if (AlignedThis) { 5631 Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice) 5632 << 2 << getOpenMPClauseName(OMPC_aligned) << E->getSourceRange(); 5633 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa) 5634 << getOpenMPClauseName(OMPC_aligned); 5635 } 5636 AlignedThis = E; 5637 continue; 5638 } 5639 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 5640 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 5641 } 5642 // The optional parameter of the aligned clause, alignment, must be a constant 5643 // positive integer expression. If no optional parameter is specified, 5644 // implementation-defined default alignments for SIMD instructions on the 5645 // target platforms are assumed. 5646 SmallVector<const Expr *, 4> NewAligns; 5647 for (Expr *E : Alignments) { 5648 ExprResult Align; 5649 if (E) 5650 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned); 5651 NewAligns.push_back(Align.get()); 5652 } 5653 // OpenMP [2.8.2, declare simd construct, Description] 5654 // The linear clause declares one or more list items to be private to a SIMD 5655 // lane and to have a linear relationship with respect to the iteration space 5656 // of a loop. 5657 // The special this pointer can be used as if was one of the arguments to the 5658 // function in any of the linear, aligned, or uniform clauses. 5659 // When a linear-step expression is specified in a linear clause it must be 5660 // either a constant integer expression or an integer-typed parameter that is 5661 // specified in a uniform clause on the directive. 5662 llvm::DenseMap<const Decl *, const Expr *> LinearArgs; 5663 const bool IsUniformedThis = UniformedLinearThis != nullptr; 5664 auto MI = LinModifiers.begin(); 5665 for (const Expr *E : Linears) { 5666 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI); 5667 ++MI; 5668 E = E->IgnoreParenImpCasts(); 5669 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 5670 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 5671 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 5672 if (FD->getNumParams() > PVD->getFunctionScopeIndex() && 5673 FD->getParamDecl(PVD->getFunctionScopeIndex()) 5674 ->getCanonicalDecl() == CanonPVD) { 5675 // OpenMP [2.15.3.7, linear Clause, Restrictions] 5676 // A list-item cannot appear in more than one linear clause. 5677 if (LinearArgs.count(CanonPVD) > 0) { 5678 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 5679 << getOpenMPClauseName(OMPC_linear) 5680 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange(); 5681 Diag(LinearArgs[CanonPVD]->getExprLoc(), 5682 diag::note_omp_explicit_dsa) 5683 << getOpenMPClauseName(OMPC_linear); 5684 continue; 5685 } 5686 // Each argument can appear in at most one uniform or linear clause. 5687 if (UniformedArgs.count(CanonPVD) > 0) { 5688 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 5689 << getOpenMPClauseName(OMPC_linear) 5690 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange(); 5691 Diag(UniformedArgs[CanonPVD]->getExprLoc(), 5692 diag::note_omp_explicit_dsa) 5693 << getOpenMPClauseName(OMPC_uniform); 5694 continue; 5695 } 5696 LinearArgs[CanonPVD] = E; 5697 if (E->isValueDependent() || E->isTypeDependent() || 5698 E->isInstantiationDependent() || 5699 E->containsUnexpandedParameterPack()) 5700 continue; 5701 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind, 5702 PVD->getOriginalType(), 5703 /*IsDeclareSimd=*/true); 5704 continue; 5705 } 5706 } 5707 if (isa<CXXThisExpr>(E)) { 5708 if (UniformedLinearThis) { 5709 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa) 5710 << getOpenMPClauseName(OMPC_linear) 5711 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear) 5712 << E->getSourceRange(); 5713 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa) 5714 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform 5715 : OMPC_linear); 5716 continue; 5717 } 5718 UniformedLinearThis = E; 5719 if (E->isValueDependent() || E->isTypeDependent() || 5720 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 5721 continue; 5722 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind, 5723 E->getType(), /*IsDeclareSimd=*/true); 5724 continue; 5725 } 5726 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause) 5727 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0); 5728 } 5729 Expr *Step = nullptr; 5730 Expr *NewStep = nullptr; 5731 SmallVector<Expr *, 4> NewSteps; 5732 for (Expr *E : Steps) { 5733 // Skip the same step expression, it was checked already. 5734 if (Step == E || !E) { 5735 NewSteps.push_back(E ? NewStep : nullptr); 5736 continue; 5737 } 5738 Step = E; 5739 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step)) 5740 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { 5741 const VarDecl *CanonPVD = PVD->getCanonicalDecl(); 5742 if (UniformedArgs.count(CanonPVD) == 0) { 5743 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param) 5744 << Step->getSourceRange(); 5745 } else if (E->isValueDependent() || E->isTypeDependent() || 5746 E->isInstantiationDependent() || 5747 E->containsUnexpandedParameterPack() || 5748 CanonPVD->getType()->hasIntegerRepresentation()) { 5749 NewSteps.push_back(Step); 5750 } else { 5751 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param) 5752 << Step->getSourceRange(); 5753 } 5754 continue; 5755 } 5756 NewStep = Step; 5757 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 5758 !Step->isInstantiationDependent() && 5759 !Step->containsUnexpandedParameterPack()) { 5760 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step) 5761 .get(); 5762 if (NewStep) 5763 NewStep = VerifyIntegerConstantExpression(NewStep).get(); 5764 } 5765 NewSteps.push_back(NewStep); 5766 } 5767 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit( 5768 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()), 5769 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(), 5770 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), 5771 const_cast<Expr **>(Linears.data()), Linears.size(), 5772 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(), 5773 NewSteps.data(), NewSteps.size(), SR); 5774 ADecl->addAttr(NewAttr); 5775 return DG; 5776 } 5777 5778 static void setPrototype(Sema &S, FunctionDecl *FD, FunctionDecl *FDWithProto, 5779 QualType NewType) { 5780 assert(NewType->isFunctionProtoType() && 5781 "Expected function type with prototype."); 5782 assert(FD->getType()->isFunctionNoProtoType() && 5783 "Expected function with type with no prototype."); 5784 assert(FDWithProto->getType()->isFunctionProtoType() && 5785 "Expected function with prototype."); 5786 // Synthesize parameters with the same types. 5787 FD->setType(NewType); 5788 SmallVector<ParmVarDecl *, 16> Params; 5789 for (const ParmVarDecl *P : FDWithProto->parameters()) { 5790 auto *Param = ParmVarDecl::Create(S.getASTContext(), FD, SourceLocation(), 5791 SourceLocation(), nullptr, P->getType(), 5792 /*TInfo=*/nullptr, SC_None, nullptr); 5793 Param->setScopeInfo(0, Params.size()); 5794 Param->setImplicit(); 5795 Params.push_back(Param); 5796 } 5797 5798 FD->setParams(Params); 5799 } 5800 5801 Sema::OMPDeclareVariantScope::OMPDeclareVariantScope(OMPTraitInfo &TI) 5802 : TI(&TI), NameSuffix(TI.getMangledName()) {} 5803 5804 FunctionDecl * 5805 Sema::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope(Scope *S, 5806 Declarator &D) { 5807 IdentifierInfo *BaseII = D.getIdentifier(); 5808 LookupResult Lookup(*this, DeclarationName(BaseII), D.getIdentifierLoc(), 5809 LookupOrdinaryName); 5810 LookupParsedName(Lookup, S, &D.getCXXScopeSpec()); 5811 5812 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 5813 QualType FType = TInfo->getType(); 5814 5815 bool IsConstexpr = D.getDeclSpec().getConstexprSpecifier() == CSK_constexpr; 5816 bool IsConsteval = D.getDeclSpec().getConstexprSpecifier() == CSK_consteval; 5817 5818 FunctionDecl *BaseFD = nullptr; 5819 for (auto *Candidate : Lookup) { 5820 auto *UDecl = dyn_cast<FunctionDecl>(Candidate->getUnderlyingDecl()); 5821 if (!UDecl) 5822 continue; 5823 5824 // Don't specialize constexpr/consteval functions with 5825 // non-constexpr/consteval functions. 5826 if (UDecl->isConstexpr() && !IsConstexpr) 5827 continue; 5828 if (UDecl->isConsteval() && !IsConsteval) 5829 continue; 5830 5831 QualType NewType = Context.mergeFunctionTypes( 5832 FType, UDecl->getType(), /* OfBlockPointer */ false, 5833 /* Unqualified */ false, /* AllowCXX */ true); 5834 if (NewType.isNull()) 5835 continue; 5836 5837 // Found a base! 5838 BaseFD = UDecl; 5839 break; 5840 } 5841 if (!BaseFD) { 5842 BaseFD = cast<FunctionDecl>(ActOnDeclarator(S, D)); 5843 BaseFD->setImplicit(true); 5844 } 5845 5846 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 5847 std::string MangledName; 5848 MangledName += D.getIdentifier()->getName(); 5849 MangledName += getOpenMPVariantManglingSeparatorStr(); 5850 MangledName += DVScope.NameSuffix; 5851 IdentifierInfo &VariantII = Context.Idents.get(MangledName); 5852 5853 VariantII.setMangledOpenMPVariantName(true); 5854 D.SetIdentifier(&VariantII, D.getBeginLoc()); 5855 return BaseFD; 5856 } 5857 5858 void Sema::ActOnFinishedFunctionDefinitionInOpenMPDeclareVariantScope( 5859 FunctionDecl *FD, FunctionDecl *BaseFD) { 5860 // Do not mark function as is used to prevent its emission if this is the 5861 // only place where it is used. 5862 EnterExpressionEvaluationContext Unevaluated( 5863 *this, Sema::ExpressionEvaluationContext::Unevaluated); 5864 5865 Expr *VariantFuncRef = DeclRefExpr::Create( 5866 Context, NestedNameSpecifierLoc(), SourceLocation(), FD, 5867 /* RefersToEnclosingVariableOrCapture */ false, 5868 /* NameLoc */ FD->getLocation(), FD->getType(), ExprValueKind::VK_RValue); 5869 5870 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back(); 5871 auto *OMPDeclareVariantA = OMPDeclareVariantAttr::CreateImplicit( 5872 Context, VariantFuncRef, DVScope.TI); 5873 BaseFD->addAttr(OMPDeclareVariantA); 5874 } 5875 5876 ExprResult Sema::ActOnOpenMPCall(ExprResult Call, Scope *Scope, 5877 SourceLocation LParenLoc, 5878 MultiExprArg ArgExprs, 5879 SourceLocation RParenLoc, Expr *ExecConfig) { 5880 // The common case is a regular call we do not want to specialize at all. Try 5881 // to make that case fast by bailing early. 5882 CallExpr *CE = dyn_cast<CallExpr>(Call.get()); 5883 if (!CE) 5884 return Call; 5885 5886 FunctionDecl *CalleeFnDecl = CE->getDirectCallee(); 5887 if (!CalleeFnDecl) 5888 return Call; 5889 5890 if (!CalleeFnDecl->hasAttr<OMPDeclareVariantAttr>()) 5891 return Call; 5892 5893 ASTContext &Context = getASTContext(); 5894 OMPContext OMPCtx(getLangOpts().OpenMPIsDevice, 5895 Context.getTargetInfo().getTriple()); 5896 5897 SmallVector<Expr *, 4> Exprs; 5898 SmallVector<VariantMatchInfo, 4> VMIs; 5899 while (CalleeFnDecl) { 5900 for (OMPDeclareVariantAttr *A : 5901 CalleeFnDecl->specific_attrs<OMPDeclareVariantAttr>()) { 5902 Expr *VariantRef = A->getVariantFuncRef(); 5903 5904 VariantMatchInfo VMI; 5905 OMPTraitInfo &TI = A->getTraitInfo(); 5906 TI.getAsVariantMatchInfo(Context, VMI); 5907 if (!isVariantApplicableInContext(VMI, OMPCtx, /* DeviceSetOnly */ false)) 5908 continue; 5909 5910 VMIs.push_back(VMI); 5911 Exprs.push_back(VariantRef); 5912 } 5913 5914 CalleeFnDecl = CalleeFnDecl->getPreviousDecl(); 5915 } 5916 5917 ExprResult NewCall; 5918 do { 5919 int BestIdx = getBestVariantMatchForContext(VMIs, OMPCtx); 5920 if (BestIdx < 0) 5921 return Call; 5922 Expr *BestExpr = cast<DeclRefExpr>(Exprs[BestIdx]); 5923 Decl *BestDecl = cast<DeclRefExpr>(BestExpr)->getDecl(); 5924 5925 { 5926 // Try to build a (member) call expression for the current best applicable 5927 // variant expression. We allow this to fail in which case we continue 5928 // with the next best variant expression. The fail case is part of the 5929 // implementation defined behavior in the OpenMP standard when it talks 5930 // about what differences in the function prototypes: "Any differences 5931 // that the specific OpenMP context requires in the prototype of the 5932 // variant from the base function prototype are implementation defined." 5933 // This wording is there to allow the specialized variant to have a 5934 // different type than the base function. This is intended and OK but if 5935 // we cannot create a call the difference is not in the "implementation 5936 // defined range" we allow. 5937 Sema::TentativeAnalysisScope Trap(*this); 5938 5939 if (auto *SpecializedMethod = dyn_cast<CXXMethodDecl>(BestDecl)) { 5940 auto *MemberCall = dyn_cast<CXXMemberCallExpr>(CE); 5941 BestExpr = MemberExpr::CreateImplicit( 5942 Context, MemberCall->getImplicitObjectArgument(), 5943 /* IsArrow */ false, SpecializedMethod, Context.BoundMemberTy, 5944 MemberCall->getValueKind(), MemberCall->getObjectKind()); 5945 } 5946 NewCall = BuildCallExpr(Scope, BestExpr, LParenLoc, ArgExprs, RParenLoc, 5947 ExecConfig); 5948 if (NewCall.isUsable()) 5949 break; 5950 } 5951 5952 VMIs.erase(VMIs.begin() + BestIdx); 5953 Exprs.erase(Exprs.begin() + BestIdx); 5954 } while (!VMIs.empty()); 5955 5956 if (!NewCall.isUsable()) 5957 return Call; 5958 return PseudoObjectExpr::Create(Context, CE, {NewCall.get()}, 0); 5959 } 5960 5961 Optional<std::pair<FunctionDecl *, Expr *>> 5962 Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG, 5963 Expr *VariantRef, OMPTraitInfo &TI, 5964 SourceRange SR) { 5965 if (!DG || DG.get().isNull()) 5966 return None; 5967 5968 const int VariantId = 1; 5969 // Must be applied only to single decl. 5970 if (!DG.get().isSingleDecl()) { 5971 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant) 5972 << VariantId << SR; 5973 return None; 5974 } 5975 Decl *ADecl = DG.get().getSingleDecl(); 5976 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl)) 5977 ADecl = FTD->getTemplatedDecl(); 5978 5979 // Decl must be a function. 5980 auto *FD = dyn_cast<FunctionDecl>(ADecl); 5981 if (!FD) { 5982 Diag(ADecl->getLocation(), diag::err_omp_function_expected) 5983 << VariantId << SR; 5984 return None; 5985 } 5986 5987 auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) { 5988 return FD->hasAttrs() && 5989 (FD->hasAttr<CPUDispatchAttr>() || FD->hasAttr<CPUSpecificAttr>() || 5990 FD->hasAttr<TargetAttr>()); 5991 }; 5992 // OpenMP is not compatible with CPU-specific attributes. 5993 if (HasMultiVersionAttributes(FD)) { 5994 Diag(FD->getLocation(), diag::err_omp_declare_variant_incompat_attributes) 5995 << SR; 5996 return None; 5997 } 5998 5999 // Allow #pragma omp declare variant only if the function is not used. 6000 if (FD->isUsed(false)) 6001 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_used) 6002 << FD->getLocation(); 6003 6004 // Check if the function was emitted already. 6005 const FunctionDecl *Definition; 6006 if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) && 6007 (LangOpts.EmitAllDecls || Context.DeclMustBeEmitted(Definition))) 6008 Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_emitted) 6009 << FD->getLocation(); 6010 6011 // The VariantRef must point to function. 6012 if (!VariantRef) { 6013 Diag(SR.getBegin(), diag::err_omp_function_expected) << VariantId; 6014 return None; 6015 } 6016 6017 auto ShouldDelayChecks = [](Expr *&E, bool) { 6018 return E && (E->isTypeDependent() || E->isValueDependent() || 6019 E->containsUnexpandedParameterPack() || 6020 E->isInstantiationDependent()); 6021 }; 6022 // Do not check templates, wait until instantiation. 6023 if (FD->isDependentContext() || ShouldDelayChecks(VariantRef, false) || 6024 TI.anyScoreOrCondition(ShouldDelayChecks)) 6025 return std::make_pair(FD, VariantRef); 6026 6027 // Deal with non-constant score and user condition expressions. 6028 auto HandleNonConstantScoresAndConditions = [this](Expr *&E, 6029 bool IsScore) -> bool { 6030 llvm::APSInt Result; 6031 if (!E || E->isIntegerConstantExpr(Result, Context)) 6032 return false; 6033 6034 if (IsScore) { 6035 // We warn on non-constant scores and pretend they were not present. 6036 Diag(E->getExprLoc(), diag::warn_omp_declare_variant_score_not_constant) 6037 << E; 6038 E = nullptr; 6039 } else { 6040 // We could replace a non-constant user condition with "false" but we 6041 // will soon need to handle these anyway for the dynamic version of 6042 // OpenMP context selectors. 6043 Diag(E->getExprLoc(), 6044 diag::err_omp_declare_variant_user_condition_not_constant) 6045 << E; 6046 } 6047 return true; 6048 }; 6049 if (TI.anyScoreOrCondition(HandleNonConstantScoresAndConditions)) 6050 return None; 6051 6052 // Convert VariantRef expression to the type of the original function to 6053 // resolve possible conflicts. 6054 ExprResult VariantRefCast; 6055 if (LangOpts.CPlusPlus) { 6056 QualType FnPtrType; 6057 auto *Method = dyn_cast<CXXMethodDecl>(FD); 6058 if (Method && !Method->isStatic()) { 6059 const Type *ClassType = 6060 Context.getTypeDeclType(Method->getParent()).getTypePtr(); 6061 FnPtrType = Context.getMemberPointerType(FD->getType(), ClassType); 6062 ExprResult ER; 6063 { 6064 // Build adrr_of unary op to correctly handle type checks for member 6065 // functions. 6066 Sema::TentativeAnalysisScope Trap(*this); 6067 ER = CreateBuiltinUnaryOp(VariantRef->getBeginLoc(), UO_AddrOf, 6068 VariantRef); 6069 } 6070 if (!ER.isUsable()) { 6071 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6072 << VariantId << VariantRef->getSourceRange(); 6073 return None; 6074 } 6075 VariantRef = ER.get(); 6076 } else { 6077 FnPtrType = Context.getPointerType(FD->getType()); 6078 } 6079 ImplicitConversionSequence ICS = 6080 TryImplicitConversion(VariantRef, FnPtrType.getUnqualifiedType(), 6081 /*SuppressUserConversions=*/false, 6082 AllowedExplicit::None, 6083 /*InOverloadResolution=*/false, 6084 /*CStyle=*/false, 6085 /*AllowObjCWritebackConversion=*/false); 6086 if (ICS.isFailure()) { 6087 Diag(VariantRef->getExprLoc(), 6088 diag::err_omp_declare_variant_incompat_types) 6089 << VariantRef->getType() 6090 << ((Method && !Method->isStatic()) ? FnPtrType : FD->getType()) 6091 << VariantRef->getSourceRange(); 6092 return None; 6093 } 6094 VariantRefCast = PerformImplicitConversion( 6095 VariantRef, FnPtrType.getUnqualifiedType(), AA_Converting); 6096 if (!VariantRefCast.isUsable()) 6097 return None; 6098 // Drop previously built artificial addr_of unary op for member functions. 6099 if (Method && !Method->isStatic()) { 6100 Expr *PossibleAddrOfVariantRef = VariantRefCast.get(); 6101 if (auto *UO = dyn_cast<UnaryOperator>( 6102 PossibleAddrOfVariantRef->IgnoreImplicit())) 6103 VariantRefCast = UO->getSubExpr(); 6104 } 6105 } else { 6106 VariantRefCast = VariantRef; 6107 } 6108 6109 ExprResult ER = CheckPlaceholderExpr(VariantRefCast.get()); 6110 if (!ER.isUsable() || 6111 !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) { 6112 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6113 << VariantId << VariantRef->getSourceRange(); 6114 return None; 6115 } 6116 6117 // The VariantRef must point to function. 6118 auto *DRE = dyn_cast<DeclRefExpr>(ER.get()->IgnoreParenImpCasts()); 6119 if (!DRE) { 6120 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6121 << VariantId << VariantRef->getSourceRange(); 6122 return None; 6123 } 6124 auto *NewFD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl()); 6125 if (!NewFD) { 6126 Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected) 6127 << VariantId << VariantRef->getSourceRange(); 6128 return None; 6129 } 6130 6131 // Check if function types are compatible in C. 6132 if (!LangOpts.CPlusPlus) { 6133 QualType NewType = 6134 Context.mergeFunctionTypes(FD->getType(), NewFD->getType()); 6135 if (NewType.isNull()) { 6136 Diag(VariantRef->getExprLoc(), 6137 diag::err_omp_declare_variant_incompat_types) 6138 << NewFD->getType() << FD->getType() << VariantRef->getSourceRange(); 6139 return None; 6140 } 6141 if (NewType->isFunctionProtoType()) { 6142 if (FD->getType()->isFunctionNoProtoType()) 6143 setPrototype(*this, FD, NewFD, NewType); 6144 else if (NewFD->getType()->isFunctionNoProtoType()) 6145 setPrototype(*this, NewFD, FD, NewType); 6146 } 6147 } 6148 6149 // Check if variant function is not marked with declare variant directive. 6150 if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) { 6151 Diag(VariantRef->getExprLoc(), 6152 diag::warn_omp_declare_variant_marked_as_declare_variant) 6153 << VariantRef->getSourceRange(); 6154 SourceRange SR = 6155 NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange(); 6156 Diag(SR.getBegin(), diag::note_omp_marked_declare_variant_here) << SR; 6157 return None; 6158 } 6159 6160 enum DoesntSupport { 6161 VirtFuncs = 1, 6162 Constructors = 3, 6163 Destructors = 4, 6164 DeletedFuncs = 5, 6165 DefaultedFuncs = 6, 6166 ConstexprFuncs = 7, 6167 ConstevalFuncs = 8, 6168 }; 6169 if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) { 6170 if (CXXFD->isVirtual()) { 6171 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6172 << VirtFuncs; 6173 return None; 6174 } 6175 6176 if (isa<CXXConstructorDecl>(FD)) { 6177 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6178 << Constructors; 6179 return None; 6180 } 6181 6182 if (isa<CXXDestructorDecl>(FD)) { 6183 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6184 << Destructors; 6185 return None; 6186 } 6187 } 6188 6189 if (FD->isDeleted()) { 6190 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6191 << DeletedFuncs; 6192 return None; 6193 } 6194 6195 if (FD->isDefaulted()) { 6196 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6197 << DefaultedFuncs; 6198 return None; 6199 } 6200 6201 if (FD->isConstexpr()) { 6202 Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support) 6203 << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs); 6204 return None; 6205 } 6206 6207 // Check general compatibility. 6208 if (areMultiversionVariantFunctionsCompatible( 6209 FD, NewFD, PartialDiagnostic::NullDiagnostic(), 6210 PartialDiagnosticAt(SourceLocation(), 6211 PartialDiagnostic::NullDiagnostic()), 6212 PartialDiagnosticAt( 6213 VariantRef->getExprLoc(), 6214 PDiag(diag::err_omp_declare_variant_doesnt_support)), 6215 PartialDiagnosticAt(VariantRef->getExprLoc(), 6216 PDiag(diag::err_omp_declare_variant_diff) 6217 << FD->getLocation()), 6218 /*TemplatesSupported=*/true, /*ConstexprSupported=*/false, 6219 /*CLinkageMayDiffer=*/true)) 6220 return None; 6221 return std::make_pair(FD, cast<Expr>(DRE)); 6222 } 6223 6224 void Sema::ActOnOpenMPDeclareVariantDirective(FunctionDecl *FD, 6225 Expr *VariantRef, 6226 OMPTraitInfo &TI, 6227 SourceRange SR) { 6228 auto *NewAttr = 6229 OMPDeclareVariantAttr::CreateImplicit(Context, VariantRef, &TI, SR); 6230 FD->addAttr(NewAttr); 6231 } 6232 6233 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 6234 Stmt *AStmt, 6235 SourceLocation StartLoc, 6236 SourceLocation EndLoc) { 6237 if (!AStmt) 6238 return StmtError(); 6239 6240 auto *CS = cast<CapturedStmt>(AStmt); 6241 // 1.2.2 OpenMP Language Terminology 6242 // Structured block - An executable statement with a single entry at the 6243 // top and a single exit at the bottom. 6244 // The point of exit cannot be a branch out of the structured block. 6245 // longjmp() and throw() must not violate the entry/exit criteria. 6246 CS->getCapturedDecl()->setNothrow(); 6247 6248 setFunctionHasBranchProtectedScope(); 6249 6250 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 6251 DSAStack->getTaskgroupReductionRef(), 6252 DSAStack->isCancelRegion()); 6253 } 6254 6255 namespace { 6256 /// Iteration space of a single for loop. 6257 struct LoopIterationSpace final { 6258 /// True if the condition operator is the strict compare operator (<, > or 6259 /// !=). 6260 bool IsStrictCompare = false; 6261 /// Condition of the loop. 6262 Expr *PreCond = nullptr; 6263 /// This expression calculates the number of iterations in the loop. 6264 /// It is always possible to calculate it before starting the loop. 6265 Expr *NumIterations = nullptr; 6266 /// The loop counter variable. 6267 Expr *CounterVar = nullptr; 6268 /// Private loop counter variable. 6269 Expr *PrivateCounterVar = nullptr; 6270 /// This is initializer for the initial value of #CounterVar. 6271 Expr *CounterInit = nullptr; 6272 /// This is step for the #CounterVar used to generate its update: 6273 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration. 6274 Expr *CounterStep = nullptr; 6275 /// Should step be subtracted? 6276 bool Subtract = false; 6277 /// Source range of the loop init. 6278 SourceRange InitSrcRange; 6279 /// Source range of the loop condition. 6280 SourceRange CondSrcRange; 6281 /// Source range of the loop increment. 6282 SourceRange IncSrcRange; 6283 /// Minimum value that can have the loop control variable. Used to support 6284 /// non-rectangular loops. Applied only for LCV with the non-iterator types, 6285 /// since only such variables can be used in non-loop invariant expressions. 6286 Expr *MinValue = nullptr; 6287 /// Maximum value that can have the loop control variable. Used to support 6288 /// non-rectangular loops. Applied only for LCV with the non-iterator type, 6289 /// since only such variables can be used in non-loop invariant expressions. 6290 Expr *MaxValue = nullptr; 6291 /// true, if the lower bound depends on the outer loop control var. 6292 bool IsNonRectangularLB = false; 6293 /// true, if the upper bound depends on the outer loop control var. 6294 bool IsNonRectangularUB = false; 6295 /// Index of the loop this loop depends on and forms non-rectangular loop 6296 /// nest. 6297 unsigned LoopDependentIdx = 0; 6298 /// Final condition for the non-rectangular loop nest support. It is used to 6299 /// check that the number of iterations for this particular counter must be 6300 /// finished. 6301 Expr *FinalCondition = nullptr; 6302 }; 6303 6304 /// Helper class for checking canonical form of the OpenMP loops and 6305 /// extracting iteration space of each loop in the loop nest, that will be used 6306 /// for IR generation. 6307 class OpenMPIterationSpaceChecker { 6308 /// Reference to Sema. 6309 Sema &SemaRef; 6310 /// Data-sharing stack. 6311 DSAStackTy &Stack; 6312 /// A location for diagnostics (when there is no some better location). 6313 SourceLocation DefaultLoc; 6314 /// A location for diagnostics (when increment is not compatible). 6315 SourceLocation ConditionLoc; 6316 /// A source location for referring to loop init later. 6317 SourceRange InitSrcRange; 6318 /// A source location for referring to condition later. 6319 SourceRange ConditionSrcRange; 6320 /// A source location for referring to increment later. 6321 SourceRange IncrementSrcRange; 6322 /// Loop variable. 6323 ValueDecl *LCDecl = nullptr; 6324 /// Reference to loop variable. 6325 Expr *LCRef = nullptr; 6326 /// Lower bound (initializer for the var). 6327 Expr *LB = nullptr; 6328 /// Upper bound. 6329 Expr *UB = nullptr; 6330 /// Loop step (increment). 6331 Expr *Step = nullptr; 6332 /// This flag is true when condition is one of: 6333 /// Var < UB 6334 /// Var <= UB 6335 /// UB > Var 6336 /// UB >= Var 6337 /// This will have no value when the condition is != 6338 llvm::Optional<bool> TestIsLessOp; 6339 /// This flag is true when condition is strict ( < or > ). 6340 bool TestIsStrictOp = false; 6341 /// This flag is true when step is subtracted on each iteration. 6342 bool SubtractStep = false; 6343 /// The outer loop counter this loop depends on (if any). 6344 const ValueDecl *DepDecl = nullptr; 6345 /// Contains number of loop (starts from 1) on which loop counter init 6346 /// expression of this loop depends on. 6347 Optional<unsigned> InitDependOnLC; 6348 /// Contains number of loop (starts from 1) on which loop counter condition 6349 /// expression of this loop depends on. 6350 Optional<unsigned> CondDependOnLC; 6351 /// Checks if the provide statement depends on the loop counter. 6352 Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer); 6353 /// Original condition required for checking of the exit condition for 6354 /// non-rectangular loop. 6355 Expr *Condition = nullptr; 6356 6357 public: 6358 OpenMPIterationSpaceChecker(Sema &SemaRef, DSAStackTy &Stack, 6359 SourceLocation DefaultLoc) 6360 : SemaRef(SemaRef), Stack(Stack), DefaultLoc(DefaultLoc), 6361 ConditionLoc(DefaultLoc) {} 6362 /// Check init-expr for canonical loop form and save loop counter 6363 /// variable - #Var and its initialization value - #LB. 6364 bool checkAndSetInit(Stmt *S, bool EmitDiags = true); 6365 /// Check test-expr for canonical form, save upper-bound (#UB), flags 6366 /// for less/greater and for strict/non-strict comparison. 6367 bool checkAndSetCond(Expr *S); 6368 /// Check incr-expr for canonical loop form and return true if it 6369 /// does not conform, otherwise save loop step (#Step). 6370 bool checkAndSetInc(Expr *S); 6371 /// Return the loop counter variable. 6372 ValueDecl *getLoopDecl() const { return LCDecl; } 6373 /// Return the reference expression to loop counter variable. 6374 Expr *getLoopDeclRefExpr() const { return LCRef; } 6375 /// Source range of the loop init. 6376 SourceRange getInitSrcRange() const { return InitSrcRange; } 6377 /// Source range of the loop condition. 6378 SourceRange getConditionSrcRange() const { return ConditionSrcRange; } 6379 /// Source range of the loop increment. 6380 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; } 6381 /// True if the step should be subtracted. 6382 bool shouldSubtractStep() const { return SubtractStep; } 6383 /// True, if the compare operator is strict (<, > or !=). 6384 bool isStrictTestOp() const { return TestIsStrictOp; } 6385 /// Build the expression to calculate the number of iterations. 6386 Expr *buildNumIterations( 6387 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 6388 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 6389 /// Build the precondition expression for the loops. 6390 Expr * 6391 buildPreCond(Scope *S, Expr *Cond, 6392 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 6393 /// Build reference expression to the counter be used for codegen. 6394 DeclRefExpr * 6395 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 6396 DSAStackTy &DSA) const; 6397 /// Build reference expression to the private counter be used for 6398 /// codegen. 6399 Expr *buildPrivateCounterVar() const; 6400 /// Build initialization of the counter be used for codegen. 6401 Expr *buildCounterInit() const; 6402 /// Build step of the counter be used for codegen. 6403 Expr *buildCounterStep() const; 6404 /// Build loop data with counter value for depend clauses in ordered 6405 /// directives. 6406 Expr * 6407 buildOrderedLoopData(Scope *S, Expr *Counter, 6408 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 6409 SourceLocation Loc, Expr *Inc = nullptr, 6410 OverloadedOperatorKind OOK = OO_Amp); 6411 /// Builds the minimum value for the loop counter. 6412 std::pair<Expr *, Expr *> buildMinMaxValues( 6413 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const; 6414 /// Builds final condition for the non-rectangular loops. 6415 Expr *buildFinalCondition(Scope *S) const; 6416 /// Return true if any expression is dependent. 6417 bool dependent() const; 6418 /// Returns true if the initializer forms non-rectangular loop. 6419 bool doesInitDependOnLC() const { return InitDependOnLC.hasValue(); } 6420 /// Returns true if the condition forms non-rectangular loop. 6421 bool doesCondDependOnLC() const { return CondDependOnLC.hasValue(); } 6422 /// Returns index of the loop we depend on (starting from 1), or 0 otherwise. 6423 unsigned getLoopDependentIdx() const { 6424 return InitDependOnLC.getValueOr(CondDependOnLC.getValueOr(0)); 6425 } 6426 6427 private: 6428 /// Check the right-hand side of an assignment in the increment 6429 /// expression. 6430 bool checkAndSetIncRHS(Expr *RHS); 6431 /// Helper to set loop counter variable and its initializer. 6432 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB, 6433 bool EmitDiags); 6434 /// Helper to set upper bound. 6435 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp, 6436 SourceRange SR, SourceLocation SL); 6437 /// Helper to set loop increment. 6438 bool setStep(Expr *NewStep, bool Subtract); 6439 }; 6440 6441 bool OpenMPIterationSpaceChecker::dependent() const { 6442 if (!LCDecl) { 6443 assert(!LB && !UB && !Step); 6444 return false; 6445 } 6446 return LCDecl->getType()->isDependentType() || 6447 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) || 6448 (Step && Step->isValueDependent()); 6449 } 6450 6451 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl, 6452 Expr *NewLCRefExpr, 6453 Expr *NewLB, bool EmitDiags) { 6454 // State consistency checking to ensure correct usage. 6455 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr && 6456 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 6457 if (!NewLCDecl || !NewLB) 6458 return true; 6459 LCDecl = getCanonicalDecl(NewLCDecl); 6460 LCRef = NewLCRefExpr; 6461 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB)) 6462 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 6463 if ((Ctor->isCopyOrMoveConstructor() || 6464 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 6465 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 6466 NewLB = CE->getArg(0)->IgnoreParenImpCasts(); 6467 LB = NewLB; 6468 if (EmitDiags) 6469 InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true); 6470 return false; 6471 } 6472 6473 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, 6474 llvm::Optional<bool> LessOp, 6475 bool StrictOp, SourceRange SR, 6476 SourceLocation SL) { 6477 // State consistency checking to ensure correct usage. 6478 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr && 6479 Step == nullptr && !TestIsLessOp && !TestIsStrictOp); 6480 if (!NewUB) 6481 return true; 6482 UB = NewUB; 6483 if (LessOp) 6484 TestIsLessOp = LessOp; 6485 TestIsStrictOp = StrictOp; 6486 ConditionSrcRange = SR; 6487 ConditionLoc = SL; 6488 CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false); 6489 return false; 6490 } 6491 6492 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) { 6493 // State consistency checking to ensure correct usage. 6494 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr); 6495 if (!NewStep) 6496 return true; 6497 if (!NewStep->isValueDependent()) { 6498 // Check that the step is integer expression. 6499 SourceLocation StepLoc = NewStep->getBeginLoc(); 6500 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion( 6501 StepLoc, getExprAsWritten(NewStep)); 6502 if (Val.isInvalid()) 6503 return true; 6504 NewStep = Val.get(); 6505 6506 // OpenMP [2.6, Canonical Loop Form, Restrictions] 6507 // If test-expr is of form var relational-op b and relational-op is < or 6508 // <= then incr-expr must cause var to increase on each iteration of the 6509 // loop. If test-expr is of form var relational-op b and relational-op is 6510 // > or >= then incr-expr must cause var to decrease on each iteration of 6511 // the loop. 6512 // If test-expr is of form b relational-op var and relational-op is < or 6513 // <= then incr-expr must cause var to decrease on each iteration of the 6514 // loop. If test-expr is of form b relational-op var and relational-op is 6515 // > or >= then incr-expr must cause var to increase on each iteration of 6516 // the loop. 6517 llvm::APSInt Result; 6518 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); 6519 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 6520 bool IsConstNeg = 6521 IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); 6522 bool IsConstPos = 6523 IsConstant && Result.isSigned() && (Subtract == Result.isNegative()); 6524 bool IsConstZero = IsConstant && !Result.getBoolValue(); 6525 6526 // != with increment is treated as <; != with decrement is treated as > 6527 if (!TestIsLessOp.hasValue()) 6528 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract); 6529 if (UB && (IsConstZero || 6530 (TestIsLessOp.getValue() ? 6531 (IsConstNeg || (IsUnsigned && Subtract)) : 6532 (IsConstPos || (IsUnsigned && !Subtract))))) { 6533 SemaRef.Diag(NewStep->getExprLoc(), 6534 diag::err_omp_loop_incr_not_compatible) 6535 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange(); 6536 SemaRef.Diag(ConditionLoc, 6537 diag::note_omp_loop_cond_requres_compatible_incr) 6538 << TestIsLessOp.getValue() << ConditionSrcRange; 6539 return true; 6540 } 6541 if (TestIsLessOp.getValue() == Subtract) { 6542 NewStep = 6543 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep) 6544 .get(); 6545 Subtract = !Subtract; 6546 } 6547 } 6548 6549 Step = NewStep; 6550 SubtractStep = Subtract; 6551 return false; 6552 } 6553 6554 namespace { 6555 /// Checker for the non-rectangular loops. Checks if the initializer or 6556 /// condition expression references loop counter variable. 6557 class LoopCounterRefChecker final 6558 : public ConstStmtVisitor<LoopCounterRefChecker, bool> { 6559 Sema &SemaRef; 6560 DSAStackTy &Stack; 6561 const ValueDecl *CurLCDecl = nullptr; 6562 const ValueDecl *DepDecl = nullptr; 6563 const ValueDecl *PrevDepDecl = nullptr; 6564 bool IsInitializer = true; 6565 unsigned BaseLoopId = 0; 6566 bool checkDecl(const Expr *E, const ValueDecl *VD) { 6567 if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) { 6568 SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter) 6569 << (IsInitializer ? 0 : 1); 6570 return false; 6571 } 6572 const auto &&Data = Stack.isLoopControlVariable(VD); 6573 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions. 6574 // The type of the loop iterator on which we depend may not have a random 6575 // access iterator type. 6576 if (Data.first && VD->getType()->isRecordType()) { 6577 SmallString<128> Name; 6578 llvm::raw_svector_ostream OS(Name); 6579 VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 6580 /*Qualified=*/true); 6581 SemaRef.Diag(E->getExprLoc(), 6582 diag::err_omp_wrong_dependency_iterator_type) 6583 << OS.str(); 6584 SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD; 6585 return false; 6586 } 6587 if (Data.first && 6588 (DepDecl || (PrevDepDecl && 6589 getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) { 6590 if (!DepDecl && PrevDepDecl) 6591 DepDecl = PrevDepDecl; 6592 SmallString<128> Name; 6593 llvm::raw_svector_ostream OS(Name); 6594 DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(), 6595 /*Qualified=*/true); 6596 SemaRef.Diag(E->getExprLoc(), 6597 diag::err_omp_invariant_or_linear_dependency) 6598 << OS.str(); 6599 return false; 6600 } 6601 if (Data.first) { 6602 DepDecl = VD; 6603 BaseLoopId = Data.first; 6604 } 6605 return Data.first; 6606 } 6607 6608 public: 6609 bool VisitDeclRefExpr(const DeclRefExpr *E) { 6610 const ValueDecl *VD = E->getDecl(); 6611 if (isa<VarDecl>(VD)) 6612 return checkDecl(E, VD); 6613 return false; 6614 } 6615 bool VisitMemberExpr(const MemberExpr *E) { 6616 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) { 6617 const ValueDecl *VD = E->getMemberDecl(); 6618 if (isa<VarDecl>(VD) || isa<FieldDecl>(VD)) 6619 return checkDecl(E, VD); 6620 } 6621 return false; 6622 } 6623 bool VisitStmt(const Stmt *S) { 6624 bool Res = false; 6625 for (const Stmt *Child : S->children()) 6626 Res = (Child && Visit(Child)) || Res; 6627 return Res; 6628 } 6629 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack, 6630 const ValueDecl *CurLCDecl, bool IsInitializer, 6631 const ValueDecl *PrevDepDecl = nullptr) 6632 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl), 6633 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer) {} 6634 unsigned getBaseLoopId() const { 6635 assert(CurLCDecl && "Expected loop dependency."); 6636 return BaseLoopId; 6637 } 6638 const ValueDecl *getDepDecl() const { 6639 assert(CurLCDecl && "Expected loop dependency."); 6640 return DepDecl; 6641 } 6642 }; 6643 } // namespace 6644 6645 Optional<unsigned> 6646 OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S, 6647 bool IsInitializer) { 6648 // Check for the non-rectangular loops. 6649 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer, 6650 DepDecl); 6651 if (LoopStmtChecker.Visit(S)) { 6652 DepDecl = LoopStmtChecker.getDepDecl(); 6653 return LoopStmtChecker.getBaseLoopId(); 6654 } 6655 return llvm::None; 6656 } 6657 6658 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) { 6659 // Check init-expr for canonical loop form and save loop counter 6660 // variable - #Var and its initialization value - #LB. 6661 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 6662 // var = lb 6663 // integer-type var = lb 6664 // random-access-iterator-type var = lb 6665 // pointer-type var = lb 6666 // 6667 if (!S) { 6668 if (EmitDiags) { 6669 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 6670 } 6671 return true; 6672 } 6673 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 6674 if (!ExprTemp->cleanupsHaveSideEffects()) 6675 S = ExprTemp->getSubExpr(); 6676 6677 InitSrcRange = S->getSourceRange(); 6678 if (Expr *E = dyn_cast<Expr>(S)) 6679 S = E->IgnoreParens(); 6680 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 6681 if (BO->getOpcode() == BO_Assign) { 6682 Expr *LHS = BO->getLHS()->IgnoreParens(); 6683 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 6684 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 6685 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 6686 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6687 EmitDiags); 6688 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags); 6689 } 6690 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 6691 if (ME->isArrow() && 6692 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 6693 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6694 EmitDiags); 6695 } 6696 } 6697 } else if (auto *DS = dyn_cast<DeclStmt>(S)) { 6698 if (DS->isSingleDecl()) { 6699 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 6700 if (Var->hasInit() && !Var->getType()->isReferenceType()) { 6701 // Accept non-canonical init form here but emit ext. warning. 6702 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags) 6703 SemaRef.Diag(S->getBeginLoc(), 6704 diag::ext_omp_loop_not_canonical_init) 6705 << S->getSourceRange(); 6706 return setLCDeclAndLB( 6707 Var, 6708 buildDeclRefExpr(SemaRef, Var, 6709 Var->getType().getNonReferenceType(), 6710 DS->getBeginLoc()), 6711 Var->getInit(), EmitDiags); 6712 } 6713 } 6714 } 6715 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 6716 if (CE->getOperator() == OO_Equal) { 6717 Expr *LHS = CE->getArg(0); 6718 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) { 6719 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) 6720 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit()))) 6721 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6722 EmitDiags); 6723 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags); 6724 } 6725 if (auto *ME = dyn_cast<MemberExpr>(LHS)) { 6726 if (ME->isArrow() && 6727 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 6728 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(), 6729 EmitDiags); 6730 } 6731 } 6732 } 6733 6734 if (dependent() || SemaRef.CurContext->isDependentContext()) 6735 return false; 6736 if (EmitDiags) { 6737 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init) 6738 << S->getSourceRange(); 6739 } 6740 return true; 6741 } 6742 6743 /// Ignore parenthesizes, implicit casts, copy constructor and return the 6744 /// variable (which may be the loop variable) if possible. 6745 static const ValueDecl *getInitLCDecl(const Expr *E) { 6746 if (!E) 6747 return nullptr; 6748 E = getExprAsWritten(E); 6749 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 6750 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 6751 if ((Ctor->isCopyOrMoveConstructor() || 6752 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) && 6753 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr) 6754 E = CE->getArg(0)->IgnoreParenImpCasts(); 6755 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) { 6756 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 6757 return getCanonicalDecl(VD); 6758 } 6759 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E)) 6760 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())) 6761 return getCanonicalDecl(ME->getMemberDecl()); 6762 return nullptr; 6763 } 6764 6765 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) { 6766 // Check test-expr for canonical form, save upper-bound UB, flags for 6767 // less/greater and for strict/non-strict comparison. 6768 // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following: 6769 // var relational-op b 6770 // b relational-op var 6771 // 6772 bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50; 6773 if (!S) { 6774 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) 6775 << (IneqCondIsCanonical ? 1 : 0) << LCDecl; 6776 return true; 6777 } 6778 Condition = S; 6779 S = getExprAsWritten(S); 6780 SourceLocation CondLoc = S->getBeginLoc(); 6781 if (auto *BO = dyn_cast<BinaryOperator>(S)) { 6782 if (BO->isRelationalOp()) { 6783 if (getInitLCDecl(BO->getLHS()) == LCDecl) 6784 return setUB(BO->getRHS(), 6785 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 6786 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 6787 BO->getSourceRange(), BO->getOperatorLoc()); 6788 if (getInitLCDecl(BO->getRHS()) == LCDecl) 6789 return setUB(BO->getLHS(), 6790 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 6791 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 6792 BO->getSourceRange(), BO->getOperatorLoc()); 6793 } else if (IneqCondIsCanonical && BO->getOpcode() == BO_NE) 6794 return setUB( 6795 getInitLCDecl(BO->getLHS()) == LCDecl ? BO->getRHS() : BO->getLHS(), 6796 /*LessOp=*/llvm::None, 6797 /*StrictOp=*/true, BO->getSourceRange(), BO->getOperatorLoc()); 6798 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 6799 if (CE->getNumArgs() == 2) { 6800 auto Op = CE->getOperator(); 6801 switch (Op) { 6802 case OO_Greater: 6803 case OO_GreaterEqual: 6804 case OO_Less: 6805 case OO_LessEqual: 6806 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6807 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 6808 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 6809 CE->getOperatorLoc()); 6810 if (getInitLCDecl(CE->getArg(1)) == LCDecl) 6811 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 6812 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 6813 CE->getOperatorLoc()); 6814 break; 6815 case OO_ExclaimEqual: 6816 if (IneqCondIsCanonical) 6817 return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ? CE->getArg(1) 6818 : CE->getArg(0), 6819 /*LessOp=*/llvm::None, 6820 /*StrictOp=*/true, CE->getSourceRange(), 6821 CE->getOperatorLoc()); 6822 break; 6823 default: 6824 break; 6825 } 6826 } 6827 } 6828 if (dependent() || SemaRef.CurContext->isDependentContext()) 6829 return false; 6830 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 6831 << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl; 6832 return true; 6833 } 6834 6835 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) { 6836 // RHS of canonical loop form increment can be: 6837 // var + incr 6838 // incr + var 6839 // var - incr 6840 // 6841 RHS = RHS->IgnoreParenImpCasts(); 6842 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) { 6843 if (BO->isAdditiveOp()) { 6844 bool IsAdd = BO->getOpcode() == BO_Add; 6845 if (getInitLCDecl(BO->getLHS()) == LCDecl) 6846 return setStep(BO->getRHS(), !IsAdd); 6847 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl) 6848 return setStep(BO->getLHS(), /*Subtract=*/false); 6849 } 6850 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 6851 bool IsAdd = CE->getOperator() == OO_Plus; 6852 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 6853 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6854 return setStep(CE->getArg(1), !IsAdd); 6855 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl) 6856 return setStep(CE->getArg(0), /*Subtract=*/false); 6857 } 6858 } 6859 if (dependent() || SemaRef.CurContext->isDependentContext()) 6860 return false; 6861 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 6862 << RHS->getSourceRange() << LCDecl; 6863 return true; 6864 } 6865 6866 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) { 6867 // Check incr-expr for canonical loop form and return true if it 6868 // does not conform. 6869 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 6870 // ++var 6871 // var++ 6872 // --var 6873 // var-- 6874 // var += incr 6875 // var -= incr 6876 // var = var + incr 6877 // var = incr + var 6878 // var = var - incr 6879 // 6880 if (!S) { 6881 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl; 6882 return true; 6883 } 6884 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S)) 6885 if (!ExprTemp->cleanupsHaveSideEffects()) 6886 S = ExprTemp->getSubExpr(); 6887 6888 IncrementSrcRange = S->getSourceRange(); 6889 S = S->IgnoreParens(); 6890 if (auto *UO = dyn_cast<UnaryOperator>(S)) { 6891 if (UO->isIncrementDecrementOp() && 6892 getInitLCDecl(UO->getSubExpr()) == LCDecl) 6893 return setStep(SemaRef 6894 .ActOnIntegerConstant(UO->getBeginLoc(), 6895 (UO->isDecrementOp() ? -1 : 1)) 6896 .get(), 6897 /*Subtract=*/false); 6898 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) { 6899 switch (BO->getOpcode()) { 6900 case BO_AddAssign: 6901 case BO_SubAssign: 6902 if (getInitLCDecl(BO->getLHS()) == LCDecl) 6903 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 6904 break; 6905 case BO_Assign: 6906 if (getInitLCDecl(BO->getLHS()) == LCDecl) 6907 return checkAndSetIncRHS(BO->getRHS()); 6908 break; 6909 default: 6910 break; 6911 } 6912 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) { 6913 switch (CE->getOperator()) { 6914 case OO_PlusPlus: 6915 case OO_MinusMinus: 6916 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6917 return setStep(SemaRef 6918 .ActOnIntegerConstant( 6919 CE->getBeginLoc(), 6920 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)) 6921 .get(), 6922 /*Subtract=*/false); 6923 break; 6924 case OO_PlusEqual: 6925 case OO_MinusEqual: 6926 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6927 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 6928 break; 6929 case OO_Equal: 6930 if (getInitLCDecl(CE->getArg(0)) == LCDecl) 6931 return checkAndSetIncRHS(CE->getArg(1)); 6932 break; 6933 default: 6934 break; 6935 } 6936 } 6937 if (dependent() || SemaRef.CurContext->isDependentContext()) 6938 return false; 6939 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr) 6940 << S->getSourceRange() << LCDecl; 6941 return true; 6942 } 6943 6944 static ExprResult 6945 tryBuildCapture(Sema &SemaRef, Expr *Capture, 6946 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 6947 if (SemaRef.CurContext->isDependentContext() || Capture->containsErrors()) 6948 return Capture; 6949 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects)) 6950 return SemaRef.PerformImplicitConversion( 6951 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting, 6952 /*AllowExplicit=*/true); 6953 auto I = Captures.find(Capture); 6954 if (I != Captures.end()) 6955 return buildCapture(SemaRef, Capture, I->second); 6956 DeclRefExpr *Ref = nullptr; 6957 ExprResult Res = buildCapture(SemaRef, Capture, Ref); 6958 Captures[Capture] = Ref; 6959 return Res; 6960 } 6961 6962 /// Calculate number of iterations, transforming to unsigned, if number of 6963 /// iterations may be larger than the original type. 6964 static Expr * 6965 calculateNumIters(Sema &SemaRef, Scope *S, SourceLocation DefaultLoc, 6966 Expr *Lower, Expr *Upper, Expr *Step, QualType LCTy, 6967 bool TestIsStrictOp, bool RoundToStep, 6968 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 6969 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 6970 if (!NewStep.isUsable()) 6971 return nullptr; 6972 llvm::APSInt LRes, URes, SRes; 6973 bool IsLowerConst = Lower->isIntegerConstantExpr(LRes, SemaRef.Context); 6974 bool IsStepConst = Step->isIntegerConstantExpr(SRes, SemaRef.Context); 6975 bool NoNeedToConvert = IsLowerConst && !RoundToStep && 6976 ((!TestIsStrictOp && LRes.isNonNegative()) || 6977 (TestIsStrictOp && LRes.isStrictlyPositive())); 6978 bool NeedToReorganize = false; 6979 // Check if any subexpressions in Lower -Step [+ 1] lead to overflow. 6980 if (!NoNeedToConvert && IsLowerConst && 6981 (TestIsStrictOp || (RoundToStep && IsStepConst))) { 6982 NoNeedToConvert = true; 6983 if (RoundToStep) { 6984 unsigned BW = LRes.getBitWidth() > SRes.getBitWidth() 6985 ? LRes.getBitWidth() 6986 : SRes.getBitWidth(); 6987 LRes = LRes.extend(BW + 1); 6988 LRes.setIsSigned(true); 6989 SRes = SRes.extend(BW + 1); 6990 SRes.setIsSigned(true); 6991 LRes -= SRes; 6992 NoNeedToConvert = LRes.trunc(BW).extend(BW + 1) == LRes; 6993 LRes = LRes.trunc(BW); 6994 } 6995 if (TestIsStrictOp) { 6996 unsigned BW = LRes.getBitWidth(); 6997 LRes = LRes.extend(BW + 1); 6998 LRes.setIsSigned(true); 6999 ++LRes; 7000 NoNeedToConvert = 7001 NoNeedToConvert && LRes.trunc(BW).extend(BW + 1) == LRes; 7002 // truncate to the original bitwidth. 7003 LRes = LRes.trunc(BW); 7004 } 7005 NeedToReorganize = NoNeedToConvert; 7006 } 7007 bool IsUpperConst = Upper->isIntegerConstantExpr(URes, SemaRef.Context); 7008 if (NoNeedToConvert && IsLowerConst && IsUpperConst && 7009 (!RoundToStep || IsStepConst)) { 7010 unsigned BW = LRes.getBitWidth() > URes.getBitWidth() ? LRes.getBitWidth() 7011 : URes.getBitWidth(); 7012 LRes = LRes.extend(BW + 1); 7013 LRes.setIsSigned(true); 7014 URes = URes.extend(BW + 1); 7015 URes.setIsSigned(true); 7016 URes -= LRes; 7017 NoNeedToConvert = URes.trunc(BW).extend(BW + 1) == URes; 7018 NeedToReorganize = NoNeedToConvert; 7019 } 7020 // If the boundaries are not constant or (Lower - Step [+ 1]) is not constant 7021 // or less than zero (Upper - (Lower - Step [+ 1]) may overflow) - promote to 7022 // unsigned. 7023 if ((!NoNeedToConvert || (LRes.isNegative() && !IsUpperConst)) && 7024 !LCTy->isDependentType() && LCTy->isIntegerType()) { 7025 QualType LowerTy = Lower->getType(); 7026 QualType UpperTy = Upper->getType(); 7027 uint64_t LowerSize = SemaRef.Context.getTypeSize(LowerTy); 7028 uint64_t UpperSize = SemaRef.Context.getTypeSize(UpperTy); 7029 if ((LowerSize <= UpperSize && UpperTy->hasSignedIntegerRepresentation()) || 7030 (LowerSize > UpperSize && LowerTy->hasSignedIntegerRepresentation())) { 7031 QualType CastType = SemaRef.Context.getIntTypeForBitwidth( 7032 LowerSize > UpperSize ? LowerSize : UpperSize, /*Signed=*/0); 7033 Upper = 7034 SemaRef 7035 .PerformImplicitConversion( 7036 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 7037 CastType, Sema::AA_Converting) 7038 .get(); 7039 Lower = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(); 7040 NewStep = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, NewStep.get()); 7041 } 7042 } 7043 if (!Lower || !Upper || NewStep.isInvalid()) 7044 return nullptr; 7045 7046 ExprResult Diff; 7047 // If need to reorganize, then calculate the form as Upper - (Lower - Step [+ 7048 // 1]). 7049 if (NeedToReorganize) { 7050 Diff = Lower; 7051 7052 if (RoundToStep) { 7053 // Lower - Step 7054 Diff = 7055 SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Diff.get(), NewStep.get()); 7056 if (!Diff.isUsable()) 7057 return nullptr; 7058 } 7059 7060 // Lower - Step [+ 1] 7061 if (TestIsStrictOp) 7062 Diff = SemaRef.BuildBinOp( 7063 S, DefaultLoc, BO_Add, Diff.get(), 7064 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 7065 if (!Diff.isUsable()) 7066 return nullptr; 7067 7068 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7069 if (!Diff.isUsable()) 7070 return nullptr; 7071 7072 // Upper - (Lower - Step [+ 1]). 7073 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Diff.get()); 7074 if (!Diff.isUsable()) 7075 return nullptr; 7076 } else { 7077 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower); 7078 7079 if (!Diff.isUsable() && LCTy->getAsCXXRecordDecl()) { 7080 // BuildBinOp already emitted error, this one is to point user to upper 7081 // and lower bound, and to tell what is passed to 'operator-'. 7082 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx) 7083 << Upper->getSourceRange() << Lower->getSourceRange(); 7084 return nullptr; 7085 } 7086 7087 if (!Diff.isUsable()) 7088 return nullptr; 7089 7090 // Upper - Lower [- 1] 7091 if (TestIsStrictOp) 7092 Diff = SemaRef.BuildBinOp( 7093 S, DefaultLoc, BO_Sub, Diff.get(), 7094 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 7095 if (!Diff.isUsable()) 7096 return nullptr; 7097 7098 if (RoundToStep) { 7099 // Upper - Lower [- 1] + Step 7100 Diff = 7101 SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get()); 7102 if (!Diff.isUsable()) 7103 return nullptr; 7104 } 7105 } 7106 7107 // Parentheses (for dumping/debugging purposes only). 7108 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7109 if (!Diff.isUsable()) 7110 return nullptr; 7111 7112 // (Upper - Lower [- 1] + Step) / Step or (Upper - Lower) / Step 7113 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get()); 7114 if (!Diff.isUsable()) 7115 return nullptr; 7116 7117 return Diff.get(); 7118 } 7119 7120 /// Build the expression to calculate the number of iterations. 7121 Expr *OpenMPIterationSpaceChecker::buildNumIterations( 7122 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType, 7123 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 7124 QualType VarType = LCDecl->getType().getNonReferenceType(); 7125 if (!VarType->isIntegerType() && !VarType->isPointerType() && 7126 !SemaRef.getLangOpts().CPlusPlus) 7127 return nullptr; 7128 Expr *LBVal = LB; 7129 Expr *UBVal = UB; 7130 // LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) : 7131 // max(LB(MinVal), LB(MaxVal)) 7132 if (InitDependOnLC) { 7133 const LoopIterationSpace &IS = 7134 ResultIterSpaces[ResultIterSpaces.size() - 1 - 7135 InitDependOnLC.getValueOr( 7136 CondDependOnLC.getValueOr(0))]; 7137 if (!IS.MinValue || !IS.MaxValue) 7138 return nullptr; 7139 // OuterVar = Min 7140 ExprResult MinValue = 7141 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 7142 if (!MinValue.isUsable()) 7143 return nullptr; 7144 7145 ExprResult LBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 7146 IS.CounterVar, MinValue.get()); 7147 if (!LBMinVal.isUsable()) 7148 return nullptr; 7149 // OuterVar = Min, LBVal 7150 LBMinVal = 7151 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMinVal.get(), LBVal); 7152 if (!LBMinVal.isUsable()) 7153 return nullptr; 7154 // (OuterVar = Min, LBVal) 7155 LBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMinVal.get()); 7156 if (!LBMinVal.isUsable()) 7157 return nullptr; 7158 7159 // OuterVar = Max 7160 ExprResult MaxValue = 7161 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 7162 if (!MaxValue.isUsable()) 7163 return nullptr; 7164 7165 ExprResult LBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 7166 IS.CounterVar, MaxValue.get()); 7167 if (!LBMaxVal.isUsable()) 7168 return nullptr; 7169 // OuterVar = Max, LBVal 7170 LBMaxVal = 7171 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMaxVal.get(), LBVal); 7172 if (!LBMaxVal.isUsable()) 7173 return nullptr; 7174 // (OuterVar = Max, LBVal) 7175 LBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMaxVal.get()); 7176 if (!LBMaxVal.isUsable()) 7177 return nullptr; 7178 7179 Expr *LBMin = tryBuildCapture(SemaRef, LBMinVal.get(), Captures).get(); 7180 Expr *LBMax = tryBuildCapture(SemaRef, LBMaxVal.get(), Captures).get(); 7181 if (!LBMin || !LBMax) 7182 return nullptr; 7183 // LB(MinVal) < LB(MaxVal) 7184 ExprResult MinLessMaxRes = 7185 SemaRef.BuildBinOp(S, DefaultLoc, BO_LT, LBMin, LBMax); 7186 if (!MinLessMaxRes.isUsable()) 7187 return nullptr; 7188 Expr *MinLessMax = 7189 tryBuildCapture(SemaRef, MinLessMaxRes.get(), Captures).get(); 7190 if (!MinLessMax) 7191 return nullptr; 7192 if (TestIsLessOp.getValue()) { 7193 // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal), 7194 // LB(MaxVal)) 7195 ExprResult MinLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 7196 MinLessMax, LBMin, LBMax); 7197 if (!MinLB.isUsable()) 7198 return nullptr; 7199 LBVal = MinLB.get(); 7200 } else { 7201 // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal), 7202 // LB(MaxVal)) 7203 ExprResult MaxLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc, 7204 MinLessMax, LBMax, LBMin); 7205 if (!MaxLB.isUsable()) 7206 return nullptr; 7207 LBVal = MaxLB.get(); 7208 } 7209 } 7210 // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) : 7211 // min(UB(MinVal), UB(MaxVal)) 7212 if (CondDependOnLC) { 7213 const LoopIterationSpace &IS = 7214 ResultIterSpaces[ResultIterSpaces.size() - 1 - 7215 InitDependOnLC.getValueOr( 7216 CondDependOnLC.getValueOr(0))]; 7217 if (!IS.MinValue || !IS.MaxValue) 7218 return nullptr; 7219 // OuterVar = Min 7220 ExprResult MinValue = 7221 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue); 7222 if (!MinValue.isUsable()) 7223 return nullptr; 7224 7225 ExprResult UBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 7226 IS.CounterVar, MinValue.get()); 7227 if (!UBMinVal.isUsable()) 7228 return nullptr; 7229 // OuterVar = Min, UBVal 7230 UBMinVal = 7231 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMinVal.get(), UBVal); 7232 if (!UBMinVal.isUsable()) 7233 return nullptr; 7234 // (OuterVar = Min, UBVal) 7235 UBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMinVal.get()); 7236 if (!UBMinVal.isUsable()) 7237 return nullptr; 7238 7239 // OuterVar = Max 7240 ExprResult MaxValue = 7241 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue); 7242 if (!MaxValue.isUsable()) 7243 return nullptr; 7244 7245 ExprResult UBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign, 7246 IS.CounterVar, MaxValue.get()); 7247 if (!UBMaxVal.isUsable()) 7248 return nullptr; 7249 // OuterVar = Max, UBVal 7250 UBMaxVal = 7251 SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMaxVal.get(), UBVal); 7252 if (!UBMaxVal.isUsable()) 7253 return nullptr; 7254 // (OuterVar = Max, UBVal) 7255 UBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMaxVal.get()); 7256 if (!UBMaxVal.isUsable()) 7257 return nullptr; 7258 7259 Expr *UBMin = tryBuildCapture(SemaRef, UBMinVal.get(), Captures).get(); 7260 Expr *UBMax = tryBuildCapture(SemaRef, UBMaxVal.get(), Captures).get(); 7261 if (!UBMin || !UBMax) 7262 return nullptr; 7263 // UB(MinVal) > UB(MaxVal) 7264 ExprResult MinGreaterMaxRes = 7265 SemaRef.BuildBinOp(S, DefaultLoc, BO_GT, UBMin, UBMax); 7266 if (!MinGreaterMaxRes.isUsable()) 7267 return nullptr; 7268 Expr *MinGreaterMax = 7269 tryBuildCapture(SemaRef, MinGreaterMaxRes.get(), Captures).get(); 7270 if (!MinGreaterMax) 7271 return nullptr; 7272 if (TestIsLessOp.getValue()) { 7273 // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal), 7274 // UB(MaxVal)) 7275 ExprResult MaxUB = SemaRef.ActOnConditionalOp( 7276 DefaultLoc, DefaultLoc, MinGreaterMax, UBMin, UBMax); 7277 if (!MaxUB.isUsable()) 7278 return nullptr; 7279 UBVal = MaxUB.get(); 7280 } else { 7281 // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal), 7282 // UB(MaxVal)) 7283 ExprResult MinUB = SemaRef.ActOnConditionalOp( 7284 DefaultLoc, DefaultLoc, MinGreaterMax, UBMax, UBMin); 7285 if (!MinUB.isUsable()) 7286 return nullptr; 7287 UBVal = MinUB.get(); 7288 } 7289 } 7290 Expr *UBExpr = TestIsLessOp.getValue() ? UBVal : LBVal; 7291 Expr *LBExpr = TestIsLessOp.getValue() ? LBVal : UBVal; 7292 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get(); 7293 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get(); 7294 if (!Upper || !Lower) 7295 return nullptr; 7296 7297 ExprResult Diff = 7298 calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, Step, VarType, 7299 TestIsStrictOp, /*RoundToStep=*/true, Captures); 7300 if (!Diff.isUsable()) 7301 return nullptr; 7302 7303 // OpenMP runtime requires 32-bit or 64-bit loop variables. 7304 QualType Type = Diff.get()->getType(); 7305 ASTContext &C = SemaRef.Context; 7306 bool UseVarType = VarType->hasIntegerRepresentation() && 7307 C.getTypeSize(Type) > C.getTypeSize(VarType); 7308 if (!Type->isIntegerType() || UseVarType) { 7309 unsigned NewSize = 7310 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type); 7311 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation() 7312 : Type->hasSignedIntegerRepresentation(); 7313 Type = C.getIntTypeForBitwidth(NewSize, IsSigned); 7314 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) { 7315 Diff = SemaRef.PerformImplicitConversion( 7316 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true); 7317 if (!Diff.isUsable()) 7318 return nullptr; 7319 } 7320 } 7321 if (LimitedType) { 7322 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32; 7323 if (NewSize != C.getTypeSize(Type)) { 7324 if (NewSize < C.getTypeSize(Type)) { 7325 assert(NewSize == 64 && "incorrect loop var size"); 7326 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var) 7327 << InitSrcRange << ConditionSrcRange; 7328 } 7329 QualType NewType = C.getIntTypeForBitwidth( 7330 NewSize, Type->hasSignedIntegerRepresentation() || 7331 C.getTypeSize(Type) < NewSize); 7332 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) { 7333 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType, 7334 Sema::AA_Converting, true); 7335 if (!Diff.isUsable()) 7336 return nullptr; 7337 } 7338 } 7339 } 7340 7341 return Diff.get(); 7342 } 7343 7344 std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues( 7345 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 7346 // Do not build for iterators, they cannot be used in non-rectangular loop 7347 // nests. 7348 if (LCDecl->getType()->isRecordType()) 7349 return std::make_pair(nullptr, nullptr); 7350 // If we subtract, the min is in the condition, otherwise the min is in the 7351 // init value. 7352 Expr *MinExpr = nullptr; 7353 Expr *MaxExpr = nullptr; 7354 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB; 7355 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB; 7356 bool LBNonRect = TestIsLessOp.getValue() ? InitDependOnLC.hasValue() 7357 : CondDependOnLC.hasValue(); 7358 bool UBNonRect = TestIsLessOp.getValue() ? CondDependOnLC.hasValue() 7359 : InitDependOnLC.hasValue(); 7360 Expr *Lower = 7361 LBNonRect ? LBExpr : tryBuildCapture(SemaRef, LBExpr, Captures).get(); 7362 Expr *Upper = 7363 UBNonRect ? UBExpr : tryBuildCapture(SemaRef, UBExpr, Captures).get(); 7364 if (!Upper || !Lower) 7365 return std::make_pair(nullptr, nullptr); 7366 7367 if (TestIsLessOp.getValue()) 7368 MinExpr = Lower; 7369 else 7370 MaxExpr = Upper; 7371 7372 // Build minimum/maximum value based on number of iterations. 7373 QualType VarType = LCDecl->getType().getNonReferenceType(); 7374 7375 ExprResult Diff = 7376 calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, Step, VarType, 7377 TestIsStrictOp, /*RoundToStep=*/false, Captures); 7378 if (!Diff.isUsable()) 7379 return std::make_pair(nullptr, nullptr); 7380 7381 // ((Upper - Lower [- 1]) / Step) * Step 7382 // Parentheses (for dumping/debugging purposes only). 7383 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7384 if (!Diff.isUsable()) 7385 return std::make_pair(nullptr, nullptr); 7386 7387 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures); 7388 if (!NewStep.isUsable()) 7389 return std::make_pair(nullptr, nullptr); 7390 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Mul, Diff.get(), NewStep.get()); 7391 if (!Diff.isUsable()) 7392 return std::make_pair(nullptr, nullptr); 7393 7394 // Parentheses (for dumping/debugging purposes only). 7395 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get()); 7396 if (!Diff.isUsable()) 7397 return std::make_pair(nullptr, nullptr); 7398 7399 // Convert to the ptrdiff_t, if original type is pointer. 7400 if (VarType->isAnyPointerType() && 7401 !SemaRef.Context.hasSameType( 7402 Diff.get()->getType(), 7403 SemaRef.Context.getUnsignedPointerDiffType())) { 7404 Diff = SemaRef.PerformImplicitConversion( 7405 Diff.get(), SemaRef.Context.getUnsignedPointerDiffType(), 7406 Sema::AA_Converting, /*AllowExplicit=*/true); 7407 } 7408 if (!Diff.isUsable()) 7409 return std::make_pair(nullptr, nullptr); 7410 7411 if (TestIsLessOp.getValue()) { 7412 // MinExpr = Lower; 7413 // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step) 7414 Diff = SemaRef.BuildBinOp( 7415 S, DefaultLoc, BO_Add, 7416 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Lower).get(), 7417 Diff.get()); 7418 if (!Diff.isUsable()) 7419 return std::make_pair(nullptr, nullptr); 7420 } else { 7421 // MaxExpr = Upper; 7422 // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step) 7423 Diff = SemaRef.BuildBinOp( 7424 S, DefaultLoc, BO_Sub, 7425 SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Upper).get(), 7426 Diff.get()); 7427 if (!Diff.isUsable()) 7428 return std::make_pair(nullptr, nullptr); 7429 } 7430 7431 // Convert to the original type. 7432 if (SemaRef.Context.hasSameType(Diff.get()->getType(), VarType)) 7433 Diff = SemaRef.PerformImplicitConversion(Diff.get(), VarType, 7434 Sema::AA_Converting, 7435 /*AllowExplicit=*/true); 7436 if (!Diff.isUsable()) 7437 return std::make_pair(nullptr, nullptr); 7438 7439 Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue=*/false); 7440 if (!Diff.isUsable()) 7441 return std::make_pair(nullptr, nullptr); 7442 7443 if (TestIsLessOp.getValue()) 7444 MaxExpr = Diff.get(); 7445 else 7446 MinExpr = Diff.get(); 7447 7448 return std::make_pair(MinExpr, MaxExpr); 7449 } 7450 7451 Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const { 7452 if (InitDependOnLC || CondDependOnLC) 7453 return Condition; 7454 return nullptr; 7455 } 7456 7457 Expr *OpenMPIterationSpaceChecker::buildPreCond( 7458 Scope *S, Expr *Cond, 7459 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const { 7460 // Do not build a precondition when the condition/initialization is dependent 7461 // to prevent pessimistic early loop exit. 7462 // TODO: this can be improved by calculating min/max values but not sure that 7463 // it will be very effective. 7464 if (CondDependOnLC || InitDependOnLC) 7465 return SemaRef.PerformImplicitConversion( 7466 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(), 7467 SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 7468 /*AllowExplicit=*/true).get(); 7469 7470 // Try to build LB <op> UB, where <op> is <, >, <=, or >=. 7471 Sema::TentativeAnalysisScope Trap(SemaRef); 7472 7473 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures); 7474 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures); 7475 if (!NewLB.isUsable() || !NewUB.isUsable()) 7476 return nullptr; 7477 7478 ExprResult CondExpr = 7479 SemaRef.BuildBinOp(S, DefaultLoc, 7480 TestIsLessOp.getValue() ? 7481 (TestIsStrictOp ? BO_LT : BO_LE) : 7482 (TestIsStrictOp ? BO_GT : BO_GE), 7483 NewLB.get(), NewUB.get()); 7484 if (CondExpr.isUsable()) { 7485 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(), 7486 SemaRef.Context.BoolTy)) 7487 CondExpr = SemaRef.PerformImplicitConversion( 7488 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting, 7489 /*AllowExplicit=*/true); 7490 } 7491 7492 // Otherwise use original loop condition and evaluate it in runtime. 7493 return CondExpr.isUsable() ? CondExpr.get() : Cond; 7494 } 7495 7496 /// Build reference expression to the counter be used for codegen. 7497 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar( 7498 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, 7499 DSAStackTy &DSA) const { 7500 auto *VD = dyn_cast<VarDecl>(LCDecl); 7501 if (!VD) { 7502 VD = SemaRef.isOpenMPCapturedDecl(LCDecl); 7503 DeclRefExpr *Ref = buildDeclRefExpr( 7504 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc); 7505 const DSAStackTy::DSAVarData Data = 7506 DSA.getTopDSA(LCDecl, /*FromParent=*/false); 7507 // If the loop control decl is explicitly marked as private, do not mark it 7508 // as captured again. 7509 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr) 7510 Captures.insert(std::make_pair(LCRef, Ref)); 7511 return Ref; 7512 } 7513 return cast<DeclRefExpr>(LCRef); 7514 } 7515 7516 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const { 7517 if (LCDecl && !LCDecl->isInvalidDecl()) { 7518 QualType Type = LCDecl->getType().getNonReferenceType(); 7519 VarDecl *PrivateVar = buildVarDecl( 7520 SemaRef, DefaultLoc, Type, LCDecl->getName(), 7521 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr, 7522 isa<VarDecl>(LCDecl) 7523 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc) 7524 : nullptr); 7525 if (PrivateVar->isInvalidDecl()) 7526 return nullptr; 7527 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc); 7528 } 7529 return nullptr; 7530 } 7531 7532 /// Build initialization of the counter to be used for codegen. 7533 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; } 7534 7535 /// Build step of the counter be used for codegen. 7536 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; } 7537 7538 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData( 7539 Scope *S, Expr *Counter, 7540 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc, 7541 Expr *Inc, OverloadedOperatorKind OOK) { 7542 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get(); 7543 if (!Cnt) 7544 return nullptr; 7545 if (Inc) { 7546 assert((OOK == OO_Plus || OOK == OO_Minus) && 7547 "Expected only + or - operations for depend clauses."); 7548 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub; 7549 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get(); 7550 if (!Cnt) 7551 return nullptr; 7552 } 7553 QualType VarType = LCDecl->getType().getNonReferenceType(); 7554 if (!VarType->isIntegerType() && !VarType->isPointerType() && 7555 !SemaRef.getLangOpts().CPlusPlus) 7556 return nullptr; 7557 // Upper - Lower 7558 Expr *Upper = TestIsLessOp.getValue() 7559 ? Cnt 7560 : tryBuildCapture(SemaRef, LB, Captures).get(); 7561 Expr *Lower = TestIsLessOp.getValue() 7562 ? tryBuildCapture(SemaRef, LB, Captures).get() 7563 : Cnt; 7564 if (!Upper || !Lower) 7565 return nullptr; 7566 7567 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, 7568 Step, VarType, /*TestIsStrictOp=*/false, 7569 /*RoundToStep=*/false, Captures); 7570 if (!Diff.isUsable()) 7571 return nullptr; 7572 7573 return Diff.get(); 7574 } 7575 } // namespace 7576 7577 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) { 7578 assert(getLangOpts().OpenMP && "OpenMP is not active."); 7579 assert(Init && "Expected loop in canonical form."); 7580 unsigned AssociatedLoops = DSAStack->getAssociatedLoops(); 7581 if (AssociatedLoops > 0 && 7582 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) { 7583 DSAStack->loopStart(); 7584 OpenMPIterationSpaceChecker ISC(*this, *DSAStack, ForLoc); 7585 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) { 7586 if (ValueDecl *D = ISC.getLoopDecl()) { 7587 auto *VD = dyn_cast<VarDecl>(D); 7588 DeclRefExpr *PrivateRef = nullptr; 7589 if (!VD) { 7590 if (VarDecl *Private = isOpenMPCapturedDecl(D)) { 7591 VD = Private; 7592 } else { 7593 PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(), 7594 /*WithInit=*/false); 7595 VD = cast<VarDecl>(PrivateRef->getDecl()); 7596 } 7597 } 7598 DSAStack->addLoopControlVariable(D, VD); 7599 const Decl *LD = DSAStack->getPossiblyLoopCunter(); 7600 if (LD != D->getCanonicalDecl()) { 7601 DSAStack->resetPossibleLoopCounter(); 7602 if (auto *Var = dyn_cast_or_null<VarDecl>(LD)) 7603 MarkDeclarationsReferencedInExpr( 7604 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var), 7605 Var->getType().getNonLValueExprType(Context), 7606 ForLoc, /*RefersToCapture=*/true)); 7607 } 7608 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 7609 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables 7610 // Referenced in a Construct, C/C++]. The loop iteration variable in the 7611 // associated for-loop of a simd construct with just one associated 7612 // for-loop may be listed in a linear clause with a constant-linear-step 7613 // that is the increment of the associated for-loop. The loop iteration 7614 // variable(s) in the associated for-loop(s) of a for or parallel for 7615 // construct may be listed in a private or lastprivate clause. 7616 DSAStackTy::DSAVarData DVar = 7617 DSAStack->getTopDSA(D, /*FromParent=*/false); 7618 // If LoopVarRefExpr is nullptr it means the corresponding loop variable 7619 // is declared in the loop and it is predetermined as a private. 7620 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr(); 7621 OpenMPClauseKind PredeterminedCKind = 7622 isOpenMPSimdDirective(DKind) 7623 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear) 7624 : OMPC_private; 7625 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 7626 DVar.CKind != PredeterminedCKind && DVar.RefExpr && 7627 (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate && 7628 DVar.CKind != OMPC_private))) || 7629 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop || 7630 DKind == OMPD_master_taskloop || 7631 DKind == OMPD_parallel_master_taskloop || 7632 isOpenMPDistributeDirective(DKind)) && 7633 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 7634 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 7635 (DVar.CKind != OMPC_private || DVar.RefExpr)) { 7636 Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa) 7637 << getOpenMPClauseName(DVar.CKind) 7638 << getOpenMPDirectiveName(DKind) 7639 << getOpenMPClauseName(PredeterminedCKind); 7640 if (DVar.RefExpr == nullptr) 7641 DVar.CKind = PredeterminedCKind; 7642 reportOriginalDsa(*this, DSAStack, D, DVar, 7643 /*IsLoopIterVar=*/true); 7644 } else if (LoopDeclRefExpr) { 7645 // Make the loop iteration variable private (for worksharing 7646 // constructs), linear (for simd directives with the only one 7647 // associated loop) or lastprivate (for simd directives with several 7648 // collapsed or ordered loops). 7649 if (DVar.CKind == OMPC_unknown) 7650 DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind, 7651 PrivateRef); 7652 } 7653 } 7654 } 7655 DSAStack->setAssociatedLoops(AssociatedLoops - 1); 7656 } 7657 } 7658 7659 /// Called on a for stmt to check and extract its iteration space 7660 /// for further processing (such as collapsing). 7661 static bool checkOpenMPIterationSpace( 7662 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA, 7663 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount, 7664 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr, 7665 Expr *OrderedLoopCountExpr, 7666 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 7667 llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces, 7668 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7669 // OpenMP [2.9.1, Canonical Loop Form] 7670 // for (init-expr; test-expr; incr-expr) structured-block 7671 // for (range-decl: range-expr) structured-block 7672 auto *For = dyn_cast_or_null<ForStmt>(S); 7673 auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(S); 7674 // Ranged for is supported only in OpenMP 5.0. 7675 if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) { 7676 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for) 7677 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr) 7678 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount 7679 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount; 7680 if (TotalNestedLoopCount > 1) { 7681 if (CollapseLoopCountExpr && OrderedLoopCountExpr) 7682 SemaRef.Diag(DSA.getConstructLoc(), 7683 diag::note_omp_collapse_ordered_expr) 7684 << 2 << CollapseLoopCountExpr->getSourceRange() 7685 << OrderedLoopCountExpr->getSourceRange(); 7686 else if (CollapseLoopCountExpr) 7687 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 7688 diag::note_omp_collapse_ordered_expr) 7689 << 0 << CollapseLoopCountExpr->getSourceRange(); 7690 else 7691 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 7692 diag::note_omp_collapse_ordered_expr) 7693 << 1 << OrderedLoopCountExpr->getSourceRange(); 7694 } 7695 return true; 7696 } 7697 assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) && 7698 "No loop body."); 7699 7700 OpenMPIterationSpaceChecker ISC(SemaRef, DSA, 7701 For ? For->getForLoc() : CXXFor->getForLoc()); 7702 7703 // Check init. 7704 Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt(); 7705 if (ISC.checkAndSetInit(Init)) 7706 return true; 7707 7708 bool HasErrors = false; 7709 7710 // Check loop variable's type. 7711 if (ValueDecl *LCDecl = ISC.getLoopDecl()) { 7712 // OpenMP [2.6, Canonical Loop Form] 7713 // Var is one of the following: 7714 // A variable of signed or unsigned integer type. 7715 // For C++, a variable of a random access iterator type. 7716 // For C, a variable of a pointer type. 7717 QualType VarType = LCDecl->getType().getNonReferenceType(); 7718 if (!VarType->isDependentType() && !VarType->isIntegerType() && 7719 !VarType->isPointerType() && 7720 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 7721 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type) 7722 << SemaRef.getLangOpts().CPlusPlus; 7723 HasErrors = true; 7724 } 7725 7726 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in 7727 // a Construct 7728 // The loop iteration variable(s) in the associated for-loop(s) of a for or 7729 // parallel for construct is (are) private. 7730 // The loop iteration variable in the associated for-loop of a simd 7731 // construct with just one associated for-loop is linear with a 7732 // constant-linear-step that is the increment of the associated for-loop. 7733 // Exclude loop var from the list of variables with implicitly defined data 7734 // sharing attributes. 7735 VarsWithImplicitDSA.erase(LCDecl); 7736 7737 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 7738 7739 // Check test-expr. 7740 HasErrors |= ISC.checkAndSetCond(For ? For->getCond() : CXXFor->getCond()); 7741 7742 // Check incr-expr. 7743 HasErrors |= ISC.checkAndSetInc(For ? For->getInc() : CXXFor->getInc()); 7744 } 7745 7746 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors) 7747 return HasErrors; 7748 7749 // Build the loop's iteration space representation. 7750 ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond( 7751 DSA.getCurScope(), For ? For->getCond() : CXXFor->getCond(), Captures); 7752 ResultIterSpaces[CurrentNestedLoopCount].NumIterations = 7753 ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces, 7754 (isOpenMPWorksharingDirective(DKind) || 7755 isOpenMPTaskLoopDirective(DKind) || 7756 isOpenMPDistributeDirective(DKind)), 7757 Captures); 7758 ResultIterSpaces[CurrentNestedLoopCount].CounterVar = 7759 ISC.buildCounterVar(Captures, DSA); 7760 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar = 7761 ISC.buildPrivateCounterVar(); 7762 ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit(); 7763 ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep(); 7764 ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange(); 7765 ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange = 7766 ISC.getConditionSrcRange(); 7767 ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange = 7768 ISC.getIncrementSrcRange(); 7769 ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep(); 7770 ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare = 7771 ISC.isStrictTestOp(); 7772 std::tie(ResultIterSpaces[CurrentNestedLoopCount].MinValue, 7773 ResultIterSpaces[CurrentNestedLoopCount].MaxValue) = 7774 ISC.buildMinMaxValues(DSA.getCurScope(), Captures); 7775 ResultIterSpaces[CurrentNestedLoopCount].FinalCondition = 7776 ISC.buildFinalCondition(DSA.getCurScope()); 7777 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB = 7778 ISC.doesInitDependOnLC(); 7779 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB = 7780 ISC.doesCondDependOnLC(); 7781 ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx = 7782 ISC.getLoopDependentIdx(); 7783 7784 HasErrors |= 7785 (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr || 7786 ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr || 7787 ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr || 7788 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr || 7789 ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr || 7790 ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr); 7791 if (!HasErrors && DSA.isOrderedRegion()) { 7792 if (DSA.getOrderedRegionParam().second->getNumForLoops()) { 7793 if (CurrentNestedLoopCount < 7794 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) { 7795 DSA.getOrderedRegionParam().second->setLoopNumIterations( 7796 CurrentNestedLoopCount, 7797 ResultIterSpaces[CurrentNestedLoopCount].NumIterations); 7798 DSA.getOrderedRegionParam().second->setLoopCounter( 7799 CurrentNestedLoopCount, 7800 ResultIterSpaces[CurrentNestedLoopCount].CounterVar); 7801 } 7802 } 7803 for (auto &Pair : DSA.getDoacrossDependClauses()) { 7804 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) { 7805 // Erroneous case - clause has some problems. 7806 continue; 7807 } 7808 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink && 7809 Pair.second.size() <= CurrentNestedLoopCount) { 7810 // Erroneous case - clause has some problems. 7811 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr); 7812 continue; 7813 } 7814 Expr *CntValue; 7815 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) 7816 CntValue = ISC.buildOrderedLoopData( 7817 DSA.getCurScope(), 7818 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 7819 Pair.first->getDependencyLoc()); 7820 else 7821 CntValue = ISC.buildOrderedLoopData( 7822 DSA.getCurScope(), 7823 ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures, 7824 Pair.first->getDependencyLoc(), 7825 Pair.second[CurrentNestedLoopCount].first, 7826 Pair.second[CurrentNestedLoopCount].second); 7827 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue); 7828 } 7829 } 7830 7831 return HasErrors; 7832 } 7833 7834 /// Build 'VarRef = Start. 7835 static ExprResult 7836 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 7837 ExprResult Start, bool IsNonRectangularLB, 7838 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7839 // Build 'VarRef = Start. 7840 ExprResult NewStart = IsNonRectangularLB 7841 ? Start.get() 7842 : tryBuildCapture(SemaRef, Start.get(), Captures); 7843 if (!NewStart.isUsable()) 7844 return ExprError(); 7845 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(), 7846 VarRef.get()->getType())) { 7847 NewStart = SemaRef.PerformImplicitConversion( 7848 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting, 7849 /*AllowExplicit=*/true); 7850 if (!NewStart.isUsable()) 7851 return ExprError(); 7852 } 7853 7854 ExprResult Init = 7855 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 7856 return Init; 7857 } 7858 7859 /// Build 'VarRef = Start + Iter * Step'. 7860 static ExprResult buildCounterUpdate( 7861 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef, 7862 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract, 7863 bool IsNonRectangularLB, 7864 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) { 7865 // Add parentheses (for debugging purposes only). 7866 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get()); 7867 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() || 7868 !Step.isUsable()) 7869 return ExprError(); 7870 7871 ExprResult NewStep = Step; 7872 if (Captures) 7873 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures); 7874 if (NewStep.isInvalid()) 7875 return ExprError(); 7876 ExprResult Update = 7877 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get()); 7878 if (!Update.isUsable()) 7879 return ExprError(); 7880 7881 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or 7882 // 'VarRef = Start (+|-) Iter * Step'. 7883 if (!Start.isUsable()) 7884 return ExprError(); 7885 ExprResult NewStart = SemaRef.ActOnParenExpr(Loc, Loc, Start.get()); 7886 if (!NewStart.isUsable()) 7887 return ExprError(); 7888 if (Captures && !IsNonRectangularLB) 7889 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures); 7890 if (NewStart.isInvalid()) 7891 return ExprError(); 7892 7893 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'. 7894 ExprResult SavedUpdate = Update; 7895 ExprResult UpdateVal; 7896 if (VarRef.get()->getType()->isOverloadableType() || 7897 NewStart.get()->getType()->isOverloadableType() || 7898 Update.get()->getType()->isOverloadableType()) { 7899 Sema::TentativeAnalysisScope Trap(SemaRef); 7900 7901 Update = 7902 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get()); 7903 if (Update.isUsable()) { 7904 UpdateVal = 7905 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign, 7906 VarRef.get(), SavedUpdate.get()); 7907 if (UpdateVal.isUsable()) { 7908 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(), 7909 UpdateVal.get()); 7910 } 7911 } 7912 } 7913 7914 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'. 7915 if (!Update.isUsable() || !UpdateVal.isUsable()) { 7916 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add, 7917 NewStart.get(), SavedUpdate.get()); 7918 if (!Update.isUsable()) 7919 return ExprError(); 7920 7921 if (!SemaRef.Context.hasSameType(Update.get()->getType(), 7922 VarRef.get()->getType())) { 7923 Update = SemaRef.PerformImplicitConversion( 7924 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true); 7925 if (!Update.isUsable()) 7926 return ExprError(); 7927 } 7928 7929 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get()); 7930 } 7931 return Update; 7932 } 7933 7934 /// Convert integer expression \a E to make it have at least \a Bits 7935 /// bits. 7936 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) { 7937 if (E == nullptr) 7938 return ExprError(); 7939 ASTContext &C = SemaRef.Context; 7940 QualType OldType = E->getType(); 7941 unsigned HasBits = C.getTypeSize(OldType); 7942 if (HasBits >= Bits) 7943 return ExprResult(E); 7944 // OK to convert to signed, because new type has more bits than old. 7945 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true); 7946 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting, 7947 true); 7948 } 7949 7950 /// Check if the given expression \a E is a constant integer that fits 7951 /// into \a Bits bits. 7952 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) { 7953 if (E == nullptr) 7954 return false; 7955 llvm::APSInt Result; 7956 if (E->isIntegerConstantExpr(Result, SemaRef.Context)) 7957 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits); 7958 return false; 7959 } 7960 7961 /// Build preinits statement for the given declarations. 7962 static Stmt *buildPreInits(ASTContext &Context, 7963 MutableArrayRef<Decl *> PreInits) { 7964 if (!PreInits.empty()) { 7965 return new (Context) DeclStmt( 7966 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()), 7967 SourceLocation(), SourceLocation()); 7968 } 7969 return nullptr; 7970 } 7971 7972 /// Build preinits statement for the given declarations. 7973 static Stmt * 7974 buildPreInits(ASTContext &Context, 7975 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) { 7976 if (!Captures.empty()) { 7977 SmallVector<Decl *, 16> PreInits; 7978 for (const auto &Pair : Captures) 7979 PreInits.push_back(Pair.second->getDecl()); 7980 return buildPreInits(Context, PreInits); 7981 } 7982 return nullptr; 7983 } 7984 7985 /// Build postupdate expression for the given list of postupdates expressions. 7986 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) { 7987 Expr *PostUpdate = nullptr; 7988 if (!PostUpdates.empty()) { 7989 for (Expr *E : PostUpdates) { 7990 Expr *ConvE = S.BuildCStyleCastExpr( 7991 E->getExprLoc(), 7992 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy), 7993 E->getExprLoc(), E) 7994 .get(); 7995 PostUpdate = PostUpdate 7996 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma, 7997 PostUpdate, ConvE) 7998 .get() 7999 : ConvE; 8000 } 8001 } 8002 return PostUpdate; 8003 } 8004 8005 /// Called on a for stmt to check itself and nested loops (if any). 8006 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 8007 /// number of collapsed loops otherwise. 8008 static unsigned 8009 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr, 8010 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef, 8011 DSAStackTy &DSA, 8012 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA, 8013 OMPLoopDirective::HelperExprs &Built) { 8014 unsigned NestedLoopCount = 1; 8015 if (CollapseLoopCountExpr) { 8016 // Found 'collapse' clause - calculate collapse number. 8017 Expr::EvalResult Result; 8018 if (!CollapseLoopCountExpr->isValueDependent() && 8019 CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) { 8020 NestedLoopCount = Result.Val.getInt().getLimitedValue(); 8021 } else { 8022 Built.clear(/*Size=*/1); 8023 return 1; 8024 } 8025 } 8026 unsigned OrderedLoopCount = 1; 8027 if (OrderedLoopCountExpr) { 8028 // Found 'ordered' clause - calculate collapse number. 8029 Expr::EvalResult EVResult; 8030 if (!OrderedLoopCountExpr->isValueDependent() && 8031 OrderedLoopCountExpr->EvaluateAsInt(EVResult, 8032 SemaRef.getASTContext())) { 8033 llvm::APSInt Result = EVResult.Val.getInt(); 8034 if (Result.getLimitedValue() < NestedLoopCount) { 8035 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(), 8036 diag::err_omp_wrong_ordered_loop_count) 8037 << OrderedLoopCountExpr->getSourceRange(); 8038 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(), 8039 diag::note_collapse_loop_count) 8040 << CollapseLoopCountExpr->getSourceRange(); 8041 } 8042 OrderedLoopCount = Result.getLimitedValue(); 8043 } else { 8044 Built.clear(/*Size=*/1); 8045 return 1; 8046 } 8047 } 8048 // This is helper routine for loop directives (e.g., 'for', 'simd', 8049 // 'for simd', etc.). 8050 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 8051 SmallVector<LoopIterationSpace, 4> IterSpaces( 8052 std::max(OrderedLoopCount, NestedLoopCount)); 8053 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true); 8054 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 8055 if (checkOpenMPIterationSpace( 8056 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 8057 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 8058 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures)) 8059 return 0; 8060 // Move on to the next nested for loop, or to the loop body. 8061 // OpenMP [2.8.1, simd construct, Restrictions] 8062 // All loops associated with the construct must be perfectly nested; that 8063 // is, there must be no intervening code nor any OpenMP directive between 8064 // any two loops. 8065 if (auto *For = dyn_cast<ForStmt>(CurStmt)) { 8066 CurStmt = For->getBody(); 8067 } else { 8068 assert(isa<CXXForRangeStmt>(CurStmt) && 8069 "Expected canonical for or range-based for loops."); 8070 CurStmt = cast<CXXForRangeStmt>(CurStmt)->getBody(); 8071 } 8072 CurStmt = OMPLoopDirective::tryToFindNextInnerLoop( 8073 CurStmt, SemaRef.LangOpts.OpenMP >= 50); 8074 } 8075 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) { 8076 if (checkOpenMPIterationSpace( 8077 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount, 8078 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr, 8079 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures)) 8080 return 0; 8081 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) { 8082 // Handle initialization of captured loop iterator variables. 8083 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar); 8084 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) { 8085 Captures[DRE] = DRE; 8086 } 8087 } 8088 // Move on to the next nested for loop, or to the loop body. 8089 // OpenMP [2.8.1, simd construct, Restrictions] 8090 // All loops associated with the construct must be perfectly nested; that 8091 // is, there must be no intervening code nor any OpenMP directive between 8092 // any two loops. 8093 if (auto *For = dyn_cast<ForStmt>(CurStmt)) { 8094 CurStmt = For->getBody(); 8095 } else { 8096 assert(isa<CXXForRangeStmt>(CurStmt) && 8097 "Expected canonical for or range-based for loops."); 8098 CurStmt = cast<CXXForRangeStmt>(CurStmt)->getBody(); 8099 } 8100 CurStmt = OMPLoopDirective::tryToFindNextInnerLoop( 8101 CurStmt, SemaRef.LangOpts.OpenMP >= 50); 8102 } 8103 8104 Built.clear(/* size */ NestedLoopCount); 8105 8106 if (SemaRef.CurContext->isDependentContext()) 8107 return NestedLoopCount; 8108 8109 // An example of what is generated for the following code: 8110 // 8111 // #pragma omp simd collapse(2) ordered(2) 8112 // for (i = 0; i < NI; ++i) 8113 // for (k = 0; k < NK; ++k) 8114 // for (j = J0; j < NJ; j+=2) { 8115 // <loop body> 8116 // } 8117 // 8118 // We generate the code below. 8119 // Note: the loop body may be outlined in CodeGen. 8120 // Note: some counters may be C++ classes, operator- is used to find number of 8121 // iterations and operator+= to calculate counter value. 8122 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32 8123 // or i64 is currently supported). 8124 // 8125 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2)) 8126 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) { 8127 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2); 8128 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2; 8129 // // similar updates for vars in clauses (e.g. 'linear') 8130 // <loop body (using local i and j)> 8131 // } 8132 // i = NI; // assign final values of counters 8133 // j = NJ; 8134 // 8135 8136 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are 8137 // the iteration counts of the collapsed for loops. 8138 // Precondition tests if there is at least one iteration (all conditions are 8139 // true). 8140 auto PreCond = ExprResult(IterSpaces[0].PreCond); 8141 Expr *N0 = IterSpaces[0].NumIterations; 8142 ExprResult LastIteration32 = 8143 widenIterationCount(/*Bits=*/32, 8144 SemaRef 8145 .PerformImplicitConversion( 8146 N0->IgnoreImpCasts(), N0->getType(), 8147 Sema::AA_Converting, /*AllowExplicit=*/true) 8148 .get(), 8149 SemaRef); 8150 ExprResult LastIteration64 = widenIterationCount( 8151 /*Bits=*/64, 8152 SemaRef 8153 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(), 8154 Sema::AA_Converting, 8155 /*AllowExplicit=*/true) 8156 .get(), 8157 SemaRef); 8158 8159 if (!LastIteration32.isUsable() || !LastIteration64.isUsable()) 8160 return NestedLoopCount; 8161 8162 ASTContext &C = SemaRef.Context; 8163 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32; 8164 8165 Scope *CurScope = DSA.getCurScope(); 8166 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) { 8167 if (PreCond.isUsable()) { 8168 PreCond = 8169 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd, 8170 PreCond.get(), IterSpaces[Cnt].PreCond); 8171 } 8172 Expr *N = IterSpaces[Cnt].NumIterations; 8173 SourceLocation Loc = N->getExprLoc(); 8174 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32; 8175 if (LastIteration32.isUsable()) 8176 LastIteration32 = SemaRef.BuildBinOp( 8177 CurScope, Loc, BO_Mul, LastIteration32.get(), 8178 SemaRef 8179 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 8180 Sema::AA_Converting, 8181 /*AllowExplicit=*/true) 8182 .get()); 8183 if (LastIteration64.isUsable()) 8184 LastIteration64 = SemaRef.BuildBinOp( 8185 CurScope, Loc, BO_Mul, LastIteration64.get(), 8186 SemaRef 8187 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(), 8188 Sema::AA_Converting, 8189 /*AllowExplicit=*/true) 8190 .get()); 8191 } 8192 8193 // Choose either the 32-bit or 64-bit version. 8194 ExprResult LastIteration = LastIteration64; 8195 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse || 8196 (LastIteration32.isUsable() && 8197 C.getTypeSize(LastIteration32.get()->getType()) == 32 && 8198 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 || 8199 fitsInto( 8200 /*Bits=*/32, 8201 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(), 8202 LastIteration64.get(), SemaRef)))) 8203 LastIteration = LastIteration32; 8204 QualType VType = LastIteration.get()->getType(); 8205 QualType RealVType = VType; 8206 QualType StrideVType = VType; 8207 if (isOpenMPTaskLoopDirective(DKind)) { 8208 VType = 8209 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); 8210 StrideVType = 8211 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); 8212 } 8213 8214 if (!LastIteration.isUsable()) 8215 return 0; 8216 8217 // Save the number of iterations. 8218 ExprResult NumIterations = LastIteration; 8219 { 8220 LastIteration = SemaRef.BuildBinOp( 8221 CurScope, LastIteration.get()->getExprLoc(), BO_Sub, 8222 LastIteration.get(), 8223 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 8224 if (!LastIteration.isUsable()) 8225 return 0; 8226 } 8227 8228 // Calculate the last iteration number beforehand instead of doing this on 8229 // each iteration. Do not do this if the number of iterations may be kfold-ed. 8230 llvm::APSInt Result; 8231 bool IsConstant = 8232 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context); 8233 ExprResult CalcLastIteration; 8234 if (!IsConstant) { 8235 ExprResult SaveRef = 8236 tryBuildCapture(SemaRef, LastIteration.get(), Captures); 8237 LastIteration = SaveRef; 8238 8239 // Prepare SaveRef + 1. 8240 NumIterations = SemaRef.BuildBinOp( 8241 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(), 8242 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()); 8243 if (!NumIterations.isUsable()) 8244 return 0; 8245 } 8246 8247 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin(); 8248 8249 // Build variables passed into runtime, necessary for worksharing directives. 8250 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; 8251 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 8252 isOpenMPDistributeDirective(DKind)) { 8253 // Lower bound variable, initialized with zero. 8254 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); 8255 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc); 8256 SemaRef.AddInitializerToDecl(LBDecl, 8257 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 8258 /*DirectInit*/ false); 8259 8260 // Upper bound variable, initialized with last iteration number. 8261 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub"); 8262 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc); 8263 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(), 8264 /*DirectInit*/ false); 8265 8266 // A 32-bit variable-flag where runtime returns 1 for the last iteration. 8267 // This will be used to implement clause 'lastprivate'. 8268 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true); 8269 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last"); 8270 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc); 8271 SemaRef.AddInitializerToDecl(ILDecl, 8272 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 8273 /*DirectInit*/ false); 8274 8275 // Stride variable returned by runtime (we initialize it to 1 by default). 8276 VarDecl *STDecl = 8277 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride"); 8278 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc); 8279 SemaRef.AddInitializerToDecl(STDecl, 8280 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(), 8281 /*DirectInit*/ false); 8282 8283 // Build expression: UB = min(UB, LastIteration) 8284 // It is necessary for CodeGen of directives with static scheduling. 8285 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT, 8286 UB.get(), LastIteration.get()); 8287 ExprResult CondOp = SemaRef.ActOnConditionalOp( 8288 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(), 8289 LastIteration.get(), UB.get()); 8290 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(), 8291 CondOp.get()); 8292 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false); 8293 8294 // If we have a combined directive that combines 'distribute', 'for' or 8295 // 'simd' we need to be able to access the bounds of the schedule of the 8296 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained 8297 // by scheduling 'distribute' have to be passed to the schedule of 'for'. 8298 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8299 // Lower bound variable, initialized with zero. 8300 VarDecl *CombLBDecl = 8301 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb"); 8302 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc); 8303 SemaRef.AddInitializerToDecl( 8304 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(), 8305 /*DirectInit*/ false); 8306 8307 // Upper bound variable, initialized with last iteration number. 8308 VarDecl *CombUBDecl = 8309 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub"); 8310 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc); 8311 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(), 8312 /*DirectInit*/ false); 8313 8314 ExprResult CombIsUBGreater = SemaRef.BuildBinOp( 8315 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get()); 8316 ExprResult CombCondOp = 8317 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(), 8318 LastIteration.get(), CombUB.get()); 8319 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(), 8320 CombCondOp.get()); 8321 CombEUB = 8322 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false); 8323 8324 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl(); 8325 // We expect to have at least 2 more parameters than the 'parallel' 8326 // directive does - the lower and upper bounds of the previous schedule. 8327 assert(CD->getNumParams() >= 4 && 8328 "Unexpected number of parameters in loop combined directive"); 8329 8330 // Set the proper type for the bounds given what we learned from the 8331 // enclosed loops. 8332 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2); 8333 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3); 8334 8335 // Previous lower and upper bounds are obtained from the region 8336 // parameters. 8337 PrevLB = 8338 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc); 8339 PrevUB = 8340 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc); 8341 } 8342 } 8343 8344 // Build the iteration variable and its initialization before loop. 8345 ExprResult IV; 8346 ExprResult Init, CombInit; 8347 { 8348 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); 8349 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); 8350 Expr *RHS = 8351 (isOpenMPWorksharingDirective(DKind) || 8352 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 8353 ? LB.get() 8354 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 8355 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS); 8356 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false); 8357 8358 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8359 Expr *CombRHS = 8360 (isOpenMPWorksharingDirective(DKind) || 8361 isOpenMPTaskLoopDirective(DKind) || 8362 isOpenMPDistributeDirective(DKind)) 8363 ? CombLB.get() 8364 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get(); 8365 CombInit = 8366 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS); 8367 CombInit = 8368 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false); 8369 } 8370 } 8371 8372 bool UseStrictCompare = 8373 RealVType->hasUnsignedIntegerRepresentation() && 8374 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) { 8375 return LIS.IsStrictCompare; 8376 }); 8377 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for 8378 // unsigned IV)) for worksharing loops. 8379 SourceLocation CondLoc = AStmt->getBeginLoc(); 8380 Expr *BoundUB = UB.get(); 8381 if (UseStrictCompare) { 8382 BoundUB = 8383 SemaRef 8384 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB, 8385 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 8386 .get(); 8387 BoundUB = 8388 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get(); 8389 } 8390 ExprResult Cond = 8391 (isOpenMPWorksharingDirective(DKind) || 8392 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) 8393 ? SemaRef.BuildBinOp(CurScope, CondLoc, 8394 UseStrictCompare ? BO_LT : BO_LE, IV.get(), 8395 BoundUB) 8396 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 8397 NumIterations.get()); 8398 ExprResult CombDistCond; 8399 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8400 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(), 8401 NumIterations.get()); 8402 } 8403 8404 ExprResult CombCond; 8405 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8406 Expr *BoundCombUB = CombUB.get(); 8407 if (UseStrictCompare) { 8408 BoundCombUB = 8409 SemaRef 8410 .BuildBinOp( 8411 CurScope, CondLoc, BO_Add, BoundCombUB, 8412 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 8413 .get(); 8414 BoundCombUB = 8415 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false) 8416 .get(); 8417 } 8418 CombCond = 8419 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 8420 IV.get(), BoundCombUB); 8421 } 8422 // Loop increment (IV = IV + 1) 8423 SourceLocation IncLoc = AStmt->getBeginLoc(); 8424 ExprResult Inc = 8425 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(), 8426 SemaRef.ActOnIntegerConstant(IncLoc, 1).get()); 8427 if (!Inc.isUsable()) 8428 return 0; 8429 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get()); 8430 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false); 8431 if (!Inc.isUsable()) 8432 return 0; 8433 8434 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST). 8435 // Used for directives with static scheduling. 8436 // In combined construct, add combined version that use CombLB and CombUB 8437 // base variables for the update 8438 ExprResult NextLB, NextUB, CombNextLB, CombNextUB; 8439 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || 8440 isOpenMPDistributeDirective(DKind)) { 8441 // LB + ST 8442 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get()); 8443 if (!NextLB.isUsable()) 8444 return 0; 8445 // LB = LB + ST 8446 NextLB = 8447 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get()); 8448 NextLB = 8449 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false); 8450 if (!NextLB.isUsable()) 8451 return 0; 8452 // UB + ST 8453 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get()); 8454 if (!NextUB.isUsable()) 8455 return 0; 8456 // UB = UB + ST 8457 NextUB = 8458 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get()); 8459 NextUB = 8460 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false); 8461 if (!NextUB.isUsable()) 8462 return 0; 8463 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8464 CombNextLB = 8465 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get()); 8466 if (!NextLB.isUsable()) 8467 return 0; 8468 // LB = LB + ST 8469 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(), 8470 CombNextLB.get()); 8471 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(), 8472 /*DiscardedValue*/ false); 8473 if (!CombNextLB.isUsable()) 8474 return 0; 8475 // UB + ST 8476 CombNextUB = 8477 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get()); 8478 if (!CombNextUB.isUsable()) 8479 return 0; 8480 // UB = UB + ST 8481 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(), 8482 CombNextUB.get()); 8483 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(), 8484 /*DiscardedValue*/ false); 8485 if (!CombNextUB.isUsable()) 8486 return 0; 8487 } 8488 } 8489 8490 // Create increment expression for distribute loop when combined in a same 8491 // directive with for as IV = IV + ST; ensure upper bound expression based 8492 // on PrevUB instead of NumIterations - used to implement 'for' when found 8493 // in combination with 'distribute', like in 'distribute parallel for' 8494 SourceLocation DistIncLoc = AStmt->getBeginLoc(); 8495 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond; 8496 if (isOpenMPLoopBoundSharingDirective(DKind)) { 8497 DistCond = SemaRef.BuildBinOp( 8498 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB); 8499 assert(DistCond.isUsable() && "distribute cond expr was not built"); 8500 8501 DistInc = 8502 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get()); 8503 assert(DistInc.isUsable() && "distribute inc expr was not built"); 8504 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(), 8505 DistInc.get()); 8506 DistInc = 8507 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false); 8508 assert(DistInc.isUsable() && "distribute inc expr was not built"); 8509 8510 // Build expression: UB = min(UB, prevUB) for #for in composite or combined 8511 // construct 8512 SourceLocation DistEUBLoc = AStmt->getBeginLoc(); 8513 ExprResult IsUBGreater = 8514 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get()); 8515 ExprResult CondOp = SemaRef.ActOnConditionalOp( 8516 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get()); 8517 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(), 8518 CondOp.get()); 8519 PrevEUB = 8520 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false); 8521 8522 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in 8523 // parallel for is in combination with a distribute directive with 8524 // schedule(static, 1) 8525 Expr *BoundPrevUB = PrevUB.get(); 8526 if (UseStrictCompare) { 8527 BoundPrevUB = 8528 SemaRef 8529 .BuildBinOp( 8530 CurScope, CondLoc, BO_Add, BoundPrevUB, 8531 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get()) 8532 .get(); 8533 BoundPrevUB = 8534 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false) 8535 .get(); 8536 } 8537 ParForInDistCond = 8538 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, 8539 IV.get(), BoundPrevUB); 8540 } 8541 8542 // Build updates and final values of the loop counters. 8543 bool HasErrors = false; 8544 Built.Counters.resize(NestedLoopCount); 8545 Built.Inits.resize(NestedLoopCount); 8546 Built.Updates.resize(NestedLoopCount); 8547 Built.Finals.resize(NestedLoopCount); 8548 Built.DependentCounters.resize(NestedLoopCount); 8549 Built.DependentInits.resize(NestedLoopCount); 8550 Built.FinalsConditions.resize(NestedLoopCount); 8551 { 8552 // We implement the following algorithm for obtaining the 8553 // original loop iteration variable values based on the 8554 // value of the collapsed loop iteration variable IV. 8555 // 8556 // Let n+1 be the number of collapsed loops in the nest. 8557 // Iteration variables (I0, I1, .... In) 8558 // Iteration counts (N0, N1, ... Nn) 8559 // 8560 // Acc = IV; 8561 // 8562 // To compute Ik for loop k, 0 <= k <= n, generate: 8563 // Prod = N(k+1) * N(k+2) * ... * Nn; 8564 // Ik = Acc / Prod; 8565 // Acc -= Ik * Prod; 8566 // 8567 ExprResult Acc = IV; 8568 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 8569 LoopIterationSpace &IS = IterSpaces[Cnt]; 8570 SourceLocation UpdLoc = IS.IncSrcRange.getBegin(); 8571 ExprResult Iter; 8572 8573 // Compute prod 8574 ExprResult Prod = 8575 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 8576 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K) 8577 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(), 8578 IterSpaces[K].NumIterations); 8579 8580 // Iter = Acc / Prod 8581 // If there is at least one more inner loop to avoid 8582 // multiplication by 1. 8583 if (Cnt + 1 < NestedLoopCount) 8584 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, 8585 Acc.get(), Prod.get()); 8586 else 8587 Iter = Acc; 8588 if (!Iter.isUsable()) { 8589 HasErrors = true; 8590 break; 8591 } 8592 8593 // Update Acc: 8594 // Acc -= Iter * Prod 8595 // Check if there is at least one more inner loop to avoid 8596 // multiplication by 1. 8597 if (Cnt + 1 < NestedLoopCount) 8598 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, 8599 Iter.get(), Prod.get()); 8600 else 8601 Prod = Iter; 8602 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub, 8603 Acc.get(), Prod.get()); 8604 8605 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step 8606 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()); 8607 DeclRefExpr *CounterVar = buildDeclRefExpr( 8608 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(), 8609 /*RefersToCapture=*/true); 8610 ExprResult Init = 8611 buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar, 8612 IS.CounterInit, IS.IsNonRectangularLB, Captures); 8613 if (!Init.isUsable()) { 8614 HasErrors = true; 8615 break; 8616 } 8617 ExprResult Update = buildCounterUpdate( 8618 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter, 8619 IS.CounterStep, IS.Subtract, IS.IsNonRectangularLB, &Captures); 8620 if (!Update.isUsable()) { 8621 HasErrors = true; 8622 break; 8623 } 8624 8625 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step 8626 ExprResult Final = 8627 buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar, 8628 IS.CounterInit, IS.NumIterations, IS.CounterStep, 8629 IS.Subtract, IS.IsNonRectangularLB, &Captures); 8630 if (!Final.isUsable()) { 8631 HasErrors = true; 8632 break; 8633 } 8634 8635 if (!Update.isUsable() || !Final.isUsable()) { 8636 HasErrors = true; 8637 break; 8638 } 8639 // Save results 8640 Built.Counters[Cnt] = IS.CounterVar; 8641 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar; 8642 Built.Inits[Cnt] = Init.get(); 8643 Built.Updates[Cnt] = Update.get(); 8644 Built.Finals[Cnt] = Final.get(); 8645 Built.DependentCounters[Cnt] = nullptr; 8646 Built.DependentInits[Cnt] = nullptr; 8647 Built.FinalsConditions[Cnt] = nullptr; 8648 if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) { 8649 Built.DependentCounters[Cnt] = 8650 Built.Counters[NestedLoopCount - 1 - IS.LoopDependentIdx]; 8651 Built.DependentInits[Cnt] = 8652 Built.Inits[NestedLoopCount - 1 - IS.LoopDependentIdx]; 8653 Built.FinalsConditions[Cnt] = IS.FinalCondition; 8654 } 8655 } 8656 } 8657 8658 if (HasErrors) 8659 return 0; 8660 8661 // Save results 8662 Built.IterationVarRef = IV.get(); 8663 Built.LastIteration = LastIteration.get(); 8664 Built.NumIterations = NumIterations.get(); 8665 Built.CalcLastIteration = SemaRef 8666 .ActOnFinishFullExpr(CalcLastIteration.get(), 8667 /*DiscardedValue=*/false) 8668 .get(); 8669 Built.PreCond = PreCond.get(); 8670 Built.PreInits = buildPreInits(C, Captures); 8671 Built.Cond = Cond.get(); 8672 Built.Init = Init.get(); 8673 Built.Inc = Inc.get(); 8674 Built.LB = LB.get(); 8675 Built.UB = UB.get(); 8676 Built.IL = IL.get(); 8677 Built.ST = ST.get(); 8678 Built.EUB = EUB.get(); 8679 Built.NLB = NextLB.get(); 8680 Built.NUB = NextUB.get(); 8681 Built.PrevLB = PrevLB.get(); 8682 Built.PrevUB = PrevUB.get(); 8683 Built.DistInc = DistInc.get(); 8684 Built.PrevEUB = PrevEUB.get(); 8685 Built.DistCombinedFields.LB = CombLB.get(); 8686 Built.DistCombinedFields.UB = CombUB.get(); 8687 Built.DistCombinedFields.EUB = CombEUB.get(); 8688 Built.DistCombinedFields.Init = CombInit.get(); 8689 Built.DistCombinedFields.Cond = CombCond.get(); 8690 Built.DistCombinedFields.NLB = CombNextLB.get(); 8691 Built.DistCombinedFields.NUB = CombNextUB.get(); 8692 Built.DistCombinedFields.DistCond = CombDistCond.get(); 8693 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get(); 8694 8695 return NestedLoopCount; 8696 } 8697 8698 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 8699 auto CollapseClauses = 8700 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses); 8701 if (CollapseClauses.begin() != CollapseClauses.end()) 8702 return (*CollapseClauses.begin())->getNumForLoops(); 8703 return nullptr; 8704 } 8705 8706 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) { 8707 auto OrderedClauses = 8708 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses); 8709 if (OrderedClauses.begin() != OrderedClauses.end()) 8710 return (*OrderedClauses.begin())->getNumForLoops(); 8711 return nullptr; 8712 } 8713 8714 static bool checkSimdlenSafelenSpecified(Sema &S, 8715 const ArrayRef<OMPClause *> Clauses) { 8716 const OMPSafelenClause *Safelen = nullptr; 8717 const OMPSimdlenClause *Simdlen = nullptr; 8718 8719 for (const OMPClause *Clause : Clauses) { 8720 if (Clause->getClauseKind() == OMPC_safelen) 8721 Safelen = cast<OMPSafelenClause>(Clause); 8722 else if (Clause->getClauseKind() == OMPC_simdlen) 8723 Simdlen = cast<OMPSimdlenClause>(Clause); 8724 if (Safelen && Simdlen) 8725 break; 8726 } 8727 8728 if (Simdlen && Safelen) { 8729 const Expr *SimdlenLength = Simdlen->getSimdlen(); 8730 const Expr *SafelenLength = Safelen->getSafelen(); 8731 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() || 8732 SimdlenLength->isInstantiationDependent() || 8733 SimdlenLength->containsUnexpandedParameterPack()) 8734 return false; 8735 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() || 8736 SafelenLength->isInstantiationDependent() || 8737 SafelenLength->containsUnexpandedParameterPack()) 8738 return false; 8739 Expr::EvalResult SimdlenResult, SafelenResult; 8740 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context); 8741 SafelenLength->EvaluateAsInt(SafelenResult, S.Context); 8742 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt(); 8743 llvm::APSInt SafelenRes = SafelenResult.Val.getInt(); 8744 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions] 8745 // If both simdlen and safelen clauses are specified, the value of the 8746 // simdlen parameter must be less than or equal to the value of the safelen 8747 // parameter. 8748 if (SimdlenRes > SafelenRes) { 8749 S.Diag(SimdlenLength->getExprLoc(), 8750 diag::err_omp_wrong_simdlen_safelen_values) 8751 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange(); 8752 return true; 8753 } 8754 } 8755 return false; 8756 } 8757 8758 StmtResult 8759 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 8760 SourceLocation StartLoc, SourceLocation EndLoc, 8761 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8762 if (!AStmt) 8763 return StmtError(); 8764 8765 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8766 OMPLoopDirective::HelperExprs B; 8767 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8768 // define the nested loops number. 8769 unsigned NestedLoopCount = checkOpenMPLoop( 8770 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 8771 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 8772 if (NestedLoopCount == 0) 8773 return StmtError(); 8774 8775 assert((CurContext->isDependentContext() || B.builtAll()) && 8776 "omp simd loop exprs were not built"); 8777 8778 if (!CurContext->isDependentContext()) { 8779 // Finalize the clauses that need pre-built expressions for CodeGen. 8780 for (OMPClause *C : Clauses) { 8781 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8782 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8783 B.NumIterations, *this, CurScope, 8784 DSAStack)) 8785 return StmtError(); 8786 } 8787 } 8788 8789 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8790 return StmtError(); 8791 8792 setFunctionHasBranchProtectedScope(); 8793 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 8794 Clauses, AStmt, B); 8795 } 8796 8797 StmtResult 8798 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt, 8799 SourceLocation StartLoc, SourceLocation EndLoc, 8800 VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8801 if (!AStmt) 8802 return StmtError(); 8803 8804 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8805 OMPLoopDirective::HelperExprs B; 8806 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8807 // define the nested loops number. 8808 unsigned NestedLoopCount = checkOpenMPLoop( 8809 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), 8810 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); 8811 if (NestedLoopCount == 0) 8812 return StmtError(); 8813 8814 assert((CurContext->isDependentContext() || B.builtAll()) && 8815 "omp for loop exprs were not built"); 8816 8817 if (!CurContext->isDependentContext()) { 8818 // Finalize the clauses that need pre-built expressions for CodeGen. 8819 for (OMPClause *C : Clauses) { 8820 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8821 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8822 B.NumIterations, *this, CurScope, 8823 DSAStack)) 8824 return StmtError(); 8825 } 8826 } 8827 8828 setFunctionHasBranchProtectedScope(); 8829 return OMPForDirective::Create( 8830 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 8831 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 8832 } 8833 8834 StmtResult Sema::ActOnOpenMPForSimdDirective( 8835 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 8836 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 8837 if (!AStmt) 8838 return StmtError(); 8839 8840 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8841 OMPLoopDirective::HelperExprs B; 8842 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 8843 // define the nested loops number. 8844 unsigned NestedLoopCount = 8845 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses), 8846 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 8847 VarsWithImplicitDSA, B); 8848 if (NestedLoopCount == 0) 8849 return StmtError(); 8850 8851 assert((CurContext->isDependentContext() || B.builtAll()) && 8852 "omp for simd loop exprs were not built"); 8853 8854 if (!CurContext->isDependentContext()) { 8855 // Finalize the clauses that need pre-built expressions for CodeGen. 8856 for (OMPClause *C : Clauses) { 8857 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 8858 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 8859 B.NumIterations, *this, CurScope, 8860 DSAStack)) 8861 return StmtError(); 8862 } 8863 } 8864 8865 if (checkSimdlenSafelenSpecified(*this, Clauses)) 8866 return StmtError(); 8867 8868 setFunctionHasBranchProtectedScope(); 8869 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 8870 Clauses, AStmt, B); 8871 } 8872 8873 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 8874 Stmt *AStmt, 8875 SourceLocation StartLoc, 8876 SourceLocation EndLoc) { 8877 if (!AStmt) 8878 return StmtError(); 8879 8880 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8881 auto BaseStmt = AStmt; 8882 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 8883 BaseStmt = CS->getCapturedStmt(); 8884 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 8885 auto S = C->children(); 8886 if (S.begin() == S.end()) 8887 return StmtError(); 8888 // All associated statements must be '#pragma omp section' except for 8889 // the first one. 8890 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 8891 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 8892 if (SectionStmt) 8893 Diag(SectionStmt->getBeginLoc(), 8894 diag::err_omp_sections_substmt_not_section); 8895 return StmtError(); 8896 } 8897 cast<OMPSectionDirective>(SectionStmt) 8898 ->setHasCancel(DSAStack->isCancelRegion()); 8899 } 8900 } else { 8901 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt); 8902 return StmtError(); 8903 } 8904 8905 setFunctionHasBranchProtectedScope(); 8906 8907 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 8908 DSAStack->getTaskgroupReductionRef(), 8909 DSAStack->isCancelRegion()); 8910 } 8911 8912 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 8913 SourceLocation StartLoc, 8914 SourceLocation EndLoc) { 8915 if (!AStmt) 8916 return StmtError(); 8917 8918 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8919 8920 setFunctionHasBranchProtectedScope(); 8921 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion()); 8922 8923 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt, 8924 DSAStack->isCancelRegion()); 8925 } 8926 8927 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 8928 Stmt *AStmt, 8929 SourceLocation StartLoc, 8930 SourceLocation EndLoc) { 8931 if (!AStmt) 8932 return StmtError(); 8933 8934 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8935 8936 setFunctionHasBranchProtectedScope(); 8937 8938 // OpenMP [2.7.3, single Construct, Restrictions] 8939 // The copyprivate clause must not be used with the nowait clause. 8940 const OMPClause *Nowait = nullptr; 8941 const OMPClause *Copyprivate = nullptr; 8942 for (const OMPClause *Clause : Clauses) { 8943 if (Clause->getClauseKind() == OMPC_nowait) 8944 Nowait = Clause; 8945 else if (Clause->getClauseKind() == OMPC_copyprivate) 8946 Copyprivate = Clause; 8947 if (Copyprivate && Nowait) { 8948 Diag(Copyprivate->getBeginLoc(), 8949 diag::err_omp_single_copyprivate_with_nowait); 8950 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here); 8951 return StmtError(); 8952 } 8953 } 8954 8955 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 8956 } 8957 8958 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt, 8959 SourceLocation StartLoc, 8960 SourceLocation EndLoc) { 8961 if (!AStmt) 8962 return StmtError(); 8963 8964 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8965 8966 setFunctionHasBranchProtectedScope(); 8967 8968 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt); 8969 } 8970 8971 StmtResult Sema::ActOnOpenMPCriticalDirective( 8972 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses, 8973 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { 8974 if (!AStmt) 8975 return StmtError(); 8976 8977 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 8978 8979 bool ErrorFound = false; 8980 llvm::APSInt Hint; 8981 SourceLocation HintLoc; 8982 bool DependentHint = false; 8983 for (const OMPClause *C : Clauses) { 8984 if (C->getClauseKind() == OMPC_hint) { 8985 if (!DirName.getName()) { 8986 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name); 8987 ErrorFound = true; 8988 } 8989 Expr *E = cast<OMPHintClause>(C)->getHint(); 8990 if (E->isTypeDependent() || E->isValueDependent() || 8991 E->isInstantiationDependent()) { 8992 DependentHint = true; 8993 } else { 8994 Hint = E->EvaluateKnownConstInt(Context); 8995 HintLoc = C->getBeginLoc(); 8996 } 8997 } 8998 } 8999 if (ErrorFound) 9000 return StmtError(); 9001 const auto Pair = DSAStack->getCriticalWithHint(DirName); 9002 if (Pair.first && DirName.getName() && !DependentHint) { 9003 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) { 9004 Diag(StartLoc, diag::err_omp_critical_with_hint); 9005 if (HintLoc.isValid()) 9006 Diag(HintLoc, diag::note_omp_critical_hint_here) 9007 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false); 9008 else 9009 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0; 9010 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) { 9011 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here) 9012 << 1 9013 << C->getHint()->EvaluateKnownConstInt(Context).toString( 9014 /*Radix=*/10, /*Signed=*/false); 9015 } else { 9016 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1; 9017 } 9018 } 9019 } 9020 9021 setFunctionHasBranchProtectedScope(); 9022 9023 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc, 9024 Clauses, AStmt); 9025 if (!Pair.first && DirName.getName() && !DependentHint) 9026 DSAStack->addCriticalWithHint(Dir, Hint); 9027 return Dir; 9028 } 9029 9030 StmtResult Sema::ActOnOpenMPParallelForDirective( 9031 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9032 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9033 if (!AStmt) 9034 return StmtError(); 9035 9036 auto *CS = cast<CapturedStmt>(AStmt); 9037 // 1.2.2 OpenMP Language Terminology 9038 // Structured block - An executable statement with a single entry at the 9039 // top and a single exit at the bottom. 9040 // The point of exit cannot be a branch out of the structured block. 9041 // longjmp() and throw() must not violate the entry/exit criteria. 9042 CS->getCapturedDecl()->setNothrow(); 9043 9044 OMPLoopDirective::HelperExprs B; 9045 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9046 // define the nested loops number. 9047 unsigned NestedLoopCount = 9048 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses), 9049 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 9050 VarsWithImplicitDSA, B); 9051 if (NestedLoopCount == 0) 9052 return StmtError(); 9053 9054 assert((CurContext->isDependentContext() || B.builtAll()) && 9055 "omp parallel for loop exprs were not built"); 9056 9057 if (!CurContext->isDependentContext()) { 9058 // Finalize the clauses that need pre-built expressions for CodeGen. 9059 for (OMPClause *C : Clauses) { 9060 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9061 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9062 B.NumIterations, *this, CurScope, 9063 DSAStack)) 9064 return StmtError(); 9065 } 9066 } 9067 9068 setFunctionHasBranchProtectedScope(); 9069 return OMPParallelForDirective::Create( 9070 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 9071 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 9072 } 9073 9074 StmtResult Sema::ActOnOpenMPParallelForSimdDirective( 9075 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 9076 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 9077 if (!AStmt) 9078 return StmtError(); 9079 9080 auto *CS = cast<CapturedStmt>(AStmt); 9081 // 1.2.2 OpenMP Language Terminology 9082 // Structured block - An executable statement with a single entry at the 9083 // top and a single exit at the bottom. 9084 // The point of exit cannot be a branch out of the structured block. 9085 // longjmp() and throw() must not violate the entry/exit criteria. 9086 CS->getCapturedDecl()->setNothrow(); 9087 9088 OMPLoopDirective::HelperExprs B; 9089 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 9090 // define the nested loops number. 9091 unsigned NestedLoopCount = 9092 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses), 9093 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack, 9094 VarsWithImplicitDSA, B); 9095 if (NestedLoopCount == 0) 9096 return StmtError(); 9097 9098 if (!CurContext->isDependentContext()) { 9099 // Finalize the clauses that need pre-built expressions for CodeGen. 9100 for (OMPClause *C : Clauses) { 9101 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 9102 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 9103 B.NumIterations, *this, CurScope, 9104 DSAStack)) 9105 return StmtError(); 9106 } 9107 } 9108 9109 if (checkSimdlenSafelenSpecified(*this, Clauses)) 9110 return StmtError(); 9111 9112 setFunctionHasBranchProtectedScope(); 9113 return OMPParallelForSimdDirective::Create( 9114 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 9115 } 9116 9117 StmtResult 9118 Sema::ActOnOpenMPParallelMasterDirective(ArrayRef<OMPClause *> Clauses, 9119 Stmt *AStmt, SourceLocation StartLoc, 9120 SourceLocation EndLoc) { 9121 if (!AStmt) 9122 return StmtError(); 9123 9124 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9125 auto *CS = cast<CapturedStmt>(AStmt); 9126 // 1.2.2 OpenMP Language Terminology 9127 // Structured block - An executable statement with a single entry at the 9128 // top and a single exit at the bottom. 9129 // The point of exit cannot be a branch out of the structured block. 9130 // longjmp() and throw() must not violate the entry/exit criteria. 9131 CS->getCapturedDecl()->setNothrow(); 9132 9133 setFunctionHasBranchProtectedScope(); 9134 9135 return OMPParallelMasterDirective::Create( 9136 Context, StartLoc, EndLoc, Clauses, AStmt, 9137 DSAStack->getTaskgroupReductionRef()); 9138 } 9139 9140 StmtResult 9141 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses, 9142 Stmt *AStmt, SourceLocation StartLoc, 9143 SourceLocation EndLoc) { 9144 if (!AStmt) 9145 return StmtError(); 9146 9147 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9148 auto BaseStmt = AStmt; 9149 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 9150 BaseStmt = CS->getCapturedStmt(); 9151 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 9152 auto S = C->children(); 9153 if (S.begin() == S.end()) 9154 return StmtError(); 9155 // All associated statements must be '#pragma omp section' except for 9156 // the first one. 9157 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) { 9158 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 9159 if (SectionStmt) 9160 Diag(SectionStmt->getBeginLoc(), 9161 diag::err_omp_parallel_sections_substmt_not_section); 9162 return StmtError(); 9163 } 9164 cast<OMPSectionDirective>(SectionStmt) 9165 ->setHasCancel(DSAStack->isCancelRegion()); 9166 } 9167 } else { 9168 Diag(AStmt->getBeginLoc(), 9169 diag::err_omp_parallel_sections_not_compound_stmt); 9170 return StmtError(); 9171 } 9172 9173 setFunctionHasBranchProtectedScope(); 9174 9175 return OMPParallelSectionsDirective::Create( 9176 Context, StartLoc, EndLoc, Clauses, AStmt, 9177 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 9178 } 9179 9180 /// detach and mergeable clauses are mutially exclusive, check for it. 9181 static bool checkDetachMergeableClauses(Sema &S, 9182 ArrayRef<OMPClause *> Clauses) { 9183 const OMPClause *PrevClause = nullptr; 9184 bool ErrorFound = false; 9185 for (const OMPClause *C : Clauses) { 9186 if (C->getClauseKind() == OMPC_detach || 9187 C->getClauseKind() == OMPC_mergeable) { 9188 if (!PrevClause) { 9189 PrevClause = C; 9190 } else if (PrevClause->getClauseKind() != C->getClauseKind()) { 9191 S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive) 9192 << getOpenMPClauseName(C->getClauseKind()) 9193 << getOpenMPClauseName(PrevClause->getClauseKind()); 9194 S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause) 9195 << getOpenMPClauseName(PrevClause->getClauseKind()); 9196 ErrorFound = true; 9197 } 9198 } 9199 } 9200 return ErrorFound; 9201 } 9202 9203 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses, 9204 Stmt *AStmt, SourceLocation StartLoc, 9205 SourceLocation EndLoc) { 9206 if (!AStmt) 9207 return StmtError(); 9208 9209 // OpenMP 5.0, 2.10.1 task Construct 9210 // If a detach clause appears on the directive, then a mergeable clause cannot 9211 // appear on the same directive. 9212 if (checkDetachMergeableClauses(*this, Clauses)) 9213 return StmtError(); 9214 9215 auto *CS = cast<CapturedStmt>(AStmt); 9216 // 1.2.2 OpenMP Language Terminology 9217 // Structured block - An executable statement with a single entry at the 9218 // top and a single exit at the bottom. 9219 // The point of exit cannot be a branch out of the structured block. 9220 // longjmp() and throw() must not violate the entry/exit criteria. 9221 CS->getCapturedDecl()->setNothrow(); 9222 9223 setFunctionHasBranchProtectedScope(); 9224 9225 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 9226 DSAStack->isCancelRegion()); 9227 } 9228 9229 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc, 9230 SourceLocation EndLoc) { 9231 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc); 9232 } 9233 9234 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc, 9235 SourceLocation EndLoc) { 9236 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc); 9237 } 9238 9239 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc, 9240 SourceLocation EndLoc) { 9241 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc); 9242 } 9243 9244 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses, 9245 Stmt *AStmt, 9246 SourceLocation StartLoc, 9247 SourceLocation EndLoc) { 9248 if (!AStmt) 9249 return StmtError(); 9250 9251 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9252 9253 setFunctionHasBranchProtectedScope(); 9254 9255 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses, 9256 AStmt, 9257 DSAStack->getTaskgroupReductionRef()); 9258 } 9259 9260 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses, 9261 SourceLocation StartLoc, 9262 SourceLocation EndLoc) { 9263 OMPFlushClause *FC = nullptr; 9264 OMPClause *OrderClause = nullptr; 9265 for (OMPClause *C : Clauses) { 9266 if (C->getClauseKind() == OMPC_flush) 9267 FC = cast<OMPFlushClause>(C); 9268 else 9269 OrderClause = C; 9270 } 9271 OpenMPClauseKind MemOrderKind = OMPC_unknown; 9272 SourceLocation MemOrderLoc; 9273 for (const OMPClause *C : Clauses) { 9274 if (C->getClauseKind() == OMPC_acq_rel || 9275 C->getClauseKind() == OMPC_acquire || 9276 C->getClauseKind() == OMPC_release) { 9277 if (MemOrderKind != OMPC_unknown) { 9278 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 9279 << getOpenMPDirectiveName(OMPD_flush) << 1 9280 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 9281 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 9282 << getOpenMPClauseName(MemOrderKind); 9283 } else { 9284 MemOrderKind = C->getClauseKind(); 9285 MemOrderLoc = C->getBeginLoc(); 9286 } 9287 } 9288 } 9289 if (FC && OrderClause) { 9290 Diag(FC->getLParenLoc(), diag::err_omp_flush_order_clause_and_list) 9291 << getOpenMPClauseName(OrderClause->getClauseKind()); 9292 Diag(OrderClause->getBeginLoc(), diag::note_omp_flush_order_clause_here) 9293 << getOpenMPClauseName(OrderClause->getClauseKind()); 9294 return StmtError(); 9295 } 9296 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses); 9297 } 9298 9299 StmtResult Sema::ActOnOpenMPDepobjDirective(ArrayRef<OMPClause *> Clauses, 9300 SourceLocation StartLoc, 9301 SourceLocation EndLoc) { 9302 if (Clauses.empty()) { 9303 Diag(StartLoc, diag::err_omp_depobj_expected); 9304 return StmtError(); 9305 } else if (Clauses[0]->getClauseKind() != OMPC_depobj) { 9306 Diag(Clauses[0]->getBeginLoc(), diag::err_omp_depobj_expected); 9307 return StmtError(); 9308 } 9309 // Only depobj expression and another single clause is allowed. 9310 if (Clauses.size() > 2) { 9311 Diag(Clauses[2]->getBeginLoc(), 9312 diag::err_omp_depobj_single_clause_expected); 9313 return StmtError(); 9314 } else if (Clauses.size() < 1) { 9315 Diag(Clauses[0]->getEndLoc(), diag::err_omp_depobj_single_clause_expected); 9316 return StmtError(); 9317 } 9318 return OMPDepobjDirective::Create(Context, StartLoc, EndLoc, Clauses); 9319 } 9320 9321 StmtResult Sema::ActOnOpenMPScanDirective(ArrayRef<OMPClause *> Clauses, 9322 SourceLocation StartLoc, 9323 SourceLocation EndLoc) { 9324 // Check that exactly one clause is specified. 9325 if (Clauses.size() != 1) { 9326 Diag(Clauses.empty() ? EndLoc : Clauses[1]->getBeginLoc(), 9327 diag::err_omp_scan_single_clause_expected); 9328 return StmtError(); 9329 } 9330 // Check that scan directive is used in the scopeof the OpenMP loop body. 9331 if (Scope *S = DSAStack->getCurScope()) { 9332 Scope *ParentS = S->getParent(); 9333 if (!ParentS || ParentS->getParent() != ParentS->getBreakParent() || 9334 !ParentS->getBreakParent()->isOpenMPLoopScope()) 9335 return StmtError(Diag(StartLoc, diag::err_omp_orphaned_device_directive) 9336 << getOpenMPDirectiveName(OMPD_scan) << 5); 9337 } 9338 // Check that only one instance of scan directives is used in the same outer 9339 // region. 9340 if (DSAStack->doesParentHasScanDirective()) { 9341 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "scan"; 9342 Diag(DSAStack->getParentScanDirectiveLoc(), 9343 diag::note_omp_previous_directive) 9344 << "scan"; 9345 return StmtError(); 9346 } 9347 DSAStack->setParentHasScanDirective(StartLoc); 9348 return OMPScanDirective::Create(Context, StartLoc, EndLoc, Clauses); 9349 } 9350 9351 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses, 9352 Stmt *AStmt, 9353 SourceLocation StartLoc, 9354 SourceLocation EndLoc) { 9355 const OMPClause *DependFound = nullptr; 9356 const OMPClause *DependSourceClause = nullptr; 9357 const OMPClause *DependSinkClause = nullptr; 9358 bool ErrorFound = false; 9359 const OMPThreadsClause *TC = nullptr; 9360 const OMPSIMDClause *SC = nullptr; 9361 for (const OMPClause *C : Clauses) { 9362 if (auto *DC = dyn_cast<OMPDependClause>(C)) { 9363 DependFound = C; 9364 if (DC->getDependencyKind() == OMPC_DEPEND_source) { 9365 if (DependSourceClause) { 9366 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause) 9367 << getOpenMPDirectiveName(OMPD_ordered) 9368 << getOpenMPClauseName(OMPC_depend) << 2; 9369 ErrorFound = true; 9370 } else { 9371 DependSourceClause = C; 9372 } 9373 if (DependSinkClause) { 9374 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 9375 << 0; 9376 ErrorFound = true; 9377 } 9378 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) { 9379 if (DependSourceClause) { 9380 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed) 9381 << 1; 9382 ErrorFound = true; 9383 } 9384 DependSinkClause = C; 9385 } 9386 } else if (C->getClauseKind() == OMPC_threads) { 9387 TC = cast<OMPThreadsClause>(C); 9388 } else if (C->getClauseKind() == OMPC_simd) { 9389 SC = cast<OMPSIMDClause>(C); 9390 } 9391 } 9392 if (!ErrorFound && !SC && 9393 isOpenMPSimdDirective(DSAStack->getParentDirective())) { 9394 // OpenMP [2.8.1,simd Construct, Restrictions] 9395 // An ordered construct with the simd clause is the only OpenMP construct 9396 // that can appear in the simd region. 9397 Diag(StartLoc, diag::err_omp_prohibited_region_simd) 9398 << (LangOpts.OpenMP >= 50 ? 1 : 0); 9399 ErrorFound = true; 9400 } else if (DependFound && (TC || SC)) { 9401 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd) 9402 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind()); 9403 ErrorFound = true; 9404 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) { 9405 Diag(DependFound->getBeginLoc(), 9406 diag::err_omp_ordered_directive_without_param); 9407 ErrorFound = true; 9408 } else if (TC || Clauses.empty()) { 9409 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) { 9410 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc; 9411 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) 9412 << (TC != nullptr); 9413 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param) << 1; 9414 ErrorFound = true; 9415 } 9416 } 9417 if ((!AStmt && !DependFound) || ErrorFound) 9418 return StmtError(); 9419 9420 // OpenMP 5.0, 2.17.9, ordered Construct, Restrictions. 9421 // During execution of an iteration of a worksharing-loop or a loop nest 9422 // within a worksharing-loop, simd, or worksharing-loop SIMD region, a thread 9423 // must not execute more than one ordered region corresponding to an ordered 9424 // construct without a depend clause. 9425 if (!DependFound) { 9426 if (DSAStack->doesParentHasOrderedDirective()) { 9427 Diag(StartLoc, diag::err_omp_several_directives_in_region) << "ordered"; 9428 Diag(DSAStack->getParentOrderedDirectiveLoc(), 9429 diag::note_omp_previous_directive) 9430 << "ordered"; 9431 return StmtError(); 9432 } 9433 DSAStack->setParentHasOrderedDirective(StartLoc); 9434 } 9435 9436 if (AStmt) { 9437 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 9438 9439 setFunctionHasBranchProtectedScope(); 9440 } 9441 9442 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 9443 } 9444 9445 namespace { 9446 /// Helper class for checking expression in 'omp atomic [update]' 9447 /// construct. 9448 class OpenMPAtomicUpdateChecker { 9449 /// Error results for atomic update expressions. 9450 enum ExprAnalysisErrorCode { 9451 /// A statement is not an expression statement. 9452 NotAnExpression, 9453 /// Expression is not builtin binary or unary operation. 9454 NotABinaryOrUnaryExpression, 9455 /// Unary operation is not post-/pre- increment/decrement operation. 9456 NotAnUnaryIncDecExpression, 9457 /// An expression is not of scalar type. 9458 NotAScalarType, 9459 /// A binary operation is not an assignment operation. 9460 NotAnAssignmentOp, 9461 /// RHS part of the binary operation is not a binary expression. 9462 NotABinaryExpression, 9463 /// RHS part is not additive/multiplicative/shift/biwise binary 9464 /// expression. 9465 NotABinaryOperator, 9466 /// RHS binary operation does not have reference to the updated LHS 9467 /// part. 9468 NotAnUpdateExpression, 9469 /// No errors is found. 9470 NoError 9471 }; 9472 /// Reference to Sema. 9473 Sema &SemaRef; 9474 /// A location for note diagnostics (when error is found). 9475 SourceLocation NoteLoc; 9476 /// 'x' lvalue part of the source atomic expression. 9477 Expr *X; 9478 /// 'expr' rvalue part of the source atomic expression. 9479 Expr *E; 9480 /// Helper expression of the form 9481 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 9482 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 9483 Expr *UpdateExpr; 9484 /// Is 'x' a LHS in a RHS part of full update expression. It is 9485 /// important for non-associative operations. 9486 bool IsXLHSInRHSPart; 9487 BinaryOperatorKind Op; 9488 SourceLocation OpLoc; 9489 /// true if the source expression is a postfix unary operation, false 9490 /// if it is a prefix unary operation. 9491 bool IsPostfixUpdate; 9492 9493 public: 9494 OpenMPAtomicUpdateChecker(Sema &SemaRef) 9495 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr), 9496 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {} 9497 /// Check specified statement that it is suitable for 'atomic update' 9498 /// constructs and extract 'x', 'expr' and Operation from the original 9499 /// expression. If DiagId and NoteId == 0, then only check is performed 9500 /// without error notification. 9501 /// \param DiagId Diagnostic which should be emitted if error is found. 9502 /// \param NoteId Diagnostic note for the main error message. 9503 /// \return true if statement is not an update expression, false otherwise. 9504 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0); 9505 /// Return the 'x' lvalue part of the source atomic expression. 9506 Expr *getX() const { return X; } 9507 /// Return the 'expr' rvalue part of the source atomic expression. 9508 Expr *getExpr() const { return E; } 9509 /// Return the update expression used in calculation of the updated 9510 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or 9511 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'. 9512 Expr *getUpdateExpr() const { return UpdateExpr; } 9513 /// Return true if 'x' is LHS in RHS part of full update expression, 9514 /// false otherwise. 9515 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; } 9516 9517 /// true if the source expression is a postfix unary operation, false 9518 /// if it is a prefix unary operation. 9519 bool isPostfixUpdate() const { return IsPostfixUpdate; } 9520 9521 private: 9522 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0, 9523 unsigned NoteId = 0); 9524 }; 9525 } // namespace 9526 9527 bool OpenMPAtomicUpdateChecker::checkBinaryOperation( 9528 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) { 9529 ExprAnalysisErrorCode ErrorFound = NoError; 9530 SourceLocation ErrorLoc, NoteLoc; 9531 SourceRange ErrorRange, NoteRange; 9532 // Allowed constructs are: 9533 // x = x binop expr; 9534 // x = expr binop x; 9535 if (AtomicBinOp->getOpcode() == BO_Assign) { 9536 X = AtomicBinOp->getLHS(); 9537 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>( 9538 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) { 9539 if (AtomicInnerBinOp->isMultiplicativeOp() || 9540 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() || 9541 AtomicInnerBinOp->isBitwiseOp()) { 9542 Op = AtomicInnerBinOp->getOpcode(); 9543 OpLoc = AtomicInnerBinOp->getOperatorLoc(); 9544 Expr *LHS = AtomicInnerBinOp->getLHS(); 9545 Expr *RHS = AtomicInnerBinOp->getRHS(); 9546 llvm::FoldingSetNodeID XId, LHSId, RHSId; 9547 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(), 9548 /*Canonical=*/true); 9549 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(), 9550 /*Canonical=*/true); 9551 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(), 9552 /*Canonical=*/true); 9553 if (XId == LHSId) { 9554 E = RHS; 9555 IsXLHSInRHSPart = true; 9556 } else if (XId == RHSId) { 9557 E = LHS; 9558 IsXLHSInRHSPart = false; 9559 } else { 9560 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 9561 ErrorRange = AtomicInnerBinOp->getSourceRange(); 9562 NoteLoc = X->getExprLoc(); 9563 NoteRange = X->getSourceRange(); 9564 ErrorFound = NotAnUpdateExpression; 9565 } 9566 } else { 9567 ErrorLoc = AtomicInnerBinOp->getExprLoc(); 9568 ErrorRange = AtomicInnerBinOp->getSourceRange(); 9569 NoteLoc = AtomicInnerBinOp->getOperatorLoc(); 9570 NoteRange = SourceRange(NoteLoc, NoteLoc); 9571 ErrorFound = NotABinaryOperator; 9572 } 9573 } else { 9574 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc(); 9575 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange(); 9576 ErrorFound = NotABinaryExpression; 9577 } 9578 } else { 9579 ErrorLoc = AtomicBinOp->getExprLoc(); 9580 ErrorRange = AtomicBinOp->getSourceRange(); 9581 NoteLoc = AtomicBinOp->getOperatorLoc(); 9582 NoteRange = SourceRange(NoteLoc, NoteLoc); 9583 ErrorFound = NotAnAssignmentOp; 9584 } 9585 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 9586 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 9587 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 9588 return true; 9589 } 9590 if (SemaRef.CurContext->isDependentContext()) 9591 E = X = UpdateExpr = nullptr; 9592 return ErrorFound != NoError; 9593 } 9594 9595 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId, 9596 unsigned NoteId) { 9597 ExprAnalysisErrorCode ErrorFound = NoError; 9598 SourceLocation ErrorLoc, NoteLoc; 9599 SourceRange ErrorRange, NoteRange; 9600 // Allowed constructs are: 9601 // x++; 9602 // x--; 9603 // ++x; 9604 // --x; 9605 // x binop= expr; 9606 // x = x binop expr; 9607 // x = expr binop x; 9608 if (auto *AtomicBody = dyn_cast<Expr>(S)) { 9609 AtomicBody = AtomicBody->IgnoreParenImpCasts(); 9610 if (AtomicBody->getType()->isScalarType() || 9611 AtomicBody->isInstantiationDependent()) { 9612 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>( 9613 AtomicBody->IgnoreParenImpCasts())) { 9614 // Check for Compound Assignment Operation 9615 Op = BinaryOperator::getOpForCompoundAssignment( 9616 AtomicCompAssignOp->getOpcode()); 9617 OpLoc = AtomicCompAssignOp->getOperatorLoc(); 9618 E = AtomicCompAssignOp->getRHS(); 9619 X = AtomicCompAssignOp->getLHS()->IgnoreParens(); 9620 IsXLHSInRHSPart = true; 9621 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>( 9622 AtomicBody->IgnoreParenImpCasts())) { 9623 // Check for Binary Operation 9624 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId)) 9625 return true; 9626 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>( 9627 AtomicBody->IgnoreParenImpCasts())) { 9628 // Check for Unary Operation 9629 if (AtomicUnaryOp->isIncrementDecrementOp()) { 9630 IsPostfixUpdate = AtomicUnaryOp->isPostfix(); 9631 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub; 9632 OpLoc = AtomicUnaryOp->getOperatorLoc(); 9633 X = AtomicUnaryOp->getSubExpr()->IgnoreParens(); 9634 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get(); 9635 IsXLHSInRHSPart = true; 9636 } else { 9637 ErrorFound = NotAnUnaryIncDecExpression; 9638 ErrorLoc = AtomicUnaryOp->getExprLoc(); 9639 ErrorRange = AtomicUnaryOp->getSourceRange(); 9640 NoteLoc = AtomicUnaryOp->getOperatorLoc(); 9641 NoteRange = SourceRange(NoteLoc, NoteLoc); 9642 } 9643 } else if (!AtomicBody->isInstantiationDependent()) { 9644 ErrorFound = NotABinaryOrUnaryExpression; 9645 NoteLoc = ErrorLoc = AtomicBody->getExprLoc(); 9646 NoteRange = ErrorRange = AtomicBody->getSourceRange(); 9647 } 9648 } else { 9649 ErrorFound = NotAScalarType; 9650 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc(); 9651 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 9652 } 9653 } else { 9654 ErrorFound = NotAnExpression; 9655 NoteLoc = ErrorLoc = S->getBeginLoc(); 9656 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 9657 } 9658 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) { 9659 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange; 9660 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange; 9661 return true; 9662 } 9663 if (SemaRef.CurContext->isDependentContext()) 9664 E = X = UpdateExpr = nullptr; 9665 if (ErrorFound == NoError && E && X) { 9666 // Build an update expression of form 'OpaqueValueExpr(x) binop 9667 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop 9668 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression. 9669 auto *OVEX = new (SemaRef.getASTContext()) 9670 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue); 9671 auto *OVEExpr = new (SemaRef.getASTContext()) 9672 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue); 9673 ExprResult Update = 9674 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr, 9675 IsXLHSInRHSPart ? OVEExpr : OVEX); 9676 if (Update.isInvalid()) 9677 return true; 9678 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(), 9679 Sema::AA_Casting); 9680 if (Update.isInvalid()) 9681 return true; 9682 UpdateExpr = Update.get(); 9683 } 9684 return ErrorFound != NoError; 9685 } 9686 9687 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses, 9688 Stmt *AStmt, 9689 SourceLocation StartLoc, 9690 SourceLocation EndLoc) { 9691 // Register location of the first atomic directive. 9692 DSAStack->addAtomicDirectiveLoc(StartLoc); 9693 if (!AStmt) 9694 return StmtError(); 9695 9696 auto *CS = cast<CapturedStmt>(AStmt); 9697 // 1.2.2 OpenMP Language Terminology 9698 // Structured block - An executable statement with a single entry at the 9699 // top and a single exit at the bottom. 9700 // The point of exit cannot be a branch out of the structured block. 9701 // longjmp() and throw() must not violate the entry/exit criteria. 9702 OpenMPClauseKind AtomicKind = OMPC_unknown; 9703 SourceLocation AtomicKindLoc; 9704 OpenMPClauseKind MemOrderKind = OMPC_unknown; 9705 SourceLocation MemOrderLoc; 9706 for (const OMPClause *C : Clauses) { 9707 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write || 9708 C->getClauseKind() == OMPC_update || 9709 C->getClauseKind() == OMPC_capture) { 9710 if (AtomicKind != OMPC_unknown) { 9711 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses) 9712 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 9713 Diag(AtomicKindLoc, diag::note_omp_previous_mem_order_clause) 9714 << getOpenMPClauseName(AtomicKind); 9715 } else { 9716 AtomicKind = C->getClauseKind(); 9717 AtomicKindLoc = C->getBeginLoc(); 9718 } 9719 } 9720 if (C->getClauseKind() == OMPC_seq_cst || 9721 C->getClauseKind() == OMPC_acq_rel || 9722 C->getClauseKind() == OMPC_acquire || 9723 C->getClauseKind() == OMPC_release || 9724 C->getClauseKind() == OMPC_relaxed) { 9725 if (MemOrderKind != OMPC_unknown) { 9726 Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses) 9727 << getOpenMPDirectiveName(OMPD_atomic) << 0 9728 << SourceRange(C->getBeginLoc(), C->getEndLoc()); 9729 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 9730 << getOpenMPClauseName(MemOrderKind); 9731 } else { 9732 MemOrderKind = C->getClauseKind(); 9733 MemOrderLoc = C->getBeginLoc(); 9734 } 9735 } 9736 } 9737 // OpenMP 5.0, 2.17.7 atomic Construct, Restrictions 9738 // If atomic-clause is read then memory-order-clause must not be acq_rel or 9739 // release. 9740 // If atomic-clause is write then memory-order-clause must not be acq_rel or 9741 // acquire. 9742 // If atomic-clause is update or not present then memory-order-clause must not 9743 // be acq_rel or acquire. 9744 if ((AtomicKind == OMPC_read && 9745 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_release)) || 9746 ((AtomicKind == OMPC_write || AtomicKind == OMPC_update || 9747 AtomicKind == OMPC_unknown) && 9748 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_acquire))) { 9749 SourceLocation Loc = AtomicKindLoc; 9750 if (AtomicKind == OMPC_unknown) 9751 Loc = StartLoc; 9752 Diag(Loc, diag::err_omp_atomic_incompatible_mem_order_clause) 9753 << getOpenMPClauseName(AtomicKind) 9754 << (AtomicKind == OMPC_unknown ? 1 : 0) 9755 << getOpenMPClauseName(MemOrderKind); 9756 Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause) 9757 << getOpenMPClauseName(MemOrderKind); 9758 } 9759 9760 Stmt *Body = CS->getCapturedStmt(); 9761 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body)) 9762 Body = EWC->getSubExpr(); 9763 9764 Expr *X = nullptr; 9765 Expr *V = nullptr; 9766 Expr *E = nullptr; 9767 Expr *UE = nullptr; 9768 bool IsXLHSInRHSPart = false; 9769 bool IsPostfixUpdate = false; 9770 // OpenMP [2.12.6, atomic Construct] 9771 // In the next expressions: 9772 // * x and v (as applicable) are both l-value expressions with scalar type. 9773 // * During the execution of an atomic region, multiple syntactic 9774 // occurrences of x must designate the same storage location. 9775 // * Neither of v and expr (as applicable) may access the storage location 9776 // designated by x. 9777 // * Neither of x and expr (as applicable) may access the storage location 9778 // designated by v. 9779 // * expr is an expression with scalar type. 9780 // * binop is one of +, *, -, /, &, ^, |, <<, or >>. 9781 // * binop, binop=, ++, and -- are not overloaded operators. 9782 // * The expression x binop expr must be numerically equivalent to x binop 9783 // (expr). This requirement is satisfied if the operators in expr have 9784 // precedence greater than binop, or by using parentheses around expr or 9785 // subexpressions of expr. 9786 // * The expression expr binop x must be numerically equivalent to (expr) 9787 // binop x. This requirement is satisfied if the operators in expr have 9788 // precedence equal to or greater than binop, or by using parentheses around 9789 // expr or subexpressions of expr. 9790 // * For forms that allow multiple occurrences of x, the number of times 9791 // that x is evaluated is unspecified. 9792 if (AtomicKind == OMPC_read) { 9793 enum { 9794 NotAnExpression, 9795 NotAnAssignmentOp, 9796 NotAScalarType, 9797 NotAnLValue, 9798 NoError 9799 } ErrorFound = NoError; 9800 SourceLocation ErrorLoc, NoteLoc; 9801 SourceRange ErrorRange, NoteRange; 9802 // If clause is read: 9803 // v = x; 9804 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 9805 const auto *AtomicBinOp = 9806 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 9807 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 9808 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 9809 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts(); 9810 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 9811 (V->isInstantiationDependent() || V->getType()->isScalarType())) { 9812 if (!X->isLValue() || !V->isLValue()) { 9813 const Expr *NotLValueExpr = X->isLValue() ? V : X; 9814 ErrorFound = NotAnLValue; 9815 ErrorLoc = AtomicBinOp->getExprLoc(); 9816 ErrorRange = AtomicBinOp->getSourceRange(); 9817 NoteLoc = NotLValueExpr->getExprLoc(); 9818 NoteRange = NotLValueExpr->getSourceRange(); 9819 } 9820 } else if (!X->isInstantiationDependent() || 9821 !V->isInstantiationDependent()) { 9822 const Expr *NotScalarExpr = 9823 (X->isInstantiationDependent() || X->getType()->isScalarType()) 9824 ? V 9825 : X; 9826 ErrorFound = NotAScalarType; 9827 ErrorLoc = AtomicBinOp->getExprLoc(); 9828 ErrorRange = AtomicBinOp->getSourceRange(); 9829 NoteLoc = NotScalarExpr->getExprLoc(); 9830 NoteRange = NotScalarExpr->getSourceRange(); 9831 } 9832 } else if (!AtomicBody->isInstantiationDependent()) { 9833 ErrorFound = NotAnAssignmentOp; 9834 ErrorLoc = AtomicBody->getExprLoc(); 9835 ErrorRange = AtomicBody->getSourceRange(); 9836 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 9837 : AtomicBody->getExprLoc(); 9838 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 9839 : AtomicBody->getSourceRange(); 9840 } 9841 } else { 9842 ErrorFound = NotAnExpression; 9843 NoteLoc = ErrorLoc = Body->getBeginLoc(); 9844 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 9845 } 9846 if (ErrorFound != NoError) { 9847 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement) 9848 << ErrorRange; 9849 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 9850 << NoteRange; 9851 return StmtError(); 9852 } 9853 if (CurContext->isDependentContext()) 9854 V = X = nullptr; 9855 } else if (AtomicKind == OMPC_write) { 9856 enum { 9857 NotAnExpression, 9858 NotAnAssignmentOp, 9859 NotAScalarType, 9860 NotAnLValue, 9861 NoError 9862 } ErrorFound = NoError; 9863 SourceLocation ErrorLoc, NoteLoc; 9864 SourceRange ErrorRange, NoteRange; 9865 // If clause is write: 9866 // x = expr; 9867 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 9868 const auto *AtomicBinOp = 9869 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 9870 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 9871 X = AtomicBinOp->getLHS(); 9872 E = AtomicBinOp->getRHS(); 9873 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) && 9874 (E->isInstantiationDependent() || E->getType()->isScalarType())) { 9875 if (!X->isLValue()) { 9876 ErrorFound = NotAnLValue; 9877 ErrorLoc = AtomicBinOp->getExprLoc(); 9878 ErrorRange = AtomicBinOp->getSourceRange(); 9879 NoteLoc = X->getExprLoc(); 9880 NoteRange = X->getSourceRange(); 9881 } 9882 } else if (!X->isInstantiationDependent() || 9883 !E->isInstantiationDependent()) { 9884 const Expr *NotScalarExpr = 9885 (X->isInstantiationDependent() || X->getType()->isScalarType()) 9886 ? E 9887 : X; 9888 ErrorFound = NotAScalarType; 9889 ErrorLoc = AtomicBinOp->getExprLoc(); 9890 ErrorRange = AtomicBinOp->getSourceRange(); 9891 NoteLoc = NotScalarExpr->getExprLoc(); 9892 NoteRange = NotScalarExpr->getSourceRange(); 9893 } 9894 } else if (!AtomicBody->isInstantiationDependent()) { 9895 ErrorFound = NotAnAssignmentOp; 9896 ErrorLoc = AtomicBody->getExprLoc(); 9897 ErrorRange = AtomicBody->getSourceRange(); 9898 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 9899 : AtomicBody->getExprLoc(); 9900 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 9901 : AtomicBody->getSourceRange(); 9902 } 9903 } else { 9904 ErrorFound = NotAnExpression; 9905 NoteLoc = ErrorLoc = Body->getBeginLoc(); 9906 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc); 9907 } 9908 if (ErrorFound != NoError) { 9909 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement) 9910 << ErrorRange; 9911 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound 9912 << NoteRange; 9913 return StmtError(); 9914 } 9915 if (CurContext->isDependentContext()) 9916 E = X = nullptr; 9917 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) { 9918 // If clause is update: 9919 // x++; 9920 // x--; 9921 // ++x; 9922 // --x; 9923 // x binop= expr; 9924 // x = x binop expr; 9925 // x = expr binop x; 9926 OpenMPAtomicUpdateChecker Checker(*this); 9927 if (Checker.checkStatement( 9928 Body, (AtomicKind == OMPC_update) 9929 ? diag::err_omp_atomic_update_not_expression_statement 9930 : diag::err_omp_atomic_not_expression_statement, 9931 diag::note_omp_atomic_update)) 9932 return StmtError(); 9933 if (!CurContext->isDependentContext()) { 9934 E = Checker.getExpr(); 9935 X = Checker.getX(); 9936 UE = Checker.getUpdateExpr(); 9937 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 9938 } 9939 } else if (AtomicKind == OMPC_capture) { 9940 enum { 9941 NotAnAssignmentOp, 9942 NotACompoundStatement, 9943 NotTwoSubstatements, 9944 NotASpecificExpression, 9945 NoError 9946 } ErrorFound = NoError; 9947 SourceLocation ErrorLoc, NoteLoc; 9948 SourceRange ErrorRange, NoteRange; 9949 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) { 9950 // If clause is a capture: 9951 // v = x++; 9952 // v = x--; 9953 // v = ++x; 9954 // v = --x; 9955 // v = x binop= expr; 9956 // v = x = x binop expr; 9957 // v = x = expr binop x; 9958 const auto *AtomicBinOp = 9959 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts()); 9960 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) { 9961 V = AtomicBinOp->getLHS(); 9962 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts(); 9963 OpenMPAtomicUpdateChecker Checker(*this); 9964 if (Checker.checkStatement( 9965 Body, diag::err_omp_atomic_capture_not_expression_statement, 9966 diag::note_omp_atomic_update)) 9967 return StmtError(); 9968 E = Checker.getExpr(); 9969 X = Checker.getX(); 9970 UE = Checker.getUpdateExpr(); 9971 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 9972 IsPostfixUpdate = Checker.isPostfixUpdate(); 9973 } else if (!AtomicBody->isInstantiationDependent()) { 9974 ErrorLoc = AtomicBody->getExprLoc(); 9975 ErrorRange = AtomicBody->getSourceRange(); 9976 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc() 9977 : AtomicBody->getExprLoc(); 9978 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange() 9979 : AtomicBody->getSourceRange(); 9980 ErrorFound = NotAnAssignmentOp; 9981 } 9982 if (ErrorFound != NoError) { 9983 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement) 9984 << ErrorRange; 9985 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 9986 return StmtError(); 9987 } 9988 if (CurContext->isDependentContext()) 9989 UE = V = E = X = nullptr; 9990 } else { 9991 // If clause is a capture: 9992 // { v = x; x = expr; } 9993 // { v = x; x++; } 9994 // { v = x; x--; } 9995 // { v = x; ++x; } 9996 // { v = x; --x; } 9997 // { v = x; x binop= expr; } 9998 // { v = x; x = x binop expr; } 9999 // { v = x; x = expr binop x; } 10000 // { x++; v = x; } 10001 // { x--; v = x; } 10002 // { ++x; v = x; } 10003 // { --x; v = x; } 10004 // { x binop= expr; v = x; } 10005 // { x = x binop expr; v = x; } 10006 // { x = expr binop x; v = x; } 10007 if (auto *CS = dyn_cast<CompoundStmt>(Body)) { 10008 // Check that this is { expr1; expr2; } 10009 if (CS->size() == 2) { 10010 Stmt *First = CS->body_front(); 10011 Stmt *Second = CS->body_back(); 10012 if (auto *EWC = dyn_cast<ExprWithCleanups>(First)) 10013 First = EWC->getSubExpr()->IgnoreParenImpCasts(); 10014 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second)) 10015 Second = EWC->getSubExpr()->IgnoreParenImpCasts(); 10016 // Need to find what subexpression is 'v' and what is 'x'. 10017 OpenMPAtomicUpdateChecker Checker(*this); 10018 bool IsUpdateExprFound = !Checker.checkStatement(Second); 10019 BinaryOperator *BinOp = nullptr; 10020 if (IsUpdateExprFound) { 10021 BinOp = dyn_cast<BinaryOperator>(First); 10022 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 10023 } 10024 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 10025 // { v = x; x++; } 10026 // { v = x; x--; } 10027 // { v = x; ++x; } 10028 // { v = x; --x; } 10029 // { v = x; x binop= expr; } 10030 // { v = x; x = x binop expr; } 10031 // { v = x; x = expr binop x; } 10032 // Check that the first expression has form v = x. 10033 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 10034 llvm::FoldingSetNodeID XId, PossibleXId; 10035 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 10036 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 10037 IsUpdateExprFound = XId == PossibleXId; 10038 if (IsUpdateExprFound) { 10039 V = BinOp->getLHS(); 10040 X = Checker.getX(); 10041 E = Checker.getExpr(); 10042 UE = Checker.getUpdateExpr(); 10043 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10044 IsPostfixUpdate = true; 10045 } 10046 } 10047 if (!IsUpdateExprFound) { 10048 IsUpdateExprFound = !Checker.checkStatement(First); 10049 BinOp = nullptr; 10050 if (IsUpdateExprFound) { 10051 BinOp = dyn_cast<BinaryOperator>(Second); 10052 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign; 10053 } 10054 if (IsUpdateExprFound && !CurContext->isDependentContext()) { 10055 // { x++; v = x; } 10056 // { x--; v = x; } 10057 // { ++x; v = x; } 10058 // { --x; v = x; } 10059 // { x binop= expr; v = x; } 10060 // { x = x binop expr; v = x; } 10061 // { x = expr binop x; v = x; } 10062 // Check that the second expression has form v = x. 10063 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts(); 10064 llvm::FoldingSetNodeID XId, PossibleXId; 10065 Checker.getX()->Profile(XId, Context, /*Canonical=*/true); 10066 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true); 10067 IsUpdateExprFound = XId == PossibleXId; 10068 if (IsUpdateExprFound) { 10069 V = BinOp->getLHS(); 10070 X = Checker.getX(); 10071 E = Checker.getExpr(); 10072 UE = Checker.getUpdateExpr(); 10073 IsXLHSInRHSPart = Checker.isXLHSInRHSPart(); 10074 IsPostfixUpdate = false; 10075 } 10076 } 10077 } 10078 if (!IsUpdateExprFound) { 10079 // { v = x; x = expr; } 10080 auto *FirstExpr = dyn_cast<Expr>(First); 10081 auto *SecondExpr = dyn_cast<Expr>(Second); 10082 if (!FirstExpr || !SecondExpr || 10083 !(FirstExpr->isInstantiationDependent() || 10084 SecondExpr->isInstantiationDependent())) { 10085 auto *FirstBinOp = dyn_cast<BinaryOperator>(First); 10086 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) { 10087 ErrorFound = NotAnAssignmentOp; 10088 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc() 10089 : First->getBeginLoc(); 10090 NoteRange = ErrorRange = FirstBinOp 10091 ? FirstBinOp->getSourceRange() 10092 : SourceRange(ErrorLoc, ErrorLoc); 10093 } else { 10094 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second); 10095 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) { 10096 ErrorFound = NotAnAssignmentOp; 10097 NoteLoc = ErrorLoc = SecondBinOp 10098 ? SecondBinOp->getOperatorLoc() 10099 : Second->getBeginLoc(); 10100 NoteRange = ErrorRange = 10101 SecondBinOp ? SecondBinOp->getSourceRange() 10102 : SourceRange(ErrorLoc, ErrorLoc); 10103 } else { 10104 Expr *PossibleXRHSInFirst = 10105 FirstBinOp->getRHS()->IgnoreParenImpCasts(); 10106 Expr *PossibleXLHSInSecond = 10107 SecondBinOp->getLHS()->IgnoreParenImpCasts(); 10108 llvm::FoldingSetNodeID X1Id, X2Id; 10109 PossibleXRHSInFirst->Profile(X1Id, Context, 10110 /*Canonical=*/true); 10111 PossibleXLHSInSecond->Profile(X2Id, Context, 10112 /*Canonical=*/true); 10113 IsUpdateExprFound = X1Id == X2Id; 10114 if (IsUpdateExprFound) { 10115 V = FirstBinOp->getLHS(); 10116 X = SecondBinOp->getLHS(); 10117 E = SecondBinOp->getRHS(); 10118 UE = nullptr; 10119 IsXLHSInRHSPart = false; 10120 IsPostfixUpdate = true; 10121 } else { 10122 ErrorFound = NotASpecificExpression; 10123 ErrorLoc = FirstBinOp->getExprLoc(); 10124 ErrorRange = FirstBinOp->getSourceRange(); 10125 NoteLoc = SecondBinOp->getLHS()->getExprLoc(); 10126 NoteRange = SecondBinOp->getRHS()->getSourceRange(); 10127 } 10128 } 10129 } 10130 } 10131 } 10132 } else { 10133 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10134 NoteRange = ErrorRange = 10135 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 10136 ErrorFound = NotTwoSubstatements; 10137 } 10138 } else { 10139 NoteLoc = ErrorLoc = Body->getBeginLoc(); 10140 NoteRange = ErrorRange = 10141 SourceRange(Body->getBeginLoc(), Body->getBeginLoc()); 10142 ErrorFound = NotACompoundStatement; 10143 } 10144 if (ErrorFound != NoError) { 10145 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement) 10146 << ErrorRange; 10147 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange; 10148 return StmtError(); 10149 } 10150 if (CurContext->isDependentContext()) 10151 UE = V = E = X = nullptr; 10152 } 10153 } 10154 10155 setFunctionHasBranchProtectedScope(); 10156 10157 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt, 10158 X, V, E, UE, IsXLHSInRHSPart, 10159 IsPostfixUpdate); 10160 } 10161 10162 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses, 10163 Stmt *AStmt, 10164 SourceLocation StartLoc, 10165 SourceLocation EndLoc) { 10166 if (!AStmt) 10167 return StmtError(); 10168 10169 auto *CS = cast<CapturedStmt>(AStmt); 10170 // 1.2.2 OpenMP Language Terminology 10171 // Structured block - An executable statement with a single entry at the 10172 // top and a single exit at the bottom. 10173 // The point of exit cannot be a branch out of the structured block. 10174 // longjmp() and throw() must not violate the entry/exit criteria. 10175 CS->getCapturedDecl()->setNothrow(); 10176 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target); 10177 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10178 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10179 // 1.2.2 OpenMP Language Terminology 10180 // Structured block - An executable statement with a single entry at the 10181 // top and a single exit at the bottom. 10182 // The point of exit cannot be a branch out of the structured block. 10183 // longjmp() and throw() must not violate the entry/exit criteria. 10184 CS->getCapturedDecl()->setNothrow(); 10185 } 10186 10187 // OpenMP [2.16, Nesting of Regions] 10188 // If specified, a teams construct must be contained within a target 10189 // construct. That target construct must contain no statements or directives 10190 // outside of the teams construct. 10191 if (DSAStack->hasInnerTeamsRegion()) { 10192 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true); 10193 bool OMPTeamsFound = true; 10194 if (const auto *CS = dyn_cast<CompoundStmt>(S)) { 10195 auto I = CS->body_begin(); 10196 while (I != CS->body_end()) { 10197 const auto *OED = dyn_cast<OMPExecutableDirective>(*I); 10198 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) || 10199 OMPTeamsFound) { 10200 10201 OMPTeamsFound = false; 10202 break; 10203 } 10204 ++I; 10205 } 10206 assert(I != CS->body_end() && "Not found statement"); 10207 S = *I; 10208 } else { 10209 const auto *OED = dyn_cast<OMPExecutableDirective>(S); 10210 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind()); 10211 } 10212 if (!OMPTeamsFound) { 10213 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams); 10214 Diag(DSAStack->getInnerTeamsRegionLoc(), 10215 diag::note_omp_nested_teams_construct_here); 10216 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here) 10217 << isa<OMPExecutableDirective>(S); 10218 return StmtError(); 10219 } 10220 } 10221 10222 setFunctionHasBranchProtectedScope(); 10223 10224 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10225 } 10226 10227 StmtResult 10228 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses, 10229 Stmt *AStmt, SourceLocation StartLoc, 10230 SourceLocation EndLoc) { 10231 if (!AStmt) 10232 return StmtError(); 10233 10234 auto *CS = cast<CapturedStmt>(AStmt); 10235 // 1.2.2 OpenMP Language Terminology 10236 // Structured block - An executable statement with a single entry at the 10237 // top and a single exit at the bottom. 10238 // The point of exit cannot be a branch out of the structured block. 10239 // longjmp() and throw() must not violate the entry/exit criteria. 10240 CS->getCapturedDecl()->setNothrow(); 10241 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel); 10242 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10243 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10244 // 1.2.2 OpenMP Language Terminology 10245 // Structured block - An executable statement with a single entry at the 10246 // top and a single exit at the bottom. 10247 // The point of exit cannot be a branch out of the structured block. 10248 // longjmp() and throw() must not violate the entry/exit criteria. 10249 CS->getCapturedDecl()->setNothrow(); 10250 } 10251 10252 setFunctionHasBranchProtectedScope(); 10253 10254 return OMPTargetParallelDirective::Create( 10255 Context, StartLoc, EndLoc, Clauses, AStmt, 10256 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10257 } 10258 10259 StmtResult Sema::ActOnOpenMPTargetParallelForDirective( 10260 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10261 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10262 if (!AStmt) 10263 return StmtError(); 10264 10265 auto *CS = cast<CapturedStmt>(AStmt); 10266 // 1.2.2 OpenMP Language Terminology 10267 // Structured block - An executable statement with a single entry at the 10268 // top and a single exit at the bottom. 10269 // The point of exit cannot be a branch out of the structured block. 10270 // longjmp() and throw() must not violate the entry/exit criteria. 10271 CS->getCapturedDecl()->setNothrow(); 10272 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 10273 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10274 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10275 // 1.2.2 OpenMP Language Terminology 10276 // Structured block - An executable statement with a single entry at the 10277 // top and a single exit at the bottom. 10278 // The point of exit cannot be a branch out of the structured block. 10279 // longjmp() and throw() must not violate the entry/exit criteria. 10280 CS->getCapturedDecl()->setNothrow(); 10281 } 10282 10283 OMPLoopDirective::HelperExprs B; 10284 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10285 // define the nested loops number. 10286 unsigned NestedLoopCount = 10287 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses), 10288 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 10289 VarsWithImplicitDSA, B); 10290 if (NestedLoopCount == 0) 10291 return StmtError(); 10292 10293 assert((CurContext->isDependentContext() || B.builtAll()) && 10294 "omp target parallel for loop exprs were not built"); 10295 10296 if (!CurContext->isDependentContext()) { 10297 // Finalize the clauses that need pre-built expressions for CodeGen. 10298 for (OMPClause *C : Clauses) { 10299 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10300 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10301 B.NumIterations, *this, CurScope, 10302 DSAStack)) 10303 return StmtError(); 10304 } 10305 } 10306 10307 setFunctionHasBranchProtectedScope(); 10308 return OMPTargetParallelForDirective::Create( 10309 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 10310 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10311 } 10312 10313 /// Check for existence of a map clause in the list of clauses. 10314 static bool hasClauses(ArrayRef<OMPClause *> Clauses, 10315 const OpenMPClauseKind K) { 10316 return llvm::any_of( 10317 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; }); 10318 } 10319 10320 template <typename... Params> 10321 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K, 10322 const Params... ClauseTypes) { 10323 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...); 10324 } 10325 10326 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses, 10327 Stmt *AStmt, 10328 SourceLocation StartLoc, 10329 SourceLocation EndLoc) { 10330 if (!AStmt) 10331 return StmtError(); 10332 10333 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10334 10335 // OpenMP [2.12.2, target data Construct, Restrictions] 10336 // At least one map, use_device_addr or use_device_ptr clause must appear on 10337 // the directive. 10338 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr) && 10339 (LangOpts.OpenMP < 50 || !hasClauses(Clauses, OMPC_use_device_addr))) { 10340 StringRef Expected; 10341 if (LangOpts.OpenMP < 50) 10342 Expected = "'map' or 'use_device_ptr'"; 10343 else 10344 Expected = "'map', 'use_device_ptr', or 'use_device_addr'"; 10345 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 10346 << Expected << getOpenMPDirectiveName(OMPD_target_data); 10347 return StmtError(); 10348 } 10349 10350 setFunctionHasBranchProtectedScope(); 10351 10352 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 10353 AStmt); 10354 } 10355 10356 StmtResult 10357 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses, 10358 SourceLocation StartLoc, 10359 SourceLocation EndLoc, Stmt *AStmt) { 10360 if (!AStmt) 10361 return StmtError(); 10362 10363 auto *CS = cast<CapturedStmt>(AStmt); 10364 // 1.2.2 OpenMP Language Terminology 10365 // Structured block - An executable statement with a single entry at the 10366 // top and a single exit at the bottom. 10367 // The point of exit cannot be a branch out of the structured block. 10368 // longjmp() and throw() must not violate the entry/exit criteria. 10369 CS->getCapturedDecl()->setNothrow(); 10370 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data); 10371 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10372 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10373 // 1.2.2 OpenMP Language Terminology 10374 // Structured block - An executable statement with a single entry at the 10375 // top and a single exit at the bottom. 10376 // The point of exit cannot be a branch out of the structured block. 10377 // longjmp() and throw() must not violate the entry/exit criteria. 10378 CS->getCapturedDecl()->setNothrow(); 10379 } 10380 10381 // OpenMP [2.10.2, Restrictions, p. 99] 10382 // At least one map clause must appear on the directive. 10383 if (!hasClauses(Clauses, OMPC_map)) { 10384 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 10385 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data); 10386 return StmtError(); 10387 } 10388 10389 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 10390 AStmt); 10391 } 10392 10393 StmtResult 10394 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses, 10395 SourceLocation StartLoc, 10396 SourceLocation EndLoc, Stmt *AStmt) { 10397 if (!AStmt) 10398 return StmtError(); 10399 10400 auto *CS = cast<CapturedStmt>(AStmt); 10401 // 1.2.2 OpenMP Language Terminology 10402 // Structured block - An executable statement with a single entry at the 10403 // top and a single exit at the bottom. 10404 // The point of exit cannot be a branch out of the structured block. 10405 // longjmp() and throw() must not violate the entry/exit criteria. 10406 CS->getCapturedDecl()->setNothrow(); 10407 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data); 10408 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10409 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10410 // 1.2.2 OpenMP Language Terminology 10411 // Structured block - An executable statement with a single entry at the 10412 // top and a single exit at the bottom. 10413 // The point of exit cannot be a branch out of the structured block. 10414 // longjmp() and throw() must not violate the entry/exit criteria. 10415 CS->getCapturedDecl()->setNothrow(); 10416 } 10417 10418 // OpenMP [2.10.3, Restrictions, p. 102] 10419 // At least one map clause must appear on the directive. 10420 if (!hasClauses(Clauses, OMPC_map)) { 10421 Diag(StartLoc, diag::err_omp_no_clause_for_directive) 10422 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data); 10423 return StmtError(); 10424 } 10425 10426 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses, 10427 AStmt); 10428 } 10429 10430 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses, 10431 SourceLocation StartLoc, 10432 SourceLocation EndLoc, 10433 Stmt *AStmt) { 10434 if (!AStmt) 10435 return StmtError(); 10436 10437 auto *CS = cast<CapturedStmt>(AStmt); 10438 // 1.2.2 OpenMP Language Terminology 10439 // Structured block - An executable statement with a single entry at the 10440 // top and a single exit at the bottom. 10441 // The point of exit cannot be a branch out of the structured block. 10442 // longjmp() and throw() must not violate the entry/exit criteria. 10443 CS->getCapturedDecl()->setNothrow(); 10444 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update); 10445 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10446 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10447 // 1.2.2 OpenMP Language Terminology 10448 // Structured block - An executable statement with a single entry at the 10449 // top and a single exit at the bottom. 10450 // The point of exit cannot be a branch out of the structured block. 10451 // longjmp() and throw() must not violate the entry/exit criteria. 10452 CS->getCapturedDecl()->setNothrow(); 10453 } 10454 10455 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) { 10456 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required); 10457 return StmtError(); 10458 } 10459 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses, 10460 AStmt); 10461 } 10462 10463 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses, 10464 Stmt *AStmt, SourceLocation StartLoc, 10465 SourceLocation EndLoc) { 10466 if (!AStmt) 10467 return StmtError(); 10468 10469 auto *CS = cast<CapturedStmt>(AStmt); 10470 // 1.2.2 OpenMP Language Terminology 10471 // Structured block - An executable statement with a single entry at the 10472 // top and a single exit at the bottom. 10473 // The point of exit cannot be a branch out of the structured block. 10474 // longjmp() and throw() must not violate the entry/exit criteria. 10475 CS->getCapturedDecl()->setNothrow(); 10476 10477 setFunctionHasBranchProtectedScope(); 10478 10479 DSAStack->setParentTeamsRegionLoc(StartLoc); 10480 10481 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 10482 } 10483 10484 StmtResult 10485 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, 10486 SourceLocation EndLoc, 10487 OpenMPDirectiveKind CancelRegion) { 10488 if (DSAStack->isParentNowaitRegion()) { 10489 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; 10490 return StmtError(); 10491 } 10492 if (DSAStack->isParentOrderedRegion()) { 10493 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; 10494 return StmtError(); 10495 } 10496 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, 10497 CancelRegion); 10498 } 10499 10500 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses, 10501 SourceLocation StartLoc, 10502 SourceLocation EndLoc, 10503 OpenMPDirectiveKind CancelRegion) { 10504 if (DSAStack->isParentNowaitRegion()) { 10505 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1; 10506 return StmtError(); 10507 } 10508 if (DSAStack->isParentOrderedRegion()) { 10509 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1; 10510 return StmtError(); 10511 } 10512 DSAStack->setParentCancelRegion(/*Cancel=*/true); 10513 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses, 10514 CancelRegion); 10515 } 10516 10517 static bool checkGrainsizeNumTasksClauses(Sema &S, 10518 ArrayRef<OMPClause *> Clauses) { 10519 const OMPClause *PrevClause = nullptr; 10520 bool ErrorFound = false; 10521 for (const OMPClause *C : Clauses) { 10522 if (C->getClauseKind() == OMPC_grainsize || 10523 C->getClauseKind() == OMPC_num_tasks) { 10524 if (!PrevClause) 10525 PrevClause = C; 10526 else if (PrevClause->getClauseKind() != C->getClauseKind()) { 10527 S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive) 10528 << getOpenMPClauseName(C->getClauseKind()) 10529 << getOpenMPClauseName(PrevClause->getClauseKind()); 10530 S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause) 10531 << getOpenMPClauseName(PrevClause->getClauseKind()); 10532 ErrorFound = true; 10533 } 10534 } 10535 } 10536 return ErrorFound; 10537 } 10538 10539 static bool checkReductionClauseWithNogroup(Sema &S, 10540 ArrayRef<OMPClause *> Clauses) { 10541 const OMPClause *ReductionClause = nullptr; 10542 const OMPClause *NogroupClause = nullptr; 10543 for (const OMPClause *C : Clauses) { 10544 if (C->getClauseKind() == OMPC_reduction) { 10545 ReductionClause = C; 10546 if (NogroupClause) 10547 break; 10548 continue; 10549 } 10550 if (C->getClauseKind() == OMPC_nogroup) { 10551 NogroupClause = C; 10552 if (ReductionClause) 10553 break; 10554 continue; 10555 } 10556 } 10557 if (ReductionClause && NogroupClause) { 10558 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup) 10559 << SourceRange(NogroupClause->getBeginLoc(), 10560 NogroupClause->getEndLoc()); 10561 return true; 10562 } 10563 return false; 10564 } 10565 10566 StmtResult Sema::ActOnOpenMPTaskLoopDirective( 10567 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10568 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10569 if (!AStmt) 10570 return StmtError(); 10571 10572 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10573 OMPLoopDirective::HelperExprs B; 10574 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10575 // define the nested loops number. 10576 unsigned NestedLoopCount = 10577 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses), 10578 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 10579 VarsWithImplicitDSA, B); 10580 if (NestedLoopCount == 0) 10581 return StmtError(); 10582 10583 assert((CurContext->isDependentContext() || B.builtAll()) && 10584 "omp for loop exprs were not built"); 10585 10586 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10587 // The grainsize clause and num_tasks clause are mutually exclusive and may 10588 // not appear on the same taskloop directive. 10589 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10590 return StmtError(); 10591 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10592 // If a reduction clause is present on the taskloop directive, the nogroup 10593 // clause must not be specified. 10594 if (checkReductionClauseWithNogroup(*this, Clauses)) 10595 return StmtError(); 10596 10597 setFunctionHasBranchProtectedScope(); 10598 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc, 10599 NestedLoopCount, Clauses, AStmt, B, 10600 DSAStack->isCancelRegion()); 10601 } 10602 10603 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective( 10604 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10605 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10606 if (!AStmt) 10607 return StmtError(); 10608 10609 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10610 OMPLoopDirective::HelperExprs B; 10611 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10612 // define the nested loops number. 10613 unsigned NestedLoopCount = 10614 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses), 10615 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 10616 VarsWithImplicitDSA, B); 10617 if (NestedLoopCount == 0) 10618 return StmtError(); 10619 10620 assert((CurContext->isDependentContext() || B.builtAll()) && 10621 "omp for loop exprs were not built"); 10622 10623 if (!CurContext->isDependentContext()) { 10624 // Finalize the clauses that need pre-built expressions for CodeGen. 10625 for (OMPClause *C : Clauses) { 10626 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10627 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10628 B.NumIterations, *this, CurScope, 10629 DSAStack)) 10630 return StmtError(); 10631 } 10632 } 10633 10634 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10635 // The grainsize clause and num_tasks clause are mutually exclusive and may 10636 // not appear on the same taskloop directive. 10637 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10638 return StmtError(); 10639 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10640 // If a reduction clause is present on the taskloop directive, the nogroup 10641 // clause must not be specified. 10642 if (checkReductionClauseWithNogroup(*this, Clauses)) 10643 return StmtError(); 10644 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10645 return StmtError(); 10646 10647 setFunctionHasBranchProtectedScope(); 10648 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc, 10649 NestedLoopCount, Clauses, AStmt, B); 10650 } 10651 10652 StmtResult Sema::ActOnOpenMPMasterTaskLoopDirective( 10653 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10654 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10655 if (!AStmt) 10656 return StmtError(); 10657 10658 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10659 OMPLoopDirective::HelperExprs B; 10660 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10661 // define the nested loops number. 10662 unsigned NestedLoopCount = 10663 checkOpenMPLoop(OMPD_master_taskloop, getCollapseNumberExpr(Clauses), 10664 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 10665 VarsWithImplicitDSA, B); 10666 if (NestedLoopCount == 0) 10667 return StmtError(); 10668 10669 assert((CurContext->isDependentContext() || B.builtAll()) && 10670 "omp for loop exprs were not built"); 10671 10672 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10673 // The grainsize clause and num_tasks clause are mutually exclusive and may 10674 // not appear on the same taskloop directive. 10675 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10676 return StmtError(); 10677 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10678 // If a reduction clause is present on the taskloop directive, the nogroup 10679 // clause must not be specified. 10680 if (checkReductionClauseWithNogroup(*this, Clauses)) 10681 return StmtError(); 10682 10683 setFunctionHasBranchProtectedScope(); 10684 return OMPMasterTaskLoopDirective::Create(Context, StartLoc, EndLoc, 10685 NestedLoopCount, Clauses, AStmt, B, 10686 DSAStack->isCancelRegion()); 10687 } 10688 10689 StmtResult Sema::ActOnOpenMPMasterTaskLoopSimdDirective( 10690 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10691 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10692 if (!AStmt) 10693 return StmtError(); 10694 10695 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10696 OMPLoopDirective::HelperExprs B; 10697 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10698 // define the nested loops number. 10699 unsigned NestedLoopCount = 10700 checkOpenMPLoop(OMPD_master_taskloop_simd, getCollapseNumberExpr(Clauses), 10701 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack, 10702 VarsWithImplicitDSA, B); 10703 if (NestedLoopCount == 0) 10704 return StmtError(); 10705 10706 assert((CurContext->isDependentContext() || B.builtAll()) && 10707 "omp for loop exprs were not built"); 10708 10709 if (!CurContext->isDependentContext()) { 10710 // Finalize the clauses that need pre-built expressions for CodeGen. 10711 for (OMPClause *C : Clauses) { 10712 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10713 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10714 B.NumIterations, *this, CurScope, 10715 DSAStack)) 10716 return StmtError(); 10717 } 10718 } 10719 10720 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10721 // The grainsize clause and num_tasks clause are mutually exclusive and may 10722 // not appear on the same taskloop directive. 10723 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10724 return StmtError(); 10725 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10726 // If a reduction clause is present on the taskloop directive, the nogroup 10727 // clause must not be specified. 10728 if (checkReductionClauseWithNogroup(*this, Clauses)) 10729 return StmtError(); 10730 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10731 return StmtError(); 10732 10733 setFunctionHasBranchProtectedScope(); 10734 return OMPMasterTaskLoopSimdDirective::Create( 10735 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10736 } 10737 10738 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopDirective( 10739 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10740 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10741 if (!AStmt) 10742 return StmtError(); 10743 10744 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10745 auto *CS = cast<CapturedStmt>(AStmt); 10746 // 1.2.2 OpenMP Language Terminology 10747 // Structured block - An executable statement with a single entry at the 10748 // top and a single exit at the bottom. 10749 // The point of exit cannot be a branch out of the structured block. 10750 // longjmp() and throw() must not violate the entry/exit criteria. 10751 CS->getCapturedDecl()->setNothrow(); 10752 for (int ThisCaptureLevel = 10753 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop); 10754 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10755 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10756 // 1.2.2 OpenMP Language Terminology 10757 // Structured block - An executable statement with a single entry at the 10758 // top and a single exit at the bottom. 10759 // The point of exit cannot be a branch out of the structured block. 10760 // longjmp() and throw() must not violate the entry/exit criteria. 10761 CS->getCapturedDecl()->setNothrow(); 10762 } 10763 10764 OMPLoopDirective::HelperExprs B; 10765 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10766 // define the nested loops number. 10767 unsigned NestedLoopCount = checkOpenMPLoop( 10768 OMPD_parallel_master_taskloop, getCollapseNumberExpr(Clauses), 10769 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 10770 VarsWithImplicitDSA, B); 10771 if (NestedLoopCount == 0) 10772 return StmtError(); 10773 10774 assert((CurContext->isDependentContext() || B.builtAll()) && 10775 "omp for loop exprs were not built"); 10776 10777 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10778 // The grainsize clause and num_tasks clause are mutually exclusive and may 10779 // not appear on the same taskloop directive. 10780 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10781 return StmtError(); 10782 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10783 // If a reduction clause is present on the taskloop directive, the nogroup 10784 // clause must not be specified. 10785 if (checkReductionClauseWithNogroup(*this, Clauses)) 10786 return StmtError(); 10787 10788 setFunctionHasBranchProtectedScope(); 10789 return OMPParallelMasterTaskLoopDirective::Create( 10790 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 10791 DSAStack->isCancelRegion()); 10792 } 10793 10794 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopSimdDirective( 10795 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10796 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10797 if (!AStmt) 10798 return StmtError(); 10799 10800 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10801 auto *CS = cast<CapturedStmt>(AStmt); 10802 // 1.2.2 OpenMP Language Terminology 10803 // Structured block - An executable statement with a single entry at the 10804 // top and a single exit at the bottom. 10805 // The point of exit cannot be a branch out of the structured block. 10806 // longjmp() and throw() must not violate the entry/exit criteria. 10807 CS->getCapturedDecl()->setNothrow(); 10808 for (int ThisCaptureLevel = 10809 getOpenMPCaptureLevels(OMPD_parallel_master_taskloop_simd); 10810 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10811 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10812 // 1.2.2 OpenMP Language Terminology 10813 // Structured block - An executable statement with a single entry at the 10814 // top and a single exit at the bottom. 10815 // The point of exit cannot be a branch out of the structured block. 10816 // longjmp() and throw() must not violate the entry/exit criteria. 10817 CS->getCapturedDecl()->setNothrow(); 10818 } 10819 10820 OMPLoopDirective::HelperExprs B; 10821 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 10822 // define the nested loops number. 10823 unsigned NestedLoopCount = checkOpenMPLoop( 10824 OMPD_parallel_master_taskloop_simd, getCollapseNumberExpr(Clauses), 10825 /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack, 10826 VarsWithImplicitDSA, B); 10827 if (NestedLoopCount == 0) 10828 return StmtError(); 10829 10830 assert((CurContext->isDependentContext() || B.builtAll()) && 10831 "omp for loop exprs were not built"); 10832 10833 if (!CurContext->isDependentContext()) { 10834 // Finalize the clauses that need pre-built expressions for CodeGen. 10835 for (OMPClause *C : Clauses) { 10836 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10837 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10838 B.NumIterations, *this, CurScope, 10839 DSAStack)) 10840 return StmtError(); 10841 } 10842 } 10843 10844 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10845 // The grainsize clause and num_tasks clause are mutually exclusive and may 10846 // not appear on the same taskloop directive. 10847 if (checkGrainsizeNumTasksClauses(*this, Clauses)) 10848 return StmtError(); 10849 // OpenMP, [2.9.2 taskloop Construct, Restrictions] 10850 // If a reduction clause is present on the taskloop directive, the nogroup 10851 // clause must not be specified. 10852 if (checkReductionClauseWithNogroup(*this, Clauses)) 10853 return StmtError(); 10854 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10855 return StmtError(); 10856 10857 setFunctionHasBranchProtectedScope(); 10858 return OMPParallelMasterTaskLoopSimdDirective::Create( 10859 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10860 } 10861 10862 StmtResult Sema::ActOnOpenMPDistributeDirective( 10863 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10864 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10865 if (!AStmt) 10866 return StmtError(); 10867 10868 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected"); 10869 OMPLoopDirective::HelperExprs B; 10870 // In presence of clause 'collapse' with number of loops, it will 10871 // define the nested loops number. 10872 unsigned NestedLoopCount = 10873 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses), 10874 nullptr /*ordered not a clause on distribute*/, AStmt, 10875 *this, *DSAStack, VarsWithImplicitDSA, B); 10876 if (NestedLoopCount == 0) 10877 return StmtError(); 10878 10879 assert((CurContext->isDependentContext() || B.builtAll()) && 10880 "omp for loop exprs were not built"); 10881 10882 setFunctionHasBranchProtectedScope(); 10883 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc, 10884 NestedLoopCount, Clauses, AStmt, B); 10885 } 10886 10887 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective( 10888 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10889 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10890 if (!AStmt) 10891 return StmtError(); 10892 10893 auto *CS = cast<CapturedStmt>(AStmt); 10894 // 1.2.2 OpenMP Language Terminology 10895 // Structured block - An executable statement with a single entry at the 10896 // top and a single exit at the bottom. 10897 // The point of exit cannot be a branch out of the structured block. 10898 // longjmp() and throw() must not violate the entry/exit criteria. 10899 CS->getCapturedDecl()->setNothrow(); 10900 for (int ThisCaptureLevel = 10901 getOpenMPCaptureLevels(OMPD_distribute_parallel_for); 10902 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10903 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10904 // 1.2.2 OpenMP Language Terminology 10905 // Structured block - An executable statement with a single entry at the 10906 // top and a single exit at the bottom. 10907 // The point of exit cannot be a branch out of the structured block. 10908 // longjmp() and throw() must not violate the entry/exit criteria. 10909 CS->getCapturedDecl()->setNothrow(); 10910 } 10911 10912 OMPLoopDirective::HelperExprs B; 10913 // In presence of clause 'collapse' with number of loops, it will 10914 // define the nested loops number. 10915 unsigned NestedLoopCount = checkOpenMPLoop( 10916 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses), 10917 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 10918 VarsWithImplicitDSA, B); 10919 if (NestedLoopCount == 0) 10920 return StmtError(); 10921 10922 assert((CurContext->isDependentContext() || B.builtAll()) && 10923 "omp for loop exprs were not built"); 10924 10925 setFunctionHasBranchProtectedScope(); 10926 return OMPDistributeParallelForDirective::Create( 10927 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 10928 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 10929 } 10930 10931 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective( 10932 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10933 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10934 if (!AStmt) 10935 return StmtError(); 10936 10937 auto *CS = cast<CapturedStmt>(AStmt); 10938 // 1.2.2 OpenMP Language Terminology 10939 // Structured block - An executable statement with a single entry at the 10940 // top and a single exit at the bottom. 10941 // The point of exit cannot be a branch out of the structured block. 10942 // longjmp() and throw() must not violate the entry/exit criteria. 10943 CS->getCapturedDecl()->setNothrow(); 10944 for (int ThisCaptureLevel = 10945 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd); 10946 ThisCaptureLevel > 1; --ThisCaptureLevel) { 10947 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 10948 // 1.2.2 OpenMP Language Terminology 10949 // Structured block - An executable statement with a single entry at the 10950 // top and a single exit at the bottom. 10951 // The point of exit cannot be a branch out of the structured block. 10952 // longjmp() and throw() must not violate the entry/exit criteria. 10953 CS->getCapturedDecl()->setNothrow(); 10954 } 10955 10956 OMPLoopDirective::HelperExprs B; 10957 // In presence of clause 'collapse' with number of loops, it will 10958 // define the nested loops number. 10959 unsigned NestedLoopCount = checkOpenMPLoop( 10960 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 10961 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 10962 VarsWithImplicitDSA, B); 10963 if (NestedLoopCount == 0) 10964 return StmtError(); 10965 10966 assert((CurContext->isDependentContext() || B.builtAll()) && 10967 "omp for loop exprs were not built"); 10968 10969 if (!CurContext->isDependentContext()) { 10970 // Finalize the clauses that need pre-built expressions for CodeGen. 10971 for (OMPClause *C : Clauses) { 10972 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 10973 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 10974 B.NumIterations, *this, CurScope, 10975 DSAStack)) 10976 return StmtError(); 10977 } 10978 } 10979 10980 if (checkSimdlenSafelenSpecified(*this, Clauses)) 10981 return StmtError(); 10982 10983 setFunctionHasBranchProtectedScope(); 10984 return OMPDistributeParallelForSimdDirective::Create( 10985 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 10986 } 10987 10988 StmtResult Sema::ActOnOpenMPDistributeSimdDirective( 10989 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 10990 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 10991 if (!AStmt) 10992 return StmtError(); 10993 10994 auto *CS = cast<CapturedStmt>(AStmt); 10995 // 1.2.2 OpenMP Language Terminology 10996 // Structured block - An executable statement with a single entry at the 10997 // top and a single exit at the bottom. 10998 // The point of exit cannot be a branch out of the structured block. 10999 // longjmp() and throw() must not violate the entry/exit criteria. 11000 CS->getCapturedDecl()->setNothrow(); 11001 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd); 11002 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11003 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11004 // 1.2.2 OpenMP Language Terminology 11005 // Structured block - An executable statement with a single entry at the 11006 // top and a single exit at the bottom. 11007 // The point of exit cannot be a branch out of the structured block. 11008 // longjmp() and throw() must not violate the entry/exit criteria. 11009 CS->getCapturedDecl()->setNothrow(); 11010 } 11011 11012 OMPLoopDirective::HelperExprs B; 11013 // In presence of clause 'collapse' with number of loops, it will 11014 // define the nested loops number. 11015 unsigned NestedLoopCount = 11016 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses), 11017 nullptr /*ordered not a clause on distribute*/, CS, *this, 11018 *DSAStack, VarsWithImplicitDSA, B); 11019 if (NestedLoopCount == 0) 11020 return StmtError(); 11021 11022 assert((CurContext->isDependentContext() || B.builtAll()) && 11023 "omp for loop exprs were not built"); 11024 11025 if (!CurContext->isDependentContext()) { 11026 // Finalize the clauses that need pre-built expressions for CodeGen. 11027 for (OMPClause *C : Clauses) { 11028 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11029 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11030 B.NumIterations, *this, CurScope, 11031 DSAStack)) 11032 return StmtError(); 11033 } 11034 } 11035 11036 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11037 return StmtError(); 11038 11039 setFunctionHasBranchProtectedScope(); 11040 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc, 11041 NestedLoopCount, Clauses, AStmt, B); 11042 } 11043 11044 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective( 11045 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11046 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11047 if (!AStmt) 11048 return StmtError(); 11049 11050 auto *CS = cast<CapturedStmt>(AStmt); 11051 // 1.2.2 OpenMP Language Terminology 11052 // Structured block - An executable statement with a single entry at the 11053 // top and a single exit at the bottom. 11054 // The point of exit cannot be a branch out of the structured block. 11055 // longjmp() and throw() must not violate the entry/exit criteria. 11056 CS->getCapturedDecl()->setNothrow(); 11057 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for); 11058 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11059 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11060 // 1.2.2 OpenMP Language Terminology 11061 // Structured block - An executable statement with a single entry at the 11062 // top and a single exit at the bottom. 11063 // The point of exit cannot be a branch out of the structured block. 11064 // longjmp() and throw() must not violate the entry/exit criteria. 11065 CS->getCapturedDecl()->setNothrow(); 11066 } 11067 11068 OMPLoopDirective::HelperExprs B; 11069 // In presence of clause 'collapse' or 'ordered' with number of loops, it will 11070 // define the nested loops number. 11071 unsigned NestedLoopCount = checkOpenMPLoop( 11072 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses), 11073 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 11074 VarsWithImplicitDSA, B); 11075 if (NestedLoopCount == 0) 11076 return StmtError(); 11077 11078 assert((CurContext->isDependentContext() || B.builtAll()) && 11079 "omp target parallel for simd loop exprs were not built"); 11080 11081 if (!CurContext->isDependentContext()) { 11082 // Finalize the clauses that need pre-built expressions for CodeGen. 11083 for (OMPClause *C : Clauses) { 11084 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11085 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11086 B.NumIterations, *this, CurScope, 11087 DSAStack)) 11088 return StmtError(); 11089 } 11090 } 11091 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11092 return StmtError(); 11093 11094 setFunctionHasBranchProtectedScope(); 11095 return OMPTargetParallelForSimdDirective::Create( 11096 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11097 } 11098 11099 StmtResult Sema::ActOnOpenMPTargetSimdDirective( 11100 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11101 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11102 if (!AStmt) 11103 return StmtError(); 11104 11105 auto *CS = cast<CapturedStmt>(AStmt); 11106 // 1.2.2 OpenMP Language Terminology 11107 // Structured block - An executable statement with a single entry at the 11108 // top and a single exit at the bottom. 11109 // The point of exit cannot be a branch out of the structured block. 11110 // longjmp() and throw() must not violate the entry/exit criteria. 11111 CS->getCapturedDecl()->setNothrow(); 11112 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd); 11113 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11114 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11115 // 1.2.2 OpenMP Language Terminology 11116 // Structured block - An executable statement with a single entry at the 11117 // top and a single exit at the bottom. 11118 // The point of exit cannot be a branch out of the structured block. 11119 // longjmp() and throw() must not violate the entry/exit criteria. 11120 CS->getCapturedDecl()->setNothrow(); 11121 } 11122 11123 OMPLoopDirective::HelperExprs B; 11124 // In presence of clause 'collapse' with number of loops, it will define the 11125 // nested loops number. 11126 unsigned NestedLoopCount = 11127 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses), 11128 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack, 11129 VarsWithImplicitDSA, B); 11130 if (NestedLoopCount == 0) 11131 return StmtError(); 11132 11133 assert((CurContext->isDependentContext() || B.builtAll()) && 11134 "omp target simd loop exprs were not built"); 11135 11136 if (!CurContext->isDependentContext()) { 11137 // Finalize the clauses that need pre-built expressions for CodeGen. 11138 for (OMPClause *C : Clauses) { 11139 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11140 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11141 B.NumIterations, *this, CurScope, 11142 DSAStack)) 11143 return StmtError(); 11144 } 11145 } 11146 11147 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11148 return StmtError(); 11149 11150 setFunctionHasBranchProtectedScope(); 11151 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc, 11152 NestedLoopCount, Clauses, AStmt, B); 11153 } 11154 11155 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective( 11156 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11157 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11158 if (!AStmt) 11159 return StmtError(); 11160 11161 auto *CS = cast<CapturedStmt>(AStmt); 11162 // 1.2.2 OpenMP Language Terminology 11163 // Structured block - An executable statement with a single entry at the 11164 // top and a single exit at the bottom. 11165 // The point of exit cannot be a branch out of the structured block. 11166 // longjmp() and throw() must not violate the entry/exit criteria. 11167 CS->getCapturedDecl()->setNothrow(); 11168 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute); 11169 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11170 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11171 // 1.2.2 OpenMP Language Terminology 11172 // Structured block - An executable statement with a single entry at the 11173 // top and a single exit at the bottom. 11174 // The point of exit cannot be a branch out of the structured block. 11175 // longjmp() and throw() must not violate the entry/exit criteria. 11176 CS->getCapturedDecl()->setNothrow(); 11177 } 11178 11179 OMPLoopDirective::HelperExprs B; 11180 // In presence of clause 'collapse' with number of loops, it will 11181 // define the nested loops number. 11182 unsigned NestedLoopCount = 11183 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses), 11184 nullptr /*ordered not a clause on distribute*/, CS, *this, 11185 *DSAStack, VarsWithImplicitDSA, B); 11186 if (NestedLoopCount == 0) 11187 return StmtError(); 11188 11189 assert((CurContext->isDependentContext() || B.builtAll()) && 11190 "omp teams distribute loop exprs were not built"); 11191 11192 setFunctionHasBranchProtectedScope(); 11193 11194 DSAStack->setParentTeamsRegionLoc(StartLoc); 11195 11196 return OMPTeamsDistributeDirective::Create( 11197 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11198 } 11199 11200 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective( 11201 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11202 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11203 if (!AStmt) 11204 return StmtError(); 11205 11206 auto *CS = cast<CapturedStmt>(AStmt); 11207 // 1.2.2 OpenMP Language Terminology 11208 // Structured block - An executable statement with a single entry at the 11209 // top and a single exit at the bottom. 11210 // The point of exit cannot be a branch out of the structured block. 11211 // longjmp() and throw() must not violate the entry/exit criteria. 11212 CS->getCapturedDecl()->setNothrow(); 11213 for (int ThisCaptureLevel = 11214 getOpenMPCaptureLevels(OMPD_teams_distribute_simd); 11215 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11216 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11217 // 1.2.2 OpenMP Language Terminology 11218 // Structured block - An executable statement with a single entry at the 11219 // top and a single exit at the bottom. 11220 // The point of exit cannot be a branch out of the structured block. 11221 // longjmp() and throw() must not violate the entry/exit criteria. 11222 CS->getCapturedDecl()->setNothrow(); 11223 } 11224 11225 OMPLoopDirective::HelperExprs B; 11226 // In presence of clause 'collapse' with number of loops, it will 11227 // define the nested loops number. 11228 unsigned NestedLoopCount = checkOpenMPLoop( 11229 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses), 11230 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11231 VarsWithImplicitDSA, B); 11232 11233 if (NestedLoopCount == 0) 11234 return StmtError(); 11235 11236 assert((CurContext->isDependentContext() || B.builtAll()) && 11237 "omp teams distribute simd loop exprs were not built"); 11238 11239 if (!CurContext->isDependentContext()) { 11240 // Finalize the clauses that need pre-built expressions for CodeGen. 11241 for (OMPClause *C : Clauses) { 11242 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11243 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11244 B.NumIterations, *this, CurScope, 11245 DSAStack)) 11246 return StmtError(); 11247 } 11248 } 11249 11250 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11251 return StmtError(); 11252 11253 setFunctionHasBranchProtectedScope(); 11254 11255 DSAStack->setParentTeamsRegionLoc(StartLoc); 11256 11257 return OMPTeamsDistributeSimdDirective::Create( 11258 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11259 } 11260 11261 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective( 11262 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11263 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11264 if (!AStmt) 11265 return StmtError(); 11266 11267 auto *CS = cast<CapturedStmt>(AStmt); 11268 // 1.2.2 OpenMP Language Terminology 11269 // Structured block - An executable statement with a single entry at the 11270 // top and a single exit at the bottom. 11271 // The point of exit cannot be a branch out of the structured block. 11272 // longjmp() and throw() must not violate the entry/exit criteria. 11273 CS->getCapturedDecl()->setNothrow(); 11274 11275 for (int ThisCaptureLevel = 11276 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd); 11277 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11278 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11279 // 1.2.2 OpenMP Language Terminology 11280 // Structured block - An executable statement with a single entry at the 11281 // top and a single exit at the bottom. 11282 // The point of exit cannot be a branch out of the structured block. 11283 // longjmp() and throw() must not violate the entry/exit criteria. 11284 CS->getCapturedDecl()->setNothrow(); 11285 } 11286 11287 OMPLoopDirective::HelperExprs B; 11288 // In presence of clause 'collapse' with number of loops, it will 11289 // define the nested loops number. 11290 unsigned NestedLoopCount = checkOpenMPLoop( 11291 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses), 11292 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11293 VarsWithImplicitDSA, B); 11294 11295 if (NestedLoopCount == 0) 11296 return StmtError(); 11297 11298 assert((CurContext->isDependentContext() || B.builtAll()) && 11299 "omp for loop exprs were not built"); 11300 11301 if (!CurContext->isDependentContext()) { 11302 // Finalize the clauses that need pre-built expressions for CodeGen. 11303 for (OMPClause *C : Clauses) { 11304 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11305 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11306 B.NumIterations, *this, CurScope, 11307 DSAStack)) 11308 return StmtError(); 11309 } 11310 } 11311 11312 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11313 return StmtError(); 11314 11315 setFunctionHasBranchProtectedScope(); 11316 11317 DSAStack->setParentTeamsRegionLoc(StartLoc); 11318 11319 return OMPTeamsDistributeParallelForSimdDirective::Create( 11320 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11321 } 11322 11323 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective( 11324 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11325 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11326 if (!AStmt) 11327 return StmtError(); 11328 11329 auto *CS = cast<CapturedStmt>(AStmt); 11330 // 1.2.2 OpenMP Language Terminology 11331 // Structured block - An executable statement with a single entry at the 11332 // top and a single exit at the bottom. 11333 // The point of exit cannot be a branch out of the structured block. 11334 // longjmp() and throw() must not violate the entry/exit criteria. 11335 CS->getCapturedDecl()->setNothrow(); 11336 11337 for (int ThisCaptureLevel = 11338 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for); 11339 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11340 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11341 // 1.2.2 OpenMP Language Terminology 11342 // Structured block - An executable statement with a single entry at the 11343 // top and a single exit at the bottom. 11344 // The point of exit cannot be a branch out of the structured block. 11345 // longjmp() and throw() must not violate the entry/exit criteria. 11346 CS->getCapturedDecl()->setNothrow(); 11347 } 11348 11349 OMPLoopDirective::HelperExprs B; 11350 // In presence of clause 'collapse' with number of loops, it will 11351 // define the nested loops number. 11352 unsigned NestedLoopCount = checkOpenMPLoop( 11353 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 11354 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11355 VarsWithImplicitDSA, B); 11356 11357 if (NestedLoopCount == 0) 11358 return StmtError(); 11359 11360 assert((CurContext->isDependentContext() || B.builtAll()) && 11361 "omp for loop exprs were not built"); 11362 11363 setFunctionHasBranchProtectedScope(); 11364 11365 DSAStack->setParentTeamsRegionLoc(StartLoc); 11366 11367 return OMPTeamsDistributeParallelForDirective::Create( 11368 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11369 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11370 } 11371 11372 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses, 11373 Stmt *AStmt, 11374 SourceLocation StartLoc, 11375 SourceLocation EndLoc) { 11376 if (!AStmt) 11377 return StmtError(); 11378 11379 auto *CS = cast<CapturedStmt>(AStmt); 11380 // 1.2.2 OpenMP Language Terminology 11381 // Structured block - An executable statement with a single entry at the 11382 // top and a single exit at the bottom. 11383 // The point of exit cannot be a branch out of the structured block. 11384 // longjmp() and throw() must not violate the entry/exit criteria. 11385 CS->getCapturedDecl()->setNothrow(); 11386 11387 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams); 11388 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11389 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11390 // 1.2.2 OpenMP Language Terminology 11391 // Structured block - An executable statement with a single entry at the 11392 // top and a single exit at the bottom. 11393 // The point of exit cannot be a branch out of the structured block. 11394 // longjmp() and throw() must not violate the entry/exit criteria. 11395 CS->getCapturedDecl()->setNothrow(); 11396 } 11397 setFunctionHasBranchProtectedScope(); 11398 11399 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, 11400 AStmt); 11401 } 11402 11403 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective( 11404 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11405 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11406 if (!AStmt) 11407 return StmtError(); 11408 11409 auto *CS = cast<CapturedStmt>(AStmt); 11410 // 1.2.2 OpenMP Language Terminology 11411 // Structured block - An executable statement with a single entry at the 11412 // top and a single exit at the bottom. 11413 // The point of exit cannot be a branch out of the structured block. 11414 // longjmp() and throw() must not violate the entry/exit criteria. 11415 CS->getCapturedDecl()->setNothrow(); 11416 for (int ThisCaptureLevel = 11417 getOpenMPCaptureLevels(OMPD_target_teams_distribute); 11418 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11419 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11420 // 1.2.2 OpenMP Language Terminology 11421 // Structured block - An executable statement with a single entry at the 11422 // top and a single exit at the bottom. 11423 // The point of exit cannot be a branch out of the structured block. 11424 // longjmp() and throw() must not violate the entry/exit criteria. 11425 CS->getCapturedDecl()->setNothrow(); 11426 } 11427 11428 OMPLoopDirective::HelperExprs B; 11429 // In presence of clause 'collapse' with number of loops, it will 11430 // define the nested loops number. 11431 unsigned NestedLoopCount = checkOpenMPLoop( 11432 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses), 11433 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11434 VarsWithImplicitDSA, B); 11435 if (NestedLoopCount == 0) 11436 return StmtError(); 11437 11438 assert((CurContext->isDependentContext() || B.builtAll()) && 11439 "omp target teams distribute loop exprs were not built"); 11440 11441 setFunctionHasBranchProtectedScope(); 11442 return OMPTargetTeamsDistributeDirective::Create( 11443 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11444 } 11445 11446 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective( 11447 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11448 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11449 if (!AStmt) 11450 return StmtError(); 11451 11452 auto *CS = cast<CapturedStmt>(AStmt); 11453 // 1.2.2 OpenMP Language Terminology 11454 // Structured block - An executable statement with a single entry at the 11455 // top and a single exit at the bottom. 11456 // The point of exit cannot be a branch out of the structured block. 11457 // longjmp() and throw() must not violate the entry/exit criteria. 11458 CS->getCapturedDecl()->setNothrow(); 11459 for (int ThisCaptureLevel = 11460 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for); 11461 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11462 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11463 // 1.2.2 OpenMP Language Terminology 11464 // Structured block - An executable statement with a single entry at the 11465 // top and a single exit at the bottom. 11466 // The point of exit cannot be a branch out of the structured block. 11467 // longjmp() and throw() must not violate the entry/exit criteria. 11468 CS->getCapturedDecl()->setNothrow(); 11469 } 11470 11471 OMPLoopDirective::HelperExprs B; 11472 // In presence of clause 'collapse' with number of loops, it will 11473 // define the nested loops number. 11474 unsigned NestedLoopCount = checkOpenMPLoop( 11475 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses), 11476 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11477 VarsWithImplicitDSA, B); 11478 if (NestedLoopCount == 0) 11479 return StmtError(); 11480 11481 assert((CurContext->isDependentContext() || B.builtAll()) && 11482 "omp target teams distribute parallel for loop exprs were not built"); 11483 11484 if (!CurContext->isDependentContext()) { 11485 // Finalize the clauses that need pre-built expressions for CodeGen. 11486 for (OMPClause *C : Clauses) { 11487 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11488 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11489 B.NumIterations, *this, CurScope, 11490 DSAStack)) 11491 return StmtError(); 11492 } 11493 } 11494 11495 setFunctionHasBranchProtectedScope(); 11496 return OMPTargetTeamsDistributeParallelForDirective::Create( 11497 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B, 11498 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion()); 11499 } 11500 11501 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective( 11502 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11503 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11504 if (!AStmt) 11505 return StmtError(); 11506 11507 auto *CS = cast<CapturedStmt>(AStmt); 11508 // 1.2.2 OpenMP Language Terminology 11509 // Structured block - An executable statement with a single entry at the 11510 // top and a single exit at the bottom. 11511 // The point of exit cannot be a branch out of the structured block. 11512 // longjmp() and throw() must not violate the entry/exit criteria. 11513 CS->getCapturedDecl()->setNothrow(); 11514 for (int ThisCaptureLevel = getOpenMPCaptureLevels( 11515 OMPD_target_teams_distribute_parallel_for_simd); 11516 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11517 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11518 // 1.2.2 OpenMP Language Terminology 11519 // Structured block - An executable statement with a single entry at the 11520 // top and a single exit at the bottom. 11521 // The point of exit cannot be a branch out of the structured block. 11522 // longjmp() and throw() must not violate the entry/exit criteria. 11523 CS->getCapturedDecl()->setNothrow(); 11524 } 11525 11526 OMPLoopDirective::HelperExprs B; 11527 // In presence of clause 'collapse' with number of loops, it will 11528 // define the nested loops number. 11529 unsigned NestedLoopCount = 11530 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd, 11531 getCollapseNumberExpr(Clauses), 11532 nullptr /*ordered not a clause on distribute*/, CS, *this, 11533 *DSAStack, VarsWithImplicitDSA, B); 11534 if (NestedLoopCount == 0) 11535 return StmtError(); 11536 11537 assert((CurContext->isDependentContext() || B.builtAll()) && 11538 "omp target teams distribute parallel for simd loop exprs were not " 11539 "built"); 11540 11541 if (!CurContext->isDependentContext()) { 11542 // Finalize the clauses that need pre-built expressions for CodeGen. 11543 for (OMPClause *C : Clauses) { 11544 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11545 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11546 B.NumIterations, *this, CurScope, 11547 DSAStack)) 11548 return StmtError(); 11549 } 11550 } 11551 11552 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11553 return StmtError(); 11554 11555 setFunctionHasBranchProtectedScope(); 11556 return OMPTargetTeamsDistributeParallelForSimdDirective::Create( 11557 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11558 } 11559 11560 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective( 11561 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc, 11562 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { 11563 if (!AStmt) 11564 return StmtError(); 11565 11566 auto *CS = cast<CapturedStmt>(AStmt); 11567 // 1.2.2 OpenMP Language Terminology 11568 // Structured block - An executable statement with a single entry at the 11569 // top and a single exit at the bottom. 11570 // The point of exit cannot be a branch out of the structured block. 11571 // longjmp() and throw() must not violate the entry/exit criteria. 11572 CS->getCapturedDecl()->setNothrow(); 11573 for (int ThisCaptureLevel = 11574 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd); 11575 ThisCaptureLevel > 1; --ThisCaptureLevel) { 11576 CS = cast<CapturedStmt>(CS->getCapturedStmt()); 11577 // 1.2.2 OpenMP Language Terminology 11578 // Structured block - An executable statement with a single entry at the 11579 // top and a single exit at the bottom. 11580 // The point of exit cannot be a branch out of the structured block. 11581 // longjmp() and throw() must not violate the entry/exit criteria. 11582 CS->getCapturedDecl()->setNothrow(); 11583 } 11584 11585 OMPLoopDirective::HelperExprs B; 11586 // In presence of clause 'collapse' with number of loops, it will 11587 // define the nested loops number. 11588 unsigned NestedLoopCount = checkOpenMPLoop( 11589 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses), 11590 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack, 11591 VarsWithImplicitDSA, B); 11592 if (NestedLoopCount == 0) 11593 return StmtError(); 11594 11595 assert((CurContext->isDependentContext() || B.builtAll()) && 11596 "omp target teams distribute simd loop exprs were not built"); 11597 11598 if (!CurContext->isDependentContext()) { 11599 // Finalize the clauses that need pre-built expressions for CodeGen. 11600 for (OMPClause *C : Clauses) { 11601 if (auto *LC = dyn_cast<OMPLinearClause>(C)) 11602 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef), 11603 B.NumIterations, *this, CurScope, 11604 DSAStack)) 11605 return StmtError(); 11606 } 11607 } 11608 11609 if (checkSimdlenSafelenSpecified(*this, Clauses)) 11610 return StmtError(); 11611 11612 setFunctionHasBranchProtectedScope(); 11613 return OMPTargetTeamsDistributeSimdDirective::Create( 11614 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B); 11615 } 11616 11617 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 11618 SourceLocation StartLoc, 11619 SourceLocation LParenLoc, 11620 SourceLocation EndLoc) { 11621 OMPClause *Res = nullptr; 11622 switch (Kind) { 11623 case OMPC_final: 11624 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc); 11625 break; 11626 case OMPC_num_threads: 11627 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 11628 break; 11629 case OMPC_safelen: 11630 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 11631 break; 11632 case OMPC_simdlen: 11633 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc); 11634 break; 11635 case OMPC_allocator: 11636 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc); 11637 break; 11638 case OMPC_collapse: 11639 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 11640 break; 11641 case OMPC_ordered: 11642 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr); 11643 break; 11644 case OMPC_num_teams: 11645 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc); 11646 break; 11647 case OMPC_thread_limit: 11648 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc); 11649 break; 11650 case OMPC_priority: 11651 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc); 11652 break; 11653 case OMPC_grainsize: 11654 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc); 11655 break; 11656 case OMPC_num_tasks: 11657 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc); 11658 break; 11659 case OMPC_hint: 11660 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc); 11661 break; 11662 case OMPC_depobj: 11663 Res = ActOnOpenMPDepobjClause(Expr, StartLoc, LParenLoc, EndLoc); 11664 break; 11665 case OMPC_detach: 11666 Res = ActOnOpenMPDetachClause(Expr, StartLoc, LParenLoc, EndLoc); 11667 break; 11668 case OMPC_device: 11669 case OMPC_if: 11670 case OMPC_default: 11671 case OMPC_proc_bind: 11672 case OMPC_schedule: 11673 case OMPC_private: 11674 case OMPC_firstprivate: 11675 case OMPC_lastprivate: 11676 case OMPC_shared: 11677 case OMPC_reduction: 11678 case OMPC_task_reduction: 11679 case OMPC_in_reduction: 11680 case OMPC_linear: 11681 case OMPC_aligned: 11682 case OMPC_copyin: 11683 case OMPC_copyprivate: 11684 case OMPC_nowait: 11685 case OMPC_untied: 11686 case OMPC_mergeable: 11687 case OMPC_threadprivate: 11688 case OMPC_allocate: 11689 case OMPC_flush: 11690 case OMPC_read: 11691 case OMPC_write: 11692 case OMPC_update: 11693 case OMPC_capture: 11694 case OMPC_seq_cst: 11695 case OMPC_acq_rel: 11696 case OMPC_acquire: 11697 case OMPC_release: 11698 case OMPC_relaxed: 11699 case OMPC_depend: 11700 case OMPC_threads: 11701 case OMPC_simd: 11702 case OMPC_map: 11703 case OMPC_nogroup: 11704 case OMPC_dist_schedule: 11705 case OMPC_defaultmap: 11706 case OMPC_unknown: 11707 case OMPC_uniform: 11708 case OMPC_to: 11709 case OMPC_from: 11710 case OMPC_use_device_ptr: 11711 case OMPC_use_device_addr: 11712 case OMPC_is_device_ptr: 11713 case OMPC_unified_address: 11714 case OMPC_unified_shared_memory: 11715 case OMPC_reverse_offload: 11716 case OMPC_dynamic_allocators: 11717 case OMPC_atomic_default_mem_order: 11718 case OMPC_device_type: 11719 case OMPC_match: 11720 case OMPC_nontemporal: 11721 case OMPC_order: 11722 case OMPC_destroy: 11723 case OMPC_inclusive: 11724 case OMPC_exclusive: 11725 case OMPC_uses_allocators: 11726 case OMPC_affinity: 11727 default: 11728 llvm_unreachable("Clause is not allowed."); 11729 } 11730 return Res; 11731 } 11732 11733 // An OpenMP directive such as 'target parallel' has two captured regions: 11734 // for the 'target' and 'parallel' respectively. This function returns 11735 // the region in which to capture expressions associated with a clause. 11736 // A return value of OMPD_unknown signifies that the expression should not 11737 // be captured. 11738 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause( 11739 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, unsigned OpenMPVersion, 11740 OpenMPDirectiveKind NameModifier = OMPD_unknown) { 11741 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 11742 switch (CKind) { 11743 case OMPC_if: 11744 switch (DKind) { 11745 case OMPD_target_parallel_for_simd: 11746 if (OpenMPVersion >= 50 && 11747 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 11748 CaptureRegion = OMPD_parallel; 11749 break; 11750 } 11751 LLVM_FALLTHROUGH; 11752 case OMPD_target_parallel: 11753 case OMPD_target_parallel_for: 11754 // If this clause applies to the nested 'parallel' region, capture within 11755 // the 'target' region, otherwise do not capture. 11756 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 11757 CaptureRegion = OMPD_target; 11758 break; 11759 case OMPD_target_teams_distribute_parallel_for_simd: 11760 if (OpenMPVersion >= 50 && 11761 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 11762 CaptureRegion = OMPD_parallel; 11763 break; 11764 } 11765 LLVM_FALLTHROUGH; 11766 case OMPD_target_teams_distribute_parallel_for: 11767 // If this clause applies to the nested 'parallel' region, capture within 11768 // the 'teams' region, otherwise do not capture. 11769 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel) 11770 CaptureRegion = OMPD_teams; 11771 break; 11772 case OMPD_teams_distribute_parallel_for_simd: 11773 if (OpenMPVersion >= 50 && 11774 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) { 11775 CaptureRegion = OMPD_parallel; 11776 break; 11777 } 11778 LLVM_FALLTHROUGH; 11779 case OMPD_teams_distribute_parallel_for: 11780 CaptureRegion = OMPD_teams; 11781 break; 11782 case OMPD_target_update: 11783 case OMPD_target_enter_data: 11784 case OMPD_target_exit_data: 11785 CaptureRegion = OMPD_task; 11786 break; 11787 case OMPD_parallel_master_taskloop: 11788 if (NameModifier == OMPD_unknown || NameModifier == OMPD_taskloop) 11789 CaptureRegion = OMPD_parallel; 11790 break; 11791 case OMPD_parallel_master_taskloop_simd: 11792 if ((OpenMPVersion <= 45 && NameModifier == OMPD_unknown) || 11793 NameModifier == OMPD_taskloop) { 11794 CaptureRegion = OMPD_parallel; 11795 break; 11796 } 11797 if (OpenMPVersion <= 45) 11798 break; 11799 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11800 CaptureRegion = OMPD_taskloop; 11801 break; 11802 case OMPD_parallel_for_simd: 11803 if (OpenMPVersion <= 45) 11804 break; 11805 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11806 CaptureRegion = OMPD_parallel; 11807 break; 11808 case OMPD_taskloop_simd: 11809 case OMPD_master_taskloop_simd: 11810 if (OpenMPVersion <= 45) 11811 break; 11812 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11813 CaptureRegion = OMPD_taskloop; 11814 break; 11815 case OMPD_distribute_parallel_for_simd: 11816 if (OpenMPVersion <= 45) 11817 break; 11818 if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd) 11819 CaptureRegion = OMPD_parallel; 11820 break; 11821 case OMPD_target_simd: 11822 if (OpenMPVersion >= 50 && 11823 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 11824 CaptureRegion = OMPD_target; 11825 break; 11826 case OMPD_teams_distribute_simd: 11827 case OMPD_target_teams_distribute_simd: 11828 if (OpenMPVersion >= 50 && 11829 (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) 11830 CaptureRegion = OMPD_teams; 11831 break; 11832 case OMPD_cancel: 11833 case OMPD_parallel: 11834 case OMPD_parallel_master: 11835 case OMPD_parallel_sections: 11836 case OMPD_parallel_for: 11837 case OMPD_target: 11838 case OMPD_target_teams: 11839 case OMPD_target_teams_distribute: 11840 case OMPD_distribute_parallel_for: 11841 case OMPD_task: 11842 case OMPD_taskloop: 11843 case OMPD_master_taskloop: 11844 case OMPD_target_data: 11845 case OMPD_simd: 11846 case OMPD_for_simd: 11847 case OMPD_distribute_simd: 11848 // Do not capture if-clause expressions. 11849 break; 11850 case OMPD_threadprivate: 11851 case OMPD_allocate: 11852 case OMPD_taskyield: 11853 case OMPD_barrier: 11854 case OMPD_taskwait: 11855 case OMPD_cancellation_point: 11856 case OMPD_flush: 11857 case OMPD_depobj: 11858 case OMPD_scan: 11859 case OMPD_declare_reduction: 11860 case OMPD_declare_mapper: 11861 case OMPD_declare_simd: 11862 case OMPD_declare_variant: 11863 case OMPD_begin_declare_variant: 11864 case OMPD_end_declare_variant: 11865 case OMPD_declare_target: 11866 case OMPD_end_declare_target: 11867 case OMPD_teams: 11868 case OMPD_for: 11869 case OMPD_sections: 11870 case OMPD_section: 11871 case OMPD_single: 11872 case OMPD_master: 11873 case OMPD_critical: 11874 case OMPD_taskgroup: 11875 case OMPD_distribute: 11876 case OMPD_ordered: 11877 case OMPD_atomic: 11878 case OMPD_teams_distribute: 11879 case OMPD_requires: 11880 llvm_unreachable("Unexpected OpenMP directive with if-clause"); 11881 case OMPD_unknown: 11882 default: 11883 llvm_unreachable("Unknown OpenMP directive"); 11884 } 11885 break; 11886 case OMPC_num_threads: 11887 switch (DKind) { 11888 case OMPD_target_parallel: 11889 case OMPD_target_parallel_for: 11890 case OMPD_target_parallel_for_simd: 11891 CaptureRegion = OMPD_target; 11892 break; 11893 case OMPD_teams_distribute_parallel_for: 11894 case OMPD_teams_distribute_parallel_for_simd: 11895 case OMPD_target_teams_distribute_parallel_for: 11896 case OMPD_target_teams_distribute_parallel_for_simd: 11897 CaptureRegion = OMPD_teams; 11898 break; 11899 case OMPD_parallel: 11900 case OMPD_parallel_master: 11901 case OMPD_parallel_sections: 11902 case OMPD_parallel_for: 11903 case OMPD_parallel_for_simd: 11904 case OMPD_distribute_parallel_for: 11905 case OMPD_distribute_parallel_for_simd: 11906 case OMPD_parallel_master_taskloop: 11907 case OMPD_parallel_master_taskloop_simd: 11908 // Do not capture num_threads-clause expressions. 11909 break; 11910 case OMPD_target_data: 11911 case OMPD_target_enter_data: 11912 case OMPD_target_exit_data: 11913 case OMPD_target_update: 11914 case OMPD_target: 11915 case OMPD_target_simd: 11916 case OMPD_target_teams: 11917 case OMPD_target_teams_distribute: 11918 case OMPD_target_teams_distribute_simd: 11919 case OMPD_cancel: 11920 case OMPD_task: 11921 case OMPD_taskloop: 11922 case OMPD_taskloop_simd: 11923 case OMPD_master_taskloop: 11924 case OMPD_master_taskloop_simd: 11925 case OMPD_threadprivate: 11926 case OMPD_allocate: 11927 case OMPD_taskyield: 11928 case OMPD_barrier: 11929 case OMPD_taskwait: 11930 case OMPD_cancellation_point: 11931 case OMPD_flush: 11932 case OMPD_depobj: 11933 case OMPD_scan: 11934 case OMPD_declare_reduction: 11935 case OMPD_declare_mapper: 11936 case OMPD_declare_simd: 11937 case OMPD_declare_variant: 11938 case OMPD_begin_declare_variant: 11939 case OMPD_end_declare_variant: 11940 case OMPD_declare_target: 11941 case OMPD_end_declare_target: 11942 case OMPD_teams: 11943 case OMPD_simd: 11944 case OMPD_for: 11945 case OMPD_for_simd: 11946 case OMPD_sections: 11947 case OMPD_section: 11948 case OMPD_single: 11949 case OMPD_master: 11950 case OMPD_critical: 11951 case OMPD_taskgroup: 11952 case OMPD_distribute: 11953 case OMPD_ordered: 11954 case OMPD_atomic: 11955 case OMPD_distribute_simd: 11956 case OMPD_teams_distribute: 11957 case OMPD_teams_distribute_simd: 11958 case OMPD_requires: 11959 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause"); 11960 case OMPD_unknown: 11961 default: 11962 llvm_unreachable("Unknown OpenMP directive"); 11963 } 11964 break; 11965 case OMPC_num_teams: 11966 switch (DKind) { 11967 case OMPD_target_teams: 11968 case OMPD_target_teams_distribute: 11969 case OMPD_target_teams_distribute_simd: 11970 case OMPD_target_teams_distribute_parallel_for: 11971 case OMPD_target_teams_distribute_parallel_for_simd: 11972 CaptureRegion = OMPD_target; 11973 break; 11974 case OMPD_teams_distribute_parallel_for: 11975 case OMPD_teams_distribute_parallel_for_simd: 11976 case OMPD_teams: 11977 case OMPD_teams_distribute: 11978 case OMPD_teams_distribute_simd: 11979 // Do not capture num_teams-clause expressions. 11980 break; 11981 case OMPD_distribute_parallel_for: 11982 case OMPD_distribute_parallel_for_simd: 11983 case OMPD_task: 11984 case OMPD_taskloop: 11985 case OMPD_taskloop_simd: 11986 case OMPD_master_taskloop: 11987 case OMPD_master_taskloop_simd: 11988 case OMPD_parallel_master_taskloop: 11989 case OMPD_parallel_master_taskloop_simd: 11990 case OMPD_target_data: 11991 case OMPD_target_enter_data: 11992 case OMPD_target_exit_data: 11993 case OMPD_target_update: 11994 case OMPD_cancel: 11995 case OMPD_parallel: 11996 case OMPD_parallel_master: 11997 case OMPD_parallel_sections: 11998 case OMPD_parallel_for: 11999 case OMPD_parallel_for_simd: 12000 case OMPD_target: 12001 case OMPD_target_simd: 12002 case OMPD_target_parallel: 12003 case OMPD_target_parallel_for: 12004 case OMPD_target_parallel_for_simd: 12005 case OMPD_threadprivate: 12006 case OMPD_allocate: 12007 case OMPD_taskyield: 12008 case OMPD_barrier: 12009 case OMPD_taskwait: 12010 case OMPD_cancellation_point: 12011 case OMPD_flush: 12012 case OMPD_depobj: 12013 case OMPD_scan: 12014 case OMPD_declare_reduction: 12015 case OMPD_declare_mapper: 12016 case OMPD_declare_simd: 12017 case OMPD_declare_variant: 12018 case OMPD_begin_declare_variant: 12019 case OMPD_end_declare_variant: 12020 case OMPD_declare_target: 12021 case OMPD_end_declare_target: 12022 case OMPD_simd: 12023 case OMPD_for: 12024 case OMPD_for_simd: 12025 case OMPD_sections: 12026 case OMPD_section: 12027 case OMPD_single: 12028 case OMPD_master: 12029 case OMPD_critical: 12030 case OMPD_taskgroup: 12031 case OMPD_distribute: 12032 case OMPD_ordered: 12033 case OMPD_atomic: 12034 case OMPD_distribute_simd: 12035 case OMPD_requires: 12036 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 12037 case OMPD_unknown: 12038 default: 12039 llvm_unreachable("Unknown OpenMP directive"); 12040 } 12041 break; 12042 case OMPC_thread_limit: 12043 switch (DKind) { 12044 case OMPD_target_teams: 12045 case OMPD_target_teams_distribute: 12046 case OMPD_target_teams_distribute_simd: 12047 case OMPD_target_teams_distribute_parallel_for: 12048 case OMPD_target_teams_distribute_parallel_for_simd: 12049 CaptureRegion = OMPD_target; 12050 break; 12051 case OMPD_teams_distribute_parallel_for: 12052 case OMPD_teams_distribute_parallel_for_simd: 12053 case OMPD_teams: 12054 case OMPD_teams_distribute: 12055 case OMPD_teams_distribute_simd: 12056 // Do not capture thread_limit-clause expressions. 12057 break; 12058 case OMPD_distribute_parallel_for: 12059 case OMPD_distribute_parallel_for_simd: 12060 case OMPD_task: 12061 case OMPD_taskloop: 12062 case OMPD_taskloop_simd: 12063 case OMPD_master_taskloop: 12064 case OMPD_master_taskloop_simd: 12065 case OMPD_parallel_master_taskloop: 12066 case OMPD_parallel_master_taskloop_simd: 12067 case OMPD_target_data: 12068 case OMPD_target_enter_data: 12069 case OMPD_target_exit_data: 12070 case OMPD_target_update: 12071 case OMPD_cancel: 12072 case OMPD_parallel: 12073 case OMPD_parallel_master: 12074 case OMPD_parallel_sections: 12075 case OMPD_parallel_for: 12076 case OMPD_parallel_for_simd: 12077 case OMPD_target: 12078 case OMPD_target_simd: 12079 case OMPD_target_parallel: 12080 case OMPD_target_parallel_for: 12081 case OMPD_target_parallel_for_simd: 12082 case OMPD_threadprivate: 12083 case OMPD_allocate: 12084 case OMPD_taskyield: 12085 case OMPD_barrier: 12086 case OMPD_taskwait: 12087 case OMPD_cancellation_point: 12088 case OMPD_flush: 12089 case OMPD_depobj: 12090 case OMPD_scan: 12091 case OMPD_declare_reduction: 12092 case OMPD_declare_mapper: 12093 case OMPD_declare_simd: 12094 case OMPD_declare_variant: 12095 case OMPD_begin_declare_variant: 12096 case OMPD_end_declare_variant: 12097 case OMPD_declare_target: 12098 case OMPD_end_declare_target: 12099 case OMPD_simd: 12100 case OMPD_for: 12101 case OMPD_for_simd: 12102 case OMPD_sections: 12103 case OMPD_section: 12104 case OMPD_single: 12105 case OMPD_master: 12106 case OMPD_critical: 12107 case OMPD_taskgroup: 12108 case OMPD_distribute: 12109 case OMPD_ordered: 12110 case OMPD_atomic: 12111 case OMPD_distribute_simd: 12112 case OMPD_requires: 12113 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause"); 12114 case OMPD_unknown: 12115 default: 12116 llvm_unreachable("Unknown OpenMP directive"); 12117 } 12118 break; 12119 case OMPC_schedule: 12120 switch (DKind) { 12121 case OMPD_parallel_for: 12122 case OMPD_parallel_for_simd: 12123 case OMPD_distribute_parallel_for: 12124 case OMPD_distribute_parallel_for_simd: 12125 case OMPD_teams_distribute_parallel_for: 12126 case OMPD_teams_distribute_parallel_for_simd: 12127 case OMPD_target_parallel_for: 12128 case OMPD_target_parallel_for_simd: 12129 case OMPD_target_teams_distribute_parallel_for: 12130 case OMPD_target_teams_distribute_parallel_for_simd: 12131 CaptureRegion = OMPD_parallel; 12132 break; 12133 case OMPD_for: 12134 case OMPD_for_simd: 12135 // Do not capture schedule-clause expressions. 12136 break; 12137 case OMPD_task: 12138 case OMPD_taskloop: 12139 case OMPD_taskloop_simd: 12140 case OMPD_master_taskloop: 12141 case OMPD_master_taskloop_simd: 12142 case OMPD_parallel_master_taskloop: 12143 case OMPD_parallel_master_taskloop_simd: 12144 case OMPD_target_data: 12145 case OMPD_target_enter_data: 12146 case OMPD_target_exit_data: 12147 case OMPD_target_update: 12148 case OMPD_teams: 12149 case OMPD_teams_distribute: 12150 case OMPD_teams_distribute_simd: 12151 case OMPD_target_teams_distribute: 12152 case OMPD_target_teams_distribute_simd: 12153 case OMPD_target: 12154 case OMPD_target_simd: 12155 case OMPD_target_parallel: 12156 case OMPD_cancel: 12157 case OMPD_parallel: 12158 case OMPD_parallel_master: 12159 case OMPD_parallel_sections: 12160 case OMPD_threadprivate: 12161 case OMPD_allocate: 12162 case OMPD_taskyield: 12163 case OMPD_barrier: 12164 case OMPD_taskwait: 12165 case OMPD_cancellation_point: 12166 case OMPD_flush: 12167 case OMPD_depobj: 12168 case OMPD_scan: 12169 case OMPD_declare_reduction: 12170 case OMPD_declare_mapper: 12171 case OMPD_declare_simd: 12172 case OMPD_declare_variant: 12173 case OMPD_begin_declare_variant: 12174 case OMPD_end_declare_variant: 12175 case OMPD_declare_target: 12176 case OMPD_end_declare_target: 12177 case OMPD_simd: 12178 case OMPD_sections: 12179 case OMPD_section: 12180 case OMPD_single: 12181 case OMPD_master: 12182 case OMPD_critical: 12183 case OMPD_taskgroup: 12184 case OMPD_distribute: 12185 case OMPD_ordered: 12186 case OMPD_atomic: 12187 case OMPD_distribute_simd: 12188 case OMPD_target_teams: 12189 case OMPD_requires: 12190 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 12191 case OMPD_unknown: 12192 default: 12193 llvm_unreachable("Unknown OpenMP directive"); 12194 } 12195 break; 12196 case OMPC_dist_schedule: 12197 switch (DKind) { 12198 case OMPD_teams_distribute_parallel_for: 12199 case OMPD_teams_distribute_parallel_for_simd: 12200 case OMPD_teams_distribute: 12201 case OMPD_teams_distribute_simd: 12202 case OMPD_target_teams_distribute_parallel_for: 12203 case OMPD_target_teams_distribute_parallel_for_simd: 12204 case OMPD_target_teams_distribute: 12205 case OMPD_target_teams_distribute_simd: 12206 CaptureRegion = OMPD_teams; 12207 break; 12208 case OMPD_distribute_parallel_for: 12209 case OMPD_distribute_parallel_for_simd: 12210 case OMPD_distribute: 12211 case OMPD_distribute_simd: 12212 // Do not capture thread_limit-clause expressions. 12213 break; 12214 case OMPD_parallel_for: 12215 case OMPD_parallel_for_simd: 12216 case OMPD_target_parallel_for_simd: 12217 case OMPD_target_parallel_for: 12218 case OMPD_task: 12219 case OMPD_taskloop: 12220 case OMPD_taskloop_simd: 12221 case OMPD_master_taskloop: 12222 case OMPD_master_taskloop_simd: 12223 case OMPD_parallel_master_taskloop: 12224 case OMPD_parallel_master_taskloop_simd: 12225 case OMPD_target_data: 12226 case OMPD_target_enter_data: 12227 case OMPD_target_exit_data: 12228 case OMPD_target_update: 12229 case OMPD_teams: 12230 case OMPD_target: 12231 case OMPD_target_simd: 12232 case OMPD_target_parallel: 12233 case OMPD_cancel: 12234 case OMPD_parallel: 12235 case OMPD_parallel_master: 12236 case OMPD_parallel_sections: 12237 case OMPD_threadprivate: 12238 case OMPD_allocate: 12239 case OMPD_taskyield: 12240 case OMPD_barrier: 12241 case OMPD_taskwait: 12242 case OMPD_cancellation_point: 12243 case OMPD_flush: 12244 case OMPD_depobj: 12245 case OMPD_scan: 12246 case OMPD_declare_reduction: 12247 case OMPD_declare_mapper: 12248 case OMPD_declare_simd: 12249 case OMPD_declare_variant: 12250 case OMPD_begin_declare_variant: 12251 case OMPD_end_declare_variant: 12252 case OMPD_declare_target: 12253 case OMPD_end_declare_target: 12254 case OMPD_simd: 12255 case OMPD_for: 12256 case OMPD_for_simd: 12257 case OMPD_sections: 12258 case OMPD_section: 12259 case OMPD_single: 12260 case OMPD_master: 12261 case OMPD_critical: 12262 case OMPD_taskgroup: 12263 case OMPD_ordered: 12264 case OMPD_atomic: 12265 case OMPD_target_teams: 12266 case OMPD_requires: 12267 llvm_unreachable("Unexpected OpenMP directive with schedule clause"); 12268 case OMPD_unknown: 12269 default: 12270 llvm_unreachable("Unknown OpenMP directive"); 12271 } 12272 break; 12273 case OMPC_device: 12274 switch (DKind) { 12275 case OMPD_target_update: 12276 case OMPD_target_enter_data: 12277 case OMPD_target_exit_data: 12278 case OMPD_target: 12279 case OMPD_target_simd: 12280 case OMPD_target_teams: 12281 case OMPD_target_parallel: 12282 case OMPD_target_teams_distribute: 12283 case OMPD_target_teams_distribute_simd: 12284 case OMPD_target_parallel_for: 12285 case OMPD_target_parallel_for_simd: 12286 case OMPD_target_teams_distribute_parallel_for: 12287 case OMPD_target_teams_distribute_parallel_for_simd: 12288 CaptureRegion = OMPD_task; 12289 break; 12290 case OMPD_target_data: 12291 // Do not capture device-clause expressions. 12292 break; 12293 case OMPD_teams_distribute_parallel_for: 12294 case OMPD_teams_distribute_parallel_for_simd: 12295 case OMPD_teams: 12296 case OMPD_teams_distribute: 12297 case OMPD_teams_distribute_simd: 12298 case OMPD_distribute_parallel_for: 12299 case OMPD_distribute_parallel_for_simd: 12300 case OMPD_task: 12301 case OMPD_taskloop: 12302 case OMPD_taskloop_simd: 12303 case OMPD_master_taskloop: 12304 case OMPD_master_taskloop_simd: 12305 case OMPD_parallel_master_taskloop: 12306 case OMPD_parallel_master_taskloop_simd: 12307 case OMPD_cancel: 12308 case OMPD_parallel: 12309 case OMPD_parallel_master: 12310 case OMPD_parallel_sections: 12311 case OMPD_parallel_for: 12312 case OMPD_parallel_for_simd: 12313 case OMPD_threadprivate: 12314 case OMPD_allocate: 12315 case OMPD_taskyield: 12316 case OMPD_barrier: 12317 case OMPD_taskwait: 12318 case OMPD_cancellation_point: 12319 case OMPD_flush: 12320 case OMPD_depobj: 12321 case OMPD_scan: 12322 case OMPD_declare_reduction: 12323 case OMPD_declare_mapper: 12324 case OMPD_declare_simd: 12325 case OMPD_declare_variant: 12326 case OMPD_begin_declare_variant: 12327 case OMPD_end_declare_variant: 12328 case OMPD_declare_target: 12329 case OMPD_end_declare_target: 12330 case OMPD_simd: 12331 case OMPD_for: 12332 case OMPD_for_simd: 12333 case OMPD_sections: 12334 case OMPD_section: 12335 case OMPD_single: 12336 case OMPD_master: 12337 case OMPD_critical: 12338 case OMPD_taskgroup: 12339 case OMPD_distribute: 12340 case OMPD_ordered: 12341 case OMPD_atomic: 12342 case OMPD_distribute_simd: 12343 case OMPD_requires: 12344 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause"); 12345 case OMPD_unknown: 12346 default: 12347 llvm_unreachable("Unknown OpenMP directive"); 12348 } 12349 break; 12350 case OMPC_grainsize: 12351 case OMPC_num_tasks: 12352 case OMPC_final: 12353 case OMPC_priority: 12354 switch (DKind) { 12355 case OMPD_task: 12356 case OMPD_taskloop: 12357 case OMPD_taskloop_simd: 12358 case OMPD_master_taskloop: 12359 case OMPD_master_taskloop_simd: 12360 break; 12361 case OMPD_parallel_master_taskloop: 12362 case OMPD_parallel_master_taskloop_simd: 12363 CaptureRegion = OMPD_parallel; 12364 break; 12365 case OMPD_target_update: 12366 case OMPD_target_enter_data: 12367 case OMPD_target_exit_data: 12368 case OMPD_target: 12369 case OMPD_target_simd: 12370 case OMPD_target_teams: 12371 case OMPD_target_parallel: 12372 case OMPD_target_teams_distribute: 12373 case OMPD_target_teams_distribute_simd: 12374 case OMPD_target_parallel_for: 12375 case OMPD_target_parallel_for_simd: 12376 case OMPD_target_teams_distribute_parallel_for: 12377 case OMPD_target_teams_distribute_parallel_for_simd: 12378 case OMPD_target_data: 12379 case OMPD_teams_distribute_parallel_for: 12380 case OMPD_teams_distribute_parallel_for_simd: 12381 case OMPD_teams: 12382 case OMPD_teams_distribute: 12383 case OMPD_teams_distribute_simd: 12384 case OMPD_distribute_parallel_for: 12385 case OMPD_distribute_parallel_for_simd: 12386 case OMPD_cancel: 12387 case OMPD_parallel: 12388 case OMPD_parallel_master: 12389 case OMPD_parallel_sections: 12390 case OMPD_parallel_for: 12391 case OMPD_parallel_for_simd: 12392 case OMPD_threadprivate: 12393 case OMPD_allocate: 12394 case OMPD_taskyield: 12395 case OMPD_barrier: 12396 case OMPD_taskwait: 12397 case OMPD_cancellation_point: 12398 case OMPD_flush: 12399 case OMPD_depobj: 12400 case OMPD_scan: 12401 case OMPD_declare_reduction: 12402 case OMPD_declare_mapper: 12403 case OMPD_declare_simd: 12404 case OMPD_declare_variant: 12405 case OMPD_begin_declare_variant: 12406 case OMPD_end_declare_variant: 12407 case OMPD_declare_target: 12408 case OMPD_end_declare_target: 12409 case OMPD_simd: 12410 case OMPD_for: 12411 case OMPD_for_simd: 12412 case OMPD_sections: 12413 case OMPD_section: 12414 case OMPD_single: 12415 case OMPD_master: 12416 case OMPD_critical: 12417 case OMPD_taskgroup: 12418 case OMPD_distribute: 12419 case OMPD_ordered: 12420 case OMPD_atomic: 12421 case OMPD_distribute_simd: 12422 case OMPD_requires: 12423 llvm_unreachable("Unexpected OpenMP directive with grainsize-clause"); 12424 case OMPD_unknown: 12425 default: 12426 llvm_unreachable("Unknown OpenMP directive"); 12427 } 12428 break; 12429 case OMPC_firstprivate: 12430 case OMPC_lastprivate: 12431 case OMPC_reduction: 12432 case OMPC_task_reduction: 12433 case OMPC_in_reduction: 12434 case OMPC_linear: 12435 case OMPC_default: 12436 case OMPC_proc_bind: 12437 case OMPC_safelen: 12438 case OMPC_simdlen: 12439 case OMPC_allocator: 12440 case OMPC_collapse: 12441 case OMPC_private: 12442 case OMPC_shared: 12443 case OMPC_aligned: 12444 case OMPC_copyin: 12445 case OMPC_copyprivate: 12446 case OMPC_ordered: 12447 case OMPC_nowait: 12448 case OMPC_untied: 12449 case OMPC_mergeable: 12450 case OMPC_threadprivate: 12451 case OMPC_allocate: 12452 case OMPC_flush: 12453 case OMPC_depobj: 12454 case OMPC_read: 12455 case OMPC_write: 12456 case OMPC_update: 12457 case OMPC_capture: 12458 case OMPC_seq_cst: 12459 case OMPC_acq_rel: 12460 case OMPC_acquire: 12461 case OMPC_release: 12462 case OMPC_relaxed: 12463 case OMPC_depend: 12464 case OMPC_threads: 12465 case OMPC_simd: 12466 case OMPC_map: 12467 case OMPC_nogroup: 12468 case OMPC_hint: 12469 case OMPC_defaultmap: 12470 case OMPC_unknown: 12471 case OMPC_uniform: 12472 case OMPC_to: 12473 case OMPC_from: 12474 case OMPC_use_device_ptr: 12475 case OMPC_use_device_addr: 12476 case OMPC_is_device_ptr: 12477 case OMPC_unified_address: 12478 case OMPC_unified_shared_memory: 12479 case OMPC_reverse_offload: 12480 case OMPC_dynamic_allocators: 12481 case OMPC_atomic_default_mem_order: 12482 case OMPC_device_type: 12483 case OMPC_match: 12484 case OMPC_nontemporal: 12485 case OMPC_order: 12486 case OMPC_destroy: 12487 case OMPC_detach: 12488 case OMPC_inclusive: 12489 case OMPC_exclusive: 12490 case OMPC_uses_allocators: 12491 case OMPC_affinity: 12492 default: 12493 llvm_unreachable("Unexpected OpenMP clause."); 12494 } 12495 return CaptureRegion; 12496 } 12497 12498 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier, 12499 Expr *Condition, SourceLocation StartLoc, 12500 SourceLocation LParenLoc, 12501 SourceLocation NameModifierLoc, 12502 SourceLocation ColonLoc, 12503 SourceLocation EndLoc) { 12504 Expr *ValExpr = Condition; 12505 Stmt *HelperValStmt = nullptr; 12506 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 12507 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 12508 !Condition->isInstantiationDependent() && 12509 !Condition->containsUnexpandedParameterPack()) { 12510 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 12511 if (Val.isInvalid()) 12512 return nullptr; 12513 12514 ValExpr = Val.get(); 12515 12516 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12517 CaptureRegion = getOpenMPCaptureRegionForClause( 12518 DKind, OMPC_if, LangOpts.OpenMP, NameModifier); 12519 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12520 ValExpr = MakeFullExpr(ValExpr).get(); 12521 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12522 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12523 HelperValStmt = buildPreInits(Context, Captures); 12524 } 12525 } 12526 12527 return new (Context) 12528 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 12529 LParenLoc, NameModifierLoc, ColonLoc, EndLoc); 12530 } 12531 12532 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition, 12533 SourceLocation StartLoc, 12534 SourceLocation LParenLoc, 12535 SourceLocation EndLoc) { 12536 Expr *ValExpr = Condition; 12537 Stmt *HelperValStmt = nullptr; 12538 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 12539 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 12540 !Condition->isInstantiationDependent() && 12541 !Condition->containsUnexpandedParameterPack()) { 12542 ExprResult Val = CheckBooleanCondition(StartLoc, Condition); 12543 if (Val.isInvalid()) 12544 return nullptr; 12545 12546 ValExpr = MakeFullExpr(Val.get()).get(); 12547 12548 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12549 CaptureRegion = 12550 getOpenMPCaptureRegionForClause(DKind, OMPC_final, LangOpts.OpenMP); 12551 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12552 ValExpr = MakeFullExpr(ValExpr).get(); 12553 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12554 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12555 HelperValStmt = buildPreInits(Context, Captures); 12556 } 12557 } 12558 12559 return new (Context) OMPFinalClause(ValExpr, HelperValStmt, CaptureRegion, 12560 StartLoc, LParenLoc, EndLoc); 12561 } 12562 12563 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 12564 Expr *Op) { 12565 if (!Op) 12566 return ExprError(); 12567 12568 class IntConvertDiagnoser : public ICEConvertDiagnoser { 12569 public: 12570 IntConvertDiagnoser() 12571 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 12572 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 12573 QualType T) override { 12574 return S.Diag(Loc, diag::err_omp_not_integral) << T; 12575 } 12576 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 12577 QualType T) override { 12578 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 12579 } 12580 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 12581 QualType T, 12582 QualType ConvTy) override { 12583 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 12584 } 12585 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 12586 QualType ConvTy) override { 12587 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 12588 << ConvTy->isEnumeralType() << ConvTy; 12589 } 12590 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 12591 QualType T) override { 12592 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 12593 } 12594 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 12595 QualType ConvTy) override { 12596 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 12597 << ConvTy->isEnumeralType() << ConvTy; 12598 } 12599 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 12600 QualType) override { 12601 llvm_unreachable("conversion functions are permitted"); 12602 } 12603 } ConvertDiagnoser; 12604 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 12605 } 12606 12607 static bool 12608 isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind, 12609 bool StrictlyPositive, bool BuildCapture = false, 12610 OpenMPDirectiveKind DKind = OMPD_unknown, 12611 OpenMPDirectiveKind *CaptureRegion = nullptr, 12612 Stmt **HelperValStmt = nullptr) { 12613 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() && 12614 !ValExpr->isInstantiationDependent()) { 12615 SourceLocation Loc = ValExpr->getExprLoc(); 12616 ExprResult Value = 12617 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr); 12618 if (Value.isInvalid()) 12619 return false; 12620 12621 ValExpr = Value.get(); 12622 // The expression must evaluate to a non-negative integer value. 12623 llvm::APSInt Result; 12624 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) && 12625 Result.isSigned() && 12626 !((!StrictlyPositive && Result.isNonNegative()) || 12627 (StrictlyPositive && Result.isStrictlyPositive()))) { 12628 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause) 12629 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 12630 << ValExpr->getSourceRange(); 12631 return false; 12632 } 12633 if (!BuildCapture) 12634 return true; 12635 *CaptureRegion = 12636 getOpenMPCaptureRegionForClause(DKind, CKind, SemaRef.LangOpts.OpenMP); 12637 if (*CaptureRegion != OMPD_unknown && 12638 !SemaRef.CurContext->isDependentContext()) { 12639 ValExpr = SemaRef.MakeFullExpr(ValExpr).get(); 12640 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12641 ValExpr = tryBuildCapture(SemaRef, ValExpr, Captures).get(); 12642 *HelperValStmt = buildPreInits(SemaRef.Context, Captures); 12643 } 12644 } 12645 return true; 12646 } 12647 12648 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 12649 SourceLocation StartLoc, 12650 SourceLocation LParenLoc, 12651 SourceLocation EndLoc) { 12652 Expr *ValExpr = NumThreads; 12653 Stmt *HelperValStmt = nullptr; 12654 12655 // OpenMP [2.5, Restrictions] 12656 // The num_threads expression must evaluate to a positive integer value. 12657 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads, 12658 /*StrictlyPositive=*/true)) 12659 return nullptr; 12660 12661 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 12662 OpenMPDirectiveKind CaptureRegion = 12663 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads, LangOpts.OpenMP); 12664 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 12665 ValExpr = MakeFullExpr(ValExpr).get(); 12666 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 12667 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 12668 HelperValStmt = buildPreInits(Context, Captures); 12669 } 12670 12671 return new (Context) OMPNumThreadsClause( 12672 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 12673 } 12674 12675 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 12676 OpenMPClauseKind CKind, 12677 bool StrictlyPositive) { 12678 if (!E) 12679 return ExprError(); 12680 if (E->isValueDependent() || E->isTypeDependent() || 12681 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 12682 return E; 12683 llvm::APSInt Result; 12684 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); 12685 if (ICE.isInvalid()) 12686 return ExprError(); 12687 if ((StrictlyPositive && !Result.isStrictlyPositive()) || 12688 (!StrictlyPositive && !Result.isNonNegative())) { 12689 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 12690 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0) 12691 << E->getSourceRange(); 12692 return ExprError(); 12693 } 12694 if (CKind == OMPC_aligned && !Result.isPowerOf2()) { 12695 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two) 12696 << E->getSourceRange(); 12697 return ExprError(); 12698 } 12699 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1) 12700 DSAStack->setAssociatedLoops(Result.getExtValue()); 12701 else if (CKind == OMPC_ordered) 12702 DSAStack->setAssociatedLoops(Result.getExtValue()); 12703 return ICE; 12704 } 12705 12706 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 12707 SourceLocation LParenLoc, 12708 SourceLocation EndLoc) { 12709 // OpenMP [2.8.1, simd construct, Description] 12710 // The parameter of the safelen clause must be a constant 12711 // positive integer expression. 12712 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 12713 if (Safelen.isInvalid()) 12714 return nullptr; 12715 return new (Context) 12716 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 12717 } 12718 12719 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc, 12720 SourceLocation LParenLoc, 12721 SourceLocation EndLoc) { 12722 // OpenMP [2.8.1, simd construct, Description] 12723 // The parameter of the simdlen clause must be a constant 12724 // positive integer expression. 12725 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen); 12726 if (Simdlen.isInvalid()) 12727 return nullptr; 12728 return new (Context) 12729 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc); 12730 } 12731 12732 /// Tries to find omp_allocator_handle_t type. 12733 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc, 12734 DSAStackTy *Stack) { 12735 QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT(); 12736 if (!OMPAllocatorHandleT.isNull()) 12737 return true; 12738 // Build the predefined allocator expressions. 12739 bool ErrorFound = false; 12740 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 12741 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 12742 StringRef Allocator = 12743 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 12744 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator); 12745 auto *VD = dyn_cast_or_null<ValueDecl>( 12746 S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName)); 12747 if (!VD) { 12748 ErrorFound = true; 12749 break; 12750 } 12751 QualType AllocatorType = 12752 VD->getType().getNonLValueExprType(S.getASTContext()); 12753 ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc); 12754 if (!Res.isUsable()) { 12755 ErrorFound = true; 12756 break; 12757 } 12758 if (OMPAllocatorHandleT.isNull()) 12759 OMPAllocatorHandleT = AllocatorType; 12760 if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) { 12761 ErrorFound = true; 12762 break; 12763 } 12764 Stack->setAllocator(AllocatorKind, Res.get()); 12765 } 12766 if (ErrorFound) { 12767 S.Diag(Loc, diag::err_omp_implied_type_not_found) 12768 << "omp_allocator_handle_t"; 12769 return false; 12770 } 12771 OMPAllocatorHandleT.addConst(); 12772 Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT); 12773 return true; 12774 } 12775 12776 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc, 12777 SourceLocation LParenLoc, 12778 SourceLocation EndLoc) { 12779 // OpenMP [2.11.3, allocate Directive, Description] 12780 // allocator is an expression of omp_allocator_handle_t type. 12781 if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack)) 12782 return nullptr; 12783 12784 ExprResult Allocator = DefaultLvalueConversion(A); 12785 if (Allocator.isInvalid()) 12786 return nullptr; 12787 Allocator = PerformImplicitConversion(Allocator.get(), 12788 DSAStack->getOMPAllocatorHandleT(), 12789 Sema::AA_Initializing, 12790 /*AllowExplicit=*/true); 12791 if (Allocator.isInvalid()) 12792 return nullptr; 12793 return new (Context) 12794 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc); 12795 } 12796 12797 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 12798 SourceLocation StartLoc, 12799 SourceLocation LParenLoc, 12800 SourceLocation EndLoc) { 12801 // OpenMP [2.7.1, loop construct, Description] 12802 // OpenMP [2.8.1, simd construct, Description] 12803 // OpenMP [2.9.6, distribute construct, Description] 12804 // The parameter of the collapse clause must be a constant 12805 // positive integer expression. 12806 ExprResult NumForLoopsResult = 12807 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 12808 if (NumForLoopsResult.isInvalid()) 12809 return nullptr; 12810 return new (Context) 12811 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 12812 } 12813 12814 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 12815 SourceLocation EndLoc, 12816 SourceLocation LParenLoc, 12817 Expr *NumForLoops) { 12818 // OpenMP [2.7.1, loop construct, Description] 12819 // OpenMP [2.8.1, simd construct, Description] 12820 // OpenMP [2.9.6, distribute construct, Description] 12821 // The parameter of the ordered clause must be a constant 12822 // positive integer expression if any. 12823 if (NumForLoops && LParenLoc.isValid()) { 12824 ExprResult NumForLoopsResult = 12825 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered); 12826 if (NumForLoopsResult.isInvalid()) 12827 return nullptr; 12828 NumForLoops = NumForLoopsResult.get(); 12829 } else { 12830 NumForLoops = nullptr; 12831 } 12832 auto *Clause = OMPOrderedClause::Create( 12833 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0, 12834 StartLoc, LParenLoc, EndLoc); 12835 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause); 12836 return Clause; 12837 } 12838 12839 OMPClause *Sema::ActOnOpenMPSimpleClause( 12840 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 12841 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 12842 OMPClause *Res = nullptr; 12843 switch (Kind) { 12844 case OMPC_default: 12845 Res = ActOnOpenMPDefaultClause(static_cast<DefaultKind>(Argument), 12846 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12847 break; 12848 case OMPC_proc_bind: 12849 Res = ActOnOpenMPProcBindClause(static_cast<ProcBindKind>(Argument), 12850 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12851 break; 12852 case OMPC_atomic_default_mem_order: 12853 Res = ActOnOpenMPAtomicDefaultMemOrderClause( 12854 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument), 12855 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12856 break; 12857 case OMPC_order: 12858 Res = ActOnOpenMPOrderClause(static_cast<OpenMPOrderClauseKind>(Argument), 12859 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12860 break; 12861 case OMPC_update: 12862 Res = ActOnOpenMPUpdateClause(static_cast<OpenMPDependClauseKind>(Argument), 12863 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 12864 break; 12865 case OMPC_if: 12866 case OMPC_final: 12867 case OMPC_num_threads: 12868 case OMPC_safelen: 12869 case OMPC_simdlen: 12870 case OMPC_allocator: 12871 case OMPC_collapse: 12872 case OMPC_schedule: 12873 case OMPC_private: 12874 case OMPC_firstprivate: 12875 case OMPC_lastprivate: 12876 case OMPC_shared: 12877 case OMPC_reduction: 12878 case OMPC_task_reduction: 12879 case OMPC_in_reduction: 12880 case OMPC_linear: 12881 case OMPC_aligned: 12882 case OMPC_copyin: 12883 case OMPC_copyprivate: 12884 case OMPC_ordered: 12885 case OMPC_nowait: 12886 case OMPC_untied: 12887 case OMPC_mergeable: 12888 case OMPC_threadprivate: 12889 case OMPC_allocate: 12890 case OMPC_flush: 12891 case OMPC_depobj: 12892 case OMPC_read: 12893 case OMPC_write: 12894 case OMPC_capture: 12895 case OMPC_seq_cst: 12896 case OMPC_acq_rel: 12897 case OMPC_acquire: 12898 case OMPC_release: 12899 case OMPC_relaxed: 12900 case OMPC_depend: 12901 case OMPC_device: 12902 case OMPC_threads: 12903 case OMPC_simd: 12904 case OMPC_map: 12905 case OMPC_num_teams: 12906 case OMPC_thread_limit: 12907 case OMPC_priority: 12908 case OMPC_grainsize: 12909 case OMPC_nogroup: 12910 case OMPC_num_tasks: 12911 case OMPC_hint: 12912 case OMPC_dist_schedule: 12913 case OMPC_defaultmap: 12914 case OMPC_unknown: 12915 case OMPC_uniform: 12916 case OMPC_to: 12917 case OMPC_from: 12918 case OMPC_use_device_ptr: 12919 case OMPC_use_device_addr: 12920 case OMPC_is_device_ptr: 12921 case OMPC_unified_address: 12922 case OMPC_unified_shared_memory: 12923 case OMPC_reverse_offload: 12924 case OMPC_dynamic_allocators: 12925 case OMPC_device_type: 12926 case OMPC_match: 12927 case OMPC_nontemporal: 12928 case OMPC_destroy: 12929 case OMPC_detach: 12930 case OMPC_inclusive: 12931 case OMPC_exclusive: 12932 case OMPC_uses_allocators: 12933 case OMPC_affinity: 12934 default: 12935 llvm_unreachable("Clause is not allowed."); 12936 } 12937 return Res; 12938 } 12939 12940 static std::string 12941 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last, 12942 ArrayRef<unsigned> Exclude = llvm::None) { 12943 SmallString<256> Buffer; 12944 llvm::raw_svector_ostream Out(Buffer); 12945 unsigned Skipped = Exclude.size(); 12946 auto S = Exclude.begin(), E = Exclude.end(); 12947 for (unsigned I = First; I < Last; ++I) { 12948 if (std::find(S, E, I) != E) { 12949 --Skipped; 12950 continue; 12951 } 12952 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'"; 12953 if (I + Skipped + 2 == Last) 12954 Out << " or "; 12955 else if (I + Skipped + 1 != Last) 12956 Out << ", "; 12957 } 12958 return std::string(Out.str()); 12959 } 12960 12961 OMPClause *Sema::ActOnOpenMPDefaultClause(DefaultKind Kind, 12962 SourceLocation KindKwLoc, 12963 SourceLocation StartLoc, 12964 SourceLocation LParenLoc, 12965 SourceLocation EndLoc) { 12966 if (Kind == OMP_DEFAULT_unknown) { 12967 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 12968 << getListOfPossibleValues(OMPC_default, /*First=*/0, 12969 /*Last=*/unsigned(OMP_DEFAULT_unknown)) 12970 << getOpenMPClauseName(OMPC_default); 12971 return nullptr; 12972 } 12973 12974 switch (Kind) { 12975 case OMP_DEFAULT_none: 12976 DSAStack->setDefaultDSANone(KindKwLoc); 12977 break; 12978 case OMP_DEFAULT_shared: 12979 DSAStack->setDefaultDSAShared(KindKwLoc); 12980 break; 12981 case OMP_DEFAULT_firstprivate: 12982 DSAStack->setDefaultDSAFirstPrivate(KindKwLoc); 12983 break; 12984 default: 12985 llvm_unreachable("DSA unexpected in OpenMP default clause"); 12986 } 12987 12988 return new (Context) 12989 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 12990 } 12991 12992 OMPClause *Sema::ActOnOpenMPProcBindClause(ProcBindKind Kind, 12993 SourceLocation KindKwLoc, 12994 SourceLocation StartLoc, 12995 SourceLocation LParenLoc, 12996 SourceLocation EndLoc) { 12997 if (Kind == OMP_PROC_BIND_unknown) { 12998 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 12999 << getListOfPossibleValues(OMPC_proc_bind, 13000 /*First=*/unsigned(OMP_PROC_BIND_master), 13001 /*Last=*/5) 13002 << getOpenMPClauseName(OMPC_proc_bind); 13003 return nullptr; 13004 } 13005 return new (Context) 13006 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 13007 } 13008 13009 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause( 13010 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc, 13011 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 13012 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) { 13013 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 13014 << getListOfPossibleValues( 13015 OMPC_atomic_default_mem_order, /*First=*/0, 13016 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) 13017 << getOpenMPClauseName(OMPC_atomic_default_mem_order); 13018 return nullptr; 13019 } 13020 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc, 13021 LParenLoc, EndLoc); 13022 } 13023 13024 OMPClause *Sema::ActOnOpenMPOrderClause(OpenMPOrderClauseKind Kind, 13025 SourceLocation KindKwLoc, 13026 SourceLocation StartLoc, 13027 SourceLocation LParenLoc, 13028 SourceLocation EndLoc) { 13029 if (Kind == OMPC_ORDER_unknown) { 13030 static_assert(OMPC_ORDER_unknown > 0, 13031 "OMPC_ORDER_unknown not greater than 0"); 13032 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 13033 << getListOfPossibleValues(OMPC_order, /*First=*/0, 13034 /*Last=*/OMPC_ORDER_unknown) 13035 << getOpenMPClauseName(OMPC_order); 13036 return nullptr; 13037 } 13038 return new (Context) 13039 OMPOrderClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 13040 } 13041 13042 OMPClause *Sema::ActOnOpenMPUpdateClause(OpenMPDependClauseKind Kind, 13043 SourceLocation KindKwLoc, 13044 SourceLocation StartLoc, 13045 SourceLocation LParenLoc, 13046 SourceLocation EndLoc) { 13047 if (Kind == OMPC_DEPEND_unknown || Kind == OMPC_DEPEND_source || 13048 Kind == OMPC_DEPEND_sink || Kind == OMPC_DEPEND_depobj) { 13049 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink, 13050 OMPC_DEPEND_depobj}; 13051 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 13052 << getListOfPossibleValues(OMPC_depend, /*First=*/0, 13053 /*Last=*/OMPC_DEPEND_unknown, Except) 13054 << getOpenMPClauseName(OMPC_update); 13055 return nullptr; 13056 } 13057 return OMPUpdateClause::Create(Context, StartLoc, LParenLoc, KindKwLoc, Kind, 13058 EndLoc); 13059 } 13060 13061 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 13062 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr, 13063 SourceLocation StartLoc, SourceLocation LParenLoc, 13064 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc, 13065 SourceLocation EndLoc) { 13066 OMPClause *Res = nullptr; 13067 switch (Kind) { 13068 case OMPC_schedule: 13069 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements }; 13070 assert(Argument.size() == NumberOfElements && 13071 ArgumentLoc.size() == NumberOfElements); 13072 Res = ActOnOpenMPScheduleClause( 13073 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]), 13074 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]), 13075 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr, 13076 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2], 13077 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc); 13078 break; 13079 case OMPC_if: 13080 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 13081 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()), 13082 Expr, StartLoc, LParenLoc, ArgumentLoc.back(), 13083 DelimLoc, EndLoc); 13084 break; 13085 case OMPC_dist_schedule: 13086 Res = ActOnOpenMPDistScheduleClause( 13087 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr, 13088 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc); 13089 break; 13090 case OMPC_defaultmap: 13091 enum { Modifier, DefaultmapKind }; 13092 Res = ActOnOpenMPDefaultmapClause( 13093 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]), 13094 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]), 13095 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind], 13096 EndLoc); 13097 break; 13098 case OMPC_device: 13099 assert(Argument.size() == 1 && ArgumentLoc.size() == 1); 13100 Res = ActOnOpenMPDeviceClause( 13101 static_cast<OpenMPDeviceClauseModifier>(Argument.back()), Expr, 13102 StartLoc, LParenLoc, ArgumentLoc.back(), EndLoc); 13103 break; 13104 case OMPC_final: 13105 case OMPC_num_threads: 13106 case OMPC_safelen: 13107 case OMPC_simdlen: 13108 case OMPC_allocator: 13109 case OMPC_collapse: 13110 case OMPC_default: 13111 case OMPC_proc_bind: 13112 case OMPC_private: 13113 case OMPC_firstprivate: 13114 case OMPC_lastprivate: 13115 case OMPC_shared: 13116 case OMPC_reduction: 13117 case OMPC_task_reduction: 13118 case OMPC_in_reduction: 13119 case OMPC_linear: 13120 case OMPC_aligned: 13121 case OMPC_copyin: 13122 case OMPC_copyprivate: 13123 case OMPC_ordered: 13124 case OMPC_nowait: 13125 case OMPC_untied: 13126 case OMPC_mergeable: 13127 case OMPC_threadprivate: 13128 case OMPC_allocate: 13129 case OMPC_flush: 13130 case OMPC_depobj: 13131 case OMPC_read: 13132 case OMPC_write: 13133 case OMPC_update: 13134 case OMPC_capture: 13135 case OMPC_seq_cst: 13136 case OMPC_acq_rel: 13137 case OMPC_acquire: 13138 case OMPC_release: 13139 case OMPC_relaxed: 13140 case OMPC_depend: 13141 case OMPC_threads: 13142 case OMPC_simd: 13143 case OMPC_map: 13144 case OMPC_num_teams: 13145 case OMPC_thread_limit: 13146 case OMPC_priority: 13147 case OMPC_grainsize: 13148 case OMPC_nogroup: 13149 case OMPC_num_tasks: 13150 case OMPC_hint: 13151 case OMPC_unknown: 13152 case OMPC_uniform: 13153 case OMPC_to: 13154 case OMPC_from: 13155 case OMPC_use_device_ptr: 13156 case OMPC_use_device_addr: 13157 case OMPC_is_device_ptr: 13158 case OMPC_unified_address: 13159 case OMPC_unified_shared_memory: 13160 case OMPC_reverse_offload: 13161 case OMPC_dynamic_allocators: 13162 case OMPC_atomic_default_mem_order: 13163 case OMPC_device_type: 13164 case OMPC_match: 13165 case OMPC_nontemporal: 13166 case OMPC_order: 13167 case OMPC_destroy: 13168 case OMPC_detach: 13169 case OMPC_inclusive: 13170 case OMPC_exclusive: 13171 case OMPC_uses_allocators: 13172 case OMPC_affinity: 13173 default: 13174 llvm_unreachable("Clause is not allowed."); 13175 } 13176 return Res; 13177 } 13178 13179 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1, 13180 OpenMPScheduleClauseModifier M2, 13181 SourceLocation M1Loc, SourceLocation M2Loc) { 13182 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) { 13183 SmallVector<unsigned, 2> Excluded; 13184 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown) 13185 Excluded.push_back(M2); 13186 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) 13187 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic); 13188 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic) 13189 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic); 13190 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value) 13191 << getListOfPossibleValues(OMPC_schedule, 13192 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1, 13193 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 13194 Excluded) 13195 << getOpenMPClauseName(OMPC_schedule); 13196 return true; 13197 } 13198 return false; 13199 } 13200 13201 OMPClause *Sema::ActOnOpenMPScheduleClause( 13202 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2, 13203 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 13204 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc, 13205 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) { 13206 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) || 13207 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc)) 13208 return nullptr; 13209 // OpenMP, 2.7.1, Loop Construct, Restrictions 13210 // Either the monotonic modifier or the nonmonotonic modifier can be specified 13211 // but not both. 13212 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) || 13213 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic && 13214 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) || 13215 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic && 13216 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) { 13217 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier) 13218 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2) 13219 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1); 13220 return nullptr; 13221 } 13222 if (Kind == OMPC_SCHEDULE_unknown) { 13223 std::string Values; 13224 if (M1Loc.isInvalid() && M2Loc.isInvalid()) { 13225 unsigned Exclude[] = {OMPC_SCHEDULE_unknown}; 13226 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 13227 /*Last=*/OMPC_SCHEDULE_MODIFIER_last, 13228 Exclude); 13229 } else { 13230 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0, 13231 /*Last=*/OMPC_SCHEDULE_unknown); 13232 } 13233 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 13234 << Values << getOpenMPClauseName(OMPC_schedule); 13235 return nullptr; 13236 } 13237 // OpenMP, 2.7.1, Loop Construct, Restrictions 13238 // The nonmonotonic modifier can only be specified with schedule(dynamic) or 13239 // schedule(guided). 13240 // OpenMP 5.0 does not have this restriction. 13241 if (LangOpts.OpenMP < 50 && 13242 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic || 13243 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) && 13244 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) { 13245 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc, 13246 diag::err_omp_schedule_nonmonotonic_static); 13247 return nullptr; 13248 } 13249 Expr *ValExpr = ChunkSize; 13250 Stmt *HelperValStmt = nullptr; 13251 if (ChunkSize) { 13252 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 13253 !ChunkSize->isInstantiationDependent() && 13254 !ChunkSize->containsUnexpandedParameterPack()) { 13255 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 13256 ExprResult Val = 13257 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 13258 if (Val.isInvalid()) 13259 return nullptr; 13260 13261 ValExpr = Val.get(); 13262 13263 // OpenMP [2.7.1, Restrictions] 13264 // chunk_size must be a loop invariant integer expression with a positive 13265 // value. 13266 llvm::APSInt Result; 13267 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 13268 if (Result.isSigned() && !Result.isStrictlyPositive()) { 13269 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 13270 << "schedule" << 1 << ChunkSize->getSourceRange(); 13271 return nullptr; 13272 } 13273 } else if (getOpenMPCaptureRegionForClause( 13274 DSAStack->getCurrentDirective(), OMPC_schedule, 13275 LangOpts.OpenMP) != OMPD_unknown && 13276 !CurContext->isDependentContext()) { 13277 ValExpr = MakeFullExpr(ValExpr).get(); 13278 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 13279 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 13280 HelperValStmt = buildPreInits(Context, Captures); 13281 } 13282 } 13283 } 13284 13285 return new (Context) 13286 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind, 13287 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc); 13288 } 13289 13290 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 13291 SourceLocation StartLoc, 13292 SourceLocation EndLoc) { 13293 OMPClause *Res = nullptr; 13294 switch (Kind) { 13295 case OMPC_ordered: 13296 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 13297 break; 13298 case OMPC_nowait: 13299 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 13300 break; 13301 case OMPC_untied: 13302 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc); 13303 break; 13304 case OMPC_mergeable: 13305 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc); 13306 break; 13307 case OMPC_read: 13308 Res = ActOnOpenMPReadClause(StartLoc, EndLoc); 13309 break; 13310 case OMPC_write: 13311 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc); 13312 break; 13313 case OMPC_update: 13314 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc); 13315 break; 13316 case OMPC_capture: 13317 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc); 13318 break; 13319 case OMPC_seq_cst: 13320 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc); 13321 break; 13322 case OMPC_acq_rel: 13323 Res = ActOnOpenMPAcqRelClause(StartLoc, EndLoc); 13324 break; 13325 case OMPC_acquire: 13326 Res = ActOnOpenMPAcquireClause(StartLoc, EndLoc); 13327 break; 13328 case OMPC_release: 13329 Res = ActOnOpenMPReleaseClause(StartLoc, EndLoc); 13330 break; 13331 case OMPC_relaxed: 13332 Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc); 13333 break; 13334 case OMPC_threads: 13335 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc); 13336 break; 13337 case OMPC_simd: 13338 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc); 13339 break; 13340 case OMPC_nogroup: 13341 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc); 13342 break; 13343 case OMPC_unified_address: 13344 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc); 13345 break; 13346 case OMPC_unified_shared_memory: 13347 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 13348 break; 13349 case OMPC_reverse_offload: 13350 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc); 13351 break; 13352 case OMPC_dynamic_allocators: 13353 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc); 13354 break; 13355 case OMPC_destroy: 13356 Res = ActOnOpenMPDestroyClause(StartLoc, EndLoc); 13357 break; 13358 case OMPC_if: 13359 case OMPC_final: 13360 case OMPC_num_threads: 13361 case OMPC_safelen: 13362 case OMPC_simdlen: 13363 case OMPC_allocator: 13364 case OMPC_collapse: 13365 case OMPC_schedule: 13366 case OMPC_private: 13367 case OMPC_firstprivate: 13368 case OMPC_lastprivate: 13369 case OMPC_shared: 13370 case OMPC_reduction: 13371 case OMPC_task_reduction: 13372 case OMPC_in_reduction: 13373 case OMPC_linear: 13374 case OMPC_aligned: 13375 case OMPC_copyin: 13376 case OMPC_copyprivate: 13377 case OMPC_default: 13378 case OMPC_proc_bind: 13379 case OMPC_threadprivate: 13380 case OMPC_allocate: 13381 case OMPC_flush: 13382 case OMPC_depobj: 13383 case OMPC_depend: 13384 case OMPC_device: 13385 case OMPC_map: 13386 case OMPC_num_teams: 13387 case OMPC_thread_limit: 13388 case OMPC_priority: 13389 case OMPC_grainsize: 13390 case OMPC_num_tasks: 13391 case OMPC_hint: 13392 case OMPC_dist_schedule: 13393 case OMPC_defaultmap: 13394 case OMPC_unknown: 13395 case OMPC_uniform: 13396 case OMPC_to: 13397 case OMPC_from: 13398 case OMPC_use_device_ptr: 13399 case OMPC_use_device_addr: 13400 case OMPC_is_device_ptr: 13401 case OMPC_atomic_default_mem_order: 13402 case OMPC_device_type: 13403 case OMPC_match: 13404 case OMPC_nontemporal: 13405 case OMPC_order: 13406 case OMPC_detach: 13407 case OMPC_inclusive: 13408 case OMPC_exclusive: 13409 case OMPC_uses_allocators: 13410 case OMPC_affinity: 13411 default: 13412 llvm_unreachable("Clause is not allowed."); 13413 } 13414 return Res; 13415 } 13416 13417 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 13418 SourceLocation EndLoc) { 13419 DSAStack->setNowaitRegion(); 13420 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 13421 } 13422 13423 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc, 13424 SourceLocation EndLoc) { 13425 return new (Context) OMPUntiedClause(StartLoc, EndLoc); 13426 } 13427 13428 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc, 13429 SourceLocation EndLoc) { 13430 return new (Context) OMPMergeableClause(StartLoc, EndLoc); 13431 } 13432 13433 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc, 13434 SourceLocation EndLoc) { 13435 return new (Context) OMPReadClause(StartLoc, EndLoc); 13436 } 13437 13438 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc, 13439 SourceLocation EndLoc) { 13440 return new (Context) OMPWriteClause(StartLoc, EndLoc); 13441 } 13442 13443 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc, 13444 SourceLocation EndLoc) { 13445 return OMPUpdateClause::Create(Context, StartLoc, EndLoc); 13446 } 13447 13448 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc, 13449 SourceLocation EndLoc) { 13450 return new (Context) OMPCaptureClause(StartLoc, EndLoc); 13451 } 13452 13453 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc, 13454 SourceLocation EndLoc) { 13455 return new (Context) OMPSeqCstClause(StartLoc, EndLoc); 13456 } 13457 13458 OMPClause *Sema::ActOnOpenMPAcqRelClause(SourceLocation StartLoc, 13459 SourceLocation EndLoc) { 13460 return new (Context) OMPAcqRelClause(StartLoc, EndLoc); 13461 } 13462 13463 OMPClause *Sema::ActOnOpenMPAcquireClause(SourceLocation StartLoc, 13464 SourceLocation EndLoc) { 13465 return new (Context) OMPAcquireClause(StartLoc, EndLoc); 13466 } 13467 13468 OMPClause *Sema::ActOnOpenMPReleaseClause(SourceLocation StartLoc, 13469 SourceLocation EndLoc) { 13470 return new (Context) OMPReleaseClause(StartLoc, EndLoc); 13471 } 13472 13473 OMPClause *Sema::ActOnOpenMPRelaxedClause(SourceLocation StartLoc, 13474 SourceLocation EndLoc) { 13475 return new (Context) OMPRelaxedClause(StartLoc, EndLoc); 13476 } 13477 13478 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc, 13479 SourceLocation EndLoc) { 13480 return new (Context) OMPThreadsClause(StartLoc, EndLoc); 13481 } 13482 13483 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc, 13484 SourceLocation EndLoc) { 13485 return new (Context) OMPSIMDClause(StartLoc, EndLoc); 13486 } 13487 13488 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc, 13489 SourceLocation EndLoc) { 13490 return new (Context) OMPNogroupClause(StartLoc, EndLoc); 13491 } 13492 13493 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc, 13494 SourceLocation EndLoc) { 13495 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc); 13496 } 13497 13498 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc, 13499 SourceLocation EndLoc) { 13500 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc); 13501 } 13502 13503 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc, 13504 SourceLocation EndLoc) { 13505 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc); 13506 } 13507 13508 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc, 13509 SourceLocation EndLoc) { 13510 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc); 13511 } 13512 13513 OMPClause *Sema::ActOnOpenMPDestroyClause(SourceLocation StartLoc, 13514 SourceLocation EndLoc) { 13515 return new (Context) OMPDestroyClause(StartLoc, EndLoc); 13516 } 13517 13518 OMPClause *Sema::ActOnOpenMPVarListClause( 13519 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *DepModOrTailExpr, 13520 const OMPVarListLocTy &Locs, SourceLocation ColonLoc, 13521 CXXScopeSpec &ReductionOrMapperIdScopeSpec, 13522 DeclarationNameInfo &ReductionOrMapperId, int ExtraModifier, 13523 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 13524 ArrayRef<SourceLocation> MapTypeModifiersLoc, bool IsMapTypeImplicit, 13525 SourceLocation ExtraModifierLoc) { 13526 SourceLocation StartLoc = Locs.StartLoc; 13527 SourceLocation LParenLoc = Locs.LParenLoc; 13528 SourceLocation EndLoc = Locs.EndLoc; 13529 OMPClause *Res = nullptr; 13530 switch (Kind) { 13531 case OMPC_private: 13532 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 13533 break; 13534 case OMPC_firstprivate: 13535 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 13536 break; 13537 case OMPC_lastprivate: 13538 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LASTPRIVATE_unknown && 13539 "Unexpected lastprivate modifier."); 13540 Res = ActOnOpenMPLastprivateClause( 13541 VarList, static_cast<OpenMPLastprivateModifier>(ExtraModifier), 13542 ExtraModifierLoc, ColonLoc, StartLoc, LParenLoc, EndLoc); 13543 break; 13544 case OMPC_shared: 13545 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 13546 break; 13547 case OMPC_reduction: 13548 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_REDUCTION_unknown && 13549 "Unexpected lastprivate modifier."); 13550 Res = ActOnOpenMPReductionClause( 13551 VarList, static_cast<OpenMPReductionClauseModifier>(ExtraModifier), 13552 StartLoc, LParenLoc, ExtraModifierLoc, ColonLoc, EndLoc, 13553 ReductionOrMapperIdScopeSpec, ReductionOrMapperId); 13554 break; 13555 case OMPC_task_reduction: 13556 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 13557 EndLoc, ReductionOrMapperIdScopeSpec, 13558 ReductionOrMapperId); 13559 break; 13560 case OMPC_in_reduction: 13561 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 13562 EndLoc, ReductionOrMapperIdScopeSpec, 13563 ReductionOrMapperId); 13564 break; 13565 case OMPC_linear: 13566 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LINEAR_unknown && 13567 "Unexpected linear modifier."); 13568 Res = ActOnOpenMPLinearClause( 13569 VarList, DepModOrTailExpr, StartLoc, LParenLoc, 13570 static_cast<OpenMPLinearClauseKind>(ExtraModifier), ExtraModifierLoc, 13571 ColonLoc, EndLoc); 13572 break; 13573 case OMPC_aligned: 13574 Res = ActOnOpenMPAlignedClause(VarList, DepModOrTailExpr, StartLoc, 13575 LParenLoc, ColonLoc, EndLoc); 13576 break; 13577 case OMPC_copyin: 13578 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 13579 break; 13580 case OMPC_copyprivate: 13581 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 13582 break; 13583 case OMPC_flush: 13584 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc); 13585 break; 13586 case OMPC_depend: 13587 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_DEPEND_unknown && 13588 "Unexpected depend modifier."); 13589 Res = ActOnOpenMPDependClause( 13590 DepModOrTailExpr, static_cast<OpenMPDependClauseKind>(ExtraModifier), 13591 ExtraModifierLoc, ColonLoc, VarList, StartLoc, LParenLoc, EndLoc); 13592 break; 13593 case OMPC_map: 13594 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_MAP_unknown && 13595 "Unexpected map modifier."); 13596 Res = ActOnOpenMPMapClause( 13597 MapTypeModifiers, MapTypeModifiersLoc, ReductionOrMapperIdScopeSpec, 13598 ReductionOrMapperId, static_cast<OpenMPMapClauseKind>(ExtraModifier), 13599 IsMapTypeImplicit, ExtraModifierLoc, ColonLoc, VarList, Locs); 13600 break; 13601 case OMPC_to: 13602 Res = ActOnOpenMPToClause(VarList, ReductionOrMapperIdScopeSpec, 13603 ReductionOrMapperId, Locs); 13604 break; 13605 case OMPC_from: 13606 Res = ActOnOpenMPFromClause(VarList, ReductionOrMapperIdScopeSpec, 13607 ReductionOrMapperId, Locs); 13608 break; 13609 case OMPC_use_device_ptr: 13610 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs); 13611 break; 13612 case OMPC_use_device_addr: 13613 Res = ActOnOpenMPUseDeviceAddrClause(VarList, Locs); 13614 break; 13615 case OMPC_is_device_ptr: 13616 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs); 13617 break; 13618 case OMPC_allocate: 13619 Res = ActOnOpenMPAllocateClause(DepModOrTailExpr, VarList, StartLoc, 13620 LParenLoc, ColonLoc, EndLoc); 13621 break; 13622 case OMPC_nontemporal: 13623 Res = ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, EndLoc); 13624 break; 13625 case OMPC_inclusive: 13626 Res = ActOnOpenMPInclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 13627 break; 13628 case OMPC_exclusive: 13629 Res = ActOnOpenMPExclusiveClause(VarList, StartLoc, LParenLoc, EndLoc); 13630 break; 13631 case OMPC_affinity: 13632 Res = ActOnOpenMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc, 13633 DepModOrTailExpr, VarList); 13634 break; 13635 case OMPC_if: 13636 case OMPC_depobj: 13637 case OMPC_final: 13638 case OMPC_num_threads: 13639 case OMPC_safelen: 13640 case OMPC_simdlen: 13641 case OMPC_allocator: 13642 case OMPC_collapse: 13643 case OMPC_default: 13644 case OMPC_proc_bind: 13645 case OMPC_schedule: 13646 case OMPC_ordered: 13647 case OMPC_nowait: 13648 case OMPC_untied: 13649 case OMPC_mergeable: 13650 case OMPC_threadprivate: 13651 case OMPC_read: 13652 case OMPC_write: 13653 case OMPC_update: 13654 case OMPC_capture: 13655 case OMPC_seq_cst: 13656 case OMPC_acq_rel: 13657 case OMPC_acquire: 13658 case OMPC_release: 13659 case OMPC_relaxed: 13660 case OMPC_device: 13661 case OMPC_threads: 13662 case OMPC_simd: 13663 case OMPC_num_teams: 13664 case OMPC_thread_limit: 13665 case OMPC_priority: 13666 case OMPC_grainsize: 13667 case OMPC_nogroup: 13668 case OMPC_num_tasks: 13669 case OMPC_hint: 13670 case OMPC_dist_schedule: 13671 case OMPC_defaultmap: 13672 case OMPC_unknown: 13673 case OMPC_uniform: 13674 case OMPC_unified_address: 13675 case OMPC_unified_shared_memory: 13676 case OMPC_reverse_offload: 13677 case OMPC_dynamic_allocators: 13678 case OMPC_atomic_default_mem_order: 13679 case OMPC_device_type: 13680 case OMPC_match: 13681 case OMPC_order: 13682 case OMPC_destroy: 13683 case OMPC_detach: 13684 case OMPC_uses_allocators: 13685 default: 13686 llvm_unreachable("Clause is not allowed."); 13687 } 13688 return Res; 13689 } 13690 13691 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK, 13692 ExprObjectKind OK, SourceLocation Loc) { 13693 ExprResult Res = BuildDeclRefExpr( 13694 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc); 13695 if (!Res.isUsable()) 13696 return ExprError(); 13697 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) { 13698 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get()); 13699 if (!Res.isUsable()) 13700 return ExprError(); 13701 } 13702 if (VK != VK_LValue && Res.get()->isGLValue()) { 13703 Res = DefaultLvalueConversion(Res.get()); 13704 if (!Res.isUsable()) 13705 return ExprError(); 13706 } 13707 return Res; 13708 } 13709 13710 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 13711 SourceLocation StartLoc, 13712 SourceLocation LParenLoc, 13713 SourceLocation EndLoc) { 13714 SmallVector<Expr *, 8> Vars; 13715 SmallVector<Expr *, 8> PrivateCopies; 13716 for (Expr *RefExpr : VarList) { 13717 assert(RefExpr && "NULL expr in OpenMP private clause."); 13718 SourceLocation ELoc; 13719 SourceRange ERange; 13720 Expr *SimpleRefExpr = RefExpr; 13721 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 13722 if (Res.second) { 13723 // It will be analyzed later. 13724 Vars.push_back(RefExpr); 13725 PrivateCopies.push_back(nullptr); 13726 } 13727 ValueDecl *D = Res.first; 13728 if (!D) 13729 continue; 13730 13731 QualType Type = D->getType(); 13732 auto *VD = dyn_cast<VarDecl>(D); 13733 13734 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 13735 // A variable that appears in a private clause must not have an incomplete 13736 // type or a reference type. 13737 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type)) 13738 continue; 13739 Type = Type.getNonReferenceType(); 13740 13741 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 13742 // A variable that is privatized must not have a const-qualified type 13743 // unless it is of class type with a mutable member. This restriction does 13744 // not apply to the firstprivate clause. 13745 // 13746 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions] 13747 // A variable that appears in a private clause must not have a 13748 // const-qualified type unless it is of class type with a mutable member. 13749 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc)) 13750 continue; 13751 13752 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 13753 // in a Construct] 13754 // Variables with the predetermined data-sharing attributes may not be 13755 // listed in data-sharing attributes clauses, except for the cases 13756 // listed below. For these exceptions only, listing a predetermined 13757 // variable in a data-sharing attribute clause is allowed and overrides 13758 // the variable's predetermined data-sharing attributes. 13759 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 13760 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 13761 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 13762 << getOpenMPClauseName(OMPC_private); 13763 reportOriginalDsa(*this, DSAStack, D, DVar); 13764 continue; 13765 } 13766 13767 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 13768 // Variably modified types are not supported for tasks. 13769 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 13770 isOpenMPTaskingDirective(CurrDir)) { 13771 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 13772 << getOpenMPClauseName(OMPC_private) << Type 13773 << getOpenMPDirectiveName(CurrDir); 13774 bool IsDecl = 13775 !VD || 13776 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 13777 Diag(D->getLocation(), 13778 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 13779 << D; 13780 continue; 13781 } 13782 13783 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 13784 // A list item cannot appear in both a map clause and a data-sharing 13785 // attribute clause on the same construct 13786 // 13787 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 13788 // A list item cannot appear in both a map clause and a data-sharing 13789 // attribute clause on the same construct unless the construct is a 13790 // combined construct. 13791 if ((LangOpts.OpenMP <= 45 && isOpenMPTargetExecutionDirective(CurrDir)) || 13792 CurrDir == OMPD_target) { 13793 OpenMPClauseKind ConflictKind; 13794 if (DSAStack->checkMappableExprComponentListsForDecl( 13795 VD, /*CurrentRegionOnly=*/true, 13796 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef, 13797 OpenMPClauseKind WhereFoundClauseKind) -> bool { 13798 ConflictKind = WhereFoundClauseKind; 13799 return true; 13800 })) { 13801 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 13802 << getOpenMPClauseName(OMPC_private) 13803 << getOpenMPClauseName(ConflictKind) 13804 << getOpenMPDirectiveName(CurrDir); 13805 reportOriginalDsa(*this, DSAStack, D, DVar); 13806 continue; 13807 } 13808 } 13809 13810 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 13811 // A variable of class type (or array thereof) that appears in a private 13812 // clause requires an accessible, unambiguous default constructor for the 13813 // class type. 13814 // Generate helper private variable and initialize it with the default 13815 // value. The address of the original variable is replaced by the address of 13816 // the new private variable in CodeGen. This new variable is not added to 13817 // IdResolver, so the code in the OpenMP region uses original variable for 13818 // proper diagnostics. 13819 Type = Type.getUnqualifiedType(); 13820 VarDecl *VDPrivate = 13821 buildVarDecl(*this, ELoc, Type, D->getName(), 13822 D->hasAttrs() ? &D->getAttrs() : nullptr, 13823 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 13824 ActOnUninitializedDecl(VDPrivate); 13825 if (VDPrivate->isInvalidDecl()) 13826 continue; 13827 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 13828 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 13829 13830 DeclRefExpr *Ref = nullptr; 13831 if (!VD && !CurContext->isDependentContext()) 13832 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 13833 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref); 13834 Vars.push_back((VD || CurContext->isDependentContext()) 13835 ? RefExpr->IgnoreParens() 13836 : Ref); 13837 PrivateCopies.push_back(VDPrivateRefExpr); 13838 } 13839 13840 if (Vars.empty()) 13841 return nullptr; 13842 13843 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 13844 PrivateCopies); 13845 } 13846 13847 namespace { 13848 class DiagsUninitializedSeveretyRAII { 13849 private: 13850 DiagnosticsEngine &Diags; 13851 SourceLocation SavedLoc; 13852 bool IsIgnored = false; 13853 13854 public: 13855 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc, 13856 bool IsIgnored) 13857 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) { 13858 if (!IsIgnored) { 13859 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init, 13860 /*Map*/ diag::Severity::Ignored, Loc); 13861 } 13862 } 13863 ~DiagsUninitializedSeveretyRAII() { 13864 if (!IsIgnored) 13865 Diags.popMappings(SavedLoc); 13866 } 13867 }; 13868 } 13869 13870 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 13871 SourceLocation StartLoc, 13872 SourceLocation LParenLoc, 13873 SourceLocation EndLoc) { 13874 SmallVector<Expr *, 8> Vars; 13875 SmallVector<Expr *, 8> PrivateCopies; 13876 SmallVector<Expr *, 8> Inits; 13877 SmallVector<Decl *, 4> ExprCaptures; 13878 bool IsImplicitClause = 13879 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid(); 13880 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc(); 13881 13882 for (Expr *RefExpr : VarList) { 13883 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 13884 SourceLocation ELoc; 13885 SourceRange ERange; 13886 Expr *SimpleRefExpr = RefExpr; 13887 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 13888 if (Res.second) { 13889 // It will be analyzed later. 13890 Vars.push_back(RefExpr); 13891 PrivateCopies.push_back(nullptr); 13892 Inits.push_back(nullptr); 13893 } 13894 ValueDecl *D = Res.first; 13895 if (!D) 13896 continue; 13897 13898 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc; 13899 QualType Type = D->getType(); 13900 auto *VD = dyn_cast<VarDecl>(D); 13901 13902 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 13903 // A variable that appears in a private clause must not have an incomplete 13904 // type or a reference type. 13905 if (RequireCompleteType(ELoc, Type, 13906 diag::err_omp_firstprivate_incomplete_type)) 13907 continue; 13908 Type = Type.getNonReferenceType(); 13909 13910 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 13911 // A variable of class type (or array thereof) that appears in a private 13912 // clause requires an accessible, unambiguous copy constructor for the 13913 // class type. 13914 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 13915 13916 // If an implicit firstprivate variable found it was checked already. 13917 DSAStackTy::DSAVarData TopDVar; 13918 if (!IsImplicitClause) { 13919 DSAStackTy::DSAVarData DVar = 13920 DSAStack->getTopDSA(D, /*FromParent=*/false); 13921 TopDVar = DVar; 13922 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 13923 bool IsConstant = ElemType.isConstant(Context); 13924 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 13925 // A list item that specifies a given variable may not appear in more 13926 // than one clause on the same directive, except that a variable may be 13927 // specified in both firstprivate and lastprivate clauses. 13928 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 13929 // A list item may appear in a firstprivate or lastprivate clause but not 13930 // both. 13931 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 13932 (isOpenMPDistributeDirective(CurrDir) || 13933 DVar.CKind != OMPC_lastprivate) && 13934 DVar.RefExpr) { 13935 Diag(ELoc, diag::err_omp_wrong_dsa) 13936 << getOpenMPClauseName(DVar.CKind) 13937 << getOpenMPClauseName(OMPC_firstprivate); 13938 reportOriginalDsa(*this, DSAStack, D, DVar); 13939 continue; 13940 } 13941 13942 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 13943 // in a Construct] 13944 // Variables with the predetermined data-sharing attributes may not be 13945 // listed in data-sharing attributes clauses, except for the cases 13946 // listed below. For these exceptions only, listing a predetermined 13947 // variable in a data-sharing attribute clause is allowed and overrides 13948 // the variable's predetermined data-sharing attributes. 13949 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 13950 // in a Construct, C/C++, p.2] 13951 // Variables with const-qualified type having no mutable member may be 13952 // listed in a firstprivate clause, even if they are static data members. 13953 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr && 13954 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 13955 Diag(ELoc, diag::err_omp_wrong_dsa) 13956 << getOpenMPClauseName(DVar.CKind) 13957 << getOpenMPClauseName(OMPC_firstprivate); 13958 reportOriginalDsa(*this, DSAStack, D, DVar); 13959 continue; 13960 } 13961 13962 // OpenMP [2.9.3.4, Restrictions, p.2] 13963 // A list item that is private within a parallel region must not appear 13964 // in a firstprivate clause on a worksharing construct if any of the 13965 // worksharing regions arising from the worksharing construct ever bind 13966 // to any of the parallel regions arising from the parallel construct. 13967 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 13968 // A list item that is private within a teams region must not appear in a 13969 // firstprivate clause on a distribute construct if any of the distribute 13970 // regions arising from the distribute construct ever bind to any of the 13971 // teams regions arising from the teams construct. 13972 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3] 13973 // A list item that appears in a reduction clause of a teams construct 13974 // must not appear in a firstprivate clause on a distribute construct if 13975 // any of the distribute regions arising from the distribute construct 13976 // ever bind to any of the teams regions arising from the teams construct. 13977 if ((isOpenMPWorksharingDirective(CurrDir) || 13978 isOpenMPDistributeDirective(CurrDir)) && 13979 !isOpenMPParallelDirective(CurrDir) && 13980 !isOpenMPTeamsDirective(CurrDir)) { 13981 DVar = DSAStack->getImplicitDSA(D, true); 13982 if (DVar.CKind != OMPC_shared && 13983 (isOpenMPParallelDirective(DVar.DKind) || 13984 isOpenMPTeamsDirective(DVar.DKind) || 13985 DVar.DKind == OMPD_unknown)) { 13986 Diag(ELoc, diag::err_omp_required_access) 13987 << getOpenMPClauseName(OMPC_firstprivate) 13988 << getOpenMPClauseName(OMPC_shared); 13989 reportOriginalDsa(*this, DSAStack, D, DVar); 13990 continue; 13991 } 13992 } 13993 // OpenMP [2.9.3.4, Restrictions, p.3] 13994 // A list item that appears in a reduction clause of a parallel construct 13995 // must not appear in a firstprivate clause on a worksharing or task 13996 // construct if any of the worksharing or task regions arising from the 13997 // worksharing or task construct ever bind to any of the parallel regions 13998 // arising from the parallel construct. 13999 // OpenMP [2.9.3.4, Restrictions, p.4] 14000 // A list item that appears in a reduction clause in worksharing 14001 // construct must not appear in a firstprivate clause in a task construct 14002 // encountered during execution of any of the worksharing regions arising 14003 // from the worksharing construct. 14004 if (isOpenMPTaskingDirective(CurrDir)) { 14005 DVar = DSAStack->hasInnermostDSA( 14006 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; }, 14007 [](OpenMPDirectiveKind K) { 14008 return isOpenMPParallelDirective(K) || 14009 isOpenMPWorksharingDirective(K) || 14010 isOpenMPTeamsDirective(K); 14011 }, 14012 /*FromParent=*/true); 14013 if (DVar.CKind == OMPC_reduction && 14014 (isOpenMPParallelDirective(DVar.DKind) || 14015 isOpenMPWorksharingDirective(DVar.DKind) || 14016 isOpenMPTeamsDirective(DVar.DKind))) { 14017 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate) 14018 << getOpenMPDirectiveName(DVar.DKind); 14019 reportOriginalDsa(*this, DSAStack, D, DVar); 14020 continue; 14021 } 14022 } 14023 14024 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 14025 // A list item cannot appear in both a map clause and a data-sharing 14026 // attribute clause on the same construct 14027 // 14028 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 14029 // A list item cannot appear in both a map clause and a data-sharing 14030 // attribute clause on the same construct unless the construct is a 14031 // combined construct. 14032 if ((LangOpts.OpenMP <= 45 && 14033 isOpenMPTargetExecutionDirective(CurrDir)) || 14034 CurrDir == OMPD_target) { 14035 OpenMPClauseKind ConflictKind; 14036 if (DSAStack->checkMappableExprComponentListsForDecl( 14037 VD, /*CurrentRegionOnly=*/true, 14038 [&ConflictKind]( 14039 OMPClauseMappableExprCommon::MappableExprComponentListRef, 14040 OpenMPClauseKind WhereFoundClauseKind) { 14041 ConflictKind = WhereFoundClauseKind; 14042 return true; 14043 })) { 14044 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 14045 << getOpenMPClauseName(OMPC_firstprivate) 14046 << getOpenMPClauseName(ConflictKind) 14047 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 14048 reportOriginalDsa(*this, DSAStack, D, DVar); 14049 continue; 14050 } 14051 } 14052 } 14053 14054 // Variably modified types are not supported for tasks. 14055 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() && 14056 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) { 14057 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 14058 << getOpenMPClauseName(OMPC_firstprivate) << Type 14059 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 14060 bool IsDecl = 14061 !VD || 14062 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 14063 Diag(D->getLocation(), 14064 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 14065 << D; 14066 continue; 14067 } 14068 14069 Type = Type.getUnqualifiedType(); 14070 VarDecl *VDPrivate = 14071 buildVarDecl(*this, ELoc, Type, D->getName(), 14072 D->hasAttrs() ? &D->getAttrs() : nullptr, 14073 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 14074 // Generate helper private variable and initialize it with the value of the 14075 // original variable. The address of the original variable is replaced by 14076 // the address of the new private variable in the CodeGen. This new variable 14077 // is not added to IdResolver, so the code in the OpenMP region uses 14078 // original variable for proper diagnostics and variable capturing. 14079 Expr *VDInitRefExpr = nullptr; 14080 // For arrays generate initializer for single element and replace it by the 14081 // original array element in CodeGen. 14082 if (Type->isArrayType()) { 14083 VarDecl *VDInit = 14084 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName()); 14085 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc); 14086 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get(); 14087 ElemType = ElemType.getUnqualifiedType(); 14088 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, 14089 ".firstprivate.temp"); 14090 InitializedEntity Entity = 14091 InitializedEntity::InitializeVariable(VDInitTemp); 14092 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc); 14093 14094 InitializationSequence InitSeq(*this, Entity, Kind, Init); 14095 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init); 14096 if (Result.isInvalid()) 14097 VDPrivate->setInvalidDecl(); 14098 else 14099 VDPrivate->setInit(Result.getAs<Expr>()); 14100 // Remove temp variable declaration. 14101 Context.Deallocate(VDInitTemp); 14102 } else { 14103 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type, 14104 ".firstprivate.temp"); 14105 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(), 14106 RefExpr->getExprLoc()); 14107 AddInitializerToDecl(VDPrivate, 14108 DefaultLvalueConversion(VDInitRefExpr).get(), 14109 /*DirectInit=*/false); 14110 } 14111 if (VDPrivate->isInvalidDecl()) { 14112 if (IsImplicitClause) { 14113 Diag(RefExpr->getExprLoc(), 14114 diag::note_omp_task_predetermined_firstprivate_here); 14115 } 14116 continue; 14117 } 14118 CurContext->addDecl(VDPrivate); 14119 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 14120 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), 14121 RefExpr->getExprLoc()); 14122 DeclRefExpr *Ref = nullptr; 14123 if (!VD && !CurContext->isDependentContext()) { 14124 if (TopDVar.CKind == OMPC_lastprivate) { 14125 Ref = TopDVar.PrivateCopy; 14126 } else { 14127 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 14128 if (!isOpenMPCapturedDecl(D)) 14129 ExprCaptures.push_back(Ref->getDecl()); 14130 } 14131 } 14132 if (!IsImplicitClause) 14133 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 14134 Vars.push_back((VD || CurContext->isDependentContext()) 14135 ? RefExpr->IgnoreParens() 14136 : Ref); 14137 PrivateCopies.push_back(VDPrivateRefExpr); 14138 Inits.push_back(VDInitRefExpr); 14139 } 14140 14141 if (Vars.empty()) 14142 return nullptr; 14143 14144 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 14145 Vars, PrivateCopies, Inits, 14146 buildPreInits(Context, ExprCaptures)); 14147 } 14148 14149 OMPClause *Sema::ActOnOpenMPLastprivateClause( 14150 ArrayRef<Expr *> VarList, OpenMPLastprivateModifier LPKind, 14151 SourceLocation LPKindLoc, SourceLocation ColonLoc, SourceLocation StartLoc, 14152 SourceLocation LParenLoc, SourceLocation EndLoc) { 14153 if (LPKind == OMPC_LASTPRIVATE_unknown && LPKindLoc.isValid()) { 14154 assert(ColonLoc.isValid() && "Colon location must be valid."); 14155 Diag(LPKindLoc, diag::err_omp_unexpected_clause_value) 14156 << getListOfPossibleValues(OMPC_lastprivate, /*First=*/0, 14157 /*Last=*/OMPC_LASTPRIVATE_unknown) 14158 << getOpenMPClauseName(OMPC_lastprivate); 14159 return nullptr; 14160 } 14161 14162 SmallVector<Expr *, 8> Vars; 14163 SmallVector<Expr *, 8> SrcExprs; 14164 SmallVector<Expr *, 8> DstExprs; 14165 SmallVector<Expr *, 8> AssignmentOps; 14166 SmallVector<Decl *, 4> ExprCaptures; 14167 SmallVector<Expr *, 4> ExprPostUpdates; 14168 for (Expr *RefExpr : VarList) { 14169 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 14170 SourceLocation ELoc; 14171 SourceRange ERange; 14172 Expr *SimpleRefExpr = RefExpr; 14173 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14174 if (Res.second) { 14175 // It will be analyzed later. 14176 Vars.push_back(RefExpr); 14177 SrcExprs.push_back(nullptr); 14178 DstExprs.push_back(nullptr); 14179 AssignmentOps.push_back(nullptr); 14180 } 14181 ValueDecl *D = Res.first; 14182 if (!D) 14183 continue; 14184 14185 QualType Type = D->getType(); 14186 auto *VD = dyn_cast<VarDecl>(D); 14187 14188 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 14189 // A variable that appears in a lastprivate clause must not have an 14190 // incomplete type or a reference type. 14191 if (RequireCompleteType(ELoc, Type, 14192 diag::err_omp_lastprivate_incomplete_type)) 14193 continue; 14194 Type = Type.getNonReferenceType(); 14195 14196 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 14197 // A variable that is privatized must not have a const-qualified type 14198 // unless it is of class type with a mutable member. This restriction does 14199 // not apply to the firstprivate clause. 14200 // 14201 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions] 14202 // A variable that appears in a lastprivate clause must not have a 14203 // const-qualified type unless it is of class type with a mutable member. 14204 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc)) 14205 continue; 14206 14207 // OpenMP 5.0 [2.19.4.5 lastprivate Clause, Restrictions] 14208 // A list item that appears in a lastprivate clause with the conditional 14209 // modifier must be a scalar variable. 14210 if (LPKind == OMPC_LASTPRIVATE_conditional && !Type->isScalarType()) { 14211 Diag(ELoc, diag::err_omp_lastprivate_conditional_non_scalar); 14212 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 14213 VarDecl::DeclarationOnly; 14214 Diag(D->getLocation(), 14215 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 14216 << D; 14217 continue; 14218 } 14219 14220 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 14221 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 14222 // in a Construct] 14223 // Variables with the predetermined data-sharing attributes may not be 14224 // listed in data-sharing attributes clauses, except for the cases 14225 // listed below. 14226 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3] 14227 // A list item may appear in a firstprivate or lastprivate clause but not 14228 // both. 14229 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 14230 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 14231 (isOpenMPDistributeDirective(CurrDir) || 14232 DVar.CKind != OMPC_firstprivate) && 14233 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 14234 Diag(ELoc, diag::err_omp_wrong_dsa) 14235 << getOpenMPClauseName(DVar.CKind) 14236 << getOpenMPClauseName(OMPC_lastprivate); 14237 reportOriginalDsa(*this, DSAStack, D, DVar); 14238 continue; 14239 } 14240 14241 // OpenMP [2.14.3.5, Restrictions, p.2] 14242 // A list item that is private within a parallel region, or that appears in 14243 // the reduction clause of a parallel construct, must not appear in a 14244 // lastprivate clause on a worksharing construct if any of the corresponding 14245 // worksharing regions ever binds to any of the corresponding parallel 14246 // regions. 14247 DSAStackTy::DSAVarData TopDVar = DVar; 14248 if (isOpenMPWorksharingDirective(CurrDir) && 14249 !isOpenMPParallelDirective(CurrDir) && 14250 !isOpenMPTeamsDirective(CurrDir)) { 14251 DVar = DSAStack->getImplicitDSA(D, true); 14252 if (DVar.CKind != OMPC_shared) { 14253 Diag(ELoc, diag::err_omp_required_access) 14254 << getOpenMPClauseName(OMPC_lastprivate) 14255 << getOpenMPClauseName(OMPC_shared); 14256 reportOriginalDsa(*this, DSAStack, D, DVar); 14257 continue; 14258 } 14259 } 14260 14261 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 14262 // A variable of class type (or array thereof) that appears in a 14263 // lastprivate clause requires an accessible, unambiguous default 14264 // constructor for the class type, unless the list item is also specified 14265 // in a firstprivate clause. 14266 // A variable of class type (or array thereof) that appears in a 14267 // lastprivate clause requires an accessible, unambiguous copy assignment 14268 // operator for the class type. 14269 Type = Context.getBaseElementType(Type).getNonReferenceType(); 14270 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(), 14271 Type.getUnqualifiedType(), ".lastprivate.src", 14272 D->hasAttrs() ? &D->getAttrs() : nullptr); 14273 DeclRefExpr *PseudoSrcExpr = 14274 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc); 14275 VarDecl *DstVD = 14276 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst", 14277 D->hasAttrs() ? &D->getAttrs() : nullptr); 14278 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 14279 // For arrays generate assignment operation for single element and replace 14280 // it by the original array element in CodeGen. 14281 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign, 14282 PseudoDstExpr, PseudoSrcExpr); 14283 if (AssignmentOp.isInvalid()) 14284 continue; 14285 AssignmentOp = 14286 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 14287 if (AssignmentOp.isInvalid()) 14288 continue; 14289 14290 DeclRefExpr *Ref = nullptr; 14291 if (!VD && !CurContext->isDependentContext()) { 14292 if (TopDVar.CKind == OMPC_firstprivate) { 14293 Ref = TopDVar.PrivateCopy; 14294 } else { 14295 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 14296 if (!isOpenMPCapturedDecl(D)) 14297 ExprCaptures.push_back(Ref->getDecl()); 14298 } 14299 if (TopDVar.CKind == OMPC_firstprivate || 14300 (!isOpenMPCapturedDecl(D) && 14301 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) { 14302 ExprResult RefRes = DefaultLvalueConversion(Ref); 14303 if (!RefRes.isUsable()) 14304 continue; 14305 ExprResult PostUpdateRes = 14306 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 14307 RefRes.get()); 14308 if (!PostUpdateRes.isUsable()) 14309 continue; 14310 ExprPostUpdates.push_back( 14311 IgnoredValueConversions(PostUpdateRes.get()).get()); 14312 } 14313 } 14314 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref); 14315 Vars.push_back((VD || CurContext->isDependentContext()) 14316 ? RefExpr->IgnoreParens() 14317 : Ref); 14318 SrcExprs.push_back(PseudoSrcExpr); 14319 DstExprs.push_back(PseudoDstExpr); 14320 AssignmentOps.push_back(AssignmentOp.get()); 14321 } 14322 14323 if (Vars.empty()) 14324 return nullptr; 14325 14326 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 14327 Vars, SrcExprs, DstExprs, AssignmentOps, 14328 LPKind, LPKindLoc, ColonLoc, 14329 buildPreInits(Context, ExprCaptures), 14330 buildPostUpdate(*this, ExprPostUpdates)); 14331 } 14332 14333 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 14334 SourceLocation StartLoc, 14335 SourceLocation LParenLoc, 14336 SourceLocation EndLoc) { 14337 SmallVector<Expr *, 8> Vars; 14338 for (Expr *RefExpr : VarList) { 14339 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 14340 SourceLocation ELoc; 14341 SourceRange ERange; 14342 Expr *SimpleRefExpr = RefExpr; 14343 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 14344 if (Res.second) { 14345 // It will be analyzed later. 14346 Vars.push_back(RefExpr); 14347 } 14348 ValueDecl *D = Res.first; 14349 if (!D) 14350 continue; 14351 14352 auto *VD = dyn_cast<VarDecl>(D); 14353 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 14354 // in a Construct] 14355 // Variables with the predetermined data-sharing attributes may not be 14356 // listed in data-sharing attributes clauses, except for the cases 14357 // listed below. For these exceptions only, listing a predetermined 14358 // variable in a data-sharing attribute clause is allowed and overrides 14359 // the variable's predetermined data-sharing attributes. 14360 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 14361 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 14362 DVar.RefExpr) { 14363 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 14364 << getOpenMPClauseName(OMPC_shared); 14365 reportOriginalDsa(*this, DSAStack, D, DVar); 14366 continue; 14367 } 14368 14369 DeclRefExpr *Ref = nullptr; 14370 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext()) 14371 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 14372 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref); 14373 Vars.push_back((VD || !Ref || CurContext->isDependentContext()) 14374 ? RefExpr->IgnoreParens() 14375 : Ref); 14376 } 14377 14378 if (Vars.empty()) 14379 return nullptr; 14380 14381 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 14382 } 14383 14384 namespace { 14385 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 14386 DSAStackTy *Stack; 14387 14388 public: 14389 bool VisitDeclRefExpr(DeclRefExpr *E) { 14390 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) { 14391 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false); 14392 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 14393 return false; 14394 if (DVar.CKind != OMPC_unknown) 14395 return true; 14396 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA( 14397 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; }, 14398 /*FromParent=*/true); 14399 return DVarPrivate.CKind != OMPC_unknown; 14400 } 14401 return false; 14402 } 14403 bool VisitStmt(Stmt *S) { 14404 for (Stmt *Child : S->children()) { 14405 if (Child && Visit(Child)) 14406 return true; 14407 } 14408 return false; 14409 } 14410 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 14411 }; 14412 } // namespace 14413 14414 namespace { 14415 // Transform MemberExpression for specified FieldDecl of current class to 14416 // DeclRefExpr to specified OMPCapturedExprDecl. 14417 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> { 14418 typedef TreeTransform<TransformExprToCaptures> BaseTransform; 14419 ValueDecl *Field = nullptr; 14420 DeclRefExpr *CapturedExpr = nullptr; 14421 14422 public: 14423 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl) 14424 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {} 14425 14426 ExprResult TransformMemberExpr(MemberExpr *E) { 14427 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) && 14428 E->getMemberDecl() == Field) { 14429 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false); 14430 return CapturedExpr; 14431 } 14432 return BaseTransform::TransformMemberExpr(E); 14433 } 14434 DeclRefExpr *getCapturedExpr() { return CapturedExpr; } 14435 }; 14436 } // namespace 14437 14438 template <typename T, typename U> 14439 static T filterLookupForUDReductionAndMapper( 14440 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) { 14441 for (U &Set : Lookups) { 14442 for (auto *D : Set) { 14443 if (T Res = Gen(cast<ValueDecl>(D))) 14444 return Res; 14445 } 14446 } 14447 return T(); 14448 } 14449 14450 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) { 14451 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case"); 14452 14453 for (auto RD : D->redecls()) { 14454 // Don't bother with extra checks if we already know this one isn't visible. 14455 if (RD == D) 14456 continue; 14457 14458 auto ND = cast<NamedDecl>(RD); 14459 if (LookupResult::isVisible(SemaRef, ND)) 14460 return ND; 14461 } 14462 14463 return nullptr; 14464 } 14465 14466 static void 14467 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id, 14468 SourceLocation Loc, QualType Ty, 14469 SmallVectorImpl<UnresolvedSet<8>> &Lookups) { 14470 // Find all of the associated namespaces and classes based on the 14471 // arguments we have. 14472 Sema::AssociatedNamespaceSet AssociatedNamespaces; 14473 Sema::AssociatedClassSet AssociatedClasses; 14474 OpaqueValueExpr OVE(Loc, Ty, VK_LValue); 14475 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces, 14476 AssociatedClasses); 14477 14478 // C++ [basic.lookup.argdep]p3: 14479 // Let X be the lookup set produced by unqualified lookup (3.4.1) 14480 // and let Y be the lookup set produced by argument dependent 14481 // lookup (defined as follows). If X contains [...] then Y is 14482 // empty. Otherwise Y is the set of declarations found in the 14483 // namespaces associated with the argument types as described 14484 // below. The set of declarations found by the lookup of the name 14485 // is the union of X and Y. 14486 // 14487 // Here, we compute Y and add its members to the overloaded 14488 // candidate set. 14489 for (auto *NS : AssociatedNamespaces) { 14490 // When considering an associated namespace, the lookup is the 14491 // same as the lookup performed when the associated namespace is 14492 // used as a qualifier (3.4.3.2) except that: 14493 // 14494 // -- Any using-directives in the associated namespace are 14495 // ignored. 14496 // 14497 // -- Any namespace-scope friend functions declared in 14498 // associated classes are visible within their respective 14499 // namespaces even if they are not visible during an ordinary 14500 // lookup (11.4). 14501 DeclContext::lookup_result R = NS->lookup(Id.getName()); 14502 for (auto *D : R) { 14503 auto *Underlying = D; 14504 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 14505 Underlying = USD->getTargetDecl(); 14506 14507 if (!isa<OMPDeclareReductionDecl>(Underlying) && 14508 !isa<OMPDeclareMapperDecl>(Underlying)) 14509 continue; 14510 14511 if (!SemaRef.isVisible(D)) { 14512 D = findAcceptableDecl(SemaRef, D); 14513 if (!D) 14514 continue; 14515 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) 14516 Underlying = USD->getTargetDecl(); 14517 } 14518 Lookups.emplace_back(); 14519 Lookups.back().addDecl(Underlying); 14520 } 14521 } 14522 } 14523 14524 static ExprResult 14525 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range, 14526 Scope *S, CXXScopeSpec &ReductionIdScopeSpec, 14527 const DeclarationNameInfo &ReductionId, QualType Ty, 14528 CXXCastPath &BasePath, Expr *UnresolvedReduction) { 14529 if (ReductionIdScopeSpec.isInvalid()) 14530 return ExprError(); 14531 SmallVector<UnresolvedSet<8>, 4> Lookups; 14532 if (S) { 14533 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 14534 Lookup.suppressDiagnostics(); 14535 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) { 14536 NamedDecl *D = Lookup.getRepresentativeDecl(); 14537 do { 14538 S = S->getParent(); 14539 } while (S && !S->isDeclScope(D)); 14540 if (S) 14541 S = S->getParent(); 14542 Lookups.emplace_back(); 14543 Lookups.back().append(Lookup.begin(), Lookup.end()); 14544 Lookup.clear(); 14545 } 14546 } else if (auto *ULE = 14547 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) { 14548 Lookups.push_back(UnresolvedSet<8>()); 14549 Decl *PrevD = nullptr; 14550 for (NamedDecl *D : ULE->decls()) { 14551 if (D == PrevD) 14552 Lookups.push_back(UnresolvedSet<8>()); 14553 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D)) 14554 Lookups.back().addDecl(DRD); 14555 PrevD = D; 14556 } 14557 } 14558 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() || 14559 Ty->isInstantiationDependentType() || 14560 Ty->containsUnexpandedParameterPack() || 14561 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 14562 return !D->isInvalidDecl() && 14563 (D->getType()->isDependentType() || 14564 D->getType()->isInstantiationDependentType() || 14565 D->getType()->containsUnexpandedParameterPack()); 14566 })) { 14567 UnresolvedSet<8> ResSet; 14568 for (const UnresolvedSet<8> &Set : Lookups) { 14569 if (Set.empty()) 14570 continue; 14571 ResSet.append(Set.begin(), Set.end()); 14572 // The last item marks the end of all declarations at the specified scope. 14573 ResSet.addDecl(Set[Set.size() - 1]); 14574 } 14575 return UnresolvedLookupExpr::Create( 14576 SemaRef.Context, /*NamingClass=*/nullptr, 14577 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId, 14578 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end()); 14579 } 14580 // Lookup inside the classes. 14581 // C++ [over.match.oper]p3: 14582 // For a unary operator @ with an operand of a type whose 14583 // cv-unqualified version is T1, and for a binary operator @ with 14584 // a left operand of a type whose cv-unqualified version is T1 and 14585 // a right operand of a type whose cv-unqualified version is T2, 14586 // three sets of candidate functions, designated member 14587 // candidates, non-member candidates and built-in candidates, are 14588 // constructed as follows: 14589 // -- If T1 is a complete class type or a class currently being 14590 // defined, the set of member candidates is the result of the 14591 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise, 14592 // the set of member candidates is empty. 14593 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName); 14594 Lookup.suppressDiagnostics(); 14595 if (const auto *TyRec = Ty->getAs<RecordType>()) { 14596 // Complete the type if it can be completed. 14597 // If the type is neither complete nor being defined, bail out now. 14598 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() || 14599 TyRec->getDecl()->getDefinition()) { 14600 Lookup.clear(); 14601 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl()); 14602 if (Lookup.empty()) { 14603 Lookups.emplace_back(); 14604 Lookups.back().append(Lookup.begin(), Lookup.end()); 14605 } 14606 } 14607 } 14608 // Perform ADL. 14609 if (SemaRef.getLangOpts().CPlusPlus) 14610 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups); 14611 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 14612 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * { 14613 if (!D->isInvalidDecl() && 14614 SemaRef.Context.hasSameType(D->getType(), Ty)) 14615 return D; 14616 return nullptr; 14617 })) 14618 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), 14619 VK_LValue, Loc); 14620 if (SemaRef.getLangOpts().CPlusPlus) { 14621 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 14622 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * { 14623 if (!D->isInvalidDecl() && 14624 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) && 14625 !Ty.isMoreQualifiedThan(D->getType())) 14626 return D; 14627 return nullptr; 14628 })) { 14629 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 14630 /*DetectVirtual=*/false); 14631 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) { 14632 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 14633 VD->getType().getUnqualifiedType()))) { 14634 if (SemaRef.CheckBaseClassAccess( 14635 Loc, VD->getType(), Ty, Paths.front(), 14636 /*DiagID=*/0) != Sema::AR_inaccessible) { 14637 SemaRef.BuildBasePathArray(Paths, BasePath); 14638 return SemaRef.BuildDeclRefExpr( 14639 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc); 14640 } 14641 } 14642 } 14643 } 14644 } 14645 if (ReductionIdScopeSpec.isSet()) { 14646 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) 14647 << Ty << Range; 14648 return ExprError(); 14649 } 14650 return ExprEmpty(); 14651 } 14652 14653 namespace { 14654 /// Data for the reduction-based clauses. 14655 struct ReductionData { 14656 /// List of original reduction items. 14657 SmallVector<Expr *, 8> Vars; 14658 /// List of private copies of the reduction items. 14659 SmallVector<Expr *, 8> Privates; 14660 /// LHS expressions for the reduction_op expressions. 14661 SmallVector<Expr *, 8> LHSs; 14662 /// RHS expressions for the reduction_op expressions. 14663 SmallVector<Expr *, 8> RHSs; 14664 /// Reduction operation expression. 14665 SmallVector<Expr *, 8> ReductionOps; 14666 /// inscan copy operation expressions. 14667 SmallVector<Expr *, 8> InscanCopyOps; 14668 /// inscan copy temp array expressions for prefix sums. 14669 SmallVector<Expr *, 8> InscanCopyArrayTemps; 14670 /// inscan copy temp array element expressions for prefix sums. 14671 SmallVector<Expr *, 8> InscanCopyArrayElems; 14672 /// Taskgroup descriptors for the corresponding reduction items in 14673 /// in_reduction clauses. 14674 SmallVector<Expr *, 8> TaskgroupDescriptors; 14675 /// List of captures for clause. 14676 SmallVector<Decl *, 4> ExprCaptures; 14677 /// List of postupdate expressions. 14678 SmallVector<Expr *, 4> ExprPostUpdates; 14679 /// Reduction modifier. 14680 unsigned RedModifier = 0; 14681 ReductionData() = delete; 14682 /// Reserves required memory for the reduction data. 14683 ReductionData(unsigned Size, unsigned Modifier = 0) : RedModifier(Modifier) { 14684 Vars.reserve(Size); 14685 Privates.reserve(Size); 14686 LHSs.reserve(Size); 14687 RHSs.reserve(Size); 14688 ReductionOps.reserve(Size); 14689 if (RedModifier == OMPC_REDUCTION_inscan) { 14690 InscanCopyOps.reserve(Size); 14691 InscanCopyArrayTemps.reserve(Size); 14692 InscanCopyArrayElems.reserve(Size); 14693 } 14694 TaskgroupDescriptors.reserve(Size); 14695 ExprCaptures.reserve(Size); 14696 ExprPostUpdates.reserve(Size); 14697 } 14698 /// Stores reduction item and reduction operation only (required for dependent 14699 /// reduction item). 14700 void push(Expr *Item, Expr *ReductionOp) { 14701 Vars.emplace_back(Item); 14702 Privates.emplace_back(nullptr); 14703 LHSs.emplace_back(nullptr); 14704 RHSs.emplace_back(nullptr); 14705 ReductionOps.emplace_back(ReductionOp); 14706 TaskgroupDescriptors.emplace_back(nullptr); 14707 if (RedModifier == OMPC_REDUCTION_inscan) { 14708 InscanCopyOps.push_back(nullptr); 14709 InscanCopyArrayTemps.push_back(nullptr); 14710 InscanCopyArrayElems.push_back(nullptr); 14711 } 14712 } 14713 /// Stores reduction data. 14714 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp, 14715 Expr *TaskgroupDescriptor, Expr *CopyOp, Expr *CopyArrayTemp, 14716 Expr *CopyArrayElem) { 14717 Vars.emplace_back(Item); 14718 Privates.emplace_back(Private); 14719 LHSs.emplace_back(LHS); 14720 RHSs.emplace_back(RHS); 14721 ReductionOps.emplace_back(ReductionOp); 14722 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor); 14723 if (RedModifier == OMPC_REDUCTION_inscan) { 14724 InscanCopyOps.push_back(CopyOp); 14725 InscanCopyArrayTemps.push_back(CopyArrayTemp); 14726 InscanCopyArrayElems.push_back(CopyArrayElem); 14727 } else { 14728 assert(CopyOp == nullptr && CopyArrayTemp == nullptr && 14729 CopyArrayElem == nullptr && 14730 "Copy operation must be used for inscan reductions only."); 14731 } 14732 } 14733 }; 14734 } // namespace 14735 14736 static bool checkOMPArraySectionConstantForReduction( 14737 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement, 14738 SmallVectorImpl<llvm::APSInt> &ArraySizes) { 14739 const Expr *Length = OASE->getLength(); 14740 if (Length == nullptr) { 14741 // For array sections of the form [1:] or [:], we would need to analyze 14742 // the lower bound... 14743 if (OASE->getColonLocFirst().isValid()) 14744 return false; 14745 14746 // This is an array subscript which has implicit length 1! 14747 SingleElement = true; 14748 ArraySizes.push_back(llvm::APSInt::get(1)); 14749 } else { 14750 Expr::EvalResult Result; 14751 if (!Length->EvaluateAsInt(Result, Context)) 14752 return false; 14753 14754 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 14755 SingleElement = (ConstantLengthValue.getSExtValue() == 1); 14756 ArraySizes.push_back(ConstantLengthValue); 14757 } 14758 14759 // Get the base of this array section and walk up from there. 14760 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); 14761 14762 // We require length = 1 for all array sections except the right-most to 14763 // guarantee that the memory region is contiguous and has no holes in it. 14764 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) { 14765 Length = TempOASE->getLength(); 14766 if (Length == nullptr) { 14767 // For array sections of the form [1:] or [:], we would need to analyze 14768 // the lower bound... 14769 if (OASE->getColonLocFirst().isValid()) 14770 return false; 14771 14772 // This is an array subscript which has implicit length 1! 14773 ArraySizes.push_back(llvm::APSInt::get(1)); 14774 } else { 14775 Expr::EvalResult Result; 14776 if (!Length->EvaluateAsInt(Result, Context)) 14777 return false; 14778 14779 llvm::APSInt ConstantLengthValue = Result.Val.getInt(); 14780 if (ConstantLengthValue.getSExtValue() != 1) 14781 return false; 14782 14783 ArraySizes.push_back(ConstantLengthValue); 14784 } 14785 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 14786 } 14787 14788 // If we have a single element, we don't need to add the implicit lengths. 14789 if (!SingleElement) { 14790 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) { 14791 // Has implicit length 1! 14792 ArraySizes.push_back(llvm::APSInt::get(1)); 14793 Base = TempASE->getBase()->IgnoreParenImpCasts(); 14794 } 14795 } 14796 14797 // This array section can be privatized as a single value or as a constant 14798 // sized array. 14799 return true; 14800 } 14801 14802 static bool actOnOMPReductionKindClause( 14803 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind, 14804 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 14805 SourceLocation ColonLoc, SourceLocation EndLoc, 14806 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 14807 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) { 14808 DeclarationName DN = ReductionId.getName(); 14809 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator(); 14810 BinaryOperatorKind BOK = BO_Comma; 14811 14812 ASTContext &Context = S.Context; 14813 // OpenMP [2.14.3.6, reduction clause] 14814 // C 14815 // reduction-identifier is either an identifier or one of the following 14816 // operators: +, -, *, &, |, ^, && and || 14817 // C++ 14818 // reduction-identifier is either an id-expression or one of the following 14819 // operators: +, -, *, &, |, ^, && and || 14820 switch (OOK) { 14821 case OO_Plus: 14822 case OO_Minus: 14823 BOK = BO_Add; 14824 break; 14825 case OO_Star: 14826 BOK = BO_Mul; 14827 break; 14828 case OO_Amp: 14829 BOK = BO_And; 14830 break; 14831 case OO_Pipe: 14832 BOK = BO_Or; 14833 break; 14834 case OO_Caret: 14835 BOK = BO_Xor; 14836 break; 14837 case OO_AmpAmp: 14838 BOK = BO_LAnd; 14839 break; 14840 case OO_PipePipe: 14841 BOK = BO_LOr; 14842 break; 14843 case OO_New: 14844 case OO_Delete: 14845 case OO_Array_New: 14846 case OO_Array_Delete: 14847 case OO_Slash: 14848 case OO_Percent: 14849 case OO_Tilde: 14850 case OO_Exclaim: 14851 case OO_Equal: 14852 case OO_Less: 14853 case OO_Greater: 14854 case OO_LessEqual: 14855 case OO_GreaterEqual: 14856 case OO_PlusEqual: 14857 case OO_MinusEqual: 14858 case OO_StarEqual: 14859 case OO_SlashEqual: 14860 case OO_PercentEqual: 14861 case OO_CaretEqual: 14862 case OO_AmpEqual: 14863 case OO_PipeEqual: 14864 case OO_LessLess: 14865 case OO_GreaterGreater: 14866 case OO_LessLessEqual: 14867 case OO_GreaterGreaterEqual: 14868 case OO_EqualEqual: 14869 case OO_ExclaimEqual: 14870 case OO_Spaceship: 14871 case OO_PlusPlus: 14872 case OO_MinusMinus: 14873 case OO_Comma: 14874 case OO_ArrowStar: 14875 case OO_Arrow: 14876 case OO_Call: 14877 case OO_Subscript: 14878 case OO_Conditional: 14879 case OO_Coawait: 14880 case NUM_OVERLOADED_OPERATORS: 14881 llvm_unreachable("Unexpected reduction identifier"); 14882 case OO_None: 14883 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) { 14884 if (II->isStr("max")) 14885 BOK = BO_GT; 14886 else if (II->isStr("min")) 14887 BOK = BO_LT; 14888 } 14889 break; 14890 } 14891 SourceRange ReductionIdRange; 14892 if (ReductionIdScopeSpec.isValid()) 14893 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 14894 else 14895 ReductionIdRange.setBegin(ReductionId.getBeginLoc()); 14896 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 14897 14898 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end(); 14899 bool FirstIter = true; 14900 for (Expr *RefExpr : VarList) { 14901 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 14902 // OpenMP [2.1, C/C++] 14903 // A list item is a variable or array section, subject to the restrictions 14904 // specified in Section 2.4 on page 42 and in each of the sections 14905 // describing clauses and directives for which a list appears. 14906 // OpenMP [2.14.3.3, Restrictions, p.1] 14907 // A variable that is part of another variable (as an array or 14908 // structure element) cannot appear in a private clause. 14909 if (!FirstIter && IR != ER) 14910 ++IR; 14911 FirstIter = false; 14912 SourceLocation ELoc; 14913 SourceRange ERange; 14914 Expr *SimpleRefExpr = RefExpr; 14915 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange, 14916 /*AllowArraySection=*/true); 14917 if (Res.second) { 14918 // Try to find 'declare reduction' corresponding construct before using 14919 // builtin/overloaded operators. 14920 QualType Type = Context.DependentTy; 14921 CXXCastPath BasePath; 14922 ExprResult DeclareReductionRef = buildDeclareReductionRef( 14923 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 14924 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 14925 Expr *ReductionOp = nullptr; 14926 if (S.CurContext->isDependentContext() && 14927 (DeclareReductionRef.isUnset() || 14928 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) 14929 ReductionOp = DeclareReductionRef.get(); 14930 // It will be analyzed later. 14931 RD.push(RefExpr, ReductionOp); 14932 } 14933 ValueDecl *D = Res.first; 14934 if (!D) 14935 continue; 14936 14937 Expr *TaskgroupDescriptor = nullptr; 14938 QualType Type; 14939 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens()); 14940 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens()); 14941 if (ASE) { 14942 Type = ASE->getType().getNonReferenceType(); 14943 } else if (OASE) { 14944 QualType BaseType = 14945 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 14946 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 14947 Type = ATy->getElementType(); 14948 else 14949 Type = BaseType->getPointeeType(); 14950 Type = Type.getNonReferenceType(); 14951 } else { 14952 Type = Context.getBaseElementType(D->getType().getNonReferenceType()); 14953 } 14954 auto *VD = dyn_cast<VarDecl>(D); 14955 14956 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 14957 // A variable that appears in a private clause must not have an incomplete 14958 // type or a reference type. 14959 if (S.RequireCompleteType(ELoc, D->getType(), 14960 diag::err_omp_reduction_incomplete_type)) 14961 continue; 14962 // OpenMP [2.14.3.6, reduction clause, Restrictions] 14963 // A list item that appears in a reduction clause must not be 14964 // const-qualified. 14965 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc, 14966 /*AcceptIfMutable*/ false, ASE || OASE)) 14967 continue; 14968 14969 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective(); 14970 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 14971 // If a list-item is a reference type then it must bind to the same object 14972 // for all threads of the team. 14973 if (!ASE && !OASE) { 14974 if (VD) { 14975 VarDecl *VDDef = VD->getDefinition(); 14976 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) { 14977 DSARefChecker Check(Stack); 14978 if (Check.Visit(VDDef->getInit())) { 14979 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg) 14980 << getOpenMPClauseName(ClauseKind) << ERange; 14981 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 14982 continue; 14983 } 14984 } 14985 } 14986 14987 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 14988 // in a Construct] 14989 // Variables with the predetermined data-sharing attributes may not be 14990 // listed in data-sharing attributes clauses, except for the cases 14991 // listed below. For these exceptions only, listing a predetermined 14992 // variable in a data-sharing attribute clause is allowed and overrides 14993 // the variable's predetermined data-sharing attributes. 14994 // OpenMP [2.14.3.6, Restrictions, p.3] 14995 // Any number of reduction clauses can be specified on the directive, 14996 // but a list item can appear only once in the reduction clauses for that 14997 // directive. 14998 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false); 14999 if (DVar.CKind == OMPC_reduction) { 15000 S.Diag(ELoc, diag::err_omp_once_referenced) 15001 << getOpenMPClauseName(ClauseKind); 15002 if (DVar.RefExpr) 15003 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 15004 continue; 15005 } 15006 if (DVar.CKind != OMPC_unknown) { 15007 S.Diag(ELoc, diag::err_omp_wrong_dsa) 15008 << getOpenMPClauseName(DVar.CKind) 15009 << getOpenMPClauseName(OMPC_reduction); 15010 reportOriginalDsa(S, Stack, D, DVar); 15011 continue; 15012 } 15013 15014 // OpenMP [2.14.3.6, Restrictions, p.1] 15015 // A list item that appears in a reduction clause of a worksharing 15016 // construct must be shared in the parallel regions to which any of the 15017 // worksharing regions arising from the worksharing construct bind. 15018 if (isOpenMPWorksharingDirective(CurrDir) && 15019 !isOpenMPParallelDirective(CurrDir) && 15020 !isOpenMPTeamsDirective(CurrDir)) { 15021 DVar = Stack->getImplicitDSA(D, true); 15022 if (DVar.CKind != OMPC_shared) { 15023 S.Diag(ELoc, diag::err_omp_required_access) 15024 << getOpenMPClauseName(OMPC_reduction) 15025 << getOpenMPClauseName(OMPC_shared); 15026 reportOriginalDsa(S, Stack, D, DVar); 15027 continue; 15028 } 15029 } 15030 } 15031 15032 // Try to find 'declare reduction' corresponding construct before using 15033 // builtin/overloaded operators. 15034 CXXCastPath BasePath; 15035 ExprResult DeclareReductionRef = buildDeclareReductionRef( 15036 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec, 15037 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR); 15038 if (DeclareReductionRef.isInvalid()) 15039 continue; 15040 if (S.CurContext->isDependentContext() && 15041 (DeclareReductionRef.isUnset() || 15042 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) { 15043 RD.push(RefExpr, DeclareReductionRef.get()); 15044 continue; 15045 } 15046 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) { 15047 // Not allowed reduction identifier is found. 15048 S.Diag(ReductionId.getBeginLoc(), 15049 diag::err_omp_unknown_reduction_identifier) 15050 << Type << ReductionIdRange; 15051 continue; 15052 } 15053 15054 // OpenMP [2.14.3.6, reduction clause, Restrictions] 15055 // The type of a list item that appears in a reduction clause must be valid 15056 // for the reduction-identifier. For a max or min reduction in C, the type 15057 // of the list item must be an allowed arithmetic data type: char, int, 15058 // float, double, or _Bool, possibly modified with long, short, signed, or 15059 // unsigned. For a max or min reduction in C++, the type of the list item 15060 // must be an allowed arithmetic data type: char, wchar_t, int, float, 15061 // double, or bool, possibly modified with long, short, signed, or unsigned. 15062 if (DeclareReductionRef.isUnset()) { 15063 if ((BOK == BO_GT || BOK == BO_LT) && 15064 !(Type->isScalarType() || 15065 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 15066 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 15067 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus; 15068 if (!ASE && !OASE) { 15069 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 15070 VarDecl::DeclarationOnly; 15071 S.Diag(D->getLocation(), 15072 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15073 << D; 15074 } 15075 continue; 15076 } 15077 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 15078 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) { 15079 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg) 15080 << getOpenMPClauseName(ClauseKind); 15081 if (!ASE && !OASE) { 15082 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 15083 VarDecl::DeclarationOnly; 15084 S.Diag(D->getLocation(), 15085 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15086 << D; 15087 } 15088 continue; 15089 } 15090 } 15091 15092 Type = Type.getNonLValueExprType(Context).getUnqualifiedType(); 15093 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs", 15094 D->hasAttrs() ? &D->getAttrs() : nullptr); 15095 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(), 15096 D->hasAttrs() ? &D->getAttrs() : nullptr); 15097 QualType PrivateTy = Type; 15098 15099 // Try if we can determine constant lengths for all array sections and avoid 15100 // the VLA. 15101 bool ConstantLengthOASE = false; 15102 if (OASE) { 15103 bool SingleElement; 15104 llvm::SmallVector<llvm::APSInt, 4> ArraySizes; 15105 ConstantLengthOASE = checkOMPArraySectionConstantForReduction( 15106 Context, OASE, SingleElement, ArraySizes); 15107 15108 // If we don't have a single element, we must emit a constant array type. 15109 if (ConstantLengthOASE && !SingleElement) { 15110 for (llvm::APSInt &Size : ArraySizes) 15111 PrivateTy = Context.getConstantArrayType(PrivateTy, Size, nullptr, 15112 ArrayType::Normal, 15113 /*IndexTypeQuals=*/0); 15114 } 15115 } 15116 15117 if ((OASE && !ConstantLengthOASE) || 15118 (!OASE && !ASE && 15119 D->getType().getNonReferenceType()->isVariablyModifiedType())) { 15120 if (!Context.getTargetInfo().isVLASupported()) { 15121 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) { 15122 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 15123 S.Diag(ELoc, diag::note_vla_unsupported); 15124 continue; 15125 } else { 15126 S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE; 15127 S.targetDiag(ELoc, diag::note_vla_unsupported); 15128 } 15129 } 15130 // For arrays/array sections only: 15131 // Create pseudo array type for private copy. The size for this array will 15132 // be generated during codegen. 15133 // For array subscripts or single variables Private Ty is the same as Type 15134 // (type of the variable or single array element). 15135 PrivateTy = Context.getVariableArrayType( 15136 Type, 15137 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue), 15138 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange()); 15139 } else if (!ASE && !OASE && 15140 Context.getAsArrayType(D->getType().getNonReferenceType())) { 15141 PrivateTy = D->getType().getNonReferenceType(); 15142 } 15143 // Private copy. 15144 VarDecl *PrivateVD = 15145 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 15146 D->hasAttrs() ? &D->getAttrs() : nullptr, 15147 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 15148 // Add initializer for private variable. 15149 Expr *Init = nullptr; 15150 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc); 15151 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc); 15152 if (DeclareReductionRef.isUsable()) { 15153 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>(); 15154 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl()); 15155 if (DRD->getInitializer()) { 15156 Init = DRDRef; 15157 RHSVD->setInit(DRDRef); 15158 RHSVD->setInitStyle(VarDecl::CallInit); 15159 } 15160 } else { 15161 switch (BOK) { 15162 case BO_Add: 15163 case BO_Xor: 15164 case BO_Or: 15165 case BO_LOr: 15166 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'. 15167 if (Type->isScalarType() || Type->isAnyComplexType()) 15168 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get(); 15169 break; 15170 case BO_Mul: 15171 case BO_LAnd: 15172 if (Type->isScalarType() || Type->isAnyComplexType()) { 15173 // '*' and '&&' reduction ops - initializer is '1'. 15174 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get(); 15175 } 15176 break; 15177 case BO_And: { 15178 // '&' reduction op - initializer is '~0'. 15179 QualType OrigType = Type; 15180 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) 15181 Type = ComplexTy->getElementType(); 15182 if (Type->isRealFloatingType()) { 15183 llvm::APFloat InitValue = llvm::APFloat::getAllOnesValue( 15184 Context.getFloatTypeSemantics(Type), 15185 Context.getTypeSize(Type)); 15186 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 15187 Type, ELoc); 15188 } else if (Type->isScalarType()) { 15189 uint64_t Size = Context.getTypeSize(Type); 15190 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0); 15191 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size); 15192 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 15193 } 15194 if (Init && OrigType->isAnyComplexType()) { 15195 // Init = 0xFFFF + 0xFFFFi; 15196 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType); 15197 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get(); 15198 } 15199 Type = OrigType; 15200 break; 15201 } 15202 case BO_LT: 15203 case BO_GT: { 15204 // 'min' reduction op - initializer is 'Largest representable number in 15205 // the reduction list item type'. 15206 // 'max' reduction op - initializer is 'Least representable number in 15207 // the reduction list item type'. 15208 if (Type->isIntegerType() || Type->isPointerType()) { 15209 bool IsSigned = Type->hasSignedIntegerRepresentation(); 15210 uint64_t Size = Context.getTypeSize(Type); 15211 QualType IntTy = 15212 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned); 15213 llvm::APInt InitValue = 15214 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size) 15215 : llvm::APInt::getMinValue(Size) 15216 : IsSigned ? llvm::APInt::getSignedMaxValue(Size) 15217 : llvm::APInt::getMaxValue(Size); 15218 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc); 15219 if (Type->isPointerType()) { 15220 // Cast to pointer type. 15221 ExprResult CastExpr = S.BuildCStyleCastExpr( 15222 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init); 15223 if (CastExpr.isInvalid()) 15224 continue; 15225 Init = CastExpr.get(); 15226 } 15227 } else if (Type->isRealFloatingType()) { 15228 llvm::APFloat InitValue = llvm::APFloat::getLargest( 15229 Context.getFloatTypeSemantics(Type), BOK != BO_LT); 15230 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true, 15231 Type, ELoc); 15232 } 15233 break; 15234 } 15235 case BO_PtrMemD: 15236 case BO_PtrMemI: 15237 case BO_MulAssign: 15238 case BO_Div: 15239 case BO_Rem: 15240 case BO_Sub: 15241 case BO_Shl: 15242 case BO_Shr: 15243 case BO_LE: 15244 case BO_GE: 15245 case BO_EQ: 15246 case BO_NE: 15247 case BO_Cmp: 15248 case BO_AndAssign: 15249 case BO_XorAssign: 15250 case BO_OrAssign: 15251 case BO_Assign: 15252 case BO_AddAssign: 15253 case BO_SubAssign: 15254 case BO_DivAssign: 15255 case BO_RemAssign: 15256 case BO_ShlAssign: 15257 case BO_ShrAssign: 15258 case BO_Comma: 15259 llvm_unreachable("Unexpected reduction operation"); 15260 } 15261 } 15262 if (Init && DeclareReductionRef.isUnset()) 15263 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false); 15264 else if (!Init) 15265 S.ActOnUninitializedDecl(RHSVD); 15266 if (RHSVD->isInvalidDecl()) 15267 continue; 15268 if (!RHSVD->hasInit() && 15269 (DeclareReductionRef.isUnset() || !S.LangOpts.CPlusPlus)) { 15270 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible) 15271 << Type << ReductionIdRange; 15272 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) == 15273 VarDecl::DeclarationOnly; 15274 S.Diag(D->getLocation(), 15275 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15276 << D; 15277 continue; 15278 } 15279 // Store initializer for single element in private copy. Will be used during 15280 // codegen. 15281 PrivateVD->setInit(RHSVD->getInit()); 15282 PrivateVD->setInitStyle(RHSVD->getInitStyle()); 15283 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc); 15284 ExprResult ReductionOp; 15285 if (DeclareReductionRef.isUsable()) { 15286 QualType RedTy = DeclareReductionRef.get()->getType(); 15287 QualType PtrRedTy = Context.getPointerType(RedTy); 15288 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE); 15289 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE); 15290 if (!BasePath.empty()) { 15291 LHS = S.DefaultLvalueConversion(LHS.get()); 15292 RHS = S.DefaultLvalueConversion(RHS.get()); 15293 LHS = ImplicitCastExpr::Create(Context, PtrRedTy, 15294 CK_UncheckedDerivedToBase, LHS.get(), 15295 &BasePath, LHS.get()->getValueKind()); 15296 RHS = ImplicitCastExpr::Create(Context, PtrRedTy, 15297 CK_UncheckedDerivedToBase, RHS.get(), 15298 &BasePath, RHS.get()->getValueKind()); 15299 } 15300 FunctionProtoType::ExtProtoInfo EPI; 15301 QualType Params[] = {PtrRedTy, PtrRedTy}; 15302 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI); 15303 auto *OVE = new (Context) OpaqueValueExpr( 15304 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary, 15305 S.DefaultLvalueConversion(DeclareReductionRef.get()).get()); 15306 Expr *Args[] = {LHS.get(), RHS.get()}; 15307 ReductionOp = 15308 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc); 15309 } else { 15310 ReductionOp = S.BuildBinOp( 15311 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE); 15312 if (ReductionOp.isUsable()) { 15313 if (BOK != BO_LT && BOK != BO_GT) { 15314 ReductionOp = 15315 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 15316 BO_Assign, LHSDRE, ReductionOp.get()); 15317 } else { 15318 auto *ConditionalOp = new (Context) 15319 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE, 15320 Type, VK_LValue, OK_Ordinary); 15321 ReductionOp = 15322 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(), 15323 BO_Assign, LHSDRE, ConditionalOp); 15324 } 15325 if (ReductionOp.isUsable()) 15326 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(), 15327 /*DiscardedValue*/ false); 15328 } 15329 if (!ReductionOp.isUsable()) 15330 continue; 15331 } 15332 15333 // Add copy operations for inscan reductions. 15334 // LHS = RHS; 15335 ExprResult CopyOpRes, TempArrayRes, TempArrayElem; 15336 if (ClauseKind == OMPC_reduction && 15337 RD.RedModifier == OMPC_REDUCTION_inscan) { 15338 ExprResult RHS = S.DefaultLvalueConversion(RHSDRE); 15339 CopyOpRes = S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, LHSDRE, 15340 RHS.get()); 15341 if (!CopyOpRes.isUsable()) 15342 continue; 15343 CopyOpRes = 15344 S.ActOnFinishFullExpr(CopyOpRes.get(), /*DiscardedValue=*/true); 15345 if (!CopyOpRes.isUsable()) 15346 continue; 15347 // For simd directive and simd-based directives in simd mode no need to 15348 // construct temp array, need just a single temp element. 15349 if (Stack->getCurrentDirective() == OMPD_simd || 15350 (S.getLangOpts().OpenMPSimd && 15351 isOpenMPSimdDirective(Stack->getCurrentDirective()))) { 15352 VarDecl *TempArrayVD = 15353 buildVarDecl(S, ELoc, PrivateTy, D->getName(), 15354 D->hasAttrs() ? &D->getAttrs() : nullptr); 15355 // Add a constructor to the temp decl. 15356 S.ActOnUninitializedDecl(TempArrayVD); 15357 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, PrivateTy, ELoc); 15358 } else { 15359 // Build temp array for prefix sum. 15360 auto *Dim = new (S.Context) 15361 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_RValue); 15362 QualType ArrayTy = 15363 S.Context.getVariableArrayType(PrivateTy, Dim, ArrayType::Normal, 15364 /*IndexTypeQuals=*/0, {ELoc, ELoc}); 15365 VarDecl *TempArrayVD = 15366 buildVarDecl(S, ELoc, ArrayTy, D->getName(), 15367 D->hasAttrs() ? &D->getAttrs() : nullptr); 15368 // Add a constructor to the temp decl. 15369 S.ActOnUninitializedDecl(TempArrayVD); 15370 TempArrayRes = buildDeclRefExpr(S, TempArrayVD, ArrayTy, ELoc); 15371 TempArrayElem = 15372 S.DefaultFunctionArrayLvalueConversion(TempArrayRes.get()); 15373 auto *Idx = new (S.Context) 15374 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_RValue); 15375 TempArrayElem = S.CreateBuiltinArraySubscriptExpr(TempArrayElem.get(), 15376 ELoc, Idx, ELoc); 15377 } 15378 } 15379 15380 // OpenMP [2.15.4.6, Restrictions, p.2] 15381 // A list item that appears in an in_reduction clause of a task construct 15382 // must appear in a task_reduction clause of a construct associated with a 15383 // taskgroup region that includes the participating task in its taskgroup 15384 // set. The construct associated with the innermost region that meets this 15385 // condition must specify the same reduction-identifier as the in_reduction 15386 // clause. 15387 if (ClauseKind == OMPC_in_reduction) { 15388 SourceRange ParentSR; 15389 BinaryOperatorKind ParentBOK; 15390 const Expr *ParentReductionOp = nullptr; 15391 Expr *ParentBOKTD = nullptr, *ParentReductionOpTD = nullptr; 15392 DSAStackTy::DSAVarData ParentBOKDSA = 15393 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK, 15394 ParentBOKTD); 15395 DSAStackTy::DSAVarData ParentReductionOpDSA = 15396 Stack->getTopMostTaskgroupReductionData( 15397 D, ParentSR, ParentReductionOp, ParentReductionOpTD); 15398 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown; 15399 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown; 15400 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) || 15401 (DeclareReductionRef.isUsable() && IsParentBOK) || 15402 (IsParentBOK && BOK != ParentBOK) || IsParentReductionOp) { 15403 bool EmitError = true; 15404 if (IsParentReductionOp && DeclareReductionRef.isUsable()) { 15405 llvm::FoldingSetNodeID RedId, ParentRedId; 15406 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true); 15407 DeclareReductionRef.get()->Profile(RedId, Context, 15408 /*Canonical=*/true); 15409 EmitError = RedId != ParentRedId; 15410 } 15411 if (EmitError) { 15412 S.Diag(ReductionId.getBeginLoc(), 15413 diag::err_omp_reduction_identifier_mismatch) 15414 << ReductionIdRange << RefExpr->getSourceRange(); 15415 S.Diag(ParentSR.getBegin(), 15416 diag::note_omp_previous_reduction_identifier) 15417 << ParentSR 15418 << (IsParentBOK ? ParentBOKDSA.RefExpr 15419 : ParentReductionOpDSA.RefExpr) 15420 ->getSourceRange(); 15421 continue; 15422 } 15423 } 15424 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD; 15425 } 15426 15427 DeclRefExpr *Ref = nullptr; 15428 Expr *VarsExpr = RefExpr->IgnoreParens(); 15429 if (!VD && !S.CurContext->isDependentContext()) { 15430 if (ASE || OASE) { 15431 TransformExprToCaptures RebuildToCapture(S, D); 15432 VarsExpr = 15433 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get(); 15434 Ref = RebuildToCapture.getCapturedExpr(); 15435 } else { 15436 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false); 15437 } 15438 if (!S.isOpenMPCapturedDecl(D)) { 15439 RD.ExprCaptures.emplace_back(Ref->getDecl()); 15440 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 15441 ExprResult RefRes = S.DefaultLvalueConversion(Ref); 15442 if (!RefRes.isUsable()) 15443 continue; 15444 ExprResult PostUpdateRes = 15445 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr, 15446 RefRes.get()); 15447 if (!PostUpdateRes.isUsable()) 15448 continue; 15449 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) || 15450 Stack->getCurrentDirective() == OMPD_taskgroup) { 15451 S.Diag(RefExpr->getExprLoc(), 15452 diag::err_omp_reduction_non_addressable_expression) 15453 << RefExpr->getSourceRange(); 15454 continue; 15455 } 15456 RD.ExprPostUpdates.emplace_back( 15457 S.IgnoredValueConversions(PostUpdateRes.get()).get()); 15458 } 15459 } 15460 } 15461 // All reduction items are still marked as reduction (to do not increase 15462 // code base size). 15463 unsigned Modifier = RD.RedModifier; 15464 // Consider task_reductions as reductions with task modifier. Required for 15465 // correct analysis of in_reduction clauses. 15466 if (CurrDir == OMPD_taskgroup && ClauseKind == OMPC_task_reduction) 15467 Modifier = OMPC_REDUCTION_task; 15468 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref, Modifier); 15469 if (Modifier == OMPC_REDUCTION_task && 15470 (CurrDir == OMPD_taskgroup || 15471 ((isOpenMPParallelDirective(CurrDir) || 15472 isOpenMPWorksharingDirective(CurrDir)) && 15473 !isOpenMPSimdDirective(CurrDir)))) { 15474 if (DeclareReductionRef.isUsable()) 15475 Stack->addTaskgroupReductionData(D, ReductionIdRange, 15476 DeclareReductionRef.get()); 15477 else 15478 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK); 15479 } 15480 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(), 15481 TaskgroupDescriptor, CopyOpRes.get(), TempArrayRes.get(), 15482 TempArrayElem.get()); 15483 } 15484 return RD.Vars.empty(); 15485 } 15486 15487 OMPClause *Sema::ActOnOpenMPReductionClause( 15488 ArrayRef<Expr *> VarList, OpenMPReductionClauseModifier Modifier, 15489 SourceLocation StartLoc, SourceLocation LParenLoc, 15490 SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc, 15491 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 15492 ArrayRef<Expr *> UnresolvedReductions) { 15493 if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) { 15494 Diag(LParenLoc, diag::err_omp_unexpected_clause_value) 15495 << getListOfPossibleValues(OMPC_reduction, /*First=*/0, 15496 /*Last=*/OMPC_REDUCTION_unknown) 15497 << getOpenMPClauseName(OMPC_reduction); 15498 return nullptr; 15499 } 15500 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions 15501 // A reduction clause with the inscan reduction-modifier may only appear on a 15502 // worksharing-loop construct, a worksharing-loop SIMD construct, a simd 15503 // construct, a parallel worksharing-loop construct or a parallel 15504 // worksharing-loop SIMD construct. 15505 if (Modifier == OMPC_REDUCTION_inscan && 15506 (DSAStack->getCurrentDirective() != OMPD_for && 15507 DSAStack->getCurrentDirective() != OMPD_for_simd && 15508 DSAStack->getCurrentDirective() != OMPD_simd && 15509 DSAStack->getCurrentDirective() != OMPD_parallel_for && 15510 DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) { 15511 Diag(ModifierLoc, diag::err_omp_wrong_inscan_reduction); 15512 return nullptr; 15513 } 15514 15515 ReductionData RD(VarList.size(), Modifier); 15516 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList, 15517 StartLoc, LParenLoc, ColonLoc, EndLoc, 15518 ReductionIdScopeSpec, ReductionId, 15519 UnresolvedReductions, RD)) 15520 return nullptr; 15521 15522 return OMPReductionClause::Create( 15523 Context, StartLoc, LParenLoc, ModifierLoc, ColonLoc, EndLoc, Modifier, 15524 RD.Vars, ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 15525 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.InscanCopyOps, 15526 RD.InscanCopyArrayTemps, RD.InscanCopyArrayElems, 15527 buildPreInits(Context, RD.ExprCaptures), 15528 buildPostUpdate(*this, RD.ExprPostUpdates)); 15529 } 15530 15531 OMPClause *Sema::ActOnOpenMPTaskReductionClause( 15532 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 15533 SourceLocation ColonLoc, SourceLocation EndLoc, 15534 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 15535 ArrayRef<Expr *> UnresolvedReductions) { 15536 ReductionData RD(VarList.size()); 15537 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList, 15538 StartLoc, LParenLoc, ColonLoc, EndLoc, 15539 ReductionIdScopeSpec, ReductionId, 15540 UnresolvedReductions, RD)) 15541 return nullptr; 15542 15543 return OMPTaskReductionClause::Create( 15544 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 15545 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 15546 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, 15547 buildPreInits(Context, RD.ExprCaptures), 15548 buildPostUpdate(*this, RD.ExprPostUpdates)); 15549 } 15550 15551 OMPClause *Sema::ActOnOpenMPInReductionClause( 15552 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 15553 SourceLocation ColonLoc, SourceLocation EndLoc, 15554 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId, 15555 ArrayRef<Expr *> UnresolvedReductions) { 15556 ReductionData RD(VarList.size()); 15557 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList, 15558 StartLoc, LParenLoc, ColonLoc, EndLoc, 15559 ReductionIdScopeSpec, ReductionId, 15560 UnresolvedReductions, RD)) 15561 return nullptr; 15562 15563 return OMPInReductionClause::Create( 15564 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars, 15565 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, 15566 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors, 15567 buildPreInits(Context, RD.ExprCaptures), 15568 buildPostUpdate(*this, RD.ExprPostUpdates)); 15569 } 15570 15571 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, 15572 SourceLocation LinLoc) { 15573 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) || 15574 LinKind == OMPC_LINEAR_unknown) { 15575 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus; 15576 return true; 15577 } 15578 return false; 15579 } 15580 15581 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc, 15582 OpenMPLinearClauseKind LinKind, QualType Type, 15583 bool IsDeclareSimd) { 15584 const auto *VD = dyn_cast_or_null<VarDecl>(D); 15585 // A variable must not have an incomplete type or a reference type. 15586 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type)) 15587 return true; 15588 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) && 15589 !Type->isReferenceType()) { 15590 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference) 15591 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind); 15592 return true; 15593 } 15594 Type = Type.getNonReferenceType(); 15595 15596 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions] 15597 // A variable that is privatized must not have a const-qualified type 15598 // unless it is of class type with a mutable member. This restriction does 15599 // not apply to the firstprivate clause, nor to the linear clause on 15600 // declarative directives (like declare simd). 15601 if (!IsDeclareSimd && 15602 rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc)) 15603 return true; 15604 15605 // A list item must be of integral or pointer type. 15606 Type = Type.getUnqualifiedType().getCanonicalType(); 15607 const auto *Ty = Type.getTypePtrOrNull(); 15608 if (!Ty || (LinKind != OMPC_LINEAR_ref && !Ty->isDependentType() && 15609 !Ty->isIntegralType(Context) && !Ty->isPointerType())) { 15610 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type; 15611 if (D) { 15612 bool IsDecl = 15613 !VD || 15614 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 15615 Diag(D->getLocation(), 15616 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15617 << D; 15618 } 15619 return true; 15620 } 15621 return false; 15622 } 15623 15624 OMPClause *Sema::ActOnOpenMPLinearClause( 15625 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc, 15626 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind, 15627 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 15628 SmallVector<Expr *, 8> Vars; 15629 SmallVector<Expr *, 8> Privates; 15630 SmallVector<Expr *, 8> Inits; 15631 SmallVector<Decl *, 4> ExprCaptures; 15632 SmallVector<Expr *, 4> ExprPostUpdates; 15633 if (CheckOpenMPLinearModifier(LinKind, LinLoc)) 15634 LinKind = OMPC_LINEAR_val; 15635 for (Expr *RefExpr : VarList) { 15636 assert(RefExpr && "NULL expr in OpenMP linear clause."); 15637 SourceLocation ELoc; 15638 SourceRange ERange; 15639 Expr *SimpleRefExpr = RefExpr; 15640 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15641 if (Res.second) { 15642 // It will be analyzed later. 15643 Vars.push_back(RefExpr); 15644 Privates.push_back(nullptr); 15645 Inits.push_back(nullptr); 15646 } 15647 ValueDecl *D = Res.first; 15648 if (!D) 15649 continue; 15650 15651 QualType Type = D->getType(); 15652 auto *VD = dyn_cast<VarDecl>(D); 15653 15654 // OpenMP [2.14.3.7, linear clause] 15655 // A list-item cannot appear in more than one linear clause. 15656 // A list-item that appears in a linear clause cannot appear in any 15657 // other data-sharing attribute clause. 15658 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 15659 if (DVar.RefExpr) { 15660 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 15661 << getOpenMPClauseName(OMPC_linear); 15662 reportOriginalDsa(*this, DSAStack, D, DVar); 15663 continue; 15664 } 15665 15666 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type)) 15667 continue; 15668 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 15669 15670 // Build private copy of original var. 15671 VarDecl *Private = 15672 buildVarDecl(*this, ELoc, Type, D->getName(), 15673 D->hasAttrs() ? &D->getAttrs() : nullptr, 15674 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 15675 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc); 15676 // Build var to save initial value. 15677 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start"); 15678 Expr *InitExpr; 15679 DeclRefExpr *Ref = nullptr; 15680 if (!VD && !CurContext->isDependentContext()) { 15681 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 15682 if (!isOpenMPCapturedDecl(D)) { 15683 ExprCaptures.push_back(Ref->getDecl()); 15684 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) { 15685 ExprResult RefRes = DefaultLvalueConversion(Ref); 15686 if (!RefRes.isUsable()) 15687 continue; 15688 ExprResult PostUpdateRes = 15689 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, 15690 SimpleRefExpr, RefRes.get()); 15691 if (!PostUpdateRes.isUsable()) 15692 continue; 15693 ExprPostUpdates.push_back( 15694 IgnoredValueConversions(PostUpdateRes.get()).get()); 15695 } 15696 } 15697 } 15698 if (LinKind == OMPC_LINEAR_uval) 15699 InitExpr = VD ? VD->getInit() : SimpleRefExpr; 15700 else 15701 InitExpr = VD ? SimpleRefExpr : Ref; 15702 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(), 15703 /*DirectInit=*/false); 15704 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc); 15705 15706 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref); 15707 Vars.push_back((VD || CurContext->isDependentContext()) 15708 ? RefExpr->IgnoreParens() 15709 : Ref); 15710 Privates.push_back(PrivateRef); 15711 Inits.push_back(InitRef); 15712 } 15713 15714 if (Vars.empty()) 15715 return nullptr; 15716 15717 Expr *StepExpr = Step; 15718 Expr *CalcStepExpr = nullptr; 15719 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 15720 !Step->isInstantiationDependent() && 15721 !Step->containsUnexpandedParameterPack()) { 15722 SourceLocation StepLoc = Step->getBeginLoc(); 15723 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 15724 if (Val.isInvalid()) 15725 return nullptr; 15726 StepExpr = Val.get(); 15727 15728 // Build var to save the step value. 15729 VarDecl *SaveVar = 15730 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step"); 15731 ExprResult SaveRef = 15732 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc); 15733 ExprResult CalcStep = 15734 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr); 15735 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false); 15736 15737 // Warn about zero linear step (it would be probably better specified as 15738 // making corresponding variables 'const'). 15739 llvm::APSInt Result; 15740 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context); 15741 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive()) 15742 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] 15743 << (Vars.size() > 1); 15744 if (!IsConstant && CalcStep.isUsable()) { 15745 // Calculate the step beforehand instead of doing this on each iteration. 15746 // (This is not used if the number of iterations may be kfold-ed). 15747 CalcStepExpr = CalcStep.get(); 15748 } 15749 } 15750 15751 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc, 15752 ColonLoc, EndLoc, Vars, Privates, Inits, 15753 StepExpr, CalcStepExpr, 15754 buildPreInits(Context, ExprCaptures), 15755 buildPostUpdate(*this, ExprPostUpdates)); 15756 } 15757 15758 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV, 15759 Expr *NumIterations, Sema &SemaRef, 15760 Scope *S, DSAStackTy *Stack) { 15761 // Walk the vars and build update/final expressions for the CodeGen. 15762 SmallVector<Expr *, 8> Updates; 15763 SmallVector<Expr *, 8> Finals; 15764 SmallVector<Expr *, 8> UsedExprs; 15765 Expr *Step = Clause.getStep(); 15766 Expr *CalcStep = Clause.getCalcStep(); 15767 // OpenMP [2.14.3.7, linear clause] 15768 // If linear-step is not specified it is assumed to be 1. 15769 if (!Step) 15770 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(); 15771 else if (CalcStep) 15772 Step = cast<BinaryOperator>(CalcStep)->getLHS(); 15773 bool HasErrors = false; 15774 auto CurInit = Clause.inits().begin(); 15775 auto CurPrivate = Clause.privates().begin(); 15776 OpenMPLinearClauseKind LinKind = Clause.getModifier(); 15777 for (Expr *RefExpr : Clause.varlists()) { 15778 SourceLocation ELoc; 15779 SourceRange ERange; 15780 Expr *SimpleRefExpr = RefExpr; 15781 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange); 15782 ValueDecl *D = Res.first; 15783 if (Res.second || !D) { 15784 Updates.push_back(nullptr); 15785 Finals.push_back(nullptr); 15786 HasErrors = true; 15787 continue; 15788 } 15789 auto &&Info = Stack->isLoopControlVariable(D); 15790 // OpenMP [2.15.11, distribute simd Construct] 15791 // A list item may not appear in a linear clause, unless it is the loop 15792 // iteration variable. 15793 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) && 15794 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) { 15795 SemaRef.Diag(ELoc, 15796 diag::err_omp_linear_distribute_var_non_loop_iteration); 15797 Updates.push_back(nullptr); 15798 Finals.push_back(nullptr); 15799 HasErrors = true; 15800 continue; 15801 } 15802 Expr *InitExpr = *CurInit; 15803 15804 // Build privatized reference to the current linear var. 15805 auto *DE = cast<DeclRefExpr>(SimpleRefExpr); 15806 Expr *CapturedRef; 15807 if (LinKind == OMPC_LINEAR_uval) 15808 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit(); 15809 else 15810 CapturedRef = 15811 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()), 15812 DE->getType().getUnqualifiedType(), DE->getExprLoc(), 15813 /*RefersToCapture=*/true); 15814 15815 // Build update: Var = InitExpr + IV * Step 15816 ExprResult Update; 15817 if (!Info.first) 15818 Update = buildCounterUpdate( 15819 SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, InitExpr, IV, Step, 15820 /*Subtract=*/false, /*IsNonRectangularLB=*/false); 15821 else 15822 Update = *CurPrivate; 15823 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(), 15824 /*DiscardedValue*/ false); 15825 15826 // Build final: Var = InitExpr + NumIterations * Step 15827 ExprResult Final; 15828 if (!Info.first) 15829 Final = 15830 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef, 15831 InitExpr, NumIterations, Step, /*Subtract=*/false, 15832 /*IsNonRectangularLB=*/false); 15833 else 15834 Final = *CurPrivate; 15835 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(), 15836 /*DiscardedValue*/ false); 15837 15838 if (!Update.isUsable() || !Final.isUsable()) { 15839 Updates.push_back(nullptr); 15840 Finals.push_back(nullptr); 15841 UsedExprs.push_back(nullptr); 15842 HasErrors = true; 15843 } else { 15844 Updates.push_back(Update.get()); 15845 Finals.push_back(Final.get()); 15846 if (!Info.first) 15847 UsedExprs.push_back(SimpleRefExpr); 15848 } 15849 ++CurInit; 15850 ++CurPrivate; 15851 } 15852 if (Expr *S = Clause.getStep()) 15853 UsedExprs.push_back(S); 15854 // Fill the remaining part with the nullptr. 15855 UsedExprs.append(Clause.varlist_size() + 1 - UsedExprs.size(), nullptr); 15856 Clause.setUpdates(Updates); 15857 Clause.setFinals(Finals); 15858 Clause.setUsedExprs(UsedExprs); 15859 return HasErrors; 15860 } 15861 15862 OMPClause *Sema::ActOnOpenMPAlignedClause( 15863 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 15864 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 15865 SmallVector<Expr *, 8> Vars; 15866 for (Expr *RefExpr : VarList) { 15867 assert(RefExpr && "NULL expr in OpenMP linear clause."); 15868 SourceLocation ELoc; 15869 SourceRange ERange; 15870 Expr *SimpleRefExpr = RefExpr; 15871 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 15872 if (Res.second) { 15873 // It will be analyzed later. 15874 Vars.push_back(RefExpr); 15875 } 15876 ValueDecl *D = Res.first; 15877 if (!D) 15878 continue; 15879 15880 QualType QType = D->getType(); 15881 auto *VD = dyn_cast<VarDecl>(D); 15882 15883 // OpenMP [2.8.1, simd construct, Restrictions] 15884 // The type of list items appearing in the aligned clause must be 15885 // array, pointer, reference to array, or reference to pointer. 15886 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType(); 15887 const Type *Ty = QType.getTypePtrOrNull(); 15888 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) { 15889 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 15890 << QType << getLangOpts().CPlusPlus << ERange; 15891 bool IsDecl = 15892 !VD || 15893 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 15894 Diag(D->getLocation(), 15895 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 15896 << D; 15897 continue; 15898 } 15899 15900 // OpenMP [2.8.1, simd construct, Restrictions] 15901 // A list-item cannot appear in more than one aligned clause. 15902 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) { 15903 Diag(ELoc, diag::err_omp_used_in_clause_twice) 15904 << 0 << getOpenMPClauseName(OMPC_aligned) << ERange; 15905 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 15906 << getOpenMPClauseName(OMPC_aligned); 15907 continue; 15908 } 15909 15910 DeclRefExpr *Ref = nullptr; 15911 if (!VD && isOpenMPCapturedDecl(D)) 15912 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 15913 Vars.push_back(DefaultFunctionArrayConversion( 15914 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref) 15915 .get()); 15916 } 15917 15918 // OpenMP [2.8.1, simd construct, Description] 15919 // The parameter of the aligned clause, alignment, must be a constant 15920 // positive integer expression. 15921 // If no optional parameter is specified, implementation-defined default 15922 // alignments for SIMD instructions on the target platforms are assumed. 15923 if (Alignment != nullptr) { 15924 ExprResult AlignResult = 15925 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 15926 if (AlignResult.isInvalid()) 15927 return nullptr; 15928 Alignment = AlignResult.get(); 15929 } 15930 if (Vars.empty()) 15931 return nullptr; 15932 15933 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 15934 EndLoc, Vars, Alignment); 15935 } 15936 15937 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 15938 SourceLocation StartLoc, 15939 SourceLocation LParenLoc, 15940 SourceLocation EndLoc) { 15941 SmallVector<Expr *, 8> Vars; 15942 SmallVector<Expr *, 8> SrcExprs; 15943 SmallVector<Expr *, 8> DstExprs; 15944 SmallVector<Expr *, 8> AssignmentOps; 15945 for (Expr *RefExpr : VarList) { 15946 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 15947 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 15948 // It will be analyzed later. 15949 Vars.push_back(RefExpr); 15950 SrcExprs.push_back(nullptr); 15951 DstExprs.push_back(nullptr); 15952 AssignmentOps.push_back(nullptr); 15953 continue; 15954 } 15955 15956 SourceLocation ELoc = RefExpr->getExprLoc(); 15957 // OpenMP [2.1, C/C++] 15958 // A list item is a variable name. 15959 // OpenMP [2.14.4.1, Restrictions, p.1] 15960 // A list item that appears in a copyin clause must be threadprivate. 15961 auto *DE = dyn_cast<DeclRefExpr>(RefExpr); 15962 if (!DE || !isa<VarDecl>(DE->getDecl())) { 15963 Diag(ELoc, diag::err_omp_expected_var_name_member_expr) 15964 << 0 << RefExpr->getSourceRange(); 15965 continue; 15966 } 15967 15968 Decl *D = DE->getDecl(); 15969 auto *VD = cast<VarDecl>(D); 15970 15971 QualType Type = VD->getType(); 15972 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 15973 // It will be analyzed later. 15974 Vars.push_back(DE); 15975 SrcExprs.push_back(nullptr); 15976 DstExprs.push_back(nullptr); 15977 AssignmentOps.push_back(nullptr); 15978 continue; 15979 } 15980 15981 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 15982 // A list item that appears in a copyin clause must be threadprivate. 15983 if (!DSAStack->isThreadPrivate(VD)) { 15984 Diag(ELoc, diag::err_omp_required_access) 15985 << getOpenMPClauseName(OMPC_copyin) 15986 << getOpenMPDirectiveName(OMPD_threadprivate); 15987 continue; 15988 } 15989 15990 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 15991 // A variable of class type (or array thereof) that appears in a 15992 // copyin clause requires an accessible, unambiguous copy assignment 15993 // operator for the class type. 15994 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType(); 15995 VarDecl *SrcVD = 15996 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(), 15997 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr); 15998 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr( 15999 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc()); 16000 VarDecl *DstVD = 16001 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst", 16002 VD->hasAttrs() ? &VD->getAttrs() : nullptr); 16003 DeclRefExpr *PseudoDstExpr = 16004 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc()); 16005 // For arrays generate assignment operation for single element and replace 16006 // it by the original array element in CodeGen. 16007 ExprResult AssignmentOp = 16008 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr, 16009 PseudoSrcExpr); 16010 if (AssignmentOp.isInvalid()) 16011 continue; 16012 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(), 16013 /*DiscardedValue*/ false); 16014 if (AssignmentOp.isInvalid()) 16015 continue; 16016 16017 DSAStack->addDSA(VD, DE, OMPC_copyin); 16018 Vars.push_back(DE); 16019 SrcExprs.push_back(PseudoSrcExpr); 16020 DstExprs.push_back(PseudoDstExpr); 16021 AssignmentOps.push_back(AssignmentOp.get()); 16022 } 16023 16024 if (Vars.empty()) 16025 return nullptr; 16026 16027 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars, 16028 SrcExprs, DstExprs, AssignmentOps); 16029 } 16030 16031 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList, 16032 SourceLocation StartLoc, 16033 SourceLocation LParenLoc, 16034 SourceLocation EndLoc) { 16035 SmallVector<Expr *, 8> Vars; 16036 SmallVector<Expr *, 8> SrcExprs; 16037 SmallVector<Expr *, 8> DstExprs; 16038 SmallVector<Expr *, 8> AssignmentOps; 16039 for (Expr *RefExpr : VarList) { 16040 assert(RefExpr && "NULL expr in OpenMP linear clause."); 16041 SourceLocation ELoc; 16042 SourceRange ERange; 16043 Expr *SimpleRefExpr = RefExpr; 16044 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 16045 if (Res.second) { 16046 // It will be analyzed later. 16047 Vars.push_back(RefExpr); 16048 SrcExprs.push_back(nullptr); 16049 DstExprs.push_back(nullptr); 16050 AssignmentOps.push_back(nullptr); 16051 } 16052 ValueDecl *D = Res.first; 16053 if (!D) 16054 continue; 16055 16056 QualType Type = D->getType(); 16057 auto *VD = dyn_cast<VarDecl>(D); 16058 16059 // OpenMP [2.14.4.2, Restrictions, p.2] 16060 // A list item that appears in a copyprivate clause may not appear in a 16061 // private or firstprivate clause on the single construct. 16062 if (!VD || !DSAStack->isThreadPrivate(VD)) { 16063 DSAStackTy::DSAVarData DVar = 16064 DSAStack->getTopDSA(D, /*FromParent=*/false); 16065 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate && 16066 DVar.RefExpr) { 16067 Diag(ELoc, diag::err_omp_wrong_dsa) 16068 << getOpenMPClauseName(DVar.CKind) 16069 << getOpenMPClauseName(OMPC_copyprivate); 16070 reportOriginalDsa(*this, DSAStack, D, DVar); 16071 continue; 16072 } 16073 16074 // OpenMP [2.11.4.2, Restrictions, p.1] 16075 // All list items that appear in a copyprivate clause must be either 16076 // threadprivate or private in the enclosing context. 16077 if (DVar.CKind == OMPC_unknown) { 16078 DVar = DSAStack->getImplicitDSA(D, false); 16079 if (DVar.CKind == OMPC_shared) { 16080 Diag(ELoc, diag::err_omp_required_access) 16081 << getOpenMPClauseName(OMPC_copyprivate) 16082 << "threadprivate or private in the enclosing context"; 16083 reportOriginalDsa(*this, DSAStack, D, DVar); 16084 continue; 16085 } 16086 } 16087 } 16088 16089 // Variably modified types are not supported. 16090 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) { 16091 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported) 16092 << getOpenMPClauseName(OMPC_copyprivate) << Type 16093 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 16094 bool IsDecl = 16095 !VD || 16096 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 16097 Diag(D->getLocation(), 16098 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 16099 << D; 16100 continue; 16101 } 16102 16103 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 16104 // A variable of class type (or array thereof) that appears in a 16105 // copyin clause requires an accessible, unambiguous copy assignment 16106 // operator for the class type. 16107 Type = Context.getBaseElementType(Type.getNonReferenceType()) 16108 .getUnqualifiedType(); 16109 VarDecl *SrcVD = 16110 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src", 16111 D->hasAttrs() ? &D->getAttrs() : nullptr); 16112 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc); 16113 VarDecl *DstVD = 16114 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst", 16115 D->hasAttrs() ? &D->getAttrs() : nullptr); 16116 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc); 16117 ExprResult AssignmentOp = BuildBinOp( 16118 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr); 16119 if (AssignmentOp.isInvalid()) 16120 continue; 16121 AssignmentOp = 16122 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false); 16123 if (AssignmentOp.isInvalid()) 16124 continue; 16125 16126 // No need to mark vars as copyprivate, they are already threadprivate or 16127 // implicitly private. 16128 assert(VD || isOpenMPCapturedDecl(D)); 16129 Vars.push_back( 16130 VD ? RefExpr->IgnoreParens() 16131 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false)); 16132 SrcExprs.push_back(PseudoSrcExpr); 16133 DstExprs.push_back(PseudoDstExpr); 16134 AssignmentOps.push_back(AssignmentOp.get()); 16135 } 16136 16137 if (Vars.empty()) 16138 return nullptr; 16139 16140 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 16141 Vars, SrcExprs, DstExprs, AssignmentOps); 16142 } 16143 16144 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList, 16145 SourceLocation StartLoc, 16146 SourceLocation LParenLoc, 16147 SourceLocation EndLoc) { 16148 if (VarList.empty()) 16149 return nullptr; 16150 16151 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList); 16152 } 16153 16154 /// Tries to find omp_depend_t. type. 16155 static bool findOMPDependT(Sema &S, SourceLocation Loc, DSAStackTy *Stack, 16156 bool Diagnose = true) { 16157 QualType OMPDependT = Stack->getOMPDependT(); 16158 if (!OMPDependT.isNull()) 16159 return true; 16160 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_depend_t"); 16161 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 16162 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 16163 if (Diagnose) 16164 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_depend_t"; 16165 return false; 16166 } 16167 Stack->setOMPDependT(PT.get()); 16168 return true; 16169 } 16170 16171 OMPClause *Sema::ActOnOpenMPDepobjClause(Expr *Depobj, SourceLocation StartLoc, 16172 SourceLocation LParenLoc, 16173 SourceLocation EndLoc) { 16174 if (!Depobj) 16175 return nullptr; 16176 16177 bool OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack); 16178 16179 // OpenMP 5.0, 2.17.10.1 depobj Construct 16180 // depobj is an lvalue expression of type omp_depend_t. 16181 if (!Depobj->isTypeDependent() && !Depobj->isValueDependent() && 16182 !Depobj->isInstantiationDependent() && 16183 !Depobj->containsUnexpandedParameterPack() && 16184 (OMPDependTFound && 16185 !Context.typesAreCompatible(DSAStack->getOMPDependT(), Depobj->getType(), 16186 /*CompareUnqualified=*/true))) { 16187 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 16188 << 0 << Depobj->getType() << Depobj->getSourceRange(); 16189 } 16190 16191 if (!Depobj->isLValue()) { 16192 Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue) 16193 << 1 << Depobj->getSourceRange(); 16194 } 16195 16196 return OMPDepobjClause::Create(Context, StartLoc, LParenLoc, EndLoc, Depobj); 16197 } 16198 16199 OMPClause * 16200 Sema::ActOnOpenMPDependClause(Expr *DepModifier, OpenMPDependClauseKind DepKind, 16201 SourceLocation DepLoc, SourceLocation ColonLoc, 16202 ArrayRef<Expr *> VarList, SourceLocation StartLoc, 16203 SourceLocation LParenLoc, SourceLocation EndLoc) { 16204 if (DSAStack->getCurrentDirective() == OMPD_ordered && 16205 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) { 16206 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 16207 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend); 16208 return nullptr; 16209 } 16210 if ((DSAStack->getCurrentDirective() != OMPD_ordered || 16211 DSAStack->getCurrentDirective() == OMPD_depobj) && 16212 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source || 16213 DepKind == OMPC_DEPEND_sink || 16214 ((LangOpts.OpenMP < 50 || 16215 DSAStack->getCurrentDirective() == OMPD_depobj) && 16216 DepKind == OMPC_DEPEND_depobj))) { 16217 SmallVector<unsigned, 3> Except; 16218 Except.push_back(OMPC_DEPEND_source); 16219 Except.push_back(OMPC_DEPEND_sink); 16220 if (LangOpts.OpenMP < 50 || DSAStack->getCurrentDirective() == OMPD_depobj) 16221 Except.push_back(OMPC_DEPEND_depobj); 16222 std::string Expected = (LangOpts.OpenMP >= 50 && !DepModifier) 16223 ? "depend modifier(iterator) or " 16224 : ""; 16225 Diag(DepLoc, diag::err_omp_unexpected_clause_value) 16226 << Expected + getListOfPossibleValues(OMPC_depend, /*First=*/0, 16227 /*Last=*/OMPC_DEPEND_unknown, 16228 Except) 16229 << getOpenMPClauseName(OMPC_depend); 16230 return nullptr; 16231 } 16232 if (DepModifier && 16233 (DepKind == OMPC_DEPEND_source || DepKind == OMPC_DEPEND_sink)) { 16234 Diag(DepModifier->getExprLoc(), 16235 diag::err_omp_depend_sink_source_with_modifier); 16236 return nullptr; 16237 } 16238 if (DepModifier && 16239 !DepModifier->getType()->isSpecificBuiltinType(BuiltinType::OMPIterator)) 16240 Diag(DepModifier->getExprLoc(), diag::err_omp_depend_modifier_not_iterator); 16241 16242 SmallVector<Expr *, 8> Vars; 16243 DSAStackTy::OperatorOffsetTy OpsOffs; 16244 llvm::APSInt DepCounter(/*BitWidth=*/32); 16245 llvm::APSInt TotalDepCount(/*BitWidth=*/32); 16246 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) { 16247 if (const Expr *OrderedCountExpr = 16248 DSAStack->getParentOrderedRegionParam().first) { 16249 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context); 16250 TotalDepCount.setIsUnsigned(/*Val=*/true); 16251 } 16252 } 16253 for (Expr *RefExpr : VarList) { 16254 assert(RefExpr && "NULL expr in OpenMP shared clause."); 16255 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 16256 // It will be analyzed later. 16257 Vars.push_back(RefExpr); 16258 continue; 16259 } 16260 16261 SourceLocation ELoc = RefExpr->getExprLoc(); 16262 Expr *SimpleExpr = RefExpr->IgnoreParenCasts(); 16263 if (DepKind == OMPC_DEPEND_sink) { 16264 if (DSAStack->getParentOrderedRegionParam().first && 16265 DepCounter >= TotalDepCount) { 16266 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr); 16267 continue; 16268 } 16269 ++DepCounter; 16270 // OpenMP [2.13.9, Summary] 16271 // depend(dependence-type : vec), where dependence-type is: 16272 // 'sink' and where vec is the iteration vector, which has the form: 16273 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn] 16274 // where n is the value specified by the ordered clause in the loop 16275 // directive, xi denotes the loop iteration variable of the i-th nested 16276 // loop associated with the loop directive, and di is a constant 16277 // non-negative integer. 16278 if (CurContext->isDependentContext()) { 16279 // It will be analyzed later. 16280 Vars.push_back(RefExpr); 16281 continue; 16282 } 16283 SimpleExpr = SimpleExpr->IgnoreImplicit(); 16284 OverloadedOperatorKind OOK = OO_None; 16285 SourceLocation OOLoc; 16286 Expr *LHS = SimpleExpr; 16287 Expr *RHS = nullptr; 16288 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) { 16289 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 16290 OOLoc = BO->getOperatorLoc(); 16291 LHS = BO->getLHS()->IgnoreParenImpCasts(); 16292 RHS = BO->getRHS()->IgnoreParenImpCasts(); 16293 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) { 16294 OOK = OCE->getOperator(); 16295 OOLoc = OCE->getOperatorLoc(); 16296 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 16297 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 16298 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) { 16299 OOK = MCE->getMethodDecl() 16300 ->getNameInfo() 16301 .getName() 16302 .getCXXOverloadedOperator(); 16303 OOLoc = MCE->getCallee()->getExprLoc(); 16304 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts(); 16305 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 16306 } 16307 SourceLocation ELoc; 16308 SourceRange ERange; 16309 auto Res = getPrivateItem(*this, LHS, ELoc, ERange); 16310 if (Res.second) { 16311 // It will be analyzed later. 16312 Vars.push_back(RefExpr); 16313 } 16314 ValueDecl *D = Res.first; 16315 if (!D) 16316 continue; 16317 16318 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) { 16319 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus); 16320 continue; 16321 } 16322 if (RHS) { 16323 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause( 16324 RHS, OMPC_depend, /*StrictlyPositive=*/false); 16325 if (RHSRes.isInvalid()) 16326 continue; 16327 } 16328 if (!CurContext->isDependentContext() && 16329 DSAStack->getParentOrderedRegionParam().first && 16330 DepCounter != DSAStack->isParentLoopControlVariable(D).first) { 16331 const ValueDecl *VD = 16332 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue()); 16333 if (VD) 16334 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) 16335 << 1 << VD; 16336 else 16337 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0; 16338 continue; 16339 } 16340 OpsOffs.emplace_back(RHS, OOK); 16341 } else { 16342 bool OMPDependTFound = LangOpts.OpenMP >= 50; 16343 if (OMPDependTFound) 16344 OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack, 16345 DepKind == OMPC_DEPEND_depobj); 16346 if (DepKind == OMPC_DEPEND_depobj) { 16347 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 16348 // List items used in depend clauses with the depobj dependence type 16349 // must be expressions of the omp_depend_t type. 16350 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 16351 !RefExpr->isInstantiationDependent() && 16352 !RefExpr->containsUnexpandedParameterPack() && 16353 (OMPDependTFound && 16354 !Context.hasSameUnqualifiedType(DSAStack->getOMPDependT(), 16355 RefExpr->getType()))) { 16356 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 16357 << 0 << RefExpr->getType() << RefExpr->getSourceRange(); 16358 continue; 16359 } 16360 if (!RefExpr->isLValue()) { 16361 Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue) 16362 << 1 << RefExpr->getType() << RefExpr->getSourceRange(); 16363 continue; 16364 } 16365 } else { 16366 // OpenMP 5.0 [2.17.11, Restrictions] 16367 // List items used in depend clauses cannot be zero-length array 16368 // sections. 16369 QualType ExprTy = RefExpr->getType().getNonReferenceType(); 16370 const auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr); 16371 if (OASE) { 16372 QualType BaseType = 16373 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 16374 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 16375 ExprTy = ATy->getElementType(); 16376 else 16377 ExprTy = BaseType->getPointeeType(); 16378 ExprTy = ExprTy.getNonReferenceType(); 16379 const Expr *Length = OASE->getLength(); 16380 Expr::EvalResult Result; 16381 if (Length && !Length->isValueDependent() && 16382 Length->EvaluateAsInt(Result, Context) && 16383 Result.Val.getInt().isNullValue()) { 16384 Diag(ELoc, 16385 diag::err_omp_depend_zero_length_array_section_not_allowed) 16386 << SimpleExpr->getSourceRange(); 16387 continue; 16388 } 16389 } 16390 16391 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++ 16392 // List items used in depend clauses with the in, out, inout or 16393 // mutexinoutset dependence types cannot be expressions of the 16394 // omp_depend_t type. 16395 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() && 16396 !RefExpr->isInstantiationDependent() && 16397 !RefExpr->containsUnexpandedParameterPack() && 16398 (OMPDependTFound && 16399 DSAStack->getOMPDependT().getTypePtr() == ExprTy.getTypePtr())) { 16400 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 16401 << (LangOpts.OpenMP >= 50 ? 1 : 0) << 1 16402 << RefExpr->getSourceRange(); 16403 continue; 16404 } 16405 16406 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr); 16407 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || 16408 (ASE && !ASE->getBase()->isTypeDependent() && 16409 !ASE->getBase() 16410 ->getType() 16411 .getNonReferenceType() 16412 ->isPointerType() && 16413 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) { 16414 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 16415 << (LangOpts.OpenMP >= 50 ? 1 : 0) 16416 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 16417 continue; 16418 } 16419 16420 ExprResult Res; 16421 { 16422 Sema::TentativeAnalysisScope Trap(*this); 16423 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, 16424 RefExpr->IgnoreParenImpCasts()); 16425 } 16426 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 16427 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 16428 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 16429 << (LangOpts.OpenMP >= 50 ? 1 : 0) 16430 << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange(); 16431 continue; 16432 } 16433 } 16434 } 16435 Vars.push_back(RefExpr->IgnoreParenImpCasts()); 16436 } 16437 16438 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink && 16439 TotalDepCount > VarList.size() && 16440 DSAStack->getParentOrderedRegionParam().first && 16441 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) { 16442 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) 16443 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1); 16444 } 16445 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink && 16446 Vars.empty()) 16447 return nullptr; 16448 16449 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, 16450 DepModifier, DepKind, DepLoc, ColonLoc, 16451 Vars, TotalDepCount.getZExtValue()); 16452 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) && 16453 DSAStack->isParentOrderedRegion()) 16454 DSAStack->addDoacrossDependClause(C, OpsOffs); 16455 return C; 16456 } 16457 16458 OMPClause *Sema::ActOnOpenMPDeviceClause(OpenMPDeviceClauseModifier Modifier, 16459 Expr *Device, SourceLocation StartLoc, 16460 SourceLocation LParenLoc, 16461 SourceLocation ModifierLoc, 16462 SourceLocation EndLoc) { 16463 assert((ModifierLoc.isInvalid() || LangOpts.OpenMP >= 50) && 16464 "Unexpected device modifier in OpenMP < 50."); 16465 16466 bool ErrorFound = false; 16467 if (ModifierLoc.isValid() && Modifier == OMPC_DEVICE_unknown) { 16468 std::string Values = 16469 getListOfPossibleValues(OMPC_device, /*First=*/0, OMPC_DEVICE_unknown); 16470 Diag(ModifierLoc, diag::err_omp_unexpected_clause_value) 16471 << Values << getOpenMPClauseName(OMPC_device); 16472 ErrorFound = true; 16473 } 16474 16475 Expr *ValExpr = Device; 16476 Stmt *HelperValStmt = nullptr; 16477 16478 // OpenMP [2.9.1, Restrictions] 16479 // The device expression must evaluate to a non-negative integer value. 16480 ErrorFound = !isNonNegativeIntegerValue(ValExpr, *this, OMPC_device, 16481 /*StrictlyPositive=*/false) || 16482 ErrorFound; 16483 if (ErrorFound) 16484 return nullptr; 16485 16486 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 16487 OpenMPDirectiveKind CaptureRegion = 16488 getOpenMPCaptureRegionForClause(DKind, OMPC_device, LangOpts.OpenMP); 16489 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 16490 ValExpr = MakeFullExpr(ValExpr).get(); 16491 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 16492 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 16493 HelperValStmt = buildPreInits(Context, Captures); 16494 } 16495 16496 return new (Context) 16497 OMPDeviceClause(Modifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc, 16498 LParenLoc, ModifierLoc, EndLoc); 16499 } 16500 16501 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef, 16502 DSAStackTy *Stack, QualType QTy, 16503 bool FullCheck = true) { 16504 NamedDecl *ND; 16505 if (QTy->isIncompleteType(&ND)) { 16506 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR; 16507 return false; 16508 } 16509 if (FullCheck && !SemaRef.CurContext->isDependentContext() && 16510 !QTy.isTriviallyCopyableType(SemaRef.Context)) 16511 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR; 16512 return true; 16513 } 16514 16515 /// Return true if it can be proven that the provided array expression 16516 /// (array section or array subscript) does NOT specify the whole size of the 16517 /// array whose base type is \a BaseQTy. 16518 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef, 16519 const Expr *E, 16520 QualType BaseQTy) { 16521 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 16522 16523 // If this is an array subscript, it refers to the whole size if the size of 16524 // the dimension is constant and equals 1. Also, an array section assumes the 16525 // format of an array subscript if no colon is used. 16526 if (isa<ArraySubscriptExpr>(E) || 16527 (OASE && OASE->getColonLocFirst().isInvalid())) { 16528 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 16529 return ATy->getSize().getSExtValue() != 1; 16530 // Size can't be evaluated statically. 16531 return false; 16532 } 16533 16534 assert(OASE && "Expecting array section if not an array subscript."); 16535 const Expr *LowerBound = OASE->getLowerBound(); 16536 const Expr *Length = OASE->getLength(); 16537 16538 // If there is a lower bound that does not evaluates to zero, we are not 16539 // covering the whole dimension. 16540 if (LowerBound) { 16541 Expr::EvalResult Result; 16542 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext())) 16543 return false; // Can't get the integer value as a constant. 16544 16545 llvm::APSInt ConstLowerBound = Result.Val.getInt(); 16546 if (ConstLowerBound.getSExtValue()) 16547 return true; 16548 } 16549 16550 // If we don't have a length we covering the whole dimension. 16551 if (!Length) 16552 return false; 16553 16554 // If the base is a pointer, we don't have a way to get the size of the 16555 // pointee. 16556 if (BaseQTy->isPointerType()) 16557 return false; 16558 16559 // We can only check if the length is the same as the size of the dimension 16560 // if we have a constant array. 16561 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()); 16562 if (!CATy) 16563 return false; 16564 16565 Expr::EvalResult Result; 16566 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 16567 return false; // Can't get the integer value as a constant. 16568 16569 llvm::APSInt ConstLength = Result.Val.getInt(); 16570 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue(); 16571 } 16572 16573 // Return true if it can be proven that the provided array expression (array 16574 // section or array subscript) does NOT specify a single element of the array 16575 // whose base type is \a BaseQTy. 16576 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef, 16577 const Expr *E, 16578 QualType BaseQTy) { 16579 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E); 16580 16581 // An array subscript always refer to a single element. Also, an array section 16582 // assumes the format of an array subscript if no colon is used. 16583 if (isa<ArraySubscriptExpr>(E) || 16584 (OASE && OASE->getColonLocFirst().isInvalid())) 16585 return false; 16586 16587 assert(OASE && "Expecting array section if not an array subscript."); 16588 const Expr *Length = OASE->getLength(); 16589 16590 // If we don't have a length we have to check if the array has unitary size 16591 // for this dimension. Also, we should always expect a length if the base type 16592 // is pointer. 16593 if (!Length) { 16594 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr())) 16595 return ATy->getSize().getSExtValue() != 1; 16596 // We cannot assume anything. 16597 return false; 16598 } 16599 16600 // Check if the length evaluates to 1. 16601 Expr::EvalResult Result; 16602 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext())) 16603 return false; // Can't get the integer value as a constant. 16604 16605 llvm::APSInt ConstLength = Result.Val.getInt(); 16606 return ConstLength.getSExtValue() != 1; 16607 } 16608 16609 // The base of elements of list in a map clause have to be either: 16610 // - a reference to variable or field. 16611 // - a member expression. 16612 // - an array expression. 16613 // 16614 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the 16615 // reference to 'r'. 16616 // 16617 // If we have: 16618 // 16619 // struct SS { 16620 // Bla S; 16621 // foo() { 16622 // #pragma omp target map (S.Arr[:12]); 16623 // } 16624 // } 16625 // 16626 // We want to retrieve the member expression 'this->S'; 16627 16628 // OpenMP 5.0 [2.19.7.1, map Clause, Restrictions, p.2] 16629 // If a list item is an array section, it must specify contiguous storage. 16630 // 16631 // For this restriction it is sufficient that we make sure only references 16632 // to variables or fields and array expressions, and that no array sections 16633 // exist except in the rightmost expression (unless they cover the whole 16634 // dimension of the array). E.g. these would be invalid: 16635 // 16636 // r.ArrS[3:5].Arr[6:7] 16637 // 16638 // r.ArrS[3:5].x 16639 // 16640 // but these would be valid: 16641 // r.ArrS[3].Arr[6:7] 16642 // 16643 // r.ArrS[3].x 16644 namespace { 16645 class MapBaseChecker final : public StmtVisitor<MapBaseChecker, bool> { 16646 Sema &SemaRef; 16647 OpenMPClauseKind CKind = OMPC_unknown; 16648 OMPClauseMappableExprCommon::MappableExprComponentList &Components; 16649 bool NoDiagnose = false; 16650 const Expr *RelevantExpr = nullptr; 16651 bool AllowUnitySizeArraySection = true; 16652 bool AllowWholeSizeArraySection = true; 16653 SourceLocation ELoc; 16654 SourceRange ERange; 16655 16656 void emitErrorMsg() { 16657 // If nothing else worked, this is not a valid map clause expression. 16658 if (SemaRef.getLangOpts().OpenMP < 50) { 16659 SemaRef.Diag(ELoc, 16660 diag::err_omp_expected_named_var_member_or_array_expression) 16661 << ERange; 16662 } else { 16663 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 16664 << getOpenMPClauseName(CKind) << ERange; 16665 } 16666 } 16667 16668 public: 16669 bool VisitDeclRefExpr(DeclRefExpr *DRE) { 16670 if (!isa<VarDecl>(DRE->getDecl())) { 16671 emitErrorMsg(); 16672 return false; 16673 } 16674 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 16675 RelevantExpr = DRE; 16676 // Record the component. 16677 Components.emplace_back(DRE, DRE->getDecl()); 16678 return true; 16679 } 16680 16681 bool VisitMemberExpr(MemberExpr *ME) { 16682 Expr *E = ME; 16683 Expr *BaseE = ME->getBase()->IgnoreParenCasts(); 16684 16685 if (isa<CXXThisExpr>(BaseE)) { 16686 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 16687 // We found a base expression: this->Val. 16688 RelevantExpr = ME; 16689 } else { 16690 E = BaseE; 16691 } 16692 16693 if (!isa<FieldDecl>(ME->getMemberDecl())) { 16694 if (!NoDiagnose) { 16695 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field) 16696 << ME->getSourceRange(); 16697 return false; 16698 } 16699 if (RelevantExpr) 16700 return false; 16701 return Visit(E); 16702 } 16703 16704 auto *FD = cast<FieldDecl>(ME->getMemberDecl()); 16705 16706 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3] 16707 // A bit-field cannot appear in a map clause. 16708 // 16709 if (FD->isBitField()) { 16710 if (!NoDiagnose) { 16711 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause) 16712 << ME->getSourceRange() << getOpenMPClauseName(CKind); 16713 return false; 16714 } 16715 if (RelevantExpr) 16716 return false; 16717 return Visit(E); 16718 } 16719 16720 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 16721 // If the type of a list item is a reference to a type T then the type 16722 // will be considered to be T for all purposes of this clause. 16723 QualType CurType = BaseE->getType().getNonReferenceType(); 16724 16725 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2] 16726 // A list item cannot be a variable that is a member of a structure with 16727 // a union type. 16728 // 16729 if (CurType->isUnionType()) { 16730 if (!NoDiagnose) { 16731 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed) 16732 << ME->getSourceRange(); 16733 return false; 16734 } 16735 return RelevantExpr || Visit(E); 16736 } 16737 16738 // If we got a member expression, we should not expect any array section 16739 // before that: 16740 // 16741 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7] 16742 // If a list item is an element of a structure, only the rightmost symbol 16743 // of the variable reference can be an array section. 16744 // 16745 AllowUnitySizeArraySection = false; 16746 AllowWholeSizeArraySection = false; 16747 16748 // Record the component. 16749 Components.emplace_back(ME, FD); 16750 return RelevantExpr || Visit(E); 16751 } 16752 16753 bool VisitArraySubscriptExpr(ArraySubscriptExpr *AE) { 16754 Expr *E = AE->getBase()->IgnoreParenImpCasts(); 16755 16756 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) { 16757 if (!NoDiagnose) { 16758 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 16759 << 0 << AE->getSourceRange(); 16760 return false; 16761 } 16762 return RelevantExpr || Visit(E); 16763 } 16764 16765 // If we got an array subscript that express the whole dimension we 16766 // can have any array expressions before. If it only expressing part of 16767 // the dimension, we can only have unitary-size array expressions. 16768 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, AE, 16769 E->getType())) 16770 AllowWholeSizeArraySection = false; 16771 16772 if (const auto *TE = dyn_cast<CXXThisExpr>(E->IgnoreParenCasts())) { 16773 Expr::EvalResult Result; 16774 if (!AE->getIdx()->isValueDependent() && 16775 AE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext()) && 16776 !Result.Val.getInt().isNullValue()) { 16777 SemaRef.Diag(AE->getIdx()->getExprLoc(), 16778 diag::err_omp_invalid_map_this_expr); 16779 SemaRef.Diag(AE->getIdx()->getExprLoc(), 16780 diag::note_omp_invalid_subscript_on_this_ptr_map); 16781 } 16782 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 16783 RelevantExpr = TE; 16784 } 16785 16786 // Record the component - we don't have any declaration associated. 16787 Components.emplace_back(AE, nullptr); 16788 16789 return RelevantExpr || Visit(E); 16790 } 16791 16792 bool VisitOMPArraySectionExpr(OMPArraySectionExpr *OASE) { 16793 assert(!NoDiagnose && "Array sections cannot be implicitly mapped."); 16794 Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 16795 QualType CurType = 16796 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 16797 16798 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 16799 // If the type of a list item is a reference to a type T then the type 16800 // will be considered to be T for all purposes of this clause. 16801 if (CurType->isReferenceType()) 16802 CurType = CurType->getPointeeType(); 16803 16804 bool IsPointer = CurType->isAnyPointerType(); 16805 16806 if (!IsPointer && !CurType->isArrayType()) { 16807 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name) 16808 << 0 << OASE->getSourceRange(); 16809 return false; 16810 } 16811 16812 bool NotWhole = 16813 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, OASE, CurType); 16814 bool NotUnity = 16815 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, OASE, CurType); 16816 16817 if (AllowWholeSizeArraySection) { 16818 // Any array section is currently allowed. Allowing a whole size array 16819 // section implies allowing a unity array section as well. 16820 // 16821 // If this array section refers to the whole dimension we can still 16822 // accept other array sections before this one, except if the base is a 16823 // pointer. Otherwise, only unitary sections are accepted. 16824 if (NotWhole || IsPointer) 16825 AllowWholeSizeArraySection = false; 16826 } else if (AllowUnitySizeArraySection && NotUnity) { 16827 // A unity or whole array section is not allowed and that is not 16828 // compatible with the properties of the current array section. 16829 SemaRef.Diag( 16830 ELoc, diag::err_array_section_does_not_specify_contiguous_storage) 16831 << OASE->getSourceRange(); 16832 return false; 16833 } 16834 16835 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) { 16836 Expr::EvalResult ResultR; 16837 Expr::EvalResult ResultL; 16838 if (!OASE->getLength()->isValueDependent() && 16839 OASE->getLength()->EvaluateAsInt(ResultR, SemaRef.getASTContext()) && 16840 !ResultR.Val.getInt().isOneValue()) { 16841 SemaRef.Diag(OASE->getLength()->getExprLoc(), 16842 diag::err_omp_invalid_map_this_expr); 16843 SemaRef.Diag(OASE->getLength()->getExprLoc(), 16844 diag::note_omp_invalid_length_on_this_ptr_mapping); 16845 } 16846 if (OASE->getLowerBound() && !OASE->getLowerBound()->isValueDependent() && 16847 OASE->getLowerBound()->EvaluateAsInt(ResultL, 16848 SemaRef.getASTContext()) && 16849 !ResultL.Val.getInt().isNullValue()) { 16850 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 16851 diag::err_omp_invalid_map_this_expr); 16852 SemaRef.Diag(OASE->getLowerBound()->getExprLoc(), 16853 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping); 16854 } 16855 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 16856 RelevantExpr = TE; 16857 } 16858 16859 // Record the component - we don't have any declaration associated. 16860 Components.emplace_back(OASE, nullptr); 16861 return RelevantExpr || Visit(E); 16862 } 16863 bool VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) { 16864 Expr *Base = E->getBase(); 16865 16866 // Record the component - we don't have any declaration associated. 16867 Components.emplace_back(E, nullptr); 16868 16869 return Visit(Base->IgnoreParenImpCasts()); 16870 } 16871 16872 bool VisitUnaryOperator(UnaryOperator *UO) { 16873 if (SemaRef.getLangOpts().OpenMP < 50 || !UO->isLValue() || 16874 UO->getOpcode() != UO_Deref) { 16875 emitErrorMsg(); 16876 return false; 16877 } 16878 if (!RelevantExpr) { 16879 // Record the component if haven't found base decl. 16880 Components.emplace_back(UO, nullptr); 16881 } 16882 return RelevantExpr || Visit(UO->getSubExpr()->IgnoreParenImpCasts()); 16883 } 16884 bool VisitBinaryOperator(BinaryOperator *BO) { 16885 if (SemaRef.getLangOpts().OpenMP < 50 || !BO->getType()->isPointerType()) { 16886 emitErrorMsg(); 16887 return false; 16888 } 16889 16890 // Pointer arithmetic is the only thing we expect to happen here so after we 16891 // make sure the binary operator is a pointer type, the we only thing need 16892 // to to is to visit the subtree that has the same type as root (so that we 16893 // know the other subtree is just an offset) 16894 Expr *LE = BO->getLHS()->IgnoreParenImpCasts(); 16895 Expr *RE = BO->getRHS()->IgnoreParenImpCasts(); 16896 Components.emplace_back(BO, nullptr); 16897 assert((LE->getType().getTypePtr() == BO->getType().getTypePtr() || 16898 RE->getType().getTypePtr() == BO->getType().getTypePtr()) && 16899 "Either LHS or RHS have base decl inside"); 16900 if (BO->getType().getTypePtr() == LE->getType().getTypePtr()) 16901 return RelevantExpr || Visit(LE); 16902 return RelevantExpr || Visit(RE); 16903 } 16904 bool VisitCXXThisExpr(CXXThisExpr *CTE) { 16905 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr"); 16906 RelevantExpr = CTE; 16907 Components.emplace_back(CTE, nullptr); 16908 return true; 16909 } 16910 bool VisitStmt(Stmt *) { 16911 emitErrorMsg(); 16912 return false; 16913 } 16914 const Expr *getFoundBase() const { 16915 return RelevantExpr; 16916 } 16917 explicit MapBaseChecker( 16918 Sema &SemaRef, OpenMPClauseKind CKind, 16919 OMPClauseMappableExprCommon::MappableExprComponentList &Components, 16920 bool NoDiagnose, SourceLocation &ELoc, SourceRange &ERange) 16921 : SemaRef(SemaRef), CKind(CKind), Components(Components), 16922 NoDiagnose(NoDiagnose), ELoc(ELoc), ERange(ERange) {} 16923 }; 16924 } // namespace 16925 16926 /// Return the expression of the base of the mappable expression or null if it 16927 /// cannot be determined and do all the necessary checks to see if the expression 16928 /// is valid as a standalone mappable expression. In the process, record all the 16929 /// components of the expression. 16930 static const Expr *checkMapClauseExpressionBase( 16931 Sema &SemaRef, Expr *E, 16932 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents, 16933 OpenMPClauseKind CKind, bool NoDiagnose) { 16934 SourceLocation ELoc = E->getExprLoc(); 16935 SourceRange ERange = E->getSourceRange(); 16936 MapBaseChecker Checker(SemaRef, CKind, CurComponents, NoDiagnose, ELoc, 16937 ERange); 16938 if (Checker.Visit(E->IgnoreParens())) 16939 return Checker.getFoundBase(); 16940 return nullptr; 16941 } 16942 16943 // Return true if expression E associated with value VD has conflicts with other 16944 // map information. 16945 static bool checkMapConflicts( 16946 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E, 16947 bool CurrentRegionOnly, 16948 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents, 16949 OpenMPClauseKind CKind) { 16950 assert(VD && E); 16951 SourceLocation ELoc = E->getExprLoc(); 16952 SourceRange ERange = E->getSourceRange(); 16953 16954 // In order to easily check the conflicts we need to match each component of 16955 // the expression under test with the components of the expressions that are 16956 // already in the stack. 16957 16958 assert(!CurComponents.empty() && "Map clause expression with no components!"); 16959 assert(CurComponents.back().getAssociatedDeclaration() == VD && 16960 "Map clause expression with unexpected base!"); 16961 16962 // Variables to help detecting enclosing problems in data environment nests. 16963 bool IsEnclosedByDataEnvironmentExpr = false; 16964 const Expr *EnclosingExpr = nullptr; 16965 16966 bool FoundError = DSAS->checkMappableExprComponentListsForDecl( 16967 VD, CurrentRegionOnly, 16968 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc, 16969 ERange, CKind, &EnclosingExpr, 16970 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef 16971 StackComponents, 16972 OpenMPClauseKind) { 16973 assert(!StackComponents.empty() && 16974 "Map clause expression with no components!"); 16975 assert(StackComponents.back().getAssociatedDeclaration() == VD && 16976 "Map clause expression with unexpected base!"); 16977 (void)VD; 16978 16979 // The whole expression in the stack. 16980 const Expr *RE = StackComponents.front().getAssociatedExpression(); 16981 16982 // Expressions must start from the same base. Here we detect at which 16983 // point both expressions diverge from each other and see if we can 16984 // detect if the memory referred to both expressions is contiguous and 16985 // do not overlap. 16986 auto CI = CurComponents.rbegin(); 16987 auto CE = CurComponents.rend(); 16988 auto SI = StackComponents.rbegin(); 16989 auto SE = StackComponents.rend(); 16990 for (; CI != CE && SI != SE; ++CI, ++SI) { 16991 16992 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3] 16993 // At most one list item can be an array item derived from a given 16994 // variable in map clauses of the same construct. 16995 if (CurrentRegionOnly && 16996 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) || 16997 isa<OMPArraySectionExpr>(CI->getAssociatedExpression()) || 16998 isa<OMPArrayShapingExpr>(CI->getAssociatedExpression())) && 16999 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) || 17000 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()) || 17001 isa<OMPArrayShapingExpr>(SI->getAssociatedExpression()))) { 17002 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(), 17003 diag::err_omp_multiple_array_items_in_map_clause) 17004 << CI->getAssociatedExpression()->getSourceRange(); 17005 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(), 17006 diag::note_used_here) 17007 << SI->getAssociatedExpression()->getSourceRange(); 17008 return true; 17009 } 17010 17011 // Do both expressions have the same kind? 17012 if (CI->getAssociatedExpression()->getStmtClass() != 17013 SI->getAssociatedExpression()->getStmtClass()) 17014 break; 17015 17016 // Are we dealing with different variables/fields? 17017 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration()) 17018 break; 17019 } 17020 // Check if the extra components of the expressions in the enclosing 17021 // data environment are redundant for the current base declaration. 17022 // If they are, the maps completely overlap, which is legal. 17023 for (; SI != SE; ++SI) { 17024 QualType Type; 17025 if (const auto *ASE = 17026 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) { 17027 Type = ASE->getBase()->IgnoreParenImpCasts()->getType(); 17028 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>( 17029 SI->getAssociatedExpression())) { 17030 const Expr *E = OASE->getBase()->IgnoreParenImpCasts(); 17031 Type = 17032 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType(); 17033 } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>( 17034 SI->getAssociatedExpression())) { 17035 Type = OASE->getBase()->getType()->getPointeeType(); 17036 } 17037 if (Type.isNull() || Type->isAnyPointerType() || 17038 checkArrayExpressionDoesNotReferToWholeSize( 17039 SemaRef, SI->getAssociatedExpression(), Type)) 17040 break; 17041 } 17042 17043 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 17044 // List items of map clauses in the same construct must not share 17045 // original storage. 17046 // 17047 // If the expressions are exactly the same or one is a subset of the 17048 // other, it means they are sharing storage. 17049 if (CI == CE && SI == SE) { 17050 if (CurrentRegionOnly) { 17051 if (CKind == OMPC_map) { 17052 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 17053 } else { 17054 assert(CKind == OMPC_to || CKind == OMPC_from); 17055 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 17056 << ERange; 17057 } 17058 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 17059 << RE->getSourceRange(); 17060 return true; 17061 } 17062 // If we find the same expression in the enclosing data environment, 17063 // that is legal. 17064 IsEnclosedByDataEnvironmentExpr = true; 17065 return false; 17066 } 17067 17068 QualType DerivedType = 17069 std::prev(CI)->getAssociatedDeclaration()->getType(); 17070 SourceLocation DerivedLoc = 17071 std::prev(CI)->getAssociatedExpression()->getExprLoc(); 17072 17073 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 17074 // If the type of a list item is a reference to a type T then the type 17075 // will be considered to be T for all purposes of this clause. 17076 DerivedType = DerivedType.getNonReferenceType(); 17077 17078 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1] 17079 // A variable for which the type is pointer and an array section 17080 // derived from that variable must not appear as list items of map 17081 // clauses of the same construct. 17082 // 17083 // Also, cover one of the cases in: 17084 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 17085 // If any part of the original storage of a list item has corresponding 17086 // storage in the device data environment, all of the original storage 17087 // must have corresponding storage in the device data environment. 17088 // 17089 if (DerivedType->isAnyPointerType()) { 17090 if (CI == CE || SI == SE) { 17091 SemaRef.Diag( 17092 DerivedLoc, 17093 diag::err_omp_pointer_mapped_along_with_derived_section) 17094 << DerivedLoc; 17095 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 17096 << RE->getSourceRange(); 17097 return true; 17098 } 17099 if (CI->getAssociatedExpression()->getStmtClass() != 17100 SI->getAssociatedExpression()->getStmtClass() || 17101 CI->getAssociatedDeclaration()->getCanonicalDecl() == 17102 SI->getAssociatedDeclaration()->getCanonicalDecl()) { 17103 assert(CI != CE && SI != SE); 17104 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced) 17105 << DerivedLoc; 17106 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 17107 << RE->getSourceRange(); 17108 return true; 17109 } 17110 } 17111 17112 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4] 17113 // List items of map clauses in the same construct must not share 17114 // original storage. 17115 // 17116 // An expression is a subset of the other. 17117 if (CurrentRegionOnly && (CI == CE || SI == SE)) { 17118 if (CKind == OMPC_map) { 17119 if (CI != CE || SI != SE) { 17120 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is 17121 // a pointer. 17122 auto Begin = 17123 CI != CE ? CurComponents.begin() : StackComponents.begin(); 17124 auto End = CI != CE ? CurComponents.end() : StackComponents.end(); 17125 auto It = Begin; 17126 while (It != End && !It->getAssociatedDeclaration()) 17127 std::advance(It, 1); 17128 assert(It != End && 17129 "Expected at least one component with the declaration."); 17130 if (It != Begin && It->getAssociatedDeclaration() 17131 ->getType() 17132 .getCanonicalType() 17133 ->isAnyPointerType()) { 17134 IsEnclosedByDataEnvironmentExpr = false; 17135 EnclosingExpr = nullptr; 17136 return false; 17137 } 17138 } 17139 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange; 17140 } else { 17141 assert(CKind == OMPC_to || CKind == OMPC_from); 17142 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update) 17143 << ERange; 17144 } 17145 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here) 17146 << RE->getSourceRange(); 17147 return true; 17148 } 17149 17150 // The current expression uses the same base as other expression in the 17151 // data environment but does not contain it completely. 17152 if (!CurrentRegionOnly && SI != SE) 17153 EnclosingExpr = RE; 17154 17155 // The current expression is a subset of the expression in the data 17156 // environment. 17157 IsEnclosedByDataEnvironmentExpr |= 17158 (!CurrentRegionOnly && CI != CE && SI == SE); 17159 17160 return false; 17161 }); 17162 17163 if (CurrentRegionOnly) 17164 return FoundError; 17165 17166 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5] 17167 // If any part of the original storage of a list item has corresponding 17168 // storage in the device data environment, all of the original storage must 17169 // have corresponding storage in the device data environment. 17170 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6] 17171 // If a list item is an element of a structure, and a different element of 17172 // the structure has a corresponding list item in the device data environment 17173 // prior to a task encountering the construct associated with the map clause, 17174 // then the list item must also have a corresponding list item in the device 17175 // data environment prior to the task encountering the construct. 17176 // 17177 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) { 17178 SemaRef.Diag(ELoc, 17179 diag::err_omp_original_storage_is_shared_and_does_not_contain) 17180 << ERange; 17181 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here) 17182 << EnclosingExpr->getSourceRange(); 17183 return true; 17184 } 17185 17186 return FoundError; 17187 } 17188 17189 // Look up the user-defined mapper given the mapper name and mapped type, and 17190 // build a reference to it. 17191 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S, 17192 CXXScopeSpec &MapperIdScopeSpec, 17193 const DeclarationNameInfo &MapperId, 17194 QualType Type, 17195 Expr *UnresolvedMapper) { 17196 if (MapperIdScopeSpec.isInvalid()) 17197 return ExprError(); 17198 // Get the actual type for the array type. 17199 if (Type->isArrayType()) { 17200 assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type"); 17201 Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType(); 17202 } 17203 // Find all user-defined mappers with the given MapperId. 17204 SmallVector<UnresolvedSet<8>, 4> Lookups; 17205 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName); 17206 Lookup.suppressDiagnostics(); 17207 if (S) { 17208 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) { 17209 NamedDecl *D = Lookup.getRepresentativeDecl(); 17210 while (S && !S->isDeclScope(D)) 17211 S = S->getParent(); 17212 if (S) 17213 S = S->getParent(); 17214 Lookups.emplace_back(); 17215 Lookups.back().append(Lookup.begin(), Lookup.end()); 17216 Lookup.clear(); 17217 } 17218 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) { 17219 // Extract the user-defined mappers with the given MapperId. 17220 Lookups.push_back(UnresolvedSet<8>()); 17221 for (NamedDecl *D : ULE->decls()) { 17222 auto *DMD = cast<OMPDeclareMapperDecl>(D); 17223 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation."); 17224 Lookups.back().addDecl(DMD); 17225 } 17226 } 17227 // Defer the lookup for dependent types. The results will be passed through 17228 // UnresolvedMapper on instantiation. 17229 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() || 17230 Type->isInstantiationDependentType() || 17231 Type->containsUnexpandedParameterPack() || 17232 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) { 17233 return !D->isInvalidDecl() && 17234 (D->getType()->isDependentType() || 17235 D->getType()->isInstantiationDependentType() || 17236 D->getType()->containsUnexpandedParameterPack()); 17237 })) { 17238 UnresolvedSet<8> URS; 17239 for (const UnresolvedSet<8> &Set : Lookups) { 17240 if (Set.empty()) 17241 continue; 17242 URS.append(Set.begin(), Set.end()); 17243 } 17244 return UnresolvedLookupExpr::Create( 17245 SemaRef.Context, /*NamingClass=*/nullptr, 17246 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId, 17247 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end()); 17248 } 17249 SourceLocation Loc = MapperId.getLoc(); 17250 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 17251 // The type must be of struct, union or class type in C and C++ 17252 if (!Type->isStructureOrClassType() && !Type->isUnionType() && 17253 (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) { 17254 SemaRef.Diag(Loc, diag::err_omp_mapper_wrong_type); 17255 return ExprError(); 17256 } 17257 // Perform argument dependent lookup. 17258 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet()) 17259 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups); 17260 // Return the first user-defined mapper with the desired type. 17261 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 17262 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * { 17263 if (!D->isInvalidDecl() && 17264 SemaRef.Context.hasSameType(D->getType(), Type)) 17265 return D; 17266 return nullptr; 17267 })) 17268 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 17269 // Find the first user-defined mapper with a type derived from the desired 17270 // type. 17271 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>( 17272 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * { 17273 if (!D->isInvalidDecl() && 17274 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) && 17275 !Type.isMoreQualifiedThan(D->getType())) 17276 return D; 17277 return nullptr; 17278 })) { 17279 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 17280 /*DetectVirtual=*/false); 17281 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) { 17282 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType( 17283 VD->getType().getUnqualifiedType()))) { 17284 if (SemaRef.CheckBaseClassAccess( 17285 Loc, VD->getType(), Type, Paths.front(), 17286 /*DiagID=*/0) != Sema::AR_inaccessible) { 17287 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc); 17288 } 17289 } 17290 } 17291 } 17292 // Report error if a mapper is specified, but cannot be found. 17293 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") { 17294 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper) 17295 << Type << MapperId.getName(); 17296 return ExprError(); 17297 } 17298 return ExprEmpty(); 17299 } 17300 17301 namespace { 17302 // Utility struct that gathers all the related lists associated with a mappable 17303 // expression. 17304 struct MappableVarListInfo { 17305 // The list of expressions. 17306 ArrayRef<Expr *> VarList; 17307 // The list of processed expressions. 17308 SmallVector<Expr *, 16> ProcessedVarList; 17309 // The mappble components for each expression. 17310 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents; 17311 // The base declaration of the variable. 17312 SmallVector<ValueDecl *, 16> VarBaseDeclarations; 17313 // The reference to the user-defined mapper associated with every expression. 17314 SmallVector<Expr *, 16> UDMapperList; 17315 17316 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) { 17317 // We have a list of components and base declarations for each entry in the 17318 // variable list. 17319 VarComponents.reserve(VarList.size()); 17320 VarBaseDeclarations.reserve(VarList.size()); 17321 } 17322 }; 17323 } 17324 17325 // Check the validity of the provided variable list for the provided clause kind 17326 // \a CKind. In the check process the valid expressions, mappable expression 17327 // components, variables, and user-defined mappers are extracted and used to 17328 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a 17329 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec, 17330 // and \a MapperId are expected to be valid if the clause kind is 'map'. 17331 static void checkMappableExpressionList( 17332 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind, 17333 MappableVarListInfo &MVLI, SourceLocation StartLoc, 17334 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId, 17335 ArrayRef<Expr *> UnresolvedMappers, 17336 OpenMPMapClauseKind MapType = OMPC_MAP_unknown, 17337 bool IsMapTypeImplicit = false) { 17338 // We only expect mappable expressions in 'to', 'from', and 'map' clauses. 17339 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) && 17340 "Unexpected clause kind with mappable expressions!"); 17341 17342 // If the identifier of user-defined mapper is not specified, it is "default". 17343 // We do not change the actual name in this clause to distinguish whether a 17344 // mapper is specified explicitly, i.e., it is not explicitly specified when 17345 // MapperId.getName() is empty. 17346 if (!MapperId.getName() || MapperId.getName().isEmpty()) { 17347 auto &DeclNames = SemaRef.getASTContext().DeclarationNames; 17348 MapperId.setName(DeclNames.getIdentifier( 17349 &SemaRef.getASTContext().Idents.get("default"))); 17350 } 17351 17352 // Iterators to find the current unresolved mapper expression. 17353 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end(); 17354 bool UpdateUMIt = false; 17355 Expr *UnresolvedMapper = nullptr; 17356 17357 // Keep track of the mappable components and base declarations in this clause. 17358 // Each entry in the list is going to have a list of components associated. We 17359 // record each set of the components so that we can build the clause later on. 17360 // In the end we should have the same amount of declarations and component 17361 // lists. 17362 17363 for (Expr *RE : MVLI.VarList) { 17364 assert(RE && "Null expr in omp to/from/map clause"); 17365 SourceLocation ELoc = RE->getExprLoc(); 17366 17367 // Find the current unresolved mapper expression. 17368 if (UpdateUMIt && UMIt != UMEnd) { 17369 UMIt++; 17370 assert( 17371 UMIt != UMEnd && 17372 "Expect the size of UnresolvedMappers to match with that of VarList"); 17373 } 17374 UpdateUMIt = true; 17375 if (UMIt != UMEnd) 17376 UnresolvedMapper = *UMIt; 17377 17378 const Expr *VE = RE->IgnoreParenLValueCasts(); 17379 17380 if (VE->isValueDependent() || VE->isTypeDependent() || 17381 VE->isInstantiationDependent() || 17382 VE->containsUnexpandedParameterPack()) { 17383 // Try to find the associated user-defined mapper. 17384 ExprResult ER = buildUserDefinedMapperRef( 17385 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 17386 VE->getType().getCanonicalType(), UnresolvedMapper); 17387 if (ER.isInvalid()) 17388 continue; 17389 MVLI.UDMapperList.push_back(ER.get()); 17390 // We can only analyze this information once the missing information is 17391 // resolved. 17392 MVLI.ProcessedVarList.push_back(RE); 17393 continue; 17394 } 17395 17396 Expr *SimpleExpr = RE->IgnoreParenCasts(); 17397 17398 if (!RE->isLValue()) { 17399 if (SemaRef.getLangOpts().OpenMP < 50) { 17400 SemaRef.Diag( 17401 ELoc, diag::err_omp_expected_named_var_member_or_array_expression) 17402 << RE->getSourceRange(); 17403 } else { 17404 SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses) 17405 << getOpenMPClauseName(CKind) << RE->getSourceRange(); 17406 } 17407 continue; 17408 } 17409 17410 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents; 17411 ValueDecl *CurDeclaration = nullptr; 17412 17413 // Obtain the array or member expression bases if required. Also, fill the 17414 // components array with all the components identified in the process. 17415 const Expr *BE = checkMapClauseExpressionBase( 17416 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false); 17417 if (!BE) 17418 continue; 17419 17420 assert(!CurComponents.empty() && 17421 "Invalid mappable expression information."); 17422 17423 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) { 17424 // Add store "this" pointer to class in DSAStackTy for future checking 17425 DSAS->addMappedClassesQualTypes(TE->getType()); 17426 // Try to find the associated user-defined mapper. 17427 ExprResult ER = buildUserDefinedMapperRef( 17428 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 17429 VE->getType().getCanonicalType(), UnresolvedMapper); 17430 if (ER.isInvalid()) 17431 continue; 17432 MVLI.UDMapperList.push_back(ER.get()); 17433 // Skip restriction checking for variable or field declarations 17434 MVLI.ProcessedVarList.push_back(RE); 17435 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 17436 MVLI.VarComponents.back().append(CurComponents.begin(), 17437 CurComponents.end()); 17438 MVLI.VarBaseDeclarations.push_back(nullptr); 17439 continue; 17440 } 17441 17442 // For the following checks, we rely on the base declaration which is 17443 // expected to be associated with the last component. The declaration is 17444 // expected to be a variable or a field (if 'this' is being mapped). 17445 CurDeclaration = CurComponents.back().getAssociatedDeclaration(); 17446 assert(CurDeclaration && "Null decl on map clause."); 17447 assert( 17448 CurDeclaration->isCanonicalDecl() && 17449 "Expecting components to have associated only canonical declarations."); 17450 17451 auto *VD = dyn_cast<VarDecl>(CurDeclaration); 17452 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration); 17453 17454 assert((VD || FD) && "Only variables or fields are expected here!"); 17455 (void)FD; 17456 17457 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10] 17458 // threadprivate variables cannot appear in a map clause. 17459 // OpenMP 4.5 [2.10.5, target update Construct] 17460 // threadprivate variables cannot appear in a from clause. 17461 if (VD && DSAS->isThreadPrivate(VD)) { 17462 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 17463 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause) 17464 << getOpenMPClauseName(CKind); 17465 reportOriginalDsa(SemaRef, DSAS, VD, DVar); 17466 continue; 17467 } 17468 17469 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 17470 // A list item cannot appear in both a map clause and a data-sharing 17471 // attribute clause on the same construct. 17472 17473 // Check conflicts with other map clause expressions. We check the conflicts 17474 // with the current construct separately from the enclosing data 17475 // environment, because the restrictions are different. We only have to 17476 // check conflicts across regions for the map clauses. 17477 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 17478 /*CurrentRegionOnly=*/true, CurComponents, CKind)) 17479 break; 17480 if (CKind == OMPC_map && 17481 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr, 17482 /*CurrentRegionOnly=*/false, CurComponents, CKind)) 17483 break; 17484 17485 // OpenMP 4.5 [2.10.5, target update Construct] 17486 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1] 17487 // If the type of a list item is a reference to a type T then the type will 17488 // be considered to be T for all purposes of this clause. 17489 auto I = llvm::find_if( 17490 CurComponents, 17491 [](const OMPClauseMappableExprCommon::MappableComponent &MC) { 17492 return MC.getAssociatedDeclaration(); 17493 }); 17494 assert(I != CurComponents.end() && "Null decl on map clause."); 17495 QualType Type; 17496 auto *ASE = dyn_cast<ArraySubscriptExpr>(VE->IgnoreParens()); 17497 auto *OASE = dyn_cast<OMPArraySectionExpr>(VE->IgnoreParens()); 17498 auto *OAShE = dyn_cast<OMPArrayShapingExpr>(VE->IgnoreParens()); 17499 if (ASE) { 17500 Type = ASE->getType().getNonReferenceType(); 17501 } else if (OASE) { 17502 QualType BaseType = 17503 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase()); 17504 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe()) 17505 Type = ATy->getElementType(); 17506 else 17507 Type = BaseType->getPointeeType(); 17508 Type = Type.getNonReferenceType(); 17509 } else if (OAShE) { 17510 Type = OAShE->getBase()->getType()->getPointeeType(); 17511 } else { 17512 Type = VE->getType(); 17513 } 17514 17515 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4] 17516 // A list item in a to or from clause must have a mappable type. 17517 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9] 17518 // A list item must have a mappable type. 17519 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef, 17520 DSAS, Type)) 17521 continue; 17522 17523 Type = I->getAssociatedDeclaration()->getType().getNonReferenceType(); 17524 17525 if (CKind == OMPC_map) { 17526 // target enter data 17527 // OpenMP [2.10.2, Restrictions, p. 99] 17528 // A map-type must be specified in all map clauses and must be either 17529 // to or alloc. 17530 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective(); 17531 if (DKind == OMPD_target_enter_data && 17532 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) { 17533 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 17534 << (IsMapTypeImplicit ? 1 : 0) 17535 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 17536 << getOpenMPDirectiveName(DKind); 17537 continue; 17538 } 17539 17540 // target exit_data 17541 // OpenMP [2.10.3, Restrictions, p. 102] 17542 // A map-type must be specified in all map clauses and must be either 17543 // from, release, or delete. 17544 if (DKind == OMPD_target_exit_data && 17545 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release || 17546 MapType == OMPC_MAP_delete)) { 17547 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 17548 << (IsMapTypeImplicit ? 1 : 0) 17549 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 17550 << getOpenMPDirectiveName(DKind); 17551 continue; 17552 } 17553 17554 // target, target data 17555 // OpenMP 5.0 [2.12.2, Restrictions, p. 163] 17556 // OpenMP 5.0 [2.12.5, Restrictions, p. 174] 17557 // A map-type in a map clause must be to, from, tofrom or alloc 17558 if ((DKind == OMPD_target_data || 17559 isOpenMPTargetExecutionDirective(DKind)) && 17560 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_from || 17561 MapType == OMPC_MAP_tofrom || MapType == OMPC_MAP_alloc)) { 17562 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive) 17563 << (IsMapTypeImplicit ? 1 : 0) 17564 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType) 17565 << getOpenMPDirectiveName(DKind); 17566 continue; 17567 } 17568 17569 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3] 17570 // A list item cannot appear in both a map clause and a data-sharing 17571 // attribute clause on the same construct 17572 // 17573 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7] 17574 // A list item cannot appear in both a map clause and a data-sharing 17575 // attribute clause on the same construct unless the construct is a 17576 // combined construct. 17577 if (VD && ((SemaRef.LangOpts.OpenMP <= 45 && 17578 isOpenMPTargetExecutionDirective(DKind)) || 17579 DKind == OMPD_target)) { 17580 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false); 17581 if (isOpenMPPrivate(DVar.CKind)) { 17582 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 17583 << getOpenMPClauseName(DVar.CKind) 17584 << getOpenMPClauseName(OMPC_map) 17585 << getOpenMPDirectiveName(DSAS->getCurrentDirective()); 17586 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar); 17587 continue; 17588 } 17589 } 17590 } 17591 17592 // Try to find the associated user-defined mapper. 17593 ExprResult ER = buildUserDefinedMapperRef( 17594 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId, 17595 Type.getCanonicalType(), UnresolvedMapper); 17596 if (ER.isInvalid()) 17597 continue; 17598 MVLI.UDMapperList.push_back(ER.get()); 17599 17600 // Save the current expression. 17601 MVLI.ProcessedVarList.push_back(RE); 17602 17603 // Store the components in the stack so that they can be used to check 17604 // against other clauses later on. 17605 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents, 17606 /*WhereFoundClauseKind=*/OMPC_map); 17607 17608 // Save the components and declaration to create the clause. For purposes of 17609 // the clause creation, any component list that has has base 'this' uses 17610 // null as base declaration. 17611 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 17612 MVLI.VarComponents.back().append(CurComponents.begin(), 17613 CurComponents.end()); 17614 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr 17615 : CurDeclaration); 17616 } 17617 } 17618 17619 OMPClause *Sema::ActOnOpenMPMapClause( 17620 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers, 17621 ArrayRef<SourceLocation> MapTypeModifiersLoc, 17622 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId, 17623 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc, 17624 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, 17625 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) { 17626 OpenMPMapModifierKind Modifiers[] = {OMPC_MAP_MODIFIER_unknown, 17627 OMPC_MAP_MODIFIER_unknown, 17628 OMPC_MAP_MODIFIER_unknown}; 17629 SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers]; 17630 17631 // Process map-type-modifiers, flag errors for duplicate modifiers. 17632 unsigned Count = 0; 17633 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) { 17634 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown && 17635 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) { 17636 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier); 17637 continue; 17638 } 17639 assert(Count < NumberOfOMPMapClauseModifiers && 17640 "Modifiers exceed the allowed number of map type modifiers"); 17641 Modifiers[Count] = MapTypeModifiers[I]; 17642 ModifiersLoc[Count] = MapTypeModifiersLoc[I]; 17643 ++Count; 17644 } 17645 17646 MappableVarListInfo MVLI(VarList); 17647 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc, 17648 MapperIdScopeSpec, MapperId, UnresolvedMappers, 17649 MapType, IsMapTypeImplicit); 17650 17651 // We need to produce a map clause even if we don't have variables so that 17652 // other diagnostics related with non-existing map clauses are accurate. 17653 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList, 17654 MVLI.VarBaseDeclarations, MVLI.VarComponents, 17655 MVLI.UDMapperList, Modifiers, ModifiersLoc, 17656 MapperIdScopeSpec.getWithLocInContext(Context), 17657 MapperId, MapType, IsMapTypeImplicit, MapLoc); 17658 } 17659 17660 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc, 17661 TypeResult ParsedType) { 17662 assert(ParsedType.isUsable()); 17663 17664 QualType ReductionType = GetTypeFromParser(ParsedType.get()); 17665 if (ReductionType.isNull()) 17666 return QualType(); 17667 17668 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++ 17669 // A type name in a declare reduction directive cannot be a function type, an 17670 // array type, a reference type, or a type qualified with const, volatile or 17671 // restrict. 17672 if (ReductionType.hasQualifiers()) { 17673 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0; 17674 return QualType(); 17675 } 17676 17677 if (ReductionType->isFunctionType()) { 17678 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1; 17679 return QualType(); 17680 } 17681 if (ReductionType->isReferenceType()) { 17682 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2; 17683 return QualType(); 17684 } 17685 if (ReductionType->isArrayType()) { 17686 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3; 17687 return QualType(); 17688 } 17689 return ReductionType; 17690 } 17691 17692 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart( 17693 Scope *S, DeclContext *DC, DeclarationName Name, 17694 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes, 17695 AccessSpecifier AS, Decl *PrevDeclInScope) { 17696 SmallVector<Decl *, 8> Decls; 17697 Decls.reserve(ReductionTypes.size()); 17698 17699 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName, 17700 forRedeclarationInCurContext()); 17701 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions 17702 // A reduction-identifier may not be re-declared in the current scope for the 17703 // same type or for a type that is compatible according to the base language 17704 // rules. 17705 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 17706 OMPDeclareReductionDecl *PrevDRD = nullptr; 17707 bool InCompoundScope = true; 17708 if (S != nullptr) { 17709 // Find previous declaration with the same name not referenced in other 17710 // declarations. 17711 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 17712 InCompoundScope = 17713 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 17714 LookupName(Lookup, S); 17715 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 17716 /*AllowInlineNamespace=*/false); 17717 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious; 17718 LookupResult::Filter Filter = Lookup.makeFilter(); 17719 while (Filter.hasNext()) { 17720 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next()); 17721 if (InCompoundScope) { 17722 auto I = UsedAsPrevious.find(PrevDecl); 17723 if (I == UsedAsPrevious.end()) 17724 UsedAsPrevious[PrevDecl] = false; 17725 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope()) 17726 UsedAsPrevious[D] = true; 17727 } 17728 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 17729 PrevDecl->getLocation(); 17730 } 17731 Filter.done(); 17732 if (InCompoundScope) { 17733 for (const auto &PrevData : UsedAsPrevious) { 17734 if (!PrevData.second) { 17735 PrevDRD = PrevData.first; 17736 break; 17737 } 17738 } 17739 } 17740 } else if (PrevDeclInScope != nullptr) { 17741 auto *PrevDRDInScope = PrevDRD = 17742 cast<OMPDeclareReductionDecl>(PrevDeclInScope); 17743 do { 17744 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] = 17745 PrevDRDInScope->getLocation(); 17746 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope(); 17747 } while (PrevDRDInScope != nullptr); 17748 } 17749 for (const auto &TyData : ReductionTypes) { 17750 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType()); 17751 bool Invalid = false; 17752 if (I != PreviousRedeclTypes.end()) { 17753 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition) 17754 << TyData.first; 17755 Diag(I->second, diag::note_previous_definition); 17756 Invalid = true; 17757 } 17758 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second; 17759 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second, 17760 Name, TyData.first, PrevDRD); 17761 DC->addDecl(DRD); 17762 DRD->setAccess(AS); 17763 Decls.push_back(DRD); 17764 if (Invalid) 17765 DRD->setInvalidDecl(); 17766 else 17767 PrevDRD = DRD; 17768 } 17769 17770 return DeclGroupPtrTy::make( 17771 DeclGroupRef::Create(Context, Decls.begin(), Decls.size())); 17772 } 17773 17774 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) { 17775 auto *DRD = cast<OMPDeclareReductionDecl>(D); 17776 17777 // Enter new function scope. 17778 PushFunctionScope(); 17779 setFunctionHasBranchProtectedScope(); 17780 getCurFunction()->setHasOMPDeclareReductionCombiner(); 17781 17782 if (S != nullptr) 17783 PushDeclContext(S, DRD); 17784 else 17785 CurContext = DRD; 17786 17787 PushExpressionEvaluationContext( 17788 ExpressionEvaluationContext::PotentiallyEvaluated); 17789 17790 QualType ReductionType = DRD->getType(); 17791 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will 17792 // be replaced by '*omp_parm' during codegen. This required because 'omp_in' 17793 // uses semantics of argument handles by value, but it should be passed by 17794 // reference. C lang does not support references, so pass all parameters as 17795 // pointers. 17796 // Create 'T omp_in;' variable. 17797 VarDecl *OmpInParm = 17798 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in"); 17799 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will 17800 // be replaced by '*omp_parm' during codegen. This required because 'omp_out' 17801 // uses semantics of argument handles by value, but it should be passed by 17802 // reference. C lang does not support references, so pass all parameters as 17803 // pointers. 17804 // Create 'T omp_out;' variable. 17805 VarDecl *OmpOutParm = 17806 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out"); 17807 if (S != nullptr) { 17808 PushOnScopeChains(OmpInParm, S); 17809 PushOnScopeChains(OmpOutParm, S); 17810 } else { 17811 DRD->addDecl(OmpInParm); 17812 DRD->addDecl(OmpOutParm); 17813 } 17814 Expr *InE = 17815 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation()); 17816 Expr *OutE = 17817 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation()); 17818 DRD->setCombinerData(InE, OutE); 17819 } 17820 17821 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) { 17822 auto *DRD = cast<OMPDeclareReductionDecl>(D); 17823 DiscardCleanupsInEvaluationContext(); 17824 PopExpressionEvaluationContext(); 17825 17826 PopDeclContext(); 17827 PopFunctionScopeInfo(); 17828 17829 if (Combiner != nullptr) 17830 DRD->setCombiner(Combiner); 17831 else 17832 DRD->setInvalidDecl(); 17833 } 17834 17835 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) { 17836 auto *DRD = cast<OMPDeclareReductionDecl>(D); 17837 17838 // Enter new function scope. 17839 PushFunctionScope(); 17840 setFunctionHasBranchProtectedScope(); 17841 17842 if (S != nullptr) 17843 PushDeclContext(S, DRD); 17844 else 17845 CurContext = DRD; 17846 17847 PushExpressionEvaluationContext( 17848 ExpressionEvaluationContext::PotentiallyEvaluated); 17849 17850 QualType ReductionType = DRD->getType(); 17851 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will 17852 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv' 17853 // uses semantics of argument handles by value, but it should be passed by 17854 // reference. C lang does not support references, so pass all parameters as 17855 // pointers. 17856 // Create 'T omp_priv;' variable. 17857 VarDecl *OmpPrivParm = 17858 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv"); 17859 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will 17860 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig' 17861 // uses semantics of argument handles by value, but it should be passed by 17862 // reference. C lang does not support references, so pass all parameters as 17863 // pointers. 17864 // Create 'T omp_orig;' variable. 17865 VarDecl *OmpOrigParm = 17866 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig"); 17867 if (S != nullptr) { 17868 PushOnScopeChains(OmpPrivParm, S); 17869 PushOnScopeChains(OmpOrigParm, S); 17870 } else { 17871 DRD->addDecl(OmpPrivParm); 17872 DRD->addDecl(OmpOrigParm); 17873 } 17874 Expr *OrigE = 17875 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation()); 17876 Expr *PrivE = 17877 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation()); 17878 DRD->setInitializerData(OrigE, PrivE); 17879 return OmpPrivParm; 17880 } 17881 17882 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer, 17883 VarDecl *OmpPrivParm) { 17884 auto *DRD = cast<OMPDeclareReductionDecl>(D); 17885 DiscardCleanupsInEvaluationContext(); 17886 PopExpressionEvaluationContext(); 17887 17888 PopDeclContext(); 17889 PopFunctionScopeInfo(); 17890 17891 if (Initializer != nullptr) { 17892 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit); 17893 } else if (OmpPrivParm->hasInit()) { 17894 DRD->setInitializer(OmpPrivParm->getInit(), 17895 OmpPrivParm->isDirectInit() 17896 ? OMPDeclareReductionDecl::DirectInit 17897 : OMPDeclareReductionDecl::CopyInit); 17898 } else { 17899 DRD->setInvalidDecl(); 17900 } 17901 } 17902 17903 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd( 17904 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) { 17905 for (Decl *D : DeclReductions.get()) { 17906 if (IsValid) { 17907 if (S) 17908 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S, 17909 /*AddToContext=*/false); 17910 } else { 17911 D->setInvalidDecl(); 17912 } 17913 } 17914 return DeclReductions; 17915 } 17916 17917 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) { 17918 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); 17919 QualType T = TInfo->getType(); 17920 if (D.isInvalidType()) 17921 return true; 17922 17923 if (getLangOpts().CPlusPlus) { 17924 // Check that there are no default arguments (C++ only). 17925 CheckExtraCXXDefaultArguments(D); 17926 } 17927 17928 return CreateParsedType(T, TInfo); 17929 } 17930 17931 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc, 17932 TypeResult ParsedType) { 17933 assert(ParsedType.isUsable() && "Expect usable parsed mapper type"); 17934 17935 QualType MapperType = GetTypeFromParser(ParsedType.get()); 17936 assert(!MapperType.isNull() && "Expect valid mapper type"); 17937 17938 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 17939 // The type must be of struct, union or class type in C and C++ 17940 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) { 17941 Diag(TyLoc, diag::err_omp_mapper_wrong_type); 17942 return QualType(); 17943 } 17944 return MapperType; 17945 } 17946 17947 OMPDeclareMapperDecl *Sema::ActOnOpenMPDeclareMapperDirectiveStart( 17948 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType, 17949 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS, 17950 Decl *PrevDeclInScope) { 17951 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName, 17952 forRedeclarationInCurContext()); 17953 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions 17954 // A mapper-identifier may not be redeclared in the current scope for the 17955 // same type or for a type that is compatible according to the base language 17956 // rules. 17957 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes; 17958 OMPDeclareMapperDecl *PrevDMD = nullptr; 17959 bool InCompoundScope = true; 17960 if (S != nullptr) { 17961 // Find previous declaration with the same name not referenced in other 17962 // declarations. 17963 FunctionScopeInfo *ParentFn = getEnclosingFunction(); 17964 InCompoundScope = 17965 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty(); 17966 LookupName(Lookup, S); 17967 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false, 17968 /*AllowInlineNamespace=*/false); 17969 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious; 17970 LookupResult::Filter Filter = Lookup.makeFilter(); 17971 while (Filter.hasNext()) { 17972 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next()); 17973 if (InCompoundScope) { 17974 auto I = UsedAsPrevious.find(PrevDecl); 17975 if (I == UsedAsPrevious.end()) 17976 UsedAsPrevious[PrevDecl] = false; 17977 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope()) 17978 UsedAsPrevious[D] = true; 17979 } 17980 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] = 17981 PrevDecl->getLocation(); 17982 } 17983 Filter.done(); 17984 if (InCompoundScope) { 17985 for (const auto &PrevData : UsedAsPrevious) { 17986 if (!PrevData.second) { 17987 PrevDMD = PrevData.first; 17988 break; 17989 } 17990 } 17991 } 17992 } else if (PrevDeclInScope) { 17993 auto *PrevDMDInScope = PrevDMD = 17994 cast<OMPDeclareMapperDecl>(PrevDeclInScope); 17995 do { 17996 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] = 17997 PrevDMDInScope->getLocation(); 17998 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope(); 17999 } while (PrevDMDInScope != nullptr); 18000 } 18001 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType()); 18002 bool Invalid = false; 18003 if (I != PreviousRedeclTypes.end()) { 18004 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition) 18005 << MapperType << Name; 18006 Diag(I->second, diag::note_previous_definition); 18007 Invalid = true; 18008 } 18009 auto *DMD = OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name, 18010 MapperType, VN, PrevDMD); 18011 DC->addDecl(DMD); 18012 DMD->setAccess(AS); 18013 if (Invalid) 18014 DMD->setInvalidDecl(); 18015 18016 // Enter new function scope. 18017 PushFunctionScope(); 18018 setFunctionHasBranchProtectedScope(); 18019 18020 CurContext = DMD; 18021 18022 return DMD; 18023 } 18024 18025 void Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(OMPDeclareMapperDecl *DMD, 18026 Scope *S, 18027 QualType MapperType, 18028 SourceLocation StartLoc, 18029 DeclarationName VN) { 18030 VarDecl *VD = buildVarDecl(*this, StartLoc, MapperType, VN.getAsString()); 18031 if (S) 18032 PushOnScopeChains(VD, S); 18033 else 18034 DMD->addDecl(VD); 18035 Expr *MapperVarRefExpr = buildDeclRefExpr(*this, VD, MapperType, StartLoc); 18036 DMD->setMapperVarRef(MapperVarRefExpr); 18037 } 18038 18039 Sema::DeclGroupPtrTy 18040 Sema::ActOnOpenMPDeclareMapperDirectiveEnd(OMPDeclareMapperDecl *D, Scope *S, 18041 ArrayRef<OMPClause *> ClauseList) { 18042 PopDeclContext(); 18043 PopFunctionScopeInfo(); 18044 18045 if (D) { 18046 if (S) 18047 PushOnScopeChains(D, S, /*AddToContext=*/false); 18048 D->CreateClauses(Context, ClauseList); 18049 } 18050 18051 return DeclGroupPtrTy::make(DeclGroupRef(D)); 18052 } 18053 18054 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams, 18055 SourceLocation StartLoc, 18056 SourceLocation LParenLoc, 18057 SourceLocation EndLoc) { 18058 Expr *ValExpr = NumTeams; 18059 Stmt *HelperValStmt = nullptr; 18060 18061 // OpenMP [teams Constrcut, Restrictions] 18062 // The num_teams expression must evaluate to a positive integer value. 18063 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams, 18064 /*StrictlyPositive=*/true)) 18065 return nullptr; 18066 18067 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 18068 OpenMPDirectiveKind CaptureRegion = 18069 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams, LangOpts.OpenMP); 18070 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 18071 ValExpr = MakeFullExpr(ValExpr).get(); 18072 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 18073 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 18074 HelperValStmt = buildPreInits(Context, Captures); 18075 } 18076 18077 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion, 18078 StartLoc, LParenLoc, EndLoc); 18079 } 18080 18081 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit, 18082 SourceLocation StartLoc, 18083 SourceLocation LParenLoc, 18084 SourceLocation EndLoc) { 18085 Expr *ValExpr = ThreadLimit; 18086 Stmt *HelperValStmt = nullptr; 18087 18088 // OpenMP [teams Constrcut, Restrictions] 18089 // The thread_limit expression must evaluate to a positive integer value. 18090 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit, 18091 /*StrictlyPositive=*/true)) 18092 return nullptr; 18093 18094 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective(); 18095 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause( 18096 DKind, OMPC_thread_limit, LangOpts.OpenMP); 18097 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) { 18098 ValExpr = MakeFullExpr(ValExpr).get(); 18099 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 18100 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 18101 HelperValStmt = buildPreInits(Context, Captures); 18102 } 18103 18104 return new (Context) OMPThreadLimitClause( 18105 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc); 18106 } 18107 18108 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority, 18109 SourceLocation StartLoc, 18110 SourceLocation LParenLoc, 18111 SourceLocation EndLoc) { 18112 Expr *ValExpr = Priority; 18113 Stmt *HelperValStmt = nullptr; 18114 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 18115 18116 // OpenMP [2.9.1, task Constrcut] 18117 // The priority-value is a non-negative numerical scalar expression. 18118 if (!isNonNegativeIntegerValue( 18119 ValExpr, *this, OMPC_priority, 18120 /*StrictlyPositive=*/false, /*BuildCapture=*/true, 18121 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 18122 return nullptr; 18123 18124 return new (Context) OMPPriorityClause(ValExpr, HelperValStmt, CaptureRegion, 18125 StartLoc, LParenLoc, EndLoc); 18126 } 18127 18128 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize, 18129 SourceLocation StartLoc, 18130 SourceLocation LParenLoc, 18131 SourceLocation EndLoc) { 18132 Expr *ValExpr = Grainsize; 18133 Stmt *HelperValStmt = nullptr; 18134 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 18135 18136 // OpenMP [2.9.2, taskloop Constrcut] 18137 // The parameter of the grainsize clause must be a positive integer 18138 // expression. 18139 if (!isNonNegativeIntegerValue( 18140 ValExpr, *this, OMPC_grainsize, 18141 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 18142 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 18143 return nullptr; 18144 18145 return new (Context) OMPGrainsizeClause(ValExpr, HelperValStmt, CaptureRegion, 18146 StartLoc, LParenLoc, EndLoc); 18147 } 18148 18149 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks, 18150 SourceLocation StartLoc, 18151 SourceLocation LParenLoc, 18152 SourceLocation EndLoc) { 18153 Expr *ValExpr = NumTasks; 18154 Stmt *HelperValStmt = nullptr; 18155 OpenMPDirectiveKind CaptureRegion = OMPD_unknown; 18156 18157 // OpenMP [2.9.2, taskloop Constrcut] 18158 // The parameter of the num_tasks clause must be a positive integer 18159 // expression. 18160 if (!isNonNegativeIntegerValue( 18161 ValExpr, *this, OMPC_num_tasks, 18162 /*StrictlyPositive=*/true, /*BuildCapture=*/true, 18163 DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt)) 18164 return nullptr; 18165 18166 return new (Context) OMPNumTasksClause(ValExpr, HelperValStmt, CaptureRegion, 18167 StartLoc, LParenLoc, EndLoc); 18168 } 18169 18170 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc, 18171 SourceLocation LParenLoc, 18172 SourceLocation EndLoc) { 18173 // OpenMP [2.13.2, critical construct, Description] 18174 // ... where hint-expression is an integer constant expression that evaluates 18175 // to a valid lock hint. 18176 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint); 18177 if (HintExpr.isInvalid()) 18178 return nullptr; 18179 return new (Context) 18180 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc); 18181 } 18182 18183 /// Tries to find omp_event_handle_t type. 18184 static bool findOMPEventHandleT(Sema &S, SourceLocation Loc, 18185 DSAStackTy *Stack) { 18186 QualType OMPEventHandleT = Stack->getOMPEventHandleT(); 18187 if (!OMPEventHandleT.isNull()) 18188 return true; 18189 IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_event_handle_t"); 18190 ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope()); 18191 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 18192 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_event_handle_t"; 18193 return false; 18194 } 18195 Stack->setOMPEventHandleT(PT.get()); 18196 return true; 18197 } 18198 18199 OMPClause *Sema::ActOnOpenMPDetachClause(Expr *Evt, SourceLocation StartLoc, 18200 SourceLocation LParenLoc, 18201 SourceLocation EndLoc) { 18202 if (!Evt->isValueDependent() && !Evt->isTypeDependent() && 18203 !Evt->isInstantiationDependent() && 18204 !Evt->containsUnexpandedParameterPack()) { 18205 if (!findOMPEventHandleT(*this, Evt->getExprLoc(), DSAStack)) 18206 return nullptr; 18207 // OpenMP 5.0, 2.10.1 task Construct. 18208 // event-handle is a variable of the omp_event_handle_t type. 18209 auto *Ref = dyn_cast<DeclRefExpr>(Evt->IgnoreParenImpCasts()); 18210 if (!Ref) { 18211 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 18212 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 18213 return nullptr; 18214 } 18215 auto *VD = dyn_cast_or_null<VarDecl>(Ref->getDecl()); 18216 if (!VD) { 18217 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 18218 << "omp_event_handle_t" << 0 << Evt->getSourceRange(); 18219 return nullptr; 18220 } 18221 if (!Context.hasSameUnqualifiedType(DSAStack->getOMPEventHandleT(), 18222 VD->getType()) || 18223 VD->getType().isConstant(Context)) { 18224 Diag(Evt->getExprLoc(), diag::err_omp_var_expected) 18225 << "omp_event_handle_t" << 1 << VD->getType() 18226 << Evt->getSourceRange(); 18227 return nullptr; 18228 } 18229 // OpenMP 5.0, 2.10.1 task Construct 18230 // [detach clause]... The event-handle will be considered as if it was 18231 // specified on a firstprivate clause. 18232 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, /*FromParent=*/false); 18233 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 18234 DVar.RefExpr) { 18235 Diag(Evt->getExprLoc(), diag::err_omp_wrong_dsa) 18236 << getOpenMPClauseName(DVar.CKind) 18237 << getOpenMPClauseName(OMPC_firstprivate); 18238 reportOriginalDsa(*this, DSAStack, VD, DVar); 18239 return nullptr; 18240 } 18241 } 18242 18243 return new (Context) OMPDetachClause(Evt, StartLoc, LParenLoc, EndLoc); 18244 } 18245 18246 OMPClause *Sema::ActOnOpenMPDistScheduleClause( 18247 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 18248 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 18249 SourceLocation EndLoc) { 18250 if (Kind == OMPC_DIST_SCHEDULE_unknown) { 18251 std::string Values; 18252 Values += "'"; 18253 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0); 18254 Values += "'"; 18255 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 18256 << Values << getOpenMPClauseName(OMPC_dist_schedule); 18257 return nullptr; 18258 } 18259 Expr *ValExpr = ChunkSize; 18260 Stmt *HelperValStmt = nullptr; 18261 if (ChunkSize) { 18262 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 18263 !ChunkSize->isInstantiationDependent() && 18264 !ChunkSize->containsUnexpandedParameterPack()) { 18265 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc(); 18266 ExprResult Val = 18267 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 18268 if (Val.isInvalid()) 18269 return nullptr; 18270 18271 ValExpr = Val.get(); 18272 18273 // OpenMP [2.7.1, Restrictions] 18274 // chunk_size must be a loop invariant integer expression with a positive 18275 // value. 18276 llvm::APSInt Result; 18277 if (ValExpr->isIntegerConstantExpr(Result, Context)) { 18278 if (Result.isSigned() && !Result.isStrictlyPositive()) { 18279 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 18280 << "dist_schedule" << ChunkSize->getSourceRange(); 18281 return nullptr; 18282 } 18283 } else if (getOpenMPCaptureRegionForClause( 18284 DSAStack->getCurrentDirective(), OMPC_dist_schedule, 18285 LangOpts.OpenMP) != OMPD_unknown && 18286 !CurContext->isDependentContext()) { 18287 ValExpr = MakeFullExpr(ValExpr).get(); 18288 llvm::MapVector<const Expr *, DeclRefExpr *> Captures; 18289 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get(); 18290 HelperValStmt = buildPreInits(Context, Captures); 18291 } 18292 } 18293 } 18294 18295 return new (Context) 18296 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, 18297 Kind, ValExpr, HelperValStmt); 18298 } 18299 18300 OMPClause *Sema::ActOnOpenMPDefaultmapClause( 18301 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind, 18302 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc, 18303 SourceLocation KindLoc, SourceLocation EndLoc) { 18304 if (getLangOpts().OpenMP < 50) { 18305 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || 18306 Kind != OMPC_DEFAULTMAP_scalar) { 18307 std::string Value; 18308 SourceLocation Loc; 18309 Value += "'"; 18310 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) { 18311 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 18312 OMPC_DEFAULTMAP_MODIFIER_tofrom); 18313 Loc = MLoc; 18314 } else { 18315 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap, 18316 OMPC_DEFAULTMAP_scalar); 18317 Loc = KindLoc; 18318 } 18319 Value += "'"; 18320 Diag(Loc, diag::err_omp_unexpected_clause_value) 18321 << Value << getOpenMPClauseName(OMPC_defaultmap); 18322 return nullptr; 18323 } 18324 } else { 18325 bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown); 18326 bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown) || 18327 (LangOpts.OpenMP >= 50 && KindLoc.isInvalid()); 18328 if (!isDefaultmapKind || !isDefaultmapModifier) { 18329 std::string ModifierValue = "'alloc', 'from', 'to', 'tofrom', " 18330 "'firstprivate', 'none', 'default'"; 18331 std::string KindValue = "'scalar', 'aggregate', 'pointer'"; 18332 if (!isDefaultmapKind && isDefaultmapModifier) { 18333 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 18334 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 18335 } else if (isDefaultmapKind && !isDefaultmapModifier) { 18336 Diag(MLoc, diag::err_omp_unexpected_clause_value) 18337 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 18338 } else { 18339 Diag(MLoc, diag::err_omp_unexpected_clause_value) 18340 << ModifierValue << getOpenMPClauseName(OMPC_defaultmap); 18341 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 18342 << KindValue << getOpenMPClauseName(OMPC_defaultmap); 18343 } 18344 return nullptr; 18345 } 18346 18347 // OpenMP [5.0, 2.12.5, Restrictions, p. 174] 18348 // At most one defaultmap clause for each category can appear on the 18349 // directive. 18350 if (DSAStack->checkDefaultmapCategory(Kind)) { 18351 Diag(StartLoc, diag::err_omp_one_defaultmap_each_category); 18352 return nullptr; 18353 } 18354 } 18355 if (Kind == OMPC_DEFAULTMAP_unknown) { 18356 // Variable category is not specified - mark all categories. 18357 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_aggregate, StartLoc); 18358 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_scalar, StartLoc); 18359 DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_pointer, StartLoc); 18360 } else { 18361 DSAStack->setDefaultDMAAttr(M, Kind, StartLoc); 18362 } 18363 18364 return new (Context) 18365 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M); 18366 } 18367 18368 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) { 18369 DeclContext *CurLexicalContext = getCurLexicalContext(); 18370 if (!CurLexicalContext->isFileContext() && 18371 !CurLexicalContext->isExternCContext() && 18372 !CurLexicalContext->isExternCXXContext() && 18373 !isa<CXXRecordDecl>(CurLexicalContext) && 18374 !isa<ClassTemplateDecl>(CurLexicalContext) && 18375 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) && 18376 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) { 18377 Diag(Loc, diag::err_omp_region_not_file_context); 18378 return false; 18379 } 18380 ++DeclareTargetNestingLevel; 18381 return true; 18382 } 18383 18384 void Sema::ActOnFinishOpenMPDeclareTargetDirective() { 18385 assert(DeclareTargetNestingLevel > 0 && 18386 "Unexpected ActOnFinishOpenMPDeclareTargetDirective"); 18387 --DeclareTargetNestingLevel; 18388 } 18389 18390 NamedDecl * 18391 Sema::lookupOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec, 18392 const DeclarationNameInfo &Id, 18393 NamedDeclSetType &SameDirectiveDecls) { 18394 LookupResult Lookup(*this, Id, LookupOrdinaryName); 18395 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 18396 18397 if (Lookup.isAmbiguous()) 18398 return nullptr; 18399 Lookup.suppressDiagnostics(); 18400 18401 if (!Lookup.isSingleResult()) { 18402 VarOrFuncDeclFilterCCC CCC(*this); 18403 if (TypoCorrection Corrected = 18404 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC, 18405 CTK_ErrorRecovery)) { 18406 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest) 18407 << Id.getName()); 18408 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl()); 18409 return nullptr; 18410 } 18411 18412 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName(); 18413 return nullptr; 18414 } 18415 18416 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>(); 18417 if (!isa<VarDecl>(ND) && !isa<FunctionDecl>(ND) && 18418 !isa<FunctionTemplateDecl>(ND)) { 18419 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName(); 18420 return nullptr; 18421 } 18422 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl()))) 18423 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName(); 18424 return ND; 18425 } 18426 18427 void Sema::ActOnOpenMPDeclareTargetName( 18428 NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT, 18429 OMPDeclareTargetDeclAttr::DevTypeTy DT) { 18430 assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) || 18431 isa<FunctionTemplateDecl>(ND)) && 18432 "Expected variable, function or function template."); 18433 18434 // Diagnose marking after use as it may lead to incorrect diagnosis and 18435 // codegen. 18436 if (LangOpts.OpenMP >= 50 && 18437 (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced())) 18438 Diag(Loc, diag::warn_omp_declare_target_after_first_use); 18439 18440 Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy = 18441 OMPDeclareTargetDeclAttr::getDeviceType(cast<ValueDecl>(ND)); 18442 if (DevTy.hasValue() && *DevTy != DT) { 18443 Diag(Loc, diag::err_omp_device_type_mismatch) 18444 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(DT) 18445 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(*DevTy); 18446 return; 18447 } 18448 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 18449 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(cast<ValueDecl>(ND)); 18450 if (!Res) { 18451 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT, DT, 18452 SourceRange(Loc, Loc)); 18453 ND->addAttr(A); 18454 if (ASTMutationListener *ML = Context.getASTMutationListener()) 18455 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); 18456 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc); 18457 } else if (*Res != MT) { 18458 Diag(Loc, diag::err_omp_declare_target_to_and_link) << ND; 18459 } 18460 } 18461 18462 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, 18463 Sema &SemaRef, Decl *D) { 18464 if (!D || !isa<VarDecl>(D)) 18465 return; 18466 auto *VD = cast<VarDecl>(D); 18467 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy = 18468 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 18469 if (SemaRef.LangOpts.OpenMP >= 50 && 18470 (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) || 18471 SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) && 18472 VD->hasGlobalStorage()) { 18473 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy = 18474 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); 18475 if (!MapTy || *MapTy != OMPDeclareTargetDeclAttr::MT_To) { 18476 // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions 18477 // If a lambda declaration and definition appears between a 18478 // declare target directive and the matching end declare target 18479 // directive, all variables that are captured by the lambda 18480 // expression must also appear in a to clause. 18481 SemaRef.Diag(VD->getLocation(), 18482 diag::err_omp_lambda_capture_in_declare_target_not_to); 18483 SemaRef.Diag(SL, diag::note_var_explicitly_captured_here) 18484 << VD << 0 << SR; 18485 return; 18486 } 18487 } 18488 if (MapTy.hasValue()) 18489 return; 18490 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context); 18491 SemaRef.Diag(SL, diag::note_used_here) << SR; 18492 } 18493 18494 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, 18495 Sema &SemaRef, DSAStackTy *Stack, 18496 ValueDecl *VD) { 18497 return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) || 18498 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(), 18499 /*FullCheck=*/false); 18500 } 18501 18502 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, 18503 SourceLocation IdLoc) { 18504 if (!D || D->isInvalidDecl()) 18505 return; 18506 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); 18507 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation(); 18508 if (auto *VD = dyn_cast<VarDecl>(D)) { 18509 // Only global variables can be marked as declare target. 18510 if (!VD->isFileVarDecl() && !VD->isStaticLocal() && 18511 !VD->isStaticDataMember()) 18512 return; 18513 // 2.10.6: threadprivate variable cannot appear in a declare target 18514 // directive. 18515 if (DSAStack->isThreadPrivate(VD)) { 18516 Diag(SL, diag::err_omp_threadprivate_in_target); 18517 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false)); 18518 return; 18519 } 18520 } 18521 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) 18522 D = FTD->getTemplatedDecl(); 18523 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 18524 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 18525 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD); 18526 if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { 18527 Diag(IdLoc, diag::err_omp_function_in_link_clause); 18528 Diag(FD->getLocation(), diag::note_defined_here) << FD; 18529 return; 18530 } 18531 } 18532 if (auto *VD = dyn_cast<ValueDecl>(D)) { 18533 // Problem if any with var declared with incomplete type will be reported 18534 // as normal, so no need to check it here. 18535 if ((E || !VD->getType()->isIncompleteType()) && 18536 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) 18537 return; 18538 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { 18539 // Checking declaration inside declare target region. 18540 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) || 18541 isa<FunctionTemplateDecl>(D)) { 18542 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit( 18543 Context, OMPDeclareTargetDeclAttr::MT_To, 18544 OMPDeclareTargetDeclAttr::DT_Any, SourceRange(IdLoc, IdLoc)); 18545 D->addAttr(A); 18546 if (ASTMutationListener *ML = Context.getASTMutationListener()) 18547 ML->DeclarationMarkedOpenMPDeclareTarget(D, A); 18548 } 18549 return; 18550 } 18551 } 18552 if (!E) 18553 return; 18554 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D); 18555 } 18556 18557 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList, 18558 CXXScopeSpec &MapperIdScopeSpec, 18559 DeclarationNameInfo &MapperId, 18560 const OMPVarListLocTy &Locs, 18561 ArrayRef<Expr *> UnresolvedMappers) { 18562 MappableVarListInfo MVLI(VarList); 18563 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc, 18564 MapperIdScopeSpec, MapperId, UnresolvedMappers); 18565 if (MVLI.ProcessedVarList.empty()) 18566 return nullptr; 18567 18568 return OMPToClause::Create( 18569 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 18570 MVLI.VarComponents, MVLI.UDMapperList, 18571 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 18572 } 18573 18574 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList, 18575 CXXScopeSpec &MapperIdScopeSpec, 18576 DeclarationNameInfo &MapperId, 18577 const OMPVarListLocTy &Locs, 18578 ArrayRef<Expr *> UnresolvedMappers) { 18579 MappableVarListInfo MVLI(VarList); 18580 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc, 18581 MapperIdScopeSpec, MapperId, UnresolvedMappers); 18582 if (MVLI.ProcessedVarList.empty()) 18583 return nullptr; 18584 18585 return OMPFromClause::Create( 18586 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations, 18587 MVLI.VarComponents, MVLI.UDMapperList, 18588 MapperIdScopeSpec.getWithLocInContext(Context), MapperId); 18589 } 18590 18591 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList, 18592 const OMPVarListLocTy &Locs) { 18593 MappableVarListInfo MVLI(VarList); 18594 SmallVector<Expr *, 8> PrivateCopies; 18595 SmallVector<Expr *, 8> Inits; 18596 18597 for (Expr *RefExpr : VarList) { 18598 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause."); 18599 SourceLocation ELoc; 18600 SourceRange ERange; 18601 Expr *SimpleRefExpr = RefExpr; 18602 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18603 if (Res.second) { 18604 // It will be analyzed later. 18605 MVLI.ProcessedVarList.push_back(RefExpr); 18606 PrivateCopies.push_back(nullptr); 18607 Inits.push_back(nullptr); 18608 } 18609 ValueDecl *D = Res.first; 18610 if (!D) 18611 continue; 18612 18613 QualType Type = D->getType(); 18614 Type = Type.getNonReferenceType().getUnqualifiedType(); 18615 18616 auto *VD = dyn_cast<VarDecl>(D); 18617 18618 // Item should be a pointer or reference to pointer. 18619 if (!Type->isPointerType()) { 18620 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer) 18621 << 0 << RefExpr->getSourceRange(); 18622 continue; 18623 } 18624 18625 // Build the private variable and the expression that refers to it. 18626 auto VDPrivate = 18627 buildVarDecl(*this, ELoc, Type, D->getName(), 18628 D->hasAttrs() ? &D->getAttrs() : nullptr, 18629 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr); 18630 if (VDPrivate->isInvalidDecl()) 18631 continue; 18632 18633 CurContext->addDecl(VDPrivate); 18634 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr( 18635 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc); 18636 18637 // Add temporary variable to initialize the private copy of the pointer. 18638 VarDecl *VDInit = 18639 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp"); 18640 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr( 18641 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc()); 18642 AddInitializerToDecl(VDPrivate, 18643 DefaultLvalueConversion(VDInitRefExpr).get(), 18644 /*DirectInit=*/false); 18645 18646 // If required, build a capture to implement the privatization initialized 18647 // with the current list item value. 18648 DeclRefExpr *Ref = nullptr; 18649 if (!VD) 18650 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 18651 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 18652 PrivateCopies.push_back(VDPrivateRefExpr); 18653 Inits.push_back(VDInitRefExpr); 18654 18655 // We need to add a data sharing attribute for this variable to make sure it 18656 // is correctly captured. A variable that shows up in a use_device_ptr has 18657 // similar properties of a first private variable. 18658 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 18659 18660 // Create a mappable component for the list item. List items in this clause 18661 // only need a component. 18662 MVLI.VarBaseDeclarations.push_back(D); 18663 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 18664 MVLI.VarComponents.back().push_back( 18665 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D)); 18666 } 18667 18668 if (MVLI.ProcessedVarList.empty()) 18669 return nullptr; 18670 18671 return OMPUseDevicePtrClause::Create( 18672 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits, 18673 MVLI.VarBaseDeclarations, MVLI.VarComponents); 18674 } 18675 18676 OMPClause *Sema::ActOnOpenMPUseDeviceAddrClause(ArrayRef<Expr *> VarList, 18677 const OMPVarListLocTy &Locs) { 18678 MappableVarListInfo MVLI(VarList); 18679 18680 for (Expr *RefExpr : VarList) { 18681 assert(RefExpr && "NULL expr in OpenMP use_device_addr clause."); 18682 SourceLocation ELoc; 18683 SourceRange ERange; 18684 Expr *SimpleRefExpr = RefExpr; 18685 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 18686 /*AllowArraySection=*/true); 18687 if (Res.second) { 18688 // It will be analyzed later. 18689 MVLI.ProcessedVarList.push_back(RefExpr); 18690 } 18691 ValueDecl *D = Res.first; 18692 if (!D) 18693 continue; 18694 auto *VD = dyn_cast<VarDecl>(D); 18695 18696 // If required, build a capture to implement the privatization initialized 18697 // with the current list item value. 18698 DeclRefExpr *Ref = nullptr; 18699 if (!VD) 18700 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true); 18701 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref); 18702 18703 // We need to add a data sharing attribute for this variable to make sure it 18704 // is correctly captured. A variable that shows up in a use_device_addr has 18705 // similar properties of a first private variable. 18706 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref); 18707 18708 // Create a mappable component for the list item. List items in this clause 18709 // only need a component. 18710 MVLI.VarBaseDeclarations.push_back(D); 18711 MVLI.VarComponents.emplace_back(); 18712 Expr *Component = SimpleRefExpr; 18713 if (VD && (isa<OMPArraySectionExpr>(RefExpr->IgnoreParenImpCasts()) || 18714 isa<ArraySubscriptExpr>(RefExpr->IgnoreParenImpCasts()))) 18715 Component = DefaultFunctionArrayLvalueConversion(SimpleRefExpr).get(); 18716 MVLI.VarComponents.back().push_back( 18717 OMPClauseMappableExprCommon::MappableComponent(Component, D)); 18718 } 18719 18720 if (MVLI.ProcessedVarList.empty()) 18721 return nullptr; 18722 18723 return OMPUseDeviceAddrClause::Create(Context, Locs, MVLI.ProcessedVarList, 18724 MVLI.VarBaseDeclarations, 18725 MVLI.VarComponents); 18726 } 18727 18728 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList, 18729 const OMPVarListLocTy &Locs) { 18730 MappableVarListInfo MVLI(VarList); 18731 for (Expr *RefExpr : VarList) { 18732 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause."); 18733 SourceLocation ELoc; 18734 SourceRange ERange; 18735 Expr *SimpleRefExpr = RefExpr; 18736 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18737 if (Res.second) { 18738 // It will be analyzed later. 18739 MVLI.ProcessedVarList.push_back(RefExpr); 18740 } 18741 ValueDecl *D = Res.first; 18742 if (!D) 18743 continue; 18744 18745 QualType Type = D->getType(); 18746 // item should be a pointer or array or reference to pointer or array 18747 if (!Type.getNonReferenceType()->isPointerType() && 18748 !Type.getNonReferenceType()->isArrayType()) { 18749 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr) 18750 << 0 << RefExpr->getSourceRange(); 18751 continue; 18752 } 18753 18754 // Check if the declaration in the clause does not show up in any data 18755 // sharing attribute. 18756 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false); 18757 if (isOpenMPPrivate(DVar.CKind)) { 18758 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa) 18759 << getOpenMPClauseName(DVar.CKind) 18760 << getOpenMPClauseName(OMPC_is_device_ptr) 18761 << getOpenMPDirectiveName(DSAStack->getCurrentDirective()); 18762 reportOriginalDsa(*this, DSAStack, D, DVar); 18763 continue; 18764 } 18765 18766 const Expr *ConflictExpr; 18767 if (DSAStack->checkMappableExprComponentListsForDecl( 18768 D, /*CurrentRegionOnly=*/true, 18769 [&ConflictExpr]( 18770 OMPClauseMappableExprCommon::MappableExprComponentListRef R, 18771 OpenMPClauseKind) -> bool { 18772 ConflictExpr = R.front().getAssociatedExpression(); 18773 return true; 18774 })) { 18775 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange(); 18776 Diag(ConflictExpr->getExprLoc(), diag::note_used_here) 18777 << ConflictExpr->getSourceRange(); 18778 continue; 18779 } 18780 18781 // Store the components in the stack so that they can be used to check 18782 // against other clauses later on. 18783 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D); 18784 DSAStack->addMappableExpressionComponents( 18785 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr); 18786 18787 // Record the expression we've just processed. 18788 MVLI.ProcessedVarList.push_back(SimpleRefExpr); 18789 18790 // Create a mappable component for the list item. List items in this clause 18791 // only need a component. We use a null declaration to signal fields in 18792 // 'this'. 18793 assert((isa<DeclRefExpr>(SimpleRefExpr) || 18794 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) && 18795 "Unexpected device pointer expression!"); 18796 MVLI.VarBaseDeclarations.push_back( 18797 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr); 18798 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1); 18799 MVLI.VarComponents.back().push_back(MC); 18800 } 18801 18802 if (MVLI.ProcessedVarList.empty()) 18803 return nullptr; 18804 18805 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList, 18806 MVLI.VarBaseDeclarations, 18807 MVLI.VarComponents); 18808 } 18809 18810 OMPClause *Sema::ActOnOpenMPAllocateClause( 18811 Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc, 18812 SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 18813 if (Allocator) { 18814 // OpenMP [2.11.4 allocate Clause, Description] 18815 // allocator is an expression of omp_allocator_handle_t type. 18816 if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack)) 18817 return nullptr; 18818 18819 ExprResult AllocatorRes = DefaultLvalueConversion(Allocator); 18820 if (AllocatorRes.isInvalid()) 18821 return nullptr; 18822 AllocatorRes = PerformImplicitConversion(AllocatorRes.get(), 18823 DSAStack->getOMPAllocatorHandleT(), 18824 Sema::AA_Initializing, 18825 /*AllowExplicit=*/true); 18826 if (AllocatorRes.isInvalid()) 18827 return nullptr; 18828 Allocator = AllocatorRes.get(); 18829 } else { 18830 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions. 18831 // allocate clauses that appear on a target construct or on constructs in a 18832 // target region must specify an allocator expression unless a requires 18833 // directive with the dynamic_allocators clause is present in the same 18834 // compilation unit. 18835 if (LangOpts.OpenMPIsDevice && 18836 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()) 18837 targetDiag(StartLoc, diag::err_expected_allocator_expression); 18838 } 18839 // Analyze and build list of variables. 18840 SmallVector<Expr *, 8> Vars; 18841 for (Expr *RefExpr : VarList) { 18842 assert(RefExpr && "NULL expr in OpenMP private clause."); 18843 SourceLocation ELoc; 18844 SourceRange ERange; 18845 Expr *SimpleRefExpr = RefExpr; 18846 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18847 if (Res.second) { 18848 // It will be analyzed later. 18849 Vars.push_back(RefExpr); 18850 } 18851 ValueDecl *D = Res.first; 18852 if (!D) 18853 continue; 18854 18855 auto *VD = dyn_cast<VarDecl>(D); 18856 DeclRefExpr *Ref = nullptr; 18857 if (!VD && !CurContext->isDependentContext()) 18858 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false); 18859 Vars.push_back((VD || CurContext->isDependentContext()) 18860 ? RefExpr->IgnoreParens() 18861 : Ref); 18862 } 18863 18864 if (Vars.empty()) 18865 return nullptr; 18866 18867 if (Allocator) 18868 DSAStack->addInnerAllocatorExpr(Allocator); 18869 return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator, 18870 ColonLoc, EndLoc, Vars); 18871 } 18872 18873 OMPClause *Sema::ActOnOpenMPNontemporalClause(ArrayRef<Expr *> VarList, 18874 SourceLocation StartLoc, 18875 SourceLocation LParenLoc, 18876 SourceLocation EndLoc) { 18877 SmallVector<Expr *, 8> Vars; 18878 for (Expr *RefExpr : VarList) { 18879 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 18880 SourceLocation ELoc; 18881 SourceRange ERange; 18882 Expr *SimpleRefExpr = RefExpr; 18883 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); 18884 if (Res.second) 18885 // It will be analyzed later. 18886 Vars.push_back(RefExpr); 18887 ValueDecl *D = Res.first; 18888 if (!D) 18889 continue; 18890 18891 // OpenMP 5.0, 2.9.3.1 simd Construct, Restrictions. 18892 // A list-item cannot appear in more than one nontemporal clause. 18893 if (const Expr *PrevRef = 18894 DSAStack->addUniqueNontemporal(D, SimpleRefExpr)) { 18895 Diag(ELoc, diag::err_omp_used_in_clause_twice) 18896 << 0 << getOpenMPClauseName(OMPC_nontemporal) << ERange; 18897 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 18898 << getOpenMPClauseName(OMPC_nontemporal); 18899 continue; 18900 } 18901 18902 Vars.push_back(RefExpr); 18903 } 18904 18905 if (Vars.empty()) 18906 return nullptr; 18907 18908 return OMPNontemporalClause::Create(Context, StartLoc, LParenLoc, EndLoc, 18909 Vars); 18910 } 18911 18912 OMPClause *Sema::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList, 18913 SourceLocation StartLoc, 18914 SourceLocation LParenLoc, 18915 SourceLocation EndLoc) { 18916 SmallVector<Expr *, 8> Vars; 18917 for (Expr *RefExpr : VarList) { 18918 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 18919 SourceLocation ELoc; 18920 SourceRange ERange; 18921 Expr *SimpleRefExpr = RefExpr; 18922 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 18923 /*AllowArraySection=*/true); 18924 if (Res.second) 18925 // It will be analyzed later. 18926 Vars.push_back(RefExpr); 18927 ValueDecl *D = Res.first; 18928 if (!D) 18929 continue; 18930 18931 const DSAStackTy::DSAVarData DVar = 18932 DSAStack->getTopDSA(D, /*FromParent=*/true); 18933 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 18934 // A list item that appears in the inclusive or exclusive clause must appear 18935 // in a reduction clause with the inscan modifier on the enclosing 18936 // worksharing-loop, worksharing-loop SIMD, or simd construct. 18937 if (DVar.CKind != OMPC_reduction || 18938 DVar.Modifier != OMPC_REDUCTION_inscan) 18939 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 18940 << RefExpr->getSourceRange(); 18941 18942 if (DSAStack->getParentDirective() != OMPD_unknown) 18943 DSAStack->markDeclAsUsedInScanDirective(D); 18944 Vars.push_back(RefExpr); 18945 } 18946 18947 if (Vars.empty()) 18948 return nullptr; 18949 18950 return OMPInclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 18951 } 18952 18953 OMPClause *Sema::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList, 18954 SourceLocation StartLoc, 18955 SourceLocation LParenLoc, 18956 SourceLocation EndLoc) { 18957 SmallVector<Expr *, 8> Vars; 18958 for (Expr *RefExpr : VarList) { 18959 assert(RefExpr && "NULL expr in OpenMP nontemporal clause."); 18960 SourceLocation ELoc; 18961 SourceRange ERange; 18962 Expr *SimpleRefExpr = RefExpr; 18963 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange, 18964 /*AllowArraySection=*/true); 18965 if (Res.second) 18966 // It will be analyzed later. 18967 Vars.push_back(RefExpr); 18968 ValueDecl *D = Res.first; 18969 if (!D) 18970 continue; 18971 18972 OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective(); 18973 DSAStackTy::DSAVarData DVar; 18974 if (ParentDirective != OMPD_unknown) 18975 DVar = DSAStack->getTopDSA(D, /*FromParent=*/true); 18976 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions. 18977 // A list item that appears in the inclusive or exclusive clause must appear 18978 // in a reduction clause with the inscan modifier on the enclosing 18979 // worksharing-loop, worksharing-loop SIMD, or simd construct. 18980 if (ParentDirective == OMPD_unknown || DVar.CKind != OMPC_reduction || 18981 DVar.Modifier != OMPC_REDUCTION_inscan) { 18982 Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction) 18983 << RefExpr->getSourceRange(); 18984 } else { 18985 DSAStack->markDeclAsUsedInScanDirective(D); 18986 } 18987 Vars.push_back(RefExpr); 18988 } 18989 18990 if (Vars.empty()) 18991 return nullptr; 18992 18993 return OMPExclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 18994 } 18995 18996 /// Tries to find omp_alloctrait_t type. 18997 static bool findOMPAlloctraitT(Sema &S, SourceLocation Loc, DSAStackTy *Stack) { 18998 QualType OMPAlloctraitT = Stack->getOMPAlloctraitT(); 18999 if (!OMPAlloctraitT.isNull()) 19000 return true; 19001 IdentifierInfo &II = S.PP.getIdentifierTable().get("omp_alloctrait_t"); 19002 ParsedType PT = S.getTypeName(II, Loc, S.getCurScope()); 19003 if (!PT.getAsOpaquePtr() || PT.get().isNull()) { 19004 S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_alloctrait_t"; 19005 return false; 19006 } 19007 Stack->setOMPAlloctraitT(PT.get()); 19008 return true; 19009 } 19010 19011 OMPClause *Sema::ActOnOpenMPUsesAllocatorClause( 19012 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc, 19013 ArrayRef<UsesAllocatorsData> Data) { 19014 // OpenMP [2.12.5, target Construct] 19015 // allocator is an identifier of omp_allocator_handle_t type. 19016 if (!findOMPAllocatorHandleT(*this, StartLoc, DSAStack)) 19017 return nullptr; 19018 // OpenMP [2.12.5, target Construct] 19019 // allocator-traits-array is an identifier of const omp_alloctrait_t * type. 19020 if (llvm::any_of( 19021 Data, 19022 [](const UsesAllocatorsData &D) { return D.AllocatorTraits; }) && 19023 !findOMPAlloctraitT(*this, StartLoc, DSAStack)) 19024 return nullptr; 19025 llvm::SmallSet<CanonicalDeclPtr<Decl>, 4> PredefinedAllocators; 19026 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) { 19027 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I); 19028 StringRef Allocator = 19029 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind); 19030 DeclarationName AllocatorName = &Context.Idents.get(Allocator); 19031 PredefinedAllocators.insert(LookupSingleName( 19032 TUScope, AllocatorName, StartLoc, Sema::LookupAnyName)); 19033 } 19034 19035 SmallVector<OMPUsesAllocatorsClause::Data, 4> NewData; 19036 for (const UsesAllocatorsData &D : Data) { 19037 Expr *AllocatorExpr = nullptr; 19038 // Check allocator expression. 19039 if (D.Allocator->isTypeDependent()) { 19040 AllocatorExpr = D.Allocator; 19041 } else { 19042 // Traits were specified - need to assign new allocator to the specified 19043 // allocator, so it must be an lvalue. 19044 AllocatorExpr = D.Allocator->IgnoreParenImpCasts(); 19045 auto *DRE = dyn_cast<DeclRefExpr>(AllocatorExpr); 19046 bool IsPredefinedAllocator = false; 19047 if (DRE) 19048 IsPredefinedAllocator = PredefinedAllocators.count(DRE->getDecl()); 19049 if (!DRE || 19050 !(Context.hasSameUnqualifiedType( 19051 AllocatorExpr->getType(), DSAStack->getOMPAllocatorHandleT()) || 19052 Context.typesAreCompatible(AllocatorExpr->getType(), 19053 DSAStack->getOMPAllocatorHandleT(), 19054 /*CompareUnqualified=*/true)) || 19055 (!IsPredefinedAllocator && 19056 (AllocatorExpr->getType().isConstant(Context) || 19057 !AllocatorExpr->isLValue()))) { 19058 Diag(D.Allocator->getExprLoc(), diag::err_omp_var_expected) 19059 << "omp_allocator_handle_t" << (DRE ? 1 : 0) 19060 << AllocatorExpr->getType() << D.Allocator->getSourceRange(); 19061 continue; 19062 } 19063 // OpenMP [2.12.5, target Construct] 19064 // Predefined allocators appearing in a uses_allocators clause cannot have 19065 // traits specified. 19066 if (IsPredefinedAllocator && D.AllocatorTraits) { 19067 Diag(D.AllocatorTraits->getExprLoc(), 19068 diag::err_omp_predefined_allocator_with_traits) 19069 << D.AllocatorTraits->getSourceRange(); 19070 Diag(D.Allocator->getExprLoc(), diag::note_omp_predefined_allocator) 19071 << cast<NamedDecl>(DRE->getDecl())->getName() 19072 << D.Allocator->getSourceRange(); 19073 continue; 19074 } 19075 // OpenMP [2.12.5, target Construct] 19076 // Non-predefined allocators appearing in a uses_allocators clause must 19077 // have traits specified. 19078 if (!IsPredefinedAllocator && !D.AllocatorTraits) { 19079 Diag(D.Allocator->getExprLoc(), 19080 diag::err_omp_nonpredefined_allocator_without_traits); 19081 continue; 19082 } 19083 // No allocator traits - just convert it to rvalue. 19084 if (!D.AllocatorTraits) 19085 AllocatorExpr = DefaultLvalueConversion(AllocatorExpr).get(); 19086 DSAStack->addUsesAllocatorsDecl( 19087 DRE->getDecl(), 19088 IsPredefinedAllocator 19089 ? DSAStackTy::UsesAllocatorsDeclKind::PredefinedAllocator 19090 : DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator); 19091 } 19092 Expr *AllocatorTraitsExpr = nullptr; 19093 if (D.AllocatorTraits) { 19094 if (D.AllocatorTraits->isTypeDependent()) { 19095 AllocatorTraitsExpr = D.AllocatorTraits; 19096 } else { 19097 // OpenMP [2.12.5, target Construct] 19098 // Arrays that contain allocator traits that appear in a uses_allocators 19099 // clause must be constant arrays, have constant values and be defined 19100 // in the same scope as the construct in which the clause appears. 19101 AllocatorTraitsExpr = D.AllocatorTraits->IgnoreParenImpCasts(); 19102 // Check that traits expr is a constant array. 19103 QualType TraitTy; 19104 if (const ArrayType *Ty = 19105 AllocatorTraitsExpr->getType()->getAsArrayTypeUnsafe()) 19106 if (const auto *ConstArrayTy = dyn_cast<ConstantArrayType>(Ty)) 19107 TraitTy = ConstArrayTy->getElementType(); 19108 if (TraitTy.isNull() || 19109 !(Context.hasSameUnqualifiedType(TraitTy, 19110 DSAStack->getOMPAlloctraitT()) || 19111 Context.typesAreCompatible(TraitTy, DSAStack->getOMPAlloctraitT(), 19112 /*CompareUnqualified=*/true))) { 19113 Diag(D.AllocatorTraits->getExprLoc(), 19114 diag::err_omp_expected_array_alloctraits) 19115 << AllocatorTraitsExpr->getType(); 19116 continue; 19117 } 19118 // Do not map by default allocator traits if it is a standalone 19119 // variable. 19120 if (auto *DRE = dyn_cast<DeclRefExpr>(AllocatorTraitsExpr)) 19121 DSAStack->addUsesAllocatorsDecl( 19122 DRE->getDecl(), 19123 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait); 19124 } 19125 } 19126 OMPUsesAllocatorsClause::Data &NewD = NewData.emplace_back(); 19127 NewD.Allocator = AllocatorExpr; 19128 NewD.AllocatorTraits = AllocatorTraitsExpr; 19129 NewD.LParenLoc = D.LParenLoc; 19130 NewD.RParenLoc = D.RParenLoc; 19131 } 19132 return OMPUsesAllocatorsClause::Create(Context, StartLoc, LParenLoc, EndLoc, 19133 NewData); 19134 } 19135 19136 OMPClause *Sema::ActOnOpenMPAffinityClause( 19137 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 19138 SourceLocation EndLoc, Expr *Modifier, ArrayRef<Expr *> Locators) { 19139 SmallVector<Expr *, 8> Vars; 19140 for (Expr *RefExpr : Locators) { 19141 assert(RefExpr && "NULL expr in OpenMP shared clause."); 19142 if (isa<DependentScopeDeclRefExpr>(RefExpr) || RefExpr->isTypeDependent()) { 19143 // It will be analyzed later. 19144 Vars.push_back(RefExpr); 19145 continue; 19146 } 19147 19148 SourceLocation ELoc = RefExpr->getExprLoc(); 19149 Expr *SimpleExpr = RefExpr->IgnoreParenImpCasts(); 19150 19151 if (!SimpleExpr->isLValue()) { 19152 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 19153 << 1 << 0 << RefExpr->getSourceRange(); 19154 continue; 19155 } 19156 19157 ExprResult Res; 19158 { 19159 Sema::TentativeAnalysisScope Trap(*this); 19160 Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, SimpleExpr); 19161 } 19162 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) && 19163 !isa<OMPArrayShapingExpr>(SimpleExpr)) { 19164 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item) 19165 << 1 << 0 << RefExpr->getSourceRange(); 19166 continue; 19167 } 19168 Vars.push_back(SimpleExpr); 19169 } 19170 19171 return OMPAffinityClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 19172 EndLoc, Modifier, Vars); 19173 } 19174